1 // SPDX-License-Identifier: GPL-2.0
3 /* Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
4 * Use is subject to license terms.
6 * Copyright (c) 2012, 2017, Intel Corporation.
9 /* This file is part of Lustre, http://www.lustre.org/
11 * Data movement routines
14 #define DEBUG_SUBSYSTEM S_LNET
16 #include <linux/pagemap.h>
18 #include <lnet/lib-lnet.h>
19 #include <linux/nsproxy.h>
20 #include <lnet/lnet_rdma.h>
21 #include <net/net_namespace.h>
23 static int local_nid_dist_zero = 1;
24 module_param(local_nid_dist_zero, int, 0444);
25 MODULE_PARM_DESC(local_nid_dist_zero, "Reserved");
27 struct lnet_send_data {
28 struct lnet_ni *sd_best_ni;
29 struct lnet_peer_ni *sd_best_lpni;
30 struct lnet_peer_ni *sd_final_dst_lpni;
31 struct lnet_peer *sd_peer;
32 struct lnet_peer *sd_gw_peer;
33 struct lnet_peer_ni *sd_gw_lpni;
34 struct lnet_peer_net *sd_peer_net;
35 struct lnet_msg *sd_msg;
36 struct lnet_nid sd_dst_nid;
37 struct lnet_nid sd_src_nid;
38 struct lnet_nid sd_rtr_nid;
45 lnet_msg_is_response(struct lnet_msg *msg)
47 return msg->msg_type == LNET_MSG_ACK || msg->msg_type == LNET_MSG_REPLY;
51 lnet_response_tracking_enabled(__u32 msg_type, unsigned int md_options)
53 if (md_options & LNET_MD_NO_TRACK_RESPONSE)
54 /* Explicitly disabled in MD options */
57 if (md_options & LNET_MD_TRACK_RESPONSE)
58 /* Explicity enabled in MD options */
61 if (lnet_response_tracking == 3)
62 /* Enabled for all message types */
65 if (msg_type == LNET_MSG_PUT)
66 return lnet_response_tracking == 2;
68 if (msg_type == LNET_MSG_GET)
69 return lnet_response_tracking == 1;
74 static inline struct lnet_comm_count *
75 get_stats_counts(struct lnet_element_stats *stats,
76 enum lnet_stats_type stats_type)
79 case LNET_STATS_TYPE_SEND:
80 return &stats->el_send_stats;
81 case LNET_STATS_TYPE_RECV:
82 return &stats->el_recv_stats;
83 case LNET_STATS_TYPE_DROP:
84 return &stats->el_drop_stats;
86 CERROR("Unknown stats type\n");
92 void lnet_incr_stats(struct lnet_element_stats *stats,
93 enum lnet_msg_type msg_type,
94 enum lnet_stats_type stats_type)
96 struct lnet_comm_count *counts = get_stats_counts(stats, stats_type);
102 atomic_inc(&counts->co_ack_count);
105 atomic_inc(&counts->co_put_count);
108 atomic_inc(&counts->co_get_count);
111 atomic_inc(&counts->co_reply_count);
114 atomic_inc(&counts->co_hello_count);
117 CERROR("There is a BUG in the code. Unknown message type\n");
122 __u32 lnet_sum_stats(struct lnet_element_stats *stats,
123 enum lnet_stats_type stats_type)
125 struct lnet_comm_count *counts = get_stats_counts(stats, stats_type);
129 return (atomic_read(&counts->co_ack_count) +
130 atomic_read(&counts->co_put_count) +
131 atomic_read(&counts->co_get_count) +
132 atomic_read(&counts->co_reply_count) +
133 atomic_read(&counts->co_hello_count));
136 static inline void assign_stats(struct lnet_ioctl_comm_count *msg_stats,
137 struct lnet_comm_count *counts)
139 msg_stats->ico_get_count = atomic_read(&counts->co_get_count);
140 msg_stats->ico_put_count = atomic_read(&counts->co_put_count);
141 msg_stats->ico_reply_count = atomic_read(&counts->co_reply_count);
142 msg_stats->ico_ack_count = atomic_read(&counts->co_ack_count);
143 msg_stats->ico_hello_count = atomic_read(&counts->co_hello_count);
146 void lnet_usr_translate_stats(struct lnet_ioctl_element_msg_stats *msg_stats,
147 struct lnet_element_stats *stats)
149 struct lnet_comm_count *counts;
154 counts = get_stats_counts(stats, LNET_STATS_TYPE_SEND);
157 assign_stats(&msg_stats->im_send_stats, counts);
159 counts = get_stats_counts(stats, LNET_STATS_TYPE_RECV);
162 assign_stats(&msg_stats->im_recv_stats, counts);
164 counts = get_stats_counts(stats, LNET_STATS_TYPE_DROP);
167 assign_stats(&msg_stats->im_drop_stats, counts);
171 lnet_fail_nid(lnet_nid_t nid4, unsigned int threshold)
173 struct lnet_test_peer *tp;
174 struct list_head *el;
175 struct list_head *next;
179 lnet_nid4_to_nid(nid4, &nid);
180 /* NB: use lnet_net_lock(0) to serialize operations on test peers */
181 if (threshold != 0) {
182 /* Adding a new entry */
183 LIBCFS_ALLOC(tp, sizeof(*tp));
188 tp->tp_threshold = threshold;
191 list_add_tail(&tp->tp_list, &the_lnet.ln_test_peers);
198 list_for_each_safe(el, next, &the_lnet.ln_test_peers) {
199 tp = list_entry(el, struct lnet_test_peer, tp_list);
201 if (tp->tp_threshold == 0 || /* needs culling anyway */
202 LNET_NID_IS_ANY(&nid) || /* removing all entries */
203 nid_same(&tp->tp_nid, &nid)) { /* matched this one */
204 list_move(&tp->tp_list, &cull);
210 while ((tp = list_first_entry_or_null(&cull,
211 struct lnet_test_peer,
213 list_del(&tp->tp_list);
214 LIBCFS_FREE(tp, sizeof(*tp));
220 fail_peer(struct lnet_nid *nid, int outgoing)
222 struct lnet_test_peer *tp;
223 struct list_head *el;
224 struct list_head *next;
228 /* NB: use lnet_net_lock(0) to serialize operations on test peers */
231 list_for_each_safe(el, next, &the_lnet.ln_test_peers) {
232 tp = list_entry(el, struct lnet_test_peer, tp_list);
234 if (tp->tp_threshold == 0) {
237 /* only cull zombies on outgoing tests,
238 * since we may be at interrupt priority on
239 * incoming messages. */
240 list_move(&tp->tp_list, &cull);
245 if (LNET_NID_IS_ANY(&tp->tp_nid) || /* fail every peer */
246 nid_same(nid, &tp->tp_nid)) { /* fail this peer */
249 if (tp->tp_threshold != LNET_MD_THRESH_INF) {
252 tp->tp_threshold == 0) {
254 list_move(&tp->tp_list, &cull);
263 while ((tp = list_first_entry_or_null(&cull,
264 struct lnet_test_peer,
266 list_del(&tp->tp_list);
267 LIBCFS_FREE(tp, sizeof(*tp));
274 lnet_iov_nob(unsigned int niov, struct kvec *iov)
276 unsigned int nob = 0;
278 LASSERT(niov == 0 || iov != NULL);
280 nob += (iov++)->iov_len;
284 EXPORT_SYMBOL(lnet_iov_nob);
287 lnet_copy_iov2iov(unsigned int ndiov, struct kvec *diov, unsigned int doffset,
288 unsigned int nsiov, struct kvec *siov, unsigned int soffset,
291 /* NB diov, siov are READ-ONLY */
292 unsigned int this_nob;
297 /* skip complete frags before 'doffset' */
299 while (doffset >= diov->iov_len) {
300 doffset -= diov->iov_len;
306 /* skip complete frags before 'soffset' */
308 while (soffset >= siov->iov_len) {
309 soffset -= siov->iov_len;
318 this_nob = min3((unsigned int)diov->iov_len - doffset,
319 (unsigned int)siov->iov_len - soffset,
322 memcpy((char *)diov->iov_base + doffset,
323 (char *)siov->iov_base + soffset, this_nob);
326 if (diov->iov_len > doffset + this_nob) {
334 if (siov->iov_len > soffset + this_nob) {
343 EXPORT_SYMBOL(lnet_copy_iov2iov);
346 lnet_kiov_nob(unsigned int niov, struct bio_vec *kiov)
348 unsigned int nob = 0;
350 LASSERT(niov == 0 || kiov != NULL);
352 nob += (kiov++)->bv_len;
356 EXPORT_SYMBOL(lnet_kiov_nob);
359 lnet_copy_kiov2kiov(unsigned int ndiov, struct bio_vec *diov,
360 unsigned int doffset,
361 unsigned int nsiov, struct bio_vec *siov,
362 unsigned int soffset,
365 /* NB diov, siov are READ-ONLY */
366 unsigned int this_nob;
373 LASSERT (!in_interrupt ());
376 while (doffset >= diov->bv_len) {
377 doffset -= diov->bv_len;
384 while (soffset >= siov->bv_len) {
385 soffset -= siov->bv_len;
394 this_nob = min3(diov->bv_len - doffset,
395 siov->bv_len - soffset,
399 daddr = ((char *)kmap(diov->bv_page)) +
400 diov->bv_offset + doffset;
402 saddr = ((char *)kmap(siov->bv_page)) +
403 siov->bv_offset + soffset;
405 /* Vanishing risk of kmap deadlock when mapping 2 pages.
406 * However in practice at least one of the kiovs will be mapped
407 * kernel pages and the map/unmap will be NOOPs */
409 memcpy (daddr, saddr, this_nob);
412 if (diov->bv_len > doffset + this_nob) {
416 kunmap(diov->bv_page);
423 if (siov->bv_len > soffset + this_nob) {
427 kunmap(siov->bv_page);
436 kunmap(diov->bv_page);
438 kunmap(siov->bv_page);
440 EXPORT_SYMBOL(lnet_copy_kiov2kiov);
443 lnet_copy_kiov2iov (unsigned int niov, struct kvec *iov, unsigned int iovoffset,
444 unsigned int nkiov, struct bio_vec *kiov,
445 unsigned int kiovoffset,
448 /* NB iov, kiov are READ-ONLY */
449 unsigned int this_nob;
455 LASSERT (!in_interrupt ());
458 while (iovoffset >= iov->iov_len) {
459 iovoffset -= iov->iov_len;
466 while (kiovoffset >= kiov->bv_len) {
467 kiovoffset -= kiov->bv_len;
476 this_nob = min3((unsigned int)iov->iov_len - iovoffset,
477 (unsigned int)kiov->bv_len - kiovoffset,
481 addr = ((char *)kmap(kiov->bv_page)) +
482 kiov->bv_offset + kiovoffset;
484 memcpy((char *)iov->iov_base + iovoffset, addr, this_nob);
487 if (iov->iov_len > iovoffset + this_nob) {
488 iovoffset += this_nob;
495 if (kiov->bv_len > kiovoffset + this_nob) {
497 kiovoffset += this_nob;
499 kunmap(kiov->bv_page);
509 kunmap(kiov->bv_page);
511 EXPORT_SYMBOL(lnet_copy_kiov2iov);
514 lnet_copy_iov2kiov(unsigned int nkiov, struct bio_vec *kiov,
515 unsigned int kiovoffset,
516 unsigned int niov, struct kvec *iov, unsigned int iovoffset,
519 /* NB kiov, iov are READ-ONLY */
520 unsigned int this_nob;
526 LASSERT (!in_interrupt ());
529 while (kiovoffset >= kiov->bv_len) {
530 kiovoffset -= kiov->bv_len;
537 while (iovoffset >= iov->iov_len) {
538 iovoffset -= iov->iov_len;
547 this_nob = min3((unsigned int)kiov->bv_len - kiovoffset,
548 (unsigned int)iov->iov_len - iovoffset,
552 addr = ((char *)kmap(kiov->bv_page)) +
553 kiov->bv_offset + kiovoffset;
555 memcpy (addr, (char *)iov->iov_base + iovoffset, this_nob);
558 if (kiov->bv_len > kiovoffset + this_nob) {
560 kiovoffset += this_nob;
562 kunmap(kiov->bv_page);
569 if (iov->iov_len > iovoffset + this_nob) {
570 iovoffset += this_nob;
579 kunmap(kiov->bv_page);
581 EXPORT_SYMBOL(lnet_copy_iov2kiov);
584 lnet_extract_kiov(int dst_niov, struct bio_vec *dst,
585 int src_niov, struct bio_vec *src,
586 unsigned int offset, unsigned int len)
588 /* Initialise 'dst' to the subset of 'src' starting at 'offset',
589 * for exactly 'len' bytes, and return the number of entries.
590 * NB not destructive to 'src' */
591 unsigned int frag_len;
594 if (len == 0) /* no data => */
595 return (0); /* no frags */
597 LASSERT(src_niov > 0);
598 while (offset >= src->bv_len) { /* skip initial frags */
599 offset -= src->bv_len;
602 LASSERT(src_niov > 0);
607 LASSERT(src_niov > 0);
608 LASSERT((int)niov <= dst_niov);
610 frag_len = src->bv_len - offset;
611 dst->bv_page = src->bv_page;
612 dst->bv_offset = src->bv_offset + offset;
614 if (len <= frag_len) {
616 LASSERT(dst->bv_offset + dst->bv_len <= PAGE_SIZE);
620 dst->bv_len = frag_len;
621 LASSERT(dst->bv_offset + dst->bv_len <= PAGE_SIZE);
631 EXPORT_SYMBOL(lnet_extract_kiov);
634 lnet_ni_recv(struct lnet_ni *ni, void *private, struct lnet_msg *msg,
635 int delayed, unsigned int offset, unsigned int mlen,
638 unsigned int niov = 0;
639 struct kvec *iov = NULL;
640 struct bio_vec *kiov = NULL;
643 LASSERT (!in_interrupt ());
644 LASSERT (mlen == 0 || msg != NULL);
647 LASSERT(msg->msg_receiving);
648 LASSERT(!msg->msg_sending);
649 LASSERT(rlen == msg->msg_len);
650 LASSERT(mlen <= msg->msg_len);
651 LASSERT(msg->msg_offset == offset);
652 LASSERT(msg->msg_wanted == mlen);
654 msg->msg_receiving = 0;
657 niov = msg->msg_niov;
658 kiov = msg->msg_kiov;
661 LASSERT ((iov == NULL) != (kiov == NULL));
665 rc = (ni->ni_net->net_lnd->lnd_recv)(ni, private, msg, delayed,
666 niov, kiov, offset, mlen,
669 lnet_finalize(msg, rc);
673 lnet_setpayloadbuffer(struct lnet_msg *msg)
675 struct lnet_libmd *md = msg->msg_md;
677 LASSERT(msg->msg_len > 0);
678 LASSERT(!msg->msg_routing);
680 LASSERT(msg->msg_niov == 0);
681 LASSERT(msg->msg_kiov == NULL);
683 msg->msg_niov = md->md_niov;
684 msg->msg_kiov = md->md_kiov;
688 lnet_prep_send(struct lnet_msg *msg, int type, struct lnet_processid *target,
689 unsigned int offset, unsigned int len)
691 msg->msg_type = type;
692 msg->msg_target = *target;
694 msg->msg_offset = offset;
697 lnet_setpayloadbuffer(msg);
699 memset (&msg->msg_hdr, 0, sizeof (msg->msg_hdr));
700 msg->msg_hdr.type = type;
701 /* dest_nid will be overwritten by lnet_select_pathway() */
702 msg->msg_hdr.dest_nid = target->nid;
703 msg->msg_hdr.dest_pid = target->pid;
704 /* src_nid will be set later */
705 msg->msg_hdr.src_pid = the_lnet.ln_pid;
706 msg->msg_hdr.payload_length = len;
710 lnet_ni_send(struct lnet_ni *ni, struct lnet_msg *msg)
712 void *priv = msg->msg_private;
715 LASSERT(!in_interrupt());
716 LASSERT(nid_is_lo0(&ni->ni_nid) ||
717 (msg->msg_txcredit && msg->msg_peertxcredit));
719 rc = (ni->ni_net->net_lnd->lnd_send)(ni, priv, msg);
721 msg->msg_no_resend = true;
722 lnet_finalize(msg, rc);
727 lnet_ni_eager_recv(struct lnet_ni *ni, struct lnet_msg *msg)
731 LASSERT(!msg->msg_sending);
732 LASSERT(msg->msg_receiving);
733 LASSERT(!msg->msg_rx_ready_delay);
734 LASSERT(ni->ni_net->net_lnd->lnd_eager_recv != NULL);
736 msg->msg_rx_ready_delay = 1;
737 rc = (ni->ni_net->net_lnd->lnd_eager_recv)(ni, msg->msg_private, msg,
740 CERROR("recv from %s / send to %s aborted: "
741 "eager_recv failed %d\n",
742 libcfs_nidstr(&msg->msg_rxpeer->lpni_nid),
743 libcfs_idstr(&msg->msg_target), rc);
744 LASSERT(rc < 0); /* required by my callers */
751 * -ETIMEDOUT if the message deadline has been exceeded
752 * -EHOSTUNREACH if the peer is down
753 * 0 if this message should not be dropped
756 lnet_check_message_drop(struct lnet_ni *ni, struct lnet_peer_ni *lpni,
757 struct lnet_msg *msg)
759 /* Drop message if we've exceeded the message deadline */
760 if (ktime_after(ktime_get(), msg->msg_deadline))
763 if (msg->msg_target.pid & LNET_PID_USERFLAG)
766 if (!lnet_peer_aliveness_enabled(lpni))
769 /* If we're resending a message, let's attempt to send it even if
770 * the peer is down to fulfill our resend quota on the message
772 if (msg->msg_retry_count > 0)
775 /* try and send recovery messages regardless */
776 if (msg->msg_recovery)
779 /* always send any responses */
780 if (lnet_msg_is_response(msg))
783 /* always send non-routed messages */
784 if (!msg->msg_routing)
787 /* assume peer_ni is alive as long as we're within the configured
790 if (ktime_get_seconds() <
791 (lpni->lpni_last_alive +
792 lpni->lpni_net->net_tunables.lct_peer_timeout))
795 if (!lnet_is_peer_ni_alive(lpni))
796 return -EHOSTUNREACH;
802 * \param msg The message to be sent.
803 * \param do_send True if lnet_ni_send() should be called in this function.
804 * lnet_send() is going to lnet_net_unlock immediately after this, so
805 * it sets do_send FALSE and I don't do the unlock/send/lock bit.
807 * \retval LNET_CREDIT_OK If \a msg sent or OK to send.
808 * \retval LNET_CREDIT_WAIT If \a msg blocked for credit.
809 * \retval -EHOSTUNREACH If the next hop of the message appears dead.
810 * \retval -ECANCELED If the MD of the message has been unlinked.
813 lnet_post_send_locked(struct lnet_msg *msg, int do_send)
815 struct lnet_peer_ni *lp = msg->msg_txpeer;
816 struct lnet_ni *ni = msg->msg_txni;
817 int cpt = msg->msg_tx_cpt;
818 struct lnet_tx_queue *tq = ni->ni_tx_queues[cpt];
821 /* non-lnet_send() callers have checked before */
822 LASSERT(!do_send || msg->msg_tx_delayed);
823 LASSERT(!msg->msg_receiving);
824 LASSERT(msg->msg_tx_committed);
826 /* can't get here if we're sending to the loopback interface */
827 if (the_lnet.ln_loni)
828 LASSERT(!nid_same(&lp->lpni_nid, &the_lnet.ln_loni->ni_nid));
830 /* NB 'lp' is always the next hop */
831 rc = lnet_check_message_drop(ni, lp, msg);
833 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
834 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length +=
836 lnet_net_unlock(cpt);
838 lnet_incr_stats(&msg->msg_txpeer->lpni_stats,
840 LNET_STATS_TYPE_DROP);
842 lnet_incr_stats(&msg->msg_txni->ni_stats,
844 LNET_STATS_TYPE_DROP);
846 if (rc == -EHOSTUNREACH) {
847 CNETERR("Dropping message for %s: peer not alive\n",
848 libcfs_idstr(&msg->msg_target));
849 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_DROPPED;
851 CNETERR("Dropping message for %s: exceeded message deadline\n",
852 libcfs_idstr(&msg->msg_target));
853 msg->msg_health_status =
854 LNET_MSG_STATUS_NETWORK_TIMEOUT;
858 lnet_finalize(msg, rc);
864 if (msg->msg_md != NULL &&
865 (msg->msg_md->md_flags & LNET_MD_FLAG_ABORTED) != 0) {
866 lnet_net_unlock(cpt);
868 CNETERR("Aborting message for %s: LNetM[DE]Unlink() already "
869 "called on the MD/ME.\n",
870 libcfs_idstr(&msg->msg_target));
872 msg->msg_no_resend = true;
873 CDEBUG(D_NET, "msg %p to %s canceled and will not be resent\n",
874 msg, libcfs_idstr(&msg->msg_target));
875 lnet_finalize(msg, -ECANCELED);
882 if (!msg->msg_peertxcredit) {
883 spin_lock(&lp->lpni_lock);
884 LASSERT((lp->lpni_txcredits < 0) ==
885 !list_empty(&lp->lpni_txq));
887 msg->msg_peertxcredit = 1;
888 lp->lpni_txqnob += msg->msg_len + sizeof(struct lnet_hdr_nid4);
889 lp->lpni_txcredits--;
891 if (lp->lpni_txcredits < lp->lpni_mintxcredits)
892 lp->lpni_mintxcredits = lp->lpni_txcredits;
894 if (lp->lpni_txcredits < 0) {
895 msg->msg_tx_delayed = 1;
896 list_add_tail(&msg->msg_list, &lp->lpni_txq);
897 spin_unlock(&lp->lpni_lock);
898 return LNET_CREDIT_WAIT;
900 spin_unlock(&lp->lpni_lock);
903 if (!msg->msg_txcredit) {
904 LASSERT((tq->tq_credits < 0) ==
905 !list_empty(&tq->tq_delayed));
907 msg->msg_txcredit = 1;
909 atomic_dec(&ni->ni_tx_credits);
911 if (tq->tq_credits < tq->tq_credits_min)
912 tq->tq_credits_min = tq->tq_credits;
914 if (tq->tq_credits < 0) {
915 msg->msg_tx_delayed = 1;
916 list_add_tail(&msg->msg_list, &tq->tq_delayed);
917 return LNET_CREDIT_WAIT;
921 if (unlikely(!list_empty(&the_lnet.ln_delay_rules)) &&
922 lnet_delay_rule_match_locked(&msg->msg_hdr, msg)) {
923 msg->msg_tx_delayed = 1;
924 return LNET_CREDIT_WAIT;
927 /* unset the tx_delay flag as we're going to send it now */
928 msg->msg_tx_delayed = 0;
931 lnet_net_unlock(cpt);
932 lnet_ni_send(ni, msg);
935 return LNET_CREDIT_OK;
939 static struct lnet_rtrbufpool *
940 lnet_msg2bufpool(struct lnet_msg *msg)
942 struct lnet_rtrbufpool *rbp;
945 LASSERT(msg->msg_rx_committed);
947 cpt = msg->msg_rx_cpt;
948 rbp = &the_lnet.ln_rtrpools[cpt][0];
950 LASSERT(msg->msg_len <= LNET_MTU);
951 while (msg->msg_len > (unsigned int)rbp->rbp_npages * PAGE_SIZE) {
953 LASSERT(rbp < &the_lnet.ln_rtrpools[cpt][LNET_NRBPOOLS]);
960 lnet_post_routed_recv_locked(struct lnet_msg *msg, int do_recv)
962 /* lnet_parse is going to lnet_net_unlock immediately after this, so it
963 * sets do_recv FALSE and I don't do the unlock/send/lock bit.
964 * I return LNET_CREDIT_WAIT if msg blocked and LNET_CREDIT_OK if
965 * received or OK to receive */
966 struct lnet_peer_ni *lpni = msg->msg_rxpeer;
967 struct lnet_peer *lp;
968 struct lnet_rtrbufpool *rbp;
969 struct lnet_rtrbuf *rb;
971 LASSERT(msg->msg_kiov == NULL);
972 LASSERT(msg->msg_niov == 0);
973 LASSERT(msg->msg_routing);
974 LASSERT(msg->msg_receiving);
975 LASSERT(!msg->msg_sending);
976 LASSERT(lpni->lpni_peer_net);
977 LASSERT(lpni->lpni_peer_net->lpn_peer);
979 lp = lpni->lpni_peer_net->lpn_peer;
981 /* non-lnet_parse callers only receive delayed messages */
982 LASSERT(!do_recv || msg->msg_rx_delayed);
984 if (!msg->msg_peerrtrcredit) {
985 /* lpni_lock protects the credit manipulation */
986 spin_lock(&lpni->lpni_lock);
988 msg->msg_peerrtrcredit = 1;
989 lpni->lpni_rtrcredits--;
990 if (lpni->lpni_rtrcredits < lpni->lpni_minrtrcredits)
991 lpni->lpni_minrtrcredits = lpni->lpni_rtrcredits;
993 if (lpni->lpni_rtrcredits < 0) {
994 spin_unlock(&lpni->lpni_lock);
995 /* must have checked eager_recv before here */
996 LASSERT(msg->msg_rx_ready_delay);
997 msg->msg_rx_delayed = 1;
998 /* lp_lock protects the lp_rtrq */
999 spin_lock(&lp->lp_lock);
1000 list_add_tail(&msg->msg_list, &lp->lp_rtrq);
1001 spin_unlock(&lp->lp_lock);
1002 return LNET_CREDIT_WAIT;
1004 spin_unlock(&lpni->lpni_lock);
1007 rbp = lnet_msg2bufpool(msg);
1009 if (!msg->msg_rtrcredit) {
1010 msg->msg_rtrcredit = 1;
1012 if (rbp->rbp_credits < rbp->rbp_mincredits)
1013 rbp->rbp_mincredits = rbp->rbp_credits;
1015 if (rbp->rbp_credits < 0) {
1016 /* must have checked eager_recv before here */
1017 LASSERT(msg->msg_rx_ready_delay);
1018 msg->msg_rx_delayed = 1;
1019 list_add_tail(&msg->msg_list, &rbp->rbp_msgs);
1020 return LNET_CREDIT_WAIT;
1024 LASSERT(!list_empty(&rbp->rbp_bufs));
1025 rb = list_first_entry(&rbp->rbp_bufs, struct lnet_rtrbuf, rb_list);
1026 list_del(&rb->rb_list);
1028 msg->msg_niov = rbp->rbp_npages;
1029 msg->msg_kiov = &rb->rb_kiov[0];
1031 /* unset the msg-rx_delayed flag since we're receiving the message */
1032 msg->msg_rx_delayed = 0;
1035 int cpt = msg->msg_rx_cpt;
1037 lnet_net_unlock(cpt);
1038 lnet_ni_recv(msg->msg_rxni, msg->msg_private, msg, 1,
1039 0, msg->msg_len, msg->msg_len);
1042 return LNET_CREDIT_OK;
1046 lnet_return_tx_credits_locked(struct lnet_msg *msg)
1048 struct lnet_peer_ni *txpeer = msg->msg_txpeer;
1049 struct lnet_ni *txni = msg->msg_txni;
1050 struct lnet_msg *msg2;
1052 if (msg->msg_txcredit) {
1053 struct lnet_ni *ni = msg->msg_txni;
1054 struct lnet_tx_queue *tq = ni->ni_tx_queues[msg->msg_tx_cpt];
1056 /* give back NI txcredits */
1057 msg->msg_txcredit = 0;
1059 LASSERT((tq->tq_credits < 0) ==
1060 !list_empty(&tq->tq_delayed));
1063 atomic_inc(&ni->ni_tx_credits);
1064 if (tq->tq_credits <= 0) {
1065 msg2 = list_first_entry(&tq->tq_delayed,
1066 struct lnet_msg, msg_list);
1067 list_del(&msg2->msg_list);
1069 LASSERT(msg2->msg_txni == ni);
1070 LASSERT(msg2->msg_tx_delayed);
1071 LASSERT(msg2->msg_tx_cpt == msg->msg_tx_cpt);
1073 (void) lnet_post_send_locked(msg2, 1);
1077 if (msg->msg_peertxcredit) {
1078 /* give back peer txcredits */
1079 msg->msg_peertxcredit = 0;
1081 spin_lock(&txpeer->lpni_lock);
1082 LASSERT((txpeer->lpni_txcredits < 0) ==
1083 !list_empty(&txpeer->lpni_txq));
1085 txpeer->lpni_txqnob -= msg->msg_len +
1086 sizeof(struct lnet_hdr_nid4);
1087 LASSERT(txpeer->lpni_txqnob >= 0);
1089 txpeer->lpni_txcredits++;
1090 if (txpeer->lpni_txcredits <= 0) {
1093 msg2 = list_first_entry(&txpeer->lpni_txq,
1094 struct lnet_msg, msg_list);
1095 list_del(&msg2->msg_list);
1096 spin_unlock(&txpeer->lpni_lock);
1098 LASSERT(msg2->msg_txpeer == txpeer);
1099 LASSERT(msg2->msg_tx_delayed);
1101 msg2_cpt = msg2->msg_tx_cpt;
1104 * The msg_cpt can be different from the msg2_cpt
1105 * so we need to make sure we lock the correct cpt
1107 * Once we call lnet_post_send_locked() it is no
1108 * longer safe to access msg2, since it could've
1109 * been freed by lnet_finalize(), but we still
1110 * need to relock the correct cpt, so we cache the
1111 * msg2_cpt for the purpose of the check that
1112 * follows the call to lnet_pose_send_locked().
1114 if (msg2_cpt != msg->msg_tx_cpt) {
1115 lnet_net_unlock(msg->msg_tx_cpt);
1116 lnet_net_lock(msg2_cpt);
1118 (void) lnet_post_send_locked(msg2, 1);
1119 if (msg2_cpt != msg->msg_tx_cpt) {
1120 lnet_net_unlock(msg2_cpt);
1121 lnet_net_lock(msg->msg_tx_cpt);
1124 spin_unlock(&txpeer->lpni_lock);
1129 msg->msg_txni = NULL;
1130 lnet_ni_decref_locked(txni, msg->msg_tx_cpt);
1133 if (txpeer != NULL) {
1134 msg->msg_txpeer = NULL;
1135 lnet_peer_ni_decref_locked(txpeer);
1140 lnet_schedule_blocked_locked(struct lnet_rtrbufpool *rbp)
1142 struct lnet_msg *msg;
1144 if (list_empty(&rbp->rbp_msgs))
1146 msg = list_first_entry(&rbp->rbp_msgs,
1147 struct lnet_msg, msg_list);
1148 list_del(&msg->msg_list);
1150 (void)lnet_post_routed_recv_locked(msg, 1);
1154 lnet_drop_routed_msgs_locked(struct list_head *list, int cpt)
1156 struct lnet_msg *msg;
1157 struct lnet_msg *tmp;
1159 lnet_net_unlock(cpt);
1161 list_for_each_entry_safe(msg, tmp, list, msg_list) {
1162 lnet_ni_recv(msg->msg_rxni, msg->msg_private, NULL,
1163 0, 0, 0, msg->msg_hdr.payload_length);
1164 list_del_init(&msg->msg_list);
1165 msg->msg_no_resend = true;
1166 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_ERROR;
1167 lnet_finalize(msg, -ECANCELED);
1174 lnet_return_rx_credits_locked(struct lnet_msg *msg)
1176 struct lnet_peer_ni *rxpeerni = msg->msg_rxpeer;
1177 struct lnet_peer *lp;
1178 struct lnet_ni *rxni = msg->msg_rxni;
1179 struct lnet_msg *msg2;
1181 if (msg->msg_rtrcredit) {
1182 /* give back global router credits */
1183 struct lnet_rtrbuf *rb;
1184 struct lnet_rtrbufpool *rbp;
1186 /* NB If a msg ever blocks for a buffer in rbp_msgs, it stays
1187 * there until it gets one allocated, or aborts the wait
1189 LASSERT(msg->msg_kiov != NULL);
1191 rb = list_entry(msg->msg_kiov, struct lnet_rtrbuf, rb_kiov[0]);
1194 msg->msg_kiov = NULL;
1195 msg->msg_rtrcredit = 0;
1197 LASSERT(rbp == lnet_msg2bufpool(msg));
1199 LASSERT((rbp->rbp_credits > 0) ==
1200 !list_empty(&rbp->rbp_bufs));
1202 /* If routing is now turned off, we just drop this buffer and
1203 * don't bother trying to return credits. */
1204 if (!the_lnet.ln_routing) {
1205 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1209 /* It is possible that a user has lowered the desired number of
1210 * buffers in this pool. Make sure we never put back
1211 * more buffers than the stated number. */
1212 if (unlikely(rbp->rbp_credits >= rbp->rbp_req_nbuffers)) {
1213 /* Discard this buffer so we don't have too
1215 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1216 rbp->rbp_nbuffers--;
1218 list_add(&rb->rb_list, &rbp->rbp_bufs);
1220 if (rbp->rbp_credits <= 0)
1221 lnet_schedule_blocked_locked(rbp);
1226 if (msg->msg_peerrtrcredit) {
1228 LASSERT(rxpeerni->lpni_peer_net);
1229 LASSERT(rxpeerni->lpni_peer_net->lpn_peer);
1231 spin_lock(&rxpeerni->lpni_lock);
1232 /* give back peer router credits */
1233 msg->msg_peerrtrcredit = 0;
1234 rxpeerni->lpni_rtrcredits++;
1235 spin_unlock(&rxpeerni->lpni_lock);
1237 lp = rxpeerni->lpni_peer_net->lpn_peer;
1238 spin_lock(&lp->lp_lock);
1240 /* drop all messages which are queued to be routed on that
1242 if (!the_lnet.ln_routing) {
1244 list_splice_init(&lp->lp_rtrq, &drop);
1245 spin_unlock(&lp->lp_lock);
1246 lnet_drop_routed_msgs_locked(&drop, msg->msg_rx_cpt);
1247 } else if (!list_empty(&lp->lp_rtrq)) {
1250 msg2 = list_first_entry(&lp->lp_rtrq,
1251 struct lnet_msg, msg_list);
1252 list_del(&msg2->msg_list);
1253 msg2_cpt = msg2->msg_rx_cpt;
1254 spin_unlock(&lp->lp_lock);
1256 * messages on the lp_rtrq can be from any NID in
1257 * the peer, which means they might have different
1258 * cpts. We need to make sure we lock the right
1261 if (msg2_cpt != msg->msg_rx_cpt) {
1262 lnet_net_unlock(msg->msg_rx_cpt);
1263 lnet_net_lock(msg2_cpt);
1265 (void) lnet_post_routed_recv_locked(msg2, 1);
1266 if (msg2_cpt != msg->msg_rx_cpt) {
1267 lnet_net_unlock(msg2_cpt);
1268 lnet_net_lock(msg->msg_rx_cpt);
1271 spin_unlock(&lp->lp_lock);
1275 msg->msg_rxni = NULL;
1276 lnet_ni_decref_locked(rxni, msg->msg_rx_cpt);
1278 if (rxpeerni != NULL) {
1279 msg->msg_rxpeer = NULL;
1280 lnet_peer_ni_decref_locked(rxpeerni);
1284 static struct lnet_peer_ni *
1285 lnet_select_peer_ni(struct lnet_ni *best_ni, struct lnet_nid *dst_nid,
1286 struct lnet_peer *peer,
1287 struct lnet_peer_ni *best_lpni,
1288 struct lnet_peer_net *peer_net)
1291 * Look at the peer NIs for the destination peer that connect
1292 * to the chosen net. If a peer_ni is preferred when using the
1293 * best_ni to communicate, we use that one. If there is no
1294 * preferred peer_ni, or there are multiple preferred peer_ni,
1295 * the available transmit credits are used. If the transmit
1296 * credits are equal, we round-robin over the peer_ni.
1298 struct lnet_peer_ni *lpni = NULL;
1299 int best_lpni_credits = (best_lpni) ? best_lpni->lpni_txcredits :
1301 int best_lpni_healthv = (best_lpni) ?
1302 atomic_read(&best_lpni->lpni_healthv) : 0;
1303 bool best_lpni_is_preferred = false;
1304 bool lpni_is_preferred;
1306 __u32 lpni_sel_prio;
1307 __u32 best_sel_prio = LNET_MAX_SELECTION_PRIORITY;
1309 while ((lpni = lnet_get_next_peer_ni_locked(peer, peer_net, lpni))) {
1311 * if the best_ni we've chosen aleady has this lpni
1312 * preferred, then let's use it
1315 lpni_is_preferred = lnet_peer_is_pref_nid_locked(
1316 lpni, &best_ni->ni_nid);
1317 CDEBUG(D_NET, "%s lpni_is_preferred = %d\n",
1318 libcfs_nidstr(&best_ni->ni_nid),
1321 lpni_is_preferred = false;
1324 lpni_healthv = atomic_read(&lpni->lpni_healthv);
1325 lpni_sel_prio = lpni->lpni_sel_priority;
1328 CDEBUG(D_NET, "n:[%s, %s] h:[%d, %d] p:[%d, %d] c:[%d, %d] s:[%d, %d]\n",
1329 libcfs_nidstr(&lpni->lpni_nid),
1330 libcfs_nidstr(&best_lpni->lpni_nid),
1331 lpni_healthv, best_lpni_healthv,
1332 lpni_sel_prio, best_sel_prio,
1333 lpni->lpni_txcredits, best_lpni_credits,
1334 lpni->lpni_seq, best_lpni->lpni_seq);
1338 /* pick the healthiest peer ni */
1339 if (lpni_healthv < best_lpni_healthv)
1341 else if (lpni_healthv > best_lpni_healthv)
1344 if (lpni_sel_prio > best_sel_prio)
1346 else if (lpni_sel_prio < best_sel_prio)
1349 /* If this is a preferred peer - use it. Otherwise, ignore it */
1350 if (!best_lpni_is_preferred && lpni_is_preferred)
1352 else if (best_lpni_is_preferred && !lpni_is_preferred)
1355 if (lpni->lpni_txcredits < best_lpni_credits)
1356 /* We already have a peer that has more credits
1357 * available than this one. No need to consider
1358 * this peer further.
1361 else if (lpni->lpni_txcredits > best_lpni_credits)
1364 /* The best peer found so far and the current peer
1365 * have the same number of available credits let's
1366 * make sure to select between them using Round Robin
1368 if (best_lpni && (best_lpni->lpni_seq <= lpni->lpni_seq))
1371 best_lpni_is_preferred = lpni_is_preferred;
1372 best_lpni_healthv = lpni_healthv;
1373 best_sel_prio = lpni_sel_prio;
1375 best_lpni_credits = lpni->lpni_txcredits;
1378 /* if we still can't find a peer ni then we can't reach it */
1380 u32 net_id = (peer_net) ? peer_net->lpn_net_id :
1381 LNET_NID_NET(dst_nid);
1382 CDEBUG(D_NET, "no peer_ni found on peer net %s\n",
1383 libcfs_net2str(net_id));
1387 CDEBUG(D_NET, "sd_best_lpni = %s\n",
1388 libcfs_nidstr(&best_lpni->lpni_nid));
1394 * Prerequisite: the best_ni should already be set in the sd
1395 * Find the best lpni.
1396 * If the net id is provided then restrict lpni selection on
1397 * that particular net.
1398 * Otherwise find any reachable lpni. When dealing with an MR
1399 * gateway and it has multiple lpnis which we can use
1400 * we want to select the best one from the list of reachable
1403 static inline struct lnet_peer_ni *
1404 lnet_find_best_lpni(struct lnet_ni *lni, struct lnet_nid *dst_nid,
1405 struct lnet_peer *peer, u32 net_id)
1407 struct lnet_peer_net *peer_net;
1409 /* find the best_lpni on any local network */
1410 if (net_id == LNET_NET_ANY) {
1411 struct lnet_peer_ni *best_lpni = NULL;
1412 struct lnet_peer_net *lpn;
1413 list_for_each_entry(lpn, &peer->lp_peer_nets, lpn_peer_nets) {
1414 /* no net specified find any reachable peer ni */
1415 if (!lnet_islocalnet_locked(lpn->lpn_net_id))
1417 best_lpni = lnet_select_peer_ni(lni, dst_nid, peer,
1423 /* restrict on the specified net */
1424 peer_net = lnet_peer_get_net_locked(peer, net_id);
1426 return lnet_select_peer_ni(lni, dst_nid, peer, NULL, peer_net);
1432 lnet_compare_gw_lpnis(struct lnet_peer_ni *lpni1, struct lnet_peer_ni *lpni2)
1434 if (lpni1->lpni_txqnob < lpni2->lpni_txqnob)
1437 if (lpni1->lpni_txqnob > lpni2->lpni_txqnob)
1440 if (lpni1->lpni_txcredits > lpni2->lpni_txcredits)
1443 if (lpni1->lpni_txcredits < lpni2->lpni_txcredits)
1449 /* Compare route priorities and hop counts */
1451 lnet_compare_routes(struct lnet_route *r1, struct lnet_route *r2)
1453 int r1_hops = (r1->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r1->lr_hops;
1454 int r2_hops = (r2->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r2->lr_hops;
1456 if (r1->lr_priority < r2->lr_priority)
1459 if (r1->lr_priority > r2->lr_priority)
1462 if (r1_hops < r2_hops)
1465 if (r1_hops > r2_hops)
1471 static struct lnet_route *
1472 lnet_find_route_locked(struct lnet_remotenet *rnet, __u32 src_net,
1473 struct lnet_peer_ni *remote_lpni,
1474 struct lnet_route **prev_route,
1475 struct lnet_peer_ni **gwni)
1477 struct lnet_peer_ni *lpni, *best_gw_ni = NULL;
1478 struct lnet_route *best_route;
1479 struct lnet_route *last_route;
1480 struct lnet_route *route;
1482 bool best_rte_is_preferred = false;
1483 struct lnet_nid *gw_pnid;
1485 CDEBUG(D_NET, "Looking up a route to %s, from %s\n",
1486 libcfs_net2str(rnet->lrn_net), libcfs_net2str(src_net));
1488 best_route = last_route = NULL;
1489 list_for_each_entry(route, &rnet->lrn_routes, lr_list) {
1490 if (!lnet_is_route_alive(route))
1492 gw_pnid = &route->lr_gateway->lp_primary_nid;
1494 /* no protection on below fields, but it's harmless */
1495 if (last_route && (last_route->lr_seq - route->lr_seq < 0))
1498 /* if the best route found is in the preferred list then
1499 * tag it as preferred and use it later on. But if we
1500 * didn't find any routes which are on the preferred list
1501 * then just use the best route possible.
1503 rc = lnet_peer_is_pref_rtr_locked(remote_lpni, gw_pnid);
1505 if (!best_route || (rc && !best_rte_is_preferred)) {
1506 /* Restrict the selection of the router NI on the
1507 * src_net provided. If the src_net is LNET_NID_ANY,
1508 * then select the best interface available.
1510 lpni = lnet_find_best_lpni(NULL, NULL,
1515 "Gateway %s does not have a peer NI on net %s\n",
1516 libcfs_nidstr(gw_pnid),
1517 libcfs_net2str(src_net));
1522 if (rc && !best_rte_is_preferred) {
1523 /* This is the first preferred route we found,
1524 * so it beats any route found previously
1530 best_rte_is_preferred = true;
1531 CDEBUG(D_NET, "preferred gw = %s\n",
1532 libcfs_nidstr(gw_pnid));
1534 } else if ((!rc) && best_rte_is_preferred)
1535 /* The best route we found so far is in the preferred
1536 * list, so it beats any non-preferred route
1541 best_route = last_route = route;
1546 rc = lnet_compare_routes(route, best_route);
1550 /* Restrict the selection of the router NI on the
1551 * src_net provided. If the src_net is LNET_NID_ANY,
1552 * then select the best interface available.
1554 lpni = lnet_find_best_lpni(NULL, NULL, route->lr_gateway,
1558 "Gateway %s does not have a peer NI on net %s\n",
1559 libcfs_nidstr(gw_pnid),
1560 libcfs_net2str(src_net));
1570 rc = lnet_compare_gw_lpnis(lpni, best_gw_ni);
1574 if (rc == 1 || route->lr_seq <= best_route->lr_seq) {
1581 *prev_route = last_route;
1587 static inline unsigned int
1588 lnet_dev_prio_of_md(struct lnet_ni *ni, unsigned int dev_idx)
1590 if (dev_idx == UINT_MAX)
1593 if (!ni || !ni->ni_net || !ni->ni_net->net_lnd ||
1594 !ni->ni_net->net_lnd->lnd_get_dev_prio)
1597 return ni->ni_net->net_lnd->lnd_get_dev_prio(ni, dev_idx);
1600 static struct lnet_ni *
1601 lnet_get_best_ni(struct lnet_net *local_net, struct lnet_ni *best_ni,
1602 struct lnet_peer *peer, struct lnet_peer_net *peer_net,
1603 struct lnet_msg *msg, int md_cpt)
1605 struct lnet_libmd *md = msg->msg_md;
1606 unsigned int offset = msg->msg_offset;
1607 unsigned int shortest_distance;
1608 struct lnet_ni *ni = NULL;
1611 __u32 best_sel_prio;
1612 unsigned int best_dev_prio;
1614 unsigned int dev_idx = UINT_MAX;
1615 bool gpu = lnet_md_is_gpu(md);
1618 struct page *page = lnet_get_first_page(md, offset);
1620 dev_idx = lnet_get_dev_idx(page);
1624 * If there is no peer_ni that we can send to on this network,
1625 * then there is no point in looking for a new best_ni here.
1627 if (!lnet_get_next_peer_ni_locked(peer, peer_net, NULL))
1630 if (best_ni == NULL) {
1631 best_sel_prio = LNET_MAX_SELECTION_PRIORITY;
1632 shortest_distance = UINT_MAX;
1633 best_dev_prio = UINT_MAX;
1634 best_credits = INT_MIN;
1636 best_ni_fatal = true;
1638 best_dev_prio = lnet_dev_prio_of_md(best_ni, dev_idx);
1639 shortest_distance = cfs_cpt_distance(lnet_cpt_table(), md_cpt,
1640 best_ni->ni_dev_cpt);
1641 best_credits = atomic_read(&best_ni->ni_tx_credits);
1642 best_healthv = atomic_read(&best_ni->ni_healthv);
1643 best_sel_prio = best_ni->ni_sel_priority;
1644 best_ni_fatal = atomic_read(&best_ni->ni_fatal_error_on);
1647 while ((ni = lnet_get_next_ni_locked(local_net, ni))) {
1648 unsigned int distance;
1653 unsigned int ni_dev_prio;
1655 ni_credits = atomic_read(&ni->ni_tx_credits);
1656 ni_healthv = atomic_read(&ni->ni_healthv);
1657 ni_fatal = atomic_read(&ni->ni_fatal_error_on);
1658 ni_sel_prio = ni->ni_sel_priority;
1661 * calculate the distance from the CPT on which
1662 * the message memory is allocated to the CPT of
1663 * the NI's physical device
1665 distance = cfs_cpt_distance(lnet_cpt_table(),
1669 ni_dev_prio = lnet_dev_prio_of_md(ni, dev_idx);
1672 * All distances smaller than the NUMA range
1673 * are treated equally.
1675 if (!gpu && distance < lnet_numa_range)
1676 distance = lnet_numa_range;
1679 * Select on health, selection policy, direct dma prio,
1680 * shorter distance, available credits, then round-robin.
1683 CDEBUG(D_NET, "compare ni %s [f:%s, c:%d, d:%d, s:%d, p:%u, g:%u, h:%d] with best_ni %s [f:%s, c:%d, d:%d, s:%d, p:%u, g:%u, h:%d]\n",
1684 libcfs_nidstr(&ni->ni_nid),
1685 ni_fatal ? "y" : "n", ni_credits, distance,
1686 ni->ni_seq, ni_sel_prio, ni_dev_prio, ni_healthv,
1687 (best_ni) ? libcfs_nidstr(&best_ni->ni_nid)
1689 best_ni_fatal ? "y" : "n", best_credits,
1691 (best_ni) ? best_ni->ni_seq : 0,
1692 best_sel_prio, best_dev_prio, best_healthv);
1696 if (ni_fatal && !best_ni_fatal)
1698 else if (!ni_fatal && best_ni_fatal)
1701 if (ni_healthv < best_healthv)
1703 else if (ni_healthv > best_healthv)
1706 if (ni_sel_prio > best_sel_prio)
1708 else if (ni_sel_prio < best_sel_prio)
1711 if (ni_dev_prio > best_dev_prio)
1713 else if (ni_dev_prio < best_dev_prio)
1716 if (distance > shortest_distance)
1718 else if (distance < shortest_distance)
1721 if (ni_credits < best_credits)
1723 else if (ni_credits > best_credits)
1726 if (best_ni && best_ni->ni_seq <= ni->ni_seq)
1730 best_sel_prio = ni_sel_prio;
1731 best_dev_prio = ni_dev_prio;
1732 shortest_distance = distance;
1733 best_healthv = ni_healthv;
1735 best_credits = ni_credits;
1736 best_ni_fatal = ni_fatal;
1739 CDEBUG(D_NET, "selected best_ni %s\n",
1740 (best_ni) ? libcfs_nidstr(&best_ni->ni_nid) : "no selection");
1746 lnet_reserved_msg(struct lnet_msg *msg)
1748 if (msg->msg_type == LNET_MSG_PUT) {
1749 if (msg->msg_hdr.msg.put.ptl_index == LNET_RESERVED_PORTAL)
1751 } else if (msg->msg_type == LNET_MSG_GET) {
1752 if (msg->msg_hdr.msg.get.ptl_index == LNET_RESERVED_PORTAL)
1758 /* Can the specified message trigger peer discovery?
1760 * Traffic to the LNET_RESERVED_PORTAL may not trigger peer discovery,
1761 * because such traffic is required to perform discovery. We therefore
1762 * exclude all GET and PUT on that portal. We also exclude all ACK and
1763 * REPLY traffic, but that is because the portal is not tracked in the
1764 * message structure for these message types. We could restrict this
1765 * further by also checking for LNET_PROTO_PING_MATCHBITS.
1768 lnet_msg_discovery(struct lnet_msg *msg)
1770 return !(lnet_reserved_msg(msg) || lnet_msg_is_response(msg));
1773 /* Is the specified message an LNet ping?
1776 lnet_msg_is_ping(struct lnet_msg *msg)
1778 if (msg->msg_type == LNET_MSG_GET &&
1779 msg->msg_hdr.msg.get.ptl_index == LNET_RESERVED_PORTAL)
1785 #define SRC_SPEC 0x0001
1786 #define SRC_ANY 0x0002
1787 #define LOCAL_DST 0x0004
1788 #define REMOTE_DST 0x0008
1789 #define MR_DST 0x0010
1790 #define NMR_DST 0x0020
1791 #define SND_RESP 0x0040
1793 /* The following to defines are used for return codes */
1794 #define REPEAT_SEND 0x1000
1795 #define PASS_THROUGH 0x2000
1797 /* The different cases lnet_select pathway needs to handle */
1798 #define SRC_SPEC_LOCAL_MR_DST (SRC_SPEC | LOCAL_DST | MR_DST)
1799 #define SRC_SPEC_ROUTER_MR_DST (SRC_SPEC | REMOTE_DST | MR_DST)
1800 #define SRC_SPEC_LOCAL_NMR_DST (SRC_SPEC | LOCAL_DST | NMR_DST)
1801 #define SRC_SPEC_ROUTER_NMR_DST (SRC_SPEC | REMOTE_DST | NMR_DST)
1802 #define SRC_ANY_LOCAL_MR_DST (SRC_ANY | LOCAL_DST | MR_DST)
1803 #define SRC_ANY_ROUTER_MR_DST (SRC_ANY | REMOTE_DST | MR_DST)
1804 #define SRC_ANY_LOCAL_NMR_DST (SRC_ANY | LOCAL_DST | NMR_DST)
1805 #define SRC_ANY_ROUTER_NMR_DST (SRC_ANY | REMOTE_DST | NMR_DST)
1808 lnet_handle_lo_send(struct lnet_send_data *sd)
1810 struct lnet_msg *msg = sd->sd_msg;
1811 int cpt = sd->sd_cpt;
1813 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1816 /* No send credit hassles with LOLND */
1817 lnet_ni_addref_locked(the_lnet.ln_loni, cpt);
1818 msg->msg_hdr.dest_nid = the_lnet.ln_loni->ni_nid;
1819 if (!msg->msg_routing)
1820 msg->msg_hdr.src_nid = the_lnet.ln_loni->ni_nid;
1821 msg->msg_target.nid = the_lnet.ln_loni->ni_nid;
1822 lnet_msg_commit(msg, cpt);
1823 msg->msg_txni = the_lnet.ln_loni;
1825 return LNET_CREDIT_OK;
1829 lnet_handle_send(struct lnet_send_data *sd)
1831 struct lnet_ni *best_ni = sd->sd_best_ni;
1832 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
1833 struct lnet_peer_ni *final_dst_lpni = sd->sd_final_dst_lpni;
1834 struct lnet_msg *msg = sd->sd_msg;
1836 __u32 send_case = sd->sd_send_case;
1838 __u32 routing = send_case & REMOTE_DST;
1839 struct lnet_rsp_tracker *rspt;
1841 /* Increment sequence number of the selected peer, peer net,
1842 * local ni and local net so that we pick the next ones
1845 best_lpni->lpni_peer_net->lpn_peer->lp_send_seq++;
1846 best_lpni->lpni_peer_net->lpn_seq =
1847 best_lpni->lpni_peer_net->lpn_peer->lp_send_seq;
1848 best_lpni->lpni_seq = best_lpni->lpni_peer_net->lpn_seq;
1849 the_lnet.ln_net_seq++;
1850 best_ni->ni_net->net_seq = the_lnet.ln_net_seq;
1851 best_ni->ni_seq = best_ni->ni_net->net_seq;
1853 CDEBUG(D_NET, "%s NI seq info: [%d:%d:%d:%u] %s LPNI seq info [%d:%d:%d:%u]\n",
1854 libcfs_nidstr(&best_ni->ni_nid),
1855 best_ni->ni_seq, best_ni->ni_net->net_seq,
1856 atomic_read(&best_ni->ni_tx_credits),
1857 best_ni->ni_sel_priority,
1858 libcfs_nidstr(&best_lpni->lpni_nid),
1859 best_lpni->lpni_seq, best_lpni->lpni_peer_net->lpn_seq,
1860 best_lpni->lpni_txcredits,
1861 best_lpni->lpni_sel_priority);
1864 * grab a reference on the peer_ni so it sticks around even if
1865 * we need to drop and relock the lnet_net_lock below.
1867 lnet_peer_ni_addref_locked(best_lpni);
1870 * Use lnet_cpt_of_nid() to determine the CPT used to commit the
1871 * message. This ensures that we get a CPT that is correct for
1872 * the NI when the NI has been restricted to a subset of all CPTs.
1873 * If the selected CPT differs from the one currently locked, we
1874 * must unlock and relock the lnet_net_lock(), and then check whether
1875 * the configuration has changed. We don't have a hold on the best_ni
1876 * yet, and it may have vanished.
1878 cpt2 = lnet_cpt_of_nid_locked(&best_lpni->lpni_nid, best_ni);
1879 if (sd->sd_cpt != cpt2) {
1880 __u32 seq = lnet_get_dlc_seq_locked();
1881 lnet_net_unlock(sd->sd_cpt);
1883 lnet_net_lock(sd->sd_cpt);
1884 if (seq != lnet_get_dlc_seq_locked()) {
1885 lnet_peer_ni_decref_locked(best_lpni);
1891 * store the best_lpni in the message right away to avoid having
1892 * to do the same operation under different conditions
1894 msg->msg_txpeer = best_lpni;
1895 msg->msg_txni = best_ni;
1898 * grab a reference for the best_ni since now it's in use in this
1899 * send. The reference will be dropped in lnet_finalize()
1901 lnet_ni_addref_locked(msg->msg_txni, sd->sd_cpt);
1904 * Always set the target.nid to the best peer picked. Either the
1905 * NID will be one of the peer NIDs selected, or the same NID as
1906 * what was originally set in the target or it will be the NID of
1907 * a router if this message should be routed
1909 msg->msg_target.nid = msg->msg_txpeer->lpni_nid;
1912 * lnet_msg_commit assigns the correct cpt to the message, which
1913 * is used to decrement the correct refcount on the ni when it's
1914 * time to return the credits
1916 lnet_msg_commit(msg, sd->sd_cpt);
1919 * If we are routing the message then we keep the src_nid that was
1920 * set by the originator. If we are not routing then we are the
1921 * originator and set it here.
1923 if (!msg->msg_routing)
1924 msg->msg_hdr.src_nid = msg->msg_txni->ni_nid;
1927 msg->msg_target_is_router = 1;
1928 msg->msg_target.pid = LNET_PID_LUSTRE;
1930 * since we're routing we want to ensure that the
1931 * msg_hdr.dest_nid is set to the final destination. When
1932 * the router receives this message it knows how to route
1935 * final_dst_lpni is set at the beginning of the
1936 * lnet_select_pathway() function and is never changed.
1937 * It's safe to use it here.
1939 final_dst_lpni->lpni_peer_net->lpn_peer->lp_send_seq++;
1940 final_dst_lpni->lpni_peer_net->lpn_seq =
1941 final_dst_lpni->lpni_peer_net->lpn_peer->lp_send_seq;
1942 final_dst_lpni->lpni_seq =
1943 final_dst_lpni->lpni_peer_net->lpn_seq;
1944 msg->msg_hdr.dest_nid = final_dst_lpni->lpni_nid;
1947 * if we're not routing set the dest_nid to the best peer
1948 * ni NID that we picked earlier in the algorithm.
1950 msg->msg_hdr.dest_nid = msg->msg_txpeer->lpni_nid;
1954 * if we have response tracker block update it with the next hop
1958 rspt = msg->msg_md->md_rspt_ptr;
1960 rspt->rspt_next_hop_nid =
1961 msg->msg_txpeer->lpni_nid;
1962 CDEBUG(D_NET, "rspt_next_hop_nid = %s\n",
1963 libcfs_nidstr(&rspt->rspt_next_hop_nid));
1967 rc = lnet_post_send_locked(msg, 0);
1970 CDEBUG(D_NET, "TRACE: %s(%s:%s) -> %s(%s:%s) %s : %s try# %d\n",
1971 libcfs_nidstr(&msg->msg_hdr.src_nid),
1972 libcfs_nidstr(&msg->msg_txni->ni_nid),
1973 libcfs_nidstr(&sd->sd_src_nid),
1974 libcfs_nidstr(&msg->msg_hdr.dest_nid),
1975 libcfs_nidstr(&sd->sd_dst_nid),
1976 libcfs_nidstr(&msg->msg_txpeer->lpni_nid),
1977 libcfs_nidstr(&sd->sd_rtr_nid),
1978 lnet_msgtyp2str(msg->msg_type), msg->msg_retry_count);
1984 lnet_set_non_mr_pref_nid(struct lnet_peer_ni *lpni, struct lnet_ni *lni,
1985 struct lnet_msg *msg)
1987 if (!lnet_peer_is_multi_rail(lpni->lpni_peer_net->lpn_peer) &&
1988 !lnet_msg_is_response(msg) && lpni->lpni_pref_nnids == 0) {
1989 CDEBUG(D_NET, "Setting preferred local NID %s on NMR peer %s\n",
1990 libcfs_nidstr(&lni->ni_nid),
1991 libcfs_nidstr(&lpni->lpni_nid));
1992 lnet_peer_ni_set_non_mr_pref_nid(lpni, &lni->ni_nid);
2001 * use the source and destination NIDs as the pathway
2004 lnet_handle_spec_local_nmr_dst(struct lnet_send_data *sd)
2006 /* the destination lpni is set before we get here. */
2009 sd->sd_best_ni = lnet_nid_to_ni_locked(&sd->sd_src_nid, sd->sd_cpt);
2010 if (!sd->sd_best_ni) {
2011 CERROR("Can't send to %s: src %s is not a local nid\n",
2012 libcfs_nidstr(&sd->sd_dst_nid),
2013 libcfs_nidstr(&sd->sd_src_nid));
2017 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
2019 return lnet_handle_send(sd);
2027 * Don't run the selection algorithm on the peer NIs. By specifying the
2028 * local NID, we're also saying that we should always use the destination NID
2029 * provided. This handles the case where we should be using the same
2030 * destination NID for the all the messages which belong to the same RPC
2034 lnet_handle_spec_local_mr_dst(struct lnet_send_data *sd)
2036 sd->sd_best_ni = lnet_nid_to_ni_locked(&sd->sd_src_nid, sd->sd_cpt);
2037 if (!sd->sd_best_ni) {
2038 CERROR("Can't send to %s: src %s is not a local nid\n",
2039 libcfs_nidstr(&sd->sd_dst_nid),
2040 libcfs_nidstr(&sd->sd_src_nid));
2044 if (sd->sd_best_lpni &&
2045 nid_same(&sd->sd_best_lpni->lpni_nid,
2046 &the_lnet.ln_loni->ni_nid))
2047 return lnet_handle_lo_send(sd);
2048 else if (sd->sd_best_lpni)
2049 return lnet_handle_send(sd);
2051 CERROR("can't send to %s. no NI on %s\n",
2052 libcfs_nidstr(&sd->sd_dst_nid),
2053 libcfs_net2str(sd->sd_best_ni->ni_net->net_id));
2055 return -EHOSTUNREACH;
2058 static struct lnet_ni *
2059 lnet_find_best_ni_on_spec_net(struct lnet_ni *cur_best_ni,
2060 struct lnet_peer *peer,
2061 struct lnet_peer_net *peer_net,
2062 struct lnet_msg *msg,
2065 struct lnet_net *local_net;
2066 struct lnet_ni *best_ni;
2068 local_net = lnet_get_net_locked(peer_net->lpn_net_id);
2073 * Iterate through the NIs in this local Net and select
2074 * the NI to send from. The selection is determined by
2075 * these 3 criterion in the following priority:
2077 * 2. NI available credits
2080 best_ni = lnet_get_best_ni(local_net, cur_best_ni,
2081 peer, peer_net, msg, cpt);
2087 lnet_initiate_peer_discovery(struct lnet_peer_ni *lpni, struct lnet_msg *msg,
2090 struct lnet_peer *peer;
2091 struct lnet_peer_ni *new_lpni;
2094 lnet_peer_ni_addref_locked(lpni);
2096 peer = lpni->lpni_peer_net->lpn_peer;
2098 if (lnet_peer_gw_discovery(peer)) {
2099 lnet_peer_ni_decref_locked(lpni);
2103 if (!lnet_msg_discovery(msg) || lnet_peer_is_uptodate(peer)) {
2104 lnet_peer_ni_decref_locked(lpni);
2108 rc = lnet_discover_peer_locked(lpni, cpt, false);
2110 lnet_peer_ni_decref_locked(lpni);
2114 new_lpni = lnet_peer_ni_find_locked(&lpni->lpni_nid);
2116 lnet_peer_ni_decref_locked(lpni);
2120 peer = new_lpni->lpni_peer_net->lpn_peer;
2121 spin_lock(&peer->lp_lock);
2122 if (lpni == new_lpni && lnet_peer_is_uptodate_locked(peer)) {
2123 /* The peer NI did not change and the peer is up to date.
2124 * Nothing more to do.
2126 spin_unlock(&peer->lp_lock);
2127 lnet_peer_ni_decref_locked(lpni);
2128 lnet_peer_ni_decref_locked(new_lpni);
2131 spin_unlock(&peer->lp_lock);
2133 /* Either the peer NI changed during discovery, or the peer isn't up
2134 * to date. In both cases we want to queue the message on the
2135 * (possibly new) peer's pending queue and queue the peer for discovery
2137 msg->msg_sending = 0;
2138 msg->msg_txpeer = NULL;
2139 lnet_net_unlock(cpt);
2140 lnet_peer_queue_message(peer, msg);
2143 lnet_peer_ni_decref_locked(lpni);
2144 lnet_peer_ni_decref_locked(new_lpni);
2146 CDEBUG(D_NET, "msg %p delayed. %s pending discovery\n",
2147 msg, libcfs_nidstr(&peer->lp_primary_nid));
2149 return LNET_DC_WAIT;
2153 lnet_handle_find_routed_path(struct lnet_send_data *sd,
2154 struct lnet_nid *dst_nid,
2155 struct lnet_peer_ni **gw_lpni,
2156 struct lnet_peer **gw_peer)
2159 struct lnet_peer *gw;
2160 struct lnet_peer *lp;
2161 struct lnet_peer_net *lpn;
2162 struct lnet_peer_net *best_lpn = NULL;
2163 struct lnet_remotenet *rnet, *best_rnet = NULL;
2164 struct lnet_route *best_route = NULL;
2165 struct lnet_route *last_route = NULL;
2166 struct lnet_peer_ni *lpni = NULL;
2167 struct lnet_peer_ni *gwni = NULL;
2168 bool route_found = false;
2169 bool gwni_decref = false;
2170 struct lnet_nid *src_nid =
2171 !LNET_NID_IS_ANY(&sd->sd_src_nid) || !sd->sd_best_ni
2173 : &sd->sd_best_ni->ni_nid;
2174 int best_lpn_healthv = 0;
2175 __u32 best_lpn_sel_prio = LNET_MAX_SELECTION_PRIORITY;
2177 CDEBUG(D_NET, "%s route (%s) from local NI %s to destination %s\n",
2178 LNET_NID_IS_ANY(&sd->sd_rtr_nid) ? "Lookup" : "Specified",
2179 libcfs_nidstr(&sd->sd_rtr_nid), libcfs_nidstr(src_nid),
2180 libcfs_nidstr(&sd->sd_dst_nid));
2182 /* If a router nid was specified then we are replying to a GET or
2183 * sending an ACK. In this case we use the gateway associated with the
2184 * specified router nid.
2186 if (!LNET_NID_IS_ANY(&sd->sd_rtr_nid)) {
2187 gwni = lnet_peer_ni_find_locked(&sd->sd_rtr_nid);
2190 gw = gwni->lpni_peer_net->lpn_peer;
2191 if (gw->lp_rtr_refcount)
2194 CWARN("No peer NI for gateway %s. Attempting to find an alternative route.\n",
2195 libcfs_nidstr(&sd->sd_rtr_nid));
2200 if (sd->sd_msg->msg_routing || !LNET_NID_IS_ANY(src_nid)) {
2201 /* If I'm routing this message then I need to find the
2202 * next hop based on the destination NID
2204 * We also find next hop based on the destination NID
2205 * if the source NI was specified
2207 best_rnet = lnet_find_rnet_locked(LNET_NID_NET(&sd->sd_dst_nid));
2209 CERROR("Unable to send message from %s to %s - Route table may be misconfigured\n",
2210 (src_nid && LNET_NID_IS_ANY(src_nid)) ?
2212 libcfs_nidstr(src_nid),
2213 libcfs_nidstr(&sd->sd_dst_nid));
2217 CDEBUG(D_NET, "best_rnet %s\n",
2218 libcfs_net2str(best_rnet->lrn_net));
2220 /* we've already looked up the initial lpni using
2223 lpni = sd->sd_best_lpni;
2224 /* the peer tree must be in existence */
2225 LASSERT(lpni && lpni->lpni_peer_net &&
2226 lpni->lpni_peer_net->lpn_peer);
2227 lp = lpni->lpni_peer_net->lpn_peer;
2229 list_for_each_entry(lpn, &lp->lp_peer_nets, lpn_peer_nets) {
2230 /* is this remote network reachable? */
2231 rnet = lnet_find_rnet_locked(lpn->lpn_net_id);
2238 CDEBUG(D_NET, "n[%s, %s] h[%d, %d], p[%u, %u], s[%d, %d]\n",
2239 libcfs_net2str(lpn->lpn_net_id),
2240 libcfs_net2str(best_lpn->lpn_net_id),
2242 best_lpn->lpn_healthv,
2243 lpn->lpn_sel_priority,
2244 best_lpn->lpn_sel_priority,
2248 /* select the preferred peer net */
2249 if (best_lpn_healthv > lpn->lpn_healthv)
2251 else if (best_lpn_healthv < lpn->lpn_healthv)
2254 if (best_lpn_sel_prio < lpn->lpn_sel_priority)
2256 else if (best_lpn_sel_prio > lpn->lpn_sel_priority)
2259 if (best_lpn->lpn_seq <= lpn->lpn_seq)
2262 best_lpn_healthv = lpn->lpn_healthv;
2263 best_lpn_sel_prio = lpn->lpn_sel_priority;
2269 CERROR("peer %s has no available nets\n",
2270 libcfs_nidstr(&sd->sd_dst_nid));
2275 CDEBUG(D_NET, "selected best_lpn %s\n",
2276 libcfs_net2str(best_lpn->lpn_net_id));
2278 sd->sd_best_lpni = lnet_find_best_lpni(sd->sd_best_ni,
2281 best_lpn->lpn_net_id);
2282 if (!sd->sd_best_lpni) {
2283 CERROR("peer %s is unreachable\n",
2284 libcfs_nidstr(&sd->sd_dst_nid));
2289 /* We're attempting to round robin over the remote peer
2290 * NI's so update the final destination we selected
2292 sd->sd_final_dst_lpni = sd->sd_best_lpni;
2296 * find the best route. Restrict the selection on the net of the
2297 * local NI if we've already picked the local NI to send from.
2298 * Otherwise, let's pick any route we can find and then find
2299 * a local NI we can reach the route's gateway on. Any route we
2300 * select will be reachable by virtue of the restriction we have
2301 * when adding a route.
2303 best_route = lnet_find_route_locked(best_rnet,
2304 LNET_NID_NET(src_nid),
2306 &last_route, &gwni);
2309 CERROR("no route to %s from %s\n",
2310 libcfs_nidstr(dst_nid),
2311 libcfs_nidstr(src_nid));
2317 CERROR("Internal Error. Route expected to %s from %s\n",
2318 libcfs_nidstr(dst_nid),
2319 libcfs_nidstr(src_nid));
2324 gw = best_route->lr_gateway;
2325 LASSERT(gw == gwni->lpni_peer_net->lpn_peer);
2329 * If the router checker is not active then discover the gateway here.
2330 * This ensures we are able to take advantage of multi-rail routing, but
2331 * if the router checker is active then we do not unecessarily delay
2332 * messages while the gateway is being checked by the dedicated monitor
2335 * NB: We're only checking the alive_router_check_interval here, rather
2336 * than calling lnet_router_checker_active(), because the other
2337 * conditions that are checked by that function are either
2338 * irrelevant (the_lnet.ln_routing) or must be true (list of routers
2341 if (alive_router_check_interval <= 0) {
2342 rc = lnet_initiate_peer_discovery(gwni, sd->sd_msg, sd->sd_cpt);
2347 if (!sd->sd_best_ni) {
2348 lpn = gwni->lpni_peer_net;
2349 sd->sd_best_ni = lnet_find_best_ni_on_spec_net(NULL, gw, lpn,
2352 if (!sd->sd_best_ni) {
2353 CERROR("Internal Error. Expected local ni on %s but non found: %s\n",
2354 libcfs_net2str(lpn->lpn_net_id),
2355 libcfs_nidstr(&sd->sd_src_nid));
2365 * increment the sequence number since now we're sure we're
2366 * going to use this route
2368 if (LNET_NID_IS_ANY(&sd->sd_rtr_nid)) {
2369 LASSERT(best_route && last_route);
2370 best_route->lr_seq = last_route->lr_seq + 1;
2374 if (gwni_decref && gwni)
2375 lnet_peer_ni_decref_locked(gwni);
2385 * Remote destination
2386 * Non-MR destination
2390 * Remote destination
2393 * The handling of these two cases is similar. Even though the destination
2394 * can be MR or non-MR, we'll deal directly with the router.
2397 lnet_handle_spec_router_dst(struct lnet_send_data *sd)
2400 struct lnet_peer_ni *gw_lpni = NULL;
2401 struct lnet_peer *gw_peer = NULL;
2404 sd->sd_best_ni = lnet_nid_to_ni_locked(&sd->sd_src_nid, sd->sd_cpt);
2405 if (!sd->sd_best_ni) {
2406 CERROR("Can't send to %s: src %s is not a local nid\n",
2407 libcfs_nidstr(&sd->sd_dst_nid),
2408 libcfs_nidstr(&sd->sd_src_nid));
2412 rc = lnet_handle_find_routed_path(sd, &sd->sd_dst_nid,
2413 &gw_lpni, &gw_peer);
2417 if (sd->sd_send_case & NMR_DST)
2419 * since the final destination is non-MR let's set its preferred
2420 * NID before we send
2422 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni,
2426 * We're going to send to the gw found so let's set its
2429 sd->sd_peer = gw_peer;
2430 sd->sd_best_lpni = gw_lpni;
2432 return lnet_handle_send(sd);
2435 static struct lnet_ni *
2436 lnet_find_best_ni_on_local_net(struct lnet_peer *peer, int md_cpt,
2437 struct lnet_msg *msg, bool discovery)
2439 struct lnet_peer_net *lpn = NULL;
2440 struct lnet_peer_net *best_lpn = NULL;
2441 struct lnet_net *net = NULL;
2442 struct lnet_net *best_net = NULL;
2443 struct lnet_ni *best_ni = NULL;
2444 int best_lpn_healthv = 0;
2445 int best_net_healthv = 0;
2447 __u32 best_lpn_sel_prio = LNET_MAX_SELECTION_PRIORITY;
2449 __u32 best_net_sel_prio = LNET_MAX_SELECTION_PRIORITY;
2452 /* If lp_disc_net_id is set, this peer is a router undergoing
2453 * discovery, and this message is an LNet ping, then this may be a
2454 * discovery message and we need to select an NI on the peer net
2455 * specified by lp_disc_net_id
2457 if (peer->lp_disc_net_id &&
2458 (peer->lp_state & LNET_PEER_RTR_DISCOVERY) &&
2459 lnet_msg_is_ping(msg)) {
2460 best_lpn = lnet_peer_get_net_locked(peer, peer->lp_disc_net_id);
2461 if (best_lpn && lnet_get_net_locked(best_lpn->lpn_net_id))
2462 goto select_best_ni;
2466 * The peer can have multiple interfaces, some of them can be on
2467 * the local network and others on a routed network. We should
2468 * prefer the local network. However if the local network is not
2469 * available then we need to try the routed network
2472 /* go through all the peer nets and find the best_ni */
2473 list_for_each_entry(lpn, &peer->lp_peer_nets, lpn_peer_nets) {
2475 * The peer's list of nets can contain non-local nets. We
2476 * want to only examine the local ones.
2478 net = lnet_get_net_locked(lpn->lpn_net_id);
2482 lpn_sel_prio = lpn->lpn_sel_priority;
2483 net_healthv = lnet_get_net_healthv_locked(net);
2484 net_sel_prio = net->net_sel_priority;
2486 if (!best_lpn || !best_net)
2490 "n[%s, %s] ph[%d, %d], pp[%u, %u], nh[%d, %d], np[%u, %u], ps[%u, %u], ns[%u, %u]\n",
2491 libcfs_net2str(lpn->lpn_net_id),
2492 libcfs_net2str(best_lpn->lpn_net_id),
2506 /* always select the lpn with the best health */
2507 if (best_lpn_healthv > lpn->lpn_healthv)
2509 else if (best_lpn_healthv < lpn->lpn_healthv)
2512 /* select the preferred peer and local nets */
2513 if (best_lpn_sel_prio < lpn_sel_prio)
2515 else if (best_lpn_sel_prio > lpn_sel_prio)
2518 if (best_net_healthv > net_healthv)
2520 else if (best_net_healthv < net_healthv)
2523 if (best_net_sel_prio < net_sel_prio)
2525 else if (best_net_sel_prio > net_sel_prio)
2528 if (best_lpn->lpn_seq < lpn->lpn_seq)
2530 else if (best_lpn->lpn_seq > lpn->lpn_seq)
2533 /* round robin over the local networks */
2534 if (best_net->net_seq <= net->net_seq)
2538 best_net_healthv = net_healthv;
2539 best_net_sel_prio = net_sel_prio;
2540 best_lpn_healthv = lpn->lpn_healthv;
2541 best_lpn_sel_prio = lpn_sel_prio;
2547 /* Select the best NI on the same net as best_lpn chosen
2551 CDEBUG(D_NET, "selected best_lpn %s\n",
2552 libcfs_net2str(best_lpn->lpn_net_id));
2553 best_ni = lnet_find_best_ni_on_spec_net(NULL, peer, best_lpn,
2560 static struct lnet_ni *
2561 lnet_find_existing_preferred_best_ni(struct lnet_peer_ni *lpni, int cpt)
2563 struct lnet_ni *best_ni = NULL;
2564 struct lnet_peer_net *peer_net = lpni->lpni_peer_net;
2565 struct lnet_peer_ni *lpni_entry;
2568 * We must use a consistent source address when sending to a
2569 * non-MR peer. However, a non-MR peer can have multiple NIDs
2570 * on multiple networks, and we may even need to talk to this
2571 * peer on multiple networks -- certain types of
2572 * load-balancing configuration do this.
2574 * So we need to pick the NI the peer prefers for this
2575 * particular network.
2578 list_for_each_entry(lpni_entry, &peer_net->lpn_peer_nis,
2580 if (lpni_entry->lpni_pref_nnids == 0)
2582 LASSERT(lpni_entry->lpni_pref_nnids == 1);
2583 best_ni = lnet_nid_to_ni_locked(&lpni_entry->lpni_pref.nid,
2591 /* Prerequisite: sd->sd_peer and sd->sd_best_lpni should be set */
2593 lnet_select_preferred_best_ni(struct lnet_send_data *sd)
2595 struct lnet_ni *best_ni = NULL;
2598 * We must use a consistent source address when sending to a
2599 * non-MR peer. However, a non-MR peer can have multiple NIDs
2600 * on multiple networks, and we may even need to talk to this
2601 * peer on multiple networks -- certain types of
2602 * load-balancing configuration do this.
2604 * So we need to pick the NI the peer prefers for this
2605 * particular network.
2607 * An exception is traffic on LNET_RESERVED_PORTAL. Internal LNet
2608 * traffic doesn't care which source NI is used, and we don't actually
2609 * want to restrict local recovery pings to a single source NI.
2611 if (!lnet_reserved_msg(sd->sd_msg))
2612 best_ni = lnet_find_existing_preferred_best_ni(sd->sd_best_lpni,
2616 best_ni = lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2617 sd->sd_best_lpni->lpni_peer_net,
2621 /* If there is no best_ni we don't have a route */
2623 CERROR("no path to %s from net %s\n",
2624 libcfs_nidstr(&sd->sd_best_lpni->lpni_nid),
2625 libcfs_net2str(sd->sd_best_lpni->lpni_net->net_id));
2626 return -EHOSTUNREACH;
2629 sd->sd_best_ni = best_ni;
2631 /* Set preferred NI if necessary. */
2632 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
2639 * Source not specified
2643 * always use the same source NID for NMR peers
2644 * If we've talked to that peer before then we already have a preferred
2645 * source NI associated with it. Otherwise, we select a preferred local NI
2646 * and store it in the peer
2649 lnet_handle_any_local_nmr_dst(struct lnet_send_data *sd)
2653 /* sd->sd_best_lpni is already set to the final destination */
2656 * At this point we should've created the peer ni and peer. If we
2657 * can't find it, then something went wrong. Instead of assert
2658 * output a relevant message and fail the send
2660 if (!sd->sd_best_lpni) {
2661 CERROR("Internal fault. Unable to send msg %s to %s. NID not known\n",
2662 lnet_msgtyp2str(sd->sd_msg->msg_type),
2663 libcfs_nidstr(&sd->sd_dst_nid));
2667 if (sd->sd_msg->msg_routing) {
2668 /* If I'm forwarding this message then I can choose any NI
2669 * on the destination peer net
2671 sd->sd_best_ni = lnet_find_best_ni_on_spec_net(NULL,
2673 sd->sd_best_lpni->lpni_peer_net,
2676 if (!sd->sd_best_ni) {
2677 CERROR("Unable to forward message to %s. No local NI available\n",
2678 libcfs_nidstr(&sd->sd_dst_nid));
2682 rc = lnet_select_preferred_best_ni(sd);
2685 rc = lnet_handle_send(sd);
2691 lnet_handle_any_mr_dsta(struct lnet_send_data *sd)
2694 * NOTE we've already handled the remote peer case. So we only
2695 * need to worry about the local case here.
2697 * if we're sending a response, ACK or reply, we need to send it
2698 * to the destination NID given to us. At this point we already
2699 * have the peer_ni we're suppose to send to, so just find the
2700 * best_ni on the peer net and use that. Since we're sending to an
2701 * MR peer then we can just run the selection algorithm on our
2702 * local NIs and pick the best one.
2704 if (sd->sd_send_case & SND_RESP) {
2706 lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2707 sd->sd_best_lpni->lpni_peer_net,
2711 if (!sd->sd_best_ni) {
2713 * We're not going to deal with not able to send
2714 * a response to the provided final destination
2716 CERROR("Can't send response to %s. No local NI available\n",
2717 libcfs_nidstr(&sd->sd_dst_nid));
2718 return -EHOSTUNREACH;
2721 return lnet_handle_send(sd);
2725 * If we get here that means we're sending a fresh request, PUT or
2726 * GET, so we need to run our standard selection algorithm.
2727 * First find the best local interface that's on any of the peer's
2730 sd->sd_best_ni = lnet_find_best_ni_on_local_net(sd->sd_peer,
2733 lnet_msg_discovery(sd->sd_msg));
2734 if (sd->sd_best_ni) {
2736 lnet_find_best_lpni(sd->sd_best_ni, &sd->sd_dst_nid,
2738 sd->sd_best_ni->ni_net->net_id);
2741 * if we're successful in selecting a peer_ni on the local
2742 * network, then send to it. Otherwise fall through and
2743 * try and see if we can reach it over another routed
2746 if (sd->sd_best_lpni &&
2747 nid_same(&sd->sd_best_lpni->lpni_nid,
2748 &the_lnet.ln_loni->ni_nid)) {
2750 * in case we initially started with a routed
2751 * destination, let's reset to local
2753 sd->sd_send_case &= ~REMOTE_DST;
2754 sd->sd_send_case |= LOCAL_DST;
2755 return lnet_handle_lo_send(sd);
2756 } else if (sd->sd_best_lpni) {
2758 * in case we initially started with a routed
2759 * destination, let's reset to local
2761 sd->sd_send_case &= ~REMOTE_DST;
2762 sd->sd_send_case |= LOCAL_DST;
2763 return lnet_handle_send(sd);
2766 CERROR("Internal Error. Expected to have a best_lpni: "
2768 libcfs_nidstr(&sd->sd_src_nid),
2769 libcfs_nidstr(&sd->sd_dst_nid));
2775 * Peer doesn't have a local network. Let's see if there is
2776 * a remote network we can reach it on.
2778 return PASS_THROUGH;
2783 * Source NID not specified
2788 * Source NID not speified
2789 * Remote destination
2792 * In both of these cases if we're sending a response, ACK or REPLY, then
2793 * we need to send to the destination NID provided.
2795 * In the remote case let's deal with MR routers.
2800 lnet_handle_any_mr_dst(struct lnet_send_data *sd)
2803 struct lnet_peer *gw_peer = NULL;
2804 struct lnet_peer_ni *gw_lpni = NULL;
2807 * handle sending a response to a remote peer here so we don't
2808 * have to worry about it if we hit lnet_handle_any_mr_dsta()
2810 if (sd->sd_send_case & REMOTE_DST &&
2811 sd->sd_send_case & SND_RESP) {
2812 struct lnet_peer_ni *gw;
2813 struct lnet_peer *gw_peer;
2815 rc = lnet_handle_find_routed_path(
2816 sd, &sd->sd_dst_nid, &gw, &gw_peer);
2818 CERROR("Can't send response to %s. No route available\n",
2819 libcfs_nidstr(&sd->sd_dst_nid));
2820 return -EHOSTUNREACH;
2821 } else if (rc > 0) {
2825 sd->sd_best_lpni = gw;
2826 sd->sd_peer = gw_peer;
2828 return lnet_handle_send(sd);
2832 * Even though the NID for the peer might not be on a local network,
2833 * since the peer is MR there could be other interfaces on the
2834 * local network. In that case we'd still like to prefer the local
2835 * network over the routed network. If we're unable to do that
2836 * then we select the best router among the different routed networks,
2837 * and if the router is MR then we can deal with it as such.
2839 rc = lnet_handle_any_mr_dsta(sd);
2840 if (rc != PASS_THROUGH)
2844 * Now that we must route to the destination, we must consider the
2845 * MR case, where the destination has multiple interfaces, some of
2846 * which we can route to and others we do not. For this reason we
2847 * need to select the destination which we can route to and if
2848 * there are multiple, we need to round robin.
2850 rc = lnet_handle_find_routed_path(sd, &sd->sd_dst_nid,
2851 &gw_lpni, &gw_peer);
2855 sd->sd_send_case &= ~LOCAL_DST;
2856 sd->sd_send_case |= REMOTE_DST;
2858 sd->sd_peer = gw_peer;
2859 sd->sd_best_lpni = gw_lpni;
2861 return lnet_handle_send(sd);
2865 * Source not specified
2866 * Remote destination
2869 * Must send to the specified peer NID using the same source NID that
2870 * we've used before. If it's the first time to talk to that peer then
2871 * find the source NI and assign it as preferred to that peer
2874 lnet_handle_any_router_nmr_dst(struct lnet_send_data *sd)
2877 struct lnet_peer_ni *gw_lpni = NULL;
2878 struct lnet_peer *gw_peer = NULL;
2881 * Let's see if we have a preferred NI to talk to this NMR peer
2883 sd->sd_best_ni = lnet_find_existing_preferred_best_ni(sd->sd_best_lpni,
2887 * find the router and that'll find the best NI if we didn't find
2890 rc = lnet_handle_find_routed_path(sd, &sd->sd_dst_nid, &gw_lpni,
2896 * set the best_ni we've chosen as the preferred one for
2899 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
2901 /* we'll be sending to the gw */
2902 sd->sd_best_lpni = gw_lpni;
2903 sd->sd_peer = gw_peer;
2905 return lnet_handle_send(sd);
2909 lnet_handle_send_case_locked(struct lnet_send_data *sd)
2912 * turn off the SND_RESP bit.
2913 * It will be checked in the case handling
2915 __u32 send_case = sd->sd_send_case &= ~SND_RESP ;
2917 CDEBUG(D_NET, "Source %s%s to %s %s %s destination\n",
2918 (send_case & SRC_SPEC) ? "Specified: " : "ANY",
2919 (send_case & SRC_SPEC) ? libcfs_nidstr(&sd->sd_src_nid) : "",
2920 (send_case & MR_DST) ? "MR: " : "NMR: ",
2921 libcfs_nidstr(&sd->sd_dst_nid),
2922 (send_case & LOCAL_DST) ? "local" : "routed");
2924 switch (send_case) {
2926 * For all cases where the source is specified, we should always
2927 * use the destination NID, whether it's an MR destination or not,
2928 * since we're continuing a series of related messages for the
2931 case SRC_SPEC_LOCAL_NMR_DST:
2932 return lnet_handle_spec_local_nmr_dst(sd);
2933 case SRC_SPEC_LOCAL_MR_DST:
2934 return lnet_handle_spec_local_mr_dst(sd);
2935 case SRC_SPEC_ROUTER_NMR_DST:
2936 case SRC_SPEC_ROUTER_MR_DST:
2937 return lnet_handle_spec_router_dst(sd);
2938 case SRC_ANY_LOCAL_NMR_DST:
2939 return lnet_handle_any_local_nmr_dst(sd);
2940 case SRC_ANY_LOCAL_MR_DST:
2941 case SRC_ANY_ROUTER_MR_DST:
2942 return lnet_handle_any_mr_dst(sd);
2943 case SRC_ANY_ROUTER_NMR_DST:
2944 return lnet_handle_any_router_nmr_dst(sd);
2946 CERROR("Unknown send case\n");
2952 lnet_select_pathway(struct lnet_nid *src_nid,
2953 struct lnet_nid *dst_nid,
2954 struct lnet_msg *msg,
2955 struct lnet_nid *rtr_nid)
2957 struct lnet_peer_ni *lpni;
2958 struct lnet_peer *peer;
2959 struct lnet_send_data send_data;
2962 __u32 send_case = 0;
2964 bool mr_forwarding_allowed;
2966 memset(&send_data, 0, sizeof(send_data));
2969 * get an initial CPT to use for locking. The idea here is not to
2970 * serialize the calls to select_pathway, so that as many
2971 * operations can run concurrently as possible. To do that we use
2972 * the CPT where this call is being executed. Later on when we
2973 * determine the CPT to use in lnet_message_commit, we switch the
2974 * lock and check if there was any configuration change. If none,
2975 * then we proceed, if there is, then we restart the operation.
2977 cpt = lnet_net_lock_current();
2979 md_cpt = lnet_cpt_of_md(msg->msg_md, msg->msg_offset);
2980 if (md_cpt == CFS_CPT_ANY)
2986 * If we're being asked to send to the loopback interface, there
2987 * is no need to go through any selection. We can just shortcut
2988 * the entire process and send over lolnd
2990 send_data.sd_msg = msg;
2991 send_data.sd_cpt = cpt;
2992 if (nid_is_lo0(dst_nid)) {
2993 rc = lnet_handle_lo_send(&send_data);
2994 lnet_net_unlock(cpt);
2999 * find an existing peer_ni, or create one and mark it as having been
3000 * created due to network traffic. This call will create the
3001 * peer->peer_net->peer_ni tree.
3003 lpni = lnet_peerni_by_nid_locked(dst_nid, NULL, cpt);
3005 lnet_net_unlock(cpt);
3006 return PTR_ERR(lpni);
3010 * Cache the original src_nid and rtr_nid. If we need to resend the
3011 * message then we'll need to know whether the src_nid was originally
3012 * specified for this message. If it was originally specified,
3013 * then we need to keep using the same src_nid since it's
3014 * continuing the same sequence of messages. Similarly, rtr_nid will
3015 * affect our choice of next hop.
3018 msg->msg_src_nid_param = *src_nid;
3020 msg->msg_src_nid_param = LNET_ANY_NID;
3022 msg->msg_rtr_nid_param = *rtr_nid;
3024 msg->msg_rtr_nid_param = LNET_ANY_NID;
3027 * If necessary, perform discovery on the peer that owns this peer_ni.
3028 * Note, this can result in the ownership of this peer_ni changing
3029 * to another peer object.
3031 rc = lnet_initiate_peer_discovery(lpni, msg, cpt);
3033 lnet_peer_ni_decref_locked(lpni);
3034 lnet_net_unlock(cpt);
3038 peer = lpni->lpni_peer_net->lpn_peer;
3041 * Identify the different send cases
3043 if (!src_nid || LNET_NID_IS_ANY(src_nid)) {
3044 send_case |= SRC_ANY;
3045 if (lnet_get_net_locked(LNET_NID_NET(dst_nid)))
3046 send_case |= LOCAL_DST;
3048 send_case |= REMOTE_DST;
3050 send_case |= SRC_SPEC;
3051 if (LNET_NID_NET(src_nid) == LNET_NID_NET(dst_nid))
3052 send_case |= LOCAL_DST;
3054 send_case |= REMOTE_DST;
3058 if (msg->msg_routing && (send_case & LOCAL_DST))
3061 /* Determine whether to allow MR forwarding for this message.
3062 * NB: MR forwarding is allowed if the message originator and the
3063 * destination are both MR capable, and the destination lpni that was
3064 * originally chosen by the originator is unhealthy or down.
3065 * We check the MR capability of the destination further below
3067 mr_forwarding_allowed = false;
3069 struct lnet_peer *src_lp;
3070 struct lnet_peer_ni *src_lpni;
3072 src_lpni = lnet_peerni_by_nid_locked(&msg->msg_hdr.src_nid,
3074 /* We don't fail the send if we hit any errors here. We'll just
3075 * try to send it via non-multi-rail criteria
3077 if (!IS_ERR(src_lpni)) {
3078 /* Drop ref taken by lnet_peerni_by_nid_locked() */
3079 lnet_peer_ni_decref_locked(src_lpni);
3080 src_lp = lpni->lpni_peer_net->lpn_peer;
3081 if (lnet_peer_is_multi_rail(src_lp) &&
3082 !lnet_is_peer_ni_alive(lpni))
3083 mr_forwarding_allowed = true;
3086 CDEBUG(D_NET, "msg %p MR forwarding %s\n", msg,
3087 mr_forwarding_allowed ? "allowed" : "not allowed");
3091 * Deal with the peer as NMR in the following cases:
3092 * 1. the peer is NMR
3093 * 2. We're trying to recover a specific peer NI
3094 * 3. I'm a router sending to the final destination and MR forwarding is
3095 * not allowed for this message (as determined above).
3096 * In this case the source of the message would've
3097 * already selected the final destination so my job
3098 * is to honor the selection.
3100 if (!lnet_peer_is_multi_rail(peer) || msg->msg_recovery ||
3101 (final_hop && !mr_forwarding_allowed))
3102 send_case |= NMR_DST;
3104 send_case |= MR_DST;
3106 if (lnet_msg_is_response(msg))
3107 send_case |= SND_RESP;
3109 /* assign parameters to the send_data */
3111 send_data.sd_rtr_nid = *rtr_nid;
3113 send_data.sd_rtr_nid = LNET_ANY_NID;
3115 send_data.sd_src_nid = *src_nid;
3117 send_data.sd_src_nid = LNET_ANY_NID;
3118 send_data.sd_dst_nid = *dst_nid;
3119 send_data.sd_best_lpni = lpni;
3121 * keep a pointer to the final destination in case we're going to
3122 * route, so we'll need to access it later
3124 send_data.sd_final_dst_lpni = lpni;
3125 send_data.sd_peer = peer;
3126 send_data.sd_md_cpt = md_cpt;
3127 send_data.sd_send_case = send_case;
3129 rc = lnet_handle_send_case_locked(&send_data);
3132 * Update the local cpt since send_data.sd_cpt might've been
3133 * updated as a result of calling lnet_handle_send_case_locked().
3135 cpt = send_data.sd_cpt;
3136 lnet_peer_ni_decref_locked(lpni);
3138 if (rc == REPEAT_SEND)
3141 lnet_net_unlock(cpt);
3147 lnet_send(struct lnet_nid *src_nid, struct lnet_msg *msg,
3148 struct lnet_nid *rtr_nid)
3150 struct lnet_nid *dst_nid = &msg->msg_target.nid;
3153 /* NB: ni != NULL == interface pre-determined (ACK/REPLY) */
3154 LASSERT(msg->msg_txpeer == NULL);
3155 LASSERT(msg->msg_txni == NULL);
3156 LASSERT(!msg->msg_sending);
3157 LASSERT(!msg->msg_target_is_router);
3158 LASSERT(!msg->msg_receiving);
3160 msg->msg_sending = 1;
3162 LASSERT(!msg->msg_tx_committed);
3164 rc = lnet_select_pathway(src_nid, dst_nid, msg, rtr_nid);
3166 if (rc == -EHOSTUNREACH)
3167 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_ERROR;
3169 msg->msg_health_status = LNET_MSG_STATUS_LOCAL_ERROR;
3173 if (rc == LNET_CREDIT_OK)
3174 lnet_ni_send(msg->msg_txni, msg);
3176 /* rc == LNET_CREDIT_OK or LNET_CREDIT_WAIT or LNET_DC_WAIT */
3180 enum lnet_mt_event_type {
3181 MT_TYPE_LOCAL_NI = 0,
3185 struct lnet_mt_event_info {
3186 enum lnet_mt_event_type mt_type;
3187 struct lnet_nid mt_nid;
3190 /* called with res_lock held */
3192 lnet_detach_rsp_tracker(struct lnet_libmd *md, int cpt)
3194 struct lnet_rsp_tracker *rspt;
3197 * msg has a refcount on the MD so the MD is not going away.
3198 * The rspt queue for the cpt is protected by
3199 * the lnet_net_lock(cpt). cpt is the cpt of the MD cookie.
3201 if (!md->md_rspt_ptr)
3204 rspt = md->md_rspt_ptr;
3207 LASSERT(rspt->rspt_cpt == cpt);
3209 md->md_rspt_ptr = NULL;
3211 if (LNetMDHandleIsInvalid(rspt->rspt_mdh)) {
3213 * The monitor thread has invalidated this handle because the
3214 * response timed out, but it failed to lookup the MD. That
3215 * means this response tracker is on the zombie list. We can
3216 * safely remove it under the resource lock (held by caller) and
3217 * free the response tracker block.
3219 list_del(&rspt->rspt_on_list);
3220 lnet_rspt_free(rspt, cpt);
3223 * invalidate the handle to indicate that a response has been
3224 * received, which will then lead the monitor thread to clean up
3227 LNetInvalidateMDHandle(&rspt->rspt_mdh);
3232 lnet_clean_zombie_rstqs(void)
3234 struct lnet_rsp_tracker *rspt, *tmp;
3237 cfs_cpt_for_each(i, lnet_cpt_table()) {
3238 list_for_each_entry_safe(rspt, tmp,
3239 the_lnet.ln_mt_zombie_rstqs[i],
3241 list_del(&rspt->rspt_on_list);
3242 lnet_rspt_free(rspt, i);
3246 cfs_percpt_free(the_lnet.ln_mt_zombie_rstqs);
3250 lnet_finalize_expired_responses(void)
3252 struct lnet_libmd *md;
3253 struct lnet_rsp_tracker *rspt, *tmp;
3257 if (the_lnet.ln_mt_rstq == NULL)
3260 cfs_cpt_for_each(i, lnet_cpt_table()) {
3261 LIST_HEAD(local_queue);
3264 if (!the_lnet.ln_mt_rstq[i]) {
3268 list_splice_init(the_lnet.ln_mt_rstq[i], &local_queue);
3273 list_for_each_entry_safe(rspt, tmp, &local_queue, rspt_on_list) {
3275 * The rspt mdh will be invalidated when a response
3276 * is received or whenever we want to discard the
3277 * block the monitor thread will walk the queue
3278 * and clean up any rsts with an invalid mdh.
3279 * The monitor thread will walk the queue until
3280 * the first unexpired rspt block. This means that
3281 * some rspt blocks which received their
3282 * corresponding responses will linger in the
3283 * queue until they are cleaned up eventually.
3286 if (LNetMDHandleIsInvalid(rspt->rspt_mdh)) {
3288 list_del(&rspt->rspt_on_list);
3289 lnet_rspt_free(rspt, i);
3293 if (ktime_compare(now, rspt->rspt_deadline) >= 0 ||
3294 the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN) {
3295 struct lnet_peer_ni *lpni;
3296 struct lnet_nid nid;
3298 md = lnet_handle2md(&rspt->rspt_mdh);
3300 /* MD has been queued for unlink, but
3301 * rspt hasn't been detached (Note we've
3302 * checked above that the rspt_mdh is
3303 * valid). Since we cannot lookup the MD
3304 * we're unable to detach the rspt
3305 * ourselves. Thus, move the rspt to the
3306 * zombie list where we'll wait for
3308 * 1. The remaining operations on the
3309 * MD to complete. In this case the
3310 * final operation will result in
3311 * lnet_msg_detach_md()->
3312 * lnet_detach_rsp_tracker() where
3313 * we will clean up this response
3315 * 2. LNet to shutdown. In this case
3316 * we'll wait until after all LND Nets
3317 * have shutdown and then we can
3318 * safely free any remaining response
3319 * tracker blocks on the zombie list.
3320 * Note: We need to hold the resource
3321 * lock when adding to the zombie list
3322 * because we may have concurrent access
3323 * with lnet_detach_rsp_tracker().
3325 LNetInvalidateMDHandle(&rspt->rspt_mdh);
3326 list_move(&rspt->rspt_on_list,
3327 the_lnet.ln_mt_zombie_rstqs[i]);
3331 LASSERT(md->md_rspt_ptr == rspt);
3332 md->md_rspt_ptr = NULL;
3335 LNetMDUnlink(rspt->rspt_mdh);
3337 nid = rspt->rspt_next_hop_nid;
3339 list_del(&rspt->rspt_on_list);
3340 lnet_rspt_free(rspt, i);
3342 /* If we're shutting down we just want to clean
3343 * up the rspt blocks
3345 if (the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN)
3349 the_lnet.ln_counters[i]->lct_health.lch_response_timeout_count++;
3353 "Response timeout: md = %p: nid = %s\n",
3354 md, libcfs_nidstr(&nid));
3357 * If there is a timeout on the response
3358 * from the next hop decrement its health
3359 * value so that we don't use it
3362 lpni = lnet_peer_ni_find_locked(&nid);
3364 lnet_handle_remote_failure_locked(lpni);
3365 lnet_peer_ni_decref_locked(lpni);
3374 if (!list_empty(&local_queue)) {
3376 list_splice(&local_queue, the_lnet.ln_mt_rstq[i]);
3383 lnet_resend_pending_msgs_locked(struct list_head *resendq, int cpt)
3385 struct lnet_msg *msg;
3387 while (!list_empty(resendq)) {
3388 struct lnet_peer_ni *lpni;
3390 msg = list_first_entry(resendq, struct lnet_msg,
3393 list_del_init(&msg->msg_list);
3395 lpni = lnet_peer_ni_find_locked(&msg->msg_hdr.dest_nid);
3397 lnet_net_unlock(cpt);
3398 CERROR("Expected that a peer is already created for %s\n",
3399 libcfs_nidstr(&msg->msg_hdr.dest_nid));
3400 msg->msg_no_resend = true;
3401 lnet_finalize(msg, -EFAULT);
3406 lnet_peer_ni_decref_locked(lpni);
3408 lnet_net_unlock(cpt);
3409 CDEBUG(D_NET, "resending %s->%s: %s recovery %d try# %d\n",
3410 libcfs_nidstr(&msg->msg_src_nid_param),
3411 libcfs_idstr(&msg->msg_target),
3412 lnet_msgtyp2str(msg->msg_type),
3414 msg->msg_retry_count);
3415 rc = lnet_send(&msg->msg_src_nid_param, msg,
3416 &msg->msg_rtr_nid_param);
3418 CERROR("Error sending %s to %s: %d\n",
3419 lnet_msgtyp2str(msg->msg_type),
3420 libcfs_idstr(&msg->msg_target), rc);
3421 msg->msg_no_resend = true;
3422 lnet_finalize(msg, rc);
3426 the_lnet.ln_counters[cpt]->lct_health.lch_resend_count++;
3432 lnet_resend_pending_msgs(void)
3436 cfs_cpt_for_each(i, lnet_cpt_table()) {
3438 lnet_resend_pending_msgs_locked(the_lnet.ln_mt_resendqs[i], i);
3443 /* called with cpt and ni_lock held */
3445 lnet_unlink_ni_recovery_mdh_locked(struct lnet_ni *ni, int cpt, bool force)
3447 struct lnet_handle_md recovery_mdh;
3449 LNetInvalidateMDHandle(&recovery_mdh);
3451 if (ni->ni_recovery_state & LNET_NI_RECOVERY_PENDING ||
3453 recovery_mdh = ni->ni_ping_mdh;
3454 LNetInvalidateMDHandle(&ni->ni_ping_mdh);
3457 lnet_net_unlock(cpt);
3458 if (!LNetMDHandleIsInvalid(recovery_mdh))
3459 LNetMDUnlink(recovery_mdh);
3464 /* Returns the total number of local NIs in recovery.
3465 * Records up to @arrsz of the associated NIDs in the @nidarr array
3468 lnet_recover_local_nis(struct lnet_nid *nidarr, unsigned int arrsz)
3470 struct lnet_mt_event_info *ev_info;
3471 LIST_HEAD(processed_list);
3472 LIST_HEAD(local_queue);
3473 struct lnet_handle_md mdh;
3474 struct lnet_ni *tmp;
3476 struct lnet_nid nid;
3480 unsigned int nnis = 0;
3483 * splice the recovery queue on a local queue. We will iterate
3484 * through the local queue and update it as needed. Once we're
3485 * done with the traversal, we'll splice the local queue back on
3486 * the head of the ln_mt_localNIRecovq. Any newly added local NIs
3487 * will be traversed in the next iteration.
3490 list_splice_init(&the_lnet.ln_mt_localNIRecovq,
3494 now = ktime_get_seconds();
3496 list_for_each_entry_safe(ni, tmp, &local_queue, ni_recovery) {
3498 * if an NI is being deleted or it is now healthy, there
3499 * is no need to keep it around in the recovery queue.
3500 * The monitor thread is the only thread responsible for
3501 * removing the NI from the recovery queue.
3502 * Multiple threads can be adding NIs to the recovery
3505 healthv = atomic_read(&ni->ni_healthv);
3509 if (ni->ni_state != LNET_NI_STATE_ACTIVE ||
3510 healthv == LNET_MAX_HEALTH_VALUE) {
3511 list_del_init(&ni->ni_recovery);
3512 lnet_unlink_ni_recovery_mdh_locked(ni, 0, false);
3514 lnet_ni_decref_locked(ni, 0);
3520 nidarr[nnis] = ni->ni_nid;
3524 * if the local NI failed recovery we must unlink the md.
3525 * But we want to keep the local_ni on the recovery queue
3526 * so we can continue the attempts to recover it.
3528 if (ni->ni_recovery_state & LNET_NI_RECOVERY_FAILED) {
3529 lnet_unlink_ni_recovery_mdh_locked(ni, 0, true);
3530 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_FAILED;
3536 if (now < ni->ni_next_ping) {
3543 CDEBUG(D_NET, "attempting to recover local ni: %s\n",
3544 libcfs_nidstr(&ni->ni_nid));
3547 if (!(ni->ni_recovery_state & LNET_NI_RECOVERY_PENDING)) {
3548 ni->ni_recovery_state |= LNET_NI_RECOVERY_PENDING;
3551 LIBCFS_ALLOC(ev_info, sizeof(*ev_info));
3553 CERROR("out of memory. Can't recover %s\n",
3554 libcfs_nidstr(&ni->ni_nid));
3556 ni->ni_recovery_state &=
3557 ~LNET_NI_RECOVERY_PENDING;
3562 mdh = ni->ni_ping_mdh;
3564 * Invalidate the ni mdh in case it's deleted.
3565 * We'll unlink the mdh in this case below.
3567 LNetInvalidateMDHandle(&ni->ni_ping_mdh);
3571 * remove the NI from the local queue and drop the
3572 * reference count to it while we're recovering
3573 * it. The reason for that, is that the NI could
3574 * be deleted, and the way the code is structured
3575 * is if we don't drop the NI, then the deletion
3576 * code will enter a loop waiting for the
3577 * reference count to be removed while holding the
3578 * ln_mutex_lock(). When we look up the peer to
3579 * send to in lnet_select_pathway() we will try to
3580 * lock the ln_mutex_lock() as well, leading to
3581 * a deadlock. By dropping the refcount and
3582 * removing it from the list, we allow for the NI
3583 * to be removed, then we use the cached NID to
3584 * look it up again. If it's gone, then we just
3585 * continue examining the rest of the queue.
3588 list_del_init(&ni->ni_recovery);
3589 lnet_ni_decref_locked(ni, 0);
3592 ev_info->mt_type = MT_TYPE_LOCAL_NI;
3593 ev_info->mt_nid = nid;
3594 rc = lnet_send_ping(&nid, &mdh, LNET_PING_INFO_MIN_SIZE,
3595 ev_info, the_lnet.ln_mt_handler,
3597 /* lookup the nid again */
3599 ni = lnet_nid_to_ni_locked(&nid, 0);
3602 * the NI has been deleted when we dropped
3609 ni->ni_ping_count++;
3611 ni->ni_ping_mdh = mdh;
3612 lnet_ni_add_to_recoveryq_locked(ni, &processed_list,
3617 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_PENDING;
3626 * put back the remaining NIs on the ln_mt_localNIRecovq to be
3627 * reexamined in the next iteration.
3629 list_splice_init(&processed_list, &local_queue);
3631 list_splice(&local_queue, &the_lnet.ln_mt_localNIRecovq);
3638 lnet_resendqs_create(void)
3640 struct list_head **resendqs;
3641 resendqs = lnet_create_array_of_queues();
3646 lnet_net_lock(LNET_LOCK_EX);
3647 the_lnet.ln_mt_resendqs = resendqs;
3648 lnet_net_unlock(LNET_LOCK_EX);
3654 lnet_clean_local_ni_recoveryq(void)
3658 /* This is only called when the monitor thread has stopped */
3661 while ((ni = list_first_entry_or_null(&the_lnet.ln_mt_localNIRecovq,
3663 ni_recovery)) != NULL) {
3664 list_del_init(&ni->ni_recovery);
3666 lnet_unlink_ni_recovery_mdh_locked(ni, 0, true);
3668 lnet_ni_decref_locked(ni, 0);
3675 lnet_unlink_lpni_recovery_mdh_locked(struct lnet_peer_ni *lpni, int cpt,
3678 struct lnet_handle_md recovery_mdh;
3680 LNetInvalidateMDHandle(&recovery_mdh);
3682 if (lpni->lpni_state & LNET_PEER_NI_RECOVERY_PENDING || force) {
3683 recovery_mdh = lpni->lpni_recovery_ping_mdh;
3684 LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
3686 spin_unlock(&lpni->lpni_lock);
3687 lnet_net_unlock(cpt);
3688 if (!LNetMDHandleIsInvalid(recovery_mdh))
3689 LNetMDUnlink(recovery_mdh);
3691 spin_lock(&lpni->lpni_lock);
3695 lnet_clean_peer_ni_recoveryq(void)
3697 struct lnet_peer_ni *lpni, *tmp;
3699 lnet_net_lock(LNET_LOCK_EX);
3701 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_mt_peerNIRecovq,
3703 list_del_init(&lpni->lpni_recovery);
3704 spin_lock(&lpni->lpni_lock);
3705 lnet_unlink_lpni_recovery_mdh_locked(lpni, LNET_LOCK_EX, true);
3706 spin_unlock(&lpni->lpni_lock);
3707 lnet_peer_ni_decref_locked(lpni);
3710 lnet_net_unlock(LNET_LOCK_EX);
3714 lnet_clean_resendqs(void)
3716 struct lnet_msg *msg, *tmp;
3720 cfs_cpt_for_each(i, lnet_cpt_table()) {
3722 list_splice_init(the_lnet.ln_mt_resendqs[i], &msgs);
3724 list_for_each_entry_safe(msg, tmp, &msgs, msg_list) {
3725 list_del_init(&msg->msg_list);
3726 msg->msg_no_resend = true;
3727 lnet_finalize(msg, -ESHUTDOWN);
3731 cfs_percpt_free(the_lnet.ln_mt_resendqs);
3734 /* Returns the total number of peer NIs in recovery.
3735 * Records up to @arrsz of the associated NIDs in the @nidarr array
3738 lnet_recover_peer_nis(struct lnet_nid *nidarr, unsigned int arrsz)
3740 struct lnet_mt_event_info *ev_info;
3741 LIST_HEAD(processed_list);
3742 LIST_HEAD(local_queue);
3743 struct lnet_handle_md mdh;
3744 struct lnet_peer_ni *lpni;
3745 struct lnet_peer_ni *tmp;
3746 struct lnet_nid nid;
3750 unsigned int nlpnis = 0;
3753 * Always use cpt 0 for locking across all interactions with
3754 * ln_mt_peerNIRecovq
3757 list_splice_init(&the_lnet.ln_mt_peerNIRecovq,
3761 now = ktime_get_seconds();
3763 list_for_each_entry_safe(lpni, tmp, &local_queue,
3766 * The same protection strategy is used here as is in the
3767 * local recovery case.
3770 healthv = atomic_read(&lpni->lpni_healthv);
3771 spin_lock(&lpni->lpni_lock);
3772 if (lpni->lpni_state & LNET_PEER_NI_DELETING ||
3773 healthv == LNET_MAX_HEALTH_VALUE) {
3774 list_del_init(&lpni->lpni_recovery);
3775 lnet_unlink_lpni_recovery_mdh_locked(lpni, 0, false);
3776 spin_unlock(&lpni->lpni_lock);
3777 lnet_peer_ni_decref_locked(lpni);
3783 nidarr[nlpnis] = lpni->lpni_nid;
3787 * If the peer NI has failed recovery we must unlink the
3788 * md. But we want to keep the peer ni on the recovery
3789 * queue so we can try to continue recovering it
3791 if (lpni->lpni_state & LNET_PEER_NI_RECOVERY_FAILED) {
3792 lnet_unlink_lpni_recovery_mdh_locked(lpni, 0, true);
3793 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_FAILED;
3796 spin_unlock(&lpni->lpni_lock);
3798 if (now < lpni->lpni_next_ping) {
3806 * NOTE: we're racing with peer deletion from user space.
3807 * It's possible that a peer is deleted after we check its
3808 * state. In this case the recovery can create a new peer
3810 spin_lock(&lpni->lpni_lock);
3811 if (!(lpni->lpni_state & LNET_PEER_NI_RECOVERY_PENDING) &&
3812 !(lpni->lpni_state & LNET_PEER_NI_DELETING)) {
3813 lpni->lpni_state |= LNET_PEER_NI_RECOVERY_PENDING;
3814 spin_unlock(&lpni->lpni_lock);
3816 LIBCFS_ALLOC(ev_info, sizeof(*ev_info));
3818 CERROR("out of memory. Can't recover %s\n",
3819 libcfs_nidstr(&lpni->lpni_nid));
3820 spin_lock(&lpni->lpni_lock);
3821 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3822 spin_unlock(&lpni->lpni_lock);
3826 /* look at the comments in lnet_recover_local_nis() */
3827 mdh = lpni->lpni_recovery_ping_mdh;
3828 nid = lpni->lpni_nid;
3829 LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
3831 list_del_init(&lpni->lpni_recovery);
3832 lnet_peer_ni_decref_locked(lpni);
3835 ev_info->mt_type = MT_TYPE_PEER_NI;
3836 ev_info->mt_nid = nid;
3837 rc = lnet_send_ping(&nid, &mdh, LNET_PING_INFO_MIN_SIZE,
3838 ev_info, the_lnet.ln_mt_handler,
3842 * lnet_peer_ni_find_locked() grabs a refcount for
3843 * us. No need to take it explicitly.
3845 lpni = lnet_peer_ni_find_locked(&nid);
3852 lpni->lpni_ping_count++;
3854 lpni->lpni_recovery_ping_mdh = mdh;
3856 lnet_peer_ni_add_to_recoveryq_locked(lpni,
3860 spin_lock(&lpni->lpni_lock);
3861 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3862 spin_unlock(&lpni->lpni_lock);
3865 /* Drop the ref taken by lnet_peer_ni_find_locked() */
3866 lnet_peer_ni_decref_locked(lpni);
3869 spin_unlock(&lpni->lpni_lock);
3873 list_splice_init(&processed_list, &local_queue);
3875 list_splice(&local_queue, &the_lnet.ln_mt_peerNIRecovq);
3881 #define LNET_MAX_NNIDS 20
3882 /* @nids is array of nids that are in recovery. It has max size of
3884 * @nnids is the total number of nids that are in recovery. It can be
3885 * larger than LNET_MAX_NNIDS.
3886 * @local tells us whether these are local or peer NIs in recovery.
3889 lnet_print_recovery_list(struct lnet_nid *nids, unsigned int nnids,
3892 static bool printed;
3902 arrsz = nnids < LNET_MAX_NNIDS ? nnids : LNET_MAX_NNIDS;
3904 /* Printing arrsz NIDs, each has max size LNET_NIDSTR_SIZE, a comma
3905 * and space for each nid after the first (2 * (arrsz - 1)),
3906 * + 1 for terminating null byte
3908 bufsz = (arrsz * LNET_NIDSTR_SIZE) + (2 * (arrsz - 1)) + 1;
3909 LIBCFS_ALLOC(buf, bufsz);
3911 LCONSOLE(D_INFO, "%u %s NIs in recovery\n",
3912 nnids, local ? "local" : "peer");
3917 tmp += sprintf(tmp, "%s", libcfs_nidstr(&nids[0]));
3918 for (i = 1; i < arrsz; i++)
3919 tmp += sprintf(tmp, ", %s", libcfs_nidstr(&nids[i]));
3921 /* LCONSOLE() used to avoid rate limiting when we have both local
3922 * and peer NIs in recovery
3924 LCONSOLE(D_INFO, "%u %s NIs in recovery (showing %u): %s\n",
3925 nnids, local ? "local" : "peer", arrsz, buf);
3927 LIBCFS_FREE(buf, bufsz);
3929 if (!printed && nnids > LNET_MAX_NNIDS) {
3930 LCONSOLE(D_INFO, "See full list with 'lnetctl debug recovery -(p|l)'\n");
3936 lnet_health_update_console(struct lnet_nid *lnids, unsigned int nnis,
3937 struct lnet_nid *rnids, unsigned int nlpnis,
3940 static time64_t next_ni_update;
3941 static time64_t next_lpni_update;
3942 static time64_t next_msg_update;
3943 static unsigned int num_ni_updates;
3944 static unsigned int num_lpni_updates;
3945 static unsigned int num_msg_updates = 1;
3948 if (now >= next_ni_update) {
3950 lnet_print_recovery_list(lnids, nnis, true);
3951 if (num_ni_updates < 5)
3953 next_ni_update = now + (60 * num_ni_updates);
3961 if (now >= next_lpni_update) {
3963 lnet_print_recovery_list(rnids, nlpnis, false);
3964 if (num_lpni_updates < 5)
3966 next_lpni_update = now + (60 * num_lpni_updates);
3968 next_lpni_update = 0;
3969 num_lpni_updates = 0;
3973 /* Let late_count accumulate for 60 seconds */
3974 if (unlikely(!next_msg_update))
3975 next_msg_update = now + 60;
3977 if (now >= next_msg_update) {
3978 late_count = atomic_read(&the_lnet.ln_late_msg_count);
3981 s64 avg = atomic64_xchg(&the_lnet.ln_late_msg_nsecs, 0) /
3982 atomic_xchg(&the_lnet.ln_late_msg_count, 0);
3984 if (avg > NSEC_PER_SEC) {
3985 unsigned int avg_msec;
3987 avg_msec = do_div(avg, NSEC_PER_SEC) /
3989 LCONSOLE_INFO("%u messages in past %us over their deadline by avg %lld.%03us\n",
3991 (60 * num_msg_updates), avg,
3994 if (num_msg_updates < 5)
3996 next_msg_update = now + (60 * num_msg_updates);
3999 next_msg_update = now + 60;
4000 num_msg_updates = 1;
4006 lnet_monitor_thread(void *arg)
4008 time64_t rsp_timeout = 0;
4011 unsigned int nlpnis;
4012 struct lnet_nid local_nids[LNET_MAX_NNIDS];
4013 struct lnet_nid peer_nids[LNET_MAX_NNIDS];
4015 wait_for_completion(&the_lnet.ln_started);
4018 * The monitor thread takes care of the following:
4019 * 1. Checks the aliveness of routers
4020 * 2. Checks if there are messages on the resend queue to resend
4022 * 3. Checks if there are any NIs on the local recovery queue and
4024 * 4. Checks if there are any NIs on the remote recovery queue
4026 * 5. Updates the ping buffer if requested by LNDs upon interface
4029 while (the_lnet.ln_mt_state == LNET_MT_STATE_RUNNING) {
4030 now = ktime_get_real_seconds();
4032 if (lnet_router_checker_active())
4033 lnet_check_routers();
4035 lnet_resend_pending_msgs();
4037 if (now >= rsp_timeout) {
4038 lnet_finalize_expired_responses();
4039 rsp_timeout = now + (lnet_transaction_timeout / 2);
4042 nnis = lnet_recover_local_nis(local_nids, LNET_MAX_NNIDS);
4043 nlpnis = lnet_recover_peer_nis(peer_nids, LNET_MAX_NNIDS);
4044 lnet_health_update_console(local_nids, nnis, peer_nids, nlpnis,
4047 lnet_queue_ping_buffer_update();
4050 * TODO do we need to check if we should sleep without
4051 * timeout? Technically, an active system will always
4052 * have messages in flight so this check will always
4053 * evaluate to false. And on an idle system do we care
4054 * if we wake up every 1 second? Although, we've seen
4055 * cases where we get a complaint that an idle thread
4056 * is waking up unnecessarily.
4058 wait_for_completion_interruptible_timeout(
4059 &the_lnet.ln_mt_wait_complete,
4060 cfs_time_seconds(1));
4061 /* Must re-init the completion before testing anything,
4062 * including ln_mt_state.
4064 reinit_completion(&the_lnet.ln_mt_wait_complete);
4068 lnet_net_lock(LNET_LOCK_EX);
4069 the_lnet.ln_mt_state = LNET_MT_STATE_SHUTDOWN;
4070 lnet_net_unlock(LNET_LOCK_EX);
4072 /* signal that the monitor thread is exiting */
4073 up(&the_lnet.ln_mt_signal);
4081 * Returns == 0 if success
4082 * Returns > 0 if LNetMDBind or prior fails
4083 * Returns < 0 if LNetGet fails
4086 lnet_send_ping(struct lnet_nid *dest_nid,
4087 struct lnet_handle_md *mdh, int bytes,
4088 void *user_data, lnet_handler_t handler, bool recovery)
4090 struct lnet_md md = { NULL };
4091 struct lnet_processid id;
4092 struct lnet_ping_buffer *pbuf;
4095 if (LNET_NID_IS_ANY(dest_nid)) {
4100 pbuf = lnet_ping_buffer_alloc(bytes, GFP_NOFS);
4106 /* initialize md content */
4107 md.start = &pbuf->pb_info;
4109 md.threshold = 2; /* GET/REPLY */
4111 md.options = LNET_MD_TRUNCATE | LNET_MD_TRACK_RESPONSE;
4112 md.user_ptr = user_data;
4113 md.handler = handler;
4115 rc = LNetMDBind(&md, LNET_UNLINK, mdh);
4117 lnet_ping_buffer_decref(pbuf);
4118 CERROR("Can't bind MD: %d\n", rc);
4119 rc = -rc; /* change the rc to positive */
4122 id.pid = LNET_PID_LUSTRE;
4125 rc = LNetGet(NULL, *mdh, &id,
4126 LNET_RESERVED_PORTAL,
4127 LNET_PROTO_PING_MATCHBITS, 0, recovery);
4130 goto fail_unlink_md;
4136 LNetInvalidateMDHandle(mdh);
4142 lnet_handle_recovery_reply(struct lnet_mt_event_info *ev_info,
4143 int status, bool send, bool unlink_event)
4145 struct lnet_nid *nid = &ev_info->mt_nid;
4147 if (ev_info->mt_type == MT_TYPE_LOCAL_NI) {
4151 ni = lnet_nid_to_ni_locked(nid, 0);
4157 if (!send || (send && status != 0))
4158 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_PENDING;
4160 ni->ni_recovery_state |= LNET_NI_RECOVERY_FAILED;
4165 CDEBUG(D_NET, "local NI (%s) recovery failed with %d\n",
4166 libcfs_nidstr(nid), status);
4170 * need to increment healthv for the ni here, because in
4171 * the lnet_finalize() path we don't have access to this
4172 * NI. And in order to get access to it, we'll need to
4173 * carry forward too much information.
4174 * In the peer case, it'll naturally be incremented
4177 lnet_inc_healthv(&ni->ni_healthv,
4178 lnet_health_sensitivity);
4180 struct lnet_peer_ni *lpni;
4183 cpt = lnet_net_lock_current();
4184 lpni = lnet_peer_ni_find_locked(nid);
4186 lnet_net_unlock(cpt);
4189 spin_lock(&lpni->lpni_lock);
4190 if (!send || (send && status != 0))
4191 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
4193 lpni->lpni_state |= LNET_PEER_NI_RECOVERY_FAILED;
4194 spin_unlock(&lpni->lpni_lock);
4195 lnet_peer_ni_decref_locked(lpni);
4196 lnet_net_unlock(cpt);
4199 CDEBUG(D_NET, "peer NI (%s) recovery failed with %d\n",
4200 libcfs_nidstr(nid), status);
4205 lnet_mt_event_handler(struct lnet_event *event)
4207 struct lnet_mt_event_info *ev_info = event->md_user_ptr;
4208 struct lnet_ping_buffer *pbuf;
4210 /* TODO: remove assert */
4211 LASSERT(event->type == LNET_EVENT_REPLY ||
4212 event->type == LNET_EVENT_SEND ||
4213 event->type == LNET_EVENT_UNLINK);
4215 CDEBUG(D_NET, "Received event: %d status: %d\n", event->type,
4218 switch (event->type) {
4219 case LNET_EVENT_UNLINK:
4220 CDEBUG(D_NET, "%s recovery ping unlinked\n",
4221 libcfs_nidstr(&ev_info->mt_nid));
4223 case LNET_EVENT_REPLY:
4224 lnet_handle_recovery_reply(ev_info, event->status, false,
4225 event->type == LNET_EVENT_UNLINK);
4227 case LNET_EVENT_SEND:
4228 CDEBUG(D_NET, "%s recovery message sent %s:%d\n",
4229 libcfs_nidstr(&ev_info->mt_nid),
4230 (event->status) ? "unsuccessfully" :
4231 "successfully", event->status);
4232 lnet_handle_recovery_reply(ev_info, event->status, true, false);
4235 CERROR("Unexpected event: %d\n", event->type);
4238 if (event->unlinked) {
4239 LIBCFS_FREE(ev_info, sizeof(*ev_info));
4240 pbuf = LNET_PING_INFO_TO_BUFFER(event->md_start);
4241 lnet_ping_buffer_decref(pbuf);
4246 lnet_rsp_tracker_create(void)
4248 struct list_head **rstqs;
4249 rstqs = lnet_create_array_of_queues();
4254 the_lnet.ln_mt_rstq = rstqs;
4260 lnet_rsp_tracker_clean(void)
4262 lnet_finalize_expired_responses();
4264 cfs_percpt_free(the_lnet.ln_mt_rstq);
4265 the_lnet.ln_mt_rstq = NULL;
4268 int lnet_monitor_thr_start(void)
4271 struct task_struct *task;
4273 if (the_lnet.ln_mt_state != LNET_MT_STATE_SHUTDOWN)
4276 rc = lnet_resendqs_create();
4280 rc = lnet_rsp_tracker_create();
4284 the_lnet.ln_pb_update_wq = alloc_workqueue("lnetpb_wq",
4287 if (!the_lnet.ln_pb_update_wq) {
4289 CERROR("Failed to allocate LNet ping buffer workqueue\n");
4292 atomic_set(&the_lnet.ln_pb_update_ready, 1);
4294 sema_init(&the_lnet.ln_mt_signal, 0);
4296 lnet_net_lock(LNET_LOCK_EX);
4297 the_lnet.ln_mt_state = LNET_MT_STATE_RUNNING;
4298 lnet_net_unlock(LNET_LOCK_EX);
4299 task = kthread_run(lnet_monitor_thread, NULL, "monitor_thread");
4302 CERROR("Can't start monitor thread: %d\n", rc);
4309 lnet_net_lock(LNET_LOCK_EX);
4310 the_lnet.ln_mt_state = LNET_MT_STATE_STOPPING;
4311 lnet_net_unlock(LNET_LOCK_EX);
4312 /* block until event callback signals exit */
4313 down(&the_lnet.ln_mt_signal);
4315 lnet_net_lock(LNET_LOCK_EX);
4316 the_lnet.ln_mt_state = LNET_MT_STATE_SHUTDOWN;
4317 lnet_net_unlock(LNET_LOCK_EX);
4318 lnet_rsp_tracker_clean();
4319 lnet_clean_local_ni_recoveryq();
4320 lnet_clean_peer_ni_recoveryq();
4321 lnet_clean_resendqs();
4322 the_lnet.ln_mt_handler = NULL;
4325 lnet_rsp_tracker_clean();
4326 lnet_clean_local_ni_recoveryq();
4327 lnet_clean_peer_ni_recoveryq();
4328 lnet_clean_resendqs();
4332 void lnet_monitor_thr_stop(void)
4334 if (the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN)
4337 LASSERT(the_lnet.ln_mt_state == LNET_MT_STATE_RUNNING);
4339 /* clean up the ping buffer update workqueue before telling
4340 * the monitor thread to shut down to avoid getting stuck
4341 * on pending messages
4343 mutex_unlock(&the_lnet.ln_api_mutex);
4344 flush_workqueue(the_lnet.ln_pb_update_wq);
4345 destroy_workqueue(the_lnet.ln_pb_update_wq);
4346 atomic_set(&the_lnet.ln_pb_update_ready, 0);
4347 the_lnet.ln_pb_update_wq = NULL;
4348 mutex_lock(&the_lnet.ln_api_mutex);
4350 lnet_net_lock(LNET_LOCK_EX);
4351 the_lnet.ln_mt_state = LNET_MT_STATE_STOPPING;
4352 lnet_net_unlock(LNET_LOCK_EX);
4354 /* tell the monitor thread that we're shutting down */
4355 complete(&the_lnet.ln_mt_wait_complete);
4357 /* block until monitor thread signals that it's done */
4358 mutex_unlock(&the_lnet.ln_api_mutex);
4359 down(&the_lnet.ln_mt_signal);
4360 mutex_lock(&the_lnet.ln_api_mutex);
4361 LASSERT(the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN);
4363 /* perform cleanup tasks */
4364 lnet_rsp_tracker_clean();
4365 lnet_clean_local_ni_recoveryq();
4366 lnet_clean_peer_ni_recoveryq();
4367 lnet_clean_resendqs();
4371 lnet_drop_message(struct lnet_ni *ni, int cpt, void *private, unsigned int nob,
4375 lnet_incr_stats(&ni->ni_stats, msg_type, LNET_STATS_TYPE_DROP);
4376 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
4377 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length += nob;
4378 lnet_net_unlock(cpt);
4380 lnet_ni_recv(ni, private, NULL, 0, 0, 0, nob);
4384 lnet_recv_put(struct lnet_ni *ni, struct lnet_msg *msg)
4386 struct lnet_hdr *hdr = &msg->msg_hdr;
4388 if (msg->msg_wanted != 0)
4389 lnet_setpayloadbuffer(msg);
4391 lnet_build_msg_event(msg, LNET_EVENT_PUT);
4393 /* Must I ACK? If so I'll grab the ack_wmd out of the header and put
4394 * it back into the ACK during lnet_finalize() */
4395 msg->msg_ack = (!lnet_is_wire_handle_none(&hdr->msg.put.ack_wmd) &&
4396 (msg->msg_md->md_options & LNET_MD_ACK_DISABLE) == 0);
4398 lnet_ni_recv(ni, msg->msg_private, msg, msg->msg_rx_delayed,
4399 msg->msg_offset, msg->msg_wanted, hdr->payload_length);
4403 lnet_parse_put(struct lnet_ni *ni, struct lnet_msg *msg)
4405 struct lnet_hdr *hdr = &msg->msg_hdr;
4406 struct lnet_match_info info;
4410 /* Convert put fields to host byte order */
4411 hdr->msg.put.match_bits = le64_to_cpu(hdr->msg.put.match_bits);
4412 hdr->msg.put.ptl_index = le32_to_cpu(hdr->msg.put.ptl_index);
4413 hdr->msg.put.offset = le32_to_cpu(hdr->msg.put.offset);
4415 /* Primary peer NID. */
4416 info.mi_id.nid = msg->msg_initiator;
4417 info.mi_id.pid = hdr->src_pid;
4418 info.mi_opc = LNET_MD_OP_PUT;
4419 info.mi_portal = hdr->msg.put.ptl_index;
4420 info.mi_rlength = hdr->payload_length;
4421 info.mi_roffset = hdr->msg.put.offset;
4422 info.mi_mbits = hdr->msg.put.match_bits;
4423 info.mi_cpt = lnet_nid2cpt(&msg->msg_initiator, ni);
4425 msg->msg_rx_ready_delay = ni->ni_net->net_lnd->lnd_eager_recv == NULL;
4426 ready_delay = msg->msg_rx_ready_delay;
4429 rc = lnet_ptl_match_md(&info, msg);
4434 case LNET_MATCHMD_OK:
4435 lnet_recv_put(ni, msg);
4438 case LNET_MATCHMD_NONE:
4440 /* no eager_recv or has already called it, should
4441 * have been attached on delayed list */
4444 rc = lnet_ni_eager_recv(ni, msg);
4451 case LNET_MATCHMD_DROP:
4452 CNETERR("Dropping PUT from %s portal %d match %llu"
4453 " offset %d length %d: %d\n",
4454 libcfs_idstr(&info.mi_id), info.mi_portal,
4455 info.mi_mbits, info.mi_roffset, info.mi_rlength, rc);
4457 return -ENOENT; /* -ve: OK but no match */
4462 lnet_parse_get(struct lnet_ni *ni, struct lnet_msg *msg, int rdma_get)
4464 struct lnet_match_info info;
4465 struct lnet_hdr *hdr = &msg->msg_hdr;
4466 struct lnet_processid source_id;
4467 struct lnet_handle_wire reply_wmd;
4470 /* Convert get fields to host byte order */
4471 hdr->msg.get.match_bits = le64_to_cpu(hdr->msg.get.match_bits);
4472 hdr->msg.get.ptl_index = le32_to_cpu(hdr->msg.get.ptl_index);
4473 hdr->msg.get.sink_length = le32_to_cpu(hdr->msg.get.sink_length);
4474 hdr->msg.get.src_offset = le32_to_cpu(hdr->msg.get.src_offset);
4476 source_id.nid = hdr->src_nid;
4477 source_id.pid = hdr->src_pid;
4478 /* Primary peer NID */
4479 info.mi_id.nid = msg->msg_initiator;
4480 info.mi_id.pid = hdr->src_pid;
4481 info.mi_opc = LNET_MD_OP_GET;
4482 info.mi_portal = hdr->msg.get.ptl_index;
4483 info.mi_rlength = hdr->msg.get.sink_length;
4484 info.mi_roffset = hdr->msg.get.src_offset;
4485 info.mi_mbits = hdr->msg.get.match_bits;
4486 info.mi_cpt = lnet_nid2cpt(&msg->msg_initiator, ni);
4488 rc = lnet_ptl_match_md(&info, msg);
4489 if (rc == LNET_MATCHMD_DROP) {
4490 CNETERR("Dropping GET from %s portal %d match %llu"
4491 " offset %d length %d\n",
4492 libcfs_idstr(&info.mi_id), info.mi_portal,
4493 info.mi_mbits, info.mi_roffset, info.mi_rlength);
4494 return -ENOENT; /* -ve: OK but no match */
4497 LASSERT(rc == LNET_MATCHMD_OK);
4499 lnet_build_msg_event(msg, LNET_EVENT_GET);
4501 reply_wmd = hdr->msg.get.return_wmd;
4503 lnet_prep_send(msg, LNET_MSG_REPLY, &source_id,
4504 msg->msg_offset, msg->msg_wanted);
4506 msg->msg_hdr.msg.reply.dst_wmd = reply_wmd;
4509 /* The LND completes the REPLY from her recv procedure */
4510 lnet_ni_recv(ni, msg->msg_private, msg, 0,
4511 msg->msg_offset, msg->msg_len, msg->msg_len);
4515 lnet_ni_recv(ni, msg->msg_private, NULL, 0, 0, 0, 0);
4516 msg->msg_receiving = 0;
4518 rc = lnet_send(&ni->ni_nid, msg, &msg->msg_from);
4520 /* didn't get as far as lnet_ni_send() */
4521 CERROR("%s: Unable to send REPLY for GET from %s: %d\n",
4522 libcfs_nidstr(&ni->ni_nid),
4523 libcfs_idstr(&info.mi_id), rc);
4525 lnet_finalize(msg, rc);
4532 lnet_parse_reply(struct lnet_ni *ni, struct lnet_msg *msg)
4534 void *private = msg->msg_private;
4535 struct lnet_hdr *hdr = &msg->msg_hdr;
4536 struct lnet_processid src = {};
4537 struct lnet_libmd *md;
4538 unsigned int rlength;
4539 unsigned int mlength;
4542 cpt = lnet_cpt_of_cookie(hdr->msg.reply.dst_wmd.wh_object_cookie);
4545 src.nid = hdr->src_nid;
4546 src.pid = hdr->src_pid;
4548 /* NB handles only looked up by creator (no flips) */
4549 md = lnet_wire_handle2md(&hdr->msg.reply.dst_wmd);
4550 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
4551 CNETERR("%s: Dropping REPLY from %s for %s "
4553 libcfs_nidstr(&ni->ni_nid), libcfs_idstr(&src),
4554 (md == NULL) ? "invalid" : "inactive",
4555 hdr->msg.reply.dst_wmd.wh_interface_cookie,
4556 hdr->msg.reply.dst_wmd.wh_object_cookie);
4557 if (md != NULL && md->md_me != NULL)
4558 CERROR("REPLY MD also attached to portal %d\n",
4559 md->md_me->me_portal);
4561 lnet_res_unlock(cpt);
4562 return -ENOENT; /* -ve: OK but no match */
4565 LASSERT(md->md_offset == 0);
4567 rlength = hdr->payload_length;
4568 mlength = min(rlength, md->md_length);
4570 if (mlength < rlength &&
4571 (md->md_options & LNET_MD_TRUNCATE) == 0) {
4572 CNETERR("%s: Dropping REPLY from %s length %d "
4573 "for MD %#llx would overflow (%d)\n",
4574 libcfs_nidstr(&ni->ni_nid), libcfs_idstr(&src),
4575 rlength, hdr->msg.reply.dst_wmd.wh_object_cookie,
4577 lnet_res_unlock(cpt);
4578 return -ENOENT; /* -ve: OK but no match */
4581 CDEBUG(D_NET, "%s: Reply from %s of length %d/%d into md %#llx\n",
4582 libcfs_nidstr(&ni->ni_nid), libcfs_idstr(&src),
4583 mlength, rlength, hdr->msg.reply.dst_wmd.wh_object_cookie);
4585 lnet_msg_attach_md(msg, md, 0, mlength);
4588 lnet_setpayloadbuffer(msg);
4590 lnet_res_unlock(cpt);
4592 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
4594 lnet_ni_recv(ni, private, msg, 0, 0, mlength, rlength);
4599 lnet_parse_ack(struct lnet_ni *ni, struct lnet_msg *msg)
4601 struct lnet_hdr *hdr = &msg->msg_hdr;
4602 struct lnet_processid src = {};
4603 struct lnet_libmd *md;
4606 src.nid = hdr->src_nid;
4607 src.pid = hdr->src_pid;
4609 /* Convert ack fields to host byte order */
4610 hdr->msg.ack.match_bits = le64_to_cpu(hdr->msg.ack.match_bits);
4611 hdr->msg.ack.mlength = le32_to_cpu(hdr->msg.ack.mlength);
4613 cpt = lnet_cpt_of_cookie(hdr->msg.ack.dst_wmd.wh_object_cookie);
4616 /* NB handles only looked up by creator (no flips) */
4617 md = lnet_wire_handle2md(&hdr->msg.ack.dst_wmd);
4618 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
4619 /* Don't moan; this is expected */
4621 "%s: Dropping ACK from %s to %s MD %#llx.%#llx\n",
4622 libcfs_nidstr(&ni->ni_nid), libcfs_idstr(&src),
4623 (md == NULL) ? "invalid" : "inactive",
4624 hdr->msg.ack.dst_wmd.wh_interface_cookie,
4625 hdr->msg.ack.dst_wmd.wh_object_cookie);
4626 if (md != NULL && md->md_me != NULL)
4627 CERROR("Source MD also attached to portal %d\n",
4628 md->md_me->me_portal);
4630 lnet_res_unlock(cpt);
4631 return -ENOENT; /* -ve! */
4634 CDEBUG(D_NET, "%s: ACK from %s into md %#llx\n",
4635 libcfs_nidstr(&ni->ni_nid), libcfs_idstr(&src),
4636 hdr->msg.ack.dst_wmd.wh_object_cookie);
4638 lnet_msg_attach_md(msg, md, 0, 0);
4640 lnet_res_unlock(cpt);
4642 lnet_build_msg_event(msg, LNET_EVENT_ACK);
4644 lnet_ni_recv(ni, msg->msg_private, msg, 0, 0, 0, msg->msg_len);
4649 * \retval LNET_CREDIT_OK If \a msg is forwarded
4650 * \retval LNET_CREDIT_WAIT If \a msg is blocked because w/o buffer
4651 * \retval -ve error code
4654 lnet_parse_forward_locked(struct lnet_ni *ni, struct lnet_msg *msg)
4658 if (!the_lnet.ln_routing)
4661 if (msg->msg_rxpeer->lpni_rtrcredits <= 0 ||
4662 lnet_msg2bufpool(msg)->rbp_credits <= 0) {
4663 if (ni->ni_net->net_lnd->lnd_eager_recv == NULL) {
4664 msg->msg_rx_ready_delay = 1;
4666 lnet_net_unlock(msg->msg_rx_cpt);
4667 rc = lnet_ni_eager_recv(ni, msg);
4668 lnet_net_lock(msg->msg_rx_cpt);
4673 rc = lnet_post_routed_recv_locked(msg, 0);
4678 lnet_parse_local(struct lnet_ni *ni, struct lnet_msg *msg)
4682 switch (msg->msg_type) {
4684 rc = lnet_parse_ack(ni, msg);
4687 rc = lnet_parse_put(ni, msg);
4690 rc = lnet_parse_get(ni, msg, msg->msg_rdma_get);
4692 case LNET_MSG_REPLY:
4693 rc = lnet_parse_reply(ni, msg);
4695 default: /* prevent an unused label if !kernel */
4700 LASSERT(rc == 0 || rc == -ENOENT);
4705 lnet_msgtyp2str (int type)
4714 case LNET_MSG_REPLY:
4716 case LNET_MSG_HELLO:
4719 return ("<UNKNOWN>");
4722 EXPORT_SYMBOL(lnet_msgtyp2str);
4725 lnet_parse(struct lnet_ni *ni, struct lnet_hdr *hdr,
4726 struct lnet_nid *from_nid, void *private, int rdma_req)
4728 struct lnet_peer_ni *lpni;
4729 struct lnet_msg *msg;
4730 __u32 payload_length;
4731 lnet_pid_t dest_pid;
4732 struct lnet_nid dest_nid;
4733 struct lnet_nid src_nid;
4739 time64_t now = ktime_get_seconds();
4741 LASSERT (!in_interrupt ());
4744 src_nid = hdr->src_nid;
4745 dest_nid = hdr->dest_nid;
4746 dest_pid = hdr->dest_pid;
4747 payload_length = hdr->payload_length;
4749 for_me = nid_same(&ni->ni_nid, &dest_nid);
4750 cpt = lnet_nid2cpt(from_nid, ni);
4752 CDEBUG(D_NET, "TRACE: %s(%s) <- %s : %s - %s\n",
4753 libcfs_nidstr(&dest_nid),
4754 libcfs_nidstr(&ni->ni_nid),
4755 libcfs_nidstr(&src_nid),
4756 lnet_msgtyp2str(type),
4757 (for_me) ? "for me" : "routed");
4762 if (payload_length > 0) {
4763 CERROR("%s, src %s: bad %s payload %d (0 expected)\n",
4764 libcfs_nidstr(from_nid),
4765 libcfs_nidstr(&src_nid),
4766 lnet_msgtyp2str(type), payload_length);
4772 case LNET_MSG_REPLY:
4773 if (payload_length >
4774 (__u32)(for_me ? LNET_MAX_PAYLOAD : LNET_MTU)) {
4775 CERROR("%s, src %s: bad %s payload %d "
4776 "(%d max expected)\n",
4777 libcfs_nidstr(from_nid),
4778 libcfs_nidstr(&src_nid),
4779 lnet_msgtyp2str(type),
4781 for_me ? LNET_MAX_PAYLOAD : LNET_MTU);
4787 CERROR("%s, src %s: Bad message type 0x%x\n",
4788 libcfs_nidstr(from_nid),
4789 libcfs_nidstr(&src_nid), type);
4793 /* Only update net_last_alive for incoming GETs on the reserved portal
4794 * (i.e. incoming lnet/discovery pings).
4795 * This avoids situations where the router's own traffic results in NI
4798 if (the_lnet.ln_routing && type == LNET_MSG_GET &&
4799 hdr->msg.get.ptl_index == LNET_RESERVED_PORTAL &&
4800 !lnet_islocalnid(&src_nid) &&
4801 ni->ni_net->net_last_alive != now) {
4803 spin_lock(&ni->ni_net->net_lock);
4804 ni->ni_net->net_last_alive = now;
4805 spin_unlock(&ni->ni_net->net_lock);
4806 push = lnet_ni_set_status_locked(ni, LNET_NI_STATUS_UP);
4811 lnet_push_update_to_peers(1);
4813 /* Regard a bad destination NID as a protocol error. Senders should
4814 * know what they're doing; if they don't they're misconfigured, buggy
4815 * or malicious so we chop them off at the knees :) */
4818 if (LNET_NID_NET(&dest_nid) == LNET_NID_NET(&ni->ni_nid)) {
4819 /* should have gone direct */
4820 CERROR("%s, src %s: Bad dest nid %s "
4821 "(should have been sent direct)\n",
4822 libcfs_nidstr(from_nid),
4823 libcfs_nidstr(&src_nid),
4824 libcfs_nidstr(&dest_nid));
4828 if (lnet_islocalnid(&dest_nid)) {
4829 /* dest is another local NI; sender should have used
4830 * this node's NID on its own network */
4831 CERROR("%s, src %s: Bad dest nid %s "
4832 "(it's my nid but on a different network)\n",
4833 libcfs_nidstr(from_nid),
4834 libcfs_nidstr(&src_nid),
4835 libcfs_nidstr(&dest_nid));
4839 if (rdma_req && type == LNET_MSG_GET) {
4840 CERROR("%s, src %s: Bad optimized GET for %s "
4841 "(final destination must be me)\n",
4842 libcfs_nidstr(from_nid),
4843 libcfs_nidstr(&src_nid),
4844 libcfs_nidstr(&dest_nid));
4848 if (!the_lnet.ln_routing) {
4849 CERROR("%s, src %s: Dropping message for %s "
4850 "(routing not enabled)\n",
4851 libcfs_nidstr(from_nid),
4852 libcfs_nidstr(&src_nid),
4853 libcfs_nidstr(&dest_nid));
4858 /* Message looks OK; we're not going to return an error, so we MUST
4859 * call back lnd_recv() come what may... */
4861 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
4862 fail_peer(&src_nid, 0)) { /* shall we now? */
4863 CERROR("%s, src %s: Dropping %s to simulate failure\n",
4864 libcfs_nidstr(from_nid), libcfs_nidstr(&src_nid),
4865 lnet_msgtyp2str(type));
4869 if (!list_empty(&the_lnet.ln_drop_rules) &&
4870 lnet_drop_rule_match(hdr, &ni->ni_nid, NULL)) {
4872 "%s, src %s, dst %s: Dropping %s to simulate silent message loss\n",
4873 libcfs_nidstr(from_nid), libcfs_nidstr(&src_nid),
4874 libcfs_nidstr(&dest_nid), lnet_msgtyp2str(type));
4878 msg = lnet_msg_alloc();
4880 CERROR("%s, src %s: Dropping %s (out of memory)\n",
4881 libcfs_nidstr(from_nid), libcfs_nidstr(&src_nid),
4882 lnet_msgtyp2str(type));
4886 /* msg zeroed in lnet_msg_alloc; i.e. flags all clear,
4887 * pointers NULL etc */
4889 msg->msg_type = type;
4890 msg->msg_private = private;
4891 msg->msg_receiving = 1;
4892 msg->msg_rdma_get = rdma_req;
4893 msg->msg_len = msg->msg_wanted = payload_length;
4894 msg->msg_offset = 0;
4895 msg->msg_hdr = *hdr;
4896 /* for building message event */
4897 msg->msg_from = *from_nid;
4899 msg->msg_target.pid = dest_pid;
4900 msg->msg_target.nid = dest_nid;
4901 msg->msg_routing = 1;
4905 lpni = lnet_peerni_by_nid_locked(from_nid, &ni->ni_nid, cpt);
4907 lnet_net_unlock(cpt);
4909 CERROR("%s, src %s: Dropping %s (error %d looking up sender)\n",
4910 libcfs_nidstr(from_nid), libcfs_nidstr(&src_nid),
4911 lnet_msgtyp2str(type), rc);
4913 if (rc == -ESHUTDOWN)
4914 /* We are shutting down. Don't do anything more */
4919 /* If this message was forwarded to us from a router then we may need
4920 * to update router aliveness or check for an asymmetrical route
4923 if (((lnet_drop_asym_route && for_me) ||
4924 !lpni->lpni_peer_net->lpn_peer->lp_alive) &&
4925 LNET_NID_NET(&src_nid) != LNET_NID_NET(from_nid)) {
4926 __u32 src_net_id = LNET_NID_NET(&src_nid);
4927 struct lnet_peer *gw = lpni->lpni_peer_net->lpn_peer;
4928 struct lnet_route *route;
4931 list_for_each_entry(route, &gw->lp_routes, lr_gwlist) {
4932 if (route->lr_net == src_net_id) {
4934 /* If we're transitioning the gateway from
4935 * dead -> alive, and discovery is disabled
4936 * locally or on the gateway, then we need to
4937 * update the cached route aliveness for each
4938 * route to the src_nid's net.
4940 * Otherwise, we're only checking for
4941 * symmetrical route, and we can break the
4944 if (!gw->lp_alive &&
4945 lnet_is_discovery_disabled(gw))
4946 lnet_set_route_aliveness(route, true);
4951 if (lnet_drop_asym_route && for_me && !found) {
4952 /* Drop ref taken by lnet_nid2peerni_locked() */
4953 lnet_peer_ni_decref_locked(lpni);
4954 lnet_net_unlock(cpt);
4955 /* we would not use from_nid to route a message to
4957 * => asymmetric routing detected but forbidden
4959 CERROR("%s, src %s: Dropping asymmetrical route %s\n",
4960 libcfs_nidstr(from_nid),
4961 libcfs_nidstr(&src_nid), lnet_msgtyp2str(type));
4965 if (!gw->lp_alive) {
4966 struct lnet_peer_net *lpn;
4967 struct lnet_peer_ni *lpni2;
4969 gw->lp_alive = true;
4970 /* Mark all remote NIs on src_nid's net UP */
4971 lpn = lnet_peer_get_net_locked(gw, src_net_id);
4973 list_for_each_entry(lpni2, &lpn->lpn_peer_nis,
4975 lpni2->lpni_ns_status = LNET_NI_STATUS_UP;
4979 lpni->lpni_last_alive = now;
4981 msg->msg_rxpeer = lpni;
4983 lnet_ni_addref_locked(ni, cpt);
4984 /* Multi-Rail: Primary NID of source. */
4985 lnet_peer_primary_nid_locked(&src_nid, &msg->msg_initiator);
4988 * mark the status of this lpni as UP since we received a message
4989 * from it. The ping response reports back the ns_status which is
4990 * marked on the remote as up or down and we cache it here.
4992 msg->msg_rxpeer->lpni_ns_status = LNET_NI_STATUS_UP;
4994 lnet_msg_commit(msg, cpt);
4996 /* message delay simulation */
4997 if (unlikely(!list_empty(&the_lnet.ln_delay_rules) &&
4998 lnet_delay_rule_match_locked(hdr, msg))) {
4999 lnet_net_unlock(cpt);
5004 rc = lnet_parse_forward_locked(ni, msg);
5005 lnet_net_unlock(cpt);
5010 if (rc == LNET_CREDIT_OK) {
5011 lnet_ni_recv(ni, msg->msg_private, msg, 0,
5012 0, payload_length, payload_length);
5017 lnet_net_unlock(cpt);
5019 rc = lnet_parse_local(ni, msg);
5025 LASSERT(msg->msg_md == NULL);
5026 lnet_finalize(msg, rc);
5029 lnet_drop_message(ni, cpt, private, payload_length, type);
5032 EXPORT_SYMBOL(lnet_parse);
5035 lnet_drop_delayed_msg_list(struct list_head *head, char *reason)
5037 struct lnet_msg *msg;
5039 while ((msg = list_first_entry_or_null(head, struct lnet_msg,
5040 msg_list)) != NULL) {
5041 struct lnet_processid id = {};
5043 list_del(&msg->msg_list);
5045 id.nid = msg->msg_hdr.src_nid;
5046 id.pid = msg->msg_hdr.src_pid;
5048 LASSERT(msg->msg_md == NULL);
5049 LASSERT(msg->msg_rx_delayed);
5050 LASSERT(msg->msg_rxpeer != NULL);
5051 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
5053 CWARN("Dropping delayed PUT from %s portal %d match %llu"
5054 " offset %d length %d: %s\n",
5056 msg->msg_hdr.msg.put.ptl_index,
5057 msg->msg_hdr.msg.put.match_bits,
5058 msg->msg_hdr.msg.put.offset,
5059 msg->msg_hdr.payload_length, reason);
5061 /* NB I can't drop msg's ref on msg_rxpeer until after I've
5062 * called lnet_drop_message(), so I just hang onto msg as well
5063 * until that's done */
5065 lnet_drop_message(msg->msg_rxni, msg->msg_rx_cpt,
5066 msg->msg_private, msg->msg_len,
5069 msg->msg_no_resend = true;
5071 * NB: message will not generate event because w/o attached MD,
5072 * but we still should give error code so lnet_msg_decommit()
5073 * can skip counters operations and other checks.
5075 lnet_finalize(msg, -ENOENT);
5080 lnet_recv_delayed_msg_list(struct list_head *head)
5082 struct lnet_msg *msg;
5084 while ((msg = list_first_entry_or_null(head, struct lnet_msg,
5085 msg_list)) != NULL) {
5086 struct lnet_processid id;
5088 list_del(&msg->msg_list);
5090 /* md won't disappear under me, since each msg
5091 * holds a ref on it */
5093 id.nid = msg->msg_hdr.src_nid;
5094 id.pid = msg->msg_hdr.src_pid;
5096 LASSERT(msg->msg_rx_delayed);
5097 LASSERT(msg->msg_md != NULL);
5098 LASSERT(msg->msg_rxpeer != NULL);
5099 LASSERT(msg->msg_rxni != NULL);
5100 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
5102 CDEBUG(D_NET, "Resuming delayed PUT from %s portal %d "
5103 "match %llu offset %d length %d.\n",
5104 libcfs_idstr(&id), msg->msg_hdr.msg.put.ptl_index,
5105 msg->msg_hdr.msg.put.match_bits,
5106 msg->msg_hdr.msg.put.offset,
5107 msg->msg_hdr.payload_length);
5109 lnet_recv_put(msg->msg_rxni, msg);
5114 lnet_attach_rsp_tracker(struct lnet_rsp_tracker *rspt, int cpt,
5115 struct lnet_libmd *md, struct lnet_handle_md mdh)
5118 struct lnet_rsp_tracker *local_rspt;
5121 * MD has a refcount taken by message so it's not going away.
5122 * The MD however can be looked up. We need to secure the access
5123 * to the md_rspt_ptr by taking the res_lock.
5124 * The rspt can be accessed without protection up to when it gets
5125 * added to the list.
5129 local_rspt = md->md_rspt_ptr;
5130 timeout_ns = lnet_transaction_timeout * NSEC_PER_SEC;
5131 if (local_rspt != NULL) {
5133 * we already have an rspt attached to the md, so we'll
5134 * update the deadline on that one.
5136 lnet_rspt_free(rspt, cpt);
5139 rspt->rspt_mdh = mdh;
5140 rspt->rspt_cpt = cpt;
5141 /* store the rspt so we can access it when we get the REPLY */
5142 md->md_rspt_ptr = rspt;
5145 local_rspt->rspt_deadline = ktime_add_ns(ktime_get(), timeout_ns);
5148 * add to the list of tracked responses. It's added to tail of the
5149 * list in order to expire all the older entries first.
5152 list_move_tail(&local_rspt->rspt_on_list, the_lnet.ln_mt_rstq[cpt]);
5153 lnet_net_unlock(cpt);
5154 lnet_res_unlock(cpt);
5158 * Initiate an asynchronous PUT operation.
5160 * There are several events associated with a PUT: completion of the send on
5161 * the initiator node (LNET_EVENT_SEND), and when the send completes
5162 * successfully, the receipt of an acknowledgment (LNET_EVENT_ACK) indicating
5163 * that the operation was accepted by the target. The event LNET_EVENT_PUT is
5164 * used at the target node to indicate the completion of incoming data
5167 * The local events will be logged in the EQ associated with the MD pointed to
5168 * by \a mdh handle. Using a MD without an associated EQ results in these
5169 * events being discarded. In this case, the caller must have another
5170 * mechanism (e.g., a higher level protocol) for determining when it is safe
5171 * to modify the memory region associated with the MD.
5173 * Note that LNet does not guarantee the order of LNET_EVENT_SEND and
5174 * LNET_EVENT_ACK, though intuitively ACK should happen after SEND.
5176 * \param self Indicates the NID of a local interface through which to send
5177 * the PUT request. Use LNET_NID_ANY to let LNet choose one by itself.
5178 * \param mdh A handle for the MD that describes the memory to be sent. The MD
5179 * must be "free floating" (See LNetMDBind()).
5180 * \param ack Controls whether an acknowledgment is requested.
5181 * Acknowledgments are only sent when they are requested by the initiating
5182 * process and the target MD enables them.
5183 * \param target A process identifier for the target process.
5184 * \param portal The index in the \a target's portal table.
5185 * \param match_bits The match bits to use for MD selection at the target
5187 * \param offset The offset into the target MD (only used when the target
5188 * MD has the LNET_MD_MANAGE_REMOTE option set).
5189 * \param hdr_data 64 bits of user data that can be included in the message
5190 * header. This data is written to an event queue entry at the target if an
5191 * EQ is present on the matching MD.
5193 * \retval 0 Success, and only in this case events will be generated
5194 * and logged to EQ (if it exists).
5195 * \retval -EIO Simulated failure.
5196 * \retval -ENOMEM Memory allocation failure.
5197 * \retval -ENOENT Invalid MD object.
5199 * \see struct lnet_event::hdr_data and lnet_event_kind_t.
5202 LNetPut(struct lnet_nid *self, struct lnet_handle_md mdh, enum lnet_ack_req ack,
5203 struct lnet_processid *target, unsigned int portal,
5204 __u64 match_bits, unsigned int offset,
5207 struct lnet_msg *msg;
5208 struct lnet_libmd *md;
5211 struct lnet_rsp_tracker *rspt = NULL;
5213 LASSERT(the_lnet.ln_refcount > 0);
5215 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
5216 fail_peer(&target->nid, 1)) { /* shall we now? */
5217 CERROR("Dropping PUT to %s: simulated failure\n",
5218 libcfs_idstr(target));
5222 msg = lnet_msg_alloc();
5224 CERROR("Dropping PUT to %s: ENOMEM on struct lnet_msg\n",
5225 libcfs_idstr(target));
5228 msg->msg_vmflush = !!(current->flags & PF_MEMALLOC);
5230 cpt = lnet_cpt_of_cookie(mdh.cookie);
5232 if (ack == LNET_ACK_REQ) {
5233 rspt = lnet_rspt_alloc(cpt);
5235 CERROR("Dropping PUT to %s: ENOMEM on response tracker\n",
5236 libcfs_idstr(target));
5240 INIT_LIST_HEAD(&rspt->rspt_on_list);
5245 md = lnet_handle2md(&mdh);
5246 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
5247 CERROR("Dropping PUT (%llu:%d:%s): MD (%d) invalid\n",
5248 match_bits, portal, libcfs_idstr(target),
5249 md == NULL ? -1 : md->md_threshold);
5250 if (md != NULL && md->md_me != NULL)
5251 CERROR("Source MD also attached to portal %d\n",
5252 md->md_me->me_portal);
5253 lnet_res_unlock(cpt);
5256 lnet_rspt_free(rspt, cpt);
5262 CDEBUG(D_NET, "%s -> %s\n", __func__, libcfs_idstr(target));
5264 lnet_msg_attach_md(msg, md, 0, 0);
5266 lnet_prep_send(msg, LNET_MSG_PUT, target, 0, md->md_length);
5268 msg->msg_hdr.msg.put.match_bits = cpu_to_le64(match_bits);
5269 msg->msg_hdr.msg.put.ptl_index = cpu_to_le32(portal);
5270 msg->msg_hdr.msg.put.offset = cpu_to_le32(offset);
5271 msg->msg_hdr.msg.put.hdr_data = hdr_data;
5273 /* NB handles only looked up by creator (no flips) */
5274 if (ack == LNET_ACK_REQ) {
5275 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
5276 the_lnet.ln_interface_cookie;
5277 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
5278 md->md_lh.lh_cookie;
5280 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
5281 LNET_WIRE_HANDLE_COOKIE_NONE;
5282 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
5283 LNET_WIRE_HANDLE_COOKIE_NONE;
5286 lnet_res_unlock(cpt);
5288 lnet_build_msg_event(msg, LNET_EVENT_SEND);
5290 if (rspt && lnet_response_tracking_enabled(LNET_MSG_PUT,
5292 lnet_attach_rsp_tracker(rspt, cpt, md, mdh);
5294 lnet_rspt_free(rspt, cpt);
5296 if (CFS_FAIL_CHECK_ORSET(CFS_FAIL_PTLRPC_OST_BULK_CB2,
5300 rc = lnet_send(self, msg, NULL);
5303 CNETERR("Error sending PUT to %s: %d\n",
5304 libcfs_idstr(target), rc);
5305 msg->msg_no_resend = true;
5306 lnet_finalize(msg, rc);
5309 /* completion will be signalled by an event */
5312 EXPORT_SYMBOL(LNetPut);
5315 * The LND can DMA direct to the GET md (i.e. no REPLY msg). This
5316 * returns a msg for the LND to pass to lnet_finalize() when the sink
5317 * data has been received.
5319 * CAVEAT EMPTOR: 'getmsg' is the original GET, which is freed when
5320 * lnet_finalize() is called on it, so the LND must call this first
5323 lnet_create_reply_msg(struct lnet_ni *ni, struct lnet_msg *getmsg)
5325 struct lnet_msg *msg = lnet_msg_alloc();
5326 struct lnet_libmd *getmd = getmsg->msg_md;
5327 struct lnet_processid *peer_id = &getmsg->msg_target;
5330 LASSERT(!getmsg->msg_target_is_router);
5331 LASSERT(!getmsg->msg_routing);
5334 CERROR("%s: Dropping REPLY from %s: can't allocate msg\n",
5335 libcfs_nidstr(&ni->ni_nid), libcfs_idstr(peer_id));
5339 cpt = lnet_cpt_of_cookie(getmd->md_lh.lh_cookie);
5342 LASSERT(getmd->md_refcount > 0);
5344 if (getmd->md_threshold == 0) {
5345 CERROR("%s: Dropping REPLY from %s for inactive MD %p\n",
5346 libcfs_nidstr(&ni->ni_nid), libcfs_idstr(peer_id),
5348 lnet_res_unlock(cpt);
5352 LASSERT(getmd->md_offset == 0);
5354 CDEBUG(D_NET, "%s: Reply from %s md %p\n",
5355 libcfs_nidstr(&ni->ni_nid), libcfs_idstr(peer_id), getmd);
5357 /* setup information for lnet_build_msg_event */
5358 msg->msg_initiator =
5359 getmsg->msg_txpeer->lpni_peer_net->lpn_peer->lp_primary_nid;
5360 msg->msg_from = peer_id->nid;
5361 msg->msg_type = LNET_MSG_GET; /* flag this msg as an "optimized" GET */
5362 msg->msg_hdr.src_nid = peer_id->nid;
5363 msg->msg_hdr.payload_length = getmd->md_length;
5364 msg->msg_receiving = 1; /* required by lnet_msg_attach_md */
5366 lnet_msg_attach_md(msg, getmd, getmd->md_offset, getmd->md_length);
5367 lnet_res_unlock(cpt);
5369 cpt = lnet_nid2cpt(&peer_id->nid, ni);
5372 lnet_msg_commit(msg, cpt);
5373 lnet_net_unlock(cpt);
5375 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
5380 cpt = lnet_nid2cpt(&peer_id->nid, ni);
5383 lnet_incr_stats(&ni->ni_stats, LNET_MSG_GET, LNET_STATS_TYPE_DROP);
5384 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
5385 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length +=
5387 lnet_net_unlock(cpt);
5394 EXPORT_SYMBOL(lnet_create_reply_msg);
5397 lnet_set_reply_msg_len(struct lnet_ni *ni, struct lnet_msg *reply,
5400 /* Set the REPLY length, now the RDMA that elides the REPLY message has
5401 * completed and I know it. */
5402 LASSERT(reply != NULL);
5403 LASSERT(reply->msg_type == LNET_MSG_GET);
5404 LASSERT(reply->msg_ev.type == LNET_EVENT_REPLY);
5406 /* NB I trusted my peer to RDMA. If she tells me she's written beyond
5407 * the end of my buffer, I might as well be dead. */
5408 LASSERT(len <= reply->msg_ev.mlength);
5410 reply->msg_ev.mlength = len;
5412 EXPORT_SYMBOL(lnet_set_reply_msg_len);
5415 * Initiate an asynchronous GET operation.
5417 * On the initiator node, an LNET_EVENT_SEND is logged when the GET request
5418 * is sent, and an LNET_EVENT_REPLY is logged when the data returned from
5419 * the target node in the REPLY has been written to local MD.
5421 * On the target node, an LNET_EVENT_GET is logged when the GET request
5422 * arrives and is accepted into a MD.
5424 * \param self,target,portal,match_bits,offset See the discussion in LNetPut().
5425 * \param mdh A handle for the MD that describes the memory into which the
5426 * requested data will be received. The MD must be "free floating" (See LNetMDBind()).
5428 * \retval 0 Success, and only in this case events will be generated
5429 * and logged to EQ (if it exists) of the MD.
5430 * \retval -EIO Simulated failure.
5431 * \retval -ENOMEM Memory allocation failure.
5432 * \retval -ENOENT Invalid MD object.
5435 LNetGet(struct lnet_nid *self, struct lnet_handle_md mdh,
5436 struct lnet_processid *target, unsigned int portal,
5437 __u64 match_bits, unsigned int offset, bool recovery)
5439 struct lnet_msg *msg;
5440 struct lnet_libmd *md;
5441 struct lnet_rsp_tracker *rspt;
5445 LASSERT(the_lnet.ln_refcount > 0);
5447 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
5448 fail_peer(&target->nid, 1)) /* shall we now? */
5450 CERROR("Dropping GET to %s: simulated failure\n",
5451 libcfs_idstr(target));
5455 msg = lnet_msg_alloc();
5457 CERROR("Dropping GET to %s: ENOMEM on struct lnet_msg\n",
5458 libcfs_idstr(target));
5462 cpt = lnet_cpt_of_cookie(mdh.cookie);
5464 rspt = lnet_rspt_alloc(cpt);
5466 CERROR("Dropping GET to %s: ENOMEM on response tracker\n",
5467 libcfs_idstr(target));
5471 INIT_LIST_HEAD(&rspt->rspt_on_list);
5473 msg->msg_recovery = recovery;
5477 md = lnet_handle2md(&mdh);
5478 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
5479 CERROR("Dropping GET (%llu:%d:%s): MD (%d) invalid\n",
5480 match_bits, portal, libcfs_idstr(target),
5481 md == NULL ? -1 : md->md_threshold);
5482 if (md != NULL && md->md_me != NULL)
5483 CERROR("REPLY MD also attached to portal %d\n",
5484 md->md_me->me_portal);
5486 lnet_res_unlock(cpt);
5489 lnet_rspt_free(rspt, cpt);
5493 CDEBUG(D_NET, "%s -> %s\n", __func__, libcfs_idstr(target));
5495 lnet_msg_attach_md(msg, md, 0, 0);
5497 lnet_prep_send(msg, LNET_MSG_GET, target, 0, 0);
5499 msg->msg_hdr.msg.get.match_bits = cpu_to_le64(match_bits);
5500 msg->msg_hdr.msg.get.ptl_index = cpu_to_le32(portal);
5501 msg->msg_hdr.msg.get.src_offset = cpu_to_le32(offset);
5502 msg->msg_hdr.msg.get.sink_length = cpu_to_le32(md->md_length);
5504 /* NB handles only looked up by creator (no flips) */
5505 msg->msg_hdr.msg.get.return_wmd.wh_interface_cookie =
5506 the_lnet.ln_interface_cookie;
5507 msg->msg_hdr.msg.get.return_wmd.wh_object_cookie =
5508 md->md_lh.lh_cookie;
5510 lnet_res_unlock(cpt);
5512 lnet_build_msg_event(msg, LNET_EVENT_SEND);
5514 if (lnet_response_tracking_enabled(LNET_MSG_GET, md->md_options))
5515 lnet_attach_rsp_tracker(rspt, cpt, md, mdh);
5517 lnet_rspt_free(rspt, cpt);
5519 rc = lnet_send(self, msg, NULL);
5521 CNETERR("Error sending GET to %s: %d\n",
5522 libcfs_idstr(target), rc);
5523 msg->msg_no_resend = true;
5524 lnet_finalize(msg, rc);
5527 /* completion will be signalled by an event */
5530 EXPORT_SYMBOL(LNetGet);
5533 * Calculate distance to node at \a dstnid.
5535 * \param dstnid Target NID.
5536 * \param srcnidp If not NULL, NID of the local interface to reach \a dstnid
5538 * \param orderp If not NULL, order of the route to reach \a dstnid is saved
5541 * \retval 0 If \a dstnid belongs to a local interface, and reserved option
5542 * local_nid_dist_zero is set, which is the default.
5543 * \retval positives Distance to target NID, i.e. number of hops plus one.
5544 * \retval -EHOSTUNREACH If \a dstnid is not reachable.
5547 LNetDist(struct lnet_nid *dstnid, struct lnet_nid *srcnid, __u32 *orderp)
5549 struct lnet_ni *ni = NULL;
5550 struct lnet_remotenet *rnet;
5551 __u32 dstnet = LNET_NID_NET(dstnid);
5555 struct list_head *rn_list;
5556 struct lnet_ni *matched_dstnet = NULL;
5558 /* if !local_nid_dist_zero, I don't return a distance of 0 ever
5559 * (when lustre sees a distance of 0, it substitutes 0@lo), so I
5560 * keep order 0 free for 0@lo and order 1 free for a local NID
5562 * WARNING: dstnid and srcnid might point to same place.
5563 * Don't set *srcnid until late.
5566 LASSERT(the_lnet.ln_refcount > 0);
5568 cpt = lnet_net_lock_current();
5570 while ((ni = lnet_get_next_ni_locked(NULL, ni))) {
5571 if (nid_same(&ni->ni_nid, dstnid)) {
5572 if (orderp != NULL) {
5573 if (nid_is_lo0(dstnid))
5580 lnet_net_unlock(cpt);
5582 return local_nid_dist_zero ? 0 : 1;
5585 if (!matched_dstnet && LNET_NID_NET(&ni->ni_nid) == dstnet) {
5586 matched_dstnet = ni;
5587 /* We matched the destination net, but we may have
5588 * additional local NIs to inspect.
5590 * We record the order as appropriate, but
5591 * they may be overwritten if we match local NI above.
5595 /* Check if ni was originally created in
5596 * current net namespace.
5597 * If not, assign order above 0xffff0000,
5598 * to make this ni not a priority.
5600 if (current->nsproxy &&
5601 !net_eq(ni->ni_net_ns,
5602 current->nsproxy->net_ns))
5603 *orderp = order + 0xffff0000;
5612 if (matched_dstnet) {
5614 *srcnid = matched_dstnet->ni_nid;
5615 lnet_net_unlock(cpt);
5619 rn_list = lnet_net2rnethash(dstnet);
5620 list_for_each_entry(rnet, rn_list, lrn_list) {
5621 if (rnet->lrn_net == dstnet) {
5622 struct lnet_route *route;
5623 struct lnet_route *shortest = NULL;
5624 __u32 shortest_hops = LNET_UNDEFINED_HOPS;
5627 LASSERT(!list_empty(&rnet->lrn_routes));
5629 list_for_each_entry(route, &rnet->lrn_routes,
5631 route_hops = route->lr_hops;
5632 if (route_hops == LNET_UNDEFINED_HOPS)
5634 if (shortest == NULL ||
5635 route_hops < shortest_hops) {
5637 shortest_hops = route_hops;
5641 LASSERT(shortest != NULL);
5642 hops = shortest_hops;
5644 struct lnet_net *net;
5645 net = lnet_get_net_locked(shortest->lr_lnet);
5647 ni = lnet_get_next_ni_locked(net, NULL);
5648 *srcnid = ni->ni_nid;
5652 lnet_net_unlock(cpt);
5658 lnet_net_unlock(cpt);
5659 return -EHOSTUNREACH;
5661 EXPORT_SYMBOL(LNetDist);