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 */
798 * This function can be called from two paths:
799 * 1. when sending a message
800 * 2. when decommiting a message (lnet_msg_decommit_tx())
801 * In both these cases the peer_ni should have it's reference count
802 * acquired by the caller and therefore it is safe to drop the spin
803 * lock before calling lnd_query()
806 lnet_ni_query_locked(struct lnet_ni *ni, struct lnet_peer_ni *lp)
808 time64_t last_alive = 0;
809 int cpt = lnet_cpt_of_nid_locked(lp->lpni_nid, ni);
811 LASSERT(lnet_peer_aliveness_enabled(lp));
812 LASSERT(ni->ni_net->net_lnd->lnd_query != NULL);
814 lnet_net_unlock(cpt);
815 (ni->ni_net->net_lnd->lnd_query)(ni, lp->lpni_nid, &last_alive);
818 lp->lpni_last_query = ktime_get_seconds();
820 if (last_alive != 0) /* NI has updated timestamp */
821 lp->lpni_last_alive = last_alive;
824 /* NB: always called with lnet_net_lock held */
826 lnet_peer_is_alive(struct lnet_peer_ni *lp, time64_t now)
831 LASSERT (lnet_peer_aliveness_enabled(lp));
834 * Trust lnet_notify() if it has more recent aliveness news, but
835 * ignore the initial assumed death (see lnet_peers_start_down()).
837 spin_lock(&lp->lpni_lock);
838 if (!lp->lpni_alive && lp->lpni_alive_count > 0 &&
839 lp->lpni_timestamp >= lp->lpni_last_alive) {
840 spin_unlock(&lp->lpni_lock);
844 deadline = lp->lpni_last_alive +
845 lp->lpni_net->net_tunables.lct_peer_timeout;
846 alive = deadline > now;
849 * Update obsolete lp_alive except for routers assumed to be dead
850 * initially, because router checker would update aliveness in this
851 * case, and moreover lpni_last_alive at peer creation is assumed.
853 if (alive && !lp->lpni_alive &&
854 !(lnet_isrouter(lp) && lp->lpni_alive_count == 0)) {
855 spin_unlock(&lp->lpni_lock);
856 lnet_notify_locked(lp, 0, 1, lp->lpni_last_alive);
858 spin_unlock(&lp->lpni_lock);
865 /* NB: returns 1 when alive, 0 when dead, negative when error;
866 * may drop the lnet_net_lock */
868 lnet_peer_alive_locked(struct lnet_ni *ni, struct lnet_peer_ni *lp,
869 struct lnet_msg *msg)
871 time64_t now = ktime_get_seconds();
873 if (!lnet_peer_aliveness_enabled(lp))
876 if (lnet_peer_is_alive(lp, now))
880 * If we're resending a message, let's attempt to send it even if
881 * the peer is down to fulfill our resend quota on the message
883 if (msg->msg_retry_count > 0)
887 * Peer appears dead, but we should avoid frequent NI queries (at
888 * most once per lnet_queryinterval seconds).
890 if (lp->lpni_last_query != 0) {
891 static const int lnet_queryinterval = 1;
894 next_query = lp->lpni_last_query + lnet_queryinterval;
896 if (now < next_query) {
898 CWARN("Unexpected aliveness of peer %s: "
899 "%lld < %lld (%d/%d)\n",
900 libcfs_nid2str(lp->lpni_nid),
903 lp->lpni_net->net_tunables.lct_peer_timeout);
908 /* query NI for latest aliveness news */
909 lnet_ni_query_locked(ni, lp);
911 if (lnet_peer_is_alive(lp, now))
914 lnet_notify_locked(lp, 0, 0, lp->lpni_last_alive);
919 * \param msg The message to be sent.
920 * \param do_send True if lnet_ni_send() should be called in this function.
921 * lnet_send() is going to lnet_net_unlock immediately after this, so
922 * it sets do_send FALSE and I don't do the unlock/send/lock bit.
924 * \retval LNET_CREDIT_OK If \a msg sent or OK to send.
925 * \retval LNET_CREDIT_WAIT If \a msg blocked for credit.
926 * \retval -EHOSTUNREACH If the next hop of the message appears dead.
927 * \retval -ECANCELED If the MD of the message has been unlinked.
930 lnet_post_send_locked(struct lnet_msg *msg, int do_send)
932 struct lnet_peer_ni *lp = msg->msg_txpeer;
933 struct lnet_ni *ni = msg->msg_txni;
934 int cpt = msg->msg_tx_cpt;
935 struct lnet_tx_queue *tq = ni->ni_tx_queues[cpt];
937 /* non-lnet_send() callers have checked before */
938 LASSERT(!do_send || msg->msg_tx_delayed);
939 LASSERT(!msg->msg_receiving);
940 LASSERT(msg->msg_tx_committed);
942 /* NB 'lp' is always the next hop */
943 if ((msg->msg_target.pid & LNET_PID_USERFLAG) == 0 &&
944 lnet_peer_alive_locked(ni, lp, msg) == 0) {
945 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
946 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length +=
948 lnet_net_unlock(cpt);
950 lnet_incr_stats(&msg->msg_txpeer->lpni_stats,
952 LNET_STATS_TYPE_DROP);
954 lnet_incr_stats(&msg->msg_txni->ni_stats,
956 LNET_STATS_TYPE_DROP);
958 CNETERR("Dropping message for %s: peer not alive\n",
959 libcfs_id2str(msg->msg_target));
960 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_DROPPED;
962 lnet_finalize(msg, -EHOSTUNREACH);
965 return -EHOSTUNREACH;
968 if (msg->msg_md != NULL &&
969 (msg->msg_md->md_flags & LNET_MD_FLAG_ABORTED) != 0) {
970 lnet_net_unlock(cpt);
972 CNETERR("Aborting message for %s: LNetM[DE]Unlink() already "
973 "called on the MD/ME.\n",
974 libcfs_id2str(msg->msg_target));
976 msg->msg_no_resend = true;
977 CDEBUG(D_NET, "msg %p to %s canceled and will not be resent\n",
978 msg, libcfs_id2str(msg->msg_target));
979 lnet_finalize(msg, -ECANCELED);
986 if (!msg->msg_peertxcredit) {
987 spin_lock(&lp->lpni_lock);
988 LASSERT((lp->lpni_txcredits < 0) ==
989 !list_empty(&lp->lpni_txq));
991 msg->msg_peertxcredit = 1;
992 lp->lpni_txqnob += msg->msg_len + sizeof(struct lnet_hdr);
993 lp->lpni_txcredits--;
995 if (lp->lpni_txcredits < lp->lpni_mintxcredits)
996 lp->lpni_mintxcredits = lp->lpni_txcredits;
998 if (lp->lpni_txcredits < 0) {
999 msg->msg_tx_delayed = 1;
1000 list_add_tail(&msg->msg_list, &lp->lpni_txq);
1001 spin_unlock(&lp->lpni_lock);
1002 return LNET_CREDIT_WAIT;
1004 spin_unlock(&lp->lpni_lock);
1007 if (!msg->msg_txcredit) {
1008 LASSERT((tq->tq_credits < 0) ==
1009 !list_empty(&tq->tq_delayed));
1011 msg->msg_txcredit = 1;
1013 atomic_dec(&ni->ni_tx_credits);
1015 if (tq->tq_credits < tq->tq_credits_min)
1016 tq->tq_credits_min = tq->tq_credits;
1018 if (tq->tq_credits < 0) {
1019 msg->msg_tx_delayed = 1;
1020 list_add_tail(&msg->msg_list, &tq->tq_delayed);
1021 return LNET_CREDIT_WAIT;
1025 /* unset the tx_delay flag as we're going to send it now */
1026 msg->msg_tx_delayed = 0;
1029 lnet_net_unlock(cpt);
1030 lnet_ni_send(ni, msg);
1033 return LNET_CREDIT_OK;
1037 static struct lnet_rtrbufpool *
1038 lnet_msg2bufpool(struct lnet_msg *msg)
1040 struct lnet_rtrbufpool *rbp;
1043 LASSERT(msg->msg_rx_committed);
1045 cpt = msg->msg_rx_cpt;
1046 rbp = &the_lnet.ln_rtrpools[cpt][0];
1048 LASSERT(msg->msg_len <= LNET_MTU);
1049 while (msg->msg_len > (unsigned int)rbp->rbp_npages * PAGE_SIZE) {
1051 LASSERT(rbp < &the_lnet.ln_rtrpools[cpt][LNET_NRBPOOLS]);
1058 lnet_post_routed_recv_locked(struct lnet_msg *msg, int do_recv)
1060 /* lnet_parse is going to lnet_net_unlock immediately after this, so it
1061 * sets do_recv FALSE and I don't do the unlock/send/lock bit.
1062 * I return LNET_CREDIT_WAIT if msg blocked and LNET_CREDIT_OK if
1063 * received or OK to receive */
1064 struct lnet_peer_ni *lp = msg->msg_rxpeer;
1065 struct lnet_rtrbufpool *rbp;
1066 struct lnet_rtrbuf *rb;
1068 LASSERT (msg->msg_iov == NULL);
1069 LASSERT (msg->msg_kiov == NULL);
1070 LASSERT (msg->msg_niov == 0);
1071 LASSERT (msg->msg_routing);
1072 LASSERT (msg->msg_receiving);
1073 LASSERT (!msg->msg_sending);
1075 /* non-lnet_parse callers only receive delayed messages */
1076 LASSERT(!do_recv || msg->msg_rx_delayed);
1078 if (!msg->msg_peerrtrcredit) {
1079 spin_lock(&lp->lpni_lock);
1080 LASSERT((lp->lpni_rtrcredits < 0) ==
1081 !list_empty(&lp->lpni_rtrq));
1083 msg->msg_peerrtrcredit = 1;
1084 lp->lpni_rtrcredits--;
1085 if (lp->lpni_rtrcredits < lp->lpni_minrtrcredits)
1086 lp->lpni_minrtrcredits = lp->lpni_rtrcredits;
1088 if (lp->lpni_rtrcredits < 0) {
1089 /* must have checked eager_recv before here */
1090 LASSERT(msg->msg_rx_ready_delay);
1091 msg->msg_rx_delayed = 1;
1092 list_add_tail(&msg->msg_list, &lp->lpni_rtrq);
1093 spin_unlock(&lp->lpni_lock);
1094 return LNET_CREDIT_WAIT;
1096 spin_unlock(&lp->lpni_lock);
1099 rbp = lnet_msg2bufpool(msg);
1101 if (!msg->msg_rtrcredit) {
1102 msg->msg_rtrcredit = 1;
1104 if (rbp->rbp_credits < rbp->rbp_mincredits)
1105 rbp->rbp_mincredits = rbp->rbp_credits;
1107 if (rbp->rbp_credits < 0) {
1108 /* must have checked eager_recv before here */
1109 LASSERT(msg->msg_rx_ready_delay);
1110 msg->msg_rx_delayed = 1;
1111 list_add_tail(&msg->msg_list, &rbp->rbp_msgs);
1112 return LNET_CREDIT_WAIT;
1116 LASSERT(!list_empty(&rbp->rbp_bufs));
1117 rb = list_entry(rbp->rbp_bufs.next, struct lnet_rtrbuf, rb_list);
1118 list_del(&rb->rb_list);
1120 msg->msg_niov = rbp->rbp_npages;
1121 msg->msg_kiov = &rb->rb_kiov[0];
1123 /* unset the msg-rx_delayed flag since we're receiving the message */
1124 msg->msg_rx_delayed = 0;
1127 int cpt = msg->msg_rx_cpt;
1129 lnet_net_unlock(cpt);
1130 lnet_ni_recv(msg->msg_rxni, msg->msg_private, msg, 1,
1131 0, msg->msg_len, msg->msg_len);
1134 return LNET_CREDIT_OK;
1138 lnet_return_tx_credits_locked(struct lnet_msg *msg)
1140 struct lnet_peer_ni *txpeer = msg->msg_txpeer;
1141 struct lnet_ni *txni = msg->msg_txni;
1142 struct lnet_msg *msg2;
1144 if (msg->msg_txcredit) {
1145 struct lnet_ni *ni = msg->msg_txni;
1146 struct lnet_tx_queue *tq = ni->ni_tx_queues[msg->msg_tx_cpt];
1148 /* give back NI txcredits */
1149 msg->msg_txcredit = 0;
1151 LASSERT((tq->tq_credits < 0) ==
1152 !list_empty(&tq->tq_delayed));
1155 atomic_inc(&ni->ni_tx_credits);
1156 if (tq->tq_credits <= 0) {
1157 msg2 = list_entry(tq->tq_delayed.next,
1158 struct lnet_msg, msg_list);
1159 list_del(&msg2->msg_list);
1161 LASSERT(msg2->msg_txni == ni);
1162 LASSERT(msg2->msg_tx_delayed);
1163 LASSERT(msg2->msg_tx_cpt == msg->msg_tx_cpt);
1165 (void) lnet_post_send_locked(msg2, 1);
1169 if (msg->msg_peertxcredit) {
1170 /* give back peer txcredits */
1171 msg->msg_peertxcredit = 0;
1173 spin_lock(&txpeer->lpni_lock);
1174 LASSERT((txpeer->lpni_txcredits < 0) ==
1175 !list_empty(&txpeer->lpni_txq));
1177 txpeer->lpni_txqnob -= msg->msg_len + sizeof(struct lnet_hdr);
1178 LASSERT(txpeer->lpni_txqnob >= 0);
1180 txpeer->lpni_txcredits++;
1181 if (txpeer->lpni_txcredits <= 0) {
1184 msg2 = list_entry(txpeer->lpni_txq.next,
1185 struct lnet_msg, msg_list);
1186 list_del(&msg2->msg_list);
1187 spin_unlock(&txpeer->lpni_lock);
1189 LASSERT(msg2->msg_txpeer == txpeer);
1190 LASSERT(msg2->msg_tx_delayed);
1192 msg2_cpt = msg2->msg_tx_cpt;
1195 * The msg_cpt can be different from the msg2_cpt
1196 * so we need to make sure we lock the correct cpt
1198 * Once we call lnet_post_send_locked() it is no
1199 * longer safe to access msg2, since it could've
1200 * been freed by lnet_finalize(), but we still
1201 * need to relock the correct cpt, so we cache the
1202 * msg2_cpt for the purpose of the check that
1203 * follows the call to lnet_pose_send_locked().
1205 if (msg2_cpt != msg->msg_tx_cpt) {
1206 lnet_net_unlock(msg->msg_tx_cpt);
1207 lnet_net_lock(msg2_cpt);
1209 (void) lnet_post_send_locked(msg2, 1);
1210 if (msg2_cpt != msg->msg_tx_cpt) {
1211 lnet_net_unlock(msg2_cpt);
1212 lnet_net_lock(msg->msg_tx_cpt);
1215 spin_unlock(&txpeer->lpni_lock);
1220 msg->msg_txni = NULL;
1221 lnet_ni_decref_locked(txni, msg->msg_tx_cpt);
1224 if (txpeer != NULL) {
1225 msg->msg_txpeer = NULL;
1226 lnet_peer_ni_decref_locked(txpeer);
1231 lnet_schedule_blocked_locked(struct lnet_rtrbufpool *rbp)
1233 struct lnet_msg *msg;
1235 if (list_empty(&rbp->rbp_msgs))
1237 msg = list_entry(rbp->rbp_msgs.next,
1238 struct lnet_msg, msg_list);
1239 list_del(&msg->msg_list);
1241 (void)lnet_post_routed_recv_locked(msg, 1);
1245 lnet_drop_routed_msgs_locked(struct list_head *list, int cpt)
1247 struct lnet_msg *msg;
1248 struct lnet_msg *tmp;
1250 lnet_net_unlock(cpt);
1252 list_for_each_entry_safe(msg, tmp, list, msg_list) {
1253 lnet_ni_recv(msg->msg_rxni, msg->msg_private, NULL,
1254 0, 0, 0, msg->msg_hdr.payload_length);
1255 list_del_init(&msg->msg_list);
1256 msg->msg_no_resend = true;
1257 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_ERROR;
1258 lnet_finalize(msg, -ECANCELED);
1265 lnet_return_rx_credits_locked(struct lnet_msg *msg)
1267 struct lnet_peer_ni *rxpeer = msg->msg_rxpeer;
1268 struct lnet_ni *rxni = msg->msg_rxni;
1269 struct lnet_msg *msg2;
1271 if (msg->msg_rtrcredit) {
1272 /* give back global router credits */
1273 struct lnet_rtrbuf *rb;
1274 struct lnet_rtrbufpool *rbp;
1276 /* NB If a msg ever blocks for a buffer in rbp_msgs, it stays
1277 * there until it gets one allocated, or aborts the wait
1279 LASSERT(msg->msg_kiov != NULL);
1281 rb = list_entry(msg->msg_kiov, struct lnet_rtrbuf, rb_kiov[0]);
1284 msg->msg_kiov = NULL;
1285 msg->msg_rtrcredit = 0;
1287 LASSERT(rbp == lnet_msg2bufpool(msg));
1289 LASSERT((rbp->rbp_credits > 0) ==
1290 !list_empty(&rbp->rbp_bufs));
1292 /* If routing is now turned off, we just drop this buffer and
1293 * don't bother trying to return credits. */
1294 if (!the_lnet.ln_routing) {
1295 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1299 /* It is possible that a user has lowered the desired number of
1300 * buffers in this pool. Make sure we never put back
1301 * more buffers than the stated number. */
1302 if (unlikely(rbp->rbp_credits >= rbp->rbp_req_nbuffers)) {
1303 /* Discard this buffer so we don't have too
1305 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1306 rbp->rbp_nbuffers--;
1308 list_add(&rb->rb_list, &rbp->rbp_bufs);
1310 if (rbp->rbp_credits <= 0)
1311 lnet_schedule_blocked_locked(rbp);
1316 if (msg->msg_peerrtrcredit) {
1317 /* give back peer router credits */
1318 msg->msg_peerrtrcredit = 0;
1320 spin_lock(&rxpeer->lpni_lock);
1321 LASSERT((rxpeer->lpni_rtrcredits < 0) ==
1322 !list_empty(&rxpeer->lpni_rtrq));
1324 rxpeer->lpni_rtrcredits++;
1326 /* drop all messages which are queued to be routed on that
1328 if (!the_lnet.ln_routing) {
1329 struct list_head drop;
1330 INIT_LIST_HEAD(&drop);
1331 list_splice_init(&rxpeer->lpni_rtrq, &drop);
1332 spin_unlock(&rxpeer->lpni_lock);
1333 lnet_drop_routed_msgs_locked(&drop, msg->msg_rx_cpt);
1334 } else if (rxpeer->lpni_rtrcredits <= 0) {
1335 msg2 = list_entry(rxpeer->lpni_rtrq.next,
1336 struct lnet_msg, msg_list);
1337 list_del(&msg2->msg_list);
1338 spin_unlock(&rxpeer->lpni_lock);
1339 (void) lnet_post_routed_recv_locked(msg2, 1);
1341 spin_unlock(&rxpeer->lpni_lock);
1345 msg->msg_rxni = NULL;
1346 lnet_ni_decref_locked(rxni, msg->msg_rx_cpt);
1348 if (rxpeer != NULL) {
1349 msg->msg_rxpeer = NULL;
1350 lnet_peer_ni_decref_locked(rxpeer);
1355 lnet_compare_peers(struct lnet_peer_ni *p1, struct lnet_peer_ni *p2)
1357 if (p1->lpni_txqnob < p2->lpni_txqnob)
1360 if (p1->lpni_txqnob > p2->lpni_txqnob)
1363 if (p1->lpni_txcredits > p2->lpni_txcredits)
1366 if (p1->lpni_txcredits < p2->lpni_txcredits)
1373 lnet_compare_routes(struct lnet_route *r1, struct lnet_route *r2)
1375 struct lnet_peer_ni *p1 = r1->lr_gateway;
1376 struct lnet_peer_ni *p2 = r2->lr_gateway;
1377 int r1_hops = (r1->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r1->lr_hops;
1378 int r2_hops = (r2->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r2->lr_hops;
1381 if (r1->lr_priority < r2->lr_priority)
1384 if (r1->lr_priority > r2->lr_priority)
1387 if (r1_hops < r2_hops)
1390 if (r1_hops > r2_hops)
1393 rc = lnet_compare_peers(p1, p2);
1397 if (r1->lr_seq - r2->lr_seq <= 0)
1403 static struct lnet_peer_ni *
1404 lnet_find_route_locked(struct lnet_net *net, __u32 remote_net,
1407 struct lnet_remotenet *rnet;
1408 struct lnet_route *route;
1409 struct lnet_route *best_route;
1410 struct lnet_route *last_route;
1411 struct lnet_peer_ni *lpni_best;
1412 struct lnet_peer_ni *lp;
1415 /* If @rtr_nid is not LNET_NID_ANY, return the gateway with
1416 * rtr_nid nid, otherwise find the best gateway I can use */
1418 rnet = lnet_find_rnet_locked(remote_net);
1423 best_route = last_route = NULL;
1424 list_for_each_entry(route, &rnet->lrn_routes, lr_list) {
1425 lp = route->lr_gateway;
1427 if (!lnet_is_route_alive(route))
1430 if (net != NULL && lp->lpni_net != net)
1433 if (lp->lpni_nid == rtr_nid) /* it's pre-determined router */
1436 if (lpni_best == NULL) {
1437 best_route = last_route = route;
1442 /* no protection on below fields, but it's harmless */
1443 if (last_route->lr_seq - route->lr_seq < 0)
1446 rc = lnet_compare_routes(route, best_route);
1454 /* set sequence number on the best router to the latest sequence + 1
1455 * so we can round-robin all routers, it's race and inaccurate but
1456 * harmless and functional */
1457 if (best_route != NULL)
1458 best_route->lr_seq = last_route->lr_seq + 1;
1462 static struct lnet_ni *
1463 lnet_get_best_ni(struct lnet_net *local_net, struct lnet_ni *best_ni,
1464 struct lnet_peer *peer, struct lnet_peer_net *peer_net,
1467 struct lnet_ni *ni = NULL;
1468 unsigned int shortest_distance;
1473 * If there is no peer_ni that we can send to on this network,
1474 * then there is no point in looking for a new best_ni here.
1476 if (!lnet_get_next_peer_ni_locked(peer, peer_net, NULL))
1479 if (best_ni == NULL) {
1480 shortest_distance = UINT_MAX;
1481 best_credits = INT_MIN;
1484 shortest_distance = cfs_cpt_distance(lnet_cpt_table(), md_cpt,
1485 best_ni->ni_dev_cpt);
1486 best_credits = atomic_read(&best_ni->ni_tx_credits);
1487 best_healthv = atomic_read(&best_ni->ni_healthv);
1490 while ((ni = lnet_get_next_ni_locked(local_net, ni))) {
1491 unsigned int distance;
1496 ni_credits = atomic_read(&ni->ni_tx_credits);
1497 ni_healthv = atomic_read(&ni->ni_healthv);
1498 ni_fatal = atomic_read(&ni->ni_fatal_error_on);
1501 * calculate the distance from the CPT on which
1502 * the message memory is allocated to the CPT of
1503 * the NI's physical device
1505 distance = cfs_cpt_distance(lnet_cpt_table(),
1509 CDEBUG(D_NET, "compare ni %s [c:%d, d:%d, s:%d] with best_ni %s [c:%d, d:%d, s:%d]\n",
1510 libcfs_nid2str(ni->ni_nid), ni_credits, distance,
1511 ni->ni_seq, (best_ni) ? libcfs_nid2str(best_ni->ni_nid)
1512 : "not seleced", best_credits, shortest_distance,
1513 (best_ni) ? best_ni->ni_seq : 0);
1516 * All distances smaller than the NUMA range
1517 * are treated equally.
1519 if (distance < lnet_numa_range)
1520 distance = lnet_numa_range;
1523 * Select on health, shorter distance, available
1524 * credits, then round-robin.
1528 } else if (ni_healthv < best_healthv) {
1530 } else if (ni_healthv > best_healthv) {
1531 best_healthv = ni_healthv;
1533 * If we're going to prefer this ni because it's
1534 * the healthiest, then we should set the
1535 * shortest_distance in the algorithm in case
1536 * there are multiple NIs with the same health but
1537 * different distances.
1539 if (distance < shortest_distance)
1540 shortest_distance = distance;
1541 } else if (distance > shortest_distance) {
1543 } else if (distance < shortest_distance) {
1544 shortest_distance = distance;
1545 } else if (ni_credits < best_credits) {
1547 } else if (ni_credits == best_credits) {
1548 if (best_ni && best_ni->ni_seq <= ni->ni_seq)
1552 best_credits = ni_credits;
1555 CDEBUG(D_NET, "selected best_ni %s\n",
1556 (best_ni) ? libcfs_nid2str(best_ni->ni_nid) : "no selection");
1562 * Traffic to the LNET_RESERVED_PORTAL may not trigger peer discovery,
1563 * because such traffic is required to perform discovery. We therefore
1564 * exclude all GET and PUT on that portal. We also exclude all ACK and
1565 * REPLY traffic, but that is because the portal is not tracked in the
1566 * message structure for these message types. We could restrict this
1567 * further by also checking for LNET_PROTO_PING_MATCHBITS.
1570 lnet_msg_discovery(struct lnet_msg *msg)
1572 if (msg->msg_type == LNET_MSG_PUT) {
1573 if (msg->msg_hdr.msg.put.ptl_index != LNET_RESERVED_PORTAL)
1575 } else if (msg->msg_type == LNET_MSG_GET) {
1576 if (msg->msg_hdr.msg.get.ptl_index != LNET_RESERVED_PORTAL)
1582 #define SRC_SPEC 0x0001
1583 #define SRC_ANY 0x0002
1584 #define LOCAL_DST 0x0004
1585 #define REMOTE_DST 0x0008
1586 #define MR_DST 0x0010
1587 #define NMR_DST 0x0020
1588 #define SND_RESP 0x0040
1590 /* The following to defines are used for return codes */
1591 #define REPEAT_SEND 0x1000
1592 #define PASS_THROUGH 0x2000
1594 /* The different cases lnet_select pathway needs to handle */
1595 #define SRC_SPEC_LOCAL_MR_DST (SRC_SPEC | LOCAL_DST | MR_DST)
1596 #define SRC_SPEC_ROUTER_MR_DST (SRC_SPEC | REMOTE_DST | MR_DST)
1597 #define SRC_SPEC_LOCAL_NMR_DST (SRC_SPEC | LOCAL_DST | NMR_DST)
1598 #define SRC_SPEC_ROUTER_NMR_DST (SRC_SPEC | REMOTE_DST | NMR_DST)
1599 #define SRC_ANY_LOCAL_MR_DST (SRC_ANY | LOCAL_DST | MR_DST)
1600 #define SRC_ANY_ROUTER_MR_DST (SRC_ANY | REMOTE_DST | MR_DST)
1601 #define SRC_ANY_LOCAL_NMR_DST (SRC_ANY | LOCAL_DST | NMR_DST)
1602 #define SRC_ANY_ROUTER_NMR_DST (SRC_ANY | REMOTE_DST | NMR_DST)
1605 lnet_handle_send(struct lnet_send_data *sd)
1607 struct lnet_ni *best_ni = sd->sd_best_ni;
1608 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
1609 struct lnet_peer_ni *final_dst_lpni = sd->sd_final_dst_lpni;
1610 struct lnet_msg *msg = sd->sd_msg;
1612 __u32 send_case = sd->sd_send_case;
1614 __u32 routing = send_case & REMOTE_DST;
1615 struct lnet_rsp_tracker *rspt;
1618 * Increment sequence number of the selected peer so that we
1619 * pick the next one in Round Robin.
1621 best_lpni->lpni_seq++;
1624 * grab a reference on the peer_ni so it sticks around even if
1625 * we need to drop and relock the lnet_net_lock below.
1627 lnet_peer_ni_addref_locked(best_lpni);
1630 * Use lnet_cpt_of_nid() to determine the CPT used to commit the
1631 * message. This ensures that we get a CPT that is correct for
1632 * the NI when the NI has been restricted to a subset of all CPTs.
1633 * If the selected CPT differs from the one currently locked, we
1634 * must unlock and relock the lnet_net_lock(), and then check whether
1635 * the configuration has changed. We don't have a hold on the best_ni
1636 * yet, and it may have vanished.
1638 cpt2 = lnet_cpt_of_nid_locked(best_lpni->lpni_nid, best_ni);
1639 if (sd->sd_cpt != cpt2) {
1640 __u32 seq = lnet_get_dlc_seq_locked();
1641 lnet_net_unlock(sd->sd_cpt);
1643 lnet_net_lock(sd->sd_cpt);
1644 if (seq != lnet_get_dlc_seq_locked()) {
1645 lnet_peer_ni_decref_locked(best_lpni);
1651 * store the best_lpni in the message right away to avoid having
1652 * to do the same operation under different conditions
1654 msg->msg_txpeer = best_lpni;
1655 msg->msg_txni = best_ni;
1658 * grab a reference for the best_ni since now it's in use in this
1659 * send. The reference will be dropped in lnet_finalize()
1661 lnet_ni_addref_locked(msg->msg_txni, sd->sd_cpt);
1664 * Always set the target.nid to the best peer picked. Either the
1665 * NID will be one of the peer NIDs selected, or the same NID as
1666 * what was originally set in the target or it will be the NID of
1667 * a router if this message should be routed
1669 msg->msg_target.nid = msg->msg_txpeer->lpni_nid;
1672 * lnet_msg_commit assigns the correct cpt to the message, which
1673 * is used to decrement the correct refcount on the ni when it's
1674 * time to return the credits
1676 lnet_msg_commit(msg, sd->sd_cpt);
1679 * If we are routing the message then we keep the src_nid that was
1680 * set by the originator. If we are not routing then we are the
1681 * originator and set it here.
1683 if (!msg->msg_routing)
1684 msg->msg_hdr.src_nid = cpu_to_le64(msg->msg_txni->ni_nid);
1687 msg->msg_target_is_router = 1;
1688 msg->msg_target.pid = LNET_PID_LUSTRE;
1690 * since we're routing we want to ensure that the
1691 * msg_hdr.dest_nid is set to the final destination. When
1692 * the router receives this message it knows how to route
1695 * final_dst_lpni is set at the beginning of the
1696 * lnet_select_pathway() function and is never changed.
1697 * It's safe to use it here.
1699 msg->msg_hdr.dest_nid = cpu_to_le64(final_dst_lpni->lpni_nid);
1702 * if we're not routing set the dest_nid to the best peer
1703 * ni NID that we picked earlier in the algorithm.
1705 msg->msg_hdr.dest_nid = cpu_to_le64(msg->msg_txpeer->lpni_nid);
1709 * if we have response tracker block update it with the next hop
1713 rspt = msg->msg_md->md_rspt_ptr;
1715 rspt->rspt_next_hop_nid = msg->msg_txpeer->lpni_nid;
1716 CDEBUG(D_NET, "rspt_next_hop_nid = %s\n",
1717 libcfs_nid2str(rspt->rspt_next_hop_nid));
1721 rc = lnet_post_send_locked(msg, 0);
1724 CDEBUG(D_NET, "TRACE: %s(%s:%s) -> %s(%s:%s) : %s try# %d\n",
1725 libcfs_nid2str(msg->msg_hdr.src_nid),
1726 libcfs_nid2str(msg->msg_txni->ni_nid),
1727 libcfs_nid2str(sd->sd_src_nid),
1728 libcfs_nid2str(msg->msg_hdr.dest_nid),
1729 libcfs_nid2str(sd->sd_dst_nid),
1730 libcfs_nid2str(msg->msg_txpeer->lpni_nid),
1731 lnet_msgtyp2str(msg->msg_type), msg->msg_retry_count);
1736 static struct lnet_peer_ni *
1737 lnet_select_peer_ni(struct lnet_send_data *sd, struct lnet_peer *peer,
1738 struct lnet_peer_net *peer_net)
1741 * Look at the peer NIs for the destination peer that connect
1742 * to the chosen net. If a peer_ni is preferred when using the
1743 * best_ni to communicate, we use that one. If there is no
1744 * preferred peer_ni, or there are multiple preferred peer_ni,
1745 * the available transmit credits are used. If the transmit
1746 * credits are equal, we round-robin over the peer_ni.
1748 struct lnet_peer_ni *lpni = NULL;
1749 struct lnet_peer_ni *best_lpni = NULL;
1750 struct lnet_ni *best_ni = sd->sd_best_ni;
1751 lnet_nid_t dst_nid = sd->sd_dst_nid;
1752 int best_lpni_credits = INT_MIN;
1753 bool preferred = false;
1755 int best_lpni_healthv = 0;
1758 while ((lpni = lnet_get_next_peer_ni_locked(peer, peer_net, lpni))) {
1760 * if the best_ni we've chosen aleady has this lpni
1761 * preferred, then let's use it
1763 ni_is_pref = lnet_peer_is_pref_nid_locked(lpni,
1766 lpni_healthv = atomic_read(&lpni->lpni_healthv);
1768 CDEBUG(D_NET, "%s ni_is_pref = %d\n",
1769 libcfs_nid2str(best_ni->ni_nid), ni_is_pref);
1772 CDEBUG(D_NET, "%s c:[%d, %d], s:[%d, %d]\n",
1773 libcfs_nid2str(lpni->lpni_nid),
1774 lpni->lpni_txcredits, best_lpni_credits,
1775 lpni->lpni_seq, best_lpni->lpni_seq);
1777 /* pick the healthiest peer ni */
1778 if (lpni_healthv < best_lpni_healthv) {
1780 } else if (lpni_healthv > best_lpni_healthv) {
1781 best_lpni_healthv = lpni_healthv;
1782 /* if this is a preferred peer use it */
1783 } else if (!preferred && ni_is_pref) {
1785 } else if (preferred && !ni_is_pref) {
1787 * this is not the preferred peer so let's ignore
1791 } else if (lpni->lpni_txcredits < best_lpni_credits) {
1793 * We already have a peer that has more credits
1794 * available than this one. No need to consider
1795 * this peer further.
1798 } else if (lpni->lpni_txcredits == best_lpni_credits) {
1800 * The best peer found so far and the current peer
1801 * have the same number of available credits let's
1802 * make sure to select between them using Round
1806 if (best_lpni->lpni_seq <= lpni->lpni_seq)
1812 best_lpni_credits = lpni->lpni_txcredits;
1815 /* if we still can't find a peer ni then we can't reach it */
1817 __u32 net_id = (peer_net) ? peer_net->lpn_net_id :
1818 LNET_NIDNET(dst_nid);
1819 CDEBUG(D_NET, "no peer_ni found on peer net %s\n",
1820 libcfs_net2str(net_id));
1824 CDEBUG(D_NET, "sd_best_lpni = %s\n",
1825 libcfs_nid2str(best_lpni->lpni_nid));
1831 * Prerequisite: the best_ni should already be set in the sd
1833 static inline struct lnet_peer_ni *
1834 lnet_find_best_lpni_on_net(struct lnet_send_data *sd, struct lnet_peer *peer,
1837 struct lnet_peer_net *peer_net;
1840 * The gateway is Multi-Rail capable so now we must select the
1843 peer_net = lnet_peer_get_net_locked(peer, net_id);
1846 CERROR("gateway peer %s has no NI on net %s\n",
1847 libcfs_nid2str(peer->lp_primary_nid),
1848 libcfs_net2str(net_id));
1852 return lnet_select_peer_ni(sd, peer, peer_net);
1856 lnet_set_non_mr_pref_nid(struct lnet_send_data *sd)
1858 if (sd->sd_send_case & NMR_DST &&
1859 sd->sd_msg->msg_type != LNET_MSG_REPLY &&
1860 sd->sd_msg->msg_type != LNET_MSG_ACK &&
1861 sd->sd_best_lpni->lpni_pref_nnids == 0) {
1862 CDEBUG(D_NET, "Setting preferred local NID %s on NMR peer %s\n",
1863 libcfs_nid2str(sd->sd_best_ni->ni_nid),
1864 libcfs_nid2str(sd->sd_best_lpni->lpni_nid));
1865 lnet_peer_ni_set_non_mr_pref_nid(sd->sd_best_lpni,
1866 sd->sd_best_ni->ni_nid);
1875 * use the source and destination NIDs as the pathway
1878 lnet_handle_spec_local_nmr_dst(struct lnet_send_data *sd)
1880 /* the destination lpni is set before we get here. */
1883 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
1884 if (!sd->sd_best_ni) {
1885 CERROR("Can't send to %s: src %s is not a "
1886 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
1887 libcfs_nid2str(sd->sd_src_nid));
1892 * the preferred NID will only be set for NMR peers
1894 lnet_set_non_mr_pref_nid(sd);
1896 return lnet_handle_send(sd);
1904 * Run the selection algorithm on the peer NIs unless we're sending
1905 * a response, in this case just send to the destination
1908 lnet_handle_spec_local_mr_dst(struct lnet_send_data *sd)
1910 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
1911 if (!sd->sd_best_ni) {
1912 CERROR("Can't send to %s: src %s is not a "
1913 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
1914 libcfs_nid2str(sd->sd_src_nid));
1919 * only run the selection algorithm to pick the peer_ni if we're
1920 * sending a GET or a PUT. Responses are sent to the same
1921 * destination NID provided.
1923 if (!(sd->sd_send_case & SND_RESP)) {
1925 lnet_find_best_lpni_on_net(sd, sd->sd_peer,
1926 sd->sd_best_ni->ni_net->net_id);
1929 if (sd->sd_best_lpni)
1930 return lnet_handle_send(sd);
1932 CERROR("can't send to %s. no NI on %s\n",
1933 libcfs_nid2str(sd->sd_dst_nid),
1934 libcfs_net2str(sd->sd_best_ni->ni_net->net_id));
1936 return -EHOSTUNREACH;
1940 lnet_find_best_ni_on_spec_net(struct lnet_ni *cur_best_ni,
1941 struct lnet_peer *peer,
1942 struct lnet_peer_net *peer_net,
1946 struct lnet_net *local_net;
1947 struct lnet_ni *best_ni;
1949 local_net = lnet_get_net_locked(peer_net->lpn_net_id);
1954 * Iterate through the NIs in this local Net and select
1955 * the NI to send from. The selection is determined by
1956 * these 3 criterion in the following priority:
1958 * 2. NI available credits
1961 best_ni = lnet_get_best_ni(local_net, cur_best_ni,
1962 peer, peer_net, cpt);
1964 if (incr_seq && best_ni)
1971 lnet_handle_find_routed_path(struct lnet_send_data *sd,
1973 struct lnet_peer_ni **gw_lpni,
1974 struct lnet_peer **gw_peer)
1976 struct lnet_peer_ni *gw;
1977 lnet_nid_t src_nid = sd->sd_src_nid;
1979 gw = lnet_find_route_locked(NULL, LNET_NIDNET(dst_nid),
1982 CERROR("no route to %s from %s\n",
1983 libcfs_nid2str(dst_nid), libcfs_nid2str(src_nid));
1984 return -EHOSTUNREACH;
1987 /* get the peer of the gw_ni */
1988 LASSERT(gw->lpni_peer_net);
1989 LASSERT(gw->lpni_peer_net->lpn_peer);
1991 *gw_peer = gw->lpni_peer_net->lpn_peer;
1993 if (!sd->sd_best_ni)
1994 sd->sd_best_ni = lnet_find_best_ni_on_spec_net(NULL, *gw_peer,
1999 if (!sd->sd_best_ni) {
2000 CERROR("Internal Error. Expected local ni on %s "
2001 "but non found :%s\n",
2002 libcfs_net2str(gw->lpni_peer_net->lpn_net_id),
2003 libcfs_nid2str(sd->sd_src_nid));
2008 * if gw is MR let's find its best peer_ni
2010 if (lnet_peer_is_multi_rail(*gw_peer)) {
2011 gw = lnet_find_best_lpni_on_net(sd, *gw_peer,
2012 sd->sd_best_ni->ni_net->net_id);
2014 * We've already verified that the gw has an NI on that
2015 * desired net, but we're not finding it. Something is
2019 CERROR("Internal Error. Route expected to %s from %s\n",
2020 libcfs_nid2str(dst_nid),
2021 libcfs_nid2str(src_nid));
2036 * Remote destination
2037 * Non-MR destination
2041 * Remote destination
2044 * The handling of these two cases is similar. Even though the destination
2045 * can be MR or non-MR, we'll deal directly with the router.
2048 lnet_handle_spec_router_dst(struct lnet_send_data *sd)
2051 struct lnet_peer_ni *gw_lpni = NULL;
2052 struct lnet_peer *gw_peer = NULL;
2055 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
2056 if (!sd->sd_best_ni) {
2057 CERROR("Can't send to %s: src %s is not a "
2058 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
2059 libcfs_nid2str(sd->sd_src_nid));
2063 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2068 if (sd->sd_send_case & NMR_DST)
2070 * since the final destination is non-MR let's set its preferred
2071 * NID before we send
2073 lnet_set_non_mr_pref_nid(sd);
2076 * We're going to send to the gw found so let's set its
2079 sd->sd_peer = gw_peer;
2080 sd->sd_best_lpni = gw_lpni;
2082 return lnet_handle_send(sd);
2086 lnet_find_best_ni_on_local_net(struct lnet_peer *peer, int md_cpt)
2088 struct lnet_peer_net *peer_net = NULL;
2089 struct lnet_ni *best_ni = NULL;
2092 * The peer can have multiple interfaces, some of them can be on
2093 * the local network and others on a routed network. We should
2094 * prefer the local network. However if the local network is not
2095 * available then we need to try the routed network
2098 /* go through all the peer nets and find the best_ni */
2099 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_peer_nets) {
2101 * The peer's list of nets can contain non-local nets. We
2102 * want to only examine the local ones.
2104 if (!lnet_get_net_locked(peer_net->lpn_net_id))
2106 best_ni = lnet_find_best_ni_on_spec_net(best_ni, peer,
2107 peer_net, md_cpt, false);
2111 /* increment sequence number so we can round robin */
2117 static struct lnet_ni *
2118 lnet_find_existing_preferred_best_ni(struct lnet_send_data *sd)
2120 struct lnet_ni *best_ni = NULL;
2121 struct lnet_peer_net *peer_net;
2122 struct lnet_peer *peer = sd->sd_peer;
2123 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
2124 struct lnet_peer_ni *lpni;
2125 int cpt = sd->sd_cpt;
2128 * We must use a consistent source address when sending to a
2129 * non-MR peer. However, a non-MR peer can have multiple NIDs
2130 * on multiple networks, and we may even need to talk to this
2131 * peer on multiple networks -- certain types of
2132 * load-balancing configuration do this.
2134 * So we need to pick the NI the peer prefers for this
2135 * particular network.
2138 /* Get the target peer_ni */
2139 peer_net = lnet_peer_get_net_locked(peer,
2140 LNET_NIDNET(best_lpni->lpni_nid));
2141 LASSERT(peer_net != NULL);
2142 list_for_each_entry(lpni, &peer_net->lpn_peer_nis,
2144 if (lpni->lpni_pref_nnids == 0)
2146 LASSERT(lpni->lpni_pref_nnids == 1);
2147 best_ni = lnet_nid2ni_locked(
2148 lpni->lpni_pref.nid, cpt);
2155 /* Prerequisite: sd->sd_peer and sd->sd_best_lpni should be set */
2157 lnet_select_preferred_best_ni(struct lnet_send_data *sd)
2159 struct lnet_ni *best_ni = NULL;
2160 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
2163 * We must use a consistent source address when sending to a
2164 * non-MR peer. However, a non-MR peer can have multiple NIDs
2165 * on multiple networks, and we may even need to talk to this
2166 * peer on multiple networks -- certain types of
2167 * load-balancing configuration do this.
2169 * So we need to pick the NI the peer prefers for this
2170 * particular network.
2173 best_ni = lnet_find_existing_preferred_best_ni(sd);
2175 /* if best_ni is still not set just pick one */
2178 lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2179 sd->sd_best_lpni->lpni_peer_net,
2180 sd->sd_md_cpt, true);
2181 /* If there is no best_ni we don't have a route */
2183 CERROR("no path to %s from net %s\n",
2184 libcfs_nid2str(best_lpni->lpni_nid),
2185 libcfs_net2str(best_lpni->lpni_net->net_id));
2186 return -EHOSTUNREACH;
2190 sd->sd_best_ni = best_ni;
2192 /* Set preferred NI if necessary. */
2193 lnet_set_non_mr_pref_nid(sd);
2200 * Source not specified
2204 * always use the same source NID for NMR peers
2205 * If we've talked to that peer before then we already have a preferred
2206 * source NI associated with it. Otherwise, we select a preferred local NI
2207 * and store it in the peer
2210 lnet_handle_any_local_nmr_dst(struct lnet_send_data *sd)
2214 /* sd->sd_best_lpni is already set to the final destination */
2217 * At this point we should've created the peer ni and peer. If we
2218 * can't find it, then something went wrong. Instead of assert
2219 * output a relevant message and fail the send
2221 if (!sd->sd_best_lpni) {
2222 CERROR("Internal fault. Unable to send msg %s to %s. "
2224 lnet_msgtyp2str(sd->sd_msg->msg_type),
2225 libcfs_nid2str(sd->sd_dst_nid));
2229 rc = lnet_select_preferred_best_ni(sd);
2231 rc = lnet_handle_send(sd);
2237 lnet_handle_any_mr_dsta(struct lnet_send_data *sd)
2240 * NOTE we've already handled the remote peer case. So we only
2241 * need to worry about the local case here.
2243 * if we're sending a response, ACK or reply, we need to send it
2244 * to the destination NID given to us. At this point we already
2245 * have the peer_ni we're suppose to send to, so just find the
2246 * best_ni on the peer net and use that. Since we're sending to an
2247 * MR peer then we can just run the selection algorithm on our
2248 * local NIs and pick the best one.
2250 if (sd->sd_send_case & SND_RESP) {
2252 lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2253 sd->sd_best_lpni->lpni_peer_net,
2254 sd->sd_md_cpt, true);
2256 if (!sd->sd_best_ni) {
2258 * We're not going to deal with not able to send
2259 * a response to the provided final destination
2261 CERROR("Can't send response to %s. "
2262 "No local NI available\n",
2263 libcfs_nid2str(sd->sd_dst_nid));
2264 return -EHOSTUNREACH;
2267 return lnet_handle_send(sd);
2271 * If we get here that means we're sending a fresh request, PUT or
2272 * GET, so we need to run our standard selection algorithm.
2273 * First find the best local interface that's on any of the peer's
2276 sd->sd_best_ni = lnet_find_best_ni_on_local_net(sd->sd_peer,
2278 if (sd->sd_best_ni) {
2280 lnet_find_best_lpni_on_net(sd, sd->sd_peer,
2281 sd->sd_best_ni->ni_net->net_id);
2284 * if we're successful in selecting a peer_ni on the local
2285 * network, then send to it. Otherwise fall through and
2286 * try and see if we can reach it over another routed
2289 if (sd->sd_best_lpni) {
2291 * in case we initially started with a routed
2292 * destination, let's reset to local
2294 sd->sd_send_case &= ~REMOTE_DST;
2295 sd->sd_send_case |= LOCAL_DST;
2296 return lnet_handle_send(sd);
2299 CERROR("Internal Error. Expected to have a best_lpni: "
2301 libcfs_nid2str(sd->sd_src_nid),
2302 libcfs_nid2str(sd->sd_dst_nid));
2308 * Peer doesn't have a local network. Let's see if there is
2309 * a remote network we can reach it on.
2311 return PASS_THROUGH;
2316 * Source NID not specified
2321 * Source NID not speified
2322 * Remote destination
2325 * In both of these cases if we're sending a response, ACK or REPLY, then
2326 * we need to send to the destination NID provided.
2328 * In the remote case let's deal with MR routers.
2333 lnet_handle_any_mr_dst(struct lnet_send_data *sd)
2336 struct lnet_peer *gw_peer = NULL;
2337 struct lnet_peer_ni *gw_lpni = NULL;
2340 * handle sending a response to a remote peer here so we don't
2341 * have to worry about it if we hit lnet_handle_any_mr_dsta()
2343 if (sd->sd_send_case & REMOTE_DST &&
2344 sd->sd_send_case & SND_RESP) {
2345 struct lnet_peer_ni *gw;
2346 struct lnet_peer *gw_peer;
2348 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw,
2351 CERROR("Can't send response to %s. "
2352 "No route available\n",
2353 libcfs_nid2str(sd->sd_dst_nid));
2354 return -EHOSTUNREACH;
2357 sd->sd_best_lpni = gw;
2358 sd->sd_peer = gw_peer;
2360 return lnet_handle_send(sd);
2364 * Even though the NID for the peer might not be on a local network,
2365 * since the peer is MR there could be other interfaces on the
2366 * local network. In that case we'd still like to prefer the local
2367 * network over the routed network. If we're unable to do that
2368 * then we select the best router among the different routed networks,
2369 * and if the router is MR then we can deal with it as such.
2371 rc = lnet_handle_any_mr_dsta(sd);
2372 if (rc != PASS_THROUGH)
2376 * TODO; One possible enhancement is to run the selection
2377 * algorithm on the peer. However for remote peers the credits are
2378 * not decremented, so we'll be basically going over the peer NIs
2379 * in round robin. An MR router will run the selection algorithm
2380 * on the next-hop interfaces.
2382 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2387 sd->sd_send_case &= ~LOCAL_DST;
2388 sd->sd_send_case |= REMOTE_DST;
2390 sd->sd_peer = gw_peer;
2391 sd->sd_best_lpni = gw_lpni;
2393 return lnet_handle_send(sd);
2397 * Source not specified
2398 * Remote destination
2401 * Must send to the specified peer NID using the same source NID that
2402 * we've used before. If it's the first time to talk to that peer then
2403 * find the source NI and assign it as preferred to that peer
2406 lnet_handle_any_router_nmr_dst(struct lnet_send_data *sd)
2409 struct lnet_peer_ni *gw_lpni = NULL;
2410 struct lnet_peer *gw_peer = NULL;
2413 * Let's set if we have a preferred NI to talk to this NMR peer
2415 sd->sd_best_ni = lnet_find_existing_preferred_best_ni(sd);
2418 * find the router and that'll find the best NI if we didn't find
2421 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2427 * set the best_ni we've chosen as the preferred one for
2430 lnet_set_non_mr_pref_nid(sd);
2432 /* we'll be sending to the gw */
2433 sd->sd_best_lpni = gw_lpni;
2434 sd->sd_peer = gw_peer;
2436 return lnet_handle_send(sd);
2440 lnet_handle_send_case_locked(struct lnet_send_data *sd)
2443 * turn off the SND_RESP bit.
2444 * It will be checked in the case handling
2446 __u32 send_case = sd->sd_send_case &= ~SND_RESP ;
2448 CDEBUG(D_NET, "Source %s%s to %s %s %s destination\n",
2449 (send_case & SRC_SPEC) ? "Specified: " : "ANY",
2450 (send_case & SRC_SPEC) ? libcfs_nid2str(sd->sd_src_nid) : "",
2451 (send_case & MR_DST) ? "MR: " : "NMR: ",
2452 libcfs_nid2str(sd->sd_dst_nid),
2453 (send_case & LOCAL_DST) ? "local" : "routed");
2455 switch (send_case) {
2457 * For all cases where the source is specified, we should always
2458 * use the destination NID, whether it's an MR destination or not,
2459 * since we're continuing a series of related messages for the
2462 case SRC_SPEC_LOCAL_NMR_DST:
2463 return lnet_handle_spec_local_nmr_dst(sd);
2464 case SRC_SPEC_LOCAL_MR_DST:
2465 return lnet_handle_spec_local_mr_dst(sd);
2466 case SRC_SPEC_ROUTER_NMR_DST:
2467 case SRC_SPEC_ROUTER_MR_DST:
2468 return lnet_handle_spec_router_dst(sd);
2469 case SRC_ANY_LOCAL_NMR_DST:
2470 return lnet_handle_any_local_nmr_dst(sd);
2471 case SRC_ANY_LOCAL_MR_DST:
2472 case SRC_ANY_ROUTER_MR_DST:
2473 return lnet_handle_any_mr_dst(sd);
2474 case SRC_ANY_ROUTER_NMR_DST:
2475 return lnet_handle_any_router_nmr_dst(sd);
2477 CERROR("Unknown send case\n");
2483 lnet_select_pathway(lnet_nid_t src_nid, lnet_nid_t dst_nid,
2484 struct lnet_msg *msg, lnet_nid_t rtr_nid)
2486 struct lnet_peer_ni *lpni;
2487 struct lnet_peer *peer;
2488 struct lnet_send_data send_data;
2491 __u32 send_case = 0;
2493 memset(&send_data, 0, sizeof(send_data));
2496 * get an initial CPT to use for locking. The idea here is not to
2497 * serialize the calls to select_pathway, so that as many
2498 * operations can run concurrently as possible. To do that we use
2499 * the CPT where this call is being executed. Later on when we
2500 * determine the CPT to use in lnet_message_commit, we switch the
2501 * lock and check if there was any configuration change. If none,
2502 * then we proceed, if there is, then we restart the operation.
2504 cpt = lnet_net_lock_current();
2506 md_cpt = lnet_cpt_of_md(msg->msg_md, msg->msg_offset);
2507 if (md_cpt == CFS_CPT_ANY)
2513 * If we're being asked to send to the loopback interface, there
2514 * is no need to go through any selection. We can just shortcut
2515 * the entire process and send over lolnd
2517 if (LNET_NETTYP(LNET_NIDNET(dst_nid)) == LOLND) {
2518 /* No send credit hassles with LOLND */
2519 lnet_ni_addref_locked(the_lnet.ln_loni, cpt);
2520 msg->msg_hdr.dest_nid = cpu_to_le64(the_lnet.ln_loni->ni_nid);
2521 if (!msg->msg_routing)
2522 msg->msg_hdr.src_nid =
2523 cpu_to_le64(the_lnet.ln_loni->ni_nid);
2524 msg->msg_target.nid = the_lnet.ln_loni->ni_nid;
2525 lnet_msg_commit(msg, cpt);
2526 msg->msg_txni = the_lnet.ln_loni;
2527 lnet_net_unlock(cpt);
2529 return LNET_CREDIT_OK;
2533 * find an existing peer_ni, or create one and mark it as having been
2534 * created due to network traffic. This call will create the
2535 * peer->peer_net->peer_ni tree.
2537 lpni = lnet_nid2peerni_locked(dst_nid, LNET_NID_ANY, cpt);
2539 lnet_net_unlock(cpt);
2540 return PTR_ERR(lpni);
2544 * Cache the original src_nid. If we need to resend the message
2545 * then we'll need to know whether the src_nid was originally
2546 * specified for this message. If it was originally specified,
2547 * then we need to keep using the same src_nid since it's
2548 * continuing the same sequence of messages.
2550 msg->msg_src_nid_param = src_nid;
2553 * Now that we have a peer_ni, check if we want to discover
2554 * the peer. Traffic to the LNET_RESERVED_PORTAL should not
2555 * trigger discovery.
2557 peer = lpni->lpni_peer_net->lpn_peer;
2558 if (lnet_msg_discovery(msg) && !lnet_peer_is_uptodate(peer)) {
2559 lnet_nid_t primary_nid;
2560 rc = lnet_discover_peer_locked(lpni, cpt, false);
2562 lnet_peer_ni_decref_locked(lpni);
2563 lnet_net_unlock(cpt);
2566 /* The peer may have changed. */
2567 peer = lpni->lpni_peer_net->lpn_peer;
2568 /* queue message and return */
2569 msg->msg_rtr_nid_param = rtr_nid;
2570 msg->msg_sending = 0;
2571 list_add_tail(&msg->msg_list, &peer->lp_dc_pendq);
2572 lnet_peer_ni_decref_locked(lpni);
2573 primary_nid = peer->lp_primary_nid;
2574 lnet_net_unlock(cpt);
2576 CDEBUG(D_NET, "%s pending discovery\n",
2577 libcfs_nid2str(primary_nid));
2579 return LNET_DC_WAIT;
2581 lnet_peer_ni_decref_locked(lpni);
2584 * Identify the different send cases
2586 if (src_nid == LNET_NID_ANY)
2587 send_case |= SRC_ANY;
2589 send_case |= SRC_SPEC;
2591 if (lnet_get_net_locked(LNET_NIDNET(dst_nid)))
2592 send_case |= LOCAL_DST;
2594 send_case |= REMOTE_DST;
2597 * if this is a non-MR peer or if we're recovering a peer ni then
2598 * let's consider this an NMR case so we can hit the destination
2601 if (!lnet_peer_is_multi_rail(peer) || msg->msg_recovery)
2602 send_case |= NMR_DST;
2604 send_case |= MR_DST;
2606 if (msg->msg_type == LNET_MSG_REPLY ||
2607 msg->msg_type == LNET_MSG_ACK)
2608 send_case |= SND_RESP;
2610 /* assign parameters to the send_data */
2611 send_data.sd_msg = msg;
2612 send_data.sd_rtr_nid = rtr_nid;
2613 send_data.sd_src_nid = src_nid;
2614 send_data.sd_dst_nid = dst_nid;
2615 send_data.sd_best_lpni = lpni;
2617 * keep a pointer to the final destination in case we're going to
2618 * route, so we'll need to access it later
2620 send_data.sd_final_dst_lpni = lpni;
2621 send_data.sd_peer = peer;
2622 send_data.sd_md_cpt = md_cpt;
2623 send_data.sd_cpt = cpt;
2624 send_data.sd_send_case = send_case;
2626 rc = lnet_handle_send_case_locked(&send_data);
2629 * Update the local cpt since send_data.sd_cpt might've been
2630 * updated as a result of calling lnet_handle_send_case_locked().
2632 cpt = send_data.sd_cpt;
2634 if (rc == REPEAT_SEND)
2637 lnet_net_unlock(cpt);
2643 lnet_send(lnet_nid_t src_nid, struct lnet_msg *msg, lnet_nid_t rtr_nid)
2645 lnet_nid_t dst_nid = msg->msg_target.nid;
2649 * NB: rtr_nid is set to LNET_NID_ANY for all current use-cases,
2650 * but we might want to use pre-determined router for ACK/REPLY
2653 /* NB: ni != NULL == interface pre-determined (ACK/REPLY) */
2654 LASSERT(msg->msg_txpeer == NULL);
2655 LASSERT(msg->msg_txni == NULL);
2656 LASSERT(!msg->msg_sending);
2657 LASSERT(!msg->msg_target_is_router);
2658 LASSERT(!msg->msg_receiving);
2660 msg->msg_sending = 1;
2662 LASSERT(!msg->msg_tx_committed);
2664 rc = lnet_select_pathway(src_nid, dst_nid, msg, rtr_nid);
2668 if (rc == LNET_CREDIT_OK)
2669 lnet_ni_send(msg->msg_txni, msg);
2671 /* rc == LNET_CREDIT_OK or LNET_CREDIT_WAIT or LNET_DC_WAIT */
2675 enum lnet_mt_event_type {
2676 MT_TYPE_LOCAL_NI = 0,
2680 struct lnet_mt_event_info {
2681 enum lnet_mt_event_type mt_type;
2685 /* called with res_lock held */
2687 lnet_detach_rsp_tracker(struct lnet_libmd *md, int cpt)
2689 struct lnet_rsp_tracker *rspt;
2692 * msg has a refcount on the MD so the MD is not going away.
2693 * The rspt queue for the cpt is protected by
2694 * the lnet_net_lock(cpt). cpt is the cpt of the MD cookie.
2696 if (!md->md_rspt_ptr)
2699 rspt = md->md_rspt_ptr;
2700 md->md_rspt_ptr = NULL;
2703 LASSERT(rspt->rspt_cpt == cpt);
2706 * invalidate the handle to indicate that a response has been
2707 * received, which will then lead the monitor thread to clean up
2710 LNetInvalidateMDHandle(&rspt->rspt_mdh);
2714 lnet_finalize_expired_responses(bool force)
2716 struct lnet_libmd *md;
2717 struct list_head local_queue;
2718 struct lnet_rsp_tracker *rspt, *tmp;
2721 if (the_lnet.ln_mt_rstq == NULL)
2724 cfs_cpt_for_each(i, lnet_cpt_table()) {
2725 INIT_LIST_HEAD(&local_queue);
2728 if (!the_lnet.ln_mt_rstq[i]) {
2732 list_splice_init(the_lnet.ln_mt_rstq[i], &local_queue);
2735 list_for_each_entry_safe(rspt, tmp, &local_queue, rspt_on_list) {
2737 * The rspt mdh will be invalidated when a response
2738 * is received or whenever we want to discard the
2739 * block the monitor thread will walk the queue
2740 * and clean up any rsts with an invalid mdh.
2741 * The monitor thread will walk the queue until
2742 * the first unexpired rspt block. This means that
2743 * some rspt blocks which received their
2744 * corresponding responses will linger in the
2745 * queue until they are cleaned up eventually.
2748 if (LNetMDHandleIsInvalid(rspt->rspt_mdh)) {
2750 list_del_init(&rspt->rspt_on_list);
2751 lnet_rspt_free(rspt, i);
2755 if (ktime_compare(ktime_get(), rspt->rspt_deadline) >= 0 ||
2757 struct lnet_peer_ni *lpni;
2760 md = lnet_handle2md(&rspt->rspt_mdh);
2762 LNetInvalidateMDHandle(&rspt->rspt_mdh);
2764 list_del_init(&rspt->rspt_on_list);
2765 lnet_rspt_free(rspt, i);
2768 LASSERT(md->md_rspt_ptr == rspt);
2769 md->md_rspt_ptr = NULL;
2773 the_lnet.ln_counters[i]->lct_health.lch_response_timeout_count++;
2776 list_del_init(&rspt->rspt_on_list);
2778 nid = rspt->rspt_next_hop_nid;
2780 CNETERR("Response timed out: md = %p: nid = %s\n",
2781 md, libcfs_nid2str(nid));
2782 LNetMDUnlink(rspt->rspt_mdh);
2783 lnet_rspt_free(rspt, i);
2786 * If there is a timeout on the response
2787 * from the next hop decrement its health
2788 * value so that we don't use it
2791 lpni = lnet_find_peer_ni_locked(nid);
2793 lnet_handle_remote_failure_locked(lpni);
2794 lnet_peer_ni_decref_locked(lpni);
2804 if (!list_empty(&local_queue))
2805 list_splice(&local_queue, the_lnet.ln_mt_rstq[i]);
2811 lnet_resend_pending_msgs_locked(struct list_head *resendq, int cpt)
2813 struct lnet_msg *msg;
2815 while (!list_empty(resendq)) {
2816 struct lnet_peer_ni *lpni;
2818 msg = list_entry(resendq->next, struct lnet_msg,
2821 list_del_init(&msg->msg_list);
2823 lpni = lnet_find_peer_ni_locked(msg->msg_hdr.dest_nid);
2825 lnet_net_unlock(cpt);
2826 CERROR("Expected that a peer is already created for %s\n",
2827 libcfs_nid2str(msg->msg_hdr.dest_nid));
2828 msg->msg_no_resend = true;
2829 lnet_finalize(msg, -EFAULT);
2832 struct lnet_peer *peer;
2834 lnet_nid_t src_nid = LNET_NID_ANY;
2837 * if this message is not being routed and the
2838 * peer is non-MR then we must use the same
2839 * src_nid that was used in the original send.
2840 * Otherwise if we're routing the message (IE
2841 * we're a router) then we can use any of our
2842 * local interfaces. It doesn't matter to the
2843 * final destination.
2845 peer = lpni->lpni_peer_net->lpn_peer;
2846 if (!msg->msg_routing &&
2847 !lnet_peer_is_multi_rail(peer))
2848 src_nid = le64_to_cpu(msg->msg_hdr.src_nid);
2851 * If we originally specified a src NID, then we
2852 * must attempt to reuse it in the resend as well.
2854 if (msg->msg_src_nid_param != LNET_NID_ANY)
2855 src_nid = msg->msg_src_nid_param;
2856 lnet_peer_ni_decref_locked(lpni);
2858 lnet_net_unlock(cpt);
2859 CDEBUG(D_NET, "resending %s->%s: %s recovery %d try# %d\n",
2860 libcfs_nid2str(src_nid),
2861 libcfs_id2str(msg->msg_target),
2862 lnet_msgtyp2str(msg->msg_type),
2864 msg->msg_retry_count);
2865 rc = lnet_send(src_nid, msg, LNET_NID_ANY);
2867 CERROR("Error sending %s to %s: %d\n",
2868 lnet_msgtyp2str(msg->msg_type),
2869 libcfs_id2str(msg->msg_target), rc);
2870 msg->msg_no_resend = true;
2871 lnet_finalize(msg, rc);
2875 the_lnet.ln_counters[cpt]->lct_health.lch_resend_count++;
2881 lnet_resend_pending_msgs(void)
2885 cfs_cpt_for_each(i, lnet_cpt_table()) {
2887 lnet_resend_pending_msgs_locked(the_lnet.ln_mt_resendqs[i], i);
2892 /* called with cpt and ni_lock held */
2894 lnet_unlink_ni_recovery_mdh_locked(struct lnet_ni *ni, int cpt, bool force)
2896 struct lnet_handle_md recovery_mdh;
2898 LNetInvalidateMDHandle(&recovery_mdh);
2900 if (ni->ni_recovery_state & LNET_NI_RECOVERY_PENDING ||
2902 recovery_mdh = ni->ni_ping_mdh;
2903 LNetInvalidateMDHandle(&ni->ni_ping_mdh);
2906 lnet_net_unlock(cpt);
2907 if (!LNetMDHandleIsInvalid(recovery_mdh))
2908 LNetMDUnlink(recovery_mdh);
2914 lnet_recover_local_nis(void)
2916 struct lnet_mt_event_info *ev_info;
2917 struct list_head processed_list;
2918 struct list_head local_queue;
2919 struct lnet_handle_md mdh;
2920 struct lnet_ni *tmp;
2926 INIT_LIST_HEAD(&local_queue);
2927 INIT_LIST_HEAD(&processed_list);
2930 * splice the recovery queue on a local queue. We will iterate
2931 * through the local queue and update it as needed. Once we're
2932 * done with the traversal, we'll splice the local queue back on
2933 * the head of the ln_mt_localNIRecovq. Any newly added local NIs
2934 * will be traversed in the next iteration.
2937 list_splice_init(&the_lnet.ln_mt_localNIRecovq,
2941 list_for_each_entry_safe(ni, tmp, &local_queue, ni_recovery) {
2943 * if an NI is being deleted or it is now healthy, there
2944 * is no need to keep it around in the recovery queue.
2945 * The monitor thread is the only thread responsible for
2946 * removing the NI from the recovery queue.
2947 * Multiple threads can be adding NIs to the recovery
2950 healthv = atomic_read(&ni->ni_healthv);
2954 if (ni->ni_state != LNET_NI_STATE_ACTIVE ||
2955 healthv == LNET_MAX_HEALTH_VALUE) {
2956 list_del_init(&ni->ni_recovery);
2957 lnet_unlink_ni_recovery_mdh_locked(ni, 0, false);
2959 lnet_ni_decref_locked(ni, 0);
2965 * if the local NI failed recovery we must unlink the md.
2966 * But we want to keep the local_ni on the recovery queue
2967 * so we can continue the attempts to recover it.
2969 if (ni->ni_recovery_state & LNET_NI_RECOVERY_FAILED) {
2970 lnet_unlink_ni_recovery_mdh_locked(ni, 0, true);
2971 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_FAILED;
2978 CDEBUG(D_NET, "attempting to recover local ni: %s\n",
2979 libcfs_nid2str(ni->ni_nid));
2982 if (!(ni->ni_recovery_state & LNET_NI_RECOVERY_PENDING)) {
2983 ni->ni_recovery_state |= LNET_NI_RECOVERY_PENDING;
2986 LIBCFS_ALLOC(ev_info, sizeof(*ev_info));
2988 CERROR("out of memory. Can't recover %s\n",
2989 libcfs_nid2str(ni->ni_nid));
2991 ni->ni_recovery_state &=
2992 ~LNET_NI_RECOVERY_PENDING;
2997 mdh = ni->ni_ping_mdh;
2999 * Invalidate the ni mdh in case it's deleted.
3000 * We'll unlink the mdh in this case below.
3002 LNetInvalidateMDHandle(&ni->ni_ping_mdh);
3006 * remove the NI from the local queue and drop the
3007 * reference count to it while we're recovering
3008 * it. The reason for that, is that the NI could
3009 * be deleted, and the way the code is structured
3010 * is if we don't drop the NI, then the deletion
3011 * code will enter a loop waiting for the
3012 * reference count to be removed while holding the
3013 * ln_mutex_lock(). When we look up the peer to
3014 * send to in lnet_select_pathway() we will try to
3015 * lock the ln_mutex_lock() as well, leading to
3016 * a deadlock. By dropping the refcount and
3017 * removing it from the list, we allow for the NI
3018 * to be removed, then we use the cached NID to
3019 * look it up again. If it's gone, then we just
3020 * continue examining the rest of the queue.
3023 list_del_init(&ni->ni_recovery);
3024 lnet_ni_decref_locked(ni, 0);
3027 ev_info->mt_type = MT_TYPE_LOCAL_NI;
3028 ev_info->mt_nid = nid;
3029 rc = lnet_send_ping(nid, &mdh, LNET_INTERFACES_MIN,
3030 ev_info, the_lnet.ln_mt_eqh, true);
3031 /* lookup the nid again */
3033 ni = lnet_nid2ni_locked(nid, 0);
3036 * the NI has been deleted when we dropped
3044 * Same note as in lnet_recover_peer_nis(). When
3045 * we're sending the ping, the NI is free to be
3046 * deleted or manipulated. By this point it
3047 * could've been added back on the recovery queue,
3048 * and a refcount taken on it.
3049 * So we can't just add it blindly again or we'll
3050 * corrupt the queue. We must check under lock if
3051 * it's not on any list and if not then add it
3052 * to the processed list, which will eventually be
3053 * spliced back on to the recovery queue.
3055 ni->ni_ping_mdh = mdh;
3056 if (list_empty(&ni->ni_recovery)) {
3057 list_add_tail(&ni->ni_recovery, &processed_list);
3058 lnet_ni_addref_locked(ni, 0);
3064 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_PENDING;
3070 * put back the remaining NIs on the ln_mt_localNIRecovq to be
3071 * reexamined in the next iteration.
3073 list_splice_init(&processed_list, &local_queue);
3075 list_splice(&local_queue, &the_lnet.ln_mt_localNIRecovq);
3079 static struct list_head **
3080 lnet_create_array_of_queues(void)
3082 struct list_head **qs;
3083 struct list_head *q;
3086 qs = cfs_percpt_alloc(lnet_cpt_table(),
3087 sizeof(struct list_head));
3089 CERROR("Failed to allocate queues\n");
3093 cfs_percpt_for_each(q, i, qs)
3100 lnet_resendqs_create(void)
3102 struct list_head **resendqs;
3103 resendqs = lnet_create_array_of_queues();
3108 lnet_net_lock(LNET_LOCK_EX);
3109 the_lnet.ln_mt_resendqs = resendqs;
3110 lnet_net_unlock(LNET_LOCK_EX);
3116 lnet_clean_local_ni_recoveryq(void)
3120 /* This is only called when the monitor thread has stopped */
3123 while (!list_empty(&the_lnet.ln_mt_localNIRecovq)) {
3124 ni = list_entry(the_lnet.ln_mt_localNIRecovq.next,
3125 struct lnet_ni, ni_recovery);
3126 list_del_init(&ni->ni_recovery);
3128 lnet_unlink_ni_recovery_mdh_locked(ni, 0, true);
3130 lnet_ni_decref_locked(ni, 0);
3137 lnet_unlink_lpni_recovery_mdh_locked(struct lnet_peer_ni *lpni, int cpt,
3140 struct lnet_handle_md recovery_mdh;
3142 LNetInvalidateMDHandle(&recovery_mdh);
3144 if (lpni->lpni_state & LNET_PEER_NI_RECOVERY_PENDING || force) {
3145 recovery_mdh = lpni->lpni_recovery_ping_mdh;
3146 LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
3148 spin_unlock(&lpni->lpni_lock);
3149 lnet_net_unlock(cpt);
3150 if (!LNetMDHandleIsInvalid(recovery_mdh))
3151 LNetMDUnlink(recovery_mdh);
3153 spin_lock(&lpni->lpni_lock);
3157 lnet_clean_peer_ni_recoveryq(void)
3159 struct lnet_peer_ni *lpni, *tmp;
3161 lnet_net_lock(LNET_LOCK_EX);
3163 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_mt_peerNIRecovq,
3165 list_del_init(&lpni->lpni_recovery);
3166 spin_lock(&lpni->lpni_lock);
3167 lnet_unlink_lpni_recovery_mdh_locked(lpni, LNET_LOCK_EX, true);
3168 spin_unlock(&lpni->lpni_lock);
3169 lnet_peer_ni_decref_locked(lpni);
3172 lnet_net_unlock(LNET_LOCK_EX);
3176 lnet_clean_resendqs(void)
3178 struct lnet_msg *msg, *tmp;
3179 struct list_head msgs;
3182 INIT_LIST_HEAD(&msgs);
3184 cfs_cpt_for_each(i, lnet_cpt_table()) {
3186 list_splice_init(the_lnet.ln_mt_resendqs[i], &msgs);
3188 list_for_each_entry_safe(msg, tmp, &msgs, msg_list) {
3189 list_del_init(&msg->msg_list);
3190 msg->msg_no_resend = true;
3191 lnet_finalize(msg, -ESHUTDOWN);
3195 cfs_percpt_free(the_lnet.ln_mt_resendqs);
3199 lnet_recover_peer_nis(void)
3201 struct lnet_mt_event_info *ev_info;
3202 struct list_head processed_list;
3203 struct list_head local_queue;
3204 struct lnet_handle_md mdh;
3205 struct lnet_peer_ni *lpni;
3206 struct lnet_peer_ni *tmp;
3211 INIT_LIST_HEAD(&local_queue);
3212 INIT_LIST_HEAD(&processed_list);
3215 * Always use cpt 0 for locking across all interactions with
3216 * ln_mt_peerNIRecovq
3219 list_splice_init(&the_lnet.ln_mt_peerNIRecovq,
3223 list_for_each_entry_safe(lpni, tmp, &local_queue,
3226 * The same protection strategy is used here as is in the
3227 * local recovery case.
3230 healthv = atomic_read(&lpni->lpni_healthv);
3231 spin_lock(&lpni->lpni_lock);
3232 if (lpni->lpni_state & LNET_PEER_NI_DELETING ||
3233 healthv == LNET_MAX_HEALTH_VALUE) {
3234 list_del_init(&lpni->lpni_recovery);
3235 lnet_unlink_lpni_recovery_mdh_locked(lpni, 0, false);
3236 spin_unlock(&lpni->lpni_lock);
3237 lnet_peer_ni_decref_locked(lpni);
3243 * If the peer NI has failed recovery we must unlink the
3244 * md. But we want to keep the peer ni on the recovery
3245 * queue so we can try to continue recovering it
3247 if (lpni->lpni_state & LNET_PEER_NI_RECOVERY_FAILED) {
3248 lnet_unlink_lpni_recovery_mdh_locked(lpni, 0, true);
3249 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_FAILED;
3252 spin_unlock(&lpni->lpni_lock);
3256 * NOTE: we're racing with peer deletion from user space.
3257 * It's possible that a peer is deleted after we check its
3258 * state. In this case the recovery can create a new peer
3260 spin_lock(&lpni->lpni_lock);
3261 if (!(lpni->lpni_state & LNET_PEER_NI_RECOVERY_PENDING) &&
3262 !(lpni->lpni_state & LNET_PEER_NI_DELETING)) {
3263 lpni->lpni_state |= LNET_PEER_NI_RECOVERY_PENDING;
3264 spin_unlock(&lpni->lpni_lock);
3266 LIBCFS_ALLOC(ev_info, sizeof(*ev_info));
3268 CERROR("out of memory. Can't recover %s\n",
3269 libcfs_nid2str(lpni->lpni_nid));
3270 spin_lock(&lpni->lpni_lock);
3271 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3272 spin_unlock(&lpni->lpni_lock);
3276 /* look at the comments in lnet_recover_local_nis() */
3277 mdh = lpni->lpni_recovery_ping_mdh;
3278 LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
3279 nid = lpni->lpni_nid;
3281 list_del_init(&lpni->lpni_recovery);
3282 lnet_peer_ni_decref_locked(lpni);
3285 ev_info->mt_type = MT_TYPE_PEER_NI;
3286 ev_info->mt_nid = nid;
3287 rc = lnet_send_ping(nid, &mdh, LNET_INTERFACES_MIN,
3288 ev_info, the_lnet.ln_mt_eqh, true);
3291 * lnet_find_peer_ni_locked() grabs a refcount for
3292 * us. No need to take it explicitly.
3294 lpni = lnet_find_peer_ni_locked(nid);
3301 lpni->lpni_recovery_ping_mdh = mdh;
3303 * While we're unlocked the lpni could've been
3304 * readded on the recovery queue. In this case we
3305 * don't need to add it to the local queue, since
3306 * it's already on there and the thread that added
3307 * it would've incremented the refcount on the
3308 * peer, which means we need to decref the refcount
3309 * that was implicitly grabbed by find_peer_ni_locked.
3310 * Otherwise, if the lpni is still not on
3311 * the recovery queue, then we'll add it to the
3314 if (list_empty(&lpni->lpni_recovery))
3315 list_add_tail(&lpni->lpni_recovery, &processed_list);
3317 lnet_peer_ni_decref_locked(lpni);
3320 spin_lock(&lpni->lpni_lock);
3322 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3324 spin_unlock(&lpni->lpni_lock);
3327 list_splice_init(&processed_list, &local_queue);
3329 list_splice(&local_queue, &the_lnet.ln_mt_peerNIRecovq);
3334 lnet_monitor_thread(void *arg)
3336 time64_t recovery_timeout = 0;
3337 time64_t rsp_timeout = 0;
3342 * The monitor thread takes care of the following:
3343 * 1. Checks the aliveness of routers
3344 * 2. Checks if there are messages on the resend queue to resend
3346 * 3. Check if there are any NIs on the local recovery queue and
3348 * 4. Checks if there are any NIs on the remote recovery queue
3351 cfs_block_allsigs();
3353 while (the_lnet.ln_mt_state == LNET_MT_STATE_RUNNING) {
3354 now = ktime_get_real_seconds();
3356 if (lnet_router_checker_active())
3357 lnet_check_routers();
3359 lnet_resend_pending_msgs();
3361 if (now >= rsp_timeout) {
3362 lnet_finalize_expired_responses(false);
3363 rsp_timeout = now + (lnet_transaction_timeout / 2);
3366 if (now >= recovery_timeout) {
3367 lnet_recover_local_nis();
3368 lnet_recover_peer_nis();
3369 recovery_timeout = now + lnet_recovery_interval;
3373 * TODO do we need to check if we should sleep without
3374 * timeout? Technically, an active system will always
3375 * have messages in flight so this check will always
3376 * evaluate to false. And on an idle system do we care
3377 * if we wake up every 1 second? Although, we've seen
3378 * cases where we get a complaint that an idle thread
3379 * is waking up unnecessarily.
3381 interval = min(lnet_recovery_interval,
3382 lnet_transaction_timeout / 2);
3383 wait_event_interruptible_timeout(the_lnet.ln_mt_waitq,
3385 cfs_time_seconds(interval));
3388 /* clean up the router checker */
3389 lnet_prune_rc_data(1);
3392 the_lnet.ln_mt_state = LNET_MT_STATE_SHUTDOWN;
3394 /* signal that the monitor thread is exiting */
3395 up(&the_lnet.ln_mt_signal);
3403 * Returns == 0 if success
3404 * Returns > 0 if LNetMDBind or prior fails
3405 * Returns < 0 if LNetGet fails
3408 lnet_send_ping(lnet_nid_t dest_nid,
3409 struct lnet_handle_md *mdh, int nnis,
3410 void *user_data, struct lnet_handle_eq eqh, bool recovery)
3412 struct lnet_md md = { NULL };
3413 struct lnet_process_id id;
3414 struct lnet_ping_buffer *pbuf;
3417 if (dest_nid == LNET_NID_ANY) {
3422 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
3428 /* initialize md content */
3429 md.start = &pbuf->pb_info;
3430 md.length = LNET_PING_INFO_SIZE(nnis);
3431 md.threshold = 2; /* GET/REPLY */
3433 md.options = LNET_MD_TRUNCATE;
3434 md.user_ptr = user_data;
3437 rc = LNetMDBind(md, LNET_UNLINK, mdh);
3439 lnet_ping_buffer_decref(pbuf);
3440 CERROR("Can't bind MD: %d\n", rc);
3441 rc = -rc; /* change the rc to positive */
3444 id.pid = LNET_PID_LUSTRE;
3447 rc = LNetGet(LNET_NID_ANY, *mdh, id,
3448 LNET_RESERVED_PORTAL,
3449 LNET_PROTO_PING_MATCHBITS, 0, recovery);
3452 goto fail_unlink_md;
3458 LNetInvalidateMDHandle(mdh);
3464 lnet_handle_recovery_reply(struct lnet_mt_event_info *ev_info,
3465 int status, bool unlink_event)
3467 lnet_nid_t nid = ev_info->mt_nid;
3469 if (ev_info->mt_type == MT_TYPE_LOCAL_NI) {
3473 ni = lnet_nid2ni_locked(nid, 0);
3479 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_PENDING;
3481 ni->ni_recovery_state |= LNET_NI_RECOVERY_FAILED;
3486 CERROR("local NI (%s) recovery failed with %d\n",
3487 libcfs_nid2str(nid), status);
3491 * need to increment healthv for the ni here, because in
3492 * the lnet_finalize() path we don't have access to this
3493 * NI. And in order to get access to it, we'll need to
3494 * carry forward too much information.
3495 * In the peer case, it'll naturally be incremented
3498 lnet_inc_healthv(&ni->ni_healthv);
3500 struct lnet_peer_ni *lpni;
3503 cpt = lnet_net_lock_current();
3504 lpni = lnet_find_peer_ni_locked(nid);
3506 lnet_net_unlock(cpt);
3509 spin_lock(&lpni->lpni_lock);
3510 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3512 lpni->lpni_state |= LNET_PEER_NI_RECOVERY_FAILED;
3513 spin_unlock(&lpni->lpni_lock);
3514 lnet_peer_ni_decref_locked(lpni);
3515 lnet_net_unlock(cpt);
3518 CERROR("peer NI (%s) recovery failed with %d\n",
3519 libcfs_nid2str(nid), status);
3524 lnet_mt_event_handler(struct lnet_event *event)
3526 struct lnet_mt_event_info *ev_info = event->md.user_ptr;
3527 struct lnet_ping_buffer *pbuf;
3529 /* TODO: remove assert */
3530 LASSERT(event->type == LNET_EVENT_REPLY ||
3531 event->type == LNET_EVENT_SEND ||
3532 event->type == LNET_EVENT_UNLINK);
3534 CDEBUG(D_NET, "Received event: %d status: %d\n", event->type,
3537 switch (event->type) {
3538 case LNET_EVENT_UNLINK:
3539 CDEBUG(D_NET, "%s recovery ping unlinked\n",
3540 libcfs_nid2str(ev_info->mt_nid));
3541 case LNET_EVENT_REPLY:
3542 lnet_handle_recovery_reply(ev_info, event->status,
3543 event->type == LNET_EVENT_UNLINK);
3545 case LNET_EVENT_SEND:
3546 CDEBUG(D_NET, "%s recovery message sent %s:%d\n",
3547 libcfs_nid2str(ev_info->mt_nid),
3548 (event->status) ? "unsuccessfully" :
3549 "successfully", event->status);
3552 CERROR("Unexpected event: %d\n", event->type);
3555 if (event->unlinked) {
3556 LIBCFS_FREE(ev_info, sizeof(*ev_info));
3557 pbuf = LNET_PING_INFO_TO_BUFFER(event->md.start);
3558 lnet_ping_buffer_decref(pbuf);
3563 lnet_rsp_tracker_create(void)
3565 struct list_head **rstqs;
3566 rstqs = lnet_create_array_of_queues();
3571 the_lnet.ln_mt_rstq = rstqs;
3577 lnet_rsp_tracker_clean(void)
3579 lnet_finalize_expired_responses(true);
3581 cfs_percpt_free(the_lnet.ln_mt_rstq);
3582 the_lnet.ln_mt_rstq = NULL;
3585 int lnet_monitor_thr_start(void)
3588 struct task_struct *task;
3590 if (the_lnet.ln_mt_state != LNET_MT_STATE_SHUTDOWN)
3593 rc = lnet_resendqs_create();
3597 rc = lnet_rsp_tracker_create();
3601 /* Pre monitor thread start processing */
3602 rc = lnet_router_pre_mt_start();
3606 sema_init(&the_lnet.ln_mt_signal, 0);
3608 the_lnet.ln_mt_state = LNET_MT_STATE_RUNNING;
3609 task = kthread_run(lnet_monitor_thread, NULL, "monitor_thread");
3612 CERROR("Can't start monitor thread: %d\n", rc);
3616 /* post monitor thread start processing */
3617 lnet_router_post_mt_start();
3622 the_lnet.ln_mt_state = LNET_MT_STATE_STOPPING;
3623 /* block until event callback signals exit */
3624 down(&the_lnet.ln_mt_signal);
3626 lnet_router_cleanup();
3628 the_lnet.ln_mt_state = LNET_MT_STATE_SHUTDOWN;
3629 lnet_rsp_tracker_clean();
3630 lnet_clean_local_ni_recoveryq();
3631 lnet_clean_peer_ni_recoveryq();
3632 lnet_clean_resendqs();
3633 LNetInvalidateEQHandle(&the_lnet.ln_mt_eqh);
3636 lnet_rsp_tracker_clean();
3637 lnet_clean_local_ni_recoveryq();
3638 lnet_clean_peer_ni_recoveryq();
3639 lnet_clean_resendqs();
3643 void lnet_monitor_thr_stop(void)
3645 if (the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN)
3648 LASSERT(the_lnet.ln_mt_state == LNET_MT_STATE_RUNNING);
3649 the_lnet.ln_mt_state = LNET_MT_STATE_STOPPING;
3651 /* tell the monitor thread that we're shutting down */
3652 wake_up(&the_lnet.ln_mt_waitq);
3654 /* block until monitor thread signals that it's done */
3655 down(&the_lnet.ln_mt_signal);
3656 LASSERT(the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN);
3658 /* perform cleanup tasks */
3659 lnet_router_cleanup();
3660 lnet_rsp_tracker_clean();
3661 lnet_clean_local_ni_recoveryq();
3662 lnet_clean_peer_ni_recoveryq();
3663 lnet_clean_resendqs();
3669 lnet_drop_message(struct lnet_ni *ni, int cpt, void *private, unsigned int nob,
3673 lnet_incr_stats(&ni->ni_stats, msg_type, LNET_STATS_TYPE_DROP);
3674 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
3675 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length += nob;
3676 lnet_net_unlock(cpt);
3678 lnet_ni_recv(ni, private, NULL, 0, 0, 0, nob);
3682 lnet_recv_put(struct lnet_ni *ni, struct lnet_msg *msg)
3684 struct lnet_hdr *hdr = &msg->msg_hdr;
3686 if (msg->msg_wanted != 0)
3687 lnet_setpayloadbuffer(msg);
3689 lnet_build_msg_event(msg, LNET_EVENT_PUT);
3691 /* Must I ACK? If so I'll grab the ack_wmd out of the header and put
3692 * it back into the ACK during lnet_finalize() */
3693 msg->msg_ack = (!lnet_is_wire_handle_none(&hdr->msg.put.ack_wmd) &&
3694 (msg->msg_md->md_options & LNET_MD_ACK_DISABLE) == 0);
3696 lnet_ni_recv(ni, msg->msg_private, msg, msg->msg_rx_delayed,
3697 msg->msg_offset, msg->msg_wanted, hdr->payload_length);
3701 lnet_parse_put(struct lnet_ni *ni, struct lnet_msg *msg)
3703 struct lnet_hdr *hdr = &msg->msg_hdr;
3704 struct lnet_match_info info;
3708 /* Convert put fields to host byte order */
3709 hdr->msg.put.match_bits = le64_to_cpu(hdr->msg.put.match_bits);
3710 hdr->msg.put.ptl_index = le32_to_cpu(hdr->msg.put.ptl_index);
3711 hdr->msg.put.offset = le32_to_cpu(hdr->msg.put.offset);
3713 /* Primary peer NID. */
3714 info.mi_id.nid = msg->msg_initiator;
3715 info.mi_id.pid = hdr->src_pid;
3716 info.mi_opc = LNET_MD_OP_PUT;
3717 info.mi_portal = hdr->msg.put.ptl_index;
3718 info.mi_rlength = hdr->payload_length;
3719 info.mi_roffset = hdr->msg.put.offset;
3720 info.mi_mbits = hdr->msg.put.match_bits;
3721 info.mi_cpt = lnet_cpt_of_nid(msg->msg_initiator, ni);
3723 msg->msg_rx_ready_delay = ni->ni_net->net_lnd->lnd_eager_recv == NULL;
3724 ready_delay = msg->msg_rx_ready_delay;
3727 rc = lnet_ptl_match_md(&info, msg);
3732 case LNET_MATCHMD_OK:
3733 lnet_recv_put(ni, msg);
3736 case LNET_MATCHMD_NONE:
3738 /* no eager_recv or has already called it, should
3739 * have been attached on delayed list */
3742 rc = lnet_ni_eager_recv(ni, msg);
3749 case LNET_MATCHMD_DROP:
3750 CNETERR("Dropping PUT from %s portal %d match %llu"
3751 " offset %d length %d: %d\n",
3752 libcfs_id2str(info.mi_id), info.mi_portal,
3753 info.mi_mbits, info.mi_roffset, info.mi_rlength, rc);
3755 return -ENOENT; /* -ve: OK but no match */
3760 lnet_parse_get(struct lnet_ni *ni, struct lnet_msg *msg, int rdma_get)
3762 struct lnet_match_info info;
3763 struct lnet_hdr *hdr = &msg->msg_hdr;
3764 struct lnet_process_id source_id;
3765 struct lnet_handle_wire reply_wmd;
3768 /* Convert get fields to host byte order */
3769 hdr->msg.get.match_bits = le64_to_cpu(hdr->msg.get.match_bits);
3770 hdr->msg.get.ptl_index = le32_to_cpu(hdr->msg.get.ptl_index);
3771 hdr->msg.get.sink_length = le32_to_cpu(hdr->msg.get.sink_length);
3772 hdr->msg.get.src_offset = le32_to_cpu(hdr->msg.get.src_offset);
3774 source_id.nid = hdr->src_nid;
3775 source_id.pid = hdr->src_pid;
3776 /* Primary peer NID */
3777 info.mi_id.nid = msg->msg_initiator;
3778 info.mi_id.pid = hdr->src_pid;
3779 info.mi_opc = LNET_MD_OP_GET;
3780 info.mi_portal = hdr->msg.get.ptl_index;
3781 info.mi_rlength = hdr->msg.get.sink_length;
3782 info.mi_roffset = hdr->msg.get.src_offset;
3783 info.mi_mbits = hdr->msg.get.match_bits;
3784 info.mi_cpt = lnet_cpt_of_nid(msg->msg_initiator, ni);
3786 rc = lnet_ptl_match_md(&info, msg);
3787 if (rc == LNET_MATCHMD_DROP) {
3788 CNETERR("Dropping GET from %s portal %d match %llu"
3789 " offset %d length %d\n",
3790 libcfs_id2str(info.mi_id), info.mi_portal,
3791 info.mi_mbits, info.mi_roffset, info.mi_rlength);
3792 return -ENOENT; /* -ve: OK but no match */
3795 LASSERT(rc == LNET_MATCHMD_OK);
3797 lnet_build_msg_event(msg, LNET_EVENT_GET);
3799 reply_wmd = hdr->msg.get.return_wmd;
3801 lnet_prep_send(msg, LNET_MSG_REPLY, source_id,
3802 msg->msg_offset, msg->msg_wanted);
3804 msg->msg_hdr.msg.reply.dst_wmd = reply_wmd;
3807 /* The LND completes the REPLY from her recv procedure */
3808 lnet_ni_recv(ni, msg->msg_private, msg, 0,
3809 msg->msg_offset, msg->msg_len, msg->msg_len);
3813 lnet_ni_recv(ni, msg->msg_private, NULL, 0, 0, 0, 0);
3814 msg->msg_receiving = 0;
3816 rc = lnet_send(ni->ni_nid, msg, msg->msg_from);
3818 /* didn't get as far as lnet_ni_send() */
3819 CERROR("%s: Unable to send REPLY for GET from %s: %d\n",
3820 libcfs_nid2str(ni->ni_nid),
3821 libcfs_id2str(info.mi_id), rc);
3823 lnet_finalize(msg, rc);
3830 lnet_parse_reply(struct lnet_ni *ni, struct lnet_msg *msg)
3832 void *private = msg->msg_private;
3833 struct lnet_hdr *hdr = &msg->msg_hdr;
3834 struct lnet_process_id src = {0};
3835 struct lnet_libmd *md;
3840 cpt = lnet_cpt_of_cookie(hdr->msg.reply.dst_wmd.wh_object_cookie);
3843 src.nid = hdr->src_nid;
3844 src.pid = hdr->src_pid;
3846 /* NB handles only looked up by creator (no flips) */
3847 md = lnet_wire_handle2md(&hdr->msg.reply.dst_wmd);
3848 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
3849 CNETERR("%s: Dropping REPLY from %s for %s "
3851 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
3852 (md == NULL) ? "invalid" : "inactive",
3853 hdr->msg.reply.dst_wmd.wh_interface_cookie,
3854 hdr->msg.reply.dst_wmd.wh_object_cookie);
3855 if (md != NULL && md->md_me != NULL)
3856 CERROR("REPLY MD also attached to portal %d\n",
3857 md->md_me->me_portal);
3859 lnet_res_unlock(cpt);
3860 return -ENOENT; /* -ve: OK but no match */
3863 LASSERT(md->md_offset == 0);
3865 rlength = hdr->payload_length;
3866 mlength = MIN(rlength, (int)md->md_length);
3868 if (mlength < rlength &&
3869 (md->md_options & LNET_MD_TRUNCATE) == 0) {
3870 CNETERR("%s: Dropping REPLY from %s length %d "
3871 "for MD %#llx would overflow (%d)\n",
3872 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
3873 rlength, hdr->msg.reply.dst_wmd.wh_object_cookie,
3875 lnet_res_unlock(cpt);
3876 return -ENOENT; /* -ve: OK but no match */
3879 CDEBUG(D_NET, "%s: Reply from %s of length %d/%d into md %#llx\n",
3880 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
3881 mlength, rlength, hdr->msg.reply.dst_wmd.wh_object_cookie);
3883 lnet_msg_attach_md(msg, md, 0, mlength);
3886 lnet_setpayloadbuffer(msg);
3888 lnet_res_unlock(cpt);
3890 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
3892 lnet_ni_recv(ni, private, msg, 0, 0, mlength, rlength);
3897 lnet_parse_ack(struct lnet_ni *ni, struct lnet_msg *msg)
3899 struct lnet_hdr *hdr = &msg->msg_hdr;
3900 struct lnet_process_id src = {0};
3901 struct lnet_libmd *md;
3904 src.nid = hdr->src_nid;
3905 src.pid = hdr->src_pid;
3907 /* Convert ack fields to host byte order */
3908 hdr->msg.ack.match_bits = le64_to_cpu(hdr->msg.ack.match_bits);
3909 hdr->msg.ack.mlength = le32_to_cpu(hdr->msg.ack.mlength);
3911 cpt = lnet_cpt_of_cookie(hdr->msg.ack.dst_wmd.wh_object_cookie);
3914 /* NB handles only looked up by creator (no flips) */
3915 md = lnet_wire_handle2md(&hdr->msg.ack.dst_wmd);
3916 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
3917 /* Don't moan; this is expected */
3919 "%s: Dropping ACK from %s to %s MD %#llx.%#llx\n",
3920 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
3921 (md == NULL) ? "invalid" : "inactive",
3922 hdr->msg.ack.dst_wmd.wh_interface_cookie,
3923 hdr->msg.ack.dst_wmd.wh_object_cookie);
3924 if (md != NULL && md->md_me != NULL)
3925 CERROR("Source MD also attached to portal %d\n",
3926 md->md_me->me_portal);
3928 lnet_res_unlock(cpt);
3929 return -ENOENT; /* -ve! */
3932 CDEBUG(D_NET, "%s: ACK from %s into md %#llx\n",
3933 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
3934 hdr->msg.ack.dst_wmd.wh_object_cookie);
3936 lnet_msg_attach_md(msg, md, 0, 0);
3938 lnet_res_unlock(cpt);
3940 lnet_build_msg_event(msg, LNET_EVENT_ACK);
3942 lnet_ni_recv(ni, msg->msg_private, msg, 0, 0, 0, msg->msg_len);
3947 * \retval LNET_CREDIT_OK If \a msg is forwarded
3948 * \retval LNET_CREDIT_WAIT If \a msg is blocked because w/o buffer
3949 * \retval -ve error code
3952 lnet_parse_forward_locked(struct lnet_ni *ni, struct lnet_msg *msg)
3956 if (!the_lnet.ln_routing)
3959 if (msg->msg_rxpeer->lpni_rtrcredits <= 0 ||
3960 lnet_msg2bufpool(msg)->rbp_credits <= 0) {
3961 if (ni->ni_net->net_lnd->lnd_eager_recv == NULL) {
3962 msg->msg_rx_ready_delay = 1;
3964 lnet_net_unlock(msg->msg_rx_cpt);
3965 rc = lnet_ni_eager_recv(ni, msg);
3966 lnet_net_lock(msg->msg_rx_cpt);
3971 rc = lnet_post_routed_recv_locked(msg, 0);
3976 lnet_parse_local(struct lnet_ni *ni, struct lnet_msg *msg)
3980 switch (msg->msg_type) {
3982 rc = lnet_parse_ack(ni, msg);
3985 rc = lnet_parse_put(ni, msg);
3988 rc = lnet_parse_get(ni, msg, msg->msg_rdma_get);
3990 case LNET_MSG_REPLY:
3991 rc = lnet_parse_reply(ni, msg);
3993 default: /* prevent an unused label if !kernel */
3998 LASSERT(rc == 0 || rc == -ENOENT);
4003 lnet_msgtyp2str (int type)
4012 case LNET_MSG_REPLY:
4014 case LNET_MSG_HELLO:
4017 return ("<UNKNOWN>");
4022 lnet_print_hdr(struct lnet_hdr *hdr)
4024 struct lnet_process_id src = {
4025 .nid = hdr->src_nid,
4026 .pid = hdr->src_pid,
4028 struct lnet_process_id dst = {
4029 .nid = hdr->dest_nid,
4030 .pid = hdr->dest_pid,
4032 char *type_str = lnet_msgtyp2str(hdr->type);
4034 CWARN("P3 Header at %p of type %s\n", hdr, type_str);
4035 CWARN(" From %s\n", libcfs_id2str(src));
4036 CWARN(" To %s\n", libcfs_id2str(dst));
4038 switch (hdr->type) {
4043 CWARN(" Ptl index %d, ack md %#llx.%#llx, "
4044 "match bits %llu\n",
4045 hdr->msg.put.ptl_index,
4046 hdr->msg.put.ack_wmd.wh_interface_cookie,
4047 hdr->msg.put.ack_wmd.wh_object_cookie,
4048 hdr->msg.put.match_bits);
4049 CWARN(" Length %d, offset %d, hdr data %#llx\n",
4050 hdr->payload_length, hdr->msg.put.offset,
4051 hdr->msg.put.hdr_data);
4055 CWARN(" Ptl index %d, return md %#llx.%#llx, "
4056 "match bits %llu\n", hdr->msg.get.ptl_index,
4057 hdr->msg.get.return_wmd.wh_interface_cookie,
4058 hdr->msg.get.return_wmd.wh_object_cookie,
4059 hdr->msg.get.match_bits);
4060 CWARN(" Length %d, src offset %d\n",
4061 hdr->msg.get.sink_length,
4062 hdr->msg.get.src_offset);
4066 CWARN(" dst md %#llx.%#llx, "
4067 "manipulated length %d\n",
4068 hdr->msg.ack.dst_wmd.wh_interface_cookie,
4069 hdr->msg.ack.dst_wmd.wh_object_cookie,
4070 hdr->msg.ack.mlength);
4073 case LNET_MSG_REPLY:
4074 CWARN(" dst md %#llx.%#llx, "
4076 hdr->msg.reply.dst_wmd.wh_interface_cookie,
4077 hdr->msg.reply.dst_wmd.wh_object_cookie,
4078 hdr->payload_length);
4084 lnet_parse(struct lnet_ni *ni, struct lnet_hdr *hdr, lnet_nid_t from_nid,
4085 void *private, int rdma_req)
4090 struct lnet_msg *msg;
4091 lnet_pid_t dest_pid;
4092 lnet_nid_t dest_nid;
4094 struct lnet_peer_ni *lpni;
4095 __u32 payload_length;
4098 LASSERT (!in_interrupt ());
4100 type = le32_to_cpu(hdr->type);
4101 src_nid = le64_to_cpu(hdr->src_nid);
4102 dest_nid = le64_to_cpu(hdr->dest_nid);
4103 dest_pid = le32_to_cpu(hdr->dest_pid);
4104 payload_length = le32_to_cpu(hdr->payload_length);
4106 for_me = (ni->ni_nid == dest_nid);
4107 cpt = lnet_cpt_of_nid(from_nid, ni);
4109 CDEBUG(D_NET, "TRACE: %s(%s) <- %s : %s - %s\n",
4110 libcfs_nid2str(dest_nid),
4111 libcfs_nid2str(ni->ni_nid),
4112 libcfs_nid2str(src_nid),
4113 lnet_msgtyp2str(type),
4114 (for_me) ? "for me" : "routed");
4119 if (payload_length > 0) {
4120 CERROR("%s, src %s: bad %s payload %d (0 expected)\n",
4121 libcfs_nid2str(from_nid),
4122 libcfs_nid2str(src_nid),
4123 lnet_msgtyp2str(type), payload_length);
4129 case LNET_MSG_REPLY:
4130 if (payload_length >
4131 (__u32)(for_me ? LNET_MAX_PAYLOAD : LNET_MTU)) {
4132 CERROR("%s, src %s: bad %s payload %d "
4133 "(%d max expected)\n",
4134 libcfs_nid2str(from_nid),
4135 libcfs_nid2str(src_nid),
4136 lnet_msgtyp2str(type),
4138 for_me ? LNET_MAX_PAYLOAD : LNET_MTU);
4144 CERROR("%s, src %s: Bad message type 0x%x\n",
4145 libcfs_nid2str(from_nid),
4146 libcfs_nid2str(src_nid), type);
4150 if (the_lnet.ln_routing &&
4151 ni->ni_last_alive != ktime_get_real_seconds()) {
4152 /* NB: so far here is the only place to set NI status to "up */
4154 ni->ni_last_alive = ktime_get_real_seconds();
4155 if (ni->ni_status != NULL &&
4156 ni->ni_status->ns_status == LNET_NI_STATUS_DOWN)
4157 ni->ni_status->ns_status = LNET_NI_STATUS_UP;
4161 /* Regard a bad destination NID as a protocol error. Senders should
4162 * know what they're doing; if they don't they're misconfigured, buggy
4163 * or malicious so we chop them off at the knees :) */
4166 if (LNET_NIDNET(dest_nid) == LNET_NIDNET(ni->ni_nid)) {
4167 /* should have gone direct */
4168 CERROR("%s, src %s: Bad dest nid %s "
4169 "(should have been sent direct)\n",
4170 libcfs_nid2str(from_nid),
4171 libcfs_nid2str(src_nid),
4172 libcfs_nid2str(dest_nid));
4176 if (lnet_islocalnid(dest_nid)) {
4177 /* dest is another local NI; sender should have used
4178 * this node's NID on its own network */
4179 CERROR("%s, src %s: Bad dest nid %s "
4180 "(it's my nid but on a different network)\n",
4181 libcfs_nid2str(from_nid),
4182 libcfs_nid2str(src_nid),
4183 libcfs_nid2str(dest_nid));
4187 if (rdma_req && type == LNET_MSG_GET) {
4188 CERROR("%s, src %s: Bad optimized GET for %s "
4189 "(final destination must be me)\n",
4190 libcfs_nid2str(from_nid),
4191 libcfs_nid2str(src_nid),
4192 libcfs_nid2str(dest_nid));
4196 if (!the_lnet.ln_routing) {
4197 CERROR("%s, src %s: Dropping message for %s "
4198 "(routing not enabled)\n",
4199 libcfs_nid2str(from_nid),
4200 libcfs_nid2str(src_nid),
4201 libcfs_nid2str(dest_nid));
4206 /* Message looks OK; we're not going to return an error, so we MUST
4207 * call back lnd_recv() come what may... */
4209 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
4210 fail_peer(src_nid, 0)) { /* shall we now? */
4211 CERROR("%s, src %s: Dropping %s to simulate failure\n",
4212 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4213 lnet_msgtyp2str(type));
4217 if (!list_empty(&the_lnet.ln_drop_rules) &&
4218 lnet_drop_rule_match(hdr, NULL)) {
4219 CDEBUG(D_NET, "%s, src %s, dst %s: Dropping %s to simulate"
4220 "silent message loss\n",
4221 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4222 libcfs_nid2str(dest_nid), lnet_msgtyp2str(type));
4226 if (lnet_drop_asym_route && for_me &&
4227 LNET_NIDNET(src_nid) != LNET_NIDNET(from_nid)) {
4228 struct lnet_net *net;
4229 struct lnet_remotenet *rnet;
4232 /* we are dealing with a routed message,
4233 * so see if route to reach src_nid goes through from_nid
4236 net = lnet_get_net_locked(LNET_NIDNET(ni->ni_nid));
4238 lnet_net_unlock(cpt);
4239 CERROR("net %s not found\n",
4240 libcfs_net2str(LNET_NIDNET(ni->ni_nid)));
4244 rnet = lnet_find_rnet_locked(LNET_NIDNET(src_nid));
4246 struct lnet_peer_ni *gw = NULL;
4247 struct lnet_route *route;
4249 list_for_each_entry(route, &rnet->lrn_routes, lr_list) {
4251 gw = route->lr_gateway;
4252 if (gw->lpni_net != net)
4254 if (gw->lpni_nid == from_nid) {
4260 lnet_net_unlock(cpt);
4262 /* we would not use from_nid to route a message to
4264 * => asymmetric routing detected but forbidden
4266 CERROR("%s, src %s: Dropping asymmetrical route %s\n",
4267 libcfs_nid2str(from_nid),
4268 libcfs_nid2str(src_nid), lnet_msgtyp2str(type));
4273 msg = lnet_msg_alloc();
4275 CERROR("%s, src %s: Dropping %s (out of memory)\n",
4276 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4277 lnet_msgtyp2str(type));
4281 /* msg zeroed in lnet_msg_alloc; i.e. flags all clear,
4282 * pointers NULL etc */
4284 msg->msg_type = type;
4285 msg->msg_private = private;
4286 msg->msg_receiving = 1;
4287 msg->msg_rdma_get = rdma_req;
4288 msg->msg_len = msg->msg_wanted = payload_length;
4289 msg->msg_offset = 0;
4290 msg->msg_hdr = *hdr;
4291 /* for building message event */
4292 msg->msg_from = from_nid;
4294 msg->msg_target.pid = dest_pid;
4295 msg->msg_target.nid = dest_nid;
4296 msg->msg_routing = 1;
4299 /* convert common msg->hdr fields to host byteorder */
4300 msg->msg_hdr.type = type;
4301 msg->msg_hdr.src_nid = src_nid;
4302 msg->msg_hdr.src_pid = le32_to_cpu(msg->msg_hdr.src_pid);
4303 msg->msg_hdr.dest_nid = dest_nid;
4304 msg->msg_hdr.dest_pid = dest_pid;
4305 msg->msg_hdr.payload_length = payload_length;
4309 lpni = lnet_nid2peerni_locked(from_nid, ni->ni_nid, cpt);
4311 lnet_net_unlock(cpt);
4312 CERROR("%s, src %s: Dropping %s "
4313 "(error %ld looking up sender)\n",
4314 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4315 lnet_msgtyp2str(type), PTR_ERR(lpni));
4317 if (rc == -ESHUTDOWN)
4318 /* We are shutting down. Don't do anything more */
4322 msg->msg_rxpeer = lpni;
4324 lnet_ni_addref_locked(ni, cpt);
4325 /* Multi-Rail: Primary NID of source. */
4326 msg->msg_initiator = lnet_peer_primary_nid_locked(src_nid);
4328 if (lnet_isrouter(msg->msg_rxpeer)) {
4329 lnet_peer_set_alive(msg->msg_rxpeer);
4330 if (avoid_asym_router_failure &&
4331 LNET_NIDNET(src_nid) != LNET_NIDNET(from_nid)) {
4332 /* received a remote message from router, update
4333 * remote NI status on this router.
4334 * NB: multi-hop routed message will be ignored.
4336 lnet_router_ni_update_locked(msg->msg_rxpeer,
4337 LNET_NIDNET(src_nid));
4341 lnet_msg_commit(msg, cpt);
4343 /* message delay simulation */
4344 if (unlikely(!list_empty(&the_lnet.ln_delay_rules) &&
4345 lnet_delay_rule_match_locked(hdr, msg))) {
4346 lnet_net_unlock(cpt);
4351 rc = lnet_parse_forward_locked(ni, msg);
4352 lnet_net_unlock(cpt);
4357 if (rc == LNET_CREDIT_OK) {
4358 lnet_ni_recv(ni, msg->msg_private, msg, 0,
4359 0, payload_length, payload_length);
4364 lnet_net_unlock(cpt);
4366 rc = lnet_parse_local(ni, msg);
4372 LASSERT(msg->msg_md == NULL);
4373 lnet_finalize(msg, rc);
4376 lnet_drop_message(ni, cpt, private, payload_length, type);
4379 EXPORT_SYMBOL(lnet_parse);
4382 lnet_drop_delayed_msg_list(struct list_head *head, char *reason)
4384 while (!list_empty(head)) {
4385 struct lnet_process_id id = {0};
4386 struct lnet_msg *msg;
4388 msg = list_entry(head->next, struct lnet_msg, msg_list);
4389 list_del(&msg->msg_list);
4391 id.nid = msg->msg_hdr.src_nid;
4392 id.pid = msg->msg_hdr.src_pid;
4394 LASSERT(msg->msg_md == NULL);
4395 LASSERT(msg->msg_rx_delayed);
4396 LASSERT(msg->msg_rxpeer != NULL);
4397 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
4399 CWARN("Dropping delayed PUT from %s portal %d match %llu"
4400 " offset %d length %d: %s\n",
4402 msg->msg_hdr.msg.put.ptl_index,
4403 msg->msg_hdr.msg.put.match_bits,
4404 msg->msg_hdr.msg.put.offset,
4405 msg->msg_hdr.payload_length, reason);
4407 /* NB I can't drop msg's ref on msg_rxpeer until after I've
4408 * called lnet_drop_message(), so I just hang onto msg as well
4409 * until that's done */
4411 lnet_drop_message(msg->msg_rxni, msg->msg_rx_cpt,
4412 msg->msg_private, msg->msg_len,
4415 msg->msg_no_resend = true;
4417 * NB: message will not generate event because w/o attached MD,
4418 * but we still should give error code so lnet_msg_decommit()
4419 * can skip counters operations and other checks.
4421 lnet_finalize(msg, -ENOENT);
4426 lnet_recv_delayed_msg_list(struct list_head *head)
4428 while (!list_empty(head)) {
4429 struct lnet_msg *msg;
4430 struct lnet_process_id id;
4432 msg = list_entry(head->next, struct lnet_msg, msg_list);
4433 list_del(&msg->msg_list);
4435 /* md won't disappear under me, since each msg
4436 * holds a ref on it */
4438 id.nid = msg->msg_hdr.src_nid;
4439 id.pid = msg->msg_hdr.src_pid;
4441 LASSERT(msg->msg_rx_delayed);
4442 LASSERT(msg->msg_md != NULL);
4443 LASSERT(msg->msg_rxpeer != NULL);
4444 LASSERT(msg->msg_rxni != NULL);
4445 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
4447 CDEBUG(D_NET, "Resuming delayed PUT from %s portal %d "
4448 "match %llu offset %d length %d.\n",
4449 libcfs_id2str(id), msg->msg_hdr.msg.put.ptl_index,
4450 msg->msg_hdr.msg.put.match_bits,
4451 msg->msg_hdr.msg.put.offset,
4452 msg->msg_hdr.payload_length);
4454 lnet_recv_put(msg->msg_rxni, msg);
4459 lnet_attach_rsp_tracker(struct lnet_rsp_tracker *rspt, int cpt,
4460 struct lnet_libmd *md, struct lnet_handle_md mdh)
4463 bool new_entry = true;
4464 struct lnet_rsp_tracker *local_rspt;
4467 * MD has a refcount taken by message so it's not going away.
4468 * The MD however can be looked up. We need to secure the access
4469 * to the md_rspt_ptr by taking the res_lock.
4470 * The rspt can be accessed without protection up to when it gets
4471 * added to the list.
4475 local_rspt = md->md_rspt_ptr;
4476 timeout_ns = lnet_transaction_timeout * NSEC_PER_SEC;
4477 if (local_rspt != NULL) {
4479 * we already have an rspt attached to the md, so we'll
4480 * update the deadline on that one.
4482 LIBCFS_FREE(rspt, sizeof(*rspt));
4486 rspt->rspt_mdh = mdh;
4487 rspt->rspt_cpt = cpt;
4488 /* store the rspt so we can access it when we get the REPLY */
4489 md->md_rspt_ptr = rspt;
4492 local_rspt->rspt_deadline = ktime_add_ns(ktime_get(), timeout_ns);
4495 * add to the list of tracked responses. It's added to tail of the
4496 * list in order to expire all the older entries first.
4499 if (!new_entry && !list_empty(&local_rspt->rspt_on_list))
4500 list_del_init(&local_rspt->rspt_on_list);
4501 list_add_tail(&local_rspt->rspt_on_list, the_lnet.ln_mt_rstq[cpt]);
4502 lnet_net_unlock(cpt);
4503 lnet_res_unlock(cpt);
4507 * Initiate an asynchronous PUT operation.
4509 * There are several events associated with a PUT: completion of the send on
4510 * the initiator node (LNET_EVENT_SEND), and when the send completes
4511 * successfully, the receipt of an acknowledgment (LNET_EVENT_ACK) indicating
4512 * that the operation was accepted by the target. The event LNET_EVENT_PUT is
4513 * used at the target node to indicate the completion of incoming data
4516 * The local events will be logged in the EQ associated with the MD pointed to
4517 * by \a mdh handle. Using a MD without an associated EQ results in these
4518 * events being discarded. In this case, the caller must have another
4519 * mechanism (e.g., a higher level protocol) for determining when it is safe
4520 * to modify the memory region associated with the MD.
4522 * Note that LNet does not guarantee the order of LNET_EVENT_SEND and
4523 * LNET_EVENT_ACK, though intuitively ACK should happen after SEND.
4525 * \param self Indicates the NID of a local interface through which to send
4526 * the PUT request. Use LNET_NID_ANY to let LNet choose one by itself.
4527 * \param mdh A handle for the MD that describes the memory to be sent. The MD
4528 * must be "free floating" (See LNetMDBind()).
4529 * \param ack Controls whether an acknowledgment is requested.
4530 * Acknowledgments are only sent when they are requested by the initiating
4531 * process and the target MD enables them.
4532 * \param target A process identifier for the target process.
4533 * \param portal The index in the \a target's portal table.
4534 * \param match_bits The match bits to use for MD selection at the target
4536 * \param offset The offset into the target MD (only used when the target
4537 * MD has the LNET_MD_MANAGE_REMOTE option set).
4538 * \param hdr_data 64 bits of user data that can be included in the message
4539 * header. This data is written to an event queue entry at the target if an
4540 * EQ is present on the matching MD.
4542 * \retval 0 Success, and only in this case events will be generated
4543 * and logged to EQ (if it exists).
4544 * \retval -EIO Simulated failure.
4545 * \retval -ENOMEM Memory allocation failure.
4546 * \retval -ENOENT Invalid MD object.
4548 * \see struct lnet_event::hdr_data and lnet_event_kind_t.
4551 LNetPut(lnet_nid_t self, struct lnet_handle_md mdh, enum lnet_ack_req ack,
4552 struct lnet_process_id target, unsigned int portal,
4553 __u64 match_bits, unsigned int offset,
4556 struct lnet_msg *msg;
4557 struct lnet_libmd *md;
4560 struct lnet_rsp_tracker *rspt = NULL;
4562 LASSERT(the_lnet.ln_refcount > 0);
4564 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
4565 fail_peer(target.nid, 1)) { /* shall we now? */
4566 CERROR("Dropping PUT to %s: simulated failure\n",
4567 libcfs_id2str(target));
4571 msg = lnet_msg_alloc();
4573 CERROR("Dropping PUT to %s: ENOMEM on struct lnet_msg\n",
4574 libcfs_id2str(target));
4577 msg->msg_vmflush = !!memory_pressure_get();
4579 cpt = lnet_cpt_of_cookie(mdh.cookie);
4581 if (ack == LNET_ACK_REQ) {
4582 rspt = lnet_rspt_alloc(cpt);
4584 CERROR("Dropping PUT to %s: ENOMEM on response tracker\n",
4585 libcfs_id2str(target));
4588 INIT_LIST_HEAD(&rspt->rspt_on_list);
4593 md = lnet_handle2md(&mdh);
4594 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
4595 CERROR("Dropping PUT (%llu:%d:%s): MD (%d) invalid\n",
4596 match_bits, portal, libcfs_id2str(target),
4597 md == NULL ? -1 : md->md_threshold);
4598 if (md != NULL && md->md_me != NULL)
4599 CERROR("Source MD also attached to portal %d\n",
4600 md->md_me->me_portal);
4601 lnet_res_unlock(cpt);
4603 LIBCFS_FREE(rspt, sizeof(*rspt));
4608 CDEBUG(D_NET, "LNetPut -> %s\n", libcfs_id2str(target));
4610 lnet_msg_attach_md(msg, md, 0, 0);
4612 lnet_prep_send(msg, LNET_MSG_PUT, target, 0, md->md_length);
4614 msg->msg_hdr.msg.put.match_bits = cpu_to_le64(match_bits);
4615 msg->msg_hdr.msg.put.ptl_index = cpu_to_le32(portal);
4616 msg->msg_hdr.msg.put.offset = cpu_to_le32(offset);
4617 msg->msg_hdr.msg.put.hdr_data = hdr_data;
4619 /* NB handles only looked up by creator (no flips) */
4620 if (ack == LNET_ACK_REQ) {
4621 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
4622 the_lnet.ln_interface_cookie;
4623 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
4624 md->md_lh.lh_cookie;
4626 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
4627 LNET_WIRE_HANDLE_COOKIE_NONE;
4628 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
4629 LNET_WIRE_HANDLE_COOKIE_NONE;
4632 lnet_res_unlock(cpt);
4634 lnet_build_msg_event(msg, LNET_EVENT_SEND);
4636 if (ack == LNET_ACK_REQ)
4637 lnet_attach_rsp_tracker(rspt, cpt, md, mdh);
4639 if (CFS_FAIL_CHECK_ORSET(CFS_FAIL_PTLRPC_OST_BULK_CB2,
4643 rc = lnet_send(self, msg, LNET_NID_ANY);
4646 CNETERR("Error sending PUT to %s: %d\n",
4647 libcfs_id2str(target), rc);
4648 msg->msg_no_resend = true;
4649 lnet_finalize(msg, rc);
4652 /* completion will be signalled by an event */
4655 EXPORT_SYMBOL(LNetPut);
4658 * The LND can DMA direct to the GET md (i.e. no REPLY msg). This
4659 * returns a msg for the LND to pass to lnet_finalize() when the sink
4660 * data has been received.
4662 * CAVEAT EMPTOR: 'getmsg' is the original GET, which is freed when
4663 * lnet_finalize() is called on it, so the LND must call this first
4666 lnet_create_reply_msg(struct lnet_ni *ni, struct lnet_msg *getmsg)
4668 struct lnet_msg *msg = lnet_msg_alloc();
4669 struct lnet_libmd *getmd = getmsg->msg_md;
4670 struct lnet_process_id peer_id = getmsg->msg_target;
4673 LASSERT(!getmsg->msg_target_is_router);
4674 LASSERT(!getmsg->msg_routing);
4677 CERROR("%s: Dropping REPLY from %s: can't allocate msg\n",
4678 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id));
4682 cpt = lnet_cpt_of_cookie(getmd->md_lh.lh_cookie);
4685 LASSERT(getmd->md_refcount > 0);
4687 if (getmd->md_threshold == 0) {
4688 CERROR("%s: Dropping REPLY from %s for inactive MD %p\n",
4689 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id),
4691 lnet_res_unlock(cpt);
4695 LASSERT(getmd->md_offset == 0);
4697 CDEBUG(D_NET, "%s: Reply from %s md %p\n",
4698 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id), getmd);
4700 /* setup information for lnet_build_msg_event */
4701 msg->msg_initiator = getmsg->msg_txpeer->lpni_peer_net->lpn_peer->lp_primary_nid;
4702 msg->msg_from = peer_id.nid;
4703 msg->msg_type = LNET_MSG_GET; /* flag this msg as an "optimized" GET */
4704 msg->msg_hdr.src_nid = peer_id.nid;
4705 msg->msg_hdr.payload_length = getmd->md_length;
4706 msg->msg_receiving = 1; /* required by lnet_msg_attach_md */
4708 lnet_msg_attach_md(msg, getmd, getmd->md_offset, getmd->md_length);
4709 lnet_res_unlock(cpt);
4711 cpt = lnet_cpt_of_nid(peer_id.nid, ni);
4714 lnet_msg_commit(msg, cpt);
4715 lnet_net_unlock(cpt);
4717 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
4722 cpt = lnet_cpt_of_nid(peer_id.nid, ni);
4725 lnet_incr_stats(&ni->ni_stats, LNET_MSG_GET, LNET_STATS_TYPE_DROP);
4726 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
4727 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length +=
4729 lnet_net_unlock(cpt);
4736 EXPORT_SYMBOL(lnet_create_reply_msg);
4739 lnet_set_reply_msg_len(struct lnet_ni *ni, struct lnet_msg *reply,
4742 /* Set the REPLY length, now the RDMA that elides the REPLY message has
4743 * completed and I know it. */
4744 LASSERT(reply != NULL);
4745 LASSERT(reply->msg_type == LNET_MSG_GET);
4746 LASSERT(reply->msg_ev.type == LNET_EVENT_REPLY);
4748 /* NB I trusted my peer to RDMA. If she tells me she's written beyond
4749 * the end of my buffer, I might as well be dead. */
4750 LASSERT(len <= reply->msg_ev.mlength);
4752 reply->msg_ev.mlength = len;
4754 EXPORT_SYMBOL(lnet_set_reply_msg_len);
4757 * Initiate an asynchronous GET operation.
4759 * On the initiator node, an LNET_EVENT_SEND is logged when the GET request
4760 * is sent, and an LNET_EVENT_REPLY is logged when the data returned from
4761 * the target node in the REPLY has been written to local MD.
4763 * On the target node, an LNET_EVENT_GET is logged when the GET request
4764 * arrives and is accepted into a MD.
4766 * \param self,target,portal,match_bits,offset See the discussion in LNetPut().
4767 * \param mdh A handle for the MD that describes the memory into which the
4768 * requested data will be received. The MD must be "free floating" (See LNetMDBind()).
4770 * \retval 0 Success, and only in this case events will be generated
4771 * and logged to EQ (if it exists) of the MD.
4772 * \retval -EIO Simulated failure.
4773 * \retval -ENOMEM Memory allocation failure.
4774 * \retval -ENOENT Invalid MD object.
4777 LNetGet(lnet_nid_t self, struct lnet_handle_md mdh,
4778 struct lnet_process_id target, unsigned int portal,
4779 __u64 match_bits, unsigned int offset, bool recovery)
4781 struct lnet_msg *msg;
4782 struct lnet_libmd *md;
4783 struct lnet_rsp_tracker *rspt;
4787 LASSERT(the_lnet.ln_refcount > 0);
4789 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
4790 fail_peer(target.nid, 1)) /* shall we now? */
4792 CERROR("Dropping GET to %s: simulated failure\n",
4793 libcfs_id2str(target));
4797 msg = lnet_msg_alloc();
4799 CERROR("Dropping GET to %s: ENOMEM on struct lnet_msg\n",
4800 libcfs_id2str(target));
4804 cpt = lnet_cpt_of_cookie(mdh.cookie);
4806 rspt = lnet_rspt_alloc(cpt);
4808 CERROR("Dropping GET to %s: ENOMEM on response tracker\n",
4809 libcfs_id2str(target));
4812 INIT_LIST_HEAD(&rspt->rspt_on_list);
4814 msg->msg_recovery = recovery;
4818 md = lnet_handle2md(&mdh);
4819 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
4820 CERROR("Dropping GET (%llu:%d:%s): MD (%d) invalid\n",
4821 match_bits, portal, libcfs_id2str(target),
4822 md == NULL ? -1 : md->md_threshold);
4823 if (md != NULL && md->md_me != NULL)
4824 CERROR("REPLY MD also attached to portal %d\n",
4825 md->md_me->me_portal);
4827 lnet_res_unlock(cpt);
4830 LIBCFS_FREE(rspt, sizeof(*rspt));
4834 CDEBUG(D_NET, "LNetGet -> %s\n", libcfs_id2str(target));
4836 lnet_msg_attach_md(msg, md, 0, 0);
4838 lnet_prep_send(msg, LNET_MSG_GET, target, 0, 0);
4840 msg->msg_hdr.msg.get.match_bits = cpu_to_le64(match_bits);
4841 msg->msg_hdr.msg.get.ptl_index = cpu_to_le32(portal);
4842 msg->msg_hdr.msg.get.src_offset = cpu_to_le32(offset);
4843 msg->msg_hdr.msg.get.sink_length = cpu_to_le32(md->md_length);
4845 /* NB handles only looked up by creator (no flips) */
4846 msg->msg_hdr.msg.get.return_wmd.wh_interface_cookie =
4847 the_lnet.ln_interface_cookie;
4848 msg->msg_hdr.msg.get.return_wmd.wh_object_cookie =
4849 md->md_lh.lh_cookie;
4851 lnet_res_unlock(cpt);
4853 lnet_build_msg_event(msg, LNET_EVENT_SEND);
4855 lnet_attach_rsp_tracker(rspt, cpt, md, mdh);
4857 rc = lnet_send(self, msg, LNET_NID_ANY);
4859 CNETERR("Error sending GET to %s: %d\n",
4860 libcfs_id2str(target), rc);
4861 msg->msg_no_resend = true;
4862 lnet_finalize(msg, rc);
4865 /* completion will be signalled by an event */
4868 EXPORT_SYMBOL(LNetGet);
4871 * Calculate distance to node at \a dstnid.
4873 * \param dstnid Target NID.
4874 * \param srcnidp If not NULL, NID of the local interface to reach \a dstnid
4876 * \param orderp If not NULL, order of the route to reach \a dstnid is saved
4879 * \retval 0 If \a dstnid belongs to a local interface, and reserved option
4880 * local_nid_dist_zero is set, which is the default.
4881 * \retval positives Distance to target NID, i.e. number of hops plus one.
4882 * \retval -EHOSTUNREACH If \a dstnid is not reachable.
4885 LNetDist(lnet_nid_t dstnid, lnet_nid_t *srcnidp, __u32 *orderp)
4887 struct list_head *e;
4888 struct lnet_ni *ni = NULL;
4889 struct lnet_remotenet *rnet;
4890 __u32 dstnet = LNET_NIDNET(dstnid);
4894 struct list_head *rn_list;
4896 /* if !local_nid_dist_zero, I don't return a distance of 0 ever
4897 * (when lustre sees a distance of 0, it substitutes 0@lo), so I
4898 * keep order 0 free for 0@lo and order 1 free for a local NID
4901 LASSERT(the_lnet.ln_refcount > 0);
4903 cpt = lnet_net_lock_current();
4905 while ((ni = lnet_get_next_ni_locked(NULL, ni))) {
4906 if (ni->ni_nid == dstnid) {
4907 if (srcnidp != NULL)
4909 if (orderp != NULL) {
4910 if (LNET_NETTYP(LNET_NIDNET(dstnid)) == LOLND)
4915 lnet_net_unlock(cpt);
4917 return local_nid_dist_zero ? 0 : 1;
4920 if (LNET_NIDNET(ni->ni_nid) == dstnet) {
4921 /* Check if ni was originally created in
4922 * current net namespace.
4923 * If not, assign order above 0xffff0000,
4924 * to make this ni not a priority. */
4925 if (!net_eq(ni->ni_net_ns, current->nsproxy->net_ns))
4926 order += 0xffff0000;
4928 if (srcnidp != NULL)
4929 *srcnidp = ni->ni_nid;
4932 lnet_net_unlock(cpt);
4939 rn_list = lnet_net2rnethash(dstnet);
4940 list_for_each(e, rn_list) {
4941 rnet = list_entry(e, struct lnet_remotenet, lrn_list);
4943 if (rnet->lrn_net == dstnet) {
4944 struct lnet_route *route;
4945 struct lnet_route *shortest = NULL;
4946 __u32 shortest_hops = LNET_UNDEFINED_HOPS;
4949 LASSERT(!list_empty(&rnet->lrn_routes));
4951 list_for_each_entry(route, &rnet->lrn_routes,
4953 route_hops = route->lr_hops;
4954 if (route_hops == LNET_UNDEFINED_HOPS)
4956 if (shortest == NULL ||
4957 route_hops < shortest_hops) {
4959 shortest_hops = route_hops;
4963 LASSERT(shortest != NULL);
4964 hops = shortest_hops;
4965 if (srcnidp != NULL) {
4966 ni = lnet_get_next_ni_locked(
4967 shortest->lr_gateway->lpni_net,
4969 *srcnidp = ni->ni_nid;
4973 lnet_net_unlock(cpt);
4979 lnet_net_unlock(cpt);
4980 return -EHOSTUNREACH;
4982 EXPORT_SYMBOL(LNetDist);