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/
31 * lnet/lnet/lib-move.c
33 * Data movement routines
36 #define DEBUG_SUBSYSTEM S_LNET
38 #include <linux/pagemap.h>
40 #include <lnet/lib-lnet.h>
41 #include <linux/nsproxy.h>
42 #include <lnet/lnet_rdma.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;
67 lnet_msg_is_response(struct lnet_msg *msg)
69 return msg->msg_type == LNET_MSG_ACK || msg->msg_type == LNET_MSG_REPLY;
73 lnet_response_tracking_enabled(__u32 msg_type, unsigned int md_options)
75 if (md_options & LNET_MD_NO_TRACK_RESPONSE)
76 /* Explicitly disabled in MD options */
79 if (md_options & LNET_MD_TRACK_RESPONSE)
80 /* Explicity enabled in MD options */
83 if (lnet_response_tracking == 3)
84 /* Enabled for all message types */
87 if (msg_type == LNET_MSG_PUT)
88 return lnet_response_tracking == 2;
90 if (msg_type == LNET_MSG_GET)
91 return lnet_response_tracking == 1;
96 static inline struct lnet_comm_count *
97 get_stats_counts(struct lnet_element_stats *stats,
98 enum lnet_stats_type stats_type)
100 switch (stats_type) {
101 case LNET_STATS_TYPE_SEND:
102 return &stats->el_send_stats;
103 case LNET_STATS_TYPE_RECV:
104 return &stats->el_recv_stats;
105 case LNET_STATS_TYPE_DROP:
106 return &stats->el_drop_stats;
108 CERROR("Unknown stats type\n");
114 void lnet_incr_stats(struct lnet_element_stats *stats,
115 enum lnet_msg_type msg_type,
116 enum lnet_stats_type stats_type)
118 struct lnet_comm_count *counts = get_stats_counts(stats, stats_type);
124 atomic_inc(&counts->co_ack_count);
127 atomic_inc(&counts->co_put_count);
130 atomic_inc(&counts->co_get_count);
133 atomic_inc(&counts->co_reply_count);
136 atomic_inc(&counts->co_hello_count);
139 CERROR("There is a BUG in the code. Unknown message type\n");
144 __u32 lnet_sum_stats(struct lnet_element_stats *stats,
145 enum lnet_stats_type stats_type)
147 struct lnet_comm_count *counts = get_stats_counts(stats, stats_type);
151 return (atomic_read(&counts->co_ack_count) +
152 atomic_read(&counts->co_put_count) +
153 atomic_read(&counts->co_get_count) +
154 atomic_read(&counts->co_reply_count) +
155 atomic_read(&counts->co_hello_count));
158 static inline void assign_stats(struct lnet_ioctl_comm_count *msg_stats,
159 struct lnet_comm_count *counts)
161 msg_stats->ico_get_count = atomic_read(&counts->co_get_count);
162 msg_stats->ico_put_count = atomic_read(&counts->co_put_count);
163 msg_stats->ico_reply_count = atomic_read(&counts->co_reply_count);
164 msg_stats->ico_ack_count = atomic_read(&counts->co_ack_count);
165 msg_stats->ico_hello_count = atomic_read(&counts->co_hello_count);
168 void lnet_usr_translate_stats(struct lnet_ioctl_element_msg_stats *msg_stats,
169 struct lnet_element_stats *stats)
171 struct lnet_comm_count *counts;
176 counts = get_stats_counts(stats, LNET_STATS_TYPE_SEND);
179 assign_stats(&msg_stats->im_send_stats, counts);
181 counts = get_stats_counts(stats, LNET_STATS_TYPE_RECV);
184 assign_stats(&msg_stats->im_recv_stats, counts);
186 counts = get_stats_counts(stats, LNET_STATS_TYPE_DROP);
189 assign_stats(&msg_stats->im_drop_stats, counts);
193 lnet_fail_nid(lnet_nid_t nid, unsigned int threshold)
195 struct lnet_test_peer *tp;
196 struct list_head *el;
197 struct list_head *next;
200 /* NB: use lnet_net_lock(0) to serialize operations on test peers */
201 if (threshold != 0) {
202 /* Adding a new entry */
203 LIBCFS_ALLOC(tp, sizeof(*tp));
208 tp->tp_threshold = threshold;
211 list_add_tail(&tp->tp_list, &the_lnet.ln_test_peers);
218 list_for_each_safe(el, next, &the_lnet.ln_test_peers) {
219 tp = list_entry(el, struct lnet_test_peer, tp_list);
221 if (tp->tp_threshold == 0 || /* needs culling anyway */
222 nid == LNET_NID_ANY || /* removing all entries */
223 tp->tp_nid == nid) { /* matched this one */
224 list_move(&tp->tp_list, &cull);
230 while (!list_empty(&cull)) {
231 tp = list_entry(cull.next, struct lnet_test_peer, tp_list);
233 list_del(&tp->tp_list);
234 LIBCFS_FREE(tp, sizeof(*tp));
240 fail_peer (lnet_nid_t nid, int outgoing)
242 struct lnet_test_peer *tp;
243 struct list_head *el;
244 struct list_head *next;
248 /* NB: use lnet_net_lock(0) to serialize operations on test peers */
251 list_for_each_safe(el, next, &the_lnet.ln_test_peers) {
252 tp = list_entry(el, struct lnet_test_peer, tp_list);
254 if (tp->tp_threshold == 0) {
257 /* only cull zombies on outgoing tests,
258 * since we may be at interrupt priority on
259 * incoming messages. */
260 list_move(&tp->tp_list, &cull);
265 if (tp->tp_nid == LNET_NID_ANY || /* fail every peer */
266 nid == tp->tp_nid) { /* fail this peer */
269 if (tp->tp_threshold != LNET_MD_THRESH_INF) {
272 tp->tp_threshold == 0) {
274 list_move(&tp->tp_list, &cull);
283 while (!list_empty(&cull)) {
284 tp = list_entry(cull.next, struct lnet_test_peer, tp_list);
285 list_del(&tp->tp_list);
287 LIBCFS_FREE(tp, sizeof(*tp));
294 lnet_iov_nob(unsigned int niov, struct kvec *iov)
296 unsigned int nob = 0;
298 LASSERT(niov == 0 || iov != NULL);
300 nob += (iov++)->iov_len;
304 EXPORT_SYMBOL(lnet_iov_nob);
307 lnet_copy_iov2iov(unsigned int ndiov, struct kvec *diov, unsigned int doffset,
308 unsigned int nsiov, struct kvec *siov, unsigned int soffset,
311 /* NB diov, siov are READ-ONLY */
312 unsigned int this_nob;
317 /* skip complete frags before 'doffset' */
319 while (doffset >= diov->iov_len) {
320 doffset -= diov->iov_len;
326 /* skip complete frags before 'soffset' */
328 while (soffset >= siov->iov_len) {
329 soffset -= siov->iov_len;
338 this_nob = min3((unsigned int)diov->iov_len - doffset,
339 (unsigned int)siov->iov_len - soffset,
342 memcpy((char *)diov->iov_base + doffset,
343 (char *)siov->iov_base + soffset, this_nob);
346 if (diov->iov_len > doffset + this_nob) {
354 if (siov->iov_len > soffset + this_nob) {
363 EXPORT_SYMBOL(lnet_copy_iov2iov);
366 lnet_kiov_nob(unsigned int niov, struct bio_vec *kiov)
368 unsigned int nob = 0;
370 LASSERT(niov == 0 || kiov != NULL);
372 nob += (kiov++)->bv_len;
376 EXPORT_SYMBOL(lnet_kiov_nob);
379 lnet_copy_kiov2kiov(unsigned int ndiov, struct bio_vec *diov,
380 unsigned int doffset,
381 unsigned int nsiov, struct bio_vec *siov,
382 unsigned int soffset,
385 /* NB diov, siov are READ-ONLY */
386 unsigned int this_nob;
393 LASSERT (!in_interrupt ());
396 while (doffset >= diov->bv_len) {
397 doffset -= diov->bv_len;
404 while (soffset >= siov->bv_len) {
405 soffset -= siov->bv_len;
414 this_nob = min3(diov->bv_len - doffset,
415 siov->bv_len - soffset,
419 daddr = ((char *)kmap(diov->bv_page)) +
420 diov->bv_offset + doffset;
422 saddr = ((char *)kmap(siov->bv_page)) +
423 siov->bv_offset + soffset;
425 /* Vanishing risk of kmap deadlock when mapping 2 pages.
426 * However in practice at least one of the kiovs will be mapped
427 * kernel pages and the map/unmap will be NOOPs */
429 memcpy (daddr, saddr, this_nob);
432 if (diov->bv_len > doffset + this_nob) {
436 kunmap(diov->bv_page);
443 if (siov->bv_len > soffset + this_nob) {
447 kunmap(siov->bv_page);
456 kunmap(diov->bv_page);
458 kunmap(siov->bv_page);
460 EXPORT_SYMBOL(lnet_copy_kiov2kiov);
463 lnet_copy_kiov2iov (unsigned int niov, struct kvec *iov, unsigned int iovoffset,
464 unsigned int nkiov, struct bio_vec *kiov,
465 unsigned int kiovoffset,
468 /* NB iov, kiov are READ-ONLY */
469 unsigned int this_nob;
475 LASSERT (!in_interrupt ());
478 while (iovoffset >= iov->iov_len) {
479 iovoffset -= iov->iov_len;
486 while (kiovoffset >= kiov->bv_len) {
487 kiovoffset -= kiov->bv_len;
496 this_nob = min3((unsigned int)iov->iov_len - iovoffset,
497 (unsigned int)kiov->bv_len - kiovoffset,
501 addr = ((char *)kmap(kiov->bv_page)) +
502 kiov->bv_offset + kiovoffset;
504 memcpy((char *)iov->iov_base + iovoffset, addr, this_nob);
507 if (iov->iov_len > iovoffset + this_nob) {
508 iovoffset += this_nob;
515 if (kiov->bv_len > kiovoffset + this_nob) {
517 kiovoffset += this_nob;
519 kunmap(kiov->bv_page);
529 kunmap(kiov->bv_page);
531 EXPORT_SYMBOL(lnet_copy_kiov2iov);
534 lnet_copy_iov2kiov(unsigned int nkiov, struct bio_vec *kiov,
535 unsigned int kiovoffset,
536 unsigned int niov, struct kvec *iov, unsigned int iovoffset,
539 /* NB kiov, iov are READ-ONLY */
540 unsigned int this_nob;
546 LASSERT (!in_interrupt ());
549 while (kiovoffset >= kiov->bv_len) {
550 kiovoffset -= kiov->bv_len;
557 while (iovoffset >= iov->iov_len) {
558 iovoffset -= iov->iov_len;
567 this_nob = min3((unsigned int)kiov->bv_len - kiovoffset,
568 (unsigned int)iov->iov_len - iovoffset,
572 addr = ((char *)kmap(kiov->bv_page)) +
573 kiov->bv_offset + kiovoffset;
575 memcpy (addr, (char *)iov->iov_base + iovoffset, this_nob);
578 if (kiov->bv_len > kiovoffset + this_nob) {
580 kiovoffset += this_nob;
582 kunmap(kiov->bv_page);
589 if (iov->iov_len > iovoffset + this_nob) {
590 iovoffset += this_nob;
599 kunmap(kiov->bv_page);
601 EXPORT_SYMBOL(lnet_copy_iov2kiov);
604 lnet_extract_kiov(int dst_niov, struct bio_vec *dst,
605 int src_niov, struct bio_vec *src,
606 unsigned int offset, unsigned int len)
608 /* Initialise 'dst' to the subset of 'src' starting at 'offset',
609 * for exactly 'len' bytes, and return the number of entries.
610 * NB not destructive to 'src' */
611 unsigned int frag_len;
614 if (len == 0) /* no data => */
615 return (0); /* no frags */
617 LASSERT(src_niov > 0);
618 while (offset >= src->bv_len) { /* skip initial frags */
619 offset -= src->bv_len;
622 LASSERT(src_niov > 0);
627 LASSERT(src_niov > 0);
628 LASSERT((int)niov <= dst_niov);
630 frag_len = src->bv_len - offset;
631 dst->bv_page = src->bv_page;
632 dst->bv_offset = src->bv_offset + offset;
634 if (len <= frag_len) {
636 LASSERT(dst->bv_offset + dst->bv_len <= PAGE_SIZE);
640 dst->bv_len = frag_len;
641 LASSERT(dst->bv_offset + dst->bv_len <= PAGE_SIZE);
651 EXPORT_SYMBOL(lnet_extract_kiov);
654 lnet_ni_recv(struct lnet_ni *ni, void *private, struct lnet_msg *msg,
655 int delayed, unsigned int offset, unsigned int mlen,
658 unsigned int niov = 0;
659 struct kvec *iov = NULL;
660 struct bio_vec *kiov = NULL;
663 LASSERT (!in_interrupt ());
664 LASSERT (mlen == 0 || msg != NULL);
667 LASSERT(msg->msg_receiving);
668 LASSERT(!msg->msg_sending);
669 LASSERT(rlen == msg->msg_len);
670 LASSERT(mlen <= msg->msg_len);
671 LASSERT(msg->msg_offset == offset);
672 LASSERT(msg->msg_wanted == mlen);
674 msg->msg_receiving = 0;
677 niov = msg->msg_niov;
678 kiov = msg->msg_kiov;
681 LASSERT ((iov == NULL) != (kiov == NULL));
685 rc = (ni->ni_net->net_lnd->lnd_recv)(ni, private, msg, delayed,
686 niov, kiov, offset, mlen,
689 lnet_finalize(msg, rc);
693 lnet_setpayloadbuffer(struct lnet_msg *msg)
695 struct lnet_libmd *md = msg->msg_md;
697 LASSERT(msg->msg_len > 0);
698 LASSERT(!msg->msg_routing);
700 LASSERT(msg->msg_niov == 0);
701 LASSERT(msg->msg_kiov == NULL);
703 msg->msg_niov = md->md_niov;
704 msg->msg_kiov = md->md_kiov;
708 lnet_prep_send(struct lnet_msg *msg, int type, struct lnet_process_id target,
709 unsigned int offset, unsigned int len)
711 msg->msg_type = type;
712 msg->msg_target = target;
714 msg->msg_offset = offset;
717 lnet_setpayloadbuffer(msg);
719 memset (&msg->msg_hdr, 0, sizeof (msg->msg_hdr));
720 msg->msg_hdr.type = cpu_to_le32(type);
721 /* dest_nid will be overwritten by lnet_select_pathway() */
722 msg->msg_hdr.dest_nid = cpu_to_le64(target.nid);
723 msg->msg_hdr.dest_pid = cpu_to_le32(target.pid);
724 /* src_nid will be set later */
725 msg->msg_hdr.src_pid = cpu_to_le32(the_lnet.ln_pid);
726 msg->msg_hdr.payload_length = cpu_to_le32(len);
730 lnet_ni_send(struct lnet_ni *ni, struct lnet_msg *msg)
732 void *priv = msg->msg_private;
735 LASSERT(!in_interrupt());
736 LASSERT(nid_is_lo0(&ni->ni_nid) ||
737 (msg->msg_txcredit && msg->msg_peertxcredit));
739 rc = (ni->ni_net->net_lnd->lnd_send)(ni, priv, msg);
741 msg->msg_no_resend = true;
742 lnet_finalize(msg, rc);
747 lnet_ni_eager_recv(struct lnet_ni *ni, struct lnet_msg *msg)
751 LASSERT(!msg->msg_sending);
752 LASSERT(msg->msg_receiving);
753 LASSERT(!msg->msg_rx_ready_delay);
754 LASSERT(ni->ni_net->net_lnd->lnd_eager_recv != NULL);
756 msg->msg_rx_ready_delay = 1;
757 rc = (ni->ni_net->net_lnd->lnd_eager_recv)(ni, msg->msg_private, msg,
760 CERROR("recv from %s / send to %s aborted: "
761 "eager_recv failed %d\n",
762 libcfs_nid2str(msg->msg_rxpeer->lpni_nid),
763 libcfs_id2str(msg->msg_target), rc);
764 LASSERT(rc < 0); /* required by my callers */
771 lnet_is_peer_deadline_passed(struct lnet_peer_ni *lpni, time64_t now)
775 deadline = lpni->lpni_last_alive +
776 lpni->lpni_net->net_tunables.lct_peer_timeout;
779 * assume peer_ni is alive as long as we're within the configured
788 /* NB: returns 1 when alive, 0 when dead, negative when error;
789 * may drop the lnet_net_lock */
791 lnet_peer_alive_locked(struct lnet_ni *ni, struct lnet_peer_ni *lpni,
792 struct lnet_msg *msg)
794 time64_t now = ktime_get_seconds();
796 if (!lnet_peer_aliveness_enabled(lpni))
800 * If we're resending a message, let's attempt to send it even if
801 * the peer is down to fulfill our resend quota on the message
803 if (msg->msg_retry_count > 0)
806 /* try and send recovery messages irregardless */
807 if (msg->msg_recovery)
810 /* always send any responses */
811 if (lnet_msg_is_response(msg))
814 if (!lnet_is_peer_deadline_passed(lpni, now))
817 return lnet_is_peer_ni_alive(lpni);
821 * \param msg The message to be sent.
822 * \param do_send True if lnet_ni_send() should be called in this function.
823 * lnet_send() is going to lnet_net_unlock immediately after this, so
824 * it sets do_send FALSE and I don't do the unlock/send/lock bit.
826 * \retval LNET_CREDIT_OK If \a msg sent or OK to send.
827 * \retval LNET_CREDIT_WAIT If \a msg blocked for credit.
828 * \retval -EHOSTUNREACH If the next hop of the message appears dead.
829 * \retval -ECANCELED If the MD of the message has been unlinked.
832 lnet_post_send_locked(struct lnet_msg *msg, int do_send)
834 struct lnet_peer_ni *lp = msg->msg_txpeer;
835 struct lnet_ni *ni = msg->msg_txni;
836 int cpt = msg->msg_tx_cpt;
837 struct lnet_tx_queue *tq = ni->ni_tx_queues[cpt];
839 /* non-lnet_send() callers have checked before */
840 LASSERT(!do_send || msg->msg_tx_delayed);
841 LASSERT(!msg->msg_receiving);
842 LASSERT(msg->msg_tx_committed);
844 /* can't get here if we're sending to the loopback interface */
845 if (the_lnet.ln_loni)
846 LASSERT(lp->lpni_nid !=
847 lnet_nid_to_nid4(&the_lnet.ln_loni->ni_nid));
849 /* NB 'lp' is always the next hop */
850 if ((msg->msg_target.pid & LNET_PID_USERFLAG) == 0 &&
851 lnet_peer_alive_locked(ni, lp, msg) == 0) {
852 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
853 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length +=
855 lnet_net_unlock(cpt);
857 lnet_incr_stats(&msg->msg_txpeer->lpni_stats,
859 LNET_STATS_TYPE_DROP);
861 lnet_incr_stats(&msg->msg_txni->ni_stats,
863 LNET_STATS_TYPE_DROP);
865 CNETERR("Dropping message for %s: peer not alive\n",
866 libcfs_id2str(msg->msg_target));
867 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_DROPPED;
869 lnet_finalize(msg, -EHOSTUNREACH);
872 return -EHOSTUNREACH;
875 if (msg->msg_md != NULL &&
876 (msg->msg_md->md_flags & LNET_MD_FLAG_ABORTED) != 0) {
877 lnet_net_unlock(cpt);
879 CNETERR("Aborting message for %s: LNetM[DE]Unlink() already "
880 "called on the MD/ME.\n",
881 libcfs_id2str(msg->msg_target));
883 msg->msg_no_resend = true;
884 CDEBUG(D_NET, "msg %p to %s canceled and will not be resent\n",
885 msg, libcfs_id2str(msg->msg_target));
886 lnet_finalize(msg, -ECANCELED);
893 if (!msg->msg_peertxcredit) {
894 spin_lock(&lp->lpni_lock);
895 LASSERT((lp->lpni_txcredits < 0) ==
896 !list_empty(&lp->lpni_txq));
898 msg->msg_peertxcredit = 1;
899 lp->lpni_txqnob += msg->msg_len + sizeof(struct lnet_hdr);
900 lp->lpni_txcredits--;
902 if (lp->lpni_txcredits < lp->lpni_mintxcredits)
903 lp->lpni_mintxcredits = lp->lpni_txcredits;
905 if (lp->lpni_txcredits < 0) {
906 msg->msg_tx_delayed = 1;
907 list_add_tail(&msg->msg_list, &lp->lpni_txq);
908 spin_unlock(&lp->lpni_lock);
909 return LNET_CREDIT_WAIT;
911 spin_unlock(&lp->lpni_lock);
914 if (!msg->msg_txcredit) {
915 LASSERT((tq->tq_credits < 0) ==
916 !list_empty(&tq->tq_delayed));
918 msg->msg_txcredit = 1;
920 atomic_dec(&ni->ni_tx_credits);
922 if (tq->tq_credits < tq->tq_credits_min)
923 tq->tq_credits_min = tq->tq_credits;
925 if (tq->tq_credits < 0) {
926 msg->msg_tx_delayed = 1;
927 list_add_tail(&msg->msg_list, &tq->tq_delayed);
928 return LNET_CREDIT_WAIT;
932 if (unlikely(!list_empty(&the_lnet.ln_delay_rules)) &&
933 lnet_delay_rule_match_locked(&msg->msg_hdr, msg)) {
934 msg->msg_tx_delayed = 1;
935 return LNET_CREDIT_WAIT;
938 /* unset the tx_delay flag as we're going to send it now */
939 msg->msg_tx_delayed = 0;
942 lnet_net_unlock(cpt);
943 lnet_ni_send(ni, msg);
946 return LNET_CREDIT_OK;
950 static struct lnet_rtrbufpool *
951 lnet_msg2bufpool(struct lnet_msg *msg)
953 struct lnet_rtrbufpool *rbp;
956 LASSERT(msg->msg_rx_committed);
958 cpt = msg->msg_rx_cpt;
959 rbp = &the_lnet.ln_rtrpools[cpt][0];
961 LASSERT(msg->msg_len <= LNET_MTU);
962 while (msg->msg_len > (unsigned int)rbp->rbp_npages * PAGE_SIZE) {
964 LASSERT(rbp < &the_lnet.ln_rtrpools[cpt][LNET_NRBPOOLS]);
971 lnet_post_routed_recv_locked(struct lnet_msg *msg, int do_recv)
973 /* lnet_parse is going to lnet_net_unlock immediately after this, so it
974 * sets do_recv FALSE and I don't do the unlock/send/lock bit.
975 * I return LNET_CREDIT_WAIT if msg blocked and LNET_CREDIT_OK if
976 * received or OK to receive */
977 struct lnet_peer_ni *lpni = msg->msg_rxpeer;
978 struct lnet_peer *lp;
979 struct lnet_rtrbufpool *rbp;
980 struct lnet_rtrbuf *rb;
982 LASSERT(msg->msg_kiov == NULL);
983 LASSERT(msg->msg_niov == 0);
984 LASSERT(msg->msg_routing);
985 LASSERT(msg->msg_receiving);
986 LASSERT(!msg->msg_sending);
987 LASSERT(lpni->lpni_peer_net);
988 LASSERT(lpni->lpni_peer_net->lpn_peer);
990 lp = lpni->lpni_peer_net->lpn_peer;
992 /* non-lnet_parse callers only receive delayed messages */
993 LASSERT(!do_recv || msg->msg_rx_delayed);
995 if (!msg->msg_peerrtrcredit) {
996 /* lpni_lock protects the credit manipulation */
997 spin_lock(&lpni->lpni_lock);
999 msg->msg_peerrtrcredit = 1;
1000 lpni->lpni_rtrcredits--;
1001 if (lpni->lpni_rtrcredits < lpni->lpni_minrtrcredits)
1002 lpni->lpni_minrtrcredits = lpni->lpni_rtrcredits;
1004 if (lpni->lpni_rtrcredits < 0) {
1005 spin_unlock(&lpni->lpni_lock);
1006 /* must have checked eager_recv before here */
1007 LASSERT(msg->msg_rx_ready_delay);
1008 msg->msg_rx_delayed = 1;
1009 /* lp_lock protects the lp_rtrq */
1010 spin_lock(&lp->lp_lock);
1011 list_add_tail(&msg->msg_list, &lp->lp_rtrq);
1012 spin_unlock(&lp->lp_lock);
1013 return LNET_CREDIT_WAIT;
1015 spin_unlock(&lpni->lpni_lock);
1018 rbp = lnet_msg2bufpool(msg);
1020 if (!msg->msg_rtrcredit) {
1021 msg->msg_rtrcredit = 1;
1023 if (rbp->rbp_credits < rbp->rbp_mincredits)
1024 rbp->rbp_mincredits = rbp->rbp_credits;
1026 if (rbp->rbp_credits < 0) {
1027 /* must have checked eager_recv before here */
1028 LASSERT(msg->msg_rx_ready_delay);
1029 msg->msg_rx_delayed = 1;
1030 list_add_tail(&msg->msg_list, &rbp->rbp_msgs);
1031 return LNET_CREDIT_WAIT;
1035 LASSERT(!list_empty(&rbp->rbp_bufs));
1036 rb = list_entry(rbp->rbp_bufs.next, struct lnet_rtrbuf, rb_list);
1037 list_del(&rb->rb_list);
1039 msg->msg_niov = rbp->rbp_npages;
1040 msg->msg_kiov = &rb->rb_kiov[0];
1042 /* unset the msg-rx_delayed flag since we're receiving the message */
1043 msg->msg_rx_delayed = 0;
1046 int cpt = msg->msg_rx_cpt;
1048 lnet_net_unlock(cpt);
1049 lnet_ni_recv(msg->msg_rxni, msg->msg_private, msg, 1,
1050 0, msg->msg_len, msg->msg_len);
1053 return LNET_CREDIT_OK;
1057 lnet_return_tx_credits_locked(struct lnet_msg *msg)
1059 struct lnet_peer_ni *txpeer = msg->msg_txpeer;
1060 struct lnet_ni *txni = msg->msg_txni;
1061 struct lnet_msg *msg2;
1063 if (msg->msg_txcredit) {
1064 struct lnet_ni *ni = msg->msg_txni;
1065 struct lnet_tx_queue *tq = ni->ni_tx_queues[msg->msg_tx_cpt];
1067 /* give back NI txcredits */
1068 msg->msg_txcredit = 0;
1070 LASSERT((tq->tq_credits < 0) ==
1071 !list_empty(&tq->tq_delayed));
1074 atomic_inc(&ni->ni_tx_credits);
1075 if (tq->tq_credits <= 0) {
1076 msg2 = list_entry(tq->tq_delayed.next,
1077 struct lnet_msg, msg_list);
1078 list_del(&msg2->msg_list);
1080 LASSERT(msg2->msg_txni == ni);
1081 LASSERT(msg2->msg_tx_delayed);
1082 LASSERT(msg2->msg_tx_cpt == msg->msg_tx_cpt);
1084 (void) lnet_post_send_locked(msg2, 1);
1088 if (msg->msg_peertxcredit) {
1089 /* give back peer txcredits */
1090 msg->msg_peertxcredit = 0;
1092 spin_lock(&txpeer->lpni_lock);
1093 LASSERT((txpeer->lpni_txcredits < 0) ==
1094 !list_empty(&txpeer->lpni_txq));
1096 txpeer->lpni_txqnob -= msg->msg_len + sizeof(struct lnet_hdr);
1097 LASSERT(txpeer->lpni_txqnob >= 0);
1099 txpeer->lpni_txcredits++;
1100 if (txpeer->lpni_txcredits <= 0) {
1103 msg2 = list_entry(txpeer->lpni_txq.next,
1104 struct lnet_msg, msg_list);
1105 list_del(&msg2->msg_list);
1106 spin_unlock(&txpeer->lpni_lock);
1108 LASSERT(msg2->msg_txpeer == txpeer);
1109 LASSERT(msg2->msg_tx_delayed);
1111 msg2_cpt = msg2->msg_tx_cpt;
1114 * The msg_cpt can be different from the msg2_cpt
1115 * so we need to make sure we lock the correct cpt
1117 * Once we call lnet_post_send_locked() it is no
1118 * longer safe to access msg2, since it could've
1119 * been freed by lnet_finalize(), but we still
1120 * need to relock the correct cpt, so we cache the
1121 * msg2_cpt for the purpose of the check that
1122 * follows the call to lnet_pose_send_locked().
1124 if (msg2_cpt != msg->msg_tx_cpt) {
1125 lnet_net_unlock(msg->msg_tx_cpt);
1126 lnet_net_lock(msg2_cpt);
1128 (void) lnet_post_send_locked(msg2, 1);
1129 if (msg2_cpt != msg->msg_tx_cpt) {
1130 lnet_net_unlock(msg2_cpt);
1131 lnet_net_lock(msg->msg_tx_cpt);
1134 spin_unlock(&txpeer->lpni_lock);
1139 msg->msg_txni = NULL;
1140 lnet_ni_decref_locked(txni, msg->msg_tx_cpt);
1143 if (txpeer != NULL) {
1144 msg->msg_txpeer = NULL;
1145 lnet_peer_ni_decref_locked(txpeer);
1150 lnet_schedule_blocked_locked(struct lnet_rtrbufpool *rbp)
1152 struct lnet_msg *msg;
1154 if (list_empty(&rbp->rbp_msgs))
1156 msg = list_entry(rbp->rbp_msgs.next,
1157 struct lnet_msg, msg_list);
1158 list_del(&msg->msg_list);
1160 (void)lnet_post_routed_recv_locked(msg, 1);
1164 lnet_drop_routed_msgs_locked(struct list_head *list, int cpt)
1166 struct lnet_msg *msg;
1167 struct lnet_msg *tmp;
1169 lnet_net_unlock(cpt);
1171 list_for_each_entry_safe(msg, tmp, list, msg_list) {
1172 lnet_ni_recv(msg->msg_rxni, msg->msg_private, NULL,
1173 0, 0, 0, msg->msg_hdr.payload_length);
1174 list_del_init(&msg->msg_list);
1175 msg->msg_no_resend = true;
1176 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_ERROR;
1177 lnet_finalize(msg, -ECANCELED);
1184 lnet_return_rx_credits_locked(struct lnet_msg *msg)
1186 struct lnet_peer_ni *rxpeerni = msg->msg_rxpeer;
1187 struct lnet_peer *lp;
1188 struct lnet_ni *rxni = msg->msg_rxni;
1189 struct lnet_msg *msg2;
1191 if (msg->msg_rtrcredit) {
1192 /* give back global router credits */
1193 struct lnet_rtrbuf *rb;
1194 struct lnet_rtrbufpool *rbp;
1196 /* NB If a msg ever blocks for a buffer in rbp_msgs, it stays
1197 * there until it gets one allocated, or aborts the wait
1199 LASSERT(msg->msg_kiov != NULL);
1201 rb = list_entry(msg->msg_kiov, struct lnet_rtrbuf, rb_kiov[0]);
1204 msg->msg_kiov = NULL;
1205 msg->msg_rtrcredit = 0;
1207 LASSERT(rbp == lnet_msg2bufpool(msg));
1209 LASSERT((rbp->rbp_credits > 0) ==
1210 !list_empty(&rbp->rbp_bufs));
1212 /* If routing is now turned off, we just drop this buffer and
1213 * don't bother trying to return credits. */
1214 if (!the_lnet.ln_routing) {
1215 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1219 /* It is possible that a user has lowered the desired number of
1220 * buffers in this pool. Make sure we never put back
1221 * more buffers than the stated number. */
1222 if (unlikely(rbp->rbp_credits >= rbp->rbp_req_nbuffers)) {
1223 /* Discard this buffer so we don't have too
1225 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1226 rbp->rbp_nbuffers--;
1228 list_add(&rb->rb_list, &rbp->rbp_bufs);
1230 if (rbp->rbp_credits <= 0)
1231 lnet_schedule_blocked_locked(rbp);
1236 if (msg->msg_peerrtrcredit) {
1238 LASSERT(rxpeerni->lpni_peer_net);
1239 LASSERT(rxpeerni->lpni_peer_net->lpn_peer);
1241 /* give back peer router credits */
1242 msg->msg_peerrtrcredit = 0;
1244 spin_lock(&rxpeerni->lpni_lock);
1245 rxpeerni->lpni_rtrcredits++;
1246 spin_unlock(&rxpeerni->lpni_lock);
1248 lp = rxpeerni->lpni_peer_net->lpn_peer;
1249 spin_lock(&lp->lp_lock);
1251 /* drop all messages which are queued to be routed on that
1253 if (!the_lnet.ln_routing) {
1255 list_splice_init(&lp->lp_rtrq, &drop);
1256 spin_unlock(&lp->lp_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);
1267 * messages on the lp_rtrq can be from any NID in
1268 * the peer, which means they might have different
1269 * cpts. We need to make sure we lock the right
1272 if (msg2_cpt != msg->msg_rx_cpt) {
1273 lnet_net_unlock(msg->msg_rx_cpt);
1274 lnet_net_lock(msg2_cpt);
1276 (void) lnet_post_routed_recv_locked(msg2, 1);
1277 if (msg2_cpt != msg->msg_rx_cpt) {
1278 lnet_net_unlock(msg2_cpt);
1279 lnet_net_lock(msg->msg_rx_cpt);
1282 spin_unlock(&lp->lp_lock);
1286 msg->msg_rxni = NULL;
1287 lnet_ni_decref_locked(rxni, msg->msg_rx_cpt);
1289 if (rxpeerni != NULL) {
1290 msg->msg_rxpeer = NULL;
1291 lnet_peer_ni_decref_locked(rxpeerni);
1295 static struct lnet_peer_ni *
1296 lnet_select_peer_ni(struct lnet_ni *best_ni, lnet_nid_t dst_nid,
1297 struct lnet_peer *peer,
1298 struct lnet_peer_ni *best_lpni,
1299 struct lnet_peer_net *peer_net)
1302 * Look at the peer NIs for the destination peer that connect
1303 * to the chosen net. If a peer_ni is preferred when using the
1304 * best_ni to communicate, we use that one. If there is no
1305 * preferred peer_ni, or there are multiple preferred peer_ni,
1306 * the available transmit credits are used. If the transmit
1307 * credits are equal, we round-robin over the peer_ni.
1309 struct lnet_peer_ni *lpni = NULL;
1310 int best_lpni_credits = (best_lpni) ? best_lpni->lpni_txcredits :
1312 int best_lpni_healthv = (best_lpni) ?
1313 atomic_read(&best_lpni->lpni_healthv) : 0;
1314 bool best_lpni_is_preferred = false;
1315 bool lpni_is_preferred;
1317 __u32 lpni_sel_prio;
1318 __u32 best_sel_prio = LNET_MAX_SELECTION_PRIORITY;
1320 while ((lpni = lnet_get_next_peer_ni_locked(peer, peer_net, lpni))) {
1322 * if the best_ni we've chosen aleady has this lpni
1323 * preferred, then let's use it
1326 /* FIXME need to handle large-addr nid */
1327 lpni_is_preferred = lnet_peer_is_pref_nid_locked(
1328 lpni, lnet_nid_to_nid4(&best_ni->ni_nid));
1329 CDEBUG(D_NET, "%s lpni_is_preferred = %d\n",
1330 libcfs_nidstr(&best_ni->ni_nid),
1333 lpni_is_preferred = false;
1336 lpni_healthv = atomic_read(&lpni->lpni_healthv);
1337 lpni_sel_prio = lpni->lpni_sel_priority;
1340 CDEBUG(D_NET, "n:[%s, %s] h:[%d, %d] p:[%d, %d] c:[%d, %d] s:[%d, %d]\n",
1341 libcfs_nid2str(lpni->lpni_nid),
1342 libcfs_nid2str(best_lpni->lpni_nid),
1343 lpni_healthv, best_lpni_healthv,
1344 lpni_sel_prio, best_sel_prio,
1345 lpni->lpni_txcredits, best_lpni_credits,
1346 lpni->lpni_seq, best_lpni->lpni_seq);
1350 /* pick the healthiest peer ni */
1351 if (lpni_healthv < best_lpni_healthv)
1353 else if (lpni_healthv > best_lpni_healthv) {
1354 if (best_lpni_is_preferred)
1355 best_lpni_is_preferred = false;
1359 if (lpni_sel_prio > best_sel_prio)
1361 else if (lpni_sel_prio < best_sel_prio) {
1362 if (best_lpni_is_preferred)
1363 best_lpni_is_preferred = false;
1367 /* if this is a preferred peer use it */
1368 if (!best_lpni_is_preferred && lpni_is_preferred) {
1369 best_lpni_is_preferred = true;
1371 } else if (best_lpni_is_preferred && !lpni_is_preferred) {
1372 /* this is not the preferred peer so let's ignore
1378 if (lpni->lpni_txcredits < best_lpni_credits)
1379 /* We already have a peer that has more credits
1380 * available than this one. No need to consider
1381 * this peer further.
1384 else if (lpni->lpni_txcredits > best_lpni_credits)
1387 /* The best peer found so far and the current peer
1388 * have the same number of available credits let's
1389 * make sure to select between them using Round Robin
1391 if (best_lpni && (best_lpni->lpni_seq <= lpni->lpni_seq))
1394 best_lpni_is_preferred = lpni_is_preferred;
1395 best_lpni_healthv = lpni_healthv;
1396 best_sel_prio = lpni_sel_prio;
1398 best_lpni_credits = lpni->lpni_txcredits;
1401 /* if we still can't find a peer ni then we can't reach it */
1403 __u32 net_id = (peer_net) ? peer_net->lpn_net_id :
1404 LNET_NIDNET(dst_nid);
1405 CDEBUG(D_NET, "no peer_ni found on peer net %s\n",
1406 libcfs_net2str(net_id));
1410 CDEBUG(D_NET, "sd_best_lpni = %s\n",
1411 libcfs_nid2str(best_lpni->lpni_nid));
1417 * Prerequisite: the best_ni should already be set in the sd
1418 * Find the best lpni.
1419 * If the net id is provided then restrict lpni selection on
1420 * that particular net.
1421 * Otherwise find any reachable lpni. When dealing with an MR
1422 * gateway and it has multiple lpnis which we can use
1423 * we want to select the best one from the list of reachable
1426 static inline struct lnet_peer_ni *
1427 lnet_find_best_lpni(struct lnet_ni *lni, lnet_nid_t dst_nid,
1428 struct lnet_peer *peer, __u32 net_id)
1430 struct lnet_peer_net *peer_net;
1432 /* find the best_lpni on any local network */
1433 if (net_id == LNET_NET_ANY) {
1434 struct lnet_peer_ni *best_lpni = NULL;
1435 struct lnet_peer_net *lpn;
1436 list_for_each_entry(lpn, &peer->lp_peer_nets, lpn_peer_nets) {
1437 /* no net specified find any reachable peer ni */
1438 if (!lnet_islocalnet_locked(lpn->lpn_net_id))
1440 best_lpni = lnet_select_peer_ni(lni, dst_nid, peer,
1446 /* restrict on the specified net */
1447 peer_net = lnet_peer_get_net_locked(peer, net_id);
1449 return lnet_select_peer_ni(lni, dst_nid, peer, NULL, peer_net);
1455 lnet_compare_gw_lpnis(struct lnet_peer_ni *lpni1, struct lnet_peer_ni *lpni2)
1457 if (lpni1->lpni_txqnob < lpni2->lpni_txqnob)
1460 if (lpni1->lpni_txqnob > lpni2->lpni_txqnob)
1463 if (lpni1->lpni_txcredits > lpni2->lpni_txcredits)
1466 if (lpni1->lpni_txcredits < lpni2->lpni_txcredits)
1472 /* Compare route priorities and hop counts */
1474 lnet_compare_routes(struct lnet_route *r1, struct lnet_route *r2)
1476 int r1_hops = (r1->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r1->lr_hops;
1477 int r2_hops = (r2->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r2->lr_hops;
1479 if (r1->lr_priority < r2->lr_priority)
1482 if (r1->lr_priority > r2->lr_priority)
1485 if (r1_hops < r2_hops)
1488 if (r1_hops > r2_hops)
1494 static struct lnet_route *
1495 lnet_find_route_locked(struct lnet_remotenet *rnet, __u32 src_net,
1496 struct lnet_peer_ni *remote_lpni,
1497 struct lnet_route **prev_route,
1498 struct lnet_peer_ni **gwni)
1500 struct lnet_peer_ni *lpni, *best_gw_ni = NULL;
1501 struct lnet_route *best_route;
1502 struct lnet_route *last_route;
1503 struct lnet_route *route;
1505 bool best_rte_is_preferred = false;
1508 CDEBUG(D_NET, "Looking up a route to %s, from %s\n",
1509 libcfs_net2str(rnet->lrn_net), libcfs_net2str(src_net));
1511 best_route = last_route = NULL;
1512 list_for_each_entry(route, &rnet->lrn_routes, lr_list) {
1513 if (!lnet_is_route_alive(route))
1515 gw_pnid = route->lr_gateway->lp_primary_nid;
1517 /* no protection on below fields, but it's harmless */
1518 if (last_route && (last_route->lr_seq - route->lr_seq < 0))
1521 /* if the best route found is in the preferred list then
1522 * tag it as preferred and use it later on. But if we
1523 * didn't find any routes which are on the preferred list
1524 * then just use the best route possible.
1526 rc = lnet_peer_is_pref_rtr_locked(remote_lpni, gw_pnid);
1528 if (!best_route || (rc && !best_rte_is_preferred)) {
1529 /* Restrict the selection of the router NI on the
1530 * src_net provided. If the src_net is LNET_NID_ANY,
1531 * then select the best interface available.
1533 lpni = lnet_find_best_lpni(NULL, LNET_NID_ANY,
1538 "Gateway %s does not have a peer NI on net %s\n",
1539 libcfs_nid2str(gw_pnid),
1540 libcfs_net2str(src_net));
1545 if (rc && !best_rte_is_preferred) {
1546 /* This is the first preferred route we found,
1547 * so it beats any route found previously
1553 best_rte_is_preferred = true;
1554 CDEBUG(D_NET, "preferred gw = %s\n",
1555 libcfs_nid2str(gw_pnid));
1557 } else if ((!rc) && best_rte_is_preferred)
1558 /* The best route we found so far is in the preferred
1559 * list, so it beats any non-preferred route
1564 best_route = last_route = route;
1569 rc = lnet_compare_routes(route, best_route);
1573 /* Restrict the selection of the router NI on the
1574 * src_net provided. If the src_net is LNET_NID_ANY,
1575 * then select the best interface available.
1577 lpni = lnet_find_best_lpni(NULL, LNET_NID_ANY,
1582 "Gateway %s does not have a peer NI on net %s\n",
1583 libcfs_nid2str(gw_pnid),
1584 libcfs_net2str(src_net));
1594 rc = lnet_compare_gw_lpnis(lpni, best_gw_ni);
1598 if (rc == 1 || route->lr_seq <= best_route->lr_seq) {
1605 *prev_route = last_route;
1611 static inline unsigned int
1612 lnet_dev_prio_of_md(struct lnet_ni *ni, unsigned int dev_idx)
1614 if (dev_idx == UINT_MAX)
1617 if (!ni || !ni->ni_net || !ni->ni_net->net_lnd ||
1618 !ni->ni_net->net_lnd->lnd_get_dev_prio)
1621 return ni->ni_net->net_lnd->lnd_get_dev_prio(ni, dev_idx);
1624 static struct lnet_ni *
1625 lnet_get_best_ni(struct lnet_net *local_net, struct lnet_ni *best_ni,
1626 struct lnet_peer *peer, struct lnet_peer_net *peer_net,
1627 struct lnet_msg *msg, int md_cpt)
1629 struct lnet_libmd *md = msg->msg_md;
1630 unsigned int offset = msg->msg_offset;
1631 unsigned int shortest_distance;
1632 struct lnet_ni *ni = NULL;
1635 __u32 best_sel_prio;
1636 unsigned int best_dev_prio;
1637 unsigned int dev_idx = UINT_MAX;
1638 struct page *page = lnet_get_first_page(md, offset);
1639 msg->msg_rdma_force = lnet_is_rdma_only_page(page);
1641 if (msg->msg_rdma_force)
1642 dev_idx = lnet_get_dev_idx(page);
1645 * If there is no peer_ni that we can send to on this network,
1646 * then there is no point in looking for a new best_ni here.
1648 if (!lnet_get_next_peer_ni_locked(peer, peer_net, NULL))
1651 if (best_ni == NULL) {
1652 best_sel_prio = LNET_MAX_SELECTION_PRIORITY;
1653 shortest_distance = UINT_MAX;
1654 best_dev_prio = UINT_MAX;
1655 best_credits = INT_MIN;
1658 best_dev_prio = lnet_dev_prio_of_md(best_ni, dev_idx);
1659 shortest_distance = cfs_cpt_distance(lnet_cpt_table(), md_cpt,
1660 best_ni->ni_dev_cpt);
1661 best_credits = atomic_read(&best_ni->ni_tx_credits);
1662 best_healthv = atomic_read(&best_ni->ni_healthv);
1663 best_sel_prio = best_ni->ni_sel_priority;
1666 while ((ni = lnet_get_next_ni_locked(local_net, ni))) {
1667 unsigned int distance;
1672 unsigned int ni_dev_prio;
1674 ni_credits = atomic_read(&ni->ni_tx_credits);
1675 ni_healthv = atomic_read(&ni->ni_healthv);
1676 ni_fatal = atomic_read(&ni->ni_fatal_error_on);
1677 ni_sel_prio = ni->ni_sel_priority;
1680 * calculate the distance from the CPT on which
1681 * the message memory is allocated to the CPT of
1682 * the NI's physical device
1684 distance = cfs_cpt_distance(lnet_cpt_table(),
1688 ni_dev_prio = lnet_dev_prio_of_md(ni, dev_idx);
1691 * All distances smaller than the NUMA range
1692 * are treated equally.
1694 if (distance < lnet_numa_range)
1695 distance = lnet_numa_range;
1698 * Select on health, selection policy, direct dma prio,
1699 * shorter distance, available credits, then round-robin.
1705 CDEBUG(D_NET, "compare ni %s [c:%d, d:%d, s:%d, p:%u, g:%u] with best_ni %s [c:%d, d:%d, s:%d, p:%u, g:%u]\n",
1706 libcfs_nidstr(&ni->ni_nid), ni_credits, distance,
1707 ni->ni_seq, ni_sel_prio, ni_dev_prio,
1708 (best_ni) ? libcfs_nidstr(&best_ni->ni_nid)
1709 : "not selected", best_credits, shortest_distance,
1710 (best_ni) ? best_ni->ni_seq : 0,
1711 best_sel_prio, best_dev_prio);
1715 if (ni_healthv < best_healthv)
1717 else if (ni_healthv > best_healthv)
1720 if (ni_sel_prio > best_sel_prio)
1722 else if (ni_sel_prio < best_sel_prio)
1725 if (ni_dev_prio > best_dev_prio)
1727 else if (ni_dev_prio < best_dev_prio)
1730 if (distance > shortest_distance)
1732 else if (distance < shortest_distance)
1735 if (ni_credits < best_credits)
1737 else if (ni_credits > best_credits)
1740 if (best_ni && best_ni->ni_seq <= ni->ni_seq)
1744 best_sel_prio = ni_sel_prio;
1745 best_dev_prio = ni_dev_prio;
1746 shortest_distance = distance;
1747 best_healthv = ni_healthv;
1749 best_credits = ni_credits;
1752 CDEBUG(D_NET, "selected best_ni %s\n",
1753 (best_ni) ? libcfs_nidstr(&best_ni->ni_nid) : "no selection");
1759 * Traffic to the LNET_RESERVED_PORTAL may not trigger peer discovery,
1760 * because such traffic is required to perform discovery. We therefore
1761 * exclude all GET and PUT on that portal. We also exclude all ACK and
1762 * REPLY traffic, but that is because the portal is not tracked in the
1763 * message structure for these message types. We could restrict this
1764 * further by also checking for LNET_PROTO_PING_MATCHBITS.
1767 lnet_msg_discovery(struct lnet_msg *msg)
1769 if (msg->msg_type == LNET_MSG_PUT) {
1770 if (msg->msg_hdr.msg.put.ptl_index != LNET_RESERVED_PORTAL)
1772 } else if (msg->msg_type == LNET_MSG_GET) {
1773 if (msg->msg_hdr.msg.get.ptl_index != LNET_RESERVED_PORTAL)
1779 #define SRC_SPEC 0x0001
1780 #define SRC_ANY 0x0002
1781 #define LOCAL_DST 0x0004
1782 #define REMOTE_DST 0x0008
1783 #define MR_DST 0x0010
1784 #define NMR_DST 0x0020
1785 #define SND_RESP 0x0040
1787 /* The following to defines are used for return codes */
1788 #define REPEAT_SEND 0x1000
1789 #define PASS_THROUGH 0x2000
1791 /* The different cases lnet_select pathway needs to handle */
1792 #define SRC_SPEC_LOCAL_MR_DST (SRC_SPEC | LOCAL_DST | MR_DST)
1793 #define SRC_SPEC_ROUTER_MR_DST (SRC_SPEC | REMOTE_DST | MR_DST)
1794 #define SRC_SPEC_LOCAL_NMR_DST (SRC_SPEC | LOCAL_DST | NMR_DST)
1795 #define SRC_SPEC_ROUTER_NMR_DST (SRC_SPEC | REMOTE_DST | NMR_DST)
1796 #define SRC_ANY_LOCAL_MR_DST (SRC_ANY | LOCAL_DST | MR_DST)
1797 #define SRC_ANY_ROUTER_MR_DST (SRC_ANY | REMOTE_DST | MR_DST)
1798 #define SRC_ANY_LOCAL_NMR_DST (SRC_ANY | LOCAL_DST | NMR_DST)
1799 #define SRC_ANY_ROUTER_NMR_DST (SRC_ANY | REMOTE_DST | NMR_DST)
1802 lnet_handle_lo_send(struct lnet_send_data *sd)
1804 struct lnet_msg *msg = sd->sd_msg;
1805 int cpt = sd->sd_cpt;
1807 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1810 /* No send credit hassles with LOLND */
1811 lnet_ni_addref_locked(the_lnet.ln_loni, cpt);
1812 msg->msg_hdr.dest_nid =
1813 cpu_to_le64(lnet_nid_to_nid4(&the_lnet.ln_loni->ni_nid));
1814 if (!msg->msg_routing)
1815 msg->msg_hdr.src_nid =
1816 cpu_to_le64(lnet_nid_to_nid4(&the_lnet.ln_loni->ni_nid));
1817 msg->msg_target.nid = lnet_nid_to_nid4(&the_lnet.ln_loni->ni_nid);
1818 lnet_msg_commit(msg, cpt);
1819 msg->msg_txni = the_lnet.ln_loni;
1821 return LNET_CREDIT_OK;
1825 lnet_handle_send(struct lnet_send_data *sd)
1827 struct lnet_ni *best_ni = sd->sd_best_ni;
1828 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
1829 struct lnet_peer_ni *final_dst_lpni = sd->sd_final_dst_lpni;
1830 struct lnet_msg *msg = sd->sd_msg;
1832 __u32 send_case = sd->sd_send_case;
1834 __u32 routing = send_case & REMOTE_DST;
1835 struct lnet_rsp_tracker *rspt;
1837 /* Increment sequence number of the selected peer, peer net,
1838 * local ni and local net so that we pick the next ones
1841 best_lpni->lpni_seq++;
1842 best_lpni->lpni_peer_net->lpn_seq++;
1844 best_ni->ni_net->net_seq++;
1846 CDEBUG(D_NET, "%s NI seq info: [%d:%d:%d:%u] %s LPNI seq info [%d:%d:%d:%u]\n",
1847 libcfs_nidstr(&best_ni->ni_nid),
1848 best_ni->ni_seq, best_ni->ni_net->net_seq,
1849 atomic_read(&best_ni->ni_tx_credits),
1850 best_ni->ni_sel_priority,
1851 libcfs_nid2str(best_lpni->lpni_nid),
1852 best_lpni->lpni_seq, best_lpni->lpni_peer_net->lpn_seq,
1853 best_lpni->lpni_txcredits,
1854 best_lpni->lpni_sel_priority);
1857 * grab a reference on the peer_ni so it sticks around even if
1858 * we need to drop and relock the lnet_net_lock below.
1860 lnet_peer_ni_addref_locked(best_lpni);
1863 * Use lnet_cpt_of_nid() to determine the CPT used to commit the
1864 * message. This ensures that we get a CPT that is correct for
1865 * the NI when the NI has been restricted to a subset of all CPTs.
1866 * If the selected CPT differs from the one currently locked, we
1867 * must unlock and relock the lnet_net_lock(), and then check whether
1868 * the configuration has changed. We don't have a hold on the best_ni
1869 * yet, and it may have vanished.
1871 cpt2 = lnet_cpt_of_nid_locked(best_lpni->lpni_nid, best_ni);
1872 if (sd->sd_cpt != cpt2) {
1873 __u32 seq = lnet_get_dlc_seq_locked();
1874 lnet_net_unlock(sd->sd_cpt);
1876 lnet_net_lock(sd->sd_cpt);
1877 if (seq != lnet_get_dlc_seq_locked()) {
1878 lnet_peer_ni_decref_locked(best_lpni);
1884 * store the best_lpni in the message right away to avoid having
1885 * to do the same operation under different conditions
1887 msg->msg_txpeer = best_lpni;
1888 msg->msg_txni = best_ni;
1891 * grab a reference for the best_ni since now it's in use in this
1892 * send. The reference will be dropped in lnet_finalize()
1894 lnet_ni_addref_locked(msg->msg_txni, sd->sd_cpt);
1897 * Always set the target.nid to the best peer picked. Either the
1898 * NID will be one of the peer NIDs selected, or the same NID as
1899 * what was originally set in the target or it will be the NID of
1900 * a router if this message should be routed
1902 msg->msg_target.nid = msg->msg_txpeer->lpni_nid;
1905 * lnet_msg_commit assigns the correct cpt to the message, which
1906 * is used to decrement the correct refcount on the ni when it's
1907 * time to return the credits
1909 lnet_msg_commit(msg, sd->sd_cpt);
1912 * If we are routing the message then we keep the src_nid that was
1913 * set by the originator. If we are not routing then we are the
1914 * originator and set it here.
1916 if (!msg->msg_routing)
1917 msg->msg_hdr.src_nid =
1918 cpu_to_le64(lnet_nid_to_nid4(&msg->msg_txni->ni_nid));
1921 msg->msg_target_is_router = 1;
1922 msg->msg_target.pid = LNET_PID_LUSTRE;
1924 * since we're routing we want to ensure that the
1925 * msg_hdr.dest_nid is set to the final destination. When
1926 * the router receives this message it knows how to route
1929 * final_dst_lpni is set at the beginning of the
1930 * lnet_select_pathway() function and is never changed.
1931 * It's safe to use it here.
1933 msg->msg_hdr.dest_nid = cpu_to_le64(final_dst_lpni->lpni_nid);
1936 * if we're not routing set the dest_nid to the best peer
1937 * ni NID that we picked earlier in the algorithm.
1939 msg->msg_hdr.dest_nid = cpu_to_le64(msg->msg_txpeer->lpni_nid);
1943 * if we have response tracker block update it with the next hop
1947 rspt = msg->msg_md->md_rspt_ptr;
1949 rspt->rspt_next_hop_nid = msg->msg_txpeer->lpni_nid;
1950 CDEBUG(D_NET, "rspt_next_hop_nid = %s\n",
1951 libcfs_nid2str(rspt->rspt_next_hop_nid));
1955 rc = lnet_post_send_locked(msg, 0);
1958 CDEBUG(D_NET, "TRACE: %s(%s:%s) -> %s(%s:%s) %s : %s try# %d\n",
1959 libcfs_nid2str(msg->msg_hdr.src_nid),
1960 libcfs_nidstr(&msg->msg_txni->ni_nid),
1961 libcfs_nid2str(sd->sd_src_nid),
1962 libcfs_nid2str(msg->msg_hdr.dest_nid),
1963 libcfs_nid2str(sd->sd_dst_nid),
1964 libcfs_nid2str(msg->msg_txpeer->lpni_nid),
1965 libcfs_nid2str(sd->sd_rtr_nid),
1966 lnet_msgtyp2str(msg->msg_type), msg->msg_retry_count);
1972 lnet_set_non_mr_pref_nid(struct lnet_peer_ni *lpni, struct lnet_ni *lni,
1973 struct lnet_msg *msg)
1975 if (!lnet_peer_is_multi_rail(lpni->lpni_peer_net->lpn_peer) &&
1976 !lnet_msg_is_response(msg) && lpni->lpni_pref_nnids == 0) {
1977 CDEBUG(D_NET, "Setting preferred local NID %s on NMR peer %s\n",
1978 libcfs_nidstr(&lni->ni_nid),
1979 libcfs_nid2str(lpni->lpni_nid));
1980 lnet_peer_ni_set_non_mr_pref_nid(
1981 lpni, lnet_nid_to_nid4(&lni->ni_nid));
1990 * use the source and destination NIDs as the pathway
1993 lnet_handle_spec_local_nmr_dst(struct lnet_send_data *sd)
1995 /* the destination lpni is set before we get here. */
1998 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
1999 if (!sd->sd_best_ni) {
2000 CERROR("Can't send to %s: src %s is not a "
2001 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
2002 libcfs_nid2str(sd->sd_src_nid));
2006 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
2008 return lnet_handle_send(sd);
2016 * Don't run the selection algorithm on the peer NIs. By specifying the
2017 * local NID, we're also saying that we should always use the destination NID
2018 * provided. This handles the case where we should be using the same
2019 * destination NID for the all the messages which belong to the same RPC
2023 lnet_handle_spec_local_mr_dst(struct lnet_send_data *sd)
2025 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
2026 if (!sd->sd_best_ni) {
2027 CERROR("Can't send to %s: src %s is not a "
2028 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
2029 libcfs_nid2str(sd->sd_src_nid));
2033 if (sd->sd_best_lpni &&
2034 sd->sd_best_lpni->lpni_nid ==
2035 lnet_nid_to_nid4(&the_lnet.ln_loni->ni_nid))
2036 return lnet_handle_lo_send(sd);
2037 else if (sd->sd_best_lpni)
2038 return lnet_handle_send(sd);
2040 CERROR("can't send to %s. no NI on %s\n",
2041 libcfs_nid2str(sd->sd_dst_nid),
2042 libcfs_net2str(sd->sd_best_ni->ni_net->net_id));
2044 return -EHOSTUNREACH;
2048 lnet_find_best_ni_on_spec_net(struct lnet_ni *cur_best_ni,
2049 struct lnet_peer *peer,
2050 struct lnet_peer_net *peer_net,
2051 struct lnet_msg *msg,
2054 struct lnet_net *local_net;
2055 struct lnet_ni *best_ni;
2057 local_net = lnet_get_net_locked(peer_net->lpn_net_id);
2062 * Iterate through the NIs in this local Net and select
2063 * the NI to send from. The selection is determined by
2064 * these 3 criterion in the following priority:
2066 * 2. NI available credits
2069 best_ni = lnet_get_best_ni(local_net, cur_best_ni,
2070 peer, peer_net, msg, cpt);
2076 lnet_initiate_peer_discovery(struct lnet_peer_ni *lpni, struct lnet_msg *msg,
2079 struct lnet_peer *peer;
2080 struct lnet_peer_ni *new_lpni;
2083 lnet_peer_ni_addref_locked(lpni);
2085 peer = lpni->lpni_peer_net->lpn_peer;
2087 if (lnet_peer_gw_discovery(peer)) {
2088 lnet_peer_ni_decref_locked(lpni);
2092 if (!lnet_msg_discovery(msg) || lnet_peer_is_uptodate(peer)) {
2093 lnet_peer_ni_decref_locked(lpni);
2097 rc = lnet_discover_peer_locked(lpni, cpt, false);
2099 lnet_peer_ni_decref_locked(lpni);
2103 new_lpni = lnet_find_peer_ni_locked(lpni->lpni_nid);
2105 lnet_peer_ni_decref_locked(lpni);
2109 peer = new_lpni->lpni_peer_net->lpn_peer;
2110 spin_lock(&peer->lp_lock);
2111 if (lpni == new_lpni && lnet_peer_is_uptodate_locked(peer)) {
2112 /* The peer NI did not change and the peer is up to date.
2113 * Nothing more to do.
2115 spin_unlock(&peer->lp_lock);
2116 lnet_peer_ni_decref_locked(lpni);
2117 lnet_peer_ni_decref_locked(new_lpni);
2120 spin_unlock(&peer->lp_lock);
2122 /* Either the peer NI changed during discovery, or the peer isn't up
2123 * to date. In both cases we want to queue the message on the
2124 * (possibly new) peer's pending queue and queue the peer for discovery
2126 msg->msg_sending = 0;
2127 msg->msg_txpeer = NULL;
2128 lnet_net_unlock(cpt);
2129 lnet_peer_queue_message(peer, msg);
2132 lnet_peer_ni_decref_locked(lpni);
2133 lnet_peer_ni_decref_locked(new_lpni);
2135 CDEBUG(D_NET, "msg %p delayed. %s pending discovery\n",
2136 msg, libcfs_nid2str(peer->lp_primary_nid));
2138 return LNET_DC_WAIT;
2142 lnet_handle_find_routed_path(struct lnet_send_data *sd,
2144 struct lnet_peer_ni **gw_lpni,
2145 struct lnet_peer **gw_peer)
2148 struct lnet_peer *gw;
2149 struct lnet_peer *lp;
2150 struct lnet_peer_net *lpn;
2151 struct lnet_peer_net *best_lpn = NULL;
2152 struct lnet_remotenet *rnet, *best_rnet = NULL;
2153 struct lnet_route *best_route = NULL;
2154 struct lnet_route *last_route = NULL;
2155 struct lnet_peer_ni *lpni = NULL;
2156 struct lnet_peer_ni *gwni = NULL;
2157 bool route_found = false;
2158 lnet_nid_t src_nid = (sd->sd_src_nid != LNET_NID_ANY) ? sd->sd_src_nid :
2159 (sd->sd_best_ni != NULL)
2160 ? lnet_nid_to_nid4(&sd->sd_best_ni->ni_nid)
2162 int best_lpn_healthv = 0;
2163 __u32 best_lpn_sel_prio = LNET_MAX_SELECTION_PRIORITY;
2165 CDEBUG(D_NET, "using src nid %s for route restriction\n",
2166 libcfs_nid2str(src_nid));
2168 /* If a router nid was specified then we are replying to a GET or
2169 * sending an ACK. In this case we use the gateway associated with the
2170 * specified router nid.
2172 if (sd->sd_rtr_nid != LNET_NID_ANY) {
2173 gwni = lnet_find_peer_ni_locked(sd->sd_rtr_nid);
2175 gw = gwni->lpni_peer_net->lpn_peer;
2176 lnet_peer_ni_decref_locked(gwni);
2177 if (gw->lp_rtr_refcount)
2180 CWARN("No peer NI for gateway %s. Attempting to find an alternative route.\n",
2181 libcfs_nid2str(sd->sd_rtr_nid));
2186 if (sd->sd_msg->msg_routing) {
2187 /* If I'm routing this message then I need to find the
2188 * next hop based on the destination NID
2190 best_rnet = lnet_find_rnet_locked(LNET_NIDNET(sd->sd_dst_nid));
2192 CERROR("Unable to route message to %s - Route table may be misconfigured\n",
2193 libcfs_nid2str(sd->sd_dst_nid));
2194 return -EHOSTUNREACH;
2197 /* we've already looked up the initial lpni using
2200 lpni = sd->sd_best_lpni;
2201 /* the peer tree must be in existence */
2202 LASSERT(lpni && lpni->lpni_peer_net &&
2203 lpni->lpni_peer_net->lpn_peer);
2204 lp = lpni->lpni_peer_net->lpn_peer;
2206 list_for_each_entry(lpn, &lp->lp_peer_nets, lpn_peer_nets) {
2207 /* is this remote network reachable? */
2208 rnet = lnet_find_rnet_locked(lpn->lpn_net_id);
2217 /* select the preferred peer net */
2218 if (best_lpn_healthv > lpn->lpn_healthv)
2220 else if (best_lpn_healthv < lpn->lpn_healthv)
2223 if (best_lpn_sel_prio < lpn->lpn_sel_priority)
2225 else if (best_lpn_sel_prio > lpn->lpn_sel_priority)
2228 if (best_lpn->lpn_seq <= lpn->lpn_seq)
2231 best_lpn_healthv = lpn->lpn_healthv;
2232 best_lpn_sel_prio = lpn->lpn_sel_priority;
2238 CERROR("peer %s has no available nets\n",
2239 libcfs_nid2str(sd->sd_dst_nid));
2240 return -EHOSTUNREACH;
2243 sd->sd_best_lpni = lnet_find_best_lpni(sd->sd_best_ni,
2246 best_lpn->lpn_net_id);
2247 if (!sd->sd_best_lpni) {
2248 CERROR("peer %s is unreachable\n",
2249 libcfs_nid2str(sd->sd_dst_nid));
2250 return -EHOSTUNREACH;
2253 /* We're attempting to round robin over the remote peer
2254 * NI's so update the final destination we selected
2256 sd->sd_final_dst_lpni = sd->sd_best_lpni;
2258 /* Increment the sequence number of the remote lpni so
2259 * we can round robin over the different interfaces of
2262 sd->sd_best_lpni->lpni_seq++;
2266 * find the best route. Restrict the selection on the net of the
2267 * local NI if we've already picked the local NI to send from.
2268 * Otherwise, let's pick any route we can find and then find
2269 * a local NI we can reach the route's gateway on. Any route we
2270 * select will be reachable by virtue of the restriction we have
2271 * when adding a route.
2273 best_route = lnet_find_route_locked(best_rnet,
2274 LNET_NIDNET(src_nid),
2276 &last_route, &gwni);
2279 CERROR("no route to %s from %s\n",
2280 libcfs_nid2str(dst_nid),
2281 libcfs_nid2str(src_nid));
2282 return -EHOSTUNREACH;
2286 CERROR("Internal Error. Route expected to %s from %s\n",
2287 libcfs_nid2str(dst_nid),
2288 libcfs_nid2str(src_nid));
2292 gw = best_route->lr_gateway;
2293 LASSERT(gw == gwni->lpni_peer_net->lpn_peer);
2297 * Discover this gateway if it hasn't already been discovered.
2298 * This means we might delay the message until discovery has
2301 rc = lnet_initiate_peer_discovery(gwni, sd->sd_msg, sd->sd_cpt);
2305 if (!sd->sd_best_ni) {
2306 lpn = gwni->lpni_peer_net;
2307 sd->sd_best_ni = lnet_find_best_ni_on_spec_net(NULL, gw, lpn,
2310 if (!sd->sd_best_ni) {
2311 CERROR("Internal Error. Expected local ni on %s but non found: %s\n",
2312 libcfs_net2str(lpn->lpn_net_id),
2313 libcfs_nid2str(sd->sd_src_nid));
2322 * increment the sequence numbers since now we're sure we're
2323 * going to use this path
2325 if (sd->sd_rtr_nid == LNET_NID_ANY) {
2326 LASSERT(best_route && last_route);
2327 best_route->lr_seq = last_route->lr_seq + 1;
2329 best_lpn->lpn_seq++;
2340 * Remote destination
2341 * Non-MR destination
2345 * Remote destination
2348 * The handling of these two cases is similar. Even though the destination
2349 * can be MR or non-MR, we'll deal directly with the router.
2352 lnet_handle_spec_router_dst(struct lnet_send_data *sd)
2355 struct lnet_peer_ni *gw_lpni = NULL;
2356 struct lnet_peer *gw_peer = NULL;
2359 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
2360 if (!sd->sd_best_ni) {
2361 CERROR("Can't send to %s: src %s is not a "
2362 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
2363 libcfs_nid2str(sd->sd_src_nid));
2367 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2372 if (sd->sd_send_case & NMR_DST)
2374 * since the final destination is non-MR let's set its preferred
2375 * NID before we send
2377 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni,
2381 * We're going to send to the gw found so let's set its
2384 sd->sd_peer = gw_peer;
2385 sd->sd_best_lpni = gw_lpni;
2387 return lnet_handle_send(sd);
2391 lnet_find_best_ni_on_local_net(struct lnet_peer *peer, int md_cpt,
2392 struct lnet_msg *msg, bool discovery)
2394 struct lnet_peer_net *lpn = NULL;
2395 struct lnet_peer_net *best_lpn = NULL;
2396 struct lnet_net *net = NULL;
2397 struct lnet_net *best_net = NULL;
2398 struct lnet_ni *best_ni = NULL;
2399 int best_lpn_healthv = 0;
2400 int best_net_healthv = 0;
2402 __u32 best_lpn_sel_prio = LNET_MAX_SELECTION_PRIORITY;
2404 __u32 best_net_sel_prio = LNET_MAX_SELECTION_PRIORITY;
2409 * The peer can have multiple interfaces, some of them can be on
2410 * the local network and others on a routed network. We should
2411 * prefer the local network. However if the local network is not
2412 * available then we need to try the routed network
2415 /* go through all the peer nets and find the best_ni */
2416 list_for_each_entry(lpn, &peer->lp_peer_nets, lpn_peer_nets) {
2418 * The peer's list of nets can contain non-local nets. We
2419 * want to only examine the local ones.
2421 net = lnet_get_net_locked(lpn->lpn_net_id);
2425 lpn_sel_prio = lpn->lpn_sel_priority;
2426 net_healthv = lnet_get_net_healthv_locked(net);
2427 net_sel_prio = net->net_sel_priority;
2430 * if this is a discovery message and lp_disc_net_id is
2431 * specified then use that net to send the discovery on.
2433 if (peer->lp_disc_net_id == lpn->lpn_net_id &&
2442 /* always select the lpn with the best health */
2443 if (best_lpn_healthv > lpn->lpn_healthv)
2445 else if (best_lpn_healthv < lpn->lpn_healthv)
2448 /* select the preferred peer and local nets */
2449 if (best_lpn_sel_prio < lpn_sel_prio)
2451 else if (best_lpn_sel_prio > lpn_sel_prio)
2454 if (best_net_healthv > net_healthv)
2456 else if (best_net_healthv < net_healthv)
2459 if (best_net_sel_prio < net_sel_prio)
2461 else if (best_net_sel_prio > net_sel_prio)
2464 if (best_lpn->lpn_seq < lpn->lpn_seq)
2466 else if (best_lpn->lpn_seq > lpn->lpn_seq)
2469 /* round robin over the local networks */
2470 if (best_net->net_seq <= net->net_seq)
2474 best_net_healthv = net_healthv;
2475 best_net_sel_prio = net_sel_prio;
2476 best_lpn_healthv = lpn->lpn_healthv;
2477 best_lpn_sel_prio = lpn_sel_prio;
2486 /* Select the best NI on the same net as best_lpn chosen
2489 best_ni = lnet_find_best_ni_on_spec_net(NULL, peer, best_lpn,
2496 static struct lnet_ni *
2497 lnet_find_existing_preferred_best_ni(struct lnet_peer_ni *lpni, int cpt)
2499 struct lnet_ni *best_ni = NULL;
2500 struct lnet_peer_net *peer_net = lpni->lpni_peer_net;
2501 struct lnet_peer_ni *lpni_entry;
2504 * We must use a consistent source address when sending to a
2505 * non-MR peer. However, a non-MR peer can have multiple NIDs
2506 * on multiple networks, and we may even need to talk to this
2507 * peer on multiple networks -- certain types of
2508 * load-balancing configuration do this.
2510 * So we need to pick the NI the peer prefers for this
2511 * particular network.
2514 list_for_each_entry(lpni_entry, &peer_net->lpn_peer_nis,
2516 if (lpni_entry->lpni_pref_nnids == 0)
2518 LASSERT(lpni_entry->lpni_pref_nnids == 1);
2519 best_ni = lnet_nid2ni_locked(lpni_entry->lpni_pref.nid, cpt);
2526 /* Prerequisite: sd->sd_peer and sd->sd_best_lpni should be set */
2528 lnet_select_preferred_best_ni(struct lnet_send_data *sd)
2530 struct lnet_ni *best_ni = NULL;
2531 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
2534 * We must use a consistent source address when sending to a
2535 * non-MR peer. However, a non-MR peer can have multiple NIDs
2536 * on multiple networks, and we may even need to talk to this
2537 * peer on multiple networks -- certain types of
2538 * load-balancing configuration do this.
2540 * So we need to pick the NI the peer prefers for this
2541 * particular network.
2544 best_ni = lnet_find_existing_preferred_best_ni(sd->sd_best_lpni,
2547 /* if best_ni is still not set just pick one */
2550 lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2551 sd->sd_best_lpni->lpni_peer_net,
2554 /* If there is no best_ni we don't have a route */
2556 CERROR("no path to %s from net %s\n",
2557 libcfs_nid2str(best_lpni->lpni_nid),
2558 libcfs_net2str(best_lpni->lpni_net->net_id));
2559 return -EHOSTUNREACH;
2563 sd->sd_best_ni = best_ni;
2565 /* Set preferred NI if necessary. */
2566 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
2573 * Source not specified
2577 * always use the same source NID for NMR peers
2578 * If we've talked to that peer before then we already have a preferred
2579 * source NI associated with it. Otherwise, we select a preferred local NI
2580 * and store it in the peer
2583 lnet_handle_any_local_nmr_dst(struct lnet_send_data *sd)
2587 /* sd->sd_best_lpni is already set to the final destination */
2590 * At this point we should've created the peer ni and peer. If we
2591 * can't find it, then something went wrong. Instead of assert
2592 * output a relevant message and fail the send
2594 if (!sd->sd_best_lpni) {
2595 CERROR("Internal fault. Unable to send msg %s to %s. "
2597 lnet_msgtyp2str(sd->sd_msg->msg_type),
2598 libcfs_nid2str(sd->sd_dst_nid));
2602 if (sd->sd_msg->msg_routing) {
2603 /* If I'm forwarding this message then I can choose any NI
2604 * on the destination peer net
2606 sd->sd_best_ni = lnet_find_best_ni_on_spec_net(NULL,
2608 sd->sd_best_lpni->lpni_peer_net,
2611 if (!sd->sd_best_ni) {
2612 CERROR("Unable to forward message to %s. No local NI available\n",
2613 libcfs_nid2str(sd->sd_dst_nid));
2617 rc = lnet_select_preferred_best_ni(sd);
2620 rc = lnet_handle_send(sd);
2626 lnet_handle_any_mr_dsta(struct lnet_send_data *sd)
2629 * NOTE we've already handled the remote peer case. So we only
2630 * need to worry about the local case here.
2632 * if we're sending a response, ACK or reply, we need to send it
2633 * to the destination NID given to us. At this point we already
2634 * have the peer_ni we're suppose to send to, so just find the
2635 * best_ni on the peer net and use that. Since we're sending to an
2636 * MR peer then we can just run the selection algorithm on our
2637 * local NIs and pick the best one.
2639 if (sd->sd_send_case & SND_RESP) {
2641 lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2642 sd->sd_best_lpni->lpni_peer_net,
2646 if (!sd->sd_best_ni) {
2648 * We're not going to deal with not able to send
2649 * a response to the provided final destination
2651 CERROR("Can't send response to %s. "
2652 "No local NI available\n",
2653 libcfs_nid2str(sd->sd_dst_nid));
2654 return -EHOSTUNREACH;
2657 return lnet_handle_send(sd);
2661 * If we get here that means we're sending a fresh request, PUT or
2662 * GET, so we need to run our standard selection algorithm.
2663 * First find the best local interface that's on any of the peer's
2666 sd->sd_best_ni = lnet_find_best_ni_on_local_net(sd->sd_peer,
2669 lnet_msg_discovery(sd->sd_msg));
2670 if (sd->sd_best_ni) {
2672 lnet_find_best_lpni(sd->sd_best_ni, sd->sd_dst_nid,
2674 sd->sd_best_ni->ni_net->net_id);
2677 * if we're successful in selecting a peer_ni on the local
2678 * network, then send to it. Otherwise fall through and
2679 * try and see if we can reach it over another routed
2682 if (sd->sd_best_lpni &&
2683 sd->sd_best_lpni->lpni_nid ==
2684 lnet_nid_to_nid4(&the_lnet.ln_loni->ni_nid)) {
2686 * in case we initially started with a routed
2687 * destination, let's reset to local
2689 sd->sd_send_case &= ~REMOTE_DST;
2690 sd->sd_send_case |= LOCAL_DST;
2691 return lnet_handle_lo_send(sd);
2692 } else if (sd->sd_best_lpni) {
2694 * in case we initially started with a routed
2695 * destination, let's reset to local
2697 sd->sd_send_case &= ~REMOTE_DST;
2698 sd->sd_send_case |= LOCAL_DST;
2699 return lnet_handle_send(sd);
2702 CERROR("Internal Error. Expected to have a best_lpni: "
2704 libcfs_nid2str(sd->sd_src_nid),
2705 libcfs_nid2str(sd->sd_dst_nid));
2711 * Peer doesn't have a local network. Let's see if there is
2712 * a remote network we can reach it on.
2714 return PASS_THROUGH;
2719 * Source NID not specified
2724 * Source NID not speified
2725 * Remote destination
2728 * In both of these cases if we're sending a response, ACK or REPLY, then
2729 * we need to send to the destination NID provided.
2731 * In the remote case let's deal with MR routers.
2736 lnet_handle_any_mr_dst(struct lnet_send_data *sd)
2739 struct lnet_peer *gw_peer = NULL;
2740 struct lnet_peer_ni *gw_lpni = NULL;
2743 * handle sending a response to a remote peer here so we don't
2744 * have to worry about it if we hit lnet_handle_any_mr_dsta()
2746 if (sd->sd_send_case & REMOTE_DST &&
2747 sd->sd_send_case & SND_RESP) {
2748 struct lnet_peer_ni *gw;
2749 struct lnet_peer *gw_peer;
2751 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw,
2754 CERROR("Can't send response to %s. "
2755 "No route available\n",
2756 libcfs_nid2str(sd->sd_dst_nid));
2757 return -EHOSTUNREACH;
2758 } else if (rc > 0) {
2762 sd->sd_best_lpni = gw;
2763 sd->sd_peer = gw_peer;
2765 return lnet_handle_send(sd);
2769 * Even though the NID for the peer might not be on a local network,
2770 * since the peer is MR there could be other interfaces on the
2771 * local network. In that case we'd still like to prefer the local
2772 * network over the routed network. If we're unable to do that
2773 * then we select the best router among the different routed networks,
2774 * and if the router is MR then we can deal with it as such.
2776 rc = lnet_handle_any_mr_dsta(sd);
2777 if (rc != PASS_THROUGH)
2781 * Now that we must route to the destination, we must consider the
2782 * MR case, where the destination has multiple interfaces, some of
2783 * which we can route to and others we do not. For this reason we
2784 * need to select the destination which we can route to and if
2785 * there are multiple, we need to round robin.
2787 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2792 sd->sd_send_case &= ~LOCAL_DST;
2793 sd->sd_send_case |= REMOTE_DST;
2795 sd->sd_peer = gw_peer;
2796 sd->sd_best_lpni = gw_lpni;
2798 return lnet_handle_send(sd);
2802 * Source not specified
2803 * Remote destination
2806 * Must send to the specified peer NID using the same source NID that
2807 * we've used before. If it's the first time to talk to that peer then
2808 * find the source NI and assign it as preferred to that peer
2811 lnet_handle_any_router_nmr_dst(struct lnet_send_data *sd)
2814 struct lnet_peer_ni *gw_lpni = NULL;
2815 struct lnet_peer *gw_peer = NULL;
2818 * Let's see if we have a preferred NI to talk to this NMR peer
2820 sd->sd_best_ni = lnet_find_existing_preferred_best_ni(sd->sd_best_lpni,
2824 * find the router and that'll find the best NI if we didn't find
2827 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2833 * set the best_ni we've chosen as the preferred one for
2836 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
2838 /* we'll be sending to the gw */
2839 sd->sd_best_lpni = gw_lpni;
2840 sd->sd_peer = gw_peer;
2842 return lnet_handle_send(sd);
2846 lnet_handle_send_case_locked(struct lnet_send_data *sd)
2849 * turn off the SND_RESP bit.
2850 * It will be checked in the case handling
2852 __u32 send_case = sd->sd_send_case &= ~SND_RESP ;
2854 CDEBUG(D_NET, "Source %s%s to %s %s %s destination\n",
2855 (send_case & SRC_SPEC) ? "Specified: " : "ANY",
2856 (send_case & SRC_SPEC) ? libcfs_nid2str(sd->sd_src_nid) : "",
2857 (send_case & MR_DST) ? "MR: " : "NMR: ",
2858 libcfs_nid2str(sd->sd_dst_nid),
2859 (send_case & LOCAL_DST) ? "local" : "routed");
2861 switch (send_case) {
2863 * For all cases where the source is specified, we should always
2864 * use the destination NID, whether it's an MR destination or not,
2865 * since we're continuing a series of related messages for the
2868 case SRC_SPEC_LOCAL_NMR_DST:
2869 return lnet_handle_spec_local_nmr_dst(sd);
2870 case SRC_SPEC_LOCAL_MR_DST:
2871 return lnet_handle_spec_local_mr_dst(sd);
2872 case SRC_SPEC_ROUTER_NMR_DST:
2873 case SRC_SPEC_ROUTER_MR_DST:
2874 return lnet_handle_spec_router_dst(sd);
2875 case SRC_ANY_LOCAL_NMR_DST:
2876 return lnet_handle_any_local_nmr_dst(sd);
2877 case SRC_ANY_LOCAL_MR_DST:
2878 case SRC_ANY_ROUTER_MR_DST:
2879 return lnet_handle_any_mr_dst(sd);
2880 case SRC_ANY_ROUTER_NMR_DST:
2881 return lnet_handle_any_router_nmr_dst(sd);
2883 CERROR("Unknown send case\n");
2889 lnet_select_pathway(lnet_nid_t src_nid, lnet_nid_t dst_nid,
2890 struct lnet_msg *msg, lnet_nid_t rtr_nid)
2892 struct lnet_peer_ni *lpni;
2893 struct lnet_peer *peer;
2894 struct lnet_send_data send_data;
2897 __u32 send_case = 0;
2899 bool mr_forwarding_allowed;
2901 memset(&send_data, 0, sizeof(send_data));
2904 * get an initial CPT to use for locking. The idea here is not to
2905 * serialize the calls to select_pathway, so that as many
2906 * operations can run concurrently as possible. To do that we use
2907 * the CPT where this call is being executed. Later on when we
2908 * determine the CPT to use in lnet_message_commit, we switch the
2909 * lock and check if there was any configuration change. If none,
2910 * then we proceed, if there is, then we restart the operation.
2912 cpt = lnet_net_lock_current();
2914 md_cpt = lnet_cpt_of_md(msg->msg_md, msg->msg_offset);
2915 if (md_cpt == CFS_CPT_ANY)
2921 * If we're being asked to send to the loopback interface, there
2922 * is no need to go through any selection. We can just shortcut
2923 * the entire process and send over lolnd
2925 send_data.sd_msg = msg;
2926 send_data.sd_cpt = cpt;
2927 if (dst_nid == LNET_NID_LO_0) {
2928 rc = lnet_handle_lo_send(&send_data);
2929 lnet_net_unlock(cpt);
2934 * find an existing peer_ni, or create one and mark it as having been
2935 * created due to network traffic. This call will create the
2936 * peer->peer_net->peer_ni tree.
2938 lpni = lnet_nid2peerni_locked(dst_nid, LNET_NID_ANY, cpt);
2940 lnet_net_unlock(cpt);
2941 return PTR_ERR(lpni);
2945 * Cache the original src_nid and rtr_nid. If we need to resend the
2946 * message then we'll need to know whether the src_nid was originally
2947 * specified for this message. If it was originally specified,
2948 * then we need to keep using the same src_nid since it's
2949 * continuing the same sequence of messages. Similarly, rtr_nid will
2950 * affect our choice of next hop.
2952 msg->msg_src_nid_param = src_nid;
2953 msg->msg_rtr_nid_param = rtr_nid;
2956 * If necessary, perform discovery on the peer that owns this peer_ni.
2957 * Note, this can result in the ownership of this peer_ni changing
2958 * to another peer object.
2960 rc = lnet_initiate_peer_discovery(lpni, msg, cpt);
2962 lnet_peer_ni_decref_locked(lpni);
2963 lnet_net_unlock(cpt);
2966 lnet_peer_ni_decref_locked(lpni);
2968 peer = lpni->lpni_peer_net->lpn_peer;
2971 * Identify the different send cases
2973 if (src_nid == LNET_NID_ANY)
2974 send_case |= SRC_ANY;
2976 send_case |= SRC_SPEC;
2978 if (lnet_get_net_locked(LNET_NIDNET(dst_nid)))
2979 send_case |= LOCAL_DST;
2981 send_case |= REMOTE_DST;
2984 if (msg->msg_routing && (send_case & LOCAL_DST))
2987 /* Determine whether to allow MR forwarding for this message.
2988 * NB: MR forwarding is allowed if the message originator and the
2989 * destination are both MR capable, and the destination lpni that was
2990 * originally chosen by the originator is unhealthy or down.
2991 * We check the MR capability of the destination further below
2993 mr_forwarding_allowed = false;
2995 struct lnet_peer *src_lp;
2996 struct lnet_peer_ni *src_lpni;
2998 src_lpni = lnet_nid2peerni_locked(msg->msg_hdr.src_nid,
3000 /* We don't fail the send if we hit any errors here. We'll just
3001 * try to send it via non-multi-rail criteria
3003 if (!IS_ERR(src_lpni)) {
3004 /* Drop ref taken by lnet_nid2peerni_locked() */
3005 lnet_peer_ni_decref_locked(src_lpni);
3006 src_lp = lpni->lpni_peer_net->lpn_peer;
3007 if (lnet_peer_is_multi_rail(src_lp) &&
3008 !lnet_is_peer_ni_alive(lpni))
3009 mr_forwarding_allowed = true;
3012 CDEBUG(D_NET, "msg %p MR forwarding %s\n", msg,
3013 mr_forwarding_allowed ? "allowed" : "not allowed");
3017 * Deal with the peer as NMR in the following cases:
3018 * 1. the peer is NMR
3019 * 2. We're trying to recover a specific peer NI
3020 * 3. I'm a router sending to the final destination and MR forwarding is
3021 * not allowed for this message (as determined above).
3022 * In this case the source of the message would've
3023 * already selected the final destination so my job
3024 * is to honor the selection.
3026 if (!lnet_peer_is_multi_rail(peer) || msg->msg_recovery ||
3027 (final_hop && !mr_forwarding_allowed))
3028 send_case |= NMR_DST;
3030 send_case |= MR_DST;
3032 if (lnet_msg_is_response(msg))
3033 send_case |= SND_RESP;
3035 /* assign parameters to the send_data */
3036 send_data.sd_rtr_nid = rtr_nid;
3037 send_data.sd_src_nid = src_nid;
3038 send_data.sd_dst_nid = dst_nid;
3039 send_data.sd_best_lpni = lpni;
3041 * keep a pointer to the final destination in case we're going to
3042 * route, so we'll need to access it later
3044 send_data.sd_final_dst_lpni = lpni;
3045 send_data.sd_peer = peer;
3046 send_data.sd_md_cpt = md_cpt;
3047 send_data.sd_send_case = send_case;
3049 rc = lnet_handle_send_case_locked(&send_data);
3052 * Update the local cpt since send_data.sd_cpt might've been
3053 * updated as a result of calling lnet_handle_send_case_locked().
3055 cpt = send_data.sd_cpt;
3057 if (rc == REPEAT_SEND)
3060 lnet_net_unlock(cpt);
3066 lnet_send(lnet_nid_t src_nid, struct lnet_msg *msg, lnet_nid_t rtr_nid)
3068 lnet_nid_t dst_nid = msg->msg_target.nid;
3072 * NB: rtr_nid is set to LNET_NID_ANY for all current use-cases,
3073 * but we might want to use pre-determined router for ACK/REPLY
3076 /* NB: ni != NULL == interface pre-determined (ACK/REPLY) */
3077 LASSERT(msg->msg_txpeer == NULL);
3078 LASSERT(msg->msg_txni == NULL);
3079 LASSERT(!msg->msg_sending);
3080 LASSERT(!msg->msg_target_is_router);
3081 LASSERT(!msg->msg_receiving);
3083 msg->msg_sending = 1;
3085 LASSERT(!msg->msg_tx_committed);
3087 rc = lnet_select_pathway(src_nid, dst_nid, msg, rtr_nid);
3089 if (rc == -EHOSTUNREACH)
3090 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_ERROR;
3092 msg->msg_health_status = LNET_MSG_STATUS_LOCAL_ERROR;
3096 if (rc == LNET_CREDIT_OK)
3097 lnet_ni_send(msg->msg_txni, msg);
3099 /* rc == LNET_CREDIT_OK or LNET_CREDIT_WAIT or LNET_DC_WAIT */
3103 enum lnet_mt_event_type {
3104 MT_TYPE_LOCAL_NI = 0,
3108 struct lnet_mt_event_info {
3109 enum lnet_mt_event_type mt_type;
3113 /* called with res_lock held */
3115 lnet_detach_rsp_tracker(struct lnet_libmd *md, int cpt)
3117 struct lnet_rsp_tracker *rspt;
3120 * msg has a refcount on the MD so the MD is not going away.
3121 * The rspt queue for the cpt is protected by
3122 * the lnet_net_lock(cpt). cpt is the cpt of the MD cookie.
3124 if (!md->md_rspt_ptr)
3127 rspt = md->md_rspt_ptr;
3130 LASSERT(rspt->rspt_cpt == cpt);
3132 md->md_rspt_ptr = NULL;
3134 if (LNetMDHandleIsInvalid(rspt->rspt_mdh)) {
3136 * The monitor thread has invalidated this handle because the
3137 * response timed out, but it failed to lookup the MD. That
3138 * means this response tracker is on the zombie list. We can
3139 * safely remove it under the resource lock (held by caller) and
3140 * free the response tracker block.
3142 list_del(&rspt->rspt_on_list);
3143 lnet_rspt_free(rspt, cpt);
3146 * invalidate the handle to indicate that a response has been
3147 * received, which will then lead the monitor thread to clean up
3150 LNetInvalidateMDHandle(&rspt->rspt_mdh);
3155 lnet_clean_zombie_rstqs(void)
3157 struct lnet_rsp_tracker *rspt, *tmp;
3160 cfs_cpt_for_each(i, lnet_cpt_table()) {
3161 list_for_each_entry_safe(rspt, tmp,
3162 the_lnet.ln_mt_zombie_rstqs[i],
3164 list_del(&rspt->rspt_on_list);
3165 lnet_rspt_free(rspt, i);
3169 cfs_percpt_free(the_lnet.ln_mt_zombie_rstqs);
3173 lnet_finalize_expired_responses(void)
3175 struct lnet_libmd *md;
3176 struct lnet_rsp_tracker *rspt, *tmp;
3180 if (the_lnet.ln_mt_rstq == NULL)
3183 cfs_cpt_for_each(i, lnet_cpt_table()) {
3184 LIST_HEAD(local_queue);
3187 if (!the_lnet.ln_mt_rstq[i]) {
3191 list_splice_init(the_lnet.ln_mt_rstq[i], &local_queue);
3196 list_for_each_entry_safe(rspt, tmp, &local_queue, rspt_on_list) {
3198 * The rspt mdh will be invalidated when a response
3199 * is received or whenever we want to discard the
3200 * block the monitor thread will walk the queue
3201 * and clean up any rsts with an invalid mdh.
3202 * The monitor thread will walk the queue until
3203 * the first unexpired rspt block. This means that
3204 * some rspt blocks which received their
3205 * corresponding responses will linger in the
3206 * queue until they are cleaned up eventually.
3209 if (LNetMDHandleIsInvalid(rspt->rspt_mdh)) {
3211 list_del(&rspt->rspt_on_list);
3212 lnet_rspt_free(rspt, i);
3216 if (ktime_compare(now, rspt->rspt_deadline) >= 0 ||
3217 the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN) {
3218 struct lnet_peer_ni *lpni;
3221 md = lnet_handle2md(&rspt->rspt_mdh);
3223 /* MD has been queued for unlink, but