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 static int local_nid_dist_zero = 1;
46 module_param(local_nid_dist_zero, int, 0444);
47 MODULE_PARM_DESC(local_nid_dist_zero, "Reserved");
49 struct lnet_send_data {
50 struct lnet_ni *sd_best_ni;
51 struct lnet_peer_ni *sd_best_lpni;
52 struct lnet_peer_ni *sd_final_dst_lpni;
53 struct lnet_peer *sd_peer;
54 struct lnet_peer *sd_gw_peer;
55 struct lnet_peer_ni *sd_gw_lpni;
56 struct lnet_peer_net *sd_peer_net;
57 struct lnet_msg *sd_msg;
58 lnet_nid_t sd_dst_nid;
59 lnet_nid_t sd_src_nid;
60 lnet_nid_t sd_rtr_nid;
66 static inline struct lnet_comm_count *
67 get_stats_counts(struct lnet_element_stats *stats,
68 enum lnet_stats_type stats_type)
71 case LNET_STATS_TYPE_SEND:
72 return &stats->el_send_stats;
73 case LNET_STATS_TYPE_RECV:
74 return &stats->el_recv_stats;
75 case LNET_STATS_TYPE_DROP:
76 return &stats->el_drop_stats;
78 CERROR("Unknown stats type\n");
84 void lnet_incr_stats(struct lnet_element_stats *stats,
85 enum lnet_msg_type msg_type,
86 enum lnet_stats_type stats_type)
88 struct lnet_comm_count *counts = get_stats_counts(stats, stats_type);
94 atomic_inc(&counts->co_ack_count);
97 atomic_inc(&counts->co_put_count);
100 atomic_inc(&counts->co_get_count);
103 atomic_inc(&counts->co_reply_count);
106 atomic_inc(&counts->co_hello_count);
109 CERROR("There is a BUG in the code. Unknown message type\n");
114 __u32 lnet_sum_stats(struct lnet_element_stats *stats,
115 enum lnet_stats_type stats_type)
117 struct lnet_comm_count *counts = get_stats_counts(stats, stats_type);
121 return (atomic_read(&counts->co_ack_count) +
122 atomic_read(&counts->co_put_count) +
123 atomic_read(&counts->co_get_count) +
124 atomic_read(&counts->co_reply_count) +
125 atomic_read(&counts->co_hello_count));
128 static inline void assign_stats(struct lnet_ioctl_comm_count *msg_stats,
129 struct lnet_comm_count *counts)
131 msg_stats->ico_get_count = atomic_read(&counts->co_get_count);
132 msg_stats->ico_put_count = atomic_read(&counts->co_put_count);
133 msg_stats->ico_reply_count = atomic_read(&counts->co_reply_count);
134 msg_stats->ico_ack_count = atomic_read(&counts->co_ack_count);
135 msg_stats->ico_hello_count = atomic_read(&counts->co_hello_count);
138 void lnet_usr_translate_stats(struct lnet_ioctl_element_msg_stats *msg_stats,
139 struct lnet_element_stats *stats)
141 struct lnet_comm_count *counts;
146 counts = get_stats_counts(stats, LNET_STATS_TYPE_SEND);
149 assign_stats(&msg_stats->im_send_stats, counts);
151 counts = get_stats_counts(stats, LNET_STATS_TYPE_RECV);
154 assign_stats(&msg_stats->im_recv_stats, counts);
156 counts = get_stats_counts(stats, LNET_STATS_TYPE_DROP);
159 assign_stats(&msg_stats->im_drop_stats, counts);
163 lnet_fail_nid(lnet_nid_t nid, unsigned int threshold)
165 struct lnet_test_peer *tp;
166 struct list_head *el;
167 struct list_head *next;
168 struct list_head cull;
170 /* NB: use lnet_net_lock(0) to serialize operations on test peers */
171 if (threshold != 0) {
172 /* Adding a new entry */
173 LIBCFS_ALLOC(tp, sizeof(*tp));
178 tp->tp_threshold = threshold;
181 list_add_tail(&tp->tp_list, &the_lnet.ln_test_peers);
186 /* removing entries */
187 INIT_LIST_HEAD(&cull);
191 list_for_each_safe(el, next, &the_lnet.ln_test_peers) {
192 tp = list_entry(el, struct lnet_test_peer, tp_list);
194 if (tp->tp_threshold == 0 || /* needs culling anyway */
195 nid == LNET_NID_ANY || /* removing all entries */
196 tp->tp_nid == nid) { /* matched this one */
197 list_del(&tp->tp_list);
198 list_add(&tp->tp_list, &cull);
204 while (!list_empty(&cull)) {
205 tp = list_entry(cull.next, struct lnet_test_peer, tp_list);
207 list_del(&tp->tp_list);
208 LIBCFS_FREE(tp, sizeof(*tp));
214 fail_peer (lnet_nid_t nid, int outgoing)
216 struct lnet_test_peer *tp;
217 struct list_head *el;
218 struct list_head *next;
219 struct list_head cull;
222 INIT_LIST_HEAD(&cull);
224 /* NB: use lnet_net_lock(0) to serialize operations on test peers */
227 list_for_each_safe(el, next, &the_lnet.ln_test_peers) {
228 tp = list_entry(el, struct lnet_test_peer, tp_list);
230 if (tp->tp_threshold == 0) {
233 /* only cull zombies on outgoing tests,
234 * since we may be at interrupt priority on
235 * incoming messages. */
236 list_del(&tp->tp_list);
237 list_add(&tp->tp_list, &cull);
242 if (tp->tp_nid == LNET_NID_ANY || /* fail every peer */
243 nid == tp->tp_nid) { /* fail this peer */
246 if (tp->tp_threshold != LNET_MD_THRESH_INF) {
249 tp->tp_threshold == 0) {
251 list_del(&tp->tp_list);
252 list_add(&tp->tp_list, &cull);
261 while (!list_empty(&cull)) {
262 tp = list_entry(cull.next, struct lnet_test_peer, tp_list);
263 list_del(&tp->tp_list);
265 LIBCFS_FREE(tp, sizeof(*tp));
272 lnet_iov_nob(unsigned int niov, struct kvec *iov)
274 unsigned int nob = 0;
276 LASSERT(niov == 0 || iov != NULL);
278 nob += (iov++)->iov_len;
282 EXPORT_SYMBOL(lnet_iov_nob);
285 lnet_copy_iov2iov(unsigned int ndiov, struct kvec *diov, unsigned int doffset,
286 unsigned int nsiov, struct kvec *siov, unsigned int soffset,
289 /* NB diov, siov are READ-ONLY */
290 unsigned int this_nob;
295 /* skip complete frags before 'doffset' */
297 while (doffset >= diov->iov_len) {
298 doffset -= diov->iov_len;
304 /* skip complete frags before 'soffset' */
306 while (soffset >= siov->iov_len) {
307 soffset -= siov->iov_len;
316 this_nob = MIN(diov->iov_len - doffset,
317 siov->iov_len - soffset);
318 this_nob = MIN(this_nob, nob);
320 memcpy((char *)diov->iov_base + doffset,
321 (char *)siov->iov_base + soffset, this_nob);
324 if (diov->iov_len > doffset + this_nob) {
332 if (siov->iov_len > soffset + this_nob) {
341 EXPORT_SYMBOL(lnet_copy_iov2iov);
344 lnet_extract_iov(int dst_niov, struct kvec *dst,
345 int src_niov, struct kvec *src,
346 unsigned int offset, unsigned int len)
348 /* Initialise 'dst' to the subset of 'src' starting at 'offset',
349 * for exactly 'len' bytes, and return the number of entries.
350 * NB not destructive to 'src' */
351 unsigned int frag_len;
354 if (len == 0) /* no data => */
355 return (0); /* no frags */
357 LASSERT(src_niov > 0);
358 while (offset >= src->iov_len) { /* skip initial frags */
359 offset -= src->iov_len;
362 LASSERT(src_niov > 0);
367 LASSERT(src_niov > 0);
368 LASSERT((int)niov <= dst_niov);
370 frag_len = src->iov_len - offset;
371 dst->iov_base = ((char *)src->iov_base) + offset;
373 if (len <= frag_len) {
378 dst->iov_len = frag_len;
388 EXPORT_SYMBOL(lnet_extract_iov);
392 lnet_kiov_nob(unsigned int niov, lnet_kiov_t *kiov)
394 unsigned int nob = 0;
396 LASSERT(niov == 0 || kiov != NULL);
398 nob += (kiov++)->kiov_len;
402 EXPORT_SYMBOL(lnet_kiov_nob);
405 lnet_copy_kiov2kiov(unsigned int ndiov, lnet_kiov_t *diov, unsigned int doffset,
406 unsigned int nsiov, lnet_kiov_t *siov, unsigned int soffset,
409 /* NB diov, siov are READ-ONLY */
410 unsigned int this_nob;
417 LASSERT (!in_interrupt ());
420 while (doffset >= diov->kiov_len) {
421 doffset -= diov->kiov_len;
428 while (soffset >= siov->kiov_len) {
429 soffset -= siov->kiov_len;
438 this_nob = MIN(diov->kiov_len - doffset,
439 siov->kiov_len - soffset);
440 this_nob = MIN(this_nob, nob);
443 daddr = ((char *)kmap(diov->kiov_page)) +
444 diov->kiov_offset + doffset;
446 saddr = ((char *)kmap(siov->kiov_page)) +
447 siov->kiov_offset + soffset;
449 /* Vanishing risk of kmap deadlock when mapping 2 pages.
450 * However in practice at least one of the kiovs will be mapped
451 * kernel pages and the map/unmap will be NOOPs */
453 memcpy (daddr, saddr, this_nob);
456 if (diov->kiov_len > doffset + this_nob) {
460 kunmap(diov->kiov_page);
467 if (siov->kiov_len > soffset + this_nob) {
471 kunmap(siov->kiov_page);
480 kunmap(diov->kiov_page);
482 kunmap(siov->kiov_page);
484 EXPORT_SYMBOL(lnet_copy_kiov2kiov);
487 lnet_copy_kiov2iov (unsigned int niov, struct kvec *iov, unsigned int iovoffset,
488 unsigned int nkiov, lnet_kiov_t *kiov, unsigned int kiovoffset,
491 /* NB iov, kiov are READ-ONLY */
492 unsigned int this_nob;
498 LASSERT (!in_interrupt ());
501 while (iovoffset >= iov->iov_len) {
502 iovoffset -= iov->iov_len;
509 while (kiovoffset >= kiov->kiov_len) {
510 kiovoffset -= kiov->kiov_len;
519 this_nob = MIN(iov->iov_len - iovoffset,
520 kiov->kiov_len - kiovoffset);
521 this_nob = MIN(this_nob, nob);
524 addr = ((char *)kmap(kiov->kiov_page)) +
525 kiov->kiov_offset + kiovoffset;
527 memcpy((char *)iov->iov_base + iovoffset, addr, this_nob);
530 if (iov->iov_len > iovoffset + this_nob) {
531 iovoffset += this_nob;
538 if (kiov->kiov_len > kiovoffset + this_nob) {
540 kiovoffset += this_nob;
542 kunmap(kiov->kiov_page);
552 kunmap(kiov->kiov_page);
554 EXPORT_SYMBOL(lnet_copy_kiov2iov);
557 lnet_copy_iov2kiov(unsigned int nkiov, lnet_kiov_t *kiov, unsigned int kiovoffset,
558 unsigned int niov, struct kvec *iov, unsigned int iovoffset,
561 /* NB kiov, iov are READ-ONLY */
562 unsigned int this_nob;
568 LASSERT (!in_interrupt ());
571 while (kiovoffset >= kiov->kiov_len) {
572 kiovoffset -= kiov->kiov_len;
579 while (iovoffset >= iov->iov_len) {
580 iovoffset -= iov->iov_len;
589 this_nob = MIN(kiov->kiov_len - kiovoffset,
590 iov->iov_len - iovoffset);
591 this_nob = MIN(this_nob, nob);
594 addr = ((char *)kmap(kiov->kiov_page)) +
595 kiov->kiov_offset + kiovoffset;
597 memcpy (addr, (char *)iov->iov_base + iovoffset, this_nob);
600 if (kiov->kiov_len > kiovoffset + this_nob) {
602 kiovoffset += this_nob;
604 kunmap(kiov->kiov_page);
611 if (iov->iov_len > iovoffset + this_nob) {
612 iovoffset += this_nob;
621 kunmap(kiov->kiov_page);
623 EXPORT_SYMBOL(lnet_copy_iov2kiov);
626 lnet_extract_kiov(int dst_niov, lnet_kiov_t *dst,
627 int src_niov, lnet_kiov_t *src,
628 unsigned int offset, unsigned int len)
630 /* Initialise 'dst' to the subset of 'src' starting at 'offset',
631 * for exactly 'len' bytes, and return the number of entries.
632 * NB not destructive to 'src' */
633 unsigned int frag_len;
636 if (len == 0) /* no data => */
637 return (0); /* no frags */
639 LASSERT(src_niov > 0);
640 while (offset >= src->kiov_len) { /* skip initial frags */
641 offset -= src->kiov_len;
644 LASSERT(src_niov > 0);
649 LASSERT(src_niov > 0);
650 LASSERT((int)niov <= dst_niov);
652 frag_len = src->kiov_len - offset;
653 dst->kiov_page = src->kiov_page;
654 dst->kiov_offset = src->kiov_offset + offset;
656 if (len <= frag_len) {
658 LASSERT(dst->kiov_offset + dst->kiov_len <= PAGE_SIZE);
662 dst->kiov_len = frag_len;
663 LASSERT(dst->kiov_offset + dst->kiov_len <= PAGE_SIZE);
673 EXPORT_SYMBOL(lnet_extract_kiov);
676 lnet_ni_recv(struct lnet_ni *ni, void *private, struct lnet_msg *msg,
677 int delayed, unsigned int offset, unsigned int mlen,
680 unsigned int niov = 0;
681 struct kvec *iov = NULL;
682 lnet_kiov_t *kiov = NULL;
685 LASSERT (!in_interrupt ());
686 LASSERT (mlen == 0 || msg != NULL);
689 LASSERT(msg->msg_receiving);
690 LASSERT(!msg->msg_sending);
691 LASSERT(rlen == msg->msg_len);
692 LASSERT(mlen <= msg->msg_len);
693 LASSERT(msg->msg_offset == offset);
694 LASSERT(msg->msg_wanted == mlen);
696 msg->msg_receiving = 0;
699 niov = msg->msg_niov;
701 kiov = msg->msg_kiov;
704 LASSERT ((iov == NULL) != (kiov == NULL));
708 rc = (ni->ni_net->net_lnd->lnd_recv)(ni, private, msg, delayed,
709 niov, iov, kiov, offset, mlen,
712 lnet_finalize(msg, rc);
716 lnet_setpayloadbuffer(struct lnet_msg *msg)
718 struct lnet_libmd *md = msg->msg_md;
720 LASSERT(msg->msg_len > 0);
721 LASSERT(!msg->msg_routing);
723 LASSERT(msg->msg_niov == 0);
724 LASSERT(msg->msg_iov == NULL);
725 LASSERT(msg->msg_kiov == NULL);
727 msg->msg_niov = md->md_niov;
728 if ((md->md_options & LNET_MD_KIOV) != 0)
729 msg->msg_kiov = md->md_iov.kiov;
731 msg->msg_iov = md->md_iov.iov;
735 lnet_prep_send(struct lnet_msg *msg, int type, struct lnet_process_id target,
736 unsigned int offset, unsigned int len)
738 msg->msg_type = type;
739 msg->msg_target = target;
741 msg->msg_offset = offset;
744 lnet_setpayloadbuffer(msg);
746 memset (&msg->msg_hdr, 0, sizeof (msg->msg_hdr));
747 msg->msg_hdr.type = cpu_to_le32(type);
748 /* dest_nid will be overwritten by lnet_select_pathway() */
749 msg->msg_hdr.dest_nid = cpu_to_le64(target.nid);
750 msg->msg_hdr.dest_pid = cpu_to_le32(target.pid);
751 /* src_nid will be set later */
752 msg->msg_hdr.src_pid = cpu_to_le32(the_lnet.ln_pid);
753 msg->msg_hdr.payload_length = cpu_to_le32(len);
757 lnet_ni_send(struct lnet_ni *ni, struct lnet_msg *msg)
759 void *priv = msg->msg_private;
762 LASSERT (!in_interrupt ());
763 LASSERT (LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND ||
764 (msg->msg_txcredit && msg->msg_peertxcredit));
766 rc = (ni->ni_net->net_lnd->lnd_send)(ni, priv, msg);
768 msg->msg_no_resend = true;
769 lnet_finalize(msg, rc);
774 lnet_ni_eager_recv(struct lnet_ni *ni, struct lnet_msg *msg)
778 LASSERT(!msg->msg_sending);
779 LASSERT(msg->msg_receiving);
780 LASSERT(!msg->msg_rx_ready_delay);
781 LASSERT(ni->ni_net->net_lnd->lnd_eager_recv != NULL);
783 msg->msg_rx_ready_delay = 1;
784 rc = (ni->ni_net->net_lnd->lnd_eager_recv)(ni, msg->msg_private, msg,
787 CERROR("recv from %s / send to %s aborted: "
788 "eager_recv failed %d\n",
789 libcfs_nid2str(msg->msg_rxpeer->lpni_nid),
790 libcfs_id2str(msg->msg_target), rc);
791 LASSERT(rc < 0); /* required by my callers */
797 /* NB: returns 1 when alive, 0 when dead, negative when error;
798 * may drop the lnet_net_lock */
800 lnet_peer_alive_locked(struct lnet_ni *ni, struct lnet_peer_ni *lpni,
801 struct lnet_msg *msg)
803 if (!lnet_peer_aliveness_enabled(lpni))
807 * If we're resending a message, let's attempt to send it even if
808 * the peer is down to fulfill our resend quota on the message
810 if (msg->msg_retry_count > 0)
813 /* try and send recovery messages irregardless */
814 if (msg->msg_recovery)
817 /* always send any responses */
818 if (msg->msg_type == LNET_MSG_ACK ||
819 msg->msg_type == LNET_MSG_REPLY)
822 return lnet_is_peer_ni_alive(lpni);
826 * \param msg The message to be sent.
827 * \param do_send True if lnet_ni_send() should be called in this function.
828 * lnet_send() is going to lnet_net_unlock immediately after this, so
829 * it sets do_send FALSE and I don't do the unlock/send/lock bit.
831 * \retval LNET_CREDIT_OK If \a msg sent or OK to send.
832 * \retval LNET_CREDIT_WAIT If \a msg blocked for credit.
833 * \retval -EHOSTUNREACH If the next hop of the message appears dead.
834 * \retval -ECANCELED If the MD of the message has been unlinked.
837 lnet_post_send_locked(struct lnet_msg *msg, int do_send)
839 struct lnet_peer_ni *lp = msg->msg_txpeer;
840 struct lnet_ni *ni = msg->msg_txni;
841 int cpt = msg->msg_tx_cpt;
842 struct lnet_tx_queue *tq = ni->ni_tx_queues[cpt];
844 /* non-lnet_send() callers have checked before */
845 LASSERT(!do_send || msg->msg_tx_delayed);
846 LASSERT(!msg->msg_receiving);
847 LASSERT(msg->msg_tx_committed);
848 /* can't get here if we're sending to the loopback interface */
849 LASSERT(lp->lpni_nid != the_lnet.ln_loni->ni_nid);
851 /* NB 'lp' is always the next hop */
852 if ((msg->msg_target.pid & LNET_PID_USERFLAG) == 0 &&
853 lnet_peer_alive_locked(ni, lp, msg) == 0) {
854 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
855 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length +=
857 lnet_net_unlock(cpt);
859 lnet_incr_stats(&msg->msg_txpeer->lpni_stats,
861 LNET_STATS_TYPE_DROP);
863 lnet_incr_stats(&msg->msg_txni->ni_stats,
865 LNET_STATS_TYPE_DROP);
867 CNETERR("Dropping message for %s: peer not alive\n",
868 libcfs_id2str(msg->msg_target));
869 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_DROPPED;
871 lnet_finalize(msg, -EHOSTUNREACH);
874 return -EHOSTUNREACH;
877 if (msg->msg_md != NULL &&
878 (msg->msg_md->md_flags & LNET_MD_FLAG_ABORTED) != 0) {
879 lnet_net_unlock(cpt);
881 CNETERR("Aborting message for %s: LNetM[DE]Unlink() already "
882 "called on the MD/ME.\n",
883 libcfs_id2str(msg->msg_target));
885 msg->msg_no_resend = true;
886 CDEBUG(D_NET, "msg %p to %s canceled and will not be resent\n",
887 msg, libcfs_id2str(msg->msg_target));
888 lnet_finalize(msg, -ECANCELED);
895 if (!msg->msg_peertxcredit) {
896 spin_lock(&lp->lpni_lock);
897 LASSERT((lp->lpni_txcredits < 0) ==
898 !list_empty(&lp->lpni_txq));
900 msg->msg_peertxcredit = 1;
901 lp->lpni_txqnob += msg->msg_len + sizeof(struct lnet_hdr);
902 lp->lpni_txcredits--;
904 if (lp->lpni_txcredits < lp->lpni_mintxcredits)
905 lp->lpni_mintxcredits = lp->lpni_txcredits;
907 if (lp->lpni_txcredits < 0) {
908 msg->msg_tx_delayed = 1;
909 list_add_tail(&msg->msg_list, &lp->lpni_txq);
910 spin_unlock(&lp->lpni_lock);
911 return LNET_CREDIT_WAIT;
913 spin_unlock(&lp->lpni_lock);
916 if (!msg->msg_txcredit) {
917 LASSERT((tq->tq_credits < 0) ==
918 !list_empty(&tq->tq_delayed));
920 msg->msg_txcredit = 1;
922 atomic_dec(&ni->ni_tx_credits);
924 if (tq->tq_credits < tq->tq_credits_min)
925 tq->tq_credits_min = tq->tq_credits;
927 if (tq->tq_credits < 0) {
928 msg->msg_tx_delayed = 1;
929 list_add_tail(&msg->msg_list, &tq->tq_delayed);
930 return LNET_CREDIT_WAIT;
934 /* unset the tx_delay flag as we're going to send it now */
935 msg->msg_tx_delayed = 0;
938 lnet_net_unlock(cpt);
939 lnet_ni_send(ni, msg);
942 return LNET_CREDIT_OK;
946 static struct lnet_rtrbufpool *
947 lnet_msg2bufpool(struct lnet_msg *msg)
949 struct lnet_rtrbufpool *rbp;
952 LASSERT(msg->msg_rx_committed);
954 cpt = msg->msg_rx_cpt;
955 rbp = &the_lnet.ln_rtrpools[cpt][0];
957 LASSERT(msg->msg_len <= LNET_MTU);
958 while (msg->msg_len > (unsigned int)rbp->rbp_npages * PAGE_SIZE) {
960 LASSERT(rbp < &the_lnet.ln_rtrpools[cpt][LNET_NRBPOOLS]);
967 lnet_post_routed_recv_locked(struct lnet_msg *msg, int do_recv)
969 /* lnet_parse is going to lnet_net_unlock immediately after this, so it
970 * sets do_recv FALSE and I don't do the unlock/send/lock bit.
971 * I return LNET_CREDIT_WAIT if msg blocked and LNET_CREDIT_OK if
972 * received or OK to receive */
973 struct lnet_peer_ni *lpni = msg->msg_rxpeer;
974 struct lnet_peer *lp;
975 struct lnet_rtrbufpool *rbp;
976 struct lnet_rtrbuf *rb;
978 LASSERT(msg->msg_iov == NULL);
979 LASSERT(msg->msg_kiov == NULL);
980 LASSERT(msg->msg_niov == 0);
981 LASSERT(msg->msg_routing);
982 LASSERT(msg->msg_receiving);
983 LASSERT(!msg->msg_sending);
984 LASSERT(lpni->lpni_peer_net);
985 LASSERT(lpni->lpni_peer_net->lpn_peer);
987 lp = lpni->lpni_peer_net->lpn_peer;
989 /* non-lnet_parse callers only receive delayed messages */
990 LASSERT(!do_recv || msg->msg_rx_delayed);
992 if (!msg->msg_peerrtrcredit) {
993 /* lpni_lock protects the credit manipulation */
994 spin_lock(&lpni->lpni_lock);
995 /* lp_lock protects the lp_rtrq */
996 spin_lock(&lp->lp_lock);
998 msg->msg_peerrtrcredit = 1;
999 lpni->lpni_rtrcredits--;
1000 if (lpni->lpni_rtrcredits < lpni->lpni_minrtrcredits)
1001 lpni->lpni_minrtrcredits = lpni->lpni_rtrcredits;
1003 if (lpni->lpni_rtrcredits < 0) {
1004 /* must have checked eager_recv before here */
1005 LASSERT(msg->msg_rx_ready_delay);
1006 msg->msg_rx_delayed = 1;
1007 list_add_tail(&msg->msg_list, &lp->lp_rtrq);
1008 spin_unlock(&lp->lp_lock);
1009 spin_unlock(&lpni->lpni_lock);
1010 return LNET_CREDIT_WAIT;
1012 spin_unlock(&lp->lp_lock);
1013 spin_unlock(&lpni->lpni_lock);
1016 rbp = lnet_msg2bufpool(msg);
1018 if (!msg->msg_rtrcredit) {
1019 msg->msg_rtrcredit = 1;
1021 if (rbp->rbp_credits < rbp->rbp_mincredits)
1022 rbp->rbp_mincredits = rbp->rbp_credits;
1024 if (rbp->rbp_credits < 0) {
1025 /* must have checked eager_recv before here */
1026 LASSERT(msg->msg_rx_ready_delay);
1027 msg->msg_rx_delayed = 1;
1028 list_add_tail(&msg->msg_list, &rbp->rbp_msgs);
1029 return LNET_CREDIT_WAIT;
1033 LASSERT(!list_empty(&rbp->rbp_bufs));
1034 rb = list_entry(rbp->rbp_bufs.next, struct lnet_rtrbuf, rb_list);
1035 list_del(&rb->rb_list);
1037 msg->msg_niov = rbp->rbp_npages;
1038 msg->msg_kiov = &rb->rb_kiov[0];
1040 /* unset the msg-rx_delayed flag since we're receiving the message */
1041 msg->msg_rx_delayed = 0;
1044 int cpt = msg->msg_rx_cpt;
1046 lnet_net_unlock(cpt);
1047 lnet_ni_recv(msg->msg_rxni, msg->msg_private, msg, 1,
1048 0, msg->msg_len, msg->msg_len);
1051 return LNET_CREDIT_OK;
1055 lnet_return_tx_credits_locked(struct lnet_msg *msg)
1057 struct lnet_peer_ni *txpeer = msg->msg_txpeer;
1058 struct lnet_ni *txni = msg->msg_txni;
1059 struct lnet_msg *msg2;
1061 if (msg->msg_txcredit) {
1062 struct lnet_ni *ni = msg->msg_txni;
1063 struct lnet_tx_queue *tq = ni->ni_tx_queues[msg->msg_tx_cpt];
1065 /* give back NI txcredits */
1066 msg->msg_txcredit = 0;
1068 LASSERT((tq->tq_credits < 0) ==
1069 !list_empty(&tq->tq_delayed));
1072 atomic_inc(&ni->ni_tx_credits);
1073 if (tq->tq_credits <= 0) {
1074 msg2 = list_entry(tq->tq_delayed.next,
1075 struct lnet_msg, msg_list);
1076 list_del(&msg2->msg_list);
1078 LASSERT(msg2->msg_txni == ni);
1079 LASSERT(msg2->msg_tx_delayed);
1080 LASSERT(msg2->msg_tx_cpt == msg->msg_tx_cpt);
1082 (void) lnet_post_send_locked(msg2, 1);
1086 if (msg->msg_peertxcredit) {
1087 /* give back peer txcredits */
1088 msg->msg_peertxcredit = 0;
1090 spin_lock(&txpeer->lpni_lock);
1091 LASSERT((txpeer->lpni_txcredits < 0) ==
1092 !list_empty(&txpeer->lpni_txq));
1094 txpeer->lpni_txqnob -= msg->msg_len + sizeof(struct lnet_hdr);
1095 LASSERT(txpeer->lpni_txqnob >= 0);
1097 txpeer->lpni_txcredits++;
1098 if (txpeer->lpni_txcredits <= 0) {
1101 msg2 = list_entry(txpeer->lpni_txq.next,
1102 struct lnet_msg, msg_list);
1103 list_del(&msg2->msg_list);
1104 spin_unlock(&txpeer->lpni_lock);
1106 LASSERT(msg2->msg_txpeer == txpeer);
1107 LASSERT(msg2->msg_tx_delayed);
1109 msg2_cpt = msg2->msg_tx_cpt;
1112 * The msg_cpt can be different from the msg2_cpt
1113 * so we need to make sure we lock the correct cpt
1115 * Once we call lnet_post_send_locked() it is no
1116 * longer safe to access msg2, since it could've
1117 * been freed by lnet_finalize(), but we still
1118 * need to relock the correct cpt, so we cache the
1119 * msg2_cpt for the purpose of the check that
1120 * follows the call to lnet_pose_send_locked().
1122 if (msg2_cpt != msg->msg_tx_cpt) {
1123 lnet_net_unlock(msg->msg_tx_cpt);
1124 lnet_net_lock(msg2_cpt);
1126 (void) lnet_post_send_locked(msg2, 1);
1127 if (msg2_cpt != msg->msg_tx_cpt) {
1128 lnet_net_unlock(msg2_cpt);
1129 lnet_net_lock(msg->msg_tx_cpt);
1132 spin_unlock(&txpeer->lpni_lock);
1137 msg->msg_txni = NULL;
1138 lnet_ni_decref_locked(txni, msg->msg_tx_cpt);
1141 if (txpeer != NULL) {
1142 msg->msg_txpeer = NULL;
1143 lnet_peer_ni_decref_locked(txpeer);
1148 lnet_schedule_blocked_locked(struct lnet_rtrbufpool *rbp)
1150 struct lnet_msg *msg;
1152 if (list_empty(&rbp->rbp_msgs))
1154 msg = list_entry(rbp->rbp_msgs.next,
1155 struct lnet_msg, msg_list);
1156 list_del(&msg->msg_list);
1158 (void)lnet_post_routed_recv_locked(msg, 1);
1162 lnet_drop_routed_msgs_locked(struct list_head *list, int cpt)
1164 struct lnet_msg *msg;
1165 struct lnet_msg *tmp;
1167 lnet_net_unlock(cpt);
1169 list_for_each_entry_safe(msg, tmp, list, msg_list) {
1170 lnet_ni_recv(msg->msg_rxni, msg->msg_private, NULL,
1171 0, 0, 0, msg->msg_hdr.payload_length);
1172 list_del_init(&msg->msg_list);
1173 msg->msg_no_resend = true;
1174 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_ERROR;
1175 lnet_finalize(msg, -ECANCELED);
1182 lnet_return_rx_credits_locked(struct lnet_msg *msg)
1184 struct lnet_peer_ni *rxpeerni = msg->msg_rxpeer;
1185 struct lnet_peer *lp;
1186 struct lnet_ni *rxni = msg->msg_rxni;
1187 struct lnet_msg *msg2;
1189 if (msg->msg_rtrcredit) {
1190 /* give back global router credits */
1191 struct lnet_rtrbuf *rb;
1192 struct lnet_rtrbufpool *rbp;
1194 /* NB If a msg ever blocks for a buffer in rbp_msgs, it stays
1195 * there until it gets one allocated, or aborts the wait
1197 LASSERT(msg->msg_kiov != NULL);
1199 rb = list_entry(msg->msg_kiov, struct lnet_rtrbuf, rb_kiov[0]);
1202 msg->msg_kiov = NULL;
1203 msg->msg_rtrcredit = 0;
1205 LASSERT(rbp == lnet_msg2bufpool(msg));
1207 LASSERT((rbp->rbp_credits > 0) ==
1208 !list_empty(&rbp->rbp_bufs));
1210 /* If routing is now turned off, we just drop this buffer and
1211 * don't bother trying to return credits. */
1212 if (!the_lnet.ln_routing) {
1213 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1217 /* It is possible that a user has lowered the desired number of
1218 * buffers in this pool. Make sure we never put back
1219 * more buffers than the stated number. */
1220 if (unlikely(rbp->rbp_credits >= rbp->rbp_req_nbuffers)) {
1221 /* Discard this buffer so we don't have too
1223 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1224 rbp->rbp_nbuffers--;
1226 list_add(&rb->rb_list, &rbp->rbp_bufs);
1228 if (rbp->rbp_credits <= 0)
1229 lnet_schedule_blocked_locked(rbp);
1234 if (msg->msg_peerrtrcredit) {
1236 LASSERT(rxpeerni->lpni_peer_net);
1237 LASSERT(rxpeerni->lpni_peer_net->lpn_peer);
1239 lp = rxpeerni->lpni_peer_net->lpn_peer;
1241 /* give back peer router credits */
1242 msg->msg_peerrtrcredit = 0;
1244 spin_lock(&rxpeerni->lpni_lock);
1245 spin_lock(&lp->lp_lock);
1247 rxpeerni->lpni_rtrcredits++;
1249 /* drop all messages which are queued to be routed on that
1251 if (!the_lnet.ln_routing) {
1252 struct list_head drop;
1253 INIT_LIST_HEAD(&drop);
1254 list_splice_init(&lp->lp_rtrq, &drop);
1255 spin_unlock(&lp->lp_lock);
1256 spin_unlock(&rxpeerni->lpni_lock);
1257 lnet_drop_routed_msgs_locked(&drop, msg->msg_rx_cpt);
1258 } else if (!list_empty(&lp->lp_rtrq)) {
1261 msg2 = list_entry(lp->lp_rtrq.next,
1262 struct lnet_msg, msg_list);
1263 list_del(&msg2->msg_list);
1264 msg2_cpt = msg2->msg_rx_cpt;
1265 spin_unlock(&lp->lp_lock);
1266 spin_unlock(&rxpeerni->lpni_lock);
1268 * messages on the lp_rtrq can be from any NID in
1269 * the peer, which means they might have different
1270 * cpts. We need to make sure we lock the right
1273 if (msg2_cpt != msg->msg_rx_cpt) {
1274 lnet_net_unlock(msg->msg_rx_cpt);
1275 lnet_net_lock(msg2_cpt);
1277 (void) lnet_post_routed_recv_locked(msg2, 1);
1278 if (msg2_cpt != msg->msg_rx_cpt) {
1279 lnet_net_unlock(msg2_cpt);
1280 lnet_net_lock(msg->msg_rx_cpt);
1283 spin_unlock(&lp->lp_lock);
1284 spin_unlock(&rxpeerni->lpni_lock);
1288 msg->msg_rxni = NULL;
1289 lnet_ni_decref_locked(rxni, msg->msg_rx_cpt);
1291 if (rxpeerni != NULL) {
1292 msg->msg_rxpeer = NULL;
1293 lnet_peer_ni_decref_locked(rxpeerni);
1298 lnet_compare_peers(struct lnet_peer_ni *p1, struct lnet_peer_ni *p2)
1300 if (p1->lpni_txqnob < p2->lpni_txqnob)
1303 if (p1->lpni_txqnob > p2->lpni_txqnob)
1306 if (p1->lpni_txcredits > p2->lpni_txcredits)
1309 if (p1->lpni_txcredits < p2->lpni_txcredits)
1315 static struct lnet_peer_ni *
1316 lnet_select_peer_ni(struct lnet_ni *best_ni, lnet_nid_t dst_nid,
1317 struct lnet_peer *peer,
1318 struct lnet_peer_net *peer_net)
1321 * Look at the peer NIs for the destination peer that connect
1322 * to the chosen net. If a peer_ni is preferred when using the
1323 * best_ni to communicate, we use that one. If there is no
1324 * preferred peer_ni, or there are multiple preferred peer_ni,
1325 * the available transmit credits are used. If the transmit
1326 * credits are equal, we round-robin over the peer_ni.
1328 struct lnet_peer_ni *lpni = NULL;
1329 struct lnet_peer_ni *best_lpni = NULL;
1330 int best_lpni_credits = INT_MIN;
1331 bool preferred = false;
1333 int best_lpni_healthv = 0;
1336 while ((lpni = lnet_get_next_peer_ni_locked(peer, peer_net, lpni))) {
1338 * if the best_ni we've chosen aleady has this lpni
1339 * preferred, then let's use it
1342 ni_is_pref = lnet_peer_is_pref_nid_locked(lpni,
1344 CDEBUG(D_NET, "%s ni_is_pref = %d\n",
1345 libcfs_nid2str(best_ni->ni_nid), ni_is_pref);
1350 lpni_healthv = atomic_read(&lpni->lpni_healthv);
1353 CDEBUG(D_NET, "%s c:[%d, %d], s:[%d, %d]\n",
1354 libcfs_nid2str(lpni->lpni_nid),
1355 lpni->lpni_txcredits, best_lpni_credits,
1356 lpni->lpni_seq, best_lpni->lpni_seq);
1358 /* pick the healthiest peer ni */
1359 if (lpni_healthv < best_lpni_healthv) {
1361 } else if (lpni_healthv > best_lpni_healthv) {
1362 best_lpni_healthv = lpni_healthv;
1363 /* if this is a preferred peer use it */
1364 } else if (!preferred && ni_is_pref) {
1366 } else if (preferred && !ni_is_pref) {
1368 * this is not the preferred peer so let's ignore
1372 } else if (lpni->lpni_txcredits < best_lpni_credits) {
1374 * We already have a peer that has more credits
1375 * available than this one. No need to consider
1376 * this peer further.
1379 } else if (lpni->lpni_txcredits == best_lpni_credits) {
1381 * The best peer found so far and the current peer
1382 * have the same number of available credits let's
1383 * make sure to select between them using Round
1387 if (best_lpni->lpni_seq <= lpni->lpni_seq)
1393 best_lpni_credits = lpni->lpni_txcredits;
1396 /* if we still can't find a peer ni then we can't reach it */
1398 __u32 net_id = (peer_net) ? peer_net->lpn_net_id :
1399 LNET_NIDNET(dst_nid);
1400 CDEBUG(D_NET, "no peer_ni found on peer net %s\n",
1401 libcfs_net2str(net_id));
1405 CDEBUG(D_NET, "sd_best_lpni = %s\n",
1406 libcfs_nid2str(best_lpni->lpni_nid));
1412 * Prerequisite: the best_ni should already be set in the sd
1414 static inline struct lnet_peer_ni *
1415 lnet_find_best_lpni_on_net(struct lnet_send_data *sd, struct lnet_peer *peer,
1418 struct lnet_peer_net *peer_net;
1421 * The gateway is Multi-Rail capable so now we must select the
1424 peer_net = lnet_peer_get_net_locked(peer, net_id);
1427 CERROR("gateway peer %s has no NI on net %s\n",
1428 libcfs_nid2str(peer->lp_primary_nid),
1429 libcfs_net2str(net_id));
1433 return lnet_select_peer_ni(sd->sd_best_ni, sd->sd_dst_nid,
1438 lnet_compare_routes(struct lnet_route *r1, struct lnet_route *r2,
1439 struct lnet_peer_ni **best_lpni)
1441 int r1_hops = (r1->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r1->lr_hops;
1442 int r2_hops = (r2->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r2->lr_hops;
1443 struct lnet_peer *lp1 = r1->lr_gateway;
1444 struct lnet_peer *lp2 = r2->lr_gateway;
1445 struct lnet_peer_ni *lpni1;
1446 struct lnet_peer_ni *lpni2;
1447 struct lnet_send_data sd;
1450 sd.sd_best_ni = NULL;
1451 sd.sd_dst_nid = LNET_NID_ANY;
1452 lpni1 = lnet_find_best_lpni_on_net(&sd, lp1, r1->lr_lnet);
1453 lpni2 = lnet_find_best_lpni_on_net(&sd, lp2, r2->lr_lnet);
1454 LASSERT(lpni1 && lpni2);
1456 if (r1->lr_priority < r2->lr_priority) {
1461 if (r1->lr_priority > r2->lr_priority) {
1466 if (r1_hops < r2_hops) {
1471 if (r1_hops > r2_hops) {
1476 rc = lnet_compare_peers(lpni1, lpni2);
1480 } else if (rc == -1) {
1485 if (r1->lr_seq - r2->lr_seq <= 0) {
1494 static struct lnet_route *
1495 lnet_find_route_locked(struct lnet_net *net, __u32 remote_net,
1496 lnet_nid_t rtr_nid, struct lnet_route **prev_route,
1497 struct lnet_peer_ni **gwni)
1499 struct lnet_peer_ni *best_gw_ni = NULL;
1500 struct lnet_route *best_route;
1501 struct lnet_route *last_route;
1502 struct lnet_remotenet *rnet;
1503 struct lnet_peer *lp_best;
1504 struct lnet_route *route;
1505 struct lnet_peer *lp;
1508 /* If @rtr_nid is not LNET_NID_ANY, return the gateway with
1509 * rtr_nid nid, otherwise find the best gateway I can use */
1511 rnet = lnet_find_rnet_locked(remote_net);
1516 best_route = last_route = NULL;
1517 list_for_each_entry(route, &rnet->lrn_routes, lr_list) {
1518 lp = route->lr_gateway;
1520 if (!lnet_is_route_alive(route))
1523 if (lp_best == NULL) {
1524 best_route = last_route = route;
1528 /* no protection on below fields, but it's harmless */
1529 if (last_route->lr_seq - route->lr_seq < 0)
1532 rc = lnet_compare_routes(route, best_route, &best_gw_ni);
1540 *prev_route = last_route;
1546 static struct lnet_ni *
1547 lnet_get_best_ni(struct lnet_net *local_net, struct lnet_ni *best_ni,
1548 struct lnet_peer *peer, struct lnet_peer_net *peer_net,
1551 struct lnet_ni *ni = NULL;
1552 unsigned int shortest_distance;
1557 * If there is no peer_ni that we can send to on this network,
1558 * then there is no point in looking for a new best_ni here.
1560 if (!lnet_get_next_peer_ni_locked(peer, peer_net, NULL))
1563 if (best_ni == NULL) {
1564 shortest_distance = UINT_MAX;
1565 best_credits = INT_MIN;
1568 shortest_distance = cfs_cpt_distance(lnet_cpt_table(), md_cpt,
1569 best_ni->ni_dev_cpt);
1570 best_credits = atomic_read(&best_ni->ni_tx_credits);
1571 best_healthv = atomic_read(&best_ni->ni_healthv);
1574 while ((ni = lnet_get_next_ni_locked(local_net, ni))) {
1575 unsigned int distance;
1580 ni_credits = atomic_read(&ni->ni_tx_credits);
1581 ni_healthv = atomic_read(&ni->ni_healthv);
1582 ni_fatal = atomic_read(&ni->ni_fatal_error_on);
1585 * calculate the distance from the CPT on which
1586 * the message memory is allocated to the CPT of
1587 * the NI's physical device
1589 distance = cfs_cpt_distance(lnet_cpt_table(),
1593 CDEBUG(D_NET, "compare ni %s [c:%d, d:%d, s:%d] with best_ni %s [c:%d, d:%d, s:%d]\n",
1594 libcfs_nid2str(ni->ni_nid), ni_credits, distance,
1595 ni->ni_seq, (best_ni) ? libcfs_nid2str(best_ni->ni_nid)
1596 : "not seleced", best_credits, shortest_distance,
1597 (best_ni) ? best_ni->ni_seq : 0);
1600 * All distances smaller than the NUMA range
1601 * are treated equally.
1603 if (distance < lnet_numa_range)
1604 distance = lnet_numa_range;
1607 * Select on health, shorter distance, available
1608 * credits, then round-robin.
1612 } else if (ni_healthv < best_healthv) {
1614 } else if (ni_healthv > best_healthv) {
1615 best_healthv = ni_healthv;
1617 * If we're going to prefer this ni because it's
1618 * the healthiest, then we should set the
1619 * shortest_distance in the algorithm in case
1620 * there are multiple NIs with the same health but
1621 * different distances.
1623 if (distance < shortest_distance)
1624 shortest_distance = distance;
1625 } else if (distance > shortest_distance) {
1627 } else if (distance < shortest_distance) {
1628 shortest_distance = distance;
1629 } else if (ni_credits < best_credits) {
1631 } else if (ni_credits == best_credits) {
1632 if (best_ni && best_ni->ni_seq <= ni->ni_seq)
1636 best_credits = ni_credits;
1639 CDEBUG(D_NET, "selected best_ni %s\n",
1640 (best_ni) ? libcfs_nid2str(best_ni->ni_nid) : "no selection");
1646 * Traffic to the LNET_RESERVED_PORTAL may not trigger peer discovery,
1647 * because such traffic is required to perform discovery. We therefore
1648 * exclude all GET and PUT on that portal. We also exclude all ACK and
1649 * REPLY traffic, but that is because the portal is not tracked in the
1650 * message structure for these message types. We could restrict this
1651 * further by also checking for LNET_PROTO_PING_MATCHBITS.
1654 lnet_msg_discovery(struct lnet_msg *msg)
1656 if (msg->msg_type == LNET_MSG_PUT) {
1657 if (msg->msg_hdr.msg.put.ptl_index != LNET_RESERVED_PORTAL)
1659 } else if (msg->msg_type == LNET_MSG_GET) {
1660 if (msg->msg_hdr.msg.get.ptl_index != LNET_RESERVED_PORTAL)
1666 #define SRC_SPEC 0x0001
1667 #define SRC_ANY 0x0002
1668 #define LOCAL_DST 0x0004
1669 #define REMOTE_DST 0x0008
1670 #define MR_DST 0x0010
1671 #define NMR_DST 0x0020
1672 #define SND_RESP 0x0040
1674 /* The following to defines are used for return codes */
1675 #define REPEAT_SEND 0x1000
1676 #define PASS_THROUGH 0x2000
1678 /* The different cases lnet_select pathway needs to handle */
1679 #define SRC_SPEC_LOCAL_MR_DST (SRC_SPEC | LOCAL_DST | MR_DST)
1680 #define SRC_SPEC_ROUTER_MR_DST (SRC_SPEC | REMOTE_DST | MR_DST)
1681 #define SRC_SPEC_LOCAL_NMR_DST (SRC_SPEC | LOCAL_DST | NMR_DST)
1682 #define SRC_SPEC_ROUTER_NMR_DST (SRC_SPEC | REMOTE_DST | NMR_DST)
1683 #define SRC_ANY_LOCAL_MR_DST (SRC_ANY | LOCAL_DST | MR_DST)
1684 #define SRC_ANY_ROUTER_MR_DST (SRC_ANY | REMOTE_DST | MR_DST)
1685 #define SRC_ANY_LOCAL_NMR_DST (SRC_ANY | LOCAL_DST | NMR_DST)
1686 #define SRC_ANY_ROUTER_NMR_DST (SRC_ANY | REMOTE_DST | NMR_DST)
1689 lnet_handle_lo_send(struct lnet_send_data *sd)
1691 struct lnet_msg *msg = sd->sd_msg;
1692 int cpt = sd->sd_cpt;
1694 /* No send credit hassles with LOLND */
1695 lnet_ni_addref_locked(the_lnet.ln_loni, cpt);
1696 msg->msg_hdr.dest_nid = cpu_to_le64(the_lnet.ln_loni->ni_nid);
1697 if (!msg->msg_routing)
1698 msg->msg_hdr.src_nid =
1699 cpu_to_le64(the_lnet.ln_loni->ni_nid);
1700 msg->msg_target.nid = the_lnet.ln_loni->ni_nid;
1701 lnet_msg_commit(msg, cpt);
1702 msg->msg_txni = the_lnet.ln_loni;
1704 return LNET_CREDIT_OK;
1708 lnet_handle_send(struct lnet_send_data *sd)
1710 struct lnet_ni *best_ni = sd->sd_best_ni;
1711 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
1712 struct lnet_peer_ni *final_dst_lpni = sd->sd_final_dst_lpni;
1713 struct lnet_msg *msg = sd->sd_msg;
1715 __u32 send_case = sd->sd_send_case;
1717 __u32 routing = send_case & REMOTE_DST;
1718 struct lnet_rsp_tracker *rspt;
1721 * Increment sequence number of the selected peer so that we
1722 * pick the next one in Round Robin.
1724 best_lpni->lpni_seq++;
1727 * grab a reference on the peer_ni so it sticks around even if
1728 * we need to drop and relock the lnet_net_lock below.
1730 lnet_peer_ni_addref_locked(best_lpni);
1733 * Use lnet_cpt_of_nid() to determine the CPT used to commit the
1734 * message. This ensures that we get a CPT that is correct for
1735 * the NI when the NI has been restricted to a subset of all CPTs.
1736 * If the selected CPT differs from the one currently locked, we
1737 * must unlock and relock the lnet_net_lock(), and then check whether
1738 * the configuration has changed. We don't have a hold on the best_ni
1739 * yet, and it may have vanished.
1741 cpt2 = lnet_cpt_of_nid_locked(best_lpni->lpni_nid, best_ni);
1742 if (sd->sd_cpt != cpt2) {
1743 __u32 seq = lnet_get_dlc_seq_locked();
1744 lnet_net_unlock(sd->sd_cpt);
1746 lnet_net_lock(sd->sd_cpt);
1747 if (seq != lnet_get_dlc_seq_locked()) {
1748 lnet_peer_ni_decref_locked(best_lpni);
1754 * store the best_lpni in the message right away to avoid having
1755 * to do the same operation under different conditions
1757 msg->msg_txpeer = best_lpni;
1758 msg->msg_txni = best_ni;
1761 * grab a reference for the best_ni since now it's in use in this
1762 * send. The reference will be dropped in lnet_finalize()
1764 lnet_ni_addref_locked(msg->msg_txni, sd->sd_cpt);
1767 * Always set the target.nid to the best peer picked. Either the
1768 * NID will be one of the peer NIDs selected, or the same NID as
1769 * what was originally set in the target or it will be the NID of
1770 * a router if this message should be routed
1772 msg->msg_target.nid = msg->msg_txpeer->lpni_nid;
1775 * lnet_msg_commit assigns the correct cpt to the message, which
1776 * is used to decrement the correct refcount on the ni when it's
1777 * time to return the credits
1779 lnet_msg_commit(msg, sd->sd_cpt);
1782 * If we are routing the message then we keep the src_nid that was
1783 * set by the originator. If we are not routing then we are the
1784 * originator and set it here.
1786 if (!msg->msg_routing)
1787 msg->msg_hdr.src_nid = cpu_to_le64(msg->msg_txni->ni_nid);
1790 msg->msg_target_is_router = 1;
1791 msg->msg_target.pid = LNET_PID_LUSTRE;
1793 * since we're routing we want to ensure that the
1794 * msg_hdr.dest_nid is set to the final destination. When
1795 * the router receives this message it knows how to route
1798 * final_dst_lpni is set at the beginning of the
1799 * lnet_select_pathway() function and is never changed.
1800 * It's safe to use it here.
1802 msg->msg_hdr.dest_nid = cpu_to_le64(final_dst_lpni->lpni_nid);
1805 * if we're not routing set the dest_nid to the best peer
1806 * ni NID that we picked earlier in the algorithm.
1808 msg->msg_hdr.dest_nid = cpu_to_le64(msg->msg_txpeer->lpni_nid);
1812 * if we have response tracker block update it with the next hop
1816 rspt = msg->msg_md->md_rspt_ptr;
1818 rspt->rspt_next_hop_nid = msg->msg_txpeer->lpni_nid;
1819 CDEBUG(D_NET, "rspt_next_hop_nid = %s\n",
1820 libcfs_nid2str(rspt->rspt_next_hop_nid));
1824 rc = lnet_post_send_locked(msg, 0);
1827 CDEBUG(D_NET, "TRACE: %s(%s:%s) -> %s(%s:%s) : %s try# %d\n",
1828 libcfs_nid2str(msg->msg_hdr.src_nid),
1829 libcfs_nid2str(msg->msg_txni->ni_nid),
1830 libcfs_nid2str(sd->sd_src_nid),
1831 libcfs_nid2str(msg->msg_hdr.dest_nid),
1832 libcfs_nid2str(sd->sd_dst_nid),
1833 libcfs_nid2str(msg->msg_txpeer->lpni_nid),
1834 lnet_msgtyp2str(msg->msg_type), msg->msg_retry_count);
1840 lnet_set_non_mr_pref_nid(struct lnet_send_data *sd)
1842 if (sd->sd_send_case & NMR_DST &&
1843 sd->sd_msg->msg_type != LNET_MSG_REPLY &&
1844 sd->sd_msg->msg_type != LNET_MSG_ACK &&
1845 sd->sd_best_lpni->lpni_pref_nnids == 0) {
1846 CDEBUG(D_NET, "Setting preferred local NID %s on NMR peer %s\n",
1847 libcfs_nid2str(sd->sd_best_ni->ni_nid),
1848 libcfs_nid2str(sd->sd_best_lpni->lpni_nid));
1849 lnet_peer_ni_set_non_mr_pref_nid(sd->sd_best_lpni,
1850 sd->sd_best_ni->ni_nid);
1859 * use the source and destination NIDs as the pathway
1862 lnet_handle_spec_local_nmr_dst(struct lnet_send_data *sd)
1864 /* the destination lpni is set before we get here. */
1867 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
1868 if (!sd->sd_best_ni) {
1869 CERROR("Can't send to %s: src %s is not a "
1870 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
1871 libcfs_nid2str(sd->sd_src_nid));
1876 * the preferred NID will only be set for NMR peers
1878 lnet_set_non_mr_pref_nid(sd);
1880 return lnet_handle_send(sd);
1888 * Run the selection algorithm on the peer NIs unless we're sending
1889 * a response, in this case just send to the destination
1892 lnet_handle_spec_local_mr_dst(struct lnet_send_data *sd)
1894 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
1895 if (!sd->sd_best_ni) {
1896 CERROR("Can't send to %s: src %s is not a "
1897 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
1898 libcfs_nid2str(sd->sd_src_nid));
1903 * only run the selection algorithm to pick the peer_ni if we're
1904 * sending a GET or a PUT. Responses are sent to the same
1905 * destination NID provided.
1907 if (!(sd->sd_send_case & SND_RESP)) {
1909 lnet_find_best_lpni_on_net(sd, sd->sd_peer,
1910 sd->sd_best_ni->ni_net->net_id);
1913 if (sd->sd_best_lpni &&
1914 sd->sd_best_lpni->lpni_nid == the_lnet.ln_loni->ni_nid)
1915 return lnet_handle_lo_send(sd);
1916 else if (sd->sd_best_lpni)
1917 return lnet_handle_send(sd);
1919 CERROR("can't send to %s. no NI on %s\n",
1920 libcfs_nid2str(sd->sd_dst_nid),
1921 libcfs_net2str(sd->sd_best_ni->ni_net->net_id));
1923 return -EHOSTUNREACH;
1927 lnet_find_best_ni_on_spec_net(struct lnet_ni *cur_best_ni,
1928 struct lnet_peer *peer,
1929 struct lnet_peer_net *peer_net,
1933 struct lnet_net *local_net;
1934 struct lnet_ni *best_ni;
1936 local_net = lnet_get_net_locked(peer_net->lpn_net_id);
1941 * Iterate through the NIs in this local Net and select
1942 * the NI to send from. The selection is determined by
1943 * these 3 criterion in the following priority:
1945 * 2. NI available credits
1948 best_ni = lnet_get_best_ni(local_net, cur_best_ni,
1949 peer, peer_net, cpt);
1951 if (incr_seq && best_ni)
1958 lnet_initiate_peer_discovery(struct lnet_peer_ni *lpni,
1959 struct lnet_msg *msg, lnet_nid_t rtr_nid,
1962 struct lnet_peer *peer;
1963 lnet_nid_t primary_nid;
1966 lnet_peer_ni_addref_locked(lpni);
1968 peer = lpni->lpni_peer_net->lpn_peer;
1970 if (lnet_peer_gw_discovery(peer)) {
1971 lnet_peer_ni_decref_locked(lpni);
1975 if (!lnet_msg_discovery(msg) || lnet_peer_is_uptodate(peer)) {
1976 lnet_peer_ni_decref_locked(lpni);
1980 rc = lnet_discover_peer_locked(lpni, cpt, false);
1982 lnet_peer_ni_decref_locked(lpni);
1985 /* The peer may have changed. */
1986 peer = lpni->lpni_peer_net->lpn_peer;
1987 /* queue message and return */
1988 msg->msg_rtr_nid_param = rtr_nid;
1989 msg->msg_sending = 0;
1990 msg->msg_txpeer = NULL;
1991 spin_lock(&peer->lp_lock);
1992 list_add_tail(&msg->msg_list, &peer->lp_dc_pendq);
1993 spin_unlock(&peer->lp_lock);
1994 lnet_peer_ni_decref_locked(lpni);
1995 primary_nid = peer->lp_primary_nid;
1997 CDEBUG(D_NET, "msg %p delayed. %s pending discovery\n",
1998 msg, libcfs_nid2str(primary_nid));
2000 return LNET_DC_WAIT;
2004 lnet_handle_find_routed_path(struct lnet_send_data *sd,
2006 struct lnet_peer_ni **gw_lpni,
2007 struct lnet_peer **gw_peer)
2010 struct lnet_peer *gw;
2011 struct lnet_route *best_route;
2012 struct lnet_route *last_route;
2013 struct lnet_peer_ni *lpni = NULL;
2014 lnet_nid_t src_nid = sd->sd_src_nid;
2016 best_route = lnet_find_route_locked(NULL, LNET_NIDNET(dst_nid),
2017 sd->sd_rtr_nid, &last_route,
2020 CERROR("no route to %s from %s\n",
2021 libcfs_nid2str(dst_nid), libcfs_nid2str(src_nid));
2022 return -EHOSTUNREACH;
2026 CERROR("Internal Error. Route expected to %s from %s\n",
2027 libcfs_nid2str(dst_nid),
2028 libcfs_nid2str(src_nid));
2032 gw = best_route->lr_gateway;
2033 LASSERT(gw == lpni->lpni_peer_net->lpn_peer);
2036 * Discover this gateway if it hasn't already been discovered.
2037 * This means we might delay the message until discovery has
2040 sd->sd_msg->msg_src_nid_param = sd->sd_src_nid;
2041 rc = lnet_initiate_peer_discovery(lpni, sd->sd_msg, sd->sd_rtr_nid,
2046 if (!sd->sd_best_ni)
2047 sd->sd_best_ni = lnet_find_best_ni_on_spec_net(NULL, gw,
2048 lnet_peer_get_net_locked(gw,
2049 best_route->lr_lnet),
2053 if (!sd->sd_best_ni) {
2054 CERROR("Internal Error. Expected local ni on %s "
2055 "but non found :%s\n",
2056 libcfs_net2str(best_route->lr_lnet),
2057 libcfs_nid2str(sd->sd_src_nid));
2065 * increment the route sequence number since now we're sure we're
2068 LASSERT(best_route && last_route);
2069 best_route->lr_seq = last_route->lr_seq + 1;
2079 * Remote destination
2080 * Non-MR destination
2084 * Remote destination
2087 * The handling of these two cases is similar. Even though the destination
2088 * can be MR or non-MR, we'll deal directly with the router.
2091 lnet_handle_spec_router_dst(struct lnet_send_data *sd)
2094 struct lnet_peer_ni *gw_lpni = NULL;
2095 struct lnet_peer *gw_peer = NULL;
2098 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
2099 if (!sd->sd_best_ni) {
2100 CERROR("Can't send to %s: src %s is not a "
2101 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
2102 libcfs_nid2str(sd->sd_src_nid));
2106 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2111 if (sd->sd_send_case & NMR_DST)
2113 * since the final destination is non-MR let's set its preferred
2114 * NID before we send
2116 lnet_set_non_mr_pref_nid(sd);
2119 * We're going to send to the gw found so let's set its
2122 sd->sd_peer = gw_peer;
2123 sd->sd_best_lpni = gw_lpni;
2125 return lnet_handle_send(sd);
2129 lnet_find_best_ni_on_local_net(struct lnet_peer *peer, int md_cpt)
2131 struct lnet_peer_net *peer_net = NULL;
2132 struct lnet_ni *best_ni = NULL;
2135 * The peer can have multiple interfaces, some of them can be on
2136 * the local network and others on a routed network. We should
2137 * prefer the local network. However if the local network is not
2138 * available then we need to try the routed network
2141 /* go through all the peer nets and find the best_ni */
2142 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_peer_nets) {
2144 * The peer's list of nets can contain non-local nets. We
2145 * want to only examine the local ones.
2147 if (!lnet_get_net_locked(peer_net->lpn_net_id))
2149 best_ni = lnet_find_best_ni_on_spec_net(best_ni, peer,
2150 peer_net, md_cpt, false);
2154 /* increment sequence number so we can round robin */
2160 static struct lnet_ni *
2161 lnet_find_existing_preferred_best_ni(struct lnet_send_data *sd)
2163 struct lnet_ni *best_ni = NULL;
2164 struct lnet_peer_net *peer_net;
2165 struct lnet_peer *peer = sd->sd_peer;
2166 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
2167 struct lnet_peer_ni *lpni;
2168 int cpt = sd->sd_cpt;
2171 * We must use a consistent source address when sending to a
2172 * non-MR peer. However, a non-MR peer can have multiple NIDs
2173 * on multiple networks, and we may even need to talk to this
2174 * peer on multiple networks -- certain types of
2175 * load-balancing configuration do this.
2177 * So we need to pick the NI the peer prefers for this
2178 * particular network.
2181 /* Get the target peer_ni */
2182 peer_net = lnet_peer_get_net_locked(peer,
2183 LNET_NIDNET(best_lpni->lpni_nid));
2184 LASSERT(peer_net != NULL);
2185 list_for_each_entry(lpni, &peer_net->lpn_peer_nis,
2187 if (lpni->lpni_pref_nnids == 0)
2189 LASSERT(lpni->lpni_pref_nnids == 1);
2190 best_ni = lnet_nid2ni_locked(
2191 lpni->lpni_pref.nid, cpt);
2198 /* Prerequisite: sd->sd_peer and sd->sd_best_lpni should be set */
2200 lnet_select_preferred_best_ni(struct lnet_send_data *sd)
2202 struct lnet_ni *best_ni = NULL;
2203 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
2206 * We must use a consistent source address when sending to a
2207 * non-MR peer. However, a non-MR peer can have multiple NIDs
2208 * on multiple networks, and we may even need to talk to this
2209 * peer on multiple networks -- certain types of
2210 * load-balancing configuration do this.
2212 * So we need to pick the NI the peer prefers for this
2213 * particular network.
2216 best_ni = lnet_find_existing_preferred_best_ni(sd);
2218 /* if best_ni is still not set just pick one */
2221 lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2222 sd->sd_best_lpni->lpni_peer_net,
2223 sd->sd_md_cpt, true);
2224 /* If there is no best_ni we don't have a route */
2226 CERROR("no path to %s from net %s\n",
2227 libcfs_nid2str(best_lpni->lpni_nid),
2228 libcfs_net2str(best_lpni->lpni_net->net_id));
2229 return -EHOSTUNREACH;
2233 sd->sd_best_ni = best_ni;
2235 /* Set preferred NI if necessary. */
2236 lnet_set_non_mr_pref_nid(sd);
2243 * Source not specified
2247 * always use the same source NID for NMR peers
2248 * If we've talked to that peer before then we already have a preferred
2249 * source NI associated with it. Otherwise, we select a preferred local NI
2250 * and store it in the peer
2253 lnet_handle_any_local_nmr_dst(struct lnet_send_data *sd)
2257 /* sd->sd_best_lpni is already set to the final destination */
2260 * At this point we should've created the peer ni and peer. If we
2261 * can't find it, then something went wrong. Instead of assert
2262 * output a relevant message and fail the send
2264 if (!sd->sd_best_lpni) {
2265 CERROR("Internal fault. Unable to send msg %s to %s. "
2267 lnet_msgtyp2str(sd->sd_msg->msg_type),
2268 libcfs_nid2str(sd->sd_dst_nid));
2272 rc = lnet_select_preferred_best_ni(sd);
2274 rc = lnet_handle_send(sd);
2280 lnet_handle_any_mr_dsta(struct lnet_send_data *sd)
2283 * NOTE we've already handled the remote peer case. So we only
2284 * need to worry about the local case here.
2286 * if we're sending a response, ACK or reply, we need to send it
2287 * to the destination NID given to us. At this point we already
2288 * have the peer_ni we're suppose to send to, so just find the
2289 * best_ni on the peer net and use that. Since we're sending to an
2290 * MR peer then we can just run the selection algorithm on our
2291 * local NIs and pick the best one.
2293 if (sd->sd_send_case & SND_RESP) {
2295 lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2296 sd->sd_best_lpni->lpni_peer_net,
2297 sd->sd_md_cpt, true);
2299 if (!sd->sd_best_ni) {
2301 * We're not going to deal with not able to send
2302 * a response to the provided final destination
2304 CERROR("Can't send response to %s. "
2305 "No local NI available\n",
2306 libcfs_nid2str(sd->sd_dst_nid));
2307 return -EHOSTUNREACH;
2310 return lnet_handle_send(sd);
2314 * If we get here that means we're sending a fresh request, PUT or
2315 * GET, so we need to run our standard selection algorithm.
2316 * First find the best local interface that's on any of the peer's
2319 sd->sd_best_ni = lnet_find_best_ni_on_local_net(sd->sd_peer,
2321 if (sd->sd_best_ni) {
2323 lnet_find_best_lpni_on_net(sd, sd->sd_peer,
2324 sd->sd_best_ni->ni_net->net_id);
2327 * if we're successful in selecting a peer_ni on the local
2328 * network, then send to it. Otherwise fall through and
2329 * try and see if we can reach it over another routed
2332 if (sd->sd_best_lpni &&
2333 sd->sd_best_lpni->lpni_nid == the_lnet.ln_loni->ni_nid) {
2335 * in case we initially started with a routed
2336 * destination, let's reset to local
2338 sd->sd_send_case &= ~REMOTE_DST;
2339 sd->sd_send_case |= LOCAL_DST;
2340 return lnet_handle_lo_send(sd);
2341 } else if (sd->sd_best_lpni) {
2343 * in case we initially started with a routed
2344 * destination, let's reset to local
2346 sd->sd_send_case &= ~REMOTE_DST;
2347 sd->sd_send_case |= LOCAL_DST;
2348 return lnet_handle_send(sd);
2351 CERROR("Internal Error. Expected to have a best_lpni: "
2353 libcfs_nid2str(sd->sd_src_nid),
2354 libcfs_nid2str(sd->sd_dst_nid));
2360 * Peer doesn't have a local network. Let's see if there is
2361 * a remote network we can reach it on.
2363 return PASS_THROUGH;
2368 * Source NID not specified
2373 * Source NID not speified
2374 * Remote destination
2377 * In both of these cases if we're sending a response, ACK or REPLY, then
2378 * we need to send to the destination NID provided.
2380 * In the remote case let's deal with MR routers.
2385 lnet_handle_any_mr_dst(struct lnet_send_data *sd)
2388 struct lnet_peer *gw_peer = NULL;
2389 struct lnet_peer_ni *gw_lpni = NULL;
2392 * handle sending a response to a remote peer here so we don't
2393 * have to worry about it if we hit lnet_handle_any_mr_dsta()
2395 if (sd->sd_send_case & REMOTE_DST &&
2396 sd->sd_send_case & SND_RESP) {
2397 struct lnet_peer_ni *gw;
2398 struct lnet_peer *gw_peer;
2400 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw,
2403 CERROR("Can't send response to %s. "
2404 "No route available\n",
2405 libcfs_nid2str(sd->sd_dst_nid));
2406 return -EHOSTUNREACH;
2407 } else if (rc > 0) {
2411 sd->sd_best_lpni = gw;
2412 sd->sd_peer = gw_peer;
2414 return lnet_handle_send(sd);
2418 * Even though the NID for the peer might not be on a local network,
2419 * since the peer is MR there could be other interfaces on the
2420 * local network. In that case we'd still like to prefer the local
2421 * network over the routed network. If we're unable to do that
2422 * then we select the best router among the different routed networks,
2423 * and if the router is MR then we can deal with it as such.
2425 rc = lnet_handle_any_mr_dsta(sd);
2426 if (rc != PASS_THROUGH)
2430 * TODO; One possible enhancement is to run the selection
2431 * algorithm on the peer. However for remote peers the credits are
2432 * not decremented, so we'll be basically going over the peer NIs
2433 * in round robin. An MR router will run the selection algorithm
2434 * on the next-hop interfaces.
2436 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2441 sd->sd_send_case &= ~LOCAL_DST;
2442 sd->sd_send_case |= REMOTE_DST;
2444 sd->sd_peer = gw_peer;
2445 sd->sd_best_lpni = gw_lpni;
2447 return lnet_handle_send(sd);
2451 * Source not specified
2452 * Remote destination
2455 * Must send to the specified peer NID using the same source NID that
2456 * we've used before. If it's the first time to talk to that peer then
2457 * find the source NI and assign it as preferred to that peer
2460 lnet_handle_any_router_nmr_dst(struct lnet_send_data *sd)
2463 struct lnet_peer_ni *gw_lpni = NULL;
2464 struct lnet_peer *gw_peer = NULL;
2467 * Let's set if we have a preferred NI to talk to this NMR peer
2469 sd->sd_best_ni = lnet_find_existing_preferred_best_ni(sd);
2472 * find the router and that'll find the best NI if we didn't find
2475 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2481 * set the best_ni we've chosen as the preferred one for
2484 lnet_set_non_mr_pref_nid(sd);
2486 /* we'll be sending to the gw */
2487 sd->sd_best_lpni = gw_lpni;
2488 sd->sd_peer = gw_peer;
2490 return lnet_handle_send(sd);
2494 lnet_handle_send_case_locked(struct lnet_send_data *sd)
2497 * turn off the SND_RESP bit.
2498 * It will be checked in the case handling
2500 __u32 send_case = sd->sd_send_case &= ~SND_RESP ;
2502 CDEBUG(D_NET, "Source %s%s to %s %s %s destination\n",
2503 (send_case & SRC_SPEC) ? "Specified: " : "ANY",
2504 (send_case & SRC_SPEC) ? libcfs_nid2str(sd->sd_src_nid) : "",
2505 (send_case & MR_DST) ? "MR: " : "NMR: ",
2506 libcfs_nid2str(sd->sd_dst_nid),
2507 (send_case & LOCAL_DST) ? "local" : "routed");
2509 switch (send_case) {
2511 * For all cases where the source is specified, we should always
2512 * use the destination NID, whether it's an MR destination or not,
2513 * since we're continuing a series of related messages for the
2516 case SRC_SPEC_LOCAL_NMR_DST:
2517 return lnet_handle_spec_local_nmr_dst(sd);
2518 case SRC_SPEC_LOCAL_MR_DST:
2519 return lnet_handle_spec_local_mr_dst(sd);
2520 case SRC_SPEC_ROUTER_NMR_DST:
2521 case SRC_SPEC_ROUTER_MR_DST:
2522 return lnet_handle_spec_router_dst(sd);
2523 case SRC_ANY_LOCAL_NMR_DST:
2524 return lnet_handle_any_local_nmr_dst(sd);
2525 case SRC_ANY_LOCAL_MR_DST:
2526 case SRC_ANY_ROUTER_MR_DST:
2527 return lnet_handle_any_mr_dst(sd);
2528 case SRC_ANY_ROUTER_NMR_DST:
2529 return lnet_handle_any_router_nmr_dst(sd);
2531 CERROR("Unknown send case\n");
2537 lnet_select_pathway(lnet_nid_t src_nid, lnet_nid_t dst_nid,
2538 struct lnet_msg *msg, lnet_nid_t rtr_nid)
2540 struct lnet_peer_ni *lpni;
2541 struct lnet_peer *peer;
2542 struct lnet_send_data send_data;
2545 __u32 send_case = 0;
2547 memset(&send_data, 0, sizeof(send_data));
2550 * get an initial CPT to use for locking. The idea here is not to
2551 * serialize the calls to select_pathway, so that as many
2552 * operations can run concurrently as possible. To do that we use
2553 * the CPT where this call is being executed. Later on when we
2554 * determine the CPT to use in lnet_message_commit, we switch the
2555 * lock and check if there was any configuration change. If none,
2556 * then we proceed, if there is, then we restart the operation.
2558 cpt = lnet_net_lock_current();
2560 md_cpt = lnet_cpt_of_md(msg->msg_md, msg->msg_offset);
2561 if (md_cpt == CFS_CPT_ANY)
2567 * If we're being asked to send to the loopback interface, there
2568 * is no need to go through any selection. We can just shortcut
2569 * the entire process and send over lolnd
2571 send_data.sd_msg = msg;
2572 send_data.sd_cpt = cpt;
2573 if (LNET_NETTYP(LNET_NIDNET(dst_nid)) == LOLND) {
2574 rc = lnet_handle_lo_send(&send_data);
2575 lnet_net_unlock(cpt);
2580 * find an existing peer_ni, or create one and mark it as having been
2581 * created due to network traffic. This call will create the
2582 * peer->peer_net->peer_ni tree.
2584 lpni = lnet_nid2peerni_locked(dst_nid, LNET_NID_ANY, cpt);
2586 lnet_net_unlock(cpt);
2587 return PTR_ERR(lpni);
2591 * Cache the original src_nid. If we need to resend the message
2592 * then we'll need to know whether the src_nid was originally
2593 * specified for this message. If it was originally specified,
2594 * then we need to keep using the same src_nid since it's
2595 * continuing the same sequence of messages.
2597 msg->msg_src_nid_param = src_nid;
2600 * Now that we have a peer_ni, check if we want to discover
2601 * the peer. Traffic to the LNET_RESERVED_PORTAL should not
2602 * trigger discovery.
2604 peer = lpni->lpni_peer_net->lpn_peer;
2605 rc = lnet_initiate_peer_discovery(lpni, msg, rtr_nid, cpt);
2607 lnet_peer_ni_decref_locked(lpni);
2608 lnet_net_unlock(cpt);
2611 lnet_peer_ni_decref_locked(lpni);
2614 * Identify the different send cases
2616 if (src_nid == LNET_NID_ANY)
2617 send_case |= SRC_ANY;
2619 send_case |= SRC_SPEC;
2621 if (lnet_get_net_locked(LNET_NIDNET(dst_nid)))
2622 send_case |= LOCAL_DST;
2624 send_case |= REMOTE_DST;
2627 * if this is a non-MR peer or if we're recovering a peer ni then
2628 * let's consider this an NMR case so we can hit the destination
2631 if (!lnet_peer_is_multi_rail(peer) || msg->msg_recovery)
2632 send_case |= NMR_DST;
2634 send_case |= MR_DST;
2636 if (msg->msg_type == LNET_MSG_REPLY ||
2637 msg->msg_type == LNET_MSG_ACK)
2638 send_case |= SND_RESP;
2640 /* assign parameters to the send_data */
2641 send_data.sd_rtr_nid = rtr_nid;
2642 send_data.sd_src_nid = src_nid;
2643 send_data.sd_dst_nid = dst_nid;
2644 send_data.sd_best_lpni = lpni;
2646 * keep a pointer to the final destination in case we're going to
2647 * route, so we'll need to access it later
2649 send_data.sd_final_dst_lpni = lpni;
2650 send_data.sd_peer = peer;
2651 send_data.sd_md_cpt = md_cpt;
2652 send_data.sd_send_case = send_case;
2654 rc = lnet_handle_send_case_locked(&send_data);
2657 * Update the local cpt since send_data.sd_cpt might've been
2658 * updated as a result of calling lnet_handle_send_case_locked().
2660 cpt = send_data.sd_cpt;
2662 if (rc == REPEAT_SEND)
2665 lnet_net_unlock(cpt);
2671 lnet_send(lnet_nid_t src_nid, struct lnet_msg *msg, lnet_nid_t rtr_nid)
2673 lnet_nid_t dst_nid = msg->msg_target.nid;
2677 * NB: rtr_nid is set to LNET_NID_ANY for all current use-cases,
2678 * but we might want to use pre-determined router for ACK/REPLY
2681 /* NB: ni != NULL == interface pre-determined (ACK/REPLY) */
2682 LASSERT(msg->msg_txpeer == NULL);
2683 LASSERT(msg->msg_txni == NULL);
2684 LASSERT(!msg->msg_sending);
2685 LASSERT(!msg->msg_target_is_router);
2686 LASSERT(!msg->msg_receiving);
2688 msg->msg_sending = 1;
2690 LASSERT(!msg->msg_tx_committed);
2692 rc = lnet_select_pathway(src_nid, dst_nid, msg, rtr_nid);
2696 if (rc == LNET_CREDIT_OK)
2697 lnet_ni_send(msg->msg_txni, msg);
2699 /* rc == LNET_CREDIT_OK or LNET_CREDIT_WAIT or LNET_DC_WAIT */
2703 enum lnet_mt_event_type {
2704 MT_TYPE_LOCAL_NI = 0,
2708 struct lnet_mt_event_info {
2709 enum lnet_mt_event_type mt_type;
2713 /* called with res_lock held */
2715 lnet_detach_rsp_tracker(struct lnet_libmd *md, int cpt)
2717 struct lnet_rsp_tracker *rspt;
2720 * msg has a refcount on the MD so the MD is not going away.
2721 * The rspt queue for the cpt is protected by
2722 * the lnet_net_lock(cpt). cpt is the cpt of the MD cookie.
2724 if (!md->md_rspt_ptr)
2727 rspt = md->md_rspt_ptr;
2728 md->md_rspt_ptr = NULL;
2731 LASSERT(rspt->rspt_cpt == cpt);
2734 * invalidate the handle to indicate that a response has been
2735 * received, which will then lead the monitor thread to clean up
2738 LNetInvalidateMDHandle(&rspt->rspt_mdh);
2742 lnet_finalize_expired_responses(bool force)
2744 struct lnet_libmd *md;
2745 struct list_head local_queue;
2746 struct lnet_rsp_tracker *rspt, *tmp;
2749 if (the_lnet.ln_mt_rstq == NULL)
2752 cfs_cpt_for_each(i, lnet_cpt_table()) {
2753 INIT_LIST_HEAD(&local_queue);
2756 if (!the_lnet.ln_mt_rstq[i]) {
2760 list_splice_init(the_lnet.ln_mt_rstq[i], &local_queue);
2763 list_for_each_entry_safe(rspt, tmp, &local_queue, rspt_on_list) {
2765 * The rspt mdh will be invalidated when a response
2766 * is received or whenever we want to discard the
2767 * block the monitor thread will walk the queue
2768 * and clean up any rsts with an invalid mdh.
2769 * The monitor thread will walk the queue until
2770 * the first unexpired rspt block. This means that
2771 * some rspt blocks which received their
2772 * corresponding responses will linger in the
2773 * queue until they are cleaned up eventually.
2776 if (LNetMDHandleIsInvalid(rspt->rspt_mdh)) {
2778 list_del_init(&rspt->rspt_on_list);
2779 lnet_rspt_free(rspt, i);
2783 if (ktime_compare(ktime_get(), rspt->rspt_deadline) >= 0 ||
2785 struct lnet_peer_ni *lpni;
2788 md = lnet_handle2md(&rspt->rspt_mdh);
2790 LNetInvalidateMDHandle(&rspt->rspt_mdh);
2792 list_del_init(&rspt->rspt_on_list);
2793 lnet_rspt_free(rspt, i);
2796 LASSERT(md->md_rspt_ptr == rspt);
2797 md->md_rspt_ptr = NULL;
2801 the_lnet.ln_counters[i]->lct_health.lch_response_timeout_count++;
2804 list_del_init(&rspt->rspt_on_list);
2806 nid = rspt->rspt_next_hop_nid;
2808 CNETERR("Response timed out: md = %p: nid = %s\n",
2809 md, libcfs_nid2str(nid));
2810 LNetMDUnlink(rspt->rspt_mdh);
2811 lnet_rspt_free(rspt, i);
2814 * If there is a timeout on the response
2815 * from the next hop decrement its health
2816 * value so that we don't use it
2819 lpni = lnet_find_peer_ni_locked(nid);
2821 lnet_handle_remote_failure_locked(lpni);
2822 lnet_peer_ni_decref_locked(lpni);
2832 if (!list_empty(&local_queue))
2833 list_splice(&local_queue, the_lnet.ln_mt_rstq[i]);
2839 lnet_resend_pending_msgs_locked(struct list_head *resendq, int cpt)
2841 struct lnet_msg *msg;
2843 while (!list_empty(resendq)) {
2844 struct lnet_peer_ni *lpni;
2846 msg = list_entry(resendq->next, struct lnet_msg,
2849 list_del_init(&msg->msg_list);
2851 lpni = lnet_find_peer_ni_locked(msg->msg_hdr.dest_nid);
2853 lnet_net_unlock(cpt);
2854 CERROR("Expected that a peer is already created for %s\n",
2855 libcfs_nid2str(msg->msg_hdr.dest_nid));
2856 msg->msg_no_resend = true;
2857 lnet_finalize(msg, -EFAULT);
2860 struct lnet_peer *peer;
2862 lnet_nid_t src_nid = LNET_NID_ANY;
2865 * if this message is not being routed and the
2866 * peer is non-MR then we must use the same
2867 * src_nid that was used in the original send.
2868 * Otherwise if we're routing the message (IE
2869 * we're a router) then we can use any of our
2870 * local interfaces. It doesn't matter to the
2871 * final destination.
2873 peer = lpni->lpni_peer_net->lpn_peer;
2874 if (!msg->msg_routing &&
2875 !lnet_peer_is_multi_rail(peer))
2876 src_nid = le64_to_cpu(msg->msg_hdr.src_nid);
2879 * If we originally specified a src NID, then we
2880 * must attempt to reuse it in the resend as well.
2882 if (msg->msg_src_nid_param != LNET_NID_ANY)
2883 src_nid = msg->msg_src_nid_param;
2884 lnet_peer_ni_decref_locked(lpni);
2886 lnet_net_unlock(cpt);
2887 CDEBUG(D_NET, "resending %s->%s: %s recovery %d try# %d\n",
2888 libcfs_nid2str(src_nid),
2889 libcfs_id2str(msg->msg_target),
2890 lnet_msgtyp2str(msg->msg_type),
2892 msg->msg_retry_count);
2893 rc = lnet_send(src_nid, msg, LNET_NID_ANY);
2895 CERROR("Error sending %s to %s: %d\n",
2896 lnet_msgtyp2str(msg->msg_type),
2897 libcfs_id2str(msg->msg_target), rc);
2898 msg->msg_no_resend = true;
2899 lnet_finalize(msg, rc);
2903 the_lnet.ln_counters[cpt]->lct_health.lch_resend_count++;
2909 lnet_resend_pending_msgs(void)
2913 cfs_cpt_for_each(i, lnet_cpt_table()) {
2915 lnet_resend_pending_msgs_locked(the_lnet.ln_mt_resendqs[i], i);
2920 /* called with cpt and ni_lock held */
2922 lnet_unlink_ni_recovery_mdh_locked(struct lnet_ni *ni, int cpt, bool force)
2924 struct lnet_handle_md recovery_mdh;
2926 LNetInvalidateMDHandle(&recovery_mdh);
2928 if (ni->ni_recovery_state & LNET_NI_RECOVERY_PENDING ||
2930 recovery_mdh = ni->ni_ping_mdh;
2931 LNetInvalidateMDHandle(&ni->ni_ping_mdh);
2934 lnet_net_unlock(cpt);
2935 if (!LNetMDHandleIsInvalid(recovery_mdh))
2936 LNetMDUnlink(recovery_mdh);
2942 lnet_recover_local_nis(void)
2944 struct lnet_mt_event_info *ev_info;
2945 struct list_head processed_list;
2946 struct list_head local_queue;
2947 struct lnet_handle_md mdh;
2948 struct lnet_ni *tmp;
2954 INIT_LIST_HEAD(&local_queue);
2955 INIT_LIST_HEAD(&processed_list);
2958 * splice the recovery queue on a local queue. We will iterate
2959 * through the local queue and update it as needed. Once we're
2960 * done with the traversal, we'll splice the local queue back on
2961 * the head of the ln_mt_localNIRecovq. Any newly added local NIs
2962 * will be traversed in the next iteration.
2965 list_splice_init(&the_lnet.ln_mt_localNIRecovq,
2969 list_for_each_entry_safe(ni, tmp, &local_queue, ni_recovery) {
2971 * if an NI is being deleted or it is now healthy, there
2972 * is no need to keep it around in the recovery queue.
2973 * The monitor thread is the only thread responsible for
2974 * removing the NI from the recovery queue.
2975 * Multiple threads can be adding NIs to the recovery
2978 healthv = atomic_read(&ni->ni_healthv);
2982 if (ni->ni_state != LNET_NI_STATE_ACTIVE ||
2983 healthv == LNET_MAX_HEALTH_VALUE) {
2984 list_del_init(&ni->ni_recovery);
2985 lnet_unlink_ni_recovery_mdh_locked(ni, 0, false);
2987 lnet_ni_decref_locked(ni, 0);
2993 * if the local NI failed recovery we must unlink the md.
2994 * But we want to keep the local_ni on the recovery queue
2995 * so we can continue the attempts to recover it.
2997 if (ni->ni_recovery_state & LNET_NI_RECOVERY_FAILED) {
2998 lnet_unlink_ni_recovery_mdh_locked(ni, 0, true);
2999 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_FAILED;
3006 CDEBUG(D_NET, "attempting to recover local ni: %s\n",
3007 libcfs_nid2str(ni->ni_nid));
3010 if (!(ni->ni_recovery_state & LNET_NI_RECOVERY_PENDING)) {
3011 ni->ni_recovery_state |= LNET_NI_RECOVERY_PENDING;
3014 LIBCFS_ALLOC(ev_info, sizeof(*ev_info));
3016 CERROR("out of memory. Can't recover %s\n",
3017 libcfs_nid2str(ni->ni_nid));
3019 ni->ni_recovery_state &=
3020 ~LNET_NI_RECOVERY_PENDING;
3025 mdh = ni->ni_ping_mdh;
3027 * Invalidate the ni mdh in case it's deleted.
3028 * We'll unlink the mdh in this case below.
3030 LNetInvalidateMDHandle(&ni->ni_ping_mdh);
3034 * remove the NI from the local queue and drop the
3035 * reference count to it while we're recovering
3036 * it. The reason for that, is that the NI could
3037 * be deleted, and the way the code is structured
3038 * is if we don't drop the NI, then the deletion
3039 * code will enter a loop waiting for the
3040 * reference count to be removed while holding the
3041 * ln_mutex_lock(). When we look up the peer to
3042 * send to in lnet_select_pathway() we will try to
3043 * lock the ln_mutex_lock() as well, leading to
3044 * a deadlock. By dropping the refcount and
3045 * removing it from the list, we allow for the NI
3046 * to be removed, then we use the cached NID to
3047 * look it up again. If it's gone, then we just
3048 * continue examining the rest of the queue.
3051 list_del_init(&ni->ni_recovery);
3052 lnet_ni_decref_locked(ni, 0);
3055 ev_info->mt_type = MT_TYPE_LOCAL_NI;
3056 ev_info->mt_nid = nid;
3057 rc = lnet_send_ping(nid, &mdh, LNET_INTERFACES_MIN,
3058 ev_info, the_lnet.ln_mt_eqh, true);
3059 /* lookup the nid again */
3061 ni = lnet_nid2ni_locked(nid, 0);
3064 * the NI has been deleted when we dropped
3072 * Same note as in lnet_recover_peer_nis(). When
3073 * we're sending the ping, the NI is free to be
3074 * deleted or manipulated. By this point it
3075 * could've been added back on the recovery queue,
3076 * and a refcount taken on it.
3077 * So we can't just add it blindly again or we'll
3078 * corrupt the queue. We must check under lock if
3079 * it's not on any list and if not then add it
3080 * to the processed list, which will eventually be
3081 * spliced back on to the recovery queue.
3083 ni->ni_ping_mdh = mdh;
3084 if (list_empty(&ni->ni_recovery)) {
3085 list_add_tail(&ni->ni_recovery, &processed_list);
3086 lnet_ni_addref_locked(ni, 0);
3092 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_PENDING;
3098 * put back the remaining NIs on the ln_mt_localNIRecovq to be
3099 * reexamined in the next iteration.
3101 list_splice_init(&processed_list, &local_queue);
3103 list_splice(&local_queue, &the_lnet.ln_mt_localNIRecovq);
3107 static struct list_head **
3108 lnet_create_array_of_queues(void)
3110 struct list_head **qs;
3111 struct list_head *q;
3114 qs = cfs_percpt_alloc(lnet_cpt_table(),
3115 sizeof(struct list_head));
3117 CERROR("Failed to allocate queues\n");
3121 cfs_percpt_for_each(q, i, qs)
3128 lnet_resendqs_create(void)
3130 struct list_head **resendqs;
3131 resendqs = lnet_create_array_of_queues();
3136 lnet_net_lock(LNET_LOCK_EX);
3137 the_lnet.ln_mt_resendqs = resendqs;
3138 lnet_net_unlock(LNET_LOCK_EX);
3144 lnet_clean_local_ni_recoveryq(void)
3148 /* This is only called when the monitor thread has stopped */
3151 while (!list_empty(&the_lnet.ln_mt_localNIRecovq)) {
3152 ni = list_entry(the_lnet.ln_mt_localNIRecovq.next,
3153 struct lnet_ni, ni_recovery);
3154 list_del_init(&ni->ni_recovery);
3156 lnet_unlink_ni_recovery_mdh_locked(ni, 0, true);
3158 lnet_ni_decref_locked(ni, 0);
3165 lnet_unlink_lpni_recovery_mdh_locked(struct lnet_peer_ni *lpni, int cpt,
3168 struct lnet_handle_md recovery_mdh;
3170 LNetInvalidateMDHandle(&recovery_mdh);
3172 if (lpni->lpni_state & LNET_PEER_NI_RECOVERY_PENDING || force) {
3173 recovery_mdh = lpni->lpni_recovery_ping_mdh;
3174 LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
3176 spin_unlock(&lpni->lpni_lock);
3177 lnet_net_unlock(cpt);
3178 if (!LNetMDHandleIsInvalid(recovery_mdh))
3179 LNetMDUnlink(recovery_mdh);
3181 spin_lock(&lpni->lpni_lock);
3185 lnet_clean_peer_ni_recoveryq(void)
3187 struct lnet_peer_ni *lpni, *tmp;
3189 lnet_net_lock(LNET_LOCK_EX);
3191 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_mt_peerNIRecovq,
3193 list_del_init(&lpni->lpni_recovery);
3194 spin_lock(&lpni->lpni_lock);
3195 lnet_unlink_lpni_recovery_mdh_locked(lpni, LNET_LOCK_EX, true);
3196 spin_unlock(&lpni->lpni_lock);
3197 lnet_peer_ni_decref_locked(lpni);
3200 lnet_net_unlock(LNET_LOCK_EX);
3204 lnet_clean_resendqs(void)
3206 struct lnet_msg *msg, *tmp;
3207 struct list_head msgs;
3210 INIT_LIST_HEAD(&msgs);
3212 cfs_cpt_for_each(i, lnet_cpt_table()) {
3214 list_splice_init(the_lnet.ln_mt_resendqs[i], &msgs);
3216 list_for_each_entry_safe(msg, tmp, &msgs, msg_list) {
3217 list_del_init(&msg->msg_list);
3218 msg->msg_no_resend = true;
3219 lnet_finalize(msg, -ESHUTDOWN);
3223 cfs_percpt_free(the_lnet.ln_mt_resendqs);
3227 lnet_recover_peer_nis(void)
3229 struct lnet_mt_event_info *ev_info;
3230 struct list_head processed_list;
3231 struct list_head local_queue;
3232 struct lnet_handle_md mdh;
3233 struct lnet_peer_ni *lpni;
3234 struct lnet_peer_ni *tmp;
3239 INIT_LIST_HEAD(&local_queue);
3240 INIT_LIST_HEAD(&processed_list);
3243 * Always use cpt 0 for locking across all interactions with
3244 * ln_mt_peerNIRecovq
3247 list_splice_init(&the_lnet.ln_mt_peerNIRecovq,
3251 list_for_each_entry_safe(lpni, tmp, &local_queue,
3254 * The same protection strategy is used here as is in the
3255 * local recovery case.
<