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_nidstr(&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(!nid_same(&lp->lpni_nid, &the_lnet.ln_loni->ni_nid));
848 /* NB 'lp' is always the next hop */
849 if ((msg->msg_target.pid & LNET_PID_USERFLAG) == 0 &&
850 lnet_peer_alive_locked(ni, lp, msg) == 0) {
851 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
852 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length +=
854 lnet_net_unlock(cpt);
856 lnet_incr_stats(&msg->msg_txpeer->lpni_stats,
858 LNET_STATS_TYPE_DROP);
860 lnet_incr_stats(&msg->msg_txni->ni_stats,
862 LNET_STATS_TYPE_DROP);
864 CNETERR("Dropping message for %s: peer not alive\n",
865 libcfs_id2str(msg->msg_target));
866 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_DROPPED;
868 lnet_finalize(msg, -EHOSTUNREACH);
871 return -EHOSTUNREACH;
874 if (msg->msg_md != NULL &&
875 (msg->msg_md->md_flags & LNET_MD_FLAG_ABORTED) != 0) {
876 lnet_net_unlock(cpt);
878 CNETERR("Aborting message for %s: LNetM[DE]Unlink() already "
879 "called on the MD/ME.\n",
880 libcfs_id2str(msg->msg_target));
882 msg->msg_no_resend = true;
883 CDEBUG(D_NET, "msg %p to %s canceled and will not be resent\n",
884 msg, libcfs_id2str(msg->msg_target));
885 lnet_finalize(msg, -ECANCELED);
892 if (!msg->msg_peertxcredit) {
893 spin_lock(&lp->lpni_lock);
894 LASSERT((lp->lpni_txcredits < 0) ==
895 !list_empty(&lp->lpni_txq));
897 msg->msg_peertxcredit = 1;
898 lp->lpni_txqnob += msg->msg_len + sizeof(struct lnet_hdr);
899 lp->lpni_txcredits--;
901 if (lp->lpni_txcredits < lp->lpni_mintxcredits)
902 lp->lpni_mintxcredits = lp->lpni_txcredits;
904 if (lp->lpni_txcredits < 0) {
905 msg->msg_tx_delayed = 1;
906 list_add_tail(&msg->msg_list, &lp->lpni_txq);
907 spin_unlock(&lp->lpni_lock);
908 return LNET_CREDIT_WAIT;
910 spin_unlock(&lp->lpni_lock);
913 if (!msg->msg_txcredit) {
914 LASSERT((tq->tq_credits < 0) ==
915 !list_empty(&tq->tq_delayed));
917 msg->msg_txcredit = 1;
919 atomic_dec(&ni->ni_tx_credits);
921 if (tq->tq_credits < tq->tq_credits_min)
922 tq->tq_credits_min = tq->tq_credits;
924 if (tq->tq_credits < 0) {
925 msg->msg_tx_delayed = 1;
926 list_add_tail(&msg->msg_list, &tq->tq_delayed);
927 return LNET_CREDIT_WAIT;
931 if (unlikely(!list_empty(&the_lnet.ln_delay_rules)) &&
932 lnet_delay_rule_match_locked(&msg->msg_hdr, msg)) {
933 msg->msg_tx_delayed = 1;
934 return LNET_CREDIT_WAIT;
937 /* unset the tx_delay flag as we're going to send it now */
938 msg->msg_tx_delayed = 0;
941 lnet_net_unlock(cpt);
942 lnet_ni_send(ni, msg);
945 return LNET_CREDIT_OK;
949 static struct lnet_rtrbufpool *
950 lnet_msg2bufpool(struct lnet_msg *msg)
952 struct lnet_rtrbufpool *rbp;
955 LASSERT(msg->msg_rx_committed);
957 cpt = msg->msg_rx_cpt;
958 rbp = &the_lnet.ln_rtrpools[cpt][0];
960 LASSERT(msg->msg_len <= LNET_MTU);
961 while (msg->msg_len > (unsigned int)rbp->rbp_npages * PAGE_SIZE) {
963 LASSERT(rbp < &the_lnet.ln_rtrpools[cpt][LNET_NRBPOOLS]);
970 lnet_post_routed_recv_locked(struct lnet_msg *msg, int do_recv)
972 /* lnet_parse is going to lnet_net_unlock immediately after this, so it
973 * sets do_recv FALSE and I don't do the unlock/send/lock bit.
974 * I return LNET_CREDIT_WAIT if msg blocked and LNET_CREDIT_OK if
975 * received or OK to receive */
976 struct lnet_peer_ni *lpni = msg->msg_rxpeer;
977 struct lnet_peer *lp;
978 struct lnet_rtrbufpool *rbp;
979 struct lnet_rtrbuf *rb;
981 LASSERT(msg->msg_kiov == NULL);
982 LASSERT(msg->msg_niov == 0);
983 LASSERT(msg->msg_routing);
984 LASSERT(msg->msg_receiving);
985 LASSERT(!msg->msg_sending);
986 LASSERT(lpni->lpni_peer_net);
987 LASSERT(lpni->lpni_peer_net->lpn_peer);
989 lp = lpni->lpni_peer_net->lpn_peer;
991 /* non-lnet_parse callers only receive delayed messages */
992 LASSERT(!do_recv || msg->msg_rx_delayed);
994 if (!msg->msg_peerrtrcredit) {
995 /* lpni_lock protects the credit manipulation */
996 spin_lock(&lpni->lpni_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 spin_unlock(&lpni->lpni_lock);
1005 /* must have checked eager_recv before here */
1006 LASSERT(msg->msg_rx_ready_delay);
1007 msg->msg_rx_delayed = 1;
1008 /* lp_lock protects the lp_rtrq */
1009 spin_lock(&lp->lp_lock);
1010 list_add_tail(&msg->msg_list, &lp->lp_rtrq);
1011 spin_unlock(&lp->lp_lock);
1012 return LNET_CREDIT_WAIT;
1014 spin_unlock(&lpni->lpni_lock);
1017 rbp = lnet_msg2bufpool(msg);
1019 if (!msg->msg_rtrcredit) {
1020 msg->msg_rtrcredit = 1;
1022 if (rbp->rbp_credits < rbp->rbp_mincredits)
1023 rbp->rbp_mincredits = rbp->rbp_credits;
1025 if (rbp->rbp_credits < 0) {
1026 /* must have checked eager_recv before here */
1027 LASSERT(msg->msg_rx_ready_delay);
1028 msg->msg_rx_delayed = 1;
1029 list_add_tail(&msg->msg_list, &rbp->rbp_msgs);
1030 return LNET_CREDIT_WAIT;
1034 LASSERT(!list_empty(&rbp->rbp_bufs));
1035 rb = list_entry(rbp->rbp_bufs.next, struct lnet_rtrbuf, rb_list);
1036 list_del(&rb->rb_list);
1038 msg->msg_niov = rbp->rbp_npages;
1039 msg->msg_kiov = &rb->rb_kiov[0];
1041 /* unset the msg-rx_delayed flag since we're receiving the message */
1042 msg->msg_rx_delayed = 0;
1045 int cpt = msg->msg_rx_cpt;
1047 lnet_net_unlock(cpt);
1048 lnet_ni_recv(msg->msg_rxni, msg->msg_private, msg, 1,
1049 0, msg->msg_len, msg->msg_len);
1052 return LNET_CREDIT_OK;
1056 lnet_return_tx_credits_locked(struct lnet_msg *msg)
1058 struct lnet_peer_ni *txpeer = msg->msg_txpeer;
1059 struct lnet_ni *txni = msg->msg_txni;
1060 struct lnet_msg *msg2;
1062 if (msg->msg_txcredit) {
1063 struct lnet_ni *ni = msg->msg_txni;
1064 struct lnet_tx_queue *tq = ni->ni_tx_queues[msg->msg_tx_cpt];
1066 /* give back NI txcredits */
1067 msg->msg_txcredit = 0;
1069 LASSERT((tq->tq_credits < 0) ==
1070 !list_empty(&tq->tq_delayed));
1073 atomic_inc(&ni->ni_tx_credits);
1074 if (tq->tq_credits <= 0) {
1075 msg2 = list_entry(tq->tq_delayed.next,
1076 struct lnet_msg, msg_list);
1077 list_del(&msg2->msg_list);
1079 LASSERT(msg2->msg_txni == ni);
1080 LASSERT(msg2->msg_tx_delayed);
1081 LASSERT(msg2->msg_tx_cpt == msg->msg_tx_cpt);
1083 (void) lnet_post_send_locked(msg2, 1);
1087 if (msg->msg_peertxcredit) {
1088 /* give back peer txcredits */
1089 msg->msg_peertxcredit = 0;
1091 spin_lock(&txpeer->lpni_lock);
1092 LASSERT((txpeer->lpni_txcredits < 0) ==
1093 !list_empty(&txpeer->lpni_txq));
1095 txpeer->lpni_txqnob -= msg->msg_len + sizeof(struct lnet_hdr);
1096 LASSERT(txpeer->lpni_txqnob >= 0);
1098 txpeer->lpni_txcredits++;
1099 if (txpeer->lpni_txcredits <= 0) {
1102 msg2 = list_entry(txpeer->lpni_txq.next,
1103 struct lnet_msg, msg_list);
1104 list_del(&msg2->msg_list);
1105 spin_unlock(&txpeer->lpni_lock);
1107 LASSERT(msg2->msg_txpeer == txpeer);
1108 LASSERT(msg2->msg_tx_delayed);
1110 msg2_cpt = msg2->msg_tx_cpt;
1113 * The msg_cpt can be different from the msg2_cpt
1114 * so we need to make sure we lock the correct cpt
1116 * Once we call lnet_post_send_locked() it is no
1117 * longer safe to access msg2, since it could've
1118 * been freed by lnet_finalize(), but we still
1119 * need to relock the correct cpt, so we cache the
1120 * msg2_cpt for the purpose of the check that
1121 * follows the call to lnet_pose_send_locked().
1123 if (msg2_cpt != msg->msg_tx_cpt) {
1124 lnet_net_unlock(msg->msg_tx_cpt);
1125 lnet_net_lock(msg2_cpt);
1127 (void) lnet_post_send_locked(msg2, 1);
1128 if (msg2_cpt != msg->msg_tx_cpt) {
1129 lnet_net_unlock(msg2_cpt);
1130 lnet_net_lock(msg->msg_tx_cpt);
1133 spin_unlock(&txpeer->lpni_lock);
1138 msg->msg_txni = NULL;
1139 lnet_ni_decref_locked(txni, msg->msg_tx_cpt);
1142 if (txpeer != NULL) {
1143 msg->msg_txpeer = NULL;
1144 lnet_peer_ni_decref_locked(txpeer);
1149 lnet_schedule_blocked_locked(struct lnet_rtrbufpool *rbp)
1151 struct lnet_msg *msg;
1153 if (list_empty(&rbp->rbp_msgs))
1155 msg = list_entry(rbp->rbp_msgs.next,
1156 struct lnet_msg, msg_list);
1157 list_del(&msg->msg_list);
1159 (void)lnet_post_routed_recv_locked(msg, 1);
1163 lnet_drop_routed_msgs_locked(struct list_head *list, int cpt)
1165 struct lnet_msg *msg;
1166 struct lnet_msg *tmp;
1168 lnet_net_unlock(cpt);
1170 list_for_each_entry_safe(msg, tmp, list, msg_list) {
1171 lnet_ni_recv(msg->msg_rxni, msg->msg_private, NULL,
1172 0, 0, 0, msg->msg_hdr.payload_length);
1173 list_del_init(&msg->msg_list);
1174 msg->msg_no_resend = true;
1175 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_ERROR;
1176 lnet_finalize(msg, -ECANCELED);
1183 lnet_return_rx_credits_locked(struct lnet_msg *msg)
1185 struct lnet_peer_ni *rxpeerni = msg->msg_rxpeer;
1186 struct lnet_peer *lp;
1187 struct lnet_ni *rxni = msg->msg_rxni;
1188 struct lnet_msg *msg2;
1190 if (msg->msg_rtrcredit) {
1191 /* give back global router credits */
1192 struct lnet_rtrbuf *rb;
1193 struct lnet_rtrbufpool *rbp;
1195 /* NB If a msg ever blocks for a buffer in rbp_msgs, it stays
1196 * there until it gets one allocated, or aborts the wait
1198 LASSERT(msg->msg_kiov != NULL);
1200 rb = list_entry(msg->msg_kiov, struct lnet_rtrbuf, rb_kiov[0]);
1203 msg->msg_kiov = NULL;
1204 msg->msg_rtrcredit = 0;
1206 LASSERT(rbp == lnet_msg2bufpool(msg));
1208 LASSERT((rbp->rbp_credits > 0) ==
1209 !list_empty(&rbp->rbp_bufs));
1211 /* If routing is now turned off, we just drop this buffer and
1212 * don't bother trying to return credits. */
1213 if (!the_lnet.ln_routing) {
1214 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1218 /* It is possible that a user has lowered the desired number of
1219 * buffers in this pool. Make sure we never put back
1220 * more buffers than the stated number. */
1221 if (unlikely(rbp->rbp_credits >= rbp->rbp_req_nbuffers)) {
1222 /* Discard this buffer so we don't have too
1224 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1225 rbp->rbp_nbuffers--;
1227 list_add(&rb->rb_list, &rbp->rbp_bufs);
1229 if (rbp->rbp_credits <= 0)
1230 lnet_schedule_blocked_locked(rbp);
1235 if (msg->msg_peerrtrcredit) {
1237 LASSERT(rxpeerni->lpni_peer_net);
1238 LASSERT(rxpeerni->lpni_peer_net->lpn_peer);
1240 /* give back peer router credits */
1241 msg->msg_peerrtrcredit = 0;
1243 spin_lock(&rxpeerni->lpni_lock);
1244 rxpeerni->lpni_rtrcredits++;
1245 spin_unlock(&rxpeerni->lpni_lock);
1247 lp = rxpeerni->lpni_peer_net->lpn_peer;
1248 spin_lock(&lp->lp_lock);
1250 /* drop all messages which are queued to be routed on that
1252 if (!the_lnet.ln_routing) {
1254 list_splice_init(&lp->lp_rtrq, &drop);
1255 spin_unlock(&lp->lp_lock);
1256 lnet_drop_routed_msgs_locked(&drop, msg->msg_rx_cpt);
1257 } else if (!list_empty(&lp->lp_rtrq)) {
1260 msg2 = list_entry(lp->lp_rtrq.next,
1261 struct lnet_msg, msg_list);
1262 list_del(&msg2->msg_list);
1263 msg2_cpt = msg2->msg_rx_cpt;
1264 spin_unlock(&lp->lp_lock);
1266 * messages on the lp_rtrq can be from any NID in
1267 * the peer, which means they might have different
1268 * cpts. We need to make sure we lock the right
1271 if (msg2_cpt != msg->msg_rx_cpt) {
1272 lnet_net_unlock(msg->msg_rx_cpt);
1273 lnet_net_lock(msg2_cpt);
1275 (void) lnet_post_routed_recv_locked(msg2, 1);
1276 if (msg2_cpt != msg->msg_rx_cpt) {
1277 lnet_net_unlock(msg2_cpt);
1278 lnet_net_lock(msg->msg_rx_cpt);
1281 spin_unlock(&lp->lp_lock);
1285 msg->msg_rxni = NULL;
1286 lnet_ni_decref_locked(rxni, msg->msg_rx_cpt);
1288 if (rxpeerni != NULL) {
1289 msg->msg_rxpeer = NULL;
1290 lnet_peer_ni_decref_locked(rxpeerni);
1294 static struct lnet_peer_ni *
1295 lnet_select_peer_ni(struct lnet_ni *best_ni, lnet_nid_t dst_nid,
1296 struct lnet_peer *peer,
1297 struct lnet_peer_ni *best_lpni,
1298 struct lnet_peer_net *peer_net)
1301 * Look at the peer NIs for the destination peer that connect
1302 * to the chosen net. If a peer_ni is preferred when using the
1303 * best_ni to communicate, we use that one. If there is no
1304 * preferred peer_ni, or there are multiple preferred peer_ni,
1305 * the available transmit credits are used. If the transmit
1306 * credits are equal, we round-robin over the peer_ni.
1308 struct lnet_peer_ni *lpni = NULL;
1309 int best_lpni_credits = (best_lpni) ? best_lpni->lpni_txcredits :
1311 int best_lpni_healthv = (best_lpni) ?
1312 atomic_read(&best_lpni->lpni_healthv) : 0;
1313 bool best_lpni_is_preferred = false;
1314 bool lpni_is_preferred;
1316 __u32 lpni_sel_prio;
1317 __u32 best_sel_prio = LNET_MAX_SELECTION_PRIORITY;
1319 while ((lpni = lnet_get_next_peer_ni_locked(peer, peer_net, lpni))) {
1321 * if the best_ni we've chosen aleady has this lpni
1322 * preferred, then let's use it
1325 /* FIXME need to handle large-addr nid */
1326 lpni_is_preferred = lnet_peer_is_pref_nid_locked(
1327 lpni, lnet_nid_to_nid4(&best_ni->ni_nid));
1328 CDEBUG(D_NET, "%s lpni_is_preferred = %d\n",
1329 libcfs_nidstr(&best_ni->ni_nid),
1332 lpni_is_preferred = false;
1335 lpni_healthv = atomic_read(&lpni->lpni_healthv);
1336 lpni_sel_prio = lpni->lpni_sel_priority;
1339 CDEBUG(D_NET, "n:[%s, %s] h:[%d, %d] p:[%d, %d] c:[%d, %d] s:[%d, %d]\n",
1340 libcfs_nidstr(&lpni->lpni_nid),
1341 libcfs_nidstr(&best_lpni->lpni_nid),
1342 lpni_healthv, best_lpni_healthv,
1343 lpni_sel_prio, best_sel_prio,
1344 lpni->lpni_txcredits, best_lpni_credits,
1345 lpni->lpni_seq, best_lpni->lpni_seq);
1349 /* pick the healthiest peer ni */
1350 if (lpni_healthv < best_lpni_healthv)
1352 else if (lpni_healthv > best_lpni_healthv) {
1353 if (best_lpni_is_preferred)
1354 best_lpni_is_preferred = false;
1358 if (lpni_sel_prio > best_sel_prio)
1360 else if (lpni_sel_prio < best_sel_prio) {
1361 if (best_lpni_is_preferred)
1362 best_lpni_is_preferred = false;
1366 /* if this is a preferred peer use it */
1367 if (!best_lpni_is_preferred && lpni_is_preferred) {
1368 best_lpni_is_preferred = true;
1370 } else if (best_lpni_is_preferred && !lpni_is_preferred) {
1371 /* this is not the preferred peer so let's ignore
1377 if (lpni->lpni_txcredits < best_lpni_credits)
1378 /* We already have a peer that has more credits
1379 * available than this one. No need to consider
1380 * this peer further.
1383 else if (lpni->lpni_txcredits > best_lpni_credits)
1386 /* The best peer found so far and the current peer
1387 * have the same number of available credits let's
1388 * make sure to select between them using Round Robin
1390 if (best_lpni && (best_lpni->lpni_seq <= lpni->lpni_seq))
1393 best_lpni_is_preferred = lpni_is_preferred;
1394 best_lpni_healthv = lpni_healthv;
1395 best_sel_prio = lpni_sel_prio;
1397 best_lpni_credits = lpni->lpni_txcredits;
1400 /* if we still can't find a peer ni then we can't reach it */
1402 __u32 net_id = (peer_net) ? peer_net->lpn_net_id :
1403 LNET_NIDNET(dst_nid);
1404 CDEBUG(D_NET, "no peer_ni found on peer net %s\n",
1405 libcfs_net2str(net_id));
1409 CDEBUG(D_NET, "sd_best_lpni = %s\n",
1410 libcfs_nidstr(&best_lpni->lpni_nid));
1416 * Prerequisite: the best_ni should already be set in the sd
1417 * Find the best lpni.
1418 * If the net id is provided then restrict lpni selection on
1419 * that particular net.
1420 * Otherwise find any reachable lpni. When dealing with an MR
1421 * gateway and it has multiple lpnis which we can use
1422 * we want to select the best one from the list of reachable
1425 static inline struct lnet_peer_ni *
1426 lnet_find_best_lpni(struct lnet_ni *lni, lnet_nid_t dst_nid,
1427 struct lnet_peer *peer, __u32 net_id)
1429 struct lnet_peer_net *peer_net;
1431 /* find the best_lpni on any local network */
1432 if (net_id == LNET_NET_ANY) {
1433 struct lnet_peer_ni *best_lpni = NULL;
1434 struct lnet_peer_net *lpn;
1435 list_for_each_entry(lpn, &peer->lp_peer_nets, lpn_peer_nets) {
1436 /* no net specified find any reachable peer ni */
1437 if (!lnet_islocalnet_locked(lpn->lpn_net_id))
1439 best_lpni = lnet_select_peer_ni(lni, dst_nid, peer,
1445 /* restrict on the specified net */
1446 peer_net = lnet_peer_get_net_locked(peer, net_id);
1448 return lnet_select_peer_ni(lni, dst_nid, peer, NULL, peer_net);
1454 lnet_compare_gw_lpnis(struct lnet_peer_ni *lpni1, struct lnet_peer_ni *lpni2)
1456 if (lpni1->lpni_txqnob < lpni2->lpni_txqnob)
1459 if (lpni1->lpni_txqnob > lpni2->lpni_txqnob)
1462 if (lpni1->lpni_txcredits > lpni2->lpni_txcredits)
1465 if (lpni1->lpni_txcredits < lpni2->lpni_txcredits)
1471 /* Compare route priorities and hop counts */
1473 lnet_compare_routes(struct lnet_route *r1, struct lnet_route *r2)
1475 int r1_hops = (r1->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r1->lr_hops;
1476 int r2_hops = (r2->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r2->lr_hops;
1478 if (r1->lr_priority < r2->lr_priority)
1481 if (r1->lr_priority > r2->lr_priority)
1484 if (r1_hops < r2_hops)
1487 if (r1_hops > r2_hops)
1493 static struct lnet_route *
1494 lnet_find_route_locked(struct lnet_remotenet *rnet, __u32 src_net,
1495 struct lnet_peer_ni *remote_lpni,
1496 struct lnet_route **prev_route,
1497 struct lnet_peer_ni **gwni)
1499 struct lnet_peer_ni *lpni, *best_gw_ni = NULL;
1500 struct lnet_route *best_route;
1501 struct lnet_route *last_route;
1502 struct lnet_route *route;
1504 bool best_rte_is_preferred = false;
1507 CDEBUG(D_NET, "Looking up a route to %s, from %s\n",
1508 libcfs_net2str(rnet->lrn_net), libcfs_net2str(src_net));
1510 best_route = last_route = NULL;
1511 list_for_each_entry(route, &rnet->lrn_routes, lr_list) {
1512 if (!lnet_is_route_alive(route))
1514 gw_pnid = lnet_nid_to_nid4(&route->lr_gateway->lp_primary_nid);
1516 /* no protection on below fields, but it's harmless */
1517 if (last_route && (last_route->lr_seq - route->lr_seq < 0))
1520 /* if the best route found is in the preferred list then
1521 * tag it as preferred and use it later on. But if we
1522 * didn't find any routes which are on the preferred list
1523 * then just use the best route possible.
1525 rc = lnet_peer_is_pref_rtr_locked(remote_lpni, gw_pnid);
1527 if (!best_route || (rc && !best_rte_is_preferred)) {
1528 /* Restrict the selection of the router NI on the
1529 * src_net provided. If the src_net is LNET_NID_ANY,
1530 * then select the best interface available.
1532 lpni = lnet_find_best_lpni(NULL, LNET_NID_ANY,
1537 "Gateway %s does not have a peer NI on net %s\n",
1538 libcfs_nid2str(gw_pnid),
1539 libcfs_net2str(src_net));
1544 if (rc && !best_rte_is_preferred) {
1545 /* This is the first preferred route we found,
1546 * so it beats any route found previously
1552 best_rte_is_preferred = true;
1553 CDEBUG(D_NET, "preferred gw = %s\n",
1554 libcfs_nid2str(gw_pnid));
1556 } else if ((!rc) && best_rte_is_preferred)
1557 /* The best route we found so far is in the preferred
1558 * list, so it beats any non-preferred route
1563 best_route = last_route = route;
1568 rc = lnet_compare_routes(route, best_route);
1572 /* Restrict the selection of the router NI on the
1573 * src_net provided. If the src_net is LNET_NID_ANY,
1574 * then select the best interface available.
1576 lpni = lnet_find_best_lpni(NULL, LNET_NID_ANY,
1581 "Gateway %s does not have a peer NI on net %s\n",
1582 libcfs_nid2str(gw_pnid),
1583 libcfs_net2str(src_net));
1593 rc = lnet_compare_gw_lpnis(lpni, best_gw_ni);
1597 if (rc == 1 || route->lr_seq <= best_route->lr_seq) {
1604 *prev_route = last_route;
1610 static inline unsigned int
1611 lnet_dev_prio_of_md(struct lnet_ni *ni, unsigned int dev_idx)
1613 if (dev_idx == UINT_MAX)
1616 if (!ni || !ni->ni_net || !ni->ni_net->net_lnd ||
1617 !ni->ni_net->net_lnd->lnd_get_dev_prio)
1620 return ni->ni_net->net_lnd->lnd_get_dev_prio(ni, dev_idx);
1623 static struct lnet_ni *
1624 lnet_get_best_ni(struct lnet_net *local_net, struct lnet_ni *best_ni,
1625 struct lnet_peer *peer, struct lnet_peer_net *peer_net,
1626 struct lnet_msg *msg, int md_cpt)
1628 struct lnet_libmd *md = msg->msg_md;
1629 unsigned int offset = msg->msg_offset;
1630 unsigned int shortest_distance;
1631 struct lnet_ni *ni = NULL;
1634 __u32 best_sel_prio;
1635 unsigned int best_dev_prio;
1636 unsigned int dev_idx = UINT_MAX;
1637 struct page *page = lnet_get_first_page(md, offset);
1638 msg->msg_rdma_force = lnet_is_rdma_only_page(page);
1640 if (msg->msg_rdma_force)
1641 dev_idx = lnet_get_dev_idx(page);
1644 * If there is no peer_ni that we can send to on this network,
1645 * then there is no point in looking for a new best_ni here.
1647 if (!lnet_get_next_peer_ni_locked(peer, peer_net, NULL))
1650 if (best_ni == NULL) {
1651 best_sel_prio = LNET_MAX_SELECTION_PRIORITY;
1652 shortest_distance = UINT_MAX;
1653 best_dev_prio = UINT_MAX;
1654 best_credits = INT_MIN;
1657 best_dev_prio = lnet_dev_prio_of_md(best_ni, dev_idx);
1658 shortest_distance = cfs_cpt_distance(lnet_cpt_table(), md_cpt,
1659 best_ni->ni_dev_cpt);
1660 best_credits = atomic_read(&best_ni->ni_tx_credits);
1661 best_healthv = atomic_read(&best_ni->ni_healthv);
1662 best_sel_prio = best_ni->ni_sel_priority;
1665 while ((ni = lnet_get_next_ni_locked(local_net, ni))) {
1666 unsigned int distance;
1671 unsigned int ni_dev_prio;
1673 ni_credits = atomic_read(&ni->ni_tx_credits);
1674 ni_healthv = atomic_read(&ni->ni_healthv);
1675 ni_fatal = atomic_read(&ni->ni_fatal_error_on);
1676 ni_sel_prio = ni->ni_sel_priority;
1679 * calculate the distance from the CPT on which
1680 * the message memory is allocated to the CPT of
1681 * the NI's physical device
1683 distance = cfs_cpt_distance(lnet_cpt_table(),
1687 ni_dev_prio = lnet_dev_prio_of_md(ni, dev_idx);
1690 * All distances smaller than the NUMA range
1691 * are treated equally.
1693 if (distance < lnet_numa_range)
1694 distance = lnet_numa_range;
1697 * Select on health, selection policy, direct dma prio,
1698 * shorter distance, available credits, then round-robin.
1704 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",
1705 libcfs_nidstr(&ni->ni_nid), ni_credits, distance,
1706 ni->ni_seq, ni_sel_prio, ni_dev_prio,
1707 (best_ni) ? libcfs_nidstr(&best_ni->ni_nid)
1708 : "not selected", best_credits, shortest_distance,
1709 (best_ni) ? best_ni->ni_seq : 0,
1710 best_sel_prio, best_dev_prio);
1714 if (ni_healthv < best_healthv)
1716 else if (ni_healthv > best_healthv)
1719 if (ni_sel_prio > best_sel_prio)
1721 else if (ni_sel_prio < best_sel_prio)
1724 if (ni_dev_prio > best_dev_prio)
1726 else if (ni_dev_prio < best_dev_prio)
1729 if (distance > shortest_distance)
1731 else if (distance < shortest_distance)
1734 if (ni_credits < best_credits)
1736 else if (ni_credits > best_credits)
1739 if (best_ni && best_ni->ni_seq <= ni->ni_seq)
1743 best_sel_prio = ni_sel_prio;
1744 best_dev_prio = ni_dev_prio;
1745 shortest_distance = distance;
1746 best_healthv = ni_healthv;
1748 best_credits = ni_credits;
1751 CDEBUG(D_NET, "selected best_ni %s\n",
1752 (best_ni) ? libcfs_nidstr(&best_ni->ni_nid) : "no selection");
1758 * Traffic to the LNET_RESERVED_PORTAL may not trigger peer discovery,
1759 * because such traffic is required to perform discovery. We therefore
1760 * exclude all GET and PUT on that portal. We also exclude all ACK and
1761 * REPLY traffic, but that is because the portal is not tracked in the
1762 * message structure for these message types. We could restrict this
1763 * further by also checking for LNET_PROTO_PING_MATCHBITS.
1766 lnet_msg_discovery(struct lnet_msg *msg)
1768 if (msg->msg_type == LNET_MSG_PUT) {
1769 if (msg->msg_hdr.msg.put.ptl_index != LNET_RESERVED_PORTAL)
1771 } else if (msg->msg_type == LNET_MSG_GET) {
1772 if (msg->msg_hdr.msg.get.ptl_index != LNET_RESERVED_PORTAL)
1778 #define SRC_SPEC 0x0001
1779 #define SRC_ANY 0x0002
1780 #define LOCAL_DST 0x0004
1781 #define REMOTE_DST 0x0008
1782 #define MR_DST 0x0010
1783 #define NMR_DST 0x0020
1784 #define SND_RESP 0x0040
1786 /* The following to defines are used for return codes */
1787 #define REPEAT_SEND 0x1000
1788 #define PASS_THROUGH 0x2000
1790 /* The different cases lnet_select pathway needs to handle */
1791 #define SRC_SPEC_LOCAL_MR_DST (SRC_SPEC | LOCAL_DST | MR_DST)
1792 #define SRC_SPEC_ROUTER_MR_DST (SRC_SPEC | REMOTE_DST | MR_DST)
1793 #define SRC_SPEC_LOCAL_NMR_DST (SRC_SPEC | LOCAL_DST | NMR_DST)
1794 #define SRC_SPEC_ROUTER_NMR_DST (SRC_SPEC | REMOTE_DST | NMR_DST)
1795 #define SRC_ANY_LOCAL_MR_DST (SRC_ANY | LOCAL_DST | MR_DST)
1796 #define SRC_ANY_ROUTER_MR_DST (SRC_ANY | REMOTE_DST | MR_DST)
1797 #define SRC_ANY_LOCAL_NMR_DST (SRC_ANY | LOCAL_DST | NMR_DST)
1798 #define SRC_ANY_ROUTER_NMR_DST (SRC_ANY | REMOTE_DST | NMR_DST)
1801 lnet_handle_lo_send(struct lnet_send_data *sd)
1803 struct lnet_msg *msg = sd->sd_msg;
1804 int cpt = sd->sd_cpt;
1806 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1809 /* No send credit hassles with LOLND */
1810 lnet_ni_addref_locked(the_lnet.ln_loni, cpt);
1811 msg->msg_hdr.dest_nid =
1812 cpu_to_le64(lnet_nid_to_nid4(&the_lnet.ln_loni->ni_nid));
1813 if (!msg->msg_routing)
1814 msg->msg_hdr.src_nid =
1815 cpu_to_le64(lnet_nid_to_nid4(&the_lnet.ln_loni->ni_nid));
1816 msg->msg_target.nid = lnet_nid_to_nid4(&the_lnet.ln_loni->ni_nid);
1817 lnet_msg_commit(msg, cpt);
1818 msg->msg_txni = the_lnet.ln_loni;
1820 return LNET_CREDIT_OK;
1824 lnet_handle_send(struct lnet_send_data *sd)
1826 struct lnet_ni *best_ni = sd->sd_best_ni;
1827 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
1828 struct lnet_peer_ni *final_dst_lpni = sd->sd_final_dst_lpni;
1829 struct lnet_msg *msg = sd->sd_msg;
1831 __u32 send_case = sd->sd_send_case;
1833 __u32 routing = send_case & REMOTE_DST;
1834 struct lnet_rsp_tracker *rspt;
1836 /* Increment sequence number of the selected peer, peer net,
1837 * local ni and local net so that we pick the next ones
1840 best_lpni->lpni_seq++;
1841 best_lpni->lpni_peer_net->lpn_seq++;
1843 best_ni->ni_net->net_seq++;
1845 CDEBUG(D_NET, "%s NI seq info: [%d:%d:%d:%u] %s LPNI seq info [%d:%d:%d:%u]\n",
1846 libcfs_nidstr(&best_ni->ni_nid),
1847 best_ni->ni_seq, best_ni->ni_net->net_seq,
1848 atomic_read(&best_ni->ni_tx_credits),
1849 best_ni->ni_sel_priority,
1850 libcfs_nidstr(&best_lpni->lpni_nid),
1851 best_lpni->lpni_seq, best_lpni->lpni_peer_net->lpn_seq,
1852 best_lpni->lpni_txcredits,
1853 best_lpni->lpni_sel_priority);
1856 * grab a reference on the peer_ni so it sticks around even if
1857 * we need to drop and relock the lnet_net_lock below.
1859 lnet_peer_ni_addref_locked(best_lpni);
1862 * Use lnet_cpt_of_nid() to determine the CPT used to commit the
1863 * message. This ensures that we get a CPT that is correct for
1864 * the NI when the NI has been restricted to a subset of all CPTs.
1865 * If the selected CPT differs from the one currently locked, we
1866 * must unlock and relock the lnet_net_lock(), and then check whether
1867 * the configuration has changed. We don't have a hold on the best_ni
1868 * yet, and it may have vanished.
1870 cpt2 = lnet_cpt_of_nid_locked(&best_lpni->lpni_nid, best_ni);
1871 if (sd->sd_cpt != cpt2) {
1872 __u32 seq = lnet_get_dlc_seq_locked();
1873 lnet_net_unlock(sd->sd_cpt);
1875 lnet_net_lock(sd->sd_cpt);
1876 if (seq != lnet_get_dlc_seq_locked()) {
1877 lnet_peer_ni_decref_locked(best_lpni);
1883 * store the best_lpni in the message right away to avoid having
1884 * to do the same operation under different conditions
1886 msg->msg_txpeer = best_lpni;
1887 msg->msg_txni = best_ni;
1890 * grab a reference for the best_ni since now it's in use in this
1891 * send. The reference will be dropped in lnet_finalize()
1893 lnet_ni_addref_locked(msg->msg_txni, sd->sd_cpt);
1896 * Always set the target.nid to the best peer picked. Either the
1897 * NID will be one of the peer NIDs selected, or the same NID as
1898 * what was originally set in the target or it will be the NID of
1899 * a router if this message should be routed
1901 /* FIXME handle large-addr nids */
1902 msg->msg_target.nid = lnet_nid_to_nid4(&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 /* FIXME handle large-addr nid */
1934 msg->msg_hdr.dest_nid =
1935 cpu_to_le64(lnet_nid_to_nid4(&final_dst_lpni->lpni_nid));
1938 * if we're not routing set the dest_nid to the best peer
1939 * ni NID that we picked earlier in the algorithm.
1941 msg->msg_hdr.dest_nid =
1942 cpu_to_le64(lnet_nid_to_nid4(&msg->msg_txpeer->lpni_nid));
1946 * if we have response tracker block update it with the next hop
1950 rspt = msg->msg_md->md_rspt_ptr;
1952 rspt->rspt_next_hop_nid =
1953 lnet_nid_to_nid4(&msg->msg_txpeer->lpni_nid);
1954 CDEBUG(D_NET, "rspt_next_hop_nid = %s\n",
1955 libcfs_nid2str(rspt->rspt_next_hop_nid));
1959 rc = lnet_post_send_locked(msg, 0);
1962 CDEBUG(D_NET, "TRACE: %s(%s:%s) -> %s(%s:%s) %s : %s try# %d\n",
1963 libcfs_nid2str(msg->msg_hdr.src_nid),
1964 libcfs_nidstr(&msg->msg_txni->ni_nid),
1965 libcfs_nid2str(sd->sd_src_nid),
1966 libcfs_nid2str(msg->msg_hdr.dest_nid),
1967 libcfs_nid2str(sd->sd_dst_nid),
1968 libcfs_nidstr(&msg->msg_txpeer->lpni_nid),
1969 libcfs_nid2str(sd->sd_rtr_nid),
1970 lnet_msgtyp2str(msg->msg_type), msg->msg_retry_count);
1976 lnet_set_non_mr_pref_nid(struct lnet_peer_ni *lpni, struct lnet_ni *lni,
1977 struct lnet_msg *msg)
1979 if (!lnet_peer_is_multi_rail(lpni->lpni_peer_net->lpn_peer) &&
1980 !lnet_msg_is_response(msg) && lpni->lpni_pref_nnids == 0) {
1981 CDEBUG(D_NET, "Setting preferred local NID %s on NMR peer %s\n",
1982 libcfs_nidstr(&lni->ni_nid),
1983 libcfs_nidstr(&lpni->lpni_nid));
1984 lnet_peer_ni_set_non_mr_pref_nid(
1985 lpni, lnet_nid_to_nid4(&lni->ni_nid));
1994 * use the source and destination NIDs as the pathway
1997 lnet_handle_spec_local_nmr_dst(struct lnet_send_data *sd)
1999 /* the destination lpni is set before we get here. */
2002 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
2003 if (!sd->sd_best_ni) {
2004 CERROR("Can't send to %s: src %s is not a "
2005 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
2006 libcfs_nid2str(sd->sd_src_nid));
2010 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
2012 return lnet_handle_send(sd);
2020 * Don't run the selection algorithm on the peer NIs. By specifying the
2021 * local NID, we're also saying that we should always use the destination NID
2022 * provided. This handles the case where we should be using the same
2023 * destination NID for the all the messages which belong to the same RPC
2027 lnet_handle_spec_local_mr_dst(struct lnet_send_data *sd)
2029 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
2030 if (!sd->sd_best_ni) {
2031 CERROR("Can't send to %s: src %s is not a "
2032 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
2033 libcfs_nid2str(sd->sd_src_nid));
2037 if (sd->sd_best_lpni &&
2038 nid_same(&sd->sd_best_lpni->lpni_nid,
2039 &the_lnet.ln_loni->ni_nid))
2040 return lnet_handle_lo_send(sd);
2041 else if (sd->sd_best_lpni)
2042 return lnet_handle_send(sd);
2044 CERROR("can't send to %s. no NI on %s\n",
2045 libcfs_nid2str(sd->sd_dst_nid),
2046 libcfs_net2str(sd->sd_best_ni->ni_net->net_id));
2048 return -EHOSTUNREACH;
2052 lnet_find_best_ni_on_spec_net(struct lnet_ni *cur_best_ni,
2053 struct lnet_peer *peer,
2054 struct lnet_peer_net *peer_net,
2055 struct lnet_msg *msg,
2058 struct lnet_net *local_net;
2059 struct lnet_ni *best_ni;
2061 local_net = lnet_get_net_locked(peer_net->lpn_net_id);
2066 * Iterate through the NIs in this local Net and select
2067 * the NI to send from. The selection is determined by
2068 * these 3 criterion in the following priority:
2070 * 2. NI available credits
2073 best_ni = lnet_get_best_ni(local_net, cur_best_ni,
2074 peer, peer_net, msg, cpt);
2080 lnet_initiate_peer_discovery(struct lnet_peer_ni *lpni, struct lnet_msg *msg,
2083 struct lnet_peer *peer;
2084 struct lnet_peer_ni *new_lpni;
2087 lnet_peer_ni_addref_locked(lpni);
2089 peer = lpni->lpni_peer_net->lpn_peer;
2091 if (lnet_peer_gw_discovery(peer)) {
2092 lnet_peer_ni_decref_locked(lpni);
2096 if (!lnet_msg_discovery(msg) || lnet_peer_is_uptodate(peer)) {
2097 lnet_peer_ni_decref_locked(lpni);
2101 rc = lnet_discover_peer_locked(lpni, cpt, false);
2103 lnet_peer_ni_decref_locked(lpni);
2107 new_lpni = lnet_find_peer_ni_locked(lnet_nid_to_nid4(&lpni->lpni_nid));
2109 lnet_peer_ni_decref_locked(lpni);
2113 peer = new_lpni->lpni_peer_net->lpn_peer;
2114 spin_lock(&peer->lp_lock);
2115 if (lpni == new_lpni && lnet_peer_is_uptodate_locked(peer)) {
2116 /* The peer NI did not change and the peer is up to date.
2117 * Nothing more to do.
2119 spin_unlock(&peer->lp_lock);
2120 lnet_peer_ni_decref_locked(lpni);
2121 lnet_peer_ni_decref_locked(new_lpni);
2124 spin_unlock(&peer->lp_lock);
2126 /* Either the peer NI changed during discovery, or the peer isn't up
2127 * to date. In both cases we want to queue the message on the
2128 * (possibly new) peer's pending queue and queue the peer for discovery
2130 msg->msg_sending = 0;
2131 msg->msg_txpeer = NULL;
2132 lnet_net_unlock(cpt);
2133 lnet_peer_queue_message(peer, msg);
2136 lnet_peer_ni_decref_locked(lpni);
2137 lnet_peer_ni_decref_locked(new_lpni);
2139 CDEBUG(D_NET, "msg %p delayed. %s pending discovery\n",
2140 msg, libcfs_nidstr(&peer->lp_primary_nid));
2142 return LNET_DC_WAIT;
2146 lnet_handle_find_routed_path(struct lnet_send_data *sd,
2148 struct lnet_peer_ni **gw_lpni,
2149 struct lnet_peer **gw_peer)
2152 struct lnet_peer *gw;
2153 struct lnet_peer *lp;
2154 struct lnet_peer_net *lpn;
2155 struct lnet_peer_net *best_lpn = NULL;
2156 struct lnet_remotenet *rnet, *best_rnet = NULL;
2157 struct lnet_route *best_route = NULL;
2158 struct lnet_route *last_route = NULL;
2159 struct lnet_peer_ni *lpni = NULL;
2160 struct lnet_peer_ni *gwni = NULL;
2161 bool route_found = false;
2162 lnet_nid_t src_nid = (sd->sd_src_nid != LNET_NID_ANY) ? sd->sd_src_nid :
2163 (sd->sd_best_ni != NULL)
2164 ? lnet_nid_to_nid4(&sd->sd_best_ni->ni_nid)
2166 int best_lpn_healthv = 0;
2167 __u32 best_lpn_sel_prio = LNET_MAX_SELECTION_PRIORITY;
2169 CDEBUG(D_NET, "using src nid %s for route restriction\n",
2170 libcfs_nid2str(src_nid));
2172 /* If a router nid was specified then we are replying to a GET or
2173 * sending an ACK. In this case we use the gateway associated with the
2174 * specified router nid.
2176 if (sd->sd_rtr_nid != LNET_NID_ANY) {
2177 gwni = lnet_find_peer_ni_locked(sd->sd_rtr_nid);
2179 gw = gwni->lpni_peer_net->lpn_peer;
2180 lnet_peer_ni_decref_locked(gwni);
2181 if (gw->lp_rtr_refcount)
2184 CWARN("No peer NI for gateway %s. Attempting to find an alternative route.\n",
2185 libcfs_nid2str(sd->sd_rtr_nid));
2190 if (sd->sd_msg->msg_routing) {
2191 /* If I'm routing this message then I need to find the
2192 * next hop based on the destination NID
2194 best_rnet = lnet_find_rnet_locked(LNET_NIDNET(sd->sd_dst_nid));
2196 CERROR("Unable to route message to %s - Route table may be misconfigured\n",
2197 libcfs_nid2str(sd->sd_dst_nid));
2198 return -EHOSTUNREACH;
2201 /* we've already looked up the initial lpni using
2204 lpni = sd->sd_best_lpni;
2205 /* the peer tree must be in existence */
2206 LASSERT(lpni && lpni->lpni_peer_net &&
2207 lpni->lpni_peer_net->lpn_peer);
2208 lp = lpni->lpni_peer_net->lpn_peer;
2210 list_for_each_entry(lpn, &lp->lp_peer_nets, lpn_peer_nets) {
2211 /* is this remote network reachable? */
2212 rnet = lnet_find_rnet_locked(lpn->lpn_net_id);
2221 /* select the preferred peer net */
2222 if (best_lpn_healthv > lpn->lpn_healthv)
2224 else if (best_lpn_healthv < lpn->lpn_healthv)
2227 if (best_lpn_sel_prio < lpn->lpn_sel_priority)
2229 else if (best_lpn_sel_prio > lpn->lpn_sel_priority)
2232 if (best_lpn->lpn_seq <= lpn->lpn_seq)
2235 best_lpn_healthv = lpn->lpn_healthv;
2236 best_lpn_sel_prio = lpn->lpn_sel_priority;
2242 CERROR("peer %s has no available nets\n",
2243 libcfs_nid2str(sd->sd_dst_nid));
2244 return -EHOSTUNREACH;
2247 sd->sd_best_lpni = lnet_find_best_lpni(sd->sd_best_ni,
2250 best_lpn->lpn_net_id);
2251 if (!sd->sd_best_lpni) {
2252 CERROR("peer %s is unreachable\n",
2253 libcfs_nid2str(sd->sd_dst_nid));
2254 return -EHOSTUNREACH;
2257 /* We're attempting to round robin over the remote peer
2258 * NI's so update the final destination we selected
2260 sd->sd_final_dst_lpni = sd->sd_best_lpni;
2262 /* Increment the sequence number of the remote lpni so
2263 * we can round robin over the different interfaces of
2266 sd->sd_best_lpni->lpni_seq++;
2270 * find the best route. Restrict the selection on the net of the
2271 * local NI if we've already picked the local NI to send from.
2272 * Otherwise, let's pick any route we can find and then find
2273 * a local NI we can reach the route's gateway on. Any route we
2274 * select will be reachable by virtue of the restriction we have
2275 * when adding a route.
2277 best_route = lnet_find_route_locked(best_rnet,
2278 LNET_NIDNET(src_nid),
2280 &last_route, &gwni);
2283 CERROR("no route to %s from %s\n",
2284 libcfs_nid2str(dst_nid),
2285 libcfs_nid2str(src_nid));
2286 return -EHOSTUNREACH;
2290 CERROR("Internal Error. Route expected to %s from %s\n",
2291 libcfs_nid2str(dst_nid),
2292 libcfs_nid2str(src_nid));
2296 gw = best_route->lr_gateway;
2297 LASSERT(gw == gwni->lpni_peer_net->lpn_peer);
2301 * Discover this gateway if it hasn't already been discovered.
2302 * This means we might delay the message until discovery has
2305 rc = lnet_initiate_peer_discovery(gwni, sd->sd_msg, sd->sd_cpt);
2309 if (!sd->sd_best_ni) {
2310 lpn = gwni->lpni_peer_net;
2311 sd->sd_best_ni = lnet_find_best_ni_on_spec_net(NULL, gw, lpn,
2314 if (!sd->sd_best_ni) {
2315 CERROR("Internal Error. Expected local ni on %s but non found: %s\n",
2316 libcfs_net2str(lpn->lpn_net_id),
2317 libcfs_nid2str(sd->sd_src_nid));
2326 * increment the sequence numbers since now we're sure we're
2327 * going to use this path
2329 if (sd->sd_rtr_nid == LNET_NID_ANY) {
2330 LASSERT(best_route && last_route);
2331 best_route->lr_seq = last_route->lr_seq + 1;
2333 best_lpn->lpn_seq++;
2344 * Remote destination
2345 * Non-MR destination
2349 * Remote destination
2352 * The handling of these two cases is similar. Even though the destination
2353 * can be MR or non-MR, we'll deal directly with the router.
2356 lnet_handle_spec_router_dst(struct lnet_send_data *sd)
2359 struct lnet_peer_ni *gw_lpni = NULL;
2360 struct lnet_peer *gw_peer = NULL;
2363 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
2364 if (!sd->sd_best_ni) {
2365 CERROR("Can't send to %s: src %s is not a "
2366 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
2367 libcfs_nid2str(sd->sd_src_nid));
2371 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2376 if (sd->sd_send_case & NMR_DST)
2378 * since the final destination is non-MR let's set its preferred
2379 * NID before we send
2381 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni,
2385 * We're going to send to the gw found so let's set its
2388 sd->sd_peer = gw_peer;
2389 sd->sd_best_lpni = gw_lpni;
2391 return lnet_handle_send(sd);
2395 lnet_find_best_ni_on_local_net(struct lnet_peer *peer, int md_cpt,
2396 struct lnet_msg *msg, bool discovery)
2398 struct lnet_peer_net *lpn = NULL;
2399 struct lnet_peer_net *best_lpn = NULL;
2400 struct lnet_net *net = NULL;
2401 struct lnet_net *best_net = NULL;
2402 struct lnet_ni *best_ni = NULL;
2403 int best_lpn_healthv = 0;
2404 int best_net_healthv = 0;
2406 __u32 best_lpn_sel_prio = LNET_MAX_SELECTION_PRIORITY;
2408 __u32 best_net_sel_prio = LNET_MAX_SELECTION_PRIORITY;
2413 * The peer can have multiple interfaces, some of them can be on
2414 * the local network and others on a routed network. We should
2415 * prefer the local network. However if the local network is not
2416 * available then we need to try the routed network
2419 /* go through all the peer nets and find the best_ni */
2420 list_for_each_entry(lpn, &peer->lp_peer_nets, lpn_peer_nets) {
2422 * The peer's list of nets can contain non-local nets. We
2423 * want to only examine the local ones.
2425 net = lnet_get_net_locked(lpn->lpn_net_id);
2429 lpn_sel_prio = lpn->lpn_sel_priority;
2430 net_healthv = lnet_get_net_healthv_locked(net);
2431 net_sel_prio = net->net_sel_priority;
2434 * if this is a discovery message and lp_disc_net_id is
2435 * specified then use that net to send the discovery on.
2437 if (peer->lp_disc_net_id == lpn->lpn_net_id &&
2446 /* always select the lpn with the best health */
2447 if (best_lpn_healthv > lpn->lpn_healthv)
2449 else if (best_lpn_healthv < lpn->lpn_healthv)
2452 /* select the preferred peer and local nets */
2453 if (best_lpn_sel_prio < lpn_sel_prio)
2455 else if (best_lpn_sel_prio > lpn_sel_prio)
2458 if (best_net_healthv > net_healthv)
2460 else if (best_net_healthv < net_healthv)
2463 if (best_net_sel_prio < net_sel_prio)
2465 else if (best_net_sel_prio > net_sel_prio)
2468 if (best_lpn->lpn_seq < lpn->lpn_seq)
2470 else if (best_lpn->lpn_seq > lpn->lpn_seq)
2473 /* round robin over the local networks */
2474 if (best_net->net_seq <= net->net_seq)
2478 best_net_healthv = net_healthv;
2479 best_net_sel_prio = net_sel_prio;
2480 best_lpn_healthv = lpn->lpn_healthv;
2481 best_lpn_sel_prio = lpn_sel_prio;
2490 /* Select the best NI on the same net as best_lpn chosen
2493 best_ni = lnet_find_best_ni_on_spec_net(NULL, peer, best_lpn,
2500 static struct lnet_ni *
2501 lnet_find_existing_preferred_best_ni(struct lnet_peer_ni *lpni, int cpt)
2503 struct lnet_ni *best_ni = NULL;
2504 struct lnet_peer_net *peer_net = lpni->lpni_peer_net;
2505 struct lnet_peer_ni *lpni_entry;
2508 * We must use a consistent source address when sending to a
2509 * non-MR peer. However, a non-MR peer can have multiple NIDs
2510 * on multiple networks, and we may even need to talk to this
2511 * peer on multiple networks -- certain types of
2512 * load-balancing configuration do this.
2514 * So we need to pick the NI the peer prefers for this
2515 * particular network.
2518 list_for_each_entry(lpni_entry, &peer_net->lpn_peer_nis,
2520 if (lpni_entry->lpni_pref_nnids == 0)
2522 LASSERT(lpni_entry->lpni_pref_nnids == 1);
2523 best_ni = lnet_nid2ni_locked(lpni_entry->lpni_pref.nid, cpt);
2530 /* Prerequisite: sd->sd_peer and sd->sd_best_lpni should be set */
2532 lnet_select_preferred_best_ni(struct lnet_send_data *sd)
2534 struct lnet_ni *best_ni = NULL;
2535 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
2538 * We must use a consistent source address when sending to a
2539 * non-MR peer. However, a non-MR peer can have multiple NIDs
2540 * on multiple networks, and we may even need to talk to this
2541 * peer on multiple networks -- certain types of
2542 * load-balancing configuration do this.
2544 * So we need to pick the NI the peer prefers for this
2545 * particular network.
2548 best_ni = lnet_find_existing_preferred_best_ni(sd->sd_best_lpni,
2551 /* if best_ni is still not set just pick one */
2554 lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2555 sd->sd_best_lpni->lpni_peer_net,
2558 /* If there is no best_ni we don't have a route */
2560 CERROR("no path to %s from net %s\n",
2561 libcfs_nidstr(&best_lpni->lpni_nid),
2562 libcfs_net2str(best_lpni->lpni_net->net_id));
2563 return -EHOSTUNREACH;
2567 sd->sd_best_ni = best_ni;
2569 /* Set preferred NI if necessary. */
2570 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
2577 * Source not specified
2581 * always use the same source NID for NMR peers
2582 * If we've talked to that peer before then we already have a preferred
2583 * source NI associated with it. Otherwise, we select a preferred local NI
2584 * and store it in the peer
2587 lnet_handle_any_local_nmr_dst(struct lnet_send_data *sd)
2591 /* sd->sd_best_lpni is already set to the final destination */
2594 * At this point we should've created the peer ni and peer. If we
2595 * can't find it, then something went wrong. Instead of assert
2596 * output a relevant message and fail the send
2598 if (!sd->sd_best_lpni) {
2599 CERROR("Internal fault. Unable to send msg %s to %s. "
2601 lnet_msgtyp2str(sd->sd_msg->msg_type),
2602 libcfs_nid2str(sd->sd_dst_nid));
2606 if (sd->sd_msg->msg_routing) {
2607 /* If I'm forwarding this message then I can choose any NI
2608 * on the destination peer net
2610 sd->sd_best_ni = lnet_find_best_ni_on_spec_net(NULL,
2612 sd->sd_best_lpni->lpni_peer_net,
2615 if (!sd->sd_best_ni) {
2616 CERROR("Unable to forward message to %s. No local NI available\n",
2617 libcfs_nid2str(sd->sd_dst_nid));
2621 rc = lnet_select_preferred_best_ni(sd);
2624 rc = lnet_handle_send(sd);
2630 lnet_handle_any_mr_dsta(struct lnet_send_data *sd)
2633 * NOTE we've already handled the remote peer case. So we only
2634 * need to worry about the local case here.
2636 * if we're sending a response, ACK or reply, we need to send it
2637 * to the destination NID given to us. At this point we already
2638 * have the peer_ni we're suppose to send to, so just find the
2639 * best_ni on the peer net and use that. Since we're sending to an
2640 * MR peer then we can just run the selection algorithm on our
2641 * local NIs and pick the best one.
2643 if (sd->sd_send_case & SND_RESP) {
2645 lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2646 sd->sd_best_lpni->lpni_peer_net,
2650 if (!sd->sd_best_ni) {
2652 * We're not going to deal with not able to send
2653 * a response to the provided final destination
2655 CERROR("Can't send response to %s. "
2656 "No local NI available\n",
2657 libcfs_nid2str(sd->sd_dst_nid));
2658 return -EHOSTUNREACH;
2661 return lnet_handle_send(sd);
2665 * If we get here that means we're sending a fresh request, PUT or
2666 * GET, so we need to run our standard selection algorithm.
2667 * First find the best local interface that's on any of the peer's
2670 sd->sd_best_ni = lnet_find_best_ni_on_local_net(sd->sd_peer,
2673 lnet_msg_discovery(sd->sd_msg));
2674 if (sd->sd_best_ni) {
2676 lnet_find_best_lpni(sd->sd_best_ni, sd->sd_dst_nid,
2678 sd->sd_best_ni->ni_net->net_id);
2681 * if we're successful in selecting a peer_ni on the local
2682 * network, then send to it. Otherwise fall through and
2683 * try and see if we can reach it over another routed
2686 if (sd->sd_best_lpni &&
2687 nid_same(&sd->sd_best_lpni->lpni_nid,
2688 &the_lnet.ln_loni->ni_nid)) {
2690 * in case we initially started with a routed
2691 * destination, let's reset to local
2693 sd->sd_send_case &= ~REMOTE_DST;
2694 sd->sd_send_case |= LOCAL_DST;
2695 return lnet_handle_lo_send(sd);
2696 } else if (sd->sd_best_lpni) {
2698 * in case we initially started with a routed
2699 * destination, let's reset to local
2701 sd->sd_send_case &= ~REMOTE_DST;
2702 sd->sd_send_case |= LOCAL_DST;
2703 return lnet_handle_send(sd);
2706 CERROR("Internal Error. Expected to have a best_lpni: "
2708 libcfs_nid2str(sd->sd_src_nid),
2709 libcfs_nid2str(sd->sd_dst_nid));
2715 * Peer doesn't have a local network. Let's see if there is
2716 * a remote network we can reach it on.
2718 return PASS_THROUGH;
2723 * Source NID not specified
2728 * Source NID not speified
2729 * Remote destination
2732 * In both of these cases if we're sending a response, ACK or REPLY, then
2733 * we need to send to the destination NID provided.
2735 * In the remote case let's deal with MR routers.
2740 lnet_handle_any_mr_dst(struct lnet_send_data *sd)
2743 struct lnet_peer *gw_peer = NULL;
2744 struct lnet_peer_ni *gw_lpni = NULL;
2747 * handle sending a response to a remote peer here so we don't
2748 * have to worry about it if we hit lnet_handle_any_mr_dsta()
2750 if (sd->sd_send_case & REMOTE_DST &&
2751 sd->sd_send_case & SND_RESP) {
2752 struct lnet_peer_ni *gw;
2753 struct lnet_peer *gw_peer;
2755 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw,
2758 CERROR("Can't send response to %s. "
2759 "No route available\n",
2760 libcfs_nid2str(sd->sd_dst_nid));
2761 return -EHOSTUNREACH;
2762 } else if (rc > 0) {
2766 sd->sd_best_lpni = gw;
2767 sd->sd_peer = gw_peer;
2769 return lnet_handle_send(sd);
2773 * Even though the NID for the peer might not be on a local network,
2774 * since the peer is MR there could be other interfaces on the
2775 * local network. In that case we'd still like to prefer the local
2776 * network over the routed network. If we're unable to do that
2777 * then we select the best router among the different routed networks,
2778 * and if the router is MR then we can deal with it as such.
2780 rc = lnet_handle_any_mr_dsta(sd);
2781 if (rc != PASS_THROUGH)
2785 * Now that we must route to the destination, we must consider the
2786 * MR case, where the destination has multiple interfaces, some of
2787 * which we can route to and others we do not. For this reason we
2788 * need to select the destination which we can route to and if
2789 * there are multiple, we need to round robin.
2791 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2796 sd->sd_send_case &= ~LOCAL_DST;
2797 sd->sd_send_case |= REMOTE_DST;
2799 sd->sd_peer = gw_peer;
2800 sd->sd_best_lpni = gw_lpni;
2802 return lnet_handle_send(sd);
2806 * Source not specified
2807 * Remote destination
2810 * Must send to the specified peer NID using the same source NID that
2811 * we've used before. If it's the first time to talk to that peer then
2812 * find the source NI and assign it as preferred to that peer
2815 lnet_handle_any_router_nmr_dst(struct lnet_send_data *sd)
2818 struct lnet_peer_ni *gw_lpni = NULL;
2819 struct lnet_peer *gw_peer = NULL;
2822 * Let's see if we have a preferred NI to talk to this NMR peer
2824 sd->sd_best_ni = lnet_find_existing_preferred_best_ni(sd->sd_best_lpni,
2828 * find the router and that'll find the best NI if we didn't find
2831 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2837 * set the best_ni we've chosen as the preferred one for
2840 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
2842 /* we'll be sending to the gw */
2843 sd->sd_best_lpni = gw_lpni;
2844 sd->sd_peer = gw_peer;
2846 return lnet_handle_send(sd);
2850 lnet_handle_send_case_locked(struct lnet_send_data *sd)
2853 * turn off the SND_RESP bit.
2854 * It will be checked in the case handling
2856 __u32 send_case = sd->sd_send_case &= ~SND_RESP ;
2858 CDEBUG(D_NET, "Source %s%s to %s %s %s destination\n",
2859 (send_case & SRC_SPEC) ? "Specified: " : "ANY",
2860 (send_case & SRC_SPEC) ? libcfs_nid2str(sd->sd_src_nid) : "",
2861 (send_case & MR_DST) ? "MR: " : "NMR: ",
2862 libcfs_nid2str(sd->sd_dst_nid),
2863 (send_case & LOCAL_DST) ? "local" : "routed");
2865 switch (send_case) {
2867 * For all cases where the source is specified, we should always
2868 * use the destination NID, whether it's an MR destination or not,
2869 * since we're continuing a series of related messages for the
2872 case SRC_SPEC_LOCAL_NMR_DST:
2873 return lnet_handle_spec_local_nmr_dst(sd);
2874 case SRC_SPEC_LOCAL_MR_DST:
2875 return lnet_handle_spec_local_mr_dst(sd);
2876 case SRC_SPEC_ROUTER_NMR_DST:
2877 case SRC_SPEC_ROUTER_MR_DST:
2878 return lnet_handle_spec_router_dst(sd);
2879 case SRC_ANY_LOCAL_NMR_DST:
2880 return lnet_handle_any_local_nmr_dst(sd);
2881 case SRC_ANY_LOCAL_MR_DST:
2882 case SRC_ANY_ROUTER_MR_DST:
2883 return lnet_handle_any_mr_dst(sd);
2884 case SRC_ANY_ROUTER_NMR_DST:
2885 return lnet_handle_any_router_nmr_dst(sd);
2887 CERROR("Unknown send case\n");
2893 lnet_select_pathway(lnet_nid_t src_nid, lnet_nid_t dst_nid,
2894 struct lnet_msg *msg, lnet_nid_t rtr_nid)
2896 struct lnet_peer_ni *lpni;
2897 struct lnet_peer *peer;
2898 struct lnet_send_data send_data;
2901 __u32 send_case = 0;
2903 bool mr_forwarding_allowed;
2905 memset(&send_data, 0, sizeof(send_data));
2908 * get an initial CPT to use for locking. The idea here is not to
2909 * serialize the calls to select_pathway, so that as many
2910 * operations can run concurrently as possible. To do that we use
2911 * the CPT where this call is being executed. Later on when we
2912 * determine the CPT to use in lnet_message_commit, we switch the
2913 * lock and check if there was any configuration change. If none,
2914 * then we proceed, if there is, then we restart the operation.
2916 cpt = lnet_net_lock_current();
2918 md_cpt = lnet_cpt_of_md(msg->msg_md, msg->msg_offset);
2919 if (md_cpt == CFS_CPT_ANY)
2925 * If we're being asked to send to the loopback interface, there
2926 * is no need to go through any selection. We can just shortcut
2927 * the entire process and send over lolnd
2929 send_data.sd_msg = msg;
2930 send_data.sd_cpt = cpt;
2931 if (dst_nid == LNET_NID_LO_0) {
2932 rc = lnet_handle_lo_send(&send_data);
2933 lnet_net_unlock(cpt);
2938 * find an existing peer_ni, or create one and mark it as having been
2939 * created due to network traffic. This call will create the
2940 * peer->peer_net->peer_ni tree.
2942 lpni = lnet_nid2peerni_locked(dst_nid, LNET_NID_ANY, cpt);
2944 lnet_net_unlock(cpt);
2945 return PTR_ERR(lpni);
2949 * Cache the original src_nid and rtr_nid. If we need to resend the
2950 * message then we'll need to know whether the src_nid was originally
2951 * specified for this message. If it was originally specified,
2952 * then we need to keep using the same src_nid since it's
2953 * continuing the same sequence of messages. Similarly, rtr_nid will
2954 * affect our choice of next hop.
2956 msg->msg_src_nid_param = src_nid;
2957 msg->msg_rtr_nid_param = rtr_nid;
2960 * If necessary, perform discovery on the peer that owns this peer_ni.
2961 * Note, this can result in the ownership of this peer_ni changing
2962 * to another peer object.
2964 rc = lnet_initiate_peer_discovery(lpni, msg, cpt);
2966 lnet_peer_ni_decref_locked(lpni);
2967 lnet_net_unlock(cpt);
2970 lnet_peer_ni_decref_locked(lpni);
2972 peer = lpni->lpni_peer_net->lpn_peer;
2975 * Identify the different send cases
2977 if (src_nid == LNET_NID_ANY)
2978 send_case |= SRC_ANY;
2980 send_case |= SRC_SPEC;
2982 if (lnet_get_net_locked(LNET_NIDNET(dst_nid)))
2983 send_case |= LOCAL_DST;
2985 send_case |= REMOTE_DST;
2988 if (msg->msg_routing && (send_case & LOCAL_DST))
2991 /* Determine whether to allow MR forwarding for this message.
2992 * NB: MR forwarding is allowed if the message originator and the
2993 * destination are both MR capable, and the destination lpni that was
2994 * originally chosen by the originator is unhealthy or down.
2995 * We check the MR capability of the destination further below
2997 mr_forwarding_allowed = false;
2999 struct lnet_peer *src_lp;
3000 struct lnet_peer_ni *src_lpni;
3002 src_lpni = lnet_nid2peerni_locked(msg->msg_hdr.src_nid,
3004 /* We don't fail the send if we hit any errors here. We'll just
3005 * try to send it via non-multi-rail criteria
3007 if (!IS_ERR(src_lpni)) {
3008 /* Drop ref taken by lnet_nid2peerni_locked() */
3009 lnet_peer_ni_decref_locked(src_lpni);
3010 src_lp = lpni->lpni_peer_net->lpn_peer;
3011 if (lnet_peer_is_multi_rail(src_lp) &&
3012 !lnet_is_peer_ni_alive(lpni))
3013 mr_forwarding_allowed = true;
3016 CDEBUG(D_NET, "msg %p MR forwarding %s\n", msg,
3017 mr_forwarding_allowed ? "allowed" : "not allowed");
3021 * Deal with the peer as NMR in the following cases:
3022 * 1. the peer is NMR
3023 * 2. We're trying to recover a specific peer NI
3024 * 3. I'm a router sending to the final destination and MR forwarding is
3025 * not allowed for this message (as determined above).
3026 * In this case the source of the message would've
3027 * already selected the final destination so my job
3028 * is to honor the selection.
3030 if (!lnet_peer_is_multi_rail(peer) || msg->msg_recovery ||
3031 (final_hop && !mr_forwarding_allowed))
3032 send_case |= NMR_DST;
3034 send_case |= MR_DST;
3036 if (lnet_msg_is_response(msg))
3037 send_case |= SND_RESP;
3039 /* assign parameters to the send_data */
3040 send_data.sd_rtr_nid = rtr_nid;
3041 send_data.sd_src_nid = src_nid;
3042 send_data.sd_dst_nid = dst_nid;
3043 send_data.sd_best_lpni = lpni;
3045 * keep a pointer to the final destination in case we're going to
3046 * route, so we'll need to access it later
3048 send_data.sd_final_dst_lpni = lpni;
3049 send_data.sd_peer = peer;
3050 send_data.sd_md_cpt = md_cpt;
3051 send_data.sd_send_case = send_case;
3053 rc = lnet_handle_send_case_locked(&send_data);
3056 * Update the local cpt since send_data.sd_cpt might've been
3057 * updated as a result of calling lnet_handle_send_case_locked().
3059 cpt = send_data.sd_cpt;
3061 if (rc == REPEAT_SEND)
3064 lnet_net_unlock(cpt);
3070 lnet_send(lnet_nid_t src_nid, struct lnet_msg *msg, lnet_nid_t rtr_nid)
3072 lnet_nid_t dst_nid = msg->msg_target.nid;
3076 * NB: rtr_nid is set to LNET_NID_ANY for all current use-cases,
3077 * but we might want to use pre-determined router for ACK/REPLY
3080 /* NB: ni != NULL == interface pre-determined (ACK/REPLY) */
3081 LASSERT(msg->msg_txpeer == NULL);
3082 LASSERT(msg->msg_txni == NULL);
3083 LASSERT(!msg->msg_sending);
3084 LASSERT(!msg->msg_target_is_router);
3085 LASSERT(!msg->msg_receiving);
3087 msg->msg_sending = 1;
3089 LASSERT(!msg->msg_tx_committed);
3091 rc = lnet_select_pathway(src_nid, dst_nid, msg, rtr_nid);
3093 if (rc == -EHOSTUNREACH)
3094 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_ERROR;
3096 msg->msg_health_status = LNET_MSG_STATUS_LOCAL_ERROR;
3100 if (rc == LNET_CREDIT_OK)
3101 lnet_ni_send(msg->msg_txni, msg);
3103 /* rc == LNET_CREDIT_OK or LNET_CREDIT_WAIT or LNET_DC_WAIT */
3107 enum lnet_mt_event_type {
3108 MT_TYPE_LOCAL_NI = 0,
3112 struct lnet_mt_event_info {
3113 enum lnet_mt_event_type mt_type;
3117 /* called with res_lock held */
3119 lnet_detach_rsp_tracker(struct lnet_libmd *md, int cpt)
3121 struct lnet_rsp_tracker *rspt;
3124 * msg has a refcount on the MD so the MD is not going away.
3125 * The rspt queue for the cpt is protected by
3126 * the lnet_net_lock(cpt). cpt is the cpt of the MD cookie.
3128 if (!md->md_rspt_ptr)
3131 rspt = md->md_rspt_ptr;
3134 LASSERT(rspt->rspt_cpt == cpt);
3136 md->md_rspt_ptr = NULL;
3138 if (LNetMDHandleIsInvalid(rspt->rspt_mdh)) {
3140 * The monitor thread has invalidated this handle because the
3141 * response timed out, but it failed to lookup the MD. That
3142 * means this response tracker is on the zombie list. We can
3143 * safely remove it under the resource lock (held by caller) and
3144 * free the response tracker block.
3146 list_del(&rspt->rspt_on_list);
3147 lnet_rspt_free(rspt, cpt);
3150 * invalidate the handle to indicate that a response has been
3151 * received, which will then lead the monitor thread to clean up
3154 LNetInvalidateMDHandle(&rspt->rspt_mdh);
3159 lnet_clean_zombie_rstqs(void)
3161 struct lnet_rsp_tracker *rspt, *tmp;
3164 cfs_cpt_for_each(i, lnet_cpt_table()) {
3165 list_for_each_entry_safe(rspt, tmp,
3166 the_lnet.ln_mt_zombie_rstqs[i],
3168 list_del(&rspt->rspt_on_list);
3169 lnet_rspt_free(rspt, i);
3173 cfs_percpt_free(the_lnet.ln_mt_zombie_rstqs);
3177 lnet_finalize_expired_responses(void)
3179 struct lnet_libmd *md;
3180 struct lnet_rsp_tracker *rspt, *tmp;
3184 if (the_lnet.ln_mt_rstq == NULL)
3187 cfs_cpt_for_each(i, lnet_cpt_table()) {
3188 LIST_HEAD(local_queue);
3191 if (!the_lnet.ln_mt_rstq[i]) {
3195 list_splice_init(the_lnet.ln_mt_rstq[i], &local_queue);
3200 list_for_each_entry_safe(rspt, tmp, &local_queue, rspt_on_list) {
3202 * The rspt mdh will be invalidated when a response
3203 * is received or whenever we want to discard the
3204 * block the monitor thread will walk the queue
3205 * and clean up any rsts with an invalid mdh.
3206 * The monitor thread will walk the queue until
3207 * the first unexpired rspt block. This means that
3208 * some rspt blocks which received their
3209 * corresponding responses will linger in the
3210 * queue until they are cleaned up eventually.
3213 if (LNetMDHandleIsInvalid(rspt->rspt_mdh)) {
3215 list_del(&rspt->rspt_on_list);
3216 lnet_rspt_free(rspt, i);
3220 if (ktime_compare(now, rspt->rspt_deadline) >= 0 ||
3221 the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN) {
3222 struct lnet_peer_ni *lpni;