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_extract_iov(int dst_niov, struct kvec *dst,
343 int src_niov, struct kvec *src,
344 unsigned int offset, unsigned int len)
346 /* Initialise 'dst' to the subset of 'src' starting at 'offset',
347 * for exactly 'len' bytes, and return the number of entries.
348 * NB not destructive to 'src' */
349 unsigned int frag_len;
352 if (len == 0) /* no data => */
353 return (0); /* no frags */
355 LASSERT(src_niov > 0);
356 while (offset >= src->iov_len) { /* skip initial frags */
357 offset -= src->iov_len;
360 LASSERT(src_niov > 0);
365 LASSERT(src_niov > 0);
366 LASSERT((int)niov <= dst_niov);
368 frag_len = src->iov_len - offset;
369 dst->iov_base = ((char *)src->iov_base) + offset;
371 if (len <= frag_len) {
376 dst->iov_len = frag_len;
386 EXPORT_SYMBOL(lnet_extract_iov);
390 lnet_kiov_nob(unsigned int niov, lnet_kiov_t *kiov)
392 unsigned int nob = 0;
394 LASSERT(niov == 0 || kiov != NULL);
396 nob += (kiov++)->kiov_len;
400 EXPORT_SYMBOL(lnet_kiov_nob);
403 lnet_copy_kiov2kiov(unsigned int ndiov, lnet_kiov_t *diov, unsigned int doffset,
404 unsigned int nsiov, lnet_kiov_t *siov, unsigned int soffset,
407 /* NB diov, siov are READ-ONLY */
408 unsigned int this_nob;
415 LASSERT (!in_interrupt ());
418 while (doffset >= diov->kiov_len) {
419 doffset -= diov->kiov_len;
426 while (soffset >= siov->kiov_len) {
427 soffset -= siov->kiov_len;
436 this_nob = min3(diov->kiov_len - doffset,
437 siov->kiov_len - soffset,
441 daddr = ((char *)kmap(diov->kiov_page)) +
442 diov->kiov_offset + doffset;
444 saddr = ((char *)kmap(siov->kiov_page)) +
445 siov->kiov_offset + soffset;
447 /* Vanishing risk of kmap deadlock when mapping 2 pages.
448 * However in practice at least one of the kiovs will be mapped
449 * kernel pages and the map/unmap will be NOOPs */
451 memcpy (daddr, saddr, this_nob);
454 if (diov->kiov_len > doffset + this_nob) {
458 kunmap(diov->kiov_page);
465 if (siov->kiov_len > soffset + this_nob) {
469 kunmap(siov->kiov_page);
478 kunmap(diov->kiov_page);
480 kunmap(siov->kiov_page);
482 EXPORT_SYMBOL(lnet_copy_kiov2kiov);
485 lnet_copy_kiov2iov (unsigned int niov, struct kvec *iov, unsigned int iovoffset,
486 unsigned int nkiov, lnet_kiov_t *kiov, unsigned int kiovoffset,
489 /* NB iov, kiov are READ-ONLY */
490 unsigned int this_nob;
496 LASSERT (!in_interrupt ());
499 while (iovoffset >= iov->iov_len) {
500 iovoffset -= iov->iov_len;
507 while (kiovoffset >= kiov->kiov_len) {
508 kiovoffset -= kiov->kiov_len;
517 this_nob = min3((unsigned int)iov->iov_len - iovoffset,
518 (unsigned int)kiov->kiov_len - kiovoffset,
522 addr = ((char *)kmap(kiov->kiov_page)) +
523 kiov->kiov_offset + kiovoffset;
525 memcpy((char *)iov->iov_base + iovoffset, addr, this_nob);
528 if (iov->iov_len > iovoffset + this_nob) {
529 iovoffset += this_nob;
536 if (kiov->kiov_len > kiovoffset + this_nob) {
538 kiovoffset += this_nob;
540 kunmap(kiov->kiov_page);
550 kunmap(kiov->kiov_page);
552 EXPORT_SYMBOL(lnet_copy_kiov2iov);
555 lnet_copy_iov2kiov(unsigned int nkiov, lnet_kiov_t *kiov, unsigned int kiovoffset,
556 unsigned int niov, struct kvec *iov, unsigned int iovoffset,
559 /* NB kiov, iov are READ-ONLY */
560 unsigned int this_nob;
566 LASSERT (!in_interrupt ());
569 while (kiovoffset >= kiov->kiov_len) {
570 kiovoffset -= kiov->kiov_len;
577 while (iovoffset >= iov->iov_len) {
578 iovoffset -= iov->iov_len;
587 this_nob = min3((unsigned int)kiov->kiov_len - kiovoffset,
588 (unsigned int)iov->iov_len - iovoffset,
592 addr = ((char *)kmap(kiov->kiov_page)) +
593 kiov->kiov_offset + kiovoffset;
595 memcpy (addr, (char *)iov->iov_base + iovoffset, this_nob);
598 if (kiov->kiov_len > kiovoffset + this_nob) {
600 kiovoffset += this_nob;
602 kunmap(kiov->kiov_page);
609 if (iov->iov_len > iovoffset + this_nob) {
610 iovoffset += this_nob;
619 kunmap(kiov->kiov_page);
621 EXPORT_SYMBOL(lnet_copy_iov2kiov);
624 lnet_extract_kiov(int dst_niov, lnet_kiov_t *dst,
625 int src_niov, lnet_kiov_t *src,
626 unsigned int offset, unsigned int len)
628 /* Initialise 'dst' to the subset of 'src' starting at 'offset',
629 * for exactly 'len' bytes, and return the number of entries.
630 * NB not destructive to 'src' */
631 unsigned int frag_len;
634 if (len == 0) /* no data => */
635 return (0); /* no frags */
637 LASSERT(src_niov > 0);
638 while (offset >= src->kiov_len) { /* skip initial frags */
639 offset -= src->kiov_len;
642 LASSERT(src_niov > 0);
647 LASSERT(src_niov > 0);
648 LASSERT((int)niov <= dst_niov);
650 frag_len = src->kiov_len - offset;
651 dst->kiov_page = src->kiov_page;
652 dst->kiov_offset = src->kiov_offset + offset;
654 if (len <= frag_len) {
656 LASSERT(dst->kiov_offset + dst->kiov_len <= PAGE_SIZE);
660 dst->kiov_len = frag_len;
661 LASSERT(dst->kiov_offset + dst->kiov_len <= PAGE_SIZE);
671 EXPORT_SYMBOL(lnet_extract_kiov);
674 lnet_ni_recv(struct lnet_ni *ni, void *private, struct lnet_msg *msg,
675 int delayed, unsigned int offset, unsigned int mlen,
678 unsigned int niov = 0;
679 struct kvec *iov = NULL;
680 lnet_kiov_t *kiov = NULL;
683 LASSERT (!in_interrupt ());
684 LASSERT (mlen == 0 || msg != NULL);
687 LASSERT(msg->msg_receiving);
688 LASSERT(!msg->msg_sending);
689 LASSERT(rlen == msg->msg_len);
690 LASSERT(mlen <= msg->msg_len);
691 LASSERT(msg->msg_offset == offset);
692 LASSERT(msg->msg_wanted == mlen);
694 msg->msg_receiving = 0;
697 niov = msg->msg_niov;
699 kiov = msg->msg_kiov;
702 LASSERT ((iov == NULL) != (kiov == NULL));
706 rc = (ni->ni_net->net_lnd->lnd_recv)(ni, private, msg, delayed,
707 niov, iov, kiov, offset, mlen,
710 lnet_finalize(msg, rc);
714 lnet_setpayloadbuffer(struct lnet_msg *msg)
716 struct lnet_libmd *md = msg->msg_md;
718 LASSERT(msg->msg_len > 0);
719 LASSERT(!msg->msg_routing);
721 LASSERT(msg->msg_niov == 0);
722 LASSERT(msg->msg_iov == NULL);
723 LASSERT(msg->msg_kiov == NULL);
725 msg->msg_niov = md->md_niov;
726 if ((md->md_options & LNET_MD_KIOV) != 0)
727 msg->msg_kiov = md->md_iov.kiov;
729 msg->msg_iov = md->md_iov.iov;
733 lnet_prep_send(struct lnet_msg *msg, int type, struct lnet_process_id target,
734 unsigned int offset, unsigned int len)
736 msg->msg_type = type;
737 msg->msg_target = target;
739 msg->msg_offset = offset;
742 lnet_setpayloadbuffer(msg);
744 memset (&msg->msg_hdr, 0, sizeof (msg->msg_hdr));
745 msg->msg_hdr.type = cpu_to_le32(type);
746 /* dest_nid will be overwritten by lnet_select_pathway() */
747 msg->msg_hdr.dest_nid = cpu_to_le64(target.nid);
748 msg->msg_hdr.dest_pid = cpu_to_le32(target.pid);
749 /* src_nid will be set later */
750 msg->msg_hdr.src_pid = cpu_to_le32(the_lnet.ln_pid);
751 msg->msg_hdr.payload_length = cpu_to_le32(len);
755 lnet_ni_send(struct lnet_ni *ni, struct lnet_msg *msg)
757 void *priv = msg->msg_private;
760 LASSERT (!in_interrupt ());
761 LASSERT (LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND ||
762 (msg->msg_txcredit && msg->msg_peertxcredit));
764 rc = (ni->ni_net->net_lnd->lnd_send)(ni, priv, msg);
766 msg->msg_no_resend = true;
767 lnet_finalize(msg, rc);
772 lnet_ni_eager_recv(struct lnet_ni *ni, struct lnet_msg *msg)
776 LASSERT(!msg->msg_sending);
777 LASSERT(msg->msg_receiving);
778 LASSERT(!msg->msg_rx_ready_delay);
779 LASSERT(ni->ni_net->net_lnd->lnd_eager_recv != NULL);
781 msg->msg_rx_ready_delay = 1;
782 rc = (ni->ni_net->net_lnd->lnd_eager_recv)(ni, msg->msg_private, msg,
785 CERROR("recv from %s / send to %s aborted: "
786 "eager_recv failed %d\n",
787 libcfs_nid2str(msg->msg_rxpeer->lpni_nid),
788 libcfs_id2str(msg->msg_target), rc);
789 LASSERT(rc < 0); /* required by my callers */
796 lnet_is_peer_deadline_passed(struct lnet_peer_ni *lpni, time64_t now)
800 deadline = lpni->lpni_last_alive +
801 lpni->lpni_net->net_tunables.lct_peer_timeout;
804 * assume peer_ni is alive as long as we're within the configured
813 /* NB: returns 1 when alive, 0 when dead, negative when error;
814 * may drop the lnet_net_lock */
816 lnet_peer_alive_locked(struct lnet_ni *ni, struct lnet_peer_ni *lpni,
817 struct lnet_msg *msg)
819 time64_t now = ktime_get_seconds();
821 if (!lnet_peer_aliveness_enabled(lpni))
825 * If we're resending a message, let's attempt to send it even if
826 * the peer is down to fulfill our resend quota on the message
828 if (msg->msg_retry_count > 0)
831 /* try and send recovery messages irregardless */
832 if (msg->msg_recovery)
835 /* always send any responses */
836 if (lnet_msg_is_response(msg))
839 if (!lnet_is_peer_deadline_passed(lpni, now))
842 return lnet_is_peer_ni_alive(lpni);
846 * \param msg The message to be sent.
847 * \param do_send True if lnet_ni_send() should be called in this function.
848 * lnet_send() is going to lnet_net_unlock immediately after this, so
849 * it sets do_send FALSE and I don't do the unlock/send/lock bit.
851 * \retval LNET_CREDIT_OK If \a msg sent or OK to send.
852 * \retval LNET_CREDIT_WAIT If \a msg blocked for credit.
853 * \retval -EHOSTUNREACH If the next hop of the message appears dead.
854 * \retval -ECANCELED If the MD of the message has been unlinked.
857 lnet_post_send_locked(struct lnet_msg *msg, int do_send)
859 struct lnet_peer_ni *lp = msg->msg_txpeer;
860 struct lnet_ni *ni = msg->msg_txni;
861 int cpt = msg->msg_tx_cpt;
862 struct lnet_tx_queue *tq = ni->ni_tx_queues[cpt];
864 /* non-lnet_send() callers have checked before */
865 LASSERT(!do_send || msg->msg_tx_delayed);
866 LASSERT(!msg->msg_receiving);
867 LASSERT(msg->msg_tx_committed);
868 /* can't get here if we're sending to the loopback interface */
869 LASSERT(lp->lpni_nid != the_lnet.ln_loni->ni_nid);
871 /* NB 'lp' is always the next hop */
872 if ((msg->msg_target.pid & LNET_PID_USERFLAG) == 0 &&
873 lnet_peer_alive_locked(ni, lp, msg) == 0) {
874 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
875 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length +=
877 lnet_net_unlock(cpt);
879 lnet_incr_stats(&msg->msg_txpeer->lpni_stats,
881 LNET_STATS_TYPE_DROP);
883 lnet_incr_stats(&msg->msg_txni->ni_stats,
885 LNET_STATS_TYPE_DROP);
887 CNETERR("Dropping message for %s: peer not alive\n",
888 libcfs_id2str(msg->msg_target));
889 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_DROPPED;
891 lnet_finalize(msg, -EHOSTUNREACH);
894 return -EHOSTUNREACH;
897 if (msg->msg_md != NULL &&
898 (msg->msg_md->md_flags & LNET_MD_FLAG_ABORTED) != 0) {
899 lnet_net_unlock(cpt);
901 CNETERR("Aborting message for %s: LNetM[DE]Unlink() already "
902 "called on the MD/ME.\n",
903 libcfs_id2str(msg->msg_target));
905 msg->msg_no_resend = true;
906 CDEBUG(D_NET, "msg %p to %s canceled and will not be resent\n",
907 msg, libcfs_id2str(msg->msg_target));
908 lnet_finalize(msg, -ECANCELED);
915 if (!msg->msg_peertxcredit) {
916 spin_lock(&lp->lpni_lock);
917 LASSERT((lp->lpni_txcredits < 0) ==
918 !list_empty(&lp->lpni_txq));
920 msg->msg_peertxcredit = 1;
921 lp->lpni_txqnob += msg->msg_len + sizeof(struct lnet_hdr);
922 lp->lpni_txcredits--;
924 if (lp->lpni_txcredits < lp->lpni_mintxcredits)
925 lp->lpni_mintxcredits = lp->lpni_txcredits;
927 if (lp->lpni_txcredits < 0) {
928 msg->msg_tx_delayed = 1;
929 list_add_tail(&msg->msg_list, &lp->lpni_txq);
930 spin_unlock(&lp->lpni_lock);
931 return LNET_CREDIT_WAIT;
933 spin_unlock(&lp->lpni_lock);
936 if (!msg->msg_txcredit) {
937 LASSERT((tq->tq_credits < 0) ==
938 !list_empty(&tq->tq_delayed));
940 msg->msg_txcredit = 1;
942 atomic_dec(&ni->ni_tx_credits);
944 if (tq->tq_credits < tq->tq_credits_min)
945 tq->tq_credits_min = tq->tq_credits;
947 if (tq->tq_credits < 0) {
948 msg->msg_tx_delayed = 1;
949 list_add_tail(&msg->msg_list, &tq->tq_delayed);
950 return LNET_CREDIT_WAIT;
954 /* unset the tx_delay flag as we're going to send it now */
955 msg->msg_tx_delayed = 0;
958 lnet_net_unlock(cpt);
959 lnet_ni_send(ni, msg);
962 return LNET_CREDIT_OK;
966 static struct lnet_rtrbufpool *
967 lnet_msg2bufpool(struct lnet_msg *msg)
969 struct lnet_rtrbufpool *rbp;
972 LASSERT(msg->msg_rx_committed);
974 cpt = msg->msg_rx_cpt;
975 rbp = &the_lnet.ln_rtrpools[cpt][0];
977 LASSERT(msg->msg_len <= LNET_MTU);
978 while (msg->msg_len > (unsigned int)rbp->rbp_npages * PAGE_SIZE) {
980 LASSERT(rbp < &the_lnet.ln_rtrpools[cpt][LNET_NRBPOOLS]);
987 lnet_post_routed_recv_locked(struct lnet_msg *msg, int do_recv)
989 /* lnet_parse is going to lnet_net_unlock immediately after this, so it
990 * sets do_recv FALSE and I don't do the unlock/send/lock bit.
991 * I return LNET_CREDIT_WAIT if msg blocked and LNET_CREDIT_OK if
992 * received or OK to receive */
993 struct lnet_peer_ni *lpni = msg->msg_rxpeer;
994 struct lnet_peer *lp;
995 struct lnet_rtrbufpool *rbp;
996 struct lnet_rtrbuf *rb;
998 LASSERT(msg->msg_iov == NULL);
999 LASSERT(msg->msg_kiov == NULL);
1000 LASSERT(msg->msg_niov == 0);
1001 LASSERT(msg->msg_routing);
1002 LASSERT(msg->msg_receiving);
1003 LASSERT(!msg->msg_sending);
1004 LASSERT(lpni->lpni_peer_net);
1005 LASSERT(lpni->lpni_peer_net->lpn_peer);
1007 lp = lpni->lpni_peer_net->lpn_peer;
1009 /* non-lnet_parse callers only receive delayed messages */
1010 LASSERT(!do_recv || msg->msg_rx_delayed);
1012 if (!msg->msg_peerrtrcredit) {
1013 /* lpni_lock protects the credit manipulation */
1014 spin_lock(&lpni->lpni_lock);
1015 /* lp_lock protects the lp_rtrq */
1016 spin_lock(&lp->lp_lock);
1018 msg->msg_peerrtrcredit = 1;
1019 lpni->lpni_rtrcredits--;
1020 if (lpni->lpni_rtrcredits < lpni->lpni_minrtrcredits)
1021 lpni->lpni_minrtrcredits = lpni->lpni_rtrcredits;
1023 if (lpni->lpni_rtrcredits < 0) {
1024 /* must have checked eager_recv before here */
1025 LASSERT(msg->msg_rx_ready_delay);
1026 msg->msg_rx_delayed = 1;
1027 list_add_tail(&msg->msg_list, &lp->lp_rtrq);
1028 spin_unlock(&lp->lp_lock);
1029 spin_unlock(&lpni->lpni_lock);
1030 return LNET_CREDIT_WAIT;
1032 spin_unlock(&lp->lp_lock);
1033 spin_unlock(&lpni->lpni_lock);
1036 rbp = lnet_msg2bufpool(msg);
1038 if (!msg->msg_rtrcredit) {
1039 msg->msg_rtrcredit = 1;
1041 if (rbp->rbp_credits < rbp->rbp_mincredits)
1042 rbp->rbp_mincredits = rbp->rbp_credits;
1044 if (rbp->rbp_credits < 0) {
1045 /* must have checked eager_recv before here */
1046 LASSERT(msg->msg_rx_ready_delay);
1047 msg->msg_rx_delayed = 1;
1048 list_add_tail(&msg->msg_list, &rbp->rbp_msgs);
1049 return LNET_CREDIT_WAIT;
1053 LASSERT(!list_empty(&rbp->rbp_bufs));
1054 rb = list_entry(rbp->rbp_bufs.next, struct lnet_rtrbuf, rb_list);
1055 list_del(&rb->rb_list);
1057 msg->msg_niov = rbp->rbp_npages;
1058 msg->msg_kiov = &rb->rb_kiov[0];
1060 /* unset the msg-rx_delayed flag since we're receiving the message */
1061 msg->msg_rx_delayed = 0;
1064 int cpt = msg->msg_rx_cpt;
1066 lnet_net_unlock(cpt);
1067 lnet_ni_recv(msg->msg_rxni, msg->msg_private, msg, 1,
1068 0, msg->msg_len, msg->msg_len);
1071 return LNET_CREDIT_OK;
1075 lnet_return_tx_credits_locked(struct lnet_msg *msg)
1077 struct lnet_peer_ni *txpeer = msg->msg_txpeer;
1078 struct lnet_ni *txni = msg->msg_txni;
1079 struct lnet_msg *msg2;
1081 if (msg->msg_txcredit) {
1082 struct lnet_ni *ni = msg->msg_txni;
1083 struct lnet_tx_queue *tq = ni->ni_tx_queues[msg->msg_tx_cpt];
1085 /* give back NI txcredits */
1086 msg->msg_txcredit = 0;
1088 LASSERT((tq->tq_credits < 0) ==
1089 !list_empty(&tq->tq_delayed));
1092 atomic_inc(&ni->ni_tx_credits);
1093 if (tq->tq_credits <= 0) {
1094 msg2 = list_entry(tq->tq_delayed.next,
1095 struct lnet_msg, msg_list);
1096 list_del(&msg2->msg_list);
1098 LASSERT(msg2->msg_txni == ni);
1099 LASSERT(msg2->msg_tx_delayed);
1100 LASSERT(msg2->msg_tx_cpt == msg->msg_tx_cpt);
1102 (void) lnet_post_send_locked(msg2, 1);
1106 if (msg->msg_peertxcredit) {
1107 /* give back peer txcredits */
1108 msg->msg_peertxcredit = 0;
1110 spin_lock(&txpeer->lpni_lock);
1111 LASSERT((txpeer->lpni_txcredits < 0) ==
1112 !list_empty(&txpeer->lpni_txq));
1114 txpeer->lpni_txqnob -= msg->msg_len + sizeof(struct lnet_hdr);
1115 LASSERT(txpeer->lpni_txqnob >= 0);
1117 txpeer->lpni_txcredits++;
1118 if (txpeer->lpni_txcredits <= 0) {
1121 msg2 = list_entry(txpeer->lpni_txq.next,
1122 struct lnet_msg, msg_list);
1123 list_del(&msg2->msg_list);
1124 spin_unlock(&txpeer->lpni_lock);
1126 LASSERT(msg2->msg_txpeer == txpeer);
1127 LASSERT(msg2->msg_tx_delayed);
1129 msg2_cpt = msg2->msg_tx_cpt;
1132 * The msg_cpt can be different from the msg2_cpt
1133 * so we need to make sure we lock the correct cpt
1135 * Once we call lnet_post_send_locked() it is no
1136 * longer safe to access msg2, since it could've
1137 * been freed by lnet_finalize(), but we still
1138 * need to relock the correct cpt, so we cache the
1139 * msg2_cpt for the purpose of the check that
1140 * follows the call to lnet_pose_send_locked().
1142 if (msg2_cpt != msg->msg_tx_cpt) {
1143 lnet_net_unlock(msg->msg_tx_cpt);
1144 lnet_net_lock(msg2_cpt);
1146 (void) lnet_post_send_locked(msg2, 1);
1147 if (msg2_cpt != msg->msg_tx_cpt) {
1148 lnet_net_unlock(msg2_cpt);
1149 lnet_net_lock(msg->msg_tx_cpt);
1152 spin_unlock(&txpeer->lpni_lock);
1157 msg->msg_txni = NULL;
1158 lnet_ni_decref_locked(txni, msg->msg_tx_cpt);
1161 if (txpeer != NULL) {
1162 msg->msg_txpeer = NULL;
1163 lnet_peer_ni_decref_locked(txpeer);
1168 lnet_schedule_blocked_locked(struct lnet_rtrbufpool *rbp)
1170 struct lnet_msg *msg;
1172 if (list_empty(&rbp->rbp_msgs))
1174 msg = list_entry(rbp->rbp_msgs.next,
1175 struct lnet_msg, msg_list);
1176 list_del(&msg->msg_list);
1178 (void)lnet_post_routed_recv_locked(msg, 1);
1182 lnet_drop_routed_msgs_locked(struct list_head *list, int cpt)
1184 struct lnet_msg *msg;
1185 struct lnet_msg *tmp;
1187 lnet_net_unlock(cpt);
1189 list_for_each_entry_safe(msg, tmp, list, msg_list) {
1190 lnet_ni_recv(msg->msg_rxni, msg->msg_private, NULL,
1191 0, 0, 0, msg->msg_hdr.payload_length);
1192 list_del_init(&msg->msg_list);
1193 msg->msg_no_resend = true;
1194 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_ERROR;
1195 lnet_finalize(msg, -ECANCELED);
1202 lnet_return_rx_credits_locked(struct lnet_msg *msg)
1204 struct lnet_peer_ni *rxpeerni = msg->msg_rxpeer;
1205 struct lnet_peer *lp;
1206 struct lnet_ni *rxni = msg->msg_rxni;
1207 struct lnet_msg *msg2;
1209 if (msg->msg_rtrcredit) {
1210 /* give back global router credits */
1211 struct lnet_rtrbuf *rb;
1212 struct lnet_rtrbufpool *rbp;
1214 /* NB If a msg ever blocks for a buffer in rbp_msgs, it stays
1215 * there until it gets one allocated, or aborts the wait
1217 LASSERT(msg->msg_kiov != NULL);
1219 rb = list_entry(msg->msg_kiov, struct lnet_rtrbuf, rb_kiov[0]);
1222 msg->msg_kiov = NULL;
1223 msg->msg_rtrcredit = 0;
1225 LASSERT(rbp == lnet_msg2bufpool(msg));
1227 LASSERT((rbp->rbp_credits > 0) ==
1228 !list_empty(&rbp->rbp_bufs));
1230 /* If routing is now turned off, we just drop this buffer and
1231 * don't bother trying to return credits. */
1232 if (!the_lnet.ln_routing) {
1233 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1237 /* It is possible that a user has lowered the desired number of
1238 * buffers in this pool. Make sure we never put back
1239 * more buffers than the stated number. */
1240 if (unlikely(rbp->rbp_credits >= rbp->rbp_req_nbuffers)) {
1241 /* Discard this buffer so we don't have too
1243 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1244 rbp->rbp_nbuffers--;
1246 list_add(&rb->rb_list, &rbp->rbp_bufs);
1248 if (rbp->rbp_credits <= 0)
1249 lnet_schedule_blocked_locked(rbp);
1254 if (msg->msg_peerrtrcredit) {
1256 LASSERT(rxpeerni->lpni_peer_net);
1257 LASSERT(rxpeerni->lpni_peer_net->lpn_peer);
1259 lp = rxpeerni->lpni_peer_net->lpn_peer;
1261 /* give back peer router credits */
1262 msg->msg_peerrtrcredit = 0;
1264 spin_lock(&rxpeerni->lpni_lock);
1265 spin_lock(&lp->lp_lock);
1267 rxpeerni->lpni_rtrcredits++;
1269 /* drop all messages which are queued to be routed on that
1271 if (!the_lnet.ln_routing) {
1273 list_splice_init(&lp->lp_rtrq, &drop);
1274 spin_unlock(&lp->lp_lock);
1275 spin_unlock(&rxpeerni->lpni_lock);
1276 lnet_drop_routed_msgs_locked(&drop, msg->msg_rx_cpt);
1277 } else if (!list_empty(&lp->lp_rtrq)) {
1280 msg2 = list_entry(lp->lp_rtrq.next,
1281 struct lnet_msg, msg_list);
1282 list_del(&msg2->msg_list);
1283 msg2_cpt = msg2->msg_rx_cpt;
1284 spin_unlock(&lp->lp_lock);
1285 spin_unlock(&rxpeerni->lpni_lock);
1287 * messages on the lp_rtrq can be from any NID in
1288 * the peer, which means they might have different
1289 * cpts. We need to make sure we lock the right
1292 if (msg2_cpt != msg->msg_rx_cpt) {
1293 lnet_net_unlock(msg->msg_rx_cpt);
1294 lnet_net_lock(msg2_cpt);
1296 (void) lnet_post_routed_recv_locked(msg2, 1);
1297 if (msg2_cpt != msg->msg_rx_cpt) {
1298 lnet_net_unlock(msg2_cpt);
1299 lnet_net_lock(msg->msg_rx_cpt);
1302 spin_unlock(&lp->lp_lock);
1303 spin_unlock(&rxpeerni->lpni_lock);
1307 msg->msg_rxni = NULL;
1308 lnet_ni_decref_locked(rxni, msg->msg_rx_cpt);
1310 if (rxpeerni != NULL) {
1311 msg->msg_rxpeer = NULL;
1312 lnet_peer_ni_decref_locked(rxpeerni);
1317 lnet_compare_gw_lpnis(struct lnet_peer_ni *p1, struct lnet_peer_ni *p2)
1319 if (p1->lpni_txqnob < p2->lpni_txqnob)
1322 if (p1->lpni_txqnob > p2->lpni_txqnob)
1325 if (p1->lpni_txcredits > p2->lpni_txcredits)
1328 if (p1->lpni_txcredits < p2->lpni_txcredits)
1334 static struct lnet_peer_ni *
1335 lnet_select_peer_ni(struct lnet_ni *best_ni, lnet_nid_t dst_nid,
1336 struct lnet_peer *peer,
1337 struct lnet_peer_net *peer_net)
1340 * Look at the peer NIs for the destination peer that connect
1341 * to the chosen net. If a peer_ni is preferred when using the
1342 * best_ni to communicate, we use that one. If there is no
1343 * preferred peer_ni, or there are multiple preferred peer_ni,
1344 * the available transmit credits are used. If the transmit
1345 * credits are equal, we round-robin over the peer_ni.
1347 struct lnet_peer_ni *lpni = NULL;
1348 struct lnet_peer_ni *best_lpni = NULL;
1349 int best_lpni_credits = INT_MIN;
1350 bool preferred = false;
1352 int best_lpni_healthv = 0;
1355 while ((lpni = lnet_get_next_peer_ni_locked(peer, peer_net, lpni))) {
1357 * if the best_ni we've chosen aleady has this lpni
1358 * preferred, then let's use it
1361 ni_is_pref = lnet_peer_is_pref_nid_locked(lpni,
1363 CDEBUG(D_NET, "%s ni_is_pref = %d\n",
1364 libcfs_nid2str(best_ni->ni_nid), ni_is_pref);
1369 lpni_healthv = atomic_read(&lpni->lpni_healthv);
1372 CDEBUG(D_NET, "%s c:[%d, %d], s:[%d, %d]\n",
1373 libcfs_nid2str(lpni->lpni_nid),
1374 lpni->lpni_txcredits, best_lpni_credits,
1375 lpni->lpni_seq, best_lpni->lpni_seq);
1377 /* pick the healthiest peer ni */
1378 if (lpni_healthv < best_lpni_healthv) {
1380 } else if (lpni_healthv > best_lpni_healthv) {
1381 best_lpni_healthv = lpni_healthv;
1382 /* if this is a preferred peer use it */
1383 } else if (!preferred && ni_is_pref) {
1385 } else if (preferred && !ni_is_pref) {
1387 * this is not the preferred peer so let's ignore
1391 } else if (lpni->lpni_txcredits < best_lpni_credits) {
1393 * We already have a peer that has more credits
1394 * available than this one. No need to consider
1395 * this peer further.
1398 } else if (lpni->lpni_txcredits == best_lpni_credits) {
1400 * The best peer found so far and the current peer
1401 * have the same number of available credits let's
1402 * make sure to select between them using Round
1406 if (best_lpni->lpni_seq <= lpni->lpni_seq)
1412 best_lpni_credits = lpni->lpni_txcredits;
1415 /* if we still can't find a peer ni then we can't reach it */
1417 __u32 net_id = (peer_net) ? peer_net->lpn_net_id :
1418 LNET_NIDNET(dst_nid);
1419 CDEBUG(D_NET, "no peer_ni found on peer net %s\n",
1420 libcfs_net2str(net_id));
1424 CDEBUG(D_NET, "sd_best_lpni = %s\n",
1425 libcfs_nid2str(best_lpni->lpni_nid));
1431 * Prerequisite: the best_ni should already be set in the sd
1433 static inline struct lnet_peer_ni *
1434 lnet_find_best_lpni_on_net(struct lnet_ni *lni, lnet_nid_t dst_nid,
1435 struct lnet_peer *peer, __u32 net_id)
1437 struct lnet_peer_net *peer_net;
1440 * The gateway is Multi-Rail capable so now we must select the
1443 peer_net = lnet_peer_get_net_locked(peer, net_id);
1446 CERROR("gateway peer %s has no NI on net %s\n",
1447 libcfs_nid2str(peer->lp_primary_nid),
1448 libcfs_net2str(net_id));
1452 return lnet_select_peer_ni(lni, dst_nid, peer, peer_net);
1455 /* Compare route priorities and hop counts */
1457 lnet_compare_routes(struct lnet_route *r1, struct lnet_route *r2)
1459 int r1_hops = (r1->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r1->lr_hops;
1460 int r2_hops = (r2->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r2->lr_hops;
1462 if (r1->lr_priority < r2->lr_priority)
1465 if (r1->lr_priority > r2->lr_priority)
1468 if (r1_hops < r2_hops)
1471 if (r1_hops > r2_hops)
1477 static struct lnet_route *
1478 lnet_find_route_locked(struct lnet_remotenet *rnet, __u32 src_net,
1479 struct lnet_route **prev_route,
1480 struct lnet_peer_ni **gwni)
1482 struct lnet_peer_ni *lpni, *best_gw_ni = NULL;
1483 struct lnet_route *best_route;
1484 struct lnet_route *last_route;
1485 struct lnet_route *route;
1488 __u32 any_net = LNET_NIDNET(LNET_NID_ANY);
1490 best_route = last_route = NULL;
1491 list_for_each_entry(route, &rnet->lrn_routes, lr_list) {
1492 if (!lnet_is_route_alive(route))
1495 /* If the src_net is specified then we need to find an lpni
1498 restrict_net = src_net == any_net ? route->lr_lnet : src_net;
1500 lpni = lnet_find_best_lpni_on_net(NULL, LNET_NID_ANY,
1504 best_route = last_route = route;
1507 CERROR("Gateway %s does not have a peer NI on net %s\n",
1508 libcfs_nid2str(route->lr_gateway->lp_primary_nid),
1509 libcfs_net2str(restrict_net));
1514 /* no protection on below fields, but it's harmless */
1515 if (last_route->lr_seq - route->lr_seq < 0)
1518 rc = lnet_compare_routes(route, best_route);
1522 lpni = lnet_find_best_lpni_on_net(NULL, LNET_NID_ANY,
1526 CERROR("Gateway %s does not have a peer NI on net %s\n",
1527 libcfs_nid2str(route->lr_gateway->lp_primary_nid),
1528 libcfs_net2str(restrict_net));
1538 rc = lnet_compare_gw_lpnis(lpni, best_gw_ni);
1542 if (rc == 1 || route->lr_seq <= best_route->lr_seq) {
1549 *prev_route = last_route;
1555 static struct lnet_ni *
1556 lnet_get_best_ni(struct lnet_net *local_net, struct lnet_ni *best_ni,
1557 struct lnet_peer *peer, struct lnet_peer_net *peer_net,
1560 struct lnet_ni *ni = NULL;
1561 unsigned int shortest_distance;
1566 * If there is no peer_ni that we can send to on this network,
1567 * then there is no point in looking for a new best_ni here.
1569 if (!lnet_get_next_peer_ni_locked(peer, peer_net, NULL))
1572 if (best_ni == NULL) {
1573 shortest_distance = UINT_MAX;
1574 best_credits = INT_MIN;
1577 shortest_distance = cfs_cpt_distance(lnet_cpt_table(), md_cpt,
1578 best_ni->ni_dev_cpt);
1579 best_credits = atomic_read(&best_ni->ni_tx_credits);
1580 best_healthv = atomic_read(&best_ni->ni_healthv);
1583 while ((ni = lnet_get_next_ni_locked(local_net, ni))) {
1584 unsigned int distance;
1589 ni_credits = atomic_read(&ni->ni_tx_credits);
1590 ni_healthv = atomic_read(&ni->ni_healthv);
1591 ni_fatal = atomic_read(&ni->ni_fatal_error_on);
1594 * calculate the distance from the CPT on which
1595 * the message memory is allocated to the CPT of
1596 * the NI's physical device
1598 distance = cfs_cpt_distance(lnet_cpt_table(),
1602 CDEBUG(D_NET, "compare ni %s [c:%d, d:%d, s:%d] with best_ni %s [c:%d, d:%d, s:%d]\n",
1603 libcfs_nid2str(ni->ni_nid), ni_credits, distance,
1604 ni->ni_seq, (best_ni) ? libcfs_nid2str(best_ni->ni_nid)
1605 : "not seleced", best_credits, shortest_distance,
1606 (best_ni) ? best_ni->ni_seq : 0);
1609 * All distances smaller than the NUMA range
1610 * are treated equally.
1612 if (distance < lnet_numa_range)
1613 distance = lnet_numa_range;
1616 * Select on health, shorter distance, available
1617 * credits, then round-robin.
1621 } else if (ni_healthv < best_healthv) {
1623 } else if (ni_healthv > best_healthv) {
1624 best_healthv = ni_healthv;
1626 * If we're going to prefer this ni because it's
1627 * the healthiest, then we should set the
1628 * shortest_distance in the algorithm in case
1629 * there are multiple NIs with the same health but
1630 * different distances.
1632 if (distance < shortest_distance)
1633 shortest_distance = distance;
1634 } else if (distance > shortest_distance) {
1636 } else if (distance < shortest_distance) {
1637 shortest_distance = distance;
1638 } else if (ni_credits < best_credits) {
1640 } else if (ni_credits == best_credits) {
1641 if (best_ni && best_ni->ni_seq <= ni->ni_seq)
1645 best_credits = ni_credits;
1648 CDEBUG(D_NET, "selected best_ni %s\n",
1649 (best_ni) ? libcfs_nid2str(best_ni->ni_nid) : "no selection");
1655 * Traffic to the LNET_RESERVED_PORTAL may not trigger peer discovery,
1656 * because such traffic is required to perform discovery. We therefore
1657 * exclude all GET and PUT on that portal. We also exclude all ACK and
1658 * REPLY traffic, but that is because the portal is not tracked in the
1659 * message structure for these message types. We could restrict this
1660 * further by also checking for LNET_PROTO_PING_MATCHBITS.
1663 lnet_msg_discovery(struct lnet_msg *msg)
1665 if (msg->msg_type == LNET_MSG_PUT) {
1666 if (msg->msg_hdr.msg.put.ptl_index != LNET_RESERVED_PORTAL)
1668 } else if (msg->msg_type == LNET_MSG_GET) {
1669 if (msg->msg_hdr.msg.get.ptl_index != LNET_RESERVED_PORTAL)
1675 #define SRC_SPEC 0x0001
1676 #define SRC_ANY 0x0002
1677 #define LOCAL_DST 0x0004
1678 #define REMOTE_DST 0x0008
1679 #define MR_DST 0x0010
1680 #define NMR_DST 0x0020
1681 #define SND_RESP 0x0040
1683 /* The following to defines are used for return codes */
1684 #define REPEAT_SEND 0x1000
1685 #define PASS_THROUGH 0x2000
1687 /* The different cases lnet_select pathway needs to handle */
1688 #define SRC_SPEC_LOCAL_MR_DST (SRC_SPEC | LOCAL_DST | MR_DST)
1689 #define SRC_SPEC_ROUTER_MR_DST (SRC_SPEC | REMOTE_DST | MR_DST)
1690 #define SRC_SPEC_LOCAL_NMR_DST (SRC_SPEC | LOCAL_DST | NMR_DST)
1691 #define SRC_SPEC_ROUTER_NMR_DST (SRC_SPEC | REMOTE_DST | NMR_DST)
1692 #define SRC_ANY_LOCAL_MR_DST (SRC_ANY | LOCAL_DST | MR_DST)
1693 #define SRC_ANY_ROUTER_MR_DST (SRC_ANY | REMOTE_DST | MR_DST)
1694 #define SRC_ANY_LOCAL_NMR_DST (SRC_ANY | LOCAL_DST | NMR_DST)
1695 #define SRC_ANY_ROUTER_NMR_DST (SRC_ANY | REMOTE_DST | NMR_DST)
1698 lnet_handle_lo_send(struct lnet_send_data *sd)
1700 struct lnet_msg *msg = sd->sd_msg;
1701 int cpt = sd->sd_cpt;
1703 /* No send credit hassles with LOLND */
1704 lnet_ni_addref_locked(the_lnet.ln_loni, cpt);
1705 msg->msg_hdr.dest_nid = cpu_to_le64(the_lnet.ln_loni->ni_nid);
1706 if (!msg->msg_routing)
1707 msg->msg_hdr.src_nid =
1708 cpu_to_le64(the_lnet.ln_loni->ni_nid);
1709 msg->msg_target.nid = the_lnet.ln_loni->ni_nid;
1710 lnet_msg_commit(msg, cpt);
1711 msg->msg_txni = the_lnet.ln_loni;
1713 return LNET_CREDIT_OK;
1717 lnet_handle_send(struct lnet_send_data *sd)
1719 struct lnet_ni *best_ni = sd->sd_best_ni;
1720 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
1721 struct lnet_peer_ni *final_dst_lpni = sd->sd_final_dst_lpni;
1722 struct lnet_msg *msg = sd->sd_msg;
1724 __u32 send_case = sd->sd_send_case;
1726 __u32 routing = send_case & REMOTE_DST;
1727 struct lnet_rsp_tracker *rspt;
1730 * Increment sequence number of the selected peer so that we
1731 * pick the next one in Round Robin.
1733 best_lpni->lpni_seq++;
1736 * grab a reference on the peer_ni so it sticks around even if
1737 * we need to drop and relock the lnet_net_lock below.
1739 lnet_peer_ni_addref_locked(best_lpni);
1742 * Use lnet_cpt_of_nid() to determine the CPT used to commit the
1743 * message. This ensures that we get a CPT that is correct for
1744 * the NI when the NI has been restricted to a subset of all CPTs.
1745 * If the selected CPT differs from the one currently locked, we
1746 * must unlock and relock the lnet_net_lock(), and then check whether
1747 * the configuration has changed. We don't have a hold on the best_ni
1748 * yet, and it may have vanished.
1750 cpt2 = lnet_cpt_of_nid_locked(best_lpni->lpni_nid, best_ni);
1751 if (sd->sd_cpt != cpt2) {
1752 __u32 seq = lnet_get_dlc_seq_locked();
1753 lnet_net_unlock(sd->sd_cpt);
1755 lnet_net_lock(sd->sd_cpt);
1756 if (seq != lnet_get_dlc_seq_locked()) {
1757 lnet_peer_ni_decref_locked(best_lpni);
1763 * store the best_lpni in the message right away to avoid having
1764 * to do the same operation under different conditions
1766 msg->msg_txpeer = best_lpni;
1767 msg->msg_txni = best_ni;
1770 * grab a reference for the best_ni since now it's in use in this
1771 * send. The reference will be dropped in lnet_finalize()
1773 lnet_ni_addref_locked(msg->msg_txni, sd->sd_cpt);
1776 * Always set the target.nid to the best peer picked. Either the
1777 * NID will be one of the peer NIDs selected, or the same NID as
1778 * what was originally set in the target or it will be the NID of
1779 * a router if this message should be routed
1781 msg->msg_target.nid = msg->msg_txpeer->lpni_nid;
1784 * lnet_msg_commit assigns the correct cpt to the message, which
1785 * is used to decrement the correct refcount on the ni when it's
1786 * time to return the credits
1788 lnet_msg_commit(msg, sd->sd_cpt);
1791 * If we are routing the message then we keep the src_nid that was
1792 * set by the originator. If we are not routing then we are the
1793 * originator and set it here.
1795 if (!msg->msg_routing)
1796 msg->msg_hdr.src_nid = cpu_to_le64(msg->msg_txni->ni_nid);
1799 msg->msg_target_is_router = 1;
1800 msg->msg_target.pid = LNET_PID_LUSTRE;
1802 * since we're routing we want to ensure that the
1803 * msg_hdr.dest_nid is set to the final destination. When
1804 * the router receives this message it knows how to route
1807 * final_dst_lpni is set at the beginning of the
1808 * lnet_select_pathway() function and is never changed.
1809 * It's safe to use it here.
1811 msg->msg_hdr.dest_nid = cpu_to_le64(final_dst_lpni->lpni_nid);
1814 * if we're not routing set the dest_nid to the best peer
1815 * ni NID that we picked earlier in the algorithm.
1817 msg->msg_hdr.dest_nid = cpu_to_le64(msg->msg_txpeer->lpni_nid);
1821 * if we have response tracker block update it with the next hop
1825 rspt = msg->msg_md->md_rspt_ptr;
1827 rspt->rspt_next_hop_nid = msg->msg_txpeer->lpni_nid;
1828 CDEBUG(D_NET, "rspt_next_hop_nid = %s\n",
1829 libcfs_nid2str(rspt->rspt_next_hop_nid));
1833 rc = lnet_post_send_locked(msg, 0);
1836 CDEBUG(D_NET, "TRACE: %s(%s:%s) -> %s(%s:%s) %s : %s try# %d\n",
1837 libcfs_nid2str(msg->msg_hdr.src_nid),
1838 libcfs_nid2str(msg->msg_txni->ni_nid),
1839 libcfs_nid2str(sd->sd_src_nid),
1840 libcfs_nid2str(msg->msg_hdr.dest_nid),
1841 libcfs_nid2str(sd->sd_dst_nid),
1842 libcfs_nid2str(msg->msg_txpeer->lpni_nid),
1843 libcfs_nid2str(sd->sd_rtr_nid),
1844 lnet_msgtyp2str(msg->msg_type), msg->msg_retry_count);
1850 lnet_set_non_mr_pref_nid(struct lnet_peer_ni *lpni, struct lnet_ni *lni,
1851 struct lnet_msg *msg)
1853 if (!lnet_msg_is_response(msg) && lpni->lpni_pref_nnids == 0) {
1854 CDEBUG(D_NET, "Setting preferred local NID %s on NMR peer %s\n",
1855 libcfs_nid2str(lni->ni_nid),
1856 libcfs_nid2str(lpni->lpni_nid));
1857 lnet_peer_ni_set_non_mr_pref_nid(lpni, lni->ni_nid);
1866 * use the source and destination NIDs as the pathway
1869 lnet_handle_spec_local_nmr_dst(struct lnet_send_data *sd)
1871 /* the destination lpni is set before we get here. */
1874 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
1875 if (!sd->sd_best_ni) {
1876 CERROR("Can't send to %s: src %s is not a "
1877 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
1878 libcfs_nid2str(sd->sd_src_nid));
1882 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
1884 return lnet_handle_send(sd);
1892 * Don't run the selection algorithm on the peer NIs. By specifying the
1893 * local NID, we're also saying that we should always use the destination NID
1894 * provided. This handles the case where we should be using the same
1895 * destination NID for the all the messages which belong to the same RPC
1899 lnet_handle_spec_local_mr_dst(struct lnet_send_data *sd)
1901 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
1902 if (!sd->sd_best_ni) {
1903 CERROR("Can't send to %s: src %s is not a "
1904 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
1905 libcfs_nid2str(sd->sd_src_nid));
1909 if (sd->sd_best_lpni &&
1910 sd->sd_best_lpni->lpni_nid == the_lnet.ln_loni->ni_nid)
1911 return lnet_handle_lo_send(sd);
1912 else if (sd->sd_best_lpni)
1913 return lnet_handle_send(sd);
1915 CERROR("can't send to %s. no NI on %s\n",
1916 libcfs_nid2str(sd->sd_dst_nid),
1917 libcfs_net2str(sd->sd_best_ni->ni_net->net_id));
1919 return -EHOSTUNREACH;
1923 lnet_find_best_ni_on_spec_net(struct lnet_ni *cur_best_ni,
1924 struct lnet_peer *peer,
1925 struct lnet_peer_net *peer_net,
1929 struct lnet_net *local_net;
1930 struct lnet_ni *best_ni;
1932 local_net = lnet_get_net_locked(peer_net->lpn_net_id);
1937 * Iterate through the NIs in this local Net and select
1938 * the NI to send from. The selection is determined by
1939 * these 3 criterion in the following priority:
1941 * 2. NI available credits
1944 best_ni = lnet_get_best_ni(local_net, cur_best_ni,
1945 peer, peer_net, cpt);
1947 if (incr_seq && best_ni)
1954 lnet_initiate_peer_discovery(struct lnet_peer_ni *lpni,
1955 struct lnet_msg *msg, lnet_nid_t rtr_nid,
1958 struct lnet_peer *peer;
1959 lnet_nid_t primary_nid;
1962 lnet_peer_ni_addref_locked(lpni);
1964 peer = lpni->lpni_peer_net->lpn_peer;
1966 if (lnet_peer_gw_discovery(peer)) {
1967 lnet_peer_ni_decref_locked(lpni);
1971 if (!lnet_msg_discovery(msg) || lnet_peer_is_uptodate(peer)) {
1972 lnet_peer_ni_decref_locked(lpni);
1976 rc = lnet_discover_peer_locked(lpni, cpt, false);
1978 lnet_peer_ni_decref_locked(lpni);
1981 /* The peer may have changed. */
1982 peer = lpni->lpni_peer_net->lpn_peer;
1983 spin_lock(&peer->lp_lock);
1984 if (lnet_peer_is_uptodate_locked(peer)) {
1985 spin_unlock(&peer->lp_lock);
1986 lnet_peer_ni_decref_locked(lpni);
1989 /* queue message and return */
1990 msg->msg_rtr_nid_param = rtr_nid;
1991 msg->msg_sending = 0;
1992 msg->msg_txpeer = NULL;
1993 list_add_tail(&msg->msg_list, &peer->lp_dc_pendq);
1994 primary_nid = peer->lp_primary_nid;
1995 spin_unlock(&peer->lp_lock);
1997 lnet_peer_ni_decref_locked(lpni);
1999 CDEBUG(D_NET, "msg %p delayed. %s pending discovery\n",
2000 msg, libcfs_nid2str(primary_nid));
2002 return LNET_DC_WAIT;
2006 lnet_handle_find_routed_path(struct lnet_send_data *sd,
2008 struct lnet_peer_ni **gw_lpni,
2009 struct lnet_peer **gw_peer)
2013 struct lnet_peer *gw;
2014 struct lnet_peer *lp;
2015 struct lnet_peer_net *lpn;
2016 struct lnet_peer_net *best_lpn = NULL;
2017 struct lnet_remotenet *rnet, *best_rnet = NULL;
2018 struct lnet_route *best_route = NULL;
2019 struct lnet_route *last_route = NULL;
2020 struct lnet_peer_ni *lpni = NULL;
2021 struct lnet_peer_ni *gwni = NULL;
2022 lnet_nid_t src_nid = sd->sd_src_nid;
2024 /* If a router nid was specified then we are replying to a GET or
2025 * sending an ACK. In this case we use the gateway associated with the
2026 * specified router nid.
2028 if (sd->sd_rtr_nid != LNET_NID_ANY) {
2029 gwni = lnet_find_peer_ni_locked(sd->sd_rtr_nid);
2031 CERROR("No peer NI for gateway %s\n",
2032 libcfs_nid2str(sd->sd_rtr_nid));
2033 return -EHOSTUNREACH;
2035 gw = gwni->lpni_peer_net->lpn_peer;
2036 lnet_peer_ni_decref_locked(gwni);
2037 local_lnet = LNET_NIDNET(sd->sd_rtr_nid);
2039 /* we've already looked up the initial lpni using dst_nid */
2040 lpni = sd->sd_best_lpni;
2041 /* the peer tree must be in existence */
2042 LASSERT(lpni && lpni->lpni_peer_net &&
2043 lpni->lpni_peer_net->lpn_peer);
2044 lp = lpni->lpni_peer_net->lpn_peer;
2046 list_for_each_entry(lpn, &lp->lp_peer_nets, lpn_peer_nets) {
2047 /* is this remote network reachable? */
2048 rnet = lnet_find_rnet_locked(lpn->lpn_net_id);
2057 if (best_lpn->lpn_seq <= lpn->lpn_seq)
2065 CERROR("peer %s has no available nets\n",
2066 libcfs_nid2str(sd->sd_dst_nid));
2067 return -EHOSTUNREACH;
2070 sd->sd_best_lpni = lnet_find_best_lpni_on_net(sd->sd_best_ni,
2073 best_lpn->lpn_net_id);
2074 if (!sd->sd_best_lpni) {
2075 CERROR("peer %s down\n",
2076 libcfs_nid2str(sd->sd_dst_nid));
2077 return -EHOSTUNREACH;
2080 best_route = lnet_find_route_locked(best_rnet,
2081 LNET_NIDNET(src_nid),
2082 &last_route, &gwni);
2084 CERROR("no route to %s from %s\n",
2085 libcfs_nid2str(dst_nid),
2086 libcfs_nid2str(src_nid));
2087 return -EHOSTUNREACH;
2091 CERROR("Internal Error. Route expected to %s from %s\n",
2092 libcfs_nid2str(dst_nid),
2093 libcfs_nid2str(src_nid));
2097 gw = best_route->lr_gateway;
2098 LASSERT(gw == gwni->lpni_peer_net->lpn_peer);
2099 local_lnet = best_route->lr_lnet;
2104 * Discover this gateway if it hasn't already been discovered.
2105 * This means we might delay the message until discovery has
2108 sd->sd_msg->msg_src_nid_param = sd->sd_src_nid;
2109 rc = lnet_initiate_peer_discovery(gwni, sd->sd_msg, sd->sd_rtr_nid,
2114 if (!sd->sd_best_ni)
2115 sd->sd_best_ni = lnet_find_best_ni_on_spec_net(NULL, gw,
2116 lnet_peer_get_net_locked(gw,
2121 if (!sd->sd_best_ni) {
2122 CERROR("Internal Error. Expected local ni on %s but non found :%s\n",
2123 libcfs_net2str(local_lnet),
2124 libcfs_nid2str(sd->sd_src_nid));
2132 * increment the sequence numbers since now we're sure we're
2133 * going to use this path
2135 if (sd->sd_rtr_nid == LNET_NID_ANY) {
2136 LASSERT(best_route && last_route);
2137 best_route->lr_seq = last_route->lr_seq + 1;
2138 best_lpn->lpn_seq++;
2149 * Remote destination
2150 * Non-MR destination
2154 * Remote destination
2157 * The handling of these two cases is similar. Even though the destination
2158 * can be MR or non-MR, we'll deal directly with the router.
2161 lnet_handle_spec_router_dst(struct lnet_send_data *sd)
2164 struct lnet_peer_ni *gw_lpni = NULL;
2165 struct lnet_peer *gw_peer = NULL;
2168 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
2169 if (!sd->sd_best_ni) {
2170 CERROR("Can't send to %s: src %s is not a "
2171 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
2172 libcfs_nid2str(sd->sd_src_nid));
2176 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2181 if (sd->sd_send_case & NMR_DST)
2183 * since the final destination is non-MR let's set its preferred
2184 * NID before we send
2186 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni,
2190 * We're going to send to the gw found so let's set its
2193 sd->sd_peer = gw_peer;
2194 sd->sd_best_lpni = gw_lpni;
2196 return lnet_handle_send(sd);
2200 lnet_find_best_ni_on_local_net(struct lnet_peer *peer, int md_cpt,
2203 struct lnet_peer_net *peer_net = NULL;
2204 struct lnet_ni *best_ni = NULL;
2207 * The peer can have multiple interfaces, some of them can be on
2208 * the local network and others on a routed network. We should
2209 * prefer the local network. However if the local network is not
2210 * available then we need to try the routed network
2213 /* go through all the peer nets and find the best_ni */
2214 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_peer_nets) {
2216 * The peer's list of nets can contain non-local nets. We
2217 * want to only examine the local ones.
2219 if (!lnet_get_net_locked(peer_net->lpn_net_id))
2221 best_ni = lnet_find_best_ni_on_spec_net(best_ni, peer,
2222 peer_net, md_cpt, false);
2225 * if this is a discovery message and lp_disc_net_id is
2226 * specified then use that net to send the discovery on.
2228 if (peer->lp_disc_net_id == peer_net->lpn_net_id &&
2234 /* increment sequence number so we can round robin */
2240 static struct lnet_ni *
2241 lnet_find_existing_preferred_best_ni(struct lnet_peer_ni *lpni, int cpt)
2243 struct lnet_ni *best_ni = NULL;
2244 struct lnet_peer_net *peer_net = lpni->lpni_peer_net;
2245 struct lnet_peer_ni *lpni_entry;
2248 * We must use a consistent source address when sending to a
2249 * non-MR peer. However, a non-MR peer can have multiple NIDs
2250 * on multiple networks, and we may even need to talk to this
2251 * peer on multiple networks -- certain types of
2252 * load-balancing configuration do this.
2254 * So we need to pick the NI the peer prefers for this
2255 * particular network.
2258 list_for_each_entry(lpni_entry, &peer_net->lpn_peer_nis,
2260 if (lpni_entry->lpni_pref_nnids == 0)
2262 LASSERT(lpni_entry->lpni_pref_nnids == 1);
2263 best_ni = lnet_nid2ni_locked(lpni_entry->lpni_pref.nid, cpt);
2270 /* Prerequisite: sd->sd_peer and sd->sd_best_lpni should be set */
2272 lnet_select_preferred_best_ni(struct lnet_send_data *sd)
2274 struct lnet_ni *best_ni = NULL;
2275 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
2278 * We must use a consistent source address when sending to a
2279 * non-MR peer. However, a non-MR peer can have multiple NIDs
2280 * on multiple networks, and we may even need to talk to this
2281 * peer on multiple networks -- certain types of
2282 * load-balancing configuration do this.
2284 * So we need to pick the NI the peer prefers for this
2285 * particular network.
2288 best_ni = lnet_find_existing_preferred_best_ni(sd->sd_best_lpni,
2291 /* if best_ni is still not set just pick one */
2294 lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2295 sd->sd_best_lpni->lpni_peer_net,
2296 sd->sd_md_cpt, true);
2297 /* If there is no best_ni we don't have a route */
2299 CERROR("no path to %s from net %s\n",
2300 libcfs_nid2str(best_lpni->lpni_nid),
2301 libcfs_net2str(best_lpni->lpni_net->net_id));
2302 return -EHOSTUNREACH;
2306 sd->sd_best_ni = best_ni;
2308 /* Set preferred NI if necessary. */
2309 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
2316 * Source not specified
2320 * always use the same source NID for NMR peers
2321 * If we've talked to that peer before then we already have a preferred
2322 * source NI associated with it. Otherwise, we select a preferred local NI
2323 * and store it in the peer
2326 lnet_handle_any_local_nmr_dst(struct lnet_send_data *sd)
2330 /* sd->sd_best_lpni is already set to the final destination */
2333 * At this point we should've created the peer ni and peer. If we
2334 * can't find it, then something went wrong. Instead of assert
2335 * output a relevant message and fail the send
2337 if (!sd->sd_best_lpni) {
2338 CERROR("Internal fault. Unable to send msg %s to %s. "
2340 lnet_msgtyp2str(sd->sd_msg->msg_type),
2341 libcfs_nid2str(sd->sd_dst_nid));
2345 rc = lnet_select_preferred_best_ni(sd);
2347 rc = lnet_handle_send(sd);
2353 lnet_handle_any_mr_dsta(struct lnet_send_data *sd)
2356 * NOTE we've already handled the remote peer case. So we only
2357 * need to worry about the local case here.
2359 * if we're sending a response, ACK or reply, we need to send it
2360 * to the destination NID given to us. At this point we already
2361 * have the peer_ni we're suppose to send to, so just find the
2362 * best_ni on the peer net and use that. Since we're sending to an
2363 * MR peer then we can just run the selection algorithm on our
2364 * local NIs and pick the best one.
2366 if (sd->sd_send_case & SND_RESP) {
2368 lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2369 sd->sd_best_lpni->lpni_peer_net,
2370 sd->sd_md_cpt, true);
2372 if (!sd->sd_best_ni) {
2374 * We're not going to deal with not able to send
2375 * a response to the provided final destination
2377 CERROR("Can't send response to %s. "
2378 "No local NI available\n",
2379 libcfs_nid2str(sd->sd_dst_nid));
2380 return -EHOSTUNREACH;
2383 return lnet_handle_send(sd);
2387 * If we get here that means we're sending a fresh request, PUT or
2388 * GET, so we need to run our standard selection algorithm.
2389 * First find the best local interface that's on any of the peer's
2392 sd->sd_best_ni = lnet_find_best_ni_on_local_net(sd->sd_peer,
2394 lnet_msg_discovery(sd->sd_msg));
2395 if (sd->sd_best_ni) {
2397 lnet_find_best_lpni_on_net(sd->sd_best_ni, sd->sd_dst_nid,
2399 sd->sd_best_ni->ni_net->net_id);
2402 * if we're successful in selecting a peer_ni on the local
2403 * network, then send to it. Otherwise fall through and
2404 * try and see if we can reach it over another routed
2407 if (sd->sd_best_lpni &&
2408 sd->sd_best_lpni->lpni_nid == the_lnet.ln_loni->ni_nid) {
2410 * in case we initially started with a routed
2411 * destination, let's reset to local
2413 sd->sd_send_case &= ~REMOTE_DST;
2414 sd->sd_send_case |= LOCAL_DST;
2415 return lnet_handle_lo_send(sd);
2416 } else if (sd->sd_best_lpni) {
2418 * in case we initially started with a routed
2419 * destination, let's reset to local
2421 sd->sd_send_case &= ~REMOTE_DST;
2422 sd->sd_send_case |= LOCAL_DST;
2423 return lnet_handle_send(sd);
2426 CERROR("Internal Error. Expected to have a best_lpni: "
2428 libcfs_nid2str(sd->sd_src_nid),
2429 libcfs_nid2str(sd->sd_dst_nid));
2435 * Peer doesn't have a local network. Let's see if there is
2436 * a remote network we can reach it on.
2438 return PASS_THROUGH;
2443 * Source NID not specified
2448 * Source NID not speified
2449 * Remote destination
2452 * In both of these cases if we're sending a response, ACK or REPLY, then
2453 * we need to send to the destination NID provided.
2455 * In the remote case let's deal with MR routers.
2460 lnet_handle_any_mr_dst(struct lnet_send_data *sd)
2463 struct lnet_peer *gw_peer = NULL;
2464 struct lnet_peer_ni *gw_lpni = NULL;
2467 * handle sending a response to a remote peer here so we don't
2468 * have to worry about it if we hit lnet_handle_any_mr_dsta()
2470 if (sd->sd_send_case & REMOTE_DST &&
2471 sd->sd_send_case & SND_RESP) {
2472 struct lnet_peer_ni *gw;
2473 struct lnet_peer *gw_peer;
2475 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw,
2478 CERROR("Can't send response to %s. "
2479 "No route available\n",
2480 libcfs_nid2str(sd->sd_dst_nid));
2481 return -EHOSTUNREACH;
2482 } else if (rc > 0) {
2486 sd->sd_best_lpni = gw;
2487 sd->sd_peer = gw_peer;
2489 return lnet_handle_send(sd);
2493 * Even though the NID for the peer might not be on a local network,
2494 * since the peer is MR there could be other interfaces on the
2495 * local network. In that case we'd still like to prefer the local
2496 * network over the routed network. If we're unable to do that
2497 * then we select the best router among the different routed networks,
2498 * and if the router is MR then we can deal with it as such.
2500 rc = lnet_handle_any_mr_dsta(sd);
2501 if (rc != PASS_THROUGH)
2505 * Now that we must route to the destination, we must consider the
2506 * MR case, where the destination has multiple interfaces, some of
2507 * which we can route to and others we do not. For this reason we
2508 * need to select the destination which we can route to and if
2509 * there are multiple, we need to round robin.
2511 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2516 sd->sd_send_case &= ~LOCAL_DST;
2517 sd->sd_send_case |= REMOTE_DST;
2519 sd->sd_peer = gw_peer;
2520 sd->sd_best_lpni = gw_lpni;
2522 return lnet_handle_send(sd);
2526 * Source not specified
2527 * Remote destination
2530 * Must send to the specified peer NID using the same source NID that
2531 * we've used before. If it's the first time to talk to that peer then
2532 * find the source NI and assign it as preferred to that peer
2535 lnet_handle_any_router_nmr_dst(struct lnet_send_data *sd)
2538 struct lnet_peer_ni *gw_lpni = NULL;
2539 struct lnet_peer *gw_peer = NULL;
2542 * Let's see if we have a preferred NI to talk to this NMR peer
2544 sd->sd_best_ni = lnet_find_existing_preferred_best_ni(sd->sd_best_lpni,
2548 * find the router and that'll find the best NI if we didn't find
2551 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2557 * set the best_ni we've chosen as the preferred one for
2560 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
2562 /* we'll be sending to the gw */
2563 sd->sd_best_lpni = gw_lpni;
2564 sd->sd_peer = gw_peer;
2566 return lnet_handle_send(sd);
2570 lnet_handle_send_case_locked(struct lnet_send_data *sd)
2573 * turn off the SND_RESP bit.
2574 * It will be checked in the case handling
2576 __u32 send_case = sd->sd_send_case &= ~SND_RESP ;
2578 CDEBUG(D_NET, "Source %s%s to %s %s %s destination\n",
2579 (send_case & SRC_SPEC) ? "Specified: " : "ANY",
2580 (send_case & SRC_SPEC) ? libcfs_nid2str(sd->sd_src_nid) : "",
2581 (send_case & MR_DST) ? "MR: " : "NMR: ",
2582 libcfs_nid2str(sd->sd_dst_nid),
2583 (send_case & LOCAL_DST) ? "local" : "routed");
2585 switch (send_case) {
2587 * For all cases where the source is specified, we should always
2588 * use the destination NID, whether it's an MR destination or not,
2589 * since we're continuing a series of related messages for the
2592 case SRC_SPEC_LOCAL_NMR_DST:
2593 return lnet_handle_spec_local_nmr_dst(sd);
2594 case SRC_SPEC_LOCAL_MR_DST:
2595 return lnet_handle_spec_local_mr_dst(sd);
2596 case SRC_SPEC_ROUTER_NMR_DST:
2597 case SRC_SPEC_ROUTER_MR_DST:
2598 return lnet_handle_spec_router_dst(sd);
2599 case SRC_ANY_LOCAL_NMR_DST:
2600 return lnet_handle_any_local_nmr_dst(sd);
2601 case SRC_ANY_LOCAL_MR_DST:
2602 case SRC_ANY_ROUTER_MR_DST:
2603 return lnet_handle_any_mr_dst(sd);
2604 case SRC_ANY_ROUTER_NMR_DST:
2605 return lnet_handle_any_router_nmr_dst(sd);
2607 CERROR("Unknown send case\n");
2613 lnet_select_pathway(lnet_nid_t src_nid, lnet_nid_t dst_nid,
2614 struct lnet_msg *msg, lnet_nid_t rtr_nid)
2616 struct lnet_peer_ni *lpni;
2617 struct lnet_peer *peer;
2618 struct lnet_send_data send_data;
2621 __u32 send_case = 0;
2623 memset(&send_data, 0, sizeof(send_data));
2626 * get an initial CPT to use for locking. The idea here is not to
2627 * serialize the calls to select_pathway, so that as many
2628 * operations can run concurrently as possible. To do that we use
2629 * the CPT where this call is being executed. Later on when we
2630 * determine the CPT to use in lnet_message_commit, we switch the
2631 * lock and check if there was any configuration change. If none,
2632 * then we proceed, if there is, then we restart the operation.
2634 cpt = lnet_net_lock_current();
2636 md_cpt = lnet_cpt_of_md(msg->msg_md, msg->msg_offset);
2637 if (md_cpt == CFS_CPT_ANY)
2643 * If we're being asked to send to the loopback interface, there
2644 * is no need to go through any selection. We can just shortcut
2645 * the entire process and send over lolnd
2647 send_data.sd_msg = msg;
2648 send_data.sd_cpt = cpt;
2649 if (LNET_NETTYP(LNET_NIDNET(dst_nid)) == LOLND) {
2650 rc = lnet_handle_lo_send(&send_data);
2651 lnet_net_unlock(cpt);
2656 * find an existing peer_ni, or create one and mark it as having been
2657 * created due to network traffic. This call will create the
2658 * peer->peer_net->peer_ni tree.
2660 lpni = lnet_nid2peerni_locked(dst_nid, LNET_NID_ANY, cpt);
2662 lnet_net_unlock(cpt);
2663 return PTR_ERR(lpni);
2667 * Cache the original src_nid. If we need to resend the message
2668 * then we'll need to know whether the src_nid was originally
2669 * specified for this message. If it was originally specified,
2670 * then we need to keep using the same src_nid since it's
2671 * continuing the same sequence of messages.
2673 msg->msg_src_nid_param = src_nid;
2676 * If necessary, perform discovery on the peer that owns this peer_ni.
2677 * Note, this can result in the ownership of this peer_ni changing
2678 * to another peer object.
2680 rc = lnet_initiate_peer_discovery(lpni, msg, rtr_nid, cpt);
2682 lnet_peer_ni_decref_locked(lpni);
2683 lnet_net_unlock(cpt);
2686 lnet_peer_ni_decref_locked(lpni);
2688 peer = lpni->lpni_peer_net->lpn_peer;
2691 * Identify the different send cases
2693 if (src_nid == LNET_NID_ANY)
2694 send_case |= SRC_ANY;
2696 send_case |= SRC_SPEC;
2698 if (lnet_get_net_locked(LNET_NIDNET(dst_nid)))
2699 send_case |= LOCAL_DST;
2701 send_case |= REMOTE_DST;
2704 * if this is a non-MR peer or if we're recovering a peer ni then
2705 * let's consider this an NMR case so we can hit the destination
2708 if (!lnet_peer_is_multi_rail(peer) || msg->msg_recovery)
2709 send_case |= NMR_DST;
2711 send_case |= MR_DST;
2713 if (lnet_msg_is_response(msg))
2714 send_case |= SND_RESP;
2716 /* assign parameters to the send_data */
2717 send_data.sd_rtr_nid = rtr_nid;
2718 send_data.sd_src_nid = src_nid;
2719 send_data.sd_dst_nid = dst_nid;
2720 send_data.sd_best_lpni = lpni;
2722 * keep a pointer to the final destination in case we're going to
2723 * route, so we'll need to access it later
2725 send_data.sd_final_dst_lpni = lpni;
2726 send_data.sd_peer = peer;
2727 send_data.sd_md_cpt = md_cpt;
2728 send_data.sd_send_case = send_case;
2730 rc = lnet_handle_send_case_locked(&send_data);
2733 * Update the local cpt since send_data.sd_cpt might've been
2734 * updated as a result of calling lnet_handle_send_case_locked().
2736 cpt = send_data.sd_cpt;
2738 if (rc == REPEAT_SEND)
2741 lnet_net_unlock(cpt);
2747 lnet_send(lnet_nid_t src_nid, struct lnet_msg *msg, lnet_nid_t rtr_nid)
2749 lnet_nid_t dst_nid = msg->msg_target.nid;
2753 * NB: rtr_nid is set to LNET_NID_ANY for all current use-cases,
2754 * but we might want to use pre-determined router for ACK/REPLY
2757 /* NB: ni != NULL == interface pre-determined (ACK/REPLY) */
2758 LASSERT(msg->msg_txpeer == NULL);
2759 LASSERT(msg->msg_txni == NULL);
2760 LASSERT(!msg->msg_sending);
2761 LASSERT(!msg->msg_target_is_router);
2762 LASSERT(!msg->msg_receiving);
2764 msg->msg_sending = 1;
2766 LASSERT(!msg->msg_tx_committed);
2768 rc = lnet_select_pathway(src_nid, dst_nid, msg, rtr_nid);
2770 if (rc == -EHOSTUNREACH)
2771 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_ERROR;
2773 msg->msg_health_status = LNET_MSG_STATUS_LOCAL_ERROR;
2777 if (rc == LNET_CREDIT_OK)
2778 lnet_ni_send(msg->msg_txni, msg);
2780 /* rc == LNET_CREDIT_OK or LNET_CREDIT_WAIT or LNET_DC_WAIT */
2784 enum lnet_mt_event_type {
2785 MT_TYPE_LOCAL_NI = 0,
2789 struct lnet_mt_event_info {
2790 enum lnet_mt_event_type mt_type;
2794 /* called with res_lock held */
2796 lnet_detach_rsp_tracker(struct lnet_libmd *md, int cpt)
2798 struct lnet_rsp_tracker *rspt;
2801 * msg has a refcount on the MD so the MD is not going away.
2802 * The rspt queue for the cpt is protected by
2803 * the lnet_net_lock(cpt). cpt is the cpt of the MD cookie.
2805 if (!md->md_rspt_ptr)
2808 rspt = md->md_rspt_ptr;
2811 LASSERT(rspt->rspt_cpt == cpt);
2813 md->md_rspt_ptr = NULL;
2815 if (LNetMDHandleIsInvalid(rspt->rspt_mdh)) {
2817 * The monitor thread has invalidated this handle because the
2818 * response timed out, but it failed to lookup the MD. That
2819 * means this response tracker is on the zombie list. We can
2820 * safely remove it under the resource lock (held by caller) and
2821 * free the response tracker block.
2823 list_del(&rspt->rspt_on_list);
2824 lnet_rspt_free(rspt, cpt);
2827 * invalidate the handle to indicate that a response has been
2828 * received, which will then lead the monitor thread to clean up
2831 LNetInvalidateMDHandle(&rspt->rspt_mdh);
2836 lnet_clean_zombie_rstqs(void)
2838 struct lnet_rsp_tracker *rspt, *tmp;
2841 cfs_cpt_for_each(i, lnet_cpt_table()) {
2842 list_for_each_entry_safe(rspt, tmp,
2843 the_lnet.ln_mt_zombie_rstqs[i],
2845 list_del(&rspt->rspt_on_list);
2846 lnet_rspt_free(rspt, i);
2850 cfs_percpt_free(the_lnet.ln_mt_zombie_rstqs);
2854 lnet_finalize_expired_responses(void)
2856 struct lnet_libmd *md;
2857 struct lnet_rsp_tracker *rspt, *tmp;
2861 if (the_lnet.ln_mt_rstq == NULL)
2864 cfs_cpt_for_each(i, lnet_cpt_table()) {
2865 LIST_HEAD(local_queue);
2868 if (!the_lnet.ln_mt_rstq[i]) {
2872 list_splice_init(the_lnet.ln_mt_rstq[i], &local_queue);
2877 list_for_each_entry_safe(rspt, tmp, &local_queue, rspt_on_list) {
2879 * The rspt mdh will be invalidated when a response
2880 * is received or whenever we want to discard the
2881 * block the monitor thread will walk the queue
2882 * and clean up any rsts with an invalid mdh.
2883 * The monitor thread will walk the queue until
2884 * the first unexpired rspt block. This means that
2885 * some rspt blocks which received their
2886 * corresponding responses will linger in the
2887 * queue until they are cleaned up eventually.
2890 if (LNetMDHandleIsInvalid(rspt->rspt_mdh)) {
2892 list_del(&rspt->rspt_on_list);
2893 lnet_rspt_free(rspt, i);
2897 if (ktime_compare(now, rspt->rspt_deadline) >= 0 ||
2898 the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN) {
2899 struct lnet_peer_ni *lpni;
2902 md = lnet_handle2md(&rspt->rspt_mdh);
2904 /* MD has been queued for unlink, but
2905 * rspt hasn't been detached (Note we've
2906 * checked above that the rspt_mdh is
2907 * valid). Since we cannot lookup the MD
2908 * we're unable to detach the rspt
2909 * ourselves. Thus, move the rspt to the
2910 * zombie list where we'll wait for
2912 * 1. The remaining operations on the
2913 * MD to complete. In this case the
2914 * final operation will result in
2915 * lnet_msg_detach_md()->
2916 * lnet_detach_rsp_tracker() where
2917 * we will clean up this response
2919 * 2. LNet to shutdown. In this case
2920 * we'll wait until after all LND Nets
2921 * have shutdown and then we can
2922 * safely free any remaining response
2923 * tracker blocks on the zombie list.
2924 * Note: We need to hold the resource
2925 * lock when adding to the zombie list
2926 * because we may have concurrent access
2927 * with lnet_detach_rsp_tracker().
2929 LNetInvalidateMDHandle(&rspt->rspt_mdh);
2930 list_move(&rspt->rspt_on_list,
2931 the_lnet.ln_mt_zombie_rstqs[i]);
2935 LASSERT(md->md_rspt_ptr == rspt);
2936 md->md_rspt_ptr = NULL;
2939 LNetMDUnlink(rspt->rspt_mdh);
2941 nid = rspt->rspt_next_hop_nid;
2943 list_del(&rspt->rspt_on_list);
2944 lnet_rspt_free(rspt, i);
2946 /* If we're shutting down we just want to clean
2947 * up the rspt blocks
2949 if (the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN)
2953 the_lnet.ln_counters[i]->lct_health.lch_response_timeout_count++;
2957 "Response timeout: md = %p: nid = %s\n",
2958 md, libcfs_nid2str(nid));
2961 * If there is a timeout on the response
2962 * from the next hop decrement its health
2963 * value so that we don't use it
2966 lpni = lnet_find_peer_ni_locked(nid);
2968 lnet_handle_remote_failure_locked(lpni);
2969 lnet_peer_ni_decref_locked(lpni);
2978 if (!list_empty(&local_queue)) {
2980 list_splice(&local_queue, the_lnet.ln_mt_rstq[i]);
2987 lnet_resend_pending_msgs_locked(struct list_head *resendq, int cpt)
2989 struct lnet_msg *msg;
2991 while (!list_empty(resendq)) {
2992 struct lnet_peer_ni *lpni;
2994 msg = list_entry(resendq->next, struct lnet_msg,
2997 list_del_init(&msg->msg_list);
2999 lpni = lnet_find_peer_ni_locked(msg->msg_hdr.dest_nid);
3001 lnet_net_unlock(cpt);
3002 CERROR("Expected that a peer is already created for %s\n",
3003 libcfs_nid2str(msg->msg_hdr.dest_nid));
3004 msg->msg_no_resend = true;
3005 lnet_finalize(msg, -EFAULT);
3008 struct lnet_peer *peer;
3010 lnet_nid_t src_nid = LNET_NID_ANY;
3013 * if this message is not being routed and the
3014 * peer is non-MR then we must use the same
3015 * src_nid that was used in the original send.
3016 * Otherwise if we're routing the message (IE
3017 * we're a router) then we can use any of our
3018 * local interfaces. It doesn't matter to the
3019 * final destination.
3021 peer = lpni->lpni_peer_net->lpn_peer;
3022 if (!msg->msg_routing &&
3023 !lnet_peer_is_multi_rail(peer))
3024 src_nid = le64_to_cpu(msg->msg_hdr.src_nid);
3027 * If we originally specified a src NID, then we
3028 * must attempt to reuse it in the resend as well.
3030 if (msg->msg_src_nid_param != LNET_NID_ANY)
3031 src_nid = msg->msg_src_nid_param;
3032 lnet_peer_ni_decref_locked(lpni);
3034 lnet_net_unlock(cpt);
3035 CDEBUG(D_NET, "resending %s->%s: %s recovery %d try# %d\n",
3036 libcfs_nid2str(src_nid),
3037 libcfs_id2str(msg->msg_target),
3038 lnet_msgtyp2str(msg->msg_type),
3040 msg->msg_retry_count);
3041 rc = lnet_send(src_nid, msg, LNET_NID_ANY);
3043 CERROR("Error sending %s to %s: %d\n",
3044 lnet_msgtyp2str(msg->msg_type),
3045 libcfs_id2str(msg->msg_target), rc);
3046 msg->msg_no_resend = true;
3047 lnet_finalize(msg, rc);
3051 the_lnet.ln_counters[cpt]->lct_health.lch_resend_count++;
3057 lnet_resend_pending_msgs(void)
3061 cfs_cpt_for_each(i, lnet_cpt_table()) {
3063 lnet_resend_pending_msgs_locked(the_lnet.ln_mt_resendqs[i], i);
3068 /* called with cpt and ni_lock held */
3070 lnet_unlink_ni_recovery_mdh_locked(struct lnet_ni *ni, int cpt, bool force)
3072 struct lnet_handle_md recovery_mdh;
3074 LNetInvalidateMDHandle(&recovery_mdh);
3076 if (ni->ni_recovery_state & LNET_NI_RECOVERY_PENDING ||
3078 recovery_mdh = ni->ni_ping_mdh;
3079 LNetInvalidateMDHandle(&ni->ni_ping_mdh);
3082 lnet_net_unlock(cpt);
3083 if (!LNetMDHandleIsInvalid(recovery_mdh))
3084 LNetMDUnlink(recovery_mdh);
3090 lnet_recover_local_nis(void)
3092 struct lnet_mt_event_info *ev_info;
3093 LIST_HEAD(processed_list);
3094 LIST_HEAD(local_queue);
3095 struct lnet_handle_md mdh;
3096 struct lnet_ni *tmp;
3103 * splice the recovery queue on a local queue. We will iterate
3104 * through the local queue and update it as needed. Once we're
3105 * done with the traversal, we'll splice the local queue back on
3106 * the head of the ln_mt_localNIRecovq. Any newly added local NIs
3107 * will be traversed in the next iteration.
3110 list_splice_init(&the_lnet.ln_mt_localNIRecovq,
3114 list_for_each_entry_safe(ni, tmp, &local_queue, ni_recovery) {
3116 * if an NI is being deleted or it is now healthy, there
3117 * is no need to keep it around in the recovery queue.
3118 * The monitor thread is the only thread responsible for
3119 * removing the NI from the recovery queue.
3120 * Multiple threads can be adding NIs to the recovery
3123 healthv = atomic_read(&ni->ni_healthv);
3127 if (ni->ni_state != LNET_NI_STATE_ACTIVE ||
3128 healthv == LNET_MAX_HEALTH_VALUE) {
3129 list_del_init(&ni->ni_recovery);
3130 lnet_unlink_ni_recovery_mdh_locked(ni, 0, false);
3132 lnet_ni_decref_locked(ni, 0);
3138 * if the local NI failed recovery we must unlink the md.
3139 * But we want to keep the local_ni on the recovery queue
3140 * so we can continue the attempts to recover it.
3142 if (ni->ni_recovery_state & LNET_NI_RECOVERY_FAILED) {
3143 lnet_unlink_ni_recovery_mdh_locked(ni, 0, true);
3144 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_FAILED;
3151 CDEBUG(D_NET, "attempting to recover local ni: %s\n",
3152 libcfs_nid2str(ni->ni_nid));
3155 if (!(ni->ni_recovery_state & LNET_NI_RECOVERY_PENDING)) {
3156 ni->ni_recovery_state |= LNET_NI_RECOVERY_PENDING;
3159 LIBCFS_ALLOC(ev_info, sizeof(*ev_info));
3161 CERROR("out of memory. Can't recover %s\n",
3162 libcfs_nid2str(ni->ni_nid));
3164 ni->ni_recovery_state &=
3165 ~LNET_NI_RECOVERY_PENDING;
3170 mdh = ni->ni_ping_mdh;
3172 * Invalidate the ni mdh in case it's deleted.
3173 * We'll unlink the mdh in this case below.
3175 LNetInvalidateMDHandle(&ni->ni_ping_mdh);
3179 * remove the NI from the local queue and drop the
3180 * reference count to it while we're recovering
3181 * it. The reason for that, is that the NI could
3182 * be deleted, and the way the code is structured
3183 * is if we don't drop the NI, then the deletion
3184 * code will enter a loop waiting for the
3185 * reference count to be removed while holding the
3186 * ln_mutex_lock(). When we look up the peer to
3187 * send to in lnet_select_pathway() we will try to
3188 * lock the ln_mutex_lock() as well, leading to
3189 * a deadlock. By dropping the refcount and
3190 * removing it from the list, we allow for the NI
3191 * to be removed, then we use the cached NID to
3192 * look it up again. If it's gone, then we just
3193 * continue examining the rest of the queue.
3196 list_del_init(&ni->ni_recovery);
3197 lnet_ni_decref_locked(ni, 0);
3200 ev_info->mt_type = MT_TYPE_LOCAL_NI;
3201 ev_info->mt_nid = nid;
3202 rc = lnet_send_ping(nid, &mdh, LNET_INTERFACES_MIN,
3203 ev_info, the_lnet.ln_mt_eqh, true);
3204 /* lookup the nid again */
3206 ni = lnet_nid2ni_locked(nid, 0);
3209 * the NI has been deleted when we dropped
3217 * Same note as in lnet_recover_peer_nis(). When
3218 * we're sending the ping, the NI is free to be
3219 * deleted or manipulated. By this point it
3220 * could've been added back on the recovery queue,
3221 * and a refcount taken on it.
3222 * So we can't just add it blindly again or we'll
3223 * corrupt the queue. We must check under lock if
3224 * it's not on any list and if not then add it
3225 * to the processed list, which will eventually be
3226 * spliced back on to the recovery queue.
3228 ni->ni_ping_mdh = mdh;
3229 if (list_empty(&ni->ni_recovery)) {
3230 list_add_tail(&ni->ni_recovery, &processed_list);
3231 lnet_ni_addref_locked(ni, 0);
3237 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_PENDING;
3243 * put back the remaining NIs on the ln_mt_localNIRecovq to be
3244 * reexamined in the next iteration.
3246 list_splice_init(&processed_list, &local_queue);
3248 list_splice(&local_queue, &the_lnet.ln_mt_localNIRecovq);
3253 lnet_resendqs_create(void)
3255 struct list_head **resendqs;
3256 resendqs = lnet_create_array_of_queues();
3261 lnet_net_lock(LNET_LOCK_EX);
3262 the_lnet.ln_mt_resendqs = resendqs;
3263 lnet_net_unlock(LNET_LOCK_EX);
3269 lnet_clean_local_ni_recoveryq(void)
3273 /* This is only called when the monitor thread has stopped */
3276 while (!list_empty(&the_lnet.ln_mt_localNIRecovq)) {
3277 ni = list_entry(the_lnet.ln_mt_localNIRecovq.next,
3278 struct lnet_ni, ni_recovery);
3279 list_del_init(&ni->ni_recovery);
3281 lnet_unlink_ni_recovery_mdh_locked(ni, 0, true);
3283 lnet_ni_decref_locked(ni, 0);
3290 lnet_unlink_lpni_recovery_mdh_locked(struct lnet_peer_ni *lpni, int cpt,
3293 struct lnet_handle_md recovery_mdh;
3295 LNetInvalidateMDHandle(&recovery_mdh);
3297 if (lpni->lpni_state & LNET_PEER_NI_RECOVERY_PENDING || force) {
3298 recovery_mdh = lpni->lpni_recovery_ping_mdh;
3299 LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
3301 spin_unlock(&lpni->lpni_lock);
3302 lnet_net_unlock(cpt);
3303 if (!LNetMDHandleIsInvalid(recovery_mdh))
3304 LNetMDUnlink(recovery_mdh);
3306 spin_lock(&lpni->lpni_lock);
3310 lnet_clean_peer_ni_recoveryq(void)
3312 struct lnet_peer_ni *lpni, *tmp;
3314 lnet_net_lock(LNET_LOCK_EX);
3316 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_mt_peerNIRecovq,
3318 list_del_init(&lpni->lpni_recovery);
3319 spin_lock(&lpni->lpni_lock);
3320 lnet_unlink_lpni_recovery_mdh_locked(lpni, LNET_LOCK_EX, true);
3321 spin_unlock(&lpni->lpni_lock);
3322 lnet_peer_ni_decref_locked(lpni);
3325 lnet_net_unlock(LNET_LOCK_EX);
3329 lnet_clean_resendqs(void)
3331 struct lnet_msg *msg, *tmp;
3335 cfs_cpt_for_each(i, lnet_cpt_table()) {
3337 list_splice_init(the_lnet.ln_mt_resendqs[i], &msgs);
3339 list_for_each_entry_safe(msg, tmp, &msgs, msg_list) {
3340 list_del_init(&msg->msg_list);
3341 msg->msg_no_resend = true;
3342 lnet_finalize(msg, -ESHUTDOWN);
3346 cfs_percpt_free(the_lnet.ln_mt_resendqs);
3350 lnet_recover_peer_nis(void)
3352 struct lnet_mt_event_info *ev_info;
3353 LIST_HEAD(processed_list);
3354 LIST_HEAD(local_queue);
3355 struct lnet_handle_md mdh;
3356 struct lnet_peer_ni *lpni;
3357 struct lnet_peer_ni *tmp;
3363 * Always use cpt 0 for locking across all interactions with
3364 * ln_mt_peerNIRecovq
3367 list_splice_init(&the_lnet.ln_mt_peerNIRecovq,
3371 list_for_each_entry_safe(lpni, tmp, &local_queue,
3374 * The same protection strategy is used here as is in the
3375 * local recovery case.
3378 healthv = atomic_read(&lpni->lpni_healthv);
3379 spin_lock(&lpni->lpni_lock);
3380 if (lpni->lpni_state & LNET_PEER_NI_DELETING ||
3381 healthv == LNET_MAX_HEALTH_VALUE) {
3382 list_del_init(&lpni->lpni_recovery);
3383 lnet_unlink_lpni_recovery_mdh_locked(lpni, 0, false);
3384 spin_unlock(&lpni->lpni_lock);
3385 lnet_peer_ni_decref_locked(lpni);
3391 * If the peer NI has failed recovery we must unlink the
3392 * md. But we want to keep the peer ni on the recovery
3393 * queue so we can try to continue recovering it
3395 if (lpni->lpni_state & LNET_PEER_NI_RECOVERY_FAILED) {
3396 lnet_unlink_lpni_recovery_mdh_locked(lpni, 0, true);
3397 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_FAILED;
3400 spin_unlock(&lpni->lpni_lock);
3404 * NOTE: we're racing with peer deletion from user space.
3405 * It's possible that a peer is deleted after we check its
3406 * state. In this case the recovery can create a new peer
3408 spin_lock(&lpni->lpni_lock);
3409 if (!(lpni->lpni_state & LNET_PEER_NI_RECOVERY_PENDING) &&
3410 !(lpni->lpni_state & LNET_PEER_NI_DELETING)) {
3411 lpni->lpni_state |= LNET_PEER_NI_RECOVERY_PENDING;
3412 spin_unlock(&lpni->lpni_lock);
3414 LIBCFS_ALLOC(ev_info, sizeof(*ev_info));
3416 CERROR("out of memory. Can't recover %s\n",
3417 libcfs_nid2str(lpni->lpni_nid));
3418 spin_lock(&lpni->lpni_lock);
3419 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3420 spin_unlock(&lpni->lpni_lock);
3424 /* look at the comments in lnet_recover_local_nis() */
3425 mdh = lpni->lpni_recovery_ping_mdh;
3426 LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
3427 nid = lpni->lpni_nid;
3429 list_del_init(&lpni->lpni_recovery);
3430 lnet_peer_ni_decref_locked(lpni);
3433 ev_info->mt_type = MT_TYPE_PEER_NI;
3434 ev_info->mt_nid = nid;
3435 rc = lnet_send_ping(nid, &mdh, LNET_INTERFACES_MIN,
3436 ev_info, the_lnet.ln_mt_eqh, true);
3439 * lnet_find_peer_ni_locked() grabs a refcount for
3440 * us. No need to take it explicitly.
3442 lpni = lnet_find_peer_ni_locked(nid);
3449 lpni->lpni_recovery_ping_mdh = mdh;
3451 * While we're unlocked the lpni could've been
3452 * readded on the recovery queue. In this case we
3453 * don't need to add it to the local queue, since
3454 * it's already on there and the thread that added
3455 * it would've incremented the refcount on the
3456 * peer, which means we need to decref the refcount
3457 * that was implicitly grabbed by find_peer_ni_locked.
3458 * Otherwise, if the lpni is still not on
3459 * the recovery queue, then we'll add it to the
3462 if (list_empty(&lpni->lpni_recovery))
3463 list_add_tail(&lpni->lpni_recovery, &processed_list);
3465 lnet_peer_ni_decref_locked(lpni);
3468 spin_lock(&lpni->lpni_lock);
3470 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3472 spin_unlock(&lpni->lpni_lock);
3475 list_splice_init(&processed_list, &local_queue);
3477 list_splice(&local_queue, &the_lnet.ln_mt_peerNIRecovq);
3482 lnet_monitor_thread(void *arg)
3484 time64_t recovery_timeout = 0;
3485 time64_t rsp_timeout = 0;
3489 wait_for_completion(&the_lnet.ln_started);
3491 * The monitor thread takes care of the following:
3492 * 1. Checks the aliveness of routers
3493 * 2. Checks if there are messages on the resend queue to resend
3495 * 3. Check if there are any NIs on the local recovery queue and
3497 * 4. Checks if there are any NIs on the remote recovery queue
3500 cfs_block_allsigs();
3502 while (the_lnet.ln_mt_state == LNET_MT_STATE_RUNNING) {
3503 now = ktime_get_real_seconds();
3505 if (lnet_router_checker_active())
3506 lnet_check_routers();
3508 lnet_resend_pending_msgs();
3510 if (now >= rsp_timeout) {
3511 lnet_finalize_expired_responses();
3512 rsp_timeout = now + (lnet_transaction_timeout / 2);
3515 if (now >= recovery_timeout) {
3516 lnet_recover_local_nis();
3517 lnet_recover_peer_nis();
3518 recovery_timeout = now + lnet_recovery_interval;
3522 * TODO do we need to check if we should sleep without
3523 * timeout? Technically, an active system will always
3524 * have messages in flight so this check will always
3525 * evaluate to false. And on an idle system do we care
3526 * if we wake up every 1 second? Although, we've seen
3527 * cases where we get a complaint that an idle thread
3528 * is waking up unnecessarily.
3530 * Take into account the current net_count when you wake
3531 * up for alive router checking, since we need to check
3532 * possibly as many networks as we have configured.
3534 interval = min(lnet_recovery_interval,
3535 min((unsigned int) alive_router_check_interval /
3536 lnet_current_net_count,
3537 lnet_transaction_timeout / 2));
3538 wait_for_completion_interruptible_timeout(
3539 &the_lnet.ln_mt_wait_complete,
3540 cfs_time_seconds(interval));
3541 /* Must re-init the completion before testing anything,
3542 * including ln_mt_state.
3544 reinit_completion(&the_lnet.ln_mt_wait_complete);
3548 lnet_net_lock(LNET_LOCK_EX);
3549 the_lnet.ln_mt_state = LNET_MT_STATE_SHUTDOWN;
3550 lnet_net_unlock(LNET_LOCK_EX);
3552 /* signal that the monitor thread is exiting */
3553 up(&the_lnet.ln_mt_signal);
3561 * Returns == 0 if success
3562 * Returns > 0 if LNetMDBind or prior fails
3563 * Returns < 0 if LNetGet fails
3566 lnet_send_ping(lnet_nid_t dest_nid,
3567 struct lnet_handle_md *mdh, int nnis,
3568 void *user_data, struct lnet_handle_eq eqh, bool recovery)
3570 struct lnet_md md = { NULL };
3571 struct lnet_process_id id;
3572 struct lnet_ping_buffer *pbuf;
3575 if (dest_nid == LNET_NID_ANY) {
3580 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
3586 /* initialize md content */
3587 md.start = &pbuf->pb_info;
3588 md.length = LNET_PING_INFO_SIZE(nnis);
3589 md.threshold = 2; /* GET/REPLY */
3591 md.options = LNET_MD_TRUNCATE;
3592 md.user_ptr = user_data;
3595 rc = LNetMDBind(md, LNET_UNLINK, mdh);
3597 lnet_ping_buffer_decref(pbuf);
3598 CERROR("Can't bind MD: %d\n", rc);
3599 rc = -rc; /* change the rc to positive */
3602 id.pid = LNET_PID_LUSTRE;
3605 rc = LNetGet(LNET_NID_ANY, *mdh, id,
3606 LNET_RESERVED_PORTAL,
3607 LNET_PROTO_PING_MATCHBITS, 0, recovery);
3610 goto fail_unlink_md;
3616 LNetInvalidateMDHandle(mdh);
3622 lnet_handle_recovery_reply(struct lnet_mt_event_info *ev_info,
3623 int status, bool unlink_event)
3625 lnet_nid_t nid = ev_info->mt_nid;
3627 if (ev_info->mt_type == MT_TYPE_LOCAL_NI) {
3631 ni = lnet_nid2ni_locked(nid, 0);
3637 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_PENDING;
3639 ni->ni_recovery_state |= LNET_NI_RECOVERY_FAILED;
3644 CERROR("local NI (%s) recovery failed with %d\n",
3645 libcfs_nid2str(nid), status);
3649 * need to increment healthv for the ni here, because in
3650 * the lnet_finalize() path we don't have access to this
3651 * NI. And in order to get access to it, we'll need to
3652 * carry forward too much information.
3653 * In the peer case, it'll naturally be incremented
3656 lnet_inc_healthv(&ni->ni_healthv);
3658 struct lnet_peer_ni *lpni;
3661 cpt = lnet_net_lock_current();
3662 lpni = lnet_find_peer_ni_locked(nid);
3664 lnet_net_unlock(cpt);
3667 spin_lock(&lpni->lpni_lock);
3668 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3670 lpni->lpni_state |= LNET_PEER_NI_RECOVERY_FAILED;
3671 spin_unlock(&lpni->lpni_lock);
3672 lnet_peer_ni_decref_locked(lpni);
3673 lnet_net_unlock(cpt);
3676 CERROR("peer NI (%s) recovery failed with %d\n",
3677 libcfs_nid2str(nid), status);
3682 lnet_mt_event_handler(struct lnet_event *event)
3684 struct lnet_mt_event_info *ev_info = event->md.user_ptr;
3685 struct lnet_ping_buffer *pbuf;
3687 /* TODO: remove assert */
3688 LASSERT(event->type == LNET_EVENT_REPLY ||
3689 event->type == LNET_EVENT_SEND ||
3690 event->type == LNET_EVENT_UNLINK);
3692 CDEBUG(D_NET, "Received event: %d status: %d\n", event->type,
3695 switch (event->type) {
3696 case LNET_EVENT_UNLINK:
3697 CDEBUG(D_NET, "%s recovery ping unlinked\n",
3698 libcfs_nid2str(ev_info->mt_nid));
3700 case LNET_EVENT_REPLY:
3701 lnet_handle_recovery_reply(ev_info, event->status,
3702 event->type == LNET_EVENT_UNLINK);