4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lnet/lnet/lib-move.c
34 * Data movement routines
37 #define DEBUG_SUBSYSTEM S_LNET
39 #include <linux/pagemap.h>
41 #include <lnet/lib-lnet.h>
42 #include <linux/nsproxy.h>
43 #include <net/net_namespace.h>
45 static int local_nid_dist_zero = 1;
46 module_param(local_nid_dist_zero, int, 0444);
47 MODULE_PARM_DESC(local_nid_dist_zero, "Reserved");
49 struct lnet_send_data {
50 struct lnet_ni *sd_best_ni;
51 struct lnet_peer_ni *sd_best_lpni;
52 struct lnet_peer_ni *sd_final_dst_lpni;
53 struct lnet_peer *sd_peer;
54 struct lnet_peer *sd_gw_peer;
55 struct lnet_peer_ni *sd_gw_lpni;
56 struct lnet_peer_net *sd_peer_net;
57 struct lnet_msg *sd_msg;
58 lnet_nid_t sd_dst_nid;
59 lnet_nid_t sd_src_nid;
60 lnet_nid_t sd_rtr_nid;
66 static inline struct lnet_comm_count *
67 get_stats_counts(struct lnet_element_stats *stats,
68 enum lnet_stats_type stats_type)
71 case LNET_STATS_TYPE_SEND:
72 return &stats->el_send_stats;
73 case LNET_STATS_TYPE_RECV:
74 return &stats->el_recv_stats;
75 case LNET_STATS_TYPE_DROP:
76 return &stats->el_drop_stats;
78 CERROR("Unknown stats type\n");
84 void lnet_incr_stats(struct lnet_element_stats *stats,
85 enum lnet_msg_type msg_type,
86 enum lnet_stats_type stats_type)
88 struct lnet_comm_count *counts = get_stats_counts(stats, stats_type);
94 atomic_inc(&counts->co_ack_count);
97 atomic_inc(&counts->co_put_count);
100 atomic_inc(&counts->co_get_count);
103 atomic_inc(&counts->co_reply_count);
106 atomic_inc(&counts->co_hello_count);
109 CERROR("There is a BUG in the code. Unknown message type\n");
114 __u32 lnet_sum_stats(struct lnet_element_stats *stats,
115 enum lnet_stats_type stats_type)
117 struct lnet_comm_count *counts = get_stats_counts(stats, stats_type);
121 return (atomic_read(&counts->co_ack_count) +
122 atomic_read(&counts->co_put_count) +
123 atomic_read(&counts->co_get_count) +
124 atomic_read(&counts->co_reply_count) +
125 atomic_read(&counts->co_hello_count));
128 static inline void assign_stats(struct lnet_ioctl_comm_count *msg_stats,
129 struct lnet_comm_count *counts)
131 msg_stats->ico_get_count = atomic_read(&counts->co_get_count);
132 msg_stats->ico_put_count = atomic_read(&counts->co_put_count);
133 msg_stats->ico_reply_count = atomic_read(&counts->co_reply_count);
134 msg_stats->ico_ack_count = atomic_read(&counts->co_ack_count);
135 msg_stats->ico_hello_count = atomic_read(&counts->co_hello_count);
138 void lnet_usr_translate_stats(struct lnet_ioctl_element_msg_stats *msg_stats,
139 struct lnet_element_stats *stats)
141 struct lnet_comm_count *counts;
146 counts = get_stats_counts(stats, LNET_STATS_TYPE_SEND);
149 assign_stats(&msg_stats->im_send_stats, counts);
151 counts = get_stats_counts(stats, LNET_STATS_TYPE_RECV);
154 assign_stats(&msg_stats->im_recv_stats, counts);
156 counts = get_stats_counts(stats, LNET_STATS_TYPE_DROP);
159 assign_stats(&msg_stats->im_drop_stats, counts);
163 lnet_fail_nid(lnet_nid_t nid, unsigned int threshold)
165 struct lnet_test_peer *tp;
166 struct list_head *el;
167 struct list_head *next;
168 struct list_head cull;
170 /* NB: use lnet_net_lock(0) to serialize operations on test peers */
171 if (threshold != 0) {
172 /* Adding a new entry */
173 LIBCFS_ALLOC(tp, sizeof(*tp));
178 tp->tp_threshold = threshold;
181 list_add_tail(&tp->tp_list, &the_lnet.ln_test_peers);
186 /* removing entries */
187 INIT_LIST_HEAD(&cull);
191 list_for_each_safe(el, next, &the_lnet.ln_test_peers) {
192 tp = list_entry(el, struct lnet_test_peer, tp_list);
194 if (tp->tp_threshold == 0 || /* needs culling anyway */
195 nid == LNET_NID_ANY || /* removing all entries */
196 tp->tp_nid == nid) { /* matched this one */
197 list_del(&tp->tp_list);
198 list_add(&tp->tp_list, &cull);
204 while (!list_empty(&cull)) {
205 tp = list_entry(cull.next, struct lnet_test_peer, tp_list);
207 list_del(&tp->tp_list);
208 LIBCFS_FREE(tp, sizeof(*tp));
214 fail_peer (lnet_nid_t nid, int outgoing)
216 struct lnet_test_peer *tp;
217 struct list_head *el;
218 struct list_head *next;
219 struct list_head cull;
222 INIT_LIST_HEAD(&cull);
224 /* NB: use lnet_net_lock(0) to serialize operations on test peers */
227 list_for_each_safe(el, next, &the_lnet.ln_test_peers) {
228 tp = list_entry(el, struct lnet_test_peer, tp_list);
230 if (tp->tp_threshold == 0) {
233 /* only cull zombies on outgoing tests,
234 * since we may be at interrupt priority on
235 * incoming messages. */
236 list_del(&tp->tp_list);
237 list_add(&tp->tp_list, &cull);
242 if (tp->tp_nid == LNET_NID_ANY || /* fail every peer */
243 nid == tp->tp_nid) { /* fail this peer */
246 if (tp->tp_threshold != LNET_MD_THRESH_INF) {
249 tp->tp_threshold == 0) {
251 list_del(&tp->tp_list);
252 list_add(&tp->tp_list, &cull);
261 while (!list_empty(&cull)) {
262 tp = list_entry(cull.next, struct lnet_test_peer, tp_list);
263 list_del(&tp->tp_list);
265 LIBCFS_FREE(tp, sizeof(*tp));
272 lnet_iov_nob(unsigned int niov, struct kvec *iov)
274 unsigned int nob = 0;
276 LASSERT(niov == 0 || iov != NULL);
278 nob += (iov++)->iov_len;
282 EXPORT_SYMBOL(lnet_iov_nob);
285 lnet_copy_iov2iov(unsigned int ndiov, struct kvec *diov, unsigned int doffset,
286 unsigned int nsiov, struct kvec *siov, unsigned int soffset,
289 /* NB diov, siov are READ-ONLY */
290 unsigned int this_nob;
295 /* skip complete frags before 'doffset' */
297 while (doffset >= diov->iov_len) {
298 doffset -= diov->iov_len;
304 /* skip complete frags before 'soffset' */
306 while (soffset >= siov->iov_len) {
307 soffset -= siov->iov_len;
316 this_nob = MIN(diov->iov_len - doffset,
317 siov->iov_len - soffset);
318 this_nob = MIN(this_nob, nob);
320 memcpy((char *)diov->iov_base + doffset,
321 (char *)siov->iov_base + soffset, this_nob);
324 if (diov->iov_len > doffset + this_nob) {
332 if (siov->iov_len > soffset + this_nob) {
341 EXPORT_SYMBOL(lnet_copy_iov2iov);
344 lnet_extract_iov(int dst_niov, struct kvec *dst,
345 int src_niov, struct kvec *src,
346 unsigned int offset, unsigned int len)
348 /* Initialise 'dst' to the subset of 'src' starting at 'offset',
349 * for exactly 'len' bytes, and return the number of entries.
350 * NB not destructive to 'src' */
351 unsigned int frag_len;
354 if (len == 0) /* no data => */
355 return (0); /* no frags */
357 LASSERT(src_niov > 0);
358 while (offset >= src->iov_len) { /* skip initial frags */
359 offset -= src->iov_len;
362 LASSERT(src_niov > 0);
367 LASSERT(src_niov > 0);
368 LASSERT((int)niov <= dst_niov);
370 frag_len = src->iov_len - offset;
371 dst->iov_base = ((char *)src->iov_base) + offset;
373 if (len <= frag_len) {
378 dst->iov_len = frag_len;
388 EXPORT_SYMBOL(lnet_extract_iov);
392 lnet_kiov_nob(unsigned int niov, lnet_kiov_t *kiov)
394 unsigned int nob = 0;
396 LASSERT(niov == 0 || kiov != NULL);
398 nob += (kiov++)->kiov_len;
402 EXPORT_SYMBOL(lnet_kiov_nob);
405 lnet_copy_kiov2kiov(unsigned int ndiov, lnet_kiov_t *diov, unsigned int doffset,
406 unsigned int nsiov, lnet_kiov_t *siov, unsigned int soffset,
409 /* NB diov, siov are READ-ONLY */
410 unsigned int this_nob;
417 LASSERT (!in_interrupt ());
420 while (doffset >= diov->kiov_len) {
421 doffset -= diov->kiov_len;
428 while (soffset >= siov->kiov_len) {
429 soffset -= siov->kiov_len;
438 this_nob = MIN(diov->kiov_len - doffset,
439 siov->kiov_len - soffset);
440 this_nob = MIN(this_nob, nob);
443 daddr = ((char *)kmap(diov->kiov_page)) +
444 diov->kiov_offset + doffset;
446 saddr = ((char *)kmap(siov->kiov_page)) +
447 siov->kiov_offset + soffset;
449 /* Vanishing risk of kmap deadlock when mapping 2 pages.
450 * However in practice at least one of the kiovs will be mapped
451 * kernel pages and the map/unmap will be NOOPs */
453 memcpy (daddr, saddr, this_nob);
456 if (diov->kiov_len > doffset + this_nob) {
460 kunmap(diov->kiov_page);
467 if (siov->kiov_len > soffset + this_nob) {
471 kunmap(siov->kiov_page);
480 kunmap(diov->kiov_page);
482 kunmap(siov->kiov_page);
484 EXPORT_SYMBOL(lnet_copy_kiov2kiov);
487 lnet_copy_kiov2iov (unsigned int niov, struct kvec *iov, unsigned int iovoffset,
488 unsigned int nkiov, lnet_kiov_t *kiov, unsigned int kiovoffset,
491 /* NB iov, kiov are READ-ONLY */
492 unsigned int this_nob;
498 LASSERT (!in_interrupt ());
501 while (iovoffset >= iov->iov_len) {
502 iovoffset -= iov->iov_len;
509 while (kiovoffset >= kiov->kiov_len) {
510 kiovoffset -= kiov->kiov_len;
519 this_nob = MIN(iov->iov_len - iovoffset,
520 kiov->kiov_len - kiovoffset);
521 this_nob = MIN(this_nob, nob);
524 addr = ((char *)kmap(kiov->kiov_page)) +
525 kiov->kiov_offset + kiovoffset;
527 memcpy((char *)iov->iov_base + iovoffset, addr, this_nob);
530 if (iov->iov_len > iovoffset + this_nob) {
531 iovoffset += this_nob;
538 if (kiov->kiov_len > kiovoffset + this_nob) {
540 kiovoffset += this_nob;
542 kunmap(kiov->kiov_page);
552 kunmap(kiov->kiov_page);
554 EXPORT_SYMBOL(lnet_copy_kiov2iov);
557 lnet_copy_iov2kiov(unsigned int nkiov, lnet_kiov_t *kiov, unsigned int kiovoffset,
558 unsigned int niov, struct kvec *iov, unsigned int iovoffset,
561 /* NB kiov, iov are READ-ONLY */
562 unsigned int this_nob;
568 LASSERT (!in_interrupt ());
571 while (kiovoffset >= kiov->kiov_len) {
572 kiovoffset -= kiov->kiov_len;
579 while (iovoffset >= iov->iov_len) {
580 iovoffset -= iov->iov_len;
589 this_nob = MIN(kiov->kiov_len - kiovoffset,
590 iov->iov_len - iovoffset);
591 this_nob = MIN(this_nob, nob);
594 addr = ((char *)kmap(kiov->kiov_page)) +
595 kiov->kiov_offset + kiovoffset;
597 memcpy (addr, (char *)iov->iov_base + iovoffset, this_nob);
600 if (kiov->kiov_len > kiovoffset + this_nob) {
602 kiovoffset += this_nob;
604 kunmap(kiov->kiov_page);
611 if (iov->iov_len > iovoffset + this_nob) {
612 iovoffset += this_nob;
621 kunmap(kiov->kiov_page);
623 EXPORT_SYMBOL(lnet_copy_iov2kiov);
626 lnet_extract_kiov(int dst_niov, lnet_kiov_t *dst,
627 int src_niov, lnet_kiov_t *src,
628 unsigned int offset, unsigned int len)
630 /* Initialise 'dst' to the subset of 'src' starting at 'offset',
631 * for exactly 'len' bytes, and return the number of entries.
632 * NB not destructive to 'src' */
633 unsigned int frag_len;
636 if (len == 0) /* no data => */
637 return (0); /* no frags */
639 LASSERT(src_niov > 0);
640 while (offset >= src->kiov_len) { /* skip initial frags */
641 offset -= src->kiov_len;
644 LASSERT(src_niov > 0);
649 LASSERT(src_niov > 0);
650 LASSERT((int)niov <= dst_niov);
652 frag_len = src->kiov_len - offset;
653 dst->kiov_page = src->kiov_page;
654 dst->kiov_offset = src->kiov_offset + offset;
656 if (len <= frag_len) {
658 LASSERT(dst->kiov_offset + dst->kiov_len <= PAGE_SIZE);
662 dst->kiov_len = frag_len;
663 LASSERT(dst->kiov_offset + dst->kiov_len <= PAGE_SIZE);
673 EXPORT_SYMBOL(lnet_extract_kiov);
676 lnet_ni_recv(struct lnet_ni *ni, void *private, struct lnet_msg *msg,
677 int delayed, unsigned int offset, unsigned int mlen,
680 unsigned int niov = 0;
681 struct kvec *iov = NULL;
682 lnet_kiov_t *kiov = NULL;
685 LASSERT (!in_interrupt ());
686 LASSERT (mlen == 0 || msg != NULL);
689 LASSERT(msg->msg_receiving);
690 LASSERT(!msg->msg_sending);
691 LASSERT(rlen == msg->msg_len);
692 LASSERT(mlen <= msg->msg_len);
693 LASSERT(msg->msg_offset == offset);
694 LASSERT(msg->msg_wanted == mlen);
696 msg->msg_receiving = 0;
699 niov = msg->msg_niov;
701 kiov = msg->msg_kiov;
704 LASSERT ((iov == NULL) != (kiov == NULL));
708 rc = (ni->ni_net->net_lnd->lnd_recv)(ni, private, msg, delayed,
709 niov, iov, kiov, offset, mlen,
712 lnet_finalize(msg, rc);
716 lnet_setpayloadbuffer(struct lnet_msg *msg)
718 struct lnet_libmd *md = msg->msg_md;
720 LASSERT(msg->msg_len > 0);
721 LASSERT(!msg->msg_routing);
723 LASSERT(msg->msg_niov == 0);
724 LASSERT(msg->msg_iov == NULL);
725 LASSERT(msg->msg_kiov == NULL);
727 msg->msg_niov = md->md_niov;
728 if ((md->md_options & LNET_MD_KIOV) != 0)
729 msg->msg_kiov = md->md_iov.kiov;
731 msg->msg_iov = md->md_iov.iov;
735 lnet_prep_send(struct lnet_msg *msg, int type, struct lnet_process_id target,
736 unsigned int offset, unsigned int len)
738 msg->msg_type = type;
739 msg->msg_target = target;
741 msg->msg_offset = offset;
744 lnet_setpayloadbuffer(msg);
746 memset (&msg->msg_hdr, 0, sizeof (msg->msg_hdr));
747 msg->msg_hdr.type = cpu_to_le32(type);
748 /* dest_nid will be overwritten by lnet_select_pathway() */
749 msg->msg_hdr.dest_nid = cpu_to_le64(target.nid);
750 msg->msg_hdr.dest_pid = cpu_to_le32(target.pid);
751 /* src_nid will be set later */
752 msg->msg_hdr.src_pid = cpu_to_le32(the_lnet.ln_pid);
753 msg->msg_hdr.payload_length = cpu_to_le32(len);
757 lnet_ni_send(struct lnet_ni *ni, struct lnet_msg *msg)
759 void *priv = msg->msg_private;
762 LASSERT (!in_interrupt ());
763 LASSERT (LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND ||
764 (msg->msg_txcredit && msg->msg_peertxcredit));
766 rc = (ni->ni_net->net_lnd->lnd_send)(ni, priv, msg);
768 msg->msg_no_resend = true;
769 lnet_finalize(msg, rc);
774 lnet_ni_eager_recv(struct lnet_ni *ni, struct lnet_msg *msg)
778 LASSERT(!msg->msg_sending);
779 LASSERT(msg->msg_receiving);
780 LASSERT(!msg->msg_rx_ready_delay);
781 LASSERT(ni->ni_net->net_lnd->lnd_eager_recv != NULL);
783 msg->msg_rx_ready_delay = 1;
784 rc = (ni->ni_net->net_lnd->lnd_eager_recv)(ni, msg->msg_private, msg,
787 CERROR("recv from %s / send to %s aborted: "
788 "eager_recv failed %d\n",
789 libcfs_nid2str(msg->msg_rxpeer->lpni_nid),
790 libcfs_id2str(msg->msg_target), rc);
791 LASSERT(rc < 0); /* required by my callers */
798 * This function can be called from two paths:
799 * 1. when sending a message
800 * 2. when decommiting a message (lnet_msg_decommit_tx())
801 * In both these cases the peer_ni should have it's reference count
802 * acquired by the caller and therefore it is safe to drop the spin
803 * lock before calling lnd_query()
806 lnet_ni_query_locked(struct lnet_ni *ni, struct lnet_peer_ni *lp)
808 time64_t last_alive = 0;
809 int cpt = lnet_cpt_of_nid_locked(lp->lpni_nid, ni);
811 LASSERT(lnet_peer_aliveness_enabled(lp));
812 LASSERT(ni->ni_net->net_lnd->lnd_query != NULL);
814 lnet_net_unlock(cpt);
815 (ni->ni_net->net_lnd->lnd_query)(ni, lp->lpni_nid, &last_alive);
818 lp->lpni_last_query = ktime_get_seconds();
820 if (last_alive != 0) /* NI has updated timestamp */
821 lp->lpni_last_alive = last_alive;
824 /* NB: always called with lnet_net_lock held */
826 lnet_peer_is_alive(struct lnet_peer_ni *lp, time64_t now)
831 LASSERT (lnet_peer_aliveness_enabled(lp));
834 * Trust lnet_notify() if it has more recent aliveness news, but
835 * ignore the initial assumed death (see lnet_peers_start_down()).
837 spin_lock(&lp->lpni_lock);
838 if (!lp->lpni_alive && lp->lpni_alive_count > 0 &&
839 lp->lpni_timestamp >= lp->lpni_last_alive) {
840 spin_unlock(&lp->lpni_lock);
844 deadline = lp->lpni_last_alive +
845 lp->lpni_net->net_tunables.lct_peer_timeout;
846 alive = deadline > now;
849 * Update obsolete lp_alive except for routers assumed to be dead
850 * initially, because router checker would update aliveness in this
851 * case, and moreover lpni_last_alive at peer creation is assumed.
853 if (alive && !lp->lpni_alive &&
854 !(lnet_isrouter(lp) && lp->lpni_alive_count == 0)) {
855 spin_unlock(&lp->lpni_lock);
856 lnet_notify_locked(lp, 0, 1, lp->lpni_last_alive);
858 spin_unlock(&lp->lpni_lock);
865 /* NB: returns 1 when alive, 0 when dead, negative when error;
866 * may drop the lnet_net_lock */
868 lnet_peer_alive_locked(struct lnet_ni *ni, struct lnet_peer_ni *lp,
869 struct lnet_msg *msg)
871 time64_t now = ktime_get_seconds();
873 if (!lnet_peer_aliveness_enabled(lp))
876 if (lnet_peer_is_alive(lp, now))
880 * If we're resending a message, let's attempt to send it even if
881 * the peer is down to fulfill our resend quota on the message
883 if (msg->msg_retry_count > 0)
887 * Peer appears dead, but we should avoid frequent NI queries (at
888 * most once per lnet_queryinterval seconds).
890 if (lp->lpni_last_query != 0) {
891 static const int lnet_queryinterval = 1;
894 next_query = lp->lpni_last_query + lnet_queryinterval;
896 if (now < next_query) {
898 CWARN("Unexpected aliveness of peer %s: "
899 "%lld < %lld (%d/%d)\n",
900 libcfs_nid2str(lp->lpni_nid),
903 lp->lpni_net->net_tunables.lct_peer_timeout);
908 /* query NI for latest aliveness news */
909 lnet_ni_query_locked(ni, lp);
911 if (lnet_peer_is_alive(lp, now))
914 lnet_notify_locked(lp, 0, 0, lp->lpni_last_alive);
919 * \param msg The message to be sent.
920 * \param do_send True if lnet_ni_send() should be called in this function.
921 * lnet_send() is going to lnet_net_unlock immediately after this, so
922 * it sets do_send FALSE and I don't do the unlock/send/lock bit.
924 * \retval LNET_CREDIT_OK If \a msg sent or OK to send.
925 * \retval LNET_CREDIT_WAIT If \a msg blocked for credit.
926 * \retval -EHOSTUNREACH If the next hop of the message appears dead.
927 * \retval -ECANCELED If the MD of the message has been unlinked.
930 lnet_post_send_locked(struct lnet_msg *msg, int do_send)
932 struct lnet_peer_ni *lp = msg->msg_txpeer;
933 struct lnet_ni *ni = msg->msg_txni;
934 int cpt = msg->msg_tx_cpt;
935 struct lnet_tx_queue *tq = ni->ni_tx_queues[cpt];
937 /* non-lnet_send() callers have checked before */
938 LASSERT(!do_send || msg->msg_tx_delayed);
939 LASSERT(!msg->msg_receiving);
940 LASSERT(msg->msg_tx_committed);
942 /* NB 'lp' is always the next hop */
943 if ((msg->msg_target.pid & LNET_PID_USERFLAG) == 0 &&
944 lnet_peer_alive_locked(ni, lp, msg) == 0) {
945 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
946 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length +=
948 lnet_net_unlock(cpt);
950 lnet_incr_stats(&msg->msg_txpeer->lpni_stats,
952 LNET_STATS_TYPE_DROP);
954 lnet_incr_stats(&msg->msg_txni->ni_stats,
956 LNET_STATS_TYPE_DROP);
958 CNETERR("Dropping message for %s: peer not alive\n",
959 libcfs_id2str(msg->msg_target));
960 msg->msg_health_status = LNET_MSG_STATUS_LOCAL_DROPPED;
962 lnet_finalize(msg, -EHOSTUNREACH);
965 return -EHOSTUNREACH;
968 if (msg->msg_md != NULL &&
969 (msg->msg_md->md_flags & LNET_MD_FLAG_ABORTED) != 0) {
970 lnet_net_unlock(cpt);
972 CNETERR("Aborting message for %s: LNetM[DE]Unlink() already "
973 "called on the MD/ME.\n",
974 libcfs_id2str(msg->msg_target));
976 msg->msg_no_resend = true;
977 lnet_finalize(msg, -ECANCELED);
984 if (!msg->msg_peertxcredit) {
985 spin_lock(&lp->lpni_lock);
986 LASSERT((lp->lpni_txcredits < 0) ==
987 !list_empty(&lp->lpni_txq));
989 msg->msg_peertxcredit = 1;
990 lp->lpni_txqnob += msg->msg_len + sizeof(struct lnet_hdr);
991 lp->lpni_txcredits--;
993 if (lp->lpni_txcredits < lp->lpni_mintxcredits)
994 lp->lpni_mintxcredits = lp->lpni_txcredits;
996 if (lp->lpni_txcredits < 0) {
997 msg->msg_tx_delayed = 1;
998 list_add_tail(&msg->msg_list, &lp->lpni_txq);
999 spin_unlock(&lp->lpni_lock);
1000 return LNET_CREDIT_WAIT;
1002 spin_unlock(&lp->lpni_lock);
1005 if (!msg->msg_txcredit) {
1006 LASSERT((tq->tq_credits < 0) ==
1007 !list_empty(&tq->tq_delayed));
1009 msg->msg_txcredit = 1;
1011 atomic_dec(&ni->ni_tx_credits);
1013 if (tq->tq_credits < tq->tq_credits_min)
1014 tq->tq_credits_min = tq->tq_credits;
1016 if (tq->tq_credits < 0) {
1017 msg->msg_tx_delayed = 1;
1018 list_add_tail(&msg->msg_list, &tq->tq_delayed);
1019 return LNET_CREDIT_WAIT;
1023 /* unset the tx_delay flag as we're going to send it now */
1024 msg->msg_tx_delayed = 0;
1027 lnet_net_unlock(cpt);
1028 lnet_ni_send(ni, msg);
1031 return LNET_CREDIT_OK;
1035 static struct lnet_rtrbufpool *
1036 lnet_msg2bufpool(struct lnet_msg *msg)
1038 struct lnet_rtrbufpool *rbp;
1041 LASSERT(msg->msg_rx_committed);
1043 cpt = msg->msg_rx_cpt;
1044 rbp = &the_lnet.ln_rtrpools[cpt][0];
1046 LASSERT(msg->msg_len <= LNET_MTU);
1047 while (msg->msg_len > (unsigned int)rbp->rbp_npages * PAGE_SIZE) {
1049 LASSERT(rbp < &the_lnet.ln_rtrpools[cpt][LNET_NRBPOOLS]);
1056 lnet_post_routed_recv_locked(struct lnet_msg *msg, int do_recv)
1058 /* lnet_parse is going to lnet_net_unlock immediately after this, so it
1059 * sets do_recv FALSE and I don't do the unlock/send/lock bit.
1060 * I return LNET_CREDIT_WAIT if msg blocked and LNET_CREDIT_OK if
1061 * received or OK to receive */
1062 struct lnet_peer_ni *lp = msg->msg_rxpeer;
1063 struct lnet_rtrbufpool *rbp;
1064 struct lnet_rtrbuf *rb;
1066 LASSERT (msg->msg_iov == NULL);
1067 LASSERT (msg->msg_kiov == NULL);
1068 LASSERT (msg->msg_niov == 0);
1069 LASSERT (msg->msg_routing);
1070 LASSERT (msg->msg_receiving);
1071 LASSERT (!msg->msg_sending);
1073 /* non-lnet_parse callers only receive delayed messages */
1074 LASSERT(!do_recv || msg->msg_rx_delayed);
1076 if (!msg->msg_peerrtrcredit) {
1077 spin_lock(&lp->lpni_lock);
1078 LASSERT((lp->lpni_rtrcredits < 0) ==
1079 !list_empty(&lp->lpni_rtrq));
1081 msg->msg_peerrtrcredit = 1;
1082 lp->lpni_rtrcredits--;
1083 if (lp->lpni_rtrcredits < lp->lpni_minrtrcredits)
1084 lp->lpni_minrtrcredits = lp->lpni_rtrcredits;
1086 if (lp->lpni_rtrcredits < 0) {
1087 /* must have checked eager_recv before here */
1088 LASSERT(msg->msg_rx_ready_delay);
1089 msg->msg_rx_delayed = 1;
1090 list_add_tail(&msg->msg_list, &lp->lpni_rtrq);
1091 spin_unlock(&lp->lpni_lock);
1092 return LNET_CREDIT_WAIT;
1094 spin_unlock(&lp->lpni_lock);
1097 rbp = lnet_msg2bufpool(msg);
1099 if (!msg->msg_rtrcredit) {
1100 msg->msg_rtrcredit = 1;
1102 if (rbp->rbp_credits < rbp->rbp_mincredits)
1103 rbp->rbp_mincredits = rbp->rbp_credits;
1105 if (rbp->rbp_credits < 0) {
1106 /* must have checked eager_recv before here */
1107 LASSERT(msg->msg_rx_ready_delay);
1108 msg->msg_rx_delayed = 1;
1109 list_add_tail(&msg->msg_list, &rbp->rbp_msgs);
1110 return LNET_CREDIT_WAIT;
1114 LASSERT(!list_empty(&rbp->rbp_bufs));
1115 rb = list_entry(rbp->rbp_bufs.next, struct lnet_rtrbuf, rb_list);
1116 list_del(&rb->rb_list);
1118 msg->msg_niov = rbp->rbp_npages;
1119 msg->msg_kiov = &rb->rb_kiov[0];
1121 /* unset the msg-rx_delayed flag since we're receiving the message */
1122 msg->msg_rx_delayed = 0;
1125 int cpt = msg->msg_rx_cpt;
1127 lnet_net_unlock(cpt);
1128 lnet_ni_recv(msg->msg_rxni, msg->msg_private, msg, 1,
1129 0, msg->msg_len, msg->msg_len);
1132 return LNET_CREDIT_OK;
1136 lnet_return_tx_credits_locked(struct lnet_msg *msg)
1138 struct lnet_peer_ni *txpeer = msg->msg_txpeer;
1139 struct lnet_ni *txni = msg->msg_txni;
1140 struct lnet_msg *msg2;
1142 if (msg->msg_txcredit) {
1143 struct lnet_ni *ni = msg->msg_txni;
1144 struct lnet_tx_queue *tq = ni->ni_tx_queues[msg->msg_tx_cpt];
1146 /* give back NI txcredits */
1147 msg->msg_txcredit = 0;
1149 LASSERT((tq->tq_credits < 0) ==
1150 !list_empty(&tq->tq_delayed));
1153 atomic_inc(&ni->ni_tx_credits);
1154 if (tq->tq_credits <= 0) {
1155 msg2 = list_entry(tq->tq_delayed.next,
1156 struct lnet_msg, msg_list);
1157 list_del(&msg2->msg_list);
1159 LASSERT(msg2->msg_txni == ni);
1160 LASSERT(msg2->msg_tx_delayed);
1161 LASSERT(msg2->msg_tx_cpt == msg->msg_tx_cpt);
1163 (void) lnet_post_send_locked(msg2, 1);
1167 if (msg->msg_peertxcredit) {
1168 /* give back peer txcredits */
1169 msg->msg_peertxcredit = 0;
1171 spin_lock(&txpeer->lpni_lock);
1172 LASSERT((txpeer->lpni_txcredits < 0) ==
1173 !list_empty(&txpeer->lpni_txq));
1175 txpeer->lpni_txqnob -= msg->msg_len + sizeof(struct lnet_hdr);
1176 LASSERT(txpeer->lpni_txqnob >= 0);
1178 txpeer->lpni_txcredits++;
1179 if (txpeer->lpni_txcredits <= 0) {
1182 msg2 = list_entry(txpeer->lpni_txq.next,
1183 struct lnet_msg, msg_list);
1184 list_del(&msg2->msg_list);
1185 spin_unlock(&txpeer->lpni_lock);
1187 LASSERT(msg2->msg_txpeer == txpeer);
1188 LASSERT(msg2->msg_tx_delayed);
1190 msg2_cpt = msg2->msg_tx_cpt;
1193 * The msg_cpt can be different from the msg2_cpt
1194 * so we need to make sure we lock the correct cpt
1196 * Once we call lnet_post_send_locked() it is no
1197 * longer safe to access msg2, since it could've
1198 * been freed by lnet_finalize(), but we still
1199 * need to relock the correct cpt, so we cache the
1200 * msg2_cpt for the purpose of the check that
1201 * follows the call to lnet_pose_send_locked().
1203 if (msg2_cpt != msg->msg_tx_cpt) {
1204 lnet_net_unlock(msg->msg_tx_cpt);
1205 lnet_net_lock(msg2_cpt);
1207 (void) lnet_post_send_locked(msg2, 1);
1208 if (msg2_cpt != msg->msg_tx_cpt) {
1209 lnet_net_unlock(msg2_cpt);
1210 lnet_net_lock(msg->msg_tx_cpt);
1213 spin_unlock(&txpeer->lpni_lock);
1218 msg->msg_txni = NULL;
1219 lnet_ni_decref_locked(txni, msg->msg_tx_cpt);
1222 if (txpeer != NULL) {
1223 msg->msg_txpeer = NULL;
1224 lnet_peer_ni_decref_locked(txpeer);
1229 lnet_schedule_blocked_locked(struct lnet_rtrbufpool *rbp)
1231 struct lnet_msg *msg;
1233 if (list_empty(&rbp->rbp_msgs))
1235 msg = list_entry(rbp->rbp_msgs.next,
1236 struct lnet_msg, msg_list);
1237 list_del(&msg->msg_list);
1239 (void)lnet_post_routed_recv_locked(msg, 1);
1243 lnet_drop_routed_msgs_locked(struct list_head *list, int cpt)
1245 struct lnet_msg *msg;
1246 struct lnet_msg *tmp;
1248 lnet_net_unlock(cpt);
1250 list_for_each_entry_safe(msg, tmp, list, msg_list) {
1251 lnet_ni_recv(msg->msg_rxni, msg->msg_private, NULL,
1252 0, 0, 0, msg->msg_hdr.payload_length);
1253 list_del_init(&msg->msg_list);
1254 msg->msg_no_resend = true;
1255 lnet_finalize(msg, -ECANCELED);
1262 lnet_return_rx_credits_locked(struct lnet_msg *msg)
1264 struct lnet_peer_ni *rxpeer = msg->msg_rxpeer;
1265 struct lnet_ni *rxni = msg->msg_rxni;
1266 struct lnet_msg *msg2;
1268 if (msg->msg_rtrcredit) {
1269 /* give back global router credits */
1270 struct lnet_rtrbuf *rb;
1271 struct lnet_rtrbufpool *rbp;
1273 /* NB If a msg ever blocks for a buffer in rbp_msgs, it stays
1274 * there until it gets one allocated, or aborts the wait
1276 LASSERT(msg->msg_kiov != NULL);
1278 rb = list_entry(msg->msg_kiov, struct lnet_rtrbuf, rb_kiov[0]);
1281 msg->msg_kiov = NULL;
1282 msg->msg_rtrcredit = 0;
1284 LASSERT(rbp == lnet_msg2bufpool(msg));
1286 LASSERT((rbp->rbp_credits > 0) ==
1287 !list_empty(&rbp->rbp_bufs));
1289 /* If routing is now turned off, we just drop this buffer and
1290 * don't bother trying to return credits. */
1291 if (!the_lnet.ln_routing) {
1292 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1296 /* It is possible that a user has lowered the desired number of
1297 * buffers in this pool. Make sure we never put back
1298 * more buffers than the stated number. */
1299 if (unlikely(rbp->rbp_credits >= rbp->rbp_req_nbuffers)) {
1300 /* Discard this buffer so we don't have too
1302 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1303 rbp->rbp_nbuffers--;
1305 list_add(&rb->rb_list, &rbp->rbp_bufs);
1307 if (rbp->rbp_credits <= 0)
1308 lnet_schedule_blocked_locked(rbp);
1313 if (msg->msg_peerrtrcredit) {
1314 /* give back peer router credits */
1315 msg->msg_peerrtrcredit = 0;
1317 spin_lock(&rxpeer->lpni_lock);
1318 LASSERT((rxpeer->lpni_rtrcredits < 0) ==
1319 !list_empty(&rxpeer->lpni_rtrq));
1321 rxpeer->lpni_rtrcredits++;
1323 /* drop all messages which are queued to be routed on that
1325 if (!the_lnet.ln_routing) {
1326 struct list_head drop;
1327 INIT_LIST_HEAD(&drop);
1328 list_splice_init(&rxpeer->lpni_rtrq, &drop);
1329 spin_unlock(&rxpeer->lpni_lock);
1330 lnet_drop_routed_msgs_locked(&drop, msg->msg_rx_cpt);
1331 } else if (rxpeer->lpni_rtrcredits <= 0) {
1332 msg2 = list_entry(rxpeer->lpni_rtrq.next,
1333 struct lnet_msg, msg_list);
1334 list_del(&msg2->msg_list);
1335 spin_unlock(&rxpeer->lpni_lock);
1336 (void) lnet_post_routed_recv_locked(msg2, 1);
1338 spin_unlock(&rxpeer->lpni_lock);
1342 msg->msg_rxni = NULL;
1343 lnet_ni_decref_locked(rxni, msg->msg_rx_cpt);
1345 if (rxpeer != NULL) {
1346 msg->msg_rxpeer = NULL;
1347 lnet_peer_ni_decref_locked(rxpeer);
1352 lnet_compare_peers(struct lnet_peer_ni *p1, struct lnet_peer_ni *p2)
1354 if (p1->lpni_txqnob < p2->lpni_txqnob)
1357 if (p1->lpni_txqnob > p2->lpni_txqnob)
1360 if (p1->lpni_txcredits > p2->lpni_txcredits)
1363 if (p1->lpni_txcredits < p2->lpni_txcredits)
1370 lnet_compare_routes(struct lnet_route *r1, struct lnet_route *r2)
1372 struct lnet_peer_ni *p1 = r1->lr_gateway;
1373 struct lnet_peer_ni *p2 = r2->lr_gateway;
1374 int r1_hops = (r1->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r1->lr_hops;
1375 int r2_hops = (r2->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r2->lr_hops;
1378 if (r1->lr_priority < r2->lr_priority)
1381 if (r1->lr_priority > r2->lr_priority)
1384 if (r1_hops < r2_hops)
1387 if (r1_hops > r2_hops)
1390 rc = lnet_compare_peers(p1, p2);
1394 if (r1->lr_seq - r2->lr_seq <= 0)
1400 static struct lnet_peer_ni *
1401 lnet_find_route_locked(struct lnet_net *net, __u32 remote_net,
1404 struct lnet_remotenet *rnet;
1405 struct lnet_route *route;
1406 struct lnet_route *best_route;
1407 struct lnet_route *last_route;
1408 struct lnet_peer_ni *lpni_best;
1409 struct lnet_peer_ni *lp;
1412 /* If @rtr_nid is not LNET_NID_ANY, return the gateway with
1413 * rtr_nid nid, otherwise find the best gateway I can use */
1415 rnet = lnet_find_rnet_locked(remote_net);
1420 best_route = last_route = NULL;
1421 list_for_each_entry(route, &rnet->lrn_routes, lr_list) {
1422 lp = route->lr_gateway;
1424 if (!lnet_is_route_alive(route))
1427 if (net != NULL && lp->lpni_net != net)
1430 if (lp->lpni_nid == rtr_nid) /* it's pre-determined router */
1433 if (lpni_best == NULL) {
1434 best_route = last_route = route;
1439 /* no protection on below fields, but it's harmless */
1440 if (last_route->lr_seq - route->lr_seq < 0)
1443 rc = lnet_compare_routes(route, best_route);
1451 /* set sequence number on the best router to the latest sequence + 1
1452 * so we can round-robin all routers, it's race and inaccurate but
1453 * harmless and functional */
1454 if (best_route != NULL)
1455 best_route->lr_seq = last_route->lr_seq + 1;
1459 static struct lnet_ni *
1460 lnet_get_best_ni(struct lnet_net *local_net, struct lnet_ni *best_ni,
1461 struct lnet_peer *peer, struct lnet_peer_net *peer_net,
1464 struct lnet_ni *ni = NULL;
1465 unsigned int shortest_distance;
1470 * If there is no peer_ni that we can send to on this network,
1471 * then there is no point in looking for a new best_ni here.
1473 if (!lnet_get_next_peer_ni_locked(peer, peer_net, NULL))
1476 if (best_ni == NULL) {
1477 shortest_distance = UINT_MAX;
1478 best_credits = INT_MIN;
1481 shortest_distance = cfs_cpt_distance(lnet_cpt_table(), md_cpt,
1482 best_ni->ni_dev_cpt);
1483 best_credits = atomic_read(&best_ni->ni_tx_credits);
1484 best_healthv = atomic_read(&best_ni->ni_healthv);
1487 while ((ni = lnet_get_next_ni_locked(local_net, ni))) {
1488 unsigned int distance;
1493 ni_credits = atomic_read(&ni->ni_tx_credits);
1494 ni_healthv = atomic_read(&ni->ni_healthv);
1495 ni_fatal = atomic_read(&ni->ni_fatal_error_on);
1498 * calculate the distance from the CPT on which
1499 * the message memory is allocated to the CPT of
1500 * the NI's physical device
1502 distance = cfs_cpt_distance(lnet_cpt_table(),
1506 CDEBUG(D_NET, "compare ni %s [c:%d, d:%d, s:%d] with best_ni %s [c:%d, d:%d, s:%d]\n",
1507 libcfs_nid2str(ni->ni_nid), ni_credits, distance,
1508 ni->ni_seq, (best_ni) ? libcfs_nid2str(best_ni->ni_nid)
1509 : "not seleced", best_credits, shortest_distance,
1510 (best_ni) ? best_ni->ni_seq : 0);
1513 * All distances smaller than the NUMA range
1514 * are treated equally.
1516 if (distance < lnet_numa_range)
1517 distance = lnet_numa_range;
1520 * Select on health, shorter distance, available
1521 * credits, then round-robin.
1525 } else if (ni_healthv < best_healthv) {
1527 } else if (ni_healthv > best_healthv) {
1528 best_healthv = ni_healthv;
1530 * If we're going to prefer this ni because it's
1531 * the healthiest, then we should set the
1532 * shortest_distance in the algorithm in case
1533 * there are multiple NIs with the same health but
1534 * different distances.
1536 if (distance < shortest_distance)
1537 shortest_distance = distance;
1538 } else if (distance > shortest_distance) {
1540 } else if (distance < shortest_distance) {
1541 shortest_distance = distance;
1542 } else if (ni_credits < best_credits) {
1544 } else if (ni_credits == best_credits) {
1545 if (best_ni && best_ni->ni_seq <= ni->ni_seq)
1549 best_credits = ni_credits;
1552 CDEBUG(D_NET, "selected best_ni %s\n",
1553 (best_ni) ? libcfs_nid2str(best_ni->ni_nid) : "no selection");
1559 * Traffic to the LNET_RESERVED_PORTAL may not trigger peer discovery,
1560 * because such traffic is required to perform discovery. We therefore
1561 * exclude all GET and PUT on that portal. We also exclude all ACK and
1562 * REPLY traffic, but that is because the portal is not tracked in the
1563 * message structure for these message types. We could restrict this
1564 * further by also checking for LNET_PROTO_PING_MATCHBITS.
1567 lnet_msg_discovery(struct lnet_msg *msg)
1569 if (msg->msg_type == LNET_MSG_PUT) {
1570 if (msg->msg_hdr.msg.put.ptl_index != LNET_RESERVED_PORTAL)
1572 } else if (msg->msg_type == LNET_MSG_GET) {
1573 if (msg->msg_hdr.msg.get.ptl_index != LNET_RESERVED_PORTAL)
1579 #define SRC_SPEC 0x0001
1580 #define SRC_ANY 0x0002
1581 #define LOCAL_DST 0x0004
1582 #define REMOTE_DST 0x0008
1583 #define MR_DST 0x0010
1584 #define NMR_DST 0x0020
1585 #define SND_RESP 0x0040
1587 /* The following to defines are used for return codes */
1588 #define REPEAT_SEND 0x1000
1589 #define PASS_THROUGH 0x2000
1591 /* The different cases lnet_select pathway needs to handle */
1592 #define SRC_SPEC_LOCAL_MR_DST (SRC_SPEC | LOCAL_DST | MR_DST)
1593 #define SRC_SPEC_ROUTER_MR_DST (SRC_SPEC | REMOTE_DST | MR_DST)
1594 #define SRC_SPEC_LOCAL_NMR_DST (SRC_SPEC | LOCAL_DST | NMR_DST)
1595 #define SRC_SPEC_ROUTER_NMR_DST (SRC_SPEC | REMOTE_DST | NMR_DST)
1596 #define SRC_ANY_LOCAL_MR_DST (SRC_ANY | LOCAL_DST | MR_DST)
1597 #define SRC_ANY_ROUTER_MR_DST (SRC_ANY | REMOTE_DST | MR_DST)
1598 #define SRC_ANY_LOCAL_NMR_DST (SRC_ANY | LOCAL_DST | NMR_DST)
1599 #define SRC_ANY_ROUTER_NMR_DST (SRC_ANY | REMOTE_DST | NMR_DST)
1602 lnet_handle_send(struct lnet_send_data *sd)
1604 struct lnet_ni *best_ni = sd->sd_best_ni;
1605 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
1606 struct lnet_peer_ni *final_dst_lpni = sd->sd_final_dst_lpni;
1607 struct lnet_msg *msg = sd->sd_msg;
1609 __u32 send_case = sd->sd_send_case;
1611 __u32 routing = send_case & REMOTE_DST;
1612 struct lnet_rsp_tracker *rspt;
1615 * Increment sequence number of the selected peer so that we
1616 * pick the next one in Round Robin.
1618 best_lpni->lpni_seq++;
1621 * grab a reference on the peer_ni so it sticks around even if
1622 * we need to drop and relock the lnet_net_lock below.
1624 lnet_peer_ni_addref_locked(best_lpni);
1627 * Use lnet_cpt_of_nid() to determine the CPT used to commit the
1628 * message. This ensures that we get a CPT that is correct for
1629 * the NI when the NI has been restricted to a subset of all CPTs.
1630 * If the selected CPT differs from the one currently locked, we
1631 * must unlock and relock the lnet_net_lock(), and then check whether
1632 * the configuration has changed. We don't have a hold on the best_ni
1633 * yet, and it may have vanished.
1635 cpt2 = lnet_cpt_of_nid_locked(best_lpni->lpni_nid, best_ni);
1636 if (sd->sd_cpt != cpt2) {
1637 __u32 seq = lnet_get_dlc_seq_locked();
1638 lnet_net_unlock(sd->sd_cpt);
1640 lnet_net_lock(sd->sd_cpt);
1641 if (seq != lnet_get_dlc_seq_locked()) {
1642 lnet_peer_ni_decref_locked(best_lpni);
1648 * store the best_lpni in the message right away to avoid having
1649 * to do the same operation under different conditions
1651 msg->msg_txpeer = best_lpni;
1652 msg->msg_txni = best_ni;
1655 * grab a reference for the best_ni since now it's in use in this
1656 * send. The reference will be dropped in lnet_finalize()
1658 lnet_ni_addref_locked(msg->msg_txni, sd->sd_cpt);
1661 * Always set the target.nid to the best peer picked. Either the
1662 * NID will be one of the peer NIDs selected, or the same NID as
1663 * what was originally set in the target or it will be the NID of
1664 * a router if this message should be routed
1666 msg->msg_target.nid = msg->msg_txpeer->lpni_nid;
1669 * lnet_msg_commit assigns the correct cpt to the message, which
1670 * is used to decrement the correct refcount on the ni when it's
1671 * time to return the credits
1673 lnet_msg_commit(msg, sd->sd_cpt);
1676 * If we are routing the message then we keep the src_nid that was
1677 * set by the originator. If we are not routing then we are the
1678 * originator and set it here.
1680 if (!msg->msg_routing)
1681 msg->msg_hdr.src_nid = cpu_to_le64(msg->msg_txni->ni_nid);
1684 msg->msg_target_is_router = 1;
1685 msg->msg_target.pid = LNET_PID_LUSTRE;
1687 * since we're routing we want to ensure that the
1688 * msg_hdr.dest_nid is set to the final destination. When
1689 * the router receives this message it knows how to route
1692 * final_dst_lpni is set at the beginning of the
1693 * lnet_select_pathway() function and is never changed.
1694 * It's safe to use it here.
1696 msg->msg_hdr.dest_nid = cpu_to_le64(final_dst_lpni->lpni_nid);
1699 * if we're not routing set the dest_nid to the best peer
1700 * ni NID that we picked earlier in the algorithm.
1702 msg->msg_hdr.dest_nid = cpu_to_le64(msg->msg_txpeer->lpni_nid);
1706 * if we have response tracker block update it with the next hop
1710 rspt = msg->msg_md->md_rspt_ptr;
1712 rspt->rspt_next_hop_nid = msg->msg_txpeer->lpni_nid;
1713 CDEBUG(D_NET, "rspt_next_hop_nid = %s\n",
1714 libcfs_nid2str(rspt->rspt_next_hop_nid));
1718 rc = lnet_post_send_locked(msg, 0);
1721 CDEBUG(D_NET, "TRACE: %s(%s:%s) -> %s(%s:%s) : %s try# %d\n",
1722 libcfs_nid2str(msg->msg_hdr.src_nid),
1723 libcfs_nid2str(msg->msg_txni->ni_nid),
1724 libcfs_nid2str(sd->sd_src_nid),
1725 libcfs_nid2str(msg->msg_hdr.dest_nid),
1726 libcfs_nid2str(sd->sd_dst_nid),
1727 libcfs_nid2str(msg->msg_txpeer->lpni_nid),
1728 lnet_msgtyp2str(msg->msg_type), msg->msg_retry_count);
1733 static struct lnet_peer_ni *
1734 lnet_select_peer_ni(struct lnet_send_data *sd, struct lnet_peer *peer,
1735 struct lnet_peer_net *peer_net)
1738 * Look at the peer NIs for the destination peer that connect
1739 * to the chosen net. If a peer_ni is preferred when using the
1740 * best_ni to communicate, we use that one. If there is no
1741 * preferred peer_ni, or there are multiple preferred peer_ni,
1742 * the available transmit credits are used. If the transmit
1743 * credits are equal, we round-robin over the peer_ni.
1745 struct lnet_peer_ni *lpni = NULL;
1746 struct lnet_peer_ni *best_lpni = NULL;
1747 struct lnet_ni *best_ni = sd->sd_best_ni;
1748 lnet_nid_t dst_nid = sd->sd_dst_nid;
1749 int best_lpni_credits = INT_MIN;
1750 bool preferred = false;
1752 int best_lpni_healthv = 0;
1755 while ((lpni = lnet_get_next_peer_ni_locked(peer, peer_net, lpni))) {
1757 * if the best_ni we've chosen aleady has this lpni
1758 * preferred, then let's use it
1760 ni_is_pref = lnet_peer_is_pref_nid_locked(lpni,
1763 lpni_healthv = atomic_read(&lpni->lpni_healthv);
1765 CDEBUG(D_NET, "%s ni_is_pref = %d\n",
1766 libcfs_nid2str(best_ni->ni_nid), ni_is_pref);
1769 CDEBUG(D_NET, "%s c:[%d, %d], s:[%d, %d]\n",
1770 libcfs_nid2str(lpni->lpni_nid),
1771 lpni->lpni_txcredits, best_lpni_credits,
1772 lpni->lpni_seq, best_lpni->lpni_seq);
1774 /* pick the healthiest peer ni */
1775 if (lpni_healthv < best_lpni_healthv) {
1777 } else if (lpni_healthv > best_lpni_healthv) {
1778 best_lpni_healthv = lpni_healthv;
1779 /* if this is a preferred peer use it */
1780 } else if (!preferred && ni_is_pref) {
1782 } else if (preferred && !ni_is_pref) {
1784 * this is not the preferred peer so let's ignore
1788 } else if (lpni->lpni_txcredits < best_lpni_credits) {
1790 * We already have a peer that has more credits
1791 * available than this one. No need to consider
1792 * this peer further.
1795 } else if (lpni->lpni_txcredits == best_lpni_credits) {
1797 * The best peer found so far and the current peer
1798 * have the same number of available credits let's
1799 * make sure to select between them using Round
1803 if (best_lpni->lpni_seq <= lpni->lpni_seq)
1809 best_lpni_credits = lpni->lpni_txcredits;
1812 /* if we still can't find a peer ni then we can't reach it */
1814 __u32 net_id = (peer_net) ? peer_net->lpn_net_id :
1815 LNET_NIDNET(dst_nid);
1816 CDEBUG(D_NET, "no peer_ni found on peer net %s\n",
1817 libcfs_net2str(net_id));
1821 CDEBUG(D_NET, "sd_best_lpni = %s\n",
1822 libcfs_nid2str(best_lpni->lpni_nid));
1828 * Prerequisite: the best_ni should already be set in the sd
1830 static inline struct lnet_peer_ni *
1831 lnet_find_best_lpni_on_net(struct lnet_send_data *sd, struct lnet_peer *peer,
1834 struct lnet_peer_net *peer_net;
1837 * The gateway is Multi-Rail capable so now we must select the
1840 peer_net = lnet_peer_get_net_locked(peer, net_id);
1843 CERROR("gateway peer %s has no NI on net %s\n",
1844 libcfs_nid2str(peer->lp_primary_nid),
1845 libcfs_net2str(net_id));
1849 return lnet_select_peer_ni(sd, peer, peer_net);
1853 lnet_set_non_mr_pref_nid(struct lnet_send_data *sd)
1855 if (sd->sd_send_case & NMR_DST &&
1856 sd->sd_msg->msg_type != LNET_MSG_REPLY &&
1857 sd->sd_msg->msg_type != LNET_MSG_ACK &&
1858 sd->sd_best_lpni->lpni_pref_nnids == 0) {
1859 CDEBUG(D_NET, "Setting preferred local NID %s on NMR peer %s\n",
1860 libcfs_nid2str(sd->sd_best_ni->ni_nid),
1861 libcfs_nid2str(sd->sd_best_lpni->lpni_nid));
1862 lnet_peer_ni_set_non_mr_pref_nid(sd->sd_best_lpni,
1863 sd->sd_best_ni->ni_nid);
1872 * use the source and destination NIDs as the pathway
1875 lnet_handle_spec_local_nmr_dst(struct lnet_send_data *sd)
1877 /* the destination lpni is set before we get here. */
1880 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
1881 if (!sd->sd_best_ni) {
1882 CERROR("Can't send to %s: src %s is not a "
1883 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
1884 libcfs_nid2str(sd->sd_src_nid));
1889 * the preferred NID will only be set for NMR peers
1891 lnet_set_non_mr_pref_nid(sd);
1893 return lnet_handle_send(sd);
1901 * Run the selection algorithm on the peer NIs unless we're sending
1902 * a response, in this case just send to the destination
1905 lnet_handle_spec_local_mr_dst(struct lnet_send_data *sd)
1907 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
1908 if (!sd->sd_best_ni) {
1909 CERROR("Can't send to %s: src %s is not a "
1910 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
1911 libcfs_nid2str(sd->sd_src_nid));
1916 * only run the selection algorithm to pick the peer_ni if we're
1917 * sending a GET or a PUT. Responses are sent to the same
1918 * destination NID provided.
1920 if (!(sd->sd_send_case & SND_RESP)) {
1922 lnet_find_best_lpni_on_net(sd, sd->sd_peer,
1923 sd->sd_best_ni->ni_net->net_id);
1926 if (sd->sd_best_lpni)
1927 return lnet_handle_send(sd);
1929 CERROR("can't send to %s. no NI on %s\n",
1930 libcfs_nid2str(sd->sd_dst_nid),
1931 libcfs_net2str(sd->sd_best_ni->ni_net->net_id));
1933 return -EHOSTUNREACH;
1937 lnet_find_best_ni_on_spec_net(struct lnet_ni *cur_best_ni,
1938 struct lnet_peer *peer,
1939 struct lnet_peer_net *peer_net,
1943 struct lnet_net *local_net;
1944 struct lnet_ni *best_ni;
1946 local_net = lnet_get_net_locked(peer_net->lpn_net_id);
1951 * Iterate through the NIs in this local Net and select
1952 * the NI to send from. The selection is determined by
1953 * these 3 criterion in the following priority:
1955 * 2. NI available credits
1958 best_ni = lnet_get_best_ni(local_net, cur_best_ni,
1959 peer, peer_net, cpt);
1961 if (incr_seq && best_ni)
1968 lnet_handle_find_routed_path(struct lnet_send_data *sd,
1970 struct lnet_peer_ni **gw_lpni,
1971 struct lnet_peer **gw_peer)
1973 struct lnet_peer_ni *gw;
1974 lnet_nid_t src_nid = sd->sd_src_nid;
1976 gw = lnet_find_route_locked(NULL, LNET_NIDNET(dst_nid),
1979 CERROR("no route to %s from %s\n",
1980 libcfs_nid2str(dst_nid), libcfs_nid2str(src_nid));
1981 return -EHOSTUNREACH;
1984 /* get the peer of the gw_ni */
1985 LASSERT(gw->lpni_peer_net);
1986 LASSERT(gw->lpni_peer_net->lpn_peer);
1988 *gw_peer = gw->lpni_peer_net->lpn_peer;
1990 if (!sd->sd_best_ni)
1991 sd->sd_best_ni = lnet_find_best_ni_on_spec_net(NULL, *gw_peer,
1996 if (!sd->sd_best_ni) {
1997 CERROR("Internal Error. Expected local ni on %s "
1998 "but non found :%s\n",
1999 libcfs_net2str(gw->lpni_peer_net->lpn_net_id),
2000 libcfs_nid2str(sd->sd_src_nid));
2005 * if gw is MR let's find its best peer_ni
2007 if (lnet_peer_is_multi_rail(*gw_peer)) {
2008 gw = lnet_find_best_lpni_on_net(sd, *gw_peer,
2009 sd->sd_best_ni->ni_net->net_id);
2011 * We've already verified that the gw has an NI on that
2012 * desired net, but we're not finding it. Something is
2016 CERROR("Internal Error. Route expected to %s from %s\n",
2017 libcfs_nid2str(dst_nid),
2018 libcfs_nid2str(src_nid));
2033 * Remote destination
2034 * Non-MR destination
2038 * Remote destination
2041 * The handling of these two cases is similar. Even though the destination
2042 * can be MR or non-MR, we'll deal directly with the router.
2045 lnet_handle_spec_router_dst(struct lnet_send_data *sd)
2048 struct lnet_peer_ni *gw_lpni = NULL;
2049 struct lnet_peer *gw_peer = NULL;
2052 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
2053 if (!sd->sd_best_ni) {
2054 CERROR("Can't send to %s: src %s is not a "
2055 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
2056 libcfs_nid2str(sd->sd_src_nid));
2060 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2065 if (sd->sd_send_case & NMR_DST)
2067 * since the final destination is non-MR let's set its preferred
2068 * NID before we send
2070 lnet_set_non_mr_pref_nid(sd);
2073 * We're going to send to the gw found so let's set its
2076 sd->sd_peer = gw_peer;
2077 sd->sd_best_lpni = gw_lpni;
2079 return lnet_handle_send(sd);
2083 lnet_find_best_ni_on_local_net(struct lnet_peer *peer, int md_cpt)
2085 struct lnet_peer_net *peer_net = NULL;
2086 struct lnet_ni *best_ni = NULL;
2089 * The peer can have multiple interfaces, some of them can be on
2090 * the local network and others on a routed network. We should
2091 * prefer the local network. However if the local network is not
2092 * available then we need to try the routed network
2095 /* go through all the peer nets and find the best_ni */
2096 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_peer_nets) {
2098 * The peer's list of nets can contain non-local nets. We
2099 * want to only examine the local ones.
2101 if (!lnet_get_net_locked(peer_net->lpn_net_id))
2103 best_ni = lnet_find_best_ni_on_spec_net(best_ni, peer,
2104 peer_net, md_cpt, false);
2108 /* increment sequence number so we can round robin */
2114 static struct lnet_ni *
2115 lnet_find_existing_preferred_best_ni(struct lnet_send_data *sd)
2117 struct lnet_ni *best_ni = NULL;
2118 struct lnet_peer_net *peer_net;
2119 struct lnet_peer *peer = sd->sd_peer;
2120 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
2121 struct lnet_peer_ni *lpni;
2122 int cpt = sd->sd_cpt;
2125 * We must use a consistent source address when sending to a
2126 * non-MR peer. However, a non-MR peer can have multiple NIDs
2127 * on multiple networks, and we may even need to talk to this
2128 * peer on multiple networks -- certain types of
2129 * load-balancing configuration do this.
2131 * So we need to pick the NI the peer prefers for this
2132 * particular network.
2135 /* Get the target peer_ni */
2136 peer_net = lnet_peer_get_net_locked(peer,
2137 LNET_NIDNET(best_lpni->lpni_nid));
2138 LASSERT(peer_net != NULL);
2139 list_for_each_entry(lpni, &peer_net->lpn_peer_nis,
2141 if (lpni->lpni_pref_nnids == 0)
2143 LASSERT(lpni->lpni_pref_nnids == 1);
2144 best_ni = lnet_nid2ni_locked(
2145 lpni->lpni_pref.nid, cpt);
2152 /* Prerequisite: sd->sd_peer and sd->sd_best_lpni should be set */
2154 lnet_select_preferred_best_ni(struct lnet_send_data *sd)
2156 struct lnet_ni *best_ni = NULL;
2157 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
2160 * We must use a consistent source address when sending to a
2161 * non-MR peer. However, a non-MR peer can have multiple NIDs
2162 * on multiple networks, and we may even need to talk to this
2163 * peer on multiple networks -- certain types of
2164 * load-balancing configuration do this.
2166 * So we need to pick the NI the peer prefers for this
2167 * particular network.
2170 best_ni = lnet_find_existing_preferred_best_ni(sd);
2172 /* if best_ni is still not set just pick one */
2175 lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2176 sd->sd_best_lpni->lpni_peer_net,
2177 sd->sd_md_cpt, true);
2178 /* If there is no best_ni we don't have a route */
2180 CERROR("no path to %s from net %s\n",
2181 libcfs_nid2str(best_lpni->lpni_nid),
2182 libcfs_net2str(best_lpni->lpni_net->net_id));
2183 return -EHOSTUNREACH;
2187 sd->sd_best_ni = best_ni;
2189 /* Set preferred NI if necessary. */
2190 lnet_set_non_mr_pref_nid(sd);
2197 * Source not specified
2201 * always use the same source NID for NMR peers
2202 * If we've talked to that peer before then we already have a preferred
2203 * source NI associated with it. Otherwise, we select a preferred local NI
2204 * and store it in the peer
2207 lnet_handle_any_local_nmr_dst(struct lnet_send_data *sd)
2211 /* sd->sd_best_lpni is already set to the final destination */
2214 * At this point we should've created the peer ni and peer. If we
2215 * can't find it, then something went wrong. Instead of assert
2216 * output a relevant message and fail the send
2218 if (!sd->sd_best_lpni) {
2219 CERROR("Internal fault. Unable to send msg %s to %s. "
2221 lnet_msgtyp2str(sd->sd_msg->msg_type),
2222 libcfs_nid2str(sd->sd_dst_nid));
2226 rc = lnet_select_preferred_best_ni(sd);
2228 rc = lnet_handle_send(sd);
2234 lnet_handle_any_mr_dsta(struct lnet_send_data *sd)
2237 * NOTE we've already handled the remote peer case. So we only
2238 * need to worry about the local case here.
2240 * if we're sending a response, ACK or reply, we need to send it
2241 * to the destination NID given to us. At this point we already
2242 * have the peer_ni we're suppose to send to, so just find the
2243 * best_ni on the peer net and use that. Since we're sending to an
2244 * MR peer then we can just run the selection algorithm on our
2245 * local NIs and pick the best one.
2247 if (sd->sd_send_case & SND_RESP) {
2249 lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2250 sd->sd_best_lpni->lpni_peer_net,
2251 sd->sd_md_cpt, true);
2253 if (!sd->sd_best_ni) {
2255 * We're not going to deal with not able to send
2256 * a response to the provided final destination
2258 CERROR("Can't send response to %s. "
2259 "No local NI available\n",
2260 libcfs_nid2str(sd->sd_dst_nid));
2261 return -EHOSTUNREACH;
2264 return lnet_handle_send(sd);
2268 * If we get here that means we're sending a fresh request, PUT or
2269 * GET, so we need to run our standard selection algorithm.
2270 * First find the best local interface that's on any of the peer's
2273 sd->sd_best_ni = lnet_find_best_ni_on_local_net(sd->sd_peer,
2275 if (sd->sd_best_ni) {
2277 lnet_find_best_lpni_on_net(sd, sd->sd_peer,
2278 sd->sd_best_ni->ni_net->net_id);
2281 * if we're successful in selecting a peer_ni on the local
2282 * network, then send to it. Otherwise fall through and
2283 * try and see if we can reach it over another routed
2286 if (sd->sd_best_lpni) {
2288 * in case we initially started with a routed
2289 * destination, let's reset to local
2291 sd->sd_send_case &= ~REMOTE_DST;
2292 sd->sd_send_case |= LOCAL_DST;
2293 return lnet_handle_send(sd);
2296 CERROR("Internal Error. Expected to have a best_lpni: "
2298 libcfs_nid2str(sd->sd_src_nid),
2299 libcfs_nid2str(sd->sd_dst_nid));
2305 * Peer doesn't have a local network. Let's see if there is
2306 * a remote network we can reach it on.
2308 return PASS_THROUGH;
2313 * Source NID not specified
2318 * Source NID not speified
2319 * Remote destination
2322 * In both of these cases if we're sending a response, ACK or REPLY, then
2323 * we need to send to the destination NID provided.
2325 * In the remote case let's deal with MR routers.
2330 lnet_handle_any_mr_dst(struct lnet_send_data *sd)
2333 struct lnet_peer *gw_peer = NULL;
2334 struct lnet_peer_ni *gw_lpni = NULL;
2337 * handle sending a response to a remote peer here so we don't
2338 * have to worry about it if we hit lnet_handle_any_mr_dsta()
2340 if (sd->sd_send_case & REMOTE_DST &&
2341 sd->sd_send_case & SND_RESP) {
2342 struct lnet_peer_ni *gw;
2343 struct lnet_peer *gw_peer;
2345 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw,
2348 CERROR("Can't send response to %s. "
2349 "No route available\n",
2350 libcfs_nid2str(sd->sd_dst_nid));
2351 return -EHOSTUNREACH;
2354 sd->sd_best_lpni = gw;
2355 sd->sd_peer = gw_peer;
2357 return lnet_handle_send(sd);
2361 * Even though the NID for the peer might not be on a local network,
2362 * since the peer is MR there could be other interfaces on the
2363 * local network. In that case we'd still like to prefer the local
2364 * network over the routed network. If we're unable to do that
2365 * then we select the best router among the different routed networks,
2366 * and if the router is MR then we can deal with it as such.
2368 rc = lnet_handle_any_mr_dsta(sd);
2369 if (rc != PASS_THROUGH)
2373 * TODO; One possible enhancement is to run the selection
2374 * algorithm on the peer. However for remote peers the credits are
2375 * not decremented, so we'll be basically going over the peer NIs
2376 * in round robin. An MR router will run the selection algorithm
2377 * on the next-hop interfaces.
2379 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2384 sd->sd_send_case &= ~LOCAL_DST;
2385 sd->sd_send_case |= REMOTE_DST;
2387 sd->sd_peer = gw_peer;
2388 sd->sd_best_lpni = gw_lpni;
2390 return lnet_handle_send(sd);
2394 * Source not specified
2395 * Remote destination
2398 * Must send to the specified peer NID using the same source NID that
2399 * we've used before. If it's the first time to talk to that peer then
2400 * find the source NI and assign it as preferred to that peer
2403 lnet_handle_any_router_nmr_dst(struct lnet_send_data *sd)
2406 struct lnet_peer_ni *gw_lpni = NULL;
2407 struct lnet_peer *gw_peer = NULL;
2410 * Let's set if we have a preferred NI to talk to this NMR peer
2412 sd->sd_best_ni = lnet_find_existing_preferred_best_ni(sd);
2415 * find the router and that'll find the best NI if we didn't find
2418 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2424 * set the best_ni we've chosen as the preferred one for
2427 lnet_set_non_mr_pref_nid(sd);
2429 /* we'll be sending to the gw */
2430 sd->sd_best_lpni = gw_lpni;
2431 sd->sd_peer = gw_peer;
2433 return lnet_handle_send(sd);
2437 lnet_handle_send_case_locked(struct lnet_send_data *sd)
2440 * turn off the SND_RESP bit.
2441 * It will be checked in the case handling
2443 __u32 send_case = sd->sd_send_case &= ~SND_RESP ;
2445 CDEBUG(D_NET, "Source %s%s to %s %s %s destination\n",
2446 (send_case & SRC_SPEC) ? "Specified: " : "ANY",
2447 (send_case & SRC_SPEC) ? libcfs_nid2str(sd->sd_src_nid) : "",
2448 (send_case & MR_DST) ? "MR: " : "NMR: ",
2449 libcfs_nid2str(sd->sd_dst_nid),
2450 (send_case & LOCAL_DST) ? "local" : "routed");
2452 switch (send_case) {
2454 * For all cases where the source is specified, we should always
2455 * use the destination NID, whether it's an MR destination or not,
2456 * since we're continuing a series of related messages for the
2459 case SRC_SPEC_LOCAL_NMR_DST:
2460 return lnet_handle_spec_local_nmr_dst(sd);
2461 case SRC_SPEC_LOCAL_MR_DST:
2462 return lnet_handle_spec_local_mr_dst(sd);
2463 case SRC_SPEC_ROUTER_NMR_DST:
2464 case SRC_SPEC_ROUTER_MR_DST:
2465 return lnet_handle_spec_router_dst(sd);
2466 case SRC_ANY_LOCAL_NMR_DST:
2467 return lnet_handle_any_local_nmr_dst(sd);
2468 case SRC_ANY_LOCAL_MR_DST:
2469 case SRC_ANY_ROUTER_MR_DST:
2470 return lnet_handle_any_mr_dst(sd);
2471 case SRC_ANY_ROUTER_NMR_DST:
2472 return lnet_handle_any_router_nmr_dst(sd);
2474 CERROR("Unknown send case\n");
2480 lnet_select_pathway(lnet_nid_t src_nid, lnet_nid_t dst_nid,
2481 struct lnet_msg *msg, lnet_nid_t rtr_nid)
2483 struct lnet_peer_ni *lpni;
2484 struct lnet_peer *peer;
2485 struct lnet_send_data send_data;
2488 __u32 send_case = 0;
2490 memset(&send_data, 0, sizeof(send_data));
2493 * get an initial CPT to use for locking. The idea here is not to
2494 * serialize the calls to select_pathway, so that as many
2495 * operations can run concurrently as possible. To do that we use
2496 * the CPT where this call is being executed. Later on when we
2497 * determine the CPT to use in lnet_message_commit, we switch the
2498 * lock and check if there was any configuration change. If none,
2499 * then we proceed, if there is, then we restart the operation.
2501 cpt = lnet_net_lock_current();
2503 md_cpt = lnet_cpt_of_md(msg->msg_md, msg->msg_offset);
2504 if (md_cpt == CFS_CPT_ANY)
2510 * If we're being asked to send to the loopback interface, there
2511 * is no need to go through any selection. We can just shortcut
2512 * the entire process and send over lolnd
2514 if (LNET_NETTYP(LNET_NIDNET(dst_nid)) == LOLND) {
2515 /* No send credit hassles with LOLND */
2516 lnet_ni_addref_locked(the_lnet.ln_loni, cpt);
2517 msg->msg_hdr.dest_nid = cpu_to_le64(the_lnet.ln_loni->ni_nid);
2518 if (!msg->msg_routing)
2519 msg->msg_hdr.src_nid =
2520 cpu_to_le64(the_lnet.ln_loni->ni_nid);
2521 msg->msg_target.nid = the_lnet.ln_loni->ni_nid;
2522 lnet_msg_commit(msg, cpt);
2523 msg->msg_txni = the_lnet.ln_loni;
2524 lnet_net_unlock(cpt);
2526 return LNET_CREDIT_OK;
2530 * find an existing peer_ni, or create one and mark it as having been
2531 * created due to network traffic. This call will create the
2532 * peer->peer_net->peer_ni tree.
2534 lpni = lnet_nid2peerni_locked(dst_nid, LNET_NID_ANY, cpt);
2536 lnet_net_unlock(cpt);
2537 return PTR_ERR(lpni);
2541 * Cache the original src_nid. If we need to resend the message
2542 * then we'll need to know whether the src_nid was originally
2543 * specified for this message. If it was originally specified,
2544 * then we need to keep using the same src_nid since it's
2545 * continuing the same sequence of messages.
2547 msg->msg_src_nid_param = src_nid;
2550 * Now that we have a peer_ni, check if we want to discover
2551 * the peer. Traffic to the LNET_RESERVED_PORTAL should not
2552 * trigger discovery.
2554 peer = lpni->lpni_peer_net->lpn_peer;
2555 if (lnet_msg_discovery(msg) && !lnet_peer_is_uptodate(peer)) {
2556 lnet_nid_t primary_nid;
2557 rc = lnet_discover_peer_locked(lpni, cpt, false);
2559 lnet_peer_ni_decref_locked(lpni);
2560 lnet_net_unlock(cpt);
2563 /* The peer may have changed. */
2564 peer = lpni->lpni_peer_net->lpn_peer;
2565 /* queue message and return */
2566 msg->msg_rtr_nid_param = rtr_nid;
2567 msg->msg_sending = 0;
2568 list_add_tail(&msg->msg_list, &peer->lp_dc_pendq);
2569 lnet_peer_ni_decref_locked(lpni);
2570 primary_nid = peer->lp_primary_nid;
2571 lnet_net_unlock(cpt);
2573 CDEBUG(D_NET, "%s pending discovery\n",
2574 libcfs_nid2str(primary_nid));
2576 return LNET_DC_WAIT;
2578 lnet_peer_ni_decref_locked(lpni);
2581 * Identify the different send cases
2583 if (src_nid == LNET_NID_ANY)
2584 send_case |= SRC_ANY;
2586 send_case |= SRC_SPEC;
2588 if (lnet_get_net_locked(LNET_NIDNET(dst_nid)))
2589 send_case |= LOCAL_DST;
2591 send_case |= REMOTE_DST;
2594 * if this is a non-MR peer or if we're recovering a peer ni then
2595 * let's consider this an NMR case so we can hit the destination
2598 if (!lnet_peer_is_multi_rail(peer) || msg->msg_recovery)
2599 send_case |= NMR_DST;
2601 send_case |= MR_DST;
2603 if (msg->msg_type == LNET_MSG_REPLY ||
2604 msg->msg_type == LNET_MSG_ACK)
2605 send_case |= SND_RESP;
2607 /* assign parameters to the send_data */
2608 send_data.sd_msg = msg;
2609 send_data.sd_rtr_nid = rtr_nid;
2610 send_data.sd_src_nid = src_nid;
2611 send_data.sd_dst_nid = dst_nid;
2612 send_data.sd_best_lpni = lpni;
2614 * keep a pointer to the final destination in case we're going to
2615 * route, so we'll need to access it later
2617 send_data.sd_final_dst_lpni = lpni;
2618 send_data.sd_peer = peer;
2619 send_data.sd_md_cpt = md_cpt;
2620 send_data.sd_cpt = cpt;
2621 send_data.sd_send_case = send_case;
2623 rc = lnet_handle_send_case_locked(&send_data);
2625 if (rc == REPEAT_SEND)
2628 lnet_net_unlock(send_data.sd_cpt);
2634 lnet_send(lnet_nid_t src_nid, struct lnet_msg *msg, lnet_nid_t rtr_nid)
2636 lnet_nid_t dst_nid = msg->msg_target.nid;
2640 * NB: rtr_nid is set to LNET_NID_ANY for all current use-cases,
2641 * but we might want to use pre-determined router for ACK/REPLY
2644 /* NB: ni != NULL == interface pre-determined (ACK/REPLY) */
2645 LASSERT(msg->msg_txpeer == NULL);
2646 LASSERT(msg->msg_txni == NULL);
2647 LASSERT(!msg->msg_sending);
2648 LASSERT(!msg->msg_target_is_router);
2649 LASSERT(!msg->msg_receiving);
2651 msg->msg_sending = 1;
2653 LASSERT(!msg->msg_tx_committed);
2655 rc = lnet_select_pathway(src_nid, dst_nid, msg, rtr_nid);
2659 if (rc == LNET_CREDIT_OK)
2660 lnet_ni_send(msg->msg_txni, msg);
2662 /* rc == LNET_CREDIT_OK or LNET_CREDIT_WAIT or LNET_DC_WAIT */
2666 enum lnet_mt_event_type {
2667 MT_TYPE_LOCAL_NI = 0,
2671 struct lnet_mt_event_info {
2672 enum lnet_mt_event_type mt_type;
2676 /* called with res_lock held */
2678 lnet_detach_rsp_tracker(struct lnet_libmd *md, int cpt)
2680 struct lnet_rsp_tracker *rspt;
2683 * msg has a refcount on the MD so the MD is not going away.
2684 * The rspt queue for the cpt is protected by
2685 * the lnet_net_lock(cpt). cpt is the cpt of the MD cookie.
2687 if (!md->md_rspt_ptr)
2690 rspt = md->md_rspt_ptr;
2691 md->md_rspt_ptr = NULL;
2694 LASSERT(rspt->rspt_cpt == cpt);
2697 * invalidate the handle to indicate that a response has been
2698 * received, which will then lead the monitor thread to clean up
2701 LNetInvalidateMDHandle(&rspt->rspt_mdh);
2705 lnet_finalize_expired_responses(bool force)
2707 struct lnet_libmd *md;
2708 struct list_head local_queue;
2709 struct lnet_rsp_tracker *rspt, *tmp;
2712 if (the_lnet.ln_mt_rstq == NULL)
2715 cfs_cpt_for_each(i, lnet_cpt_table()) {
2716 INIT_LIST_HEAD(&local_queue);
2719 if (!the_lnet.ln_mt_rstq[i]) {
2723 list_splice_init(the_lnet.ln_mt_rstq[i], &local_queue);
2726 list_for_each_entry_safe(rspt, tmp, &local_queue, rspt_on_list) {
2728 * The rspt mdh will be invalidated when a response
2729 * is received or whenever we want to discard the
2730 * block the monitor thread will walk the queue
2731 * and clean up any rsts with an invalid mdh.
2732 * The monitor thread will walk the queue until
2733 * the first unexpired rspt block. This means that
2734 * some rspt blocks which received their
2735 * corresponding responses will linger in the
2736 * queue until they are cleaned up eventually.
2739 if (LNetMDHandleIsInvalid(rspt->rspt_mdh)) {
2741 list_del_init(&rspt->rspt_on_list);
2742 lnet_rspt_free(rspt, i);
2746 if (ktime_compare(ktime_get(), rspt->rspt_deadline) >= 0 ||
2748 struct lnet_peer_ni *lpni;
2751 md = lnet_handle2md(&rspt->rspt_mdh);
2753 LNetInvalidateMDHandle(&rspt->rspt_mdh);
2755 list_del_init(&rspt->rspt_on_list);
2756 lnet_rspt_free(rspt, i);
2759 LASSERT(md->md_rspt_ptr == rspt);
2760 md->md_rspt_ptr = NULL;
2764 the_lnet.ln_counters[i]->lct_health.lch_response_timeout_count++;
2767 list_del_init(&rspt->rspt_on_list);
2769 nid = rspt->rspt_next_hop_nid;
2771 CNETERR("Response timed out: md = %p: nid = %s\n",
2772 md, libcfs_nid2str(nid));
2773 LNetMDUnlink(rspt->rspt_mdh);
2774 lnet_rspt_free(rspt, i);
2777 * If there is a timeout on the response
2778 * from the next hop decrement its health
2779 * value so that we don't use it
2782 lpni = lnet_find_peer_ni_locked(nid);
2784 lnet_handle_remote_failure_locked(lpni);
2785 lnet_peer_ni_decref_locked(lpni);
2795 if (!list_empty(&local_queue))
2796 list_splice(&local_queue, the_lnet.ln_mt_rstq[i]);
2802 lnet_resend_pending_msgs_locked(struct list_head *resendq, int cpt)
2804 struct lnet_msg *msg;
2806 while (!list_empty(resendq)) {
2807 struct lnet_peer_ni *lpni;
2809 msg = list_entry(resendq->next, struct lnet_msg,
2812 list_del_init(&msg->msg_list);
2814 lpni = lnet_find_peer_ni_locked(msg->msg_hdr.dest_nid);
2816 lnet_net_unlock(cpt);
2817 CERROR("Expected that a peer is already created for %s\n",
2818 libcfs_nid2str(msg->msg_hdr.dest_nid));
2819 msg->msg_no_resend = true;
2820 lnet_finalize(msg, -EFAULT);
2823 struct lnet_peer *peer;
2825 lnet_nid_t src_nid = LNET_NID_ANY;
2828 * if this message is not being routed and the
2829 * peer is non-MR then we must use the same
2830 * src_nid that was used in the original send.
2831 * Otherwise if we're routing the message (IE
2832 * we're a router) then we can use any of our
2833 * local interfaces. It doesn't matter to the
2834 * final destination.
2836 peer = lpni->lpni_peer_net->lpn_peer;
2837 if (!msg->msg_routing &&
2838 !lnet_peer_is_multi_rail(peer))
2839 src_nid = le64_to_cpu(msg->msg_hdr.src_nid);
2842 * If we originally specified a src NID, then we
2843 * must attempt to reuse it in the resend as well.
2845 if (msg->msg_src_nid_param != LNET_NID_ANY)
2846 src_nid = msg->msg_src_nid_param;
2847 lnet_peer_ni_decref_locked(lpni);
2849 lnet_net_unlock(cpt);
2850 CDEBUG(D_NET, "resending %s->%s: %s recovery %d try# %d\n",
2851 libcfs_nid2str(src_nid),
2852 libcfs_id2str(msg->msg_target),
2853 lnet_msgtyp2str(msg->msg_type),
2855 msg->msg_retry_count);
2856 rc = lnet_send(src_nid, msg, LNET_NID_ANY);
2858 CERROR("Error sending %s to %s: %d\n",
2859 lnet_msgtyp2str(msg->msg_type),
2860 libcfs_id2str(msg->msg_target), rc);
2861 msg->msg_no_resend = true;
2862 lnet_finalize(msg, rc);
2866 the_lnet.ln_counters[cpt]->lct_health.lch_resend_count++;
2872 lnet_resend_pending_msgs(void)
2876 cfs_cpt_for_each(i, lnet_cpt_table()) {
2878 lnet_resend_pending_msgs_locked(the_lnet.ln_mt_resendqs[i], i);
2883 /* called with cpt and ni_lock held */
2885 lnet_unlink_ni_recovery_mdh_locked(struct lnet_ni *ni, int cpt, bool force)
2887 struct lnet_handle_md recovery_mdh;
2889 LNetInvalidateMDHandle(&recovery_mdh);
2891 if (ni->ni_recovery_state & LNET_NI_RECOVERY_PENDING ||
2893 recovery_mdh = ni->ni_ping_mdh;
2894 LNetInvalidateMDHandle(&ni->ni_ping_mdh);
2897 lnet_net_unlock(cpt);
2898 if (!LNetMDHandleIsInvalid(recovery_mdh))
2899 LNetMDUnlink(recovery_mdh);
2905 lnet_recover_local_nis(void)
2907 struct lnet_mt_event_info *ev_info;
2908 struct list_head processed_list;
2909 struct list_head local_queue;
2910 struct lnet_handle_md mdh;
2911 struct lnet_ni *tmp;
2917 INIT_LIST_HEAD(&local_queue);
2918 INIT_LIST_HEAD(&processed_list);
2921 * splice the recovery queue on a local queue. We will iterate
2922 * through the local queue and update it as needed. Once we're
2923 * done with the traversal, we'll splice the local queue back on
2924 * the head of the ln_mt_localNIRecovq. Any newly added local NIs
2925 * will be traversed in the next iteration.
2928 list_splice_init(&the_lnet.ln_mt_localNIRecovq,
2932 list_for_each_entry_safe(ni, tmp, &local_queue, ni_recovery) {
2934 * if an NI is being deleted or it is now healthy, there
2935 * is no need to keep it around in the recovery queue.
2936 * The monitor thread is the only thread responsible for
2937 * removing the NI from the recovery queue.
2938 * Multiple threads can be adding NIs to the recovery
2941 healthv = atomic_read(&ni->ni_healthv);
2945 if (ni->ni_state != LNET_NI_STATE_ACTIVE ||
2946 healthv == LNET_MAX_HEALTH_VALUE) {
2947 list_del_init(&ni->ni_recovery);
2948 lnet_unlink_ni_recovery_mdh_locked(ni, 0, false);
2950 lnet_ni_decref_locked(ni, 0);
2956 * if the local NI failed recovery we must unlink the md.
2957 * But we want to keep the local_ni on the recovery queue
2958 * so we can continue the attempts to recover it.
2960 if (ni->ni_recovery_state & LNET_NI_RECOVERY_FAILED) {
2961 lnet_unlink_ni_recovery_mdh_locked(ni, 0, true);
2962 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_FAILED;
2969 CDEBUG(D_NET, "attempting to recover local ni: %s\n",
2970 libcfs_nid2str(ni->ni_nid));
2973 if (!(ni->ni_recovery_state & LNET_NI_RECOVERY_PENDING)) {
2974 ni->ni_recovery_state |= LNET_NI_RECOVERY_PENDING;
2977 LIBCFS_ALLOC(ev_info, sizeof(*ev_info));
2979 CERROR("out of memory. Can't recover %s\n",
2980 libcfs_nid2str(ni->ni_nid));
2982 ni->ni_recovery_state &=
2983 ~LNET_NI_RECOVERY_PENDING;
2988 mdh = ni->ni_ping_mdh;
2990 * Invalidate the ni mdh in case it's deleted.
2991 * We'll unlink the mdh in this case below.
2993 LNetInvalidateMDHandle(&ni->ni_ping_mdh);
2997 * remove the NI from the local queue and drop the
2998 * reference count to it while we're recovering
2999 * it. The reason for that, is that the NI could
3000 * be deleted, and the way the code is structured
3001 * is if we don't drop the NI, then the deletion
3002 * code will enter a loop waiting for the
3003 * reference count to be removed while holding the
3004 * ln_mutex_lock(). When we look up the peer to
3005 * send to in lnet_select_pathway() we will try to
3006 * lock the ln_mutex_lock() as well, leading to
3007 * a deadlock. By dropping the refcount and
3008 * removing it from the list, we allow for the NI
3009 * to be removed, then we use the cached NID to
3010 * look it up again. If it's gone, then we just
3011 * continue examining the rest of the queue.
3014 list_del_init(&ni->ni_recovery);
3015 lnet_ni_decref_locked(ni, 0);
3018 ev_info->mt_type = MT_TYPE_LOCAL_NI;
3019 ev_info->mt_nid = nid;
3020 rc = lnet_send_ping(nid, &mdh, LNET_INTERFACES_MIN,
3021 ev_info, the_lnet.ln_mt_eqh, true);
3022 /* lookup the nid again */
3024 ni = lnet_nid2ni_locked(nid, 0);
3027 * the NI has been deleted when we dropped
3035 * Same note as in lnet_recover_peer_nis(). When
3036 * we're sending the ping, the NI is free to be
3037 * deleted or manipulated. By this point it
3038 * could've been added back on the recovery queue,
3039 * and a refcount taken on it.
3040 * So we can't just add it blindly again or we'll
3041 * corrupt the queue. We must check under lock if
3042 * it's not on any list and if not then add it
3043 * to the processed list, which will eventually be
3044 * spliced back on to the recovery queue.
3046 ni->ni_ping_mdh = mdh;
3047 if (list_empty(&ni->ni_recovery)) {
3048 list_add_tail(&ni->ni_recovery, &processed_list);
3049 lnet_ni_addref_locked(ni, 0);
3055 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_PENDING;
3061 * put back the remaining NIs on the ln_mt_localNIRecovq to be
3062 * reexamined in the next iteration.
3064 list_splice_init(&processed_list, &local_queue);
3066 list_splice(&local_queue, &the_lnet.ln_mt_localNIRecovq);
3070 static struct list_head **
3071 lnet_create_array_of_queues(void)
3073 struct list_head **qs;
3074 struct list_head *q;
3077 qs = cfs_percpt_alloc(lnet_cpt_table(),
3078 sizeof(struct list_head));
3080 CERROR("Failed to allocate queues\n");
3084 cfs_percpt_for_each(q, i, qs)
3091 lnet_resendqs_create(void)
3093 struct list_head **resendqs;
3094 resendqs = lnet_create_array_of_queues();
3099 lnet_net_lock(LNET_LOCK_EX);
3100 the_lnet.ln_mt_resendqs = resendqs;
3101 lnet_net_unlock(LNET_LOCK_EX);
3107 lnet_clean_local_ni_recoveryq(void)
3111 /* This is only called when the monitor thread has stopped */
3114 while (!list_empty(&the_lnet.ln_mt_localNIRecovq)) {
3115 ni = list_entry(the_lnet.ln_mt_localNIRecovq.next,
3116 struct lnet_ni, ni_recovery);
3117 list_del_init(&ni->ni_recovery);
3119 lnet_unlink_ni_recovery_mdh_locked(ni, 0, true);
3121 lnet_ni_decref_locked(ni, 0);
3128 lnet_unlink_lpni_recovery_mdh_locked(struct lnet_peer_ni *lpni, int cpt,
3131 struct lnet_handle_md recovery_mdh;
3133 LNetInvalidateMDHandle(&recovery_mdh);
3135 if (lpni->lpni_state & LNET_PEER_NI_RECOVERY_PENDING || force) {
3136 recovery_mdh = lpni->lpni_recovery_ping_mdh;
3137 LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
3139 spin_unlock(&lpni->lpni_lock);
3140 lnet_net_unlock(cpt);
3141 if (!LNetMDHandleIsInvalid(recovery_mdh))
3142 LNetMDUnlink(recovery_mdh);
3144 spin_lock(&lpni->lpni_lock);
3148 lnet_clean_peer_ni_recoveryq(void)
3150 struct lnet_peer_ni *lpni, *tmp;
3152 lnet_net_lock(LNET_LOCK_EX);
3154 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_mt_peerNIRecovq,
3156 list_del_init(&lpni->lpni_recovery);
3157 spin_lock(&lpni->lpni_lock);
3158 lnet_unlink_lpni_recovery_mdh_locked(lpni, LNET_LOCK_EX, true);
3159 spin_unlock(&lpni->lpni_lock);
3160 lnet_peer_ni_decref_locked(lpni);
3163 lnet_net_unlock(LNET_LOCK_EX);
3167 lnet_clean_resendqs(void)
3169 struct lnet_msg *msg, *tmp;
3170 struct list_head msgs;
3173 INIT_LIST_HEAD(&msgs);
3175 cfs_cpt_for_each(i, lnet_cpt_table()) {
3177 list_splice_init(the_lnet.ln_mt_resendqs[i], &msgs);
3179 list_for_each_entry_safe(msg, tmp, &msgs, msg_list) {
3180 list_del_init(&msg->msg_list);
3181 msg->msg_no_resend = true;
3182 lnet_finalize(msg, -ESHUTDOWN);
3186 cfs_percpt_free(the_lnet.ln_mt_resendqs);
3190 lnet_recover_peer_nis(void)
3192 struct lnet_mt_event_info *ev_info;
3193 struct list_head processed_list;
3194 struct list_head local_queue;
3195 struct lnet_handle_md mdh;
3196 struct lnet_peer_ni *lpni;
3197 struct lnet_peer_ni *tmp;
3202 INIT_LIST_HEAD(&local_queue);
3203 INIT_LIST_HEAD(&processed_list);
3206 * Always use cpt 0 for locking across all interactions with
3207 * ln_mt_peerNIRecovq
3210 list_splice_init(&the_lnet.ln_mt_peerNIRecovq,
3214 list_for_each_entry_safe(lpni, tmp, &local_queue,
3217 * The same protection strategy is used here as is in the
3218 * local recovery case.
3221 healthv = atomic_read(&lpni->lpni_healthv);
3222 spin_lock(&lpni->lpni_lock);
3223 if (lpni->lpni_state & LNET_PEER_NI_DELETING ||
3224 healthv == LNET_MAX_HEALTH_VALUE) {
3225 list_del_init(&lpni->lpni_recovery);
3226 lnet_unlink_lpni_recovery_mdh_locked(lpni, 0, false);
3227 spin_unlock(&lpni->lpni_lock);
3228 lnet_peer_ni_decref_locked(lpni);
3234 * If the peer NI has failed recovery we must unlink the
3235 * md. But we want to keep the peer ni on the recovery
3236 * queue so we can try to continue recovering it
3238 if (lpni->lpni_state & LNET_PEER_NI_RECOVERY_FAILED) {
3239 lnet_unlink_lpni_recovery_mdh_locked(lpni, 0, true);
3240 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_FAILED;
3243 spin_unlock(&lpni->lpni_lock);
3247 * NOTE: we're racing with peer deletion from user space.
3248 * It's possible that a peer is deleted after we check its
3249 * state. In this case the recovery can create a new peer
3251 spin_lock(&lpni->lpni_lock);