4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lnet/lnet/lib-move.c
34 * Data movement routines
37 #define DEBUG_SUBSYSTEM S_LNET
39 #include <linux/pagemap.h>
41 #include <lnet/lib-lnet.h>
42 #include <linux/nsproxy.h>
43 #include <net/net_namespace.h>
45 static int local_nid_dist_zero = 1;
46 module_param(local_nid_dist_zero, int, 0444);
47 MODULE_PARM_DESC(local_nid_dist_zero, "Reserved");
49 struct lnet_send_data {
50 struct lnet_ni *sd_best_ni;
51 struct lnet_peer_ni *sd_best_lpni;
52 struct lnet_peer_ni *sd_final_dst_lpni;
53 struct lnet_peer *sd_peer;
54 struct lnet_peer *sd_gw_peer;
55 struct lnet_peer_ni *sd_gw_lpni;
56 struct lnet_peer_net *sd_peer_net;
57 struct lnet_msg *sd_msg;
58 lnet_nid_t sd_dst_nid;
59 lnet_nid_t sd_src_nid;
60 lnet_nid_t sd_rtr_nid;
67 lnet_msg_is_response(struct lnet_msg *msg)
69 return msg->msg_type == LNET_MSG_ACK || msg->msg_type == LNET_MSG_REPLY;
72 static inline struct lnet_comm_count *
73 get_stats_counts(struct lnet_element_stats *stats,
74 enum lnet_stats_type stats_type)
77 case LNET_STATS_TYPE_SEND:
78 return &stats->el_send_stats;
79 case LNET_STATS_TYPE_RECV:
80 return &stats->el_recv_stats;
81 case LNET_STATS_TYPE_DROP:
82 return &stats->el_drop_stats;
84 CERROR("Unknown stats type\n");
90 void lnet_incr_stats(struct lnet_element_stats *stats,
91 enum lnet_msg_type msg_type,
92 enum lnet_stats_type stats_type)
94 struct lnet_comm_count *counts = get_stats_counts(stats, stats_type);
100 atomic_inc(&counts->co_ack_count);
103 atomic_inc(&counts->co_put_count);
106 atomic_inc(&counts->co_get_count);
109 atomic_inc(&counts->co_reply_count);
112 atomic_inc(&counts->co_hello_count);
115 CERROR("There is a BUG in the code. Unknown message type\n");
120 __u32 lnet_sum_stats(struct lnet_element_stats *stats,
121 enum lnet_stats_type stats_type)
123 struct lnet_comm_count *counts = get_stats_counts(stats, stats_type);
127 return (atomic_read(&counts->co_ack_count) +
128 atomic_read(&counts->co_put_count) +
129 atomic_read(&counts->co_get_count) +
130 atomic_read(&counts->co_reply_count) +
131 atomic_read(&counts->co_hello_count));
134 static inline void assign_stats(struct lnet_ioctl_comm_count *msg_stats,
135 struct lnet_comm_count *counts)
137 msg_stats->ico_get_count = atomic_read(&counts->co_get_count);
138 msg_stats->ico_put_count = atomic_read(&counts->co_put_count);
139 msg_stats->ico_reply_count = atomic_read(&counts->co_reply_count);
140 msg_stats->ico_ack_count = atomic_read(&counts->co_ack_count);
141 msg_stats->ico_hello_count = atomic_read(&counts->co_hello_count);
144 void lnet_usr_translate_stats(struct lnet_ioctl_element_msg_stats *msg_stats,
145 struct lnet_element_stats *stats)
147 struct lnet_comm_count *counts;
152 counts = get_stats_counts(stats, LNET_STATS_TYPE_SEND);
155 assign_stats(&msg_stats->im_send_stats, counts);
157 counts = get_stats_counts(stats, LNET_STATS_TYPE_RECV);
160 assign_stats(&msg_stats->im_recv_stats, counts);
162 counts = get_stats_counts(stats, LNET_STATS_TYPE_DROP);
165 assign_stats(&msg_stats->im_drop_stats, counts);
169 lnet_fail_nid(lnet_nid_t nid, unsigned int threshold)
171 struct lnet_test_peer *tp;
172 struct list_head *el;
173 struct list_head *next;
176 /* NB: use lnet_net_lock(0) to serialize operations on test peers */
177 if (threshold != 0) {
178 /* Adding a new entry */
179 LIBCFS_ALLOC(tp, sizeof(*tp));
184 tp->tp_threshold = threshold;
187 list_add_tail(&tp->tp_list, &the_lnet.ln_test_peers);
194 list_for_each_safe(el, next, &the_lnet.ln_test_peers) {
195 tp = list_entry(el, struct lnet_test_peer, tp_list);
197 if (tp->tp_threshold == 0 || /* needs culling anyway */
198 nid == LNET_NID_ANY || /* removing all entries */
199 tp->tp_nid == nid) { /* matched this one */
200 list_move(&tp->tp_list, &cull);
206 while (!list_empty(&cull)) {
207 tp = list_entry(cull.next, struct lnet_test_peer, tp_list);
209 list_del(&tp->tp_list);
210 LIBCFS_FREE(tp, sizeof(*tp));
216 fail_peer (lnet_nid_t nid, int outgoing)
218 struct lnet_test_peer *tp;
219 struct list_head *el;
220 struct list_head *next;
224 /* NB: use lnet_net_lock(0) to serialize operations on test peers */
227 list_for_each_safe(el, next, &the_lnet.ln_test_peers) {
228 tp = list_entry(el, struct lnet_test_peer, tp_list);
230 if (tp->tp_threshold == 0) {
233 /* only cull zombies on outgoing tests,
234 * since we may be at interrupt priority on
235 * incoming messages. */
236 list_move(&tp->tp_list, &cull);
241 if (tp->tp_nid == LNET_NID_ANY || /* fail every peer */
242 nid == tp->tp_nid) { /* fail this peer */
245 if (tp->tp_threshold != LNET_MD_THRESH_INF) {
248 tp->tp_threshold == 0) {
250 list_move(&tp->tp_list, &cull);
259 while (!list_empty(&cull)) {
260 tp = list_entry(cull.next, struct lnet_test_peer, tp_list);
261 list_del(&tp->tp_list);
263 LIBCFS_FREE(tp, sizeof(*tp));
270 lnet_iov_nob(unsigned int niov, struct kvec *iov)
272 unsigned int nob = 0;
274 LASSERT(niov == 0 || iov != NULL);
276 nob += (iov++)->iov_len;
280 EXPORT_SYMBOL(lnet_iov_nob);
283 lnet_copy_iov2iov(unsigned int ndiov, struct kvec *diov, unsigned int doffset,
284 unsigned int nsiov, struct kvec *siov, unsigned int soffset,
287 /* NB diov, siov are READ-ONLY */
288 unsigned int this_nob;
293 /* skip complete frags before 'doffset' */
295 while (doffset >= diov->iov_len) {
296 doffset -= diov->iov_len;
302 /* skip complete frags before 'soffset' */
304 while (soffset >= siov->iov_len) {
305 soffset -= siov->iov_len;
314 this_nob = min3((unsigned int)diov->iov_len - doffset,
315 (unsigned int)siov->iov_len - soffset,
318 memcpy((char *)diov->iov_base + doffset,
319 (char *)siov->iov_base + soffset, this_nob);
322 if (diov->iov_len > doffset + this_nob) {
330 if (siov->iov_len > soffset + this_nob) {
339 EXPORT_SYMBOL(lnet_copy_iov2iov);
342 lnet_kiov_nob(unsigned int niov, struct bio_vec *kiov)
344 unsigned int nob = 0;
346 LASSERT(niov == 0 || kiov != NULL);
348 nob += (kiov++)->bv_len;
352 EXPORT_SYMBOL(lnet_kiov_nob);
355 lnet_copy_kiov2kiov(unsigned int ndiov, struct bio_vec *diov,
356 unsigned int doffset,
357 unsigned int nsiov, struct bio_vec *siov,
358 unsigned int soffset,
361 /* NB diov, siov are READ-ONLY */
362 unsigned int this_nob;
369 LASSERT (!in_interrupt ());
372 while (doffset >= diov->bv_len) {
373 doffset -= diov->bv_len;
380 while (soffset >= siov->bv_len) {
381 soffset -= siov->bv_len;
390 this_nob = min3(diov->bv_len - doffset,
391 siov->bv_len - soffset,
395 daddr = ((char *)kmap(diov->bv_page)) +
396 diov->bv_offset + doffset;
398 saddr = ((char *)kmap(siov->bv_page)) +
399 siov->bv_offset + soffset;
401 /* Vanishing risk of kmap deadlock when mapping 2 pages.
402 * However in practice at least one of the kiovs will be mapped
403 * kernel pages and the map/unmap will be NOOPs */
405 memcpy (daddr, saddr, this_nob);
408 if (diov->bv_len > doffset + this_nob) {
412 kunmap(diov->bv_page);
419 if (siov->bv_len > soffset + this_nob) {
423 kunmap(siov->bv_page);
432 kunmap(diov->bv_page);
434 kunmap(siov->bv_page);
436 EXPORT_SYMBOL(lnet_copy_kiov2kiov);
439 lnet_copy_kiov2iov (unsigned int niov, struct kvec *iov, unsigned int iovoffset,
440 unsigned int nkiov, struct bio_vec *kiov,
441 unsigned int kiovoffset,
444 /* NB iov, kiov are READ-ONLY */
445 unsigned int this_nob;
451 LASSERT (!in_interrupt ());
454 while (iovoffset >= iov->iov_len) {
455 iovoffset -= iov->iov_len;
462 while (kiovoffset >= kiov->bv_len) {
463 kiovoffset -= kiov->bv_len;
472 this_nob = min3((unsigned int)iov->iov_len - iovoffset,
473 (unsigned int)kiov->bv_len - kiovoffset,
477 addr = ((char *)kmap(kiov->bv_page)) +
478 kiov->bv_offset + kiovoffset;
480 memcpy((char *)iov->iov_base + iovoffset, addr, this_nob);
483 if (iov->iov_len > iovoffset + this_nob) {
484 iovoffset += this_nob;
491 if (kiov->bv_len > kiovoffset + this_nob) {
493 kiovoffset += this_nob;
495 kunmap(kiov->bv_page);
505 kunmap(kiov->bv_page);
507 EXPORT_SYMBOL(lnet_copy_kiov2iov);
510 lnet_copy_iov2kiov(unsigned int nkiov, struct bio_vec *kiov,
511 unsigned int kiovoffset,
512 unsigned int niov, struct kvec *iov, unsigned int iovoffset,
515 /* NB kiov, iov are READ-ONLY */
516 unsigned int this_nob;
522 LASSERT (!in_interrupt ());
525 while (kiovoffset >= kiov->bv_len) {
526 kiovoffset -= kiov->bv_len;
533 while (iovoffset >= iov->iov_len) {
534 iovoffset -= iov->iov_len;
543 this_nob = min3((unsigned int)kiov->bv_len - kiovoffset,
544 (unsigned int)iov->iov_len - iovoffset,
548 addr = ((char *)kmap(kiov->bv_page)) +
549 kiov->bv_offset + kiovoffset;
551 memcpy (addr, (char *)iov->iov_base + iovoffset, this_nob);
554 if (kiov->bv_len > kiovoffset + this_nob) {
556 kiovoffset += this_nob;
558 kunmap(kiov->bv_page);
565 if (iov->iov_len > iovoffset + this_nob) {
566 iovoffset += this_nob;
575 kunmap(kiov->bv_page);
577 EXPORT_SYMBOL(lnet_copy_iov2kiov);
580 lnet_extract_kiov(int dst_niov, struct bio_vec *dst,
581 int src_niov, struct bio_vec *src,
582 unsigned int offset, unsigned int len)
584 /* Initialise 'dst' to the subset of 'src' starting at 'offset',
585 * for exactly 'len' bytes, and return the number of entries.
586 * NB not destructive to 'src' */
587 unsigned int frag_len;
590 if (len == 0) /* no data => */
591 return (0); /* no frags */
593 LASSERT(src_niov > 0);
594 while (offset >= src->bv_len) { /* skip initial frags */
595 offset -= src->bv_len;
598 LASSERT(src_niov > 0);
603 LASSERT(src_niov > 0);
604 LASSERT((int)niov <= dst_niov);
606 frag_len = src->bv_len - offset;
607 dst->bv_page = src->bv_page;
608 dst->bv_offset = src->bv_offset + offset;
610 if (len <= frag_len) {
612 LASSERT(dst->bv_offset + dst->bv_len <= PAGE_SIZE);
616 dst->bv_len = frag_len;
617 LASSERT(dst->bv_offset + dst->bv_len <= PAGE_SIZE);
627 EXPORT_SYMBOL(lnet_extract_kiov);
630 lnet_ni_recv(struct lnet_ni *ni, void *private, struct lnet_msg *msg,
631 int delayed, unsigned int offset, unsigned int mlen,
634 unsigned int niov = 0;
635 struct kvec *iov = NULL;
636 struct bio_vec *kiov = NULL;
639 LASSERT (!in_interrupt ());
640 LASSERT (mlen == 0 || msg != NULL);
643 LASSERT(msg->msg_receiving);
644 LASSERT(!msg->msg_sending);
645 LASSERT(rlen == msg->msg_len);
646 LASSERT(mlen <= msg->msg_len);
647 LASSERT(msg->msg_offset == offset);
648 LASSERT(msg->msg_wanted == mlen);
650 msg->msg_receiving = 0;
653 niov = msg->msg_niov;
654 kiov = msg->msg_kiov;
657 LASSERT ((iov == NULL) != (kiov == NULL));
661 rc = (ni->ni_net->net_lnd->lnd_recv)(ni, private, msg, delayed,
662 niov, kiov, offset, mlen,
665 lnet_finalize(msg, rc);
669 lnet_setpayloadbuffer(struct lnet_msg *msg)
671 struct lnet_libmd *md = msg->msg_md;
673 LASSERT(msg->msg_len > 0);
674 LASSERT(!msg->msg_routing);
676 LASSERT(msg->msg_niov == 0);
677 LASSERT(msg->msg_kiov == NULL);
679 msg->msg_niov = md->md_niov;
680 msg->msg_kiov = md->md_kiov;
684 lnet_prep_send(struct lnet_msg *msg, int type, struct lnet_process_id target,
685 unsigned int offset, unsigned int len)
687 msg->msg_type = type;
688 msg->msg_target = target;
690 msg->msg_offset = offset;
693 lnet_setpayloadbuffer(msg);
695 memset (&msg->msg_hdr, 0, sizeof (msg->msg_hdr));
696 msg->msg_hdr.type = cpu_to_le32(type);
697 /* dest_nid will be overwritten by lnet_select_pathway() */
698 msg->msg_hdr.dest_nid = cpu_to_le64(target.nid);
699 msg->msg_hdr.dest_pid = cpu_to_le32(target.pid);
700 /* src_nid will be set later */
701 msg->msg_hdr.src_pid = cpu_to_le32(the_lnet.ln_pid);
702 msg->msg_hdr.payload_length = cpu_to_le32(len);
706 lnet_ni_send(struct lnet_ni *ni, struct lnet_msg *msg)
708 void *priv = msg->msg_private;
711 LASSERT(!in_interrupt());
712 LASSERT(ni->ni_nid == LNET_NID_LO_0 ||
713 (msg->msg_txcredit && msg->msg_peertxcredit));
715 rc = (ni->ni_net->net_lnd->lnd_send)(ni, priv, msg);
717 msg->msg_no_resend = true;
718 lnet_finalize(msg, rc);
723 lnet_ni_eager_recv(struct lnet_ni *ni, struct lnet_msg *msg)
727 LASSERT(!msg->msg_sending);
728 LASSERT(msg->msg_receiving);
729 LASSERT(!msg->msg_rx_ready_delay);
730 LASSERT(ni->ni_net->net_lnd->lnd_eager_recv != NULL);
732 msg->msg_rx_ready_delay = 1;
733 rc = (ni->ni_net->net_lnd->lnd_eager_recv)(ni, msg->msg_private, msg,
736 CERROR("recv from %s / send to %s aborted: "
737 "eager_recv failed %d\n",
738 libcfs_nid2str(msg->msg_rxpeer->lpni_nid),
739 libcfs_id2str(msg->msg_target), rc);
740 LASSERT(rc < 0); /* required by my callers */
747 lnet_is_peer_deadline_passed(struct lnet_peer_ni *lpni, time64_t now)
751 deadline = lpni->lpni_last_alive +
752 lpni->lpni_net->net_tunables.lct_peer_timeout;
755 * assume peer_ni is alive as long as we're within the configured
764 /* NB: returns 1 when alive, 0 when dead, negative when error;
765 * may drop the lnet_net_lock */
767 lnet_peer_alive_locked(struct lnet_ni *ni, struct lnet_peer_ni *lpni,
768 struct lnet_msg *msg)
770 time64_t now = ktime_get_seconds();
772 if (!lnet_peer_aliveness_enabled(lpni))
776 * If we're resending a message, let's attempt to send it even if
777 * the peer is down to fulfill our resend quota on the message
779 if (msg->msg_retry_count > 0)
782 /* try and send recovery messages irregardless */
783 if (msg->msg_recovery)
786 /* always send any responses */
787 if (lnet_msg_is_response(msg))
790 if (!lnet_is_peer_deadline_passed(lpni, now))
793 return lnet_is_peer_ni_alive(lpni);
797 * \param msg The message to be sent.
798 * \param do_send True if lnet_ni_send() should be called in this function.
799 * lnet_send() is going to lnet_net_unlock immediately after this, so
800 * it sets do_send FALSE and I don't do the unlock/send/lock bit.
802 * \retval LNET_CREDIT_OK If \a msg sent or OK to send.
803 * \retval LNET_CREDIT_WAIT If \a msg blocked for credit.
804 * \retval -EHOSTUNREACH If the next hop of the message appears dead.
805 * \retval -ECANCELED If the MD of the message has been unlinked.
808 lnet_post_send_locked(struct lnet_msg *msg, int do_send)
810 struct lnet_peer_ni *lp = msg->msg_txpeer;
811 struct lnet_ni *ni = msg->msg_txni;
812 int cpt = msg->msg_tx_cpt;
813 struct lnet_tx_queue *tq = ni->ni_tx_queues[cpt];
815 /* non-lnet_send() callers have checked before */
816 LASSERT(!do_send || msg->msg_tx_delayed);
817 LASSERT(!msg->msg_receiving);
818 LASSERT(msg->msg_tx_committed);
819 /* can't get here if we're sending to the loopback interface */
820 LASSERT(lp->lpni_nid != the_lnet.ln_loni->ni_nid);
822 /* NB 'lp' is always the next hop */
823 if ((msg->msg_target.pid & LNET_PID_USERFLAG) == 0 &&
824 lnet_peer_alive_locked(ni, lp, msg) == 0) {
825 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
826 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length +=
828 lnet_net_unlock(cpt);
830 lnet_incr_stats(&msg->msg_txpeer->lpni_stats,
832 LNET_STATS_TYPE_DROP);
834 lnet_incr_stats(&msg->msg_txni->ni_stats,
836 LNET_STATS_TYPE_DROP);
838 CNETERR("Dropping message for %s: peer not alive\n",
839 libcfs_id2str(msg->msg_target));
840 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_DROPPED;
842 lnet_finalize(msg, -EHOSTUNREACH);
845 return -EHOSTUNREACH;
848 if (msg->msg_md != NULL &&
849 (msg->msg_md->md_flags & LNET_MD_FLAG_ABORTED) != 0) {
850 lnet_net_unlock(cpt);
852 CNETERR("Aborting message for %s: LNetM[DE]Unlink() already "
853 "called on the MD/ME.\n",
854 libcfs_id2str(msg->msg_target));
856 msg->msg_no_resend = true;
857 CDEBUG(D_NET, "msg %p to %s canceled and will not be resent\n",
858 msg, libcfs_id2str(msg->msg_target));
859 lnet_finalize(msg, -ECANCELED);
866 if (!msg->msg_peertxcredit) {
867 spin_lock(&lp->lpni_lock);
868 LASSERT((lp->lpni_txcredits < 0) ==
869 !list_empty(&lp->lpni_txq));
871 msg->msg_peertxcredit = 1;
872 lp->lpni_txqnob += msg->msg_len + sizeof(struct lnet_hdr);
873 lp->lpni_txcredits--;
875 if (lp->lpni_txcredits < lp->lpni_mintxcredits)
876 lp->lpni_mintxcredits = lp->lpni_txcredits;
878 if (lp->lpni_txcredits < 0) {
879 msg->msg_tx_delayed = 1;
880 list_add_tail(&msg->msg_list, &lp->lpni_txq);
881 spin_unlock(&lp->lpni_lock);
882 return LNET_CREDIT_WAIT;
884 spin_unlock(&lp->lpni_lock);
887 if (!msg->msg_txcredit) {
888 LASSERT((tq->tq_credits < 0) ==
889 !list_empty(&tq->tq_delayed));
891 msg->msg_txcredit = 1;
893 atomic_dec(&ni->ni_tx_credits);
895 if (tq->tq_credits < tq->tq_credits_min)
896 tq->tq_credits_min = tq->tq_credits;
898 if (tq->tq_credits < 0) {
899 msg->msg_tx_delayed = 1;
900 list_add_tail(&msg->msg_list, &tq->tq_delayed);
901 return LNET_CREDIT_WAIT;
905 /* unset the tx_delay flag as we're going to send it now */
906 msg->msg_tx_delayed = 0;
909 lnet_net_unlock(cpt);
910 lnet_ni_send(ni, msg);
913 return LNET_CREDIT_OK;
917 static struct lnet_rtrbufpool *
918 lnet_msg2bufpool(struct lnet_msg *msg)
920 struct lnet_rtrbufpool *rbp;
923 LASSERT(msg->msg_rx_committed);
925 cpt = msg->msg_rx_cpt;
926 rbp = &the_lnet.ln_rtrpools[cpt][0];
928 LASSERT(msg->msg_len <= LNET_MTU);
929 while (msg->msg_len > (unsigned int)rbp->rbp_npages * PAGE_SIZE) {
931 LASSERT(rbp < &the_lnet.ln_rtrpools[cpt][LNET_NRBPOOLS]);
938 lnet_post_routed_recv_locked(struct lnet_msg *msg, int do_recv)
940 /* lnet_parse is going to lnet_net_unlock immediately after this, so it
941 * sets do_recv FALSE and I don't do the unlock/send/lock bit.
942 * I return LNET_CREDIT_WAIT if msg blocked and LNET_CREDIT_OK if
943 * received or OK to receive */
944 struct lnet_peer_ni *lpni = msg->msg_rxpeer;
945 struct lnet_peer *lp;
946 struct lnet_rtrbufpool *rbp;
947 struct lnet_rtrbuf *rb;
949 LASSERT(msg->msg_kiov == NULL);
950 LASSERT(msg->msg_niov == 0);
951 LASSERT(msg->msg_routing);
952 LASSERT(msg->msg_receiving);
953 LASSERT(!msg->msg_sending);
954 LASSERT(lpni->lpni_peer_net);
955 LASSERT(lpni->lpni_peer_net->lpn_peer);
957 lp = lpni->lpni_peer_net->lpn_peer;
959 /* non-lnet_parse callers only receive delayed messages */
960 LASSERT(!do_recv || msg->msg_rx_delayed);
962 if (!msg->msg_peerrtrcredit) {
963 /* lpni_lock protects the credit manipulation */
964 spin_lock(&lpni->lpni_lock);
966 msg->msg_peerrtrcredit = 1;
967 lpni->lpni_rtrcredits--;
968 if (lpni->lpni_rtrcredits < lpni->lpni_minrtrcredits)
969 lpni->lpni_minrtrcredits = lpni->lpni_rtrcredits;
971 if (lpni->lpni_rtrcredits < 0) {
972 spin_unlock(&lpni->lpni_lock);
973 /* must have checked eager_recv before here */
974 LASSERT(msg->msg_rx_ready_delay);
975 msg->msg_rx_delayed = 1;
976 /* lp_lock protects the lp_rtrq */
977 spin_lock(&lp->lp_lock);
978 list_add_tail(&msg->msg_list, &lp->lp_rtrq);
979 spin_unlock(&lp->lp_lock);
980 return LNET_CREDIT_WAIT;
982 spin_unlock(&lpni->lpni_lock);
985 rbp = lnet_msg2bufpool(msg);
987 if (!msg->msg_rtrcredit) {
988 msg->msg_rtrcredit = 1;
990 if (rbp->rbp_credits < rbp->rbp_mincredits)
991 rbp->rbp_mincredits = rbp->rbp_credits;
993 if (rbp->rbp_credits < 0) {
994 /* must have checked eager_recv before here */
995 LASSERT(msg->msg_rx_ready_delay);
996 msg->msg_rx_delayed = 1;
997 list_add_tail(&msg->msg_list, &rbp->rbp_msgs);
998 return LNET_CREDIT_WAIT;
1002 LASSERT(!list_empty(&rbp->rbp_bufs));
1003 rb = list_entry(rbp->rbp_bufs.next, struct lnet_rtrbuf, rb_list);
1004 list_del(&rb->rb_list);
1006 msg->msg_niov = rbp->rbp_npages;
1007 msg->msg_kiov = &rb->rb_kiov[0];
1009 /* unset the msg-rx_delayed flag since we're receiving the message */
1010 msg->msg_rx_delayed = 0;
1013 int cpt = msg->msg_rx_cpt;
1015 lnet_net_unlock(cpt);
1016 lnet_ni_recv(msg->msg_rxni, msg->msg_private, msg, 1,
1017 0, msg->msg_len, msg->msg_len);
1020 return LNET_CREDIT_OK;
1024 lnet_return_tx_credits_locked(struct lnet_msg *msg)
1026 struct lnet_peer_ni *txpeer = msg->msg_txpeer;
1027 struct lnet_ni *txni = msg->msg_txni;
1028 struct lnet_msg *msg2;
1030 if (msg->msg_txcredit) {
1031 struct lnet_ni *ni = msg->msg_txni;
1032 struct lnet_tx_queue *tq = ni->ni_tx_queues[msg->msg_tx_cpt];
1034 /* give back NI txcredits */
1035 msg->msg_txcredit = 0;
1037 LASSERT((tq->tq_credits < 0) ==
1038 !list_empty(&tq->tq_delayed));
1041 atomic_inc(&ni->ni_tx_credits);
1042 if (tq->tq_credits <= 0) {
1043 msg2 = list_entry(tq->tq_delayed.next,
1044 struct lnet_msg, msg_list);
1045 list_del(&msg2->msg_list);
1047 LASSERT(msg2->msg_txni == ni);
1048 LASSERT(msg2->msg_tx_delayed);
1049 LASSERT(msg2->msg_tx_cpt == msg->msg_tx_cpt);
1051 (void) lnet_post_send_locked(msg2, 1);
1055 if (msg->msg_peertxcredit) {
1056 /* give back peer txcredits */
1057 msg->msg_peertxcredit = 0;
1059 spin_lock(&txpeer->lpni_lock);
1060 LASSERT((txpeer->lpni_txcredits < 0) ==
1061 !list_empty(&txpeer->lpni_txq));
1063 txpeer->lpni_txqnob -= msg->msg_len + sizeof(struct lnet_hdr);
1064 LASSERT(txpeer->lpni_txqnob >= 0);
1066 txpeer->lpni_txcredits++;
1067 if (txpeer->lpni_txcredits <= 0) {
1070 msg2 = list_entry(txpeer->lpni_txq.next,
1071 struct lnet_msg, msg_list);
1072 list_del(&msg2->msg_list);
1073 spin_unlock(&txpeer->lpni_lock);
1075 LASSERT(msg2->msg_txpeer == txpeer);
1076 LASSERT(msg2->msg_tx_delayed);
1078 msg2_cpt = msg2->msg_tx_cpt;
1081 * The msg_cpt can be different from the msg2_cpt
1082 * so we need to make sure we lock the correct cpt
1084 * Once we call lnet_post_send_locked() it is no
1085 * longer safe to access msg2, since it could've
1086 * been freed by lnet_finalize(), but we still
1087 * need to relock the correct cpt, so we cache the
1088 * msg2_cpt for the purpose of the check that
1089 * follows the call to lnet_pose_send_locked().
1091 if (msg2_cpt != msg->msg_tx_cpt) {
1092 lnet_net_unlock(msg->msg_tx_cpt);
1093 lnet_net_lock(msg2_cpt);
1095 (void) lnet_post_send_locked(msg2, 1);
1096 if (msg2_cpt != msg->msg_tx_cpt) {
1097 lnet_net_unlock(msg2_cpt);
1098 lnet_net_lock(msg->msg_tx_cpt);
1101 spin_unlock(&txpeer->lpni_lock);
1106 msg->msg_txni = NULL;
1107 lnet_ni_decref_locked(txni, msg->msg_tx_cpt);
1110 if (txpeer != NULL) {
1111 msg->msg_txpeer = NULL;
1112 lnet_peer_ni_decref_locked(txpeer);
1117 lnet_schedule_blocked_locked(struct lnet_rtrbufpool *rbp)
1119 struct lnet_msg *msg;
1121 if (list_empty(&rbp->rbp_msgs))
1123 msg = list_entry(rbp->rbp_msgs.next,
1124 struct lnet_msg, msg_list);
1125 list_del(&msg->msg_list);
1127 (void)lnet_post_routed_recv_locked(msg, 1);
1131 lnet_drop_routed_msgs_locked(struct list_head *list, int cpt)
1133 struct lnet_msg *msg;
1134 struct lnet_msg *tmp;
1136 lnet_net_unlock(cpt);
1138 list_for_each_entry_safe(msg, tmp, list, msg_list) {
1139 lnet_ni_recv(msg->msg_rxni, msg->msg_private, NULL,
1140 0, 0, 0, msg->msg_hdr.payload_length);
1141 list_del_init(&msg->msg_list);
1142 msg->msg_no_resend = true;
1143 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_ERROR;
1144 lnet_finalize(msg, -ECANCELED);
1151 lnet_return_rx_credits_locked(struct lnet_msg *msg)
1153 struct lnet_peer_ni *rxpeerni = msg->msg_rxpeer;
1154 struct lnet_peer *lp;
1155 struct lnet_ni *rxni = msg->msg_rxni;
1156 struct lnet_msg *msg2;
1158 if (msg->msg_rtrcredit) {
1159 /* give back global router credits */
1160 struct lnet_rtrbuf *rb;
1161 struct lnet_rtrbufpool *rbp;
1163 /* NB If a msg ever blocks for a buffer in rbp_msgs, it stays
1164 * there until it gets one allocated, or aborts the wait
1166 LASSERT(msg->msg_kiov != NULL);
1168 rb = list_entry(msg->msg_kiov, struct lnet_rtrbuf, rb_kiov[0]);
1171 msg->msg_kiov = NULL;
1172 msg->msg_rtrcredit = 0;
1174 LASSERT(rbp == lnet_msg2bufpool(msg));
1176 LASSERT((rbp->rbp_credits > 0) ==
1177 !list_empty(&rbp->rbp_bufs));
1179 /* If routing is now turned off, we just drop this buffer and
1180 * don't bother trying to return credits. */
1181 if (!the_lnet.ln_routing) {
1182 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1186 /* It is possible that a user has lowered the desired number of
1187 * buffers in this pool. Make sure we never put back
1188 * more buffers than the stated number. */
1189 if (unlikely(rbp->rbp_credits >= rbp->rbp_req_nbuffers)) {
1190 /* Discard this buffer so we don't have too
1192 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1193 rbp->rbp_nbuffers--;
1195 list_add(&rb->rb_list, &rbp->rbp_bufs);
1197 if (rbp->rbp_credits <= 0)
1198 lnet_schedule_blocked_locked(rbp);
1203 if (msg->msg_peerrtrcredit) {
1205 LASSERT(rxpeerni->lpni_peer_net);
1206 LASSERT(rxpeerni->lpni_peer_net->lpn_peer);
1208 /* give back peer router credits */
1209 msg->msg_peerrtrcredit = 0;
1211 spin_lock(&rxpeerni->lpni_lock);
1212 rxpeerni->lpni_rtrcredits++;
1213 spin_unlock(&rxpeerni->lpni_lock);
1215 lp = rxpeerni->lpni_peer_net->lpn_peer;
1216 spin_lock(&lp->lp_lock);
1218 /* drop all messages which are queued to be routed on that
1220 if (!the_lnet.ln_routing) {
1222 list_splice_init(&lp->lp_rtrq, &drop);
1223 spin_unlock(&lp->lp_lock);
1224 lnet_drop_routed_msgs_locked(&drop, msg->msg_rx_cpt);
1225 } else if (!list_empty(&lp->lp_rtrq)) {
1228 msg2 = list_entry(lp->lp_rtrq.next,
1229 struct lnet_msg, msg_list);
1230 list_del(&msg2->msg_list);
1231 msg2_cpt = msg2->msg_rx_cpt;
1232 spin_unlock(&lp->lp_lock);
1234 * messages on the lp_rtrq can be from any NID in
1235 * the peer, which means they might have different
1236 * cpts. We need to make sure we lock the right
1239 if (msg2_cpt != msg->msg_rx_cpt) {
1240 lnet_net_unlock(msg->msg_rx_cpt);
1241 lnet_net_lock(msg2_cpt);
1243 (void) lnet_post_routed_recv_locked(msg2, 1);
1244 if (msg2_cpt != msg->msg_rx_cpt) {
1245 lnet_net_unlock(msg2_cpt);
1246 lnet_net_lock(msg->msg_rx_cpt);
1249 spin_unlock(&lp->lp_lock);
1253 msg->msg_rxni = NULL;
1254 lnet_ni_decref_locked(rxni, msg->msg_rx_cpt);
1256 if (rxpeerni != NULL) {
1257 msg->msg_rxpeer = NULL;
1258 lnet_peer_ni_decref_locked(rxpeerni);
1263 lnet_compare_gw_lpnis(struct lnet_peer_ni *p1, struct lnet_peer_ni *p2)
1265 if (p1->lpni_txqnob < p2->lpni_txqnob)
1268 if (p1->lpni_txqnob > p2->lpni_txqnob)
1271 if (p1->lpni_txcredits > p2->lpni_txcredits)
1274 if (p1->lpni_txcredits < p2->lpni_txcredits)
1280 static struct lnet_peer_ni *
1281 lnet_select_peer_ni(struct lnet_ni *best_ni, lnet_nid_t dst_nid,
1282 struct lnet_peer *peer,
1283 struct lnet_peer_ni *best_lpni,
1284 struct lnet_peer_net *peer_net)
1287 * Look at the peer NIs for the destination peer that connect
1288 * to the chosen net. If a peer_ni is preferred when using the
1289 * best_ni to communicate, we use that one. If there is no
1290 * preferred peer_ni, or there are multiple preferred peer_ni,
1291 * the available transmit credits are used. If the transmit
1292 * credits are equal, we round-robin over the peer_ni.
1294 struct lnet_peer_ni *lpni = NULL;
1295 int best_lpni_credits = (best_lpni) ? best_lpni->lpni_txcredits :
1297 int best_lpni_healthv = (best_lpni) ?
1298 atomic_read(&best_lpni->lpni_healthv) : 0;
1299 bool preferred = false;
1303 while ((lpni = lnet_get_next_peer_ni_locked(peer, peer_net, lpni))) {
1305 * if the best_ni we've chosen aleady has this lpni
1306 * preferred, then let's use it
1309 ni_is_pref = lnet_peer_is_pref_nid_locked(lpni,
1311 CDEBUG(D_NET, "%s ni_is_pref = %d\n",
1312 libcfs_nid2str(best_ni->ni_nid), ni_is_pref);
1317 lpni_healthv = atomic_read(&lpni->lpni_healthv);
1320 CDEBUG(D_NET, "%s c:[%d, %d], s:[%d, %d]\n",
1321 libcfs_nid2str(lpni->lpni_nid),
1322 lpni->lpni_txcredits, best_lpni_credits,
1323 lpni->lpni_seq, best_lpni->lpni_seq);
1325 /* pick the healthiest peer ni */
1326 if (lpni_healthv < best_lpni_healthv) {
1328 } else if (lpni_healthv > best_lpni_healthv) {
1329 best_lpni_healthv = lpni_healthv;
1330 /* if this is a preferred peer use it */
1331 } else if (!preferred && ni_is_pref) {
1333 } else if (preferred && !ni_is_pref) {
1335 * this is not the preferred peer so let's ignore
1339 } else if (lpni->lpni_txcredits < best_lpni_credits) {
1341 * We already have a peer that has more credits
1342 * available than this one. No need to consider
1343 * this peer further.
1346 } else if (lpni->lpni_txcredits == best_lpni_credits) {
1348 * The best peer found so far and the current peer
1349 * have the same number of available credits let's
1350 * make sure to select between them using Round
1354 if (best_lpni->lpni_seq <= lpni->lpni_seq)
1360 best_lpni_credits = lpni->lpni_txcredits;
1363 /* if we still can't find a peer ni then we can't reach it */
1365 __u32 net_id = (peer_net) ? peer_net->lpn_net_id :
1366 LNET_NIDNET(dst_nid);
1367 CDEBUG(D_NET, "no peer_ni found on peer net %s\n",
1368 libcfs_net2str(net_id));
1372 CDEBUG(D_NET, "sd_best_lpni = %s\n",
1373 libcfs_nid2str(best_lpni->lpni_nid));
1379 * Prerequisite: the best_ni should already be set in the sd
1380 * Find the best lpni.
1381 * If the net id is provided then restrict lpni selection on
1382 * that particular net.
1383 * Otherwise find any reachable lpni. When dealing with an MR
1384 * gateway and it has multiple lpnis which we can use
1385 * we want to select the best one from the list of reachable
1388 static inline struct lnet_peer_ni *
1389 lnet_find_best_lpni(struct lnet_ni *lni, lnet_nid_t dst_nid,
1390 struct lnet_peer *peer, __u32 net_id)
1392 struct lnet_peer_net *peer_net;
1393 __u32 any_net = LNET_NIDNET(LNET_NID_ANY);
1395 /* find the best_lpni on any local network */
1396 if (net_id == any_net) {
1397 struct lnet_peer_ni *best_lpni = NULL;
1398 struct lnet_peer_net *lpn;
1399 list_for_each_entry(lpn, &peer->lp_peer_nets, lpn_peer_nets) {
1400 /* no net specified find any reachable peer ni */
1401 if (!lnet_islocalnet_locked(lpn->lpn_net_id))
1403 best_lpni = lnet_select_peer_ni(lni, dst_nid, peer,
1409 /* restrict on the specified net */
1410 peer_net = lnet_peer_get_net_locked(peer, net_id);
1412 return lnet_select_peer_ni(lni, dst_nid, peer, NULL, peer_net);
1417 /* Compare route priorities and hop counts */
1419 lnet_compare_routes(struct lnet_route *r1, struct lnet_route *r2)
1421 int r1_hops = (r1->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r1->lr_hops;
1422 int r2_hops = (r2->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r2->lr_hops;
1424 if (r1->lr_priority < r2->lr_priority)
1427 if (r1->lr_priority > r2->lr_priority)
1430 if (r1_hops < r2_hops)
1433 if (r1_hops > r2_hops)
1439 static struct lnet_route *
1440 lnet_find_route_locked(struct lnet_remotenet *rnet, __u32 src_net,
1441 struct lnet_route **prev_route,
1442 struct lnet_peer_ni **gwni)
1444 struct lnet_peer_ni *lpni, *best_gw_ni = NULL;
1445 struct lnet_route *best_route;
1446 struct lnet_route *last_route;
1447 struct lnet_route *route;
1450 CDEBUG(D_NET, "Looking up a route to %s, from %s\n",
1451 libcfs_net2str(rnet->lrn_net), libcfs_net2str(src_net));
1453 best_route = last_route = NULL;
1454 list_for_each_entry(route, &rnet->lrn_routes, lr_list) {
1455 if (!lnet_is_route_alive(route))
1459 * Restrict the selection of the router NI on the src_net
1460 * provided. If the src_net is LNET_NID_ANY, then select
1461 * the best interface available.
1464 lpni = lnet_find_best_lpni(NULL, LNET_NID_ANY,
1468 best_route = last_route = route;
1471 CDEBUG(D_NET, "Gateway %s does not have a peer NI on net %s\n",
1472 libcfs_nid2str(route->lr_gateway->lp_primary_nid),
1473 libcfs_net2str(src_net));
1479 /* no protection on below fields, but it's harmless */
1480 if (last_route->lr_seq - route->lr_seq < 0)
1483 rc = lnet_compare_routes(route, best_route);
1487 lpni = lnet_find_best_lpni(NULL, LNET_NID_ANY,
1490 /* restrict the lpni on the src_net if specified */
1492 CDEBUG(D_NET, "Gateway %s does not have a peer NI on net %s\n",
1493 libcfs_nid2str(route->lr_gateway->lp_primary_nid),
1494 libcfs_net2str(src_net));
1504 rc = lnet_compare_gw_lpnis(lpni, best_gw_ni);
1508 if (rc == 1 || route->lr_seq <= best_route->lr_seq) {
1515 *prev_route = last_route;
1521 static struct lnet_ni *
1522 lnet_get_best_ni(struct lnet_net *local_net, struct lnet_ni *best_ni,
1523 struct lnet_peer *peer, struct lnet_peer_net *peer_net,
1526 struct lnet_ni *ni = NULL;
1527 unsigned int shortest_distance;
1532 * If there is no peer_ni that we can send to on this network,
1533 * then there is no point in looking for a new best_ni here.
1535 if (!lnet_get_next_peer_ni_locked(peer, peer_net, NULL))
1538 if (best_ni == NULL) {
1539 shortest_distance = UINT_MAX;
1540 best_credits = INT_MIN;
1543 shortest_distance = cfs_cpt_distance(lnet_cpt_table(), md_cpt,
1544 best_ni->ni_dev_cpt);
1545 best_credits = atomic_read(&best_ni->ni_tx_credits);
1546 best_healthv = atomic_read(&best_ni->ni_healthv);
1549 while ((ni = lnet_get_next_ni_locked(local_net, ni))) {
1550 unsigned int distance;
1555 ni_credits = atomic_read(&ni->ni_tx_credits);
1556 ni_healthv = atomic_read(&ni->ni_healthv);
1557 ni_fatal = atomic_read(&ni->ni_fatal_error_on);
1560 * calculate the distance from the CPT on which
1561 * the message memory is allocated to the CPT of
1562 * the NI's physical device
1564 distance = cfs_cpt_distance(lnet_cpt_table(),
1568 CDEBUG(D_NET, "compare ni %s [c:%d, d:%d, s:%d] with best_ni %s [c:%d, d:%d, s:%d]\n",
1569 libcfs_nid2str(ni->ni_nid), ni_credits, distance,
1570 ni->ni_seq, (best_ni) ? libcfs_nid2str(best_ni->ni_nid)
1571 : "not seleced", best_credits, shortest_distance,
1572 (best_ni) ? best_ni->ni_seq : 0);
1575 * All distances smaller than the NUMA range
1576 * are treated equally.
1578 if (distance < lnet_numa_range)
1579 distance = lnet_numa_range;
1582 * Select on health, shorter distance, available
1583 * credits, then round-robin.
1587 } else if (ni_healthv < best_healthv) {
1589 } else if (ni_healthv > best_healthv) {
1590 best_healthv = ni_healthv;
1592 * If we're going to prefer this ni because it's
1593 * the healthiest, then we should set the
1594 * shortest_distance in the algorithm in case
1595 * there are multiple NIs with the same health but
1596 * different distances.
1598 if (distance < shortest_distance)
1599 shortest_distance = distance;
1600 } else if (distance > shortest_distance) {
1602 } else if (distance < shortest_distance) {
1603 shortest_distance = distance;
1604 } else if (ni_credits < best_credits) {
1606 } else if (ni_credits == best_credits) {
1607 if (best_ni && best_ni->ni_seq <= ni->ni_seq)
1611 best_credits = ni_credits;
1614 CDEBUG(D_NET, "selected best_ni %s\n",
1615 (best_ni) ? libcfs_nid2str(best_ni->ni_nid) : "no selection");
1621 * Traffic to the LNET_RESERVED_PORTAL may not trigger peer discovery,
1622 * because such traffic is required to perform discovery. We therefore
1623 * exclude all GET and PUT on that portal. We also exclude all ACK and
1624 * REPLY traffic, but that is because the portal is not tracked in the
1625 * message structure for these message types. We could restrict this
1626 * further by also checking for LNET_PROTO_PING_MATCHBITS.
1629 lnet_msg_discovery(struct lnet_msg *msg)
1631 if (msg->msg_type == LNET_MSG_PUT) {
1632 if (msg->msg_hdr.msg.put.ptl_index != LNET_RESERVED_PORTAL)
1634 } else if (msg->msg_type == LNET_MSG_GET) {
1635 if (msg->msg_hdr.msg.get.ptl_index != LNET_RESERVED_PORTAL)
1641 #define SRC_SPEC 0x0001
1642 #define SRC_ANY 0x0002
1643 #define LOCAL_DST 0x0004
1644 #define REMOTE_DST 0x0008
1645 #define MR_DST 0x0010
1646 #define NMR_DST 0x0020
1647 #define SND_RESP 0x0040
1649 /* The following to defines are used for return codes */
1650 #define REPEAT_SEND 0x1000
1651 #define PASS_THROUGH 0x2000
1653 /* The different cases lnet_select pathway needs to handle */
1654 #define SRC_SPEC_LOCAL_MR_DST (SRC_SPEC | LOCAL_DST | MR_DST)
1655 #define SRC_SPEC_ROUTER_MR_DST (SRC_SPEC | REMOTE_DST | MR_DST)
1656 #define SRC_SPEC_LOCAL_NMR_DST (SRC_SPEC | LOCAL_DST | NMR_DST)
1657 #define SRC_SPEC_ROUTER_NMR_DST (SRC_SPEC | REMOTE_DST | NMR_DST)
1658 #define SRC_ANY_LOCAL_MR_DST (SRC_ANY | LOCAL_DST | MR_DST)
1659 #define SRC_ANY_ROUTER_MR_DST (SRC_ANY | REMOTE_DST | MR_DST)
1660 #define SRC_ANY_LOCAL_NMR_DST (SRC_ANY | LOCAL_DST | NMR_DST)
1661 #define SRC_ANY_ROUTER_NMR_DST (SRC_ANY | REMOTE_DST | NMR_DST)
1664 lnet_handle_lo_send(struct lnet_send_data *sd)
1666 struct lnet_msg *msg = sd->sd_msg;
1667 int cpt = sd->sd_cpt;
1669 /* No send credit hassles with LOLND */
1670 lnet_ni_addref_locked(the_lnet.ln_loni, cpt);
1671 msg->msg_hdr.dest_nid = cpu_to_le64(the_lnet.ln_loni->ni_nid);
1672 if (!msg->msg_routing)
1673 msg->msg_hdr.src_nid =
1674 cpu_to_le64(the_lnet.ln_loni->ni_nid);
1675 msg->msg_target.nid = the_lnet.ln_loni->ni_nid;
1676 lnet_msg_commit(msg, cpt);
1677 msg->msg_txni = the_lnet.ln_loni;
1679 return LNET_CREDIT_OK;
1683 lnet_handle_send(struct lnet_send_data *sd)
1685 struct lnet_ni *best_ni = sd->sd_best_ni;
1686 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
1687 struct lnet_peer_ni *final_dst_lpni = sd->sd_final_dst_lpni;
1688 struct lnet_msg *msg = sd->sd_msg;
1690 __u32 send_case = sd->sd_send_case;
1692 __u32 routing = send_case & REMOTE_DST;
1693 struct lnet_rsp_tracker *rspt;
1696 * Increment sequence number of the selected peer so that we
1697 * pick the next one in Round Robin.
1699 best_lpni->lpni_seq++;
1702 * grab a reference on the peer_ni so it sticks around even if
1703 * we need to drop and relock the lnet_net_lock below.
1705 lnet_peer_ni_addref_locked(best_lpni);
1708 * Use lnet_cpt_of_nid() to determine the CPT used to commit the
1709 * message. This ensures that we get a CPT that is correct for
1710 * the NI when the NI has been restricted to a subset of all CPTs.
1711 * If the selected CPT differs from the one currently locked, we
1712 * must unlock and relock the lnet_net_lock(), and then check whether
1713 * the configuration has changed. We don't have a hold on the best_ni
1714 * yet, and it may have vanished.
1716 cpt2 = lnet_cpt_of_nid_locked(best_lpni->lpni_nid, best_ni);
1717 if (sd->sd_cpt != cpt2) {
1718 __u32 seq = lnet_get_dlc_seq_locked();
1719 lnet_net_unlock(sd->sd_cpt);
1721 lnet_net_lock(sd->sd_cpt);
1722 if (seq != lnet_get_dlc_seq_locked()) {
1723 lnet_peer_ni_decref_locked(best_lpni);
1729 * store the best_lpni in the message right away to avoid having
1730 * to do the same operation under different conditions
1732 msg->msg_txpeer = best_lpni;
1733 msg->msg_txni = best_ni;
1736 * grab a reference for the best_ni since now it's in use in this
1737 * send. The reference will be dropped in lnet_finalize()
1739 lnet_ni_addref_locked(msg->msg_txni, sd->sd_cpt);
1742 * Always set the target.nid to the best peer picked. Either the
1743 * NID will be one of the peer NIDs selected, or the same NID as
1744 * what was originally set in the target or it will be the NID of
1745 * a router if this message should be routed
1747 msg->msg_target.nid = msg->msg_txpeer->lpni_nid;
1750 * lnet_msg_commit assigns the correct cpt to the message, which
1751 * is used to decrement the correct refcount on the ni when it's
1752 * time to return the credits
1754 lnet_msg_commit(msg, sd->sd_cpt);
1757 * If we are routing the message then we keep the src_nid that was
1758 * set by the originator. If we are not routing then we are the
1759 * originator and set it here.
1761 if (!msg->msg_routing)
1762 msg->msg_hdr.src_nid = cpu_to_le64(msg->msg_txni->ni_nid);
1765 msg->msg_target_is_router = 1;
1766 msg->msg_target.pid = LNET_PID_LUSTRE;
1768 * since we're routing we want to ensure that the
1769 * msg_hdr.dest_nid is set to the final destination. When
1770 * the router receives this message it knows how to route
1773 * final_dst_lpni is set at the beginning of the
1774 * lnet_select_pathway() function and is never changed.
1775 * It's safe to use it here.
1777 msg->msg_hdr.dest_nid = cpu_to_le64(final_dst_lpni->lpni_nid);
1780 * if we're not routing set the dest_nid to the best peer
1781 * ni NID that we picked earlier in the algorithm.
1783 msg->msg_hdr.dest_nid = cpu_to_le64(msg->msg_txpeer->lpni_nid);
1787 * if we have response tracker block update it with the next hop
1791 rspt = msg->msg_md->md_rspt_ptr;
1793 rspt->rspt_next_hop_nid = msg->msg_txpeer->lpni_nid;
1794 CDEBUG(D_NET, "rspt_next_hop_nid = %s\n",
1795 libcfs_nid2str(rspt->rspt_next_hop_nid));
1799 rc = lnet_post_send_locked(msg, 0);
1802 CDEBUG(D_NET, "TRACE: %s(%s:%s) -> %s(%s:%s) %s : %s try# %d\n",
1803 libcfs_nid2str(msg->msg_hdr.src_nid),
1804 libcfs_nid2str(msg->msg_txni->ni_nid),
1805 libcfs_nid2str(sd->sd_src_nid),
1806 libcfs_nid2str(msg->msg_hdr.dest_nid),
1807 libcfs_nid2str(sd->sd_dst_nid),
1808 libcfs_nid2str(msg->msg_txpeer->lpni_nid),
1809 libcfs_nid2str(sd->sd_rtr_nid),
1810 lnet_msgtyp2str(msg->msg_type), msg->msg_retry_count);
1816 lnet_set_non_mr_pref_nid(struct lnet_peer_ni *lpni, struct lnet_ni *lni,
1817 struct lnet_msg *msg)
1819 if (!lnet_msg_is_response(msg) && lpni->lpni_pref_nnids == 0) {
1820 CDEBUG(D_NET, "Setting preferred local NID %s on NMR peer %s\n",
1821 libcfs_nid2str(lni->ni_nid),
1822 libcfs_nid2str(lpni->lpni_nid));
1823 lnet_peer_ni_set_non_mr_pref_nid(lpni, lni->ni_nid);
1832 * use the source and destination NIDs as the pathway
1835 lnet_handle_spec_local_nmr_dst(struct lnet_send_data *sd)
1837 /* the destination lpni is set before we get here. */
1840 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
1841 if (!sd->sd_best_ni) {
1842 CERROR("Can't send to %s: src %s is not a "
1843 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
1844 libcfs_nid2str(sd->sd_src_nid));
1848 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
1850 return lnet_handle_send(sd);
1858 * Don't run the selection algorithm on the peer NIs. By specifying the
1859 * local NID, we're also saying that we should always use the destination NID
1860 * provided. This handles the case where we should be using the same
1861 * destination NID for the all the messages which belong to the same RPC
1865 lnet_handle_spec_local_mr_dst(struct lnet_send_data *sd)
1867 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
1868 if (!sd->sd_best_ni) {
1869 CERROR("Can't send to %s: src %s is not a "
1870 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
1871 libcfs_nid2str(sd->sd_src_nid));
1875 if (sd->sd_best_lpni &&
1876 sd->sd_best_lpni->lpni_nid == the_lnet.ln_loni->ni_nid)
1877 return lnet_handle_lo_send(sd);
1878 else if (sd->sd_best_lpni)
1879 return lnet_handle_send(sd);
1881 CERROR("can't send to %s. no NI on %s\n",
1882 libcfs_nid2str(sd->sd_dst_nid),
1883 libcfs_net2str(sd->sd_best_ni->ni_net->net_id));
1885 return -EHOSTUNREACH;
1889 lnet_find_best_ni_on_spec_net(struct lnet_ni *cur_best_ni,
1890 struct lnet_peer *peer,
1891 struct lnet_peer_net *peer_net,
1895 struct lnet_net *local_net;
1896 struct lnet_ni *best_ni;
1898 local_net = lnet_get_net_locked(peer_net->lpn_net_id);
1903 * Iterate through the NIs in this local Net and select
1904 * the NI to send from. The selection is determined by
1905 * these 3 criterion in the following priority:
1907 * 2. NI available credits
1910 best_ni = lnet_get_best_ni(local_net, cur_best_ni,
1911 peer, peer_net, cpt);
1913 if (incr_seq && best_ni)
1920 lnet_initiate_peer_discovery(struct lnet_peer_ni *lpni, struct lnet_msg *msg,
1923 struct lnet_peer *peer;
1926 lnet_peer_ni_addref_locked(lpni);
1928 peer = lpni->lpni_peer_net->lpn_peer;
1930 if (lnet_peer_gw_discovery(peer)) {
1931 lnet_peer_ni_decref_locked(lpni);
1935 if (!lnet_msg_discovery(msg) || lnet_peer_is_uptodate(peer)) {
1936 lnet_peer_ni_decref_locked(lpni);
1940 rc = lnet_discover_peer_locked(lpni, cpt, false);
1942 lnet_peer_ni_decref_locked(lpni);
1945 /* The peer may have changed. */
1946 peer = lpni->lpni_peer_net->lpn_peer;
1947 spin_lock(&peer->lp_lock);
1948 if (lnet_peer_is_uptodate_locked(peer)) {
1949 spin_unlock(&peer->lp_lock);
1950 lnet_peer_ni_decref_locked(lpni);
1953 /* queue message and return */
1954 msg->msg_sending = 0;
1955 msg->msg_txpeer = NULL;
1956 list_add_tail(&msg->msg_list, &peer->lp_dc_pendq);
1957 spin_unlock(&peer->lp_lock);
1959 lnet_peer_ni_decref_locked(lpni);
1961 CDEBUG(D_NET, "msg %p delayed. %s pending discovery\n",
1962 msg, libcfs_nid2str(peer->lp_primary_nid));
1964 return LNET_DC_WAIT;
1968 lnet_handle_find_routed_path(struct lnet_send_data *sd,
1970 struct lnet_peer_ni **gw_lpni,
1971 struct lnet_peer **gw_peer)
1975 struct lnet_peer *gw;
1976 struct lnet_peer *lp;
1977 struct lnet_peer_net *lpn;
1978 struct lnet_peer_net *best_lpn = NULL;
1979 struct lnet_remotenet *rnet, *best_rnet = NULL;
1980 struct lnet_route *best_route = NULL;
1981 struct lnet_route *last_route = NULL;
1982 struct lnet_peer_ni *lpni = NULL;
1983 struct lnet_peer_ni *gwni = NULL;
1984 bool route_found = false;
1985 lnet_nid_t src_nid = (sd->sd_src_nid != LNET_NID_ANY) ? sd->sd_src_nid :
1986 (sd->sd_best_ni != NULL) ? sd->sd_best_ni->ni_nid :
1989 CDEBUG(D_NET, "using src nid %s for route restriction\n",
1990 libcfs_nid2str(src_nid));
1992 /* If a router nid was specified then we are replying to a GET or
1993 * sending an ACK. In this case we use the gateway associated with the
1994 * specified router nid.
1996 if (sd->sd_rtr_nid != LNET_NID_ANY) {
1997 gwni = lnet_find_peer_ni_locked(sd->sd_rtr_nid);
1999 gw = gwni->lpni_peer_net->lpn_peer;
2000 lnet_peer_ni_decref_locked(gwni);
2001 if (gw->lp_rtr_refcount) {
2002 local_lnet = LNET_NIDNET(sd->sd_rtr_nid);
2006 CWARN("No peer NI for gateway %s. Attempting to find an alternative route.\n",
2007 libcfs_nid2str(sd->sd_rtr_nid));
2012 /* we've already looked up the initial lpni using dst_nid */
2013 lpni = sd->sd_best_lpni;
2014 /* the peer tree must be in existence */
2015 LASSERT(lpni && lpni->lpni_peer_net &&
2016 lpni->lpni_peer_net->lpn_peer);
2017 lp = lpni->lpni_peer_net->lpn_peer;
2019 list_for_each_entry(lpn, &lp->lp_peer_nets, lpn_peer_nets) {
2020 /* is this remote network reachable? */
2021 rnet = lnet_find_rnet_locked(lpn->lpn_net_id);
2030 if (best_lpn->lpn_seq <= lpn->lpn_seq)
2038 CERROR("peer %s has no available nets\n",
2039 libcfs_nid2str(sd->sd_dst_nid));
2040 return -EHOSTUNREACH;
2043 sd->sd_best_lpni = lnet_find_best_lpni(sd->sd_best_ni,
2046 best_lpn->lpn_net_id);
2047 if (!sd->sd_best_lpni) {
2048 CERROR("peer %s is unreachable\n",
2049 libcfs_nid2str(sd->sd_dst_nid));
2050 return -EHOSTUNREACH;
2054 * We're attempting to round robin over the remote peer
2055 * NI's so update the final destination we selected
2057 sd->sd_final_dst_lpni = sd->sd_best_lpni;
2060 * find the best route. Restrict the selection on the net of the
2061 * local NI if we've already picked the local NI to send from.
2062 * Otherwise, let's pick any route we can find and then find
2063 * a local NI we can reach the route's gateway on. Any route we select
2064 * will be reachable by virtue of the restriction we have when
2067 best_route = lnet_find_route_locked(best_rnet,
2068 LNET_NIDNET(src_nid),
2069 &last_route, &gwni);
2072 CERROR("no route to %s from %s\n",
2073 libcfs_nid2str(dst_nid),
2074 libcfs_nid2str(src_nid));
2075 return -EHOSTUNREACH;
2079 CERROR("Internal Error. Route expected to %s from %s\n",
2080 libcfs_nid2str(dst_nid),
2081 libcfs_nid2str(src_nid));
2085 gw = best_route->lr_gateway;
2086 LASSERT(gw == gwni->lpni_peer_net->lpn_peer);
2087 local_lnet = best_route->lr_lnet;
2090 * Increment the sequence number of the remote lpni so we
2091 * can round robin over the different interfaces of the
2094 sd->sd_best_lpni->lpni_seq++;
2098 * Discover this gateway if it hasn't already been discovered.
2099 * This means we might delay the message until discovery has
2102 sd->sd_msg->msg_src_nid_param = sd->sd_src_nid;
2103 rc = lnet_initiate_peer_discovery(gwni, sd->sd_msg, sd->sd_cpt);
2107 if (!sd->sd_best_ni)
2108 sd->sd_best_ni = lnet_find_best_ni_on_spec_net(NULL, gw,
2109 lnet_peer_get_net_locked(gw,
2114 if (!sd->sd_best_ni) {
2115 CERROR("Internal Error. Expected local ni on %s but non found :%s\n",
2116 libcfs_net2str(local_lnet),
2117 libcfs_nid2str(sd->sd_src_nid));
2125 * increment the sequence numbers since now we're sure we're
2126 * going to use this path
2128 if (sd->sd_rtr_nid == LNET_NID_ANY) {
2129 LASSERT(best_route && last_route);
2130 best_route->lr_seq = last_route->lr_seq + 1;
2131 best_lpn->lpn_seq++;
2142 * Remote destination
2143 * Non-MR destination
2147 * Remote destination
2150 * The handling of these two cases is similar. Even though the destination
2151 * can be MR or non-MR, we'll deal directly with the router.
2154 lnet_handle_spec_router_dst(struct lnet_send_data *sd)
2157 struct lnet_peer_ni *gw_lpni = NULL;
2158 struct lnet_peer *gw_peer = NULL;
2161 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
2162 if (!sd->sd_best_ni) {
2163 CERROR("Can't send to %s: src %s is not a "
2164 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
2165 libcfs_nid2str(sd->sd_src_nid));
2169 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2174 if (sd->sd_send_case & NMR_DST)
2176 * since the final destination is non-MR let's set its preferred
2177 * NID before we send
2179 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni,
2183 * We're going to send to the gw found so let's set its
2186 sd->sd_peer = gw_peer;
2187 sd->sd_best_lpni = gw_lpni;
2189 return lnet_handle_send(sd);
2193 lnet_find_best_ni_on_local_net(struct lnet_peer *peer, int md_cpt,
2196 struct lnet_peer_net *peer_net = NULL;
2197 struct lnet_ni *best_ni = NULL;
2198 int lpn_healthv = 0;
2201 * The peer can have multiple interfaces, some of them can be on
2202 * the local network and others on a routed network. We should
2203 * prefer the local network. However if the local network is not
2204 * available then we need to try the routed network
2207 /* go through all the peer nets and find the best_ni */
2208 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_peer_nets) {
2210 * The peer's list of nets can contain non-local nets. We
2211 * want to only examine the local ones.
2213 if (!lnet_get_net_locked(peer_net->lpn_net_id))
2216 /* always select the lpn with the best health */
2217 if (lpn_healthv <= peer_net->lpn_healthv)
2218 lpn_healthv = peer_net->lpn_healthv;
2222 best_ni = lnet_find_best_ni_on_spec_net(best_ni, peer, peer_net,
2226 * if this is a discovery message and lp_disc_net_id is
2227 * specified then use that net to send the discovery on.
2229 if (peer->lp_disc_net_id == peer_net->lpn_net_id &&
2235 /* increment sequence number so we can round robin */
2241 static struct lnet_ni *
2242 lnet_find_existing_preferred_best_ni(struct lnet_peer_ni *lpni, int cpt)
2244 struct lnet_ni *best_ni = NULL;
2245 struct lnet_peer_net *peer_net = lpni->lpni_peer_net;
2246 struct lnet_peer_ni *lpni_entry;
2249 * We must use a consistent source address when sending to a
2250 * non-MR peer. However, a non-MR peer can have multiple NIDs
2251 * on multiple networks, and we may even need to talk to this
2252 * peer on multiple networks -- certain types of
2253 * load-balancing configuration do this.
2255 * So we need to pick the NI the peer prefers for this
2256 * particular network.
2259 list_for_each_entry(lpni_entry, &peer_net->lpn_peer_nis,
2261 if (lpni_entry->lpni_pref_nnids == 0)
2263 LASSERT(lpni_entry->lpni_pref_nnids == 1);
2264 best_ni = lnet_nid2ni_locked(lpni_entry->lpni_pref.nid, cpt);
2271 /* Prerequisite: sd->sd_peer and sd->sd_best_lpni should be set */
2273 lnet_select_preferred_best_ni(struct lnet_send_data *sd)
2275 struct lnet_ni *best_ni = NULL;
2276 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
2279 * We must use a consistent source address when sending to a
2280 * non-MR peer. However, a non-MR peer can have multiple NIDs
2281 * on multiple networks, and we may even need to talk to this
2282 * peer on multiple networks -- certain types of
2283 * load-balancing configuration do this.
2285 * So we need to pick the NI the peer prefers for this
2286 * particular network.
2289 best_ni = lnet_find_existing_preferred_best_ni(sd->sd_best_lpni,
2292 /* if best_ni is still not set just pick one */
2295 lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2296 sd->sd_best_lpni->lpni_peer_net,
2297 sd->sd_md_cpt, true);
2298 /* If there is no best_ni we don't have a route */
2300 CERROR("no path to %s from net %s\n",
2301 libcfs_nid2str(best_lpni->lpni_nid),
2302 libcfs_net2str(best_lpni->lpni_net->net_id));
2303 return -EHOSTUNREACH;
2307 sd->sd_best_ni = best_ni;
2309 /* Set preferred NI if necessary. */
2310 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
2317 * Source not specified
2321 * always use the same source NID for NMR peers
2322 * If we've talked to that peer before then we already have a preferred
2323 * source NI associated with it. Otherwise, we select a preferred local NI
2324 * and store it in the peer
2327 lnet_handle_any_local_nmr_dst(struct lnet_send_data *sd)
2331 /* sd->sd_best_lpni is already set to the final destination */
2334 * At this point we should've created the peer ni and peer. If we
2335 * can't find it, then something went wrong. Instead of assert
2336 * output a relevant message and fail the send
2338 if (!sd->sd_best_lpni) {
2339 CERROR("Internal fault. Unable to send msg %s to %s. "
2341 lnet_msgtyp2str(sd->sd_msg->msg_type),
2342 libcfs_nid2str(sd->sd_dst_nid));
2346 rc = lnet_select_preferred_best_ni(sd);
2348 rc = lnet_handle_send(sd);
2354 lnet_handle_any_mr_dsta(struct lnet_send_data *sd)
2357 * NOTE we've already handled the remote peer case. So we only
2358 * need to worry about the local case here.
2360 * if we're sending a response, ACK or reply, we need to send it
2361 * to the destination NID given to us. At this point we already
2362 * have the peer_ni we're suppose to send to, so just find the
2363 * best_ni on the peer net and use that. Since we're sending to an
2364 * MR peer then we can just run the selection algorithm on our
2365 * local NIs and pick the best one.
2367 if (sd->sd_send_case & SND_RESP) {
2369 lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2370 sd->sd_best_lpni->lpni_peer_net,
2371 sd->sd_md_cpt, true);
2373 if (!sd->sd_best_ni) {
2375 * We're not going to deal with not able to send
2376 * a response to the provided final destination
2378 CERROR("Can't send response to %s. "
2379 "No local NI available\n",
2380 libcfs_nid2str(sd->sd_dst_nid));
2381 return -EHOSTUNREACH;
2384 return lnet_handle_send(sd);
2388 * If we get here that means we're sending a fresh request, PUT or
2389 * GET, so we need to run our standard selection algorithm.
2390 * First find the best local interface that's on any of the peer's
2393 sd->sd_best_ni = lnet_find_best_ni_on_local_net(sd->sd_peer,
2395 lnet_msg_discovery(sd->sd_msg));
2396 if (sd->sd_best_ni) {
2398 lnet_find_best_lpni(sd->sd_best_ni, sd->sd_dst_nid,
2400 sd->sd_best_ni->ni_net->net_id);
2403 * if we're successful in selecting a peer_ni on the local
2404 * network, then send to it. Otherwise fall through and
2405 * try and see if we can reach it over another routed
2408 if (sd->sd_best_lpni &&
2409 sd->sd_best_lpni->lpni_nid == the_lnet.ln_loni->ni_nid) {
2411 * in case we initially started with a routed
2412 * destination, let's reset to local
2414 sd->sd_send_case &= ~REMOTE_DST;
2415 sd->sd_send_case |= LOCAL_DST;
2416 return lnet_handle_lo_send(sd);
2417 } else if (sd->sd_best_lpni) {
2419 * in case we initially started with a routed
2420 * destination, let's reset to local
2422 sd->sd_send_case &= ~REMOTE_DST;
2423 sd->sd_send_case |= LOCAL_DST;
2424 return lnet_handle_send(sd);
2427 CERROR("Internal Error. Expected to have a best_lpni: "
2429 libcfs_nid2str(sd->sd_src_nid),
2430 libcfs_nid2str(sd->sd_dst_nid));
2436 * Peer doesn't have a local network. Let's see if there is
2437 * a remote network we can reach it on.
2439 return PASS_THROUGH;
2444 * Source NID not specified
2449 * Source NID not speified
2450 * Remote destination
2453 * In both of these cases if we're sending a response, ACK or REPLY, then
2454 * we need to send to the destination NID provided.
2456 * In the remote case let's deal with MR routers.
2461 lnet_handle_any_mr_dst(struct lnet_send_data *sd)
2464 struct lnet_peer *gw_peer = NULL;
2465 struct lnet_peer_ni *gw_lpni = NULL;
2468 * handle sending a response to a remote peer here so we don't
2469 * have to worry about it if we hit lnet_handle_any_mr_dsta()
2471 if (sd->sd_send_case & REMOTE_DST &&
2472 sd->sd_send_case & SND_RESP) {
2473 struct lnet_peer_ni *gw;
2474 struct lnet_peer *gw_peer;
2476 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw,
2479 CERROR("Can't send response to %s. "
2480 "No route available\n",
2481 libcfs_nid2str(sd->sd_dst_nid));
2482 return -EHOSTUNREACH;
2483 } else if (rc > 0) {
2487 sd->sd_best_lpni = gw;
2488 sd->sd_peer = gw_peer;
2490 return lnet_handle_send(sd);
2494 * Even though the NID for the peer might not be on a local network,
2495 * since the peer is MR there could be other interfaces on the
2496 * local network. In that case we'd still like to prefer the local
2497 * network over the routed network. If we're unable to do that
2498 * then we select the best router among the different routed networks,
2499 * and if the router is MR then we can deal with it as such.
2501 rc = lnet_handle_any_mr_dsta(sd);
2502 if (rc != PASS_THROUGH)
2506 * Now that we must route to the destination, we must consider the
2507 * MR case, where the destination has multiple interfaces, some of
2508 * which we can route to and others we do not. For this reason we
2509 * need to select the destination which we can route to and if
2510 * there are multiple, we need to round robin.
2512 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2517 sd->sd_send_case &= ~LOCAL_DST;
2518 sd->sd_send_case |= REMOTE_DST;
2520 sd->sd_peer = gw_peer;
2521 sd->sd_best_lpni = gw_lpni;
2523 return lnet_handle_send(sd);
2527 * Source not specified
2528 * Remote destination
2531 * Must send to the specified peer NID using the same source NID that
2532 * we've used before. If it's the first time to talk to that peer then
2533 * find the source NI and assign it as preferred to that peer
2536 lnet_handle_any_router_nmr_dst(struct lnet_send_data *sd)
2539 struct lnet_peer_ni *gw_lpni = NULL;
2540 struct lnet_peer *gw_peer = NULL;
2543 * Let's see if we have a preferred NI to talk to this NMR peer
2545 sd->sd_best_ni = lnet_find_existing_preferred_best_ni(sd->sd_best_lpni,
2549 * find the router and that'll find the best NI if we didn't find
2552 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2558 * set the best_ni we've chosen as the preferred one for
2561 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
2563 /* we'll be sending to the gw */
2564 sd->sd_best_lpni = gw_lpni;
2565 sd->sd_peer = gw_peer;
2567 return lnet_handle_send(sd);
2571 lnet_handle_send_case_locked(struct lnet_send_data *sd)
2574 * turn off the SND_RESP bit.
2575 * It will be checked in the case handling
2577 __u32 send_case = sd->sd_send_case &= ~SND_RESP ;
2579 CDEBUG(D_NET, "Source %s%s to %s %s %s destination\n",
2580 (send_case & SRC_SPEC) ? "Specified: " : "ANY",
2581 (send_case & SRC_SPEC) ? libcfs_nid2str(sd->sd_src_nid) : "",
2582 (send_case & MR_DST) ? "MR: " : "NMR: ",
2583 libcfs_nid2str(sd->sd_dst_nid),
2584 (send_case & LOCAL_DST) ? "local" : "routed");
2586 switch (send_case) {
2588 * For all cases where the source is specified, we should always
2589 * use the destination NID, whether it's an MR destination or not,
2590 * since we're continuing a series of related messages for the
2593 case SRC_SPEC_LOCAL_NMR_DST:
2594 return lnet_handle_spec_local_nmr_dst(sd);
2595 case SRC_SPEC_LOCAL_MR_DST:
2596 return lnet_handle_spec_local_mr_dst(sd);
2597 case SRC_SPEC_ROUTER_NMR_DST:
2598 case SRC_SPEC_ROUTER_MR_DST:
2599 return lnet_handle_spec_router_dst(sd);
2600 case SRC_ANY_LOCAL_NMR_DST:
2601 return lnet_handle_any_local_nmr_dst(sd);
2602 case SRC_ANY_LOCAL_MR_DST:
2603 case SRC_ANY_ROUTER_MR_DST:
2604 return lnet_handle_any_mr_dst(sd);
2605 case SRC_ANY_ROUTER_NMR_DST:
2606 return lnet_handle_any_router_nmr_dst(sd);
2608 CERROR("Unknown send case\n");
2614 lnet_select_pathway(lnet_nid_t src_nid, lnet_nid_t dst_nid,
2615 struct lnet_msg *msg, lnet_nid_t rtr_nid)
2617 struct lnet_peer_ni *lpni;
2618 struct lnet_peer *peer;
2619 struct lnet_send_data send_data;
2622 __u32 send_case = 0;
2624 bool mr_forwarding_allowed;
2626 memset(&send_data, 0, sizeof(send_data));
2629 * get an initial CPT to use for locking. The idea here is not to
2630 * serialize the calls to select_pathway, so that as many
2631 * operations can run concurrently as possible. To do that we use
2632 * the CPT where this call is being executed. Later on when we
2633 * determine the CPT to use in lnet_message_commit, we switch the
2634 * lock and check if there was any configuration change. If none,
2635 * then we proceed, if there is, then we restart the operation.
2637 cpt = lnet_net_lock_current();
2639 md_cpt = lnet_cpt_of_md(msg->msg_md, msg->msg_offset);
2640 if (md_cpt == CFS_CPT_ANY)
2646 * If we're being asked to send to the loopback interface, there
2647 * is no need to go through any selection. We can just shortcut
2648 * the entire process and send over lolnd
2650 send_data.sd_msg = msg;
2651 send_data.sd_cpt = cpt;
2652 if (dst_nid == LNET_NID_LO_0) {
2653 rc = lnet_handle_lo_send(&send_data);
2654 lnet_net_unlock(cpt);
2659 * find an existing peer_ni, or create one and mark it as having been
2660 * created due to network traffic. This call will create the
2661 * peer->peer_net->peer_ni tree.
2663 lpni = lnet_nid2peerni_locked(dst_nid, LNET_NID_ANY, cpt);
2665 lnet_net_unlock(cpt);
2666 return PTR_ERR(lpni);
2670 * Cache the original src_nid and rtr_nid. If we need to resend the
2671 * message then we'll need to know whether the src_nid was originally
2672 * specified for this message. If it was originally specified,
2673 * then we need to keep using the same src_nid since it's
2674 * continuing the same sequence of messages. Similarly, rtr_nid will
2675 * affect our choice of next hop.
2677 msg->msg_src_nid_param = src_nid;
2678 msg->msg_rtr_nid_param = rtr_nid;
2681 * If necessary, perform discovery on the peer that owns this peer_ni.
2682 * Note, this can result in the ownership of this peer_ni changing
2683 * to another peer object.
2685 rc = lnet_initiate_peer_discovery(lpni, msg, cpt);
2687 lnet_peer_ni_decref_locked(lpni);
2688 lnet_net_unlock(cpt);
2691 lnet_peer_ni_decref_locked(lpni);
2693 peer = lpni->lpni_peer_net->lpn_peer;
2696 * Identify the different send cases
2698 if (src_nid == LNET_NID_ANY)
2699 send_case |= SRC_ANY;
2701 send_case |= SRC_SPEC;
2703 if (lnet_get_net_locked(LNET_NIDNET(dst_nid)))
2704 send_case |= LOCAL_DST;
2706 send_case |= REMOTE_DST;
2709 if (msg->msg_routing && (send_case & LOCAL_DST))
2712 /* Determine whether to allow MR forwarding for this message.
2713 * NB: MR forwarding is allowed if the message originator and the
2714 * destination are both MR capable, and the destination lpni that was
2715 * originally chosen by the originator is unhealthy or down.
2716 * We check the MR capability of the destination further below
2718 mr_forwarding_allowed = false;
2720 struct lnet_peer *src_lp;
2721 struct lnet_peer_ni *src_lpni;
2723 src_lpni = lnet_nid2peerni_locked(msg->msg_hdr.src_nid,
2725 /* We don't fail the send if we hit any errors here. We'll just
2726 * try to send it via non-multi-rail criteria
2728 if (!IS_ERR(src_lpni)) {
2729 src_lp = lpni->lpni_peer_net->lpn_peer;
2730 if (lnet_peer_is_multi_rail(src_lp) &&
2731 !lnet_is_peer_ni_alive(lpni))
2732 mr_forwarding_allowed = true;
2735 CDEBUG(D_NET, "msg %p MR forwarding %s\n", msg,
2736 mr_forwarding_allowed ? "allowed" : "not allowed");
2740 * Deal with the peer as NMR in the following cases:
2741 * 1. the peer is NMR
2742 * 2. We're trying to recover a specific peer NI
2743 * 3. I'm a router sending to the final destination and MR forwarding is
2744 * not allowed for this message (as determined above).
2745 * In this case the source of the message would've
2746 * already selected the final destination so my job
2747 * is to honor the selection.
2749 if (!lnet_peer_is_multi_rail(peer) || msg->msg_recovery ||
2750 (final_hop && !mr_forwarding_allowed))
2751 send_case |= NMR_DST;
2753 send_case |= MR_DST;
2755 if (lnet_msg_is_response(msg))
2756 send_case |= SND_RESP;
2758 /* assign parameters to the send_data */
2759 send_data.sd_rtr_nid = rtr_nid;
2760 send_data.sd_src_nid = src_nid;
2761 send_data.sd_dst_nid = dst_nid;
2762 send_data.sd_best_lpni = lpni;
2764 * keep a pointer to the final destination in case we're going to
2765 * route, so we'll need to access it later
2767 send_data.sd_final_dst_lpni = lpni;
2768 send_data.sd_peer = peer;
2769 send_data.sd_md_cpt = md_cpt;
2770 send_data.sd_send_case = send_case;
2772 rc = lnet_handle_send_case_locked(&send_data);
2775 * Update the local cpt since send_data.sd_cpt might've been
2776 * updated as a result of calling lnet_handle_send_case_locked().
2778 cpt = send_data.sd_cpt;
2780 if (rc == REPEAT_SEND)
2783 lnet_net_unlock(cpt);
2789 lnet_send(lnet_nid_t src_nid, struct lnet_msg *msg, lnet_nid_t rtr_nid)
2791 lnet_nid_t dst_nid = msg->msg_target.nid;
2795 * NB: rtr_nid is set to LNET_NID_ANY for all current use-cases,
2796 * but we might want to use pre-determined router for ACK/REPLY
2799 /* NB: ni != NULL == interface pre-determined (ACK/REPLY) */
2800 LASSERT(msg->msg_txpeer == NULL);
2801 LASSERT(msg->msg_txni == NULL);
2802 LASSERT(!msg->msg_sending);
2803 LASSERT(!msg->msg_target_is_router);
2804 LASSERT(!msg->msg_receiving);
2806 msg->msg_sending = 1;
2808 LASSERT(!msg->msg_tx_committed);
2810 rc = lnet_select_pathway(src_nid, dst_nid, msg, rtr_nid);
2812 if (rc == -EHOSTUNREACH)
2813 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_ERROR;
2815 msg->msg_health_status = LNET_MSG_STATUS_LOCAL_ERROR;
2819 if (rc == LNET_CREDIT_OK)
2820 lnet_ni_send(msg->msg_txni, msg);
2822 /* rc == LNET_CREDIT_OK or LNET_CREDIT_WAIT or LNET_DC_WAIT */
2826 enum lnet_mt_event_type {
2827 MT_TYPE_LOCAL_NI = 0,
2831 struct lnet_mt_event_info {
2832 enum lnet_mt_event_type mt_type;
2836 /* called with res_lock held */
2838 lnet_detach_rsp_tracker(struct lnet_libmd *md, int cpt)
2840 struct lnet_rsp_tracker *rspt;
2843 * msg has a refcount on the MD so the MD is not going away.
2844 * The rspt queue for the cpt is protected by
2845 * the lnet_net_lock(cpt). cpt is the cpt of the MD cookie.
2847 if (!md->md_rspt_ptr)
2850 rspt = md->md_rspt_ptr;
2853 LASSERT(rspt->rspt_cpt == cpt);
2855 md->md_rspt_ptr = NULL;
2857 if (LNetMDHandleIsInvalid(rspt->rspt_mdh)) {
2859 * The monitor thread has invalidated this handle because the
2860 * response timed out, but it failed to lookup the MD. That
2861 * means this response tracker is on the zombie list. We can
2862 * safely remove it under the resource lock (held by caller) and
2863 * free the response tracker block.
2865 list_del(&rspt->rspt_on_list);
2866 lnet_rspt_free(rspt, cpt);
2869 * invalidate the handle to indicate that a response has been
2870 * received, which will then lead the monitor thread to clean up
2873 LNetInvalidateMDHandle(&rspt->rspt_mdh);
2878 lnet_clean_zombie_rstqs(void)
2880 struct lnet_rsp_tracker *rspt, *tmp;
2883 cfs_cpt_for_each(i, lnet_cpt_table()) {
2884 list_for_each_entry_safe(rspt, tmp,
2885 the_lnet.ln_mt_zombie_rstqs[i],
2887 list_del(&rspt->rspt_on_list);
2888 lnet_rspt_free(rspt, i);
2892 cfs_percpt_free(the_lnet.ln_mt_zombie_rstqs);
2896 lnet_finalize_expired_responses(void)
2898 struct lnet_libmd *md;
2899 struct lnet_rsp_tracker *rspt, *tmp;
2903 if (the_lnet.ln_mt_rstq == NULL)
2906 cfs_cpt_for_each(i, lnet_cpt_table()) {
2907 LIST_HEAD(local_queue);
2910 if (!the_lnet.ln_mt_rstq[i]) {
2914 list_splice_init(the_lnet.ln_mt_rstq[i], &local_queue);
2919 list_for_each_entry_safe(rspt, tmp, &local_queue, rspt_on_list) {
2921 * The rspt mdh will be invalidated when a response
2922 * is received or whenever we want to discard the
2923 * block the monitor thread will walk the queue
2924 * and clean up any rsts with an invalid mdh.
2925 * The monitor thread will walk the queue until
2926 * the first unexpired rspt block. This means that
2927 * some rspt blocks which received their
2928 * corresponding responses will linger in the
2929 * queue until they are cleaned up eventually.
2932 if (LNetMDHandleIsInvalid(rspt->rspt_mdh)) {
2934 list_del(&rspt->rspt_on_list);
2935 lnet_rspt_free(rspt, i);
2939 if (ktime_compare(now, rspt->rspt_deadline) >= 0 ||
2940 the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN) {
2941 struct lnet_peer_ni *lpni;
2944 md = lnet_handle2md(&rspt->rspt_mdh);
2946 /* MD has been queued for unlink, but
2947 * rspt hasn't been detached (Note we've
2948 * checked above that the rspt_mdh is
2949 * valid). Since we cannot lookup the MD
2950 * we're unable to detach the rspt
2951 * ourselves. Thus, move the rspt to the
2952 * zombie list where we'll wait for
2954 * 1. The remaining operations on the
2955 * MD to complete. In this case the
2956 * final operation will result in
2957 * lnet_msg_detach_md()->
2958 * lnet_detach_rsp_tracker() where
2959 * we will clean up this response
2961 * 2. LNet to shutdown. In this case
2962 * we'll wait until after all LND Nets
2963 * have shutdown and then we can
2964 * safely free any remaining response
2965 * tracker blocks on the zombie list.
2966 * Note: We need to hold the resource
2967 * lock when adding to the zombie list
2968 * because we may have concurrent access
2969 * with lnet_detach_rsp_tracker().
2971 LNetInvalidateMDHandle(&rspt->rspt_mdh);
2972 list_move(&rspt->rspt_on_list,
2973 the_lnet.ln_mt_zombie_rstqs[i]);
2977 LASSERT(md->md_rspt_ptr == rspt);
2978 md->md_rspt_ptr = NULL;
2981 LNetMDUnlink(rspt->rspt_mdh);
2983 nid = rspt->rspt_next_hop_nid;
2985 list_del(&rspt->rspt_on_list);
2986 lnet_rspt_free(rspt, i);
2988 /* If we're shutting down we just want to clean
2989 * up the rspt blocks
2991 if (the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN)
2995 the_lnet.ln_counters[i]->lct_health.lch_response_timeout_count++;
2999 "Response timeout: md = %p: nid = %s\n",
3000 md, libcfs_nid2str(nid));
3003 * If there is a timeout on the response
3004 * from the next hop decrement its health
3005 * value so that we don't use it
3008 lpni = lnet_find_peer_ni_locked(nid);
3010 lnet_handle_remote_failure_locked(lpni);
3011 lnet_peer_ni_decref_locked(lpni);
3020 if (!list_empty(&local_queue)) {
3022 list_splice(&local_queue, the_lnet.ln_mt_rstq[i]);
3029 lnet_resend_pending_msgs_locked(struct list_head *resendq, int cpt)
3031 struct lnet_msg *msg;
3033 while (!list_empty(resendq)) {
3034 struct lnet_peer_ni *lpni;
3036 msg = list_entry(resendq->next, struct lnet_msg,
3039 list_del_init(&msg->msg_list);
3041 lpni = lnet_find_peer_ni_locked(msg->msg_hdr.dest_nid);
3043 lnet_net_unlock(cpt);
3044 CERROR("Expected that a peer is already created for %s\n",
3045 libcfs_nid2str(msg->msg_hdr.dest_nid));
3046 msg->msg_no_resend = true;
3047 lnet_finalize(msg, -EFAULT);
3052 lnet_peer_ni_decref_locked(lpni);
3054 lnet_net_unlock(cpt);
3055 CDEBUG(D_NET, "resending %s->%s: %s recovery %d try# %d\n",
3056 libcfs_nid2str(msg->msg_src_nid_param),
3057 libcfs_id2str(msg->msg_target),
3058 lnet_msgtyp2str(msg->msg_type),
3060 msg->msg_retry_count);
3061 rc = lnet_send(msg->msg_src_nid_param, msg,
3062 msg->msg_rtr_nid_param);
3064 CERROR("Error sending %s to %s: %d\n",
3065 lnet_msgtyp2str(msg->msg_type),
3066 libcfs_id2str(msg->msg_target), rc);
3067 msg->msg_no_resend = true;
3068 lnet_finalize(msg, rc);
3072 the_lnet.ln_counters[cpt]->lct_health.lch_resend_count++;
3078 lnet_resend_pending_msgs(void)
3082 cfs_cpt_for_each(i, lnet_cpt_table()) {
3084 lnet_resend_pending_msgs_locked(the_lnet.ln_mt_resendqs[i], i);
3089 /* called with cpt and ni_lock held */
3091 lnet_unlink_ni_recovery_mdh_locked(struct lnet_ni *ni, int cpt, bool force)
3093 struct lnet_handle_md recovery_mdh;
3095 LNetInvalidateMDHandle(&recovery_mdh);
3097 if (ni->ni_recovery_state & LNET_NI_RECOVERY_PENDING ||
3099 recovery_mdh = ni->ni_ping_mdh;
3100 LNetInvalidateMDHandle(&ni->ni_ping_mdh);
3103 lnet_net_unlock(cpt);
3104 if (!LNetMDHandleIsInvalid(recovery_mdh))
3105 LNetMDUnlink(recovery_mdh);
3111 lnet_recover_local_nis(void)
3113 struct lnet_mt_event_info *ev_info;
3114 LIST_HEAD(processed_list);
3115 LIST_HEAD(local_queue);
3116 struct lnet_handle_md mdh;
3117 struct lnet_ni *tmp;
3124 * splice the recovery queue on a local queue. We will iterate
3125 * through the local queue and update it as needed. Once we're
3126 * done with the traversal, we'll splice the local queue back on
3127 * the head of the ln_mt_localNIRecovq. Any newly added local NIs
3128 * will be traversed in the next iteration.
3131 list_splice_init(&the_lnet.ln_mt_localNIRecovq,
3135 list_for_each_entry_safe(ni, tmp, &local_queue, ni_recovery) {
3137 * if an NI is being deleted or it is now healthy, there
3138 * is no need to keep it around in the recovery queue.
3139 * The monitor thread is the only thread responsible for
3140 * removing the NI from the recovery queue.
3141 * Multiple threads can be adding NIs to the recovery
3144 healthv = atomic_read(&ni->ni_healthv);
3148 if (ni->ni_state != LNET_NI_STATE_ACTIVE ||
3149 healthv == LNET_MAX_HEALTH_VALUE) {
3150 list_del_init(&ni->ni_recovery);
3151 lnet_unlink_ni_recovery_mdh_locked(ni, 0, false);
3153 lnet_ni_decref_locked(ni, 0);
3159 * if the local NI failed recovery we must unlink the md.
3160 * But we want to keep the local_ni on the recovery queue
3161 * so we can continue the attempts to recover it.
3163 if (ni->ni_recovery_state & LNET_NI_RECOVERY_FAILED) {
3164 lnet_unlink_ni_recovery_mdh_locked(ni, 0, true);
3165 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_FAILED;
3172 CDEBUG(D_NET, "attempting to recover local ni: %s\n",
3173 libcfs_nid2str(ni->ni_nid));
3176 if (!(ni->ni_recovery_state & LNET_NI_RECOVERY_PENDING)) {
3177 ni->ni_recovery_state |= LNET_NI_RECOVERY_PENDING;
3180 LIBCFS_ALLOC(ev_info, sizeof(*ev_info));
3182 CERROR("out of memory. Can't recover %s\n",
3183 libcfs_nid2str(ni->ni_nid));
3185 ni->ni_recovery_state &=
3186 ~LNET_NI_RECOVERY_PENDING;
3191 mdh = ni->ni_ping_mdh;
3193 * Invalidate the ni mdh in case it's deleted.
3194 * We'll unlink the mdh in this case below.
3196 LNetInvalidateMDHandle(&ni->ni_ping_mdh);
3200 * remove the NI from the local queue and drop the
3201 * reference count to it while we're recovering
3202 * it. The reason for that, is that the NI could
3203 * be deleted, and the way the code is structured
3204 * is if we don't drop the NI, then the deletion
3205 * code will enter a loop waiting for the
3206 * reference count to be removed while holding the
3207 * ln_mutex_lock(). When we look up the peer to
3208 * send to in lnet_select_pathway() we will try to
3209 * lock the ln_mutex_lock() as well, leading to