4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lnet/lnet/lib-move.c
34 * Data movement routines
37 #define DEBUG_SUBSYSTEM S_LNET
39 #include <linux/pagemap.h>
41 #include <lnet/lib-lnet.h>
42 #include <linux/nsproxy.h>
43 #include <net/net_namespace.h>
45 extern unsigned int lnet_current_net_count;
47 static int local_nid_dist_zero = 1;
48 module_param(local_nid_dist_zero, int, 0444);
49 MODULE_PARM_DESC(local_nid_dist_zero, "Reserved");
51 struct lnet_send_data {
52 struct lnet_ni *sd_best_ni;
53 struct lnet_peer_ni *sd_best_lpni;
54 struct lnet_peer_ni *sd_final_dst_lpni;
55 struct lnet_peer *sd_peer;
56 struct lnet_peer *sd_gw_peer;
57 struct lnet_peer_ni *sd_gw_lpni;
58 struct lnet_peer_net *sd_peer_net;
59 struct lnet_msg *sd_msg;
60 lnet_nid_t sd_dst_nid;
61 lnet_nid_t sd_src_nid;
62 lnet_nid_t sd_rtr_nid;
68 static inline struct lnet_comm_count *
69 get_stats_counts(struct lnet_element_stats *stats,
70 enum lnet_stats_type stats_type)
73 case LNET_STATS_TYPE_SEND:
74 return &stats->el_send_stats;
75 case LNET_STATS_TYPE_RECV:
76 return &stats->el_recv_stats;
77 case LNET_STATS_TYPE_DROP:
78 return &stats->el_drop_stats;
80 CERROR("Unknown stats type\n");
86 void lnet_incr_stats(struct lnet_element_stats *stats,
87 enum lnet_msg_type msg_type,
88 enum lnet_stats_type stats_type)
90 struct lnet_comm_count *counts = get_stats_counts(stats, stats_type);
96 atomic_inc(&counts->co_ack_count);
99 atomic_inc(&counts->co_put_count);
102 atomic_inc(&counts->co_get_count);
105 atomic_inc(&counts->co_reply_count);
108 atomic_inc(&counts->co_hello_count);
111 CERROR("There is a BUG in the code. Unknown message type\n");
116 __u32 lnet_sum_stats(struct lnet_element_stats *stats,
117 enum lnet_stats_type stats_type)
119 struct lnet_comm_count *counts = get_stats_counts(stats, stats_type);
123 return (atomic_read(&counts->co_ack_count) +
124 atomic_read(&counts->co_put_count) +
125 atomic_read(&counts->co_get_count) +
126 atomic_read(&counts->co_reply_count) +
127 atomic_read(&counts->co_hello_count));
130 static inline void assign_stats(struct lnet_ioctl_comm_count *msg_stats,
131 struct lnet_comm_count *counts)
133 msg_stats->ico_get_count = atomic_read(&counts->co_get_count);
134 msg_stats->ico_put_count = atomic_read(&counts->co_put_count);
135 msg_stats->ico_reply_count = atomic_read(&counts->co_reply_count);
136 msg_stats->ico_ack_count = atomic_read(&counts->co_ack_count);
137 msg_stats->ico_hello_count = atomic_read(&counts->co_hello_count);
140 void lnet_usr_translate_stats(struct lnet_ioctl_element_msg_stats *msg_stats,
141 struct lnet_element_stats *stats)
143 struct lnet_comm_count *counts;
148 counts = get_stats_counts(stats, LNET_STATS_TYPE_SEND);
151 assign_stats(&msg_stats->im_send_stats, counts);
153 counts = get_stats_counts(stats, LNET_STATS_TYPE_RECV);
156 assign_stats(&msg_stats->im_recv_stats, counts);
158 counts = get_stats_counts(stats, LNET_STATS_TYPE_DROP);
161 assign_stats(&msg_stats->im_drop_stats, counts);
165 lnet_fail_nid(lnet_nid_t nid, unsigned int threshold)
167 struct lnet_test_peer *tp;
168 struct list_head *el;
169 struct list_head *next;
170 struct list_head cull;
172 /* NB: use lnet_net_lock(0) to serialize operations on test peers */
173 if (threshold != 0) {
174 /* Adding a new entry */
175 LIBCFS_ALLOC(tp, sizeof(*tp));
180 tp->tp_threshold = threshold;
183 list_add_tail(&tp->tp_list, &the_lnet.ln_test_peers);
188 /* removing entries */
189 INIT_LIST_HEAD(&cull);
193 list_for_each_safe(el, next, &the_lnet.ln_test_peers) {
194 tp = list_entry(el, struct lnet_test_peer, tp_list);
196 if (tp->tp_threshold == 0 || /* needs culling anyway */
197 nid == LNET_NID_ANY || /* removing all entries */
198 tp->tp_nid == nid) { /* matched this one */
199 list_del(&tp->tp_list);
200 list_add(&tp->tp_list, &cull);
206 while (!list_empty(&cull)) {
207 tp = list_entry(cull.next, struct lnet_test_peer, tp_list);
209 list_del(&tp->tp_list);
210 LIBCFS_FREE(tp, sizeof(*tp));
216 fail_peer (lnet_nid_t nid, int outgoing)
218 struct lnet_test_peer *tp;
219 struct list_head *el;
220 struct list_head *next;
221 struct list_head cull;
224 INIT_LIST_HEAD(&cull);
226 /* NB: use lnet_net_lock(0) to serialize operations on test peers */
229 list_for_each_safe(el, next, &the_lnet.ln_test_peers) {
230 tp = list_entry(el, struct lnet_test_peer, tp_list);
232 if (tp->tp_threshold == 0) {
235 /* only cull zombies on outgoing tests,
236 * since we may be at interrupt priority on
237 * incoming messages. */
238 list_del(&tp->tp_list);
239 list_add(&tp->tp_list, &cull);
244 if (tp->tp_nid == LNET_NID_ANY || /* fail every peer */
245 nid == tp->tp_nid) { /* fail this peer */
248 if (tp->tp_threshold != LNET_MD_THRESH_INF) {
251 tp->tp_threshold == 0) {
253 list_del(&tp->tp_list);
254 list_add(&tp->tp_list, &cull);
263 while (!list_empty(&cull)) {
264 tp = list_entry(cull.next, struct lnet_test_peer, tp_list);
265 list_del(&tp->tp_list);
267 LIBCFS_FREE(tp, sizeof(*tp));
274 lnet_iov_nob(unsigned int niov, struct kvec *iov)
276 unsigned int nob = 0;
278 LASSERT(niov == 0 || iov != NULL);
280 nob += (iov++)->iov_len;
284 EXPORT_SYMBOL(lnet_iov_nob);
287 lnet_copy_iov2iov(unsigned int ndiov, struct kvec *diov, unsigned int doffset,
288 unsigned int nsiov, struct kvec *siov, unsigned int soffset,
291 /* NB diov, siov are READ-ONLY */
292 unsigned int this_nob;
297 /* skip complete frags before 'doffset' */
299 while (doffset >= diov->iov_len) {
300 doffset -= diov->iov_len;
306 /* skip complete frags before 'soffset' */
308 while (soffset >= siov->iov_len) {
309 soffset -= siov->iov_len;
318 this_nob = MIN(diov->iov_len - doffset,
319 siov->iov_len - soffset);
320 this_nob = MIN(this_nob, nob);
322 memcpy((char *)diov->iov_base + doffset,
323 (char *)siov->iov_base + soffset, this_nob);
326 if (diov->iov_len > doffset + this_nob) {
334 if (siov->iov_len > soffset + this_nob) {
343 EXPORT_SYMBOL(lnet_copy_iov2iov);
346 lnet_extract_iov(int dst_niov, struct kvec *dst,
347 int src_niov, struct kvec *src,
348 unsigned int offset, unsigned int len)
350 /* Initialise 'dst' to the subset of 'src' starting at 'offset',
351 * for exactly 'len' bytes, and return the number of entries.
352 * NB not destructive to 'src' */
353 unsigned int frag_len;
356 if (len == 0) /* no data => */
357 return (0); /* no frags */
359 LASSERT(src_niov > 0);
360 while (offset >= src->iov_len) { /* skip initial frags */
361 offset -= src->iov_len;
364 LASSERT(src_niov > 0);
369 LASSERT(src_niov > 0);
370 LASSERT((int)niov <= dst_niov);
372 frag_len = src->iov_len - offset;
373 dst->iov_base = ((char *)src->iov_base) + offset;
375 if (len <= frag_len) {
380 dst->iov_len = frag_len;
390 EXPORT_SYMBOL(lnet_extract_iov);
394 lnet_kiov_nob(unsigned int niov, lnet_kiov_t *kiov)
396 unsigned int nob = 0;
398 LASSERT(niov == 0 || kiov != NULL);
400 nob += (kiov++)->kiov_len;
404 EXPORT_SYMBOL(lnet_kiov_nob);
407 lnet_copy_kiov2kiov(unsigned int ndiov, lnet_kiov_t *diov, unsigned int doffset,
408 unsigned int nsiov, lnet_kiov_t *siov, unsigned int soffset,
411 /* NB diov, siov are READ-ONLY */
412 unsigned int this_nob;
419 LASSERT (!in_interrupt ());
422 while (doffset >= diov->kiov_len) {
423 doffset -= diov->kiov_len;
430 while (soffset >= siov->kiov_len) {
431 soffset -= siov->kiov_len;
440 this_nob = MIN(diov->kiov_len - doffset,
441 siov->kiov_len - soffset);
442 this_nob = MIN(this_nob, nob);
445 daddr = ((char *)kmap(diov->kiov_page)) +
446 diov->kiov_offset + doffset;
448 saddr = ((char *)kmap(siov->kiov_page)) +
449 siov->kiov_offset + soffset;
451 /* Vanishing risk of kmap deadlock when mapping 2 pages.
452 * However in practice at least one of the kiovs will be mapped
453 * kernel pages and the map/unmap will be NOOPs */
455 memcpy (daddr, saddr, this_nob);
458 if (diov->kiov_len > doffset + this_nob) {
462 kunmap(diov->kiov_page);
469 if (siov->kiov_len > soffset + this_nob) {
473 kunmap(siov->kiov_page);
482 kunmap(diov->kiov_page);
484 kunmap(siov->kiov_page);
486 EXPORT_SYMBOL(lnet_copy_kiov2kiov);
489 lnet_copy_kiov2iov (unsigned int niov, struct kvec *iov, unsigned int iovoffset,
490 unsigned int nkiov, lnet_kiov_t *kiov, unsigned int kiovoffset,
493 /* NB iov, kiov are READ-ONLY */
494 unsigned int this_nob;
500 LASSERT (!in_interrupt ());
503 while (iovoffset >= iov->iov_len) {
504 iovoffset -= iov->iov_len;
511 while (kiovoffset >= kiov->kiov_len) {
512 kiovoffset -= kiov->kiov_len;
521 this_nob = MIN(iov->iov_len - iovoffset,
522 kiov->kiov_len - kiovoffset);
523 this_nob = MIN(this_nob, nob);
526 addr = ((char *)kmap(kiov->kiov_page)) +
527 kiov->kiov_offset + kiovoffset;
529 memcpy((char *)iov->iov_base + iovoffset, addr, this_nob);
532 if (iov->iov_len > iovoffset + this_nob) {
533 iovoffset += this_nob;
540 if (kiov->kiov_len > kiovoffset + this_nob) {
542 kiovoffset += this_nob;
544 kunmap(kiov->kiov_page);
554 kunmap(kiov->kiov_page);
556 EXPORT_SYMBOL(lnet_copy_kiov2iov);
559 lnet_copy_iov2kiov(unsigned int nkiov, lnet_kiov_t *kiov, unsigned int kiovoffset,
560 unsigned int niov, struct kvec *iov, unsigned int iovoffset,
563 /* NB kiov, iov are READ-ONLY */
564 unsigned int this_nob;
570 LASSERT (!in_interrupt ());
573 while (kiovoffset >= kiov->kiov_len) {
574 kiovoffset -= kiov->kiov_len;
581 while (iovoffset >= iov->iov_len) {
582 iovoffset -= iov->iov_len;
591 this_nob = MIN(kiov->kiov_len - kiovoffset,
592 iov->iov_len - iovoffset);
593 this_nob = MIN(this_nob, nob);
596 addr = ((char *)kmap(kiov->kiov_page)) +
597 kiov->kiov_offset + kiovoffset;
599 memcpy (addr, (char *)iov->iov_base + iovoffset, this_nob);
602 if (kiov->kiov_len > kiovoffset + this_nob) {
604 kiovoffset += this_nob;
606 kunmap(kiov->kiov_page);
613 if (iov->iov_len > iovoffset + this_nob) {
614 iovoffset += this_nob;
623 kunmap(kiov->kiov_page);
625 EXPORT_SYMBOL(lnet_copy_iov2kiov);
628 lnet_extract_kiov(int dst_niov, lnet_kiov_t *dst,
629 int src_niov, lnet_kiov_t *src,
630 unsigned int offset, unsigned int len)
632 /* Initialise 'dst' to the subset of 'src' starting at 'offset',
633 * for exactly 'len' bytes, and return the number of entries.
634 * NB not destructive to 'src' */
635 unsigned int frag_len;
638 if (len == 0) /* no data => */
639 return (0); /* no frags */
641 LASSERT(src_niov > 0);
642 while (offset >= src->kiov_len) { /* skip initial frags */
643 offset -= src->kiov_len;
646 LASSERT(src_niov > 0);
651 LASSERT(src_niov > 0);
652 LASSERT((int)niov <= dst_niov);
654 frag_len = src->kiov_len - offset;
655 dst->kiov_page = src->kiov_page;
656 dst->kiov_offset = src->kiov_offset + offset;
658 if (len <= frag_len) {
660 LASSERT(dst->kiov_offset + dst->kiov_len <= PAGE_SIZE);
664 dst->kiov_len = frag_len;
665 LASSERT(dst->kiov_offset + dst->kiov_len <= PAGE_SIZE);
675 EXPORT_SYMBOL(lnet_extract_kiov);
678 lnet_ni_recv(struct lnet_ni *ni, void *private, struct lnet_msg *msg,
679 int delayed, unsigned int offset, unsigned int mlen,
682 unsigned int niov = 0;
683 struct kvec *iov = NULL;
684 lnet_kiov_t *kiov = NULL;
687 LASSERT (!in_interrupt ());
688 LASSERT (mlen == 0 || msg != NULL);
691 LASSERT(msg->msg_receiving);
692 LASSERT(!msg->msg_sending);
693 LASSERT(rlen == msg->msg_len);
694 LASSERT(mlen <= msg->msg_len);
695 LASSERT(msg->msg_offset == offset);
696 LASSERT(msg->msg_wanted == mlen);
698 msg->msg_receiving = 0;
701 niov = msg->msg_niov;
703 kiov = msg->msg_kiov;
706 LASSERT ((iov == NULL) != (kiov == NULL));
710 rc = (ni->ni_net->net_lnd->lnd_recv)(ni, private, msg, delayed,
711 niov, iov, kiov, offset, mlen,
714 lnet_finalize(msg, rc);
718 lnet_setpayloadbuffer(struct lnet_msg *msg)
720 struct lnet_libmd *md = msg->msg_md;
722 LASSERT(msg->msg_len > 0);
723 LASSERT(!msg->msg_routing);
725 LASSERT(msg->msg_niov == 0);
726 LASSERT(msg->msg_iov == NULL);
727 LASSERT(msg->msg_kiov == NULL);
729 msg->msg_niov = md->md_niov;
730 if ((md->md_options & LNET_MD_KIOV) != 0)
731 msg->msg_kiov = md->md_iov.kiov;
733 msg->msg_iov = md->md_iov.iov;
737 lnet_prep_send(struct lnet_msg *msg, int type, struct lnet_process_id target,
738 unsigned int offset, unsigned int len)
740 msg->msg_type = type;
741 msg->msg_target = target;
743 msg->msg_offset = offset;
746 lnet_setpayloadbuffer(msg);
748 memset (&msg->msg_hdr, 0, sizeof (msg->msg_hdr));
749 msg->msg_hdr.type = cpu_to_le32(type);
750 /* dest_nid will be overwritten by lnet_select_pathway() */
751 msg->msg_hdr.dest_nid = cpu_to_le64(target.nid);
752 msg->msg_hdr.dest_pid = cpu_to_le32(target.pid);
753 /* src_nid will be set later */
754 msg->msg_hdr.src_pid = cpu_to_le32(the_lnet.ln_pid);
755 msg->msg_hdr.payload_length = cpu_to_le32(len);
759 lnet_ni_send(struct lnet_ni *ni, struct lnet_msg *msg)
761 void *priv = msg->msg_private;
764 LASSERT (!in_interrupt ());
765 LASSERT (LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND ||
766 (msg->msg_txcredit && msg->msg_peertxcredit));
768 rc = (ni->ni_net->net_lnd->lnd_send)(ni, priv, msg);
770 msg->msg_no_resend = true;
771 lnet_finalize(msg, rc);
776 lnet_ni_eager_recv(struct lnet_ni *ni, struct lnet_msg *msg)
780 LASSERT(!msg->msg_sending);
781 LASSERT(msg->msg_receiving);
782 LASSERT(!msg->msg_rx_ready_delay);
783 LASSERT(ni->ni_net->net_lnd->lnd_eager_recv != NULL);
785 msg->msg_rx_ready_delay = 1;
786 rc = (ni->ni_net->net_lnd->lnd_eager_recv)(ni, msg->msg_private, msg,
789 CERROR("recv from %s / send to %s aborted: "
790 "eager_recv failed %d\n",
791 libcfs_nid2str(msg->msg_rxpeer->lpni_nid),
792 libcfs_id2str(msg->msg_target), rc);
793 LASSERT(rc < 0); /* required by my callers */
800 lnet_is_peer_deadline_passed(struct lnet_peer_ni *lpni, time64_t now)
804 deadline = lpni->lpni_last_alive +
805 lpni->lpni_net->net_tunables.lct_peer_timeout;
808 * assume peer_ni is alive as long as we're within the configured
817 /* NB: returns 1 when alive, 0 when dead, negative when error;
818 * may drop the lnet_net_lock */
820 lnet_peer_alive_locked(struct lnet_ni *ni, struct lnet_peer_ni *lpni,
821 struct lnet_msg *msg)
823 time64_t now = ktime_get_seconds();
825 if (!lnet_peer_aliveness_enabled(lpni))
829 * If we're resending a message, let's attempt to send it even if
830 * the peer is down to fulfill our resend quota on the message
832 if (msg->msg_retry_count > 0)
835 /* try and send recovery messages irregardless */
836 if (msg->msg_recovery)
839 /* always send any responses */
840 if (msg->msg_type == LNET_MSG_ACK ||
841 msg->msg_type == LNET_MSG_REPLY)
844 if (!lnet_is_peer_deadline_passed(lpni, now))
847 return lnet_is_peer_ni_alive(lpni);
851 * \param msg The message to be sent.
852 * \param do_send True if lnet_ni_send() should be called in this function.
853 * lnet_send() is going to lnet_net_unlock immediately after this, so
854 * it sets do_send FALSE and I don't do the unlock/send/lock bit.
856 * \retval LNET_CREDIT_OK If \a msg sent or OK to send.
857 * \retval LNET_CREDIT_WAIT If \a msg blocked for credit.
858 * \retval -EHOSTUNREACH If the next hop of the message appears dead.
859 * \retval -ECANCELED If the MD of the message has been unlinked.
862 lnet_post_send_locked(struct lnet_msg *msg, int do_send)
864 struct lnet_peer_ni *lp = msg->msg_txpeer;
865 struct lnet_ni *ni = msg->msg_txni;
866 int cpt = msg->msg_tx_cpt;
867 struct lnet_tx_queue *tq = ni->ni_tx_queues[cpt];
869 /* non-lnet_send() callers have checked before */
870 LASSERT(!do_send || msg->msg_tx_delayed);
871 LASSERT(!msg->msg_receiving);
872 LASSERT(msg->msg_tx_committed);
873 /* can't get here if we're sending to the loopback interface */
874 LASSERT(lp->lpni_nid != the_lnet.ln_loni->ni_nid);
876 /* NB 'lp' is always the next hop */
877 if ((msg->msg_target.pid & LNET_PID_USERFLAG) == 0 &&
878 lnet_peer_alive_locked(ni, lp, msg) == 0) {
879 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
880 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length +=
882 lnet_net_unlock(cpt);
884 lnet_incr_stats(&msg->msg_txpeer->lpni_stats,
886 LNET_STATS_TYPE_DROP);
888 lnet_incr_stats(&msg->msg_txni->ni_stats,
890 LNET_STATS_TYPE_DROP);
892 CNETERR("Dropping message for %s: peer not alive\n",
893 libcfs_id2str(msg->msg_target));
894 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_DROPPED;
896 lnet_finalize(msg, -EHOSTUNREACH);
899 return -EHOSTUNREACH;
902 if (msg->msg_md != NULL &&
903 (msg->msg_md->md_flags & LNET_MD_FLAG_ABORTED) != 0) {
904 lnet_net_unlock(cpt);
906 CNETERR("Aborting message for %s: LNetM[DE]Unlink() already "
907 "called on the MD/ME.\n",
908 libcfs_id2str(msg->msg_target));
910 msg->msg_no_resend = true;
911 CDEBUG(D_NET, "msg %p to %s canceled and will not be resent\n",
912 msg, libcfs_id2str(msg->msg_target));
913 lnet_finalize(msg, -ECANCELED);
920 if (!msg->msg_peertxcredit) {
921 spin_lock(&lp->lpni_lock);
922 LASSERT((lp->lpni_txcredits < 0) ==
923 !list_empty(&lp->lpni_txq));
925 msg->msg_peertxcredit = 1;
926 lp->lpni_txqnob += msg->msg_len + sizeof(struct lnet_hdr);
927 lp->lpni_txcredits--;
929 if (lp->lpni_txcredits < lp->lpni_mintxcredits)
930 lp->lpni_mintxcredits = lp->lpni_txcredits;
932 if (lp->lpni_txcredits < 0) {
933 msg->msg_tx_delayed = 1;
934 list_add_tail(&msg->msg_list, &lp->lpni_txq);
935 spin_unlock(&lp->lpni_lock);
936 return LNET_CREDIT_WAIT;
938 spin_unlock(&lp->lpni_lock);
941 if (!msg->msg_txcredit) {
942 LASSERT((tq->tq_credits < 0) ==
943 !list_empty(&tq->tq_delayed));
945 msg->msg_txcredit = 1;
947 atomic_dec(&ni->ni_tx_credits);
949 if (tq->tq_credits < tq->tq_credits_min)
950 tq->tq_credits_min = tq->tq_credits;
952 if (tq->tq_credits < 0) {
953 msg->msg_tx_delayed = 1;
954 list_add_tail(&msg->msg_list, &tq->tq_delayed);
955 return LNET_CREDIT_WAIT;
959 /* unset the tx_delay flag as we're going to send it now */
960 msg->msg_tx_delayed = 0;
963 lnet_net_unlock(cpt);
964 lnet_ni_send(ni, msg);
967 return LNET_CREDIT_OK;
971 static struct lnet_rtrbufpool *
972 lnet_msg2bufpool(struct lnet_msg *msg)
974 struct lnet_rtrbufpool *rbp;
977 LASSERT(msg->msg_rx_committed);
979 cpt = msg->msg_rx_cpt;
980 rbp = &the_lnet.ln_rtrpools[cpt][0];
982 LASSERT(msg->msg_len <= LNET_MTU);
983 while (msg->msg_len > (unsigned int)rbp->rbp_npages * PAGE_SIZE) {
985 LASSERT(rbp < &the_lnet.ln_rtrpools[cpt][LNET_NRBPOOLS]);
992 lnet_post_routed_recv_locked(struct lnet_msg *msg, int do_recv)
994 /* lnet_parse is going to lnet_net_unlock immediately after this, so it
995 * sets do_recv FALSE and I don't do the unlock/send/lock bit.
996 * I return LNET_CREDIT_WAIT if msg blocked and LNET_CREDIT_OK if
997 * received or OK to receive */
998 struct lnet_peer_ni *lpni = msg->msg_rxpeer;
999 struct lnet_peer *lp;
1000 struct lnet_rtrbufpool *rbp;
1001 struct lnet_rtrbuf *rb;
1003 LASSERT(msg->msg_iov == NULL);
1004 LASSERT(msg->msg_kiov == NULL);
1005 LASSERT(msg->msg_niov == 0);
1006 LASSERT(msg->msg_routing);
1007 LASSERT(msg->msg_receiving);
1008 LASSERT(!msg->msg_sending);
1009 LASSERT(lpni->lpni_peer_net);
1010 LASSERT(lpni->lpni_peer_net->lpn_peer);
1012 lp = lpni->lpni_peer_net->lpn_peer;
1014 /* non-lnet_parse callers only receive delayed messages */
1015 LASSERT(!do_recv || msg->msg_rx_delayed);
1017 if (!msg->msg_peerrtrcredit) {
1018 /* lpni_lock protects the credit manipulation */
1019 spin_lock(&lpni->lpni_lock);
1020 /* lp_lock protects the lp_rtrq */
1021 spin_lock(&lp->lp_lock);
1023 msg->msg_peerrtrcredit = 1;
1024 lpni->lpni_rtrcredits--;
1025 if (lpni->lpni_rtrcredits < lpni->lpni_minrtrcredits)
1026 lpni->lpni_minrtrcredits = lpni->lpni_rtrcredits;
1028 if (lpni->lpni_rtrcredits < 0) {
1029 /* must have checked eager_recv before here */
1030 LASSERT(msg->msg_rx_ready_delay);
1031 msg->msg_rx_delayed = 1;
1032 list_add_tail(&msg->msg_list, &lp->lp_rtrq);
1033 spin_unlock(&lp->lp_lock);
1034 spin_unlock(&lpni->lpni_lock);
1035 return LNET_CREDIT_WAIT;
1037 spin_unlock(&lp->lp_lock);
1038 spin_unlock(&lpni->lpni_lock);
1041 rbp = lnet_msg2bufpool(msg);
1043 if (!msg->msg_rtrcredit) {
1044 msg->msg_rtrcredit = 1;
1046 if (rbp->rbp_credits < rbp->rbp_mincredits)
1047 rbp->rbp_mincredits = rbp->rbp_credits;
1049 if (rbp->rbp_credits < 0) {
1050 /* must have checked eager_recv before here */
1051 LASSERT(msg->msg_rx_ready_delay);
1052 msg->msg_rx_delayed = 1;
1053 list_add_tail(&msg->msg_list, &rbp->rbp_msgs);
1054 return LNET_CREDIT_WAIT;
1058 LASSERT(!list_empty(&rbp->rbp_bufs));
1059 rb = list_entry(rbp->rbp_bufs.next, struct lnet_rtrbuf, rb_list);
1060 list_del(&rb->rb_list);
1062 msg->msg_niov = rbp->rbp_npages;
1063 msg->msg_kiov = &rb->rb_kiov[0];
1065 /* unset the msg-rx_delayed flag since we're receiving the message */
1066 msg->msg_rx_delayed = 0;
1069 int cpt = msg->msg_rx_cpt;
1071 lnet_net_unlock(cpt);
1072 lnet_ni_recv(msg->msg_rxni, msg->msg_private, msg, 1,
1073 0, msg->msg_len, msg->msg_len);
1076 return LNET_CREDIT_OK;
1080 lnet_return_tx_credits_locked(struct lnet_msg *msg)
1082 struct lnet_peer_ni *txpeer = msg->msg_txpeer;
1083 struct lnet_ni *txni = msg->msg_txni;
1084 struct lnet_msg *msg2;
1086 if (msg->msg_txcredit) {
1087 struct lnet_ni *ni = msg->msg_txni;
1088 struct lnet_tx_queue *tq = ni->ni_tx_queues[msg->msg_tx_cpt];
1090 /* give back NI txcredits */
1091 msg->msg_txcredit = 0;
1093 LASSERT((tq->tq_credits < 0) ==
1094 !list_empty(&tq->tq_delayed));
1097 atomic_inc(&ni->ni_tx_credits);
1098 if (tq->tq_credits <= 0) {
1099 msg2 = list_entry(tq->tq_delayed.next,
1100 struct lnet_msg, msg_list);
1101 list_del(&msg2->msg_list);
1103 LASSERT(msg2->msg_txni == ni);
1104 LASSERT(msg2->msg_tx_delayed);
1105 LASSERT(msg2->msg_tx_cpt == msg->msg_tx_cpt);
1107 (void) lnet_post_send_locked(msg2, 1);
1111 if (msg->msg_peertxcredit) {
1112 /* give back peer txcredits */
1113 msg->msg_peertxcredit = 0;
1115 spin_lock(&txpeer->lpni_lock);
1116 LASSERT((txpeer->lpni_txcredits < 0) ==
1117 !list_empty(&txpeer->lpni_txq));
1119 txpeer->lpni_txqnob -= msg->msg_len + sizeof(struct lnet_hdr);
1120 LASSERT(txpeer->lpni_txqnob >= 0);
1122 txpeer->lpni_txcredits++;
1123 if (txpeer->lpni_txcredits <= 0) {
1126 msg2 = list_entry(txpeer->lpni_txq.next,
1127 struct lnet_msg, msg_list);
1128 list_del(&msg2->msg_list);
1129 spin_unlock(&txpeer->lpni_lock);
1131 LASSERT(msg2->msg_txpeer == txpeer);
1132 LASSERT(msg2->msg_tx_delayed);
1134 msg2_cpt = msg2->msg_tx_cpt;
1137 * The msg_cpt can be different from the msg2_cpt
1138 * so we need to make sure we lock the correct cpt
1140 * Once we call lnet_post_send_locked() it is no
1141 * longer safe to access msg2, since it could've
1142 * been freed by lnet_finalize(), but we still
1143 * need to relock the correct cpt, so we cache the
1144 * msg2_cpt for the purpose of the check that
1145 * follows the call to lnet_pose_send_locked().
1147 if (msg2_cpt != msg->msg_tx_cpt) {
1148 lnet_net_unlock(msg->msg_tx_cpt);
1149 lnet_net_lock(msg2_cpt);
1151 (void) lnet_post_send_locked(msg2, 1);
1152 if (msg2_cpt != msg->msg_tx_cpt) {
1153 lnet_net_unlock(msg2_cpt);
1154 lnet_net_lock(msg->msg_tx_cpt);
1157 spin_unlock(&txpeer->lpni_lock);
1162 msg->msg_txni = NULL;
1163 lnet_ni_decref_locked(txni, msg->msg_tx_cpt);
1166 if (txpeer != NULL) {
1167 msg->msg_txpeer = NULL;
1168 lnet_peer_ni_decref_locked(txpeer);
1173 lnet_schedule_blocked_locked(struct lnet_rtrbufpool *rbp)
1175 struct lnet_msg *msg;
1177 if (list_empty(&rbp->rbp_msgs))
1179 msg = list_entry(rbp->rbp_msgs.next,
1180 struct lnet_msg, msg_list);
1181 list_del(&msg->msg_list);
1183 (void)lnet_post_routed_recv_locked(msg, 1);
1187 lnet_drop_routed_msgs_locked(struct list_head *list, int cpt)
1189 struct lnet_msg *msg;
1190 struct lnet_msg *tmp;
1192 lnet_net_unlock(cpt);
1194 list_for_each_entry_safe(msg, tmp, list, msg_list) {
1195 lnet_ni_recv(msg->msg_rxni, msg->msg_private, NULL,
1196 0, 0, 0, msg->msg_hdr.payload_length);
1197 list_del_init(&msg->msg_list);
1198 msg->msg_no_resend = true;
1199 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_ERROR;
1200 lnet_finalize(msg, -ECANCELED);
1207 lnet_return_rx_credits_locked(struct lnet_msg *msg)
1209 struct lnet_peer_ni *rxpeerni = msg->msg_rxpeer;
1210 struct lnet_peer *lp;
1211 struct lnet_ni *rxni = msg->msg_rxni;
1212 struct lnet_msg *msg2;
1214 if (msg->msg_rtrcredit) {
1215 /* give back global router credits */
1216 struct lnet_rtrbuf *rb;
1217 struct lnet_rtrbufpool *rbp;
1219 /* NB If a msg ever blocks for a buffer in rbp_msgs, it stays
1220 * there until it gets one allocated, or aborts the wait
1222 LASSERT(msg->msg_kiov != NULL);
1224 rb = list_entry(msg->msg_kiov, struct lnet_rtrbuf, rb_kiov[0]);
1227 msg->msg_kiov = NULL;
1228 msg->msg_rtrcredit = 0;
1230 LASSERT(rbp == lnet_msg2bufpool(msg));
1232 LASSERT((rbp->rbp_credits > 0) ==
1233 !list_empty(&rbp->rbp_bufs));
1235 /* If routing is now turned off, we just drop this buffer and
1236 * don't bother trying to return credits. */
1237 if (!the_lnet.ln_routing) {
1238 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1242 /* It is possible that a user has lowered the desired number of
1243 * buffers in this pool. Make sure we never put back
1244 * more buffers than the stated number. */
1245 if (unlikely(rbp->rbp_credits >= rbp->rbp_req_nbuffers)) {
1246 /* Discard this buffer so we don't have too
1248 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1249 rbp->rbp_nbuffers--;
1251 list_add(&rb->rb_list, &rbp->rbp_bufs);
1253 if (rbp->rbp_credits <= 0)
1254 lnet_schedule_blocked_locked(rbp);
1259 if (msg->msg_peerrtrcredit) {
1261 LASSERT(rxpeerni->lpni_peer_net);
1262 LASSERT(rxpeerni->lpni_peer_net->lpn_peer);
1264 lp = rxpeerni->lpni_peer_net->lpn_peer;
1266 /* give back peer router credits */
1267 msg->msg_peerrtrcredit = 0;
1269 spin_lock(&rxpeerni->lpni_lock);
1270 spin_lock(&lp->lp_lock);
1272 rxpeerni->lpni_rtrcredits++;
1274 /* drop all messages which are queued to be routed on that
1276 if (!the_lnet.ln_routing) {
1277 struct list_head drop;
1278 INIT_LIST_HEAD(&drop);
1279 list_splice_init(&lp->lp_rtrq, &drop);
1280 spin_unlock(&lp->lp_lock);
1281 spin_unlock(&rxpeerni->lpni_lock);
1282 lnet_drop_routed_msgs_locked(&drop, msg->msg_rx_cpt);
1283 } else if (!list_empty(&lp->lp_rtrq)) {
1286 msg2 = list_entry(lp->lp_rtrq.next,
1287 struct lnet_msg, msg_list);
1288 list_del(&msg2->msg_list);
1289 msg2_cpt = msg2->msg_rx_cpt;
1290 spin_unlock(&lp->lp_lock);
1291 spin_unlock(&rxpeerni->lpni_lock);
1293 * messages on the lp_rtrq can be from any NID in
1294 * the peer, which means they might have different
1295 * cpts. We need to make sure we lock the right
1298 if (msg2_cpt != msg->msg_rx_cpt) {
1299 lnet_net_unlock(msg->msg_rx_cpt);
1300 lnet_net_lock(msg2_cpt);
1302 (void) lnet_post_routed_recv_locked(msg2, 1);
1303 if (msg2_cpt != msg->msg_rx_cpt) {
1304 lnet_net_unlock(msg2_cpt);
1305 lnet_net_lock(msg->msg_rx_cpt);
1308 spin_unlock(&lp->lp_lock);
1309 spin_unlock(&rxpeerni->lpni_lock);
1313 msg->msg_rxni = NULL;
1314 lnet_ni_decref_locked(rxni, msg->msg_rx_cpt);
1316 if (rxpeerni != NULL) {
1317 msg->msg_rxpeer = NULL;
1318 lnet_peer_ni_decref_locked(rxpeerni);
1323 lnet_compare_peers(struct lnet_peer_ni *p1, struct lnet_peer_ni *p2)
1325 if (p1->lpni_txqnob < p2->lpni_txqnob)
1328 if (p1->lpni_txqnob > p2->lpni_txqnob)
1331 if (p1->lpni_txcredits > p2->lpni_txcredits)
1334 if (p1->lpni_txcredits < p2->lpni_txcredits)
1340 static struct lnet_peer_ni *
1341 lnet_select_peer_ni(struct lnet_ni *best_ni, lnet_nid_t dst_nid,
1342 struct lnet_peer *peer,
1343 struct lnet_peer_net *peer_net)
1346 * Look at the peer NIs for the destination peer that connect
1347 * to the chosen net. If a peer_ni is preferred when using the
1348 * best_ni to communicate, we use that one. If there is no
1349 * preferred peer_ni, or there are multiple preferred peer_ni,
1350 * the available transmit credits are used. If the transmit
1351 * credits are equal, we round-robin over the peer_ni.
1353 struct lnet_peer_ni *lpni = NULL;
1354 struct lnet_peer_ni *best_lpni = NULL;
1355 int best_lpni_credits = INT_MIN;
1356 bool preferred = false;
1358 int best_lpni_healthv = 0;
1361 while ((lpni = lnet_get_next_peer_ni_locked(peer, peer_net, lpni))) {
1363 * if the best_ni we've chosen aleady has this lpni
1364 * preferred, then let's use it
1367 ni_is_pref = lnet_peer_is_pref_nid_locked(lpni,
1369 CDEBUG(D_NET, "%s ni_is_pref = %d\n",
1370 libcfs_nid2str(best_ni->ni_nid), ni_is_pref);
1375 lpni_healthv = atomic_read(&lpni->lpni_healthv);
1378 CDEBUG(D_NET, "%s c:[%d, %d], s:[%d, %d]\n",
1379 libcfs_nid2str(lpni->lpni_nid),
1380 lpni->lpni_txcredits, best_lpni_credits,
1381 lpni->lpni_seq, best_lpni->lpni_seq);
1383 /* pick the healthiest peer ni */
1384 if (lpni_healthv < best_lpni_healthv) {
1386 } else if (lpni_healthv > best_lpni_healthv) {
1387 best_lpni_healthv = lpni_healthv;
1388 /* if this is a preferred peer use it */
1389 } else if (!preferred && ni_is_pref) {
1391 } else if (preferred && !ni_is_pref) {
1393 * this is not the preferred peer so let's ignore
1397 } else if (lpni->lpni_txcredits < best_lpni_credits) {
1399 * We already have a peer that has more credits
1400 * available than this one. No need to consider
1401 * this peer further.
1404 } else if (lpni->lpni_txcredits == best_lpni_credits) {
1406 * The best peer found so far and the current peer
1407 * have the same number of available credits let's
1408 * make sure to select between them using Round
1412 if (best_lpni->lpni_seq <= lpni->lpni_seq)
1418 best_lpni_credits = lpni->lpni_txcredits;
1421 /* if we still can't find a peer ni then we can't reach it */
1423 __u32 net_id = (peer_net) ? peer_net->lpn_net_id :
1424 LNET_NIDNET(dst_nid);
1425 CDEBUG(D_NET, "no peer_ni found on peer net %s\n",
1426 libcfs_net2str(net_id));
1430 CDEBUG(D_NET, "sd_best_lpni = %s\n",
1431 libcfs_nid2str(best_lpni->lpni_nid));
1437 * Prerequisite: the best_ni should already be set in the sd
1439 static inline struct lnet_peer_ni *
1440 lnet_find_best_lpni_on_net(struct lnet_send_data *sd, struct lnet_peer *peer,
1443 struct lnet_peer_net *peer_net;
1446 * The gateway is Multi-Rail capable so now we must select the
1449 peer_net = lnet_peer_get_net_locked(peer, net_id);
1452 CERROR("gateway peer %s has no NI on net %s\n",
1453 libcfs_nid2str(peer->lp_primary_nid),
1454 libcfs_net2str(net_id));
1458 return lnet_select_peer_ni(sd->sd_best_ni, sd->sd_dst_nid,
1463 lnet_compare_routes(struct lnet_route *r1, struct lnet_route *r2,
1464 struct lnet_peer_ni **best_lpni)
1466 int r1_hops = (r1->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r1->lr_hops;
1467 int r2_hops = (r2->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r2->lr_hops;
1468 struct lnet_peer *lp1 = r1->lr_gateway;
1469 struct lnet_peer *lp2 = r2->lr_gateway;
1470 struct lnet_peer_ni *lpni1;
1471 struct lnet_peer_ni *lpni2;
1472 struct lnet_send_data sd;
1475 sd.sd_best_ni = NULL;
1476 sd.sd_dst_nid = LNET_NID_ANY;
1477 lpni1 = lnet_find_best_lpni_on_net(&sd, lp1, r1->lr_lnet);
1478 lpni2 = lnet_find_best_lpni_on_net(&sd, lp2, r2->lr_lnet);
1479 LASSERT(lpni1 && lpni2);
1481 if (r1->lr_priority < r2->lr_priority) {
1486 if (r1->lr_priority > r2->lr_priority) {
1491 if (r1_hops < r2_hops) {
1496 if (r1_hops > r2_hops) {
1501 rc = lnet_compare_peers(lpni1, lpni2);
1505 } else if (rc == -1) {
1510 if (r1->lr_seq - r2->lr_seq <= 0) {
1519 static struct lnet_route *
1520 lnet_find_route_locked(struct lnet_net *net, __u32 remote_net,
1521 lnet_nid_t rtr_nid, struct lnet_route **prev_route,
1522 struct lnet_peer_ni **gwni)
1524 struct lnet_peer_ni *best_gw_ni = NULL;
1525 struct lnet_route *best_route;
1526 struct lnet_route *last_route;
1527 struct lnet_remotenet *rnet;
1528 struct lnet_peer *lp_best;
1529 struct lnet_route *route;
1530 struct lnet_peer *lp;
1533 /* If @rtr_nid is not LNET_NID_ANY, return the gateway with
1534 * rtr_nid nid, otherwise find the best gateway I can use */
1536 rnet = lnet_find_rnet_locked(remote_net);
1541 best_route = last_route = NULL;
1542 list_for_each_entry(route, &rnet->lrn_routes, lr_list) {
1543 lp = route->lr_gateway;
1545 if (!lnet_is_route_alive(route))
1548 if (lp_best == NULL) {
1549 best_route = last_route = route;
1553 /* no protection on below fields, but it's harmless */
1554 if (last_route->lr_seq - route->lr_seq < 0)
1557 rc = lnet_compare_routes(route, best_route, &best_gw_ni);
1565 *prev_route = last_route;
1571 static struct lnet_ni *
1572 lnet_get_best_ni(struct lnet_net *local_net, struct lnet_ni *best_ni,
1573 struct lnet_peer *peer, struct lnet_peer_net *peer_net,
1576 struct lnet_ni *ni = NULL;
1577 unsigned int shortest_distance;
1582 * If there is no peer_ni that we can send to on this network,
1583 * then there is no point in looking for a new best_ni here.
1585 if (!lnet_get_next_peer_ni_locked(peer, peer_net, NULL))
1588 if (best_ni == NULL) {
1589 shortest_distance = UINT_MAX;
1590 best_credits = INT_MIN;
1593 shortest_distance = cfs_cpt_distance(lnet_cpt_table(), md_cpt,
1594 best_ni->ni_dev_cpt);
1595 best_credits = atomic_read(&best_ni->ni_tx_credits);
1596 best_healthv = atomic_read(&best_ni->ni_healthv);
1599 while ((ni = lnet_get_next_ni_locked(local_net, ni))) {
1600 unsigned int distance;
1605 ni_credits = atomic_read(&ni->ni_tx_credits);
1606 ni_healthv = atomic_read(&ni->ni_healthv);
1607 ni_fatal = atomic_read(&ni->ni_fatal_error_on);
1610 * calculate the distance from the CPT on which
1611 * the message memory is allocated to the CPT of
1612 * the NI's physical device
1614 distance = cfs_cpt_distance(lnet_cpt_table(),
1618 CDEBUG(D_NET, "compare ni %s [c:%d, d:%d, s:%d] with best_ni %s [c:%d, d:%d, s:%d]\n",
1619 libcfs_nid2str(ni->ni_nid), ni_credits, distance,
1620 ni->ni_seq, (best_ni) ? libcfs_nid2str(best_ni->ni_nid)
1621 : "not seleced", best_credits, shortest_distance,
1622 (best_ni) ? best_ni->ni_seq : 0);
1625 * All distances smaller than the NUMA range
1626 * are treated equally.
1628 if (distance < lnet_numa_range)
1629 distance = lnet_numa_range;
1632 * Select on health, shorter distance, available
1633 * credits, then round-robin.
1637 } else if (ni_healthv < best_healthv) {
1639 } else if (ni_healthv > best_healthv) {
1640 best_healthv = ni_healthv;
1642 * If we're going to prefer this ni because it's
1643 * the healthiest, then we should set the
1644 * shortest_distance in the algorithm in case
1645 * there are multiple NIs with the same health but
1646 * different distances.
1648 if (distance < shortest_distance)
1649 shortest_distance = distance;
1650 } else if (distance > shortest_distance) {
1652 } else if (distance < shortest_distance) {
1653 shortest_distance = distance;
1654 } else if (ni_credits < best_credits) {
1656 } else if (ni_credits == best_credits) {
1657 if (best_ni && best_ni->ni_seq <= ni->ni_seq)
1661 best_credits = ni_credits;
1664 CDEBUG(D_NET, "selected best_ni %s\n",
1665 (best_ni) ? libcfs_nid2str(best_ni->ni_nid) : "no selection");
1671 * Traffic to the LNET_RESERVED_PORTAL may not trigger peer discovery,
1672 * because such traffic is required to perform discovery. We therefore
1673 * exclude all GET and PUT on that portal. We also exclude all ACK and
1674 * REPLY traffic, but that is because the portal is not tracked in the
1675 * message structure for these message types. We could restrict this
1676 * further by also checking for LNET_PROTO_PING_MATCHBITS.
1679 lnet_msg_discovery(struct lnet_msg *msg)
1681 if (msg->msg_type == LNET_MSG_PUT) {
1682 if (msg->msg_hdr.msg.put.ptl_index != LNET_RESERVED_PORTAL)
1684 } else if (msg->msg_type == LNET_MSG_GET) {
1685 if (msg->msg_hdr.msg.get.ptl_index != LNET_RESERVED_PORTAL)
1691 #define SRC_SPEC 0x0001
1692 #define SRC_ANY 0x0002
1693 #define LOCAL_DST 0x0004
1694 #define REMOTE_DST 0x0008
1695 #define MR_DST 0x0010
1696 #define NMR_DST 0x0020
1697 #define SND_RESP 0x0040
1699 /* The following to defines are used for return codes */
1700 #define REPEAT_SEND 0x1000
1701 #define PASS_THROUGH 0x2000
1703 /* The different cases lnet_select pathway needs to handle */
1704 #define SRC_SPEC_LOCAL_MR_DST (SRC_SPEC | LOCAL_DST | MR_DST)
1705 #define SRC_SPEC_ROUTER_MR_DST (SRC_SPEC | REMOTE_DST | MR_DST)
1706 #define SRC_SPEC_LOCAL_NMR_DST (SRC_SPEC | LOCAL_DST | NMR_DST)
1707 #define SRC_SPEC_ROUTER_NMR_DST (SRC_SPEC | REMOTE_DST | NMR_DST)
1708 #define SRC_ANY_LOCAL_MR_DST (SRC_ANY | LOCAL_DST | MR_DST)
1709 #define SRC_ANY_ROUTER_MR_DST (SRC_ANY | REMOTE_DST | MR_DST)
1710 #define SRC_ANY_LOCAL_NMR_DST (SRC_ANY | LOCAL_DST | NMR_DST)
1711 #define SRC_ANY_ROUTER_NMR_DST (SRC_ANY | REMOTE_DST | NMR_DST)
1714 lnet_handle_lo_send(struct lnet_send_data *sd)
1716 struct lnet_msg *msg = sd->sd_msg;
1717 int cpt = sd->sd_cpt;
1719 /* No send credit hassles with LOLND */
1720 lnet_ni_addref_locked(the_lnet.ln_loni, cpt);
1721 msg->msg_hdr.dest_nid = cpu_to_le64(the_lnet.ln_loni->ni_nid);
1722 if (!msg->msg_routing)
1723 msg->msg_hdr.src_nid =
1724 cpu_to_le64(the_lnet.ln_loni->ni_nid);
1725 msg->msg_target.nid = the_lnet.ln_loni->ni_nid;
1726 lnet_msg_commit(msg, cpt);
1727 msg->msg_txni = the_lnet.ln_loni;
1729 return LNET_CREDIT_OK;
1733 lnet_handle_send(struct lnet_send_data *sd)
1735 struct lnet_ni *best_ni = sd->sd_best_ni;
1736 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
1737 struct lnet_peer_ni *final_dst_lpni = sd->sd_final_dst_lpni;
1738 struct lnet_msg *msg = sd->sd_msg;
1740 __u32 send_case = sd->sd_send_case;
1742 __u32 routing = send_case & REMOTE_DST;
1743 struct lnet_rsp_tracker *rspt;
1746 * Increment sequence number of the selected peer so that we
1747 * pick the next one in Round Robin.
1749 best_lpni->lpni_seq++;
1752 * grab a reference on the peer_ni so it sticks around even if
1753 * we need to drop and relock the lnet_net_lock below.
1755 lnet_peer_ni_addref_locked(best_lpni);
1758 * Use lnet_cpt_of_nid() to determine the CPT used to commit the
1759 * message. This ensures that we get a CPT that is correct for
1760 * the NI when the NI has been restricted to a subset of all CPTs.
1761 * If the selected CPT differs from the one currently locked, we
1762 * must unlock and relock the lnet_net_lock(), and then check whether
1763 * the configuration has changed. We don't have a hold on the best_ni
1764 * yet, and it may have vanished.
1766 cpt2 = lnet_cpt_of_nid_locked(best_lpni->lpni_nid, best_ni);
1767 if (sd->sd_cpt != cpt2) {
1768 __u32 seq = lnet_get_dlc_seq_locked();
1769 lnet_net_unlock(sd->sd_cpt);
1771 lnet_net_lock(sd->sd_cpt);
1772 if (seq != lnet_get_dlc_seq_locked()) {
1773 lnet_peer_ni_decref_locked(best_lpni);
1779 * store the best_lpni in the message right away to avoid having
1780 * to do the same operation under different conditions
1782 msg->msg_txpeer = best_lpni;
1783 msg->msg_txni = best_ni;
1786 * grab a reference for the best_ni since now it's in use in this
1787 * send. The reference will be dropped in lnet_finalize()
1789 lnet_ni_addref_locked(msg->msg_txni, sd->sd_cpt);
1792 * Always set the target.nid to the best peer picked. Either the
1793 * NID will be one of the peer NIDs selected, or the same NID as
1794 * what was originally set in the target or it will be the NID of
1795 * a router if this message should be routed
1797 msg->msg_target.nid = msg->msg_txpeer->lpni_nid;
1800 * lnet_msg_commit assigns the correct cpt to the message, which
1801 * is used to decrement the correct refcount on the ni when it's
1802 * time to return the credits
1804 lnet_msg_commit(msg, sd->sd_cpt);
1807 * If we are routing the message then we keep the src_nid that was
1808 * set by the originator. If we are not routing then we are the
1809 * originator and set it here.
1811 if (!msg->msg_routing)
1812 msg->msg_hdr.src_nid = cpu_to_le64(msg->msg_txni->ni_nid);
1815 msg->msg_target_is_router = 1;
1816 msg->msg_target.pid = LNET_PID_LUSTRE;
1818 * since we're routing we want to ensure that the
1819 * msg_hdr.dest_nid is set to the final destination. When
1820 * the router receives this message it knows how to route
1823 * final_dst_lpni is set at the beginning of the
1824 * lnet_select_pathway() function and is never changed.
1825 * It's safe to use it here.
1827 msg->msg_hdr.dest_nid = cpu_to_le64(final_dst_lpni->lpni_nid);
1830 * if we're not routing set the dest_nid to the best peer
1831 * ni NID that we picked earlier in the algorithm.
1833 msg->msg_hdr.dest_nid = cpu_to_le64(msg->msg_txpeer->lpni_nid);
1837 * if we have response tracker block update it with the next hop
1841 rspt = msg->msg_md->md_rspt_ptr;
1843 rspt->rspt_next_hop_nid = msg->msg_txpeer->lpni_nid;
1844 CDEBUG(D_NET, "rspt_next_hop_nid = %s\n",
1845 libcfs_nid2str(rspt->rspt_next_hop_nid));
1849 rc = lnet_post_send_locked(msg, 0);
1852 CDEBUG(D_NET, "TRACE: %s(%s:%s) -> %s(%s:%s) : %s try# %d\n",
1853 libcfs_nid2str(msg->msg_hdr.src_nid),
1854 libcfs_nid2str(msg->msg_txni->ni_nid),
1855 libcfs_nid2str(sd->sd_src_nid),
1856 libcfs_nid2str(msg->msg_hdr.dest_nid),
1857 libcfs_nid2str(sd->sd_dst_nid),
1858 libcfs_nid2str(msg->msg_txpeer->lpni_nid),
1859 lnet_msgtyp2str(msg->msg_type), msg->msg_retry_count);
1865 lnet_set_non_mr_pref_nid(struct lnet_send_data *sd)
1867 if (sd->sd_send_case & NMR_DST &&
1868 sd->sd_msg->msg_type != LNET_MSG_REPLY &&
1869 sd->sd_msg->msg_type != LNET_MSG_ACK &&
1870 sd->sd_best_lpni->lpni_pref_nnids == 0) {
1871 CDEBUG(D_NET, "Setting preferred local NID %s on NMR peer %s\n",
1872 libcfs_nid2str(sd->sd_best_ni->ni_nid),
1873 libcfs_nid2str(sd->sd_best_lpni->lpni_nid));
1874 lnet_peer_ni_set_non_mr_pref_nid(sd->sd_best_lpni,
1875 sd->sd_best_ni->ni_nid);
1884 * use the source and destination NIDs as the pathway
1887 lnet_handle_spec_local_nmr_dst(struct lnet_send_data *sd)
1889 /* the destination lpni is set before we get here. */
1892 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
1893 if (!sd->sd_best_ni) {
1894 CERROR("Can't send to %s: src %s is not a "
1895 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
1896 libcfs_nid2str(sd->sd_src_nid));
1901 * the preferred NID will only be set for NMR peers
1903 lnet_set_non_mr_pref_nid(sd);
1905 return lnet_handle_send(sd);
1913 * Don't run the selection algorithm on the peer NIs. By specifying the
1914 * local NID, we're also saying that we should always use the destination NID
1915 * provided. This handles the case where we should be using the same
1916 * destination NID for the all the messages which belong to the same RPC
1920 lnet_handle_spec_local_mr_dst(struct lnet_send_data *sd)
1922 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
1923 if (!sd->sd_best_ni) {
1924 CERROR("Can't send to %s: src %s is not a "
1925 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
1926 libcfs_nid2str(sd->sd_src_nid));
1930 if (sd->sd_best_lpni &&
1931 sd->sd_best_lpni->lpni_nid == the_lnet.ln_loni->ni_nid)
1932 return lnet_handle_lo_send(sd);
1933 else if (sd->sd_best_lpni)
1934 return lnet_handle_send(sd);
1936 CERROR("can't send to %s. no NI on %s\n",
1937 libcfs_nid2str(sd->sd_dst_nid),
1938 libcfs_net2str(sd->sd_best_ni->ni_net->net_id));
1940 return -EHOSTUNREACH;
1944 lnet_find_best_ni_on_spec_net(struct lnet_ni *cur_best_ni,
1945 struct lnet_peer *peer,
1946 struct lnet_peer_net *peer_net,
1950 struct lnet_net *local_net;
1951 struct lnet_ni *best_ni;
1953 local_net = lnet_get_net_locked(peer_net->lpn_net_id);
1958 * Iterate through the NIs in this local Net and select
1959 * the NI to send from. The selection is determined by
1960 * these 3 criterion in the following priority:
1962 * 2. NI available credits
1965 best_ni = lnet_get_best_ni(local_net, cur_best_ni,
1966 peer, peer_net, cpt);
1968 if (incr_seq && best_ni)
1975 lnet_initiate_peer_discovery(struct lnet_peer_ni *lpni,
1976 struct lnet_msg *msg, lnet_nid_t rtr_nid,
1979 struct lnet_peer *peer;
1980 lnet_nid_t primary_nid;
1983 lnet_peer_ni_addref_locked(lpni);
1985 peer = lpni->lpni_peer_net->lpn_peer;
1987 if (lnet_peer_gw_discovery(peer)) {
1988 lnet_peer_ni_decref_locked(lpni);
1992 if (!lnet_msg_discovery(msg) || lnet_peer_is_uptodate(peer)) {
1993 lnet_peer_ni_decref_locked(lpni);
1997 rc = lnet_discover_peer_locked(lpni, cpt, false);
1999 lnet_peer_ni_decref_locked(lpni);
2002 /* The peer may have changed. */
2003 peer = lpni->lpni_peer_net->lpn_peer;
2004 spin_lock(&peer->lp_lock);
2005 if (lnet_peer_is_uptodate_locked(peer)) {
2006 spin_unlock(&peer->lp_lock);
2007 lnet_peer_ni_decref_locked(lpni);
2010 /* queue message and return */
2011 msg->msg_rtr_nid_param = rtr_nid;
2012 msg->msg_sending = 0;
2013 msg->msg_txpeer = NULL;
2014 list_add_tail(&msg->msg_list, &peer->lp_dc_pendq);
2015 primary_nid = peer->lp_primary_nid;
2016 spin_unlock(&peer->lp_lock);
2018 lnet_peer_ni_decref_locked(lpni);
2020 CDEBUG(D_NET, "msg %p delayed. %s pending discovery\n",
2021 msg, libcfs_nid2str(primary_nid));
2023 return LNET_DC_WAIT;
2027 lnet_handle_find_routed_path(struct lnet_send_data *sd,
2029 struct lnet_peer_ni **gw_lpni,
2030 struct lnet_peer **gw_peer)
2033 struct lnet_peer *gw;
2034 struct lnet_peer *lp;
2035 struct lnet_peer_net *lpn;
2036 struct lnet_peer_net *best_lpn = NULL;
2037 struct lnet_remotenet *rnet;
2038 struct lnet_route *best_route;
2039 struct lnet_route *last_route;
2040 struct lnet_peer_ni *lpni = NULL;
2041 struct lnet_peer_ni *gwni = NULL;
2042 lnet_nid_t src_nid = sd->sd_src_nid;
2044 /* we've already looked up the initial lpni using dst_nid */
2045 lpni = sd->sd_best_lpni;
2046 /* the peer tree must be in existence */
2047 LASSERT(lpni && lpni->lpni_peer_net && lpni->lpni_peer_net->lpn_peer);
2048 lp = lpni->lpni_peer_net->lpn_peer;
2050 list_for_each_entry(lpn, &lp->lp_peer_nets, lpn_peer_nets) {
2051 /* is this remote network reachable? */
2052 rnet = lnet_find_rnet_locked(lpn->lpn_net_id);
2059 if (best_lpn->lpn_seq <= lpn->lpn_seq)
2066 CERROR("peer %s has no available nets \n",
2067 libcfs_nid2str(sd->sd_dst_nid));
2068 return -EHOSTUNREACH;
2071 sd->sd_best_lpni = lnet_find_best_lpni_on_net(sd, lp, best_lpn->lpn_net_id);
2072 if (!sd->sd_best_lpni) {
2073 CERROR("peer %s down\n", libcfs_nid2str(sd->sd_dst_nid));
2074 return -EHOSTUNREACH;
2077 best_route = lnet_find_route_locked(NULL, best_lpn->lpn_net_id,
2078 sd->sd_rtr_nid, &last_route,
2081 CERROR("no route to %s from %s\n",
2082 libcfs_nid2str(dst_nid), libcfs_nid2str(src_nid));
2083 return -EHOSTUNREACH;
2087 CERROR("Internal Error. Route expected to %s from %s\n",
2088 libcfs_nid2str(dst_nid),
2089 libcfs_nid2str(src_nid));
2093 gw = best_route->lr_gateway;
2094 LASSERT(gw == gwni->lpni_peer_net->lpn_peer);
2097 * Discover this gateway if it hasn't already been discovered.
2098 * This means we might delay the message until discovery has
2101 sd->sd_msg->msg_src_nid_param = sd->sd_src_nid;
2102 rc = lnet_initiate_peer_discovery(gwni, sd->sd_msg, sd->sd_rtr_nid,
2107 if (!sd->sd_best_ni)
2108 sd->sd_best_ni = lnet_find_best_ni_on_spec_net(NULL, gw,
2109 lnet_peer_get_net_locked(gw,
2110 best_route->lr_lnet),
2114 if (!sd->sd_best_ni) {
2115 CERROR("Internal Error. Expected local ni on %s "
2116 "but non found :%s\n",
2117 libcfs_net2str(best_route->lr_lnet),
2118 libcfs_nid2str(sd->sd_src_nid));
2126 * increment the sequence numbers since now we're sure we're
2127 * going to use this path
2129 LASSERT(best_route && last_route);
2130 best_route->lr_seq = last_route->lr_seq + 1;
2131 best_lpn->lpn_seq++;
2141 * Remote destination
2142 * Non-MR destination
2146 * Remote destination
2149 * The handling of these two cases is similar. Even though the destination
2150 * can be MR or non-MR, we'll deal directly with the router.
2153 lnet_handle_spec_router_dst(struct lnet_send_data *sd)
2156 struct lnet_peer_ni *gw_lpni = NULL;
2157 struct lnet_peer *gw_peer = NULL;
2160 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
2161 if (!sd->sd_best_ni) {
2162 CERROR("Can't send to %s: src %s is not a "
2163 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
2164 libcfs_nid2str(sd->sd_src_nid));
2168 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2173 if (sd->sd_send_case & NMR_DST)
2175 * since the final destination is non-MR let's set its preferred
2176 * NID before we send
2178 lnet_set_non_mr_pref_nid(sd);
2181 * We're going to send to the gw found so let's set its
2184 sd->sd_peer = gw_peer;
2185 sd->sd_best_lpni = gw_lpni;
2187 return lnet_handle_send(sd);
2191 lnet_find_best_ni_on_local_net(struct lnet_peer *peer, int md_cpt,
2194 struct lnet_peer_net *peer_net = NULL;
2195 struct lnet_ni *best_ni = NULL;
2198 * The peer can have multiple interfaces, some of them can be on
2199 * the local network and others on a routed network. We should
2200 * prefer the local network. However if the local network is not
2201 * available then we need to try the routed network
2204 /* go through all the peer nets and find the best_ni */
2205 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_peer_nets) {
2207 * The peer's list of nets can contain non-local nets. We
2208 * want to only examine the local ones.
2210 if (!lnet_get_net_locked(peer_net->lpn_net_id))
2212 best_ni = lnet_find_best_ni_on_spec_net(best_ni, peer,
2213 peer_net, md_cpt, false);
2216 * if this is a discovery message and lp_disc_net_id is
2217 * specified then use that net to send the discovery on.
2219 if (peer->lp_disc_net_id == peer_net->lpn_net_id &&
2225 /* increment sequence number so we can round robin */
2231 static struct lnet_ni *
2232 lnet_find_existing_preferred_best_ni(struct lnet_send_data *sd)
2234 struct lnet_ni *best_ni = NULL;
2235 struct lnet_peer_net *peer_net;
2236 struct lnet_peer *peer = sd->sd_peer;
2237 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
2238 struct lnet_peer_ni *lpni;
2239 int cpt = sd->sd_cpt;
2242 * We must use a consistent source address when sending to a
2243 * non-MR peer. However, a non-MR peer can have multiple NIDs
2244 * on multiple networks, and we may even need to talk to this
2245 * peer on multiple networks -- certain types of
2246 * load-balancing configuration do this.
2248 * So we need to pick the NI the peer prefers for this
2249 * particular network.
2252 /* Get the target peer_ni */
2253 peer_net = lnet_peer_get_net_locked(peer,
2254 LNET_NIDNET(best_lpni->lpni_nid));
2255 LASSERT(peer_net != NULL);
2256 list_for_each_entry(lpni, &peer_net->lpn_peer_nis,
2258 if (lpni->lpni_pref_nnids == 0)
2260 LASSERT(lpni->lpni_pref_nnids == 1);
2261 best_ni = lnet_nid2ni_locked(
2262 lpni->lpni_pref.nid, cpt);
2269 /* Prerequisite: sd->sd_peer and sd->sd_best_lpni should be set */
2271 lnet_select_preferred_best_ni(struct lnet_send_data *sd)
2273 struct lnet_ni *best_ni = NULL;
2274 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
2277 * We must use a consistent source address when sending to a
2278 * non-MR peer. However, a non-MR peer can have multiple NIDs
2279 * on multiple networks, and we may even need to talk to this
2280 * peer on multiple networks -- certain types of
2281 * load-balancing configuration do this.
2283 * So we need to pick the NI the peer prefers for this
2284 * particular network.
2287 best_ni = lnet_find_existing_preferred_best_ni(sd);
2289 /* if best_ni is still not set just pick one */
2292 lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2293 sd->sd_best_lpni->lpni_peer_net,
2294 sd->sd_md_cpt, true);
2295 /* If there is no best_ni we don't have a route */
2297 CERROR("no path to %s from net %s\n",
2298 libcfs_nid2str(best_lpni->lpni_nid),
2299 libcfs_net2str(best_lpni->lpni_net->net_id));
2300 return -EHOSTUNREACH;
2304 sd->sd_best_ni = best_ni;
2306 /* Set preferred NI if necessary. */
2307 lnet_set_non_mr_pref_nid(sd);
2314 * Source not specified
2318 * always use the same source NID for NMR peers
2319 * If we've talked to that peer before then we already have a preferred
2320 * source NI associated with it. Otherwise, we select a preferred local NI
2321 * and store it in the peer
2324 lnet_handle_any_local_nmr_dst(struct lnet_send_data *sd)
2328 /* sd->sd_best_lpni is already set to the final destination */
2331 * At this point we should've created the peer ni and peer. If we
2332 * can't find it, then something went wrong. Instead of assert
2333 * output a relevant message and fail the send
2335 if (!sd->sd_best_lpni) {
2336 CERROR("Internal fault. Unable to send msg %s to %s. "
2338 lnet_msgtyp2str(sd->sd_msg->msg_type),
2339 libcfs_nid2str(sd->sd_dst_nid));
2343 rc = lnet_select_preferred_best_ni(sd);
2345 rc = lnet_handle_send(sd);
2351 lnet_handle_any_mr_dsta(struct lnet_send_data *sd)
2354 * NOTE we've already handled the remote peer case. So we only
2355 * need to worry about the local case here.
2357 * if we're sending a response, ACK or reply, we need to send it
2358 * to the destination NID given to us. At this point we already
2359 * have the peer_ni we're suppose to send to, so just find the
2360 * best_ni on the peer net and use that. Since we're sending to an
2361 * MR peer then we can just run the selection algorithm on our
2362 * local NIs and pick the best one.
2364 if (sd->sd_send_case & SND_RESP) {
2366 lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2367 sd->sd_best_lpni->lpni_peer_net,
2368 sd->sd_md_cpt, true);
2370 if (!sd->sd_best_ni) {
2372 * We're not going to deal with not able to send
2373 * a response to the provided final destination
2375 CERROR("Can't send response to %s. "
2376 "No local NI available\n",
2377 libcfs_nid2str(sd->sd_dst_nid));
2378 return -EHOSTUNREACH;
2381 return lnet_handle_send(sd);
2385 * If we get here that means we're sending a fresh request, PUT or
2386 * GET, so we need to run our standard selection algorithm.
2387 * First find the best local interface that's on any of the peer's
2390 sd->sd_best_ni = lnet_find_best_ni_on_local_net(sd->sd_peer,
2392 lnet_msg_discovery(sd->sd_msg));
2393 if (sd->sd_best_ni) {
2395 lnet_find_best_lpni_on_net(sd, sd->sd_peer,
2396 sd->sd_best_ni->ni_net->net_id);
2399 * if we're successful in selecting a peer_ni on the local
2400 * network, then send to it. Otherwise fall through and
2401 * try and see if we can reach it over another routed
2404 if (sd->sd_best_lpni &&
2405 sd->sd_best_lpni->lpni_nid == the_lnet.ln_loni->ni_nid) {
2407 * in case we initially started with a routed
2408 * destination, let's reset to local
2410 sd->sd_send_case &= ~REMOTE_DST;
2411 sd->sd_send_case |= LOCAL_DST;
2412 return lnet_handle_lo_send(sd);
2413 } else if (sd->sd_best_lpni) {
2415 * in case we initially started with a routed
2416 * destination, let's reset to local
2418 sd->sd_send_case &= ~REMOTE_DST;
2419 sd->sd_send_case |= LOCAL_DST;
2420 return lnet_handle_send(sd);
2423 CERROR("Internal Error. Expected to have a best_lpni: "
2425 libcfs_nid2str(sd->sd_src_nid),
2426 libcfs_nid2str(sd->sd_dst_nid));
2432 * Peer doesn't have a local network. Let's see if there is
2433 * a remote network we can reach it on.
2435 return PASS_THROUGH;
2440 * Source NID not specified
2445 * Source NID not speified
2446 * Remote destination
2449 * In both of these cases if we're sending a response, ACK or REPLY, then
2450 * we need to send to the destination NID provided.
2452 * In the remote case let's deal with MR routers.
2457 lnet_handle_any_mr_dst(struct lnet_send_data *sd)
2460 struct lnet_peer *gw_peer = NULL;
2461 struct lnet_peer_ni *gw_lpni = NULL;
2464 * handle sending a response to a remote peer here so we don't
2465 * have to worry about it if we hit lnet_handle_any_mr_dsta()
2467 if (sd->sd_send_case & REMOTE_DST &&
2468 sd->sd_send_case & SND_RESP) {
2469 struct lnet_peer_ni *gw;
2470 struct lnet_peer *gw_peer;
2472 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw,
2475 CERROR("Can't send response to %s. "
2476 "No route available\n",
2477 libcfs_nid2str(sd->sd_dst_nid));
2478 return -EHOSTUNREACH;
2479 } else if (rc > 0) {
2483 sd->sd_best_lpni = gw;
2484 sd->sd_peer = gw_peer;
2486 return lnet_handle_send(sd);
2490 * Even though the NID for the peer might not be on a local network,
2491 * since the peer is MR there could be other interfaces on the
2492 * local network. In that case we'd still like to prefer the local
2493 * network over the routed network. If we're unable to do that
2494 * then we select the best router among the different routed networks,
2495 * and if the router is MR then we can deal with it as such.
2497 rc = lnet_handle_any_mr_dsta(sd);
2498 if (rc != PASS_THROUGH)
2502 * Now that we must route to the destination, we must consider the
2503 * MR case, where the destination has multiple interfaces, some of
2504 * which we can route to and others we do not. For this reason we
2505 * need to select the destination which we can route to and if
2506 * there are multiple, we need to round robin.
2508 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2513 sd->sd_send_case &= ~LOCAL_DST;
2514 sd->sd_send_case |= REMOTE_DST;
2516 sd->sd_peer = gw_peer;
2517 sd->sd_best_lpni = gw_lpni;
2519 return lnet_handle_send(sd);
2523 * Source not specified
2524 * Remote destination
2527 * Must send to the specified peer NID using the same source NID that
2528 * we've used before. If it's the first time to talk to that peer then
2529 * find the source NI and assign it as preferred to that peer
2532 lnet_handle_any_router_nmr_dst(struct lnet_send_data *sd)
2535 struct lnet_peer_ni *gw_lpni = NULL;
2536 struct lnet_peer *gw_peer = NULL;
2539 * Let's set if we have a preferred NI to talk to this NMR peer
2541 sd->sd_best_ni = lnet_find_existing_preferred_best_ni(sd);
2544 * find the router and that'll find the best NI if we didn't find
2547 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2553 * set the best_ni we've chosen as the preferred one for
2556 lnet_set_non_mr_pref_nid(sd);
2558 /* we'll be sending to the gw */
2559 sd->sd_best_lpni = gw_lpni;
2560 sd->sd_peer = gw_peer;
2562 return lnet_handle_send(sd);
2566 lnet_handle_send_case_locked(struct lnet_send_data *sd)
2569 * turn off the SND_RESP bit.
2570 * It will be checked in the case handling
2572 __u32 send_case = sd->sd_send_case &= ~SND_RESP ;
2574 CDEBUG(D_NET, "Source %s%s to %s %s %s destination\n",
2575 (send_case & SRC_SPEC) ? "Specified: " : "ANY",
2576 (send_case & SRC_SPEC) ? libcfs_nid2str(sd->sd_src_nid) : "",
2577 (send_case & MR_DST) ? "MR: " : "NMR: ",
2578 libcfs_nid2str(sd->sd_dst_nid),
2579 (send_case & LOCAL_DST) ? "local" : "routed");
2581 switch (send_case) {
2583 * For all cases where the source is specified, we should always
2584 * use the destination NID, whether it's an MR destination or not,
2585 * since we're continuing a series of related messages for the
2588 case SRC_SPEC_LOCAL_NMR_DST:
2589 return lnet_handle_spec_local_nmr_dst(sd);
2590 case SRC_SPEC_LOCAL_MR_DST:
2591 return lnet_handle_spec_local_mr_dst(sd);
2592 case SRC_SPEC_ROUTER_NMR_DST:
2593 case SRC_SPEC_ROUTER_MR_DST:
2594 return lnet_handle_spec_router_dst(sd);
2595 case SRC_ANY_LOCAL_NMR_DST:
2596 return lnet_handle_any_local_nmr_dst(sd);
2597 case SRC_ANY_LOCAL_MR_DST:
2598 case SRC_ANY_ROUTER_MR_DST:
2599 return lnet_handle_any_mr_dst(sd);
2600 case SRC_ANY_ROUTER_NMR_DST:
2601 return lnet_handle_any_router_nmr_dst(sd);
2603 CERROR("Unknown send case\n");
2609 lnet_select_pathway(lnet_nid_t src_nid, lnet_nid_t dst_nid,
2610 struct lnet_msg *msg, lnet_nid_t rtr_nid)
2612 struct lnet_peer_ni *lpni;
2613 struct lnet_peer *peer;
2614 struct lnet_send_data send_data;
2617 __u32 send_case = 0;
2619 memset(&send_data, 0, sizeof(send_data));
2622 * get an initial CPT to use for locking. The idea here is not to
2623 * serialize the calls to select_pathway, so that as many
2624 * operations can run concurrently as possible. To do that we use
2625 * the CPT where this call is being executed. Later on when we
2626 * determine the CPT to use in lnet_message_commit, we switch the
2627 * lock and check if there was any configuration change. If none,
2628 * then we proceed, if there is, then we restart the operation.
2630 cpt = lnet_net_lock_current();
2632 md_cpt = lnet_cpt_of_md(msg->msg_md, msg->msg_offset);
2633 if (md_cpt == CFS_CPT_ANY)
2639 * If we're being asked to send to the loopback interface, there
2640 * is no need to go through any selection. We can just shortcut
2641 * the entire process and send over lolnd
2643 send_data.sd_msg = msg;
2644 send_data.sd_cpt = cpt;
2645 if (LNET_NETTYP(LNET_NIDNET(dst_nid)) == LOLND) {
2646 rc = lnet_handle_lo_send(&send_data);
2647 lnet_net_unlock(cpt);
2652 * find an existing peer_ni, or create one and mark it as having been
2653 * created due to network traffic. This call will create the
2654 * peer->peer_net->peer_ni tree.
2656 lpni = lnet_nid2peerni_locked(dst_nid, LNET_NID_ANY, cpt);
2658 lnet_net_unlock(cpt);
2659 return PTR_ERR(lpni);
2663 * Cache the original src_nid. If we need to resend the message
2664 * then we'll need to know whether the src_nid was originally
2665 * specified for this message. If it was originally specified,
2666 * then we need to keep using the same src_nid since it's
2667 * continuing the same sequence of messages.
2669 msg->msg_src_nid_param = src_nid;
2672 * If necessary, perform discovery on the peer that owns this peer_ni.
2673 * Note, this can result in the ownership of this peer_ni changing
2674 * to another peer object.
2676 rc = lnet_initiate_peer_discovery(lpni, msg, rtr_nid, cpt);
2678 lnet_peer_ni_decref_locked(lpni);
2679 lnet_net_unlock(cpt);
2682 lnet_peer_ni_decref_locked(lpni);
2684 peer = lpni->lpni_peer_net->lpn_peer;
2687 * Identify the different send cases
2689 if (src_nid == LNET_NID_ANY)
2690 send_case |= SRC_ANY;
2692 send_case |= SRC_SPEC;
2694 if (lnet_get_net_locked(LNET_NIDNET(dst_nid)))
2695 send_case |= LOCAL_DST;
2697 send_case |= REMOTE_DST;
2700 * if this is a non-MR peer or if we're recovering a peer ni then
2701 * let's consider this an NMR case so we can hit the destination
2704 if (!lnet_peer_is_multi_rail(peer) || msg->msg_recovery)
2705 send_case |= NMR_DST;
2707 send_case |= MR_DST;
2709 if (msg->msg_type == LNET_MSG_REPLY ||
2710 msg->msg_type == LNET_MSG_ACK)
2711 send_case |= SND_RESP;
2713 /* assign parameters to the send_data */
2714 send_data.sd_rtr_nid = rtr_nid;
2715 send_data.sd_src_nid = src_nid;
2716 send_data.sd_dst_nid = dst_nid;
2717 send_data.sd_best_lpni = lpni;
2719 * keep a pointer to the final destination in case we're going to
2720 * route, so we'll need to access it later
2722 send_data.sd_final_dst_lpni = lpni;
2723 send_data.sd_peer = peer;
2724 send_data.sd_md_cpt = md_cpt;
2725 send_data.sd_send_case = send_case;
2727 rc = lnet_handle_send_case_locked(&send_data);
2730 * Update the local cpt since send_data.sd_cpt might've been
2731 * updated as a result of calling lnet_handle_send_case_locked().
2733 cpt = send_data.sd_cpt;
2735 if (rc == REPEAT_SEND)
2738 lnet_net_unlock(cpt);
2744 lnet_send(lnet_nid_t src_nid, struct lnet_msg *msg, lnet_nid_t rtr_nid)
2746 lnet_nid_t dst_nid = msg->msg_target.nid;
2750 * NB: rtr_nid is set to LNET_NID_ANY for all current use-cases,
2751 * but we might want to use pre-determined router for ACK/REPLY
2754 /* NB: ni != NULL == interface pre-determined (ACK/REPLY) */
2755 LASSERT(msg->msg_txpeer == NULL);
2756 LASSERT(msg->msg_txni == NULL);
2757 LASSERT(!msg->msg_sending);
2758 LASSERT(!msg->msg_target_is_router);
2759 LASSERT(!msg->msg_receiving);
2761 msg->msg_sending = 1;
2763 LASSERT(!msg->msg_tx_committed);
2765 rc = lnet_select_pathway(src_nid, dst_nid, msg, rtr_nid);
2767 if (rc == -EHOSTUNREACH)
2768 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_ERROR;
2770 msg->msg_health_status = LNET_MSG_STATUS_LOCAL_ERROR;
2774 if (rc == LNET_CREDIT_OK)
2775 lnet_ni_send(msg->msg_txni, msg);
2777 /* rc == LNET_CREDIT_OK or LNET_CREDIT_WAIT or LNET_DC_WAIT */
2781 enum lnet_mt_event_type {
2782 MT_TYPE_LOCAL_NI = 0,
2786 struct lnet_mt_event_info {
2787 enum lnet_mt_event_type mt_type;
2791 /* called with res_lock held */
2793 lnet_detach_rsp_tracker(struct lnet_libmd *md, int cpt)
2795 struct lnet_rsp_tracker *rspt;
2798 * msg has a refcount on the MD so the MD is not going away.
2799 * The rspt queue for the cpt is protected by
2800 * the lnet_net_lock(cpt). cpt is the cpt of the MD cookie.
2802 if (!md->md_rspt_ptr)
2805 rspt = md->md_rspt_ptr;
2808 LASSERT(rspt->rspt_cpt == cpt);
2810 md->md_rspt_ptr = NULL;
2812 if (LNetMDHandleIsInvalid(rspt->rspt_mdh)) {
2814 * The monitor thread has invalidated this handle because the
2815 * response timed out, but it failed to lookup the MD. That
2816 * means this response tracker is on the zombie list. We can
2817 * safely remove it under the resource lock (held by caller) and
2818 * free the response tracker block.
2820 list_del(&rspt->rspt_on_list);
2821 lnet_rspt_free(rspt, cpt);
2824 * invalidate the handle to indicate that a response has been
2825 * received, which will then lead the monitor thread to clean up
2828 LNetInvalidateMDHandle(&rspt->rspt_mdh);
2833 lnet_clean_zombie_rstqs(void)
2835 struct lnet_rsp_tracker *rspt, *tmp;
2838 cfs_cpt_for_each(i, lnet_cpt_table()) {
2839 list_for_each_entry_safe(rspt, tmp,
2840 the_lnet.ln_mt_zombie_rstqs[i],
2842 list_del(&rspt->rspt_on_list);
2843 lnet_rspt_free(rspt, i);
2847 cfs_percpt_free(the_lnet.ln_mt_zombie_rstqs);
2851 lnet_finalize_expired_responses(void)
2853 struct lnet_libmd *md;
2854 struct list_head local_queue;
2855 struct lnet_rsp_tracker *rspt, *tmp;
2859 if (the_lnet.ln_mt_rstq == NULL)
2862 cfs_cpt_for_each(i, lnet_cpt_table()) {
2863 INIT_LIST_HEAD(&local_queue);
2866 if (!the_lnet.ln_mt_rstq[i]) {
2870 list_splice_init(the_lnet.ln_mt_rstq[i], &local_queue);
2875 list_for_each_entry_safe(rspt, tmp, &local_queue, rspt_on_list) {
2877 * The rspt mdh will be invalidated when a response
2878 * is received or whenever we want to discard the
2879 * block the monitor thread will walk the queue
2880 * and clean up any rsts with an invalid mdh.
2881 * The monitor thread will walk the queue until
2882 * the first unexpired rspt block. This means that
2883 * some rspt blocks which received their
2884 * corresponding responses will linger in the
2885 * queue until they are cleaned up eventually.
2888 if (LNetMDHandleIsInvalid(rspt->rspt_mdh)) {
2890 list_del(&rspt->rspt_on_list);
2891 lnet_rspt_free(rspt, i);
2895 if (ktime_compare(now, rspt->rspt_deadline) >= 0 ||
2896 the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN) {
2897 struct lnet_peer_ni *lpni;
2900 md = lnet_handle2md(&rspt->rspt_mdh);
2902 /* MD has been queued for unlink, but
2903 * rspt hasn't been detached (Note we've
2904 * checked above that the rspt_mdh is
2905 * valid). Since we cannot lookup the MD
2906 * we're unable to detach the rspt
2907 * ourselves. Thus, move the rspt to the
2908 * zombie list where we'll wait for
2910 * 1. The remaining operations on the
2911 * MD to complete. In this case the
2912 * final operation will result in
2913 * lnet_msg_detach_md()->
2914 * lnet_detach_rsp_tracker() where
2915 * we will clean up this response
2917 * 2. LNet to shutdown. In this case
2918 * we'll wait until after all LND Nets
2919 * have shutdown and then we can
2920 * safely free any remaining response
2921 * tracker blocks on the zombie list.
2922 * Note: We need to hold the resource
2923 * lock when adding to the zombie list
2924 * because we may have concurrent access
2925 * with lnet_detach_rsp_tracker().
2927 LNetInvalidateMDHandle(&rspt->rspt_mdh);
2928 list_move(&rspt->rspt_on_list,
2929 the_lnet.ln_mt_zombie_rstqs[i]);
2933 LASSERT(md->md_rspt_ptr == rspt);
2934 md->md_rspt_ptr = NULL;
2937 LNetMDUnlink(rspt->rspt_mdh);
2939 nid = rspt->rspt_next_hop_nid;
2941 list_del(&rspt->rspt_on_list);
2942 lnet_rspt_free(rspt, i);
2944 /* If we're shutting down we just want to clean
2945 * up the rspt blocks
2947 if (the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN)
2951 the_lnet.ln_counters[i]->lct_health.lch_response_timeout_count++;
2955 "Response timeout: md = %p: nid = %s\n",
2956 md, libcfs_nid2str(nid));
2959 * If there is a timeout on the response
2960 * from the next hop decrement its health
2961 * value so that we don't use it
2964 lpni = lnet_find_peer_ni_locked(nid);
2966 lnet_handle_remote_failure_locked(lpni);
2967 lnet_peer_ni_decref_locked(lpni);
2976 if (!list_empty(&local_queue)) {
2978 list_splice(&local_queue, the_lnet.ln_mt_rstq[i]);
2985 lnet_resend_pending_msgs_locked(struct list_head *resendq, int cpt)
2987 struct lnet_msg *msg;
2989 while (!list_empty(resendq)) {
2990 struct lnet_peer_ni *lpni;
2992 msg = list_entry(resendq->next, struct lnet_msg,
2995 list_del_init(&msg->msg_list);
2997 lpni = lnet_find_peer_ni_locked(msg->msg_hdr.dest_nid);
2999 lnet_net_unlock(cpt);
3000 CERROR("Expected that a peer is already created for %s\n",
3001 libcfs_nid2str(msg->msg_hdr.dest_nid));
3002 msg->msg_no_resend = true;
3003 lnet_finalize(msg, -EFAULT);
3006 struct lnet_peer *peer;
3008 lnet_nid_t src_nid = LNET_NID_ANY;
3011 * if this message is not being routed and the
3012 * peer is non-MR then we must use the same
3013 * src_nid that was used in the original send.
3014 * Otherwise if we're routing the message (IE
3015 * we're a router) then we can use any of our
3016 * local interfaces. It doesn't matter to the
3017 * final destination.
3019 peer = lpni->lpni_peer_net->lpn_peer;
3020 if (!msg->msg_routing &&
3021 !lnet_peer_is_multi_rail(peer))
3022 src_nid = le64_to_cpu(msg->msg_hdr.src_nid);
3025 * If we originally specified a src NID, then we
3026 * must attempt to reuse it in the resend as well.
3028 if (msg->msg_src_nid_param != LNET_NID_ANY)
3029 src_nid = msg->msg_src_nid_param;
3030 lnet_peer_ni_decref_locked(lpni);
3032 lnet_net_unlock(cpt);
3033 CDEBUG(D_NET, "resending %s->%s: %s recovery %d try# %d\n",
3034 libcfs_nid2str(src_nid),
3035 libcfs_id2str(msg->msg_target),
3036 lnet_msgtyp2str(msg->msg_type),
3038 msg->msg_retry_count);
3039 rc = lnet_send(src_nid, msg, LNET_NID_ANY);
3041 CERROR("Error sending %s to %s: %d\n",
3042 lnet_msgtyp2str(msg->msg_type),
3043 libcfs_id2str(msg->msg_target), rc);
3044 msg->msg_no_resend = true;
3045 lnet_finalize(msg, rc);
3049 the_lnet.ln_counters[cpt]->lct_health.lch_resend_count++;
3055 lnet_resend_pending_msgs(void)
3059 cfs_cpt_for_each(i, lnet_cpt_table()) {
3061 lnet_resend_pending_msgs_locked(the_lnet.ln_mt_resendqs[i], i);
3066 /* called with cpt and ni_lock held */
3068 lnet_unlink_ni_recovery_mdh_locked(struct lnet_ni *ni, int cpt, bool force)
3070 struct lnet_handle_md recovery_mdh;
3072 LNetInvalidateMDHandle(&recovery_mdh);
3074 if (ni->ni_recovery_state & LNET_NI_RECOVERY_PENDING ||
3076 recovery_mdh = ni->ni_ping_mdh;
3077 LNetInvalidateMDHandle(&ni->ni_ping_mdh);
3080 lnet_net_unlock(cpt);
3081 if (!LNetMDHandleIsInvalid(recovery_mdh))
3082 LNetMDUnlink(recovery_mdh);
3088 lnet_recover_local_nis(void)
3090 struct lnet_mt_event_info *ev_info;
3091 struct list_head processed_list;
3092 struct list_head local_queue;
3093 struct lnet_handle_md mdh;
3094 struct lnet_ni *tmp;
3100 INIT_LIST_HEAD(&local_queue);
3101 INIT_LIST_HEAD(&processed_list);
3104 * splice the recovery queue on a local queue. We will iterate
3105 * through the local queue and update it as needed. Once we're
3106 * done with the traversal, we'll splice the local queue back on
3107 * the head of the ln_mt_localNIRecovq. Any newly added local NIs
3108 * will be traversed in the next iteration.
3111 list_splice_init(&the_lnet.ln_mt_localNIRecovq,
3115 list_for_each_entry_safe(ni, tmp, &local_queue, ni_recovery) {
3117 * if an NI is being deleted or it is now healthy, there
3118 * is no need to keep it around in the recovery queue.
3119 * The monitor thread is the only thread responsible for
3120 * removing the NI from the recovery queue.
3121 * Multiple threads can be adding NIs to the recovery
3124 healthv = atomic_read(&ni->ni_healthv);
3128 if (ni->ni_state != LNET_NI_STATE_ACTIVE ||
3129 healthv == LNET_MAX_HEALTH_VALUE) {
3130 list_del_init(&ni->ni_recovery);
3131 lnet_unlink_ni_recovery_mdh_locked(ni, 0, false);
3133 lnet_ni_decref_locked(ni, 0);
3139 * if the local NI failed recovery we must unlink the md.
3140 * But we want to keep the local_ni on the recovery queue
3141 * so we can continue the attempts to recover it.
3143 if (ni->ni_recovery_state & LNET_NI_RECOVERY_FAILED) {
3144 lnet_unlink_ni_recovery_mdh_locked(ni, 0, true);
3145 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_FAILED;
3152 CDEBUG(D_NET, "attempting to recover local ni: %s\n",
3153 libcfs_nid2str(ni->ni_nid));
3156 if (!(ni->ni_recovery_state & LNET_NI_RECOVERY_PENDING)) {
3157 ni->ni_recovery_state |= LNET_NI_RECOVERY_PENDING;
3160 LIBCFS_ALLOC(ev_info, sizeof(*ev_info));
3162 CERROR("out of memory. Can't recover %s\n",
3163 libcfs_nid2str(ni->ni_nid));
3165 ni->ni_recovery_state &=
3166 ~LNET_NI_RECOVERY_PENDING;
3171 mdh = ni->ni_ping_mdh;
3173 * Invalidate the ni mdh in case it's deleted.
3174 * We'll unlink the mdh in this case below.
3176 LNetInvalidateMDHandle(&ni->ni_ping_mdh);
3180 * remove the NI from the local queue and drop the
3181 * reference count to it while we're recovering
3182 * it. The reason for that, is that the NI could
3183 * be deleted, and the way the code is structured
3184 * is if we don't drop the NI, then the deletion
3185 * code will enter a loop waiting for the
3186 * reference count to be removed while holding the
3187 * ln_mutex_lock(). When we look up the peer to
3188 * send to in lnet_select_pathway() we will try to
3189 * lock the ln_mutex_lock() as well, leading to
3190 * a deadlock. By dropping the refcount and
3191 * removing it from the list, we allow for the NI
3192 * to be removed, then we use the cached NID to
3193 * look it up again. If it's gone, then we just
3194 * continue examining the rest of the queue.
3197 list_del_init(&ni->ni_recovery);
3198 lnet_ni_decref_locked(ni, 0);
3201 ev_info->mt_type = MT_TYPE_LOCAL_NI;
3202 ev_info->mt_nid = nid;
3203 rc = lnet_send_ping(nid, &mdh, LNET_INTERFACES_MIN,
3204 ev_info, the_lnet.ln_mt_eqh, true);
3205 /* lookup the nid again */
3207 ni = lnet_nid2ni_locked(nid, 0);
3210 * the NI has been deleted when we dropped
3218 * Same note as in lnet_recover_peer_nis(). When
3219 * we're sending the ping, the NI is free to be
3220 * deleted or manipulated. By this point it
3221 * could've been added back on the recovery queue,
3222 * and a refcount taken on it.
3223 * So we can't just add it blindly again or we'll
3224 * corrupt the queue. We must check under lock if
3225 * it's not on any list and if not then add it
3226 * to the processed list, which will eventually be
3227 * spliced back on to the recovery queue.
3229 ni->ni_ping_mdh = mdh;
3230 if (list_empty(&ni->ni_recovery)) {
3231 list_add_tail(&ni->ni_recovery, &processed_list);
3232 lnet_ni_addref_locked(ni, 0);
3238 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_PENDING;
3244 * put back the remaining NIs on the ln_mt_localNIRecovq to be
3245 * reexamined in the next iteration.
3247 list_splice_init(&processed_list, &local_queue);
3249 list_splice(&local_queue, &the_lnet.ln_mt_localNIRecovq);
3254 lnet_resendqs_create(void)
3256 struct list_head **resendqs;
3257 resendqs = lnet_create_array_of_queues();
3262 lnet_net_lock(LNET_LOCK_EX);
3263 the_lnet.ln_mt_resendqs = resendqs;
3264 lnet_net_unlock(LNET_LOCK_EX);
3270 lnet_clean_local_ni_recoveryq(void)
3274 /* This is only called when the monitor thread has stopped */
3277 while (!list_empty(&the_lnet.ln_mt_localNIRecovq)) {
3278 ni = list_entry(the_lnet.ln_mt_localNIRecovq.next,
3279 struct lnet_ni, ni_recovery);
3280 list_del_init(&ni->ni_recovery);
3282 lnet_unlink_ni_recovery_mdh_locked(ni, 0, true);
3284 lnet_ni_decref_locked(ni, 0);
3291 lnet_unlink_lpni_recovery_mdh_locked(struct lnet_peer_ni *lpni, int cpt,
3294 struct lnet_handle_md recovery_mdh;
3296 LNetInvalidateMDHandle(&recovery_mdh);
3298 if (lpni->lpni_state & LNET_PEER_NI_RECOVERY_PENDING || force) {
3299 recovery_mdh = lpni->lpni_recovery_ping_mdh;
3300 LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
3302 spin_unlock(&lpni->lpni_lock);
3303 lnet_net_unlock(cpt);
3304 if (!LNetMDHandleIsInvalid(recovery_mdh))
3305 LNetMDUnlink(recovery_mdh);
3307 spin_lock(&lpni->lpni_lock);
3311 lnet_clean_peer_ni_recoveryq(void)
3313 struct lnet_peer_ni *lpni, *tmp;
3315 lnet_net_lock(LNET_LOCK_EX);
3317 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_mt_peerNIRecovq,
3319 list_del_init(&lpni->lpni_recovery);
3320 spin_lock(&lpni->lpni_lock);
3321 lnet_unlink_lpni_recovery_mdh_locked(lpni, LNET_LOCK_EX, true);
3322 spin_unlock(&lpni->lpni_lock);
3323 lnet_peer_ni_decref_locked(lpni);
3326 lnet_net_unlock(LNET_LOCK_EX);
3330 lnet_clean_resendqs(void)
3332 struct lnet_msg *msg, *tmp;
3333 struct list_head msgs;
3336 INIT_LIST_HEAD(&msgs);
3338 cfs_cpt_for_each(i, lnet_cpt_table()) {
3340 list_splice_init(the_lnet.ln_mt_resendqs[i], &msgs);
3342 list_for_each_entry_safe(msg, tmp, &msgs, msg_list) {
3343 list_del_init(&msg->msg_list);
3344 msg->msg_no_resend = true;
3345 lnet_finalize(msg, -ESHUTDOWN);
3349 cfs_percpt_free(the_lnet.ln_mt_resendqs);
3353 lnet_recover_peer_nis(void)
3355 struct lnet_mt_event_info *ev_info;
3356 struct list_head processed_list;
3357 struct list_head local_queue;
3358 struct lnet_handle_md mdh;
3359 struct lnet_peer_ni *lpni;
3360 struct lnet_peer_ni *tmp;
3365 INIT_LIST_HEAD(&local_queue);
3366 INIT_LIST_HEAD(&processed_list);
3369 * Always use cpt 0 for locking across all interactions with
3370 * ln_mt_peerNIRecovq
3373 list_splice_init(&the_lnet.ln_mt_peerNIRecovq,
3377 list_for_each_entry_safe(lpni, tmp, &local_queue,
3380 * The same protection strategy is used here as is in the
3381 * local recovery case.
3384 healthv = atomic_read(&lpni->lpni_healthv);
3385 spin_lock(&lpni->lpni_lock);
3386 if (lpni->lpni_state & LNET_PEER_NI_DELETING ||
3387 healthv == LNET_MAX_HEALTH_VALUE) {
3388 list_del_init(&lpni->lpni_recovery);
3389 lnet_unlink_lpni_recovery_mdh_locked(lpni, 0, false);
3390 spin_unlock(&lpni->lpni_lock);
3391 lnet_peer_ni_decref_locked(lpni);
3397 * If the peer NI has failed recovery we must unlink the
3398 * md. But we want to keep the peer ni on the recovery
3399 * queue so we can try to continue recovering it
3401 if (lpni->lpni_state & LNET_PEER_NI_RECOVERY_FAILED) {
3402 lnet_unlink_lpni_recovery_mdh_locked(lpni, 0, true);
3403 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_FAILED;
3406 spin_unlock(&lpni->lpni_lock);
3410 * NOTE: we're racing with peer deletion from user space.
3411 * It's possible that a peer is deleted after we check its
3412 * state. In this case the recovery can create a new peer
3414 spin_lock(&lpni->lpni_lock);
3415 if (!(lpni->lpni_state & LNET_PEER_NI_RECOVERY_PENDING) &&
3416 !(lpni->lpni_state & LNET_PEER_NI_DELETING)) {
3417 lpni->lpni_state |= LNET_PEER_NI_RECOVERY_PENDING;
3418 spin_unlock(&lpni->lpni_lock);
3420 LIBCFS_ALLOC(ev_info, sizeof(*ev_info));
3422 CERROR("out of memory. Can't recover %s\n",
3423 libcfs_nid2str(lpni->lpni_nid));
3424 spin_lock(&lpni->lpni_lock);
3425 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3426 spin_unlock(&lpni->lpni_lock);
3430 /* look at the comments in lnet_recover_local_nis() */
3431 mdh = lpni->lpni_recovery_ping_mdh;
3432 LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
3433 nid = lpni->lpni_nid;
3435 list_del_init(&lpni->lpni_recovery);
3436 lnet_peer_ni_decref_locked(lpni);
3439 ev_info->mt_type = MT_TYPE_PEER_NI;
3440 ev_info->mt_nid = nid;
3441 rc = lnet_send_ping(nid, &mdh, LNET_INTERFACES_MIN,
3442 ev_info, the_lnet.ln_mt_eqh, true);
3445 * lnet_find_peer_ni_locked() grabs a refcount for
3446 * us. No need to take it explicitly.
3448 lpni = lnet_find_peer_ni_locked(nid);
3455 lpni->lpni_recovery_ping_mdh = mdh;
3457 * While we're unlocked the lpni could've been
3458 * readded on the recovery queue. In this case we
3459 * don't need to add it to the local queue, since
3460 * it's already on there and the thread that added
3461 * it would've incremented the refcount on the
3462 * peer, which means we need to decref the refcount
3463 * that was implicitly grabbed by find_peer_ni_locked.
3464 * Otherwise, if the lpni is still not on
3465 * the recovery queue, then we'll add it to the
3468 if (list_empty(&lpni->lpni_recovery))
3469 list_add_tail(&lpni->lpni_recovery, &processed_list);
3471 lnet_peer_ni_decref_locked(lpni);
3474 spin_lock(&lpni->lpni_lock);
3476 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3478 spin_unlock(&lpni->lpni_lock);
3481 list_splice_init(&processed_list, &local_queue);
3483 list_splice(&local_queue, &the_lnet.ln_mt_peerNIRecovq);
3488 lnet_monitor_thread(void *arg)
3490 time64_t recovery_timeout = 0;
3491 time64_t rsp_timeout = 0;
3495 wait_for_completion(&the_lnet.ln_started);
3497 * The monitor thread takes care of the following:
3498 * 1. Checks the aliveness of routers
3499 * 2. Checks if there are messages on the resend queue to resend
3501 * 3. Check if there are any NIs on the local recovery queue and
3503 * 4. Checks if there are any NIs on the remote recovery queue
3506 cfs_block_allsigs();
3508 while (the_lnet.ln_mt_state == LNET_MT_STATE_RUNNING) {
3509 now = ktime_get_real_seconds();
3511 if (lnet_router_checker_active())
3512 lnet_check_routers();
3514 lnet_resend_pending_msgs();
3516 if (now >= rsp_timeout) {
3517 lnet_finalize_expired_responses();
3518 rsp_timeout = now + (lnet_transaction_timeout / 2);
3521 if (now >= recovery_timeout) {
3522 lnet_recover_local_nis();
3523 lnet_recover_peer_nis();
3524 recovery_timeout = now + lnet_recovery_interval;
3528 * TODO do we need to check if we should sleep without
3529 * timeout? Technically, an active system will always
3530 * have messages in flight so this check will always
3531 * evaluate to false. And on an idle system do we care
3532 * if we wake up every 1 second? Although, we've seen
3533 * cases where we get a complaint that an idle thread
3534 * is waking up unnecessarily.
3536 * Take into account the current net_count when you wake
3537 * up for alive router checking, since we need to check
3538 * possibly as many networks as we have configured.
3540 interval = min(lnet_recovery_interval,
3541 min((unsigned int) alive_router_check_interval /
3542 lnet_current_net_count,
3543 lnet_transaction_timeout / 2));
3544 wait_for_completion_interruptible_timeout(
3545 &the_lnet.ln_mt_wait_complete,
3546 cfs_time_seconds(interval));
3547 /* Must re-init the completion before testing anything,
3548 * including ln_mt_state.
3550 reinit_completion(&the_lnet.ln_mt_wait_complete);
3554 lnet_net_lock(LNET_LOCK_EX);
3555 the_lnet.ln_mt_state = LNET_MT_STATE_SHUTDOWN;
3556 lnet_net_unlock(LNET_LOCK_EX);
3558 /* signal that the monitor thread is exiting */
3559 up(&the_lnet.ln_mt_signal);
3567 * Returns == 0 if success
3568 * Returns > 0 if LNetMDBind or prior fails
3569 * Returns < 0 if LNetGet fails
3572 lnet_send_ping(lnet_nid_t dest_nid,
3573 struct lnet_handle_md *mdh, int nnis,
3574 void *user_data, struct lnet_handle_eq eqh, bool recovery)
3576 struct lnet_md md = { NULL };
3577 struct lnet_process_id id;
3578 struct lnet_ping_buffer *pbuf;
3581 if (dest_nid == LNET_NID_ANY) {
3586 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
3592 /* initialize md content */
3593 md.start = &pbuf->pb_info;
3594 md.length = LNET_PING_INFO_SIZE(nnis);
3595 md.threshold = 2; /* GET/REPLY */
3597 md.options = LNET_MD_TRUNCATE;
3598 md.user_ptr = user_data;
3601 rc = LNetMDBind(md, LNET_UNLINK, mdh);
3603 lnet_ping_buffer_decref(pbuf);
3604 CERROR("Can't bind MD: %d\n", rc);
3605 rc = -rc; /* change the rc to positive */
3608 id.pid = LNET_PID_LUSTRE;
3611 rc = LNetGet(LNET_NID_ANY, *mdh, id,
3612 LNET_RESERVED_PORTAL,
3613 LNET_PROTO_PING_MATCHBITS, 0, recovery);
3616 goto fail_unlink_md;
3622 LNetInvalidateMDHandle(mdh);
3628 lnet_handle_recovery_reply(struct lnet_mt_event_info *ev_info,
3629 int status, bool unlink_event)
3631 lnet_nid_t nid = ev_info->mt_nid;
3633 if (ev_info->mt_type == MT_TYPE_LOCAL_NI) {
3637 ni = lnet_nid2ni_locked(nid, 0);
3643 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_PENDING;
3645 ni->ni_recovery_state |= LNET_NI_RECOVERY_FAILED;
3650 CERROR("local NI (%s) recovery failed with %d\n",
3651 libcfs_nid2str(nid), status);
3655 * need to increment healthv for the ni here, because in
3656 * the lnet_finalize() path we don't have access to this
3657 * NI. And in order to get access to it, we'll need to
3658 * carry forward too much information.
3659 * In the peer case, it'll naturally be incremented
3662 lnet_inc_healthv(&ni->ni_healthv);
3664 struct lnet_peer_ni *lpni;
3667 cpt = lnet_net_lock_current();
3668 lpni = lnet_find_peer_ni_locked(nid);
3670 lnet_net_unlock(cpt);
3673 spin_lock(&lpni->lpni_lock);
3674 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3676 lpni->lpni_state |= LNET_PEER_NI_RECOVERY_FAILED;
3677 spin_unlock(&lpni->lpni_lock);
3678 lnet_peer_ni_decref_locked(lpni);
3679 lnet_net_unlock(cpt);
3682 CERROR("peer NI (%s) recovery failed with %d\n",
3683 libcfs_nid2str(nid), status);
3688 lnet_mt_event_handler(struct lnet_event *event)
3690 struct lnet_mt_event_info *ev_info = event->md.user_ptr;
3691 struct lnet_ping_buffer *pbuf;
3693 /* TODO: remove assert */
3694 LASSERT(event->type == LNET_EVENT_REPLY ||
3695 event->type == LNET_EVENT_SEND ||
3696 event->type == LNET_EVENT_UNLINK);
3698 CDEBUG(D_NET, "Received event: %d status: %d\n", event->type,
3701 switch (event->type) {
3702 case LNET_EVENT_UNLINK:
3703 CDEBUG(D_NET, "%s recovery ping unlinked\n",
3704 libcfs_nid2str(ev_info->mt_nid));
3706 case LNET_EVENT_REPLY:
3707 lnet_handle_recovery_reply(ev_info, event->status,
3708 event->type == LNET_EVENT_UNLINK);
3710 case LNET_EVENT_SEND:
3711 CDEBUG(D_NET, "%s recovery message sent %s:%d\n",
3712 libcfs_nid2str(ev_info->mt_nid),
3713 (event->status) ? "unsuccessfully" :
3714 "successfully", event->status);
3717 CERROR("Unexpected event: %d\n", event->type);
3720 if (event->unlinked) {
3721 LIBCFS_FREE(ev_info, sizeof(*ev_info));
3722 pbuf = LNET_PING_INFO_TO_BUFFER(event->md.start);
3723 lnet_ping_buffer_decref(pbuf);
3728 lnet_rsp_tracker_create(void)
3730 struct list_head **rstqs;
3731 rstqs = lnet_create_array_of_queues();
3736 the_lnet.ln_mt_rstq = rstqs;
3742 lnet_rsp_tracker_clean(void)
3744 lnet_finalize_expired_responses();
3746 cfs_percpt_free(the_lnet.ln_mt_rstq);
3747 the_lnet.ln_mt_rstq = NULL;
3750 int lnet_monitor_thr_start(void)
3753 struct task_struct *task;
3755 if (the_lnet.ln_mt_state != LNET_MT_STATE_SHUTDOWN)
3758 rc = lnet_resendqs_create();
3762 rc = lnet_rsp_tracker_create();
3766 sema_init(&the_lnet.ln_mt_signal, 0);
3768 lnet_net_lock(LNET_LOCK_EX);
3769 the_lnet.ln_mt_state = LNET_MT_STATE_RUNNING;
3770 lnet_net_unlock(LNET_LOCK_EX);
3771 task = kthread_run(lnet_monitor_thread, NULL, "monitor_thread");
3774 CERROR("Can't start monitor thread: %d\n", rc);
3781 lnet_net_lock(LNET_LOCK_EX);
3782 the_lnet.ln_mt_state = LNET_MT_STATE_STOPPING;
3783 lnet_net_unlock(LNET_LOCK_EX);
3784 /* block until event callback signals exit */
3785 down(&the_lnet.ln_mt_signal);
3787 lnet_net_lock(LNET_LOCK_EX);
3788 the_lnet.ln_mt_state = LNET_MT_STATE_SHUTDOWN;
3789 lnet_net_unlock(LNET_LOCK_EX);
3790 lnet_rsp_tracker_clean();
3791 lnet_clean_local_ni_recoveryq();
3792 lnet_clean_peer_ni_recoveryq();
3793 lnet_clean_resendqs();
3794 LNetInvalidateEQHandle(&the_lnet.ln_mt_eqh);
3797 lnet_rsp_tracker_clean();
3798 lnet_clean_local_ni_recoveryq();
3799 lnet_clean_peer_ni_recoveryq();
3800 lnet_clean_resendqs();
3804 void lnet_monitor_thr_stop(void)
3806 if (the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN)
3809 LASSERT(the_lnet.ln_mt_state == LNET_MT_STATE_RUNNING);
3810 lnet_net_lock(LNET_LOCK_EX);
3811 the_lnet.ln_mt_state = LNET_MT_STATE_STOPPING;
3812 lnet_net_unlock(LNET_LOCK_EX);
3814 /* tell the monitor thread that we're shutting down */
3815 complete(&the_lnet.ln_mt_wait_complete);
3817 /* block until monitor thread signals that it's done */
3818 down(&the_lnet.ln_mt_signal);
3819 LASSERT(the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN);
3821 /* perform cleanup tasks */
3822 lnet_rsp_tracker_clean();
3823 lnet_clean_local_ni_recoveryq();
3824 lnet_clean_peer_ni_recoveryq();
3825 lnet_clean_resendqs();
3829 lnet_drop_message(struct lnet_ni *ni, int cpt, void *private, unsigned int nob,
3833 lnet_incr_stats(&ni->ni_stats, msg_type, LNET_STATS_TYPE_DROP);
3834 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
3835 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length += nob;
3836 lnet_net_unlock(cpt);
3838 lnet_ni_recv(ni, private, NULL, 0, 0, 0, nob);
3842 lnet_recv_put(struct lnet_ni *ni, struct lnet_msg *msg)
3844 struct lnet_hdr *hdr = &msg->msg_hdr;
3846 if (msg->msg_wanted != 0)
3847 lnet_setpayloadbuffer(msg);
3849 lnet_build_msg_event(msg, LNET_EVENT_PUT);
3851 /* Must I ACK? If so I'll grab the ack_wmd out of the header and put
3852 * it back into the ACK during lnet_finalize() */
3853 msg->msg_ack = (!lnet_is_wire_handle_none(&hdr->msg.put.ack_wmd) &&
3854 (msg->msg_md->md_options & LNET_MD_ACK_DISABLE) == 0);
3856 lnet_ni_recv(ni, msg->msg_private, msg, msg->msg_rx_delayed,
3857 msg->msg_offset, msg->msg_wanted, hdr->payload_length);
3861 lnet_parse_put(struct lnet_ni *ni, struct lnet_msg *msg)
3863 struct lnet_hdr *hdr = &msg->msg_hdr;
3864 struct lnet_match_info info;
3868 /* Convert put fields to host byte order */
3869 hdr->msg.put.match_bits = le64_to_cpu(hdr->msg.put.match_bits);
3870 hdr->msg.put.ptl_index = le32_to_cpu(hdr->msg.put.ptl_index);
3871 hdr->msg.put.offset = le32_to_cpu(hdr->msg.put.offset);
3873 /* Primary peer NID. */
3874 info.mi_id.nid = msg->msg_initiator;
3875 info.mi_id.pid = hdr->src_pid;
3876 info.mi_opc = LNET_MD_OP_PUT;
3877 info.mi_portal = hdr->msg.put.ptl_index;
3878 info.mi_rlength = hdr->payload_length;
3879 info.mi_roffset = hdr->msg.put.offset;
3880 info.mi_mbits = hdr->msg.put.match_bits;
3881 info.mi_cpt = lnet_cpt_of_nid(msg->msg_initiator, ni);
3883 msg->msg_rx_ready_delay = ni->ni_net->net_lnd->lnd_eager_recv == NULL;
3884 ready_delay = msg->msg_rx_ready_delay;
3887 rc = lnet_ptl_match_md(&info, msg);
3892 case LNET_MATCHMD_OK:
3893 lnet_recv_put(ni, msg);
3896 case LNET_MATCHMD_NONE:
3898 /* no eager_recv or has already called it, should
3899 * have been attached on delayed list */
3902 rc = lnet_ni_eager_recv(ni, msg);
3909 case LNET_MATCHMD_DROP:
3910 CNETERR("Dropping PUT from %s portal %d match %llu"
3911 " offset %d length %d: %d\n",
3912 libcfs_id2str(info.mi_id), info.mi_portal,
3913 info.mi_mbits, info.mi_roffset, info.mi_rlength, rc);
3915 return -ENOENT; /* -ve: OK but no match */
3920 lnet_parse_get(struct lnet_ni *ni, struct lnet_msg *msg, int rdma_get)
3922 struct lnet_match_info info;
3923 struct lnet_hdr *hdr = &msg->msg_hdr;
3924 struct lnet_process_id source_id;
3925 struct lnet_handle_wire reply_wmd;
3928 /* Convert get fields to host byte order */
3929 hdr->msg.get.match_bits = le64_to_cpu(hdr->msg.get.match_bits);
3930 hdr->msg.get.ptl_index = le32_to_cpu(hdr->msg.get.ptl_index);
3931 hdr->msg.get.sink_length = le32_to_cpu(hdr->msg.get.sink_length);
3932 hdr->msg.get.src_offset = le32_to_cpu(hdr->msg.get.src_offset);
3934 source_id.nid = hdr->src_nid;
3935 source_id.pid = hdr->src_pid;
3936 /* Primary peer NID */
3937 info.mi_id.nid = msg->msg_initiator;
3938 info.mi_id.pid = hdr->src_pid;
3939 info.mi_opc = LNET_MD_OP_GET;
3940 info.mi_portal = hdr->msg.get.ptl_index;
3941 info.mi_rlength = hdr->msg.get.sink_length;
3942 info.mi_roffset = hdr->msg.get.src_offset;
3943 info.mi_mbits = hdr->msg.get.match_bits;
3944 info.mi_cpt = lnet_cpt_of_nid(msg->msg_initiator, ni);
3946 rc = lnet_ptl_match_md(&info, msg);
3947 if (rc == LNET_MATCHMD_DROP) {
3948 CNETERR("Dropping GET from %s portal %d match %llu"
3949 " offset %d length %d\n",
3950 libcfs_id2str(info.mi_id), info.mi_portal,
3951 info.mi_mbits, info.mi_roffset, info.mi_rlength);
3952 return -ENOENT; /* -ve: OK but no match */
3955 LASSERT(rc == LNET_MATCHMD_OK);
3957 lnet_build_msg_event(msg, LNET_EVENT_GET);
3959 reply_wmd = hdr->msg.get.return_wmd;
3961 lnet_prep_send(msg, LNET_MSG_REPLY, source_id,
3962 msg->msg_offset, msg->msg_wanted);
3964 msg->msg_hdr.msg.reply.dst_wmd = reply_wmd;
3967 /* The LND completes the REPLY from her recv procedure */
3968 lnet_ni_recv(ni, msg->msg_private, msg, 0,
3969 msg->msg_offset, msg->msg_len, msg->msg_len);
3973 lnet_ni_recv(ni, msg->msg_private, NULL, 0, 0, 0, 0);
3974 msg->msg_receiving = 0;
3976 rc = lnet_send(ni->ni_nid, msg, msg->msg_from);
3978 /* didn't get as far as lnet_ni_send() */
3979 CERROR("%s: Unable to send REPLY for GET from %s: %d\n",
3980 libcfs_nid2str(ni->ni_nid),
3981 libcfs_id2str(info.mi_id), rc);
3983 lnet_finalize(msg, rc);
3990 lnet_parse_reply(struct lnet_ni *ni, struct lnet_msg *msg)
3992 void *private = msg->msg_private;
3993 struct lnet_hdr *hdr = &msg->msg_hdr;
3994 struct lnet_process_id src = {0};
3995 struct lnet_libmd *md;
4000 cpt = lnet_cpt_of_cookie(hdr->msg.reply.dst_wmd.wh_object_cookie);
4003 src.nid = hdr->src_nid;
4004 src.pid = hdr->src_pid;
4006 /* NB handles only looked up by creator (no flips) */
4007 md = lnet_wire_handle2md(&hdr->msg.reply.dst_wmd);
4008 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
4009 CNETERR("%s: Dropping REPLY from %s for %s "
4011 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
4012 (md == NULL) ? "invalid" : "inactive",
4013 hdr->msg.reply.dst_wmd.wh_interface_cookie,
4014 hdr->msg.reply.dst_wmd.wh_object_cookie);
4015 if (md != NULL && md->md_me != NULL)
4016 CERROR("REPLY MD also attached to portal %d\n",
4017 md->md_me->me_portal);
4019 lnet_res_unlock(cpt);
4020 return -ENOENT; /* -ve: OK but no match */
4023 LASSERT(md->md_offset == 0);
4025 rlength = hdr->payload_length;
4026 mlength = MIN(rlength, (int)md->md_length);
4028 if (mlength < rlength &&
4029 (md->md_options & LNET_MD_TRUNCATE) == 0) {
4030 CNETERR("%s: Dropping REPLY from %s length %d "
4031 "for MD %#llx would overflow (%d)\n",
4032 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
4033 rlength, hdr->msg.reply.dst_wmd.wh_object_cookie,
4035 lnet_res_unlock(cpt);
4036 return -ENOENT; /* -ve: OK but no match */
4039 CDEBUG(D_NET, "%s: Reply from %s of length %d/%d into md %#llx\n",
4040 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
4041 mlength, rlength, hdr->msg.reply.dst_wmd.wh_object_cookie);
4043 lnet_msg_attach_md(msg, md, 0, mlength);
4046 lnet_setpayloadbuffer(msg);
4048 lnet_res_unlock(cpt);
4050 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
4052 lnet_ni_recv(ni, private, msg, 0, 0, mlength, rlength);
4057 lnet_parse_ack(struct lnet_ni *ni, struct lnet_msg *msg)
4059 struct lnet_hdr *hdr = &msg->msg_hdr;
4060 struct lnet_process_id src = {0};
4061 struct lnet_libmd *md;
4064 src.nid = hdr->src_nid;
4065 src.pid = hdr->src_pid;
4067 /* Convert ack fields to host byte order */
4068 hdr->msg.ack.match_bits = le64_to_cpu(hdr->msg.ack.match_bits);
4069 hdr->msg.ack.mlength = le32_to_cpu(hdr->msg.ack.mlength);
4071 cpt = lnet_cpt_of_cookie(hdr->msg.ack.dst_wmd.wh_object_cookie);
4074 /* NB handles only looked up by creator (no flips) */
4075 md = lnet_wire_handle2md(&hdr->msg.ack.dst_wmd);
4076 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
4077 /* Don't moan; this is expected */
4079 "%s: Dropping ACK from %s to %s MD %#llx.%#llx\n",
4080 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
4081 (md == NULL) ? "invalid" : "inactive",
4082 hdr->msg.ack.dst_wmd.wh_interface_cookie,
4083 hdr->msg.ack.dst_wmd.wh_object_cookie);
4084 if (md != NULL && md->md_me != NULL)
4085 CERROR("Source MD also attached to portal %d\n",
4086 md->md_me->me_portal);
4088 lnet_res_unlock(cpt);
4089 return -ENOENT; /* -ve! */
4092 CDEBUG(D_NET, "%s: ACK from %s into md %#llx\n",
4093 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
4094 hdr->msg.ack.dst_wmd.wh_object_cookie);
4096 lnet_msg_attach_md(msg, md, 0, 0);
4098 lnet_res_unlock(cpt);
4100 lnet_build_msg_event(msg, LNET_EVENT_ACK);
4102 lnet_ni_recv(ni, msg->msg_private, msg, 0, 0, 0, msg->msg_len);
4107 * \retval LNET_CREDIT_OK If \a msg is forwarded
4108 * \retval LNET_CREDIT_WAIT If \a msg is blocked because w/o buffer
4109 * \retval -ve error code
4112 lnet_parse_forward_locked(struct lnet_ni *ni, struct lnet_msg *msg)
4116 if (!the_lnet.ln_routing)
4119 if (msg->msg_rxpeer->lpni_rtrcredits <= 0 ||
4120 lnet_msg2bufpool(msg)->rbp_credits <= 0) {
4121 if (ni->ni_net->net_lnd->lnd_eager_recv == NULL) {
4122 msg->msg_rx_ready_delay = 1;
4124 lnet_net_unlock(msg->msg_rx_cpt);
4125 rc = lnet_ni_eager_recv(ni, msg);
4126 lnet_net_lock(msg->msg_rx_cpt);
4131 rc = lnet_post_routed_recv_locked(msg, 0);
4136 lnet_parse_local(struct lnet_ni *ni, struct lnet_msg *msg)
4140 switch (msg->msg_type) {
4142 rc = lnet_parse_ack(ni, msg);
4145 rc = lnet_parse_put(ni, msg);
4148 rc = lnet_parse_get(ni, msg, msg->msg_rdma_get);
4150 case LNET_MSG_REPLY:
4151 rc = lnet_parse_reply(ni, msg);
4153 default: /* prevent an unused label if !kernel */
4158 LASSERT(rc == 0 || rc == -ENOENT);
4163 lnet_msgtyp2str (int type)
4172 case LNET_MSG_REPLY:
4174 case LNET_MSG_HELLO:
4177 return ("<UNKNOWN>");
4182 lnet_print_hdr(struct lnet_hdr *hdr)
4184 struct lnet_process_id src = {
4185 .nid = hdr->src_nid,
4186 .pid = hdr->src_pid,
4188 struct lnet_process_id dst = {
4189 .nid = hdr->dest_nid,
4190 .pid = hdr->dest_pid,
4192 char *type_str = lnet_msgtyp2str(hdr->type);
4194 CWARN("P3 Header at %p of type %s\n", hdr, type_str);
4195 CWARN(" From %s\n", libcfs_id2str(src));
4196 CWARN(" To %s\n", libcfs_id2str(dst));
4198 switch (hdr->type) {
4203 CWARN(" Ptl index %d, ack md %#llx.%#llx, "
4204 "match bits %llu\n",
4205 hdr->msg.put.ptl_index,
4206 hdr->msg.put.ack_wmd.wh_interface_cookie,
4207 hdr->msg.put.ack_wmd.wh_object_cookie,
4208 hdr->msg.put.match_bits);
4209 CWARN(" Length %d, offset %d, hdr data %#llx\n",
4210 hdr->payload_length, hdr->msg.put.offset,
4211 hdr->msg.put.hdr_data);
4215 CWARN(" Ptl index %d, return md %#llx.%#llx, "
4216 "match bits %llu\n", hdr->msg.get.ptl_index,
4217 hdr->msg.get.return_wmd.wh_interface_cookie,
4218 hdr->msg.get.return_wmd.wh_object_cookie,
4219 hdr->msg.get.match_bits);
4220 CWARN(" Length %d, src offset %d\n",
4221 hdr->msg.get.sink_length,
4222 hdr->msg.get.src_offset);
4226 CWARN(" dst md %#llx.%#llx, "
4227 "manipulated length %d\n",
4228 hdr->msg.ack.dst_wmd.wh_interface_cookie,
4229 hdr->msg.ack.dst_wmd.wh_object_cookie,
4230 hdr->msg.ack.mlength);
4233 case LNET_MSG_REPLY:
4234 CWARN(" dst md %#llx.%#llx, "
4236 hdr->msg.reply.dst_wmd.wh_interface_cookie,
4237 hdr->msg.reply.dst_wmd.wh_object_cookie,
4238 hdr->payload_length);
4244 lnet_parse(struct lnet_ni *ni, struct lnet_hdr *hdr, lnet_nid_t from_nid,
4245 void *private, int rdma_req)
4247 struct lnet_peer_ni *lpni;
4248 struct lnet_msg *msg;
4249 __u32 payload_length;
4250 lnet_pid_t dest_pid;
4251 lnet_nid_t dest_nid;
4259 LASSERT (!in_interrupt ());
4261 type = le32_to_cpu(hdr->type);
4262 src_nid = le64_to_cpu(hdr->src_nid);
4263 dest_nid = le64_to_cpu(hdr->dest_nid);
4264 dest_pid = le32_to_cpu(hdr->dest_pid);
4265 payload_length = le32_to_cpu(hdr->payload_length);
4267 for_me = (ni->ni_nid == dest_nid);
4268 cpt = lnet_cpt_of_nid(from_nid, ni);
4270 CDEBUG(D_NET, "TRACE: %s(%s) <- %s : %s - %s\n",
4271 libcfs_nid2str(dest_nid),
4272 libcfs_nid2str(ni->ni_nid),
4273 libcfs_nid2str(src_nid),
4274 lnet_msgtyp2str(type),
4275 (for_me) ? "for me" : "routed");
4280 if (payload_length > 0) {
4281 CERROR("%s, src %s: bad %s payload %d (0 expected)\n",
4282 libcfs_nid2str(from_nid),
4283 libcfs_nid2str(src_nid),
4284 lnet_msgtyp2str(type), payload_length);
4290 case LNET_MSG_REPLY:
4291 if (payload_length >
4292 (__u32)(for_me ? LNET_MAX_PAYLOAD : LNET_MTU)) {
4293 CERROR("%s, src %s: bad %s payload %d "
4294 "(%d max expected)\n",
4295 libcfs_nid2str(from_nid),
4296 libcfs_nid2str(src_nid),
4297 lnet_msgtyp2str(type),
4299 for_me ? LNET_MAX_PAYLOAD : LNET_MTU);
4305 CERROR("%s, src %s: Bad message type 0x%x\n",
4306 libcfs_nid2str(from_nid),
4307 libcfs_nid2str(src_nid), type);
4311 if (the_lnet.ln_routing &&
4312 ni->ni_net->net_last_alive != ktime_get_real_seconds()) {
4314 spin_lock(&ni->ni_net->net_lock);
4315 ni->ni_net->net_last_alive = ktime_get_real_seconds();
4316 spin_unlock(&ni->ni_net->net_lock);
4317 if (ni->ni_status != NULL &&
4318 ni->ni_status->ns_status == LNET_NI_STATUS_DOWN) {
4319 ni->ni_status->ns_status = LNET_NI_STATUS_UP;
4326 lnet_push_update_to_peers(1);
4328 /* Regard a bad destination NID as a protocol error. Senders should
4329 * know what they're doing; if they don't they're misconfigured, buggy
4330 * or malicious so we chop them off at the knees :) */
4333 if (LNET_NIDNET(dest_nid) == LNET_NIDNET(ni->ni_nid)) {
4334 /* should have gone direct */
4335 CERROR("%s, src %s: Bad dest nid %s "
4336 "(should have been sent direct)\n",
4337 libcfs_nid2str(from_nid),
4338 libcfs_nid2str(src_nid),
4339 libcfs_nid2str(dest_nid));
4343 if (lnet_islocalnid(dest_nid)) {
4344 /* dest is another local NI; sender should have used
4345 * this node's NID on its own network */
4346 CERROR("%s, src %s: Bad dest nid %s "
4347 "(it's my nid but on a different network)\n",
4348 libcfs_nid2str(from_nid),
4349 libcfs_nid2str(src_nid),
4350 libcfs_nid2str(dest_nid));
4354 if (rdma_req && type == LNET_MSG_GET) {
4355 CERROR("%s, src %s: Bad optimized GET for %s "
4356 "(final destination must be me)\n",
4357 libcfs_nid2str(from_nid),
4358 libcfs_nid2str(src_nid),
4359 libcfs_nid2str(dest_nid));
4363 if (!the_lnet.ln_routing) {
4364 CERROR("%s, src %s: Dropping message for %s "
4365 "(routing not enabled)\n",
4366 libcfs_nid2str(from_nid),
4367 libcfs_nid2str(src_nid),
4368 libcfs_nid2str(dest_nid));
4373 /* Message looks OK; we're not going to return an error, so we MUST
4374 * call back lnd_recv() come what may... */
4376 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
4377 fail_peer(src_nid, 0)) { /* shall we now? */
4378 CERROR("%s, src %s: Dropping %s to simulate failure\n",
4379 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4380 lnet_msgtyp2str(type));
4384 if (!list_empty(&the_lnet.ln_drop_rules) &&
4385 lnet_drop_rule_match(hdr, ni->ni_nid, NULL)) {
4386 CDEBUG(D_NET, "%s, src %s, dst %s: Dropping %s to simulate"
4387 "silent message loss\n",
4388 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4389 libcfs_nid2str(dest_nid), lnet_msgtyp2str(type));
4393 if (lnet_drop_asym_route && for_me &&
4394 LNET_NIDNET(src_nid) != LNET_NIDNET(from_nid)) {
4395 struct lnet_net *net;
4396 struct lnet_remotenet *rnet;
4399 /* we are dealing with a routed message,
4400 * so see if route to reach src_nid goes through from_nid
4403 net = lnet_get_net_locked(LNET_NIDNET(ni->ni_nid));
4405 lnet_net_unlock(cpt);
4406 CERROR("net %s not found\n",
4407 libcfs_net2str(LNET_NIDNET(ni->ni_nid)));
4411 rnet = lnet_find_rnet_locked(LNET_NIDNET(src_nid));
4413 struct lnet_peer *gw = NULL;
4414 struct lnet_peer_ni *lpni = NULL;
4415 struct lnet_route *route;
4417 list_for_each_entry(route, &rnet->lrn_routes, lr_list) {
4419 gw = route->lr_gateway;
4420 if (route->lr_lnet != net->net_id)
4423 * if the nid is one of the gateway's NIDs
4424 * then this is a valid gateway
4426 while ((lpni = lnet_get_next_peer_ni_locked(gw,
4427 NULL, lpni)) != NULL) {
4428 if (lpni->lpni_nid == from_nid) {
4435 lnet_net_unlock(cpt);
4437 /* we would not use from_nid to route a message to
4439 * => asymmetric routing detected but forbidden
4441 CERROR("%s, src %s: Dropping asymmetrical route %s\n",
4442 libcfs_nid2str(from_nid),
4443 libcfs_nid2str(src_nid), lnet_msgtyp2str(type));
4448 msg = lnet_msg_alloc();
4450 CERROR("%s, src %s: Dropping %s (out of memory)\n",
4451 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4452 lnet_msgtyp2str(type));
4456 /* msg zeroed in lnet_msg_alloc; i.e. flags all clear,
4457 * pointers NULL etc */
4459 msg->msg_type = type;
4460 msg->msg_private = private;
4461 msg->msg_receiving = 1;
4462 msg->msg_rdma_get = rdma_req;
4463 msg->msg_len = msg->msg_wanted = payload_length;
4464 msg->msg_offset = 0;
4465 msg->msg_hdr = *hdr;
4466 /* for building message event */
4467 msg->msg_from = from_nid;
4469 msg->msg_target.pid = dest_pid;
4470 msg->msg_target.nid = dest_nid;
4471 msg->msg_routing = 1;
4474 /* convert common msg->hdr fields to host byteorder */
4475 msg->msg_hdr.type = type;
4476 msg->msg_hdr.src_nid = src_nid;
4477 msg->msg_hdr.src_pid = le32_to_cpu(msg->msg_hdr.src_pid);
4478 msg->msg_hdr.dest_nid = dest_nid;
4479 msg->msg_hdr.dest_pid = dest_pid;
4480 msg->msg_hdr.payload_length = payload_length;
4484 lpni = lnet_nid2peerni_locked(from_nid, ni->ni_nid, cpt);
4486 lnet_net_unlock(cpt);
4487 CERROR("%s, src %s: Dropping %s "
4488 "(error %ld looking up sender)\n",
4489 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4490 lnet_msgtyp2str(type), PTR_ERR(lpni));
4492 if (rc == -ESHUTDOWN)
4493 /* We are shutting down. Don't do anything more */
4498 if (the_lnet.ln_routing)
4499 lpni->lpni_last_alive = ktime_get_seconds();
4501 msg->msg_rxpeer = lpni;
4503 lnet_ni_addref_locked(ni, cpt);
4504 /* Multi-Rail: Primary NID of source. */
4505 msg->msg_initiator = lnet_peer_primary_nid_locked(src_nid);
4508 * mark the status of this lpni as UP since we received a message
4509 * from it. The ping response reports back the ns_status which is
4510 * marked on the remote as up or down and we cache it here.
4512 msg->msg_rxpeer->lpni_ns_status = LNET_NI_STATUS_UP;
4514 lnet_msg_commit(msg, cpt);
4516 /* message delay simulation */
4517 if (unlikely(!list_empty(&the_lnet.ln_delay_rules) &&
4518 lnet_delay_rule_match_locked(hdr, msg))) {
4519 lnet_net_unlock(cpt);
4524 rc = lnet_parse_forward_locked(ni, msg);
4525 lnet_net_unlock(cpt);
4530 if (rc == LNET_CREDIT_OK) {
4531 lnet_ni_recv(ni, msg->msg_private, msg, 0,
4532 0, payload_length, payload_length);
4537 lnet_net_unlock(cpt);
4539 rc = lnet_parse_local(ni, msg);
4545 LASSERT(msg->msg_md == NULL);
4546 lnet_finalize(msg, rc);
4549 lnet_drop_message(ni, cpt, private, payload_length, type);
4552 EXPORT_SYMBOL(lnet_parse);
4555 lnet_drop_delayed_msg_list(struct list_head *head, char *reason)
4557 while (!list_empty(head)) {
4558 struct lnet_process_id id = {0};
4559 struct lnet_msg *msg;
4561 msg = list_entry(head->next, struct lnet_msg, msg_list);
4562 list_del(&msg->msg_list);
4564 id.nid = msg->msg_hdr.src_nid;
4565 id.pid = msg->msg_hdr.src_pid;
4567 LASSERT(msg->msg_md == NULL);
4568 LASSERT(msg->msg_rx_delayed);
4569 LASSERT(msg->msg_rxpeer != NULL);
4570 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
4572 CWARN("Dropping delayed PUT from %s portal %d match %llu"
4573 " offset %d length %d: %s\n",
4575 msg->msg_hdr.msg.put.ptl_index,
4576 msg->msg_hdr.msg.put.match_bits,
4577 msg->msg_hdr.msg.put.offset,
4578 msg->msg_hdr.payload_length, reason);
4580 /* NB I can't drop msg's ref on msg_rxpeer until after I've
4581 * called lnet_drop_message(), so I just hang onto msg as well
4582 * until that's done */
4584 lnet_drop_message(msg->msg_rxni, msg->msg_rx_cpt,
4585 msg->msg_private, msg->msg_len,
4588 msg->msg_no_resend = true;
4590 * NB: message will not generate event because w/o attached MD,
4591 * but we still should give error code so lnet_msg_decommit()
4592 * can skip counters operations and other checks.
4594 lnet_finalize(msg, -ENOENT);
4599 lnet_recv_delayed_msg_list(struct list_head *head)
4601 while (!list_empty(head)) {
4602 struct lnet_msg *msg;
4603 struct lnet_process_id id;
4605 msg = list_entry(head->next, struct lnet_msg, msg_list);
4606 list_del(&msg->msg_list);
4608 /* md won't disappear under me, since each msg
4609 * holds a ref on it */
4611 id.nid = msg->msg_hdr.src_nid;
4612 id.pid = msg->msg_hdr.src_pid;
4614 LASSERT(msg->msg_rx_delayed);
4615 LASSERT(msg->msg_md != NULL);
4616 LASSERT(msg->msg_rxpeer != NULL);
4617 LASSERT(msg->msg_rxni != NULL);
4618 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
4620 CDEBUG(D_NET, "Resuming delayed PUT from %s portal %d "
4621 "match %llu offset %d length %d.\n",
4622 libcfs_id2str(id), msg->msg_hdr.msg.put.ptl_index,
4623 msg->msg_hdr.msg.put.match_bits,
4624 msg->msg_hdr.msg.put.offset,
4625 msg->msg_hdr.payload_length);
4627 lnet_recv_put(msg->msg_rxni, msg);
4632 lnet_attach_rsp_tracker(struct lnet_rsp_tracker *rspt, int cpt,
4633 struct lnet_libmd *md, struct lnet_handle_md mdh)
4636 bool new_entry = true;
4637 struct lnet_rsp_tracker *local_rspt;
4640 * MD has a refcount taken by message so it's not going away.
4641 * The MD however can be looked up. We need to secure the access
4642 * to the md_rspt_ptr by taking the res_lock.
4643 * The rspt can be accessed without protection up to when it gets
4644 * added to the list.
4648 local_rspt = md->md_rspt_ptr;
4649 timeout_ns = lnet_transaction_timeout * NSEC_PER_SEC;
4650 if (local_rspt != NULL) {
4652 * we already have an rspt attached to the md, so we'll
4653 * update the deadline on that one.
4655 LIBCFS_FREE(rspt, sizeof(*rspt));
4659 rspt->rspt_mdh = mdh;
4660 rspt->rspt_cpt = cpt;
4661 /* store the rspt so we can access it when we get the REPLY */
4662 md->md_rspt_ptr = rspt;
4665 local_rspt->rspt_deadline = ktime_add_ns(ktime_get(), timeout_ns);
4668 * add to the list of tracked responses. It's added to tail of the
4669 * list in order to expire all the older entries first.
4672 if (!new_entry && !list_empty(&local_rspt->rspt_on_list))
4673 list_del_init(&local_rspt->rspt_on_list);
4674 list_add_tail(&local_rspt->rspt_on_list, the_lnet.ln_mt_rstq[cpt]);
4675 lnet_net_unlock(cpt);
4676 lnet_res_unlock(cpt);
4680 * Initiate an asynchronous PUT operation.
4682 * There are several events associated with a PUT: completion of the send on
4683 * the initiator node (LNET_EVENT_SEND), and when the send completes
4684 * successfully, the receipt of an acknowledgment (LNET_EVENT_ACK) indicating
4685 * that the operation was accepted by the target. The event LNET_EVENT_PUT is
4686 * used at the target node to indicate the completion of incoming data
4689 * The local events will be logged in the EQ associated with the MD pointed to
4690 * by \a mdh handle. Using a MD without an associated EQ results in these
4691 * events being discarded. In this case, the caller must have another
4692 * mechanism (e.g., a higher level protocol) for determining when it is safe
4693 * to modify the memory region associated with the MD.
4695 * Note that LNet does not guarantee the order of LNET_EVENT_SEND and
4696 * LNET_EVENT_ACK, though intuitively ACK should happen after SEND.
4698 * \param self Indicates the NID of a local interface through which to send
4699 * the PUT request. Use LNET_NID_ANY to let LNet choose one by itself.
4700 * \param mdh A handle for the MD that describes the memory to be sent. The MD
4701 * must be "free floating" (See LNetMDBind()).
4702 * \param ack Controls whether an acknowledgment is requested.
4703 * Acknowledgments are only sent when they are requested by the initiating
4704 * process and the target MD enables them.
4705 * \param target A process identifier for the target process.
4706 * \param portal The index in the \a target's portal table.
4707 * \param match_bits The match bits to use for MD selection at the target
4709 * \param offset The offset into the target MD (only used when the target
4710 * MD has the LNET_MD_MANAGE_REMOTE option set).
4711 * \param hdr_data 64 bits of user data that can be included in the message
4712 * header. This data is written to an event queue entry at the target if an
4713 * EQ is present on the matching MD.
4715 * \retval 0 Success, and only in this case events will be generated
4716 * and logged to EQ (if it exists).
4717 * \retval -EIO Simulated failure.
4718 * \retval -ENOMEM Memory allocation failure.
4719 * \retval -ENOENT Invalid MD object.
4721 * \see struct lnet_event::hdr_data and lnet_event_kind_t.
4724 LNetPut(lnet_nid_t self, struct lnet_handle_md mdh, enum lnet_ack_req ack,
4725 struct lnet_process_id target, unsigned int portal,
4726 __u64 match_bits, unsigned int offset,
4729 struct lnet_msg *msg;
4730 struct lnet_libmd *md;
4733 struct lnet_rsp_tracker *rspt = NULL;
4735 LASSERT(the_lnet.ln_refcount > 0);
4737 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
4738 fail_peer(target.nid, 1)) { /* shall we now? */
4739 CERROR("Dropping PUT to %s: simulated failure\n",
4740 libcfs_id2str(target));
4744 msg = lnet_msg_alloc();
4746 CERROR("Dropping PUT to %s: ENOMEM on struct lnet_msg\n",
4747 libcfs_id2str(target));
4750 msg->msg_vmflush = !!memory_pressure_get();
4752 cpt = lnet_cpt_of_cookie(mdh.cookie);
4754 if (ack == LNET_ACK_REQ) {
4755 rspt = lnet_rspt_alloc(cpt);
4757 CERROR("Dropping PUT to %s: ENOMEM on response tracker\n",
4758 libcfs_id2str(target));
4761 INIT_LIST_HEAD(&rspt->rspt_on_list);
4766 md = lnet_handle2md(&mdh);
4767 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
4768 CERROR("Dropping PUT (%llu:%d:%s): MD (%d) invalid\n",
4769 match_bits, portal, libcfs_id2str(target),
4770 md == NULL ? -1 : md->md_threshold);
4771 if (md != NULL && md->md_me != NULL)
4772 CERROR("Source MD also attached to portal %d\n",
4773 md->md_me->me_portal);
4774 lnet_res_unlock(cpt);
4776 LIBCFS_FREE(rspt, sizeof(*rspt));
4781 CDEBUG(D_NET, "LNetPut -> %s\n", libcfs_id2str(target));
4783 lnet_msg_attach_md(msg, md, 0, 0);
4785 lnet_prep_send(msg, LNET_MSG_PUT, target, 0, md->md_length);
4787 msg->msg_hdr.msg.put.match_bits = cpu_to_le64(match_bits);
4788 msg->msg_hdr.msg.put.ptl_index = cpu_to_le32(portal);
4789 msg->msg_hdr.msg.put.offset = cpu_to_le32(offset);
4790 msg->msg_hdr.msg.put.hdr_data = hdr_data;
4792 /* NB handles only looked up by creator (no flips) */
4793 if (ack == LNET_ACK_REQ) {
4794 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
4795 the_lnet.ln_interface_cookie;
4796 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
4797 md->md_lh.lh_cookie;
4799 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
4800 LNET_WIRE_HANDLE_COOKIE_NONE;
4801 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
4802 LNET_WIRE_HANDLE_COOKIE_NONE;
4805 lnet_res_unlock(cpt);
4807 lnet_build_msg_event(msg, LNET_EVENT_SEND);
4809 if (ack == LNET_ACK_REQ)
4810 lnet_attach_rsp_tracker(rspt, cpt, md, mdh);
4812 if (CFS_FAIL_CHECK_ORSET(CFS_FAIL_PTLRPC_OST_BULK_CB2,
4816 rc = lnet_send(self, msg, LNET_NID_ANY);
4819 CNETERR("Error sending PUT to %s: %d\n",
4820 libcfs_id2str(target), rc);
4821 msg->msg_no_resend = true;
4822 lnet_finalize(msg, rc);
4825 /* completion will be signalled by an event */
4828 EXPORT_SYMBOL(LNetPut);
4831 * The LND can DMA direct to the GET md (i.e. no REPLY msg). This
4832 * returns a msg for the LND to pass to lnet_finalize() when the sink
4833 * data has been received.
4835 * CAVEAT EMPTOR: 'getmsg' is the original GET, which is freed when
4836 * lnet_finalize() is called on it, so the LND must call this first
4839 lnet_create_reply_msg(struct lnet_ni *ni, struct lnet_msg *getmsg)
4841 struct lnet_msg *msg = lnet_msg_alloc();
4842 struct lnet_libmd *getmd = getmsg->msg_md;
4843 struct lnet_process_id peer_id = getmsg->msg_target;
4846 LASSERT(!getmsg->msg_target_is_router);
4847 LASSERT(!getmsg->msg_routing);
4850 CERROR("%s: Dropping REPLY from %s: can't allocate msg\n",
4851 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id));
4855 cpt = lnet_cpt_of_cookie(getmd->md_lh.lh_cookie);
4858 LASSERT(getmd->md_refcount > 0);
4860 if (getmd->md_threshold == 0) {
4861 CERROR("%s: Dropping REPLY from %s for inactive MD %p\n",
4862 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id),
4864 lnet_res_unlock(cpt);
4868 LASSERT(getmd->md_offset == 0);
4870 CDEBUG(D_NET, "%s: Reply from %s md %p\n",
4871 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id), getmd);
4873 /* setup information for lnet_build_msg_event */
4874 msg->msg_initiator = getmsg->msg_txpeer->lpni_peer_net->lpn_peer->lp_primary_nid;
4875 msg->msg_from = peer_id.nid;
4876 msg->msg_type = LNET_MSG_GET; /* flag this msg as an "optimized" GET */
4877 msg->msg_hdr.src_nid = peer_id.nid;
4878 msg->msg_hdr.payload_length = getmd->md_length;
4879 msg->msg_receiving = 1; /* required by lnet_msg_attach_md */
4881 lnet_msg_attach_md(msg, getmd, getmd->md_offset, getmd->md_length);
4882 lnet_res_unlock(cpt);
4884 cpt = lnet_cpt_of_nid(peer_id.nid, ni);
4887 lnet_msg_commit(msg, cpt);
4888 lnet_net_unlock(cpt);
4890 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
4895 cpt = lnet_cpt_of_nid(peer_id.nid, ni);
4898 lnet_incr_stats(&ni->ni_stats, LNET_MSG_GET, LNET_STATS_TYPE_DROP);
4899 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
4900 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length +=
4902 lnet_net_unlock(cpt);
4909 EXPORT_SYMBOL(lnet_create_reply_msg);
4912 lnet_set_reply_msg_len(struct lnet_ni *ni, struct lnet_msg *reply,
4915 /* Set the REPLY length, now the RDMA that elides the REPLY message has
4916 * completed and I know it. */
4917 LASSERT(reply != NULL);
4918 LASSERT(reply->msg_type == LNET_MSG_GET);
4919 LASSERT(reply->msg_ev.type == LNET_EVENT_REPLY);
4921 /* NB I trusted my peer to RDMA. If she tells me she's written beyond
4922 * the end of my buffer, I might as well be dead. */
4923 LASSERT(len <= reply->msg_ev.mlength);
4925 reply->msg_ev.mlength = len;
4927 EXPORT_SYMBOL(lnet_set_reply_msg_len);
4930 * Initiate an asynchronous GET operation.
4932 * On the initiator node, an LNET_EVENT_SEND is logged when the GET request
4933 * is sent, and an LNET_EVENT_REPLY is logged when the data returned from
4934 * the target node in the REPLY has been written to local MD.
4936 * On the target node, an LNET_EVENT_GET is logged when the GET request
4937 * arrives and is accepted into a MD.
4939 * \param self,target,portal,match_bits,offset See the discussion in LNetPut().
4940 * \param mdh A handle for the MD that describes the memory into which the
4941 * requested data will be received. The MD must be "free floating" (See LNetMDBind()).
4943 * \retval 0 Success, and only in this case events will be generated
4944 * and logged to EQ (if it exists) of the MD.
4945 * \retval -EIO Simulated failure.
4946 * \retval -ENOMEM Memory allocation failure.
4947 * \retval -ENOENT Invalid MD object.
4950 LNetGet(lnet_nid_t self, struct lnet_handle_md mdh,
4951 struct lnet_process_id target, unsigned int portal,
4952 __u64 match_bits, unsigned int offset, bool recovery)
4954 struct lnet_msg *msg;
4955 struct lnet_libmd *md;
4956 struct lnet_rsp_tracker *rspt;
4960 LASSERT(the_lnet.ln_refcount > 0);
4962 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
4963 fail_peer(target.nid, 1)) /* shall we now? */
4965 CERROR("Dropping GET to %s: simulated failure\n",
4966 libcfs_id2str(target));
4970 msg = lnet_msg_alloc();
4972 CERROR("Dropping GET to %s: ENOMEM on struct lnet_msg\n",
4973 libcfs_id2str(target));
4977 cpt = lnet_cpt_of_cookie(mdh.cookie);
4979 rspt = lnet_rspt_alloc(cpt);
4981 CERROR("Dropping GET to %s: ENOMEM on response tracker\n",
4982 libcfs_id2str(target));
4985 INIT_LIST_HEAD(&rspt->rspt_on_list);
4987 msg->msg_recovery = recovery;
4991 md = lnet_handle2md(&mdh);
4992 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
4993 CERROR("Dropping GET (%llu:%d:%s): MD (%d) invalid\n",
4994 match_bits, portal, libcfs_id2str(target),
4995 md == NULL ? -1 : md->md_threshold);
4996 if (md != NULL && md->md_me != NULL)
4997 CERROR("REPLY MD also attached to portal %d\n",
4998 md->md_me->me_portal);
5000 lnet_res_unlock(cpt);
5003 LIBCFS_FREE(rspt, sizeof(*rspt));
5007 CDEBUG(D_NET, "LNetGet -> %s\n", libcfs_id2str(target));
5009 lnet_msg_attach_md(msg, md, 0, 0);
5011 lnet_prep_send(msg, LNET_MSG_GET, target, 0, 0);
5013 msg->msg_hdr.msg.get.match_bits = cpu_to_le64(match_bits);
5014 msg->msg_hdr.msg.get.ptl_index = cpu_to_le32(portal);
5015 msg->msg_hdr.msg.get.src_offset = cpu_to_le32(offset);
5016 msg->msg_hdr.msg.get.sink_length = cpu_to_le32(md->md_length);
5018 /* NB handles only looked up by creator (no flips) */
5019 msg->msg_hdr.msg.get.return_wmd.wh_interface_cookie =
5020 the_lnet.ln_interface_cookie;
5021 msg->msg_hdr.msg.get.return_wmd.wh_object_cookie =
5022 md->md_lh.lh_cookie;
5024 lnet_res_unlock(cpt);
5026 lnet_build_msg_event(msg, LNET_EVENT_SEND);
5028 lnet_attach_rsp_tracker(rspt, cpt, md, mdh);
5030 rc = lnet_send(self, msg, LNET_NID_ANY);
5032 CNETERR("Error sending GET to %s: %d\n",
5033 libcfs_id2str(target), rc);
5034 msg->msg_no_resend = true;
5035 lnet_finalize(msg, rc);
5038 /* completion will be signalled by an event */
5041 EXPORT_SYMBOL(LNetGet);
5044 * Calculate distance to node at \a dstnid.
5046 * \param dstnid Target NID.
5047 * \param srcnidp If not NULL, NID of the local interface to reach \a dstnid
5049 * \param orderp If not NULL, order of the route to reach \a dstnid is saved
5052 * \retval 0 If \a dstnid belongs to a local interface, and reserved option
5053 * local_nid_dist_zero is set, which is the default.
5054 * \retval positives Distance to target NID, i.e. number of hops plus one.
5055 * \retval -EHOSTUNREACH If \a dstnid is not reachable.
5058 LNetDist(lnet_nid_t dstnid, lnet_nid_t *srcnidp, __u32 *orderp)
5060 struct list_head *e;
5061 struct lnet_ni *ni = NULL;
5062 struct lnet_remotenet *rnet;
5063 __u32 dstnet = LNET_NIDNET(dstnid);
5067 struct list_head *rn_list;
5069 /* if !local_nid_dist_zero, I don't return a distance of 0 ever
5070 * (when lustre sees a distance of 0, it substitutes 0@lo), so I
5071 * keep order 0 free for 0@lo and order 1 free for a local NID
5074 LASSERT(the_lnet.ln_refcount > 0);
5076 cpt = lnet_net_lock_current();
5078 while ((ni = lnet_get_next_ni_locked(NULL, ni))) {
5079 if (ni->ni_nid == dstnid) {
5080 if (srcnidp != NULL)
5082 if (orderp != NULL) {
5083 if (LNET_NETTYP(LNET_NIDNET(dstnid)) == LOLND)
5088 lnet_net_unlock(cpt);
5090 return local_nid_dist_zero ? 0 : 1;
5093 if (LNET_NIDNET(ni->ni_nid) == dstnet) {
5094 /* Check if ni was originally created in
5095 * current net namespace.
5096 * If not, assign order above 0xffff0000,
5097 * to make this ni not a priority. */
5098 if (current->nsproxy &&
5099 !net_eq(ni->ni_net_ns, current->nsproxy->net_ns))
5100 order += 0xffff0000;
5101 if (srcnidp != NULL)
5102 *srcnidp = ni->ni_nid;
5105 lnet_net_unlock(cpt);
5112 rn_list = lnet_net2rnethash(dstnet);
5113 list_for_each(e, rn_list) {
5114 rnet = list_entry(e, struct lnet_remotenet, lrn_list);
5116 if (rnet->lrn_net == dstnet) {
5117 struct lnet_route *route;
5118 struct lnet_route *shortest = NULL;
5119 __u32 shortest_hops = LNET_UNDEFINED_HOPS;
5122 LASSERT(!list_empty(&rnet->lrn_routes));
5124 list_for_each_entry(route, &rnet->lrn_routes,
5126 route_hops = route->lr_hops;
5127 if (route_hops == LNET_UNDEFINED_HOPS)
5129 if (shortest == NULL ||
5130 route_hops < shortest_hops) {
5132 shortest_hops = route_hops;
5136 LASSERT(shortest != NULL);
5137 hops = shortest_hops;
5138 if (srcnidp != NULL) {
5139 struct lnet_net *net;
5140 net = lnet_get_net_locked(shortest->lr_lnet);
5142 ni = lnet_get_next_ni_locked(net, NULL);
5143 *srcnidp = ni->ni_nid;
5147 lnet_net_unlock(cpt);
5153 lnet_net_unlock(cpt);
5154 return -EHOSTUNREACH;
5156 EXPORT_SYMBOL(LNetDist);