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(lnet_nid_t nid4, int outgoing)
244 struct lnet_test_peer *tp;
245 struct list_head *el;
246 struct list_head *next;
251 lnet_nid4_to_nid(nid4, &nid);
252 /* NB: use lnet_net_lock(0) to serialize operations on test peers */
255 list_for_each_safe(el, next, &the_lnet.ln_test_peers) {
256 tp = list_entry(el, struct lnet_test_peer, tp_list);
258 if (tp->tp_threshold == 0) {
261 /* only cull zombies on outgoing tests,
262 * since we may be at interrupt priority on
263 * incoming messages. */
264 list_move(&tp->tp_list, &cull);
269 if (LNET_NID_IS_ANY(&tp->tp_nid) || /* fail every peer */
270 nid_same(&nid, &tp->tp_nid)) { /* fail this peer */
273 if (tp->tp_threshold != LNET_MD_THRESH_INF) {
276 tp->tp_threshold == 0) {
278 list_move(&tp->tp_list, &cull);
287 while (!list_empty(&cull)) {
288 tp = list_entry(cull.next, struct lnet_test_peer, tp_list);
289 list_del(&tp->tp_list);
291 LIBCFS_FREE(tp, sizeof(*tp));
298 lnet_iov_nob(unsigned int niov, struct kvec *iov)
300 unsigned int nob = 0;
302 LASSERT(niov == 0 || iov != NULL);
304 nob += (iov++)->iov_len;
308 EXPORT_SYMBOL(lnet_iov_nob);
311 lnet_copy_iov2iov(unsigned int ndiov, struct kvec *diov, unsigned int doffset,
312 unsigned int nsiov, struct kvec *siov, unsigned int soffset,
315 /* NB diov, siov are READ-ONLY */
316 unsigned int this_nob;
321 /* skip complete frags before 'doffset' */
323 while (doffset >= diov->iov_len) {
324 doffset -= diov->iov_len;
330 /* skip complete frags before 'soffset' */
332 while (soffset >= siov->iov_len) {
333 soffset -= siov->iov_len;
342 this_nob = min3((unsigned int)diov->iov_len - doffset,
343 (unsigned int)siov->iov_len - soffset,
346 memcpy((char *)diov->iov_base + doffset,
347 (char *)siov->iov_base + soffset, this_nob);
350 if (diov->iov_len > doffset + this_nob) {
358 if (siov->iov_len > soffset + this_nob) {
367 EXPORT_SYMBOL(lnet_copy_iov2iov);
370 lnet_kiov_nob(unsigned int niov, struct bio_vec *kiov)
372 unsigned int nob = 0;
374 LASSERT(niov == 0 || kiov != NULL);
376 nob += (kiov++)->bv_len;
380 EXPORT_SYMBOL(lnet_kiov_nob);
383 lnet_copy_kiov2kiov(unsigned int ndiov, struct bio_vec *diov,
384 unsigned int doffset,
385 unsigned int nsiov, struct bio_vec *siov,
386 unsigned int soffset,
389 /* NB diov, siov are READ-ONLY */
390 unsigned int this_nob;
397 LASSERT (!in_interrupt ());
400 while (doffset >= diov->bv_len) {
401 doffset -= diov->bv_len;
408 while (soffset >= siov->bv_len) {
409 soffset -= siov->bv_len;
418 this_nob = min3(diov->bv_len - doffset,
419 siov->bv_len - soffset,
423 daddr = ((char *)kmap(diov->bv_page)) +
424 diov->bv_offset + doffset;
426 saddr = ((char *)kmap(siov->bv_page)) +
427 siov->bv_offset + soffset;
429 /* Vanishing risk of kmap deadlock when mapping 2 pages.
430 * However in practice at least one of the kiovs will be mapped
431 * kernel pages and the map/unmap will be NOOPs */
433 memcpy (daddr, saddr, this_nob);
436 if (diov->bv_len > doffset + this_nob) {
440 kunmap(diov->bv_page);
447 if (siov->bv_len > soffset + this_nob) {
451 kunmap(siov->bv_page);
460 kunmap(diov->bv_page);
462 kunmap(siov->bv_page);
464 EXPORT_SYMBOL(lnet_copy_kiov2kiov);
467 lnet_copy_kiov2iov (unsigned int niov, struct kvec *iov, unsigned int iovoffset,
468 unsigned int nkiov, struct bio_vec *kiov,
469 unsigned int kiovoffset,
472 /* NB iov, kiov are READ-ONLY */
473 unsigned int this_nob;
479 LASSERT (!in_interrupt ());
482 while (iovoffset >= iov->iov_len) {
483 iovoffset -= iov->iov_len;
490 while (kiovoffset >= kiov->bv_len) {
491 kiovoffset -= kiov->bv_len;
500 this_nob = min3((unsigned int)iov->iov_len - iovoffset,
501 (unsigned int)kiov->bv_len - kiovoffset,
505 addr = ((char *)kmap(kiov->bv_page)) +
506 kiov->bv_offset + kiovoffset;
508 memcpy((char *)iov->iov_base + iovoffset, addr, this_nob);
511 if (iov->iov_len > iovoffset + this_nob) {
512 iovoffset += this_nob;
519 if (kiov->bv_len > kiovoffset + this_nob) {
521 kiovoffset += this_nob;
523 kunmap(kiov->bv_page);
533 kunmap(kiov->bv_page);
535 EXPORT_SYMBOL(lnet_copy_kiov2iov);
538 lnet_copy_iov2kiov(unsigned int nkiov, struct bio_vec *kiov,
539 unsigned int kiovoffset,
540 unsigned int niov, struct kvec *iov, unsigned int iovoffset,
543 /* NB kiov, iov are READ-ONLY */
544 unsigned int this_nob;
550 LASSERT (!in_interrupt ());
553 while (kiovoffset >= kiov->bv_len) {
554 kiovoffset -= kiov->bv_len;
561 while (iovoffset >= iov->iov_len) {
562 iovoffset -= iov->iov_len;
571 this_nob = min3((unsigned int)kiov->bv_len - kiovoffset,
572 (unsigned int)iov->iov_len - iovoffset,
576 addr = ((char *)kmap(kiov->bv_page)) +
577 kiov->bv_offset + kiovoffset;
579 memcpy (addr, (char *)iov->iov_base + iovoffset, this_nob);
582 if (kiov->bv_len > kiovoffset + this_nob) {
584 kiovoffset += this_nob;
586 kunmap(kiov->bv_page);
593 if (iov->iov_len > iovoffset + this_nob) {
594 iovoffset += this_nob;
603 kunmap(kiov->bv_page);
605 EXPORT_SYMBOL(lnet_copy_iov2kiov);
608 lnet_extract_kiov(int dst_niov, struct bio_vec *dst,
609 int src_niov, struct bio_vec *src,
610 unsigned int offset, unsigned int len)
612 /* Initialise 'dst' to the subset of 'src' starting at 'offset',
613 * for exactly 'len' bytes, and return the number of entries.
614 * NB not destructive to 'src' */
615 unsigned int frag_len;
618 if (len == 0) /* no data => */
619 return (0); /* no frags */
621 LASSERT(src_niov > 0);
622 while (offset >= src->bv_len) { /* skip initial frags */
623 offset -= src->bv_len;
626 LASSERT(src_niov > 0);
631 LASSERT(src_niov > 0);
632 LASSERT((int)niov <= dst_niov);
634 frag_len = src->bv_len - offset;
635 dst->bv_page = src->bv_page;
636 dst->bv_offset = src->bv_offset + offset;
638 if (len <= frag_len) {
640 LASSERT(dst->bv_offset + dst->bv_len <= PAGE_SIZE);
644 dst->bv_len = frag_len;
645 LASSERT(dst->bv_offset + dst->bv_len <= PAGE_SIZE);
655 EXPORT_SYMBOL(lnet_extract_kiov);
658 lnet_ni_recv(struct lnet_ni *ni, void *private, struct lnet_msg *msg,
659 int delayed, unsigned int offset, unsigned int mlen,
662 unsigned int niov = 0;
663 struct kvec *iov = NULL;
664 struct bio_vec *kiov = NULL;
667 LASSERT (!in_interrupt ());
668 LASSERT (mlen == 0 || msg != NULL);
671 LASSERT(msg->msg_receiving);
672 LASSERT(!msg->msg_sending);
673 LASSERT(rlen == msg->msg_len);
674 LASSERT(mlen <= msg->msg_len);
675 LASSERT(msg->msg_offset == offset);
676 LASSERT(msg->msg_wanted == mlen);
678 msg->msg_receiving = 0;
681 niov = msg->msg_niov;
682 kiov = msg->msg_kiov;
685 LASSERT ((iov == NULL) != (kiov == NULL));
689 rc = (ni->ni_net->net_lnd->lnd_recv)(ni, private, msg, delayed,
690 niov, kiov, offset, mlen,
693 lnet_finalize(msg, rc);
697 lnet_setpayloadbuffer(struct lnet_msg *msg)
699 struct lnet_libmd *md = msg->msg_md;
701 LASSERT(msg->msg_len > 0);
702 LASSERT(!msg->msg_routing);
704 LASSERT(msg->msg_niov == 0);
705 LASSERT(msg->msg_kiov == NULL);
707 msg->msg_niov = md->md_niov;
708 msg->msg_kiov = md->md_kiov;
712 lnet_prep_send(struct lnet_msg *msg, int type, struct lnet_processid *target,
713 unsigned int offset, unsigned int len)
715 msg->msg_type = type;
716 msg->msg_target = *target;
718 msg->msg_offset = offset;
721 lnet_setpayloadbuffer(msg);
723 memset (&msg->msg_hdr, 0, sizeof (msg->msg_hdr));
724 msg->msg_hdr.type = type;
725 /* dest_nid will be overwritten by lnet_select_pathway() */
726 msg->msg_hdr.dest_nid = target->nid;
727 msg->msg_hdr.dest_pid = target->pid;
728 /* src_nid will be set later */
729 msg->msg_hdr.src_pid = the_lnet.ln_pid;
730 msg->msg_hdr.payload_length = len;
734 lnet_ni_send(struct lnet_ni *ni, struct lnet_msg *msg)
736 void *priv = msg->msg_private;
739 LASSERT(!in_interrupt());
740 LASSERT(nid_is_lo0(&ni->ni_nid) ||
741 (msg->msg_txcredit && msg->msg_peertxcredit));
743 rc = (ni->ni_net->net_lnd->lnd_send)(ni, priv, msg);
745 msg->msg_no_resend = true;
746 lnet_finalize(msg, rc);
751 lnet_ni_eager_recv(struct lnet_ni *ni, struct lnet_msg *msg)
755 LASSERT(!msg->msg_sending);
756 LASSERT(msg->msg_receiving);
757 LASSERT(!msg->msg_rx_ready_delay);
758 LASSERT(ni->ni_net->net_lnd->lnd_eager_recv != NULL);
760 msg->msg_rx_ready_delay = 1;
761 rc = (ni->ni_net->net_lnd->lnd_eager_recv)(ni, msg->msg_private, msg,
764 CERROR("recv from %s / send to %s aborted: "
765 "eager_recv failed %d\n",
766 libcfs_nidstr(&msg->msg_rxpeer->lpni_nid),
767 libcfs_idstr(&msg->msg_target), rc);
768 LASSERT(rc < 0); /* required by my callers */
775 lnet_is_peer_deadline_passed(struct lnet_peer_ni *lpni, time64_t now)
779 deadline = lpni->lpni_last_alive +
780 lpni->lpni_net->net_tunables.lct_peer_timeout;
783 * assume peer_ni is alive as long as we're within the configured
792 /* NB: returns 1 when alive, 0 when dead, negative when error;
793 * may drop the lnet_net_lock */
795 lnet_peer_alive_locked(struct lnet_ni *ni, struct lnet_peer_ni *lpni,
796 struct lnet_msg *msg)
798 time64_t now = ktime_get_seconds();
800 if (!lnet_peer_aliveness_enabled(lpni))
804 * If we're resending a message, let's attempt to send it even if
805 * the peer is down to fulfill our resend quota on the message
807 if (msg->msg_retry_count > 0)
810 /* try and send recovery messages irregardless */
811 if (msg->msg_recovery)
814 /* always send any responses */
815 if (lnet_msg_is_response(msg))
818 if (!lnet_is_peer_deadline_passed(lpni, now))
821 return lnet_is_peer_ni_alive(lpni);
825 * \param msg The message to be sent.
826 * \param do_send True if lnet_ni_send() should be called in this function.
827 * lnet_send() is going to lnet_net_unlock immediately after this, so
828 * it sets do_send FALSE and I don't do the unlock/send/lock bit.
830 * \retval LNET_CREDIT_OK If \a msg sent or OK to send.
831 * \retval LNET_CREDIT_WAIT If \a msg blocked for credit.
832 * \retval -EHOSTUNREACH If the next hop of the message appears dead.
833 * \retval -ECANCELED If the MD of the message has been unlinked.
836 lnet_post_send_locked(struct lnet_msg *msg, int do_send)
838 struct lnet_peer_ni *lp = msg->msg_txpeer;
839 struct lnet_ni *ni = msg->msg_txni;
840 int cpt = msg->msg_tx_cpt;
841 struct lnet_tx_queue *tq = ni->ni_tx_queues[cpt];
843 /* non-lnet_send() callers have checked before */
844 LASSERT(!do_send || msg->msg_tx_delayed);
845 LASSERT(!msg->msg_receiving);
846 LASSERT(msg->msg_tx_committed);
848 /* can't get here if we're sending to the loopback interface */
849 if (the_lnet.ln_loni)
850 LASSERT(!nid_same(&lp->lpni_nid, &the_lnet.ln_loni->ni_nid));
852 /* NB 'lp' is always the next hop */
853 if ((msg->msg_target.pid & LNET_PID_USERFLAG) == 0 &&
854 lnet_peer_alive_locked(ni, lp, msg) == 0) {
855 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
856 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length +=
858 lnet_net_unlock(cpt);
860 lnet_incr_stats(&msg->msg_txpeer->lpni_stats,
862 LNET_STATS_TYPE_DROP);
864 lnet_incr_stats(&msg->msg_txni->ni_stats,
866 LNET_STATS_TYPE_DROP);
868 CNETERR("Dropping message for %s: peer not alive\n",
869 libcfs_idstr(&msg->msg_target));
870 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_DROPPED;
872 lnet_finalize(msg, -EHOSTUNREACH);
875 return -EHOSTUNREACH;
878 if (msg->msg_md != NULL &&
879 (msg->msg_md->md_flags & LNET_MD_FLAG_ABORTED) != 0) {
880 lnet_net_unlock(cpt);
882 CNETERR("Aborting message for %s: LNetM[DE]Unlink() already "
883 "called on the MD/ME.\n",
884 libcfs_idstr(&msg->msg_target));
886 msg->msg_no_resend = true;
887 CDEBUG(D_NET, "msg %p to %s canceled and will not be resent\n",
888 msg, libcfs_idstr(&msg->msg_target));
889 lnet_finalize(msg, -ECANCELED);
896 if (!msg->msg_peertxcredit) {
897 spin_lock(&lp->lpni_lock);
898 LASSERT((lp->lpni_txcredits < 0) ==
899 !list_empty(&lp->lpni_txq));
901 msg->msg_peertxcredit = 1;
902 lp->lpni_txqnob += msg->msg_len + sizeof(struct lnet_hdr_nid4);
903 lp->lpni_txcredits--;
905 if (lp->lpni_txcredits < lp->lpni_mintxcredits)
906 lp->lpni_mintxcredits = lp->lpni_txcredits;
908 if (lp->lpni_txcredits < 0) {
909 msg->msg_tx_delayed = 1;
910 list_add_tail(&msg->msg_list, &lp->lpni_txq);
911 spin_unlock(&lp->lpni_lock);
912 return LNET_CREDIT_WAIT;
914 spin_unlock(&lp->lpni_lock);
917 if (!msg->msg_txcredit) {
918 LASSERT((tq->tq_credits < 0) ==
919 !list_empty(&tq->tq_delayed));
921 msg->msg_txcredit = 1;
923 atomic_dec(&ni->ni_tx_credits);
925 if (tq->tq_credits < tq->tq_credits_min)
926 tq->tq_credits_min = tq->tq_credits;
928 if (tq->tq_credits < 0) {
929 msg->msg_tx_delayed = 1;
930 list_add_tail(&msg->msg_list, &tq->tq_delayed);
931 return LNET_CREDIT_WAIT;
935 if (unlikely(!list_empty(&the_lnet.ln_delay_rules)) &&
936 lnet_delay_rule_match_locked(&msg->msg_hdr, msg)) {
937 msg->msg_tx_delayed = 1;
938 return LNET_CREDIT_WAIT;
941 /* unset the tx_delay flag as we're going to send it now */
942 msg->msg_tx_delayed = 0;
945 lnet_net_unlock(cpt);
946 lnet_ni_send(ni, msg);
949 return LNET_CREDIT_OK;
953 static struct lnet_rtrbufpool *
954 lnet_msg2bufpool(struct lnet_msg *msg)
956 struct lnet_rtrbufpool *rbp;
959 LASSERT(msg->msg_rx_committed);
961 cpt = msg->msg_rx_cpt;
962 rbp = &the_lnet.ln_rtrpools[cpt][0];
964 LASSERT(msg->msg_len <= LNET_MTU);
965 while (msg->msg_len > (unsigned int)rbp->rbp_npages * PAGE_SIZE) {
967 LASSERT(rbp < &the_lnet.ln_rtrpools[cpt][LNET_NRBPOOLS]);
974 lnet_post_routed_recv_locked(struct lnet_msg *msg, int do_recv)
976 /* lnet_parse is going to lnet_net_unlock immediately after this, so it
977 * sets do_recv FALSE and I don't do the unlock/send/lock bit.
978 * I return LNET_CREDIT_WAIT if msg blocked and LNET_CREDIT_OK if
979 * received or OK to receive */
980 struct lnet_peer_ni *lpni = msg->msg_rxpeer;
981 struct lnet_peer *lp;
982 struct lnet_rtrbufpool *rbp;
983 struct lnet_rtrbuf *rb;
985 LASSERT(msg->msg_kiov == NULL);
986 LASSERT(msg->msg_niov == 0);
987 LASSERT(msg->msg_routing);
988 LASSERT(msg->msg_receiving);
989 LASSERT(!msg->msg_sending);
990 LASSERT(lpni->lpni_peer_net);
991 LASSERT(lpni->lpni_peer_net->lpn_peer);
993 lp = lpni->lpni_peer_net->lpn_peer;
995 /* non-lnet_parse callers only receive delayed messages */
996 LASSERT(!do_recv || msg->msg_rx_delayed);
998 if (!msg->msg_peerrtrcredit) {
999 /* lpni_lock protects the credit manipulation */
1000 spin_lock(&lpni->lpni_lock);
1002 msg->msg_peerrtrcredit = 1;
1003 lpni->lpni_rtrcredits--;
1004 if (lpni->lpni_rtrcredits < lpni->lpni_minrtrcredits)
1005 lpni->lpni_minrtrcredits = lpni->lpni_rtrcredits;
1007 if (lpni->lpni_rtrcredits < 0) {
1008 spin_unlock(&lpni->lpni_lock);
1009 /* must have checked eager_recv before here */
1010 LASSERT(msg->msg_rx_ready_delay);
1011 msg->msg_rx_delayed = 1;
1012 /* lp_lock protects the lp_rtrq */
1013 spin_lock(&lp->lp_lock);
1014 list_add_tail(&msg->msg_list, &lp->lp_rtrq);
1015 spin_unlock(&lp->lp_lock);
1016 return LNET_CREDIT_WAIT;
1018 spin_unlock(&lpni->lpni_lock);
1021 rbp = lnet_msg2bufpool(msg);
1023 if (!msg->msg_rtrcredit) {
1024 msg->msg_rtrcredit = 1;
1026 if (rbp->rbp_credits < rbp->rbp_mincredits)
1027 rbp->rbp_mincredits = rbp->rbp_credits;
1029 if (rbp->rbp_credits < 0) {
1030 /* must have checked eager_recv before here */
1031 LASSERT(msg->msg_rx_ready_delay);
1032 msg->msg_rx_delayed = 1;
1033 list_add_tail(&msg->msg_list, &rbp->rbp_msgs);
1034 return LNET_CREDIT_WAIT;
1038 LASSERT(!list_empty(&rbp->rbp_bufs));
1039 rb = list_entry(rbp->rbp_bufs.next, struct lnet_rtrbuf, rb_list);
1040 list_del(&rb->rb_list);
1042 msg->msg_niov = rbp->rbp_npages;
1043 msg->msg_kiov = &rb->rb_kiov[0];
1045 /* unset the msg-rx_delayed flag since we're receiving the message */
1046 msg->msg_rx_delayed = 0;
1049 int cpt = msg->msg_rx_cpt;
1051 lnet_net_unlock(cpt);
1052 lnet_ni_recv(msg->msg_rxni, msg->msg_private, msg, 1,
1053 0, msg->msg_len, msg->msg_len);
1056 return LNET_CREDIT_OK;
1060 lnet_return_tx_credits_locked(struct lnet_msg *msg)
1062 struct lnet_peer_ni *txpeer = msg->msg_txpeer;
1063 struct lnet_ni *txni = msg->msg_txni;
1064 struct lnet_msg *msg2;
1066 if (msg->msg_txcredit) {
1067 struct lnet_ni *ni = msg->msg_txni;
1068 struct lnet_tx_queue *tq = ni->ni_tx_queues[msg->msg_tx_cpt];
1070 /* give back NI txcredits */
1071 msg->msg_txcredit = 0;
1073 LASSERT((tq->tq_credits < 0) ==
1074 !list_empty(&tq->tq_delayed));
1077 atomic_inc(&ni->ni_tx_credits);
1078 if (tq->tq_credits <= 0) {
1079 msg2 = list_entry(tq->tq_delayed.next,
1080 struct lnet_msg, msg_list);
1081 list_del(&msg2->msg_list);
1083 LASSERT(msg2->msg_txni == ni);
1084 LASSERT(msg2->msg_tx_delayed);
1085 LASSERT(msg2->msg_tx_cpt == msg->msg_tx_cpt);
1087 (void) lnet_post_send_locked(msg2, 1);
1091 if (msg->msg_peertxcredit) {
1092 /* give back peer txcredits */
1093 msg->msg_peertxcredit = 0;
1095 spin_lock(&txpeer->lpni_lock);
1096 LASSERT((txpeer->lpni_txcredits < 0) ==
1097 !list_empty(&txpeer->lpni_txq));
1099 txpeer->lpni_txqnob -= msg->msg_len +
1100 sizeof(struct lnet_hdr_nid4);
1101 LASSERT(txpeer->lpni_txqnob >= 0);
1103 txpeer->lpni_txcredits++;
1104 if (txpeer->lpni_txcredits <= 0) {
1107 msg2 = list_entry(txpeer->lpni_txq.next,
1108 struct lnet_msg, msg_list);
1109 list_del(&msg2->msg_list);
1110 spin_unlock(&txpeer->lpni_lock);
1112 LASSERT(msg2->msg_txpeer == txpeer);
1113 LASSERT(msg2->msg_tx_delayed);
1115 msg2_cpt = msg2->msg_tx_cpt;
1118 * The msg_cpt can be different from the msg2_cpt
1119 * so we need to make sure we lock the correct cpt
1121 * Once we call lnet_post_send_locked() it is no
1122 * longer safe to access msg2, since it could've
1123 * been freed by lnet_finalize(), but we still
1124 * need to relock the correct cpt, so we cache the
1125 * msg2_cpt for the purpose of the check that
1126 * follows the call to lnet_pose_send_locked().
1128 if (msg2_cpt != msg->msg_tx_cpt) {
1129 lnet_net_unlock(msg->msg_tx_cpt);
1130 lnet_net_lock(msg2_cpt);
1132 (void) lnet_post_send_locked(msg2, 1);
1133 if (msg2_cpt != msg->msg_tx_cpt) {
1134 lnet_net_unlock(msg2_cpt);
1135 lnet_net_lock(msg->msg_tx_cpt);
1138 spin_unlock(&txpeer->lpni_lock);
1143 msg->msg_txni = NULL;
1144 lnet_ni_decref_locked(txni, msg->msg_tx_cpt);
1147 if (txpeer != NULL) {
1148 msg->msg_txpeer = NULL;
1149 lnet_peer_ni_decref_locked(txpeer);
1154 lnet_schedule_blocked_locked(struct lnet_rtrbufpool *rbp)
1156 struct lnet_msg *msg;
1158 if (list_empty(&rbp->rbp_msgs))
1160 msg = list_entry(rbp->rbp_msgs.next,
1161 struct lnet_msg, msg_list);
1162 list_del(&msg->msg_list);
1164 (void)lnet_post_routed_recv_locked(msg, 1);
1168 lnet_drop_routed_msgs_locked(struct list_head *list, int cpt)
1170 struct lnet_msg *msg;
1171 struct lnet_msg *tmp;
1173 lnet_net_unlock(cpt);
1175 list_for_each_entry_safe(msg, tmp, list, msg_list) {
1176 lnet_ni_recv(msg->msg_rxni, msg->msg_private, NULL,
1177 0, 0, 0, msg->msg_hdr.payload_length);
1178 list_del_init(&msg->msg_list);
1179 msg->msg_no_resend = true;
1180 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_ERROR;
1181 lnet_finalize(msg, -ECANCELED);
1188 lnet_return_rx_credits_locked(struct lnet_msg *msg)
1190 struct lnet_peer_ni *rxpeerni = msg->msg_rxpeer;
1191 struct lnet_peer *lp;
1192 struct lnet_ni *rxni = msg->msg_rxni;
1193 struct lnet_msg *msg2;
1195 if (msg->msg_rtrcredit) {
1196 /* give back global router credits */
1197 struct lnet_rtrbuf *rb;
1198 struct lnet_rtrbufpool *rbp;
1200 /* NB If a msg ever blocks for a buffer in rbp_msgs, it stays
1201 * there until it gets one allocated, or aborts the wait
1203 LASSERT(msg->msg_kiov != NULL);
1205 rb = list_entry(msg->msg_kiov, struct lnet_rtrbuf, rb_kiov[0]);
1208 msg->msg_kiov = NULL;
1209 msg->msg_rtrcredit = 0;
1211 LASSERT(rbp == lnet_msg2bufpool(msg));
1213 LASSERT((rbp->rbp_credits > 0) ==
1214 !list_empty(&rbp->rbp_bufs));
1216 /* If routing is now turned off, we just drop this buffer and
1217 * don't bother trying to return credits. */
1218 if (!the_lnet.ln_routing) {
1219 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1223 /* It is possible that a user has lowered the desired number of
1224 * buffers in this pool. Make sure we never put back
1225 * more buffers than the stated number. */
1226 if (unlikely(rbp->rbp_credits >= rbp->rbp_req_nbuffers)) {
1227 /* Discard this buffer so we don't have too
1229 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1230 rbp->rbp_nbuffers--;
1232 list_add(&rb->rb_list, &rbp->rbp_bufs);
1234 if (rbp->rbp_credits <= 0)
1235 lnet_schedule_blocked_locked(rbp);
1240 if (msg->msg_peerrtrcredit) {
1242 LASSERT(rxpeerni->lpni_peer_net);
1243 LASSERT(rxpeerni->lpni_peer_net->lpn_peer);
1245 /* give back peer router credits */
1246 msg->msg_peerrtrcredit = 0;
1248 spin_lock(&rxpeerni->lpni_lock);
1249 rxpeerni->lpni_rtrcredits++;
1250 spin_unlock(&rxpeerni->lpni_lock);
1252 lp = rxpeerni->lpni_peer_net->lpn_peer;
1253 spin_lock(&lp->lp_lock);
1255 /* drop all messages which are queued to be routed on that
1257 if (!the_lnet.ln_routing) {
1259 list_splice_init(&lp->lp_rtrq, &drop);
1260 spin_unlock(&lp->lp_lock);
1261 lnet_drop_routed_msgs_locked(&drop, msg->msg_rx_cpt);
1262 } else if (!list_empty(&lp->lp_rtrq)) {
1265 msg2 = list_entry(lp->lp_rtrq.next,
1266 struct lnet_msg, msg_list);
1267 list_del(&msg2->msg_list);
1268 msg2_cpt = msg2->msg_rx_cpt;
1269 spin_unlock(&lp->lp_lock);
1271 * messages on the lp_rtrq can be from any NID in
1272 * the peer, which means they might have different
1273 * cpts. We need to make sure we lock the right
1276 if (msg2_cpt != msg->msg_rx_cpt) {
1277 lnet_net_unlock(msg->msg_rx_cpt);
1278 lnet_net_lock(msg2_cpt);
1280 (void) lnet_post_routed_recv_locked(msg2, 1);
1281 if (msg2_cpt != msg->msg_rx_cpt) {
1282 lnet_net_unlock(msg2_cpt);
1283 lnet_net_lock(msg->msg_rx_cpt);
1286 spin_unlock(&lp->lp_lock);
1290 msg->msg_rxni = NULL;
1291 lnet_ni_decref_locked(rxni, msg->msg_rx_cpt);
1293 if (rxpeerni != NULL) {
1294 msg->msg_rxpeer = NULL;
1295 lnet_peer_ni_decref_locked(rxpeerni);
1299 static struct lnet_peer_ni *
1300 lnet_select_peer_ni(struct lnet_ni *best_ni, lnet_nid_t dst_nid,
1301 struct lnet_peer *peer,
1302 struct lnet_peer_ni *best_lpni,
1303 struct lnet_peer_net *peer_net)
1306 * Look at the peer NIs for the destination peer that connect
1307 * to the chosen net. If a peer_ni is preferred when using the
1308 * best_ni to communicate, we use that one. If there is no
1309 * preferred peer_ni, or there are multiple preferred peer_ni,
1310 * the available transmit credits are used. If the transmit
1311 * credits are equal, we round-robin over the peer_ni.
1313 struct lnet_peer_ni *lpni = NULL;
1314 int best_lpni_credits = (best_lpni) ? best_lpni->lpni_txcredits :
1316 int best_lpni_healthv = (best_lpni) ?
1317 atomic_read(&best_lpni->lpni_healthv) : 0;
1318 bool best_lpni_is_preferred = false;
1319 bool lpni_is_preferred;
1321 __u32 lpni_sel_prio;
1322 __u32 best_sel_prio = LNET_MAX_SELECTION_PRIORITY;
1324 while ((lpni = lnet_get_next_peer_ni_locked(peer, peer_net, lpni))) {
1326 * if the best_ni we've chosen aleady has this lpni
1327 * preferred, then let's use it
1330 lpni_is_preferred = lnet_peer_is_pref_nid_locked(
1331 lpni, &best_ni->ni_nid);
1332 CDEBUG(D_NET, "%s lpni_is_preferred = %d\n",
1333 libcfs_nidstr(&best_ni->ni_nid),
1336 lpni_is_preferred = false;
1339 lpni_healthv = atomic_read(&lpni->lpni_healthv);
1340 lpni_sel_prio = lpni->lpni_sel_priority;
1343 CDEBUG(D_NET, "n:[%s, %s] h:[%d, %d] p:[%d, %d] c:[%d, %d] s:[%d, %d]\n",
1344 libcfs_nidstr(&lpni->lpni_nid),
1345 libcfs_nidstr(&best_lpni->lpni_nid),
1346 lpni_healthv, best_lpni_healthv,
1347 lpni_sel_prio, best_sel_prio,
1348 lpni->lpni_txcredits, best_lpni_credits,
1349 lpni->lpni_seq, best_lpni->lpni_seq);
1353 /* pick the healthiest peer ni */
1354 if (lpni_healthv < best_lpni_healthv)
1356 else if (lpni_healthv > best_lpni_healthv) {
1357 if (best_lpni_is_preferred)
1358 best_lpni_is_preferred = false;
1362 if (lpni_sel_prio > best_sel_prio)
1364 else if (lpni_sel_prio < best_sel_prio) {
1365 if (best_lpni_is_preferred)
1366 best_lpni_is_preferred = false;
1370 /* if this is a preferred peer use it */
1371 if (!best_lpni_is_preferred && lpni_is_preferred) {
1372 best_lpni_is_preferred = true;
1374 } else if (best_lpni_is_preferred && !lpni_is_preferred) {
1375 /* this is not the preferred peer so let's ignore
1381 if (lpni->lpni_txcredits < best_lpni_credits)
1382 /* We already have a peer that has more credits
1383 * available than this one. No need to consider
1384 * this peer further.
1387 else if (lpni->lpni_txcredits > best_lpni_credits)
1390 /* The best peer found so far and the current peer
1391 * have the same number of available credits let's
1392 * make sure to select between them using Round Robin
1394 if (best_lpni && (best_lpni->lpni_seq <= lpni->lpni_seq))
1397 best_lpni_is_preferred = lpni_is_preferred;
1398 best_lpni_healthv = lpni_healthv;
1399 best_sel_prio = lpni_sel_prio;
1401 best_lpni_credits = lpni->lpni_txcredits;
1404 /* if we still can't find a peer ni then we can't reach it */
1406 __u32 net_id = (peer_net) ? peer_net->lpn_net_id :
1407 LNET_NIDNET(dst_nid);
1408 CDEBUG(D_NET, "no peer_ni found on peer net %s\n",
1409 libcfs_net2str(net_id));
1413 CDEBUG(D_NET, "sd_best_lpni = %s\n",
1414 libcfs_nidstr(&best_lpni->lpni_nid));
1420 * Prerequisite: the best_ni should already be set in the sd
1421 * Find the best lpni.
1422 * If the net id is provided then restrict lpni selection on
1423 * that particular net.
1424 * Otherwise find any reachable lpni. When dealing with an MR
1425 * gateway and it has multiple lpnis which we can use
1426 * we want to select the best one from the list of reachable
1429 static inline struct lnet_peer_ni *
1430 lnet_find_best_lpni(struct lnet_ni *lni, lnet_nid_t dst_nid,
1431 struct lnet_peer *peer, __u32 net_id)
1433 struct lnet_peer_net *peer_net;
1435 /* find the best_lpni on any local network */
1436 if (net_id == LNET_NET_ANY) {
1437 struct lnet_peer_ni *best_lpni = NULL;
1438 struct lnet_peer_net *lpn;
1439 list_for_each_entry(lpn, &peer->lp_peer_nets, lpn_peer_nets) {
1440 /* no net specified find any reachable peer ni */
1441 if (!lnet_islocalnet_locked(lpn->lpn_net_id))
1443 best_lpni = lnet_select_peer_ni(lni, dst_nid, peer,
1449 /* restrict on the specified net */
1450 peer_net = lnet_peer_get_net_locked(peer, net_id);
1452 return lnet_select_peer_ni(lni, dst_nid, peer, NULL, peer_net);
1458 lnet_compare_gw_lpnis(struct lnet_peer_ni *lpni1, struct lnet_peer_ni *lpni2)
1460 if (lpni1->lpni_txqnob < lpni2->lpni_txqnob)
1463 if (lpni1->lpni_txqnob > lpni2->lpni_txqnob)
1466 if (lpni1->lpni_txcredits > lpni2->lpni_txcredits)
1469 if (lpni1->lpni_txcredits < lpni2->lpni_txcredits)
1475 /* Compare route priorities and hop counts */
1477 lnet_compare_routes(struct lnet_route *r1, struct lnet_route *r2)
1479 int r1_hops = (r1->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r1->lr_hops;
1480 int r2_hops = (r2->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r2->lr_hops;
1482 if (r1->lr_priority < r2->lr_priority)
1485 if (r1->lr_priority > r2->lr_priority)
1488 if (r1_hops < r2_hops)
1491 if (r1_hops > r2_hops)
1497 static struct lnet_route *
1498 lnet_find_route_locked(struct lnet_remotenet *rnet, __u32 src_net,
1499 struct lnet_peer_ni *remote_lpni,
1500 struct lnet_route **prev_route,
1501 struct lnet_peer_ni **gwni)
1503 struct lnet_peer_ni *lpni, *best_gw_ni = NULL;
1504 struct lnet_route *best_route;
1505 struct lnet_route *last_route;
1506 struct lnet_route *route;
1508 bool best_rte_is_preferred = false;
1509 struct lnet_nid *gw_pnid;
1511 CDEBUG(D_NET, "Looking up a route to %s, from %s\n",
1512 libcfs_net2str(rnet->lrn_net), libcfs_net2str(src_net));
1514 best_route = last_route = NULL;
1515 list_for_each_entry(route, &rnet->lrn_routes, lr_list) {
1516 if (!lnet_is_route_alive(route))
1518 gw_pnid = &route->lr_gateway->lp_primary_nid;
1520 /* no protection on below fields, but it's harmless */
1521 if (last_route && (last_route->lr_seq - route->lr_seq < 0))
1524 /* if the best route found is in the preferred list then
1525 * tag it as preferred and use it later on. But if we
1526 * didn't find any routes which are on the preferred list
1527 * then just use the best route possible.
1529 rc = lnet_peer_is_pref_rtr_locked(remote_lpni, gw_pnid);
1531 if (!best_route || (rc && !best_rte_is_preferred)) {
1532 /* Restrict the selection of the router NI on the
1533 * src_net provided. If the src_net is LNET_NID_ANY,
1534 * then select the best interface available.
1536 lpni = lnet_find_best_lpni(NULL, LNET_NID_ANY,
1541 "Gateway %s does not have a peer NI on net %s\n",
1542 libcfs_nidstr(gw_pnid),
1543 libcfs_net2str(src_net));
1548 if (rc && !best_rte_is_preferred) {
1549 /* This is the first preferred route we found,
1550 * so it beats any route found previously
1556 best_rte_is_preferred = true;
1557 CDEBUG(D_NET, "preferred gw = %s\n",
1558 libcfs_nidstr(gw_pnid));
1560 } else if ((!rc) && best_rte_is_preferred)
1561 /* The best route we found so far is in the preferred
1562 * list, so it beats any non-preferred route
1567 best_route = last_route = route;
1572 rc = lnet_compare_routes(route, best_route);
1576 /* Restrict the selection of the router NI on the
1577 * src_net provided. If the src_net is LNET_NID_ANY,
1578 * then select the best interface available.
1580 lpni = lnet_find_best_lpni(NULL, LNET_NID_ANY,
1585 "Gateway %s does not have a peer NI on net %s\n",
1586 libcfs_nidstr(gw_pnid),
1587 libcfs_net2str(src_net));
1597 rc = lnet_compare_gw_lpnis(lpni, best_gw_ni);
1601 if (rc == 1 || route->lr_seq <= best_route->lr_seq) {
1608 *prev_route = last_route;
1614 static inline unsigned int
1615 lnet_dev_prio_of_md(struct lnet_ni *ni, unsigned int dev_idx)
1617 if (dev_idx == UINT_MAX)
1620 if (!ni || !ni->ni_net || !ni->ni_net->net_lnd ||
1621 !ni->ni_net->net_lnd->lnd_get_dev_prio)
1624 return ni->ni_net->net_lnd->lnd_get_dev_prio(ni, dev_idx);
1627 static struct lnet_ni *
1628 lnet_get_best_ni(struct lnet_net *local_net, struct lnet_ni *best_ni,
1629 struct lnet_peer *peer, struct lnet_peer_net *peer_net,
1630 struct lnet_msg *msg, int md_cpt)
1632 struct lnet_libmd *md = msg->msg_md;
1633 unsigned int offset = msg->msg_offset;
1634 unsigned int shortest_distance;
1635 struct lnet_ni *ni = NULL;
1638 __u32 best_sel_prio;
1639 unsigned int best_dev_prio;
1640 unsigned int dev_idx = UINT_MAX;
1641 struct page *page = lnet_get_first_page(md, offset);
1642 msg->msg_rdma_force = lnet_is_rdma_only_page(page);
1644 if (msg->msg_rdma_force)
1645 dev_idx = lnet_get_dev_idx(page);
1648 * If there is no peer_ni that we can send to on this network,
1649 * then there is no point in looking for a new best_ni here.
1651 if (!lnet_get_next_peer_ni_locked(peer, peer_net, NULL))
1654 if (best_ni == NULL) {
1655 best_sel_prio = LNET_MAX_SELECTION_PRIORITY;
1656 shortest_distance = UINT_MAX;
1657 best_dev_prio = UINT_MAX;
1658 best_credits = INT_MIN;
1661 best_dev_prio = lnet_dev_prio_of_md(best_ni, dev_idx);
1662 shortest_distance = cfs_cpt_distance(lnet_cpt_table(), md_cpt,
1663 best_ni->ni_dev_cpt);
1664 best_credits = atomic_read(&best_ni->ni_tx_credits);
1665 best_healthv = atomic_read(&best_ni->ni_healthv);
1666 best_sel_prio = best_ni->ni_sel_priority;
1669 while ((ni = lnet_get_next_ni_locked(local_net, ni))) {
1670 unsigned int distance;
1675 unsigned int ni_dev_prio;
1677 ni_credits = atomic_read(&ni->ni_tx_credits);
1678 ni_healthv = atomic_read(&ni->ni_healthv);
1679 ni_fatal = atomic_read(&ni->ni_fatal_error_on);
1680 ni_sel_prio = ni->ni_sel_priority;
1683 * calculate the distance from the CPT on which
1684 * the message memory is allocated to the CPT of
1685 * the NI's physical device
1687 distance = cfs_cpt_distance(lnet_cpt_table(),
1691 ni_dev_prio = lnet_dev_prio_of_md(ni, dev_idx);
1694 * All distances smaller than the NUMA range
1695 * are treated equally.
1697 if (distance < lnet_numa_range)
1698 distance = lnet_numa_range;
1701 * Select on health, selection policy, direct dma prio,
1702 * shorter distance, available credits, then round-robin.
1708 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",
1709 libcfs_nidstr(&ni->ni_nid), ni_credits, distance,
1710 ni->ni_seq, ni_sel_prio, ni_dev_prio,
1711 (best_ni) ? libcfs_nidstr(&best_ni->ni_nid)
1712 : "not selected", best_credits, shortest_distance,
1713 (best_ni) ? best_ni->ni_seq : 0,
1714 best_sel_prio, best_dev_prio);
1718 if (ni_healthv < best_healthv)
1720 else if (ni_healthv > best_healthv)
1723 if (ni_sel_prio > best_sel_prio)
1725 else if (ni_sel_prio < best_sel_prio)
1728 if (ni_dev_prio > best_dev_prio)
1730 else if (ni_dev_prio < best_dev_prio)
1733 if (distance > shortest_distance)
1735 else if (distance < shortest_distance)
1738 if (ni_credits < best_credits)
1740 else if (ni_credits > best_credits)
1743 if (best_ni && best_ni->ni_seq <= ni->ni_seq)
1747 best_sel_prio = ni_sel_prio;
1748 best_dev_prio = ni_dev_prio;
1749 shortest_distance = distance;
1750 best_healthv = ni_healthv;
1752 best_credits = ni_credits;
1755 CDEBUG(D_NET, "selected best_ni %s\n",
1756 (best_ni) ? libcfs_nidstr(&best_ni->ni_nid) : "no selection");
1762 * Traffic to the LNET_RESERVED_PORTAL may not trigger peer discovery,
1763 * because such traffic is required to perform discovery. We therefore
1764 * exclude all GET and PUT on that portal. We also exclude all ACK and
1765 * REPLY traffic, but that is because the portal is not tracked in the
1766 * message structure for these message types. We could restrict this
1767 * further by also checking for LNET_PROTO_PING_MATCHBITS.
1770 lnet_msg_discovery(struct lnet_msg *msg)
1772 if (msg->msg_type == LNET_MSG_PUT) {
1773 if (msg->msg_hdr.msg.put.ptl_index != LNET_RESERVED_PORTAL)
1775 } else if (msg->msg_type == LNET_MSG_GET) {
1776 if (msg->msg_hdr.msg.get.ptl_index != LNET_RESERVED_PORTAL)
1782 #define SRC_SPEC 0x0001
1783 #define SRC_ANY 0x0002
1784 #define LOCAL_DST 0x0004
1785 #define REMOTE_DST 0x0008
1786 #define MR_DST 0x0010
1787 #define NMR_DST 0x0020
1788 #define SND_RESP 0x0040
1790 /* The following to defines are used for return codes */
1791 #define REPEAT_SEND 0x1000
1792 #define PASS_THROUGH 0x2000
1794 /* The different cases lnet_select pathway needs to handle */
1795 #define SRC_SPEC_LOCAL_MR_DST (SRC_SPEC | LOCAL_DST | MR_DST)
1796 #define SRC_SPEC_ROUTER_MR_DST (SRC_SPEC | REMOTE_DST | MR_DST)
1797 #define SRC_SPEC_LOCAL_NMR_DST (SRC_SPEC | LOCAL_DST | NMR_DST)
1798 #define SRC_SPEC_ROUTER_NMR_DST (SRC_SPEC | REMOTE_DST | NMR_DST)
1799 #define SRC_ANY_LOCAL_MR_DST (SRC_ANY | LOCAL_DST | MR_DST)
1800 #define SRC_ANY_ROUTER_MR_DST (SRC_ANY | REMOTE_DST | MR_DST)
1801 #define SRC_ANY_LOCAL_NMR_DST (SRC_ANY | LOCAL_DST | NMR_DST)
1802 #define SRC_ANY_ROUTER_NMR_DST (SRC_ANY | REMOTE_DST | NMR_DST)
1805 lnet_handle_lo_send(struct lnet_send_data *sd)
1807 struct lnet_msg *msg = sd->sd_msg;
1808 int cpt = sd->sd_cpt;
1810 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1813 /* No send credit hassles with LOLND */
1814 lnet_ni_addref_locked(the_lnet.ln_loni, cpt);
1815 msg->msg_hdr.dest_nid = the_lnet.ln_loni->ni_nid;
1816 if (!msg->msg_routing)
1817 msg->msg_hdr.src_nid = the_lnet.ln_loni->ni_nid;
1818 msg->msg_target.nid = the_lnet.ln_loni->ni_nid;
1819 lnet_msg_commit(msg, cpt);
1820 msg->msg_txni = the_lnet.ln_loni;
1822 return LNET_CREDIT_OK;
1826 lnet_handle_send(struct lnet_send_data *sd)
1828 struct lnet_ni *best_ni = sd->sd_best_ni;
1829 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
1830 struct lnet_peer_ni *final_dst_lpni = sd->sd_final_dst_lpni;
1831 struct lnet_msg *msg = sd->sd_msg;
1833 __u32 send_case = sd->sd_send_case;
1835 __u32 routing = send_case & REMOTE_DST;
1836 struct lnet_rsp_tracker *rspt;
1838 /* Increment sequence number of the selected peer, peer net,
1839 * local ni and local net so that we pick the next ones
1842 best_lpni->lpni_peer_net->lpn_seq++;
1843 best_lpni->lpni_seq = best_lpni->lpni_peer_net->lpn_seq;
1844 best_ni->ni_net->net_seq++;
1845 best_ni->ni_seq = best_ni->ni_net->net_seq;
1847 CDEBUG(D_NET, "%s NI seq info: [%d:%d:%d:%u] %s LPNI seq info [%d:%d:%d:%u]\n",
1848 libcfs_nidstr(&best_ni->ni_nid),
1849 best_ni->ni_seq, best_ni->ni_net->net_seq,
1850 atomic_read(&best_ni->ni_tx_credits),
1851 best_ni->ni_sel_priority,
1852 libcfs_nidstr(&best_lpni->lpni_nid),
1853 best_lpni->lpni_seq, best_lpni->lpni_peer_net->lpn_seq,
1854 best_lpni->lpni_txcredits,
1855 best_lpni->lpni_sel_priority);
1858 * grab a reference on the peer_ni so it sticks around even if
1859 * we need to drop and relock the lnet_net_lock below.
1861 lnet_peer_ni_addref_locked(best_lpni);
1864 * Use lnet_cpt_of_nid() to determine the CPT used to commit the
1865 * message. This ensures that we get a CPT that is correct for
1866 * the NI when the NI has been restricted to a subset of all CPTs.
1867 * If the selected CPT differs from the one currently locked, we
1868 * must unlock and relock the lnet_net_lock(), and then check whether
1869 * the configuration has changed. We don't have a hold on the best_ni
1870 * yet, and it may have vanished.
1872 cpt2 = lnet_cpt_of_nid_locked(&best_lpni->lpni_nid, best_ni);
1873 if (sd->sd_cpt != cpt2) {
1874 __u32 seq = lnet_get_dlc_seq_locked();
1875 lnet_net_unlock(sd->sd_cpt);
1877 lnet_net_lock(sd->sd_cpt);
1878 if (seq != lnet_get_dlc_seq_locked()) {
1879 lnet_peer_ni_decref_locked(best_lpni);
1885 * store the best_lpni in the message right away to avoid having
1886 * to do the same operation under different conditions
1888 msg->msg_txpeer = best_lpni;
1889 msg->msg_txni = best_ni;
1892 * grab a reference for the best_ni since now it's in use in this
1893 * send. The reference will be dropped in lnet_finalize()
1895 lnet_ni_addref_locked(msg->msg_txni, sd->sd_cpt);
1898 * Always set the target.nid to the best peer picked. Either the
1899 * NID will be one of the peer NIDs selected, or the same NID as
1900 * what was originally set in the target or it will be the NID of
1901 * a router if this message should be routed
1903 msg->msg_target.nid = msg->msg_txpeer->lpni_nid;
1906 * lnet_msg_commit assigns the correct cpt to the message, which
1907 * is used to decrement the correct refcount on the ni when it's
1908 * time to return the credits
1910 lnet_msg_commit(msg, sd->sd_cpt);
1913 * If we are routing the message then we keep the src_nid that was
1914 * set by the originator. If we are not routing then we are the
1915 * originator and set it here.
1917 if (!msg->msg_routing)
1918 msg->msg_hdr.src_nid = msg->msg_txni->ni_nid;
1921 msg->msg_target_is_router = 1;
1922 msg->msg_target.pid = LNET_PID_LUSTRE;
1924 * since we're routing we want to ensure that the
1925 * msg_hdr.dest_nid is set to the final destination. When
1926 * the router receives this message it knows how to route
1929 * final_dst_lpni is set at the beginning of the
1930 * lnet_select_pathway() function and is never changed.
1931 * It's safe to use it here.
1933 msg->msg_hdr.dest_nid = final_dst_lpni->lpni_nid;
1936 * if we're not routing set the dest_nid to the best peer
1937 * ni NID that we picked earlier in the algorithm.
1939 msg->msg_hdr.dest_nid = msg->msg_txpeer->lpni_nid;
1943 * if we have response tracker block update it with the next hop
1947 rspt = msg->msg_md->md_rspt_ptr;
1949 rspt->rspt_next_hop_nid =
1950 msg->msg_txpeer->lpni_nid;
1951 CDEBUG(D_NET, "rspt_next_hop_nid = %s\n",
1952 libcfs_nidstr(&rspt->rspt_next_hop_nid));
1956 rc = lnet_post_send_locked(msg, 0);
1959 CDEBUG(D_NET, "TRACE: %s(%s:%s) -> %s(%s:%s) %s : %s try# %d\n",
1960 libcfs_nidstr(&msg->msg_hdr.src_nid),
1961 libcfs_nidstr(&msg->msg_txni->ni_nid),
1962 libcfs_nidstr(&sd->sd_src_nid),
1963 libcfs_nidstr(&msg->msg_hdr.dest_nid),
1964 libcfs_nidstr(&sd->sd_dst_nid),
1965 libcfs_nidstr(&msg->msg_txpeer->lpni_nid),
1966 libcfs_nidstr(&sd->sd_rtr_nid),
1967 lnet_msgtyp2str(msg->msg_type), msg->msg_retry_count);
1973 lnet_set_non_mr_pref_nid(struct lnet_peer_ni *lpni, struct lnet_ni *lni,
1974 struct lnet_msg *msg)
1976 if (!lnet_peer_is_multi_rail(lpni->lpni_peer_net->lpn_peer) &&
1977 !lnet_msg_is_response(msg) && lpni->lpni_pref_nnids == 0) {
1978 CDEBUG(D_NET, "Setting preferred local NID %s on NMR peer %s\n",
1979 libcfs_nidstr(&lni->ni_nid),
1980 libcfs_nidstr(&lpni->lpni_nid));
1981 lnet_peer_ni_set_non_mr_pref_nid(lpni, &lni->ni_nid);
1990 * use the source and destination NIDs as the pathway
1993 lnet_handle_spec_local_nmr_dst(struct lnet_send_data *sd)
1995 /* the destination lpni is set before we get here. */
1998 sd->sd_best_ni = lnet_nid_to_ni_locked(&sd->sd_src_nid, sd->sd_cpt);
1999 if (!sd->sd_best_ni) {
2000 CERROR("Can't send to %s: src %s is not a local nid\n",
2001 libcfs_nidstr(&sd->sd_dst_nid),
2002 libcfs_nidstr(&sd->sd_src_nid));
2006 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
2008 return lnet_handle_send(sd);
2016 * Don't run the selection algorithm on the peer NIs. By specifying the
2017 * local NID, we're also saying that we should always use the destination NID
2018 * provided. This handles the case where we should be using the same
2019 * destination NID for the all the messages which belong to the same RPC
2023 lnet_handle_spec_local_mr_dst(struct lnet_send_data *sd)
2025 sd->sd_best_ni = lnet_nid_to_ni_locked(&sd->sd_src_nid, sd->sd_cpt);
2026 if (!sd->sd_best_ni) {
2027 CERROR("Can't send to %s: src %s is not a local nid\n",
2028 libcfs_nidstr(&sd->sd_dst_nid),
2029 libcfs_nidstr(&sd->sd_src_nid));
2033 if (sd->sd_best_lpni &&
2034 nid_same(&sd->sd_best_lpni->lpni_nid,
2035 &the_lnet.ln_loni->ni_nid))
2036 return lnet_handle_lo_send(sd);
2037 else if (sd->sd_best_lpni)
2038 return lnet_handle_send(sd);
2040 CERROR("can't send to %s. no NI on %s\n",
2041 libcfs_nidstr(&sd->sd_dst_nid),
2042 libcfs_net2str(sd->sd_best_ni->ni_net->net_id));
2044 return -EHOSTUNREACH;
2048 lnet_find_best_ni_on_spec_net(struct lnet_ni *cur_best_ni,
2049 struct lnet_peer *peer,
2050 struct lnet_peer_net *peer_net,
2051 struct lnet_msg *msg,
2054 struct lnet_net *local_net;
2055 struct lnet_ni *best_ni;
2057 local_net = lnet_get_net_locked(peer_net->lpn_net_id);
2062 * Iterate through the NIs in this local Net and select
2063 * the NI to send from. The selection is determined by
2064 * these 3 criterion in the following priority:
2066 * 2. NI available credits
2069 best_ni = lnet_get_best_ni(local_net, cur_best_ni,
2070 peer, peer_net, msg, cpt);
2076 lnet_initiate_peer_discovery(struct lnet_peer_ni *lpni, struct lnet_msg *msg,
2079 struct lnet_peer *peer;
2080 struct lnet_peer_ni *new_lpni;
2083 lnet_peer_ni_addref_locked(lpni);
2085 peer = lpni->lpni_peer_net->lpn_peer;
2087 if (lnet_peer_gw_discovery(peer)) {
2088 lnet_peer_ni_decref_locked(lpni);
2092 if (!lnet_msg_discovery(msg) || lnet_peer_is_uptodate(peer)) {
2093 lnet_peer_ni_decref_locked(lpni);
2097 rc = lnet_discover_peer_locked(lpni, cpt, false);
2099 lnet_peer_ni_decref_locked(lpni);
2103 new_lpni = lnet_find_peer_ni_locked(lnet_nid_to_nid4(&lpni->lpni_nid));
2105 lnet_peer_ni_decref_locked(lpni);
2109 peer = new_lpni->lpni_peer_net->lpn_peer;
2110 spin_lock(&peer->lp_lock);
2111 if (lpni == new_lpni && lnet_peer_is_uptodate_locked(peer)) {
2112 /* The peer NI did not change and the peer is up to date.
2113 * Nothing more to do.
2115 spin_unlock(&peer->lp_lock);
2116 lnet_peer_ni_decref_locked(lpni);
2117 lnet_peer_ni_decref_locked(new_lpni);
2120 spin_unlock(&peer->lp_lock);
2122 /* Either the peer NI changed during discovery, or the peer isn't up
2123 * to date. In both cases we want to queue the message on the
2124 * (possibly new) peer's pending queue and queue the peer for discovery
2126 msg->msg_sending = 0;
2127 msg->msg_txpeer = NULL;
2128 lnet_net_unlock(cpt);
2129 lnet_peer_queue_message(peer, msg);
2132 lnet_peer_ni_decref_locked(lpni);
2133 lnet_peer_ni_decref_locked(new_lpni);
2135 CDEBUG(D_NET, "msg %p delayed. %s pending discovery\n",
2136 msg, libcfs_nidstr(&peer->lp_primary_nid));
2138 return LNET_DC_WAIT;
2142 lnet_handle_find_routed_path(struct lnet_send_data *sd,
2143 struct lnet_nid *dst_nid,
2144 struct lnet_peer_ni **gw_lpni,
2145 struct lnet_peer **gw_peer)
2148 struct lnet_peer *gw;
2149 struct lnet_peer *lp;
2150 struct lnet_peer_net *lpn;
2151 struct lnet_peer_net *best_lpn = NULL;
2152 struct lnet_remotenet *rnet, *best_rnet = NULL;
2153 struct lnet_route *best_route = NULL;
2154 struct lnet_route *last_route = NULL;
2155 struct lnet_peer_ni *lpni = NULL;
2156 struct lnet_peer_ni *gwni = NULL;
2157 bool route_found = false;
2158 struct lnet_nid *src_nid =
2159 !LNET_NID_IS_ANY(&sd->sd_src_nid) || !sd->sd_best_ni
2161 : &sd->sd_best_ni->ni_nid;
2162 int best_lpn_healthv = 0;
2163 __u32 best_lpn_sel_prio = LNET_MAX_SELECTION_PRIORITY;
2165 CDEBUG(D_NET, "using src nid %s for route restriction\n",
2166 src_nid ? libcfs_nidstr(src_nid) : "ANY");
2168 /* If a router nid was specified then we are replying to a GET or
2169 * sending an ACK. In this case we use the gateway associated with the
2170 * specified router nid.
2172 if (!LNET_NID_IS_ANY(&sd->sd_rtr_nid)) {
2173 gwni = lnet_peer_ni_find_locked(&sd->sd_rtr_nid);
2175 gw = gwni->lpni_peer_net->lpn_peer;
2176 lnet_peer_ni_decref_locked(gwni);
2177 if (gw->lp_rtr_refcount)
2180 CWARN("No peer NI for gateway %s. Attempting to find an alternative route.\n",
2181 libcfs_nidstr(&sd->sd_rtr_nid));
2186 if (sd->sd_msg->msg_routing || (src_nid && !LNET_NID_IS_ANY(src_nid))) {
2187 /* If I'm routing this message then I need to find the
2188 * next hop based on the destination NID
2190 * We also find next hop based on the destination NID
2191 * if the source NI was specified
2193 best_rnet = lnet_find_rnet_locked(LNET_NID_NET(&sd->sd_dst_nid));
2195 CERROR("Unable to send message from %s to %s - Route table may be misconfigured\n",
2196 (src_nid && LNET_NID_IS_ANY(src_nid)) ?
2198 libcfs_nidstr(src_nid),
2199 libcfs_nidstr(&sd->sd_dst_nid));
2200 return -EHOSTUNREACH;
2203 /* we've already looked up the initial lpni using
2206 lpni = sd->sd_best_lpni;
2207 /* the peer tree must be in existence */
2208 LASSERT(lpni && lpni->lpni_peer_net &&
2209 lpni->lpni_peer_net->lpn_peer);
2210 lp = lpni->lpni_peer_net->lpn_peer;
2212 list_for_each_entry(lpn, &lp->lp_peer_nets, lpn_peer_nets) {
2213 /* is this remote network reachable? */
2214 rnet = lnet_find_rnet_locked(lpn->lpn_net_id);
2223 /* select the preferred peer net */
2224 if (best_lpn_healthv > lpn->lpn_healthv)
2226 else if (best_lpn_healthv < lpn->lpn_healthv)
2229 if (best_lpn_sel_prio < lpn->lpn_sel_priority)
2231 else if (best_lpn_sel_prio > lpn->lpn_sel_priority)
2234 if (best_lpn->lpn_seq <= lpn->lpn_seq)
2237 best_lpn_healthv = lpn->lpn_healthv;
2238 best_lpn_sel_prio = lpn->lpn_sel_priority;
2244 CERROR("peer %s has no available nets\n",
2245 libcfs_nidstr(&sd->sd_dst_nid));
2246 return -EHOSTUNREACH;
2249 sd->sd_best_lpni = lnet_find_best_lpni(sd->sd_best_ni,
2250 lnet_nid_to_nid4(&sd->sd_dst_nid),
2252 best_lpn->lpn_net_id);
2253 if (!sd->sd_best_lpni) {
2254 CERROR("peer %s is unreachable\n",
2255 libcfs_nidstr(&sd->sd_dst_nid));
2256 return -EHOSTUNREACH;
2259 /* We're attempting to round robin over the remote peer
2260 * NI's so update the final destination we selected
2262 sd->sd_final_dst_lpni = sd->sd_best_lpni;
2264 /* Increment the sequence number of the remote lpni so
2265 * we can round robin over the different interfaces of
2268 sd->sd_best_lpni->lpni_seq++;
2272 * find the best route. Restrict the selection on the net of the
2273 * local NI if we've already picked the local NI to send from.
2274 * Otherwise, let's pick any route we can find and then find
2275 * a local NI we can reach the route's gateway on. Any route we
2276 * select will be reachable by virtue of the restriction we have
2277 * when adding a route.
2279 best_route = lnet_find_route_locked(best_rnet,
2280 LNET_NID_NET(src_nid),
2282 &last_route, &gwni);
2285 CERROR("no route to %s from %s\n",
2286 libcfs_nidstr(dst_nid),
2287 libcfs_nidstr(src_nid));
2288 return -EHOSTUNREACH;
2292 CERROR("Internal Error. Route expected to %s from %s\n",
2293 libcfs_nidstr(dst_nid),
2294 libcfs_nidstr(src_nid));
2298 gw = best_route->lr_gateway;
2299 LASSERT(gw == gwni->lpni_peer_net->lpn_peer);
2303 * If the router checker is not active then discover the gateway here.
2304 * This ensures we are able to take advantage of multi-rail routing, but
2305 * if the router checker is active then we do not unecessarily delay
2306 * messages while the gateway is being checked by the dedicated monitor
2309 * NB: We're only checking the alive_router_check_interval here, rather
2310 * than calling lnet_router_checker_active(), because the other
2311 * conditions that are checked by that function are either
2312 * irrelevant (the_lnet.ln_routing) or must be true (list of routers
2315 if (alive_router_check_interval <= 0) {
2316 rc = lnet_initiate_peer_discovery(gwni, sd->sd_msg, sd->sd_cpt);
2321 if (!sd->sd_best_ni) {
2322 lpn = gwni->lpni_peer_net;
2323 sd->sd_best_ni = lnet_find_best_ni_on_spec_net(NULL, gw, lpn,
2326 if (!sd->sd_best_ni) {
2327 CERROR("Internal Error. Expected local ni on %s but non found: %s\n",
2328 libcfs_net2str(lpn->lpn_net_id),
2329 libcfs_nidstr(&sd->sd_src_nid));
2338 * increment the sequence numbers since now we're sure we're
2339 * going to use this path
2341 if (LNET_NID_IS_ANY(&sd->sd_rtr_nid)) {
2342 LASSERT(best_route && last_route);
2343 best_route->lr_seq = last_route->lr_seq + 1;
2345 best_lpn->lpn_seq++;
2356 * Remote destination
2357 * Non-MR destination
2361 * Remote destination
2364 * The handling of these two cases is similar. Even though the destination
2365 * can be MR or non-MR, we'll deal directly with the router.
2368 lnet_handle_spec_router_dst(struct lnet_send_data *sd)
2371 struct lnet_peer_ni *gw_lpni = NULL;
2372 struct lnet_peer *gw_peer = NULL;
2375 sd->sd_best_ni = lnet_nid_to_ni_locked(&sd->sd_src_nid, sd->sd_cpt);
2376 if (!sd->sd_best_ni) {
2377 CERROR("Can't send to %s: src %s is not a local nid\n",
2378 libcfs_nidstr(&sd->sd_dst_nid),
2379 libcfs_nidstr(&sd->sd_src_nid));
2383 rc = lnet_handle_find_routed_path(sd, &sd->sd_dst_nid,
2384 &gw_lpni, &gw_peer);
2388 if (sd->sd_send_case & NMR_DST)
2390 * since the final destination is non-MR let's set its preferred
2391 * NID before we send
2393 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni,
2397 * We're going to send to the gw found so let's set its
2400 sd->sd_peer = gw_peer;
2401 sd->sd_best_lpni = gw_lpni;
2403 return lnet_handle_send(sd);
2407 lnet_find_best_ni_on_local_net(struct lnet_peer *peer, int md_cpt,
2408 struct lnet_msg *msg, bool discovery)
2410 struct lnet_peer_net *lpn = NULL;
2411 struct lnet_peer_net *best_lpn = NULL;
2412 struct lnet_net *net = NULL;
2413 struct lnet_net *best_net = NULL;
2414 struct lnet_ni *best_ni = NULL;
2415 int best_lpn_healthv = 0;
2416 int best_net_healthv = 0;
2418 __u32 best_lpn_sel_prio = LNET_MAX_SELECTION_PRIORITY;
2420 __u32 best_net_sel_prio = LNET_MAX_SELECTION_PRIORITY;
2425 * The peer can have multiple interfaces, some of them can be on
2426 * the local network and others on a routed network. We should
2427 * prefer the local network. However if the local network is not
2428 * available then we need to try the routed network
2431 /* go through all the peer nets and find the best_ni */
2432 list_for_each_entry(lpn, &peer->lp_peer_nets, lpn_peer_nets) {
2434 * The peer's list of nets can contain non-local nets. We
2435 * want to only examine the local ones.
2437 net = lnet_get_net_locked(lpn->lpn_net_id);
2441 lpn_sel_prio = lpn->lpn_sel_priority;
2442 net_healthv = lnet_get_net_healthv_locked(net);
2443 net_sel_prio = net->net_sel_priority;
2446 * if this is a discovery message and lp_disc_net_id is
2447 * specified then use that net to send the discovery on.
2449 if (peer->lp_disc_net_id == lpn->lpn_net_id &&
2458 /* always select the lpn with the best health */
2459 if (best_lpn_healthv > lpn->lpn_healthv)
2461 else if (best_lpn_healthv < lpn->lpn_healthv)
2464 /* select the preferred peer and local nets */
2465 if (best_lpn_sel_prio < lpn_sel_prio)
2467 else if (best_lpn_sel_prio > lpn_sel_prio)
2470 if (best_net_healthv > net_healthv)
2472 else if (best_net_healthv < net_healthv)
2475 if (best_net_sel_prio < net_sel_prio)
2477 else if (best_net_sel_prio > net_sel_prio)
2480 if (best_lpn->lpn_seq < lpn->lpn_seq)
2482 else if (best_lpn->lpn_seq > lpn->lpn_seq)
2485 /* round robin over the local networks */
2486 if (best_net->net_seq <= net->net_seq)
2490 best_net_healthv = net_healthv;
2491 best_net_sel_prio = net_sel_prio;
2492 best_lpn_healthv = lpn->lpn_healthv;
2493 best_lpn_sel_prio = lpn_sel_prio;
2502 /* Select the best NI on the same net as best_lpn chosen
2505 best_ni = lnet_find_best_ni_on_spec_net(NULL, peer, best_lpn,
2512 static struct lnet_ni *
2513 lnet_find_existing_preferred_best_ni(struct lnet_peer_ni *lpni, int cpt)
2515 struct lnet_ni *best_ni = NULL;
2516 struct lnet_peer_net *peer_net = lpni->lpni_peer_net;
2517 struct lnet_peer_ni *lpni_entry;
2520 * We must use a consistent source address when sending to a
2521 * non-MR peer. However, a non-MR peer can have multiple NIDs
2522 * on multiple networks, and we may even need to talk to this
2523 * peer on multiple networks -- certain types of
2524 * load-balancing configuration do this.
2526 * So we need to pick the NI the peer prefers for this
2527 * particular network.
2530 list_for_each_entry(lpni_entry, &peer_net->lpn_peer_nis,
2532 if (lpni_entry->lpni_pref_nnids == 0)
2534 LASSERT(lpni_entry->lpni_pref_nnids == 1);
2535 best_ni = lnet_nid_to_ni_locked(&lpni_entry->lpni_pref.nid,
2543 /* Prerequisite: sd->sd_peer and sd->sd_best_lpni should be set */
2545 lnet_select_preferred_best_ni(struct lnet_send_data *sd)
2547 struct lnet_ni *best_ni = NULL;
2548 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
2551 * We must use a consistent source address when sending to a
2552 * non-MR peer. However, a non-MR peer can have multiple NIDs
2553 * on multiple networks, and we may even need to talk to this
2554 * peer on multiple networks -- certain types of
2555 * load-balancing configuration do this.
2557 * So we need to pick the NI the peer prefers for this
2558 * particular network.
2561 best_ni = lnet_find_existing_preferred_best_ni(sd->sd_best_lpni,
2564 /* if best_ni is still not set just pick one */
2567 lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2568 sd->sd_best_lpni->lpni_peer_net,
2571 /* If there is no best_ni we don't have a route */
2573 CERROR("no path to %s from net %s\n",
2574 libcfs_nidstr(&best_lpni->lpni_nid),
2575 libcfs_net2str(best_lpni->lpni_net->net_id));
2576 return -EHOSTUNREACH;
2580 sd->sd_best_ni = best_ni;
2582 /* Set preferred NI if necessary. */
2583 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
2590 * Source not specified
2594 * always use the same source NID for NMR peers
2595 * If we've talked to that peer before then we already have a preferred
2596 * source NI associated with it. Otherwise, we select a preferred local NI
2597 * and store it in the peer
2600 lnet_handle_any_local_nmr_dst(struct lnet_send_data *sd)
2604 /* sd->sd_best_lpni is already set to the final destination */
2607 * At this point we should've created the peer ni and peer. If we
2608 * can't find it, then something went wrong. Instead of assert
2609 * output a relevant message and fail the send
2611 if (!sd->sd_best_lpni) {
2612 CERROR("Internal fault. Unable to send msg %s to %s. NID not known\n",
2613 lnet_msgtyp2str(sd->sd_msg->msg_type),
2614 libcfs_nidstr(&sd->sd_dst_nid));
2618 if (sd->sd_msg->msg_routing) {
2619 /* If I'm forwarding this message then I can choose any NI
2620 * on the destination peer net
2622 sd->sd_best_ni = lnet_find_best_ni_on_spec_net(NULL,
2624 sd->sd_best_lpni->lpni_peer_net,
2627 if (!sd->sd_best_ni) {
2628 CERROR("Unable to forward message to %s. No local NI available\n",
2629 libcfs_nidstr(&sd->sd_dst_nid));
2633 rc = lnet_select_preferred_best_ni(sd);
2636 rc = lnet_handle_send(sd);
2642 lnet_handle_any_mr_dsta(struct lnet_send_data *sd)
2645 * NOTE we've already handled the remote peer case. So we only
2646 * need to worry about the local case here.
2648 * if we're sending a response, ACK or reply, we need to send it
2649 * to the destination NID given to us. At this point we already
2650 * have the peer_ni we're suppose to send to, so just find the
2651 * best_ni on the peer net and use that. Since we're sending to an
2652 * MR peer then we can just run the selection algorithm on our
2653 * local NIs and pick the best one.
2655 if (sd->sd_send_case & SND_RESP) {
2657 lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2658 sd->sd_best_lpni->lpni_peer_net,
2662 if (!sd->sd_best_ni) {
2664 * We're not going to deal with not able to send
2665 * a response to the provided final destination
2667 CERROR("Can't send response to %s. No local NI available\n",
2668 libcfs_nidstr(&sd->sd_dst_nid));
2669 return -EHOSTUNREACH;
2672 return lnet_handle_send(sd);
2676 * If we get here that means we're sending a fresh request, PUT or
2677 * GET, so we need to run our standard selection algorithm.
2678 * First find the best local interface that's on any of the peer's
2681 sd->sd_best_ni = lnet_find_best_ni_on_local_net(sd->sd_peer,
2684 lnet_msg_discovery(sd->sd_msg));
2685 if (sd->sd_best_ni) {
2687 lnet_find_best_lpni(sd->sd_best_ni,
2688 lnet_nid_to_nid4(&sd->sd_dst_nid),
2690 sd->sd_best_ni->ni_net->net_id);
2693 * if we're successful in selecting a peer_ni on the local
2694 * network, then send to it. Otherwise fall through and
2695 * try and see if we can reach it over another routed
2698 if (sd->sd_best_lpni &&
2699 nid_same(&sd->sd_best_lpni->lpni_nid,
2700 &the_lnet.ln_loni->ni_nid)) {
2702 * in case we initially started with a routed
2703 * destination, let's reset to local
2705 sd->sd_send_case &= ~REMOTE_DST;
2706 sd->sd_send_case |= LOCAL_DST;
2707 return lnet_handle_lo_send(sd);
2708 } else if (sd->sd_best_lpni) {
2710 * in case we initially started with a routed
2711 * destination, let's reset to local
2713 sd->sd_send_case &= ~REMOTE_DST;
2714 sd->sd_send_case |= LOCAL_DST;
2715 return lnet_handle_send(sd);
2718 CERROR("Internal Error. Expected to have a best_lpni: "
2720 libcfs_nidstr(&sd->sd_src_nid),
2721 libcfs_nidstr(&sd->sd_dst_nid));
2727 * Peer doesn't have a local network. Let's see if there is
2728 * a remote network we can reach it on.
2730 return PASS_THROUGH;
2735 * Source NID not specified
2740 * Source NID not speified
2741 * Remote destination
2744 * In both of these cases if we're sending a response, ACK or REPLY, then
2745 * we need to send to the destination NID provided.
2747 * In the remote case let's deal with MR routers.
2752 lnet_handle_any_mr_dst(struct lnet_send_data *sd)
2755 struct lnet_peer *gw_peer = NULL;
2756 struct lnet_peer_ni *gw_lpni = NULL;
2759 * handle sending a response to a remote peer here so we don't
2760 * have to worry about it if we hit lnet_handle_any_mr_dsta()
2762 if (sd->sd_send_case & REMOTE_DST &&
2763 sd->sd_send_case & SND_RESP) {
2764 struct lnet_peer_ni *gw;
2765 struct lnet_peer *gw_peer;
2767 rc = lnet_handle_find_routed_path(
2768 sd, &sd->sd_dst_nid, &gw, &gw_peer);
2770 CERROR("Can't send response to %s. No route available\n",
2771 libcfs_nidstr(&sd->sd_dst_nid));
2772 return -EHOSTUNREACH;
2773 } else if (rc > 0) {
2777 sd->sd_best_lpni = gw;
2778 sd->sd_peer = gw_peer;
2780 return lnet_handle_send(sd);
2784 * Even though the NID for the peer might not be on a local network,
2785 * since the peer is MR there could be other interfaces on the
2786 * local network. In that case we'd still like to prefer the local
2787 * network over the routed network. If we're unable to do that
2788 * then we select the best router among the different routed networks,
2789 * and if the router is MR then we can deal with it as such.
2791 rc = lnet_handle_any_mr_dsta(sd);
2792 if (rc != PASS_THROUGH)
2796 * Now that we must route to the destination, we must consider the
2797 * MR case, where the destination has multiple interfaces, some of
2798 * which we can route to and others we do not. For this reason we
2799 * need to select the destination which we can route to and if
2800 * there are multiple, we need to round robin.
2802 rc = lnet_handle_find_routed_path(sd, &sd->sd_dst_nid,
2803 &gw_lpni, &gw_peer);
2807 sd->sd_send_case &= ~LOCAL_DST;
2808 sd->sd_send_case |= REMOTE_DST;
2810 sd->sd_peer = gw_peer;
2811 sd->sd_best_lpni = gw_lpni;
2813 return lnet_handle_send(sd);
2817 * Source not specified
2818 * Remote destination
2821 * Must send to the specified peer NID using the same source NID that
2822 * we've used before. If it's the first time to talk to that peer then
2823 * find the source NI and assign it as preferred to that peer
2826 lnet_handle_any_router_nmr_dst(struct lnet_send_data *sd)
2829 struct lnet_peer_ni *gw_lpni = NULL;
2830 struct lnet_peer *gw_peer = NULL;
2833 * Let's see if we have a preferred NI to talk to this NMR peer
2835 sd->sd_best_ni = lnet_find_existing_preferred_best_ni(sd->sd_best_lpni,
2839 * find the router and that'll find the best NI if we didn't find
2842 rc = lnet_handle_find_routed_path(sd, &sd->sd_dst_nid, &gw_lpni,
2848 * set the best_ni we've chosen as the preferred one for
2851 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
2853 /* we'll be sending to the gw */
2854 sd->sd_best_lpni = gw_lpni;
2855 sd->sd_peer = gw_peer;
2857 return lnet_handle_send(sd);
2861 lnet_handle_send_case_locked(struct lnet_send_data *sd)
2864 * turn off the SND_RESP bit.
2865 * It will be checked in the case handling
2867 __u32 send_case = sd->sd_send_case &= ~SND_RESP ;
2869 CDEBUG(D_NET, "Source %s%s to %s %s %s destination\n",
2870 (send_case & SRC_SPEC) ? "Specified: " : "ANY",
2871 (send_case & SRC_SPEC) ? libcfs_nidstr(&sd->sd_src_nid) : "",
2872 (send_case & MR_DST) ? "MR: " : "NMR: ",
2873 libcfs_nidstr(&sd->sd_dst_nid),
2874 (send_case & LOCAL_DST) ? "local" : "routed");
2876 switch (send_case) {
2878 * For all cases where the source is specified, we should always
2879 * use the destination NID, whether it's an MR destination or not,
2880 * since we're continuing a series of related messages for the
2883 case SRC_SPEC_LOCAL_NMR_DST:
2884 return lnet_handle_spec_local_nmr_dst(sd);
2885 case SRC_SPEC_LOCAL_MR_DST:
2886 return lnet_handle_spec_local_mr_dst(sd);
2887 case SRC_SPEC_ROUTER_NMR_DST:
2888 case SRC_SPEC_ROUTER_MR_DST:
2889 return lnet_handle_spec_router_dst(sd);
2890 case SRC_ANY_LOCAL_NMR_DST:
2891 return lnet_handle_any_local_nmr_dst(sd);
2892 case SRC_ANY_LOCAL_MR_DST:
2893 case SRC_ANY_ROUTER_MR_DST:
2894 return lnet_handle_any_mr_dst(sd);
2895 case SRC_ANY_ROUTER_NMR_DST:
2896 return lnet_handle_any_router_nmr_dst(sd);
2898 CERROR("Unknown send case\n");
2904 lnet_select_pathway(struct lnet_nid *src_nid,
2905 struct lnet_nid *dst_nid,
2906 struct lnet_msg *msg,
2907 struct lnet_nid *rtr_nid)
2909 struct lnet_peer_ni *lpni;
2910 struct lnet_peer *peer;
2911 struct lnet_send_data send_data;
2914 __u32 send_case = 0;
2916 bool mr_forwarding_allowed;
2918 memset(&send_data, 0, sizeof(send_data));
2921 * get an initial CPT to use for locking. The idea here is not to
2922 * serialize the calls to select_pathway, so that as many
2923 * operations can run concurrently as possible. To do that we use
2924 * the CPT where this call is being executed. Later on when we
2925 * determine the CPT to use in lnet_message_commit, we switch the
2926 * lock and check if there was any configuration change. If none,
2927 * then we proceed, if there is, then we restart the operation.
2929 cpt = lnet_net_lock_current();
2931 md_cpt = lnet_cpt_of_md(msg->msg_md, msg->msg_offset);
2932 if (md_cpt == CFS_CPT_ANY)
2938 * If we're being asked to send to the loopback interface, there
2939 * is no need to go through any selection. We can just shortcut
2940 * the entire process and send over lolnd
2942 send_data.sd_msg = msg;
2943 send_data.sd_cpt = cpt;
2944 if (nid_is_lo0(dst_nid)) {
2945 rc = lnet_handle_lo_send(&send_data);
2946 lnet_net_unlock(cpt);
2951 * find an existing peer_ni, or create one and mark it as having been
2952 * created due to network traffic. This call will create the
2953 * peer->peer_net->peer_ni tree.
2955 lpni = lnet_peerni_by_nid_locked(dst_nid, NULL, cpt);
2957 lnet_net_unlock(cpt);
2958 return PTR_ERR(lpni);
2962 * Cache the original src_nid and rtr_nid. If we need to resend the
2963 * message then we'll need to know whether the src_nid was originally
2964 * specified for this message. If it was originally specified,
2965 * then we need to keep using the same src_nid since it's
2966 * continuing the same sequence of messages. Similarly, rtr_nid will
2967 * affect our choice of next hop.
2970 msg->msg_src_nid_param = *src_nid;
2972 msg->msg_src_nid_param = LNET_ANY_NID;
2974 msg->msg_rtr_nid_param = *rtr_nid;
2976 msg->msg_rtr_nid_param = LNET_ANY_NID;
2979 * If necessary, perform discovery on the peer that owns this peer_ni.
2980 * Note, this can result in the ownership of this peer_ni changing
2981 * to another peer object.
2983 rc = lnet_initiate_peer_discovery(lpni, msg, cpt);
2985 lnet_peer_ni_decref_locked(lpni);
2986 lnet_net_unlock(cpt);
2989 lnet_peer_ni_decref_locked(lpni);
2991 peer = lpni->lpni_peer_net->lpn_peer;
2994 * Identify the different send cases
2996 if (!src_nid || LNET_NID_IS_ANY(src_nid)) {
2997 send_case |= SRC_ANY;
2998 if (lnet_get_net_locked(LNET_NID_NET(dst_nid)))
2999 send_case |= LOCAL_DST;
3001 send_case |= REMOTE_DST;
3003 send_case |= SRC_SPEC;
3004 if (LNET_NID_NET(src_nid) == LNET_NID_NET(dst_nid))
3005 send_case |= LOCAL_DST;
3007 send_case |= REMOTE_DST;
3011 if (msg->msg_routing && (send_case & LOCAL_DST))
3014 /* Determine whether to allow MR forwarding for this message.
3015 * NB: MR forwarding is allowed if the message originator and the
3016 * destination are both MR capable, and the destination lpni that was
3017 * originally chosen by the originator is unhealthy or down.
3018 * We check the MR capability of the destination further below
3020 mr_forwarding_allowed = false;
3022 struct lnet_peer *src_lp;
3023 struct lnet_peer_ni *src_lpni;
3025 src_lpni = lnet_peerni_by_nid_locked(&msg->msg_hdr.src_nid,
3027 /* We don't fail the send if we hit any errors here. We'll just
3028 * try to send it via non-multi-rail criteria
3030 if (!IS_ERR(src_lpni)) {
3031 /* Drop ref taken by lnet_nid2peerni_locked() */
3032 lnet_peer_ni_decref_locked(src_lpni);
3033 src_lp = lpni->lpni_peer_net->lpn_peer;
3034 if (lnet_peer_is_multi_rail(src_lp) &&
3035 !lnet_is_peer_ni_alive(lpni))
3036 mr_forwarding_allowed = true;
3039 CDEBUG(D_NET, "msg %p MR forwarding %s\n", msg,
3040 mr_forwarding_allowed ? "allowed" : "not allowed");
3044 * Deal with the peer as NMR in the following cases:
3045 * 1. the peer is NMR
3046 * 2. We're trying to recover a specific peer NI
3047 * 3. I'm a router sending to the final destination and MR forwarding is
3048 * not allowed for this message (as determined above).
3049 * In this case the source of the message would've
3050 * already selected the final destination so my job
3051 * is to honor the selection.
3053 if (!lnet_peer_is_multi_rail(peer) || msg->msg_recovery ||
3054 (final_hop && !mr_forwarding_allowed))
3055 send_case |= NMR_DST;
3057 send_case |= MR_DST;
3059 if (lnet_msg_is_response(msg))
3060 send_case |= SND_RESP;
3062 /* assign parameters to the send_data */
3064 send_data.sd_rtr_nid = *rtr_nid;
3066 send_data.sd_rtr_nid = LNET_ANY_NID;
3068 send_data.sd_src_nid = *src_nid;
3070 send_data.sd_src_nid = LNET_ANY_NID;
3071 send_data.sd_dst_nid = *dst_nid;
3072 send_data.sd_best_lpni = lpni;
3074 * keep a pointer to the final destination in case we're going to
3075 * route, so we'll need to access it later
3077 send_data.sd_final_dst_lpni = lpni;
3078 send_data.sd_peer = peer;
3079 send_data.sd_md_cpt = md_cpt;
3080 send_data.sd_send_case = send_case;
3082 rc = lnet_handle_send_case_locked(&send_data);
3085 * Update the local cpt since send_data.sd_cpt might've been
3086 * updated as a result of calling lnet_handle_send_case_locked().
3088 cpt = send_data.sd_cpt;
3090 if (rc == REPEAT_SEND)
3093 lnet_net_unlock(cpt);
3099 lnet_send(struct lnet_nid *src_nid, struct lnet_msg *msg,
3100 struct lnet_nid *rtr_nid)
3102 struct lnet_nid *dst_nid = &msg->msg_target.nid;
3105 /* NB: ni != NULL == interface pre-determined (ACK/REPLY) */
3106 LASSERT(msg->msg_txpeer == NULL);
3107 LASSERT(msg->msg_txni == NULL);
3108 LASSERT(!msg->msg_sending);
3109 LASSERT(!msg->msg_target_is_router);
3110 LASSERT(!msg->msg_receiving);
3112 msg->msg_sending = 1;
3114 LASSERT(!msg->msg_tx_committed);
3116 rc = lnet_select_pathway(src_nid, dst_nid, msg, rtr_nid);
3118 if (rc == -EHOSTUNREACH)
3119 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_ERROR;
3121 msg->msg_health_status = LNET_MSG_STATUS_LOCAL_ERROR;
3125 if (rc == LNET_CREDIT_OK)
3126 lnet_ni_send(msg->msg_txni, msg);
3128 /* rc == LNET_CREDIT_OK or LNET_CREDIT_WAIT or LNET_DC_WAIT */
3132 enum lnet_mt_event_type {
3133 MT_TYPE_LOCAL_NI = 0,
3137 struct lnet_mt_event_info {
3138 enum lnet_mt_event_type mt_type;
3142 /* called with res_lock held */
3144 lnet_detach_rsp_tracker(struct lnet_libmd *md, int cpt)
3146 struct lnet_rsp_tracker *rspt;
3149 * msg has a refcount on the MD so the MD is not going away.
3150 * The rspt queue for the cpt is protected by
3151 * the lnet_net_lock(cpt). cpt is the cpt of the MD cookie.
3153 if (!md->md_rspt_ptr)
3156 rspt = md->md_rspt_ptr;
3159 LASSERT(rspt->rspt_cpt == cpt);
3161 md->md_rspt_ptr = NULL;
3163 if (LNetMDHandleIsInvalid(rspt->rspt_mdh)) {
3165 * The monitor thread has invalidated this handle because the
3166 * response timed out, but it failed to lookup the MD. That
3167 * means this response tracker is on the zombie list. We can
3168 * safely remove it under the resource lock (held by caller) and
3169 * free the response tracker block.
3171 list_del(&rspt->rspt_on_list);
3172 lnet_rspt_free(rspt, cpt);
3175 * invalidate the handle to indicate that a response has been
3176 * received, which will then lead the monitor thread to clean up
3179 LNetInvalidateMDHandle(&rspt->rspt_mdh);
3184 lnet_clean_zombie_rstqs(void)
3186 struct lnet_rsp_tracker *rspt, *tmp;
3189 cfs_cpt_for_each(i, lnet_cpt_table()) {
3190 list_for_each_entry_safe(rspt, tmp,
3191 the_lnet.ln_mt_zombie_rstqs[i],
3193 list_del(&rspt->rspt_on_list);
3194 lnet_rspt_free(rspt, i);
3198 cfs_percpt_free(the_lnet.ln_mt_zombie_rstqs);
3202 lnet_finalize_expired_responses(void)
3204 struct lnet_libmd *md;
3205 struct lnet_rsp_tracker *rspt, *tmp;
3209 if (the_lnet.ln_mt_rstq == NULL)
3212 cfs_cpt_for_each(i, lnet_cpt_table()) {
3213 LIST_HEAD(local_queue);
3216 if (!the_lnet.ln_mt_rstq[i]) {
3220 list_splice_init(the_lnet.ln_mt_rstq[i], &local_queue);
3225 list_for_each_entry_safe(rspt, tmp, &local_queue, rspt_on_list) {
3227 * The rspt mdh will be invalidated when a response
3228 * is received or whenever we want to discard the
3229 * block the monitor thread will walk the queue
3230 * and clean up any rsts with an invalid mdh.
3231 * The monitor thread will walk the queue until
3232 * the first unexpired rspt block. This means that
3233 * some rspt blocks which received their
3234 * corresponding responses will linger in the
3235 * queue until they are cleaned up eventually.
3238 if (LNetMDHandleIsInvalid(rspt->rspt_mdh)) {
3240 list_del(&rspt->rspt_on_list);
3241 lnet_rspt_free(rspt, i);
3245 if (ktime_compare(now, rspt->rspt_deadline) >= 0 ||
3246 the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN) {
3247 struct lnet_peer_ni *lpni;
3248 struct lnet_nid nid;
3250 md = lnet_handle2md(&rspt->rspt_mdh);
3252 /* MD has been queued for unlink, but
3253 * rspt hasn't been detached (Note we've
3254 * checked above that the rspt_mdh is
3255 * valid). Since we cannot lookup the MD
3256 * we're unable to detach the rspt
3257 * ourselves. Thus, move the rspt to the
3258 * zombie list where we'll wait for
3260 * 1. The remaining operations on the
3261 * MD to complete. In this case the
3262 * final operation will result in
3263 * lnet_msg_detach_md()->
3264 * lnet_detach_rsp_tracker() where
3265 * we will clean up this response
3267 * 2. LNet to shutdown. In this case
3268 * we'll wait until after all LND Nets
3269 * have shutdown and then we can
3270 * safely free any remaining response
3271 * tracker blocks on the zombie list.
3272 * Note: We need to hold the resource
3273 * lock when adding to the zombie list
3274 * because we may have concurrent access
3275 * with lnet_detach_rsp_tracker().
3277 LNetInvalidateMDHandle(&rspt->rspt_mdh);
3278 list_move(&rspt->rspt_on_list,
3279 the_lnet.ln_mt_zombie_rstqs[i]);
3283 LASSERT(md->md_rspt_ptr == rspt);
3284 md->md_rspt_ptr = NULL;
3287 LNetMDUnlink(rspt->rspt_mdh);
3289 nid = rspt->rspt_next_hop_nid;
3291 list_del(&rspt->rspt_on_list);
3292 lnet_rspt_free(rspt, i);
3294 /* If we're shutting down we just want to clean
3295 * up the rspt blocks
3297 if (the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN)
3301 the_lnet.ln_counters[i]->lct_health.lch_response_timeout_count++;
3305 "Response timeout: md = %p: nid = %s\n",
3306 md, libcfs_nidstr(&nid));
3309 * If there is a timeout on the response
3310 * from the next hop decrement its health
3311 * value so that we don't use it
3314 lpni = lnet_peer_ni_find_locked(&nid);
3316 lnet_handle_remote_failure_locked(lpni);
3317 lnet_peer_ni_decref_locked(lpni);
3326 if (!list_empty(&local_queue)) {
3328 list_splice(&local_queue, the_lnet.ln_mt_rstq[i]);
3335 lnet_resend_pending_msgs_locked(struct list_head *resendq, int cpt)
3337 struct lnet_msg *msg;
3339 while (!list_empty(resendq)) {
3340 struct lnet_peer_ni *lpni;
3342 msg = list_entry(resendq->next, struct lnet_msg,
3345 list_del_init(&msg->msg_list);
3347 lpni = lnet_peer_ni_find_locked(&msg->msg_hdr.dest_nid);
3349 lnet_net_unlock(cpt);
3350 CERROR("Expected that a peer is already created for %s\n",
3351 libcfs_nidstr(&msg->msg_hdr.dest_nid));
3352 msg->msg_no_resend = true;
3353 lnet_finalize(msg, -EFAULT);
3358 lnet_peer_ni_decref_locked(lpni);
3360 lnet_net_unlock(cpt);
3361 CDEBUG(D_NET, "resending %s->%s: %s recovery %d try# %d\n",
3362 libcfs_nidstr(&msg->msg_src_nid_param),
3363 libcfs_idstr(&msg->msg_target),
3364 lnet_msgtyp2str(msg->msg_type),
3366 msg->msg_retry_count);
3367 rc = lnet_send(&msg->msg_src_nid_param, msg,
3368 &msg->msg_rtr_nid_param);
3370 CERROR("Error sending %s to %s: %d\n",
3371 lnet_msgtyp2str(msg->msg_type),
3372 libcfs_idstr(&msg->msg_target), rc);
3373 msg->msg_no_resend = true;
3374 lnet_finalize(msg, rc);
3378 the_lnet.ln_counters[cpt]->lct_health.lch_resend_count++;
3384 lnet_resend_pending_msgs(void)
3388 cfs_cpt_for_each(i, lnet_cpt_table()) {
3390 lnet_resend_pending_msgs_locked(the_lnet.ln_mt_resendqs[i], i);
3395 /* called with cpt and ni_lock held */
3397 lnet_unlink_ni_recovery_mdh_locked(struct lnet_ni *ni, int cpt, bool force)
3399 struct lnet_handle_md recovery_mdh;
3401 LNetInvalidateMDHandle(&recovery_mdh);
3403 if (ni->ni_recovery_state & LNET_NI_RECOVERY_PENDING ||
3405 recovery_mdh = ni->ni_ping_mdh;
3406 LNetInvalidateMDHandle(&ni->ni_ping_mdh);
3409 lnet_net_unlock(cpt);
3410 if (!LNetMDHandleIsInvalid(recovery_mdh))
3411 LNetMDUnlink(recovery_mdh);
3417 lnet_recover_local_nis(void)
3419 struct lnet_mt_event_info *ev_info;
3420 LIST_HEAD(processed_list);
3421 LIST_HEAD(local_queue);
3422 struct lnet_handle_md mdh;
3423 struct lnet_ni *tmp;
3431 * splice the recovery queue on a local queue. We will iterate
3432 * through the local queue and update it as needed. Once we're
3433 * done with the traversal, we'll splice the local queue back on
3434 * the head of the ln_mt_localNIRecovq. Any newly added local NIs
3435 * will be traversed in the next iteration.
3438 list_splice_init(&the_lnet.ln_mt_localNIRecovq,
3442 now = ktime_get_seconds();
3444 list_for_each_entry_safe(ni, tmp, &local_queue, ni_recovery) {
3446 * if an NI is being deleted or it is now healthy, there
3447 * is no need to keep it around in the recovery queue.
3448 * The monitor thread is the only thread responsible for
3449 * removing the NI from the recovery queue.
3450 * Multiple threads can be adding NIs to the recovery
3453 healthv = atomic_read(&ni->ni_healthv);
3457 if (ni->ni_state != LNET_NI_STATE_ACTIVE ||
3458 healthv == LNET_MAX_HEALTH_VALUE) {
3459 list_del_init(&ni->ni_recovery);
3460 lnet_unlink_ni_recovery_mdh_locked(ni, 0, false);
3462 lnet_ni_decref_locked(ni, 0);
3468 * if the local NI failed recovery we must unlink the md.
3469 * But we want to keep the local_ni on the recovery queue
3470 * so we can continue the attempts to recover it.
3472 if (ni->ni_recovery_state & LNET_NI_RECOVERY_FAILED) {
3473 lnet_unlink_ni_recovery_mdh_locked(ni, 0, true);
3474 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_FAILED;
3480 if (now < ni->ni_next_ping) {
3487 CDEBUG(D_NET, "attempting to recover local ni: %s\n",
3488 libcfs_nidstr(&ni->ni_nid));
3491 if (!(ni->ni_recovery_state & LNET_NI_RECOVERY_PENDING)) {
3492 ni->ni_recovery_state |= LNET_NI_RECOVERY_PENDING;
3495 LIBCFS_ALLOC(ev_info, sizeof(*ev_info));
3497 CERROR("out of memory. Can't recover %s\n",
3498 libcfs_nidstr(&ni->ni_nid));
3500 ni->ni_recovery_state &=
3501 ~LNET_NI_RECOVERY_PENDING;
3506 mdh = ni->ni_ping_mdh;
3508 * Invalidate the ni mdh in case it's deleted.
3509 * We'll unlink the mdh in this case below.
3511 LNetInvalidateMDHandle(&ni->ni_ping_mdh);
3512 /* FIXME need to handle large-addr nid */
3513 nid = lnet_nid_to_nid4(&ni->ni_nid);
3516 * remove the NI from the local queue and drop the
3517 * reference count to it while we're recovering
3518 * it. The reason for that, is that the NI could
3519 * be deleted, and the way the code is structured
3520 * is if we don't drop the NI, then the deletion
3521 * code will enter a loop waiting for the
3522 * reference count to be removed while holding the
3523 * ln_mutex_lock(). When we look up the peer to
3524 * send to in lnet_select_pathway() we will try to
3525 * lock the ln_mutex_lock() as well, leading to
3526 * a deadlock. By dropping the refcount and
3527 * removing it from the list, we allow for the NI
3528 * to be removed, then we use the cached NID to
3529 * look it up again. If it's gone, then we just
3530 * continue examining the rest of the queue.
3533 list_del_init(&ni->ni_recovery);
3534 lnet_ni_decref_locked(ni, 0);
3537 ev_info->mt_type = MT_TYPE_LOCAL_NI;
3538 ev_info->mt_nid = nid;
3539 rc = lnet_send_ping(nid, &mdh, LNET_INTERFACES_MIN,
3540 ev_info, the_lnet.ln_mt_handler,
3542 /* lookup the nid again */
3544 ni = lnet_nid2ni_locked(nid, 0);
3547 * the NI has been deleted when we dropped
3554 ni->ni_ping_count++;
3556 ni->ni_ping_mdh = mdh;
3557 lnet_ni_add_to_recoveryq_locked(ni, &processed_list,
3562 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_PENDING;
3571 * put back the remaining NIs on the ln_mt_localNIRecovq to be
3572 * reexamined in the next iteration.
3574 list_splice_init(&processed_list, &local_queue);
3576 list_splice(&local_queue, &the_lnet.ln_mt_localNIRecovq);
3581 lnet_resendqs_create(void)
3583 struct list_head **resendqs;
3584 resendqs = lnet_create_array_of_queues();
3589 lnet_net_lock(LNET_LOCK_EX);
3590 the_lnet.ln_mt_resendqs = resendqs;
3591 lnet_net_unlock(LNET_LOCK_EX);
3597 lnet_clean_local_ni_recoveryq(void)
3601 /* This is only called when the monitor thread has stopped */
3604 while (!list_empty(&the_lnet.ln_mt_localNIRecovq)) {
3605 ni = list_entry(the_lnet.ln_mt_localNIRecovq.next,
3606 struct lnet_ni, ni_recovery);
3607 list_del_init(&ni->ni_recovery);
3609 lnet_unlink_ni_recovery_mdh_locked(ni, 0, true);
3611 lnet_ni_decref_locked(ni, 0);
3618 lnet_unlink_lpni_recovery_mdh_locked(struct lnet_peer_ni *lpni, int cpt,
3621 struct lnet_handle_md recovery_mdh;
3623 LNetInvalidateMDHandle(&recovery_mdh);
3625 if (lpni->lpni_state & LNET_PEER_NI_RECOVERY_PENDING || force) {
3626 recovery_mdh = lpni->lpni_recovery_ping_mdh;
3627 LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
3629 spin_unlock(&lpni->lpni_lock);
3630 lnet_net_unlock(cpt);
3631 if (!LNetMDHandleIsInvalid(recovery_mdh))
3632 LNetMDUnlink(recovery_mdh);
3634 spin_lock(&lpni->lpni_lock);
3638 lnet_clean_peer_ni_recoveryq(void)
3640 struct lnet_peer_ni *lpni, *tmp;
3642 lnet_net_lock(LNET_LOCK_EX);
3644 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_mt_peerNIRecovq,
3646 list_del_init(&lpni->lpni_recovery);
3647 spin_lock(&lpni->lpni_lock);
3648 lnet_unlink_lpni_recovery_mdh_locked(lpni, LNET_LOCK_EX, true);
3649 spin_unlock(&lpni->lpni_lock);
3650 lnet_peer_ni_decref_locked(lpni);
3653 lnet_net_unlock(LNET_LOCK_EX);
3657 lnet_clean_resendqs(void)
3659 struct lnet_msg *msg, *tmp;
3663 cfs_cpt_for_each(i, lnet_cpt_table()) {
3665 list_splice_init(the_lnet.ln_mt_resendqs[i], &msgs);
3667 list_for_each_entry_safe(msg, tmp, &msgs, msg_list) {
3668 list_del_init(&msg->msg_list);
3669 msg->msg_no_resend = true;
3670 lnet_finalize(msg, -ESHUTDOWN);
3674 cfs_percpt_free(the_lnet.ln_mt_resendqs);
3678 lnet_recover_peer_nis(void)
3680 struct lnet_mt_event_info *ev_info;
3681 LIST_HEAD(processed_list);
3682 LIST_HEAD(local_queue);
3683 struct lnet_handle_md mdh;
3684 struct lnet_peer_ni *lpni;
3685 struct lnet_peer_ni *tmp;
3692 * Always use cpt 0 for locking across all interactions with
3693 * ln_mt_peerNIRecovq
3696 list_splice_init(&the_lnet.ln_mt_peerNIRecovq,
3700 now = ktime_get_seconds();
3702 list_for_each_entry_safe(lpni, tmp, &local_queue,
3705 * The same protection strategy is used here as is in the
3706 * local recovery case.
3709 healthv = atomic_read(&lpni->lpni_healthv);
3710 spin_lock(&lpni->lpni_lock);
3711 if (lpni->lpni_state & LNET_PEER_NI_DELETING ||
3712 healthv == LNET_MAX_HEALTH_VALUE) {
3713 list_del_init(&lpni->lpni_recovery);
3714 lnet_unlink_lpni_recovery_mdh_locked(lpni, 0, false);
3715 spin_unlock(&lpni->lpni_lock);
3716 lnet_peer_ni_decref_locked(lpni);
3722 * If the peer NI has failed recovery we must unlink the
3723 * md. But we want to keep the peer ni on the recovery
3724 * queue so we can try to continue recovering it
3726 if (lpni->lpni_state & LNET_PEER_NI_RECOVERY_FAILED) {
3727 lnet_unlink_lpni_recovery_mdh_locked(lpni, 0, true);
3728 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_FAILED;
3731 spin_unlock(&lpni->lpni_lock);
3733 if (now < lpni->lpni_next_ping) {
3741 * NOTE: we're racing with peer deletion from user space.
3742 * It's possible that a peer is deleted after we check its
3743 * state. In this case the recovery can create a new peer
3745 spin_lock(&lpni->lpni_lock);
3746 if (!(lpni->lpni_state & LNET_PEER_NI_RECOVERY_PENDING) &&
3747 !(lpni->lpni_state & LNET_PEER_NI_DELETING)) {
3748 lpni->lpni_state |= LNET_PEER_NI_RECOVERY_PENDING;
3749 spin_unlock(&lpni->lpni_lock);
3751 LIBCFS_ALLOC(ev_info, sizeof(*ev_info));
3753 CERROR("out of memory. Can't recover %s\n",
3754 libcfs_nidstr(&lpni->lpni_nid));
3755 spin_lock(&lpni->lpni_lock);
3756 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3757 spin_unlock(&lpni->lpni_lock);
3761 /* look at the comments in lnet_recover_local_nis() */
3762 mdh = lpni->lpni_recovery_ping_mdh;
3763 LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
3764 /* FIXME handle large-addr nid */
3765 nid = lnet_nid_to_nid4(&lpni->lpni_nid);
3767 list_del_init(&lpni->lpni_recovery);
3768 lnet_peer_ni_decref_locked(lpni);
3771 ev_info->mt_type = MT_TYPE_PEER_NI;
3772 ev_info->mt_nid = nid;
3773 rc = lnet_send_ping(nid, &mdh, LNET_INTERFACES_MIN,
3774 ev_info, the_lnet.ln_mt_handler,
3778 * lnet_find_peer_ni_locked() grabs a refcount for
3779 * us. No need to take it explicitly.
3781 lpni = lnet_find_peer_ni_locked(nid);
3788 lpni->lpni_ping_count++;
3790 lpni->lpni_recovery_ping_mdh = mdh;
3792 lnet_peer_ni_add_to_recoveryq_locked(lpni,
3796 spin_lock(&lpni->lpni_lock);
3797 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3798 spin_unlock(&lpni->lpni_lock);
3801 /* Drop the ref taken by lnet_find_peer_ni_locked() */
3802 lnet_peer_ni_decref_locked(lpni);
3805 spin_unlock(&lpni->lpni_lock);
3808 list_splice_init(&processed_list, &local_queue);
3810 list_splice(&local_queue, &the_lnet.ln_mt_peerNIRecovq);
3815 lnet_monitor_thread(void *arg)
3817 time64_t rsp_timeout = 0;
3820 wait_for_completion(&the_lnet.ln_started);
3822 * The monitor thread takes care of the following:
3823 * 1. Checks the aliveness of routers
3824 * 2. Checks if there are messages on the resend queue to resend
3826 * 3. Check if there are any NIs on the local recovery queue and
3828 * 4. Checks if there are any NIs on the remote recovery queue
3831 while (the_lnet.ln_mt_state == LNET_MT_STATE_RUNNING) {
3832 now = ktime_get_real_seconds();
3834 if (lnet_router_checker_active())
3835 lnet_check_routers();
3837 lnet_resend_pending_msgs();
3839 if (now >= rsp_timeout) {
3840 lnet_finalize_expired_responses();
3841 rsp_timeout = now + (lnet_transaction_timeout / 2);
3844 lnet_recover_local_nis();
3845 lnet_recover_peer_nis();
3848 * TODO do we need to check if we should sleep without
3849 * timeout? Technically, an active system will always
3850 * have messages in flight so this check will always
3851 * evaluate to false. And on an idle system do we care
3852 * if we wake up every 1 second? Although, we've seen
3853 * cases where we get a complaint that an idle thread
3854 * is waking up unnecessarily.
3856 wait_for_completion_interruptible_timeout(
3857 &the_lnet.ln_mt_wait_complete,
3858 cfs_time_seconds(1));
3859 /* Must re-init the completion before testing anything,
3860 * including ln_mt_state.
3862 reinit_completion(&the_lnet.ln_mt_wait_complete);
3866 lnet_net_lock(LNET_LOCK_EX);
3867 the_lnet.ln_mt_state = LNET_MT_STATE_SHUTDOWN;
3868 lnet_net_unlock(LNET_LOCK_EX);
3870 /* signal that the monitor thread is exiting */
3871 up(&the_lnet.ln_mt_signal);
3879 * Returns == 0 if success
3880 * Returns > 0 if LNetMDBind or prior fails
3881 * Returns < 0 if LNetGet fails
3884 lnet_send_ping(lnet_nid_t dest_nid,
3885 struct lnet_handle_md *mdh, int nnis,
3886 void *user_data, lnet_handler_t handler, bool recovery)
3888 struct lnet_md md = { NULL };
3889 struct lnet_process_id id;
3890 struct lnet_ping_buffer *pbuf;
3893 if (dest_nid == LNET_NID_ANY) {
3898 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
3904 /* initialize md content */
3905 md.start = &pbuf->pb_info;
3906 md.length = LNET_PING_INFO_SIZE(nnis);
3907 md.threshold = 2; /* GET/REPLY */
3909 md.options = LNET_MD_TRUNCATE | LNET_MD_TRACK_RESPONSE;
3910 md.user_ptr = user_data;
3911 md.handler = handler;
3913 rc = LNetMDBind(&md, LNET_UNLINK, mdh);
3915 lnet_ping_buffer_decref(pbuf);
3916 CERROR("Can't bind MD: %d\n", rc);
3917 rc = -rc; /* change the rc to positive */
3920 id.pid = LNET_PID_LUSTRE;
3923 rc = LNetGet(LNET_NID_ANY, *mdh, id,
3924 LNET_RESERVED_PORTAL,
3925 LNET_PROTO_PING_MATCHBITS, 0, recovery);
3928 goto fail_unlink_md;
3934 LNetInvalidateMDHandle(mdh);
3940 lnet_handle_recovery_reply(struct lnet_mt_event_info *ev_info,
3941 int status, bool send, bool unlink_event)
3943 lnet_nid_t nid = ev_info->mt_nid;
3945 if (ev_info->mt_type == MT_TYPE_LOCAL_NI) {
3949 ni = lnet_nid2ni_locked(nid, 0);
3955 if (!send || (send && status != 0))
3956 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_PENDING;
3958 ni->ni_recovery_state |= LNET_NI_RECOVERY_FAILED;
3963 CERROR("local NI (%s) recovery failed with %d\n",
3964 libcfs_nid2str(nid), status);
3968 * need to increment healthv for the ni here, because in
3969 * the lnet_finalize() path we don't have access to this
3970 * NI. And in order to get access to it, we'll need to
3971 * carry forward too much information.
3972 * In the peer case, it'll naturally be incremented
3975 lnet_inc_healthv(&ni->ni_healthv,
3976 lnet_health_sensitivity);
3978 struct lnet_peer_ni *lpni;
3981 cpt = lnet_net_lock_current();
3982 lpni = lnet_find_peer_ni_locked(nid);
3984 lnet_net_unlock(cpt);
3987 spin_lock(&lpni->lpni_lock);
3988 if (!send || (send && status != 0))
3989 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3991 lpni->lpni_state |= LNET_PEER_NI_RECOVERY_FAILED;
3992 spin_unlock(&lpni->lpni_lock);
3993 lnet_peer_ni_decref_locked(lpni);
3994 lnet_net_unlock(cpt);
3997 CERROR("peer NI (%s) recovery failed with %d\n",
3998 libcfs_nid2str(nid), status);
4003 lnet_mt_event_handler(struct lnet_event *event)
4005 struct lnet_mt_event_info *ev_info = event->md_user_ptr;
4006 struct lnet_ping_buffer *pbuf;
4008 /* TODO: remove assert */
4009 LASSERT(event->type == LNET_EVENT_REPLY ||
4010 event->type == LNET_EVENT_SEND ||
4011 event->type == LNET_EVENT_UNLINK);
4013 CDEBUG(D_NET, "Received event: %d status: %d\n", event->type,
4016 switch (event->type) {
4017 case LNET_EVENT_UNLINK:
4018 CDEBUG(D_NET, "%s recovery ping unlinked\n",
4019 libcfs_nid2str(ev_info->mt_nid));
4021 case LNET_EVENT_REPLY:
4022 lnet_handle_recovery_reply(ev_info, event->status, false,
4023 event->type == LNET_EVENT_UNLINK);
4025 case LNET_EVENT_SEND:
4026 CDEBUG(D_NET, "%s recovery message sent %s:%d\n",
4027 libcfs_nid2str(ev_info->mt_nid),
4028 (event->status) ? "unsuccessfully" :
4029 "successfully", event->status);
4030 lnet_handle_recovery_reply(ev_info, event->status, true, false);
4033 CERROR("Unexpected event: %d\n", event->type);
4036 if (event->unlinked) {
4037 LIBCFS_FREE(ev_info, sizeof(*ev_info));
4038 pbuf = LNET_PING_INFO_TO_BUFFER(event->md_start);
4039 lnet_ping_buffer_decref(pbuf);
4044 lnet_rsp_tracker_create(void)
4046 struct list_head **rstqs;
4047 rstqs = lnet_create_array_of_queues();
4052 the_lnet.ln_mt_rstq = rstqs;
4058 lnet_rsp_tracker_clean(void)
4060 lnet_finalize_expired_responses();
4062 cfs_percpt_free(the_lnet.ln_mt_rstq);
4063 the_lnet.ln_mt_rstq = NULL;
4066 int lnet_monitor_thr_start(void)
4069 struct task_struct *task;
4071 if (the_lnet.ln_mt_state != LNET_MT_STATE_SHUTDOWN)
4074 rc = lnet_resendqs_create();
4078 rc = lnet_rsp_tracker_create();
4082 sema_init(&the_lnet.ln_mt_signal, 0);
4084 lnet_net_lock(LNET_LOCK_EX);
4085 the_lnet.ln_mt_state = LNET_MT_STATE_RUNNING;
4086 lnet_net_unlock(LNET_LOCK_EX);
4087 task = kthread_run(lnet_monitor_thread, NULL, "monitor_thread");
4090 CERROR("Can't start monitor thread: %d\n", rc);
4097 lnet_net_lock(LNET_LOCK_EX);
4098 the_lnet.ln_mt_state = LNET_MT_STATE_STOPPING;
4099 lnet_net_unlock(LNET_LOCK_EX);
4100 /* block until event callback signals exit */
4101 down(&the_lnet.ln_mt_signal);
4103 lnet_net_lock(LNET_LOCK_EX);
4104 the_lnet.ln_mt_state = LNET_MT_STATE_SHUTDOWN;
4105 lnet_net_unlock(LNET_LOCK_EX);
4106 lnet_rsp_tracker_clean();
4107 lnet_clean_local_ni_recoveryq();
4108 lnet_clean_peer_ni_recoveryq();
4109 lnet_clean_resendqs();
4110 the_lnet.ln_mt_handler = NULL;
4113 lnet_rsp_tracker_clean();
4114 lnet_clean_local_ni_recoveryq();
4115 lnet_clean_peer_ni_recoveryq();
4116 lnet_clean_resendqs();
4120 void lnet_monitor_thr_stop(void)
4122 if (the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN)
4125 LASSERT(the_lnet.ln_mt_state == LNET_MT_STATE_RUNNING);
4126 lnet_net_lock(LNET_LOCK_EX);
4127 the_lnet.ln_mt_state = LNET_MT_STATE_STOPPING;
4128 lnet_net_unlock(LNET_LOCK_EX);
4130 /* tell the monitor thread that we're shutting down */
4131 complete(&the_lnet.ln_mt_wait_complete);
4133 /* block until monitor thread signals that it's done */
4134 down(&the_lnet.ln_mt_signal);
4135 LASSERT(the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN);
4137 /* perform cleanup tasks */
4138 lnet_rsp_tracker_clean();
4139 lnet_clean_local_ni_recoveryq();
4140 lnet_clean_peer_ni_recoveryq();
4141 lnet_clean_resendqs();
4145 lnet_drop_message(struct lnet_ni *ni, int cpt, void *private, unsigned int nob,
4149 lnet_incr_stats(&ni->ni_stats, msg_type, LNET_STATS_TYPE_DROP);
4150 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
4151 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length += nob;
4152 lnet_net_unlock(cpt);
4154 lnet_ni_recv(ni, private, NULL, 0, 0, 0, nob);
4158 lnet_recv_put(struct lnet_ni *ni, struct lnet_msg *msg)
4160 struct lnet_hdr *hdr = &msg->msg_hdr;
4162 if (msg->msg_wanted != 0)
4163 lnet_setpayloadbuffer(msg);
4165 lnet_build_msg_event(msg, LNET_EVENT_PUT);
4167 /* Must I ACK? If so I'll grab the ack_wmd out of the header and put
4168 * it back into the ACK during lnet_finalize() */
4169 msg->msg_ack = (!lnet_is_wire_handle_none(&hdr->msg.put.ack_wmd) &&
4170 (msg->msg_md->md_options & LNET_MD_ACK_DISABLE) == 0);
4172 lnet_ni_recv(ni, msg->msg_private, msg, msg->msg_rx_delayed,
4173 msg->msg_offset, msg->msg_wanted, hdr->payload_length);
4177 lnet_parse_put(struct lnet_ni *ni, struct lnet_msg *msg)
4179 struct lnet_hdr *hdr = &msg->msg_hdr;
4180 struct lnet_match_info info;
4184 /* Convert put fields to host byte order */
4185 hdr->msg.put.match_bits = le64_to_cpu(hdr->msg.put.match_bits);
4186 hdr->msg.put.ptl_index = le32_to_cpu(hdr->msg.put.ptl_index);
4187 hdr->msg.put.offset = le32_to_cpu(hdr->msg.put.offset);
4189 /* Primary peer NID. */
4190 info.mi_id.nid = msg->msg_initiator;
4191 info.mi_id.pid = hdr->src_pid;
4192 info.mi_opc = LNET_MD_OP_PUT;
4193 info.mi_portal = hdr->msg.put.ptl_index;
4194 info.mi_rlength = hdr->payload_length;
4195 info.mi_roffset = hdr->msg.put.offset;
4196 info.mi_mbits = hdr->msg.put.match_bits;
4197 info.mi_cpt = lnet_nid2cpt(&msg->msg_initiator, ni);
4199 msg->msg_rx_ready_delay = ni->ni_net->net_lnd->lnd_eager_recv == NULL;
4200 ready_delay = msg->msg_rx_ready_delay;
4203 rc = lnet_ptl_match_md(&info, msg);
4208 case LNET_MATCHMD_OK:
4209 lnet_recv_put(ni, msg);
4212 case LNET_MATCHMD_NONE:
4214 /* no eager_recv or has already called it, should
4215 * have been attached on delayed list */
4218 rc = lnet_ni_eager_recv(ni, msg);
4225 case LNET_MATCHMD_DROP:
4226 CNETERR("Dropping PUT from %s portal %d match %llu"
4227 " offset %d length %d: %d\n",
4228 libcfs_idstr(&info.mi_id), info.mi_portal,
4229 info.mi_mbits, info.mi_roffset, info.mi_rlength, rc);
4231 return -ENOENT; /* -ve: OK but no match */
4236 lnet_parse_get(struct lnet_ni *ni, struct lnet_msg *msg, int rdma_get)
4238 struct lnet_match_info info;
4239 struct lnet_hdr *hdr = &msg->msg_hdr;
4240 struct lnet_processid source_id;
4241 struct lnet_handle_wire reply_wmd;
4244 /* Convert get fields to host byte order */
4245 hdr->msg.get.match_bits = le64_to_cpu(hdr->msg.get.match_bits);
4246 hdr->msg.get.ptl_index = le32_to_cpu(hdr->msg.get.ptl_index);
4247 hdr->msg.get.sink_length = le32_to_cpu(hdr->msg.get.sink_length);
4248 hdr->msg.get.src_offset = le32_to_cpu(hdr->msg.get.src_offset);
4250 source_id.nid = hdr->src_nid;
4251 source_id.pid = hdr->src_pid;
4252 /* Primary peer NID */
4253 info.mi_id.nid = msg->msg_initiator;
4254 info.mi_id.pid = hdr->src_pid;
4255 info.mi_opc = LNET_MD_OP_GET;
4256 info.mi_portal = hdr->msg.get.ptl_index;
4257 info.mi_rlength = hdr->msg.get.sink_length;
4258 info.mi_roffset = hdr->msg.get.src_offset;
4259 info.mi_mbits = hdr->msg.get.match_bits;
4260 info.mi_cpt = lnet_nid2cpt(&msg->msg_initiator, ni);
4262 rc = lnet_ptl_match_md(&info, msg);
4263 if (rc == LNET_MATCHMD_DROP) {
4264 CNETERR("Dropping GET from %s portal %d match %llu"
4265 " offset %d length %d\n",
4266 libcfs_idstr(&info.mi_id), info.mi_portal,
4267 info.mi_mbits, info.mi_roffset, info.mi_rlength);
4268 return -ENOENT; /* -ve: OK but no match */
4271 LASSERT(rc == LNET_MATCHMD_OK);
4273 lnet_build_msg_event(msg, LNET_EVENT_GET);
4275 reply_wmd = hdr->msg.get.return_wmd;
4277 lnet_prep_send(msg, LNET_MSG_REPLY, &source_id,
4278 msg->msg_offset, msg->msg_wanted);
4280 msg->msg_hdr.msg.reply.dst_wmd = reply_wmd;
4283 /* The LND completes the REPLY from her recv procedure */
4284 lnet_ni_recv(ni, msg->msg_private, msg, 0,
4285 msg->msg_offset, msg->msg_len, msg->msg_len);
4289 lnet_ni_recv(ni, msg->msg_private, NULL, 0, 0, 0, 0);
4290 msg->msg_receiving = 0;
4292 rc = lnet_send(&ni->ni_nid, msg, &msg->msg_from);
4294 /* didn't get as far as lnet_ni_send() */
4295 CERROR("%s: Unable to send REPLY for GET from %s: %d\n",
4296 libcfs_nidstr(&ni->ni_nid),
4297 libcfs_idstr(&info.mi_id), rc);
4299 lnet_finalize(msg, rc);
4306 lnet_parse_reply(struct lnet_ni *ni, struct lnet_msg *msg)
4308 void *private = msg->msg_private;
4309 struct lnet_hdr *hdr = &msg->msg_hdr;
4310 struct lnet_processid src = {};
4311 struct lnet_libmd *md;
4312 unsigned int rlength;
4313 unsigned int mlength;
4316 cpt = lnet_cpt_of_cookie(hdr->msg.reply.dst_wmd.wh_object_cookie);
4319 src.nid = hdr->src_nid;
4320 src.pid = hdr->src_pid;
4322 /* NB handles only looked up by creator (no flips) */
4323 md = lnet_wire_handle2md(&hdr->msg.reply.dst_wmd);
4324 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
4325 CNETERR("%s: Dropping REPLY from %s for %s "
4327 libcfs_nidstr(&ni->ni_nid), libcfs_idstr(&src),
4328 (md == NULL) ? "invalid" : "inactive",
4329 hdr->msg.reply.dst_wmd.wh_interface_cookie,
4330 hdr->msg.reply.dst_wmd.wh_object_cookie);
4331 if (md != NULL && md->md_me != NULL)
4332 CERROR("REPLY MD also attached to portal %d\n",
4333 md->md_me->me_portal);
4335 lnet_res_unlock(cpt);
4336 return -ENOENT; /* -ve: OK but no match */
4339 LASSERT(md->md_offset == 0);
4341 rlength = hdr->payload_length;
4342 mlength = min(rlength, md->md_length);
4344 if (mlength < rlength &&
4345 (md->md_options & LNET_MD_TRUNCATE) == 0) {
4346 CNETERR("%s: Dropping REPLY from %s length %d "
4347 "for MD %#llx would overflow (%d)\n",
4348 libcfs_nidstr(&ni->ni_nid), libcfs_idstr(&src),
4349 rlength, hdr->msg.reply.dst_wmd.wh_object_cookie,
4351 lnet_res_unlock(cpt);
4352 return -ENOENT; /* -ve: OK but no match */
4355 CDEBUG(D_NET, "%s: Reply from %s of length %d/%d into md %#llx\n",
4356 libcfs_nidstr(&ni->ni_nid), libcfs_idstr(&src),
4357 mlength, rlength, hdr->msg.reply.dst_wmd.wh_object_cookie);
4359 lnet_msg_attach_md(msg, md, 0, mlength);
4362 lnet_setpayloadbuffer(msg);
4364 lnet_res_unlock(cpt);
4366 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
4368 lnet_ni_recv(ni, private, msg, 0, 0, mlength, rlength);
4373 lnet_parse_ack(struct lnet_ni *ni, struct lnet_msg *msg)
4375 struct lnet_hdr *hdr = &msg->msg_hdr;
4376 struct lnet_processid src = {};
4377 struct lnet_libmd *md;
4380 src.nid = hdr->src_nid;
4381 src.pid = hdr->src_pid;
4383 /* Convert ack fields to host byte order */
4384 hdr->msg.ack.match_bits = le64_to_cpu(hdr->msg.ack.match_bits);
4385 hdr->msg.ack.mlength = le32_to_cpu(hdr->msg.ack.mlength);
4387 cpt = lnet_cpt_of_cookie(hdr->msg.ack.dst_wmd.wh_object_cookie);
4390 /* NB handles only looked up by creator (no flips) */
4391 md = lnet_wire_handle2md(&hdr->msg.ack.dst_wmd);
4392 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
4393 /* Don't moan; this is expected */
4395 "%s: Dropping ACK from %s to %s MD %#llx.%#llx\n",
4396 libcfs_nidstr(&ni->ni_nid), libcfs_idstr(&src),
4397 (md == NULL) ? "invalid" : "inactive",
4398 hdr->msg.ack.dst_wmd.wh_interface_cookie,
4399 hdr->msg.ack.dst_wmd.wh_object_cookie);
4400 if (md != NULL && md->md_me != NULL)
4401 CERROR("Source MD also attached to portal %d\n",
4402 md->md_me->me_portal);
4404 lnet_res_unlock(cpt);
4405 return -ENOENT; /* -ve! */
4408 CDEBUG(D_NET, "%s: ACK from %s into md %#llx\n",
4409 libcfs_nidstr(&ni->ni_nid), libcfs_idstr(&src),
4410 hdr->msg.ack.dst_wmd.wh_object_cookie);
4412 lnet_msg_attach_md(msg, md, 0, 0);
4414 lnet_res_unlock(cpt);
4416 lnet_build_msg_event(msg, LNET_EVENT_ACK);
4418 lnet_ni_recv(ni, msg->msg_private, msg, 0, 0, 0, msg->msg_len);
4423 * \retval LNET_CREDIT_OK If \a msg is forwarded
4424 * \retval LNET_CREDIT_WAIT If \a msg is blocked because w/o buffer
4425 * \retval -ve error code
4428 lnet_parse_forward_locked(struct lnet_ni *ni, struct lnet_msg *msg)
4432 if (!the_lnet.ln_routing)
4435 if (msg->msg_rxpeer->lpni_rtrcredits <= 0 ||
4436 lnet_msg2bufpool(msg)->rbp_credits <= 0) {
4437 if (ni->ni_net->net_lnd->lnd_eager_recv == NULL) {
4438 msg->msg_rx_ready_delay = 1;
4440 lnet_net_unlock(msg->msg_rx_cpt);
4441 rc = lnet_ni_eager_recv(ni, msg);
4442 lnet_net_lock(msg->msg_rx_cpt);
4447 rc = lnet_post_routed_recv_locked(msg, 0);
4452 lnet_parse_local(struct lnet_ni *ni, struct lnet_msg *msg)
4456 switch (msg->msg_type) {
4458 rc = lnet_parse_ack(ni, msg);
4461 rc = lnet_parse_put(ni, msg);
4464 rc = lnet_parse_get(ni, msg, msg->msg_rdma_get);
4466 case LNET_MSG_REPLY:
4467 rc = lnet_parse_reply(ni, msg);
4469 default: /* prevent an unused label if !kernel */
4474 LASSERT(rc == 0 || rc == -ENOENT);
4479 lnet_msgtyp2str (int type)
4488 case LNET_MSG_REPLY:
4490 case LNET_MSG_HELLO:
4493 return ("<UNKNOWN>");
4496 EXPORT_SYMBOL(lnet_msgtyp2str);
4499 lnet_parse(struct lnet_ni *ni, struct lnet_hdr *hdr, lnet_nid_t from_nid4,
4500 void *private, int rdma_req)
4502 struct lnet_peer_ni *lpni;
4503 struct lnet_msg *msg;
4504 __u32 payload_length;
4505 lnet_pid_t dest_pid;
4506 lnet_nid_t dest_nid;
4508 struct lnet_nid from_nid;
4515 LASSERT (!in_interrupt ());
4517 lnet_nid4_to_nid(from_nid4, &from_nid);
4520 src_nid = lnet_nid_to_nid4(&hdr->src_nid);
4521 dest_nid = lnet_nid_to_nid4(&hdr->dest_nid);
4522 dest_pid = hdr->dest_pid;
4523 payload_length = hdr->payload_length;
4525 /* FIXME handle large-addr nids */
4526 for_me = (lnet_nid_to_nid4(&ni->ni_nid) == dest_nid);
4527 cpt = lnet_cpt_of_nid(from_nid4, ni);
4529 CDEBUG(D_NET, "TRACE: %s(%s) <- %s : %s - %s\n",
4530 libcfs_nid2str(dest_nid),
4531 libcfs_nidstr(&ni->ni_nid),
4532 libcfs_nid2str(src_nid),
4533 lnet_msgtyp2str(type),
4534 (for_me) ? "for me" : "routed");
4539 if (payload_length > 0) {
4540 CERROR("%s, src %s: bad %s payload %d (0 expected)\n",
4541 libcfs_nid2str(from_nid4),
4542 libcfs_nid2str(src_nid),
4543 lnet_msgtyp2str(type), payload_length);
4549 case LNET_MSG_REPLY:
4550 if (payload_length >
4551 (__u32)(for_me ? LNET_MAX_PAYLOAD : LNET_MTU)) {
4552 CERROR("%s, src %s: bad %s payload %d "
4553 "(%d max expected)\n",
4554 libcfs_nid2str(from_nid4),
4555 libcfs_nid2str(src_nid),
4556 lnet_msgtyp2str(type),
4558 for_me ? LNET_MAX_PAYLOAD : LNET_MTU);
4564 CERROR("%s, src %s: Bad message type 0x%x\n",
4565 libcfs_nid2str(from_nid4),
4566 libcfs_nid2str(src_nid), type);
4570 if (the_lnet.ln_routing &&
4571 ni->ni_net->net_last_alive != ktime_get_real_seconds()) {
4573 spin_lock(&ni->ni_net->net_lock);
4574 ni->ni_net->net_last_alive = ktime_get_real_seconds();
4575 spin_unlock(&ni->ni_net->net_lock);
4576 push = lnet_ni_set_status_locked(ni, LNET_NI_STATUS_UP);
4581 lnet_push_update_to_peers(1);
4583 /* Regard a bad destination NID as a protocol error. Senders should
4584 * know what they're doing; if they don't they're misconfigured, buggy
4585 * or malicious so we chop them off at the knees :) */
4588 if (LNET_NIDNET(dest_nid) == LNET_NID_NET(&ni->ni_nid)) {
4589 /* should have gone direct */
4590 CERROR("%s, src %s: Bad dest nid %s "
4591 "(should have been sent direct)\n",
4592 libcfs_nid2str(from_nid4),
4593 libcfs_nid2str(src_nid),
4594 libcfs_nid2str(dest_nid));
4598 if (lnet_islocalnid4(dest_nid)) {
4599 /* dest is another local NI; sender should have used
4600 * this node's NID on its own network */
4601 CERROR("%s, src %s: Bad dest nid %s "
4602 "(it's my nid but on a different network)\n",
4603 libcfs_nid2str(from_nid4),
4604 libcfs_nid2str(src_nid),
4605 libcfs_nid2str(dest_nid));
4609 if (rdma_req && type == LNET_MSG_GET) {
4610 CERROR("%s, src %s: Bad optimized GET for %s "
4611 "(final destination must be me)\n",
4612 libcfs_nid2str(from_nid4),
4613 libcfs_nid2str(src_nid),
4614 libcfs_nid2str(dest_nid));
4618 if (!the_lnet.ln_routing) {
4619 CERROR("%s, src %s: Dropping message for %s "
4620 "(routing not enabled)\n",
4621 libcfs_nid2str(from_nid4),
4622 libcfs_nid2str(src_nid),
4623 libcfs_nid2str(dest_nid));
4628 /* Message looks OK; we're not going to return an error, so we MUST
4629 * call back lnd_recv() come what may... */
4631 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
4632 fail_peer(src_nid, 0)) { /* shall we now? */
4633 CERROR("%s, src %s: Dropping %s to simulate failure\n",
4634 libcfs_nid2str(from_nid4), libcfs_nid2str(src_nid),
4635 lnet_msgtyp2str(type));
4639 /* FIXME need to support large-addr nid */
4640 if (!list_empty(&the_lnet.ln_drop_rules) &&
4641 lnet_drop_rule_match(hdr, lnet_nid_to_nid4(&ni->ni_nid), NULL)) {
4643 "%s, src %s, dst %s: Dropping %s to simulate silent message loss\n",
4644 libcfs_nid2str(from_nid4), libcfs_nid2str(src_nid),
4645 libcfs_nid2str(dest_nid), lnet_msgtyp2str(type));
4649 msg = lnet_msg_alloc();
4651 CERROR("%s, src %s: Dropping %s (out of memory)\n",
4652 libcfs_nid2str(from_nid4), libcfs_nid2str(src_nid),
4653 lnet_msgtyp2str(type));
4657 /* msg zeroed in lnet_msg_alloc; i.e. flags all clear,
4658 * pointers NULL etc */
4660 msg->msg_type = type;
4661 msg->msg_private = private;
4662 msg->msg_receiving = 1;
4663 msg->msg_rdma_get = rdma_req;
4664 msg->msg_len = msg->msg_wanted = payload_length;
4665 msg->msg_offset = 0;
4666 msg->msg_hdr = *hdr;
4667 /* for building message event */
4668 msg->msg_from = from_nid;
4670 msg->msg_target.pid = dest_pid;
4671 lnet_nid4_to_nid(dest_nid, &msg->msg_target.nid);
4672 msg->msg_routing = 1;
4676 lpni = lnet_peerni_by_nid_locked(&from_nid, &ni->ni_nid, cpt);
4678 lnet_net_unlock(cpt);
4680 CERROR("%s, src %s: Dropping %s (error %d looking up sender)\n",
4681 libcfs_nid2str(from_nid4), libcfs_nid2str(src_nid),
4682 lnet_msgtyp2str(type), rc);
4684 if (rc == -ESHUTDOWN)
4685 /* We are shutting down. Don't do anything more */
4690 /* If this message was forwarded to us from a router then we may need
4691 * to update router aliveness or check for an asymmetrical route
4694 if (((lnet_drop_asym_route && for_me) ||
4695 !lpni->lpni_peer_net->lpn_peer->lp_alive) &&
4696 LNET_NIDNET(src_nid) != LNET_NIDNET(from_nid4)) {
4697 __u32 src_net_id = LNET_NIDNET(src_nid);
4698 struct lnet_peer *gw = lpni->lpni_peer_net->lpn_peer;
4699 struct lnet_route *route;
4702 list_for_each_entry(route, &gw->lp_routes, lr_gwlist) {
4703 if (route->lr_net == src_net_id) {
4705 /* If we're transitioning the gateway from
4706 * dead -> alive, and discovery is disabled
4707 * locally or on the gateway, then we need to
4708 * update the cached route aliveness for each
4709 * route to the src_nid's net.
4711 * Otherwise, we're only checking for
4712 * symmetrical route, and we can break the
4715 if (!gw->lp_alive &&
4716 lnet_is_discovery_disabled(gw))
4717 lnet_set_route_aliveness(route, true);
4722 if (lnet_drop_asym_route && for_me && !found) {
4723 /* Drop ref taken by lnet_nid2peerni_locked() */
4724 lnet_peer_ni_decref_locked(lpni);
4725 lnet_net_unlock(cpt);
4726 /* we would not use from_nid to route a message to
4728 * => asymmetric routing detected but forbidden
4730 CERROR("%s, src %s: Dropping asymmetrical route %s\n",
4731 libcfs_nid2str(from_nid4),
4732 libcfs_nid2str(src_nid), lnet_msgtyp2str(type));
4736 if (!gw->lp_alive) {
4737 struct lnet_peer_net *lpn;
4738 struct lnet_peer_ni *lpni2;
4740 gw->lp_alive = true;
4741 /* Mark all remote NIs on src_nid's net UP */
4742 lpn = lnet_peer_get_net_locked(gw, src_net_id);
4744 list_for_each_entry(lpni2, &lpn->lpn_peer_nis,
4746 lpni2->lpni_ns_status = LNET_NI_STATUS_UP;
4750 lpni->lpni_last_alive = ktime_get_seconds();
4752 msg->msg_rxpeer = lpni;
4754 lnet_ni_addref_locked(ni, cpt);
4755 /* Multi-Rail: Primary NID of source. */
4756 lnet_peer_primary_nid_locked(src_nid, &msg->msg_initiator);
4759 * mark the status of this lpni as UP since we received a message
4760 * from it. The ping response reports back the ns_status which is
4761 * marked on the remote as up or down and we cache it here.
4763 msg->msg_rxpeer->lpni_ns_status = LNET_NI_STATUS_UP;
4765 lnet_msg_commit(msg, cpt);
4767 /* message delay simulation */
4768 if (unlikely(!list_empty(&the_lnet.ln_delay_rules) &&
4769 lnet_delay_rule_match_locked(hdr, msg))) {
4770 lnet_net_unlock(cpt);
4775 rc = lnet_parse_forward_locked(ni, msg);
4776 lnet_net_unlock(cpt);
4781 if (rc == LNET_CREDIT_OK) {
4782 lnet_ni_recv(ni, msg->msg_private, msg, 0,
4783 0, payload_length, payload_length);
4788 lnet_net_unlock(cpt);
4790 rc = lnet_parse_local(ni, msg);
4796 LASSERT(msg->msg_md == NULL);
4797 lnet_finalize(msg, rc);
4800 lnet_drop_message(ni, cpt, private, payload_length, type);
4803 EXPORT_SYMBOL(lnet_parse);
4806 lnet_drop_delayed_msg_list(struct list_head *head, char *reason)
4808 while (!list_empty(head)) {
4809 struct lnet_processid id = {};
4810 struct lnet_msg *msg;
4812 msg = list_entry(head->next, struct lnet_msg, msg_list);
4813 list_del(&msg->msg_list);
4815 id.nid = msg->msg_hdr.src_nid;
4816 id.pid = msg->msg_hdr.src_pid;
4818 LASSERT(msg->msg_md == NULL);
4819 LASSERT(msg->msg_rx_delayed);
4820 LASSERT(msg->msg_rxpeer != NULL);
4821 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
4823 CWARN("Dropping delayed PUT from %s portal %d match %llu"
4824 " offset %d length %d: %s\n",
4826 msg->msg_hdr.msg.put.ptl_index,
4827 msg->msg_hdr.msg.put.match_bits,
4828 msg->msg_hdr.msg.put.offset,
4829 msg->msg_hdr.payload_length, reason);
4831 /* NB I can't drop msg's ref on msg_rxpeer until after I've
4832 * called lnet_drop_message(), so I just hang onto msg as well
4833 * until that's done */
4835 lnet_drop_message(msg->msg_rxni, msg->msg_rx_cpt,
4836 msg->msg_private, msg->msg_len,
4839 msg->msg_no_resend = true;
4841 * NB: message will not generate event because w/o attached MD,
4842 * but we still should give error code so lnet_msg_decommit()
4843 * can skip counters operations and other checks.
4845 lnet_finalize(msg, -ENOENT);
4850 lnet_recv_delayed_msg_list(struct list_head *head)
4852 while (!list_empty(head)) {
4853 struct lnet_msg *msg;
4854 struct lnet_processid id;
4856 msg = list_entry(head->next, struct lnet_msg, msg_list);
4857 list_del(&msg->msg_list);
4859 /* md won't disappear under me, since each msg
4860 * holds a ref on it */
4862 id.nid = msg->msg_hdr.src_nid;
4863 id.pid = msg->msg_hdr.src_pid;
4865 LASSERT(msg->msg_rx_delayed);
4866 LASSERT(msg->msg_md != NULL);
4867 LASSERT(msg->msg_rxpeer != NULL);
4868 LASSERT(msg->msg_rxni != NULL);
4869 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
4871 CDEBUG(D_NET, "Resuming delayed PUT from %s portal %d "
4872 "match %llu offset %d length %d.\n",
4873 libcfs_idstr(&id), msg->msg_hdr.msg.put.ptl_index,
4874 msg->msg_hdr.msg.put.match_bits,
4875 msg->msg_hdr.msg.put.offset,
4876 msg->msg_hdr.payload_length);
4878 lnet_recv_put(msg->msg_rxni, msg);
4883 lnet_attach_rsp_tracker(struct lnet_rsp_tracker *rspt, int cpt,
4884 struct lnet_libmd *md, struct lnet_handle_md mdh)
4887 struct lnet_rsp_tracker *local_rspt;
4890 * MD has a refcount taken by message so it's not going away.
4891 * The MD however can be looked up. We need to secure the access
4892 * to the md_rspt_ptr by taking the res_lock.
4893 * The rspt can be accessed without protection up to when it gets
4894 * added to the list.
4898 local_rspt = md->md_rspt_ptr;
4899 timeout_ns = lnet_transaction_timeout * NSEC_PER_SEC;
4900 if (local_rspt != NULL) {
4902 * we already have an rspt attached to the md, so we'll
4903 * update the deadline on that one.
4905 lnet_rspt_free(rspt, cpt);
4908 rspt->rspt_mdh = mdh;
4909 rspt->rspt_cpt = cpt;
4910 /* store the rspt so we can access it when we get the REPLY */
4911 md->md_rspt_ptr = rspt;
4914 local_rspt->rspt_deadline = ktime_add_ns(ktime_get(), timeout_ns);
4917 * add to the list of tracked responses. It's added to tail of the
4918 * list in order to expire all the older entries first.
4921 list_move_tail(&local_rspt->rspt_on_list, the_lnet.ln_mt_rstq[cpt]);
4922 lnet_net_unlock(cpt);
4923 lnet_res_unlock(cpt);
4927 * Initiate an asynchronous PUT operation.
4929 * There are several events associated with a PUT: completion of the send on
4930 * the initiator node (LNET_EVENT_SEND), and when the send completes
4931 * successfully, the receipt of an acknowledgment (LNET_EVENT_ACK) indicating
4932 * that the operation was accepted by the target. The event LNET_EVENT_PUT is
4933 * used at the target node to indicate the completion of incoming data
4936 * The local events will be logged in the EQ associated with the MD pointed to
4937 * by \a mdh handle. Using a MD without an associated EQ results in these
4938 * events being discarded. In this case, the caller must have another
4939 * mechanism (e.g., a higher level protocol) for determining when it is safe
4940 * to modify the memory region associated with the MD.
4942 * Note that LNet does not guarantee the order of LNET_EVENT_SEND and
4943 * LNET_EVENT_ACK, though intuitively ACK should happen after SEND.
4945 * \param self Indicates the NID of a local interface through which to send
4946 * the PUT request. Use LNET_NID_ANY to let LNet choose one by itself.
4947 * \param mdh A handle for the MD that describes the memory to be sent. The MD
4948 * must be "free floating" (See LNetMDBind()).
4949 * \param ack Controls whether an acknowledgment is requested.
4950 * Acknowledgments are only sent when they are requested by the initiating
4951 * process and the target MD enables them.
4952 * \param target A process identifier for the target process.
4953 * \param portal The index in the \a target's portal table.
4954 * \param match_bits The match bits to use for MD selection at the target
4956 * \param offset The offset into the target MD (only used when the target
4957 * MD has the LNET_MD_MANAGE_REMOTE option set).
4958 * \param hdr_data 64 bits of user data that can be included in the message
4959 * header. This data is written to an event queue entry at the target if an
4960 * EQ is present on the matching MD.
4962 * \retval 0 Success, and only in this case events will be generated
4963 * and logged to EQ (if it exists).
4964 * \retval -EIO Simulated failure.
4965 * \retval -ENOMEM Memory allocation failure.
4966 * \retval -ENOENT Invalid MD object.
4968 * \see struct lnet_event::hdr_data and lnet_event_kind_t.
4971 LNetPut(lnet_nid_t self4, struct lnet_handle_md mdh, enum lnet_ack_req ack,
4972 struct lnet_process_id target4, unsigned int portal,
4973 __u64 match_bits, unsigned int offset,
4976 struct lnet_msg *msg;
4977 struct lnet_libmd *md;
4980 struct lnet_processid target;
4981 struct lnet_rsp_tracker *rspt = NULL;
4982 struct lnet_nid self;
4984 LASSERT(the_lnet.ln_refcount > 0);
4986 lnet_nid4_to_nid(self4, &self);
4987 lnet_nid4_to_nid(target4.nid, &target.nid);
4988 target.pid = target4.pid;
4990 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
4991 fail_peer(target4.nid, 1)) { /* shall we now? */
4992 CERROR("Dropping PUT to %s: simulated failure\n",
4993 libcfs_id2str(target4));
4997 msg = lnet_msg_alloc();
4999 CERROR("Dropping PUT to %s: ENOMEM on struct lnet_msg\n",
5000 libcfs_id2str(target4));
5003 msg->msg_vmflush = !!(current->flags & PF_MEMALLOC);
5005 cpt = lnet_cpt_of_cookie(mdh.cookie);
5007 if (ack == LNET_ACK_REQ) {
5008 rspt = lnet_rspt_alloc(cpt);
5010 CERROR("Dropping PUT to %s: ENOMEM on response tracker\n",
5011 libcfs_id2str(target4));
5014 INIT_LIST_HEAD(&rspt->rspt_on_list);
5019 md = lnet_handle2md(&mdh);
5020 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
5021 CERROR("Dropping PUT (%llu:%d:%s): MD (%d) invalid\n",
5022 match_bits, portal, libcfs_id2str(target4),
5023 md == NULL ? -1 : md->md_threshold);
5024 if (md != NULL && md->md_me != NULL)
5025 CERROR("Source MD also attached to portal %d\n",
5026 md->md_me->me_portal);
5027 lnet_res_unlock(cpt);
5030 lnet_rspt_free(rspt, cpt);
5036 CDEBUG(D_NET, "%s -> %s\n", __func__, libcfs_id2str(target4));
5038 lnet_msg_attach_md(msg, md, 0, 0);
5040 lnet_prep_send(msg, LNET_MSG_PUT, &target, 0, md->md_length);
5042 msg->msg_hdr.msg.put.match_bits = cpu_to_le64(match_bits);
5043 msg->msg_hdr.msg.put.ptl_index = cpu_to_le32(portal);
5044 msg->msg_hdr.msg.put.offset = cpu_to_le32(offset);
5045 msg->msg_hdr.msg.put.hdr_data = hdr_data;
5047 /* NB handles only looked up by creator (no flips) */
5048 if (ack == LNET_ACK_REQ) {
5049 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
5050 the_lnet.ln_interface_cookie;
5051 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
5052 md->md_lh.lh_cookie;
5054 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
5055 LNET_WIRE_HANDLE_COOKIE_NONE;
5056 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
5057 LNET_WIRE_HANDLE_COOKIE_NONE;
5060 lnet_res_unlock(cpt);
5062 lnet_build_msg_event(msg, LNET_EVENT_SEND);
5064 if (rspt && lnet_response_tracking_enabled(LNET_MSG_PUT,
5066 lnet_attach_rsp_tracker(rspt, cpt, md, mdh);
5068 lnet_rspt_free(rspt, cpt);
5070 if (CFS_FAIL_CHECK_ORSET(CFS_FAIL_PTLRPC_OST_BULK_CB2,
5074 rc = lnet_send(&self, msg, NULL);
5077 CNETERR("Error sending PUT to %s: %d\n",
5078 libcfs_id2str(target4), rc);
5079 msg->msg_no_resend = true;
5080 lnet_finalize(msg, rc);
5083 /* completion will be signalled by an event */
5086 EXPORT_SYMBOL(LNetPut);
5089 * The LND can DMA direct to the GET md (i.e. no REPLY msg). This
5090 * returns a msg for the LND to pass to lnet_finalize() when the sink
5091 * data has been received.
5093 * CAVEAT EMPTOR: 'getmsg' is the original GET, which is freed when
5094 * lnet_finalize() is called on it, so the LND must call this first
5097 lnet_create_reply_msg(struct lnet_ni *ni, struct lnet_msg *getmsg)
5099 struct lnet_msg *msg = lnet_msg_alloc();
5100 struct lnet_libmd *getmd = getmsg->msg_md;
5101 struct lnet_processid *peer_id = &getmsg->msg_target;
5104 LASSERT(!getmsg->msg_target_is_router);
5105 LASSERT(!getmsg->msg_routing);
5108 CERROR("%s: Dropping REPLY from %s: can't allocate msg\n",
5109 libcfs_nidstr(&ni->ni_nid), libcfs_idstr(peer_id));
5113 cpt = lnet_cpt_of_cookie(getmd->md_lh.lh_cookie);
5116 LASSERT(getmd->md_refcount > 0);
5118 if (getmd->md_threshold == 0) {
5119 CERROR("%s: Dropping REPLY from %s for inactive MD %p\n",
5120 libcfs_nidstr(&ni->ni_nid), libcfs_idstr(peer_id),
5122 lnet_res_unlock(cpt);
5126 LASSERT(getmd->md_offset == 0);
5128 CDEBUG(D_NET, "%s: Reply from %s md %p\n",
5129 libcfs_nidstr(&ni->ni_nid), libcfs_idstr(peer_id), getmd);
5131 /* setup information for lnet_build_msg_event */
5132 msg->msg_initiator =
5133 getmsg->msg_txpeer->lpni_peer_net->lpn_peer->lp_primary_nid;
5134 msg->msg_from = peer_id->nid;
5135 msg->msg_type = LNET_MSG_GET; /* flag this msg as an "optimized" GET */
5136 msg->msg_hdr.src_nid = peer_id->nid;
5137 msg->msg_hdr.payload_length = getmd->md_length;
5138 msg->msg_receiving = 1; /* required by lnet_msg_attach_md */
5140 lnet_msg_attach_md(msg, getmd, getmd->md_offset, getmd->md_length);
5141 lnet_res_unlock(cpt);
5143 cpt = lnet_nid2cpt(&peer_id->nid, ni);
5146 lnet_msg_commit(msg, cpt);
5147 lnet_net_unlock(cpt);
5149 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
5154 cpt = lnet_nid2cpt(&peer_id->nid, ni);
5157 lnet_incr_stats(&ni->ni_stats, LNET_MSG_GET, LNET_STATS_TYPE_DROP);
5158 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
5159 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length +=
5161 lnet_net_unlock(cpt);
5168 EXPORT_SYMBOL(lnet_create_reply_msg);
5171 lnet_set_reply_msg_len(struct lnet_ni *ni, struct lnet_msg *reply,
5174 /* Set the REPLY length, now the RDMA that elides the REPLY message has
5175 * completed and I know it. */
5176 LASSERT(reply != NULL);
5177 LASSERT(reply->msg_type == LNET_MSG_GET);
5178 LASSERT(reply->msg_ev.type == LNET_EVENT_REPLY);
5180 /* NB I trusted my peer to RDMA. If she tells me she's written beyond
5181 * the end of my buffer, I might as well be dead. */
5182 LASSERT(len <= reply->msg_ev.mlength);
5184 reply->msg_ev.mlength = len;
5186 EXPORT_SYMBOL(lnet_set_reply_msg_len);
5189 * Initiate an asynchronous GET operation.
5191 * On the initiator node, an LNET_EVENT_SEND is logged when the GET request
5192 * is sent, and an LNET_EVENT_REPLY is logged when the data returned from
5193 * the target node in the REPLY has been written to local MD.
5195 * On the target node, an LNET_EVENT_GET is logged when the GET request
5196 * arrives and is accepted into a MD.
5198 * \param self,target,portal,match_bits,offset See the discussion in LNetPut().
5199 * \param mdh A handle for the MD that describes the memory into which the
5200 * requested data will be received. The MD must be "free floating" (See LNetMDBind()).
5202 * \retval 0 Success, and only in this case events will be generated
5203 * and logged to EQ (if it exists) of the MD.
5204 * \retval -EIO Simulated failure.
5205 * \retval -ENOMEM Memory allocation failure.
5206 * \retval -ENOENT Invalid MD object.
5209 LNetGet(lnet_nid_t self4, struct lnet_handle_md mdh,
5210 struct lnet_process_id target4, unsigned int portal,
5211 __u64 match_bits, unsigned int offset, bool recovery)
5213 struct lnet_msg *msg;
5214 struct lnet_libmd *md;
5215 struct lnet_rsp_tracker *rspt;
5218 struct lnet_nid self;
5219 struct lnet_processid target;
5221 LASSERT(the_lnet.ln_refcount > 0);
5223 lnet_nid4_to_nid(self4, &self);
5224 lnet_nid4_to_nid(target4.nid, &target.nid);
5225 target.pid = target4.pid;
5227 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
5228 fail_peer(target4.nid, 1)) /* shall we now? */
5230 CERROR("Dropping GET to %s: simulated failure\n",
5231 libcfs_id2str(target4));
5235 msg = lnet_msg_alloc();
5237 CERROR("Dropping GET to %s: ENOMEM on struct lnet_msg\n",
5238 libcfs_id2str(target4));
5242 cpt = lnet_cpt_of_cookie(mdh.cookie);
5244 rspt = lnet_rspt_alloc(cpt);
5246 CERROR("Dropping GET to %s: ENOMEM on response tracker\n",
5247 libcfs_id2str(target4));
5250 INIT_LIST_HEAD(&rspt->rspt_on_list);
5252 msg->msg_recovery = recovery;
5256 md = lnet_handle2md(&mdh);
5257 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
5258 CERROR("Dropping GET (%llu:%d:%s): MD (%d) invalid\n",
5259 match_bits, portal, libcfs_id2str(target4),
5260 md == NULL ? -1 : md->md_threshold);
5261 if (md != NULL && md->md_me != NULL)
5262 CERROR("REPLY MD also attached to portal %d\n",
5263 md->md_me->me_portal);
5265 lnet_res_unlock(cpt);
5268 lnet_rspt_free(rspt, cpt);
5272 CDEBUG(D_NET, "%s -> %s\n", __func__, libcfs_id2str(target4));
5274 lnet_msg_attach_md(msg, md, 0, 0);
5276 lnet_prep_send(msg, LNET_MSG_GET, &target, 0, 0);
5278 msg->msg_hdr.msg.get.match_bits = cpu_to_le64(match_bits);
5279 msg->msg_hdr.msg.get.ptl_index = cpu_to_le32(portal);
5280 msg->msg_hdr.msg.get.src_offset = cpu_to_le32(offset);
5281 msg->msg_hdr.msg.get.sink_length = cpu_to_le32(md->md_length);
5283 /* NB handles only looked up by creator (no flips) */
5284 msg->msg_hdr.msg.get.return_wmd.wh_interface_cookie =
5285 the_lnet.ln_interface_cookie;
5286 msg->msg_hdr.msg.get.return_wmd.wh_object_cookie =
5287 md->md_lh.lh_cookie;
5289 lnet_res_unlock(cpt);
5291 lnet_build_msg_event(msg, LNET_EVENT_SEND);
5293 if (lnet_response_tracking_enabled(LNET_MSG_GET, md->md_options))
5294 lnet_attach_rsp_tracker(rspt, cpt, md, mdh);
5296 lnet_rspt_free(rspt, cpt);
5298 rc = lnet_send(&self, msg, NULL);
5300 CNETERR("Error sending GET to %s: %d\n",
5301 libcfs_id2str(target4), rc);
5302 msg->msg_no_resend = true;
5303 lnet_finalize(msg, rc);
5306 /* completion will be signalled by an event */
5309 EXPORT_SYMBOL(LNetGet);
5312 * Calculate distance to node at \a dstnid.
5314 * \param dstnid Target NID.
5315 * \param srcnidp If not NULL, NID of the local interface to reach \a dstnid
5317 * \param orderp If not NULL, order of the route to reach \a dstnid is saved
5320 * \retval 0 If \a dstnid belongs to a local interface, and reserved option
5321 * local_nid_dist_zero is set, which is the default.
5322 * \retval positives Distance to target NID, i.e. number of hops plus one.
5323 * \retval -EHOSTUNREACH If \a dstnid is not reachable.
5326 LNetDist(lnet_nid_t dstnid, lnet_nid_t *srcnidp, __u32 *orderp)
5328 struct list_head *e;
5329 struct lnet_ni *ni = NULL;
5330 struct lnet_remotenet *rnet;
5331 __u32 dstnet = LNET_NIDNET(dstnid);
5335 struct list_head *rn_list;
5336 bool matched_dstnet = false;
5338 /* if !local_nid_dist_zero, I don't return a distance of 0 ever
5339 * (when lustre sees a distance of 0, it substitutes 0@lo), so I
5340 * keep order 0 free for 0@lo and order 1 free for a local NID
5343 LASSERT(the_lnet.ln_refcount > 0);
5345 cpt = lnet_net_lock_current();
5347 while ((ni = lnet_get_next_ni_locked(NULL, ni))) {
5348 /* FIXME support large-addr nid */
5349 if (lnet_nid_to_nid4(&ni->ni_nid) == dstnid) {
5350 if (srcnidp != NULL)
5352 if (orderp != NULL) {
5353 if (dstnid == LNET_NID_LO_0)
5358 lnet_net_unlock(cpt);
5360 return local_nid_dist_zero ? 0 : 1;
5363 if (!matched_dstnet && LNET_NID_NET(&ni->ni_nid) == dstnet) {
5364 matched_dstnet = true;
5365 /* We matched the destination net, but we may have
5366 * additional local NIs to inspect.
5368 * We record the nid and order as appropriate, but
5369 * they may be overwritten if we match local NI above.
5372 /* FIXME support large-addr nids */
5373 *srcnidp = lnet_nid_to_nid4(&ni->ni_nid);
5376 /* Check if ni was originally created in
5377 * current net namespace.
5378 * If not, assign order above 0xffff0000,
5379 * to make this ni not a priority.
5381 if (current->nsproxy &&
5382 !net_eq(ni->ni_net_ns,
5383 current->nsproxy->net_ns))
5384 *orderp = order + 0xffff0000;
5393 if (matched_dstnet) {
5394 lnet_net_unlock(cpt);
5398 rn_list = lnet_net2rnethash(dstnet);
5399 list_for_each(e, rn_list) {
5400 rnet = list_entry(e, struct lnet_remotenet, lrn_list);
5402 if (rnet->lrn_net == dstnet) {
5403 struct lnet_route *route;
5404 struct lnet_route *shortest = NULL;
5405 __u32 shortest_hops = LNET_UNDEFINED_HOPS;
5408 LASSERT(!list_empty(&rnet->lrn_routes));
5410 list_for_each_entry(route, &rnet->lrn_routes,
5412 route_hops = route->lr_hops;
5413 if (route_hops == LNET_UNDEFINED_HOPS)
5415 if (shortest == NULL ||
5416 route_hops < shortest_hops) {
5418 shortest_hops = route_hops;
5422 LASSERT(shortest != NULL);
5423 hops = shortest_hops;
5424 if (srcnidp != NULL) {
5425 struct lnet_net *net;
5426 net = lnet_get_net_locked(shortest->lr_lnet);
5428 ni = lnet_get_next_ni_locked(net, NULL);
5429 /* FIXME support large-addr nids */
5430 *srcnidp = lnet_nid_to_nid4(&ni->ni_nid);
5434 lnet_net_unlock(cpt);
5440 lnet_net_unlock(cpt);
5441 return -EHOSTUNREACH;
5443 EXPORT_SYMBOL(LNetDist);