4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * lnet/lnet/lib-move.c
33 * Data movement routines
36 #define DEBUG_SUBSYSTEM S_LNET
38 #include <linux/pagemap.h>
40 #include <lnet/lib-lnet.h>
41 #include <linux/nsproxy.h>
42 #include <lnet/lnet_rdma.h>
43 #include <net/net_namespace.h>
45 static int local_nid_dist_zero = 1;
46 module_param(local_nid_dist_zero, int, 0444);
47 MODULE_PARM_DESC(local_nid_dist_zero, "Reserved");
49 struct lnet_send_data {
50 struct lnet_ni *sd_best_ni;
51 struct lnet_peer_ni *sd_best_lpni;
52 struct lnet_peer_ni *sd_final_dst_lpni;
53 struct lnet_peer *sd_peer;
54 struct lnet_peer *sd_gw_peer;
55 struct lnet_peer_ni *sd_gw_lpni;
56 struct lnet_peer_net *sd_peer_net;
57 struct lnet_msg *sd_msg;
58 struct lnet_nid sd_dst_nid;
59 struct lnet_nid sd_src_nid;
60 struct lnet_nid sd_rtr_nid;
67 lnet_msg_is_response(struct lnet_msg *msg)
69 return msg->msg_type == LNET_MSG_ACK || msg->msg_type == LNET_MSG_REPLY;
73 lnet_response_tracking_enabled(__u32 msg_type, unsigned int md_options)
75 if (md_options & LNET_MD_NO_TRACK_RESPONSE)
76 /* Explicitly disabled in MD options */
79 if (md_options & LNET_MD_TRACK_RESPONSE)
80 /* Explicity enabled in MD options */
83 if (lnet_response_tracking == 3)
84 /* Enabled for all message types */
87 if (msg_type == LNET_MSG_PUT)
88 return lnet_response_tracking == 2;
90 if (msg_type == LNET_MSG_GET)
91 return lnet_response_tracking == 1;
96 static inline struct lnet_comm_count *
97 get_stats_counts(struct lnet_element_stats *stats,
98 enum lnet_stats_type stats_type)
100 switch (stats_type) {
101 case LNET_STATS_TYPE_SEND:
102 return &stats->el_send_stats;
103 case LNET_STATS_TYPE_RECV:
104 return &stats->el_recv_stats;
105 case LNET_STATS_TYPE_DROP:
106 return &stats->el_drop_stats;
108 CERROR("Unknown stats type\n");
114 void lnet_incr_stats(struct lnet_element_stats *stats,
115 enum lnet_msg_type msg_type,
116 enum lnet_stats_type stats_type)
118 struct lnet_comm_count *counts = get_stats_counts(stats, stats_type);
124 atomic_inc(&counts->co_ack_count);
127 atomic_inc(&counts->co_put_count);
130 atomic_inc(&counts->co_get_count);
133 atomic_inc(&counts->co_reply_count);
136 atomic_inc(&counts->co_hello_count);
139 CERROR("There is a BUG in the code. Unknown message type\n");
144 __u32 lnet_sum_stats(struct lnet_element_stats *stats,
145 enum lnet_stats_type stats_type)
147 struct lnet_comm_count *counts = get_stats_counts(stats, stats_type);
151 return (atomic_read(&counts->co_ack_count) +
152 atomic_read(&counts->co_put_count) +
153 atomic_read(&counts->co_get_count) +
154 atomic_read(&counts->co_reply_count) +
155 atomic_read(&counts->co_hello_count));
158 static inline void assign_stats(struct lnet_ioctl_comm_count *msg_stats,
159 struct lnet_comm_count *counts)
161 msg_stats->ico_get_count = atomic_read(&counts->co_get_count);
162 msg_stats->ico_put_count = atomic_read(&counts->co_put_count);
163 msg_stats->ico_reply_count = atomic_read(&counts->co_reply_count);
164 msg_stats->ico_ack_count = atomic_read(&counts->co_ack_count);
165 msg_stats->ico_hello_count = atomic_read(&counts->co_hello_count);
168 void lnet_usr_translate_stats(struct lnet_ioctl_element_msg_stats *msg_stats,
169 struct lnet_element_stats *stats)
171 struct lnet_comm_count *counts;
176 counts = get_stats_counts(stats, LNET_STATS_TYPE_SEND);
179 assign_stats(&msg_stats->im_send_stats, counts);
181 counts = get_stats_counts(stats, LNET_STATS_TYPE_RECV);
184 assign_stats(&msg_stats->im_recv_stats, counts);
186 counts = get_stats_counts(stats, LNET_STATS_TYPE_DROP);
189 assign_stats(&msg_stats->im_drop_stats, counts);
193 lnet_fail_nid(lnet_nid_t nid4, unsigned int threshold)
195 struct lnet_test_peer *tp;
196 struct list_head *el;
197 struct list_head *next;
201 lnet_nid4_to_nid(nid4, &nid);
202 /* NB: use lnet_net_lock(0) to serialize operations on test peers */
203 if (threshold != 0) {
204 /* Adding a new entry */
205 LIBCFS_ALLOC(tp, sizeof(*tp));
210 tp->tp_threshold = threshold;
213 list_add_tail(&tp->tp_list, &the_lnet.ln_test_peers);
220 list_for_each_safe(el, next, &the_lnet.ln_test_peers) {
221 tp = list_entry(el, struct lnet_test_peer, tp_list);
223 if (tp->tp_threshold == 0 || /* needs culling anyway */
224 LNET_NID_IS_ANY(&nid) || /* removing all entries */
225 nid_same(&tp->tp_nid, &nid)) { /* matched this one */
226 list_move(&tp->tp_list, &cull);
232 while (!list_empty(&cull)) {
233 tp = list_entry(cull.next, struct lnet_test_peer, tp_list);
235 list_del(&tp->tp_list);
236 LIBCFS_FREE(tp, sizeof(*tp));
242 fail_peer(struct lnet_nid *nid, int outgoing)
244 struct lnet_test_peer *tp;
245 struct list_head *el;
246 struct list_head *next;
250 /* NB: use lnet_net_lock(0) to serialize operations on test peers */
253 list_for_each_safe(el, next, &the_lnet.ln_test_peers) {
254 tp = list_entry(el, struct lnet_test_peer, tp_list);
256 if (tp->tp_threshold == 0) {
259 /* only cull zombies on outgoing tests,
260 * since we may be at interrupt priority on
261 * incoming messages. */
262 list_move(&tp->tp_list, &cull);
267 if (LNET_NID_IS_ANY(&tp->tp_nid) || /* fail every peer */
268 nid_same(nid, &tp->tp_nid)) { /* fail this peer */
271 if (tp->tp_threshold != LNET_MD_THRESH_INF) {
274 tp->tp_threshold == 0) {
276 list_move(&tp->tp_list, &cull);
285 while (!list_empty(&cull)) {
286 tp = list_entry(cull.next, struct lnet_test_peer, tp_list);
287 list_del(&tp->tp_list);
289 LIBCFS_FREE(tp, sizeof(*tp));
296 lnet_iov_nob(unsigned int niov, struct kvec *iov)
298 unsigned int nob = 0;
300 LASSERT(niov == 0 || iov != NULL);
302 nob += (iov++)->iov_len;
306 EXPORT_SYMBOL(lnet_iov_nob);
309 lnet_copy_iov2iov(unsigned int ndiov, struct kvec *diov, unsigned int doffset,
310 unsigned int nsiov, struct kvec *siov, unsigned int soffset,
313 /* NB diov, siov are READ-ONLY */
314 unsigned int this_nob;
319 /* skip complete frags before 'doffset' */
321 while (doffset >= diov->iov_len) {
322 doffset -= diov->iov_len;
328 /* skip complete frags before 'soffset' */
330 while (soffset >= siov->iov_len) {
331 soffset -= siov->iov_len;
340 this_nob = min3((unsigned int)diov->iov_len - doffset,
341 (unsigned int)siov->iov_len - soffset,
344 memcpy((char *)diov->iov_base + doffset,
345 (char *)siov->iov_base + soffset, this_nob);
348 if (diov->iov_len > doffset + this_nob) {
356 if (siov->iov_len > soffset + this_nob) {
365 EXPORT_SYMBOL(lnet_copy_iov2iov);
368 lnet_kiov_nob(unsigned int niov, struct bio_vec *kiov)
370 unsigned int nob = 0;
372 LASSERT(niov == 0 || kiov != NULL);
374 nob += (kiov++)->bv_len;
378 EXPORT_SYMBOL(lnet_kiov_nob);
381 lnet_copy_kiov2kiov(unsigned int ndiov, struct bio_vec *diov,
382 unsigned int doffset,
383 unsigned int nsiov, struct bio_vec *siov,
384 unsigned int soffset,
387 /* NB diov, siov are READ-ONLY */
388 unsigned int this_nob;
395 LASSERT (!in_interrupt ());
398 while (doffset >= diov->bv_len) {
399 doffset -= diov->bv_len;
406 while (soffset >= siov->bv_len) {
407 soffset -= siov->bv_len;
416 this_nob = min3(diov->bv_len - doffset,
417 siov->bv_len - soffset,
421 daddr = ((char *)kmap(diov->bv_page)) +
422 diov->bv_offset + doffset;
424 saddr = ((char *)kmap(siov->bv_page)) +
425 siov->bv_offset + soffset;
427 /* Vanishing risk of kmap deadlock when mapping 2 pages.
428 * However in practice at least one of the kiovs will be mapped
429 * kernel pages and the map/unmap will be NOOPs */
431 memcpy (daddr, saddr, this_nob);
434 if (diov->bv_len > doffset + this_nob) {
438 kunmap(diov->bv_page);
445 if (siov->bv_len > soffset + this_nob) {
449 kunmap(siov->bv_page);
458 kunmap(diov->bv_page);
460 kunmap(siov->bv_page);
462 EXPORT_SYMBOL(lnet_copy_kiov2kiov);
465 lnet_copy_kiov2iov (unsigned int niov, struct kvec *iov, unsigned int iovoffset,
466 unsigned int nkiov, struct bio_vec *kiov,
467 unsigned int kiovoffset,
470 /* NB iov, kiov are READ-ONLY */
471 unsigned int this_nob;
477 LASSERT (!in_interrupt ());
480 while (iovoffset >= iov->iov_len) {
481 iovoffset -= iov->iov_len;
488 while (kiovoffset >= kiov->bv_len) {
489 kiovoffset -= kiov->bv_len;
498 this_nob = min3((unsigned int)iov->iov_len - iovoffset,
499 (unsigned int)kiov->bv_len - kiovoffset,
503 addr = ((char *)kmap(kiov->bv_page)) +
504 kiov->bv_offset + kiovoffset;
506 memcpy((char *)iov->iov_base + iovoffset, addr, this_nob);
509 if (iov->iov_len > iovoffset + this_nob) {
510 iovoffset += this_nob;
517 if (kiov->bv_len > kiovoffset + this_nob) {
519 kiovoffset += this_nob;
521 kunmap(kiov->bv_page);
531 kunmap(kiov->bv_page);
533 EXPORT_SYMBOL(lnet_copy_kiov2iov);
536 lnet_copy_iov2kiov(unsigned int nkiov, struct bio_vec *kiov,
537 unsigned int kiovoffset,
538 unsigned int niov, struct kvec *iov, unsigned int iovoffset,
541 /* NB kiov, iov are READ-ONLY */
542 unsigned int this_nob;
548 LASSERT (!in_interrupt ());
551 while (kiovoffset >= kiov->bv_len) {
552 kiovoffset -= kiov->bv_len;
559 while (iovoffset >= iov->iov_len) {
560 iovoffset -= iov->iov_len;
569 this_nob = min3((unsigned int)kiov->bv_len - kiovoffset,
570 (unsigned int)iov->iov_len - iovoffset,
574 addr = ((char *)kmap(kiov->bv_page)) +
575 kiov->bv_offset + kiovoffset;
577 memcpy (addr, (char *)iov->iov_base + iovoffset, this_nob);
580 if (kiov->bv_len > kiovoffset + this_nob) {
582 kiovoffset += this_nob;
584 kunmap(kiov->bv_page);
591 if (iov->iov_len > iovoffset + this_nob) {
592 iovoffset += this_nob;
601 kunmap(kiov->bv_page);
603 EXPORT_SYMBOL(lnet_copy_iov2kiov);
606 lnet_extract_kiov(int dst_niov, struct bio_vec *dst,
607 int src_niov, struct bio_vec *src,
608 unsigned int offset, unsigned int len)
610 /* Initialise 'dst' to the subset of 'src' starting at 'offset',
611 * for exactly 'len' bytes, and return the number of entries.
612 * NB not destructive to 'src' */
613 unsigned int frag_len;
616 if (len == 0) /* no data => */
617 return (0); /* no frags */
619 LASSERT(src_niov > 0);
620 while (offset >= src->bv_len) { /* skip initial frags */
621 offset -= src->bv_len;
624 LASSERT(src_niov > 0);
629 LASSERT(src_niov > 0);
630 LASSERT((int)niov <= dst_niov);
632 frag_len = src->bv_len - offset;
633 dst->bv_page = src->bv_page;
634 dst->bv_offset = src->bv_offset + offset;
636 if (len <= frag_len) {
638 LASSERT(dst->bv_offset + dst->bv_len <= PAGE_SIZE);
642 dst->bv_len = frag_len;
643 LASSERT(dst->bv_offset + dst->bv_len <= PAGE_SIZE);
653 EXPORT_SYMBOL(lnet_extract_kiov);
656 lnet_ni_recv(struct lnet_ni *ni, void *private, struct lnet_msg *msg,
657 int delayed, unsigned int offset, unsigned int mlen,
660 unsigned int niov = 0;
661 struct kvec *iov = NULL;
662 struct bio_vec *kiov = NULL;
665 LASSERT (!in_interrupt ());
666 LASSERT (mlen == 0 || msg != NULL);
669 LASSERT(msg->msg_receiving);
670 LASSERT(!msg->msg_sending);
671 LASSERT(rlen == msg->msg_len);
672 LASSERT(mlen <= msg->msg_len);
673 LASSERT(msg->msg_offset == offset);
674 LASSERT(msg->msg_wanted == mlen);
676 msg->msg_receiving = 0;
679 niov = msg->msg_niov;
680 kiov = msg->msg_kiov;
683 LASSERT ((iov == NULL) != (kiov == NULL));
687 rc = (ni->ni_net->net_lnd->lnd_recv)(ni, private, msg, delayed,
688 niov, kiov, offset, mlen,
691 lnet_finalize(msg, rc);
695 lnet_setpayloadbuffer(struct lnet_msg *msg)
697 struct lnet_libmd *md = msg->msg_md;
699 LASSERT(msg->msg_len > 0);
700 LASSERT(!msg->msg_routing);
702 LASSERT(msg->msg_niov == 0);
703 LASSERT(msg->msg_kiov == NULL);
705 msg->msg_niov = md->md_niov;
706 msg->msg_kiov = md->md_kiov;
710 lnet_prep_send(struct lnet_msg *msg, int type, struct lnet_processid *target,
711 unsigned int offset, unsigned int len)
713 msg->msg_type = type;
714 msg->msg_target = *target;
716 msg->msg_offset = offset;
719 lnet_setpayloadbuffer(msg);
721 memset (&msg->msg_hdr, 0, sizeof (msg->msg_hdr));
722 msg->msg_hdr.type = type;
723 /* dest_nid will be overwritten by lnet_select_pathway() */
724 msg->msg_hdr.dest_nid = target->nid;
725 msg->msg_hdr.dest_pid = target->pid;
726 /* src_nid will be set later */
727 msg->msg_hdr.src_pid = the_lnet.ln_pid;
728 msg->msg_hdr.payload_length = len;
732 lnet_ni_send(struct lnet_ni *ni, struct lnet_msg *msg)
734 void *priv = msg->msg_private;
737 LASSERT(!in_interrupt());
738 LASSERT(nid_is_lo0(&ni->ni_nid) ||
739 (msg->msg_txcredit && msg->msg_peertxcredit));
741 rc = (ni->ni_net->net_lnd->lnd_send)(ni, priv, msg);
743 msg->msg_no_resend = true;
744 lnet_finalize(msg, rc);
749 lnet_ni_eager_recv(struct lnet_ni *ni, struct lnet_msg *msg)
753 LASSERT(!msg->msg_sending);
754 LASSERT(msg->msg_receiving);
755 LASSERT(!msg->msg_rx_ready_delay);
756 LASSERT(ni->ni_net->net_lnd->lnd_eager_recv != NULL);
758 msg->msg_rx_ready_delay = 1;
759 rc = (ni->ni_net->net_lnd->lnd_eager_recv)(ni, msg->msg_private, msg,
762 CERROR("recv from %s / send to %s aborted: "
763 "eager_recv failed %d\n",
764 libcfs_nidstr(&msg->msg_rxpeer->lpni_nid),
765 libcfs_idstr(&msg->msg_target), rc);
766 LASSERT(rc < 0); /* required by my callers */
773 lnet_is_peer_deadline_passed(struct lnet_peer_ni *lpni, time64_t now)
777 deadline = lpni->lpni_last_alive +
778 lpni->lpni_net->net_tunables.lct_peer_timeout;
781 * assume peer_ni is alive as long as we're within the configured
790 /* NB: returns 1 when alive, 0 when dead, negative when error;
791 * may drop the lnet_net_lock */
793 lnet_peer_alive_locked(struct lnet_ni *ni, struct lnet_peer_ni *lpni,
794 struct lnet_msg *msg)
796 time64_t now = ktime_get_seconds();
798 if (!lnet_peer_aliveness_enabled(lpni))
802 * If we're resending a message, let's attempt to send it even if
803 * the peer is down to fulfill our resend quota on the message
805 if (msg->msg_retry_count > 0)
808 /* try and send recovery messages irregardless */
809 if (msg->msg_recovery)
812 /* always send any responses */
813 if (lnet_msg_is_response(msg))
816 if (!lnet_is_peer_deadline_passed(lpni, now))
819 return lnet_is_peer_ni_alive(lpni);
823 * \param msg The message to be sent.
824 * \param do_send True if lnet_ni_send() should be called in this function.
825 * lnet_send() is going to lnet_net_unlock immediately after this, so
826 * it sets do_send FALSE and I don't do the unlock/send/lock bit.
828 * \retval LNET_CREDIT_OK If \a msg sent or OK to send.
829 * \retval LNET_CREDIT_WAIT If \a msg blocked for credit.
830 * \retval -EHOSTUNREACH If the next hop of the message appears dead.
831 * \retval -ECANCELED If the MD of the message has been unlinked.
834 lnet_post_send_locked(struct lnet_msg *msg, int do_send)
836 struct lnet_peer_ni *lp = msg->msg_txpeer;
837 struct lnet_ni *ni = msg->msg_txni;
838 int cpt = msg->msg_tx_cpt;
839 struct lnet_tx_queue *tq = ni->ni_tx_queues[cpt];
841 /* non-lnet_send() callers have checked before */
842 LASSERT(!do_send || msg->msg_tx_delayed);
843 LASSERT(!msg->msg_receiving);
844 LASSERT(msg->msg_tx_committed);
846 /* can't get here if we're sending to the loopback interface */
847 if (the_lnet.ln_loni)
848 LASSERT(!nid_same(&lp->lpni_nid, &the_lnet.ln_loni->ni_nid));
850 /* NB 'lp' is always the next hop */
851 if ((msg->msg_target.pid & LNET_PID_USERFLAG) == 0 &&
852 lnet_peer_alive_locked(ni, lp, msg) == 0) {
853 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
854 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length +=
856 lnet_net_unlock(cpt);
858 lnet_incr_stats(&msg->msg_txpeer->lpni_stats,
860 LNET_STATS_TYPE_DROP);
862 lnet_incr_stats(&msg->msg_txni->ni_stats,
864 LNET_STATS_TYPE_DROP);
866 CNETERR("Dropping message for %s: peer not alive\n",
867 libcfs_idstr(&msg->msg_target));
868 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_DROPPED;
870 lnet_finalize(msg, -EHOSTUNREACH);
873 return -EHOSTUNREACH;
876 if (msg->msg_md != NULL &&
877 (msg->msg_md->md_flags & LNET_MD_FLAG_ABORTED) != 0) {
878 lnet_net_unlock(cpt);
880 CNETERR("Aborting message for %s: LNetM[DE]Unlink() already "
881 "called on the MD/ME.\n",
882 libcfs_idstr(&msg->msg_target));
884 msg->msg_no_resend = true;
885 CDEBUG(D_NET, "msg %p to %s canceled and will not be resent\n",
886 msg, libcfs_idstr(&msg->msg_target));
887 lnet_finalize(msg, -ECANCELED);
894 if (!msg->msg_peertxcredit) {
895 spin_lock(&lp->lpni_lock);
896 LASSERT((lp->lpni_txcredits < 0) ==
897 !list_empty(&lp->lpni_txq));
899 msg->msg_peertxcredit = 1;
900 lp->lpni_txqnob += msg->msg_len + sizeof(struct lnet_hdr_nid4);
901 lp->lpni_txcredits--;
903 if (lp->lpni_txcredits < lp->lpni_mintxcredits)
904 lp->lpni_mintxcredits = lp->lpni_txcredits;
906 if (lp->lpni_txcredits < 0) {
907 msg->msg_tx_delayed = 1;
908 list_add_tail(&msg->msg_list, &lp->lpni_txq);
909 spin_unlock(&lp->lpni_lock);
910 return LNET_CREDIT_WAIT;
912 spin_unlock(&lp->lpni_lock);
915 if (!msg->msg_txcredit) {
916 LASSERT((tq->tq_credits < 0) ==
917 !list_empty(&tq->tq_delayed));
919 msg->msg_txcredit = 1;
921 atomic_dec(&ni->ni_tx_credits);
923 if (tq->tq_credits < tq->tq_credits_min)
924 tq->tq_credits_min = tq->tq_credits;
926 if (tq->tq_credits < 0) {
927 msg->msg_tx_delayed = 1;
928 list_add_tail(&msg->msg_list, &tq->tq_delayed);
929 return LNET_CREDIT_WAIT;
933 if (unlikely(!list_empty(&the_lnet.ln_delay_rules)) &&
934 lnet_delay_rule_match_locked(&msg->msg_hdr, msg)) {
935 msg->msg_tx_delayed = 1;
936 return LNET_CREDIT_WAIT;
939 /* unset the tx_delay flag as we're going to send it now */
940 msg->msg_tx_delayed = 0;
943 lnet_net_unlock(cpt);
944 lnet_ni_send(ni, msg);
947 return LNET_CREDIT_OK;
951 static struct lnet_rtrbufpool *
952 lnet_msg2bufpool(struct lnet_msg *msg)
954 struct lnet_rtrbufpool *rbp;
957 LASSERT(msg->msg_rx_committed);
959 cpt = msg->msg_rx_cpt;
960 rbp = &the_lnet.ln_rtrpools[cpt][0];
962 LASSERT(msg->msg_len <= LNET_MTU);
963 while (msg->msg_len > (unsigned int)rbp->rbp_npages * PAGE_SIZE) {
965 LASSERT(rbp < &the_lnet.ln_rtrpools[cpt][LNET_NRBPOOLS]);
972 lnet_post_routed_recv_locked(struct lnet_msg *msg, int do_recv)
974 /* lnet_parse is going to lnet_net_unlock immediately after this, so it
975 * sets do_recv FALSE and I don't do the unlock/send/lock bit.
976 * I return LNET_CREDIT_WAIT if msg blocked and LNET_CREDIT_OK if
977 * received or OK to receive */
978 struct lnet_peer_ni *lpni = msg->msg_rxpeer;
979 struct lnet_peer *lp;
980 struct lnet_rtrbufpool *rbp;
981 struct lnet_rtrbuf *rb;
983 LASSERT(msg->msg_kiov == NULL);
984 LASSERT(msg->msg_niov == 0);
985 LASSERT(msg->msg_routing);
986 LASSERT(msg->msg_receiving);
987 LASSERT(!msg->msg_sending);
988 LASSERT(lpni->lpni_peer_net);
989 LASSERT(lpni->lpni_peer_net->lpn_peer);
991 lp = lpni->lpni_peer_net->lpn_peer;
993 /* non-lnet_parse callers only receive delayed messages */
994 LASSERT(!do_recv || msg->msg_rx_delayed);
996 if (!msg->msg_peerrtrcredit) {
997 /* lpni_lock protects the credit manipulation */
998 spin_lock(&lpni->lpni_lock);
1000 msg->msg_peerrtrcredit = 1;
1001 lpni->lpni_rtrcredits--;
1002 if (lpni->lpni_rtrcredits < lpni->lpni_minrtrcredits)
1003 lpni->lpni_minrtrcredits = lpni->lpni_rtrcredits;
1005 if (lpni->lpni_rtrcredits < 0) {
1006 spin_unlock(&lpni->lpni_lock);
1007 /* must have checked eager_recv before here */
1008 LASSERT(msg->msg_rx_ready_delay);
1009 msg->msg_rx_delayed = 1;
1010 /* lp_lock protects the lp_rtrq */
1011 spin_lock(&lp->lp_lock);
1012 list_add_tail(&msg->msg_list, &lp->lp_rtrq);
1013 spin_unlock(&lp->lp_lock);
1014 return LNET_CREDIT_WAIT;
1016 spin_unlock(&lpni->lpni_lock);
1019 rbp = lnet_msg2bufpool(msg);
1021 if (!msg->msg_rtrcredit) {
1022 msg->msg_rtrcredit = 1;
1024 if (rbp->rbp_credits < rbp->rbp_mincredits)
1025 rbp->rbp_mincredits = rbp->rbp_credits;
1027 if (rbp->rbp_credits < 0) {
1028 /* must have checked eager_recv before here */
1029 LASSERT(msg->msg_rx_ready_delay);
1030 msg->msg_rx_delayed = 1;
1031 list_add_tail(&msg->msg_list, &rbp->rbp_msgs);
1032 return LNET_CREDIT_WAIT;
1036 LASSERT(!list_empty(&rbp->rbp_bufs));
1037 rb = list_entry(rbp->rbp_bufs.next, struct lnet_rtrbuf, rb_list);
1038 list_del(&rb->rb_list);
1040 msg->msg_niov = rbp->rbp_npages;
1041 msg->msg_kiov = &rb->rb_kiov[0];
1043 /* unset the msg-rx_delayed flag since we're receiving the message */
1044 msg->msg_rx_delayed = 0;
1047 int cpt = msg->msg_rx_cpt;
1049 lnet_net_unlock(cpt);
1050 lnet_ni_recv(msg->msg_rxni, msg->msg_private, msg, 1,
1051 0, msg->msg_len, msg->msg_len);
1054 return LNET_CREDIT_OK;
1058 lnet_return_tx_credits_locked(struct lnet_msg *msg)
1060 struct lnet_peer_ni *txpeer = msg->msg_txpeer;
1061 struct lnet_ni *txni = msg->msg_txni;
1062 struct lnet_msg *msg2;
1064 if (msg->msg_txcredit) {
1065 struct lnet_ni *ni = msg->msg_txni;
1066 struct lnet_tx_queue *tq = ni->ni_tx_queues[msg->msg_tx_cpt];
1068 /* give back NI txcredits */
1069 msg->msg_txcredit = 0;
1071 LASSERT((tq->tq_credits < 0) ==
1072 !list_empty(&tq->tq_delayed));
1075 atomic_inc(&ni->ni_tx_credits);
1076 if (tq->tq_credits <= 0) {
1077 msg2 = list_entry(tq->tq_delayed.next,
1078 struct lnet_msg, msg_list);
1079 list_del(&msg2->msg_list);
1081 LASSERT(msg2->msg_txni == ni);
1082 LASSERT(msg2->msg_tx_delayed);
1083 LASSERT(msg2->msg_tx_cpt == msg->msg_tx_cpt);
1085 (void) lnet_post_send_locked(msg2, 1);
1089 if (msg->msg_peertxcredit) {
1090 /* give back peer txcredits */
1091 msg->msg_peertxcredit = 0;
1093 spin_lock(&txpeer->lpni_lock);
1094 LASSERT((txpeer->lpni_txcredits < 0) ==
1095 !list_empty(&txpeer->lpni_txq));
1097 txpeer->lpni_txqnob -= msg->msg_len +
1098 sizeof(struct lnet_hdr_nid4);
1099 LASSERT(txpeer->lpni_txqnob >= 0);
1101 txpeer->lpni_txcredits++;
1102 if (txpeer->lpni_txcredits <= 0) {
1105 msg2 = list_entry(txpeer->lpni_txq.next,
1106 struct lnet_msg, msg_list);
1107 list_del(&msg2->msg_list);
1108 spin_unlock(&txpeer->lpni_lock);
1110 LASSERT(msg2->msg_txpeer == txpeer);
1111 LASSERT(msg2->msg_tx_delayed);
1113 msg2_cpt = msg2->msg_tx_cpt;
1116 * The msg_cpt can be different from the msg2_cpt
1117 * so we need to make sure we lock the correct cpt
1119 * Once we call lnet_post_send_locked() it is no
1120 * longer safe to access msg2, since it could've
1121 * been freed by lnet_finalize(), but we still
1122 * need to relock the correct cpt, so we cache the
1123 * msg2_cpt for the purpose of the check that
1124 * follows the call to lnet_pose_send_locked().
1126 if (msg2_cpt != msg->msg_tx_cpt) {
1127 lnet_net_unlock(msg->msg_tx_cpt);
1128 lnet_net_lock(msg2_cpt);
1130 (void) lnet_post_send_locked(msg2, 1);
1131 if (msg2_cpt != msg->msg_tx_cpt) {
1132 lnet_net_unlock(msg2_cpt);
1133 lnet_net_lock(msg->msg_tx_cpt);
1136 spin_unlock(&txpeer->lpni_lock);
1141 msg->msg_txni = NULL;
1142 lnet_ni_decref_locked(txni, msg->msg_tx_cpt);
1145 if (txpeer != NULL) {
1146 msg->msg_txpeer = NULL;
1147 lnet_peer_ni_decref_locked(txpeer);
1152 lnet_schedule_blocked_locked(struct lnet_rtrbufpool *rbp)
1154 struct lnet_msg *msg;
1156 if (list_empty(&rbp->rbp_msgs))
1158 msg = list_entry(rbp->rbp_msgs.next,
1159 struct lnet_msg, msg_list);
1160 list_del(&msg->msg_list);
1162 (void)lnet_post_routed_recv_locked(msg, 1);
1166 lnet_drop_routed_msgs_locked(struct list_head *list, int cpt)
1168 struct lnet_msg *msg;
1169 struct lnet_msg *tmp;
1171 lnet_net_unlock(cpt);
1173 list_for_each_entry_safe(msg, tmp, list, msg_list) {
1174 lnet_ni_recv(msg->msg_rxni, msg->msg_private, NULL,
1175 0, 0, 0, msg->msg_hdr.payload_length);
1176 list_del_init(&msg->msg_list);
1177 msg->msg_no_resend = true;
1178 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_ERROR;
1179 lnet_finalize(msg, -ECANCELED);
1186 lnet_return_rx_credits_locked(struct lnet_msg *msg)
1188 struct lnet_peer_ni *rxpeerni = msg->msg_rxpeer;
1189 struct lnet_peer *lp;
1190 struct lnet_ni *rxni = msg->msg_rxni;
1191 struct lnet_msg *msg2;
1193 if (msg->msg_rtrcredit) {
1194 /* give back global router credits */
1195 struct lnet_rtrbuf *rb;
1196 struct lnet_rtrbufpool *rbp;
1198 /* NB If a msg ever blocks for a buffer in rbp_msgs, it stays
1199 * there until it gets one allocated, or aborts the wait
1201 LASSERT(msg->msg_kiov != NULL);
1203 rb = list_entry(msg->msg_kiov, struct lnet_rtrbuf, rb_kiov[0]);
1206 msg->msg_kiov = NULL;
1207 msg->msg_rtrcredit = 0;
1209 LASSERT(rbp == lnet_msg2bufpool(msg));
1211 LASSERT((rbp->rbp_credits > 0) ==
1212 !list_empty(&rbp->rbp_bufs));
1214 /* If routing is now turned off, we just drop this buffer and
1215 * don't bother trying to return credits. */
1216 if (!the_lnet.ln_routing) {
1217 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1221 /* It is possible that a user has lowered the desired number of
1222 * buffers in this pool. Make sure we never put back
1223 * more buffers than the stated number. */
1224 if (unlikely(rbp->rbp_credits >= rbp->rbp_req_nbuffers)) {
1225 /* Discard this buffer so we don't have too
1227 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1228 rbp->rbp_nbuffers--;
1230 list_add(&rb->rb_list, &rbp->rbp_bufs);
1232 if (rbp->rbp_credits <= 0)
1233 lnet_schedule_blocked_locked(rbp);
1238 if (msg->msg_peerrtrcredit) {
1240 LASSERT(rxpeerni->lpni_peer_net);
1241 LASSERT(rxpeerni->lpni_peer_net->lpn_peer);
1243 /* give back peer router credits */
1244 msg->msg_peerrtrcredit = 0;
1246 spin_lock(&rxpeerni->lpni_lock);
1247 rxpeerni->lpni_rtrcredits++;
1248 spin_unlock(&rxpeerni->lpni_lock);
1250 lp = rxpeerni->lpni_peer_net->lpn_peer;
1251 spin_lock(&lp->lp_lock);
1253 /* drop all messages which are queued to be routed on that
1255 if (!the_lnet.ln_routing) {
1257 list_splice_init(&lp->lp_rtrq, &drop);
1258 spin_unlock(&lp->lp_lock);
1259 lnet_drop_routed_msgs_locked(&drop, msg->msg_rx_cpt);
1260 } else if (!list_empty(&lp->lp_rtrq)) {
1263 msg2 = list_entry(lp->lp_rtrq.next,
1264 struct lnet_msg, msg_list);
1265 list_del(&msg2->msg_list);
1266 msg2_cpt = msg2->msg_rx_cpt;
1267 spin_unlock(&lp->lp_lock);
1269 * messages on the lp_rtrq can be from any NID in
1270 * the peer, which means they might have different
1271 * cpts. We need to make sure we lock the right
1274 if (msg2_cpt != msg->msg_rx_cpt) {
1275 lnet_net_unlock(msg->msg_rx_cpt);
1276 lnet_net_lock(msg2_cpt);
1278 (void) lnet_post_routed_recv_locked(msg2, 1);
1279 if (msg2_cpt != msg->msg_rx_cpt) {
1280 lnet_net_unlock(msg2_cpt);
1281 lnet_net_lock(msg->msg_rx_cpt);
1284 spin_unlock(&lp->lp_lock);
1288 msg->msg_rxni = NULL;
1289 lnet_ni_decref_locked(rxni, msg->msg_rx_cpt);
1291 if (rxpeerni != NULL) {
1292 msg->msg_rxpeer = NULL;
1293 lnet_peer_ni_decref_locked(rxpeerni);
1297 static struct lnet_peer_ni *
1298 lnet_select_peer_ni(struct lnet_ni *best_ni, lnet_nid_t dst_nid,
1299 struct lnet_peer *peer,
1300 struct lnet_peer_ni *best_lpni,
1301 struct lnet_peer_net *peer_net)
1304 * Look at the peer NIs for the destination peer that connect
1305 * to the chosen net. If a peer_ni is preferred when using the
1306 * best_ni to communicate, we use that one. If there is no
1307 * preferred peer_ni, or there are multiple preferred peer_ni,
1308 * the available transmit credits are used. If the transmit
1309 * credits are equal, we round-robin over the peer_ni.
1311 struct lnet_peer_ni *lpni = NULL;
1312 int best_lpni_credits = (best_lpni) ? best_lpni->lpni_txcredits :
1314 int best_lpni_healthv = (best_lpni) ?
1315 atomic_read(&best_lpni->lpni_healthv) : 0;
1316 bool best_lpni_is_preferred = false;
1317 bool lpni_is_preferred;
1319 __u32 lpni_sel_prio;
1320 __u32 best_sel_prio = LNET_MAX_SELECTION_PRIORITY;
1322 while ((lpni = lnet_get_next_peer_ni_locked(peer, peer_net, lpni))) {
1324 * if the best_ni we've chosen aleady has this lpni
1325 * preferred, then let's use it
1328 lpni_is_preferred = lnet_peer_is_pref_nid_locked(
1329 lpni, &best_ni->ni_nid);
1330 CDEBUG(D_NET, "%s lpni_is_preferred = %d\n",
1331 libcfs_nidstr(&best_ni->ni_nid),
1334 lpni_is_preferred = false;
1337 lpni_healthv = atomic_read(&lpni->lpni_healthv);
1338 lpni_sel_prio = lpni->lpni_sel_priority;
1341 CDEBUG(D_NET, "n:[%s, %s] h:[%d, %d] p:[%d, %d] c:[%d, %d] s:[%d, %d]\n",
1342 libcfs_nidstr(&lpni->lpni_nid),
1343 libcfs_nidstr(&best_lpni->lpni_nid),
1344 lpni_healthv, best_lpni_healthv,
1345 lpni_sel_prio, best_sel_prio,
1346 lpni->lpni_txcredits, best_lpni_credits,
1347 lpni->lpni_seq, best_lpni->lpni_seq);
1351 /* pick the healthiest peer ni */
1352 if (lpni_healthv < best_lpni_healthv)
1354 else if (lpni_healthv > best_lpni_healthv) {
1355 if (best_lpni_is_preferred)
1356 best_lpni_is_preferred = false;
1360 if (lpni_sel_prio > best_sel_prio)
1362 else if (lpni_sel_prio < best_sel_prio) {
1363 if (best_lpni_is_preferred)
1364 best_lpni_is_preferred = false;
1368 /* if this is a preferred peer use it */
1369 if (!best_lpni_is_preferred && lpni_is_preferred) {
1370 best_lpni_is_preferred = true;
1372 } else if (best_lpni_is_preferred && !lpni_is_preferred) {
1373 /* this is not the preferred peer so let's ignore
1379 if (lpni->lpni_txcredits < best_lpni_credits)
1380 /* We already have a peer that has more credits
1381 * available than this one. No need to consider
1382 * this peer further.
1385 else if (lpni->lpni_txcredits > best_lpni_credits)
1388 /* The best peer found so far and the current peer
1389 * have the same number of available credits let's
1390 * make sure to select between them using Round Robin
1392 if (best_lpni && (best_lpni->lpni_seq <= lpni->lpni_seq))
1395 best_lpni_is_preferred = lpni_is_preferred;
1396 best_lpni_healthv = lpni_healthv;
1397 best_sel_prio = lpni_sel_prio;
1399 best_lpni_credits = lpni->lpni_txcredits;
1402 /* if we still can't find a peer ni then we can't reach it */
1404 __u32 net_id = (peer_net) ? peer_net->lpn_net_id :
1405 LNET_NIDNET(dst_nid);
1406 CDEBUG(D_NET, "no peer_ni found on peer net %s\n",
1407 libcfs_net2str(net_id));
1411 CDEBUG(D_NET, "sd_best_lpni = %s\n",
1412 libcfs_nidstr(&best_lpni->lpni_nid));
1418 * Prerequisite: the best_ni should already be set in the sd
1419 * Find the best lpni.
1420 * If the net id is provided then restrict lpni selection on
1421 * that particular net.
1422 * Otherwise find any reachable lpni. When dealing with an MR
1423 * gateway and it has multiple lpnis which we can use
1424 * we want to select the best one from the list of reachable
1427 static inline struct lnet_peer_ni *
1428 lnet_find_best_lpni(struct lnet_ni *lni, lnet_nid_t dst_nid,
1429 struct lnet_peer *peer, __u32 net_id)
1431 struct lnet_peer_net *peer_net;
1433 /* find the best_lpni on any local network */
1434 if (net_id == LNET_NET_ANY) {
1435 struct lnet_peer_ni *best_lpni = NULL;
1436 struct lnet_peer_net *lpn;
1437 list_for_each_entry(lpn, &peer->lp_peer_nets, lpn_peer_nets) {
1438 /* no net specified find any reachable peer ni */
1439 if (!lnet_islocalnet_locked(lpn->lpn_net_id))
1441 best_lpni = lnet_select_peer_ni(lni, dst_nid, peer,
1447 /* restrict on the specified net */
1448 peer_net = lnet_peer_get_net_locked(peer, net_id);
1450 return lnet_select_peer_ni(lni, dst_nid, peer, NULL, peer_net);
1456 lnet_compare_gw_lpnis(struct lnet_peer_ni *lpni1, struct lnet_peer_ni *lpni2)
1458 if (lpni1->lpni_txqnob < lpni2->lpni_txqnob)
1461 if (lpni1->lpni_txqnob > lpni2->lpni_txqnob)
1464 if (lpni1->lpni_txcredits > lpni2->lpni_txcredits)
1467 if (lpni1->lpni_txcredits < lpni2->lpni_txcredits)
1473 /* Compare route priorities and hop counts */
1475 lnet_compare_routes(struct lnet_route *r1, struct lnet_route *r2)
1477 int r1_hops = (r1->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r1->lr_hops;
1478 int r2_hops = (r2->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r2->lr_hops;
1480 if (r1->lr_priority < r2->lr_priority)
1483 if (r1->lr_priority > r2->lr_priority)
1486 if (r1_hops < r2_hops)
1489 if (r1_hops > r2_hops)
1495 static struct lnet_route *
1496 lnet_find_route_locked(struct lnet_remotenet *rnet, __u32 src_net,
1497 struct lnet_peer_ni *remote_lpni,
1498 struct lnet_route **prev_route,
1499 struct lnet_peer_ni **gwni)
1501 struct lnet_peer_ni *lpni, *best_gw_ni = NULL;
1502 struct lnet_route *best_route;
1503 struct lnet_route *last_route;
1504 struct lnet_route *route;
1506 bool best_rte_is_preferred = false;
1507 struct lnet_nid *gw_pnid;
1509 CDEBUG(D_NET, "Looking up a route to %s, from %s\n",
1510 libcfs_net2str(rnet->lrn_net), libcfs_net2str(src_net));
1512 best_route = last_route = NULL;
1513 list_for_each_entry(route, &rnet->lrn_routes, lr_list) {
1514 if (!lnet_is_route_alive(route))
1516 gw_pnid = &route->lr_gateway->lp_primary_nid;
1518 /* no protection on below fields, but it's harmless */
1519 if (last_route && (last_route->lr_seq - route->lr_seq < 0))
1522 /* if the best route found is in the preferred list then
1523 * tag it as preferred and use it later on. But if we
1524 * didn't find any routes which are on the preferred list
1525 * then just use the best route possible.
1527 rc = lnet_peer_is_pref_rtr_locked(remote_lpni, gw_pnid);
1529 if (!best_route || (rc && !best_rte_is_preferred)) {
1530 /* Restrict the selection of the router NI on the
1531 * src_net provided. If the src_net is LNET_NID_ANY,
1532 * then select the best interface available.
1534 lpni = lnet_find_best_lpni(NULL, LNET_NID_ANY,
1539 "Gateway %s does not have a peer NI on net %s\n",
1540 libcfs_nidstr(gw_pnid),
1541 libcfs_net2str(src_net));
1546 if (rc && !best_rte_is_preferred) {
1547 /* This is the first preferred route we found,
1548 * so it beats any route found previously
1554 best_rte_is_preferred = true;
1555 CDEBUG(D_NET, "preferred gw = %s\n",
1556 libcfs_nidstr(gw_pnid));
1558 } else if ((!rc) && best_rte_is_preferred)
1559 /* The best route we found so far is in the preferred
1560 * list, so it beats any non-preferred route
1565 best_route = last_route = route;
1570 rc = lnet_compare_routes(route, best_route);
1574 /* Restrict the selection of the router NI on the
1575 * src_net provided. If the src_net is LNET_NID_ANY,
1576 * then select the best interface available.
1578 lpni = lnet_find_best_lpni(NULL, LNET_NID_ANY,
1583 "Gateway %s does not have a peer NI on net %s\n",
1584 libcfs_nidstr(gw_pnid),
1585 libcfs_net2str(src_net));
1595 rc = lnet_compare_gw_lpnis(lpni, best_gw_ni);
1599 if (rc == 1 || route->lr_seq <= best_route->lr_seq) {
1606 *prev_route = last_route;
1612 static inline unsigned int
1613 lnet_dev_prio_of_md(struct lnet_ni *ni, unsigned int dev_idx)
1615 if (dev_idx == UINT_MAX)
1618 if (!ni || !ni->ni_net || !ni->ni_net->net_lnd ||
1619 !ni->ni_net->net_lnd->lnd_get_dev_prio)
1622 return ni->ni_net->net_lnd->lnd_get_dev_prio(ni, dev_idx);
1625 static struct lnet_ni *
1626 lnet_get_best_ni(struct lnet_net *local_net, struct lnet_ni *best_ni,
1627 struct lnet_peer *peer, struct lnet_peer_net *peer_net,
1628 struct lnet_msg *msg, int md_cpt)
1630 struct lnet_libmd *md = msg->msg_md;
1631 unsigned int offset = msg->msg_offset;
1632 unsigned int shortest_distance;
1633 struct lnet_ni *ni = NULL;
1636 __u32 best_sel_prio;
1637 unsigned int best_dev_prio;
1638 unsigned int dev_idx = UINT_MAX;
1639 struct page *page = lnet_get_first_page(md, offset);
1640 msg->msg_rdma_force = lnet_is_rdma_only_page(page);
1642 if (msg->msg_rdma_force)
1643 dev_idx = lnet_get_dev_idx(page);
1646 * If there is no peer_ni that we can send to on this network,
1647 * then there is no point in looking for a new best_ni here.
1649 if (!lnet_get_next_peer_ni_locked(peer, peer_net, NULL))
1652 if (best_ni == NULL) {
1653 best_sel_prio = LNET_MAX_SELECTION_PRIORITY;
1654 shortest_distance = UINT_MAX;
1655 best_dev_prio = UINT_MAX;
1656 best_credits = INT_MIN;
1659 best_dev_prio = lnet_dev_prio_of_md(best_ni, dev_idx);
1660 shortest_distance = cfs_cpt_distance(lnet_cpt_table(), md_cpt,
1661 best_ni->ni_dev_cpt);
1662 best_credits = atomic_read(&best_ni->ni_tx_credits);
1663 best_healthv = atomic_read(&best_ni->ni_healthv);
1664 best_sel_prio = best_ni->ni_sel_priority;
1667 while ((ni = lnet_get_next_ni_locked(local_net, ni))) {
1668 unsigned int distance;
1673 unsigned int ni_dev_prio;
1675 ni_credits = atomic_read(&ni->ni_tx_credits);
1676 ni_healthv = atomic_read(&ni->ni_healthv);
1677 ni_fatal = atomic_read(&ni->ni_fatal_error_on);
1678 ni_sel_prio = ni->ni_sel_priority;
1681 * calculate the distance from the CPT on which
1682 * the message memory is allocated to the CPT of
1683 * the NI's physical device
1685 distance = cfs_cpt_distance(lnet_cpt_table(),
1689 ni_dev_prio = lnet_dev_prio_of_md(ni, dev_idx);
1692 * All distances smaller than the NUMA range
1693 * are treated equally.
1695 if (distance < lnet_numa_range)
1696 distance = lnet_numa_range;
1699 * Select on health, selection policy, direct dma prio,
1700 * shorter distance, available credits, then round-robin.
1706 CDEBUG(D_NET, "compare ni %s [c:%d, d:%d, s:%d, p:%u, g:%u] with best_ni %s [c:%d, d:%d, s:%d, p:%u, g:%u]\n",
1707 libcfs_nidstr(&ni->ni_nid), ni_credits, distance,
1708 ni->ni_seq, ni_sel_prio, ni_dev_prio,
1709 (best_ni) ? libcfs_nidstr(&best_ni->ni_nid)
1710 : "not selected", best_credits, shortest_distance,
1711 (best_ni) ? best_ni->ni_seq : 0,
1712 best_sel_prio, best_dev_prio);
1716 if (ni_healthv < best_healthv)
1718 else if (ni_healthv > best_healthv)
1721 if (ni_sel_prio > best_sel_prio)
1723 else if (ni_sel_prio < best_sel_prio)
1726 if (ni_dev_prio > best_dev_prio)
1728 else if (ni_dev_prio < best_dev_prio)
1731 if (distance > shortest_distance)
1733 else if (distance < shortest_distance)
1736 if (ni_credits < best_credits)
1738 else if (ni_credits > best_credits)
1741 if (best_ni && best_ni->ni_seq <= ni->ni_seq)
1745 best_sel_prio = ni_sel_prio;
1746 best_dev_prio = ni_dev_prio;
1747 shortest_distance = distance;
1748 best_healthv = ni_healthv;
1750 best_credits = ni_credits;
1753 CDEBUG(D_NET, "selected best_ni %s\n",
1754 (best_ni) ? libcfs_nidstr(&best_ni->ni_nid) : "no selection");
1760 * Traffic to the LNET_RESERVED_PORTAL may not trigger peer discovery,
1761 * because such traffic is required to perform discovery. We therefore
1762 * exclude all GET and PUT on that portal. We also exclude all ACK and
1763 * REPLY traffic, but that is because the portal is not tracked in the
1764 * message structure for these message types. We could restrict this
1765 * further by also checking for LNET_PROTO_PING_MATCHBITS.
1768 lnet_msg_discovery(struct lnet_msg *msg)
1770 if (msg->msg_type == LNET_MSG_PUT) {
1771 if (msg->msg_hdr.msg.put.ptl_index != LNET_RESERVED_PORTAL)
1773 } else if (msg->msg_type == LNET_MSG_GET) {
1774 if (msg->msg_hdr.msg.get.ptl_index != LNET_RESERVED_PORTAL)
1780 #define SRC_SPEC 0x0001
1781 #define SRC_ANY 0x0002
1782 #define LOCAL_DST 0x0004
1783 #define REMOTE_DST 0x0008
1784 #define MR_DST 0x0010
1785 #define NMR_DST 0x0020
1786 #define SND_RESP 0x0040
1788 /* The following to defines are used for return codes */
1789 #define REPEAT_SEND 0x1000
1790 #define PASS_THROUGH 0x2000
1792 /* The different cases lnet_select pathway needs to handle */
1793 #define SRC_SPEC_LOCAL_MR_DST (SRC_SPEC | LOCAL_DST | MR_DST)
1794 #define SRC_SPEC_ROUTER_MR_DST (SRC_SPEC | REMOTE_DST | MR_DST)
1795 #define SRC_SPEC_LOCAL_NMR_DST (SRC_SPEC | LOCAL_DST | NMR_DST)
1796 #define SRC_SPEC_ROUTER_NMR_DST (SRC_SPEC | REMOTE_DST | NMR_DST)
1797 #define SRC_ANY_LOCAL_MR_DST (SRC_ANY | LOCAL_DST | MR_DST)
1798 #define SRC_ANY_ROUTER_MR_DST (SRC_ANY | REMOTE_DST | MR_DST)
1799 #define SRC_ANY_LOCAL_NMR_DST (SRC_ANY | LOCAL_DST | NMR_DST)
1800 #define SRC_ANY_ROUTER_NMR_DST (SRC_ANY | REMOTE_DST | NMR_DST)
1803 lnet_handle_lo_send(struct lnet_send_data *sd)
1805 struct lnet_msg *msg = sd->sd_msg;
1806 int cpt = sd->sd_cpt;
1808 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1811 /* No send credit hassles with LOLND */
1812 lnet_ni_addref_locked(the_lnet.ln_loni, cpt);
1813 msg->msg_hdr.dest_nid = the_lnet.ln_loni->ni_nid;
1814 if (!msg->msg_routing)
1815 msg->msg_hdr.src_nid = the_lnet.ln_loni->ni_nid;
1816 msg->msg_target.nid = the_lnet.ln_loni->ni_nid;
1817 lnet_msg_commit(msg, cpt);
1818 msg->msg_txni = the_lnet.ln_loni;
1820 return LNET_CREDIT_OK;
1824 lnet_handle_send(struct lnet_send_data *sd)
1826 struct lnet_ni *best_ni = sd->sd_best_ni;
1827 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
1828 struct lnet_peer_ni *final_dst_lpni = sd->sd_final_dst_lpni;
1829 struct lnet_msg *msg = sd->sd_msg;
1831 __u32 send_case = sd->sd_send_case;
1833 __u32 routing = send_case & REMOTE_DST;
1834 struct lnet_rsp_tracker *rspt;
1836 /* Increment sequence number of the selected peer, peer net,
1837 * local ni and local net so that we pick the next ones
1840 best_lpni->lpni_peer_net->lpn_seq++;
1841 best_lpni->lpni_seq = best_lpni->lpni_peer_net->lpn_seq;
1842 best_ni->ni_net->net_seq++;
1843 best_ni->ni_seq = best_ni->ni_net->net_seq;
1845 CDEBUG(D_NET, "%s NI seq info: [%d:%d:%d:%u] %s LPNI seq info [%d:%d:%d:%u]\n",
1846 libcfs_nidstr(&best_ni->ni_nid),
1847 best_ni->ni_seq, best_ni->ni_net->net_seq,
1848 atomic_read(&best_ni->ni_tx_credits),
1849 best_ni->ni_sel_priority,
1850 libcfs_nidstr(&best_lpni->lpni_nid),
1851 best_lpni->lpni_seq, best_lpni->lpni_peer_net->lpn_seq,
1852 best_lpni->lpni_txcredits,
1853 best_lpni->lpni_sel_priority);
1856 * grab a reference on the peer_ni so it sticks around even if
1857 * we need to drop and relock the lnet_net_lock below.
1859 lnet_peer_ni_addref_locked(best_lpni);
1862 * Use lnet_cpt_of_nid() to determine the CPT used to commit the
1863 * message. This ensures that we get a CPT that is correct for
1864 * the NI when the NI has been restricted to a subset of all CPTs.
1865 * If the selected CPT differs from the one currently locked, we
1866 * must unlock and relock the lnet_net_lock(), and then check whether
1867 * the configuration has changed. We don't have a hold on the best_ni
1868 * yet, and it may have vanished.
1870 cpt2 = lnet_cpt_of_nid_locked(&best_lpni->lpni_nid, best_ni);
1871 if (sd->sd_cpt != cpt2) {
1872 __u32 seq = lnet_get_dlc_seq_locked();
1873 lnet_net_unlock(sd->sd_cpt);
1875 lnet_net_lock(sd->sd_cpt);
1876 if (seq != lnet_get_dlc_seq_locked()) {
1877 lnet_peer_ni_decref_locked(best_lpni);
1883 * store the best_lpni in the message right away to avoid having
1884 * to do the same operation under different conditions
1886 msg->msg_txpeer = best_lpni;
1887 msg->msg_txni = best_ni;
1890 * grab a reference for the best_ni since now it's in use in this
1891 * send. The reference will be dropped in lnet_finalize()
1893 lnet_ni_addref_locked(msg->msg_txni, sd->sd_cpt);
1896 * Always set the target.nid to the best peer picked. Either the
1897 * NID will be one of the peer NIDs selected, or the same NID as
1898 * what was originally set in the target or it will be the NID of
1899 * a router if this message should be routed
1901 msg->msg_target.nid = msg->msg_txpeer->lpni_nid;
1904 * lnet_msg_commit assigns the correct cpt to the message, which
1905 * is used to decrement the correct refcount on the ni when it's
1906 * time to return the credits
1908 lnet_msg_commit(msg, sd->sd_cpt);
1911 * If we are routing the message then we keep the src_nid that was
1912 * set by the originator. If we are not routing then we are the
1913 * originator and set it here.
1915 if (!msg->msg_routing)
1916 msg->msg_hdr.src_nid = msg->msg_txni->ni_nid;
1919 msg->msg_target_is_router = 1;
1920 msg->msg_target.pid = LNET_PID_LUSTRE;
1922 * since we're routing we want to ensure that the
1923 * msg_hdr.dest_nid is set to the final destination. When
1924 * the router receives this message it knows how to route
1927 * final_dst_lpni is set at the beginning of the
1928 * lnet_select_pathway() function and is never changed.
1929 * It's safe to use it here.
1931 msg->msg_hdr.dest_nid = final_dst_lpni->lpni_nid;
1934 * if we're not routing set the dest_nid to the best peer
1935 * ni NID that we picked earlier in the algorithm.
1937 msg->msg_hdr.dest_nid = msg->msg_txpeer->lpni_nid;
1941 * if we have response tracker block update it with the next hop
1945 rspt = msg->msg_md->md_rspt_ptr;
1947 rspt->rspt_next_hop_nid =
1948 msg->msg_txpeer->lpni_nid;
1949 CDEBUG(D_NET, "rspt_next_hop_nid = %s\n",
1950 libcfs_nidstr(&rspt->rspt_next_hop_nid));
1954 rc = lnet_post_send_locked(msg, 0);
1957 CDEBUG(D_NET, "TRACE: %s(%s:%s) -> %s(%s:%s) %s : %s try# %d\n",
1958 libcfs_nidstr(&msg->msg_hdr.src_nid),
1959 libcfs_nidstr(&msg->msg_txni->ni_nid),
1960 libcfs_nidstr(&sd->sd_src_nid),
1961 libcfs_nidstr(&msg->msg_hdr.dest_nid),
1962 libcfs_nidstr(&sd->sd_dst_nid),
1963 libcfs_nidstr(&msg->msg_txpeer->lpni_nid),
1964 libcfs_nidstr(&sd->sd_rtr_nid),
1965 lnet_msgtyp2str(msg->msg_type), msg->msg_retry_count);
1971 lnet_set_non_mr_pref_nid(struct lnet_peer_ni *lpni, struct lnet_ni *lni,
1972 struct lnet_msg *msg)
1974 if (!lnet_peer_is_multi_rail(lpni->lpni_peer_net->lpn_peer) &&
1975 !lnet_msg_is_response(msg) && lpni->lpni_pref_nnids == 0) {
1976 CDEBUG(D_NET, "Setting preferred local NID %s on NMR peer %s\n",
1977 libcfs_nidstr(&lni->ni_nid),
1978 libcfs_nidstr(&lpni->lpni_nid));
1979 lnet_peer_ni_set_non_mr_pref_nid(lpni, &lni->ni_nid);
1988 * use the source and destination NIDs as the pathway
1991 lnet_handle_spec_local_nmr_dst(struct lnet_send_data *sd)
1993 /* the destination lpni is set before we get here. */
1996 sd->sd_best_ni = lnet_nid_to_ni_locked(&sd->sd_src_nid, sd->sd_cpt);
1997 if (!sd->sd_best_ni) {
1998 CERROR("Can't send to %s: src %s is not a local nid\n",
1999 libcfs_nidstr(&sd->sd_dst_nid),
2000 libcfs_nidstr(&sd->sd_src_nid));
2004 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
2006 return lnet_handle_send(sd);
2014 * Don't run the selection algorithm on the peer NIs. By specifying the
2015 * local NID, we're also saying that we should always use the destination NID
2016 * provided. This handles the case where we should be using the same
2017 * destination NID for the all the messages which belong to the same RPC
2021 lnet_handle_spec_local_mr_dst(struct lnet_send_data *sd)
2023 sd->sd_best_ni = lnet_nid_to_ni_locked(&sd->sd_src_nid, sd->sd_cpt);
2024 if (!sd->sd_best_ni) {
2025 CERROR("Can't send to %s: src %s is not a local nid\n",
2026 libcfs_nidstr(&sd->sd_dst_nid),
2027 libcfs_nidstr(&sd->sd_src_nid));
2031 if (sd->sd_best_lpni &&
2032 nid_same(&sd->sd_best_lpni->lpni_nid,
2033 &the_lnet.ln_loni->ni_nid))
2034 return lnet_handle_lo_send(sd);
2035 else if (sd->sd_best_lpni)
2036 return lnet_handle_send(sd);
2038 CERROR("can't send to %s. no NI on %s\n",
2039 libcfs_nidstr(&sd->sd_dst_nid),
2040 libcfs_net2str(sd->sd_best_ni->ni_net->net_id));
2042 return -EHOSTUNREACH;
2046 lnet_find_best_ni_on_spec_net(struct lnet_ni *cur_best_ni,
2047 struct lnet_peer *peer,
2048 struct lnet_peer_net *peer_net,
2049 struct lnet_msg *msg,
2052 struct lnet_net *local_net;
2053 struct lnet_ni *best_ni;
2055 local_net = lnet_get_net_locked(peer_net->lpn_net_id);
2060 * Iterate through the NIs in this local Net and select
2061 * the NI to send from. The selection is determined by
2062 * these 3 criterion in the following priority:
2064 * 2. NI available credits
2067 best_ni = lnet_get_best_ni(local_net, cur_best_ni,
2068 peer, peer_net, msg, cpt);
2074 lnet_initiate_peer_discovery(struct lnet_peer_ni *lpni, struct lnet_msg *msg,
2077 struct lnet_peer *peer;
2078 struct lnet_peer_ni *new_lpni;
2081 lnet_peer_ni_addref_locked(lpni);
2083 peer = lpni->lpni_peer_net->lpn_peer;
2085 if (lnet_peer_gw_discovery(peer)) {
2086 lnet_peer_ni_decref_locked(lpni);
2090 if (!lnet_msg_discovery(msg) || lnet_peer_is_uptodate(peer)) {
2091 lnet_peer_ni_decref_locked(lpni);
2095 rc = lnet_discover_peer_locked(lpni, cpt, false);
2097 lnet_peer_ni_decref_locked(lpni);
2101 new_lpni = lnet_find_peer_ni_locked(lnet_nid_to_nid4(&lpni->lpni_nid));
2103 lnet_peer_ni_decref_locked(lpni);
2107 peer = new_lpni->lpni_peer_net->lpn_peer;
2108 spin_lock(&peer->lp_lock);
2109 if (lpni == new_lpni && lnet_peer_is_uptodate_locked(peer)) {
2110 /* The peer NI did not change and the peer is up to date.
2111 * Nothing more to do.
2113 spin_unlock(&peer->lp_lock);
2114 lnet_peer_ni_decref_locked(lpni);
2115 lnet_peer_ni_decref_locked(new_lpni);
2118 spin_unlock(&peer->lp_lock);
2120 /* Either the peer NI changed during discovery, or the peer isn't up
2121 * to date. In both cases we want to queue the message on the
2122 * (possibly new) peer's pending queue and queue the peer for discovery
2124 msg->msg_sending = 0;
2125 msg->msg_txpeer = NULL;
2126 lnet_net_unlock(cpt);
2127 lnet_peer_queue_message(peer, msg);
2130 lnet_peer_ni_decref_locked(lpni);
2131 lnet_peer_ni_decref_locked(new_lpni);
2133 CDEBUG(D_NET, "msg %p delayed. %s pending discovery\n",
2134 msg, libcfs_nidstr(&peer->lp_primary_nid));
2136 return LNET_DC_WAIT;
2140 lnet_handle_find_routed_path(struct lnet_send_data *sd,
2141 struct lnet_nid *dst_nid,
2142 struct lnet_peer_ni **gw_lpni,
2143 struct lnet_peer **gw_peer)
2146 struct lnet_peer *gw;
2147 struct lnet_peer *lp;
2148 struct lnet_peer_net *lpn;
2149 struct lnet_peer_net *best_lpn = NULL;
2150 struct lnet_remotenet *rnet, *best_rnet = NULL;
2151 struct lnet_route *best_route = NULL;
2152 struct lnet_route *last_route = NULL;
2153 struct lnet_peer_ni *lpni = NULL;
2154 struct lnet_peer_ni *gwni = NULL;
2155 bool route_found = false;
2156 struct lnet_nid *src_nid =
2157 !LNET_NID_IS_ANY(&sd->sd_src_nid) || !sd->sd_best_ni
2159 : &sd->sd_best_ni->ni_nid;
2160 int best_lpn_healthv = 0;
2161 __u32 best_lpn_sel_prio = LNET_MAX_SELECTION_PRIORITY;
2163 CDEBUG(D_NET, "using src nid %s for route restriction\n",
2164 src_nid ? libcfs_nidstr(src_nid) : "ANY");
2166 /* If a router nid was specified then we are replying to a GET or
2167 * sending an ACK. In this case we use the gateway associated with the
2168 * specified router nid.
2170 if (!LNET_NID_IS_ANY(&sd->sd_rtr_nid)) {
2171 gwni = lnet_peer_ni_find_locked(&sd->sd_rtr_nid);
2173 gw = gwni->lpni_peer_net->lpn_peer;
2174 lnet_peer_ni_decref_locked(gwni);
2175 if (gw->lp_rtr_refcount)
2178 CWARN("No peer NI for gateway %s. Attempting to find an alternative route.\n",
2179 libcfs_nidstr(&sd->sd_rtr_nid));
2184 if (sd->sd_msg->msg_routing || (src_nid && !LNET_NID_IS_ANY(src_nid))) {
2185 /* If I'm routing this message then I need to find the
2186 * next hop based on the destination NID
2188 * We also find next hop based on the destination NID
2189 * if the source NI was specified
2191 best_rnet = lnet_find_rnet_locked(LNET_NID_NET(&sd->sd_dst_nid));
2193 CERROR("Unable to send message from %s to %s - Route table may be misconfigured\n",
2194 (src_nid && LNET_NID_IS_ANY(src_nid)) ?
2196 libcfs_nidstr(src_nid),
2197 libcfs_nidstr(&sd->sd_dst_nid));
2198 return -EHOSTUNREACH;
2201 /* we've already looked up the initial lpni using
2204 lpni = sd->sd_best_lpni;
2205 /* the peer tree must be in existence */
2206 LASSERT(lpni && lpni->lpni_peer_net &&
2207 lpni->lpni_peer_net->lpn_peer);
2208 lp = lpni->lpni_peer_net->lpn_peer;
2210 list_for_each_entry(lpn, &lp->lp_peer_nets, lpn_peer_nets) {
2211 /* is this remote network reachable? */
2212 rnet = lnet_find_rnet_locked(lpn->lpn_net_id);
2221 /* select the preferred peer net */
2222 if (best_lpn_healthv > lpn->lpn_healthv)
2224 else if (best_lpn_healthv < lpn->lpn_healthv)
2227 if (best_lpn_sel_prio < lpn->lpn_sel_priority)
2229 else if (best_lpn_sel_prio > lpn->lpn_sel_priority)
2232 if (best_lpn->lpn_seq <= lpn->lpn_seq)
2235 best_lpn_healthv = lpn->lpn_healthv;
2236 best_lpn_sel_prio = lpn->lpn_sel_priority;
2242 CERROR("peer %s has no available nets\n",
2243 libcfs_nidstr(&sd->sd_dst_nid));
2244 return -EHOSTUNREACH;
2247 sd->sd_best_lpni = lnet_find_best_lpni(sd->sd_best_ni,
2248 lnet_nid_to_nid4(&sd->sd_dst_nid),
2250 best_lpn->lpn_net_id);
2251 if (!sd->sd_best_lpni) {
2252 CERROR("peer %s is unreachable\n",
2253 libcfs_nidstr(&sd->sd_dst_nid));
2254 return -EHOSTUNREACH;
2257 /* We're attempting to round robin over the remote peer
2258 * NI's so update the final destination we selected
2260 sd->sd_final_dst_lpni = sd->sd_best_lpni;
2262 /* Increment the sequence number of the remote lpni so
2263 * we can round robin over the different interfaces of
2266 sd->sd_best_lpni->lpni_seq++;
2270 * find the best route. Restrict the selection on the net of the
2271 * local NI if we've already picked the local NI to send from.
2272 * Otherwise, let's pick any route we can find and then find
2273 * a local NI we can reach the route's gateway on. Any route we
2274 * select will be reachable by virtue of the restriction we have
2275 * when adding a route.
2277 best_route = lnet_find_route_locked(best_rnet,
2278 LNET_NID_NET(src_nid),
2280 &last_route, &gwni);
2283 CERROR("no route to %s from %s\n",
2284 libcfs_nidstr(dst_nid),
2285 libcfs_nidstr(src_nid));
2286 return -EHOSTUNREACH;
2290 CERROR("Internal Error. Route expected to %s from %s\n",
2291 libcfs_nidstr(dst_nid),
2292 libcfs_nidstr(src_nid));
2296 gw = best_route->lr_gateway;
2297 LASSERT(gw == gwni->lpni_peer_net->lpn_peer);
2301 * If the router checker is not active then discover the gateway here.
2302 * This ensures we are able to take advantage of multi-rail routing, but
2303 * if the router checker is active then we do not unecessarily delay
2304 * messages while the gateway is being checked by the dedicated monitor
2307 * NB: We're only checking the alive_router_check_interval here, rather
2308 * than calling lnet_router_checker_active(), because the other
2309 * conditions that are checked by that function are either
2310 * irrelevant (the_lnet.ln_routing) or must be true (list of routers
2313 if (alive_router_check_interval <= 0) {
2314 rc = lnet_initiate_peer_discovery(gwni, sd->sd_msg, sd->sd_cpt);
2319 if (!sd->sd_best_ni) {
2320 lpn = gwni->lpni_peer_net;
2321 sd->sd_best_ni = lnet_find_best_ni_on_spec_net(NULL, gw, lpn,
2324 if (!sd->sd_best_ni) {
2325 CERROR("Internal Error. Expected local ni on %s but non found: %s\n",
2326 libcfs_net2str(lpn->lpn_net_id),
2327 libcfs_nidstr(&sd->sd_src_nid));
2336 * increment the sequence numbers since now we're sure we're
2337 * going to use this path
2339 if (LNET_NID_IS_ANY(&sd->sd_rtr_nid)) {
2340 LASSERT(best_route && last_route);
2341 best_route->lr_seq = last_route->lr_seq + 1;
2343 best_lpn->lpn_seq++;
2354 * Remote destination
2355 * Non-MR destination
2359 * Remote destination
2362 * The handling of these two cases is similar. Even though the destination
2363 * can be MR or non-MR, we'll deal directly with the router.
2366 lnet_handle_spec_router_dst(struct lnet_send_data *sd)
2369 struct lnet_peer_ni *gw_lpni = NULL;
2370 struct lnet_peer *gw_peer = NULL;
2373 sd->sd_best_ni = lnet_nid_to_ni_locked(&sd->sd_src_nid, sd->sd_cpt);
2374 if (!sd->sd_best_ni) {
2375 CERROR("Can't send to %s: src %s is not a local nid\n",
2376 libcfs_nidstr(&sd->sd_dst_nid),
2377 libcfs_nidstr(&sd->sd_src_nid));
2381 rc = lnet_handle_find_routed_path(sd, &sd->sd_dst_nid,
2382 &gw_lpni, &gw_peer);
2386 if (sd->sd_send_case & NMR_DST)
2388 * since the final destination is non-MR let's set its preferred
2389 * NID before we send
2391 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni,
2395 * We're going to send to the gw found so let's set its
2398 sd->sd_peer = gw_peer;
2399 sd->sd_best_lpni = gw_lpni;
2401 return lnet_handle_send(sd);
2405 lnet_find_best_ni_on_local_net(struct lnet_peer *peer, int md_cpt,
2406 struct lnet_msg *msg, bool discovery)
2408 struct lnet_peer_net *lpn = NULL;
2409 struct lnet_peer_net *best_lpn = NULL;
2410 struct lnet_net *net = NULL;
2411 struct lnet_net *best_net = NULL;
2412 struct lnet_ni *best_ni = NULL;
2413 int best_lpn_healthv = 0;
2414 int best_net_healthv = 0;
2416 __u32 best_lpn_sel_prio = LNET_MAX_SELECTION_PRIORITY;
2418 __u32 best_net_sel_prio = LNET_MAX_SELECTION_PRIORITY;
2423 * The peer can have multiple interfaces, some of them can be on
2424 * the local network and others on a routed network. We should
2425 * prefer the local network. However if the local network is not
2426 * available then we need to try the routed network
2429 /* go through all the peer nets and find the best_ni */
2430 list_for_each_entry(lpn, &peer->lp_peer_nets, lpn_peer_nets) {
2432 * The peer's list of nets can contain non-local nets. We
2433 * want to only examine the local ones.
2435 net = lnet_get_net_locked(lpn->lpn_net_id);
2439 lpn_sel_prio = lpn->lpn_sel_priority;
2440 net_healthv = lnet_get_net_healthv_locked(net);
2441 net_sel_prio = net->net_sel_priority;
2444 * if this is a discovery message and lp_disc_net_id is
2445 * specified then use that net to send the discovery on.
2447 if (peer->lp_disc_net_id == lpn->lpn_net_id &&
2456 /* always select the lpn with the best health */
2457 if (best_lpn_healthv > lpn->lpn_healthv)
2459 else if (best_lpn_healthv < lpn->lpn_healthv)
2462 /* select the preferred peer and local nets */
2463 if (best_lpn_sel_prio < lpn_sel_prio)
2465 else if (best_lpn_sel_prio > lpn_sel_prio)
2468 if (best_net_healthv > net_healthv)
2470 else if (best_net_healthv < net_healthv)
2473 if (best_net_sel_prio < net_sel_prio)
2475 else if (best_net_sel_prio > net_sel_prio)
2478 if (best_lpn->lpn_seq < lpn->lpn_seq)
2480 else if (best_lpn->lpn_seq > lpn->lpn_seq)
2483 /* round robin over the local networks */
2484 if (best_net->net_seq <= net->net_seq)
2488 best_net_healthv = net_healthv;
2489 best_net_sel_prio = net_sel_prio;
2490 best_lpn_healthv = lpn->lpn_healthv;
2491 best_lpn_sel_prio = lpn_sel_prio;
2500 /* Select the best NI on the same net as best_lpn chosen
2503 best_ni = lnet_find_best_ni_on_spec_net(NULL, peer, best_lpn,
2510 static struct lnet_ni *
2511 lnet_find_existing_preferred_best_ni(struct lnet_peer_ni *lpni, int cpt)
2513 struct lnet_ni *best_ni = NULL;
2514 struct lnet_peer_net *peer_net = lpni->lpni_peer_net;
2515 struct lnet_peer_ni *lpni_entry;
2518 * We must use a consistent source address when sending to a
2519 * non-MR peer. However, a non-MR peer can have multiple NIDs
2520 * on multiple networks, and we may even need to talk to this
2521 * peer on multiple networks -- certain types of
2522 * load-balancing configuration do this.
2524 * So we need to pick the NI the peer prefers for this
2525 * particular network.
2528 list_for_each_entry(lpni_entry, &peer_net->lpn_peer_nis,
2530 if (lpni_entry->lpni_pref_nnids == 0)
2532 LASSERT(lpni_entry->lpni_pref_nnids == 1);
2533 best_ni = lnet_nid_to_ni_locked(&lpni_entry->lpni_pref.nid,
2541 /* Prerequisite: sd->sd_peer and sd->sd_best_lpni should be set */
2543 lnet_select_preferred_best_ni(struct lnet_send_data *sd)
2545 struct lnet_ni *best_ni = NULL;
2546 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
2549 * We must use a consistent source address when sending to a
2550 * non-MR peer. However, a non-MR peer can have multiple NIDs
2551 * on multiple networks, and we may even need to talk to this
2552 * peer on multiple networks -- certain types of
2553 * load-balancing configuration do this.
2555 * So we need to pick the NI the peer prefers for this
2556 * particular network.
2559 best_ni = lnet_find_existing_preferred_best_ni(sd->sd_best_lpni,
2562 /* if best_ni is still not set just pick one */
2565 lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2566 sd->sd_best_lpni->lpni_peer_net,
2569 /* If there is no best_ni we don't have a route */
2571 CERROR("no path to %s from net %s\n",
2572 libcfs_nidstr(&best_lpni->lpni_nid),
2573 libcfs_net2str(best_lpni->lpni_net->net_id));
2574 return -EHOSTUNREACH;
2578 sd->sd_best_ni = best_ni;
2580 /* Set preferred NI if necessary. */
2581 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
2588 * Source not specified
2592 * always use the same source NID for NMR peers
2593 * If we've talked to that peer before then we already have a preferred
2594 * source NI associated with it. Otherwise, we select a preferred local NI
2595 * and store it in the peer
2598 lnet_handle_any_local_nmr_dst(struct lnet_send_data *sd)
2602 /* sd->sd_best_lpni is already set to the final destination */
2605 * At this point we should've created the peer ni and peer. If we
2606 * can't find it, then something went wrong. Instead of assert
2607 * output a relevant message and fail the send
2609 if (!sd->sd_best_lpni) {
2610 CERROR("Internal fault. Unable to send msg %s to %s. NID not known\n",
2611 lnet_msgtyp2str(sd->sd_msg->msg_type),
2612 libcfs_nidstr(&sd->sd_dst_nid));
2616 if (sd->sd_msg->msg_routing) {
2617 /* If I'm forwarding this message then I can choose any NI
2618 * on the destination peer net
2620 sd->sd_best_ni = lnet_find_best_ni_on_spec_net(NULL,
2622 sd->sd_best_lpni->lpni_peer_net,
2625 if (!sd->sd_best_ni) {
2626 CERROR("Unable to forward message to %s. No local NI available\n",
2627 libcfs_nidstr(&sd->sd_dst_nid));
2631 rc = lnet_select_preferred_best_ni(sd);
2634 rc = lnet_handle_send(sd);
2640 lnet_handle_any_mr_dsta(struct lnet_send_data *sd)
2643 * NOTE we've already handled the remote peer case. So we only
2644 * need to worry about the local case here.
2646 * if we're sending a response, ACK or reply, we need to send it
2647 * to the destination NID given to us. At this point we already
2648 * have the peer_ni we're suppose to send to, so just find the
2649 * best_ni on the peer net and use that. Since we're sending to an
2650 * MR peer then we can just run the selection algorithm on our
2651 * local NIs and pick the best one.
2653 if (sd->sd_send_case & SND_RESP) {
2655 lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2656 sd->sd_best_lpni->lpni_peer_net,
2660 if (!sd->sd_best_ni) {
2662 * We're not going to deal with not able to send
2663 * a response to the provided final destination
2665 CERROR("Can't send response to %s. No local NI available\n",
2666 libcfs_nidstr(&sd->sd_dst_nid));
2667 return -EHOSTUNREACH;
2670 return lnet_handle_send(sd);
2674 * If we get here that means we're sending a fresh request, PUT or
2675 * GET, so we need to run our standard selection algorithm.
2676 * First find the best local interface that's on any of the peer's
2679 sd->sd_best_ni = lnet_find_best_ni_on_local_net(sd->sd_peer,
2682 lnet_msg_discovery(sd->sd_msg));
2683 if (sd->sd_best_ni) {
2685 lnet_find_best_lpni(sd->sd_best_ni,
2686 lnet_nid_to_nid4(&sd->sd_dst_nid),
2688 sd->sd_best_ni->ni_net->net_id);
2691 * if we're successful in selecting a peer_ni on the local
2692 * network, then send to it. Otherwise fall through and
2693 * try and see if we can reach it over another routed
2696 if (sd->sd_best_lpni &&
2697 nid_same(&sd->sd_best_lpni->lpni_nid,
2698 &the_lnet.ln_loni->ni_nid)) {
2700 * in case we initially started with a routed
2701 * destination, let's reset to local
2703 sd->sd_send_case &= ~REMOTE_DST;
2704 sd->sd_send_case |= LOCAL_DST;
2705 return lnet_handle_lo_send(sd);
2706 } else if (sd->sd_best_lpni) {
2708 * in case we initially started with a routed
2709 * destination, let's reset to local
2711 sd->sd_send_case &= ~REMOTE_DST;
2712 sd->sd_send_case |= LOCAL_DST;
2713 return lnet_handle_send(sd);
2716 CERROR("Internal Error. Expected to have a best_lpni: "
2718 libcfs_nidstr(&sd->sd_src_nid),
2719 libcfs_nidstr(&sd->sd_dst_nid));
2725 * Peer doesn't have a local network. Let's see if there is
2726 * a remote network we can reach it on.
2728 return PASS_THROUGH;
2733 * Source NID not specified
2738 * Source NID not speified
2739 * Remote destination
2742 * In both of these cases if we're sending a response, ACK or REPLY, then
2743 * we need to send to the destination NID provided.
2745 * In the remote case let's deal with MR routers.
2750 lnet_handle_any_mr_dst(struct lnet_send_data *sd)
2753 struct lnet_peer *gw_peer = NULL;
2754 struct lnet_peer_ni *gw_lpni = NULL;
2757 * handle sending a response to a remote peer here so we don't
2758 * have to worry about it if we hit lnet_handle_any_mr_dsta()
2760 if (sd->sd_send_case & REMOTE_DST &&
2761 sd->sd_send_case & SND_RESP) {
2762 struct lnet_peer_ni *gw;
2763 struct lnet_peer *gw_peer;
2765 rc = lnet_handle_find_routed_path(
2766 sd, &sd->sd_dst_nid, &gw, &gw_peer);
2768 CERROR("Can't send response to %s. No route available\n",
2769 libcfs_nidstr(&sd->sd_dst_nid));
2770 return -EHOSTUNREACH;
2771 } else if (rc > 0) {
2775 sd->sd_best_lpni = gw;
2776 sd->sd_peer = gw_peer;
2778 return lnet_handle_send(sd);
2782 * Even though the NID for the peer might not be on a local network,
2783 * since the peer is MR there could be other interfaces on the
2784 * local network. In that case we'd still like to prefer the local
2785 * network over the routed network. If we're unable to do that
2786 * then we select the best router among the different routed networks,
2787 * and if the router is MR then we can deal with it as such.
2789 rc = lnet_handle_any_mr_dsta(sd);
2790 if (rc != PASS_THROUGH)
2794 * Now that we must route to the destination, we must consider the
2795 * MR case, where the destination has multiple interfaces, some of
2796 * which we can route to and others we do not. For this reason we
2797 * need to select the destination which we can route to and if
2798 * there are multiple, we need to round robin.
2800 rc = lnet_handle_find_routed_path(sd, &sd->sd_dst_nid,
2801 &gw_lpni, &gw_peer);
2805 sd->sd_send_case &= ~LOCAL_DST;
2806 sd->sd_send_case |= REMOTE_DST;
2808 sd->sd_peer = gw_peer;
2809 sd->sd_best_lpni = gw_lpni;
2811 return lnet_handle_send(sd);
2815 * Source not specified
2816 * Remote destination
2819 * Must send to the specified peer NID using the same source NID that
2820 * we've used before. If it's the first time to talk to that peer then
2821 * find the source NI and assign it as preferred to that peer
2824 lnet_handle_any_router_nmr_dst(struct lnet_send_data *sd)
2827 struct lnet_peer_ni *gw_lpni = NULL;
2828 struct lnet_peer *gw_peer = NULL;
2831 * Let's see if we have a preferred NI to talk to this NMR peer
2833 sd->sd_best_ni = lnet_find_existing_preferred_best_ni(sd->sd_best_lpni,
2837 * find the router and that'll find the best NI if we didn't find
2840 rc = lnet_handle_find_routed_path(sd, &sd->sd_dst_nid, &gw_lpni,
2846 * set the best_ni we've chosen as the preferred one for
2849 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
2851 /* we'll be sending to the gw */
2852 sd->sd_best_lpni = gw_lpni;
2853 sd->sd_peer = gw_peer;
2855 return lnet_handle_send(sd);
2859 lnet_handle_send_case_locked(struct lnet_send_data *sd)
2862 * turn off the SND_RESP bit.
2863 * It will be checked in the case handling
2865 __u32 send_case = sd->sd_send_case &= ~SND_RESP ;
2867 CDEBUG(D_NET, "Source %s%s to %s %s %s destination\n",
2868 (send_case & SRC_SPEC) ? "Specified: " : "ANY",
2869 (send_case & SRC_SPEC) ? libcfs_nidstr(&sd->sd_src_nid) : "",
2870 (send_case & MR_DST) ? "MR: " : "NMR: ",
2871 libcfs_nidstr(&sd->sd_dst_nid),
2872 (send_case & LOCAL_DST) ? "local" : "routed");
2874 switch (send_case) {
2876 * For all cases where the source is specified, we should always
2877 * use the destination NID, whether it's an MR destination or not,
2878 * since we're continuing a series of related messages for the
2881 case SRC_SPEC_LOCAL_NMR_DST:
2882 return lnet_handle_spec_local_nmr_dst(sd);
2883 case SRC_SPEC_LOCAL_MR_DST:
2884 return lnet_handle_spec_local_mr_dst(sd);
2885 case SRC_SPEC_ROUTER_NMR_DST:
2886 case SRC_SPEC_ROUTER_MR_DST:
2887 return lnet_handle_spec_router_dst(sd);
2888 case SRC_ANY_LOCAL_NMR_DST:
2889 return lnet_handle_any_local_nmr_dst(sd);
2890 case SRC_ANY_LOCAL_MR_DST:
2891 case SRC_ANY_ROUTER_MR_DST:
2892 return lnet_handle_any_mr_dst(sd);
2893 case SRC_ANY_ROUTER_NMR_DST:
2894 return lnet_handle_any_router_nmr_dst(sd);
2896 CERROR("Unknown send case\n");
2902 lnet_select_pathway(struct lnet_nid *src_nid,
2903 struct lnet_nid *dst_nid,
2904 struct lnet_msg *msg,
2905 struct lnet_nid *rtr_nid)
2907 struct lnet_peer_ni *lpni;
2908 struct lnet_peer *peer;
2909 struct lnet_send_data send_data;
2912 __u32 send_case = 0;
2914 bool mr_forwarding_allowed;
2916 memset(&send_data, 0, sizeof(send_data));
2919 * get an initial CPT to use for locking. The idea here is not to
2920 * serialize the calls to select_pathway, so that as many
2921 * operations can run concurrently as possible. To do that we use
2922 * the CPT where this call is being executed. Later on when we
2923 * determine the CPT to use in lnet_message_commit, we switch the
2924 * lock and check if there was any configuration change. If none,
2925 * then we proceed, if there is, then we restart the operation.
2927 cpt = lnet_net_lock_current();
2929 md_cpt = lnet_cpt_of_md(msg->msg_md, msg->msg_offset);
2930 if (md_cpt == CFS_CPT_ANY)
2936 * If we're being asked to send to the loopback interface, there
2937 * is no need to go through any selection. We can just shortcut
2938 * the entire process and send over lolnd
2940 send_data.sd_msg = msg;
2941 send_data.sd_cpt = cpt;
2942 if (nid_is_lo0(dst_nid)) {
2943 rc = lnet_handle_lo_send(&send_data);
2944 lnet_net_unlock(cpt);
2949 * find an existing peer_ni, or create one and mark it as having been
2950 * created due to network traffic. This call will create the
2951 * peer->peer_net->peer_ni tree.
2953 lpni = lnet_peerni_by_nid_locked(dst_nid, NULL, cpt);
2955 lnet_net_unlock(cpt);
2956 return PTR_ERR(lpni);
2960 * Cache the original src_nid and rtr_nid. If we need to resend the
2961 * message then we'll need to know whether the src_nid was originally
2962 * specified for this message. If it was originally specified,
2963 * then we need to keep using the same src_nid since it's
2964 * continuing the same sequence of messages. Similarly, rtr_nid will
2965 * affect our choice of next hop.
2968 msg->msg_src_nid_param = *src_nid;
2970 msg->msg_src_nid_param = LNET_ANY_NID;
2972 msg->msg_rtr_nid_param = *rtr_nid;
2974 msg->msg_rtr_nid_param = LNET_ANY_NID;
2977 * If necessary, perform discovery on the peer that owns this peer_ni.
2978 * Note, this can result in the ownership of this peer_ni changing
2979 * to another peer object.
2981 rc = lnet_initiate_peer_discovery(lpni, msg, cpt);
2983 lnet_peer_ni_decref_locked(lpni);
2984 lnet_net_unlock(cpt);
2987 lnet_peer_ni_decref_locked(lpni);
2989 peer = lpni->lpni_peer_net->lpn_peer;
2992 * Identify the different send cases
2994 if (!src_nid || LNET_NID_IS_ANY(src_nid)) {
2995 send_case |= SRC_ANY;
2996 if (lnet_get_net_locked(LNET_NID_NET(dst_nid)))
2997 send_case |= LOCAL_DST;
2999 send_case |= REMOTE_DST;
3001 send_case |= SRC_SPEC;
3002 if (LNET_NID_NET(src_nid) == LNET_NID_NET(dst_nid))
3003 send_case |= LOCAL_DST;
3005 send_case |= REMOTE_DST;
3009 if (msg->msg_routing && (send_case & LOCAL_DST))
3012 /* Determine whether to allow MR forwarding for this message.
3013 * NB: MR forwarding is allowed if the message originator and the
3014 * destination are both MR capable, and the destination lpni that was
3015 * originally chosen by the originator is unhealthy or down.
3016 * We check the MR capability of the destination further below
3018 mr_forwarding_allowed = false;
3020 struct lnet_peer *src_lp;
3021 struct lnet_peer_ni *src_lpni;
3023 src_lpni = lnet_peerni_by_nid_locked(&msg->msg_hdr.src_nid,
3025 /* We don't fail the send if we hit any errors here. We'll just
3026 * try to send it via non-multi-rail criteria
3028 if (!IS_ERR(src_lpni)) {
3029 /* Drop ref taken by lnet_nid2peerni_locked() */
3030 lnet_peer_ni_decref_locked(src_lpni);
3031 src_lp = lpni->lpni_peer_net->lpn_peer;
3032 if (lnet_peer_is_multi_rail(src_lp) &&
3033 !lnet_is_peer_ni_alive(lpni))
3034 mr_forwarding_allowed = true;
3037 CDEBUG(D_NET, "msg %p MR forwarding %s\n", msg,
3038 mr_forwarding_allowed ? "allowed" : "not allowed");
3042 * Deal with the peer as NMR in the following cases:
3043 * 1. the peer is NMR
3044 * 2. We're trying to recover a specific peer NI
3045 * 3. I'm a router sending to the final destination and MR forwarding is
3046 * not allowed for this message (as determined above).
3047 * In this case the source of the message would've
3048 * already selected the final destination so my job
3049 * is to honor the selection.
3051 if (!lnet_peer_is_multi_rail(peer) || msg->msg_recovery ||
3052 (final_hop && !mr_forwarding_allowed))
3053 send_case |= NMR_DST;
3055 send_case |= MR_DST;
3057 if (lnet_msg_is_response(msg))
3058 send_case |= SND_RESP;
3060 /* assign parameters to the send_data */
3062 send_data.sd_rtr_nid = *rtr_nid;
3064 send_data.sd_rtr_nid = LNET_ANY_NID;
3066 send_data.sd_src_nid = *src_nid;
3068 send_data.sd_src_nid = LNET_ANY_NID;
3069 send_data.sd_dst_nid = *dst_nid;
3070 send_data.sd_best_lpni = lpni;
3072 * keep a pointer to the final destination in case we're going to
3073 * route, so we'll need to access it later
3075 send_data.sd_final_dst_lpni = lpni;
3076 send_data.sd_peer = peer;
3077 send_data.sd_md_cpt = md_cpt;
3078 send_data.sd_send_case = send_case;
3080 rc = lnet_handle_send_case_locked(&send_data);
3083 * Update the local cpt since send_data.sd_cpt might've been
3084 * updated as a result of calling lnet_handle_send_case_locked().
3086 cpt = send_data.sd_cpt;
3088 if (rc == REPEAT_SEND)
3091 lnet_net_unlock(cpt);
3097 lnet_send(struct lnet_nid *src_nid, struct lnet_msg *msg,
3098 struct lnet_nid *rtr_nid)
3100 struct lnet_nid *dst_nid = &msg->msg_target.nid;
3103 /* NB: ni != NULL == interface pre-determined (ACK/REPLY) */
3104 LASSERT(msg->msg_txpeer == NULL);
3105 LASSERT(msg->msg_txni == NULL);
3106 LASSERT(!msg->msg_sending);
3107 LASSERT(!msg->msg_target_is_router);
3108 LASSERT(!msg->msg_receiving);
3110 msg->msg_sending = 1;
3112 LASSERT(!msg->msg_tx_committed);
3114 rc = lnet_select_pathway(src_nid, dst_nid, msg, rtr_nid);
3116 if (rc == -EHOSTUNREACH)
3117 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_ERROR;
3119 msg->msg_health_status = LNET_MSG_STATUS_LOCAL_ERROR;
3123 if (rc == LNET_CREDIT_OK)
3124 lnet_ni_send(msg->msg_txni, msg);
3126 /* rc == LNET_CREDIT_OK or LNET_CREDIT_WAIT or LNET_DC_WAIT */
3130 enum lnet_mt_event_type {
3131 MT_TYPE_LOCAL_NI = 0,
3135 struct lnet_mt_event_info {
3136 enum lnet_mt_event_type mt_type;
3137 struct lnet_nid mt_nid;
3140 /* called with res_lock held */
3142 lnet_detach_rsp_tracker(struct lnet_libmd *md, int cpt)
3144 struct lnet_rsp_tracker *rspt;
3147 * msg has a refcount on the MD so the MD is not going away.
3148 * The rspt queue for the cpt is protected by
3149 * the lnet_net_lock(cpt). cpt is the cpt of the MD cookie.
3151 if (!md->md_rspt_ptr)
3154 rspt = md->md_rspt_ptr;
3157 LASSERT(rspt->rspt_cpt == cpt);
3159 md->md_rspt_ptr = NULL;
3161 if (LNetMDHandleIsInvalid(rspt->rspt_mdh)) {
3163 * The monitor thread has invalidated this handle because the
3164 * response timed out, but it failed to lookup the MD. That
3165 * means this response tracker is on the zombie list. We can
3166 * safely remove it under the resource lock (held by caller) and
3167 * free the response tracker block.
3169 list_del(&rspt->rspt_on_list);
3170 lnet_rspt_free(rspt, cpt);
3173 * invalidate the handle to indicate that a response has been
3174 * received, which will then lead the monitor thread to clean up
3177 LNetInvalidateMDHandle(&rspt->rspt_mdh);
3182 lnet_clean_zombie_rstqs(void)
3184 struct lnet_rsp_tracker *rspt, *tmp;
3187 cfs_cpt_for_each(i, lnet_cpt_table()) {
3188 list_for_each_entry_safe(rspt, tmp,
3189 the_lnet.ln_mt_zombie_rstqs[i],
3191 list_del(&rspt->rspt_on_list);
3192 lnet_rspt_free(rspt, i);
3196 cfs_percpt_free(the_lnet.ln_mt_zombie_rstqs);
3200 lnet_finalize_expired_responses(void)
3202 struct lnet_libmd *md;
3203 struct lnet_rsp_tracker *rspt, *tmp;
3207 if (the_lnet.ln_mt_rstq == NULL)
3210 cfs_cpt_for_each(i, lnet_cpt_table()) {
3211 LIST_HEAD(local_queue);
3214 if (!the_lnet.ln_mt_rstq[i]) {
3218 list_splice_init(the_lnet.ln_mt_rstq[i], &local_queue);
3223 list_for_each_entry_safe(rspt, tmp, &local_queue, rspt_on_list) {
3225 * The rspt mdh will be invalidated when a response
3226 * is received or whenever we want to discard the
3227 * block the monitor thread will walk the queue
3228 * and clean up any rsts with an invalid mdh.
3229 * The monitor thread will walk the queue until
3230 * the first unexpired rspt block. This means that
3231 * some rspt blocks which received their
3232 * corresponding responses will linger in the
3233 * queue until they are cleaned up eventually.
3236 if (LNetMDHandleIsInvalid(rspt->rspt_mdh)) {
3238 list_del(&rspt->rspt_on_list);
3239 lnet_rspt_free(rspt, i);
3243 if (ktime_compare(now, rspt->rspt_deadline) >= 0 ||
3244 the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN) {
3245 struct lnet_peer_ni *lpni;
3246 struct lnet_nid nid;
3248 md = lnet_handle2md(&rspt->rspt_mdh);
3250 /* MD has been queued for unlink, but
3251 * rspt hasn't been detached (Note we've
3252 * checked above that the rspt_mdh is
3253 * valid). Since we cannot lookup the MD
3254 * we're unable to detach the rspt
3255 * ourselves. Thus, move the rspt to the
3256 * zombie list where we'll wait for
3258 * 1. The remaining operations on the
3259 * MD to complete. In this case the
3260 * final operation will result in
3261 * lnet_msg_detach_md()->
3262 * lnet_detach_rsp_tracker() where
3263 * we will clean up this response
3265 * 2. LNet to shutdown. In this case
3266 * we'll wait until after all LND Nets
3267 * have shutdown and then we can
3268 * safely free any remaining response
3269 * tracker blocks on the zombie list.
3270 * Note: We need to hold the resource
3271 * lock when adding to the zombie list
3272 * because we may have concurrent access
3273 * with lnet_detach_rsp_tracker().
3275 LNetInvalidateMDHandle(&rspt->rspt_mdh);
3276 list_move(&rspt->rspt_on_list,
3277 the_lnet.ln_mt_zombie_rstqs[i]);
3281 LASSERT(md->md_rspt_ptr == rspt);
3282 md->md_rspt_ptr = NULL;
3285 LNetMDUnlink(rspt->rspt_mdh);
3287 nid = rspt->rspt_next_hop_nid;
3289 list_del(&rspt->rspt_on_list);
3290 lnet_rspt_free(rspt, i);
3292 /* If we're shutting down we just want to clean
3293 * up the rspt blocks
3295 if (the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN)
3299 the_lnet.ln_counters[i]->lct_health.lch_response_timeout_count++;
3303 "Response timeout: md = %p: nid = %s\n",
3304 md, libcfs_nidstr(&nid));
3307 * If there is a timeout on the response
3308 * from the next hop decrement its health
3309 * value so that we don't use it
3312 lpni = lnet_peer_ni_find_locked(&nid);
3314 lnet_handle_remote_failure_locked(lpni);
3315 lnet_peer_ni_decref_locked(lpni);
3324 if (!list_empty(&local_queue)) {
3326 list_splice(&local_queue, the_lnet.ln_mt_rstq[i]);
3333 lnet_resend_pending_msgs_locked(struct list_head *resendq, int cpt)
3335 struct lnet_msg *msg;
3337 while (!list_empty(resendq)) {
3338 struct lnet_peer_ni *lpni;
3340 msg = list_entry(resendq->next, struct lnet_msg,
3343 list_del_init(&msg->msg_list);
3345 lpni = lnet_peer_ni_find_locked(&msg->msg_hdr.dest_nid);
3347 lnet_net_unlock(cpt);
3348 CERROR("Expected that a peer is already created for %s\n",
3349 libcfs_nidstr(&msg->msg_hdr.dest_nid));
3350 msg->msg_no_resend = true;
3351 lnet_finalize(msg, -EFAULT);
3356 lnet_peer_ni_decref_locked(lpni);
3358 lnet_net_unlock(cpt);
3359 CDEBUG(D_NET, "resending %s->%s: %s recovery %d try# %d\n",
3360 libcfs_nidstr(&msg->msg_src_nid_param),
3361 libcfs_idstr(&msg->msg_target),
3362 lnet_msgtyp2str(msg->msg_type),
3364 msg->msg_retry_count);
3365 rc = lnet_send(&msg->msg_src_nid_param, msg,
3366 &msg->msg_rtr_nid_param);
3368 CERROR("Error sending %s to %s: %d\n",
3369 lnet_msgtyp2str(msg->msg_type),
3370 libcfs_idstr(&msg->msg_target), rc);
3371 msg->msg_no_resend = true;
3372 lnet_finalize(msg, rc);
3376 the_lnet.ln_counters[cpt]->lct_health.lch_resend_count++;
3382 lnet_resend_pending_msgs(void)
3386 cfs_cpt_for_each(i, lnet_cpt_table()) {
3388 lnet_resend_pending_msgs_locked(the_lnet.ln_mt_resendqs[i], i);
3393 /* called with cpt and ni_lock held */
3395 lnet_unlink_ni_recovery_mdh_locked(struct lnet_ni *ni, int cpt, bool force)
3397 struct lnet_handle_md recovery_mdh;
3399 LNetInvalidateMDHandle(&recovery_mdh);
3401 if (ni->ni_recovery_state & LNET_NI_RECOVERY_PENDING ||
3403 recovery_mdh = ni->ni_ping_mdh;
3404 LNetInvalidateMDHandle(&ni->ni_ping_mdh);
3407 lnet_net_unlock(cpt);
3408 if (!LNetMDHandleIsInvalid(recovery_mdh))
3409 LNetMDUnlink(recovery_mdh);
3415 lnet_recover_local_nis(void)
3417 struct lnet_mt_event_info *ev_info;
3418 LIST_HEAD(processed_list);
3419 LIST_HEAD(local_queue);
3420 struct lnet_handle_md mdh;
3421 struct lnet_ni *tmp;
3423 struct lnet_nid nid;
3429 * splice the recovery queue on a local queue. We will iterate
3430 * through the local queue and update it as needed. Once we're
3431 * done with the traversal, we'll splice the local queue back on
3432 * the head of the ln_mt_localNIRecovq. Any newly added local NIs
3433 * will be traversed in the next iteration.
3436 list_splice_init(&the_lnet.ln_mt_localNIRecovq,
3440 now = ktime_get_seconds();
3442 list_for_each_entry_safe(ni, tmp, &local_queue, ni_recovery) {
3444 * if an NI is being deleted or it is now healthy, there
3445 * is no need to keep it around in the recovery queue.
3446 * The monitor thread is the only thread responsible for
3447 * removing the NI from the recovery queue.
3448 * Multiple threads can be adding NIs to the recovery
3451 healthv = atomic_read(&ni->ni_healthv);
3455 if (ni->ni_state != LNET_NI_STATE_ACTIVE ||
3456 healthv == LNET_MAX_HEALTH_VALUE) {
3457 list_del_init(&ni->ni_recovery);
3458 lnet_unlink_ni_recovery_mdh_locked(ni, 0, false);
3460 lnet_ni_decref_locked(ni, 0);
3466 * if the local NI failed recovery we must unlink the md.
3467 * But we want to keep the local_ni on the recovery queue
3468 * so we can continue the attempts to recover it.
3470 if (ni->ni_recovery_state & LNET_NI_RECOVERY_FAILED) {
3471 lnet_unlink_ni_recovery_mdh_locked(ni, 0, true);
3472 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_FAILED;
3478 if (now < ni->ni_next_ping) {
3485 CDEBUG(D_NET, "attempting to recover local ni: %s\n",
3486 libcfs_nidstr(&ni->ni_nid));
3489 if (!(ni->ni_recovery_state & LNET_NI_RECOVERY_PENDING)) {
3490 ni->ni_recovery_state |= LNET_NI_RECOVERY_PENDING;
3493 LIBCFS_ALLOC(ev_info, sizeof(*ev_info));
3495 CERROR("out of memory. Can't recover %s\n",
3496 libcfs_nidstr(&ni->ni_nid));
3498 ni->ni_recovery_state &=
3499 ~LNET_NI_RECOVERY_PENDING;
3504 mdh = ni->ni_ping_mdh;
3506 * Invalidate the ni mdh in case it's deleted.
3507 * We'll unlink the mdh in this case below.
3509 LNetInvalidateMDHandle(&ni->ni_ping_mdh);
3513 * remove the NI from the local queue and drop the
3514 * reference count to it while we're recovering
3515 * it. The reason for that, is that the NI could
3516 * be deleted, and the way the code is structured
3517 * is if we don't drop the NI, then the deletion
3518 * code will enter a loop waiting for the
3519 * reference count to be removed while holding the
3520 * ln_mutex_lock(). When we look up the peer to
3521 * send to in lnet_select_pathway() we will try to
3522 * lock the ln_mutex_lock() as well, leading to
3523 * a deadlock. By dropping the refcount and
3524 * removing it from the list, we allow for the NI
3525 * to be removed, then we use the cached NID to
3526 * look it up again. If it's gone, then we just
3527 * continue examining the rest of the queue.
3530 list_del_init(&ni->ni_recovery);
3531 lnet_ni_decref_locked(ni, 0);
3534 ev_info->mt_type = MT_TYPE_LOCAL_NI;
3535 ev_info->mt_nid = nid;
3536 rc = lnet_send_ping(&nid, &mdh, LNET_INTERFACES_MIN,
3537 ev_info, the_lnet.ln_mt_handler,
3539 /* lookup the nid again */
3541 ni = lnet_nid_to_ni_locked(&nid, 0);
3544 * the NI has been deleted when we dropped
3551 ni->ni_ping_count++;
3553 ni->ni_ping_mdh = mdh;
3554 lnet_ni_add_to_recoveryq_locked(ni, &processed_list,
3559 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_PENDING;
3568 * put back the remaining NIs on the ln_mt_localNIRecovq to be
3569 * reexamined in the next iteration.
3571 list_splice_init(&processed_list, &local_queue);
3573 list_splice(&local_queue, &the_lnet.ln_mt_localNIRecovq);
3578 lnet_resendqs_create(void)
3580 struct list_head **resendqs;
3581 resendqs = lnet_create_array_of_queues();
3586 lnet_net_lock(LNET_LOCK_EX);
3587 the_lnet.ln_mt_resendqs = resendqs;
3588 lnet_net_unlock(LNET_LOCK_EX);
3594 lnet_clean_local_ni_recoveryq(void)
3598 /* This is only called when the monitor thread has stopped */
3601 while (!list_empty(&the_lnet.ln_mt_localNIRecovq)) {
3602 ni = list_entry(the_lnet.ln_mt_localNIRecovq.next,
3603 struct lnet_ni, ni_recovery);
3604 list_del_init(&ni->ni_recovery);
3606 lnet_unlink_ni_recovery_mdh_locked(ni, 0, true);
3608 lnet_ni_decref_locked(ni, 0);
3615 lnet_unlink_lpni_recovery_mdh_locked(struct lnet_peer_ni *lpni, int cpt,
3618 struct lnet_handle_md recovery_mdh;
3620 LNetInvalidateMDHandle(&recovery_mdh);
3622 if (lpni->lpni_state & LNET_PEER_NI_RECOVERY_PENDING || force) {
3623 recovery_mdh = lpni->lpni_recovery_ping_mdh;
3624 LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
3626 spin_unlock(&lpni->lpni_lock);
3627 lnet_net_unlock(cpt);
3628 if (!LNetMDHandleIsInvalid(recovery_mdh))
3629 LNetMDUnlink(recovery_mdh);
3631 spin_lock(&lpni->lpni_lock);
3635 lnet_clean_peer_ni_recoveryq(void)
3637 struct lnet_peer_ni *lpni, *tmp;
3639 lnet_net_lock(LNET_LOCK_EX);
3641 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_mt_peerNIRecovq,
3643 list_del_init(&lpni->lpni_recovery);
3644 spin_lock(&lpni->lpni_lock);
3645 lnet_unlink_lpni_recovery_mdh_locked(lpni, LNET_LOCK_EX, true);
3646 spin_unlock(&lpni->lpni_lock);
3647 lnet_peer_ni_decref_locked(lpni);
3650 lnet_net_unlock(LNET_LOCK_EX);
3654 lnet_clean_resendqs(void)
3656 struct lnet_msg *msg, *tmp;
3660 cfs_cpt_for_each(i, lnet_cpt_table()) {
3662 list_splice_init(the_lnet.ln_mt_resendqs[i], &msgs);
3664 list_for_each_entry_safe(msg, tmp, &msgs, msg_list) {
3665 list_del_init(&msg->msg_list);
3666 msg->msg_no_resend = true;
3667 lnet_finalize(msg, -ESHUTDOWN);
3671 cfs_percpt_free(the_lnet.ln_mt_resendqs);
3675 lnet_recover_peer_nis(void)
3677 struct lnet_mt_event_info *ev_info;
3678 LIST_HEAD(processed_list);
3679 LIST_HEAD(local_queue);
3680 struct lnet_handle_md mdh;
3681 struct lnet_peer_ni *lpni;
3682 struct lnet_peer_ni *tmp;
3683 struct lnet_nid nid;
3689 * Always use cpt 0 for locking across all interactions with
3690 * ln_mt_peerNIRecovq
3693 list_splice_init(&the_lnet.ln_mt_peerNIRecovq,
3697 now = ktime_get_seconds();
3699 list_for_each_entry_safe(lpni, tmp, &local_queue,
3702 * The same protection strategy is used here as is in the
3703 * local recovery case.
3706 healthv = atomic_read(&lpni->lpni_healthv);
3707 spin_lock(&lpni->lpni_lock);
3708 if (lpni->lpni_state & LNET_PEER_NI_DELETING ||
3709 healthv == LNET_MAX_HEALTH_VALUE) {
3710 list_del_init(&lpni->lpni_recovery);
3711 lnet_unlink_lpni_recovery_mdh_locked(lpni, 0, false);
3712 spin_unlock(&lpni->lpni_lock);
3713 lnet_peer_ni_decref_locked(lpni);
3719 * If the peer NI has failed recovery we must unlink the
3720 * md. But we want to keep the peer ni on the recovery
3721 * queue so we can try to continue recovering it
3723 if (lpni->lpni_state & LNET_PEER_NI_RECOVERY_FAILED) {
3724 lnet_unlink_lpni_recovery_mdh_locked(lpni, 0, true);
3725 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_FAILED;
3728 spin_unlock(&lpni->lpni_lock);
3730 if (now < lpni->lpni_next_ping) {
3738 * NOTE: we're racing with peer deletion from user space.
3739 * It's possible that a peer is deleted after we check its
3740 * state. In this case the recovery can create a new peer
3742 spin_lock(&lpni->lpni_lock);
3743 if (!(lpni->lpni_state & LNET_PEER_NI_RECOVERY_PENDING) &&
3744 !(lpni->lpni_state & LNET_PEER_NI_DELETING)) {
3745 lpni->lpni_state |= LNET_PEER_NI_RECOVERY_PENDING;
3746 spin_unlock(&lpni->lpni_lock);
3748 LIBCFS_ALLOC(ev_info, sizeof(*ev_info));
3750 CERROR("out of memory. Can't recover %s\n",
3751 libcfs_nidstr(&lpni->lpni_nid));
3752 spin_lock(&lpni->lpni_lock);
3753 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3754 spin_unlock(&lpni->lpni_lock);
3758 /* look at the comments in lnet_recover_local_nis() */
3759 mdh = lpni->lpni_recovery_ping_mdh;
3760 nid = lpni->lpni_nid;
3761 LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
3763 list_del_init(&lpni->lpni_recovery);
3764 lnet_peer_ni_decref_locked(lpni);
3767 ev_info->mt_type = MT_TYPE_PEER_NI;
3768 ev_info->mt_nid = nid;
3769 rc = lnet_send_ping(&nid, &mdh, LNET_INTERFACES_MIN,
3770 ev_info, the_lnet.ln_mt_handler,
3774 * lnet_find_peer_ni_locked() grabs a refcount for
3775 * us. No need to take it explicitly.
3777 lpni = lnet_peer_ni_find_locked(&nid);
3784 lpni->lpni_ping_count++;
3786 lpni->lpni_recovery_ping_mdh = mdh;
3788 lnet_peer_ni_add_to_recoveryq_locked(lpni,
3792 spin_lock(&lpni->lpni_lock);
3793 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3794 spin_unlock(&lpni->lpni_lock);
3797 /* Drop the ref taken by lnet_find_peer_ni_locked() */
3798 lnet_peer_ni_decref_locked(lpni);
3801 spin_unlock(&lpni->lpni_lock);
3804 list_splice_init(&processed_list, &local_queue);
3806 list_splice(&local_queue, &the_lnet.ln_mt_peerNIRecovq);
3811 lnet_monitor_thread(void *arg)
3813 time64_t rsp_timeout = 0;
3816 wait_for_completion(&the_lnet.ln_started);
3818 * The monitor thread takes care of the following:
3819 * 1. Checks the aliveness of routers
3820 * 2. Checks if there are messages on the resend queue to resend
3822 * 3. Check if there are any NIs on the local recovery queue and
3824 * 4. Checks if there are any NIs on the remote recovery queue
3827 while (the_lnet.ln_mt_state == LNET_MT_STATE_RUNNING) {
3828 now = ktime_get_real_seconds();
3830 if (lnet_router_checker_active())
3831 lnet_check_routers();
3833 lnet_resend_pending_msgs();
3835 if (now >= rsp_timeout) {
3836 lnet_finalize_expired_responses();
3837 rsp_timeout = now + (lnet_transaction_timeout / 2);
3840 lnet_recover_local_nis();
3841 lnet_recover_peer_nis();
3844 * TODO do we need to check if we should sleep without
3845 * timeout? Technically, an active system will always
3846 * have messages in flight so this check will always
3847 * evaluate to false. And on an idle system do we care
3848 * if we wake up every 1 second? Although, we've seen
3849 * cases where we get a complaint that an idle thread
3850 * is waking up unnecessarily.
3852 wait_for_completion_interruptible_timeout(
3853 &the_lnet.ln_mt_wait_complete,
3854 cfs_time_seconds(1));
3855 /* Must re-init the completion before testing anything,
3856 * including ln_mt_state.
3858 reinit_completion(&the_lnet.ln_mt_wait_complete);
3862 lnet_net_lock(LNET_LOCK_EX);
3863 the_lnet.ln_mt_state = LNET_MT_STATE_SHUTDOWN;
3864 lnet_net_unlock(LNET_LOCK_EX);
3866 /* signal that the monitor thread is exiting */
3867 up(&the_lnet.ln_mt_signal);
3875 * Returns == 0 if success
3876 * Returns > 0 if LNetMDBind or prior fails
3877 * Returns < 0 if LNetGet fails
3880 lnet_send_ping(struct lnet_nid *dest_nid,
3881 struct lnet_handle_md *mdh, int nnis,
3882 void *user_data, lnet_handler_t handler, bool recovery)
3884 struct lnet_md md = { NULL };
3885 struct lnet_process_id id;
3886 struct lnet_ping_buffer *pbuf;
3889 if (LNET_NID_IS_ANY(dest_nid)) {
3894 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
3900 /* initialize md content */
3901 md.start = &pbuf->pb_info;
3902 md.length = LNET_PING_INFO_SIZE(nnis);
3903 md.threshold = 2; /* GET/REPLY */
3905 md.options = LNET_MD_TRUNCATE | LNET_MD_TRACK_RESPONSE;
3906 md.user_ptr = user_data;
3907 md.handler = handler;
3909 rc = LNetMDBind(&md, LNET_UNLINK, mdh);
3911 lnet_ping_buffer_decref(pbuf);
3912 CERROR("Can't bind MD: %d\n", rc);
3913 rc = -rc; /* change the rc to positive */
3916 id.pid = LNET_PID_LUSTRE;
3917 id.nid = lnet_nid_to_nid4(dest_nid);
3919 rc = LNetGet(LNET_NID_ANY, *mdh, id,
3920 LNET_RESERVED_PORTAL,
3921 LNET_PROTO_PING_MATCHBITS, 0, recovery);
3924 goto fail_unlink_md;
3930 LNetInvalidateMDHandle(mdh);
3936 lnet_handle_recovery_reply(struct lnet_mt_event_info *ev_info,
3937 int status, bool send, bool unlink_event)
3939 struct lnet_nid *nid = &ev_info->mt_nid;
3941 if (ev_info->mt_type == MT_TYPE_LOCAL_NI) {
3945 ni = lnet_nid_to_ni_locked(nid, 0);
3951 if (!send || (send && status != 0))
3952 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_PENDING;
3954 ni->ni_recovery_state |= LNET_NI_RECOVERY_FAILED;
3959 CERROR("local NI (%s) recovery failed with %d\n",
3960 libcfs_nidstr(nid), status);
3964 * need to increment healthv for the ni here, because in
3965 * the lnet_finalize() path we don't have access to this
3966 * NI. And in order to get access to it, we'll need to
3967 * carry forward too much information.
3968 * In the peer case, it'll naturally be incremented
3971 lnet_inc_healthv(&ni->ni_healthv,
3972 lnet_health_sensitivity);
3974 struct lnet_peer_ni *lpni;
3977 cpt = lnet_net_lock_current();
3978 lpni = lnet_peer_ni_find_locked(nid);
3980 lnet_net_unlock(cpt);
3983 spin_lock(&lpni->lpni_lock);
3984 if (!send || (send && status != 0))
3985 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3987 lpni->lpni_state |= LNET_PEER_NI_RECOVERY_FAILED;
3988 spin_unlock(&lpni->lpni_lock);
3989 lnet_peer_ni_decref_locked(lpni);
3990 lnet_net_unlock(cpt);
3993 CERROR("peer NI (%s) recovery failed with %d\n",
3994 libcfs_nidstr(nid), status);
3999 lnet_mt_event_handler(struct lnet_event *event)
4001 struct lnet_mt_event_info *ev_info = event->md_user_ptr;
4002 struct lnet_ping_buffer *pbuf;
4004 /* TODO: remove assert */
4005 LASSERT(event->type == LNET_EVENT_REPLY ||
4006 event->type == LNET_EVENT_SEND ||
4007 event->type == LNET_EVENT_UNLINK);
4009 CDEBUG(D_NET, "Received event: %d status: %d\n", event->type,
4012 switch (event->type) {
4013 case LNET_EVENT_UNLINK:
4014 CDEBUG(D_NET, "%s recovery ping unlinked\n",
4015 libcfs_nidstr(&ev_info->mt_nid));
4017 case LNET_EVENT_REPLY:
4018 lnet_handle_recovery_reply(ev_info, event->status, false,
4019 event->type == LNET_EVENT_UNLINK);
4021 case LNET_EVENT_SEND:
4022 CDEBUG(D_NET, "%s recovery message sent %s:%d\n",
4023 libcfs_nidstr(&ev_info->mt_nid),
4024 (event->status) ? "unsuccessfully" :
4025 "successfully", event->status);
4026 lnet_handle_recovery_reply(ev_info, event->status, true, false);
4029 CERROR("Unexpected event: %d\n", event->type);
4032 if (event->unlinked) {
4033 LIBCFS_FREE(ev_info, sizeof(*ev_info));
4034 pbuf = LNET_PING_INFO_TO_BUFFER(event->md_start);
4035 lnet_ping_buffer_decref(pbuf);
4040 lnet_rsp_tracker_create(void)
4042 struct list_head **rstqs;
4043 rstqs = lnet_create_array_of_queues();
4048 the_lnet.ln_mt_rstq = rstqs;
4054 lnet_rsp_tracker_clean(void)
4056 lnet_finalize_expired_responses();
4058 cfs_percpt_free(the_lnet.ln_mt_rstq);
4059 the_lnet.ln_mt_rstq = NULL;
4062 int lnet_monitor_thr_start(void)
4065 struct task_struct *task;
4067 if (the_lnet.ln_mt_state != LNET_MT_STATE_SHUTDOWN)
4070 rc = lnet_resendqs_create();
4074 rc = lnet_rsp_tracker_create();
4078 sema_init(&the_lnet.ln_mt_signal, 0);
4080 lnet_net_lock(LNET_LOCK_EX);
4081 the_lnet.ln_mt_state = LNET_MT_STATE_RUNNING;
4082 lnet_net_unlock(LNET_LOCK_EX);
4083 task = kthread_run(lnet_monitor_thread, NULL, "monitor_thread");
4086 CERROR("Can't start monitor thread: %d\n", rc);
4093 lnet_net_lock(LNET_LOCK_EX);
4094 the_lnet.ln_mt_state = LNET_MT_STATE_STOPPING;
4095 lnet_net_unlock(LNET_LOCK_EX);
4096 /* block until event callback signals exit */
4097 down(&the_lnet.ln_mt_signal);
4099 lnet_net_lock(LNET_LOCK_EX);
4100 the_lnet.ln_mt_state = LNET_MT_STATE_SHUTDOWN;
4101 lnet_net_unlock(LNET_LOCK_EX);
4102 lnet_rsp_tracker_clean();
4103 lnet_clean_local_ni_recoveryq();
4104 lnet_clean_peer_ni_recoveryq();
4105 lnet_clean_resendqs();
4106 the_lnet.ln_mt_handler = NULL;
4109 lnet_rsp_tracker_clean();
4110 lnet_clean_local_ni_recoveryq();
4111 lnet_clean_peer_ni_recoveryq();
4112 lnet_clean_resendqs();
4116 void lnet_monitor_thr_stop(void)
4118 if (the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN)
4121 LASSERT(the_lnet.ln_mt_state == LNET_MT_STATE_RUNNING);
4122 lnet_net_lock(LNET_LOCK_EX);
4123 the_lnet.ln_mt_state = LNET_MT_STATE_STOPPING;
4124 lnet_net_unlock(LNET_LOCK_EX);
4126 /* tell the monitor thread that we're shutting down */
4127 complete(&the_lnet.ln_mt_wait_complete);
4129 /* block until monitor thread signals that it's done */
4130 down(&the_lnet.ln_mt_signal);
4131 LASSERT(the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN);
4133 /* perform cleanup tasks */
4134 lnet_rsp_tracker_clean();
4135 lnet_clean_local_ni_recoveryq();
4136 lnet_clean_peer_ni_recoveryq();
4137 lnet_clean_resendqs();
4141 lnet_drop_message(struct lnet_ni *ni, int cpt, void *private, unsigned int nob,
4145 lnet_incr_stats(&ni->ni_stats, msg_type, LNET_STATS_TYPE_DROP);
4146 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
4147 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length += nob;
4148 lnet_net_unlock(cpt);
4150 lnet_ni_recv(ni, private, NULL, 0, 0, 0, nob);
4154 lnet_recv_put(struct lnet_ni *ni, struct lnet_msg *msg)
4156 struct lnet_hdr *hdr = &msg->msg_hdr;
4158 if (msg->msg_wanted != 0)
4159 lnet_setpayloadbuffer(msg);
4161 lnet_build_msg_event(msg, LNET_EVENT_PUT);
4163 /* Must I ACK? If so I'll grab the ack_wmd out of the header and put
4164 * it back into the ACK during lnet_finalize() */
4165 msg->msg_ack = (!lnet_is_wire_handle_none(&hdr->msg.put.ack_wmd) &&
4166 (msg->msg_md->md_options & LNET_MD_ACK_DISABLE) == 0);
4168 lnet_ni_recv(ni, msg->msg_private, msg, msg->msg_rx_delayed,
4169 msg->msg_offset, msg->msg_wanted, hdr->payload_length);
4173 lnet_parse_put(struct lnet_ni *ni, struct lnet_msg *msg)
4175 struct lnet_hdr *hdr = &msg->msg_hdr;
4176 struct lnet_match_info info;
4180 /* Convert put fields to host byte order */
4181 hdr->msg.put.match_bits = le64_to_cpu(hdr->msg.put.match_bits);
4182 hdr->msg.put.ptl_index = le32_to_cpu(hdr->msg.put.ptl_index);
4183 hdr->msg.put.offset = le32_to_cpu(hdr->msg.put.offset);
4185 /* Primary peer NID. */
4186 info.mi_id.nid = msg->msg_initiator;
4187 info.mi_id.pid = hdr->src_pid;
4188 info.mi_opc = LNET_MD_OP_PUT;
4189 info.mi_portal = hdr->msg.put.ptl_index;
4190 info.mi_rlength = hdr->payload_length;
4191 info.mi_roffset = hdr->msg.put.offset;
4192 info.mi_mbits = hdr->msg.put.match_bits;
4193 info.mi_cpt = lnet_nid2cpt(&msg->msg_initiator, ni);
4195 msg->msg_rx_ready_delay = ni->ni_net->net_lnd->lnd_eager_recv == NULL;
4196 ready_delay = msg->msg_rx_ready_delay;
4199 rc = lnet_ptl_match_md(&info, msg);
4204 case LNET_MATCHMD_OK:
4205 lnet_recv_put(ni, msg);
4208 case LNET_MATCHMD_NONE:
4210 /* no eager_recv or has already called it, should
4211 * have been attached on delayed list */
4214 rc = lnet_ni_eager_recv(ni, msg);
4221 case LNET_MATCHMD_DROP:
4222 CNETERR("Dropping PUT from %s portal %d match %llu"
4223 " offset %d length %d: %d\n",
4224 libcfs_idstr(&info.mi_id), info.mi_portal,
4225 info.mi_mbits, info.mi_roffset, info.mi_rlength, rc);
4227 return -ENOENT; /* -ve: OK but no match */
4232 lnet_parse_get(struct lnet_ni *ni, struct lnet_msg *msg, int rdma_get)
4234 struct lnet_match_info info;
4235 struct lnet_hdr *hdr = &msg->msg_hdr;
4236 struct lnet_processid source_id;
4237 struct lnet_handle_wire reply_wmd;
4240 /* Convert get fields to host byte order */
4241 hdr->msg.get.match_bits = le64_to_cpu(hdr->msg.get.match_bits);
4242 hdr->msg.get.ptl_index = le32_to_cpu(hdr->msg.get.ptl_index);
4243 hdr->msg.get.sink_length = le32_to_cpu(hdr->msg.get.sink_length);
4244 hdr->msg.get.src_offset = le32_to_cpu(hdr->msg.get.src_offset);
4246 source_id.nid = hdr->src_nid;
4247 source_id.pid = hdr->src_pid;
4248 /* Primary peer NID */
4249 info.mi_id.nid = msg->msg_initiator;
4250 info.mi_id.pid = hdr->src_pid;
4251 info.mi_opc = LNET_MD_OP_GET;
4252 info.mi_portal = hdr->msg.get.ptl_index;
4253 info.mi_rlength = hdr->msg.get.sink_length;
4254 info.mi_roffset = hdr->msg.get.src_offset;
4255 info.mi_mbits = hdr->msg.get.match_bits;
4256 info.mi_cpt = lnet_nid2cpt(&msg->msg_initiator, ni);
4258 rc = lnet_ptl_match_md(&info, msg);
4259 if (rc == LNET_MATCHMD_DROP) {
4260 CNETERR("Dropping GET from %s portal %d match %llu"
4261 " offset %d length %d\n",
4262 libcfs_idstr(&info.mi_id), info.mi_portal,
4263 info.mi_mbits, info.mi_roffset, info.mi_rlength);
4264 return -ENOENT; /* -ve: OK but no match */
4267 LASSERT(rc == LNET_MATCHMD_OK);
4269 lnet_build_msg_event(msg, LNET_EVENT_GET);
4271 reply_wmd = hdr->msg.get.return_wmd;
4273 lnet_prep_send(msg, LNET_MSG_REPLY, &source_id,
4274 msg->msg_offset, msg->msg_wanted);
4276 msg->msg_hdr.msg.reply.dst_wmd = reply_wmd;
4279 /* The LND completes the REPLY from her recv procedure */
4280 lnet_ni_recv(ni, msg->msg_private, msg, 0,
4281 msg->msg_offset, msg->msg_len, msg->msg_len);
4285 lnet_ni_recv(ni, msg->msg_private, NULL, 0, 0, 0, 0);
4286 msg->msg_receiving = 0;
4288 rc = lnet_send(&ni->ni_nid, msg, &msg->msg_from);
4290 /* didn't get as far as lnet_ni_send() */
4291 CERROR("%s: Unable to send REPLY for GET from %s: %d\n",
4292 libcfs_nidstr(&ni->ni_nid),
4293 libcfs_idstr(&info.mi_id), rc);
4295 lnet_finalize(msg, rc);
4302 lnet_parse_reply(struct lnet_ni *ni, struct lnet_msg *msg)
4304 void *private = msg->msg_private;
4305 struct lnet_hdr *hdr = &msg->msg_hdr;
4306 struct lnet_processid src = {};
4307 struct lnet_libmd *md;
4308 unsigned int rlength;
4309 unsigned int mlength;
4312 cpt = lnet_cpt_of_cookie(hdr->msg.reply.dst_wmd.wh_object_cookie);
4315 src.nid = hdr->src_nid;
4316 src.pid = hdr->src_pid;
4318 /* NB handles only looked up by creator (no flips) */
4319 md = lnet_wire_handle2md(&hdr->msg.reply.dst_wmd);
4320 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
4321 CNETERR("%s: Dropping REPLY from %s for %s "
4323 libcfs_nidstr(&ni->ni_nid), libcfs_idstr(&src),
4324 (md == NULL) ? "invalid" : "inactive",
4325 hdr->msg.reply.dst_wmd.wh_interface_cookie,
4326 hdr->msg.reply.dst_wmd.wh_object_cookie);
4327 if (md != NULL && md->md_me != NULL)
4328 CERROR("REPLY MD also attached to portal %d\n",
4329 md->md_me->me_portal);
4331 lnet_res_unlock(cpt);
4332 return -ENOENT; /* -ve: OK but no match */
4335 LASSERT(md->md_offset == 0);
4337 rlength = hdr->payload_length;
4338 mlength = min(rlength, md->md_length);
4340 if (mlength < rlength &&
4341 (md->md_options & LNET_MD_TRUNCATE) == 0) {
4342 CNETERR("%s: Dropping REPLY from %s length %d "
4343 "for MD %#llx would overflow (%d)\n",
4344 libcfs_nidstr(&ni->ni_nid), libcfs_idstr(&src),
4345 rlength, hdr->msg.reply.dst_wmd.wh_object_cookie,
4347 lnet_res_unlock(cpt);
4348 return -ENOENT; /* -ve: OK but no match */
4351 CDEBUG(D_NET, "%s: Reply from %s of length %d/%d into md %#llx\n",
4352 libcfs_nidstr(&ni->ni_nid), libcfs_idstr(&src),
4353 mlength, rlength, hdr->msg.reply.dst_wmd.wh_object_cookie);
4355 lnet_msg_attach_md(msg, md, 0, mlength);
4358 lnet_setpayloadbuffer(msg);
4360 lnet_res_unlock(cpt);
4362 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
4364 lnet_ni_recv(ni, private, msg, 0, 0, mlength, rlength);
4369 lnet_parse_ack(struct lnet_ni *ni, struct lnet_msg *msg)
4371 struct lnet_hdr *hdr = &msg->msg_hdr;
4372 struct lnet_processid src = {};
4373 struct lnet_libmd *md;
4376 src.nid = hdr->src_nid;
4377 src.pid = hdr->src_pid;
4379 /* Convert ack fields to host byte order */
4380 hdr->msg.ack.match_bits = le64_to_cpu(hdr->msg.ack.match_bits);
4381 hdr->msg.ack.mlength = le32_to_cpu(hdr->msg.ack.mlength);
4383 cpt = lnet_cpt_of_cookie(hdr->msg.ack.dst_wmd.wh_object_cookie);
4386 /* NB handles only looked up by creator (no flips) */
4387 md = lnet_wire_handle2md(&hdr->msg.ack.dst_wmd);
4388 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
4389 /* Don't moan; this is expected */
4391 "%s: Dropping ACK from %s to %s MD %#llx.%#llx\n",
4392 libcfs_nidstr(&ni->ni_nid), libcfs_idstr(&src),
4393 (md == NULL) ? "invalid" : "inactive",
4394 hdr->msg.ack.dst_wmd.wh_interface_cookie,
4395 hdr->msg.ack.dst_wmd.wh_object_cookie);
4396 if (md != NULL && md->md_me != NULL)
4397 CERROR("Source MD also attached to portal %d\n",
4398 md->md_me->me_portal);
4400 lnet_res_unlock(cpt);
4401 return -ENOENT; /* -ve! */
4404 CDEBUG(D_NET, "%s: ACK from %s into md %#llx\n",
4405 libcfs_nidstr(&ni->ni_nid), libcfs_idstr(&src),
4406 hdr->msg.ack.dst_wmd.wh_object_cookie);
4408 lnet_msg_attach_md(msg, md, 0, 0);
4410 lnet_res_unlock(cpt);
4412 lnet_build_msg_event(msg, LNET_EVENT_ACK);
4414 lnet_ni_recv(ni, msg->msg_private, msg, 0, 0, 0, msg->msg_len);
4419 * \retval LNET_CREDIT_OK If \a msg is forwarded
4420 * \retval LNET_CREDIT_WAIT If \a msg is blocked because w/o buffer
4421 * \retval -ve error code
4424 lnet_parse_forward_locked(struct lnet_ni *ni, struct lnet_msg *msg)
4428 if (!the_lnet.ln_routing)
4431 if (msg->msg_rxpeer->lpni_rtrcredits <= 0 ||
4432 lnet_msg2bufpool(msg)->rbp_credits <= 0) {
4433 if (ni->ni_net->net_lnd->lnd_eager_recv == NULL) {
4434 msg->msg_rx_ready_delay = 1;
4436 lnet_net_unlock(msg->msg_rx_cpt);
4437 rc = lnet_ni_eager_recv(ni, msg);
4438 lnet_net_lock(msg->msg_rx_cpt);
4443 rc = lnet_post_routed_recv_locked(msg, 0);
4448 lnet_parse_local(struct lnet_ni *ni, struct lnet_msg *msg)
4452 switch (msg->msg_type) {
4454 rc = lnet_parse_ack(ni, msg);
4457 rc = lnet_parse_put(ni, msg);
4460 rc = lnet_parse_get(ni, msg, msg->msg_rdma_get);
4462 case LNET_MSG_REPLY:
4463 rc = lnet_parse_reply(ni, msg);
4465 default: /* prevent an unused label if !kernel */
4470 LASSERT(rc == 0 || rc == -ENOENT);
4475 lnet_msgtyp2str (int type)
4484 case LNET_MSG_REPLY:
4486 case LNET_MSG_HELLO:
4489 return ("<UNKNOWN>");
4492 EXPORT_SYMBOL(lnet_msgtyp2str);
4495 lnet_parse(struct lnet_ni *ni, struct lnet_hdr *hdr, lnet_nid_t from_nid4,
4496 void *private, int rdma_req)
4498 struct lnet_peer_ni *lpni;
4499 struct lnet_msg *msg;
4500 __u32 payload_length;
4501 lnet_pid_t dest_pid;
4502 struct lnet_nid dest_nid;
4503 struct lnet_nid src_nid;
4504 struct lnet_nid from_nid;
4511 LASSERT (!in_interrupt ());
4513 lnet_nid4_to_nid(from_nid4, &from_nid);
4516 src_nid = hdr->src_nid;
4517 dest_nid = hdr->dest_nid;
4518 dest_pid = hdr->dest_pid;
4519 payload_length = hdr->payload_length;
4521 for_me = nid_same(&ni->ni_nid, &dest_nid);
4522 cpt = lnet_nid2cpt(&from_nid, ni);
4524 CDEBUG(D_NET, "TRACE: %s(%s) <- %s : %s - %s\n",
4525 libcfs_nidstr(&dest_nid),
4526 libcfs_nidstr(&ni->ni_nid),
4527 libcfs_nidstr(&src_nid),
4528 lnet_msgtyp2str(type),
4529 (for_me) ? "for me" : "routed");
4534 if (payload_length > 0) {
4535 CERROR("%s, src %s: bad %s payload %d (0 expected)\n",
4536 libcfs_nid2str(from_nid4),
4537 libcfs_nidstr(&src_nid),
4538 lnet_msgtyp2str(type), payload_length);
4544 case LNET_MSG_REPLY:
4545 if (payload_length >
4546 (__u32)(for_me ? LNET_MAX_PAYLOAD : LNET_MTU)) {
4547 CERROR("%s, src %s: bad %s payload %d "
4548 "(%d max expected)\n",
4549 libcfs_nid2str(from_nid4),
4550 libcfs_nidstr(&src_nid),
4551 lnet_msgtyp2str(type),
4553 for_me ? LNET_MAX_PAYLOAD : LNET_MTU);
4559 CERROR("%s, src %s: Bad message type 0x%x\n",
4560 libcfs_nid2str(from_nid4),
4561 libcfs_nidstr(&src_nid), type);
4565 if (the_lnet.ln_routing &&
4566 ni->ni_net->net_last_alive != ktime_get_real_seconds()) {
4568 spin_lock(&ni->ni_net->net_lock);
4569 ni->ni_net->net_last_alive = ktime_get_real_seconds();
4570 spin_unlock(&ni->ni_net->net_lock);
4571 push = lnet_ni_set_status_locked(ni, LNET_NI_STATUS_UP);
4576 lnet_push_update_to_peers(1);
4578 /* Regard a bad destination NID as a protocol error. Senders should
4579 * know what they're doing; if they don't they're misconfigured, buggy
4580 * or malicious so we chop them off at the knees :) */
4583 if (LNET_NID_NET(&dest_nid) == LNET_NID_NET(&ni->ni_nid)) {
4584 /* should have gone direct */
4585 CERROR("%s, src %s: Bad dest nid %s "
4586 "(should have been sent direct)\n",
4587 libcfs_nid2str(from_nid4),
4588 libcfs_nidstr(&src_nid),
4589 libcfs_nidstr(&dest_nid));
4593 if (lnet_islocalnid(&dest_nid)) {
4594 /* dest is another local NI; sender should have used
4595 * this node's NID on its own network */
4596 CERROR("%s, src %s: Bad dest nid %s "
4597 "(it's my nid but on a different network)\n",
4598 libcfs_nid2str(from_nid4),
4599 libcfs_nidstr(&src_nid),
4600 libcfs_nidstr(&dest_nid));
4604 if (rdma_req && type == LNET_MSG_GET) {
4605 CERROR("%s, src %s: Bad optimized GET for %s "
4606 "(final destination must be me)\n",
4607 libcfs_nid2str(from_nid4),
4608 libcfs_nidstr(&src_nid),
4609 libcfs_nidstr(&dest_nid));
4613 if (!the_lnet.ln_routing) {
4614 CERROR("%s, src %s: Dropping message for %s "
4615 "(routing not enabled)\n",
4616 libcfs_nid2str(from_nid4),
4617 libcfs_nidstr(&src_nid),
4618 libcfs_nidstr(&dest_nid));
4623 /* Message looks OK; we're not going to return an error, so we MUST
4624 * call back lnd_recv() come what may... */
4626 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
4627 fail_peer(&src_nid, 0)) { /* shall we now? */
4628 CERROR("%s, src %s: Dropping %s to simulate failure\n",
4629 libcfs_nid2str(from_nid4), libcfs_nidstr(&src_nid),
4630 lnet_msgtyp2str(type));
4634 /* FIXME need to support large-addr nid */
4635 if (!list_empty(&the_lnet.ln_drop_rules) &&
4636 lnet_drop_rule_match(hdr, lnet_nid_to_nid4(&ni->ni_nid), NULL)) {
4638 "%s, src %s, dst %s: Dropping %s to simulate silent message loss\n",
4639 libcfs_nid2str(from_nid4), libcfs_nidstr(&src_nid),
4640 libcfs_nidstr(&dest_nid), lnet_msgtyp2str(type));
4644 msg = lnet_msg_alloc();
4646 CERROR("%s, src %s: Dropping %s (out of memory)\n",
4647 libcfs_nid2str(from_nid4), libcfs_nidstr(&src_nid),
4648 lnet_msgtyp2str(type));
4652 /* msg zeroed in lnet_msg_alloc; i.e. flags all clear,
4653 * pointers NULL etc */
4655 msg->msg_type = type;
4656 msg->msg_private = private;
4657 msg->msg_receiving = 1;
4658 msg->msg_rdma_get = rdma_req;
4659 msg->msg_len = msg->msg_wanted = payload_length;
4660 msg->msg_offset = 0;
4661 msg->msg_hdr = *hdr;
4662 /* for building message event */
4663 msg->msg_from = from_nid;
4665 msg->msg_target.pid = dest_pid;
4666 msg->msg_target.nid = dest_nid;
4667 msg->msg_routing = 1;
4671 lpni = lnet_peerni_by_nid_locked(&from_nid, &ni->ni_nid, cpt);
4673 lnet_net_unlock(cpt);
4675 CERROR("%s, src %s: Dropping %s (error %d looking up sender)\n",
4676 libcfs_nid2str(from_nid4), libcfs_nidstr(&src_nid),
4677 lnet_msgtyp2str(type), rc);
4679 if (rc == -ESHUTDOWN)
4680 /* We are shutting down. Don't do anything more */
4685 /* If this message was forwarded to us from a router then we may need
4686 * to update router aliveness or check for an asymmetrical route
4689 if (((lnet_drop_asym_route && for_me) ||
4690 !lpni->lpni_peer_net->lpn_peer->lp_alive) &&
4691 LNET_NID_NET(&src_nid) != LNET_NIDNET(from_nid4)) {
4692 __u32 src_net_id = LNET_NID_NET(&src_nid);
4693 struct lnet_peer *gw = lpni->lpni_peer_net->lpn_peer;
4694 struct lnet_route *route;
4697 list_for_each_entry(route, &gw->lp_routes, lr_gwlist) {
4698 if (route->lr_net == src_net_id) {
4700 /* If we're transitioning the gateway from
4701 * dead -> alive, and discovery is disabled
4702 * locally or on the gateway, then we need to
4703 * update the cached route aliveness for each
4704 * route to the src_nid's net.
4706 * Otherwise, we're only checking for
4707 * symmetrical route, and we can break the
4710 if (!gw->lp_alive &&
4711 lnet_is_discovery_disabled(gw))
4712 lnet_set_route_aliveness(route, true);
4717 if (lnet_drop_asym_route && for_me && !found) {
4718 /* Drop ref taken by lnet_nid2peerni_locked() */
4719 lnet_peer_ni_decref_locked(lpni);
4720 lnet_net_unlock(cpt);
4721 /* we would not use from_nid to route a message to
4723 * => asymmetric routing detected but forbidden
4725 CERROR("%s, src %s: Dropping asymmetrical route %s\n",
4726 libcfs_nid2str(from_nid4),
4727 libcfs_nidstr(&src_nid), lnet_msgtyp2str(type));
4731 if (!gw->lp_alive) {
4732 struct lnet_peer_net *lpn;
4733 struct lnet_peer_ni *lpni2;
4735 gw->lp_alive = true;
4736 /* Mark all remote NIs on src_nid's net UP */
4737 lpn = lnet_peer_get_net_locked(gw, src_net_id);
4739 list_for_each_entry(lpni2, &lpn->lpn_peer_nis,
4741 lpni2->lpni_ns_status = LNET_NI_STATUS_UP;
4745 lpni->lpni_last_alive = ktime_get_seconds();
4747 msg->msg_rxpeer = lpni;
4749 lnet_ni_addref_locked(ni, cpt);
4750 /* Multi-Rail: Primary NID of source. */
4751 lnet_peer_primary_nid_locked(&src_nid, &msg->msg_initiator);
4754 * mark the status of this lpni as UP since we received a message
4755 * from it. The ping response reports back the ns_status which is
4756 * marked on the remote as up or down and we cache it here.
4758 msg->msg_rxpeer->lpni_ns_status = LNET_NI_STATUS_UP;
4760 lnet_msg_commit(msg, cpt);
4762 /* message delay simulation */
4763 if (unlikely(!list_empty(&the_lnet.ln_delay_rules) &&
4764 lnet_delay_rule_match_locked(hdr, msg))) {
4765 lnet_net_unlock(cpt);
4770 rc = lnet_parse_forward_locked(ni, msg);
4771 lnet_net_unlock(cpt);
4776 if (rc == LNET_CREDIT_OK) {
4777 lnet_ni_recv(ni, msg->msg_private, msg, 0,
4778 0, payload_length, payload_length);
4783 lnet_net_unlock(cpt);
4785 rc = lnet_parse_local(ni, msg);
4791 LASSERT(msg->msg_md == NULL);
4792 lnet_finalize(msg, rc);
4795 lnet_drop_message(ni, cpt, private, payload_length, type);
4798 EXPORT_SYMBOL(lnet_parse);
4801 lnet_drop_delayed_msg_list(struct list_head *head, char *reason)
4803 while (!list_empty(head)) {
4804 struct lnet_processid id = {};
4805 struct lnet_msg *msg;
4807 msg = list_entry(head->next, struct lnet_msg, msg_list);
4808 list_del(&msg->msg_list);
4810 id.nid = msg->msg_hdr.src_nid;
4811 id.pid = msg->msg_hdr.src_pid;
4813 LASSERT(msg->msg_md == NULL);
4814 LASSERT(msg->msg_rx_delayed);
4815 LASSERT(msg->msg_rxpeer != NULL);
4816 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
4818 CWARN("Dropping delayed PUT from %s portal %d match %llu"
4819 " offset %d length %d: %s\n",
4821 msg->msg_hdr.msg.put.ptl_index,
4822 msg->msg_hdr.msg.put.match_bits,
4823 msg->msg_hdr.msg.put.offset,
4824 msg->msg_hdr.payload_length, reason);
4826 /* NB I can't drop msg's ref on msg_rxpeer until after I've
4827 * called lnet_drop_message(), so I just hang onto msg as well
4828 * until that's done */
4830 lnet_drop_message(msg->msg_rxni, msg->msg_rx_cpt,
4831 msg->msg_private, msg->msg_len,
4834 msg->msg_no_resend = true;
4836 * NB: message will not generate event because w/o attached MD,
4837 * but we still should give error code so lnet_msg_decommit()
4838 * can skip counters operations and other checks.
4840 lnet_finalize(msg, -ENOENT);
4845 lnet_recv_delayed_msg_list(struct list_head *head)
4847 while (!list_empty(head)) {
4848 struct lnet_msg *msg;
4849 struct lnet_processid id;
4851 msg = list_entry(head->next, struct lnet_msg, msg_list);
4852 list_del(&msg->msg_list);
4854 /* md won't disappear under me, since each msg
4855 * holds a ref on it */
4857 id.nid = msg->msg_hdr.src_nid;
4858 id.pid = msg->msg_hdr.src_pid;
4860 LASSERT(msg->msg_rx_delayed);
4861 LASSERT(msg->msg_md != NULL);
4862 LASSERT(msg->msg_rxpeer != NULL);
4863 LASSERT(msg->msg_rxni != NULL);
4864 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
4866 CDEBUG(D_NET, "Resuming delayed PUT from %s portal %d "
4867 "match %llu offset %d length %d.\n",
4868 libcfs_idstr(&id), msg->msg_hdr.msg.put.ptl_index,
4869 msg->msg_hdr.msg.put.match_bits,
4870 msg->msg_hdr.msg.put.offset,
4871 msg->msg_hdr.payload_length);
4873 lnet_recv_put(msg->msg_rxni, msg);
4878 lnet_attach_rsp_tracker(struct lnet_rsp_tracker *rspt, int cpt,
4879 struct lnet_libmd *md, struct lnet_handle_md mdh)
4882 struct lnet_rsp_tracker *local_rspt;
4885 * MD has a refcount taken by message so it's not going away.
4886 * The MD however can be looked up. We need to secure the access
4887 * to the md_rspt_ptr by taking the res_lock.
4888 * The rspt can be accessed without protection up to when it gets
4889 * added to the list.
4893 local_rspt = md->md_rspt_ptr;
4894 timeout_ns = lnet_transaction_timeout * NSEC_PER_SEC;
4895 if (local_rspt != NULL) {
4897 * we already have an rspt attached to the md, so we'll
4898 * update the deadline on that one.
4900 lnet_rspt_free(rspt, cpt);
4903 rspt->rspt_mdh = mdh;
4904 rspt->rspt_cpt = cpt;
4905 /* store the rspt so we can access it when we get the REPLY */
4906 md->md_rspt_ptr = rspt;
4909 local_rspt->rspt_deadline = ktime_add_ns(ktime_get(), timeout_ns);
4912 * add to the list of tracked responses. It's added to tail of the
4913 * list in order to expire all the older entries first.
4916 list_move_tail(&local_rspt->rspt_on_list, the_lnet.ln_mt_rstq[cpt]);
4917 lnet_net_unlock(cpt);
4918 lnet_res_unlock(cpt);
4922 * Initiate an asynchronous PUT operation.
4924 * There are several events associated with a PUT: completion of the send on
4925 * the initiator node (LNET_EVENT_SEND), and when the send completes
4926 * successfully, the receipt of an acknowledgment (LNET_EVENT_ACK) indicating
4927 * that the operation was accepted by the target. The event LNET_EVENT_PUT is
4928 * used at the target node to indicate the completion of incoming data
4931 * The local events will be logged in the EQ associated with the MD pointed to
4932 * by \a mdh handle. Using a MD without an associated EQ results in these
4933 * events being discarded. In this case, the caller must have another
4934 * mechanism (e.g., a higher level protocol) for determining when it is safe
4935 * to modify the memory region associated with the MD.
4937 * Note that LNet does not guarantee the order of LNET_EVENT_SEND and
4938 * LNET_EVENT_ACK, though intuitively ACK should happen after SEND.
4940 * \param self Indicates the NID of a local interface through which to send
4941 * the PUT request. Use LNET_NID_ANY to let LNet choose one by itself.
4942 * \param mdh A handle for the MD that describes the memory to be sent. The MD
4943 * must be "free floating" (See LNetMDBind()).
4944 * \param ack Controls whether an acknowledgment is requested.
4945 * Acknowledgments are only sent when they are requested by the initiating
4946 * process and the target MD enables them.
4947 * \param target A process identifier for the target process.
4948 * \param portal The index in the \a target's portal table.
4949 * \param match_bits The match bits to use for MD selection at the target
4951 * \param offset The offset into the target MD (only used when the target
4952 * MD has the LNET_MD_MANAGE_REMOTE option set).
4953 * \param hdr_data 64 bits of user data that can be included in the message
4954 * header. This data is written to an event queue entry at the target if an
4955 * EQ is present on the matching MD.
4957 * \retval 0 Success, and only in this case events will be generated
4958 * and logged to EQ (if it exists).
4959 * \retval -EIO Simulated failure.
4960 * \retval -ENOMEM Memory allocation failure.
4961 * \retval -ENOENT Invalid MD object.
4963 * \see struct lnet_event::hdr_data and lnet_event_kind_t.
4966 LNetPut(lnet_nid_t self4, struct lnet_handle_md mdh, enum lnet_ack_req ack,
4967 struct lnet_process_id target4, unsigned int portal,
4968 __u64 match_bits, unsigned int offset,
4971 struct lnet_msg *msg;
4972 struct lnet_libmd *md;
4975 struct lnet_processid target;
4976 struct lnet_rsp_tracker *rspt = NULL;
4977 struct lnet_nid self;
4979 LASSERT(the_lnet.ln_refcount > 0);
4981 lnet_nid4_to_nid(self4, &self);
4982 lnet_nid4_to_nid(target4.nid, &target.nid);
4983 target.pid = target4.pid;
4985 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
4986 fail_peer(&target.nid, 1)) { /* shall we now? */
4987 CERROR("Dropping PUT to %s: simulated failure\n",
4988 libcfs_id2str(target4));
4992 msg = lnet_msg_alloc();
4994 CERROR("Dropping PUT to %s: ENOMEM on struct lnet_msg\n",
4995 libcfs_id2str(target4));
4998 msg->msg_vmflush = !!(current->flags & PF_MEMALLOC);
5000 cpt = lnet_cpt_of_cookie(mdh.cookie);
5002 if (ack == LNET_ACK_REQ) {
5003 rspt = lnet_rspt_alloc(cpt);
5005 CERROR("Dropping PUT to %s: ENOMEM on response tracker\n",
5006 libcfs_id2str(target4));
5009 INIT_LIST_HEAD(&rspt->rspt_on_list);
5014 md = lnet_handle2md(&mdh);
5015 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
5016 CERROR("Dropping PUT (%llu:%d:%s): MD (%d) invalid\n",
5017 match_bits, portal, libcfs_id2str(target4),
5018 md == NULL ? -1 : md->md_threshold);
5019 if (md != NULL && md->md_me != NULL)
5020 CERROR("Source MD also attached to portal %d\n",
5021 md->md_me->me_portal);
5022 lnet_res_unlock(cpt);
5025 lnet_rspt_free(rspt, cpt);
5031 CDEBUG(D_NET, "%s -> %s\n", __func__, libcfs_id2str(target4));
5033 lnet_msg_attach_md(msg, md, 0, 0);
5035 lnet_prep_send(msg, LNET_MSG_PUT, &target, 0, md->md_length);
5037 msg->msg_hdr.msg.put.match_bits = cpu_to_le64(match_bits);
5038 msg->msg_hdr.msg.put.ptl_index = cpu_to_le32(portal);
5039 msg->msg_hdr.msg.put.offset = cpu_to_le32(offset);
5040 msg->msg_hdr.msg.put.hdr_data = hdr_data;
5042 /* NB handles only looked up by creator (no flips) */
5043 if (ack == LNET_ACK_REQ) {
5044 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
5045 the_lnet.ln_interface_cookie;
5046 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
5047 md->md_lh.lh_cookie;
5049 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
5050 LNET_WIRE_HANDLE_COOKIE_NONE;
5051 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
5052 LNET_WIRE_HANDLE_COOKIE_NONE;
5055 lnet_res_unlock(cpt);
5057 lnet_build_msg_event(msg, LNET_EVENT_SEND);
5059 if (rspt && lnet_response_tracking_enabled(LNET_MSG_PUT,
5061 lnet_attach_rsp_tracker(rspt, cpt, md, mdh);
5063 lnet_rspt_free(rspt, cpt);
5065 if (CFS_FAIL_CHECK_ORSET(CFS_FAIL_PTLRPC_OST_BULK_CB2,
5069 rc = lnet_send(&self, msg, NULL);
5072 CNETERR("Error sending PUT to %s: %d\n",
5073 libcfs_id2str(target4), rc);
5074 msg->msg_no_resend = true;
5075 lnet_finalize(msg, rc);
5078 /* completion will be signalled by an event */
5081 EXPORT_SYMBOL(LNetPut);
5084 * The LND can DMA direct to the GET md (i.e. no REPLY msg). This
5085 * returns a msg for the LND to pass to lnet_finalize() when the sink
5086 * data has been received.
5088 * CAVEAT EMPTOR: 'getmsg' is the original GET, which is freed when
5089 * lnet_finalize() is called on it, so the LND must call this first
5092 lnet_create_reply_msg(struct lnet_ni *ni, struct lnet_msg *getmsg)
5094 struct lnet_msg *msg = lnet_msg_alloc();
5095 struct lnet_libmd *getmd = getmsg->msg_md;
5096 struct lnet_processid *peer_id = &getmsg->msg_target;
5099 LASSERT(!getmsg->msg_target_is_router);
5100 LASSERT(!getmsg->msg_routing);
5103 CERROR("%s: Dropping REPLY from %s: can't allocate msg\n",
5104 libcfs_nidstr(&ni->ni_nid), libcfs_idstr(peer_id));
5108 cpt = lnet_cpt_of_cookie(getmd->md_lh.lh_cookie);
5111 LASSERT(getmd->md_refcount > 0);
5113 if (getmd->md_threshold == 0) {
5114 CERROR("%s: Dropping REPLY from %s for inactive MD %p\n",
5115 libcfs_nidstr(&ni->ni_nid), libcfs_idstr(peer_id),
5117 lnet_res_unlock(cpt);
5121 LASSERT(getmd->md_offset == 0);
5123 CDEBUG(D_NET, "%s: Reply from %s md %p\n",
5124 libcfs_nidstr(&ni->ni_nid), libcfs_idstr(peer_id), getmd);
5126 /* setup information for lnet_build_msg_event */
5127 msg->msg_initiator =
5128 getmsg->msg_txpeer->lpni_peer_net->lpn_peer->lp_primary_nid;
5129 msg->msg_from = peer_id->nid;
5130 msg->msg_type = LNET_MSG_GET; /* flag this msg as an "optimized" GET */
5131 msg->msg_hdr.src_nid = peer_id->nid;
5132 msg->msg_hdr.payload_length = getmd->md_length;
5133 msg->msg_receiving = 1; /* required by lnet_msg_attach_md */
5135 lnet_msg_attach_md(msg, getmd, getmd->md_offset, getmd->md_length);
5136 lnet_res_unlock(cpt);
5138 cpt = lnet_nid2cpt(&peer_id->nid, ni);
5141 lnet_msg_commit(msg, cpt);
5142 lnet_net_unlock(cpt);
5144 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
5149 cpt = lnet_nid2cpt(&peer_id->nid, ni);
5152 lnet_incr_stats(&ni->ni_stats, LNET_MSG_GET, LNET_STATS_TYPE_DROP);
5153 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
5154 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length +=
5156 lnet_net_unlock(cpt);
5163 EXPORT_SYMBOL(lnet_create_reply_msg);
5166 lnet_set_reply_msg_len(struct lnet_ni *ni, struct lnet_msg *reply,
5169 /* Set the REPLY length, now the RDMA that elides the REPLY message has
5170 * completed and I know it. */
5171 LASSERT(reply != NULL);
5172 LASSERT(reply->msg_type == LNET_MSG_GET);
5173 LASSERT(reply->msg_ev.type == LNET_EVENT_REPLY);
5175 /* NB I trusted my peer to RDMA. If she tells me she's written beyond
5176 * the end of my buffer, I might as well be dead. */
5177 LASSERT(len <= reply->msg_ev.mlength);
5179 reply->msg_ev.mlength = len;
5181 EXPORT_SYMBOL(lnet_set_reply_msg_len);
5184 * Initiate an asynchronous GET operation.
5186 * On the initiator node, an LNET_EVENT_SEND is logged when the GET request
5187 * is sent, and an LNET_EVENT_REPLY is logged when the data returned from
5188 * the target node in the REPLY has been written to local MD.
5190 * On the target node, an LNET_EVENT_GET is logged when the GET request
5191 * arrives and is accepted into a MD.
5193 * \param self,target,portal,match_bits,offset See the discussion in LNetPut().
5194 * \param mdh A handle for the MD that describes the memory into which the
5195 * requested data will be received. The MD must be "free floating" (See LNetMDBind()).
5197 * \retval 0 Success, and only in this case events will be generated
5198 * and logged to EQ (if it exists) of the MD.
5199 * \retval -EIO Simulated failure.
5200 * \retval -ENOMEM Memory allocation failure.
5201 * \retval -ENOENT Invalid MD object.
5204 LNetGet(lnet_nid_t self4, struct lnet_handle_md mdh,
5205 struct lnet_process_id target4, unsigned int portal,
5206 __u64 match_bits, unsigned int offset, bool recovery)
5208 struct lnet_msg *msg;
5209 struct lnet_libmd *md;
5210 struct lnet_rsp_tracker *rspt;
5213 struct lnet_nid self;
5214 struct lnet_processid target;
5216 LASSERT(the_lnet.ln_refcount > 0);
5218 lnet_nid4_to_nid(self4, &self);
5219 lnet_nid4_to_nid(target4.nid, &target.nid);
5220 target.pid = target4.pid;
5222 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
5223 fail_peer(&target.nid, 1)) /* shall we now? */
5225 CERROR("Dropping GET to %s: simulated failure\n",
5226 libcfs_id2str(target4));
5230 msg = lnet_msg_alloc();
5232 CERROR("Dropping GET to %s: ENOMEM on struct lnet_msg\n",
5233 libcfs_id2str(target4));
5237 cpt = lnet_cpt_of_cookie(mdh.cookie);
5239 rspt = lnet_rspt_alloc(cpt);
5241 CERROR("Dropping GET to %s: ENOMEM on response tracker\n",
5242 libcfs_id2str(target4));
5245 INIT_LIST_HEAD(&rspt->rspt_on_list);
5247 msg->msg_recovery = recovery;
5251 md = lnet_handle2md(&mdh);
5252 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
5253 CERROR("Dropping GET (%llu:%d:%s): MD (%d) invalid\n",
5254 match_bits, portal, libcfs_id2str(target4),
5255 md == NULL ? -1 : md->md_threshold);
5256 if (md != NULL && md->md_me != NULL)
5257 CERROR("REPLY MD also attached to portal %d\n",
5258 md->md_me->me_portal);
5260 lnet_res_unlock(cpt);
5263 lnet_rspt_free(rspt, cpt);
5267 CDEBUG(D_NET, "%s -> %s\n", __func__, libcfs_id2str(target4));
5269 lnet_msg_attach_md(msg, md, 0, 0);
5271 lnet_prep_send(msg, LNET_MSG_GET, &target, 0, 0);
5273 msg->msg_hdr.msg.get.match_bits = cpu_to_le64(match_bits);
5274 msg->msg_hdr.msg.get.ptl_index = cpu_to_le32(portal);
5275 msg->msg_hdr.msg.get.src_offset = cpu_to_le32(offset);
5276 msg->msg_hdr.msg.get.sink_length = cpu_to_le32(md->md_length);
5278 /* NB handles only looked up by creator (no flips) */
5279 msg->msg_hdr.msg.get.return_wmd.wh_interface_cookie =
5280 the_lnet.ln_interface_cookie;
5281 msg->msg_hdr.msg.get.return_wmd.wh_object_cookie =
5282 md->md_lh.lh_cookie;
5284 lnet_res_unlock(cpt);
5286 lnet_build_msg_event(msg, LNET_EVENT_SEND);
5288 if (lnet_response_tracking_enabled(LNET_MSG_GET, md->md_options))
5289 lnet_attach_rsp_tracker(rspt, cpt, md, mdh);
5291 lnet_rspt_free(rspt, cpt);
5293 rc = lnet_send(&self, msg, NULL);
5295 CNETERR("Error sending GET to %s: %d\n",
5296 libcfs_id2str(target4), rc);
5297 msg->msg_no_resend = true;
5298 lnet_finalize(msg, rc);
5301 /* completion will be signalled by an event */
5304 EXPORT_SYMBOL(LNetGet);
5307 * Calculate distance to node at \a dstnid.
5309 * \param dstnid Target NID.
5310 * \param srcnidp If not NULL, NID of the local interface to reach \a dstnid
5312 * \param orderp If not NULL, order of the route to reach \a dstnid is saved
5315 * \retval 0 If \a dstnid belongs to a local interface, and reserved option
5316 * local_nid_dist_zero is set, which is the default.
5317 * \retval positives Distance to target NID, i.e. number of hops plus one.
5318 * \retval -EHOSTUNREACH If \a dstnid is not reachable.
5321 LNetDist(lnet_nid_t dstnid, lnet_nid_t *srcnidp, __u32 *orderp)
5323 struct list_head *e;
5324 struct lnet_ni *ni = NULL;
5325 struct lnet_remotenet *rnet;
5326 __u32 dstnet = LNET_NIDNET(dstnid);
5330 struct list_head *rn_list;
5331 bool matched_dstnet = false;
5333 /* if !local_nid_dist_zero, I don't return a distance of 0 ever
5334 * (when lustre sees a distance of 0, it substitutes 0@lo), so I
5335 * keep order 0 free for 0@lo and order 1 free for a local NID
5338 LASSERT(the_lnet.ln_refcount > 0);
5340 cpt = lnet_net_lock_current();
5342 while ((ni = lnet_get_next_ni_locked(NULL, ni))) {
5343 /* FIXME support large-addr nid */
5344 if (lnet_nid_to_nid4(&ni->ni_nid) == dstnid) {
5345 if (srcnidp != NULL)
5347 if (orderp != NULL) {
5348 if (dstnid == LNET_NID_LO_0)
5353 lnet_net_unlock(cpt);
5355 return local_nid_dist_zero ? 0 : 1;
5358 if (!matched_dstnet && LNET_NID_NET(&ni->ni_nid) == dstnet) {
5359 matched_dstnet = true;
5360 /* We matched the destination net, but we may have
5361 * additional local NIs to inspect.
5363 * We record the nid and order as appropriate, but
5364 * they may be overwritten if we match local NI above.
5367 /* FIXME support large-addr nids */
5368 *srcnidp = lnet_nid_to_nid4(&ni->ni_nid);
5371 /* Check if ni was originally created in
5372 * current net namespace.
5373 * If not, assign order above 0xffff0000,
5374 * to make this ni not a priority.
5376 if (current->nsproxy &&
5377 !net_eq(ni->ni_net_ns,
5378 current->nsproxy->net_ns))
5379 *orderp = order + 0xffff0000;
5388 if (matched_dstnet) {
5389 lnet_net_unlock(cpt);
5393 rn_list = lnet_net2rnethash(dstnet);
5394 list_for_each(e, rn_list) {
5395 rnet = list_entry(e, struct lnet_remotenet, lrn_list);
5397 if (rnet->lrn_net == dstnet) {
5398 struct lnet_route *route;
5399 struct lnet_route *shortest = NULL;
5400 __u32 shortest_hops = LNET_UNDEFINED_HOPS;
5403 LASSERT(!list_empty(&rnet->lrn_routes));
5405 list_for_each_entry(route, &rnet->lrn_routes,
5407 route_hops = route->lr_hops;
5408 if (route_hops == LNET_UNDEFINED_HOPS)
5410 if (shortest == NULL ||
5411 route_hops < shortest_hops) {
5413 shortest_hops = route_hops;
5417 LASSERT(shortest != NULL);
5418 hops = shortest_hops;
5419 if (srcnidp != NULL) {
5420 struct lnet_net *net;
5421 net = lnet_get_net_locked(shortest->lr_lnet);
5423 ni = lnet_get_next_ni_locked(net, NULL);
5424 /* FIXME support large-addr nids */
5425 *srcnidp = lnet_nid_to_nid4(&ni->ni_nid);
5429 lnet_net_unlock(cpt);
5435 lnet_net_unlock(cpt);
5436 return -EHOSTUNREACH;
5438 EXPORT_SYMBOL(LNetDist);