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)
870 time64_t now = ktime_get_seconds();
872 if (!lnet_peer_aliveness_enabled(lp))
875 if (lnet_peer_is_alive(lp, now))
879 * Peer appears dead, but we should avoid frequent NI queries (at
880 * most once per lnet_queryinterval seconds).
882 if (lp->lpni_last_query != 0) {
883 static const int lnet_queryinterval = 1;
886 next_query = lp->lpni_last_query + lnet_queryinterval;
888 if (now < next_query) {
890 CWARN("Unexpected aliveness of peer %s: "
891 "%lld < %lld (%d/%d)\n",
892 libcfs_nid2str(lp->lpni_nid),
895 lp->lpni_net->net_tunables.lct_peer_timeout);
900 /* query NI for latest aliveness news */
901 lnet_ni_query_locked(ni, lp);
903 if (lnet_peer_is_alive(lp, now))
906 lnet_notify_locked(lp, 0, 0, lp->lpni_last_alive);
911 * \param msg The message to be sent.
912 * \param do_send True if lnet_ni_send() should be called in this function.
913 * lnet_send() is going to lnet_net_unlock immediately after this, so
914 * it sets do_send FALSE and I don't do the unlock/send/lock bit.
916 * \retval LNET_CREDIT_OK If \a msg sent or OK to send.
917 * \retval LNET_CREDIT_WAIT If \a msg blocked for credit.
918 * \retval -EHOSTUNREACH If the next hop of the message appears dead.
919 * \retval -ECANCELED If the MD of the message has been unlinked.
922 lnet_post_send_locked(struct lnet_msg *msg, int do_send)
924 struct lnet_peer_ni *lp = msg->msg_txpeer;
925 struct lnet_ni *ni = msg->msg_txni;
926 int cpt = msg->msg_tx_cpt;
927 struct lnet_tx_queue *tq = ni->ni_tx_queues[cpt];
929 /* non-lnet_send() callers have checked before */
930 LASSERT(!do_send || msg->msg_tx_delayed);
931 LASSERT(!msg->msg_receiving);
932 LASSERT(msg->msg_tx_committed);
934 /* NB 'lp' is always the next hop */
935 if ((msg->msg_target.pid & LNET_PID_USERFLAG) == 0 &&
936 lnet_peer_alive_locked(ni, lp) == 0) {
937 the_lnet.ln_counters[cpt]->drop_count++;
938 the_lnet.ln_counters[cpt]->drop_length += msg->msg_len;
939 lnet_net_unlock(cpt);
941 lnet_incr_stats(&msg->msg_txpeer->lpni_stats,
943 LNET_STATS_TYPE_DROP);
945 lnet_incr_stats(&msg->msg_txni->ni_stats,
947 LNET_STATS_TYPE_DROP);
949 CNETERR("Dropping message for %s: peer not alive\n",
950 libcfs_id2str(msg->msg_target));
952 msg->msg_health_status = LNET_MSG_STATUS_LOCAL_DROPPED;
953 lnet_finalize(msg, -EHOSTUNREACH);
957 return -EHOSTUNREACH;
960 if (msg->msg_md != NULL &&
961 (msg->msg_md->md_flags & LNET_MD_FLAG_ABORTED) != 0) {
962 lnet_net_unlock(cpt);
964 CNETERR("Aborting message for %s: LNetM[DE]Unlink() already "
965 "called on the MD/ME.\n",
966 libcfs_id2str(msg->msg_target));
968 msg->msg_no_resend = true;
969 lnet_finalize(msg, -ECANCELED);
976 if (!msg->msg_peertxcredit) {
977 spin_lock(&lp->lpni_lock);
978 LASSERT((lp->lpni_txcredits < 0) ==
979 !list_empty(&lp->lpni_txq));
981 msg->msg_peertxcredit = 1;
982 lp->lpni_txqnob += msg->msg_len + sizeof(struct lnet_hdr);
983 lp->lpni_txcredits--;
985 if (lp->lpni_txcredits < lp->lpni_mintxcredits)
986 lp->lpni_mintxcredits = lp->lpni_txcredits;
988 if (lp->lpni_txcredits < 0) {
989 msg->msg_tx_delayed = 1;
990 list_add_tail(&msg->msg_list, &lp->lpni_txq);
991 spin_unlock(&lp->lpni_lock);
992 return LNET_CREDIT_WAIT;
994 spin_unlock(&lp->lpni_lock);
997 if (!msg->msg_txcredit) {
998 LASSERT((tq->tq_credits < 0) ==
999 !list_empty(&tq->tq_delayed));
1001 msg->msg_txcredit = 1;
1003 atomic_dec(&ni->ni_tx_credits);
1005 if (tq->tq_credits < tq->tq_credits_min)
1006 tq->tq_credits_min = tq->tq_credits;
1008 if (tq->tq_credits < 0) {
1009 msg->msg_tx_delayed = 1;
1010 list_add_tail(&msg->msg_list, &tq->tq_delayed);
1011 return LNET_CREDIT_WAIT;
1015 /* unset the tx_delay flag as we're going to send it now */
1016 msg->msg_tx_delayed = 0;
1019 lnet_net_unlock(cpt);
1020 lnet_ni_send(ni, msg);
1023 return LNET_CREDIT_OK;
1027 static struct lnet_rtrbufpool *
1028 lnet_msg2bufpool(struct lnet_msg *msg)
1030 struct lnet_rtrbufpool *rbp;
1033 LASSERT(msg->msg_rx_committed);
1035 cpt = msg->msg_rx_cpt;
1036 rbp = &the_lnet.ln_rtrpools[cpt][0];
1038 LASSERT(msg->msg_len <= LNET_MTU);
1039 while (msg->msg_len > (unsigned int)rbp->rbp_npages * PAGE_SIZE) {
1041 LASSERT(rbp < &the_lnet.ln_rtrpools[cpt][LNET_NRBPOOLS]);
1048 lnet_post_routed_recv_locked(struct lnet_msg *msg, int do_recv)
1050 /* lnet_parse is going to lnet_net_unlock immediately after this, so it
1051 * sets do_recv FALSE and I don't do the unlock/send/lock bit.
1052 * I return LNET_CREDIT_WAIT if msg blocked and LNET_CREDIT_OK if
1053 * received or OK to receive */
1054 struct lnet_peer_ni *lp = msg->msg_rxpeer;
1055 struct lnet_rtrbufpool *rbp;
1056 struct lnet_rtrbuf *rb;
1058 LASSERT (msg->msg_iov == NULL);
1059 LASSERT (msg->msg_kiov == NULL);
1060 LASSERT (msg->msg_niov == 0);
1061 LASSERT (msg->msg_routing);
1062 LASSERT (msg->msg_receiving);
1063 LASSERT (!msg->msg_sending);
1065 /* non-lnet_parse callers only receive delayed messages */
1066 LASSERT(!do_recv || msg->msg_rx_delayed);
1068 if (!msg->msg_peerrtrcredit) {
1069 spin_lock(&lp->lpni_lock);
1070 LASSERT((lp->lpni_rtrcredits < 0) ==
1071 !list_empty(&lp->lpni_rtrq));
1073 msg->msg_peerrtrcredit = 1;
1074 lp->lpni_rtrcredits--;
1075 if (lp->lpni_rtrcredits < lp->lpni_minrtrcredits)
1076 lp->lpni_minrtrcredits = lp->lpni_rtrcredits;
1078 if (lp->lpni_rtrcredits < 0) {
1079 /* must have checked eager_recv before here */
1080 LASSERT(msg->msg_rx_ready_delay);
1081 msg->msg_rx_delayed = 1;
1082 list_add_tail(&msg->msg_list, &lp->lpni_rtrq);
1083 spin_unlock(&lp->lpni_lock);
1084 return LNET_CREDIT_WAIT;
1086 spin_unlock(&lp->lpni_lock);
1089 rbp = lnet_msg2bufpool(msg);
1091 if (!msg->msg_rtrcredit) {
1092 msg->msg_rtrcredit = 1;
1094 if (rbp->rbp_credits < rbp->rbp_mincredits)
1095 rbp->rbp_mincredits = rbp->rbp_credits;
1097 if (rbp->rbp_credits < 0) {
1098 /* must have checked eager_recv before here */
1099 LASSERT(msg->msg_rx_ready_delay);
1100 msg->msg_rx_delayed = 1;
1101 list_add_tail(&msg->msg_list, &rbp->rbp_msgs);
1102 return LNET_CREDIT_WAIT;
1106 LASSERT(!list_empty(&rbp->rbp_bufs));
1107 rb = list_entry(rbp->rbp_bufs.next, struct lnet_rtrbuf, rb_list);
1108 list_del(&rb->rb_list);
1110 msg->msg_niov = rbp->rbp_npages;
1111 msg->msg_kiov = &rb->rb_kiov[0];
1113 /* unset the msg-rx_delayed flag since we're receiving the message */
1114 msg->msg_rx_delayed = 0;
1117 int cpt = msg->msg_rx_cpt;
1119 lnet_net_unlock(cpt);
1120 lnet_ni_recv(msg->msg_rxni, msg->msg_private, msg, 1,
1121 0, msg->msg_len, msg->msg_len);
1124 return LNET_CREDIT_OK;
1128 lnet_return_tx_credits_locked(struct lnet_msg *msg)
1130 struct lnet_peer_ni *txpeer = msg->msg_txpeer;
1131 struct lnet_ni *txni = msg->msg_txni;
1132 struct lnet_msg *msg2;
1134 if (msg->msg_txcredit) {
1135 struct lnet_ni *ni = msg->msg_txni;
1136 struct lnet_tx_queue *tq = ni->ni_tx_queues[msg->msg_tx_cpt];
1138 /* give back NI txcredits */
1139 msg->msg_txcredit = 0;
1141 LASSERT((tq->tq_credits < 0) ==
1142 !list_empty(&tq->tq_delayed));
1145 atomic_inc(&ni->ni_tx_credits);
1146 if (tq->tq_credits <= 0) {
1147 msg2 = list_entry(tq->tq_delayed.next,
1148 struct lnet_msg, msg_list);
1149 list_del(&msg2->msg_list);
1151 LASSERT(msg2->msg_txni == ni);
1152 LASSERT(msg2->msg_tx_delayed);
1153 LASSERT(msg2->msg_tx_cpt == msg->msg_tx_cpt);
1155 (void) lnet_post_send_locked(msg2, 1);
1159 if (msg->msg_peertxcredit) {
1160 /* give back peer txcredits */
1161 msg->msg_peertxcredit = 0;
1163 spin_lock(&txpeer->lpni_lock);
1164 LASSERT((txpeer->lpni_txcredits < 0) ==
1165 !list_empty(&txpeer->lpni_txq));
1167 txpeer->lpni_txqnob -= msg->msg_len + sizeof(struct lnet_hdr);
1168 LASSERT(txpeer->lpni_txqnob >= 0);
1170 txpeer->lpni_txcredits++;
1171 if (txpeer->lpni_txcredits <= 0) {
1174 msg2 = list_entry(txpeer->lpni_txq.next,
1175 struct lnet_msg, msg_list);
1176 list_del(&msg2->msg_list);
1177 spin_unlock(&txpeer->lpni_lock);
1179 LASSERT(msg2->msg_txpeer == txpeer);
1180 LASSERT(msg2->msg_tx_delayed);
1182 msg2_cpt = msg2->msg_tx_cpt;
1185 * The msg_cpt can be different from the msg2_cpt
1186 * so we need to make sure we lock the correct cpt
1188 * Once we call lnet_post_send_locked() it is no
1189 * longer safe to access msg2, since it could've
1190 * been freed by lnet_finalize(), but we still
1191 * need to relock the correct cpt, so we cache the
1192 * msg2_cpt for the purpose of the check that
1193 * follows the call to lnet_pose_send_locked().
1195 if (msg2_cpt != msg->msg_tx_cpt) {
1196 lnet_net_unlock(msg->msg_tx_cpt);
1197 lnet_net_lock(msg2_cpt);
1199 (void) lnet_post_send_locked(msg2, 1);
1200 if (msg2_cpt != msg->msg_tx_cpt) {
1201 lnet_net_unlock(msg2_cpt);
1202 lnet_net_lock(msg->msg_tx_cpt);
1205 spin_unlock(&txpeer->lpni_lock);
1210 msg->msg_txni = NULL;
1211 lnet_ni_decref_locked(txni, msg->msg_tx_cpt);
1214 if (txpeer != NULL) {
1215 msg->msg_txpeer = NULL;
1216 lnet_peer_ni_decref_locked(txpeer);
1221 lnet_schedule_blocked_locked(struct lnet_rtrbufpool *rbp)
1223 struct lnet_msg *msg;
1225 if (list_empty(&rbp->rbp_msgs))
1227 msg = list_entry(rbp->rbp_msgs.next,
1228 struct lnet_msg, msg_list);
1229 list_del(&msg->msg_list);
1231 (void)lnet_post_routed_recv_locked(msg, 1);
1235 lnet_drop_routed_msgs_locked(struct list_head *list, int cpt)
1237 struct lnet_msg *msg;
1238 struct lnet_msg *tmp;
1240 lnet_net_unlock(cpt);
1242 list_for_each_entry_safe(msg, tmp, list, msg_list) {
1243 lnet_ni_recv(msg->msg_rxni, msg->msg_private, NULL,
1244 0, 0, 0, msg->msg_hdr.payload_length);
1245 list_del_init(&msg->msg_list);
1246 msg->msg_no_resend = true;
1247 lnet_finalize(msg, -ECANCELED);
1254 lnet_return_rx_credits_locked(struct lnet_msg *msg)
1256 struct lnet_peer_ni *rxpeer = msg->msg_rxpeer;
1257 struct lnet_ni *rxni = msg->msg_rxni;
1258 struct lnet_msg *msg2;
1260 if (msg->msg_rtrcredit) {
1261 /* give back global router credits */
1262 struct lnet_rtrbuf *rb;
1263 struct lnet_rtrbufpool *rbp;
1265 /* NB If a msg ever blocks for a buffer in rbp_msgs, it stays
1266 * there until it gets one allocated, or aborts the wait
1268 LASSERT(msg->msg_kiov != NULL);
1270 rb = list_entry(msg->msg_kiov, struct lnet_rtrbuf, rb_kiov[0]);
1273 msg->msg_kiov = NULL;
1274 msg->msg_rtrcredit = 0;
1276 LASSERT(rbp == lnet_msg2bufpool(msg));
1278 LASSERT((rbp->rbp_credits > 0) ==
1279 !list_empty(&rbp->rbp_bufs));
1281 /* If routing is now turned off, we just drop this buffer and
1282 * don't bother trying to return credits. */
1283 if (!the_lnet.ln_routing) {
1284 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1288 /* It is possible that a user has lowered the desired number of
1289 * buffers in this pool. Make sure we never put back
1290 * more buffers than the stated number. */
1291 if (unlikely(rbp->rbp_credits >= rbp->rbp_req_nbuffers)) {
1292 /* Discard this buffer so we don't have too
1294 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1295 rbp->rbp_nbuffers--;
1297 list_add(&rb->rb_list, &rbp->rbp_bufs);
1299 if (rbp->rbp_credits <= 0)
1300 lnet_schedule_blocked_locked(rbp);
1305 if (msg->msg_peerrtrcredit) {
1306 /* give back peer router credits */
1307 msg->msg_peerrtrcredit = 0;
1309 spin_lock(&rxpeer->lpni_lock);
1310 LASSERT((rxpeer->lpni_rtrcredits < 0) ==
1311 !list_empty(&rxpeer->lpni_rtrq));
1313 rxpeer->lpni_rtrcredits++;
1315 /* drop all messages which are queued to be routed on that
1317 if (!the_lnet.ln_routing) {
1318 struct list_head drop;
1319 INIT_LIST_HEAD(&drop);
1320 list_splice_init(&rxpeer->lpni_rtrq, &drop);
1321 spin_unlock(&rxpeer->lpni_lock);
1322 lnet_drop_routed_msgs_locked(&drop, msg->msg_rx_cpt);
1323 } else if (rxpeer->lpni_rtrcredits <= 0) {
1324 msg2 = list_entry(rxpeer->lpni_rtrq.next,
1325 struct lnet_msg, msg_list);
1326 list_del(&msg2->msg_list);
1327 spin_unlock(&rxpeer->lpni_lock);
1328 (void) lnet_post_routed_recv_locked(msg2, 1);
1330 spin_unlock(&rxpeer->lpni_lock);
1334 msg->msg_rxni = NULL;
1335 lnet_ni_decref_locked(rxni, msg->msg_rx_cpt);
1337 if (rxpeer != NULL) {
1338 msg->msg_rxpeer = NULL;
1339 lnet_peer_ni_decref_locked(rxpeer);
1344 lnet_compare_peers(struct lnet_peer_ni *p1, struct lnet_peer_ni *p2)
1346 if (p1->lpni_txqnob < p2->lpni_txqnob)
1349 if (p1->lpni_txqnob > p2->lpni_txqnob)
1352 if (p1->lpni_txcredits > p2->lpni_txcredits)
1355 if (p1->lpni_txcredits < p2->lpni_txcredits)
1362 lnet_compare_routes(struct lnet_route *r1, struct lnet_route *r2)
1364 struct lnet_peer_ni *p1 = r1->lr_gateway;
1365 struct lnet_peer_ni *p2 = r2->lr_gateway;
1366 int r1_hops = (r1->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r1->lr_hops;
1367 int r2_hops = (r2->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r2->lr_hops;
1370 if (r1->lr_priority < r2->lr_priority)
1373 if (r1->lr_priority > r2->lr_priority)
1376 if (r1_hops < r2_hops)
1379 if (r1_hops > r2_hops)
1382 rc = lnet_compare_peers(p1, p2);
1386 if (r1->lr_seq - r2->lr_seq <= 0)
1392 static struct lnet_peer_ni *
1393 lnet_find_route_locked(struct lnet_net *net, __u32 remote_net,
1396 struct lnet_remotenet *rnet;
1397 struct lnet_route *route;
1398 struct lnet_route *best_route;
1399 struct lnet_route *last_route;
1400 struct lnet_peer_ni *lpni_best;
1401 struct lnet_peer_ni *lp;
1404 /* If @rtr_nid is not LNET_NID_ANY, return the gateway with
1405 * rtr_nid nid, otherwise find the best gateway I can use */
1407 rnet = lnet_find_rnet_locked(remote_net);
1412 best_route = last_route = NULL;
1413 list_for_each_entry(route, &rnet->lrn_routes, lr_list) {
1414 lp = route->lr_gateway;
1416 if (!lnet_is_route_alive(route))
1419 if (net != NULL && lp->lpni_net != net)
1422 if (lp->lpni_nid == rtr_nid) /* it's pre-determined router */
1425 if (lpni_best == NULL) {
1426 best_route = last_route = route;
1431 /* no protection on below fields, but it's harmless */
1432 if (last_route->lr_seq - route->lr_seq < 0)
1435 rc = lnet_compare_routes(route, best_route);
1443 /* set sequence number on the best router to the latest sequence + 1
1444 * so we can round-robin all routers, it's race and inaccurate but
1445 * harmless and functional */
1446 if (best_route != NULL)
1447 best_route->lr_seq = last_route->lr_seq + 1;
1451 static struct lnet_ni *
1452 lnet_get_best_ni(struct lnet_net *local_net, struct lnet_ni *best_ni,
1453 struct lnet_peer *peer, struct lnet_peer_net *peer_net,
1456 struct lnet_ni *ni = NULL;
1457 unsigned int shortest_distance;
1462 * If there is no peer_ni that we can send to on this network,
1463 * then there is no point in looking for a new best_ni here.
1465 if (!lnet_get_next_peer_ni_locked(peer, peer_net, NULL))
1468 if (best_ni == NULL) {
1469 shortest_distance = UINT_MAX;
1470 best_credits = INT_MIN;
1473 shortest_distance = cfs_cpt_distance(lnet_cpt_table(), md_cpt,
1474 best_ni->ni_dev_cpt);
1475 best_credits = atomic_read(&best_ni->ni_tx_credits);
1476 best_healthv = atomic_read(&best_ni->ni_healthv);
1479 while ((ni = lnet_get_next_ni_locked(local_net, ni))) {
1480 unsigned int distance;
1484 ni_credits = atomic_read(&ni->ni_tx_credits);
1485 ni_healthv = atomic_read(&ni->ni_healthv);
1488 * calculate the distance from the CPT on which
1489 * the message memory is allocated to the CPT of
1490 * the NI's physical device
1492 distance = cfs_cpt_distance(lnet_cpt_table(),
1496 CDEBUG(D_NET, "compare ni %s [c:%d, d:%d, s:%d] with best_ni %s [c:%d, d:%d, s:%d]\n",
1497 libcfs_nid2str(ni->ni_nid), ni_credits, distance,
1498 ni->ni_seq, (best_ni) ? libcfs_nid2str(best_ni->ni_nid)
1499 : "not seleced", best_credits, shortest_distance,
1500 (best_ni) ? best_ni->ni_seq : 0);
1503 * All distances smaller than the NUMA range
1504 * are treated equally.
1506 if (distance < lnet_numa_range)
1507 distance = lnet_numa_range;
1510 * Select on health, shorter distance, available
1511 * credits, then round-robin.
1513 if (ni_healthv < best_healthv) {
1515 } else if (ni_healthv > best_healthv) {
1516 best_healthv = ni_healthv;
1518 * If we're going to prefer this ni because it's
1519 * the healthiest, then we should set the
1520 * shortest_distance in the algorithm in case
1521 * there are multiple NIs with the same health but
1522 * different distances.
1524 if (distance < shortest_distance)
1525 shortest_distance = distance;
1526 } else if (distance > shortest_distance) {
1528 } else if (distance < shortest_distance) {
1529 shortest_distance = distance;
1530 } else if (ni_credits < best_credits) {
1532 } else if (ni_credits == best_credits) {
1533 if (best_ni && best_ni->ni_seq <= ni->ni_seq)
1537 best_credits = ni_credits;
1540 CDEBUG(D_NET, "selected best_ni %s\n",
1541 (best_ni) ? libcfs_nid2str(best_ni->ni_nid) : "no selection");
1547 * Traffic to the LNET_RESERVED_PORTAL may not trigger peer discovery,
1548 * because such traffic is required to perform discovery. We therefore
1549 * exclude all GET and PUT on that portal. We also exclude all ACK and
1550 * REPLY traffic, but that is because the portal is not tracked in the
1551 * message structure for these message types. We could restrict this
1552 * further by also checking for LNET_PROTO_PING_MATCHBITS.
1555 lnet_msg_discovery(struct lnet_msg *msg)
1557 if (msg->msg_type == LNET_MSG_PUT) {
1558 if (msg->msg_hdr.msg.put.ptl_index != LNET_RESERVED_PORTAL)
1560 } else if (msg->msg_type == LNET_MSG_GET) {
1561 if (msg->msg_hdr.msg.get.ptl_index != LNET_RESERVED_PORTAL)
1567 #define SRC_SPEC 0x0001
1568 #define SRC_ANY 0x0002
1569 #define LOCAL_DST 0x0004
1570 #define REMOTE_DST 0x0008
1571 #define MR_DST 0x0010
1572 #define NMR_DST 0x0020
1573 #define SND_RESP 0x0040
1575 /* The following to defines are used for return codes */
1576 #define REPEAT_SEND 0x1000
1577 #define PASS_THROUGH 0x2000
1579 /* The different cases lnet_select pathway needs to handle */
1580 #define SRC_SPEC_LOCAL_MR_DST (SRC_SPEC | LOCAL_DST | MR_DST)
1581 #define SRC_SPEC_ROUTER_MR_DST (SRC_SPEC | REMOTE_DST | MR_DST)
1582 #define SRC_SPEC_LOCAL_NMR_DST (SRC_SPEC | LOCAL_DST | NMR_DST)
1583 #define SRC_SPEC_ROUTER_NMR_DST (SRC_SPEC | REMOTE_DST | NMR_DST)
1584 #define SRC_ANY_LOCAL_MR_DST (SRC_ANY | LOCAL_DST | MR_DST)
1585 #define SRC_ANY_ROUTER_MR_DST (SRC_ANY | REMOTE_DST | MR_DST)
1586 #define SRC_ANY_LOCAL_NMR_DST (SRC_ANY | LOCAL_DST | NMR_DST)
1587 #define SRC_ANY_ROUTER_NMR_DST (SRC_ANY | REMOTE_DST | NMR_DST)
1590 lnet_handle_send(struct lnet_send_data *sd)
1592 struct lnet_ni *best_ni = sd->sd_best_ni;
1593 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
1594 struct lnet_peer_ni *final_dst_lpni = sd->sd_final_dst_lpni;
1595 struct lnet_msg *msg = sd->sd_msg;
1597 __u32 send_case = sd->sd_send_case;
1599 __u32 routing = send_case & REMOTE_DST;
1602 * Increment sequence number of the selected peer so that we
1603 * pick the next one in Round Robin.
1605 best_lpni->lpni_seq++;
1608 * grab a reference on the peer_ni so it sticks around even if
1609 * we need to drop and relock the lnet_net_lock below.
1611 lnet_peer_ni_addref_locked(best_lpni);
1614 * Use lnet_cpt_of_nid() to determine the CPT used to commit the
1615 * message. This ensures that we get a CPT that is correct for
1616 * the NI when the NI has been restricted to a subset of all CPTs.
1617 * If the selected CPT differs from the one currently locked, we
1618 * must unlock and relock the lnet_net_lock(), and then check whether
1619 * the configuration has changed. We don't have a hold on the best_ni
1620 * yet, and it may have vanished.
1622 cpt2 = lnet_cpt_of_nid_locked(best_lpni->lpni_nid, best_ni);
1623 if (sd->sd_cpt != cpt2) {
1624 __u32 seq = lnet_get_dlc_seq_locked();
1625 lnet_net_unlock(sd->sd_cpt);
1627 lnet_net_lock(sd->sd_cpt);
1628 if (seq != lnet_get_dlc_seq_locked()) {
1629 lnet_peer_ni_decref_locked(best_lpni);
1635 * store the best_lpni in the message right away to avoid having
1636 * to do the same operation under different conditions
1638 msg->msg_txpeer = best_lpni;
1639 msg->msg_txni = best_ni;
1642 * grab a reference for the best_ni since now it's in use in this
1643 * send. The reference will be dropped in lnet_finalize()
1645 lnet_ni_addref_locked(msg->msg_txni, sd->sd_cpt);
1648 * Always set the target.nid to the best peer picked. Either the
1649 * NID will be one of the peer NIDs selected, or the same NID as
1650 * what was originally set in the target or it will be the NID of
1651 * a router if this message should be routed
1653 msg->msg_target.nid = msg->msg_txpeer->lpni_nid;
1656 * lnet_msg_commit assigns the correct cpt to the message, which
1657 * is used to decrement the correct refcount on the ni when it's
1658 * time to return the credits
1660 lnet_msg_commit(msg, sd->sd_cpt);
1663 * If we are routing the message then we keep the src_nid that was
1664 * set by the originator. If we are not routing then we are the
1665 * originator and set it here.
1667 if (!msg->msg_routing)
1668 msg->msg_hdr.src_nid = cpu_to_le64(msg->msg_txni->ni_nid);
1671 msg->msg_target_is_router = 1;
1672 msg->msg_target.pid = LNET_PID_LUSTRE;
1674 * since we're routing we want to ensure that the
1675 * msg_hdr.dest_nid is set to the final destination. When
1676 * the router receives this message it knows how to route
1679 * final_dst_lpni is set at the beginning of the
1680 * lnet_select_pathway() function and is never changed.
1681 * It's safe to use it here.
1683 msg->msg_hdr.dest_nid = cpu_to_le64(final_dst_lpni->lpni_nid);
1686 * if we're not routing set the dest_nid to the best peer
1687 * ni NID that we picked earlier in the algorithm.
1689 msg->msg_hdr.dest_nid = cpu_to_le64(msg->msg_txpeer->lpni_nid);
1692 rc = lnet_post_send_locked(msg, 0);
1695 CDEBUG(D_NET, "TRACE: %s(%s:%s) -> %s(%s:%s) : %s\n",
1696 libcfs_nid2str(msg->msg_hdr.src_nid),
1697 libcfs_nid2str(msg->msg_txni->ni_nid),
1698 libcfs_nid2str(sd->sd_src_nid),
1699 libcfs_nid2str(msg->msg_hdr.dest_nid),
1700 libcfs_nid2str(sd->sd_dst_nid),
1701 libcfs_nid2str(msg->msg_txpeer->lpni_nid),
1702 lnet_msgtyp2str(msg->msg_type));
1707 static struct lnet_peer_ni *
1708 lnet_select_peer_ni(struct lnet_send_data *sd, struct lnet_peer *peer,
1709 struct lnet_peer_net *peer_net)
1712 * Look at the peer NIs for the destination peer that connect
1713 * to the chosen net. If a peer_ni is preferred when using the
1714 * best_ni to communicate, we use that one. If there is no
1715 * preferred peer_ni, or there are multiple preferred peer_ni,
1716 * the available transmit credits are used. If the transmit
1717 * credits are equal, we round-robin over the peer_ni.
1719 struct lnet_peer_ni *lpni = NULL;
1720 struct lnet_peer_ni *best_lpni = NULL;
1721 struct lnet_ni *best_ni = sd->sd_best_ni;
1722 lnet_nid_t dst_nid = sd->sd_dst_nid;
1723 int best_lpni_credits = INT_MIN;
1724 bool preferred = false;
1726 int best_lpni_healthv = 0;
1729 while ((lpni = lnet_get_next_peer_ni_locked(peer, peer_net, lpni))) {
1731 * if the best_ni we've chosen aleady has this lpni
1732 * preferred, then let's use it
1734 ni_is_pref = lnet_peer_is_pref_nid_locked(lpni,
1737 lpni_healthv = atomic_read(&lpni->lpni_healthv);
1739 CDEBUG(D_NET, "%s ni_is_pref = %d\n",
1740 libcfs_nid2str(best_ni->ni_nid), ni_is_pref);
1743 CDEBUG(D_NET, "%s c:[%d, %d], s:[%d, %d]\n",
1744 libcfs_nid2str(lpni->lpni_nid),
1745 lpni->lpni_txcredits, best_lpni_credits,
1746 lpni->lpni_seq, best_lpni->lpni_seq);
1748 /* pick the healthiest peer ni */
1749 if (lpni_healthv < best_lpni_healthv) {
1751 } else if (lpni_healthv > best_lpni_healthv) {
1752 best_lpni_healthv = lpni_healthv;
1753 /* if this is a preferred peer use it */
1754 } else if (!preferred && ni_is_pref) {
1756 } else if (preferred && !ni_is_pref) {
1758 * this is not the preferred peer so let's ignore
1762 } else if (lpni->lpni_txcredits < best_lpni_credits) {
1764 * We already have a peer that has more credits
1765 * available than this one. No need to consider
1766 * this peer further.
1769 } else if (lpni->lpni_txcredits == best_lpni_credits) {
1771 * The best peer found so far and the current peer
1772 * have the same number of available credits let's
1773 * make sure to select between them using Round
1777 if (best_lpni->lpni_seq <= lpni->lpni_seq)
1783 best_lpni_credits = lpni->lpni_txcredits;
1786 /* if we still can't find a peer ni then we can't reach it */
1788 __u32 net_id = (peer_net) ? peer_net->lpn_net_id :
1789 LNET_NIDNET(dst_nid);
1790 CDEBUG(D_NET, "no peer_ni found on peer net %s\n",
1791 libcfs_net2str(net_id));
1795 CDEBUG(D_NET, "sd_best_lpni = %s\n",
1796 libcfs_nid2str(best_lpni->lpni_nid));
1802 * Prerequisite: the best_ni should already be set in the sd
1804 static inline struct lnet_peer_ni *
1805 lnet_find_best_lpni_on_net(struct lnet_send_data *sd, struct lnet_peer *peer,
1808 struct lnet_peer_net *peer_net;
1811 * The gateway is Multi-Rail capable so now we must select the
1814 peer_net = lnet_peer_get_net_locked(peer, net_id);
1817 CERROR("gateway peer %s has no NI on net %s\n",
1818 libcfs_nid2str(peer->lp_primary_nid),
1819 libcfs_net2str(net_id));
1823 return lnet_select_peer_ni(sd, peer, peer_net);
1827 lnet_set_non_mr_pref_nid(struct lnet_send_data *sd)
1829 if (sd->sd_send_case & NMR_DST &&
1830 sd->sd_msg->msg_type != LNET_MSG_REPLY &&
1831 sd->sd_msg->msg_type != LNET_MSG_ACK &&
1832 sd->sd_best_lpni->lpni_pref_nnids == 0) {
1833 CDEBUG(D_NET, "Setting preferred local NID %s on NMR peer %s\n",
1834 libcfs_nid2str(sd->sd_best_ni->ni_nid),
1835 libcfs_nid2str(sd->sd_best_lpni->lpni_nid));
1836 lnet_peer_ni_set_non_mr_pref_nid(sd->sd_best_lpni,
1837 sd->sd_best_ni->ni_nid);
1846 * use the source and destination NIDs as the pathway
1849 lnet_handle_spec_local_nmr_dst(struct lnet_send_data *sd)
1851 /* the destination lpni is set before we get here. */
1854 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
1855 if (!sd->sd_best_ni) {
1856 CERROR("Can't send to %s: src %s is not a "
1857 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
1858 libcfs_nid2str(sd->sd_src_nid));
1863 * the preferred NID will only be set for NMR peers
1865 lnet_set_non_mr_pref_nid(sd);
1867 return lnet_handle_send(sd);
1875 * Run the selection algorithm on the peer NIs unless we're sending
1876 * a response, in this case just send to the destination
1879 lnet_handle_spec_local_mr_dst(struct lnet_send_data *sd)
1881 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
1882 if (!sd->sd_best_ni) {
1883 CERROR("Can't send to %s: src %s is not a "
1884 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
1885 libcfs_nid2str(sd->sd_src_nid));
1890 * only run the selection algorithm to pick the peer_ni if we're
1891 * sending a GET or a PUT. Responses are sent to the same
1892 * destination NID provided.
1894 if (!(sd->sd_send_case & SND_RESP)) {
1896 lnet_find_best_lpni_on_net(sd, sd->sd_peer,
1897 sd->sd_best_ni->ni_net->net_id);
1900 if (sd->sd_best_lpni)
1901 return lnet_handle_send(sd);
1903 CERROR("can't send to %s. no NI on %s\n",
1904 libcfs_nid2str(sd->sd_dst_nid),
1905 libcfs_net2str(sd->sd_best_ni->ni_net->net_id));
1907 return -EHOSTUNREACH;
1911 lnet_find_best_ni_on_spec_net(struct lnet_ni *cur_best_ni,
1912 struct lnet_peer *peer,
1913 struct lnet_peer_net *peer_net,
1917 struct lnet_net *local_net;
1918 struct lnet_ni *best_ni;
1920 local_net = lnet_get_net_locked(peer_net->lpn_net_id);
1925 * Iterate through the NIs in this local Net and select
1926 * the NI to send from. The selection is determined by
1927 * these 3 criterion in the following priority:
1929 * 2. NI available credits
1932 best_ni = lnet_get_best_ni(local_net, cur_best_ni,
1933 peer, peer_net, cpt);
1935 if (incr_seq && best_ni)
1942 lnet_handle_find_routed_path(struct lnet_send_data *sd,
1944 struct lnet_peer_ni **gw_lpni,
1945 struct lnet_peer **gw_peer)
1947 struct lnet_peer_ni *gw;
1948 lnet_nid_t src_nid = sd->sd_src_nid;
1950 gw = lnet_find_route_locked(NULL, LNET_NIDNET(dst_nid),
1953 CERROR("no route to %s from %s\n",
1954 libcfs_nid2str(dst_nid), libcfs_nid2str(src_nid));
1955 return -EHOSTUNREACH;
1958 /* get the peer of the gw_ni */
1959 LASSERT(gw->lpni_peer_net);
1960 LASSERT(gw->lpni_peer_net->lpn_peer);
1962 *gw_peer = gw->lpni_peer_net->lpn_peer;
1964 if (!sd->sd_best_ni)
1965 sd->sd_best_ni = lnet_find_best_ni_on_spec_net(NULL, *gw_peer,
1970 if (!sd->sd_best_ni) {
1971 CERROR("Internal Error. Expected local ni on %s "
1972 "but non found :%s\n",
1973 libcfs_net2str(gw->lpni_peer_net->lpn_net_id),
1974 libcfs_nid2str(sd->sd_src_nid));
1979 * if gw is MR let's find its best peer_ni
1981 if (lnet_peer_is_multi_rail(*gw_peer)) {
1982 gw = lnet_find_best_lpni_on_net(sd, *gw_peer,
1983 sd->sd_best_ni->ni_net->net_id);
1985 * We've already verified that the gw has an NI on that
1986 * desired net, but we're not finding it. Something is
1990 CERROR("Internal Error. Route expected to %s from %s\n",
1991 libcfs_nid2str(dst_nid),
1992 libcfs_nid2str(src_nid));
2007 * Remote destination
2008 * Non-MR destination
2012 * Remote destination
2015 * The handling of these two cases is similar. Even though the destination
2016 * can be MR or non-MR, we'll deal directly with the router.
2019 lnet_handle_spec_router_dst(struct lnet_send_data *sd)
2022 struct lnet_peer_ni *gw_lpni = NULL;
2023 struct lnet_peer *gw_peer = NULL;
2026 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
2027 if (!sd->sd_best_ni) {
2028 CERROR("Can't send to %s: src %s is not a "
2029 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
2030 libcfs_nid2str(sd->sd_src_nid));
2034 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2039 if (sd->sd_send_case & NMR_DST)
2041 * since the final destination is non-MR let's set its preferred
2042 * NID before we send
2044 lnet_set_non_mr_pref_nid(sd);
2047 * We're going to send to the gw found so let's set its
2050 sd->sd_peer = gw_peer;
2051 sd->sd_best_lpni = gw_lpni;
2053 return lnet_handle_send(sd);
2057 lnet_find_best_ni_on_local_net(struct lnet_peer *peer, int md_cpt)
2059 struct lnet_peer_net *peer_net = NULL;
2060 struct lnet_ni *best_ni = NULL;
2063 * The peer can have multiple interfaces, some of them can be on
2064 * the local network and others on a routed network. We should
2065 * prefer the local network. However if the local network is not
2066 * available then we need to try the routed network
2069 /* go through all the peer nets and find the best_ni */
2070 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_peer_nets) {
2072 * The peer's list of nets can contain non-local nets. We
2073 * want to only examine the local ones.
2075 if (!lnet_get_net_locked(peer_net->lpn_net_id))
2077 best_ni = lnet_find_best_ni_on_spec_net(best_ni, peer,
2078 peer_net, md_cpt, false);
2082 /* increment sequence number so we can round robin */
2088 static struct lnet_ni *
2089 lnet_find_existing_preferred_best_ni(struct lnet_send_data *sd)
2091 struct lnet_ni *best_ni = NULL;
2092 struct lnet_peer_net *peer_net;
2093 struct lnet_peer *peer = sd->sd_peer;
2094 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
2095 struct lnet_peer_ni *lpni;
2096 int cpt = sd->sd_cpt;
2099 * We must use a consistent source address when sending to a
2100 * non-MR peer. However, a non-MR peer can have multiple NIDs
2101 * on multiple networks, and we may even need to talk to this
2102 * peer on multiple networks -- certain types of
2103 * load-balancing configuration do this.
2105 * So we need to pick the NI the peer prefers for this
2106 * particular network.
2109 /* Get the target peer_ni */
2110 peer_net = lnet_peer_get_net_locked(peer,
2111 LNET_NIDNET(best_lpni->lpni_nid));
2112 LASSERT(peer_net != NULL);
2113 list_for_each_entry(lpni, &peer_net->lpn_peer_nis,
2115 if (lpni->lpni_pref_nnids == 0)
2117 LASSERT(lpni->lpni_pref_nnids == 1);
2118 best_ni = lnet_nid2ni_locked(
2119 lpni->lpni_pref.nid, cpt);
2126 /* Prerequisite: sd->sd_peer and sd->sd_best_lpni should be set */
2128 lnet_select_preferred_best_ni(struct lnet_send_data *sd)
2130 struct lnet_ni *best_ni = NULL;
2131 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
2134 * We must use a consistent source address when sending to a
2135 * non-MR peer. However, a non-MR peer can have multiple NIDs
2136 * on multiple networks, and we may even need to talk to this
2137 * peer on multiple networks -- certain types of
2138 * load-balancing configuration do this.
2140 * So we need to pick the NI the peer prefers for this
2141 * particular network.
2144 best_ni = lnet_find_existing_preferred_best_ni(sd);
2146 /* if best_ni is still not set just pick one */
2149 lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2150 sd->sd_best_lpni->lpni_peer_net,
2151 sd->sd_md_cpt, true);
2152 /* If there is no best_ni we don't have a route */
2154 CERROR("no path to %s from net %s\n",
2155 libcfs_nid2str(best_lpni->lpni_nid),
2156 libcfs_net2str(best_lpni->lpni_net->net_id));
2157 return -EHOSTUNREACH;
2161 sd->sd_best_ni = best_ni;
2163 /* Set preferred NI if necessary. */
2164 lnet_set_non_mr_pref_nid(sd);
2171 * Source not specified
2175 * always use the same source NID for NMR peers
2176 * If we've talked to that peer before then we already have a preferred
2177 * source NI associated with it. Otherwise, we select a preferred local NI
2178 * and store it in the peer
2181 lnet_handle_any_local_nmr_dst(struct lnet_send_data *sd)
2185 /* sd->sd_best_lpni is already set to the final destination */
2188 * At this point we should've created the peer ni and peer. If we
2189 * can't find it, then something went wrong. Instead of assert
2190 * output a relevant message and fail the send
2192 if (!sd->sd_best_lpni) {
2193 CERROR("Internal fault. Unable to send msg %s to %s. "
2195 lnet_msgtyp2str(sd->sd_msg->msg_type),
2196 libcfs_nid2str(sd->sd_dst_nid));
2200 rc = lnet_select_preferred_best_ni(sd);
2202 rc = lnet_handle_send(sd);
2208 lnet_handle_any_mr_dsta(struct lnet_send_data *sd)
2211 * NOTE we've already handled the remote peer case. So we only
2212 * need to worry about the local case here.
2214 * if we're sending a response, ACK or reply, we need to send it
2215 * to the destination NID given to us. At this point we already
2216 * have the peer_ni we're suppose to send to, so just find the
2217 * best_ni on the peer net and use that. Since we're sending to an
2218 * MR peer then we can just run the selection algorithm on our
2219 * local NIs and pick the best one.
2221 if (sd->sd_send_case & SND_RESP) {
2223 lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2224 sd->sd_best_lpni->lpni_peer_net,
2225 sd->sd_md_cpt, true);
2227 if (!sd->sd_best_ni) {
2229 * We're not going to deal with not able to send
2230 * a response to the provided final destination
2232 CERROR("Can't send response to %s. "
2233 "No local NI available\n",
2234 libcfs_nid2str(sd->sd_dst_nid));
2235 return -EHOSTUNREACH;
2238 return lnet_handle_send(sd);
2242 * If we get here that means we're sending a fresh request, PUT or
2243 * GET, so we need to run our standard selection algorithm.
2244 * First find the best local interface that's on any of the peer's
2247 sd->sd_best_ni = lnet_find_best_ni_on_local_net(sd->sd_peer,
2249 if (sd->sd_best_ni) {
2251 lnet_find_best_lpni_on_net(sd, sd->sd_peer,
2252 sd->sd_best_ni->ni_net->net_id);
2255 * if we're successful in selecting a peer_ni on the local
2256 * network, then send to it. Otherwise fall through and
2257 * try and see if we can reach it over another routed
2260 if (sd->sd_best_lpni) {
2262 * in case we initially started with a routed
2263 * destination, let's reset to local
2265 sd->sd_send_case &= ~REMOTE_DST;
2266 sd->sd_send_case |= LOCAL_DST;
2267 return lnet_handle_send(sd);
2270 CERROR("Internal Error. Expected to have a best_lpni: "
2272 libcfs_nid2str(sd->sd_src_nid),
2273 libcfs_nid2str(sd->sd_dst_nid));
2279 * Peer doesn't have a local network. Let's see if there is
2280 * a remote network we can reach it on.
2282 return PASS_THROUGH;
2287 * Source NID not specified
2292 * Source NID not speified
2293 * Remote destination
2296 * In both of these cases if we're sending a response, ACK or REPLY, then
2297 * we need to send to the destination NID provided.
2299 * In the remote case let's deal with MR routers.
2304 lnet_handle_any_mr_dst(struct lnet_send_data *sd)
2307 struct lnet_peer *gw_peer = NULL;
2308 struct lnet_peer_ni *gw_lpni = NULL;
2311 * handle sending a response to a remote peer here so we don't
2312 * have to worry about it if we hit lnet_handle_any_mr_dsta()
2314 if (sd->sd_send_case & REMOTE_DST &&
2315 sd->sd_send_case & SND_RESP) {
2316 struct lnet_peer_ni *gw;
2317 struct lnet_peer *gw_peer;
2319 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw,
2322 CERROR("Can't send response to %s. "
2323 "No route available\n",
2324 libcfs_nid2str(sd->sd_dst_nid));
2325 return -EHOSTUNREACH;
2328 sd->sd_best_lpni = gw;
2329 sd->sd_peer = gw_peer;
2331 return lnet_handle_send(sd);
2335 * Even though the NID for the peer might not be on a local network,
2336 * since the peer is MR there could be other interfaces on the
2337 * local network. In that case we'd still like to prefer the local
2338 * network over the routed network. If we're unable to do that
2339 * then we select the best router among the different routed networks,
2340 * and if the router is MR then we can deal with it as such.
2342 rc = lnet_handle_any_mr_dsta(sd);
2343 if (rc != PASS_THROUGH)
2347 * TODO; One possible enhancement is to run the selection
2348 * algorithm on the peer. However for remote peers the credits are
2349 * not decremented, so we'll be basically going over the peer NIs
2350 * in round robin. An MR router will run the selection algorithm
2351 * on the next-hop interfaces.
2353 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2358 sd->sd_send_case &= ~LOCAL_DST;
2359 sd->sd_send_case |= REMOTE_DST;
2361 sd->sd_peer = gw_peer;
2362 sd->sd_best_lpni = gw_lpni;
2364 return lnet_handle_send(sd);
2368 * Source not specified
2369 * Remote destination
2372 * Must send to the specified peer NID using the same source NID that
2373 * we've used before. If it's the first time to talk to that peer then
2374 * find the source NI and assign it as preferred to that peer
2377 lnet_handle_any_router_nmr_dst(struct lnet_send_data *sd)
2380 struct lnet_peer_ni *gw_lpni = NULL;
2381 struct lnet_peer *gw_peer = NULL;
2384 * Let's set if we have a preferred NI to talk to this NMR peer
2386 sd->sd_best_ni = lnet_find_existing_preferred_best_ni(sd);
2389 * find the router and that'll find the best NI if we didn't find
2392 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2398 * set the best_ni we've chosen as the preferred one for
2401 lnet_set_non_mr_pref_nid(sd);
2403 /* we'll be sending to the gw */
2404 sd->sd_best_lpni = gw_lpni;
2405 sd->sd_peer = gw_peer;
2407 return lnet_handle_send(sd);
2411 lnet_handle_send_case_locked(struct lnet_send_data *sd)
2414 * turn off the SND_RESP bit.
2415 * It will be checked in the case handling
2417 __u32 send_case = sd->sd_send_case &= ~SND_RESP ;
2419 CDEBUG(D_NET, "Source %s%s to %s %s %s destination\n",
2420 (send_case & SRC_SPEC) ? "Specified: " : "ANY",
2421 (send_case & SRC_SPEC) ? libcfs_nid2str(sd->sd_src_nid) : "",
2422 (send_case & MR_DST) ? "MR: " : "NMR: ",
2423 libcfs_nid2str(sd->sd_dst_nid),
2424 (send_case & LOCAL_DST) ? "local" : "routed");
2426 switch (send_case) {
2428 * For all cases where the source is specified, we should always
2429 * use the destination NID, whether it's an MR destination or not,
2430 * since we're continuing a series of related messages for the
2433 case SRC_SPEC_LOCAL_NMR_DST:
2434 return lnet_handle_spec_local_nmr_dst(sd);
2435 case SRC_SPEC_LOCAL_MR_DST:
2436 return lnet_handle_spec_local_mr_dst(sd);
2437 case SRC_SPEC_ROUTER_NMR_DST:
2438 case SRC_SPEC_ROUTER_MR_DST:
2439 return lnet_handle_spec_router_dst(sd);
2440 case SRC_ANY_LOCAL_NMR_DST:
2441 return lnet_handle_any_local_nmr_dst(sd);
2442 case SRC_ANY_LOCAL_MR_DST:
2443 case SRC_ANY_ROUTER_MR_DST:
2444 return lnet_handle_any_mr_dst(sd);
2445 case SRC_ANY_ROUTER_NMR_DST:
2446 return lnet_handle_any_router_nmr_dst(sd);
2448 CERROR("Unknown send case\n");
2454 lnet_select_pathway(lnet_nid_t src_nid, lnet_nid_t dst_nid,
2455 struct lnet_msg *msg, lnet_nid_t rtr_nid)
2457 struct lnet_peer_ni *lpni;
2458 struct lnet_peer *peer;
2459 struct lnet_send_data send_data;
2462 __u32 send_case = 0;
2464 memset(&send_data, 0, sizeof(send_data));
2467 * get an initial CPT to use for locking. The idea here is not to
2468 * serialize the calls to select_pathway, so that as many
2469 * operations can run concurrently as possible. To do that we use
2470 * the CPT where this call is being executed. Later on when we
2471 * determine the CPT to use in lnet_message_commit, we switch the
2472 * lock and check if there was any configuration change. If none,
2473 * then we proceed, if there is, then we restart the operation.
2475 cpt = lnet_net_lock_current();
2477 md_cpt = lnet_cpt_of_md(msg->msg_md, msg->msg_offset);
2478 if (md_cpt == CFS_CPT_ANY)
2484 * If we're being asked to send to the loopback interface, there
2485 * is no need to go through any selection. We can just shortcut
2486 * the entire process and send over lolnd
2488 if (LNET_NETTYP(LNET_NIDNET(dst_nid)) == LOLND) {
2489 /* No send credit hassles with LOLND */
2490 lnet_ni_addref_locked(the_lnet.ln_loni, cpt);
2491 msg->msg_hdr.dest_nid = cpu_to_le64(the_lnet.ln_loni->ni_nid);
2492 if (!msg->msg_routing)
2493 msg->msg_hdr.src_nid =
2494 cpu_to_le64(the_lnet.ln_loni->ni_nid);
2495 msg->msg_target.nid = the_lnet.ln_loni->ni_nid;
2496 lnet_msg_commit(msg, cpt);
2497 msg->msg_txni = the_lnet.ln_loni;
2498 lnet_net_unlock(cpt);
2500 return LNET_CREDIT_OK;
2504 * find an existing peer_ni, or create one and mark it as having been
2505 * created due to network traffic. This call will create the
2506 * peer->peer_net->peer_ni tree.
2508 lpni = lnet_nid2peerni_locked(dst_nid, LNET_NID_ANY, cpt);
2510 lnet_net_unlock(cpt);
2511 return PTR_ERR(lpni);
2515 * Cache the original src_nid. If we need to resend the message
2516 * then we'll need to know whether the src_nid was originally
2517 * specified for this message. If it was originally specified,
2518 * then we need to keep using the same src_nid since it's
2519 * continuing the same sequence of messages.
2521 msg->msg_src_nid_param = src_nid;
2524 * Now that we have a peer_ni, check if we want to discover
2525 * the peer. Traffic to the LNET_RESERVED_PORTAL should not
2526 * trigger discovery.
2528 peer = lpni->lpni_peer_net->lpn_peer;
2529 if (lnet_msg_discovery(msg) && !lnet_peer_is_uptodate(peer)) {
2530 lnet_nid_t primary_nid;
2531 rc = lnet_discover_peer_locked(lpni, cpt, false);
2533 lnet_peer_ni_decref_locked(lpni);
2534 lnet_net_unlock(cpt);
2537 /* The peer may have changed. */
2538 peer = lpni->lpni_peer_net->lpn_peer;
2539 /* queue message and return */
2540 msg->msg_rtr_nid_param = rtr_nid;
2541 msg->msg_sending = 0;
2542 list_add_tail(&msg->msg_list, &peer->lp_dc_pendq);
2543 lnet_peer_ni_decref_locked(lpni);
2544 primary_nid = peer->lp_primary_nid;
2545 lnet_net_unlock(cpt);
2547 CDEBUG(D_NET, "%s pending discovery\n",
2548 libcfs_nid2str(primary_nid));
2550 return LNET_DC_WAIT;
2552 lnet_peer_ni_decref_locked(lpni);
2554 /* If peer is not healthy then can not send anything to it */
2555 if (!lnet_is_peer_healthy_locked(peer)) {
2556 lnet_net_unlock(cpt);
2557 return -EHOSTUNREACH;
2561 * Identify the different send cases
2563 if (src_nid == LNET_NID_ANY)
2564 send_case |= SRC_ANY;
2566 send_case |= SRC_SPEC;
2568 if (lnet_get_net_locked(LNET_NIDNET(dst_nid)))
2569 send_case |= LOCAL_DST;
2571 send_case |= REMOTE_DST;
2574 * if this is a non-MR peer or if we're recovering a peer ni then
2575 * let's consider this an NMR case so we can hit the destination
2578 if (!lnet_peer_is_multi_rail(peer) || msg->msg_recovery)
2579 send_case |= NMR_DST;
2581 send_case |= MR_DST;
2583 if (msg->msg_type == LNET_MSG_REPLY ||
2584 msg->msg_type == LNET_MSG_ACK)
2585 send_case |= SND_RESP;
2587 /* assign parameters to the send_data */
2588 send_data.sd_msg = msg;
2589 send_data.sd_rtr_nid = rtr_nid;
2590 send_data.sd_src_nid = src_nid;
2591 send_data.sd_dst_nid = dst_nid;
2592 send_data.sd_best_lpni = lpni;
2594 * keep a pointer to the final destination in case we're going to
2595 * route, so we'll need to access it later
2597 send_data.sd_final_dst_lpni = lpni;
2598 send_data.sd_peer = peer;
2599 send_data.sd_md_cpt = md_cpt;
2600 send_data.sd_cpt = cpt;
2601 send_data.sd_send_case = send_case;
2603 rc = lnet_handle_send_case_locked(&send_data);
2605 if (rc == REPEAT_SEND)
2608 lnet_net_unlock(send_data.sd_cpt);
2614 lnet_send(lnet_nid_t src_nid, struct lnet_msg *msg, lnet_nid_t rtr_nid)
2616 lnet_nid_t dst_nid = msg->msg_target.nid;
2620 * NB: rtr_nid is set to LNET_NID_ANY for all current use-cases,
2621 * but we might want to use pre-determined router for ACK/REPLY
2624 /* NB: ni != NULL == interface pre-determined (ACK/REPLY) */
2625 LASSERT(msg->msg_txpeer == NULL);
2626 LASSERT(msg->msg_txni == NULL);
2627 LASSERT(!msg->msg_sending);
2628 LASSERT(!msg->msg_target_is_router);
2629 LASSERT(!msg->msg_receiving);
2631 msg->msg_sending = 1;
2633 LASSERT(!msg->msg_tx_committed);
2635 rc = lnet_select_pathway(src_nid, dst_nid, msg, rtr_nid);
2639 if (rc == LNET_CREDIT_OK)
2640 lnet_ni_send(msg->msg_txni, msg);
2642 /* rc == LNET_CREDIT_OK or LNET_CREDIT_WAIT or LNET_DC_WAIT */
2646 enum lnet_mt_event_type {
2647 MT_TYPE_LOCAL_NI = 0,
2651 struct lnet_mt_event_info {
2652 enum lnet_mt_event_type mt_type;
2657 lnet_resend_pending_msgs_locked(struct list_head *resendq, int cpt)
2659 struct lnet_msg *msg;
2661 while (!list_empty(resendq)) {
2662 struct lnet_peer_ni *lpni;
2664 msg = list_entry(resendq->next, struct lnet_msg,
2667 list_del_init(&msg->msg_list);
2669 lpni = lnet_find_peer_ni_locked(msg->msg_hdr.dest_nid);
2671 lnet_net_unlock(cpt);
2672 CERROR("Expected that a peer is already created for %s\n",
2673 libcfs_nid2str(msg->msg_hdr.dest_nid));
2674 msg->msg_no_resend = true;
2675 lnet_finalize(msg, -EFAULT);
2678 struct lnet_peer *peer;
2680 lnet_nid_t src_nid = LNET_NID_ANY;
2683 * if this message is not being routed and the
2684 * peer is non-MR then we must use the same
2685 * src_nid that was used in the original send.
2686 * Otherwise if we're routing the message (IE
2687 * we're a router) then we can use any of our
2688 * local interfaces. It doesn't matter to the
2689 * final destination.
2691 peer = lpni->lpni_peer_net->lpn_peer;
2692 if (!msg->msg_routing &&
2693 !lnet_peer_is_multi_rail(peer))
2694 src_nid = le64_to_cpu(msg->msg_hdr.src_nid);
2697 * If we originally specified a src NID, then we
2698 * must attempt to reuse it in the resend as well.
2700 if (msg->msg_src_nid_param != LNET_NID_ANY)
2701 src_nid = msg->msg_src_nid_param;
2702 lnet_peer_ni_decref_locked(lpni);
2704 lnet_net_unlock(cpt);
2705 rc = lnet_send(src_nid, msg, LNET_NID_ANY);
2707 CERROR("Error sending %s to %s: %d\n",
2708 lnet_msgtyp2str(msg->msg_type),
2709 libcfs_id2str(msg->msg_target), rc);
2710 msg->msg_no_resend = true;
2711 lnet_finalize(msg, rc);
2719 lnet_resend_pending_msgs(void)
2723 cfs_cpt_for_each(i, lnet_cpt_table()) {
2725 lnet_resend_pending_msgs_locked(the_lnet.ln_mt_resendqs[i], i);
2730 /* called with cpt and ni_lock held */
2732 lnet_unlink_ni_recovery_mdh_locked(struct lnet_ni *ni, int cpt)
2734 struct lnet_handle_md recovery_mdh;
2736 LNetInvalidateMDHandle(&recovery_mdh);
2738 if (ni->ni_state & LNET_NI_STATE_RECOVERY_PENDING) {
2739 recovery_mdh = ni->ni_ping_mdh;
2740 LNetInvalidateMDHandle(&ni->ni_ping_mdh);
2743 lnet_net_unlock(cpt);
2744 if (!LNetMDHandleIsInvalid(recovery_mdh))
2745 LNetMDUnlink(recovery_mdh);
2751 lnet_recover_local_nis(void)
2753 struct lnet_mt_event_info *ev_info;
2754 struct list_head processed_list;
2755 struct list_head local_queue;
2756 struct lnet_handle_md mdh;
2757 struct lnet_ni *tmp;
2763 INIT_LIST_HEAD(&local_queue);
2764 INIT_LIST_HEAD(&processed_list);
2767 * splice the recovery queue on a local queue. We will iterate
2768 * through the local queue and update it as needed. Once we're
2769 * done with the traversal, we'll splice the local queue back on
2770 * the head of the ln_mt_localNIRecovq. Any newly added local NIs
2771 * will be traversed in the next iteration.
2774 list_splice_init(&the_lnet.ln_mt_localNIRecovq,
2778 list_for_each_entry_safe(ni, tmp, &local_queue, ni_recovery) {
2780 * if an NI is being deleted or it is now healthy, there
2781 * is no need to keep it around in the recovery queue.
2782 * The monitor thread is the only thread responsible for
2783 * removing the NI from the recovery queue.
2784 * Multiple threads can be adding NIs to the recovery
2787 healthv = atomic_read(&ni->ni_healthv);
2791 if (!(ni->ni_state & LNET_NI_STATE_ACTIVE) ||
2792 healthv == LNET_MAX_HEALTH_VALUE) {
2793 list_del_init(&ni->ni_recovery);
2794 lnet_unlink_ni_recovery_mdh_locked(ni, 0);
2796 lnet_ni_decref_locked(ni, 0);
2804 CDEBUG(D_NET, "attempting to recover local ni: %s\n",
2805 libcfs_nid2str(ni->ni_nid));
2808 if (!(ni->ni_state & LNET_NI_STATE_RECOVERY_PENDING)) {
2809 ni->ni_state |= LNET_NI_STATE_RECOVERY_PENDING;
2812 LIBCFS_ALLOC(ev_info, sizeof(*ev_info));
2814 CERROR("out of memory. Can't recover %s\n",
2815 libcfs_nid2str(ni->ni_nid));
2817 ni->ni_state &= ~LNET_NI_STATE_RECOVERY_PENDING;
2822 mdh = ni->ni_ping_mdh;
2824 * Invalidate the ni mdh in case it's deleted.
2825 * We'll unlink the mdh in this case below.
2827 LNetInvalidateMDHandle(&ni->ni_ping_mdh);
2831 * remove the NI from the local queue and drop the
2832 * reference count to it while we're recovering
2833 * it. The reason for that, is that the NI could
2834 * be deleted, and the way the code is structured
2835 * is if we don't drop the NI, then the deletion
2836 * code will enter a loop waiting for the
2837 * reference count to be removed while holding the
2838 * ln_mutex_lock(). When we look up the peer to
2839 * send to in lnet_select_pathway() we will try to
2840 * lock the ln_mutex_lock() as well, leading to
2841 * a deadlock. By dropping the refcount and
2842 * removing it from the list, we allow for the NI
2843 * to be removed, then we use the cached NID to
2844 * look it up again. If it's gone, then we just
2845 * continue examining the rest of the queue.
2848 list_del_init(&ni->ni_recovery);
2849 lnet_ni_decref_locked(ni, 0);
2852 ev_info->mt_type = MT_TYPE_LOCAL_NI;
2853 ev_info->mt_nid = nid;
2854 rc = lnet_send_ping(nid, &mdh, LNET_INTERFACES_MIN,
2855 ev_info, the_lnet.ln_mt_eqh, true);
2856 /* lookup the nid again */
2858 ni = lnet_nid2ni_locked(nid, 0);
2861 * the NI has been deleted when we dropped
2869 * Same note as in lnet_recover_peer_nis(). When
2870 * we're sending the ping, the NI is free to be
2871 * deleted or manipulated. By this point it
2872 * could've been added back on the recovery queue,
2873 * and a refcount taken on it.
2874 * So we can't just add it blindly again or we'll
2875 * corrupt the queue. We must check under lock if
2876 * it's not on any list and if not then add it
2877 * to the processed list, which will eventually be
2878 * spliced back on to the recovery queue.
2880 ni->ni_ping_mdh = mdh;
2881 if (list_empty(&ni->ni_recovery)) {
2882 list_add_tail(&ni->ni_recovery, &processed_list);
2883 lnet_ni_addref_locked(ni, 0);
2889 ni->ni_state &= ~LNET_NI_STATE_RECOVERY_PENDING;
2895 * put back the remaining NIs on the ln_mt_localNIRecovq to be
2896 * reexamined in the next iteration.
2898 list_splice_init(&processed_list, &local_queue);
2900 list_splice(&local_queue, &the_lnet.ln_mt_localNIRecovq);
2904 static struct list_head **
2905 lnet_create_array_of_queues(void)
2907 struct list_head **qs;
2908 struct list_head *q;
2911 qs = cfs_percpt_alloc(lnet_cpt_table(),
2912 sizeof(struct list_head));
2914 CERROR("Failed to allocate queues\n");
2918 cfs_percpt_for_each(q, i, qs)
2925 lnet_resendqs_create(void)
2927 struct list_head **resendqs;
2928 resendqs = lnet_create_array_of_queues();
2933 lnet_net_lock(LNET_LOCK_EX);
2934 the_lnet.ln_mt_resendqs = resendqs;
2935 lnet_net_unlock(LNET_LOCK_EX);
2941 lnet_clean_local_ni_recoveryq(void)
2945 /* This is only called when the monitor thread has stopped */
2948 while (!list_empty(&the_lnet.ln_mt_localNIRecovq)) {
2949 ni = list_entry(the_lnet.ln_mt_localNIRecovq.next,
2950 struct lnet_ni, ni_recovery);
2951 list_del_init(&ni->ni_recovery);
2953 lnet_unlink_ni_recovery_mdh_locked(ni, 0);
2955 lnet_ni_decref_locked(ni, 0);
2962 lnet_unlink_lpni_recovery_mdh_locked(struct lnet_peer_ni *lpni, int cpt)
2964 struct lnet_handle_md recovery_mdh;
2966 LNetInvalidateMDHandle(&recovery_mdh);
2968 if (lpni->lpni_state & LNET_PEER_NI_RECOVERY_PENDING) {
2969 recovery_mdh = lpni->lpni_recovery_ping_mdh;
2970 LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
2972 spin_unlock(&lpni->lpni_lock);
2973 lnet_net_unlock(cpt);
2974 if (!LNetMDHandleIsInvalid(recovery_mdh))
2975 LNetMDUnlink(recovery_mdh);
2977 spin_lock(&lpni->lpni_lock);
2981 lnet_clean_peer_ni_recoveryq(void)
2983 struct lnet_peer_ni *lpni, *tmp;
2985 lnet_net_lock(LNET_LOCK_EX);
2987 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_mt_peerNIRecovq,
2989 list_del_init(&lpni->lpni_recovery);
2990 spin_lock(&lpni->lpni_lock);
2991 lnet_unlink_lpni_recovery_mdh_locked(lpni, LNET_LOCK_EX);
2992 spin_unlock(&lpni->lpni_lock);
2993 lnet_peer_ni_decref_locked(lpni);
2996 lnet_net_unlock(LNET_LOCK_EX);
3000 lnet_clean_resendqs(void)
3002 struct lnet_msg *msg, *tmp;
3003 struct list_head msgs;
3006 INIT_LIST_HEAD(&msgs);
3008 cfs_cpt_for_each(i, lnet_cpt_table()) {
3010 list_splice_init(the_lnet.ln_mt_resendqs[i], &msgs);
3012 list_for_each_entry_safe(msg, tmp, &msgs, msg_list) {
3013 list_del_init(&msg->msg_list);
3014 msg->msg_no_resend = true;
3015 lnet_finalize(msg, -ESHUTDOWN);
3019 cfs_percpt_free(the_lnet.ln_mt_resendqs);
3023 lnet_recover_peer_nis(void)
3025 struct lnet_mt_event_info *ev_info;
3026 struct list_head processed_list;
3027 struct list_head local_queue;
3028 struct lnet_handle_md mdh;
3029 struct lnet_peer_ni *lpni;
3030 struct lnet_peer_ni *tmp;
3035 INIT_LIST_HEAD(&local_queue);
3036 INIT_LIST_HEAD(&processed_list);
3039 * Always use cpt 0 for locking across all interactions with
3040 * ln_mt_peerNIRecovq
3043 list_splice_init(&the_lnet.ln_mt_peerNIRecovq,
3047 list_for_each_entry_safe(lpni, tmp, &local_queue,
3050 * The same protection strategy is used here as is in the
3051 * local recovery case.
3054 healthv = atomic_read(&lpni->lpni_healthv);
3055 spin_lock(&lpni->lpni_lock);
3056 if (lpni->lpni_state & LNET_PEER_NI_DELETING ||
3057 healthv == LNET_MAX_HEALTH_VALUE) {
3058 list_del_init(&lpni->lpni_recovery);
3059 lnet_unlink_lpni_recovery_mdh_locked(lpni, 0);
3060 spin_unlock(&lpni->lpni_lock);
3061 lnet_peer_ni_decref_locked(lpni);
3065 spin_unlock(&lpni->lpni_lock);
3069 * NOTE: we're racing with peer deletion from user space.
3070 * It's possible that a peer is deleted after we check its
3071 * state. In this case the recovery can create a new peer
3073 spin_lock(&lpni->lpni_lock);
3074 if (!(lpni->lpni_state & LNET_PEER_NI_RECOVERY_PENDING) &&
3075 !(lpni->lpni_state & LNET_PEER_NI_DELETING)) {
3076 lpni->lpni_state |= LNET_PEER_NI_RECOVERY_PENDING;
3077 spin_unlock(&lpni->lpni_lock);
3079 LIBCFS_ALLOC(ev_info, sizeof(*ev_info));
3081 CERROR("out of memory. Can't recover %s\n",
3082 libcfs_nid2str(lpni->lpni_nid));
3083 spin_lock(&lpni->lpni_lock);
3084 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3085 spin_unlock(&lpni->lpni_lock);
3089 /* look at the comments in lnet_recover_local_nis() */
3090 mdh = lpni->lpni_recovery_ping_mdh;
3091 LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
3092 nid = lpni->lpni_nid;
3094 list_del_init(&lpni->lpni_recovery);
3095 lnet_peer_ni_decref_locked(lpni);
3098 ev_info->mt_type = MT_TYPE_PEER_NI;
3099 ev_info->mt_nid = nid;
3100 rc = lnet_send_ping(nid, &mdh, LNET_INTERFACES_MIN,
3101 ev_info, the_lnet.ln_mt_eqh, true);
3104 * lnet_find_peer_ni_locked() grabs a refcount for
3105 * us. No need to take it explicitly.
3107 lpni = lnet_find_peer_ni_locked(nid);
3114 lpni->lpni_recovery_ping_mdh = mdh;
3116 * While we're unlocked the lpni could've been
3117 * readded on the recovery queue. In this case we
3118 * don't need to add it to the local queue, since
3119 * it's already on there and the thread that added
3120 * it would've incremented the refcount on the
3121 * peer, which means we need to decref the refcount
3122 * that was implicitly grabbed by find_peer_ni_locked.
3123 * Otherwise, if the lpni is still not on
3124 * the recovery queue, then we'll add it to the
3127 if (list_empty(&lpni->lpni_recovery))
3128 list_add_tail(&lpni->lpni_recovery, &processed_list);
3130 lnet_peer_ni_decref_locked(lpni);
3133 spin_lock(&lpni->lpni_lock);
3135 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3137 spin_unlock(&lpni->lpni_lock);
3140 list_splice_init(&processed_list, &local_queue);
3142 list_splice(&local_queue, &the_lnet.ln_mt_peerNIRecovq);
3147 lnet_monitor_thread(void *arg)
3150 * The monitor thread takes care of the following:
3151 * 1. Checks the aliveness of routers
3152 * 2. Checks if there are messages on the resend queue to resend
3154 * 3. Check if there are any NIs on the local recovery queue and
3156 * 4. Checks if there are any NIs on the remote recovery queue
3159 cfs_block_allsigs();
3161 while (the_lnet.ln_mt_state == LNET_MT_STATE_RUNNING) {
3162 if (lnet_router_checker_active())
3163 lnet_check_routers();
3165 lnet_resend_pending_msgs();
3167 lnet_recover_local_nis();
3169 lnet_recover_peer_nis();
3172 * TODO do we need to check if we should sleep without
3173 * timeout? Technically, an active system will always
3174 * have messages in flight so this check will always
3175 * evaluate to false. And on an idle system do we care
3176 * if we wake up every 1 second? Although, we've seen
3177 * cases where we get a complaint that an idle thread
3178 * is waking up unnecessarily.
3180 wait_event_interruptible_timeout(the_lnet.ln_mt_waitq,
3182 cfs_time_seconds(1));
3185 /* clean up the router checker */
3186 lnet_prune_rc_data(1);
3189 the_lnet.ln_mt_state = LNET_MT_STATE_SHUTDOWN;
3191 /* signal that the monitor thread is exiting */
3192 up(&the_lnet.ln_mt_signal);
3200 * Returns == 0 if success
3201 * Returns > 0 if LNetMDBind or prior fails
3202 * Returns < 0 if LNetGet fails
3205 lnet_send_ping(lnet_nid_t dest_nid,
3206 struct lnet_handle_md *mdh, int nnis,
3207 void *user_data, struct lnet_handle_eq eqh, bool recovery)
3209 struct lnet_md md = { NULL };
3210 struct lnet_process_id id;
3211 struct lnet_ping_buffer *pbuf;
3214 if (dest_nid == LNET_NID_ANY) {
3219 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
3225 /* initialize md content */
3226 md.start = &pbuf->pb_info;
3227 md.length = LNET_PING_INFO_SIZE(nnis);
3228 md.threshold = 2; /* GET/REPLY */
3230 md.options = LNET_MD_TRUNCATE;
3231 md.user_ptr = user_data;
3234 rc = LNetMDBind(md, LNET_UNLINK, mdh);
3236 lnet_ping_buffer_decref(pbuf);
3237 CERROR("Can't bind MD: %d\n", rc);
3238 rc = -rc; /* change the rc to positive */
3241 id.pid = LNET_PID_LUSTRE;
3244 rc = LNetGet(LNET_NID_ANY, *mdh, id,
3245 LNET_RESERVED_PORTAL,
3246 LNET_PROTO_PING_MATCHBITS, 0, recovery);
3249 goto fail_unlink_md;
3255 LNetInvalidateMDHandle(mdh);
3261 lnet_handle_recovery_reply(struct lnet_mt_event_info *ev_info,
3264 lnet_nid_t nid = ev_info->mt_nid;
3266 if (ev_info->mt_type == MT_TYPE_LOCAL_NI) {
3270 ni = lnet_nid2ni_locked(nid, 0);
3276 ni->ni_state &= ~LNET_NI_STATE_RECOVERY_PENDING;
3281 CERROR("local NI recovery failed with %d\n", status);
3285 * need to increment healthv for the ni here, because in
3286 * the lnet_finalize() path we don't have access to this
3287 * NI. And in order to get access to it, we'll need to
3288 * carry forward too much information.
3289 * In the peer case, it'll naturally be incremented
3291 lnet_inc_healthv(&ni->ni_healthv);
3293 struct lnet_peer_ni *lpni;
3296 cpt = lnet_net_lock_current();
3297 lpni = lnet_find_peer_ni_locked(nid);
3299 lnet_net_unlock(cpt);
3302 spin_lock(&lpni->lpni_lock);
3303 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3304 spin_unlock(&lpni->lpni_lock);
3305 lnet_peer_ni_decref_locked(lpni);
3306 lnet_net_unlock(cpt);
3309 CERROR("peer NI recovery failed with %d\n", status);
3314 lnet_mt_event_handler(struct lnet_event *event)
3316 struct lnet_mt_event_info *ev_info = event->md.user_ptr;
3317 struct lnet_ping_buffer *pbuf;
3319 /* TODO: remove assert */
3320 LASSERT(event->type == LNET_EVENT_REPLY ||
3321 event->type == LNET_EVENT_SEND ||
3322 event->type == LNET_EVENT_UNLINK);
3324 CDEBUG(D_NET, "Received event: %d status: %d\n", event->type,
3327 switch (event->type) {
3328 case LNET_EVENT_UNLINK:
3329 CDEBUG(D_NET, "%s recovery ping unlinked\n",
3330 libcfs_nid2str(ev_info->mt_nid));
3331 case LNET_EVENT_REPLY:
3332 lnet_handle_recovery_reply(ev_info, event->status);
3334 case LNET_EVENT_SEND:
3335 CDEBUG(D_NET, "%s recovery message sent %s:%d\n",
3336 libcfs_nid2str(ev_info->mt_nid),
3337 (event->status) ? "unsuccessfully" :
3338 "successfully", event->status);
3341 CERROR("Unexpected event: %d\n", event->type);
3344 if (event->unlinked) {
3345 LIBCFS_FREE(ev_info, sizeof(*ev_info));
3346 pbuf = LNET_PING_INFO_TO_BUFFER(event->md.start);
3347 lnet_ping_buffer_decref(pbuf);
3351 int lnet_monitor_thr_start(void)
3354 struct task_struct *task;
3356 if (the_lnet.ln_mt_state != LNET_MT_STATE_SHUTDOWN)
3359 rc = lnet_resendqs_create();
3363 rc = LNetEQAlloc(0, lnet_mt_event_handler, &the_lnet.ln_mt_eqh);
3365 CERROR("Can't allocate monitor thread EQ: %d\n", rc);
3369 /* Pre monitor thread start processing */
3370 rc = lnet_router_pre_mt_start();
3374 sema_init(&the_lnet.ln_mt_signal, 0);
3376 the_lnet.ln_mt_state = LNET_MT_STATE_RUNNING;
3377 task = kthread_run(lnet_monitor_thread, NULL, "monitor_thread");
3380 CERROR("Can't start monitor thread: %d\n", rc);
3384 /* post monitor thread start processing */
3385 lnet_router_post_mt_start();
3390 the_lnet.ln_mt_state = LNET_MT_STATE_STOPPING;
3391 /* block until event callback signals exit */
3392 down(&the_lnet.ln_mt_signal);
3394 lnet_router_cleanup();
3396 the_lnet.ln_mt_state = LNET_MT_STATE_SHUTDOWN;
3397 lnet_clean_local_ni_recoveryq();
3398 lnet_clean_peer_ni_recoveryq();
3399 lnet_clean_resendqs();
3400 LNetEQFree(the_lnet.ln_mt_eqh);
3401 LNetInvalidateEQHandle(&the_lnet.ln_mt_eqh);
3404 lnet_clean_local_ni_recoveryq();
3405 lnet_clean_peer_ni_recoveryq();
3406 lnet_clean_resendqs();
3410 void lnet_monitor_thr_stop(void)
3414 if (the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN)
3417 LASSERT(the_lnet.ln_mt_state == LNET_MT_STATE_RUNNING);
3418 the_lnet.ln_mt_state = LNET_MT_STATE_STOPPING;
3420 /* tell the monitor thread that we're shutting down */
3421 wake_up(&the_lnet.ln_mt_waitq);
3423 /* block until monitor thread signals that it's done */
3424 down(&the_lnet.ln_mt_signal);
3425 LASSERT(the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN);
3427 /* perform cleanup tasks */
3428 lnet_router_cleanup();
3429 lnet_clean_local_ni_recoveryq();
3430 lnet_clean_peer_ni_recoveryq();
3431 lnet_clean_resendqs();
3432 rc = LNetEQFree(the_lnet.ln_mt_eqh);
3438 lnet_drop_message(struct lnet_ni *ni, int cpt, void *private, unsigned int nob,
3442 lnet_incr_stats(&ni->ni_stats, msg_type, LNET_STATS_TYPE_DROP);
3443 the_lnet.ln_counters[cpt]->drop_count++;
3444 the_lnet.ln_counters[cpt]->drop_length += nob;
3445 lnet_net_unlock(cpt);
3447 lnet_ni_recv(ni, private, NULL, 0, 0, 0, nob);
3451 lnet_recv_put(struct lnet_ni *ni, struct lnet_msg *msg)
3453 struct lnet_hdr *hdr = &msg->msg_hdr;
3455 if (msg->msg_wanted != 0)
3456 lnet_setpayloadbuffer(msg);
3458 lnet_build_msg_event(msg, LNET_EVENT_PUT);
3460 /* Must I ACK? If so I'll grab the ack_wmd out of the header and put
3461 * it back into the ACK during lnet_finalize() */
3462 msg->msg_ack = (!lnet_is_wire_handle_none(&hdr->msg.put.ack_wmd) &&
3463 (msg->msg_md->md_options & LNET_MD_ACK_DISABLE) == 0);
3465 lnet_ni_recv(ni, msg->msg_private, msg, msg->msg_rx_delayed,
3466 msg->msg_offset, msg->msg_wanted, hdr->payload_length);
3470 lnet_parse_put(struct lnet_ni *ni, struct lnet_msg *msg)
3472 struct lnet_hdr *hdr = &msg->msg_hdr;
3473 struct lnet_match_info info;
3477 /* Convert put fields to host byte order */
3478 hdr->msg.put.match_bits = le64_to_cpu(hdr->msg.put.match_bits);
3479 hdr->msg.put.ptl_index = le32_to_cpu(hdr->msg.put.ptl_index);
3480 hdr->msg.put.offset = le32_to_cpu(hdr->msg.put.offset);
3482 /* Primary peer NID. */
3483 info.mi_id.nid = msg->msg_initiator;
3484 info.mi_id.pid = hdr->src_pid;
3485 info.mi_opc = LNET_MD_OP_PUT;
3486 info.mi_portal = hdr->msg.put.ptl_index;
3487 info.mi_rlength = hdr->payload_length;
3488 info.mi_roffset = hdr->msg.put.offset;
3489 info.mi_mbits = hdr->msg.put.match_bits;
3490 info.mi_cpt = lnet_cpt_of_nid(msg->msg_rxpeer->lpni_nid, ni);
3492 msg->msg_rx_ready_delay = ni->ni_net->net_lnd->lnd_eager_recv == NULL;
3493 ready_delay = msg->msg_rx_ready_delay;
3496 rc = lnet_ptl_match_md(&info, msg);
3501 case LNET_MATCHMD_OK:
3502 lnet_recv_put(ni, msg);
3505 case LNET_MATCHMD_NONE:
3507 /* no eager_recv or has already called it, should
3508 * have been attached on delayed list */
3511 rc = lnet_ni_eager_recv(ni, msg);
3518 case LNET_MATCHMD_DROP:
3519 CNETERR("Dropping PUT from %s portal %d match %llu"
3520 " offset %d length %d: %d\n",
3521 libcfs_id2str(info.mi_id), info.mi_portal,
3522 info.mi_mbits, info.mi_roffset, info.mi_rlength, rc);
3524 return -ENOENT; /* -ve: OK but no match */
3529 lnet_parse_get(struct lnet_ni *ni, struct lnet_msg *msg, int rdma_get)
3531 struct lnet_match_info info;
3532 struct lnet_hdr *hdr = &msg->msg_hdr;
3533 struct lnet_process_id source_id;
3534 struct lnet_handle_wire reply_wmd;
3537 /* Convert get fields to host byte order */
3538 hdr->msg.get.match_bits = le64_to_cpu(hdr->msg.get.match_bits);
3539 hdr->msg.get.ptl_index = le32_to_cpu(hdr->msg.get.ptl_index);
3540 hdr->msg.get.sink_length = le32_to_cpu(hdr->msg.get.sink_length);
3541 hdr->msg.get.src_offset = le32_to_cpu(hdr->msg.get.src_offset);
3543 source_id.nid = hdr->src_nid;
3544 source_id.pid = hdr->src_pid;
3545 /* Primary peer NID */
3546 info.mi_id.nid = msg->msg_initiator;
3547 info.mi_id.pid = hdr->src_pid;
3548 info.mi_opc = LNET_MD_OP_GET;
3549 info.mi_portal = hdr->msg.get.ptl_index;
3550 info.mi_rlength = hdr->msg.get.sink_length;
3551 info.mi_roffset = hdr->msg.get.src_offset;
3552 info.mi_mbits = hdr->msg.get.match_bits;
3553 info.mi_cpt = lnet_cpt_of_nid(msg->msg_rxpeer->lpni_nid, ni);
3555 rc = lnet_ptl_match_md(&info, msg);
3556 if (rc == LNET_MATCHMD_DROP) {
3557 CNETERR("Dropping GET from %s portal %d match %llu"
3558 " offset %d length %d\n",
3559 libcfs_id2str(info.mi_id), info.mi_portal,
3560 info.mi_mbits, info.mi_roffset, info.mi_rlength);
3561 return -ENOENT; /* -ve: OK but no match */
3564 LASSERT(rc == LNET_MATCHMD_OK);
3566 lnet_build_msg_event(msg, LNET_EVENT_GET);
3568 reply_wmd = hdr->msg.get.return_wmd;
3570 lnet_prep_send(msg, LNET_MSG_REPLY, source_id,
3571 msg->msg_offset, msg->msg_wanted);
3573 msg->msg_hdr.msg.reply.dst_wmd = reply_wmd;
3576 /* The LND completes the REPLY from her recv procedure */
3577 lnet_ni_recv(ni, msg->msg_private, msg, 0,
3578 msg->msg_offset, msg->msg_len, msg->msg_len);
3582 lnet_ni_recv(ni, msg->msg_private, NULL, 0, 0, 0, 0);
3583 msg->msg_receiving = 0;
3585 rc = lnet_send(ni->ni_nid, msg, LNET_NID_ANY);
3587 /* didn't get as far as lnet_ni_send() */
3588 CERROR("%s: Unable to send REPLY for GET from %s: %d\n",
3589 libcfs_nid2str(ni->ni_nid),
3590 libcfs_id2str(info.mi_id), rc);
3592 lnet_finalize(msg, rc);
3599 lnet_parse_reply(struct lnet_ni *ni, struct lnet_msg *msg)
3601 void *private = msg->msg_private;
3602 struct lnet_hdr *hdr = &msg->msg_hdr;
3603 struct lnet_process_id src = {0};
3604 struct lnet_libmd *md;
3609 cpt = lnet_cpt_of_cookie(hdr->msg.reply.dst_wmd.wh_object_cookie);
3612 src.nid = hdr->src_nid;
3613 src.pid = hdr->src_pid;
3615 /* NB handles only looked up by creator (no flips) */
3616 md = lnet_wire_handle2md(&hdr->msg.reply.dst_wmd);
3617 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
3618 CNETERR("%s: Dropping REPLY from %s for %s "
3620 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
3621 (md == NULL) ? "invalid" : "inactive",
3622 hdr->msg.reply.dst_wmd.wh_interface_cookie,
3623 hdr->msg.reply.dst_wmd.wh_object_cookie);
3624 if (md != NULL && md->md_me != NULL)
3625 CERROR("REPLY MD also attached to portal %d\n",
3626 md->md_me->me_portal);
3628 lnet_res_unlock(cpt);
3629 return -ENOENT; /* -ve: OK but no match */
3632 LASSERT(md->md_offset == 0);
3634 rlength = hdr->payload_length;
3635 mlength = MIN(rlength, (int)md->md_length);
3637 if (mlength < rlength &&
3638 (md->md_options & LNET_MD_TRUNCATE) == 0) {
3639 CNETERR("%s: Dropping REPLY from %s length %d "
3640 "for MD %#llx would overflow (%d)\n",
3641 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
3642 rlength, hdr->msg.reply.dst_wmd.wh_object_cookie,
3644 lnet_res_unlock(cpt);
3645 return -ENOENT; /* -ve: OK but no match */
3648 CDEBUG(D_NET, "%s: Reply from %s of length %d/%d into md %#llx\n",
3649 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
3650 mlength, rlength, hdr->msg.reply.dst_wmd.wh_object_cookie);
3652 lnet_msg_attach_md(msg, md, 0, mlength);
3655 lnet_setpayloadbuffer(msg);
3657 lnet_res_unlock(cpt);
3659 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
3661 lnet_ni_recv(ni, private, msg, 0, 0, mlength, rlength);
3666 lnet_parse_ack(struct lnet_ni *ni, struct lnet_msg *msg)
3668 struct lnet_hdr *hdr = &msg->msg_hdr;
3669 struct lnet_process_id src = {0};
3670 struct lnet_libmd *md;
3673 src.nid = hdr->src_nid;
3674 src.pid = hdr->src_pid;
3676 /* Convert ack fields to host byte order */
3677 hdr->msg.ack.match_bits = le64_to_cpu(hdr->msg.ack.match_bits);
3678 hdr->msg.ack.mlength = le32_to_cpu(hdr->msg.ack.mlength);
3680 cpt = lnet_cpt_of_cookie(hdr->msg.ack.dst_wmd.wh_object_cookie);
3683 /* NB handles only looked up by creator (no flips) */
3684 md = lnet_wire_handle2md(&hdr->msg.ack.dst_wmd);
3685 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
3686 /* Don't moan; this is expected */
3688 "%s: Dropping ACK from %s to %s MD %#llx.%#llx\n",
3689 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
3690 (md == NULL) ? "invalid" : "inactive",
3691 hdr->msg.ack.dst_wmd.wh_interface_cookie,
3692 hdr->msg.ack.dst_wmd.wh_object_cookie);
3693 if (md != NULL && md->md_me != NULL)
3694 CERROR("Source MD also attached to portal %d\n",
3695 md->md_me->me_portal);
3697 lnet_res_unlock(cpt);
3698 return -ENOENT; /* -ve! */
3701 CDEBUG(D_NET, "%s: ACK from %s into md %#llx\n",
3702 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
3703 hdr->msg.ack.dst_wmd.wh_object_cookie);
3705 lnet_msg_attach_md(msg, md, 0, 0);
3707 lnet_res_unlock(cpt);
3709 lnet_build_msg_event(msg, LNET_EVENT_ACK);
3711 lnet_ni_recv(ni, msg->msg_private, msg, 0, 0, 0, msg->msg_len);
3716 * \retval LNET_CREDIT_OK If \a msg is forwarded
3717 * \retval LNET_CREDIT_WAIT If \a msg is blocked because w/o buffer
3718 * \retval -ve error code
3721 lnet_parse_forward_locked(struct lnet_ni *ni, struct lnet_msg *msg)
3725 if (!the_lnet.ln_routing)
3728 if (msg->msg_rxpeer->lpni_rtrcredits <= 0 ||
3729 lnet_msg2bufpool(msg)->rbp_credits <= 0) {
3730 if (ni->ni_net->net_lnd->lnd_eager_recv == NULL) {
3731 msg->msg_rx_ready_delay = 1;
3733 lnet_net_unlock(msg->msg_rx_cpt);
3734 rc = lnet_ni_eager_recv(ni, msg);
3735 lnet_net_lock(msg->msg_rx_cpt);
3740 rc = lnet_post_routed_recv_locked(msg, 0);
3745 lnet_parse_local(struct lnet_ni *ni, struct lnet_msg *msg)
3749 switch (msg->msg_type) {
3751 rc = lnet_parse_ack(ni, msg);
3754 rc = lnet_parse_put(ni, msg);
3757 rc = lnet_parse_get(ni, msg, msg->msg_rdma_get);
3759 case LNET_MSG_REPLY:
3760 rc = lnet_parse_reply(ni, msg);
3762 default: /* prevent an unused label if !kernel */
3767 LASSERT(rc == 0 || rc == -ENOENT);
3772 lnet_msgtyp2str (int type)
3781 case LNET_MSG_REPLY:
3783 case LNET_MSG_HELLO:
3786 return ("<UNKNOWN>");
3791 lnet_print_hdr(struct lnet_hdr *hdr)
3793 struct lnet_process_id src = {
3794 .nid = hdr->src_nid,
3795 .pid = hdr->src_pid,
3797 struct lnet_process_id dst = {
3798 .nid = hdr->dest_nid,
3799 .pid = hdr->dest_pid,
3801 char *type_str = lnet_msgtyp2str(hdr->type);
3803 CWARN("P3 Header at %p of type %s\n", hdr, type_str);
3804 CWARN(" From %s\n", libcfs_id2str(src));
3805 CWARN(" To %s\n", libcfs_id2str(dst));
3807 switch (hdr->type) {
3812 CWARN(" Ptl index %d, ack md %#llx.%#llx, "
3813 "match bits %llu\n",
3814 hdr->msg.put.ptl_index,
3815 hdr->msg.put.ack_wmd.wh_interface_cookie,
3816 hdr->msg.put.ack_wmd.wh_object_cookie,
3817 hdr->msg.put.match_bits);
3818 CWARN(" Length %d, offset %d, hdr data %#llx\n",
3819 hdr->payload_length, hdr->msg.put.offset,
3820 hdr->msg.put.hdr_data);
3824 CWARN(" Ptl index %d, return md %#llx.%#llx, "
3825 "match bits %llu\n", hdr->msg.get.ptl_index,
3826 hdr->msg.get.return_wmd.wh_interface_cookie,
3827 hdr->msg.get.return_wmd.wh_object_cookie,
3828 hdr->msg.get.match_bits);
3829 CWARN(" Length %d, src offset %d\n",
3830 hdr->msg.get.sink_length,
3831 hdr->msg.get.src_offset);
3835 CWARN(" dst md %#llx.%#llx, "
3836 "manipulated length %d\n",
3837 hdr->msg.ack.dst_wmd.wh_interface_cookie,
3838 hdr->msg.ack.dst_wmd.wh_object_cookie,
3839 hdr->msg.ack.mlength);
3842 case LNET_MSG_REPLY:
3843 CWARN(" dst md %#llx.%#llx, "
3845 hdr->msg.reply.dst_wmd.wh_interface_cookie,
3846 hdr->msg.reply.dst_wmd.wh_object_cookie,
3847 hdr->payload_length);
3853 lnet_parse(struct lnet_ni *ni, struct lnet_hdr *hdr, lnet_nid_t from_nid,
3854 void *private, int rdma_req)
3859 struct lnet_msg *msg;
3860 lnet_pid_t dest_pid;
3861 lnet_nid_t dest_nid;
3863 struct lnet_peer_ni *lpni;
3864 __u32 payload_length;
3867 LASSERT (!in_interrupt ());
3869 type = le32_to_cpu(hdr->type);
3870 src_nid = le64_to_cpu(hdr->src_nid);
3871 dest_nid = le64_to_cpu(hdr->dest_nid);
3872 dest_pid = le32_to_cpu(hdr->dest_pid);
3873 payload_length = le32_to_cpu(hdr->payload_length);
3875 for_me = (ni->ni_nid == dest_nid);
3876 cpt = lnet_cpt_of_nid(from_nid, ni);
3878 CDEBUG(D_NET, "TRACE: %s(%s) <- %s : %s - %s\n",
3879 libcfs_nid2str(dest_nid),
3880 libcfs_nid2str(ni->ni_nid),
3881 libcfs_nid2str(src_nid),
3882 lnet_msgtyp2str(type),
3883 (for_me) ? "for me" : "routed");
3888 if (payload_length > 0) {
3889 CERROR("%s, src %s: bad %s payload %d (0 expected)\n",
3890 libcfs_nid2str(from_nid),
3891 libcfs_nid2str(src_nid),
3892 lnet_msgtyp2str(type), payload_length);
3898 case LNET_MSG_REPLY:
3899 if (payload_length >
3900 (__u32)(for_me ? LNET_MAX_PAYLOAD : LNET_MTU)) {
3901 CERROR("%s, src %s: bad %s payload %d "
3902 "(%d max expected)\n",
3903 libcfs_nid2str(from_nid),
3904 libcfs_nid2str(src_nid),
3905 lnet_msgtyp2str(type),
3907 for_me ? LNET_MAX_PAYLOAD : LNET_MTU);
3913 CERROR("%s, src %s: Bad message type 0x%x\n",
3914 libcfs_nid2str(from_nid),
3915 libcfs_nid2str(src_nid), type);
3919 if (the_lnet.ln_routing &&
3920 ni->ni_last_alive != ktime_get_real_seconds()) {
3921 /* NB: so far here is the only place to set NI status to "up */
3923 ni->ni_last_alive = ktime_get_real_seconds();
3924 if (ni->ni_status != NULL &&
3925 ni->ni_status->ns_status == LNET_NI_STATUS_DOWN)
3926 ni->ni_status->ns_status = LNET_NI_STATUS_UP;
3930 /* Regard a bad destination NID as a protocol error. Senders should
3931 * know what they're doing; if they don't they're misconfigured, buggy
3932 * or malicious so we chop them off at the knees :) */
3935 if (LNET_NIDNET(dest_nid) == LNET_NIDNET(ni->ni_nid)) {
3936 /* should have gone direct */
3937 CERROR("%s, src %s: Bad dest nid %s "
3938 "(should have been sent direct)\n",
3939 libcfs_nid2str(from_nid),
3940 libcfs_nid2str(src_nid),
3941 libcfs_nid2str(dest_nid));
3945 if (lnet_islocalnid(dest_nid)) {
3946 /* dest is another local NI; sender should have used
3947 * this node's NID on its own network */
3948 CERROR("%s, src %s: Bad dest nid %s "
3949 "(it's my nid but on a different network)\n",
3950 libcfs_nid2str(from_nid),
3951 libcfs_nid2str(src_nid),
3952 libcfs_nid2str(dest_nid));
3956 if (rdma_req && type == LNET_MSG_GET) {
3957 CERROR("%s, src %s: Bad optimized GET for %s "
3958 "(final destination must be me)\n",
3959 libcfs_nid2str(from_nid),
3960 libcfs_nid2str(src_nid),
3961 libcfs_nid2str(dest_nid));
3965 if (!the_lnet.ln_routing) {
3966 CERROR("%s, src %s: Dropping message for %s "
3967 "(routing not enabled)\n",
3968 libcfs_nid2str(from_nid),
3969 libcfs_nid2str(src_nid),
3970 libcfs_nid2str(dest_nid));
3975 /* Message looks OK; we're not going to return an error, so we MUST
3976 * call back lnd_recv() come what may... */
3978 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
3979 fail_peer(src_nid, 0)) { /* shall we now? */
3980 CERROR("%s, src %s: Dropping %s to simulate failure\n",
3981 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
3982 lnet_msgtyp2str(type));
3986 if (!list_empty(&the_lnet.ln_drop_rules) &&
3987 lnet_drop_rule_match(hdr)) {
3988 CDEBUG(D_NET, "%s, src %s, dst %s: Dropping %s to simulate"
3989 "silent message loss\n",
3990 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
3991 libcfs_nid2str(dest_nid), lnet_msgtyp2str(type));
3996 msg = lnet_msg_alloc();
3998 CERROR("%s, src %s: Dropping %s (out of memory)\n",
3999 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4000 lnet_msgtyp2str(type));
4004 /* msg zeroed in lnet_msg_alloc; i.e. flags all clear,
4005 * pointers NULL etc */
4007 msg->msg_type = type;
4008 msg->msg_private = private;
4009 msg->msg_receiving = 1;
4010 msg->msg_rdma_get = rdma_req;
4011 msg->msg_len = msg->msg_wanted = payload_length;
4012 msg->msg_offset = 0;
4013 msg->msg_hdr = *hdr;
4014 /* for building message event */
4015 msg->msg_from = from_nid;
4017 msg->msg_target.pid = dest_pid;
4018 msg->msg_target.nid = dest_nid;
4019 msg->msg_routing = 1;
4022 /* convert common msg->hdr fields to host byteorder */
4023 msg->msg_hdr.type = type;
4024 msg->msg_hdr.src_nid = src_nid;
4025 msg->msg_hdr.src_pid = le32_to_cpu(msg->msg_hdr.src_pid);
4026 msg->msg_hdr.dest_nid = dest_nid;
4027 msg->msg_hdr.dest_pid = dest_pid;
4028 msg->msg_hdr.payload_length = payload_length;
4032 lpni = lnet_nid2peerni_locked(from_nid, ni->ni_nid, cpt);
4034 lnet_net_unlock(cpt);
4035 CERROR("%s, src %s: Dropping %s "
4036 "(error %ld looking up sender)\n",
4037 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4038 lnet_msgtyp2str(type), PTR_ERR(lpni));
4040 if (rc == -ESHUTDOWN)
4041 /* We are shutting down. Don't do anything more */
4045 msg->msg_rxpeer = lpni;
4047 lnet_ni_addref_locked(ni, cpt);
4048 /* Multi-Rail: Primary NID of source. */
4049 msg->msg_initiator = lnet_peer_primary_nid_locked(src_nid);
4051 if (lnet_isrouter(msg->msg_rxpeer)) {
4052 lnet_peer_set_alive(msg->msg_rxpeer);
4053 if (avoid_asym_router_failure &&
4054 LNET_NIDNET(src_nid) != LNET_NIDNET(from_nid)) {
4055 /* received a remote message from router, update
4056 * remote NI status on this router.
4057 * NB: multi-hop routed message will be ignored.
4059 lnet_router_ni_update_locked(msg->msg_rxpeer,
4060 LNET_NIDNET(src_nid));
4064 lnet_msg_commit(msg, cpt);
4066 /* message delay simulation */
4067 if (unlikely(!list_empty(&the_lnet.ln_delay_rules) &&
4068 lnet_delay_rule_match_locked(hdr, msg))) {
4069 lnet_net_unlock(cpt);
4074 rc = lnet_parse_forward_locked(ni, msg);
4075 lnet_net_unlock(cpt);
4080 if (rc == LNET_CREDIT_OK) {
4081 lnet_ni_recv(ni, msg->msg_private, msg, 0,
4082 0, payload_length, payload_length);
4087 lnet_net_unlock(cpt);
4089 rc = lnet_parse_local(ni, msg);
4095 LASSERT(msg->msg_md == NULL);
4096 lnet_finalize(msg, rc);
4099 lnet_drop_message(ni, cpt, private, payload_length, type);
4102 EXPORT_SYMBOL(lnet_parse);
4105 lnet_drop_delayed_msg_list(struct list_head *head, char *reason)
4107 while (!list_empty(head)) {
4108 struct lnet_process_id id = {0};
4109 struct lnet_msg *msg;
4111 msg = list_entry(head->next, struct lnet_msg, msg_list);
4112 list_del(&msg->msg_list);
4114 id.nid = msg->msg_hdr.src_nid;
4115 id.pid = msg->msg_hdr.src_pid;
4117 LASSERT(msg->msg_md == NULL);
4118 LASSERT(msg->msg_rx_delayed);
4119 LASSERT(msg->msg_rxpeer != NULL);
4120 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
4122 CWARN("Dropping delayed PUT from %s portal %d match %llu"
4123 " offset %d length %d: %s\n",
4125 msg->msg_hdr.msg.put.ptl_index,
4126 msg->msg_hdr.msg.put.match_bits,
4127 msg->msg_hdr.msg.put.offset,
4128 msg->msg_hdr.payload_length, reason);
4130 /* NB I can't drop msg's ref on msg_rxpeer until after I've
4131 * called lnet_drop_message(), so I just hang onto msg as well
4132 * until that's done */
4134 lnet_drop_message(msg->msg_rxni, msg->msg_rx_cpt,
4135 msg->msg_private, msg->msg_len,
4138 msg->msg_no_resend = true;
4140 * NB: message will not generate event because w/o attached MD,
4141 * but we still should give error code so lnet_msg_decommit()
4142 * can skip counters operations and other checks.
4144 lnet_finalize(msg, -ENOENT);
4149 lnet_recv_delayed_msg_list(struct list_head *head)
4151 while (!list_empty(head)) {
4152 struct lnet_msg *msg;
4153 struct lnet_process_id id;
4155 msg = list_entry(head->next, struct lnet_msg, msg_list);
4156 list_del(&msg->msg_list);
4158 /* md won't disappear under me, since each msg
4159 * holds a ref on it */
4161 id.nid = msg->msg_hdr.src_nid;
4162 id.pid = msg->msg_hdr.src_pid;
4164 LASSERT(msg->msg_rx_delayed);
4165 LASSERT(msg->msg_md != NULL);
4166 LASSERT(msg->msg_rxpeer != NULL);
4167 LASSERT(msg->msg_rxni != NULL);
4168 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
4170 CDEBUG(D_NET, "Resuming delayed PUT from %s portal %d "
4171 "match %llu offset %d length %d.\n",
4172 libcfs_id2str(id), msg->msg_hdr.msg.put.ptl_index,
4173 msg->msg_hdr.msg.put.match_bits,
4174 msg->msg_hdr.msg.put.offset,
4175 msg->msg_hdr.payload_length);
4177 lnet_recv_put(msg->msg_rxni, msg);
4182 * Initiate an asynchronous PUT operation.
4184 * There are several events associated with a PUT: completion of the send on
4185 * the initiator node (LNET_EVENT_SEND), and when the send completes
4186 * successfully, the receipt of an acknowledgment (LNET_EVENT_ACK) indicating
4187 * that the operation was accepted by the target. The event LNET_EVENT_PUT is
4188 * used at the target node to indicate the completion of incoming data
4191 * The local events will be logged in the EQ associated with the MD pointed to
4192 * by \a mdh handle. Using a MD without an associated EQ results in these
4193 * events being discarded. In this case, the caller must have another
4194 * mechanism (e.g., a higher level protocol) for determining when it is safe
4195 * to modify the memory region associated with the MD.
4197 * Note that LNet does not guarantee the order of LNET_EVENT_SEND and
4198 * LNET_EVENT_ACK, though intuitively ACK should happen after SEND.
4200 * \param self Indicates the NID of a local interface through which to send
4201 * the PUT request. Use LNET_NID_ANY to let LNet choose one by itself.
4202 * \param mdh A handle for the MD that describes the memory to be sent. The MD
4203 * must be "free floating" (See LNetMDBind()).
4204 * \param ack Controls whether an acknowledgment is requested.
4205 * Acknowledgments are only sent when they are requested by the initiating
4206 * process and the target MD enables them.
4207 * \param target A process identifier for the target process.
4208 * \param portal The index in the \a target's portal table.
4209 * \param match_bits The match bits to use for MD selection at the target
4211 * \param offset The offset into the target MD (only used when the target
4212 * MD has the LNET_MD_MANAGE_REMOTE option set).
4213 * \param hdr_data 64 bits of user data that can be included in the message
4214 * header. This data is written to an event queue entry at the target if an
4215 * EQ is present on the matching MD.
4217 * \retval 0 Success, and only in this case events will be generated
4218 * and logged to EQ (if it exists).
4219 * \retval -EIO Simulated failure.
4220 * \retval -ENOMEM Memory allocation failure.
4221 * \retval -ENOENT Invalid MD object.
4223 * \see struct lnet_event::hdr_data and lnet_event_kind_t.
4226 LNetPut(lnet_nid_t self, struct lnet_handle_md mdh, enum lnet_ack_req ack,
4227 struct lnet_process_id target, unsigned int portal,
4228 __u64 match_bits, unsigned int offset,
4231 struct lnet_msg *msg;
4232 struct lnet_libmd *md;
4236 LASSERT(the_lnet.ln_refcount > 0);
4238 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
4239 fail_peer(target.nid, 1)) { /* shall we now? */
4240 CERROR("Dropping PUT to %s: simulated failure\n",
4241 libcfs_id2str(target));
4245 msg = lnet_msg_alloc();
4247 CERROR("Dropping PUT to %s: ENOMEM on struct lnet_msg\n",
4248 libcfs_id2str(target));
4251 msg->msg_vmflush = !!memory_pressure_get();
4253 cpt = lnet_cpt_of_cookie(mdh.cookie);
4256 md = lnet_handle2md(&mdh);
4257 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
4258 CERROR("Dropping PUT (%llu:%d:%s): MD (%d) invalid\n",
4259 match_bits, portal, libcfs_id2str(target),
4260 md == NULL ? -1 : md->md_threshold);
4261 if (md != NULL && md->md_me != NULL)
4262 CERROR("Source MD also attached to portal %d\n",
4263 md->md_me->me_portal);
4264 lnet_res_unlock(cpt);
4270 CDEBUG(D_NET, "LNetPut -> %s\n", libcfs_id2str(target));
4272 lnet_msg_attach_md(msg, md, 0, 0);
4274 lnet_prep_send(msg, LNET_MSG_PUT, target, 0, md->md_length);
4276 msg->msg_hdr.msg.put.match_bits = cpu_to_le64(match_bits);
4277 msg->msg_hdr.msg.put.ptl_index = cpu_to_le32(portal);
4278 msg->msg_hdr.msg.put.offset = cpu_to_le32(offset);
4279 msg->msg_hdr.msg.put.hdr_data = hdr_data;
4281 /* NB handles only looked up by creator (no flips) */
4282 if (ack == LNET_ACK_REQ) {
4283 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
4284 the_lnet.ln_interface_cookie;
4285 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
4286 md->md_lh.lh_cookie;
4288 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
4289 LNET_WIRE_HANDLE_COOKIE_NONE;
4290 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
4291 LNET_WIRE_HANDLE_COOKIE_NONE;
4294 lnet_res_unlock(cpt);
4296 lnet_build_msg_event(msg, LNET_EVENT_SEND);
4298 rc = lnet_send(self, msg, LNET_NID_ANY);
4300 CNETERR("Error sending PUT to %s: %d\n",
4301 libcfs_id2str(target), rc);
4302 msg->msg_no_resend = true;
4303 lnet_finalize(msg, rc);
4306 /* completion will be signalled by an event */
4309 EXPORT_SYMBOL(LNetPut);
4312 * The LND can DMA direct to the GET md (i.e. no REPLY msg). This
4313 * returns a msg for the LND to pass to lnet_finalize() when the sink
4314 * data has been received.
4316 * CAVEAT EMPTOR: 'getmsg' is the original GET, which is freed when
4317 * lnet_finalize() is called on it, so the LND must call this first
4320 lnet_create_reply_msg(struct lnet_ni *ni, struct lnet_msg *getmsg)
4322 struct lnet_msg *msg = lnet_msg_alloc();
4323 struct lnet_libmd *getmd = getmsg->msg_md;
4324 struct lnet_process_id peer_id = getmsg->msg_target;
4327 LASSERT(!getmsg->msg_target_is_router);
4328 LASSERT(!getmsg->msg_routing);
4331 CERROR("%s: Dropping REPLY from %s: can't allocate msg\n",
4332 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id));
4336 cpt = lnet_cpt_of_cookie(getmd->md_lh.lh_cookie);
4339 LASSERT(getmd->md_refcount > 0);
4341 if (getmd->md_threshold == 0) {
4342 CERROR("%s: Dropping REPLY from %s for inactive MD %p\n",
4343 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id),
4345 lnet_res_unlock(cpt);
4349 LASSERT(getmd->md_offset == 0);
4351 CDEBUG(D_NET, "%s: Reply from %s md %p\n",
4352 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id), getmd);
4354 /* setup information for lnet_build_msg_event */
4355 msg->msg_initiator = getmsg->msg_txpeer->lpni_peer_net->lpn_peer->lp_primary_nid;
4356 msg->msg_from = peer_id.nid;
4357 msg->msg_type = LNET_MSG_GET; /* flag this msg as an "optimized" GET */
4358 msg->msg_hdr.src_nid = peer_id.nid;
4359 msg->msg_hdr.payload_length = getmd->md_length;
4360 msg->msg_receiving = 1; /* required by lnet_msg_attach_md */
4362 lnet_msg_attach_md(msg, getmd, getmd->md_offset, getmd->md_length);
4363 lnet_res_unlock(cpt);
4365 cpt = lnet_cpt_of_nid(peer_id.nid, ni);
4368 lnet_msg_commit(msg, cpt);
4369 lnet_net_unlock(cpt);
4371 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
4376 cpt = lnet_cpt_of_nid(peer_id.nid, ni);
4379 lnet_incr_stats(&ni->ni_stats, LNET_MSG_GET, LNET_STATS_TYPE_DROP);
4380 the_lnet.ln_counters[cpt]->drop_count++;
4381 the_lnet.ln_counters[cpt]->drop_length += getmd->md_length;
4382 lnet_net_unlock(cpt);
4389 EXPORT_SYMBOL(lnet_create_reply_msg);
4392 lnet_set_reply_msg_len(struct lnet_ni *ni, struct lnet_msg *reply,
4395 /* Set the REPLY length, now the RDMA that elides the REPLY message has
4396 * completed and I know it. */
4397 LASSERT(reply != NULL);
4398 LASSERT(reply->msg_type == LNET_MSG_GET);
4399 LASSERT(reply->msg_ev.type == LNET_EVENT_REPLY);
4401 /* NB I trusted my peer to RDMA. If she tells me she's written beyond
4402 * the end of my buffer, I might as well be dead. */
4403 LASSERT(len <= reply->msg_ev.mlength);
4405 reply->msg_ev.mlength = len;
4407 EXPORT_SYMBOL(lnet_set_reply_msg_len);
4410 * Initiate an asynchronous GET operation.
4412 * On the initiator node, an LNET_EVENT_SEND is logged when the GET request
4413 * is sent, and an LNET_EVENT_REPLY is logged when the data returned from
4414 * the target node in the REPLY has been written to local MD.
4416 * On the target node, an LNET_EVENT_GET is logged when the GET request
4417 * arrives and is accepted into a MD.
4419 * \param self,target,portal,match_bits,offset See the discussion in LNetPut().
4420 * \param mdh A handle for the MD that describes the memory into which the
4421 * requested data will be received. The MD must be "free floating" (See LNetMDBind()).
4423 * \retval 0 Success, and only in this case events will be generated
4424 * and logged to EQ (if it exists) of the MD.
4425 * \retval -EIO Simulated failure.
4426 * \retval -ENOMEM Memory allocation failure.
4427 * \retval -ENOENT Invalid MD object.
4430 LNetGet(lnet_nid_t self, struct lnet_handle_md mdh,
4431 struct lnet_process_id target, unsigned int portal,
4432 __u64 match_bits, unsigned int offset, bool recovery)
4434 struct lnet_msg *msg;
4435 struct lnet_libmd *md;
4439 LASSERT(the_lnet.ln_refcount > 0);
4441 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
4442 fail_peer(target.nid, 1)) /* shall we now? */
4444 CERROR("Dropping GET to %s: simulated failure\n",
4445 libcfs_id2str(target));
4449 msg = lnet_msg_alloc();
4451 CERROR("Dropping GET to %s: ENOMEM on struct lnet_msg\n",
4452 libcfs_id2str(target));
4456 msg->msg_recovery = recovery;
4458 cpt = lnet_cpt_of_cookie(mdh.cookie);
4461 md = lnet_handle2md(&mdh);
4462 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
4463 CERROR("Dropping GET (%llu:%d:%s): MD (%d) invalid\n",
4464 match_bits, portal, libcfs_id2str(target),
4465 md == NULL ? -1 : md->md_threshold);
4466 if (md != NULL && md->md_me != NULL)
4467 CERROR("REPLY MD also attached to portal %d\n",
4468 md->md_me->me_portal);
4470 lnet_res_unlock(cpt);
4476 CDEBUG(D_NET, "LNetGet -> %s\n", libcfs_id2str(target));
4478 lnet_msg_attach_md(msg, md, 0, 0);
4480 lnet_prep_send(msg, LNET_MSG_GET, target, 0, 0);
4482 msg->msg_hdr.msg.get.match_bits = cpu_to_le64(match_bits);
4483 msg->msg_hdr.msg.get.ptl_index = cpu_to_le32(portal);
4484 msg->msg_hdr.msg.get.src_offset = cpu_to_le32(offset);
4485 msg->msg_hdr.msg.get.sink_length = cpu_to_le32(md->md_length);
4487 /* NB handles only looked up by creator (no flips) */
4488 msg->msg_hdr.msg.get.return_wmd.wh_interface_cookie =
4489 the_lnet.ln_interface_cookie;
4490 msg->msg_hdr.msg.get.return_wmd.wh_object_cookie =
4491 md->md_lh.lh_cookie;
4493 lnet_res_unlock(cpt);
4495 lnet_build_msg_event(msg, LNET_EVENT_SEND);
4497 rc = lnet_send(self, msg, LNET_NID_ANY);
4499 CNETERR("Error sending GET to %s: %d\n",
4500 libcfs_id2str(target), rc);
4501 msg->msg_no_resend = true;
4502 lnet_finalize(msg, rc);
4505 /* completion will be signalled by an event */
4508 EXPORT_SYMBOL(LNetGet);
4511 * Calculate distance to node at \a dstnid.
4513 * \param dstnid Target NID.
4514 * \param srcnidp If not NULL, NID of the local interface to reach \a dstnid
4516 * \param orderp If not NULL, order of the route to reach \a dstnid is saved
4519 * \retval 0 If \a dstnid belongs to a local interface, and reserved option
4520 * local_nid_dist_zero is set, which is the default.
4521 * \retval positives Distance to target NID, i.e. number of hops plus one.
4522 * \retval -EHOSTUNREACH If \a dstnid is not reachable.
4525 LNetDist(lnet_nid_t dstnid, lnet_nid_t *srcnidp, __u32 *orderp)
4527 struct list_head *e;
4528 struct lnet_ni *ni = NULL;
4529 struct lnet_remotenet *rnet;
4530 __u32 dstnet = LNET_NIDNET(dstnid);
4534 struct list_head *rn_list;
4536 /* if !local_nid_dist_zero, I don't return a distance of 0 ever
4537 * (when lustre sees a distance of 0, it substitutes 0@lo), so I
4538 * keep order 0 free for 0@lo and order 1 free for a local NID
4541 LASSERT(the_lnet.ln_refcount > 0);
4543 cpt = lnet_net_lock_current();
4545 while ((ni = lnet_get_next_ni_locked(NULL, ni))) {
4546 if (ni->ni_nid == dstnid) {
4547 if (srcnidp != NULL)
4549 if (orderp != NULL) {
4550 if (LNET_NETTYP(LNET_NIDNET(dstnid)) == LOLND)
4555 lnet_net_unlock(cpt);
4557 return local_nid_dist_zero ? 0 : 1;
4560 if (LNET_NIDNET(ni->ni_nid) == dstnet) {
4561 /* Check if ni was originally created in
4562 * current net namespace.
4563 * If not, assign order above 0xffff0000,
4564 * to make this ni not a priority. */
4565 if (!net_eq(ni->ni_net_ns, current->nsproxy->net_ns))
4566 order += 0xffff0000;
4568 if (srcnidp != NULL)
4569 *srcnidp = ni->ni_nid;
4572 lnet_net_unlock(cpt);
4579 rn_list = lnet_net2rnethash(dstnet);
4580 list_for_each(e, rn_list) {
4581 rnet = list_entry(e, struct lnet_remotenet, lrn_list);
4583 if (rnet->lrn_net == dstnet) {
4584 struct lnet_route *route;
4585 struct lnet_route *shortest = NULL;
4586 __u32 shortest_hops = LNET_UNDEFINED_HOPS;
4589 LASSERT(!list_empty(&rnet->lrn_routes));
4591 list_for_each_entry(route, &rnet->lrn_routes,
4593 route_hops = route->lr_hops;
4594 if (route_hops == LNET_UNDEFINED_HOPS)
4596 if (shortest == NULL ||
4597 route_hops < shortest_hops) {
4599 shortest_hops = route_hops;
4603 LASSERT(shortest != NULL);
4604 hops = shortest_hops;
4605 if (srcnidp != NULL) {
4606 ni = lnet_get_next_ni_locked(
4607 shortest->lr_gateway->lpni_net,
4609 *srcnidp = ni->ni_nid;
4613 lnet_net_unlock(cpt);
4619 lnet_net_unlock(cpt);
4620 return -EHOSTUNREACH;
4622 EXPORT_SYMBOL(LNetDist);