4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lnet/lnet/lib-move.c
34 * Data movement routines
37 #define DEBUG_SUBSYSTEM S_LNET
39 #include <linux/pagemap.h>
41 #include <lnet/lib-lnet.h>
42 #include <linux/nsproxy.h>
43 #include <net/net_namespace.h>
45 static int local_nid_dist_zero = 1;
46 module_param(local_nid_dist_zero, int, 0444);
47 MODULE_PARM_DESC(local_nid_dist_zero, "Reserved");
49 struct lnet_send_data {
50 struct lnet_ni *sd_best_ni;
51 struct lnet_peer_ni *sd_best_lpni;
52 struct lnet_peer_ni *sd_final_dst_lpni;
53 struct lnet_peer *sd_peer;
54 struct lnet_peer *sd_gw_peer;
55 struct lnet_peer_ni *sd_gw_lpni;
56 struct lnet_peer_net *sd_peer_net;
57 struct lnet_msg *sd_msg;
58 lnet_nid_t sd_dst_nid;
59 lnet_nid_t sd_src_nid;
60 lnet_nid_t sd_rtr_nid;
66 static inline struct lnet_comm_count *
67 get_stats_counts(struct lnet_element_stats *stats,
68 enum lnet_stats_type stats_type)
71 case LNET_STATS_TYPE_SEND:
72 return &stats->el_send_stats;
73 case LNET_STATS_TYPE_RECV:
74 return &stats->el_recv_stats;
75 case LNET_STATS_TYPE_DROP:
76 return &stats->el_drop_stats;
78 CERROR("Unknown stats type\n");
84 void lnet_incr_stats(struct lnet_element_stats *stats,
85 enum lnet_msg_type msg_type,
86 enum lnet_stats_type stats_type)
88 struct lnet_comm_count *counts = get_stats_counts(stats, stats_type);
94 atomic_inc(&counts->co_ack_count);
97 atomic_inc(&counts->co_put_count);
100 atomic_inc(&counts->co_get_count);
103 atomic_inc(&counts->co_reply_count);
106 atomic_inc(&counts->co_hello_count);
109 CERROR("There is a BUG in the code. Unknown message type\n");
114 __u32 lnet_sum_stats(struct lnet_element_stats *stats,
115 enum lnet_stats_type stats_type)
117 struct lnet_comm_count *counts = get_stats_counts(stats, stats_type);
121 return (atomic_read(&counts->co_ack_count) +
122 atomic_read(&counts->co_put_count) +
123 atomic_read(&counts->co_get_count) +
124 atomic_read(&counts->co_reply_count) +
125 atomic_read(&counts->co_hello_count));
128 static inline void assign_stats(struct lnet_ioctl_comm_count *msg_stats,
129 struct lnet_comm_count *counts)
131 msg_stats->ico_get_count = atomic_read(&counts->co_get_count);
132 msg_stats->ico_put_count = atomic_read(&counts->co_put_count);
133 msg_stats->ico_reply_count = atomic_read(&counts->co_reply_count);
134 msg_stats->ico_ack_count = atomic_read(&counts->co_ack_count);
135 msg_stats->ico_hello_count = atomic_read(&counts->co_hello_count);
138 void lnet_usr_translate_stats(struct lnet_ioctl_element_msg_stats *msg_stats,
139 struct lnet_element_stats *stats)
141 struct lnet_comm_count *counts;
146 counts = get_stats_counts(stats, LNET_STATS_TYPE_SEND);
149 assign_stats(&msg_stats->im_send_stats, counts);
151 counts = get_stats_counts(stats, LNET_STATS_TYPE_RECV);
154 assign_stats(&msg_stats->im_recv_stats, counts);
156 counts = get_stats_counts(stats, LNET_STATS_TYPE_DROP);
159 assign_stats(&msg_stats->im_drop_stats, counts);
163 lnet_fail_nid(lnet_nid_t nid, unsigned int threshold)
165 struct lnet_test_peer *tp;
166 struct list_head *el;
167 struct list_head *next;
168 struct list_head cull;
170 /* NB: use lnet_net_lock(0) to serialize operations on test peers */
171 if (threshold != 0) {
172 /* Adding a new entry */
173 LIBCFS_ALLOC(tp, sizeof(*tp));
178 tp->tp_threshold = threshold;
181 list_add_tail(&tp->tp_list, &the_lnet.ln_test_peers);
186 /* removing entries */
187 INIT_LIST_HEAD(&cull);
191 list_for_each_safe(el, next, &the_lnet.ln_test_peers) {
192 tp = list_entry(el, struct lnet_test_peer, tp_list);
194 if (tp->tp_threshold == 0 || /* needs culling anyway */
195 nid == LNET_NID_ANY || /* removing all entries */
196 tp->tp_nid == nid) { /* matched this one */
197 list_del(&tp->tp_list);
198 list_add(&tp->tp_list, &cull);
204 while (!list_empty(&cull)) {
205 tp = list_entry(cull.next, struct lnet_test_peer, tp_list);
207 list_del(&tp->tp_list);
208 LIBCFS_FREE(tp, sizeof(*tp));
214 fail_peer (lnet_nid_t nid, int outgoing)
216 struct lnet_test_peer *tp;
217 struct list_head *el;
218 struct list_head *next;
219 struct list_head cull;
222 INIT_LIST_HEAD(&cull);
224 /* NB: use lnet_net_lock(0) to serialize operations on test peers */
227 list_for_each_safe(el, next, &the_lnet.ln_test_peers) {
228 tp = list_entry(el, struct lnet_test_peer, tp_list);
230 if (tp->tp_threshold == 0) {
233 /* only cull zombies on outgoing tests,
234 * since we may be at interrupt priority on
235 * incoming messages. */
236 list_del(&tp->tp_list);
237 list_add(&tp->tp_list, &cull);
242 if (tp->tp_nid == LNET_NID_ANY || /* fail every peer */
243 nid == tp->tp_nid) { /* fail this peer */
246 if (tp->tp_threshold != LNET_MD_THRESH_INF) {
249 tp->tp_threshold == 0) {
251 list_del(&tp->tp_list);
252 list_add(&tp->tp_list, &cull);
261 while (!list_empty(&cull)) {
262 tp = list_entry(cull.next, struct lnet_test_peer, tp_list);
263 list_del(&tp->tp_list);
265 LIBCFS_FREE(tp, sizeof(*tp));
272 lnet_iov_nob(unsigned int niov, struct kvec *iov)
274 unsigned int nob = 0;
276 LASSERT(niov == 0 || iov != NULL);
278 nob += (iov++)->iov_len;
282 EXPORT_SYMBOL(lnet_iov_nob);
285 lnet_copy_iov2iov(unsigned int ndiov, struct kvec *diov, unsigned int doffset,
286 unsigned int nsiov, struct kvec *siov, unsigned int soffset,
289 /* NB diov, siov are READ-ONLY */
290 unsigned int this_nob;
295 /* skip complete frags before 'doffset' */
297 while (doffset >= diov->iov_len) {
298 doffset -= diov->iov_len;
304 /* skip complete frags before 'soffset' */
306 while (soffset >= siov->iov_len) {
307 soffset -= siov->iov_len;
316 this_nob = MIN(diov->iov_len - doffset,
317 siov->iov_len - soffset);
318 this_nob = MIN(this_nob, nob);
320 memcpy((char *)diov->iov_base + doffset,
321 (char *)siov->iov_base + soffset, this_nob);
324 if (diov->iov_len > doffset + this_nob) {
332 if (siov->iov_len > soffset + this_nob) {
341 EXPORT_SYMBOL(lnet_copy_iov2iov);
344 lnet_extract_iov(int dst_niov, struct kvec *dst,
345 int src_niov, struct kvec *src,
346 unsigned int offset, unsigned int len)
348 /* Initialise 'dst' to the subset of 'src' starting at 'offset',
349 * for exactly 'len' bytes, and return the number of entries.
350 * NB not destructive to 'src' */
351 unsigned int frag_len;
354 if (len == 0) /* no data => */
355 return (0); /* no frags */
357 LASSERT(src_niov > 0);
358 while (offset >= src->iov_len) { /* skip initial frags */
359 offset -= src->iov_len;
362 LASSERT(src_niov > 0);
367 LASSERT(src_niov > 0);
368 LASSERT((int)niov <= dst_niov);
370 frag_len = src->iov_len - offset;
371 dst->iov_base = ((char *)src->iov_base) + offset;
373 if (len <= frag_len) {
378 dst->iov_len = frag_len;
388 EXPORT_SYMBOL(lnet_extract_iov);
392 lnet_kiov_nob(unsigned int niov, lnet_kiov_t *kiov)
394 unsigned int nob = 0;
396 LASSERT(niov == 0 || kiov != NULL);
398 nob += (kiov++)->kiov_len;
402 EXPORT_SYMBOL(lnet_kiov_nob);
405 lnet_copy_kiov2kiov(unsigned int ndiov, lnet_kiov_t *diov, unsigned int doffset,
406 unsigned int nsiov, lnet_kiov_t *siov, unsigned int soffset,
409 /* NB diov, siov are READ-ONLY */
410 unsigned int this_nob;
417 LASSERT (!in_interrupt ());
420 while (doffset >= diov->kiov_len) {
421 doffset -= diov->kiov_len;
428 while (soffset >= siov->kiov_len) {
429 soffset -= siov->kiov_len;
438 this_nob = MIN(diov->kiov_len - doffset,
439 siov->kiov_len - soffset);
440 this_nob = MIN(this_nob, nob);
443 daddr = ((char *)kmap(diov->kiov_page)) +
444 diov->kiov_offset + doffset;
446 saddr = ((char *)kmap(siov->kiov_page)) +
447 siov->kiov_offset + soffset;
449 /* Vanishing risk of kmap deadlock when mapping 2 pages.
450 * However in practice at least one of the kiovs will be mapped
451 * kernel pages and the map/unmap will be NOOPs */
453 memcpy (daddr, saddr, this_nob);
456 if (diov->kiov_len > doffset + this_nob) {
460 kunmap(diov->kiov_page);
467 if (siov->kiov_len > soffset + this_nob) {
471 kunmap(siov->kiov_page);
480 kunmap(diov->kiov_page);
482 kunmap(siov->kiov_page);
484 EXPORT_SYMBOL(lnet_copy_kiov2kiov);
487 lnet_copy_kiov2iov (unsigned int niov, struct kvec *iov, unsigned int iovoffset,
488 unsigned int nkiov, lnet_kiov_t *kiov, unsigned int kiovoffset,
491 /* NB iov, kiov are READ-ONLY */
492 unsigned int this_nob;
498 LASSERT (!in_interrupt ());
501 while (iovoffset >= iov->iov_len) {
502 iovoffset -= iov->iov_len;
509 while (kiovoffset >= kiov->kiov_len) {
510 kiovoffset -= kiov->kiov_len;
519 this_nob = MIN(iov->iov_len - iovoffset,
520 kiov->kiov_len - kiovoffset);
521 this_nob = MIN(this_nob, nob);
524 addr = ((char *)kmap(kiov->kiov_page)) +
525 kiov->kiov_offset + kiovoffset;
527 memcpy((char *)iov->iov_base + iovoffset, addr, this_nob);
530 if (iov->iov_len > iovoffset + this_nob) {
531 iovoffset += this_nob;
538 if (kiov->kiov_len > kiovoffset + this_nob) {
540 kiovoffset += this_nob;
542 kunmap(kiov->kiov_page);
552 kunmap(kiov->kiov_page);
554 EXPORT_SYMBOL(lnet_copy_kiov2iov);
557 lnet_copy_iov2kiov(unsigned int nkiov, lnet_kiov_t *kiov, unsigned int kiovoffset,
558 unsigned int niov, struct kvec *iov, unsigned int iovoffset,
561 /* NB kiov, iov are READ-ONLY */
562 unsigned int this_nob;
568 LASSERT (!in_interrupt ());
571 while (kiovoffset >= kiov->kiov_len) {
572 kiovoffset -= kiov->kiov_len;
579 while (iovoffset >= iov->iov_len) {
580 iovoffset -= iov->iov_len;
589 this_nob = MIN(kiov->kiov_len - kiovoffset,
590 iov->iov_len - iovoffset);
591 this_nob = MIN(this_nob, nob);
594 addr = ((char *)kmap(kiov->kiov_page)) +
595 kiov->kiov_offset + kiovoffset;
597 memcpy (addr, (char *)iov->iov_base + iovoffset, this_nob);
600 if (kiov->kiov_len > kiovoffset + this_nob) {
602 kiovoffset += this_nob;
604 kunmap(kiov->kiov_page);
611 if (iov->iov_len > iovoffset + this_nob) {
612 iovoffset += this_nob;
621 kunmap(kiov->kiov_page);
623 EXPORT_SYMBOL(lnet_copy_iov2kiov);
626 lnet_extract_kiov(int dst_niov, lnet_kiov_t *dst,
627 int src_niov, lnet_kiov_t *src,
628 unsigned int offset, unsigned int len)
630 /* Initialise 'dst' to the subset of 'src' starting at 'offset',
631 * for exactly 'len' bytes, and return the number of entries.
632 * NB not destructive to 'src' */
633 unsigned int frag_len;
636 if (len == 0) /* no data => */
637 return (0); /* no frags */
639 LASSERT(src_niov > 0);
640 while (offset >= src->kiov_len) { /* skip initial frags */
641 offset -= src->kiov_len;
644 LASSERT(src_niov > 0);
649 LASSERT(src_niov > 0);
650 LASSERT((int)niov <= dst_niov);
652 frag_len = src->kiov_len - offset;
653 dst->kiov_page = src->kiov_page;
654 dst->kiov_offset = src->kiov_offset + offset;
656 if (len <= frag_len) {
658 LASSERT(dst->kiov_offset + dst->kiov_len <= PAGE_SIZE);
662 dst->kiov_len = frag_len;
663 LASSERT(dst->kiov_offset + dst->kiov_len <= PAGE_SIZE);
673 EXPORT_SYMBOL(lnet_extract_kiov);
676 lnet_ni_recv(struct lnet_ni *ni, void *private, struct lnet_msg *msg,
677 int delayed, unsigned int offset, unsigned int mlen,
680 unsigned int niov = 0;
681 struct kvec *iov = NULL;
682 lnet_kiov_t *kiov = NULL;
685 LASSERT (!in_interrupt ());
686 LASSERT (mlen == 0 || msg != NULL);
689 LASSERT(msg->msg_receiving);
690 LASSERT(!msg->msg_sending);
691 LASSERT(rlen == msg->msg_len);
692 LASSERT(mlen <= msg->msg_len);
693 LASSERT(msg->msg_offset == offset);
694 LASSERT(msg->msg_wanted == mlen);
696 msg->msg_receiving = 0;
699 niov = msg->msg_niov;
701 kiov = msg->msg_kiov;
704 LASSERT ((iov == NULL) != (kiov == NULL));
708 rc = (ni->ni_net->net_lnd->lnd_recv)(ni, private, msg, delayed,
709 niov, iov, kiov, offset, mlen,
712 lnet_finalize(msg, rc);
716 lnet_setpayloadbuffer(struct lnet_msg *msg)
718 struct lnet_libmd *md = msg->msg_md;
720 LASSERT(msg->msg_len > 0);
721 LASSERT(!msg->msg_routing);
723 LASSERT(msg->msg_niov == 0);
724 LASSERT(msg->msg_iov == NULL);
725 LASSERT(msg->msg_kiov == NULL);
727 msg->msg_niov = md->md_niov;
728 if ((md->md_options & LNET_MD_KIOV) != 0)
729 msg->msg_kiov = md->md_iov.kiov;
731 msg->msg_iov = md->md_iov.iov;
735 lnet_prep_send(struct lnet_msg *msg, int type, struct lnet_process_id target,
736 unsigned int offset, unsigned int len)
738 msg->msg_type = type;
739 msg->msg_target = target;
741 msg->msg_offset = offset;
744 lnet_setpayloadbuffer(msg);
746 memset (&msg->msg_hdr, 0, sizeof (msg->msg_hdr));
747 msg->msg_hdr.type = cpu_to_le32(type);
748 /* dest_nid will be overwritten by lnet_select_pathway() */
749 msg->msg_hdr.dest_nid = cpu_to_le64(target.nid);
750 msg->msg_hdr.dest_pid = cpu_to_le32(target.pid);
751 /* src_nid will be set later */
752 msg->msg_hdr.src_pid = cpu_to_le32(the_lnet.ln_pid);
753 msg->msg_hdr.payload_length = cpu_to_le32(len);
757 lnet_ni_send(struct lnet_ni *ni, struct lnet_msg *msg)
759 void *priv = msg->msg_private;
762 LASSERT (!in_interrupt ());
763 LASSERT (LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND ||
764 (msg->msg_txcredit && msg->msg_peertxcredit));
766 rc = (ni->ni_net->net_lnd->lnd_send)(ni, priv, msg);
768 msg->msg_no_resend = true;
769 lnet_finalize(msg, rc);
774 lnet_ni_eager_recv(struct lnet_ni *ni, struct lnet_msg *msg)
778 LASSERT(!msg->msg_sending);
779 LASSERT(msg->msg_receiving);
780 LASSERT(!msg->msg_rx_ready_delay);
781 LASSERT(ni->ni_net->net_lnd->lnd_eager_recv != NULL);
783 msg->msg_rx_ready_delay = 1;
784 rc = (ni->ni_net->net_lnd->lnd_eager_recv)(ni, msg->msg_private, msg,
787 CERROR("recv from %s / send to %s aborted: "
788 "eager_recv failed %d\n",
789 libcfs_nid2str(msg->msg_rxpeer->lpni_nid),
790 libcfs_id2str(msg->msg_target), rc);
791 LASSERT(rc < 0); /* required by my callers */
797 /* NB: returns 1 when alive, 0 when dead, negative when error;
798 * may drop the lnet_net_lock */
800 lnet_peer_alive_locked(struct lnet_ni *ni, struct lnet_peer_ni *lpni,
801 struct lnet_msg *msg)
803 if (!lnet_peer_aliveness_enabled(lpni))
807 * If we're resending a message, let's attempt to send it even if
808 * the peer is down to fulfill our resend quota on the message
810 if (msg->msg_retry_count > 0)
813 /* try and send recovery messages irregardless */
814 if (msg->msg_recovery)
817 /* always send any responses */
818 if (msg->msg_type == LNET_MSG_ACK ||
819 msg->msg_type == LNET_MSG_REPLY)
822 return lnet_is_peer_ni_alive(lpni);
826 * \param msg The message to be sent.
827 * \param do_send True if lnet_ni_send() should be called in this function.
828 * lnet_send() is going to lnet_net_unlock immediately after this, so
829 * it sets do_send FALSE and I don't do the unlock/send/lock bit.
831 * \retval LNET_CREDIT_OK If \a msg sent or OK to send.
832 * \retval LNET_CREDIT_WAIT If \a msg blocked for credit.
833 * \retval -EHOSTUNREACH If the next hop of the message appears dead.
834 * \retval -ECANCELED If the MD of the message has been unlinked.
837 lnet_post_send_locked(struct lnet_msg *msg, int do_send)
839 struct lnet_peer_ni *lp = msg->msg_txpeer;
840 struct lnet_ni *ni = msg->msg_txni;
841 int cpt = msg->msg_tx_cpt;
842 struct lnet_tx_queue *tq = ni->ni_tx_queues[cpt];
844 /* non-lnet_send() callers have checked before */
845 LASSERT(!do_send || msg->msg_tx_delayed);
846 LASSERT(!msg->msg_receiving);
847 LASSERT(msg->msg_tx_committed);
848 /* can't get here if we're sending to the loopback interface */
849 LASSERT(lp->lpni_nid != the_lnet.ln_loni->ni_nid);
851 /* NB 'lp' is always the next hop */
852 if ((msg->msg_target.pid & LNET_PID_USERFLAG) == 0 &&
853 lnet_peer_alive_locked(ni, lp, msg) == 0) {
854 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
855 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length +=
857 lnet_net_unlock(cpt);
859 lnet_incr_stats(&msg->msg_txpeer->lpni_stats,
861 LNET_STATS_TYPE_DROP);
863 lnet_incr_stats(&msg->msg_txni->ni_stats,
865 LNET_STATS_TYPE_DROP);
867 CNETERR("Dropping message for %s: peer not alive\n",
868 libcfs_id2str(msg->msg_target));
869 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_DROPPED;
871 lnet_finalize(msg, -EHOSTUNREACH);
874 return -EHOSTUNREACH;
877 if (msg->msg_md != NULL &&
878 (msg->msg_md->md_flags & LNET_MD_FLAG_ABORTED) != 0) {
879 lnet_net_unlock(cpt);
881 CNETERR("Aborting message for %s: LNetM[DE]Unlink() already "
882 "called on the MD/ME.\n",
883 libcfs_id2str(msg->msg_target));
885 msg->msg_no_resend = true;
886 CDEBUG(D_NET, "msg %p to %s canceled and will not be resent\n",
887 msg, libcfs_id2str(msg->msg_target));
888 lnet_finalize(msg, -ECANCELED);
895 if (!msg->msg_peertxcredit) {
896 spin_lock(&lp->lpni_lock);
897 LASSERT((lp->lpni_txcredits < 0) ==
898 !list_empty(&lp->lpni_txq));
900 msg->msg_peertxcredit = 1;
901 lp->lpni_txqnob += msg->msg_len + sizeof(struct lnet_hdr);
902 lp->lpni_txcredits--;
904 if (lp->lpni_txcredits < lp->lpni_mintxcredits)
905 lp->lpni_mintxcredits = lp->lpni_txcredits;
907 if (lp->lpni_txcredits < 0) {
908 msg->msg_tx_delayed = 1;
909 list_add_tail(&msg->msg_list, &lp->lpni_txq);
910 spin_unlock(&lp->lpni_lock);
911 return LNET_CREDIT_WAIT;
913 spin_unlock(&lp->lpni_lock);
916 if (!msg->msg_txcredit) {
917 LASSERT((tq->tq_credits < 0) ==
918 !list_empty(&tq->tq_delayed));
920 msg->msg_txcredit = 1;
922 atomic_dec(&ni->ni_tx_credits);
924 if (tq->tq_credits < tq->tq_credits_min)
925 tq->tq_credits_min = tq->tq_credits;
927 if (tq->tq_credits < 0) {
928 msg->msg_tx_delayed = 1;
929 list_add_tail(&msg->msg_list, &tq->tq_delayed);
930 return LNET_CREDIT_WAIT;
934 /* unset the tx_delay flag as we're going to send it now */
935 msg->msg_tx_delayed = 0;
938 lnet_net_unlock(cpt);
939 lnet_ni_send(ni, msg);
942 return LNET_CREDIT_OK;
946 static struct lnet_rtrbufpool *
947 lnet_msg2bufpool(struct lnet_msg *msg)
949 struct lnet_rtrbufpool *rbp;
952 LASSERT(msg->msg_rx_committed);
954 cpt = msg->msg_rx_cpt;
955 rbp = &the_lnet.ln_rtrpools[cpt][0];
957 LASSERT(msg->msg_len <= LNET_MTU);
958 while (msg->msg_len > (unsigned int)rbp->rbp_npages * PAGE_SIZE) {
960 LASSERT(rbp < &the_lnet.ln_rtrpools[cpt][LNET_NRBPOOLS]);
967 lnet_post_routed_recv_locked(struct lnet_msg *msg, int do_recv)
969 /* lnet_parse is going to lnet_net_unlock immediately after this, so it
970 * sets do_recv FALSE and I don't do the unlock/send/lock bit.
971 * I return LNET_CREDIT_WAIT if msg blocked and LNET_CREDIT_OK if
972 * received or OK to receive */
973 struct lnet_peer_ni *lpni = msg->msg_rxpeer;
974 struct lnet_peer *lp;
975 struct lnet_rtrbufpool *rbp;
976 struct lnet_rtrbuf *rb;
978 LASSERT(msg->msg_iov == NULL);
979 LASSERT(msg->msg_kiov == NULL);
980 LASSERT(msg->msg_niov == 0);
981 LASSERT(msg->msg_routing);
982 LASSERT(msg->msg_receiving);
983 LASSERT(!msg->msg_sending);
984 LASSERT(lpni->lpni_peer_net);
985 LASSERT(lpni->lpni_peer_net->lpn_peer);
987 lp = lpni->lpni_peer_net->lpn_peer;
989 /* non-lnet_parse callers only receive delayed messages */
990 LASSERT(!do_recv || msg->msg_rx_delayed);
992 if (!msg->msg_peerrtrcredit) {
993 /* lpni_lock protects the credit manipulation */
994 spin_lock(&lpni->lpni_lock);
995 /* lp_lock protects the lp_rtrq */
996 spin_lock(&lp->lp_lock);
998 msg->msg_peerrtrcredit = 1;
999 lpni->lpni_rtrcredits--;
1000 if (lpni->lpni_rtrcredits < lpni->lpni_minrtrcredits)
1001 lpni->lpni_minrtrcredits = lpni->lpni_rtrcredits;
1003 if (lpni->lpni_rtrcredits < 0) {
1004 /* must have checked eager_recv before here */
1005 LASSERT(msg->msg_rx_ready_delay);
1006 msg->msg_rx_delayed = 1;
1007 list_add_tail(&msg->msg_list, &lp->lp_rtrq);
1008 spin_unlock(&lp->lp_lock);
1009 spin_unlock(&lpni->lpni_lock);
1010 return LNET_CREDIT_WAIT;
1012 spin_unlock(&lp->lp_lock);
1013 spin_unlock(&lpni->lpni_lock);
1016 rbp = lnet_msg2bufpool(msg);
1018 if (!msg->msg_rtrcredit) {
1019 msg->msg_rtrcredit = 1;
1021 if (rbp->rbp_credits < rbp->rbp_mincredits)
1022 rbp->rbp_mincredits = rbp->rbp_credits;
1024 if (rbp->rbp_credits < 0) {
1025 /* must have checked eager_recv before here */
1026 LASSERT(msg->msg_rx_ready_delay);
1027 msg->msg_rx_delayed = 1;
1028 list_add_tail(&msg->msg_list, &rbp->rbp_msgs);
1029 return LNET_CREDIT_WAIT;
1033 LASSERT(!list_empty(&rbp->rbp_bufs));
1034 rb = list_entry(rbp->rbp_bufs.next, struct lnet_rtrbuf, rb_list);
1035 list_del(&rb->rb_list);
1037 msg->msg_niov = rbp->rbp_npages;
1038 msg->msg_kiov = &rb->rb_kiov[0];
1040 /* unset the msg-rx_delayed flag since we're receiving the message */
1041 msg->msg_rx_delayed = 0;
1044 int cpt = msg->msg_rx_cpt;
1046 lnet_net_unlock(cpt);
1047 lnet_ni_recv(msg->msg_rxni, msg->msg_private, msg, 1,
1048 0, msg->msg_len, msg->msg_len);
1051 return LNET_CREDIT_OK;
1055 lnet_return_tx_credits_locked(struct lnet_msg *msg)
1057 struct lnet_peer_ni *txpeer = msg->msg_txpeer;
1058 struct lnet_ni *txni = msg->msg_txni;
1059 struct lnet_msg *msg2;
1061 if (msg->msg_txcredit) {
1062 struct lnet_ni *ni = msg->msg_txni;
1063 struct lnet_tx_queue *tq = ni->ni_tx_queues[msg->msg_tx_cpt];
1065 /* give back NI txcredits */
1066 msg->msg_txcredit = 0;
1068 LASSERT((tq->tq_credits < 0) ==
1069 !list_empty(&tq->tq_delayed));
1072 atomic_inc(&ni->ni_tx_credits);
1073 if (tq->tq_credits <= 0) {
1074 msg2 = list_entry(tq->tq_delayed.next,
1075 struct lnet_msg, msg_list);
1076 list_del(&msg2->msg_list);
1078 LASSERT(msg2->msg_txni == ni);
1079 LASSERT(msg2->msg_tx_delayed);
1080 LASSERT(msg2->msg_tx_cpt == msg->msg_tx_cpt);
1082 (void) lnet_post_send_locked(msg2, 1);
1086 if (msg->msg_peertxcredit) {
1087 /* give back peer txcredits */
1088 msg->msg_peertxcredit = 0;
1090 spin_lock(&txpeer->lpni_lock);
1091 LASSERT((txpeer->lpni_txcredits < 0) ==
1092 !list_empty(&txpeer->lpni_txq));
1094 txpeer->lpni_txqnob -= msg->msg_len + sizeof(struct lnet_hdr);
1095 LASSERT(txpeer->lpni_txqnob >= 0);
1097 txpeer->lpni_txcredits++;
1098 if (txpeer->lpni_txcredits <= 0) {
1101 msg2 = list_entry(txpeer->lpni_txq.next,
1102 struct lnet_msg, msg_list);
1103 list_del(&msg2->msg_list);
1104 spin_unlock(&txpeer->lpni_lock);
1106 LASSERT(msg2->msg_txpeer == txpeer);
1107 LASSERT(msg2->msg_tx_delayed);
1109 msg2_cpt = msg2->msg_tx_cpt;
1112 * The msg_cpt can be different from the msg2_cpt
1113 * so we need to make sure we lock the correct cpt
1115 * Once we call lnet_post_send_locked() it is no
1116 * longer safe to access msg2, since it could've
1117 * been freed by lnet_finalize(), but we still
1118 * need to relock the correct cpt, so we cache the
1119 * msg2_cpt for the purpose of the check that
1120 * follows the call to lnet_pose_send_locked().
1122 if (msg2_cpt != msg->msg_tx_cpt) {
1123 lnet_net_unlock(msg->msg_tx_cpt);
1124 lnet_net_lock(msg2_cpt);
1126 (void) lnet_post_send_locked(msg2, 1);
1127 if (msg2_cpt != msg->msg_tx_cpt) {
1128 lnet_net_unlock(msg2_cpt);
1129 lnet_net_lock(msg->msg_tx_cpt);
1132 spin_unlock(&txpeer->lpni_lock);
1137 msg->msg_txni = NULL;
1138 lnet_ni_decref_locked(txni, msg->msg_tx_cpt);
1141 if (txpeer != NULL) {
1142 msg->msg_txpeer = NULL;
1143 lnet_peer_ni_decref_locked(txpeer);
1148 lnet_schedule_blocked_locked(struct lnet_rtrbufpool *rbp)
1150 struct lnet_msg *msg;
1152 if (list_empty(&rbp->rbp_msgs))
1154 msg = list_entry(rbp->rbp_msgs.next,
1155 struct lnet_msg, msg_list);
1156 list_del(&msg->msg_list);
1158 (void)lnet_post_routed_recv_locked(msg, 1);
1162 lnet_drop_routed_msgs_locked(struct list_head *list, int cpt)
1164 struct lnet_msg *msg;
1165 struct lnet_msg *tmp;
1167 lnet_net_unlock(cpt);
1169 list_for_each_entry_safe(msg, tmp, list, msg_list) {
1170 lnet_ni_recv(msg->msg_rxni, msg->msg_private, NULL,
1171 0, 0, 0, msg->msg_hdr.payload_length);
1172 list_del_init(&msg->msg_list);
1173 msg->msg_no_resend = true;
1174 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_ERROR;
1175 lnet_finalize(msg, -ECANCELED);
1182 lnet_return_rx_credits_locked(struct lnet_msg *msg)
1184 struct lnet_peer_ni *rxpeerni = msg->msg_rxpeer;
1185 struct lnet_peer *lp;
1186 struct lnet_ni *rxni = msg->msg_rxni;
1187 struct lnet_msg *msg2;
1189 if (msg->msg_rtrcredit) {
1190 /* give back global router credits */
1191 struct lnet_rtrbuf *rb;
1192 struct lnet_rtrbufpool *rbp;
1194 /* NB If a msg ever blocks for a buffer in rbp_msgs, it stays
1195 * there until it gets one allocated, or aborts the wait
1197 LASSERT(msg->msg_kiov != NULL);
1199 rb = list_entry(msg->msg_kiov, struct lnet_rtrbuf, rb_kiov[0]);
1202 msg->msg_kiov = NULL;
1203 msg->msg_rtrcredit = 0;
1205 LASSERT(rbp == lnet_msg2bufpool(msg));
1207 LASSERT((rbp->rbp_credits > 0) ==
1208 !list_empty(&rbp->rbp_bufs));
1210 /* If routing is now turned off, we just drop this buffer and
1211 * don't bother trying to return credits. */
1212 if (!the_lnet.ln_routing) {
1213 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1217 /* It is possible that a user has lowered the desired number of
1218 * buffers in this pool. Make sure we never put back
1219 * more buffers than the stated number. */
1220 if (unlikely(rbp->rbp_credits >= rbp->rbp_req_nbuffers)) {
1221 /* Discard this buffer so we don't have too
1223 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1224 rbp->rbp_nbuffers--;
1226 list_add(&rb->rb_list, &rbp->rbp_bufs);
1228 if (rbp->rbp_credits <= 0)
1229 lnet_schedule_blocked_locked(rbp);
1234 if (msg->msg_peerrtrcredit) {
1236 LASSERT(rxpeerni->lpni_peer_net);
1237 LASSERT(rxpeerni->lpni_peer_net->lpn_peer);
1239 lp = rxpeerni->lpni_peer_net->lpn_peer;
1241 /* give back peer router credits */
1242 msg->msg_peerrtrcredit = 0;
1244 spin_lock(&rxpeerni->lpni_lock);
1245 spin_lock(&lp->lp_lock);
1247 rxpeerni->lpni_rtrcredits++;
1249 /* drop all messages which are queued to be routed on that
1251 if (!the_lnet.ln_routing) {
1252 struct list_head drop;
1253 INIT_LIST_HEAD(&drop);
1254 list_splice_init(&lp->lp_rtrq, &drop);
1255 spin_unlock(&lp->lp_lock);
1256 spin_unlock(&rxpeerni->lpni_lock);
1257 lnet_drop_routed_msgs_locked(&drop, msg->msg_rx_cpt);
1258 } else if (!list_empty(&lp->lp_rtrq)) {
1261 msg2 = list_entry(lp->lp_rtrq.next,
1262 struct lnet_msg, msg_list);
1263 list_del(&msg2->msg_list);
1264 msg2_cpt = msg2->msg_rx_cpt;
1265 spin_unlock(&lp->lp_lock);
1266 spin_unlock(&rxpeerni->lpni_lock);
1268 * messages on the lp_rtrq can be from any NID in
1269 * the peer, which means they might have different
1270 * cpts. We need to make sure we lock the right
1273 if (msg2_cpt != msg->msg_rx_cpt) {
1274 lnet_net_unlock(msg->msg_rx_cpt);
1275 lnet_net_lock(msg2_cpt);
1277 (void) lnet_post_routed_recv_locked(msg2, 1);
1278 if (msg2_cpt != msg->msg_rx_cpt) {
1279 lnet_net_unlock(msg2_cpt);
1280 lnet_net_lock(msg->msg_rx_cpt);
1283 spin_unlock(&lp->lp_lock);
1284 spin_unlock(&rxpeerni->lpni_lock);
1288 msg->msg_rxni = NULL;
1289 lnet_ni_decref_locked(rxni, msg->msg_rx_cpt);
1291 if (rxpeerni != NULL) {
1292 msg->msg_rxpeer = NULL;
1293 lnet_peer_ni_decref_locked(rxpeerni);
1299 lnet_compare_peers(struct lnet_peer_ni *p1, struct lnet_peer_ni *p2)
1301 if (p1->lpni_txqnob < p2->lpni_txqnob)
1304 if (p1->lpni_txqnob > p2->lpni_txqnob)
1307 if (p1->lpni_txcredits > p2->lpni_txcredits)
1310 if (p1->lpni_txcredits < p2->lpni_txcredits)
1318 lnet_compare_routes(struct lnet_route *r1, struct lnet_route *r2)
1320 /* TODO re-implement gateway comparison
1321 struct lnet_peer_ni *p1 = r1->lr_gateway;
1322 struct lnet_peer_ni *p2 = r2->lr_gateway;
1324 int r1_hops = (r1->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r1->lr_hops;
1325 int r2_hops = (r2->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r2->lr_hops;
1328 if (r1->lr_priority < r2->lr_priority)
1331 if (r1->lr_priority > r2->lr_priority)
1334 if (r1_hops < r2_hops)
1337 if (r1_hops > r2_hops)
1341 rc = lnet_compare_peers(p1, p2);
1346 if (r1->lr_seq - r2->lr_seq <= 0)
1352 /* TODO: lnet_find_route_locked() needs to be reimplemented */
1353 static struct lnet_route *
1354 lnet_find_route_locked(struct lnet_net *net, __u32 remote_net,
1355 lnet_nid_t rtr_nid, struct lnet_route **prev_route)
1357 struct lnet_remotenet *rnet;
1358 struct lnet_route *route;
1359 struct lnet_route *best_route;
1360 struct lnet_route *last_route;
1361 struct lnet_peer *lp_best;
1362 struct lnet_peer *lp;
1365 /* If @rtr_nid is not LNET_NID_ANY, return the gateway with
1366 * rtr_nid nid, otherwise find the best gateway I can use */
1368 rnet = lnet_find_rnet_locked(remote_net);
1373 best_route = last_route = NULL;
1374 list_for_each_entry(route, &rnet->lrn_routes, lr_list) {
1375 lp = route->lr_gateway;
1377 if (!lnet_is_route_alive(route))
1380 if (lp_best == NULL) {
1381 best_route = last_route = route;
1386 /* no protection on below fields, but it's harmless */
1387 if (last_route->lr_seq - route->lr_seq < 0)
1390 rc = lnet_compare_routes(route, best_route);
1398 *prev_route = last_route;
1403 static struct lnet_ni *
1404 lnet_get_best_ni(struct lnet_net *local_net, struct lnet_ni *best_ni,
1405 struct lnet_peer *peer, struct lnet_peer_net *peer_net,
1408 struct lnet_ni *ni = NULL;
1409 unsigned int shortest_distance;
1414 * If there is no peer_ni that we can send to on this network,
1415 * then there is no point in looking for a new best_ni here.
1417 if (!lnet_get_next_peer_ni_locked(peer, peer_net, NULL))
1420 if (best_ni == NULL) {
1421 shortest_distance = UINT_MAX;
1422 best_credits = INT_MIN;
1425 shortest_distance = cfs_cpt_distance(lnet_cpt_table(), md_cpt,
1426 best_ni->ni_dev_cpt);
1427 best_credits = atomic_read(&best_ni->ni_tx_credits);
1428 best_healthv = atomic_read(&best_ni->ni_healthv);
1431 while ((ni = lnet_get_next_ni_locked(local_net, ni))) {
1432 unsigned int distance;
1437 ni_credits = atomic_read(&ni->ni_tx_credits);
1438 ni_healthv = atomic_read(&ni->ni_healthv);
1439 ni_fatal = atomic_read(&ni->ni_fatal_error_on);
1442 * calculate the distance from the CPT on which
1443 * the message memory is allocated to the CPT of
1444 * the NI's physical device
1446 distance = cfs_cpt_distance(lnet_cpt_table(),
1450 CDEBUG(D_NET, "compare ni %s [c:%d, d:%d, s:%d] with best_ni %s [c:%d, d:%d, s:%d]\n",
1451 libcfs_nid2str(ni->ni_nid), ni_credits, distance,
1452 ni->ni_seq, (best_ni) ? libcfs_nid2str(best_ni->ni_nid)
1453 : "not seleced", best_credits, shortest_distance,
1454 (best_ni) ? best_ni->ni_seq : 0);
1457 * All distances smaller than the NUMA range
1458 * are treated equally.
1460 if (distance < lnet_numa_range)
1461 distance = lnet_numa_range;
1464 * Select on health, shorter distance, available
1465 * credits, then round-robin.
1469 } else if (ni_healthv < best_healthv) {
1471 } else if (ni_healthv > best_healthv) {
1472 best_healthv = ni_healthv;
1474 * If we're going to prefer this ni because it's
1475 * the healthiest, then we should set the
1476 * shortest_distance in the algorithm in case
1477 * there are multiple NIs with the same health but
1478 * different distances.
1480 if (distance < shortest_distance)
1481 shortest_distance = distance;
1482 } else if (distance > shortest_distance) {
1484 } else if (distance < shortest_distance) {
1485 shortest_distance = distance;
1486 } else if (ni_credits < best_credits) {
1488 } else if (ni_credits == best_credits) {
1489 if (best_ni && best_ni->ni_seq <= ni->ni_seq)
1493 best_credits = ni_credits;
1496 CDEBUG(D_NET, "selected best_ni %s\n",
1497 (best_ni) ? libcfs_nid2str(best_ni->ni_nid) : "no selection");
1503 * Traffic to the LNET_RESERVED_PORTAL may not trigger peer discovery,
1504 * because such traffic is required to perform discovery. We therefore
1505 * exclude all GET and PUT on that portal. We also exclude all ACK and
1506 * REPLY traffic, but that is because the portal is not tracked in the
1507 * message structure for these message types. We could restrict this
1508 * further by also checking for LNET_PROTO_PING_MATCHBITS.
1511 lnet_msg_discovery(struct lnet_msg *msg)
1513 if (msg->msg_type == LNET_MSG_PUT) {
1514 if (msg->msg_hdr.msg.put.ptl_index != LNET_RESERVED_PORTAL)
1516 } else if (msg->msg_type == LNET_MSG_GET) {
1517 if (msg->msg_hdr.msg.get.ptl_index != LNET_RESERVED_PORTAL)
1523 #define SRC_SPEC 0x0001
1524 #define SRC_ANY 0x0002
1525 #define LOCAL_DST 0x0004
1526 #define REMOTE_DST 0x0008
1527 #define MR_DST 0x0010
1528 #define NMR_DST 0x0020
1529 #define SND_RESP 0x0040
1531 /* The following to defines are used for return codes */
1532 #define REPEAT_SEND 0x1000
1533 #define PASS_THROUGH 0x2000
1535 /* The different cases lnet_select pathway needs to handle */
1536 #define SRC_SPEC_LOCAL_MR_DST (SRC_SPEC | LOCAL_DST | MR_DST)
1537 #define SRC_SPEC_ROUTER_MR_DST (SRC_SPEC | REMOTE_DST | MR_DST)
1538 #define SRC_SPEC_LOCAL_NMR_DST (SRC_SPEC | LOCAL_DST | NMR_DST)
1539 #define SRC_SPEC_ROUTER_NMR_DST (SRC_SPEC | REMOTE_DST | NMR_DST)
1540 #define SRC_ANY_LOCAL_MR_DST (SRC_ANY | LOCAL_DST | MR_DST)
1541 #define SRC_ANY_ROUTER_MR_DST (SRC_ANY | REMOTE_DST | MR_DST)
1542 #define SRC_ANY_LOCAL_NMR_DST (SRC_ANY | LOCAL_DST | NMR_DST)
1543 #define SRC_ANY_ROUTER_NMR_DST (SRC_ANY | REMOTE_DST | NMR_DST)
1546 lnet_handle_lo_send(struct lnet_send_data *sd)
1548 struct lnet_msg *msg = sd->sd_msg;
1549 int cpt = sd->sd_cpt;
1551 /* No send credit hassles with LOLND */
1552 lnet_ni_addref_locked(the_lnet.ln_loni, cpt);
1553 msg->msg_hdr.dest_nid = cpu_to_le64(the_lnet.ln_loni->ni_nid);
1554 if (!msg->msg_routing)
1555 msg->msg_hdr.src_nid =
1556 cpu_to_le64(the_lnet.ln_loni->ni_nid);
1557 msg->msg_target.nid = the_lnet.ln_loni->ni_nid;
1558 lnet_msg_commit(msg, cpt);
1559 msg->msg_txni = the_lnet.ln_loni;
1561 return LNET_CREDIT_OK;
1565 lnet_handle_send(struct lnet_send_data *sd)
1567 struct lnet_ni *best_ni = sd->sd_best_ni;
1568 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
1569 struct lnet_peer_ni *final_dst_lpni = sd->sd_final_dst_lpni;
1570 struct lnet_msg *msg = sd->sd_msg;
1572 __u32 send_case = sd->sd_send_case;
1574 __u32 routing = send_case & REMOTE_DST;
1575 struct lnet_rsp_tracker *rspt;
1578 * Increment sequence number of the selected peer so that we
1579 * pick the next one in Round Robin.
1581 best_lpni->lpni_seq++;
1584 * grab a reference on the peer_ni so it sticks around even if
1585 * we need to drop and relock the lnet_net_lock below.
1587 lnet_peer_ni_addref_locked(best_lpni);
1590 * Use lnet_cpt_of_nid() to determine the CPT used to commit the
1591 * message. This ensures that we get a CPT that is correct for
1592 * the NI when the NI has been restricted to a subset of all CPTs.
1593 * If the selected CPT differs from the one currently locked, we
1594 * must unlock and relock the lnet_net_lock(), and then check whether
1595 * the configuration has changed. We don't have a hold on the best_ni
1596 * yet, and it may have vanished.
1598 cpt2 = lnet_cpt_of_nid_locked(best_lpni->lpni_nid, best_ni);
1599 if (sd->sd_cpt != cpt2) {
1600 __u32 seq = lnet_get_dlc_seq_locked();
1601 lnet_net_unlock(sd->sd_cpt);
1603 lnet_net_lock(sd->sd_cpt);
1604 if (seq != lnet_get_dlc_seq_locked()) {
1605 lnet_peer_ni_decref_locked(best_lpni);
1611 * store the best_lpni in the message right away to avoid having
1612 * to do the same operation under different conditions
1614 msg->msg_txpeer = best_lpni;
1615 msg->msg_txni = best_ni;
1618 * grab a reference for the best_ni since now it's in use in this
1619 * send. The reference will be dropped in lnet_finalize()
1621 lnet_ni_addref_locked(msg->msg_txni, sd->sd_cpt);
1624 * Always set the target.nid to the best peer picked. Either the
1625 * NID will be one of the peer NIDs selected, or the same NID as
1626 * what was originally set in the target or it will be the NID of
1627 * a router if this message should be routed
1629 msg->msg_target.nid = msg->msg_txpeer->lpni_nid;
1632 * lnet_msg_commit assigns the correct cpt to the message, which
1633 * is used to decrement the correct refcount on the ni when it's
1634 * time to return the credits
1636 lnet_msg_commit(msg, sd->sd_cpt);
1639 * If we are routing the message then we keep the src_nid that was
1640 * set by the originator. If we are not routing then we are the
1641 * originator and set it here.
1643 if (!msg->msg_routing)
1644 msg->msg_hdr.src_nid = cpu_to_le64(msg->msg_txni->ni_nid);
1647 msg->msg_target_is_router = 1;
1648 msg->msg_target.pid = LNET_PID_LUSTRE;
1650 * since we're routing we want to ensure that the
1651 * msg_hdr.dest_nid is set to the final destination. When
1652 * the router receives this message it knows how to route
1655 * final_dst_lpni is set at the beginning of the
1656 * lnet_select_pathway() function and is never changed.
1657 * It's safe to use it here.
1659 msg->msg_hdr.dest_nid = cpu_to_le64(final_dst_lpni->lpni_nid);
1662 * if we're not routing set the dest_nid to the best peer
1663 * ni NID that we picked earlier in the algorithm.
1665 msg->msg_hdr.dest_nid = cpu_to_le64(msg->msg_txpeer->lpni_nid);
1669 * if we have response tracker block update it with the next hop
1673 rspt = msg->msg_md->md_rspt_ptr;
1675 rspt->rspt_next_hop_nid = msg->msg_txpeer->lpni_nid;
1676 CDEBUG(D_NET, "rspt_next_hop_nid = %s\n",
1677 libcfs_nid2str(rspt->rspt_next_hop_nid));
1681 rc = lnet_post_send_locked(msg, 0);
1684 CDEBUG(D_NET, "TRACE: %s(%s:%s) -> %s(%s:%s) : %s try# %d\n",
1685 libcfs_nid2str(msg->msg_hdr.src_nid),
1686 libcfs_nid2str(msg->msg_txni->ni_nid),
1687 libcfs_nid2str(sd->sd_src_nid),
1688 libcfs_nid2str(msg->msg_hdr.dest_nid),
1689 libcfs_nid2str(sd->sd_dst_nid),
1690 libcfs_nid2str(msg->msg_txpeer->lpni_nid),
1691 lnet_msgtyp2str(msg->msg_type), msg->msg_retry_count);
1696 static struct lnet_peer_ni *
1697 lnet_select_peer_ni(struct lnet_send_data *sd, struct lnet_peer *peer,
1698 struct lnet_peer_net *peer_net)
1701 * Look at the peer NIs for the destination peer that connect
1702 * to the chosen net. If a peer_ni is preferred when using the
1703 * best_ni to communicate, we use that one. If there is no
1704 * preferred peer_ni, or there are multiple preferred peer_ni,
1705 * the available transmit credits are used. If the transmit
1706 * credits are equal, we round-robin over the peer_ni.
1708 struct lnet_peer_ni *lpni = NULL;
1709 struct lnet_peer_ni *best_lpni = NULL;
1710 struct lnet_ni *best_ni = sd->sd_best_ni;
1711 lnet_nid_t dst_nid = sd->sd_dst_nid;
1712 int best_lpni_credits = INT_MIN;
1713 bool preferred = false;
1715 int best_lpni_healthv = 0;
1718 while ((lpni = lnet_get_next_peer_ni_locked(peer, peer_net, lpni))) {
1720 * if the best_ni we've chosen aleady has this lpni
1721 * preferred, then let's use it
1723 ni_is_pref = lnet_peer_is_pref_nid_locked(lpni,
1726 lpni_healthv = atomic_read(&lpni->lpni_healthv);
1728 CDEBUG(D_NET, "%s ni_is_pref = %d\n",
1729 libcfs_nid2str(best_ni->ni_nid), ni_is_pref);
1732 CDEBUG(D_NET, "%s c:[%d, %d], s:[%d, %d]\n",
1733 libcfs_nid2str(lpni->lpni_nid),
1734 lpni->lpni_txcredits, best_lpni_credits,
1735 lpni->lpni_seq, best_lpni->lpni_seq);
1737 /* pick the healthiest peer ni */
1738 if (lpni_healthv < best_lpni_healthv) {
1740 } else if (lpni_healthv > best_lpni_healthv) {
1741 best_lpni_healthv = lpni_healthv;
1742 /* if this is a preferred peer use it */
1743 } else if (!preferred && ni_is_pref) {
1745 } else if (preferred && !ni_is_pref) {
1747 * this is not the preferred peer so let's ignore
1751 } else if (lpni->lpni_txcredits < best_lpni_credits) {
1753 * We already have a peer that has more credits
1754 * available than this one. No need to consider
1755 * this peer further.
1758 } else if (lpni->lpni_txcredits == best_lpni_credits) {
1760 * The best peer found so far and the current peer
1761 * have the same number of available credits let's
1762 * make sure to select between them using Round
1766 if (best_lpni->lpni_seq <= lpni->lpni_seq)
1772 best_lpni_credits = lpni->lpni_txcredits;
1775 /* if we still can't find a peer ni then we can't reach it */
1777 __u32 net_id = (peer_net) ? peer_net->lpn_net_id :
1778 LNET_NIDNET(dst_nid);
1779 CDEBUG(D_NET, "no peer_ni found on peer net %s\n",
1780 libcfs_net2str(net_id));
1784 CDEBUG(D_NET, "sd_best_lpni = %s\n",
1785 libcfs_nid2str(best_lpni->lpni_nid));
1791 * Prerequisite: the best_ni should already be set in the sd
1793 static inline struct lnet_peer_ni *
1794 lnet_find_best_lpni_on_net(struct lnet_send_data *sd, struct lnet_peer *peer,
1797 struct lnet_peer_net *peer_net;
1800 * The gateway is Multi-Rail capable so now we must select the
1803 peer_net = lnet_peer_get_net_locked(peer, net_id);
1806 CERROR("gateway peer %s has no NI on net %s\n",
1807 libcfs_nid2str(peer->lp_primary_nid),
1808 libcfs_net2str(net_id));
1812 return lnet_select_peer_ni(sd, peer, peer_net);
1816 lnet_set_non_mr_pref_nid(struct lnet_send_data *sd)
1818 if (sd->sd_send_case & NMR_DST &&
1819 sd->sd_msg->msg_type != LNET_MSG_REPLY &&
1820 sd->sd_msg->msg_type != LNET_MSG_ACK &&
1821 sd->sd_best_lpni->lpni_pref_nnids == 0) {
1822 CDEBUG(D_NET, "Setting preferred local NID %s on NMR peer %s\n",
1823 libcfs_nid2str(sd->sd_best_ni->ni_nid),
1824 libcfs_nid2str(sd->sd_best_lpni->lpni_nid));
1825 lnet_peer_ni_set_non_mr_pref_nid(sd->sd_best_lpni,
1826 sd->sd_best_ni->ni_nid);
1835 * use the source and destination NIDs as the pathway
1838 lnet_handle_spec_local_nmr_dst(struct lnet_send_data *sd)
1840 /* the destination lpni is set before we get here. */
1843 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
1844 if (!sd->sd_best_ni) {
1845 CERROR("Can't send to %s: src %s is not a "
1846 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
1847 libcfs_nid2str(sd->sd_src_nid));
1852 * the preferred NID will only be set for NMR peers
1854 lnet_set_non_mr_pref_nid(sd);
1856 return lnet_handle_send(sd);
1864 * Run the selection algorithm on the peer NIs unless we're sending
1865 * a response, in this case just send to the destination
1868 lnet_handle_spec_local_mr_dst(struct lnet_send_data *sd)
1870 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
1871 if (!sd->sd_best_ni) {
1872 CERROR("Can't send to %s: src %s is not a "
1873 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
1874 libcfs_nid2str(sd->sd_src_nid));
1879 * only run the selection algorithm to pick the peer_ni if we're
1880 * sending a GET or a PUT. Responses are sent to the same
1881 * destination NID provided.
1883 if (!(sd->sd_send_case & SND_RESP)) {
1885 lnet_find_best_lpni_on_net(sd, sd->sd_peer,
1886 sd->sd_best_ni->ni_net->net_id);
1889 if (sd->sd_best_lpni &&
1890 sd->sd_best_lpni->lpni_nid == the_lnet.ln_loni->ni_nid)
1891 return lnet_handle_lo_send(sd);
1892 else if (sd->sd_best_lpni)
1893 return lnet_handle_send(sd);
1895 CERROR("can't send to %s. no NI on %s\n",
1896 libcfs_nid2str(sd->sd_dst_nid),
1897 libcfs_net2str(sd->sd_best_ni->ni_net->net_id));
1899 return -EHOSTUNREACH;
1903 lnet_find_best_ni_on_spec_net(struct lnet_ni *cur_best_ni,
1904 struct lnet_peer *peer,
1905 struct lnet_peer_net *peer_net,
1909 struct lnet_net *local_net;
1910 struct lnet_ni *best_ni;
1912 local_net = lnet_get_net_locked(peer_net->lpn_net_id);
1917 * Iterate through the NIs in this local Net and select
1918 * the NI to send from. The selection is determined by
1919 * these 3 criterion in the following priority:
1921 * 2. NI available credits
1924 best_ni = lnet_get_best_ni(local_net, cur_best_ni,
1925 peer, peer_net, cpt);
1927 if (incr_seq && best_ni)
1934 lnet_initiate_peer_discovery(struct lnet_peer_ni *lpni,
1935 struct lnet_msg *msg, lnet_nid_t rtr_nid,
1938 struct lnet_peer *peer;
1939 lnet_nid_t primary_nid;
1942 lnet_peer_ni_addref_locked(lpni);
1944 peer = lpni->lpni_peer_net->lpn_peer;
1946 if (lnet_peer_gw_discovery(peer)) {
1947 lnet_peer_ni_decref_locked(lpni);
1951 rc = lnet_discover_peer_locked(lpni, cpt, false);
1953 lnet_peer_ni_decref_locked(lpni);
1956 /* The peer may have changed. */
1957 peer = lpni->lpni_peer_net->lpn_peer;
1958 /* queue message and return */
1959 msg->msg_rtr_nid_param = rtr_nid;
1960 msg->msg_sending = 0;
1961 msg->msg_txpeer = NULL;
1962 spin_lock(&peer->lp_lock);
1963 list_add_tail(&msg->msg_list, &peer->lp_dc_pendq);
1964 spin_unlock(&peer->lp_lock);
1965 lnet_peer_ni_decref_locked(lpni);
1966 primary_nid = peer->lp_primary_nid;
1968 CDEBUG(D_NET, "msg %p delayed. %s pending discovery\n",
1969 msg, libcfs_nid2str(primary_nid));
1971 return LNET_DC_WAIT;
1975 lnet_handle_find_routed_path(struct lnet_send_data *sd,
1977 struct lnet_peer_ni **gw_lpni,
1978 struct lnet_peer **gw_peer)
1980 struct lnet_peer *gw;
1981 struct lnet_route *best_route;
1982 struct lnet_route *last_route;
1983 struct lnet_peer_ni *lpni = NULL;
1984 lnet_nid_t src_nid = sd->sd_src_nid;
1986 best_route = lnet_find_route_locked(NULL, LNET_NIDNET(dst_nid),
1987 sd->sd_rtr_nid, &last_route);
1989 CERROR("no route to %s from %s\n",
1990 libcfs_nid2str(dst_nid), libcfs_nid2str(src_nid));
1991 return -EHOSTUNREACH;
1994 gw = best_route->lr_gateway;
1998 * Discover this gateway if it hasn't already been discovered.
1999 * This means we might delay the message until discovery has
2003 /* TODO: disable discovey for now */
2004 if (lnet_msg_discovery(sd->sd_msg) &&
2005 !lnet_peer_is_uptodate(*gw_peer)) {
2006 sd->sd_msg->msg_src_nid_param = sd->sd_src_nid;
2007 return lnet_initiate_peer_discovery(gw, sd->sd_msg,
2008 sd->sd_rtr_nid, sd->sd_cpt);
2012 if (!sd->sd_best_ni)
2013 sd->sd_best_ni = lnet_find_best_ni_on_spec_net(NULL, gw,
2014 lnet_peer_get_net_locked(gw,
2015 best_route->lr_lnet),
2019 if (!sd->sd_best_ni) {
2020 CERROR("Internal Error. Expected local ni on %s "
2021 "but non found :%s\n",
2022 libcfs_net2str(best_route->lr_lnet),
2023 libcfs_nid2str(sd->sd_src_nid));
2028 * if gw is MR let's find its best peer_ni
2030 if (lnet_peer_is_multi_rail(gw)) {
2031 lpni = lnet_find_best_lpni_on_net(sd, gw,
2032 sd->sd_best_ni->ni_net->net_id);
2034 * We've already verified that the gw has an NI on that
2035 * desired net, but we're not finding it. Something is
2039 CERROR("Internal Error. Route expected to %s from %s\n",
2040 libcfs_nid2str(dst_nid),
2041 libcfs_nid2str(src_nid));
2045 struct lnet_peer_net *lpn;
2046 lpn = lnet_peer_get_net_locked(gw, best_route->lr_lnet);
2048 CERROR("Internal Error. Route expected to %s from %s\n",
2049 libcfs_nid2str(dst_nid),
2050 libcfs_nid2str(src_nid));
2053 lpni = list_entry(lpn->lpn_peer_nis.next, struct lnet_peer_ni,
2056 CERROR("Internal Error. Route expected to %s from %s\n",
2057 libcfs_nid2str(dst_nid),
2058 libcfs_nid2str(src_nid));
2066 * increment the route sequence number since now we're sure we're
2069 LASSERT(best_route && last_route);
2070 best_route->lr_seq = last_route->lr_seq + 1;
2080 * Remote destination
2081 * Non-MR destination
2085 * Remote destination
2088 * The handling of these two cases is similar. Even though the destination
2089 * can be MR or non-MR, we'll deal directly with the router.
2092 lnet_handle_spec_router_dst(struct lnet_send_data *sd)
2095 struct lnet_peer_ni *gw_lpni = NULL;
2096 struct lnet_peer *gw_peer = NULL;
2099 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
2100 if (!sd->sd_best_ni) {
2101 CERROR("Can't send to %s: src %s is not a "
2102 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
2103 libcfs_nid2str(sd->sd_src_nid));
2107 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2112 if (sd->sd_send_case & NMR_DST)
2114 * since the final destination is non-MR let's set its preferred
2115 * NID before we send
2117 lnet_set_non_mr_pref_nid(sd);
2120 * We're going to send to the gw found so let's set its
2123 sd->sd_peer = gw_peer;
2124 sd->sd_best_lpni = gw_lpni;
2126 return lnet_handle_send(sd);
2130 lnet_find_best_ni_on_local_net(struct lnet_peer *peer, int md_cpt)
2132 struct lnet_peer_net *peer_net = NULL;
2133 struct lnet_ni *best_ni = NULL;
2136 * The peer can have multiple interfaces, some of them can be on
2137 * the local network and others on a routed network. We should
2138 * prefer the local network. However if the local network is not
2139 * available then we need to try the routed network
2142 /* go through all the peer nets and find the best_ni */
2143 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_peer_nets) {
2145 * The peer's list of nets can contain non-local nets. We
2146 * want to only examine the local ones.
2148 if (!lnet_get_net_locked(peer_net->lpn_net_id))
2150 best_ni = lnet_find_best_ni_on_spec_net(best_ni, peer,
2151 peer_net, md_cpt, false);
2155 /* increment sequence number so we can round robin */
2161 static struct lnet_ni *
2162 lnet_find_existing_preferred_best_ni(struct lnet_send_data *sd)
2164 struct lnet_ni *best_ni = NULL;
2165 struct lnet_peer_net *peer_net;
2166 struct lnet_peer *peer = sd->sd_peer;
2167 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
2168 struct lnet_peer_ni *lpni;
2169 int cpt = sd->sd_cpt;
2172 * We must use a consistent source address when sending to a
2173 * non-MR peer. However, a non-MR peer can have multiple NIDs
2174 * on multiple networks, and we may even need to talk to this
2175 * peer on multiple networks -- certain types of
2176 * load-balancing configuration do this.
2178 * So we need to pick the NI the peer prefers for this
2179 * particular network.
2182 /* Get the target peer_ni */
2183 peer_net = lnet_peer_get_net_locked(peer,
2184 LNET_NIDNET(best_lpni->lpni_nid));
2185 LASSERT(peer_net != NULL);
2186 list_for_each_entry(lpni, &peer_net->lpn_peer_nis,
2188 if (lpni->lpni_pref_nnids == 0)
2190 LASSERT(lpni->lpni_pref_nnids == 1);
2191 best_ni = lnet_nid2ni_locked(
2192 lpni->lpni_pref.nid, cpt);
2199 /* Prerequisite: sd->sd_peer and sd->sd_best_lpni should be set */
2201 lnet_select_preferred_best_ni(struct lnet_send_data *sd)
2203 struct lnet_ni *best_ni = NULL;
2204 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
2207 * We must use a consistent source address when sending to a
2208 * non-MR peer. However, a non-MR peer can have multiple NIDs
2209 * on multiple networks, and we may even need to talk to this
2210 * peer on multiple networks -- certain types of
2211 * load-balancing configuration do this.
2213 * So we need to pick the NI the peer prefers for this
2214 * particular network.
2217 best_ni = lnet_find_existing_preferred_best_ni(sd);
2219 /* if best_ni is still not set just pick one */
2222 lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2223 sd->sd_best_lpni->lpni_peer_net,
2224 sd->sd_md_cpt, true);
2225 /* If there is no best_ni we don't have a route */
2227 CERROR("no path to %s from net %s\n",
2228 libcfs_nid2str(best_lpni->lpni_nid),
2229 libcfs_net2str(best_lpni->lpni_net->net_id));
2230 return -EHOSTUNREACH;
2234 sd->sd_best_ni = best_ni;
2236 /* Set preferred NI if necessary. */
2237 lnet_set_non_mr_pref_nid(sd);
2244 * Source not specified
2248 * always use the same source NID for NMR peers
2249 * If we've talked to that peer before then we already have a preferred
2250 * source NI associated with it. Otherwise, we select a preferred local NI
2251 * and store it in the peer
2254 lnet_handle_any_local_nmr_dst(struct lnet_send_data *sd)
2258 /* sd->sd_best_lpni is already set to the final destination */
2261 * At this point we should've created the peer ni and peer. If we
2262 * can't find it, then something went wrong. Instead of assert
2263 * output a relevant message and fail the send
2265 if (!sd->sd_best_lpni) {
2266 CERROR("Internal fault. Unable to send msg %s to %s. "
2268 lnet_msgtyp2str(sd->sd_msg->msg_type),
2269 libcfs_nid2str(sd->sd_dst_nid));
2273 rc = lnet_select_preferred_best_ni(sd);
2275 rc = lnet_handle_send(sd);
2281 lnet_handle_any_mr_dsta(struct lnet_send_data *sd)
2284 * NOTE we've already handled the remote peer case. So we only
2285 * need to worry about the local case here.
2287 * if we're sending a response, ACK or reply, we need to send it
2288 * to the destination NID given to us. At this point we already
2289 * have the peer_ni we're suppose to send to, so just find the
2290 * best_ni on the peer net and use that. Since we're sending to an
2291 * MR peer then we can just run the selection algorithm on our
2292 * local NIs and pick the best one.
2294 if (sd->sd_send_case & SND_RESP) {
2296 lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2297 sd->sd_best_lpni->lpni_peer_net,
2298 sd->sd_md_cpt, true);
2300 if (!sd->sd_best_ni) {
2302 * We're not going to deal with not able to send
2303 * a response to the provided final destination
2305 CERROR("Can't send response to %s. "
2306 "No local NI available\n",
2307 libcfs_nid2str(sd->sd_dst_nid));
2308 return -EHOSTUNREACH;
2311 return lnet_handle_send(sd);
2315 * If we get here that means we're sending a fresh request, PUT or
2316 * GET, so we need to run our standard selection algorithm.
2317 * First find the best local interface that's on any of the peer's
2320 sd->sd_best_ni = lnet_find_best_ni_on_local_net(sd->sd_peer,
2322 if (sd->sd_best_ni) {
2324 lnet_find_best_lpni_on_net(sd, sd->sd_peer,
2325 sd->sd_best_ni->ni_net->net_id);
2328 * if we're successful in selecting a peer_ni on the local
2329 * network, then send to it. Otherwise fall through and
2330 * try and see if we can reach it over another routed
2333 if (sd->sd_best_lpni &&
2334 sd->sd_best_lpni->lpni_nid == the_lnet.ln_loni->ni_nid) {
2336 * in case we initially started with a routed
2337 * destination, let's reset to local
2339 sd->sd_send_case &= ~REMOTE_DST;
2340 sd->sd_send_case |= LOCAL_DST;
2341 return lnet_handle_lo_send(sd);
2342 } else if (sd->sd_best_lpni) {
2344 * in case we initially started with a routed
2345 * destination, let's reset to local
2347 sd->sd_send_case &= ~REMOTE_DST;
2348 sd->sd_send_case |= LOCAL_DST;
2349 return lnet_handle_send(sd);
2352 CERROR("Internal Error. Expected to have a best_lpni: "
2354 libcfs_nid2str(sd->sd_src_nid),
2355 libcfs_nid2str(sd->sd_dst_nid));
2361 * Peer doesn't have a local network. Let's see if there is
2362 * a remote network we can reach it on.
2364 return PASS_THROUGH;
2369 * Source NID not specified
2374 * Source NID not speified
2375 * Remote destination
2378 * In both of these cases if we're sending a response, ACK or REPLY, then
2379 * we need to send to the destination NID provided.
2381 * In the remote case let's deal with MR routers.
2386 lnet_handle_any_mr_dst(struct lnet_send_data *sd)
2389 struct lnet_peer *gw_peer = NULL;
2390 struct lnet_peer_ni *gw_lpni = NULL;
2393 * handle sending a response to a remote peer here so we don't
2394 * have to worry about it if we hit lnet_handle_any_mr_dsta()
2396 if (sd->sd_send_case & REMOTE_DST &&
2397 sd->sd_send_case & SND_RESP) {
2398 struct lnet_peer_ni *gw;
2399 struct lnet_peer *gw_peer;
2401 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw,
2404 CERROR("Can't send response to %s. "
2405 "No route available\n",
2406 libcfs_nid2str(sd->sd_dst_nid));
2407 return -EHOSTUNREACH;
2408 } else if (rc > 0) {
2412 sd->sd_best_lpni = gw;
2413 sd->sd_peer = gw_peer;
2415 return lnet_handle_send(sd);
2419 * Even though the NID for the peer might not be on a local network,
2420 * since the peer is MR there could be other interfaces on the
2421 * local network. In that case we'd still like to prefer the local
2422 * network over the routed network. If we're unable to do that
2423 * then we select the best router among the different routed networks,
2424 * and if the router is MR then we can deal with it as such.
2426 rc = lnet_handle_any_mr_dsta(sd);
2427 if (rc != PASS_THROUGH)
2431 * TODO; One possible enhancement is to run the selection
2432 * algorithm on the peer. However for remote peers the credits are
2433 * not decremented, so we'll be basically going over the peer NIs
2434 * in round robin. An MR router will run the selection algorithm
2435 * on the next-hop interfaces.
2437 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2442 sd->sd_send_case &= ~LOCAL_DST;
2443 sd->sd_send_case |= REMOTE_DST;
2445 sd->sd_peer = gw_peer;
2446 sd->sd_best_lpni = gw_lpni;
2448 return lnet_handle_send(sd);
2452 * Source not specified
2453 * Remote destination
2456 * Must send to the specified peer NID using the same source NID that
2457 * we've used before. If it's the first time to talk to that peer then
2458 * find the source NI and assign it as preferred to that peer
2461 lnet_handle_any_router_nmr_dst(struct lnet_send_data *sd)
2464 struct lnet_peer_ni *gw_lpni = NULL;
2465 struct lnet_peer *gw_peer = NULL;
2468 * Let's set if we have a preferred NI to talk to this NMR peer
2470 sd->sd_best_ni = lnet_find_existing_preferred_best_ni(sd);
2473 * find the router and that'll find the best NI if we didn't find
2476 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2482 * set the best_ni we've chosen as the preferred one for
2485 lnet_set_non_mr_pref_nid(sd);
2487 /* we'll be sending to the gw */
2488 sd->sd_best_lpni = gw_lpni;
2489 sd->sd_peer = gw_peer;
2491 return lnet_handle_send(sd);
2495 lnet_handle_send_case_locked(struct lnet_send_data *sd)
2498 * turn off the SND_RESP bit.
2499 * It will be checked in the case handling
2501 __u32 send_case = sd->sd_send_case &= ~SND_RESP ;
2503 CDEBUG(D_NET, "Source %s%s to %s %s %s destination\n",
2504 (send_case & SRC_SPEC) ? "Specified: " : "ANY",
2505 (send_case & SRC_SPEC) ? libcfs_nid2str(sd->sd_src_nid) : "",
2506 (send_case & MR_DST) ? "MR: " : "NMR: ",
2507 libcfs_nid2str(sd->sd_dst_nid),
2508 (send_case & LOCAL_DST) ? "local" : "routed");
2510 switch (send_case) {
2512 * For all cases where the source is specified, we should always
2513 * use the destination NID, whether it's an MR destination or not,
2514 * since we're continuing a series of related messages for the
2517 case SRC_SPEC_LOCAL_NMR_DST:
2518 return lnet_handle_spec_local_nmr_dst(sd);
2519 case SRC_SPEC_LOCAL_MR_DST:
2520 return lnet_handle_spec_local_mr_dst(sd);
2521 case SRC_SPEC_ROUTER_NMR_DST:
2522 case SRC_SPEC_ROUTER_MR_DST:
2523 return lnet_handle_spec_router_dst(sd);
2524 case SRC_ANY_LOCAL_NMR_DST:
2525 return lnet_handle_any_local_nmr_dst(sd);
2526 case SRC_ANY_LOCAL_MR_DST:
2527 case SRC_ANY_ROUTER_MR_DST:
2528 return lnet_handle_any_mr_dst(sd);
2529 case SRC_ANY_ROUTER_NMR_DST:
2530 return lnet_handle_any_router_nmr_dst(sd);
2532 CERROR("Unknown send case\n");
2538 lnet_select_pathway(lnet_nid_t src_nid, lnet_nid_t dst_nid,
2539 struct lnet_msg *msg, lnet_nid_t rtr_nid)
2541 struct lnet_peer_ni *lpni;
2542 struct lnet_peer *peer;
2543 struct lnet_send_data send_data;
2546 __u32 send_case = 0;
2548 memset(&send_data, 0, sizeof(send_data));
2551 * get an initial CPT to use for locking. The idea here is not to
2552 * serialize the calls to select_pathway, so that as many
2553 * operations can run concurrently as possible. To do that we use
2554 * the CPT where this call is being executed. Later on when we
2555 * determine the CPT to use in lnet_message_commit, we switch the
2556 * lock and check if there was any configuration change. If none,
2557 * then we proceed, if there is, then we restart the operation.
2559 cpt = lnet_net_lock_current();
2561 md_cpt = lnet_cpt_of_md(msg->msg_md, msg->msg_offset);
2562 if (md_cpt == CFS_CPT_ANY)
2568 * If we're being asked to send to the loopback interface, there
2569 * is no need to go through any selection. We can just shortcut
2570 * the entire process and send over lolnd
2572 send_data.sd_msg = msg;
2573 send_data.sd_cpt = cpt;
2574 if (LNET_NETTYP(LNET_NIDNET(dst_nid)) == LOLND) {
2575 rc = lnet_handle_lo_send(&send_data);
2576 lnet_net_unlock(cpt);
2581 * find an existing peer_ni, or create one and mark it as having been
2582 * created due to network traffic. This call will create the
2583 * peer->peer_net->peer_ni tree.
2585 lpni = lnet_nid2peerni_locked(dst_nid, LNET_NID_ANY, cpt);
2587 lnet_net_unlock(cpt);
2588 return PTR_ERR(lpni);
2592 * Cache the original src_nid. If we need to resend the message
2593 * then we'll need to know whether the src_nid was originally
2594 * specified for this message. If it was originally specified,
2595 * then we need to keep using the same src_nid since it's
2596 * continuing the same sequence of messages.
2598 msg->msg_src_nid_param = src_nid;
2601 * Now that we have a peer_ni, check if we want to discover
2602 * the peer. Traffic to the LNET_RESERVED_PORTAL should not
2603 * trigger discovery.
2605 peer = lpni->lpni_peer_net->lpn_peer;
2606 if (lnet_msg_discovery(msg) && !lnet_peer_is_uptodate(peer)) {
2607 rc = lnet_initiate_peer_discovery(lpni, msg, rtr_nid, cpt);
2608 lnet_peer_ni_decref_locked(lpni);
2609 lnet_net_unlock(cpt);
2612 lnet_peer_ni_decref_locked(lpni);
2615 * Identify the different send cases
2617 if (src_nid == LNET_NID_ANY)
2618 send_case |= SRC_ANY;
2620 send_case |= SRC_SPEC;
2622 if (lnet_get_net_locked(LNET_NIDNET(dst_nid)))
2623 send_case |= LOCAL_DST;
2625 send_case |= REMOTE_DST;
2628 * if this is a non-MR peer or if we're recovering a peer ni then
2629 * let's consider this an NMR case so we can hit the destination
2632 if (!lnet_peer_is_multi_rail(peer) || msg->msg_recovery)
2633 send_case |= NMR_DST;
2635 send_case |= MR_DST;
2637 if (msg->msg_type == LNET_MSG_REPLY ||
2638 msg->msg_type == LNET_MSG_ACK)
2639 send_case |= SND_RESP;
2641 /* assign parameters to the send_data */
2642 send_data.sd_rtr_nid = rtr_nid;
2643 send_data.sd_src_nid = src_nid;
2644 send_data.sd_dst_nid = dst_nid;
2645 send_data.sd_best_lpni = lpni;
2647 * keep a pointer to the final destination in case we're going to
2648 * route, so we'll need to access it later
2650 send_data.sd_final_dst_lpni = lpni;
2651 send_data.sd_peer = peer;
2652 send_data.sd_md_cpt = md_cpt;
2653 send_data.sd_send_case = send_case;
2655 rc = lnet_handle_send_case_locked(&send_data);
2658 * Update the local cpt since send_data.sd_cpt might've been
2659 * updated as a result of calling lnet_handle_send_case_locked().
2661 cpt = send_data.sd_cpt;
2663 if (rc == REPEAT_SEND)
2666 lnet_net_unlock(cpt);
2672 lnet_send(lnet_nid_t src_nid, struct lnet_msg *msg, lnet_nid_t rtr_nid)
2674 lnet_nid_t dst_nid = msg->msg_target.nid;
2678 * NB: rtr_nid is set to LNET_NID_ANY for all current use-cases,
2679 * but we might want to use pre-determined router for ACK/REPLY
2682 /* NB: ni != NULL == interface pre-determined (ACK/REPLY) */
2683 LASSERT(msg->msg_txpeer == NULL);
2684 LASSERT(msg->msg_txni == NULL);
2685 LASSERT(!msg->msg_sending);
2686 LASSERT(!msg->msg_target_is_router);
2687 LASSERT(!msg->msg_receiving);
2689 msg->msg_sending = 1;
2691 LASSERT(!msg->msg_tx_committed);
2693 rc = lnet_select_pathway(src_nid, dst_nid, msg, rtr_nid);
2697 if (rc == LNET_CREDIT_OK)
2698 lnet_ni_send(msg->msg_txni, msg);
2700 /* rc == LNET_CREDIT_OK or LNET_CREDIT_WAIT or LNET_DC_WAIT */
2704 enum lnet_mt_event_type {
2705 MT_TYPE_LOCAL_NI = 0,
2709 struct lnet_mt_event_info {
2710 enum lnet_mt_event_type mt_type;
2714 /* called with res_lock held */
2716 lnet_detach_rsp_tracker(struct lnet_libmd *md, int cpt)
2718 struct lnet_rsp_tracker *rspt;
2721 * msg has a refcount on the MD so the MD is not going away.
2722 * The rspt queue for the cpt is protected by
2723 * the lnet_net_lock(cpt). cpt is the cpt of the MD cookie.
2725 if (!md->md_rspt_ptr)
2728 rspt = md->md_rspt_ptr;
2729 md->md_rspt_ptr = NULL;
2732 LASSERT(rspt->rspt_cpt == cpt);
2735 * invalidate the handle to indicate that a response has been
2736 * received, which will then lead the monitor thread to clean up
2739 LNetInvalidateMDHandle(&rspt->rspt_mdh);
2743 lnet_finalize_expired_responses(bool force)
2745 struct lnet_libmd *md;
2746 struct list_head local_queue;
2747 struct lnet_rsp_tracker *rspt, *tmp;
2750 if (the_lnet.ln_mt_rstq == NULL)
2753 cfs_cpt_for_each(i, lnet_cpt_table()) {
2754 INIT_LIST_HEAD(&local_queue);
2757 if (!the_lnet.ln_mt_rstq[i]) {
2761 list_splice_init(the_lnet.ln_mt_rstq[i], &local_queue);
2764 list_for_each_entry_safe(rspt, tmp, &local_queue, rspt_on_list) {
2766 * The rspt mdh will be invalidated when a response
2767 * is received or whenever we want to discard the
2768 * block the monitor thread will walk the queue
2769 * and clean up any rsts with an invalid mdh.
2770 * The monitor thread will walk the queue until
2771 * the first unexpired rspt block. This means that
2772 * some rspt blocks which received their
2773 * corresponding responses will linger in the
2774 * queue until they are cleaned up eventually.
2777 if (LNetMDHandleIsInvalid(rspt->rspt_mdh)) {
2779 list_del_init(&rspt->rspt_on_list);
2780 lnet_rspt_free(rspt, i);
2784 if (ktime_compare(ktime_get(), rspt->rspt_deadline) >= 0 ||
2786 struct lnet_peer_ni *lpni;
2789 md = lnet_handle2md(&rspt->rspt_mdh);
2791 LNetInvalidateMDHandle(&rspt->rspt_mdh);
2793 list_del_init(&rspt->rspt_on_list);
2794 lnet_rspt_free(rspt, i);
2797 LASSERT(md->md_rspt_ptr == rspt);
2798 md->md_rspt_ptr = NULL;
2802 the_lnet.ln_counters[i]->lct_health.lch_response_timeout_count++;
2805 list_del_init(&rspt->rspt_on_list);
2807 nid = rspt->rspt_next_hop_nid;
2809 CNETERR("Response timed out: md = %p: nid = %s\n",
2810 md, libcfs_nid2str(nid));
2811 LNetMDUnlink(rspt->rspt_mdh);
2812 lnet_rspt_free(rspt, i);
2815 * If there is a timeout on the response
2816 * from the next hop decrement its health
2817 * value so that we don't use it
2820 lpni = lnet_find_peer_ni_locked(nid);
2822 lnet_handle_remote_failure_locked(lpni);
2823 lnet_peer_ni_decref_locked(lpni);
2833 if (!list_empty(&local_queue))
2834 list_splice(&local_queue, the_lnet.ln_mt_rstq[i]);
2840 lnet_resend_pending_msgs_locked(struct list_head *resendq, int cpt)
2842 struct lnet_msg *msg;
2844 while (!list_empty(resendq)) {
2845 struct lnet_peer_ni *lpni;
2847 msg = list_entry(resendq->next, struct lnet_msg,
2850 list_del_init(&msg->msg_list);
2852 lpni = lnet_find_peer_ni_locked(msg->msg_hdr.dest_nid);
2854 lnet_net_unlock(cpt);
2855 CERROR("Expected that a peer is already created for %s\n",
2856 libcfs_nid2str(msg->msg_hdr.dest_nid));
2857 msg->msg_no_resend = true;
2858 lnet_finalize(msg, -EFAULT);
2861 struct lnet_peer *peer;
2863 lnet_nid_t src_nid = LNET_NID_ANY;
2866 * if this message is not being routed and the
2867 * peer is non-MR then we must use the same
2868 * src_nid that was used in the original send.
2869 * Otherwise if we're routing the message (IE
2870 * we're a router) then we can use any of our
2871 * local interfaces. It doesn't matter to the
2872 * final destination.
2874 peer = lpni->lpni_peer_net->lpn_peer;
2875 if (!msg->msg_routing &&
2876 !lnet_peer_is_multi_rail(peer))
2877 src_nid = le64_to_cpu(msg->msg_hdr.src_nid);
2880 * If we originally specified a src NID, then we
2881 * must attempt to reuse it in the resend as well.
2883 if (msg->msg_src_nid_param != LNET_NID_ANY)
2884 src_nid = msg->msg_src_nid_param;
2885 lnet_peer_ni_decref_locked(lpni);
2887 lnet_net_unlock(cpt);
2888 CDEBUG(D_NET, "resending %s->%s: %s recovery %d try# %d\n",
2889 libcfs_nid2str(src_nid),
2890 libcfs_id2str(msg->msg_target),
2891 lnet_msgtyp2str(msg->msg_type),
2893 msg->msg_retry_count);
2894 rc = lnet_send(src_nid, msg, LNET_NID_ANY);
2896 CERROR("Error sending %s to %s: %d\n",
2897 lnet_msgtyp2str(msg->msg_type),
2898 libcfs_id2str(msg->msg_target), rc);
2899 msg->msg_no_resend = true;
2900 lnet_finalize(msg, rc);
2904 the_lnet.ln_counters[cpt]->lct_health.lch_resend_count++;
2910 lnet_resend_pending_msgs(void)
2914 cfs_cpt_for_each(i, lnet_cpt_table()) {
2916 lnet_resend_pending_msgs_locked(the_lnet.ln_mt_resendqs[i], i);
2921 /* called with cpt and ni_lock held */
2923 lnet_unlink_ni_recovery_mdh_locked(struct lnet_ni *ni, int cpt, bool force)
2925 struct lnet_handle_md recovery_mdh;
2927 LNetInvalidateMDHandle(&recovery_mdh);
2929 if (ni->ni_recovery_state & LNET_NI_RECOVERY_PENDING ||
2931 recovery_mdh = ni->ni_ping_mdh;
2932 LNetInvalidateMDHandle(&ni->ni_ping_mdh);
2935 lnet_net_unlock(cpt);
2936 if (!LNetMDHandleIsInvalid(recovery_mdh))
2937 LNetMDUnlink(recovery_mdh);
2943 lnet_recover_local_nis(void)
2945 struct lnet_mt_event_info *ev_info;
2946 struct list_head processed_list;
2947 struct list_head local_queue;
2948 struct lnet_handle_md mdh;
2949 struct lnet_ni *tmp;
2955 INIT_LIST_HEAD(&local_queue);
2956 INIT_LIST_HEAD(&processed_list);
2959 * splice the recovery queue on a local queue. We will iterate
2960 * through the local queue and update it as needed. Once we're
2961 * done with the traversal, we'll splice the local queue back on
2962 * the head of the ln_mt_localNIRecovq. Any newly added local NIs
2963 * will be traversed in the next iteration.
2966 list_splice_init(&the_lnet.ln_mt_localNIRecovq,
2970 list_for_each_entry_safe(ni, tmp, &local_queue, ni_recovery) {
2972 * if an NI is being deleted or it is now healthy, there
2973 * is no need to keep it around in the recovery queue.
2974 * The monitor thread is the only thread responsible for
2975 * removing the NI from the recovery queue.
2976 * Multiple threads can be adding NIs to the recovery
2979 healthv = atomic_read(&ni->ni_healthv);
2983 if (ni->ni_state != LNET_NI_STATE_ACTIVE ||
2984 healthv == LNET_MAX_HEALTH_VALUE) {
2985 list_del_init(&ni->ni_recovery);
2986 lnet_unlink_ni_recovery_mdh_locked(ni, 0, false);
2988 lnet_ni_decref_locked(ni, 0);
2994 * if the local NI failed recovery we must unlink the md.
2995 * But we want to keep the local_ni on the recovery queue
2996 * so we can continue the attempts to recover it.
2998 if (ni->ni_recovery_state & LNET_NI_RECOVERY_FAILED) {
2999 lnet_unlink_ni_recovery_mdh_locked(ni, 0, true);
3000 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_FAILED;
3007 CDEBUG(D_NET, "attempting to recover local ni: %s\n",
3008 libcfs_nid2str(ni->ni_nid));
3011 if (!(ni->ni_recovery_state & LNET_NI_RECOVERY_PENDING)) {
3012 ni->ni_recovery_state |= LNET_NI_RECOVERY_PENDING;
3015 LIBCFS_ALLOC(ev_info, sizeof(*ev_info));
3017 CERROR("out of memory. Can't recover %s\n",
3018 libcfs_nid2str(ni->ni_nid));
3020 ni->ni_recovery_state &=
3021 ~LNET_NI_RECOVERY_PENDING;
3026 mdh = ni->ni_ping_mdh;
3028 * Invalidate the ni mdh in case it's deleted.
3029 * We'll unlink the mdh in this case below.
3031 LNetInvalidateMDHandle(&ni->ni_ping_mdh);
3035 * remove the NI from the local queue and drop the
3036 * reference count to it while we're recovering
3037 * it. The reason for that, is that the NI could
3038 * be deleted, and the way the code is structured
3039 * is if we don't drop the NI, then the deletion
3040 * code will enter a loop waiting for the
3041 * reference count to be removed while holding the
3042 * ln_mutex_lock(). When we look up the peer to
3043 * send to in lnet_select_pathway() we will try to
3044 * lock the ln_mutex_lock() as well, leading to
3045 * a deadlock. By dropping the refcount and
3046 * removing it from the list, we allow for the NI
3047 * to be removed, then we use the cached NID to
3048 * look it up again. If it's gone, then we just
3049 * continue examining the rest of the queue.
3052 list_del_init(&ni->ni_recovery);
3053 lnet_ni_decref_locked(ni, 0);
3056 ev_info->mt_type = MT_TYPE_LOCAL_NI;
3057 ev_info->mt_nid = nid;
3058 rc = lnet_send_ping(nid, &mdh, LNET_INTERFACES_MIN,
3059 ev_info, the_lnet.ln_mt_eqh, true);
3060 /* lookup the nid again */
3062 ni = lnet_nid2ni_locked(nid, 0);
3065 * the NI has been deleted when we dropped
3073 * Same note as in lnet_recover_peer_nis(). When
3074 * we're sending the ping, the NI is free to be
3075 * deleted or manipulated. By this point it
3076 * could've been added back on the recovery queue,
3077 * and a refcount taken on it.
3078 * So we can't just add it blindly again or we'll
3079 * corrupt the queue. We must check under lock if
3080 * it's not on any list and if not then add it
3081 * to the processed list, which will eventually be
3082 * spliced back on to the recovery queue.
3084 ni->ni_ping_mdh = mdh;
3085 if (list_empty(&ni->ni_recovery)) {
3086 list_add_tail(&ni->ni_recovery, &processed_list);
3087 lnet_ni_addref_locked(ni, 0);
3093 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_PENDING;
3099 * put back the remaining NIs on the ln_mt_localNIRecovq to be
3100 * reexamined in the next iteration.
3102 list_splice_init(&processed_list, &local_queue);
3104 list_splice(&local_queue, &the_lnet.ln_mt_localNIRecovq);
3108 static struct list_head **
3109 lnet_create_array_of_queues(void)
3111 struct list_head **qs;
3112 struct list_head *q;
3115 qs = cfs_percpt_alloc(lnet_cpt_table(),
3116 sizeof(struct list_head));
3118 CERROR("Failed to allocate queues\n");
3122 cfs_percpt_for_each(q, i, qs)
3129 lnet_resendqs_create(void)
3131 struct list_head **resendqs;
3132 resendqs = lnet_create_array_of_queues();
3137 lnet_net_lock(LNET_LOCK_EX);
3138 the_lnet.ln_mt_resendqs = resendqs;
3139 lnet_net_unlock(LNET_LOCK_EX);
3145 lnet_clean_local_ni_recoveryq(void)
3149 /* This is only called when the monitor thread has stopped */
3152 while (!list_empty(&the_lnet.ln_mt_localNIRecovq)) {
3153 ni = list_entry(the_lnet.ln_mt_localNIRecovq.next,
3154 struct lnet_ni, ni_recovery);
3155 list_del_init(&ni->ni_recovery);
3157 lnet_unlink_ni_recovery_mdh_locked(ni, 0, true);
3159 lnet_ni_decref_locked(ni, 0);
3166 lnet_unlink_lpni_recovery_mdh_locked(struct lnet_peer_ni *lpni, int cpt,
3169 struct lnet_handle_md recovery_mdh;
3171 LNetInvalidateMDHandle(&recovery_mdh);
3173 if (lpni->lpni_state & LNET_PEER_NI_RECOVERY_PENDING || force) {
3174 recovery_mdh = lpni->lpni_recovery_ping_mdh;
3175 LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
3177 spin_unlock(&lpni->lpni_lock);
3178 lnet_net_unlock(cpt);
3179 if (!LNetMDHandleIsInvalid(recovery_mdh))
3180 LNetMDUnlink(recovery_mdh);
3182 spin_lock(&lpni->lpni_lock);
3186 lnet_clean_peer_ni_recoveryq(void)
3188 struct lnet_peer_ni *lpni, *tmp;
3190 lnet_net_lock(LNET_LOCK_EX);
3192 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_mt_peerNIRecovq,
3194 list_del_init(&lpni->lpni_recovery);
3195 spin_lock(&lpni->lpni_lock);
3196 lnet_unlink_lpni_recovery_mdh_locked(lpni, LNET_LOCK_EX, true);
3197 spin_unlock(&lpni->lpni_lock);
3198 lnet_peer_ni_decref_locked(lpni);
3201 lnet_net_unlock(LNET_LOCK_EX);
3205 lnet_clean_resendqs(void)
3207 struct lnet_msg *msg, *tmp;
3208 struct list_head msgs;
3211 INIT_LIST_HEAD(&msgs);
3213 cfs_cpt_for_each(i, lnet_cpt_table()) {
3215 list_splice_init(the_lnet.ln_mt_resendqs[i], &msgs);
3217 list_for_each_entry_safe(msg, tmp, &msgs, msg_list) {
3218 list_del_init(&msg->msg_list);
3219 msg->msg_no_resend = true;
3220 lnet_finalize(msg, -ESHUTDOWN);
3224 cfs_percpt_free(the_lnet.ln_mt_resendqs);
3228 lnet_recover_peer_nis(void)
3230 struct lnet_mt_event_info *ev_info;
3231 struct list_head processed_list;
3232 struct list_head local_queue;
3233 struct lnet_handle_md mdh;
3234 struct lnet_peer_ni *lpni;
3235 struct lnet_peer_ni *tmp;
3240 INIT_LIST_HEAD(&local_queue);
3241 INIT_LIST_HEAD(&processed_list);
3244 * Always use cpt 0 for locking across all interactions with
3245 * ln_mt_peerNIRecovq
3248 list_splice_init(&the_lnet.ln_mt_peerNIRecovq,
3252 list_for_each_entry_safe(lpni, tmp, &local_queue,
3255 * The same protection strategy is used here as is in the
3256 * local recovery case.
3259 healthv = atomic_read(&lpni->lpni_healthv);
3260 spin_lock(&lpni->lpni_lock);
3261 if (lpni->lpni_state & LNET_PEER_NI_DELETING ||
3262 healthv == LNET_MAX_HEALTH_VALUE) {
3263 list_del_init(&lpni->lpni_recovery);
3264 lnet_unlink_lpni_recovery_mdh_locked(lpni, 0, false);
3265 spin_unlock(&lpni->lpni_lock);
3266 lnet_peer_ni_decref_locked(lpni);
3272 * If the peer NI has failed recovery we must unlink the
3273 * md. But we want to keep the peer ni on the recovery
3274 * queue so we can try to continue recovering it
3276 if (lpni->lpni_state & LNET_PEER_NI_RECOVERY_FAILED) {
3277 lnet_unlink_lpni_recovery_mdh_locked(lpni, 0, true);
3278 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_FAILED;
3281 spin_unlock(&lpni->lpni_lock);
3285 * NOTE: we're racing with peer deletion from user space.
3286 * It's possible that a peer is deleted after we check its
3287 * state. In this case the recovery can create a new peer
3289 spin_lock(&lpni->lpni_lock);
3290 if (!(lpni->lpni_state & LNET_PEER_NI_RECOVERY_PENDING) &&
3291 !(lpni->lpni_state & LNET_PEER_NI_DELETING)) {
3292 lpni->lpni_state |= LNET_PEER_NI_RECOVERY_PENDING;
3293 spin_unlock(&lpni->lpni_lock);
3295 LIBCFS_ALLOC(ev_info, sizeof(*ev_info));
3297 CERROR("out of memory. Can't recover %s\n",
3298 libcfs_nid2str(lpni->lpni_nid));
3299 spin_lock(&lpni->lpni_lock);
3300 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3301 spin_unlock(&lpni->lpni_lock);
3305 /* look at the comments in lnet_recover_local_nis() */
3306 mdh = lpni->lpni_recovery_ping_mdh;
3307 LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
3308 nid = lpni->lpni_nid;
3310 list_del_init(&lpni->lpni_recovery);
3311 lnet_peer_ni_decref_locked(lpni);
3314 ev_info->mt_type = MT_TYPE_PEER_NI;
3315 ev_info->mt_nid = nid;
3316 rc = lnet_send_ping(nid, &mdh, LNET_INTERFACES_MIN,
3317 ev_info, the_lnet.ln_mt_eqh, true);
3320 * lnet_find_peer_ni_locked() grabs a refcount for
3321 * us. No need to take it explicitly.
3323 lpni = lnet_find_peer_ni_locked(nid);
3330 lpni->lpni_recovery_ping_mdh = mdh;
3332 * While we're unlocked the lpni could've been
3333 * readded on the recovery queue. In this case we
3334 * don't need to add it to the local queue, since
3335 * it's already on there and the thread that added
3336 * it would've incremented the refcount on the
3337 * peer, which means we need to decref the refcount
3338 * that was implicitly grabbed by find_peer_ni_locked.
3339 * Otherwise, if the lpni is still not on
3340 * the recovery queue, then we'll add it to the
3343 if (list_empty(&lpni->lpni_recovery))
3344 list_add_tail(&lpni->lpni_recovery, &processed_list);
3346 lnet_peer_ni_decref_locked(lpni);
3349 spin_lock(&lpni->lpni_lock);
3351 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3353 spin_unlock(&lpni->lpni_lock);
3356 list_splice_init(&processed_list, &local_queue);
3358 list_splice(&local_queue, &the_lnet.ln_mt_peerNIRecovq);
3363 lnet_monitor_thread(void *arg)
3365 time64_t recovery_timeout = 0;
3366 time64_t rsp_timeout = 0;
3371 * The monitor thread takes care of the following:
3372 * 1. Checks the aliveness of routers
3373 * 2. Checks if there are messages on the resend queue to resend
3375 * 3. Check if there are any NIs on the local recovery queue and
3377 * 4. Checks if there are any NIs on the remote recovery queue
3380 cfs_block_allsigs();
3382 while (the_lnet.ln_mt_state == LNET_MT_STATE_RUNNING) {
3383 now = ktime_get_real_seconds();
3385 if (lnet_router_checker_active())
3386 lnet_check_routers();
3388 lnet_resend_pending_msgs();
3390 if (now >= rsp_timeout) {
3391 lnet_finalize_expired_responses(false);
3392 rsp_timeout = now + (lnet_transaction_timeout / 2);
3395 if (now >= recovery_timeout) {
3396 lnet_recover_local_nis();
3397 lnet_recover_peer_nis();
3398 recovery_timeout = now + lnet_recovery_interval;
3402 * TODO do we need to check if we should sleep without
3403 * timeout? Technically, an active system will always
3404 * have messages in flight so this check will always
3405 * evaluate to false. And on an idle system do we care
3406 * if we wake up every 1 second? Although, we've seen
3407 * cases where we get a complaint that an idle thread
3408 * is waking up unnecessarily.
3410 interval = min(lnet_recovery_interval,
3411 lnet_transaction_timeout / 2);
3412 wait_event_interruptible_timeout(the_lnet.ln_mt_waitq,
3414 cfs_time_seconds(interval));
3418 lnet_net_lock(LNET_LOCK_EX);
3419 the_lnet.ln_mt_state = LNET_MT_STATE_SHUTDOWN;
3420 lnet_net_unlock(LNET_LOCK_EX);
3422 /* signal that the monitor thread is exiting */
3423 up(&the_lnet.ln_mt_signal);
3431 * Returns == 0 if success
3432 * Returns > 0 if LNetMDBind or prior fails
3433 * Returns < 0 if LNetGet fails
3436 lnet_send_ping(lnet_nid_t dest_nid,
3437 struct lnet_handle_md *mdh, int nnis,
3438 void *user_data, struct lnet_handle_eq eqh, bool recovery)
3440 struct lnet_md md = { NULL };
3441 struct lnet_process_id id;
3442 struct lnet_ping_buffer *pbuf;
3445 if (dest_nid == LNET_NID_ANY) {
3450 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
3456 /* initialize md content */
3457 md.start = &pbuf->pb_info;
3458 md.length = LNET_PING_INFO_SIZE(nnis);
3459 md.threshold = 2; /* GET/REPLY */
3461 md.options = LNET_MD_TRUNCATE;
3462 md.user_ptr = user_data;
3465 rc = LNetMDBind(md, LNET_UNLINK, mdh);
3467 lnet_ping_buffer_decref(pbuf);
3468 CERROR("Can't bind MD: %d\n", rc);
3469 rc = -rc; /* change the rc to positive */
3472 id.pid = LNET_PID_LUSTRE;
3475 rc = LNetGet(LNET_NID_ANY, *mdh, id,
3476 LNET_RESERVED_PORTAL,
3477 LNET_PROTO_PING_MATCHBITS, 0, recovery);
3480 goto fail_unlink_md;
3486 LNetInvalidateMDHandle(mdh);
3492 lnet_handle_recovery_reply(struct lnet_mt_event_info *ev_info,
3493 int status, bool unlink_event)
3495 lnet_nid_t nid = ev_info->mt_nid;
3497 if (ev_info->mt_type == MT_TYPE_LOCAL_NI) {
3501 ni = lnet_nid2ni_locked(nid, 0);
3507 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_PENDING;
3509 ni->ni_recovery_state |= LNET_NI_RECOVERY_FAILED;
3514 CERROR("local NI (%s) recovery failed with %d\n",
3515 libcfs_nid2str(nid), status);
3519 * need to increment healthv for the ni here, because in
3520 * the lnet_finalize() path we don't have access to this
3521 * NI. And in order to get access to it, we'll need to
3522 * carry forward too much information.
3523 * In the peer case, it'll naturally be incremented
3526 lnet_inc_healthv(&ni->ni_healthv);
3528 struct lnet_peer_ni *lpni;
3531 cpt = lnet_net_lock_current();
3532 lpni = lnet_find_peer_ni_locked(nid);
3534 lnet_net_unlock(cpt);
3537 spin_lock(&lpni->lpni_lock);
3538 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3540 lpni->lpni_state |= LNET_PEER_NI_RECOVERY_FAILED;
3541 spin_unlock(&lpni->lpni_lock);
3542 lnet_peer_ni_decref_locked(lpni);
3543 lnet_net_unlock(cpt);
3546 CERROR("peer NI (%s) recovery failed with %d\n",
3547 libcfs_nid2str(nid), status);
3552 lnet_mt_event_handler(struct lnet_event *event)
3554 struct lnet_mt_event_info *ev_info = event->md.user_ptr;
3555 struct lnet_ping_buffer *pbuf;
3557 /* TODO: remove assert */
3558 LASSERT(event->type == LNET_EVENT_REPLY ||
3559 event->type == LNET_EVENT_SEND ||
3560 event->type == LNET_EVENT_UNLINK);
3562 CDEBUG(D_NET, "Received event: %d status: %d\n", event->type,
3565 switch (event->type) {
3566 case LNET_EVENT_UNLINK:
3567 CDEBUG(D_NET, "%s recovery ping unlinked\n",
3568 libcfs_nid2str(ev_info->mt_nid));
3569 case LNET_EVENT_REPLY:
3570 lnet_handle_recovery_reply(ev_info, event->status,
3571 event->type == LNET_EVENT_UNLINK);
3573 case LNET_EVENT_SEND:
3574 CDEBUG(D_NET, "%s recovery message sent %s:%d\n",
3575 libcfs_nid2str(ev_info->mt_nid),
3576 (event->status) ? "unsuccessfully" :
3577 "successfully", event->status);
3580 CERROR("Unexpected event: %d\n", event->type);
3583 if (event->unlinked) {
3584 LIBCFS_FREE(ev_info, sizeof(*ev_info));
3585 pbuf = LNET_PING_INFO_TO_BUFFER(event->md.start);
3586 lnet_ping_buffer_decref(pbuf);
3591 lnet_rsp_tracker_create(void)
3593 struct list_head **rstqs;
3594 rstqs = lnet_create_array_of_queues();
3599 the_lnet.ln_mt_rstq = rstqs;
3605 lnet_rsp_tracker_clean(void)
3607 lnet_finalize_expired_responses(true);
3609 cfs_percpt_free(the_lnet.ln_mt_rstq);
3610 the_lnet.ln_mt_rstq = NULL;
3613 int lnet_monitor_thr_start(void)
3616 struct task_struct *task;
3618 if (the_lnet.ln_mt_state != LNET_MT_STATE_SHUTDOWN)
3621 rc = lnet_resendqs_create();
3625 rc = lnet_rsp_tracker_create();
3629 sema_init(&the_lnet.ln_mt_signal, 0);
3631 lnet_net_lock(LNET_LOCK_EX);
3632 the_lnet.ln_mt_state = LNET_MT_STATE_RUNNING;
3633 lnet_net_unlock(LNET_LOCK_EX);
3634 task = kthread_run(lnet_monitor_thread, NULL, "monitor_thread");
3637 CERROR("Can't start monitor thread: %d\n", rc);
3644 lnet_net_lock(LNET_LOCK_EX);
3645 the_lnet.ln_mt_state = LNET_MT_STATE_STOPPING;
3646 lnet_net_unlock(LNET_LOCK_EX);
3647 /* block until event callback signals exit */
3648 down(&the_lnet.ln_mt_signal);
3650 lnet_net_lock(LNET_LOCK_EX);
3651 the_lnet.ln_mt_state = LNET_MT_STATE_SHUTDOWN;
3652 lnet_net_unlock(LNET_LOCK_EX);
3653 lnet_rsp_tracker_clean();
3654 lnet_clean_local_ni_recoveryq();
3655 lnet_clean_peer_ni_recoveryq();
3656 lnet_clean_resendqs();
3657 LNetInvalidateEQHandle(&the_lnet.ln_mt_eqh);
3660 lnet_rsp_tracker_clean();
3661 lnet_clean_local_ni_recoveryq();
3662 lnet_clean_peer_ni_recoveryq();
3663 lnet_clean_resendqs();
3667 void lnet_monitor_thr_stop(void)
3669 if (the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN)
3672 LASSERT(the_lnet.ln_mt_state == LNET_MT_STATE_RUNNING);
3673 lnet_net_lock(LNET_LOCK_EX);
3674 the_lnet.ln_mt_state = LNET_MT_STATE_STOPPING;
3675 lnet_net_unlock(LNET_LOCK_EX);
3677 /* tell the monitor thread that we're shutting down */
3678 wake_up(&the_lnet.ln_mt_waitq);
3680 /* block until monitor thread signals that it's done */
3681 down(&the_lnet.ln_mt_signal);
3682 LASSERT(the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN);
3684 /* perform cleanup tasks */
3685 lnet_rsp_tracker_clean();
3686 lnet_clean_local_ni_recoveryq();
3687 lnet_clean_peer_ni_recoveryq();
3688 lnet_clean_resendqs();
3694 lnet_drop_message(struct lnet_ni *ni, int cpt, void *private, unsigned int nob,
3698 lnet_incr_stats(&ni->ni_stats, msg_type, LNET_STATS_TYPE_DROP);
3699 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
3700 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length += nob;
3701 lnet_net_unlock(cpt);
3703 lnet_ni_recv(ni, private, NULL, 0, 0, 0, nob);
3707 lnet_recv_put(struct lnet_ni *ni, struct lnet_msg *msg)
3709 struct lnet_hdr *hdr = &msg->msg_hdr;
3711 if (msg->msg_wanted != 0)
3712 lnet_setpayloadbuffer(msg);
3714 lnet_build_msg_event(msg, LNET_EVENT_PUT);
3716 /* Must I ACK? If so I'll grab the ack_wmd out of the header and put
3717 * it back into the ACK during lnet_finalize() */
3718 msg->msg_ack = (!lnet_is_wire_handle_none(&hdr->msg.put.ack_wmd) &&
3719 (msg->msg_md->md_options & LNET_MD_ACK_DISABLE) == 0);
3721 lnet_ni_recv(ni, msg->msg_private, msg, msg->msg_rx_delayed,
3722 msg->msg_offset, msg->msg_wanted, hdr->payload_length);
3726 lnet_parse_put(struct lnet_ni *ni, struct lnet_msg *msg)
3728 struct lnet_hdr *hdr = &msg->msg_hdr;
3729 struct lnet_match_info info;
3733 /* Convert put fields to host byte order */
3734 hdr->msg.put.match_bits = le64_to_cpu(hdr->msg.put.match_bits);
3735 hdr->msg.put.ptl_index = le32_to_cpu(hdr->msg.put.ptl_index);
3736 hdr->msg.put.offset = le32_to_cpu(hdr->msg.put.offset);
3738 /* Primary peer NID. */
3739 info.mi_id.nid = msg->msg_initiator;
3740 info.mi_id.pid = hdr->src_pid;
3741 info.mi_opc = LNET_MD_OP_PUT;
3742 info.mi_portal = hdr->msg.put.ptl_index;
3743 info.mi_rlength = hdr->payload_length;
3744 info.mi_roffset = hdr->msg.put.offset;
3745 info.mi_mbits = hdr->msg.put.match_bits;
3746 info.mi_cpt = lnet_cpt_of_nid(msg->msg_initiator, ni);
3748 msg->msg_rx_ready_delay = ni->ni_net->net_lnd->lnd_eager_recv == NULL;
3749 ready_delay = msg->msg_rx_ready_delay;
3752 rc = lnet_ptl_match_md(&info, msg);
3757 case LNET_MATCHMD_OK:
3758 lnet_recv_put(ni, msg);
3761 case LNET_MATCHMD_NONE:
3763 /* no eager_recv or has already called it, should
3764 * have been attached on delayed list */
3767 rc = lnet_ni_eager_recv(ni, msg);
3774 case LNET_MATCHMD_DROP:
3775 CNETERR("Dropping PUT from %s portal %d match %llu"
3776 " offset %d length %d: %d\n",
3777 libcfs_id2str(info.mi_id), info.mi_portal,
3778 info.mi_mbits, info.mi_roffset, info.mi_rlength, rc);
3780 return -ENOENT; /* -ve: OK but no match */
3785 lnet_parse_get(struct lnet_ni *ni, struct lnet_msg *msg, int rdma_get)
3787 struct lnet_match_info info;
3788 struct lnet_hdr *hdr = &msg->msg_hdr;
3789 struct lnet_process_id source_id;
3790 struct lnet_handle_wire reply_wmd;
3793 /* Convert get fields to host byte order */
3794 hdr->msg.get.match_bits = le64_to_cpu(hdr->msg.get.match_bits);
3795 hdr->msg.get.ptl_index = le32_to_cpu(hdr->msg.get.ptl_index);
3796 hdr->msg.get.sink_length = le32_to_cpu(hdr->msg.get.sink_length);
3797 hdr->msg.get.src_offset = le32_to_cpu(hdr->msg.get.src_offset);
3799 source_id.nid = hdr->src_nid;
3800 source_id.pid = hdr->src_pid;
3801 /* Primary peer NID */
3802 info.mi_id.nid = msg->msg_initiator;
3803 info.mi_id.pid = hdr->src_pid;
3804 info.mi_opc = LNET_MD_OP_GET;
3805 info.mi_portal = hdr->msg.get.ptl_index;
3806 info.mi_rlength = hdr->msg.get.sink_length;
3807 info.mi_roffset = hdr->msg.get.src_offset;
3808 info.mi_mbits = hdr->msg.get.match_bits;
3809 info.mi_cpt = lnet_cpt_of_nid(msg->msg_initiator, ni);
3811 rc = lnet_ptl_match_md(&info, msg);
3812 if (rc == LNET_MATCHMD_DROP) {
3813 CNETERR("Dropping GET from %s portal %d match %llu"
3814 " offset %d length %d\n",
3815 libcfs_id2str(info.mi_id), info.mi_portal,
3816 info.mi_mbits, info.mi_roffset, info.mi_rlength);
3817 return -ENOENT; /* -ve: OK but no match */
3820 LASSERT(rc == LNET_MATCHMD_OK);
3822 lnet_build_msg_event(msg, LNET_EVENT_GET);
3824 reply_wmd = hdr->msg.get.return_wmd;
3826 lnet_prep_send(msg, LNET_MSG_REPLY, source_id,
3827 msg->msg_offset, msg->msg_wanted);
3829 msg->msg_hdr.msg.reply.dst_wmd = reply_wmd;
3832 /* The LND completes the REPLY from her recv procedure */
3833 lnet_ni_recv(ni, msg->msg_private, msg, 0,
3834 msg->msg_offset, msg->msg_len, msg->msg_len);
3838 lnet_ni_recv(ni, msg->msg_private, NULL, 0, 0, 0, 0);
3839 msg->msg_receiving = 0;
3841 rc = lnet_send(ni->ni_nid, msg, msg->msg_from);
3843 /* didn't get as far as lnet_ni_send() */
3844 CERROR("%s: Unable to send REPLY for GET from %s: %d\n",
3845 libcfs_nid2str(ni->ni_nid),
3846 libcfs_id2str(info.mi_id), rc);
3848 lnet_finalize(msg, rc);
3855 lnet_parse_reply(struct lnet_ni *ni, struct lnet_msg *msg)
3857 void *private = msg->msg_private;
3858 struct lnet_hdr *hdr = &msg->msg_hdr;
3859 struct lnet_process_id src = {0};
3860 struct lnet_libmd *md;
3865 cpt = lnet_cpt_of_cookie(hdr->msg.reply.dst_wmd.wh_object_cookie);
3868 src.nid = hdr->src_nid;
3869 src.pid = hdr->src_pid;
3871 /* NB handles only looked up by creator (no flips) */
3872 md = lnet_wire_handle2md(&hdr->msg.reply.dst_wmd);
3873 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
3874 CNETERR("%s: Dropping REPLY from %s for %s "
3876 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
3877 (md == NULL) ? "invalid" : "inactive",
3878 hdr->msg.reply.dst_wmd.wh_interface_cookie,
3879 hdr->msg.reply.dst_wmd.wh_object_cookie);
3880 if (md != NULL && md->md_me != NULL)
3881 CERROR("REPLY MD also attached to portal %d\n",
3882 md->md_me->me_portal);
3884 lnet_res_unlock(cpt);
3885 return -ENOENT; /* -ve: OK but no match */
3888 LASSERT(md->md_offset == 0);
3890 rlength = hdr->payload_length;
3891 mlength = MIN(rlength, (int)md->md_length);
3893 if (mlength < rlength &&
3894 (md->md_options & LNET_MD_TRUNCATE) == 0) {
3895 CNETERR("%s: Dropping REPLY from %s length %d "
3896 "for MD %#llx would overflow (%d)\n",
3897 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
3898 rlength, hdr->msg.reply.dst_wmd.wh_object_cookie,
3900 lnet_res_unlock(cpt);
3901 return -ENOENT; /* -ve: OK but no match */
3904 CDEBUG(D_NET, "%s: Reply from %s of length %d/%d into md %#llx\n",
3905 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
3906 mlength, rlength, hdr->msg.reply.dst_wmd.wh_object_cookie);
3908 lnet_msg_attach_md(msg, md, 0, mlength);
3911 lnet_setpayloadbuffer(msg);
3913 lnet_res_unlock(cpt);
3915 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
3917 lnet_ni_recv(ni, private, msg, 0, 0, mlength, rlength);
3922 lnet_parse_ack(struct lnet_ni *ni, struct lnet_msg *msg)
3924 struct lnet_hdr *hdr = &msg->msg_hdr;
3925 struct lnet_process_id src = {0};
3926 struct lnet_libmd *md;
3929 src.nid = hdr->src_nid;
3930 src.pid = hdr->src_pid;
3932 /* Convert ack fields to host byte order */
3933 hdr->msg.ack.match_bits = le64_to_cpu(hdr->msg.ack.match_bits);
3934 hdr->msg.ack.mlength = le32_to_cpu(hdr->msg.ack.mlength);
3936 cpt = lnet_cpt_of_cookie(hdr->msg.ack.dst_wmd.wh_object_cookie);
3939 /* NB handles only looked up by creator (no flips) */
3940 md = lnet_wire_handle2md(&hdr->msg.ack.dst_wmd);
3941 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
3942 /* Don't moan; this is expected */
3944 "%s: Dropping ACK from %s to %s MD %#llx.%#llx\n",
3945 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
3946 (md == NULL) ? "invalid" : "inactive",
3947 hdr->msg.ack.dst_wmd.wh_interface_cookie,
3948 hdr->msg.ack.dst_wmd.wh_object_cookie);
3949 if (md != NULL && md->md_me != NULL)
3950 CERROR("Source MD also attached to portal %d\n",
3951 md->md_me->me_portal);
3953 lnet_res_unlock(cpt);
3954 return -ENOENT; /* -ve! */
3957 CDEBUG(D_NET, "%s: ACK from %s into md %#llx\n",
3958 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
3959 hdr->msg.ack.dst_wmd.wh_object_cookie);
3961 lnet_msg_attach_md(msg, md, 0, 0);
3963 lnet_res_unlock(cpt);
3965 lnet_build_msg_event(msg, LNET_EVENT_ACK);
3967 lnet_ni_recv(ni, msg->msg_private, msg, 0, 0, 0, msg->msg_len);
3972 * \retval LNET_CREDIT_OK If \a msg is forwarded
3973 * \retval LNET_CREDIT_WAIT If \a msg is blocked because w/o buffer
3974 * \retval -ve error code
3977 lnet_parse_forward_locked(struct lnet_ni *ni, struct lnet_msg *msg)
3981 if (!the_lnet.ln_routing)
3984 if (msg->msg_rxpeer->lpni_rtrcredits <= 0 ||
3985 lnet_msg2bufpool(msg)->rbp_credits <= 0) {
3986 if (ni->ni_net->net_lnd->lnd_eager_recv == NULL) {
3987 msg->msg_rx_ready_delay = 1;
3989 lnet_net_unlock(msg->msg_rx_cpt);
3990 rc = lnet_ni_eager_recv(ni, msg);
3991 lnet_net_lock(msg->msg_rx_cpt);
3996 rc = lnet_post_routed_recv_locked(msg, 0);
4001 lnet_parse_local(struct lnet_ni *ni, struct lnet_msg *msg)
4005 switch (msg->msg_type) {
4007 rc = lnet_parse_ack(ni, msg);
4010 rc = lnet_parse_put(ni, msg);
4013 rc = lnet_parse_get(ni, msg, msg->msg_rdma_get);
4015 case LNET_MSG_REPLY:
4016 rc = lnet_parse_reply(ni, msg);
4018 default: /* prevent an unused label if !kernel */
4023 LASSERT(rc == 0 || rc == -ENOENT);
4028 lnet_msgtyp2str (int type)
4037 case LNET_MSG_REPLY:
4039 case LNET_MSG_HELLO:
4042 return ("<UNKNOWN>");
4047 lnet_print_hdr(struct lnet_hdr *hdr)
4049 struct lnet_process_id src = {
4050 .nid = hdr->src_nid,
4051 .pid = hdr->src_pid,
4053 struct lnet_process_id dst = {
4054 .nid = hdr->dest_nid,
4055 .pid = hdr->dest_pid,
4057 char *type_str = lnet_msgtyp2str(hdr->type);
4059 CWARN("P3 Header at %p of type %s\n", hdr, type_str);
4060 CWARN(" From %s\n", libcfs_id2str(src));
4061 CWARN(" To %s\n", libcfs_id2str(dst));
4063 switch (hdr->type) {
4068 CWARN(" Ptl index %d, ack md %#llx.%#llx, "
4069 "match bits %llu\n",
4070 hdr->msg.put.ptl_index,
4071 hdr->msg.put.ack_wmd.wh_interface_cookie,
4072 hdr->msg.put.ack_wmd.wh_object_cookie,
4073 hdr->msg.put.match_bits);
4074 CWARN(" Length %d, offset %d, hdr data %#llx\n",
4075 hdr->payload_length, hdr->msg.put.offset,
4076 hdr->msg.put.hdr_data);
4080 CWARN(" Ptl index %d, return md %#llx.%#llx, "
4081 "match bits %llu\n", hdr->msg.get.ptl_index,
4082 hdr->msg.get.return_wmd.wh_interface_cookie,
4083 hdr->msg.get.return_wmd.wh_object_cookie,
4084 hdr->msg.get.match_bits);
4085 CWARN(" Length %d, src offset %d\n",
4086 hdr->msg.get.sink_length,
4087 hdr->msg.get.src_offset);
4091 CWARN(" dst md %#llx.%#llx, "
4092 "manipulated length %d\n",
4093 hdr->msg.ack.dst_wmd.wh_interface_cookie,
4094 hdr->msg.ack.dst_wmd.wh_object_cookie,
4095 hdr->msg.ack.mlength);
4098 case LNET_MSG_REPLY:
4099 CWARN(" dst md %#llx.%#llx, "
4101 hdr->msg.reply.dst_wmd.wh_interface_cookie,
4102 hdr->msg.reply.dst_wmd.wh_object_cookie,
4103 hdr->payload_length);
4109 lnet_parse(struct lnet_ni *ni, struct lnet_hdr *hdr, lnet_nid_t from_nid,
4110 void *private, int rdma_req)
4115 struct lnet_msg *msg;
4116 lnet_pid_t dest_pid;
4117 lnet_nid_t dest_nid;
4119 struct lnet_peer_ni *lpni;
4120 __u32 payload_length;
4123 LASSERT (!in_interrupt ());
4125 type = le32_to_cpu(hdr->type);
4126 src_nid = le64_to_cpu(hdr->src_nid);
4127 dest_nid = le64_to_cpu(hdr->dest_nid);
4128 dest_pid = le32_to_cpu(hdr->dest_pid);
4129 payload_length = le32_to_cpu(hdr->payload_length);
4131 for_me = (ni->ni_nid == dest_nid);
4132 cpt = lnet_cpt_of_nid(from_nid, ni);
4134 CDEBUG(D_NET, "TRACE: %s(%s) <- %s : %s - %s\n",
4135 libcfs_nid2str(dest_nid),
4136 libcfs_nid2str(ni->ni_nid),
4137 libcfs_nid2str(src_nid),
4138 lnet_msgtyp2str(type),
4139 (for_me) ? "for me" : "routed");
4144 if (payload_length > 0) {
4145 CERROR("%s, src %s: bad %s payload %d (0 expected)\n",
4146 libcfs_nid2str(from_nid),
4147 libcfs_nid2str(src_nid),
4148 lnet_msgtyp2str(type), payload_length);
4154 case LNET_MSG_REPLY:
4155 if (payload_length >
4156 (__u32)(for_me ? LNET_MAX_PAYLOAD : LNET_MTU)) {
4157 CERROR("%s, src %s: bad %s payload %d "
4158 "(%d max expected)\n",
4159 libcfs_nid2str(from_nid),
4160 libcfs_nid2str(src_nid),
4161 lnet_msgtyp2str(type),
4163 for_me ? LNET_MAX_PAYLOAD : LNET_MTU);
4169 CERROR("%s, src %s: Bad message type 0x%x\n",
4170 libcfs_nid2str(from_nid),
4171 libcfs_nid2str(src_nid), type);
4175 if (the_lnet.ln_routing &&
4176 ni->ni_last_alive != ktime_get_real_seconds()) {
4177 /* NB: so far here is the only place to set NI status to "up */
4179 ni->ni_last_alive = ktime_get_real_seconds();
4180 if (ni->ni_status != NULL &&
4181 ni->ni_status->ns_status == LNET_NI_STATUS_DOWN)
4182 ni->ni_status->ns_status = LNET_NI_STATUS_UP;
4186 /* Regard a bad destination NID as a protocol error. Senders should
4187 * know what they're doing; if they don't they're misconfigured, buggy
4188 * or malicious so we chop them off at the knees :) */
4191 if (LNET_NIDNET(dest_nid) == LNET_NIDNET(ni->ni_nid)) {
4192 /* should have gone direct */
4193 CERROR("%s, src %s: Bad dest nid %s "
4194 "(should have been sent direct)\n",
4195 libcfs_nid2str(from_nid),
4196 libcfs_nid2str(src_nid),
4197 libcfs_nid2str(dest_nid));
4201 if (lnet_islocalnid(dest_nid)) {
4202 /* dest is another local NI; sender should have used
4203 * this node's NID on its own network */
4204 CERROR("%s, src %s: Bad dest nid %s "
4205 "(it's my nid but on a different network)\n",
4206 libcfs_nid2str(from_nid),
4207 libcfs_nid2str(src_nid),
4208 libcfs_nid2str(dest_nid));
4212 if (rdma_req && type == LNET_MSG_GET) {
4213 CERROR("%s, src %s: Bad optimized GET for %s "
4214 "(final destination must be me)\n",
4215 libcfs_nid2str(from_nid),
4216 libcfs_nid2str(src_nid),
4217 libcfs_nid2str(dest_nid));
4221 if (!the_lnet.ln_routing) {
4222 CERROR("%s, src %s: Dropping message for %s "
4223 "(routing not enabled)\n",
4224 libcfs_nid2str(from_nid),
4225 libcfs_nid2str(src_nid),
4226 libcfs_nid2str(dest_nid));
4231 /* Message looks OK; we're not going to return an error, so we MUST
4232 * call back lnd_recv() come what may... */
4234 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
4235 fail_peer(src_nid, 0)) { /* shall we now? */
4236 CERROR("%s, src %s: Dropping %s to simulate failure\n",
4237 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4238 lnet_msgtyp2str(type));
4242 if (!list_empty(&the_lnet.ln_drop_rules) &&
4243 lnet_drop_rule_match(hdr, NULL)) {
4244 CDEBUG(D_NET, "%s, src %s, dst %s: Dropping %s to simulate"
4245 "silent message loss\n",
4246 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4247 libcfs_nid2str(dest_nid), lnet_msgtyp2str(type));
4251 if (lnet_drop_asym_route && for_me &&
4252 LNET_NIDNET(src_nid) != LNET_NIDNET(from_nid)) {
4253 struct lnet_net *net;
4254 struct lnet_remotenet *rnet;
4257 /* we are dealing with a routed message,
4258 * so see if route to reach src_nid goes through from_nid
4261 net = lnet_get_net_locked(LNET_NIDNET(ni->ni_nid));
4263 lnet_net_unlock(cpt);
4264 CERROR("net %s not found\n",
4265 libcfs_net2str(LNET_NIDNET(ni->ni_nid)));
4269 rnet = lnet_find_rnet_locked(LNET_NIDNET(src_nid));
4271 struct lnet_peer *gw = NULL;
4272 struct lnet_peer_ni *lpni = NULL;
4273 struct lnet_route *route;
4275 list_for_each_entry(route, &rnet->lrn_routes, lr_list) {
4277 gw = route->lr_gateway;
4278 if (route->lr_lnet != net->net_id)
4281 * if the nid is one of the gateway's NIDs
4282 * then this is a valid gateway
4284 while ((lpni = lnet_get_next_peer_ni_locked(gw,
4285 NULL, lpni)) != NULL) {
4286 if (lpni->lpni_nid == from_nid) {
4293 lnet_net_unlock(cpt);
4295 /* we would not use from_nid to route a message to
4297 * => asymmetric routing detected but forbidden
4299 CERROR("%s, src %s: Dropping asymmetrical route %s\n",
4300 libcfs_nid2str(from_nid),
4301 libcfs_nid2str(src_nid), lnet_msgtyp2str(type));
4306 msg = lnet_msg_alloc();
4308 CERROR("%s, src %s: Dropping %s (out of memory)\n",
4309 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4310 lnet_msgtyp2str(type));
4314 /* msg zeroed in lnet_msg_alloc; i.e. flags all clear,
4315 * pointers NULL etc */
4317 msg->msg_type = type;
4318 msg->msg_private = private;
4319 msg->msg_receiving = 1;
4320 msg->msg_rdma_get = rdma_req;
4321 msg->msg_len = msg->msg_wanted = payload_length;
4322 msg->msg_offset = 0;
4323 msg->msg_hdr = *hdr;
4324 /* for building message event */
4325 msg->msg_from = from_nid;
4327 msg->msg_target.pid = dest_pid;
4328 msg->msg_target.nid = dest_nid;
4329 msg->msg_routing = 1;
4332 /* convert common msg->hdr fields to host byteorder */
4333 msg->msg_hdr.type = type;
4334 msg->msg_hdr.src_nid = src_nid;
4335 msg->msg_hdr.src_pid = le32_to_cpu(msg->msg_hdr.src_pid);
4336 msg->msg_hdr.dest_nid = dest_nid;
4337 msg->msg_hdr.dest_pid = dest_pid;
4338 msg->msg_hdr.payload_length = payload_length;
4342 lpni = lnet_nid2peerni_locked(from_nid, ni->ni_nid, cpt);
4344 lnet_net_unlock(cpt);
4345 CERROR("%s, src %s: Dropping %s "
4346 "(error %ld looking up sender)\n",
4347 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4348 lnet_msgtyp2str(type), PTR_ERR(lpni));
4350 if (rc == -ESHUTDOWN)
4351 /* We are shutting down. Don't do anything more */
4355 msg->msg_rxpeer = lpni;
4357 lnet_ni_addref_locked(ni, cpt);
4358 /* Multi-Rail: Primary NID of source. */
4359 msg->msg_initiator = lnet_peer_primary_nid_locked(src_nid);
4362 * mark the status of this lpni as UP since we received a message
4363 * from it. The ping response reports back the ns_status which is
4364 * marked on the remote as up or down and we cache it here.
4366 msg->msg_rxpeer->lpni_ns_status = LNET_NI_STATUS_UP;
4368 lnet_msg_commit(msg, cpt);
4370 /* message delay simulation */
4371 if (unlikely(!list_empty(&the_lnet.ln_delay_rules) &&
4372 lnet_delay_rule_match_locked(hdr, msg))) {
4373 lnet_net_unlock(cpt);
4378 rc = lnet_parse_forward_locked(ni, msg);
4379 lnet_net_unlock(cpt);
4384 if (rc == LNET_CREDIT_OK) {
4385 lnet_ni_recv(ni, msg->msg_private, msg, 0,
4386 0, payload_length, payload_length);
4391 lnet_net_unlock(cpt);
4393 rc = lnet_parse_local(ni, msg);
4399 LASSERT(msg->msg_md == NULL);
4400 lnet_finalize(msg, rc);
4403 lnet_drop_message(ni, cpt, private, payload_length, type);
4406 EXPORT_SYMBOL(lnet_parse);
4409 lnet_drop_delayed_msg_list(struct list_head *head, char *reason)
4411 while (!list_empty(head)) {
4412 struct lnet_process_id id = {0};
4413 struct lnet_msg *msg;
4415 msg = list_entry(head->next, struct lnet_msg, msg_list);
4416 list_del(&msg->msg_list);
4418 id.nid = msg->msg_hdr.src_nid;
4419 id.pid = msg->msg_hdr.src_pid;
4421 LASSERT(msg->msg_md == NULL);
4422 LASSERT(msg->msg_rx_delayed);
4423 LASSERT(msg->msg_rxpeer != NULL);
4424 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
4426 CWARN("Dropping delayed PUT from %s portal %d match %llu"
4427 " offset %d length %d: %s\n",
4429 msg->msg_hdr.msg.put.ptl_index,
4430 msg->msg_hdr.msg.put.match_bits,
4431 msg->msg_hdr.msg.put.offset,
4432 msg->msg_hdr.payload_length, reason);
4434 /* NB I can't drop msg's ref on msg_rxpeer until after I've
4435 * called lnet_drop_message(), so I just hang onto msg as well
4436 * until that's done */
4438 lnet_drop_message(msg->msg_rxni, msg->msg_rx_cpt,
4439 msg->msg_private, msg->msg_len,
4442 msg->msg_no_resend = true;
4444 * NB: message will not generate event because w/o attached MD,
4445 * but we still should give error code so lnet_msg_decommit()
4446 * can skip counters operations and other checks.
4448 lnet_finalize(msg, -ENOENT);
4453 lnet_recv_delayed_msg_list(struct list_head *head)
4455 while (!list_empty(head)) {
4456 struct lnet_msg *msg;
4457 struct lnet_process_id id;
4459 msg = list_entry(head->next, struct lnet_msg, msg_list);
4460 list_del(&msg->msg_list);
4462 /* md won't disappear under me, since each msg
4463 * holds a ref on it */
4465 id.nid = msg->msg_hdr.src_nid;
4466 id.pid = msg->msg_hdr.src_pid;
4468 LASSERT(msg->msg_rx_delayed);
4469 LASSERT(msg->msg_md != NULL);
4470 LASSERT(msg->msg_rxpeer != NULL);
4471 LASSERT(msg->msg_rxni != NULL);
4472 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
4474 CDEBUG(D_NET, "Resuming delayed PUT from %s portal %d "
4475 "match %llu offset %d length %d.\n",
4476 libcfs_id2str(id), msg->msg_hdr.msg.put.ptl_index,
4477 msg->msg_hdr.msg.put.match_bits,
4478 msg->msg_hdr.msg.put.offset,
4479 msg->msg_hdr.payload_length);
4481 lnet_recv_put(msg->msg_rxni, msg);
4486 lnet_attach_rsp_tracker(struct lnet_rsp_tracker *rspt, int cpt,
4487 struct lnet_libmd *md, struct lnet_handle_md mdh)
4490 bool new_entry = true;
4491 struct lnet_rsp_tracker *local_rspt;
4494 * MD has a refcount taken by message so it's not going away.
4495 * The MD however can be looked up. We need to secure the access
4496 * to the md_rspt_ptr by taking the res_lock.
4497 * The rspt can be accessed without protection up to when it gets
4498 * added to the list.
4502 local_rspt = md->md_rspt_ptr;
4503 timeout_ns = lnet_transaction_timeout * NSEC_PER_SEC;
4504 if (local_rspt != NULL) {
4506 * we already have an rspt attached to the md, so we'll
4507 * update the deadline on that one.
4509 LIBCFS_FREE(rspt, sizeof(*rspt));
4513 rspt->rspt_mdh = mdh;
4514 rspt->rspt_cpt = cpt;
4515 /* store the rspt so we can access it when we get the REPLY */
4516 md->md_rspt_ptr = rspt;
4519 local_rspt->rspt_deadline = ktime_add_ns(ktime_get(), timeout_ns);
4522 * add to the list of tracked responses. It's added to tail of the
4523 * list in order to expire all the older entries first.
4526 if (!new_entry && !list_empty(&local_rspt->rspt_on_list))
4527 list_del_init(&local_rspt->rspt_on_list);
4528 list_add_tail(&local_rspt->rspt_on_list, the_lnet.ln_mt_rstq[cpt]);
4529 lnet_net_unlock(cpt);
4530 lnet_res_unlock(cpt);
4534 * Initiate an asynchronous PUT operation.
4536 * There are several events associated with a PUT: completion of the send on
4537 * the initiator node (LNET_EVENT_SEND), and when the send completes
4538 * successfully, the receipt of an acknowledgment (LNET_EVENT_ACK) indicating
4539 * that the operation was accepted by the target. The event LNET_EVENT_PUT is
4540 * used at the target node to indicate the completion of incoming data
4543 * The local events will be logged in the EQ associated with the MD pointed to
4544 * by \a mdh handle. Using a MD without an associated EQ results in these
4545 * events being discarded. In this case, the caller must have another
4546 * mechanism (e.g., a higher level protocol) for determining when it is safe
4547 * to modify the memory region associated with the MD.
4549 * Note that LNet does not guarantee the order of LNET_EVENT_SEND and
4550 * LNET_EVENT_ACK, though intuitively ACK should happen after SEND.
4552 * \param self Indicates the NID of a local interface through which to send
4553 * the PUT request. Use LNET_NID_ANY to let LNet choose one by itself.
4554 * \param mdh A handle for the MD that describes the memory to be sent. The MD
4555 * must be "free floating" (See LNetMDBind()).
4556 * \param ack Controls whether an acknowledgment is requested.
4557 * Acknowledgments are only sent when they are requested by the initiating
4558 * process and the target MD enables them.
4559 * \param target A process identifier for the target process.
4560 * \param portal The index in the \a target's portal table.
4561 * \param match_bits The match bits to use for MD selection at the target
4563 * \param offset The offset into the target MD (only used when the target
4564 * MD has the LNET_MD_MANAGE_REMOTE option set).
4565 * \param hdr_data 64 bits of user data that can be included in the message
4566 * header. This data is written to an event queue entry at the target if an
4567 * EQ is present on the matching MD.
4569 * \retval 0 Success, and only in this case events will be generated
4570 * and logged to EQ (if it exists).
4571 * \retval -EIO Simulated failure.
4572 * \retval -ENOMEM Memory allocation failure.
4573 * \retval -ENOENT Invalid MD object.
4575 * \see struct lnet_event::hdr_data and lnet_event_kind_t.
4578 LNetPut(lnet_nid_t self, struct lnet_handle_md mdh, enum lnet_ack_req ack,
4579 struct lnet_process_id target, unsigned int portal,
4580 __u64 match_bits, unsigned int offset,
4583 struct lnet_msg *msg;
4584 struct lnet_libmd *md;
4587 struct lnet_rsp_tracker *rspt = NULL;
4589 LASSERT(the_lnet.ln_refcount > 0);
4591 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
4592 fail_peer(target.nid, 1)) { /* shall we now? */
4593 CERROR("Dropping PUT to %s: simulated failure\n",
4594 libcfs_id2str(target));
4598 msg = lnet_msg_alloc();
4600 CERROR("Dropping PUT to %s: ENOMEM on struct lnet_msg\n",
4601 libcfs_id2str(target));
4604 msg->msg_vmflush = !!memory_pressure_get();
4606 cpt = lnet_cpt_of_cookie(mdh.cookie);
4608 if (ack == LNET_ACK_REQ) {
4609 rspt = lnet_rspt_alloc(cpt);
4611 CERROR("Dropping PUT to %s: ENOMEM on response tracker\n",
4612 libcfs_id2str(target));
4615 INIT_LIST_HEAD(&rspt->rspt_on_list);
4620 md = lnet_handle2md(&mdh);
4621 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
4622 CERROR("Dropping PUT (%llu:%d:%s): MD (%d) invalid\n",
4623 match_bits, portal, libcfs_id2str(target),
4624 md == NULL ? -1 : md->md_threshold);
4625 if (md != NULL && md->md_me != NULL)
4626 CERROR("Source MD also attached to portal %d\n",
4627 md->md_me->me_portal);
4628 lnet_res_unlock(cpt);
4630 LIBCFS_FREE(rspt, sizeof(*rspt));
4635 CDEBUG(D_NET, "LNetPut -> %s\n", libcfs_id2str(target));
4637 lnet_msg_attach_md(msg, md, 0, 0);
4639 lnet_prep_send(msg, LNET_MSG_PUT, target, 0, md->md_length);
4641 msg->msg_hdr.msg.put.match_bits = cpu_to_le64(match_bits);
4642 msg->msg_hdr.msg.put.ptl_index = cpu_to_le32(portal);
4643 msg->msg_hdr.msg.put.offset = cpu_to_le32(offset);
4644 msg->msg_hdr.msg.put.hdr_data = hdr_data;
4646 /* NB handles only looked up by creator (no flips) */
4647 if (ack == LNET_ACK_REQ) {
4648 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
4649 the_lnet.ln_interface_cookie;
4650 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
4651 md->md_lh.lh_cookie;
4653 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
4654 LNET_WIRE_HANDLE_COOKIE_NONE;
4655 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
4656 LNET_WIRE_HANDLE_COOKIE_NONE;
4659 lnet_res_unlock(cpt);
4661 lnet_build_msg_event(msg, LNET_EVENT_SEND);
4663 if (ack == LNET_ACK_REQ)
4664 lnet_attach_rsp_tracker(rspt, cpt, md, mdh);
4666 if (CFS_FAIL_CHECK_ORSET(CFS_FAIL_PTLRPC_OST_BULK_CB2,
4670 rc = lnet_send(self, msg, LNET_NID_ANY);
4673 CNETERR("Error sending PUT to %s: %d\n",
4674 libcfs_id2str(target), rc);
4675 msg->msg_no_resend = true;
4676 lnet_finalize(msg, rc);
4679 /* completion will be signalled by an event */
4682 EXPORT_SYMBOL(LNetPut);
4685 * The LND can DMA direct to the GET md (i.e. no REPLY msg). This
4686 * returns a msg for the LND to pass to lnet_finalize() when the sink
4687 * data has been received.
4689 * CAVEAT EMPTOR: 'getmsg' is the original GET, which is freed when
4690 * lnet_finalize() is called on it, so the LND must call this first
4693 lnet_create_reply_msg(struct lnet_ni *ni, struct lnet_msg *getmsg)
4695 struct lnet_msg *msg = lnet_msg_alloc();
4696 struct lnet_libmd *getmd = getmsg->msg_md;
4697 struct lnet_process_id peer_id = getmsg->msg_target;
4700 LASSERT(!getmsg->msg_target_is_router);
4701 LASSERT(!getmsg->msg_routing);
4704 CERROR("%s: Dropping REPLY from %s: can't allocate msg\n",
4705 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id));
4709 cpt = lnet_cpt_of_cookie(getmd->md_lh.lh_cookie);
4712 LASSERT(getmd->md_refcount > 0);
4714 if (getmd->md_threshold == 0) {
4715 CERROR("%s: Dropping REPLY from %s for inactive MD %p\n",
4716 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id),
4718 lnet_res_unlock(cpt);
4722 LASSERT(getmd->md_offset == 0);
4724 CDEBUG(D_NET, "%s: Reply from %s md %p\n",
4725 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id), getmd);
4727 /* setup information for lnet_build_msg_event */
4728 msg->msg_initiator = getmsg->msg_txpeer->lpni_peer_net->lpn_peer->lp_primary_nid;
4729 msg->msg_from = peer_id.nid;
4730 msg->msg_type = LNET_MSG_GET; /* flag this msg as an "optimized" GET */
4731 msg->msg_hdr.src_nid = peer_id.nid;
4732 msg->msg_hdr.payload_length = getmd->md_length;
4733 msg->msg_receiving = 1; /* required by lnet_msg_attach_md */
4735 lnet_msg_attach_md(msg, getmd, getmd->md_offset, getmd->md_length);
4736 lnet_res_unlock(cpt);
4738 cpt = lnet_cpt_of_nid(peer_id.nid, ni);
4741 lnet_msg_commit(msg, cpt);
4742 lnet_net_unlock(cpt);
4744 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
4749 cpt = lnet_cpt_of_nid(peer_id.nid, ni);
4752 lnet_incr_stats(&ni->ni_stats, LNET_MSG_GET, LNET_STATS_TYPE_DROP);
4753 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
4754 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length +=
4756 lnet_net_unlock(cpt);
4763 EXPORT_SYMBOL(lnet_create_reply_msg);
4766 lnet_set_reply_msg_len(struct lnet_ni *ni, struct lnet_msg *reply,
4769 /* Set the REPLY length, now the RDMA that elides the REPLY message has
4770 * completed and I know it. */
4771 LASSERT(reply != NULL);
4772 LASSERT(reply->msg_type == LNET_MSG_GET);
4773 LASSERT(reply->msg_ev.type == LNET_EVENT_REPLY);
4775 /* NB I trusted my peer to RDMA. If she tells me she's written beyond
4776 * the end of my buffer, I might as well be dead. */
4777 LASSERT(len <= reply->msg_ev.mlength);
4779 reply->msg_ev.mlength = len;
4781 EXPORT_SYMBOL(lnet_set_reply_msg_len);
4784 * Initiate an asynchronous GET operation.
4786 * On the initiator node, an LNET_EVENT_SEND is logged when the GET request
4787 * is sent, and an LNET_EVENT_REPLY is logged when the data returned from
4788 * the target node in the REPLY has been written to local MD.
4790 * On the target node, an LNET_EVENT_GET is logged when the GET request
4791 * arrives and is accepted into a MD.
4793 * \param self,target,portal,match_bits,offset See the discussion in LNetPut().
4794 * \param mdh A handle for the MD that describes the memory into which the
4795 * requested data will be received. The MD must be "free floating" (See LNetMDBind()).
4797 * \retval 0 Success, and only in this case events will be generated
4798 * and logged to EQ (if it exists) of the MD.
4799 * \retval -EIO Simulated failure.
4800 * \retval -ENOMEM Memory allocation failure.
4801 * \retval -ENOENT Invalid MD object.
4804 LNetGet(lnet_nid_t self, struct lnet_handle_md mdh,
4805 struct lnet_process_id target, unsigned int portal,
4806 __u64 match_bits, unsigned int offset, bool recovery)
4808 struct lnet_msg *msg;
4809 struct lnet_libmd *md;
4810 struct lnet_rsp_tracker *rspt;
4814 LASSERT(the_lnet.ln_refcount > 0);
4816 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
4817 fail_peer(target.nid, 1)) /* shall we now? */
4819 CERROR("Dropping GET to %s: simulated failure\n",
4820 libcfs_id2str(target));
4824 msg = lnet_msg_alloc();
4826 CERROR("Dropping GET to %s: ENOMEM on struct lnet_msg\n",
4827 libcfs_id2str(target));
4831 cpt = lnet_cpt_of_cookie(mdh.cookie);
4833 rspt = lnet_rspt_alloc(cpt);
4835 CERROR("Dropping GET to %s: ENOMEM on response tracker\n",
4836 libcfs_id2str(target));
4839 INIT_LIST_HEAD(&rspt->rspt_on_list);
4841 msg->msg_recovery = recovery;
4845 md = lnet_handle2md(&mdh);
4846 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
4847 CERROR("Dropping GET (%llu:%d:%s): MD (%d) invalid\n",
4848 match_bits, portal, libcfs_id2str(target),
4849 md == NULL ? -1 : md->md_threshold);
4850 if (md != NULL && md->md_me != NULL)
4851 CERROR("REPLY MD also attached to portal %d\n",
4852 md->md_me->me_portal);
4854 lnet_res_unlock(cpt);
4857 LIBCFS_FREE(rspt, sizeof(*rspt));
4861 CDEBUG(D_NET, "LNetGet -> %s\n", libcfs_id2str(target));
4863 lnet_msg_attach_md(msg, md, 0, 0);
4865 lnet_prep_send(msg, LNET_MSG_GET, target, 0, 0);
4867 msg->msg_hdr.msg.get.match_bits = cpu_to_le64(match_bits);
4868 msg->msg_hdr.msg.get.ptl_index = cpu_to_le32(portal);
4869 msg->msg_hdr.msg.get.src_offset = cpu_to_le32(offset);
4870 msg->msg_hdr.msg.get.sink_length = cpu_to_le32(md->md_length);
4872 /* NB handles only looked up by creator (no flips) */
4873 msg->msg_hdr.msg.get.return_wmd.wh_interface_cookie =
4874 the_lnet.ln_interface_cookie;
4875 msg->msg_hdr.msg.get.return_wmd.wh_object_cookie =
4876 md->md_lh.lh_cookie;
4878 lnet_res_unlock(cpt);
4880 lnet_build_msg_event(msg, LNET_EVENT_SEND);
4882 lnet_attach_rsp_tracker(rspt, cpt, md, mdh);
4884 rc = lnet_send(self, msg, LNET_NID_ANY);
4886 CNETERR("Error sending GET to %s: %d\n",
4887 libcfs_id2str(target), rc);
4888 msg->msg_no_resend = true;
4889 lnet_finalize(msg, rc);
4892 /* completion will be signalled by an event */
4895 EXPORT_SYMBOL(LNetGet);
4898 * Calculate distance to node at \a dstnid.
4900 * \param dstnid Target NID.
4901 * \param srcnidp If not NULL, NID of the local interface to reach \a dstnid
4903 * \param orderp If not NULL, order of the route to reach \a dstnid is saved
4906 * \retval 0 If \a dstnid belongs to a local interface, and reserved option
4907 * local_nid_dist_zero is set, which is the default.
4908 * \retval positives Distance to target NID, i.e. number of hops plus one.
4909 * \retval -EHOSTUNREACH If \a dstnid is not reachable.
4912 LNetDist(lnet_nid_t dstnid, lnet_nid_t *srcnidp, __u32 *orderp)
4914 struct list_head *e;
4915 struct lnet_ni *ni = NULL;
4916 struct lnet_remotenet *rnet;
4917 __u32 dstnet = LNET_NIDNET(dstnid);
4921 struct list_head *rn_list;
4923 /* if !local_nid_dist_zero, I don't return a distance of 0 ever
4924 * (when lustre sees a distance of 0, it substitutes 0@lo), so I
4925 * keep order 0 free for 0@lo and order 1 free for a local NID
4928 LASSERT(the_lnet.ln_refcount > 0);
4930 cpt = lnet_net_lock_current();
4932 while ((ni = lnet_get_next_ni_locked(NULL, ni))) {
4933 if (ni->ni_nid == dstnid) {
4934 if (srcnidp != NULL)
4936 if (orderp != NULL) {
4937 if (LNET_NETTYP(LNET_NIDNET(dstnid)) == LOLND)
4942 lnet_net_unlock(cpt);
4944 return local_nid_dist_zero ? 0 : 1;
4947 if (LNET_NIDNET(ni->ni_nid) == dstnet) {
4948 /* Check if ni was originally created in
4949 * current net namespace.
4950 * If not, assign order above 0xffff0000,
4951 * to make this ni not a priority. */
4952 if (!net_eq(ni->ni_net_ns, current->nsproxy->net_ns))
4953 order += 0xffff0000;
4955 if (srcnidp != NULL)
4956 *srcnidp = ni->ni_nid;
4959 lnet_net_unlock(cpt);
4966 rn_list = lnet_net2rnethash(dstnet);
4967 list_for_each(e, rn_list) {
4968 rnet = list_entry(e, struct lnet_remotenet, lrn_list);
4970 if (rnet->lrn_net == dstnet) {
4971 struct lnet_route *route;
4972 struct lnet_route *shortest = NULL;
4973 __u32 shortest_hops = LNET_UNDEFINED_HOPS;
4976 LASSERT(!list_empty(&rnet->lrn_routes));
4978 list_for_each_entry(route, &rnet->lrn_routes,
4980 route_hops = route->lr_hops;
4981 if (route_hops == LNET_UNDEFINED_HOPS)
4983 if (shortest == NULL ||
4984 route_hops < shortest_hops) {
4986 shortest_hops = route_hops;
4990 LASSERT(shortest != NULL);
4991 hops = shortest_hops;
4992 if (srcnidp != NULL) {
4993 struct lnet_net *net;
4994 net = lnet_get_net_locked(shortest->lr_lnet);
4996 ni = lnet_get_next_ni_locked(net, NULL);
4997 *srcnidp = ni->ni_nid;
5001 lnet_net_unlock(cpt);
5007 lnet_net_unlock(cpt);
5008 return -EHOSTUNREACH;
5010 EXPORT_SYMBOL(LNetDist);