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);
941 /* can't get here if we're sending to the loopback interface */
942 LASSERT(lp->lpni_nid != the_lnet.ln_loni->ni_nid);
944 /* NB 'lp' is always the next hop */
945 if ((msg->msg_target.pid & LNET_PID_USERFLAG) == 0 &&
946 lnet_peer_alive_locked(ni, lp, msg) == 0) {
947 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
948 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length +=
950 lnet_net_unlock(cpt);
952 lnet_incr_stats(&msg->msg_txpeer->lpni_stats,
954 LNET_STATS_TYPE_DROP);
956 lnet_incr_stats(&msg->msg_txni->ni_stats,
958 LNET_STATS_TYPE_DROP);
960 CNETERR("Dropping message for %s: peer not alive\n",
961 libcfs_id2str(msg->msg_target));
962 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_DROPPED;
964 lnet_finalize(msg, -EHOSTUNREACH);
967 return -EHOSTUNREACH;
970 if (msg->msg_md != NULL &&
971 (msg->msg_md->md_flags & LNET_MD_FLAG_ABORTED) != 0) {
972 lnet_net_unlock(cpt);
974 CNETERR("Aborting message for %s: LNetM[DE]Unlink() already "
975 "called on the MD/ME.\n",
976 libcfs_id2str(msg->msg_target));
978 msg->msg_no_resend = true;
979 CDEBUG(D_NET, "msg %p to %s canceled and will not be resent\n",
980 msg, libcfs_id2str(msg->msg_target));
981 lnet_finalize(msg, -ECANCELED);
988 if (!msg->msg_peertxcredit) {
989 spin_lock(&lp->lpni_lock);
990 LASSERT((lp->lpni_txcredits < 0) ==
991 !list_empty(&lp->lpni_txq));
993 msg->msg_peertxcredit = 1;
994 lp->lpni_txqnob += msg->msg_len + sizeof(struct lnet_hdr);
995 lp->lpni_txcredits--;
997 if (lp->lpni_txcredits < lp->lpni_mintxcredits)
998 lp->lpni_mintxcredits = lp->lpni_txcredits;
1000 if (lp->lpni_txcredits < 0) {
1001 msg->msg_tx_delayed = 1;
1002 list_add_tail(&msg->msg_list, &lp->lpni_txq);
1003 spin_unlock(&lp->lpni_lock);
1004 return LNET_CREDIT_WAIT;
1006 spin_unlock(&lp->lpni_lock);
1009 if (!msg->msg_txcredit) {
1010 LASSERT((tq->tq_credits < 0) ==
1011 !list_empty(&tq->tq_delayed));
1013 msg->msg_txcredit = 1;
1015 atomic_dec(&ni->ni_tx_credits);
1017 if (tq->tq_credits < tq->tq_credits_min)
1018 tq->tq_credits_min = tq->tq_credits;
1020 if (tq->tq_credits < 0) {
1021 msg->msg_tx_delayed = 1;
1022 list_add_tail(&msg->msg_list, &tq->tq_delayed);
1023 return LNET_CREDIT_WAIT;
1027 /* unset the tx_delay flag as we're going to send it now */
1028 msg->msg_tx_delayed = 0;
1031 lnet_net_unlock(cpt);
1032 lnet_ni_send(ni, msg);
1035 return LNET_CREDIT_OK;
1039 static struct lnet_rtrbufpool *
1040 lnet_msg2bufpool(struct lnet_msg *msg)
1042 struct lnet_rtrbufpool *rbp;
1045 LASSERT(msg->msg_rx_committed);
1047 cpt = msg->msg_rx_cpt;
1048 rbp = &the_lnet.ln_rtrpools[cpt][0];
1050 LASSERT(msg->msg_len <= LNET_MTU);
1051 while (msg->msg_len > (unsigned int)rbp->rbp_npages * PAGE_SIZE) {
1053 LASSERT(rbp < &the_lnet.ln_rtrpools[cpt][LNET_NRBPOOLS]);
1060 lnet_post_routed_recv_locked(struct lnet_msg *msg, int do_recv)
1062 /* lnet_parse is going to lnet_net_unlock immediately after this, so it
1063 * sets do_recv FALSE and I don't do the unlock/send/lock bit.
1064 * I return LNET_CREDIT_WAIT if msg blocked and LNET_CREDIT_OK if
1065 * received or OK to receive */
1066 struct lnet_peer_ni *lp = msg->msg_rxpeer;
1067 struct lnet_rtrbufpool *rbp;
1068 struct lnet_rtrbuf *rb;
1070 LASSERT (msg->msg_iov == NULL);
1071 LASSERT (msg->msg_kiov == NULL);
1072 LASSERT (msg->msg_niov == 0);
1073 LASSERT (msg->msg_routing);
1074 LASSERT (msg->msg_receiving);
1075 LASSERT (!msg->msg_sending);
1077 /* non-lnet_parse callers only receive delayed messages */
1078 LASSERT(!do_recv || msg->msg_rx_delayed);
1080 if (!msg->msg_peerrtrcredit) {
1081 spin_lock(&lp->lpni_lock);
1082 LASSERT((lp->lpni_rtrcredits < 0) ==
1083 !list_empty(&lp->lpni_rtrq));
1085 msg->msg_peerrtrcredit = 1;
1086 lp->lpni_rtrcredits--;
1087 if (lp->lpni_rtrcredits < lp->lpni_minrtrcredits)
1088 lp->lpni_minrtrcredits = lp->lpni_rtrcredits;
1090 if (lp->lpni_rtrcredits < 0) {
1091 /* must have checked eager_recv before here */
1092 LASSERT(msg->msg_rx_ready_delay);
1093 msg->msg_rx_delayed = 1;
1094 list_add_tail(&msg->msg_list, &lp->lpni_rtrq);
1095 spin_unlock(&lp->lpni_lock);
1096 return LNET_CREDIT_WAIT;
1098 spin_unlock(&lp->lpni_lock);
1101 rbp = lnet_msg2bufpool(msg);
1103 if (!msg->msg_rtrcredit) {
1104 msg->msg_rtrcredit = 1;
1106 if (rbp->rbp_credits < rbp->rbp_mincredits)
1107 rbp->rbp_mincredits = rbp->rbp_credits;
1109 if (rbp->rbp_credits < 0) {
1110 /* must have checked eager_recv before here */
1111 LASSERT(msg->msg_rx_ready_delay);
1112 msg->msg_rx_delayed = 1;
1113 list_add_tail(&msg->msg_list, &rbp->rbp_msgs);
1114 return LNET_CREDIT_WAIT;
1118 LASSERT(!list_empty(&rbp->rbp_bufs));
1119 rb = list_entry(rbp->rbp_bufs.next, struct lnet_rtrbuf, rb_list);
1120 list_del(&rb->rb_list);
1122 msg->msg_niov = rbp->rbp_npages;
1123 msg->msg_kiov = &rb->rb_kiov[0];
1125 /* unset the msg-rx_delayed flag since we're receiving the message */
1126 msg->msg_rx_delayed = 0;
1129 int cpt = msg->msg_rx_cpt;
1131 lnet_net_unlock(cpt);
1132 lnet_ni_recv(msg->msg_rxni, msg->msg_private, msg, 1,
1133 0, msg->msg_len, msg->msg_len);
1136 return LNET_CREDIT_OK;
1140 lnet_return_tx_credits_locked(struct lnet_msg *msg)
1142 struct lnet_peer_ni *txpeer = msg->msg_txpeer;
1143 struct lnet_ni *txni = msg->msg_txni;
1144 struct lnet_msg *msg2;
1146 if (msg->msg_txcredit) {
1147 struct lnet_ni *ni = msg->msg_txni;
1148 struct lnet_tx_queue *tq = ni->ni_tx_queues[msg->msg_tx_cpt];
1150 /* give back NI txcredits */
1151 msg->msg_txcredit = 0;
1153 LASSERT((tq->tq_credits < 0) ==
1154 !list_empty(&tq->tq_delayed));
1157 atomic_inc(&ni->ni_tx_credits);
1158 if (tq->tq_credits <= 0) {
1159 msg2 = list_entry(tq->tq_delayed.next,
1160 struct lnet_msg, msg_list);
1161 list_del(&msg2->msg_list);
1163 LASSERT(msg2->msg_txni == ni);
1164 LASSERT(msg2->msg_tx_delayed);
1165 LASSERT(msg2->msg_tx_cpt == msg->msg_tx_cpt);
1167 (void) lnet_post_send_locked(msg2, 1);
1171 if (msg->msg_peertxcredit) {
1172 /* give back peer txcredits */
1173 msg->msg_peertxcredit = 0;
1175 spin_lock(&txpeer->lpni_lock);
1176 LASSERT((txpeer->lpni_txcredits < 0) ==
1177 !list_empty(&txpeer->lpni_txq));
1179 txpeer->lpni_txqnob -= msg->msg_len + sizeof(struct lnet_hdr);
1180 LASSERT(txpeer->lpni_txqnob >= 0);
1182 txpeer->lpni_txcredits++;
1183 if (txpeer->lpni_txcredits <= 0) {
1186 msg2 = list_entry(txpeer->lpni_txq.next,
1187 struct lnet_msg, msg_list);
1188 list_del(&msg2->msg_list);
1189 spin_unlock(&txpeer->lpni_lock);
1191 LASSERT(msg2->msg_txpeer == txpeer);
1192 LASSERT(msg2->msg_tx_delayed);
1194 msg2_cpt = msg2->msg_tx_cpt;
1197 * The msg_cpt can be different from the msg2_cpt
1198 * so we need to make sure we lock the correct cpt
1200 * Once we call lnet_post_send_locked() it is no
1201 * longer safe to access msg2, since it could've
1202 * been freed by lnet_finalize(), but we still
1203 * need to relock the correct cpt, so we cache the
1204 * msg2_cpt for the purpose of the check that
1205 * follows the call to lnet_pose_send_locked().
1207 if (msg2_cpt != msg->msg_tx_cpt) {
1208 lnet_net_unlock(msg->msg_tx_cpt);
1209 lnet_net_lock(msg2_cpt);
1211 (void) lnet_post_send_locked(msg2, 1);
1212 if (msg2_cpt != msg->msg_tx_cpt) {
1213 lnet_net_unlock(msg2_cpt);
1214 lnet_net_lock(msg->msg_tx_cpt);
1217 spin_unlock(&txpeer->lpni_lock);
1222 msg->msg_txni = NULL;
1223 lnet_ni_decref_locked(txni, msg->msg_tx_cpt);
1226 if (txpeer != NULL) {
1227 msg->msg_txpeer = NULL;
1228 lnet_peer_ni_decref_locked(txpeer);
1233 lnet_schedule_blocked_locked(struct lnet_rtrbufpool *rbp)
1235 struct lnet_msg *msg;
1237 if (list_empty(&rbp->rbp_msgs))
1239 msg = list_entry(rbp->rbp_msgs.next,
1240 struct lnet_msg, msg_list);
1241 list_del(&msg->msg_list);
1243 (void)lnet_post_routed_recv_locked(msg, 1);
1247 lnet_drop_routed_msgs_locked(struct list_head *list, int cpt)
1249 struct lnet_msg *msg;
1250 struct lnet_msg *tmp;
1252 lnet_net_unlock(cpt);
1254 list_for_each_entry_safe(msg, tmp, list, msg_list) {
1255 lnet_ni_recv(msg->msg_rxni, msg->msg_private, NULL,
1256 0, 0, 0, msg->msg_hdr.payload_length);
1257 list_del_init(&msg->msg_list);
1258 msg->msg_no_resend = true;
1259 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_ERROR;
1260 lnet_finalize(msg, -ECANCELED);
1267 lnet_return_rx_credits_locked(struct lnet_msg *msg)
1269 struct lnet_peer_ni *rxpeer = msg->msg_rxpeer;
1270 struct lnet_ni *rxni = msg->msg_rxni;
1271 struct lnet_msg *msg2;
1273 if (msg->msg_rtrcredit) {
1274 /* give back global router credits */
1275 struct lnet_rtrbuf *rb;
1276 struct lnet_rtrbufpool *rbp;
1278 /* NB If a msg ever blocks for a buffer in rbp_msgs, it stays
1279 * there until it gets one allocated, or aborts the wait
1281 LASSERT(msg->msg_kiov != NULL);
1283 rb = list_entry(msg->msg_kiov, struct lnet_rtrbuf, rb_kiov[0]);
1286 msg->msg_kiov = NULL;
1287 msg->msg_rtrcredit = 0;
1289 LASSERT(rbp == lnet_msg2bufpool(msg));
1291 LASSERT((rbp->rbp_credits > 0) ==
1292 !list_empty(&rbp->rbp_bufs));
1294 /* If routing is now turned off, we just drop this buffer and
1295 * don't bother trying to return credits. */
1296 if (!the_lnet.ln_routing) {
1297 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1301 /* It is possible that a user has lowered the desired number of
1302 * buffers in this pool. Make sure we never put back
1303 * more buffers than the stated number. */
1304 if (unlikely(rbp->rbp_credits >= rbp->rbp_req_nbuffers)) {
1305 /* Discard this buffer so we don't have too
1307 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1308 rbp->rbp_nbuffers--;
1310 list_add(&rb->rb_list, &rbp->rbp_bufs);
1312 if (rbp->rbp_credits <= 0)
1313 lnet_schedule_blocked_locked(rbp);
1318 if (msg->msg_peerrtrcredit) {
1319 /* give back peer router credits */
1320 msg->msg_peerrtrcredit = 0;
1322 spin_lock(&rxpeer->lpni_lock);
1323 LASSERT((rxpeer->lpni_rtrcredits < 0) ==
1324 !list_empty(&rxpeer->lpni_rtrq));
1326 rxpeer->lpni_rtrcredits++;
1328 /* drop all messages which are queued to be routed on that
1330 if (!the_lnet.ln_routing) {
1331 struct list_head drop;
1332 INIT_LIST_HEAD(&drop);
1333 list_splice_init(&rxpeer->lpni_rtrq, &drop);
1334 spin_unlock(&rxpeer->lpni_lock);
1335 lnet_drop_routed_msgs_locked(&drop, msg->msg_rx_cpt);
1336 } else if (rxpeer->lpni_rtrcredits <= 0) {
1337 msg2 = list_entry(rxpeer->lpni_rtrq.next,
1338 struct lnet_msg, msg_list);
1339 list_del(&msg2->msg_list);
1340 spin_unlock(&rxpeer->lpni_lock);
1341 (void) lnet_post_routed_recv_locked(msg2, 1);
1343 spin_unlock(&rxpeer->lpni_lock);
1347 msg->msg_rxni = NULL;
1348 lnet_ni_decref_locked(rxni, msg->msg_rx_cpt);
1350 if (rxpeer != NULL) {
1351 msg->msg_rxpeer = NULL;
1352 lnet_peer_ni_decref_locked(rxpeer);
1357 lnet_compare_peers(struct lnet_peer_ni *p1, struct lnet_peer_ni *p2)
1359 if (p1->lpni_txqnob < p2->lpni_txqnob)
1362 if (p1->lpni_txqnob > p2->lpni_txqnob)
1365 if (p1->lpni_txcredits > p2->lpni_txcredits)
1368 if (p1->lpni_txcredits < p2->lpni_txcredits)
1375 lnet_compare_routes(struct lnet_route *r1, struct lnet_route *r2)
1377 struct lnet_peer_ni *p1 = r1->lr_gateway;
1378 struct lnet_peer_ni *p2 = r2->lr_gateway;
1379 int r1_hops = (r1->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r1->lr_hops;
1380 int r2_hops = (r2->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r2->lr_hops;
1383 if (r1->lr_priority < r2->lr_priority)
1386 if (r1->lr_priority > r2->lr_priority)
1389 if (r1_hops < r2_hops)
1392 if (r1_hops > r2_hops)
1395 rc = lnet_compare_peers(p1, p2);
1399 if (r1->lr_seq - r2->lr_seq <= 0)
1405 static struct lnet_peer_ni *
1406 lnet_find_route_locked(struct lnet_net *net, __u32 remote_net,
1409 struct lnet_remotenet *rnet;
1410 struct lnet_route *route;
1411 struct lnet_route *best_route;
1412 struct lnet_route *last_route;
1413 struct lnet_peer_ni *lpni_best;
1414 struct lnet_peer_ni *lp;
1417 /* If @rtr_nid is not LNET_NID_ANY, return the gateway with
1418 * rtr_nid nid, otherwise find the best gateway I can use */
1420 rnet = lnet_find_rnet_locked(remote_net);
1425 best_route = last_route = NULL;
1426 list_for_each_entry(route, &rnet->lrn_routes, lr_list) {
1427 lp = route->lr_gateway;
1429 if (!lnet_is_route_alive(route))
1432 if (net != NULL && lp->lpni_net != net)
1435 if (lp->lpni_nid == rtr_nid) /* it's pre-determined router */
1438 if (lpni_best == NULL) {
1439 best_route = last_route = route;
1444 /* no protection on below fields, but it's harmless */
1445 if (last_route->lr_seq - route->lr_seq < 0)
1448 rc = lnet_compare_routes(route, best_route);
1456 /* set sequence number on the best router to the latest sequence + 1
1457 * so we can round-robin all routers, it's race and inaccurate but
1458 * harmless and functional */
1459 if (best_route != NULL)
1460 best_route->lr_seq = last_route->lr_seq + 1;
1464 static struct lnet_ni *
1465 lnet_get_best_ni(struct lnet_net *local_net, struct lnet_ni *best_ni,
1466 struct lnet_peer *peer, struct lnet_peer_net *peer_net,
1469 struct lnet_ni *ni = NULL;
1470 unsigned int shortest_distance;
1475 * If there is no peer_ni that we can send to on this network,
1476 * then there is no point in looking for a new best_ni here.
1478 if (!lnet_get_next_peer_ni_locked(peer, peer_net, NULL))
1481 if (best_ni == NULL) {
1482 shortest_distance = UINT_MAX;
1483 best_credits = INT_MIN;
1486 shortest_distance = cfs_cpt_distance(lnet_cpt_table(), md_cpt,
1487 best_ni->ni_dev_cpt);
1488 best_credits = atomic_read(&best_ni->ni_tx_credits);
1489 best_healthv = atomic_read(&best_ni->ni_healthv);
1492 while ((ni = lnet_get_next_ni_locked(local_net, ni))) {
1493 unsigned int distance;
1498 ni_credits = atomic_read(&ni->ni_tx_credits);
1499 ni_healthv = atomic_read(&ni->ni_healthv);
1500 ni_fatal = atomic_read(&ni->ni_fatal_error_on);
1503 * calculate the distance from the CPT on which
1504 * the message memory is allocated to the CPT of
1505 * the NI's physical device
1507 distance = cfs_cpt_distance(lnet_cpt_table(),
1511 CDEBUG(D_NET, "compare ni %s [c:%d, d:%d, s:%d] with best_ni %s [c:%d, d:%d, s:%d]\n",
1512 libcfs_nid2str(ni->ni_nid), ni_credits, distance,
1513 ni->ni_seq, (best_ni) ? libcfs_nid2str(best_ni->ni_nid)
1514 : "not seleced", best_credits, shortest_distance,
1515 (best_ni) ? best_ni->ni_seq : 0);
1518 * All distances smaller than the NUMA range
1519 * are treated equally.
1521 if (distance < lnet_numa_range)
1522 distance = lnet_numa_range;
1525 * Select on health, shorter distance, available
1526 * credits, then round-robin.
1530 } else if (ni_healthv < best_healthv) {
1532 } else if (ni_healthv > best_healthv) {
1533 best_healthv = ni_healthv;
1535 * If we're going to prefer this ni because it's
1536 * the healthiest, then we should set the
1537 * shortest_distance in the algorithm in case
1538 * there are multiple NIs with the same health but
1539 * different distances.
1541 if (distance < shortest_distance)
1542 shortest_distance = distance;
1543 } else if (distance > shortest_distance) {
1545 } else if (distance < shortest_distance) {
1546 shortest_distance = distance;
1547 } else if (ni_credits < best_credits) {
1549 } else if (ni_credits == best_credits) {
1550 if (best_ni && best_ni->ni_seq <= ni->ni_seq)
1554 best_credits = ni_credits;
1557 CDEBUG(D_NET, "selected best_ni %s\n",
1558 (best_ni) ? libcfs_nid2str(best_ni->ni_nid) : "no selection");
1564 * Traffic to the LNET_RESERVED_PORTAL may not trigger peer discovery,
1565 * because such traffic is required to perform discovery. We therefore
1566 * exclude all GET and PUT on that portal. We also exclude all ACK and
1567 * REPLY traffic, but that is because the portal is not tracked in the
1568 * message structure for these message types. We could restrict this
1569 * further by also checking for LNET_PROTO_PING_MATCHBITS.
1572 lnet_msg_discovery(struct lnet_msg *msg)
1574 if (msg->msg_type == LNET_MSG_PUT) {
1575 if (msg->msg_hdr.msg.put.ptl_index != LNET_RESERVED_PORTAL)
1577 } else if (msg->msg_type == LNET_MSG_GET) {
1578 if (msg->msg_hdr.msg.get.ptl_index != LNET_RESERVED_PORTAL)
1584 #define SRC_SPEC 0x0001
1585 #define SRC_ANY 0x0002
1586 #define LOCAL_DST 0x0004
1587 #define REMOTE_DST 0x0008
1588 #define MR_DST 0x0010
1589 #define NMR_DST 0x0020
1590 #define SND_RESP 0x0040
1592 /* The following to defines are used for return codes */
1593 #define REPEAT_SEND 0x1000
1594 #define PASS_THROUGH 0x2000
1596 /* The different cases lnet_select pathway needs to handle */
1597 #define SRC_SPEC_LOCAL_MR_DST (SRC_SPEC | LOCAL_DST | MR_DST)
1598 #define SRC_SPEC_ROUTER_MR_DST (SRC_SPEC | REMOTE_DST | MR_DST)
1599 #define SRC_SPEC_LOCAL_NMR_DST (SRC_SPEC | LOCAL_DST | NMR_DST)
1600 #define SRC_SPEC_ROUTER_NMR_DST (SRC_SPEC | REMOTE_DST | NMR_DST)
1601 #define SRC_ANY_LOCAL_MR_DST (SRC_ANY | LOCAL_DST | MR_DST)
1602 #define SRC_ANY_ROUTER_MR_DST (SRC_ANY | REMOTE_DST | MR_DST)
1603 #define SRC_ANY_LOCAL_NMR_DST (SRC_ANY | LOCAL_DST | NMR_DST)
1604 #define SRC_ANY_ROUTER_NMR_DST (SRC_ANY | REMOTE_DST | NMR_DST)
1607 lnet_handle_lo_send(struct lnet_send_data *sd)
1609 struct lnet_msg *msg = sd->sd_msg;
1610 int cpt = sd->sd_cpt;
1612 /* No send credit hassles with LOLND */
1613 lnet_ni_addref_locked(the_lnet.ln_loni, cpt);
1614 msg->msg_hdr.dest_nid = cpu_to_le64(the_lnet.ln_loni->ni_nid);
1615 if (!msg->msg_routing)
1616 msg->msg_hdr.src_nid =
1617 cpu_to_le64(the_lnet.ln_loni->ni_nid);
1618 msg->msg_target.nid = the_lnet.ln_loni->ni_nid;
1619 lnet_msg_commit(msg, cpt);
1620 msg->msg_txni = the_lnet.ln_loni;
1622 return LNET_CREDIT_OK;
1626 lnet_handle_send(struct lnet_send_data *sd)
1628 struct lnet_ni *best_ni = sd->sd_best_ni;
1629 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
1630 struct lnet_peer_ni *final_dst_lpni = sd->sd_final_dst_lpni;
1631 struct lnet_msg *msg = sd->sd_msg;
1633 __u32 send_case = sd->sd_send_case;
1635 __u32 routing = send_case & REMOTE_DST;
1636 struct lnet_rsp_tracker *rspt;
1639 * Increment sequence number of the selected peer so that we
1640 * pick the next one in Round Robin.
1642 best_lpni->lpni_seq++;
1645 * grab a reference on the peer_ni so it sticks around even if
1646 * we need to drop and relock the lnet_net_lock below.
1648 lnet_peer_ni_addref_locked(best_lpni);
1651 * Use lnet_cpt_of_nid() to determine the CPT used to commit the
1652 * message. This ensures that we get a CPT that is correct for
1653 * the NI when the NI has been restricted to a subset of all CPTs.
1654 * If the selected CPT differs from the one currently locked, we
1655 * must unlock and relock the lnet_net_lock(), and then check whether
1656 * the configuration has changed. We don't have a hold on the best_ni
1657 * yet, and it may have vanished.
1659 cpt2 = lnet_cpt_of_nid_locked(best_lpni->lpni_nid, best_ni);
1660 if (sd->sd_cpt != cpt2) {
1661 __u32 seq = lnet_get_dlc_seq_locked();
1662 lnet_net_unlock(sd->sd_cpt);
1664 lnet_net_lock(sd->sd_cpt);
1665 if (seq != lnet_get_dlc_seq_locked()) {
1666 lnet_peer_ni_decref_locked(best_lpni);
1672 * store the best_lpni in the message right away to avoid having
1673 * to do the same operation under different conditions
1675 msg->msg_txpeer = best_lpni;
1676 msg->msg_txni = best_ni;
1679 * grab a reference for the best_ni since now it's in use in this
1680 * send. The reference will be dropped in lnet_finalize()
1682 lnet_ni_addref_locked(msg->msg_txni, sd->sd_cpt);
1685 * Always set the target.nid to the best peer picked. Either the
1686 * NID will be one of the peer NIDs selected, or the same NID as
1687 * what was originally set in the target or it will be the NID of
1688 * a router if this message should be routed
1690 msg->msg_target.nid = msg->msg_txpeer->lpni_nid;
1693 * lnet_msg_commit assigns the correct cpt to the message, which
1694 * is used to decrement the correct refcount on the ni when it's
1695 * time to return the credits
1697 lnet_msg_commit(msg, sd->sd_cpt);
1700 * If we are routing the message then we keep the src_nid that was
1701 * set by the originator. If we are not routing then we are the
1702 * originator and set it here.
1704 if (!msg->msg_routing)
1705 msg->msg_hdr.src_nid = cpu_to_le64(msg->msg_txni->ni_nid);
1708 msg->msg_target_is_router = 1;
1709 msg->msg_target.pid = LNET_PID_LUSTRE;
1711 * since we're routing we want to ensure that the
1712 * msg_hdr.dest_nid is set to the final destination. When
1713 * the router receives this message it knows how to route
1716 * final_dst_lpni is set at the beginning of the
1717 * lnet_select_pathway() function and is never changed.
1718 * It's safe to use it here.
1720 msg->msg_hdr.dest_nid = cpu_to_le64(final_dst_lpni->lpni_nid);
1723 * if we're not routing set the dest_nid to the best peer
1724 * ni NID that we picked earlier in the algorithm.
1726 msg->msg_hdr.dest_nid = cpu_to_le64(msg->msg_txpeer->lpni_nid);
1730 * if we have response tracker block update it with the next hop
1734 rspt = msg->msg_md->md_rspt_ptr;
1736 rspt->rspt_next_hop_nid = msg->msg_txpeer->lpni_nid;
1737 CDEBUG(D_NET, "rspt_next_hop_nid = %s\n",
1738 libcfs_nid2str(rspt->rspt_next_hop_nid));
1742 rc = lnet_post_send_locked(msg, 0);
1745 CDEBUG(D_NET, "TRACE: %s(%s:%s) -> %s(%s:%s) : %s try# %d\n",
1746 libcfs_nid2str(msg->msg_hdr.src_nid),
1747 libcfs_nid2str(msg->msg_txni->ni_nid),
1748 libcfs_nid2str(sd->sd_src_nid),
1749 libcfs_nid2str(msg->msg_hdr.dest_nid),
1750 libcfs_nid2str(sd->sd_dst_nid),
1751 libcfs_nid2str(msg->msg_txpeer->lpni_nid),
1752 lnet_msgtyp2str(msg->msg_type), msg->msg_retry_count);
1757 static struct lnet_peer_ni *
1758 lnet_select_peer_ni(struct lnet_send_data *sd, struct lnet_peer *peer,
1759 struct lnet_peer_net *peer_net)
1762 * Look at the peer NIs for the destination peer that connect
1763 * to the chosen net. If a peer_ni is preferred when using the
1764 * best_ni to communicate, we use that one. If there is no
1765 * preferred peer_ni, or there are multiple preferred peer_ni,
1766 * the available transmit credits are used. If the transmit
1767 * credits are equal, we round-robin over the peer_ni.
1769 struct lnet_peer_ni *lpni = NULL;
1770 struct lnet_peer_ni *best_lpni = NULL;
1771 struct lnet_ni *best_ni = sd->sd_best_ni;
1772 lnet_nid_t dst_nid = sd->sd_dst_nid;
1773 int best_lpni_credits = INT_MIN;
1774 bool preferred = false;
1776 int best_lpni_healthv = 0;
1779 while ((lpni = lnet_get_next_peer_ni_locked(peer, peer_net, lpni))) {
1781 * if the best_ni we've chosen aleady has this lpni
1782 * preferred, then let's use it
1784 ni_is_pref = lnet_peer_is_pref_nid_locked(lpni,
1787 lpni_healthv = atomic_read(&lpni->lpni_healthv);
1789 CDEBUG(D_NET, "%s ni_is_pref = %d\n",
1790 libcfs_nid2str(best_ni->ni_nid), ni_is_pref);
1793 CDEBUG(D_NET, "%s c:[%d, %d], s:[%d, %d]\n",
1794 libcfs_nid2str(lpni->lpni_nid),
1795 lpni->lpni_txcredits, best_lpni_credits,
1796 lpni->lpni_seq, best_lpni->lpni_seq);
1798 /* pick the healthiest peer ni */
1799 if (lpni_healthv < best_lpni_healthv) {
1801 } else if (lpni_healthv > best_lpni_healthv) {
1802 best_lpni_healthv = lpni_healthv;
1803 /* if this is a preferred peer use it */
1804 } else if (!preferred && ni_is_pref) {
1806 } else if (preferred && !ni_is_pref) {
1808 * this is not the preferred peer so let's ignore
1812 } else if (lpni->lpni_txcredits < best_lpni_credits) {
1814 * We already have a peer that has more credits
1815 * available than this one. No need to consider
1816 * this peer further.
1819 } else if (lpni->lpni_txcredits == best_lpni_credits) {
1821 * The best peer found so far and the current peer
1822 * have the same number of available credits let's
1823 * make sure to select between them using Round
1827 if (best_lpni->lpni_seq <= lpni->lpni_seq)
1833 best_lpni_credits = lpni->lpni_txcredits;
1836 /* if we still can't find a peer ni then we can't reach it */
1838 __u32 net_id = (peer_net) ? peer_net->lpn_net_id :
1839 LNET_NIDNET(dst_nid);
1840 CDEBUG(D_NET, "no peer_ni found on peer net %s\n",
1841 libcfs_net2str(net_id));
1845 CDEBUG(D_NET, "sd_best_lpni = %s\n",
1846 libcfs_nid2str(best_lpni->lpni_nid));
1852 * Prerequisite: the best_ni should already be set in the sd
1854 static inline struct lnet_peer_ni *
1855 lnet_find_best_lpni_on_net(struct lnet_send_data *sd, struct lnet_peer *peer,
1858 struct lnet_peer_net *peer_net;
1861 * The gateway is Multi-Rail capable so now we must select the
1864 peer_net = lnet_peer_get_net_locked(peer, net_id);
1867 CERROR("gateway peer %s has no NI on net %s\n",
1868 libcfs_nid2str(peer->lp_primary_nid),
1869 libcfs_net2str(net_id));
1873 return lnet_select_peer_ni(sd, peer, peer_net);
1877 lnet_set_non_mr_pref_nid(struct lnet_send_data *sd)
1879 if (sd->sd_send_case & NMR_DST &&
1880 sd->sd_msg->msg_type != LNET_MSG_REPLY &&
1881 sd->sd_msg->msg_type != LNET_MSG_ACK &&
1882 sd->sd_best_lpni->lpni_pref_nnids == 0) {
1883 CDEBUG(D_NET, "Setting preferred local NID %s on NMR peer %s\n",
1884 libcfs_nid2str(sd->sd_best_ni->ni_nid),
1885 libcfs_nid2str(sd->sd_best_lpni->lpni_nid));
1886 lnet_peer_ni_set_non_mr_pref_nid(sd->sd_best_lpni,
1887 sd->sd_best_ni->ni_nid);
1896 * use the source and destination NIDs as the pathway
1899 lnet_handle_spec_local_nmr_dst(struct lnet_send_data *sd)
1901 /* the destination lpni is set before we get here. */
1904 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
1905 if (!sd->sd_best_ni) {
1906 CERROR("Can't send to %s: src %s is not a "
1907 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
1908 libcfs_nid2str(sd->sd_src_nid));
1913 * the preferred NID will only be set for NMR peers
1915 lnet_set_non_mr_pref_nid(sd);
1917 return lnet_handle_send(sd);
1925 * Run the selection algorithm on the peer NIs unless we're sending
1926 * a response, in this case just send to the destination
1929 lnet_handle_spec_local_mr_dst(struct lnet_send_data *sd)
1931 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
1932 if (!sd->sd_best_ni) {
1933 CERROR("Can't send to %s: src %s is not a "
1934 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
1935 libcfs_nid2str(sd->sd_src_nid));
1940 * only run the selection algorithm to pick the peer_ni if we're
1941 * sending a GET or a PUT. Responses are sent to the same
1942 * destination NID provided.
1944 if (!(sd->sd_send_case & SND_RESP)) {
1946 lnet_find_best_lpni_on_net(sd, sd->sd_peer,
1947 sd->sd_best_ni->ni_net->net_id);
1950 if (sd->sd_best_lpni &&
1951 sd->sd_best_lpni->lpni_nid == the_lnet.ln_loni->ni_nid)
1952 return lnet_handle_lo_send(sd);
1953 else if (sd->sd_best_lpni)
1954 return lnet_handle_send(sd);
1956 CERROR("can't send to %s. no NI on %s\n",
1957 libcfs_nid2str(sd->sd_dst_nid),
1958 libcfs_net2str(sd->sd_best_ni->ni_net->net_id));
1960 return -EHOSTUNREACH;
1964 lnet_find_best_ni_on_spec_net(struct lnet_ni *cur_best_ni,
1965 struct lnet_peer *peer,
1966 struct lnet_peer_net *peer_net,
1970 struct lnet_net *local_net;
1971 struct lnet_ni *best_ni;
1973 local_net = lnet_get_net_locked(peer_net->lpn_net_id);
1978 * Iterate through the NIs in this local Net and select
1979 * the NI to send from. The selection is determined by
1980 * these 3 criterion in the following priority:
1982 * 2. NI available credits
1985 best_ni = lnet_get_best_ni(local_net, cur_best_ni,
1986 peer, peer_net, cpt);
1988 if (incr_seq && best_ni)
1995 lnet_handle_find_routed_path(struct lnet_send_data *sd,
1997 struct lnet_peer_ni **gw_lpni,
1998 struct lnet_peer **gw_peer)
2000 struct lnet_peer_ni *gw;
2001 lnet_nid_t src_nid = sd->sd_src_nid;
2003 gw = lnet_find_route_locked(NULL, LNET_NIDNET(dst_nid),
2006 CERROR("no route to %s from %s\n",
2007 libcfs_nid2str(dst_nid), libcfs_nid2str(src_nid));
2008 return -EHOSTUNREACH;
2011 /* get the peer of the gw_ni */
2012 LASSERT(gw->lpni_peer_net);
2013 LASSERT(gw->lpni_peer_net->lpn_peer);
2015 *gw_peer = gw->lpni_peer_net->lpn_peer;
2017 if (!sd->sd_best_ni)
2018 sd->sd_best_ni = lnet_find_best_ni_on_spec_net(NULL, *gw_peer,
2023 if (!sd->sd_best_ni) {
2024 CERROR("Internal Error. Expected local ni on %s "
2025 "but non found :%s\n",
2026 libcfs_net2str(gw->lpni_peer_net->lpn_net_id),
2027 libcfs_nid2str(sd->sd_src_nid));
2032 * if gw is MR let's find its best peer_ni
2034 if (lnet_peer_is_multi_rail(*gw_peer)) {
2035 gw = lnet_find_best_lpni_on_net(sd, *gw_peer,
2036 sd->sd_best_ni->ni_net->net_id);
2038 * We've already verified that the gw has an NI on that
2039 * desired net, but we're not finding it. Something is
2043 CERROR("Internal Error. Route expected to %s from %s\n",
2044 libcfs_nid2str(dst_nid),
2045 libcfs_nid2str(src_nid));
2060 * Remote destination
2061 * Non-MR destination
2065 * Remote destination
2068 * The handling of these two cases is similar. Even though the destination
2069 * can be MR or non-MR, we'll deal directly with the router.
2072 lnet_handle_spec_router_dst(struct lnet_send_data *sd)
2075 struct lnet_peer_ni *gw_lpni = NULL;
2076 struct lnet_peer *gw_peer = NULL;
2079 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
2080 if (!sd->sd_best_ni) {
2081 CERROR("Can't send to %s: src %s is not a "
2082 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
2083 libcfs_nid2str(sd->sd_src_nid));
2087 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2092 if (sd->sd_send_case & NMR_DST)
2094 * since the final destination is non-MR let's set its preferred
2095 * NID before we send
2097 lnet_set_non_mr_pref_nid(sd);
2100 * We're going to send to the gw found so let's set its
2103 sd->sd_peer = gw_peer;
2104 sd->sd_best_lpni = gw_lpni;
2106 return lnet_handle_send(sd);
2110 lnet_find_best_ni_on_local_net(struct lnet_peer *peer, int md_cpt)
2112 struct lnet_peer_net *peer_net = NULL;
2113 struct lnet_ni *best_ni = NULL;
2116 * The peer can have multiple interfaces, some of them can be on
2117 * the local network and others on a routed network. We should
2118 * prefer the local network. However if the local network is not
2119 * available then we need to try the routed network
2122 /* go through all the peer nets and find the best_ni */
2123 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_peer_nets) {
2125 * The peer's list of nets can contain non-local nets. We
2126 * want to only examine the local ones.
2128 if (!lnet_get_net_locked(peer_net->lpn_net_id))
2130 best_ni = lnet_find_best_ni_on_spec_net(best_ni, peer,
2131 peer_net, md_cpt, false);
2135 /* increment sequence number so we can round robin */
2141 static struct lnet_ni *
2142 lnet_find_existing_preferred_best_ni(struct lnet_send_data *sd)
2144 struct lnet_ni *best_ni = NULL;
2145 struct lnet_peer_net *peer_net;
2146 struct lnet_peer *peer = sd->sd_peer;
2147 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
2148 struct lnet_peer_ni *lpni;
2149 int cpt = sd->sd_cpt;
2152 * We must use a consistent source address when sending to a
2153 * non-MR peer. However, a non-MR peer can have multiple NIDs
2154 * on multiple networks, and we may even need to talk to this
2155 * peer on multiple networks -- certain types of
2156 * load-balancing configuration do this.
2158 * So we need to pick the NI the peer prefers for this
2159 * particular network.
2162 /* Get the target peer_ni */
2163 peer_net = lnet_peer_get_net_locked(peer,
2164 LNET_NIDNET(best_lpni->lpni_nid));
2165 LASSERT(peer_net != NULL);
2166 list_for_each_entry(lpni, &peer_net->lpn_peer_nis,
2168 if (lpni->lpni_pref_nnids == 0)
2170 LASSERT(lpni->lpni_pref_nnids == 1);
2171 best_ni = lnet_nid2ni_locked(
2172 lpni->lpni_pref.nid, cpt);
2179 /* Prerequisite: sd->sd_peer and sd->sd_best_lpni should be set */
2181 lnet_select_preferred_best_ni(struct lnet_send_data *sd)
2183 struct lnet_ni *best_ni = NULL;
2184 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
2187 * We must use a consistent source address when sending to a
2188 * non-MR peer. However, a non-MR peer can have multiple NIDs
2189 * on multiple networks, and we may even need to talk to this
2190 * peer on multiple networks -- certain types of
2191 * load-balancing configuration do this.
2193 * So we need to pick the NI the peer prefers for this
2194 * particular network.
2197 best_ni = lnet_find_existing_preferred_best_ni(sd);
2199 /* if best_ni is still not set just pick one */
2202 lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2203 sd->sd_best_lpni->lpni_peer_net,
2204 sd->sd_md_cpt, true);
2205 /* If there is no best_ni we don't have a route */
2207 CERROR("no path to %s from net %s\n",
2208 libcfs_nid2str(best_lpni->lpni_nid),
2209 libcfs_net2str(best_lpni->lpni_net->net_id));
2210 return -EHOSTUNREACH;
2214 sd->sd_best_ni = best_ni;
2216 /* Set preferred NI if necessary. */
2217 lnet_set_non_mr_pref_nid(sd);
2224 * Source not specified
2228 * always use the same source NID for NMR peers
2229 * If we've talked to that peer before then we already have a preferred
2230 * source NI associated with it. Otherwise, we select a preferred local NI
2231 * and store it in the peer
2234 lnet_handle_any_local_nmr_dst(struct lnet_send_data *sd)
2238 /* sd->sd_best_lpni is already set to the final destination */
2241 * At this point we should've created the peer ni and peer. If we
2242 * can't find it, then something went wrong. Instead of assert
2243 * output a relevant message and fail the send
2245 if (!sd->sd_best_lpni) {
2246 CERROR("Internal fault. Unable to send msg %s to %s. "
2248 lnet_msgtyp2str(sd->sd_msg->msg_type),
2249 libcfs_nid2str(sd->sd_dst_nid));
2253 rc = lnet_select_preferred_best_ni(sd);
2255 rc = lnet_handle_send(sd);
2261 lnet_handle_any_mr_dsta(struct lnet_send_data *sd)
2264 * NOTE we've already handled the remote peer case. So we only
2265 * need to worry about the local case here.
2267 * if we're sending a response, ACK or reply, we need to send it
2268 * to the destination NID given to us. At this point we already
2269 * have the peer_ni we're suppose to send to, so just find the
2270 * best_ni on the peer net and use that. Since we're sending to an
2271 * MR peer then we can just run the selection algorithm on our
2272 * local NIs and pick the best one.
2274 if (sd->sd_send_case & SND_RESP) {
2276 lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2277 sd->sd_best_lpni->lpni_peer_net,
2278 sd->sd_md_cpt, true);
2280 if (!sd->sd_best_ni) {
2282 * We're not going to deal with not able to send
2283 * a response to the provided final destination
2285 CERROR("Can't send response to %s. "
2286 "No local NI available\n",
2287 libcfs_nid2str(sd->sd_dst_nid));
2288 return -EHOSTUNREACH;
2291 return lnet_handle_send(sd);
2295 * If we get here that means we're sending a fresh request, PUT or
2296 * GET, so we need to run our standard selection algorithm.
2297 * First find the best local interface that's on any of the peer's
2300 sd->sd_best_ni = lnet_find_best_ni_on_local_net(sd->sd_peer,
2302 if (sd->sd_best_ni) {
2304 lnet_find_best_lpni_on_net(sd, sd->sd_peer,
2305 sd->sd_best_ni->ni_net->net_id);
2308 * if we're successful in selecting a peer_ni on the local
2309 * network, then send to it. Otherwise fall through and
2310 * try and see if we can reach it over another routed
2313 if (sd->sd_best_lpni &&
2314 sd->sd_best_lpni->lpni_nid == the_lnet.ln_loni->ni_nid) {
2316 * in case we initially started with a routed
2317 * destination, let's reset to local
2319 sd->sd_send_case &= ~REMOTE_DST;
2320 sd->sd_send_case |= LOCAL_DST;
2321 return lnet_handle_lo_send(sd);
2322 } else if (sd->sd_best_lpni) {
2324 * in case we initially started with a routed
2325 * destination, let's reset to local
2327 sd->sd_send_case &= ~REMOTE_DST;
2328 sd->sd_send_case |= LOCAL_DST;
2329 return lnet_handle_send(sd);
2332 CERROR("Internal Error. Expected to have a best_lpni: "
2334 libcfs_nid2str(sd->sd_src_nid),
2335 libcfs_nid2str(sd->sd_dst_nid));
2341 * Peer doesn't have a local network. Let's see if there is
2342 * a remote network we can reach it on.
2344 return PASS_THROUGH;
2349 * Source NID not specified
2354 * Source NID not speified
2355 * Remote destination
2358 * In both of these cases if we're sending a response, ACK or REPLY, then
2359 * we need to send to the destination NID provided.
2361 * In the remote case let's deal with MR routers.
2366 lnet_handle_any_mr_dst(struct lnet_send_data *sd)
2369 struct lnet_peer *gw_peer = NULL;
2370 struct lnet_peer_ni *gw_lpni = NULL;
2373 * handle sending a response to a remote peer here so we don't
2374 * have to worry about it if we hit lnet_handle_any_mr_dsta()
2376 if (sd->sd_send_case & REMOTE_DST &&
2377 sd->sd_send_case & SND_RESP) {
2378 struct lnet_peer_ni *gw;
2379 struct lnet_peer *gw_peer;
2381 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw,
2384 CERROR("Can't send response to %s. "
2385 "No route available\n",
2386 libcfs_nid2str(sd->sd_dst_nid));
2387 return -EHOSTUNREACH;
2390 sd->sd_best_lpni = gw;
2391 sd->sd_peer = gw_peer;
2393 return lnet_handle_send(sd);
2397 * Even though the NID for the peer might not be on a local network,
2398 * since the peer is MR there could be other interfaces on the
2399 * local network. In that case we'd still like to prefer the local
2400 * network over the routed network. If we're unable to do that
2401 * then we select the best router among the different routed networks,
2402 * and if the router is MR then we can deal with it as such.
2404 rc = lnet_handle_any_mr_dsta(sd);
2405 if (rc != PASS_THROUGH)
2409 * TODO; One possible enhancement is to run the selection
2410 * algorithm on the peer. However for remote peers the credits are
2411 * not decremented, so we'll be basically going over the peer NIs
2412 * in round robin. An MR router will run the selection algorithm
2413 * on the next-hop interfaces.
2415 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2420 sd->sd_send_case &= ~LOCAL_DST;
2421 sd->sd_send_case |= REMOTE_DST;
2423 sd->sd_peer = gw_peer;
2424 sd->sd_best_lpni = gw_lpni;
2426 return lnet_handle_send(sd);
2430 * Source not specified
2431 * Remote destination
2434 * Must send to the specified peer NID using the same source NID that
2435 * we've used before. If it's the first time to talk to that peer then
2436 * find the source NI and assign it as preferred to that peer
2439 lnet_handle_any_router_nmr_dst(struct lnet_send_data *sd)
2442 struct lnet_peer_ni *gw_lpni = NULL;
2443 struct lnet_peer *gw_peer = NULL;
2446 * Let's set if we have a preferred NI to talk to this NMR peer
2448 sd->sd_best_ni = lnet_find_existing_preferred_best_ni(sd);
2451 * find the router and that'll find the best NI if we didn't find
2454 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2460 * set the best_ni we've chosen as the preferred one for
2463 lnet_set_non_mr_pref_nid(sd);
2465 /* we'll be sending to the gw */
2466 sd->sd_best_lpni = gw_lpni;
2467 sd->sd_peer = gw_peer;
2469 return lnet_handle_send(sd);
2473 lnet_handle_send_case_locked(struct lnet_send_data *sd)
2476 * turn off the SND_RESP bit.
2477 * It will be checked in the case handling
2479 __u32 send_case = sd->sd_send_case &= ~SND_RESP ;
2481 CDEBUG(D_NET, "Source %s%s to %s %s %s destination\n",
2482 (send_case & SRC_SPEC) ? "Specified: " : "ANY",
2483 (send_case & SRC_SPEC) ? libcfs_nid2str(sd->sd_src_nid) : "",
2484 (send_case & MR_DST) ? "MR: " : "NMR: ",
2485 libcfs_nid2str(sd->sd_dst_nid),
2486 (send_case & LOCAL_DST) ? "local" : "routed");
2488 switch (send_case) {
2490 * For all cases where the source is specified, we should always
2491 * use the destination NID, whether it's an MR destination or not,
2492 * since we're continuing a series of related messages for the
2495 case SRC_SPEC_LOCAL_NMR_DST:
2496 return lnet_handle_spec_local_nmr_dst(sd);
2497 case SRC_SPEC_LOCAL_MR_DST:
2498 return lnet_handle_spec_local_mr_dst(sd);
2499 case SRC_SPEC_ROUTER_NMR_DST:
2500 case SRC_SPEC_ROUTER_MR_DST:
2501 return lnet_handle_spec_router_dst(sd);
2502 case SRC_ANY_LOCAL_NMR_DST:
2503 return lnet_handle_any_local_nmr_dst(sd);
2504 case SRC_ANY_LOCAL_MR_DST:
2505 case SRC_ANY_ROUTER_MR_DST:
2506 return lnet_handle_any_mr_dst(sd);
2507 case SRC_ANY_ROUTER_NMR_DST:
2508 return lnet_handle_any_router_nmr_dst(sd);
2510 CERROR("Unknown send case\n");
2516 lnet_select_pathway(lnet_nid_t src_nid, lnet_nid_t dst_nid,
2517 struct lnet_msg *msg, lnet_nid_t rtr_nid)
2519 struct lnet_peer_ni *lpni;
2520 struct lnet_peer *peer;
2521 struct lnet_send_data send_data;
2524 __u32 send_case = 0;
2526 memset(&send_data, 0, sizeof(send_data));
2529 * get an initial CPT to use for locking. The idea here is not to
2530 * serialize the calls to select_pathway, so that as many
2531 * operations can run concurrently as possible. To do that we use
2532 * the CPT where this call is being executed. Later on when we
2533 * determine the CPT to use in lnet_message_commit, we switch the
2534 * lock and check if there was any configuration change. If none,
2535 * then we proceed, if there is, then we restart the operation.
2537 cpt = lnet_net_lock_current();
2539 md_cpt = lnet_cpt_of_md(msg->msg_md, msg->msg_offset);
2540 if (md_cpt == CFS_CPT_ANY)
2546 * If we're being asked to send to the loopback interface, there
2547 * is no need to go through any selection. We can just shortcut
2548 * the entire process and send over lolnd
2550 send_data.sd_msg = msg;
2551 send_data.sd_cpt = cpt;
2552 if (LNET_NETTYP(LNET_NIDNET(dst_nid)) == LOLND) {
2553 rc = lnet_handle_lo_send(&send_data);
2554 lnet_net_unlock(cpt);
2559 * find an existing peer_ni, or create one and mark it as having been
2560 * created due to network traffic. This call will create the
2561 * peer->peer_net->peer_ni tree.
2563 lpni = lnet_nid2peerni_locked(dst_nid, LNET_NID_ANY, cpt);
2565 lnet_net_unlock(cpt);
2566 return PTR_ERR(lpni);
2570 * Cache the original src_nid. If we need to resend the message
2571 * then we'll need to know whether the src_nid was originally
2572 * specified for this message. If it was originally specified,
2573 * then we need to keep using the same src_nid since it's
2574 * continuing the same sequence of messages.
2576 msg->msg_src_nid_param = src_nid;
2579 * Now that we have a peer_ni, check if we want to discover
2580 * the peer. Traffic to the LNET_RESERVED_PORTAL should not
2581 * trigger discovery.
2583 peer = lpni->lpni_peer_net->lpn_peer;
2584 if (lnet_msg_discovery(msg) && !lnet_peer_is_uptodate(peer)) {
2585 lnet_nid_t primary_nid;
2586 rc = lnet_discover_peer_locked(lpni, cpt, false);
2588 lnet_peer_ni_decref_locked(lpni);
2589 lnet_net_unlock(cpt);
2592 /* The peer may have changed. */
2593 peer = lpni->lpni_peer_net->lpn_peer;
2594 /* queue message and return */
2595 msg->msg_rtr_nid_param = rtr_nid;
2596 msg->msg_sending = 0;
2597 spin_lock(&peer->lp_lock);
2598 list_add_tail(&msg->msg_list, &peer->lp_dc_pendq);
2599 spin_unlock(&peer->lp_lock);
2600 lnet_peer_ni_decref_locked(lpni);
2601 primary_nid = peer->lp_primary_nid;
2602 lnet_net_unlock(cpt);
2604 CDEBUG(D_NET, "%s pending discovery\n",
2605 libcfs_nid2str(primary_nid));
2607 return LNET_DC_WAIT;
2609 lnet_peer_ni_decref_locked(lpni);
2612 * Identify the different send cases
2614 if (src_nid == LNET_NID_ANY)
2615 send_case |= SRC_ANY;
2617 send_case |= SRC_SPEC;
2619 if (lnet_get_net_locked(LNET_NIDNET(dst_nid)))
2620 send_case |= LOCAL_DST;
2622 send_case |= REMOTE_DST;
2625 * if this is a non-MR peer or if we're recovering a peer ni then
2626 * let's consider this an NMR case so we can hit the destination
2629 if (!lnet_peer_is_multi_rail(peer) || msg->msg_recovery)
2630 send_case |= NMR_DST;
2632 send_case |= MR_DST;
2634 if (msg->msg_type == LNET_MSG_REPLY ||
2635 msg->msg_type == LNET_MSG_ACK)
2636 send_case |= SND_RESP;
2638 /* assign parameters to the send_data */
2639 send_data.sd_rtr_nid = rtr_nid;
2640 send_data.sd_src_nid = src_nid;
2641 send_data.sd_dst_nid = dst_nid;
2642 send_data.sd_best_lpni = lpni;
2644 * keep a pointer to the final destination in case we're going to
2645 * route, so we'll need to access it later
2647 send_data.sd_final_dst_lpni = lpni;
2648 send_data.sd_peer = peer;
2649 send_data.sd_md_cpt = md_cpt;
2650 send_data.sd_send_case = send_case;
2652 rc = lnet_handle_send_case_locked(&send_data);
2655 * Update the local cpt since send_data.sd_cpt might've been
2656 * updated as a result of calling lnet_handle_send_case_locked().
2658 cpt = send_data.sd_cpt;
2660 if (rc == REPEAT_SEND)
2663 lnet_net_unlock(cpt);
2669 lnet_send(lnet_nid_t src_nid, struct lnet_msg *msg, lnet_nid_t rtr_nid)
2671 lnet_nid_t dst_nid = msg->msg_target.nid;
2675 * NB: rtr_nid is set to LNET_NID_ANY for all current use-cases,
2676 * but we might want to use pre-determined router for ACK/REPLY
2679 /* NB: ni != NULL == interface pre-determined (ACK/REPLY) */
2680 LASSERT(msg->msg_txpeer == NULL);
2681 LASSERT(msg->msg_txni == NULL);
2682 LASSERT(!msg->msg_sending);
2683 LASSERT(!msg->msg_target_is_router);
2684 LASSERT(!msg->msg_receiving);
2686 msg->msg_sending = 1;
2688 LASSERT(!msg->msg_tx_committed);
2690 rc = lnet_select_pathway(src_nid, dst_nid, msg, rtr_nid);
2694 if (rc == LNET_CREDIT_OK)
2695 lnet_ni_send(msg->msg_txni, msg);
2697 /* rc == LNET_CREDIT_OK or LNET_CREDIT_WAIT or LNET_DC_WAIT */
2701 enum lnet_mt_event_type {
2702 MT_TYPE_LOCAL_NI = 0,
2706 struct lnet_mt_event_info {
2707 enum lnet_mt_event_type mt_type;
2711 /* called with res_lock held */
2713 lnet_detach_rsp_tracker(struct lnet_libmd *md, int cpt)
2715 struct lnet_rsp_tracker *rspt;
2718 * msg has a refcount on the MD so the MD is not going away.
2719 * The rspt queue for the cpt is protected by
2720 * the lnet_net_lock(cpt). cpt is the cpt of the MD cookie.
2722 if (!md->md_rspt_ptr)
2725 rspt = md->md_rspt_ptr;
2726 md->md_rspt_ptr = NULL;
2729 LASSERT(rspt->rspt_cpt == cpt);
2732 * invalidate the handle to indicate that a response has been
2733 * received, which will then lead the monitor thread to clean up
2736 LNetInvalidateMDHandle(&rspt->rspt_mdh);
2740 lnet_finalize_expired_responses(bool force)
2742 struct lnet_libmd *md;
2743 struct list_head local_queue;
2744 struct lnet_rsp_tracker *rspt, *tmp;
2747 if (the_lnet.ln_mt_rstq == NULL)
2750 cfs_cpt_for_each(i, lnet_cpt_table()) {
2751 INIT_LIST_HEAD(&local_queue);
2754 if (!the_lnet.ln_mt_rstq[i]) {
2758 list_splice_init(the_lnet.ln_mt_rstq[i], &local_queue);
2761 list_for_each_entry_safe(rspt, tmp, &local_queue, rspt_on_list) {
2763 * The rspt mdh will be invalidated when a response
2764 * is received or whenever we want to discard the
2765 * block the monitor thread will walk the queue
2766 * and clean up any rsts with an invalid mdh.
2767 * The monitor thread will walk the queue until
2768 * the first unexpired rspt block. This means that
2769 * some rspt blocks which received their
2770 * corresponding responses will linger in the
2771 * queue until they are cleaned up eventually.
2774 if (LNetMDHandleIsInvalid(rspt->rspt_mdh)) {
2776 list_del_init(&rspt->rspt_on_list);
2777 lnet_rspt_free(rspt, i);
2781 if (ktime_compare(ktime_get(), rspt->rspt_deadline) >= 0 ||
2783 struct lnet_peer_ni *lpni;
2786 md = lnet_handle2md(&rspt->rspt_mdh);
2788 LNetInvalidateMDHandle(&rspt->rspt_mdh);
2790 list_del_init(&rspt->rspt_on_list);
2791 lnet_rspt_free(rspt, i);
2794 LASSERT(md->md_rspt_ptr == rspt);
2795 md->md_rspt_ptr = NULL;
2799 the_lnet.ln_counters[i]->lct_health.lch_response_timeout_count++;
2802 list_del_init(&rspt->rspt_on_list);
2804 nid = rspt->rspt_next_hop_nid;
2806 CNETERR("Response timed out: md = %p: nid = %s\n",
2807 md, libcfs_nid2str(nid));
2808 LNetMDUnlink(rspt->rspt_mdh);
2809 lnet_rspt_free(rspt, i);
2812 * If there is a timeout on the response
2813 * from the next hop decrement its health
2814 * value so that we don't use it
2817 lpni = lnet_find_peer_ni_locked(nid);
2819 lnet_handle_remote_failure_locked(lpni);
2820 lnet_peer_ni_decref_locked(lpni);
2830 if (!list_empty(&local_queue))
2831 list_splice(&local_queue, the_lnet.ln_mt_rstq[i]);
2837 lnet_resend_pending_msgs_locked(struct list_head *resendq, int cpt)
2839 struct lnet_msg *msg;
2841 while (!list_empty(resendq)) {
2842 struct lnet_peer_ni *lpni;
2844 msg = list_entry(resendq->next, struct lnet_msg,
2847 list_del_init(&msg->msg_list);
2849 lpni = lnet_find_peer_ni_locked(msg->msg_hdr.dest_nid);
2851 lnet_net_unlock(cpt);
2852 CERROR("Expected that a peer is already created for %s\n",
2853 libcfs_nid2str(msg->msg_hdr.dest_nid));
2854 msg->msg_no_resend = true;
2855 lnet_finalize(msg, -EFAULT);
2858 struct lnet_peer *peer;
2860 lnet_nid_t src_nid = LNET_NID_ANY;
2863 * if this message is not being routed and the
2864 * peer is non-MR then we must use the same
2865 * src_nid that was used in the original send.
2866 * Otherwise if we're routing the message (IE
2867 * we're a router) then we can use any of our
2868 * local interfaces. It doesn't matter to the
2869 * final destination.
2871 peer = lpni->lpni_peer_net->lpn_peer;
2872 if (!msg->msg_routing &&
2873 !lnet_peer_is_multi_rail(peer))
2874 src_nid = le64_to_cpu(msg->msg_hdr.src_nid);
2877 * If we originally specified a src NID, then we
2878 * must attempt to reuse it in the resend as well.
2880 if (msg->msg_src_nid_param != LNET_NID_ANY)
2881 src_nid = msg->msg_src_nid_param;
2882 lnet_peer_ni_decref_locked(lpni);
2884 lnet_net_unlock(cpt);
2885 CDEBUG(D_NET, "resending %s->%s: %s recovery %d try# %d\n",
2886 libcfs_nid2str(src_nid),
2887 libcfs_id2str(msg->msg_target),
2888 lnet_msgtyp2str(msg->msg_type),
2890 msg->msg_retry_count);
2891 rc = lnet_send(src_nid, msg, LNET_NID_ANY);
2893 CERROR("Error sending %s to %s: %d\n",
2894 lnet_msgtyp2str(msg->msg_type),
2895 libcfs_id2str(msg->msg_target), rc);
2896 msg->msg_no_resend = true;
2897 lnet_finalize(msg, rc);
2901 the_lnet.ln_counters[cpt]->lct_health.lch_resend_count++;
2907 lnet_resend_pending_msgs(void)
2911 cfs_cpt_for_each(i, lnet_cpt_table()) {
2913 lnet_resend_pending_msgs_locked(the_lnet.ln_mt_resendqs[i], i);
2918 /* called with cpt and ni_lock held */
2920 lnet_unlink_ni_recovery_mdh_locked(struct lnet_ni *ni, int cpt, bool force)
2922 struct lnet_handle_md recovery_mdh;
2924 LNetInvalidateMDHandle(&recovery_mdh);
2926 if (ni->ni_recovery_state & LNET_NI_RECOVERY_PENDING ||
2928 recovery_mdh = ni->ni_ping_mdh;
2929 LNetInvalidateMDHandle(&ni->ni_ping_mdh);
2932 lnet_net_unlock(cpt);
2933 if (!LNetMDHandleIsInvalid(recovery_mdh))
2934 LNetMDUnlink(recovery_mdh);
2940 lnet_recover_local_nis(void)
2942 struct lnet_mt_event_info *ev_info;
2943 struct list_head processed_list;
2944 struct list_head local_queue;
2945 struct lnet_handle_md mdh;
2946 struct lnet_ni *tmp;
2952 INIT_LIST_HEAD(&local_queue);
2953 INIT_LIST_HEAD(&processed_list);
2956 * splice the recovery queue on a local queue. We will iterate
2957 * through the local queue and update it as needed. Once we're
2958 * done with the traversal, we'll splice the local queue back on
2959 * the head of the ln_mt_localNIRecovq. Any newly added local NIs
2960 * will be traversed in the next iteration.
2963 list_splice_init(&the_lnet.ln_mt_localNIRecovq,
2967 list_for_each_entry_safe(ni, tmp, &local_queue, ni_recovery) {
2969 * if an NI is being deleted or it is now healthy, there
2970 * is no need to keep it around in the recovery queue.
2971 * The monitor thread is the only thread responsible for
2972 * removing the NI from the recovery queue.
2973 * Multiple threads can be adding NIs to the recovery
2976 healthv = atomic_read(&ni->ni_healthv);
2980 if (ni->ni_state != LNET_NI_STATE_ACTIVE ||
2981 healthv == LNET_MAX_HEALTH_VALUE) {
2982 list_del_init(&ni->ni_recovery);
2983 lnet_unlink_ni_recovery_mdh_locked(ni, 0, false);
2985 lnet_ni_decref_locked(ni, 0);
2991 * if the local NI failed recovery we must unlink the md.
2992 * But we want to keep the local_ni on the recovery queue
2993 * so we can continue the attempts to recover it.
2995 if (ni->ni_recovery_state & LNET_NI_RECOVERY_FAILED) {
2996 lnet_unlink_ni_recovery_mdh_locked(ni, 0, true);
2997 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_FAILED;
3004 CDEBUG(D_NET, "attempting to recover local ni: %s\n",
3005 libcfs_nid2str(ni->ni_nid));
3008 if (!(ni->ni_recovery_state & LNET_NI_RECOVERY_PENDING)) {
3009 ni->ni_recovery_state |= LNET_NI_RECOVERY_PENDING;
3012 LIBCFS_ALLOC(ev_info, sizeof(*ev_info));
3014 CERROR("out of memory. Can't recover %s\n",
3015 libcfs_nid2str(ni->ni_nid));
3017 ni->ni_recovery_state &=
3018 ~LNET_NI_RECOVERY_PENDING;
3023 mdh = ni->ni_ping_mdh;
3025 * Invalidate the ni mdh in case it's deleted.
3026 * We'll unlink the mdh in this case below.
3028 LNetInvalidateMDHandle(&ni->ni_ping_mdh);
3032 * remove the NI from the local queue and drop the
3033 * reference count to it while we're recovering
3034 * it. The reason for that, is that the NI could
3035 * be deleted, and the way the code is structured
3036 * is if we don't drop the NI, then the deletion
3037 * code will enter a loop waiting for the
3038 * reference count to be removed while holding the
3039 * ln_mutex_lock(). When we look up the peer to
3040 * send to in lnet_select_pathway() we will try to
3041 * lock the ln_mutex_lock() as well, leading to
3042 * a deadlock. By dropping the refcount and
3043 * removing it from the list, we allow for the NI
3044 * to be removed, then we use the cached NID to
3045 * look it up again. If it's gone, then we just
3046 * continue examining the rest of the queue.
3049 list_del_init(&ni->ni_recovery);
3050 lnet_ni_decref_locked(ni, 0);
3053 ev_info->mt_type = MT_TYPE_LOCAL_NI;
3054 ev_info->mt_nid = nid;
3055 rc = lnet_send_ping(nid, &mdh, LNET_INTERFACES_MIN,
3056 ev_info, the_lnet.ln_mt_eqh, true);
3057 /* lookup the nid again */
3059 ni = lnet_nid2ni_locked(nid, 0);
3062 * the NI has been deleted when we dropped
3070 * Same note as in lnet_recover_peer_nis(). When
3071 * we're sending the ping, the NI is free to be
3072 * deleted or manipulated. By this point it
3073 * could've been added back on the recovery queue,
3074 * and a refcount taken on it.
3075 * So we can't just add it blindly again or we'll
3076 * corrupt the queue. We must check under lock if
3077 * it's not on any list and if not then add it
3078 * to the processed list, which will eventually be
3079 * spliced back on to the recovery queue.
3081 ni->ni_ping_mdh = mdh;
3082 if (list_empty(&ni->ni_recovery)) {
3083 list_add_tail(&ni->ni_recovery, &processed_list);
3084 lnet_ni_addref_locked(ni, 0);
3090 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_PENDING;
3096 * put back the remaining NIs on the ln_mt_localNIRecovq to be
3097 * reexamined in the next iteration.
3099 list_splice_init(&processed_list, &local_queue);
3101 list_splice(&local_queue, &the_lnet.ln_mt_localNIRecovq);
3105 static struct list_head **
3106 lnet_create_array_of_queues(void)
3108 struct list_head **qs;
3109 struct list_head *q;
3112 qs = cfs_percpt_alloc(lnet_cpt_table(),
3113 sizeof(struct list_head));
3115 CERROR("Failed to allocate queues\n");
3119 cfs_percpt_for_each(q, i, qs)
3126 lnet_resendqs_create(void)
3128 struct list_head **resendqs;
3129 resendqs = lnet_create_array_of_queues();
3134 lnet_net_lock(LNET_LOCK_EX);
3135 the_lnet.ln_mt_resendqs = resendqs;
3136 lnet_net_unlock(LNET_LOCK_EX);
3142 lnet_clean_local_ni_recoveryq(void)
3146 /* This is only called when the monitor thread has stopped */
3149 while (!list_empty(&the_lnet.ln_mt_localNIRecovq)) {
3150 ni = list_entry(the_lnet.ln_mt_localNIRecovq.next,
3151 struct lnet_ni, ni_recovery);
3152 list_del_init(&ni->ni_recovery);
3154 lnet_unlink_ni_recovery_mdh_locked(ni, 0, true);
3156 lnet_ni_decref_locked(ni, 0);
3163 lnet_unlink_lpni_recovery_mdh_locked(struct lnet_peer_ni *lpni, int cpt,
3166 struct lnet_handle_md recovery_mdh;
3168 LNetInvalidateMDHandle(&recovery_mdh);
3170 if (lpni->lpni_state & LNET_PEER_NI_RECOVERY_PENDING || force) {
3171 recovery_mdh = lpni->lpni_recovery_ping_mdh;
3172 LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
3174 spin_unlock(&lpni->lpni_lock);
3175 lnet_net_unlock(cpt);
3176 if (!LNetMDHandleIsInvalid(recovery_mdh))
3177 LNetMDUnlink(recovery_mdh);
3179 spin_lock(&lpni->lpni_lock);
3183 lnet_clean_peer_ni_recoveryq(void)
3185 struct lnet_peer_ni *lpni, *tmp;
3187 lnet_net_lock(LNET_LOCK_EX);
3189 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_mt_peerNIRecovq,
3191 list_del_init(&lpni->lpni_recovery);
3192 spin_lock(&lpni->lpni_lock);
3193 lnet_unlink_lpni_recovery_mdh_locked(lpni, LNET_LOCK_EX, true);
3194 spin_unlock(&lpni->lpni_lock);
3195 lnet_peer_ni_decref_locked(lpni);
3198 lnet_net_unlock(LNET_LOCK_EX);
3202 lnet_clean_resendqs(void)
3204 struct lnet_msg *msg, *tmp;
3205 struct list_head msgs;
3208 INIT_LIST_HEAD(&msgs);
3210 cfs_cpt_for_each(i, lnet_cpt_table()) {
3212 list_splice_init(the_lnet.ln_mt_resendqs[i], &msgs);
3214 list_for_each_entry_safe(msg, tmp, &msgs, msg_list) {
3215 list_del_init(&msg->msg_list);
3216 msg->msg_no_resend = true;
3217 lnet_finalize(msg, -ESHUTDOWN);
3221 cfs_percpt_free(the_lnet.ln_mt_resendqs);
3225 lnet_recover_peer_nis(void)
3227 struct lnet_mt_event_info *ev_info;
3228 struct list_head processed_list;
3229 struct list_head local_queue;
3230 struct lnet_handle_md mdh;
3231 struct lnet_peer_ni *lpni;
3232 struct lnet_peer_ni *tmp;
3237 INIT_LIST_HEAD(&local_queue);
3238 INIT_LIST_HEAD(&processed_list);
3241 * Always use cpt 0 for locking across all interactions with
3242 * ln_mt_peerNIRecovq
3245 list_splice_init(&the_lnet.ln_mt_peerNIRecovq,
3249 list_for_each_entry_safe(lpni, tmp, &local_queue,
3252 * The same protection strategy is used here as is in the
3253 * local recovery case.
3256 healthv = atomic_read(&lpni->lpni_healthv);
3257 spin_lock(&lpni->lpni_lock);
3258 if (lpni->lpni_state & LNET_PEER_NI_DELETING ||
3259 healthv == LNET_MAX_HEALTH_VALUE) {
3260 list_del_init(&lpni->lpni_recovery);
3261 lnet_unlink_lpni_recovery_mdh_locked(lpni, 0, false);
3262 spin_unlock(&lpni->lpni_lock);
3263 lnet_peer_ni_decref_locked(lpni);
3269 * If the peer NI has failed recovery we must unlink the
3270 * md. But we want to keep the peer ni on the recovery
3271 * queue so we can try to continue recovering it
3273 if (lpni->lpni_state & LNET_PEER_NI_RECOVERY_FAILED) {
3274 lnet_unlink_lpni_recovery_mdh_locked(lpni, 0, true);
3275 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_FAILED;
3278 spin_unlock(&lpni->lpni_lock);
3282 * NOTE: we're racing with peer deletion from user space.
3283 * It's possible that a peer is deleted after we check its
3284 * state. In this case the recovery can create a new peer
3286 spin_lock(&lpni->lpni_lock);
3287 if (!(lpni->lpni_state & LNET_PEER_NI_RECOVERY_PENDING) &&
3288 !(lpni->lpni_state & LNET_PEER_NI_DELETING)) {
3289 lpni->lpni_state |= LNET_PEER_NI_RECOVERY_PENDING;
3290 spin_unlock(&lpni->lpni_lock);
3292 LIBCFS_ALLOC(ev_info, sizeof(*ev_info));
3294 CERROR("out of memory. Can't recover %s\n",
3295 libcfs_nid2str(lpni->lpni_nid));
3296 spin_lock(&lpni->lpni_lock);
3297 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3298 spin_unlock(&lpni->lpni_lock);
3302 /* look at the comments in lnet_recover_local_nis() */
3303 mdh = lpni->lpni_recovery_ping_mdh;
3304 LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
3305 nid = lpni->lpni_nid;
3307 list_del_init(&lpni->lpni_recovery);
3308 lnet_peer_ni_decref_locked(lpni);
3311 ev_info->mt_type = MT_TYPE_PEER_NI;
3312 ev_info->mt_nid = nid;
3313 rc = lnet_send_ping(nid, &mdh, LNET_INTERFACES_MIN,
3314 ev_info, the_lnet.ln_mt_eqh, true);
3317 * lnet_find_peer_ni_locked() grabs a refcount for
3318 * us. No need to take it explicitly.
3320 lpni = lnet_find_peer_ni_locked(nid);
3327 lpni->lpni_recovery_ping_mdh = mdh;
3329 * While we're unlocked the lpni could've been
3330 * readded on the recovery queue. In this case we
3331 * don't need to add it to the local queue, since
3332 * it's already on there and the thread that added
3333 * it would've incremented the refcount on the
3334 * peer, which means we need to decref the refcount
3335 * that was implicitly grabbed by find_peer_ni_locked.
3336 * Otherwise, if the lpni is still not on
3337 * the recovery queue, then we'll add it to the
3340 if (list_empty(&lpni->lpni_recovery))
3341 list_add_tail(&lpni->lpni_recovery, &processed_list);
3343 lnet_peer_ni_decref_locked(lpni);
3346 spin_lock(&lpni->lpni_lock);
3348 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3350 spin_unlock(&lpni->lpni_lock);
3353 list_splice_init(&processed_list, &local_queue);
3355 list_splice(&local_queue, &the_lnet.ln_mt_peerNIRecovq);
3360 lnet_monitor_thread(void *arg)
3362 time64_t recovery_timeout = 0;
3363 time64_t rsp_timeout = 0;
3368 * The monitor thread takes care of the following:
3369 * 1. Checks the aliveness of routers
3370 * 2. Checks if there are messages on the resend queue to resend
3372 * 3. Check if there are any NIs on the local recovery queue and
3374 * 4. Checks if there are any NIs on the remote recovery queue
3377 cfs_block_allsigs();
3379 while (the_lnet.ln_mt_state == LNET_MT_STATE_RUNNING) {
3380 now = ktime_get_real_seconds();
3382 if (lnet_router_checker_active())
3383 lnet_check_routers();
3385 lnet_resend_pending_msgs();
3387 if (now >= rsp_timeout) {
3388 lnet_finalize_expired_responses(false);
3389 rsp_timeout = now + (lnet_transaction_timeout / 2);
3392 if (now >= recovery_timeout) {
3393 lnet_recover_local_nis();
3394 lnet_recover_peer_nis();
3395 recovery_timeout = now + lnet_recovery_interval;
3399 * TODO do we need to check if we should sleep without
3400 * timeout? Technically, an active system will always
3401 * have messages in flight so this check will always
3402 * evaluate to false. And on an idle system do we care
3403 * if we wake up every 1 second? Although, we've seen
3404 * cases where we get a complaint that an idle thread
3405 * is waking up unnecessarily.
3407 interval = min(lnet_recovery_interval,
3408 lnet_transaction_timeout / 2);
3409 wait_event_interruptible_timeout(the_lnet.ln_mt_waitq,
3411 cfs_time_seconds(interval));
3414 /* clean up the router checker */
3415 lnet_prune_rc_data(1);
3418 lnet_net_lock(LNET_LOCK_EX);
3419 the_lnet.ln_mt_state = LNET_MT_STATE_SHUTDOWN;
3420 lnet_net_unlock(LNET_LOCK_EX);
3422 /* signal that the monitor thread is exiting */
3423 up(&the_lnet.ln_mt_signal);
3431 * Returns == 0 if success
3432 * Returns > 0 if LNetMDBind or prior fails
3433 * Returns < 0 if LNetGet fails
3436 lnet_send_ping(lnet_nid_t dest_nid,
3437 struct lnet_handle_md *mdh, int nnis,
3438 void *user_data, struct lnet_handle_eq eqh, bool recovery)
3440 struct lnet_md md = { NULL };
3441 struct lnet_process_id id;
3442 struct lnet_ping_buffer *pbuf;
3445 if (dest_nid == LNET_NID_ANY) {
3450 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
3456 /* initialize md content */
3457 md.start = &pbuf->pb_info;
3458 md.length = LNET_PING_INFO_SIZE(nnis);
3459 md.threshold = 2; /* GET/REPLY */
3461 md.options = LNET_MD_TRUNCATE;
3462 md.user_ptr = user_data;
3465 rc = LNetMDBind(md, LNET_UNLINK, mdh);
3467 lnet_ping_buffer_decref(pbuf);
3468 CERROR("Can't bind MD: %d\n", rc);
3469 rc = -rc; /* change the rc to positive */
3472 id.pid = LNET_PID_LUSTRE;
3475 rc = LNetGet(LNET_NID_ANY, *mdh, id,
3476 LNET_RESERVED_PORTAL,
3477 LNET_PROTO_PING_MATCHBITS, 0, recovery);
3480 goto fail_unlink_md;
3486 LNetInvalidateMDHandle(mdh);
3492 lnet_handle_recovery_reply(struct lnet_mt_event_info *ev_info,
3493 int status, bool unlink_event)
3495 lnet_nid_t nid = ev_info->mt_nid;
3497 if (ev_info->mt_type == MT_TYPE_LOCAL_NI) {
3501 ni = lnet_nid2ni_locked(nid, 0);
3507 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_PENDING;
3509 ni->ni_recovery_state |= LNET_NI_RECOVERY_FAILED;
3514 CERROR("local NI (%s) recovery failed with %d\n",
3515 libcfs_nid2str(nid), status);
3519 * need to increment healthv for the ni here, because in
3520 * the lnet_finalize() path we don't have access to this
3521 * NI. And in order to get access to it, we'll need to
3522 * carry forward too much information.
3523 * In the peer case, it'll naturally be incremented
3526 lnet_inc_healthv(&ni->ni_healthv);
3528 struct lnet_peer_ni *lpni;
3531 cpt = lnet_net_lock_current();
3532 lpni = lnet_find_peer_ni_locked(nid);
3534 lnet_net_unlock(cpt);
3537 spin_lock(&lpni->lpni_lock);
3538 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3540 lpni->lpni_state |= LNET_PEER_NI_RECOVERY_FAILED;
3541 spin_unlock(&lpni->lpni_lock);
3542 lnet_peer_ni_decref_locked(lpni);
3543 lnet_net_unlock(cpt);
3546 CERROR("peer NI (%s) recovery failed with %d\n",
3547 libcfs_nid2str(nid), status);
3552 lnet_mt_event_handler(struct lnet_event *event)
3554 struct lnet_mt_event_info *ev_info = event->md.user_ptr;
3555 struct lnet_ping_buffer *pbuf;
3557 /* TODO: remove assert */
3558 LASSERT(event->type == LNET_EVENT_REPLY ||
3559 event->type == LNET_EVENT_SEND ||
3560 event->type == LNET_EVENT_UNLINK);
3562 CDEBUG(D_NET, "Received event: %d status: %d\n", event->type,
3565 switch (event->type) {
3566 case LNET_EVENT_UNLINK:
3567 CDEBUG(D_NET, "%s recovery ping unlinked\n",
3568 libcfs_nid2str(ev_info->mt_nid));
3569 case LNET_EVENT_REPLY:
3570 lnet_handle_recovery_reply(ev_info, event->status,
3571 event->type == LNET_EVENT_UNLINK);
3573 case LNET_EVENT_SEND:
3574 CDEBUG(D_NET, "%s recovery message sent %s:%d\n",
3575 libcfs_nid2str(ev_info->mt_nid),
3576 (event->status) ? "unsuccessfully" :
3577 "successfully", event->status);
3580 CERROR("Unexpected event: %d\n", event->type);
3583 if (event->unlinked) {
3584 LIBCFS_FREE(ev_info, sizeof(*ev_info));
3585 pbuf = LNET_PING_INFO_TO_BUFFER(event->md.start);
3586 lnet_ping_buffer_decref(pbuf);
3591 lnet_rsp_tracker_create(void)
3593 struct list_head **rstqs;
3594 rstqs = lnet_create_array_of_queues();
3599 the_lnet.ln_mt_rstq = rstqs;
3605 lnet_rsp_tracker_clean(void)
3607 lnet_finalize_expired_responses(true);
3609 cfs_percpt_free(the_lnet.ln_mt_rstq);
3610 the_lnet.ln_mt_rstq = NULL;
3613 int lnet_monitor_thr_start(void)
3616 struct task_struct *task;
3618 if (the_lnet.ln_mt_state != LNET_MT_STATE_SHUTDOWN)
3621 rc = lnet_resendqs_create();
3625 rc = lnet_rsp_tracker_create();
3629 /* Pre monitor thread start processing */
3630 rc = lnet_router_pre_mt_start();
3634 sema_init(&the_lnet.ln_mt_signal, 0);
3636 lnet_net_lock(LNET_LOCK_EX);
3637 the_lnet.ln_mt_state = LNET_MT_STATE_RUNNING;
3638 lnet_net_unlock(LNET_LOCK_EX);
3639 task = kthread_run(lnet_monitor_thread, NULL, "monitor_thread");
3642 CERROR("Can't start monitor thread: %d\n", rc);
3646 /* post monitor thread start processing */
3647 lnet_router_post_mt_start();
3652 lnet_net_lock(LNET_LOCK_EX);
3653 the_lnet.ln_mt_state = LNET_MT_STATE_STOPPING;
3654 lnet_net_unlock(LNET_LOCK_EX);
3655 /* block until event callback signals exit */
3656 down(&the_lnet.ln_mt_signal);
3658 lnet_router_cleanup();
3660 lnet_net_lock(LNET_LOCK_EX);
3661 the_lnet.ln_mt_state = LNET_MT_STATE_SHUTDOWN;
3662 lnet_net_unlock(LNET_LOCK_EX);
3663 lnet_rsp_tracker_clean();
3664 lnet_clean_local_ni_recoveryq();
3665 lnet_clean_peer_ni_recoveryq();
3666 lnet_clean_resendqs();
3667 LNetInvalidateEQHandle(&the_lnet.ln_mt_eqh);
3670 lnet_rsp_tracker_clean();
3671 lnet_clean_local_ni_recoveryq();
3672 lnet_clean_peer_ni_recoveryq();
3673 lnet_clean_resendqs();
3677 void lnet_monitor_thr_stop(void)
3679 if (the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN)
3682 LASSERT(the_lnet.ln_mt_state == LNET_MT_STATE_RUNNING);
3683 lnet_net_lock(LNET_LOCK_EX);
3684 the_lnet.ln_mt_state = LNET_MT_STATE_STOPPING;
3685 lnet_net_unlock(LNET_LOCK_EX);
3687 /* tell the monitor thread that we're shutting down */
3688 wake_up(&the_lnet.ln_mt_waitq);
3690 /* block until monitor thread signals that it's done */
3691 down(&the_lnet.ln_mt_signal);
3692 LASSERT(the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN);
3694 /* perform cleanup tasks */
3695 lnet_router_cleanup();
3696 lnet_rsp_tracker_clean();
3697 lnet_clean_local_ni_recoveryq();
3698 lnet_clean_peer_ni_recoveryq();
3699 lnet_clean_resendqs();
3705 lnet_drop_message(struct lnet_ni *ni, int cpt, void *private, unsigned int nob,
3709 lnet_incr_stats(&ni->ni_stats, msg_type, LNET_STATS_TYPE_DROP);
3710 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
3711 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length += nob;
3712 lnet_net_unlock(cpt);
3714 lnet_ni_recv(ni, private, NULL, 0, 0, 0, nob);
3718 lnet_recv_put(struct lnet_ni *ni, struct lnet_msg *msg)
3720 struct lnet_hdr *hdr = &msg->msg_hdr;
3722 if (msg->msg_wanted != 0)
3723 lnet_setpayloadbuffer(msg);
3725 lnet_build_msg_event(msg, LNET_EVENT_PUT);
3727 /* Must I ACK? If so I'll grab the ack_wmd out of the header and put
3728 * it back into the ACK during lnet_finalize() */
3729 msg->msg_ack = (!lnet_is_wire_handle_none(&hdr->msg.put.ack_wmd) &&
3730 (msg->msg_md->md_options & LNET_MD_ACK_DISABLE) == 0);
3732 lnet_ni_recv(ni, msg->msg_private, msg, msg->msg_rx_delayed,
3733 msg->msg_offset, msg->msg_wanted, hdr->payload_length);
3737 lnet_parse_put(struct lnet_ni *ni, struct lnet_msg *msg)
3739 struct lnet_hdr *hdr = &msg->msg_hdr;
3740 struct lnet_match_info info;
3744 /* Convert put fields to host byte order */
3745 hdr->msg.put.match_bits = le64_to_cpu(hdr->msg.put.match_bits);
3746 hdr->msg.put.ptl_index = le32_to_cpu(hdr->msg.put.ptl_index);
3747 hdr->msg.put.offset = le32_to_cpu(hdr->msg.put.offset);
3749 /* Primary peer NID. */
3750 info.mi_id.nid = msg->msg_initiator;
3751 info.mi_id.pid = hdr->src_pid;
3752 info.mi_opc = LNET_MD_OP_PUT;
3753 info.mi_portal = hdr->msg.put.ptl_index;
3754 info.mi_rlength = hdr->payload_length;
3755 info.mi_roffset = hdr->msg.put.offset;
3756 info.mi_mbits = hdr->msg.put.match_bits;
3757 info.mi_cpt = lnet_cpt_of_nid(msg->msg_initiator, ni);
3759 msg->msg_rx_ready_delay = ni->ni_net->net_lnd->lnd_eager_recv == NULL;
3760 ready_delay = msg->msg_rx_ready_delay;
3763 rc = lnet_ptl_match_md(&info, msg);
3768 case LNET_MATCHMD_OK:
3769 lnet_recv_put(ni, msg);
3772 case LNET_MATCHMD_NONE:
3774 /* no eager_recv or has already called it, should
3775 * have been attached on delayed list */
3778 rc = lnet_ni_eager_recv(ni, msg);
3785 case LNET_MATCHMD_DROP:
3786 CNETERR("Dropping PUT from %s portal %d match %llu"
3787 " offset %d length %d: %d\n",
3788 libcfs_id2str(info.mi_id), info.mi_portal,
3789 info.mi_mbits, info.mi_roffset, info.mi_rlength, rc);
3791 return -ENOENT; /* -ve: OK but no match */
3796 lnet_parse_get(struct lnet_ni *ni, struct lnet_msg *msg, int rdma_get)
3798 struct lnet_match_info info;
3799 struct lnet_hdr *hdr = &msg->msg_hdr;
3800 struct lnet_process_id source_id;
3801 struct lnet_handle_wire reply_wmd;
3804 /* Convert get fields to host byte order */
3805 hdr->msg.get.match_bits = le64_to_cpu(hdr->msg.get.match_bits);
3806 hdr->msg.get.ptl_index = le32_to_cpu(hdr->msg.get.ptl_index);
3807 hdr->msg.get.sink_length = le32_to_cpu(hdr->msg.get.sink_length);
3808 hdr->msg.get.src_offset = le32_to_cpu(hdr->msg.get.src_offset);
3810 source_id.nid = hdr->src_nid;
3811 source_id.pid = hdr->src_pid;
3812 /* Primary peer NID */
3813 info.mi_id.nid = msg->msg_initiator;
3814 info.mi_id.pid = hdr->src_pid;
3815 info.mi_opc = LNET_MD_OP_GET;
3816 info.mi_portal = hdr->msg.get.ptl_index;
3817 info.mi_rlength = hdr->msg.get.sink_length;
3818 info.mi_roffset = hdr->msg.get.src_offset;
3819 info.mi_mbits = hdr->msg.get.match_bits;
3820 info.mi_cpt = lnet_cpt_of_nid(msg->msg_initiator, ni);
3822 rc = lnet_ptl_match_md(&info, msg);
3823 if (rc == LNET_MATCHMD_DROP) {
3824 CNETERR("Dropping GET from %s portal %d match %llu"
3825 " offset %d length %d\n",
3826 libcfs_id2str(info.mi_id), info.mi_portal,
3827 info.mi_mbits, info.mi_roffset, info.mi_rlength);
3828 return -ENOENT; /* -ve: OK but no match */
3831 LASSERT(rc == LNET_MATCHMD_OK);
3833 lnet_build_msg_event(msg, LNET_EVENT_GET);
3835 reply_wmd = hdr->msg.get.return_wmd;
3837 lnet_prep_send(msg, LNET_MSG_REPLY, source_id,
3838 msg->msg_offset, msg->msg_wanted);
3840 msg->msg_hdr.msg.reply.dst_wmd = reply_wmd;
3843 /* The LND completes the REPLY from her recv procedure */
3844 lnet_ni_recv(ni, msg->msg_private, msg, 0,
3845 msg->msg_offset, msg->msg_len, msg->msg_len);
3849 lnet_ni_recv(ni, msg->msg_private, NULL, 0, 0, 0, 0);
3850 msg->msg_receiving = 0;
3852 rc = lnet_send(ni->ni_nid, msg, msg->msg_from);
3854 /* didn't get as far as lnet_ni_send() */
3855 CERROR("%s: Unable to send REPLY for GET from %s: %d\n",
3856 libcfs_nid2str(ni->ni_nid),
3857 libcfs_id2str(info.mi_id), rc);
3859 lnet_finalize(msg, rc);
3866 lnet_parse_reply(struct lnet_ni *ni, struct lnet_msg *msg)
3868 void *private = msg->msg_private;
3869 struct lnet_hdr *hdr = &msg->msg_hdr;
3870 struct lnet_process_id src = {0};
3871 struct lnet_libmd *md;
3876 cpt = lnet_cpt_of_cookie(hdr->msg.reply.dst_wmd.wh_object_cookie);
3879 src.nid = hdr->src_nid;
3880 src.pid = hdr->src_pid;
3882 /* NB handles only looked up by creator (no flips) */
3883 md = lnet_wire_handle2md(&hdr->msg.reply.dst_wmd);
3884 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
3885 CNETERR("%s: Dropping REPLY from %s for %s "
3887 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
3888 (md == NULL) ? "invalid" : "inactive",
3889 hdr->msg.reply.dst_wmd.wh_interface_cookie,
3890 hdr->msg.reply.dst_wmd.wh_object_cookie);
3891 if (md != NULL && md->md_me != NULL)
3892 CERROR("REPLY MD also attached to portal %d\n",
3893 md->md_me->me_portal);
3895 lnet_res_unlock(cpt);
3896 return -ENOENT; /* -ve: OK but no match */
3899 LASSERT(md->md_offset == 0);
3901 rlength = hdr->payload_length;
3902 mlength = MIN(rlength, (int)md->md_length);
3904 if (mlength < rlength &&
3905 (md->md_options & LNET_MD_TRUNCATE) == 0) {
3906 CNETERR("%s: Dropping REPLY from %s length %d "
3907 "for MD %#llx would overflow (%d)\n",
3908 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
3909 rlength, hdr->msg.reply.dst_wmd.wh_object_cookie,
3911 lnet_res_unlock(cpt);
3912 return -ENOENT; /* -ve: OK but no match */
3915 CDEBUG(D_NET, "%s: Reply from %s of length %d/%d into md %#llx\n",
3916 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
3917 mlength, rlength, hdr->msg.reply.dst_wmd.wh_object_cookie);
3919 lnet_msg_attach_md(msg, md, 0, mlength);
3922 lnet_setpayloadbuffer(msg);
3924 lnet_res_unlock(cpt);
3926 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
3928 lnet_ni_recv(ni, private, msg, 0, 0, mlength, rlength);
3933 lnet_parse_ack(struct lnet_ni *ni, struct lnet_msg *msg)
3935 struct lnet_hdr *hdr = &msg->msg_hdr;
3936 struct lnet_process_id src = {0};
3937 struct lnet_libmd *md;
3940 src.nid = hdr->src_nid;
3941 src.pid = hdr->src_pid;
3943 /* Convert ack fields to host byte order */
3944 hdr->msg.ack.match_bits = le64_to_cpu(hdr->msg.ack.match_bits);
3945 hdr->msg.ack.mlength = le32_to_cpu(hdr->msg.ack.mlength);
3947 cpt = lnet_cpt_of_cookie(hdr->msg.ack.dst_wmd.wh_object_cookie);
3950 /* NB handles only looked up by creator (no flips) */
3951 md = lnet_wire_handle2md(&hdr->msg.ack.dst_wmd);
3952 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
3953 /* Don't moan; this is expected */
3955 "%s: Dropping ACK from %s to %s MD %#llx.%#llx\n",
3956 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
3957 (md == NULL) ? "invalid" : "inactive",
3958 hdr->msg.ack.dst_wmd.wh_interface_cookie,
3959 hdr->msg.ack.dst_wmd.wh_object_cookie);
3960 if (md != NULL && md->md_me != NULL)
3961 CERROR("Source MD also attached to portal %d\n",
3962 md->md_me->me_portal);
3964 lnet_res_unlock(cpt);
3965 return -ENOENT; /* -ve! */
3968 CDEBUG(D_NET, "%s: ACK from %s into md %#llx\n",
3969 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
3970 hdr->msg.ack.dst_wmd.wh_object_cookie);
3972 lnet_msg_attach_md(msg, md, 0, 0);
3974 lnet_res_unlock(cpt);
3976 lnet_build_msg_event(msg, LNET_EVENT_ACK);
3978 lnet_ni_recv(ni, msg->msg_private, msg, 0, 0, 0, msg->msg_len);
3983 * \retval LNET_CREDIT_OK If \a msg is forwarded
3984 * \retval LNET_CREDIT_WAIT If \a msg is blocked because w/o buffer
3985 * \retval -ve error code
3988 lnet_parse_forward_locked(struct lnet_ni *ni, struct lnet_msg *msg)
3992 if (!the_lnet.ln_routing)
3995 if (msg->msg_rxpeer->lpni_rtrcredits <= 0 ||
3996 lnet_msg2bufpool(msg)->rbp_credits <= 0) {
3997 if (ni->ni_net->net_lnd->lnd_eager_recv == NULL) {
3998 msg->msg_rx_ready_delay = 1;
4000 lnet_net_unlock(msg->msg_rx_cpt);
4001 rc = lnet_ni_eager_recv(ni, msg);
4002 lnet_net_lock(msg->msg_rx_cpt);
4007 rc = lnet_post_routed_recv_locked(msg, 0);
4012 lnet_parse_local(struct lnet_ni *ni, struct lnet_msg *msg)
4016 switch (msg->msg_type) {
4018 rc = lnet_parse_ack(ni, msg);
4021 rc = lnet_parse_put(ni, msg);
4024 rc = lnet_parse_get(ni, msg, msg->msg_rdma_get);
4026 case LNET_MSG_REPLY:
4027 rc = lnet_parse_reply(ni, msg);
4029 default: /* prevent an unused label if !kernel */
4034 LASSERT(rc == 0 || rc == -ENOENT);
4039 lnet_msgtyp2str (int type)
4048 case LNET_MSG_REPLY:
4050 case LNET_MSG_HELLO:
4053 return ("<UNKNOWN>");
4058 lnet_print_hdr(struct lnet_hdr *hdr)
4060 struct lnet_process_id src = {
4061 .nid = hdr->src_nid,
4062 .pid = hdr->src_pid,
4064 struct lnet_process_id dst = {
4065 .nid = hdr->dest_nid,
4066 .pid = hdr->dest_pid,
4068 char *type_str = lnet_msgtyp2str(hdr->type);
4070 CWARN("P3 Header at %p of type %s\n", hdr, type_str);
4071 CWARN(" From %s\n", libcfs_id2str(src));
4072 CWARN(" To %s\n", libcfs_id2str(dst));
4074 switch (hdr->type) {
4079 CWARN(" Ptl index %d, ack md %#llx.%#llx, "
4080 "match bits %llu\n",
4081 hdr->msg.put.ptl_index,
4082 hdr->msg.put.ack_wmd.wh_interface_cookie,
4083 hdr->msg.put.ack_wmd.wh_object_cookie,
4084 hdr->msg.put.match_bits);
4085 CWARN(" Length %d, offset %d, hdr data %#llx\n",
4086 hdr->payload_length, hdr->msg.put.offset,
4087 hdr->msg.put.hdr_data);
4091 CWARN(" Ptl index %d, return md %#llx.%#llx, "
4092 "match bits %llu\n", hdr->msg.get.ptl_index,
4093 hdr->msg.get.return_wmd.wh_interface_cookie,
4094 hdr->msg.get.return_wmd.wh_object_cookie,
4095 hdr->msg.get.match_bits);
4096 CWARN(" Length %d, src offset %d\n",
4097 hdr->msg.get.sink_length,
4098 hdr->msg.get.src_offset);
4102 CWARN(" dst md %#llx.%#llx, "
4103 "manipulated length %d\n",
4104 hdr->msg.ack.dst_wmd.wh_interface_cookie,
4105 hdr->msg.ack.dst_wmd.wh_object_cookie,
4106 hdr->msg.ack.mlength);
4109 case LNET_MSG_REPLY:
4110 CWARN(" dst md %#llx.%#llx, "
4112 hdr->msg.reply.dst_wmd.wh_interface_cookie,
4113 hdr->msg.reply.dst_wmd.wh_object_cookie,
4114 hdr->payload_length);
4120 lnet_parse(struct lnet_ni *ni, struct lnet_hdr *hdr, lnet_nid_t from_nid,
4121 void *private, int rdma_req)
4126 struct lnet_msg *msg;
4127 lnet_pid_t dest_pid;
4128 lnet_nid_t dest_nid;
4130 struct lnet_peer_ni *lpni;
4131 __u32 payload_length;
4134 LASSERT (!in_interrupt ());
4136 type = le32_to_cpu(hdr->type);
4137 src_nid = le64_to_cpu(hdr->src_nid);
4138 dest_nid = le64_to_cpu(hdr->dest_nid);
4139 dest_pid = le32_to_cpu(hdr->dest_pid);
4140 payload_length = le32_to_cpu(hdr->payload_length);
4142 for_me = (ni->ni_nid == dest_nid);
4143 cpt = lnet_cpt_of_nid(from_nid, ni);
4145 CDEBUG(D_NET, "TRACE: %s(%s) <- %s : %s - %s\n",
4146 libcfs_nid2str(dest_nid),
4147 libcfs_nid2str(ni->ni_nid),
4148 libcfs_nid2str(src_nid),
4149 lnet_msgtyp2str(type),
4150 (for_me) ? "for me" : "routed");
4155 if (payload_length > 0) {
4156 CERROR("%s, src %s: bad %s payload %d (0 expected)\n",
4157 libcfs_nid2str(from_nid),
4158 libcfs_nid2str(src_nid),
4159 lnet_msgtyp2str(type), payload_length);
4165 case LNET_MSG_REPLY:
4166 if (payload_length >
4167 (__u32)(for_me ? LNET_MAX_PAYLOAD : LNET_MTU)) {
4168 CERROR("%s, src %s: bad %s payload %d "
4169 "(%d max expected)\n",
4170 libcfs_nid2str(from_nid),
4171 libcfs_nid2str(src_nid),
4172 lnet_msgtyp2str(type),
4174 for_me ? LNET_MAX_PAYLOAD : LNET_MTU);
4180 CERROR("%s, src %s: Bad message type 0x%x\n",
4181 libcfs_nid2str(from_nid),
4182 libcfs_nid2str(src_nid), type);
4186 if (the_lnet.ln_routing &&
4187 ni->ni_last_alive != ktime_get_real_seconds()) {
4188 /* NB: so far here is the only place to set NI status to "up */
4190 ni->ni_last_alive = ktime_get_real_seconds();
4191 if (ni->ni_status != NULL &&
4192 ni->ni_status->ns_status == LNET_NI_STATUS_DOWN)
4193 ni->ni_status->ns_status = LNET_NI_STATUS_UP;
4197 /* Regard a bad destination NID as a protocol error. Senders should
4198 * know what they're doing; if they don't they're misconfigured, buggy
4199 * or malicious so we chop them off at the knees :) */
4202 if (LNET_NIDNET(dest_nid) == LNET_NIDNET(ni->ni_nid)) {
4203 /* should have gone direct */
4204 CERROR("%s, src %s: Bad dest nid %s "
4205 "(should have been sent direct)\n",
4206 libcfs_nid2str(from_nid),
4207 libcfs_nid2str(src_nid),
4208 libcfs_nid2str(dest_nid));
4212 if (lnet_islocalnid(dest_nid)) {
4213 /* dest is another local NI; sender should have used
4214 * this node's NID on its own network */
4215 CERROR("%s, src %s: Bad dest nid %s "
4216 "(it's my nid but on a different network)\n",
4217 libcfs_nid2str(from_nid),
4218 libcfs_nid2str(src_nid),
4219 libcfs_nid2str(dest_nid));
4223 if (rdma_req && type == LNET_MSG_GET) {
4224 CERROR("%s, src %s: Bad optimized GET for %s "
4225 "(final destination must be me)\n",
4226 libcfs_nid2str(from_nid),
4227 libcfs_nid2str(src_nid),
4228 libcfs_nid2str(dest_nid));
4232 if (!the_lnet.ln_routing) {
4233 CERROR("%s, src %s: Dropping message for %s "
4234 "(routing not enabled)\n",
4235 libcfs_nid2str(from_nid),
4236 libcfs_nid2str(src_nid),
4237 libcfs_nid2str(dest_nid));
4242 /* Message looks OK; we're not going to return an error, so we MUST
4243 * call back lnd_recv() come what may... */
4245 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
4246 fail_peer(src_nid, 0)) { /* shall we now? */
4247 CERROR("%s, src %s: Dropping %s to simulate failure\n",
4248 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4249 lnet_msgtyp2str(type));
4253 if (!list_empty(&the_lnet.ln_drop_rules) &&
4254 lnet_drop_rule_match(hdr, NULL)) {
4255 CDEBUG(D_NET, "%s, src %s, dst %s: Dropping %s to simulate"
4256 "silent message loss\n",
4257 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4258 libcfs_nid2str(dest_nid), lnet_msgtyp2str(type));
4262 if (lnet_drop_asym_route && for_me &&
4263 LNET_NIDNET(src_nid) != LNET_NIDNET(from_nid)) {
4264 struct lnet_net *net;
4265 struct lnet_remotenet *rnet;
4268 /* we are dealing with a routed message,
4269 * so see if route to reach src_nid goes through from_nid
4272 net = lnet_get_net_locked(LNET_NIDNET(ni->ni_nid));
4274 lnet_net_unlock(cpt);
4275 CERROR("net %s not found\n",
4276 libcfs_net2str(LNET_NIDNET(ni->ni_nid)));
4280 rnet = lnet_find_rnet_locked(LNET_NIDNET(src_nid));
4282 struct lnet_peer_ni *gw = NULL;
4283 struct lnet_route *route;
4285 list_for_each_entry(route, &rnet->lrn_routes, lr_list) {
4287 gw = route->lr_gateway;
4288 if (gw->lpni_net != net)
4290 if (gw->lpni_nid == from_nid) {
4296 lnet_net_unlock(cpt);
4298 /* we would not use from_nid to route a message to
4300 * => asymmetric routing detected but forbidden
4302 CERROR("%s, src %s: Dropping asymmetrical route %s\n",
4303 libcfs_nid2str(from_nid),
4304 libcfs_nid2str(src_nid), lnet_msgtyp2str(type));
4309 msg = lnet_msg_alloc();
4311 CERROR("%s, src %s: Dropping %s (out of memory)\n",
4312 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4313 lnet_msgtyp2str(type));
4317 /* msg zeroed in lnet_msg_alloc; i.e. flags all clear,
4318 * pointers NULL etc */
4320 msg->msg_type = type;
4321 msg->msg_private = private;
4322 msg->msg_receiving = 1;
4323 msg->msg_rdma_get = rdma_req;
4324 msg->msg_len = msg->msg_wanted = payload_length;
4325 msg->msg_offset = 0;
4326 msg->msg_hdr = *hdr;
4327 /* for building message event */
4328 msg->msg_from = from_nid;
4330 msg->msg_target.pid = dest_pid;
4331 msg->msg_target.nid = dest_nid;
4332 msg->msg_routing = 1;
4335 /* convert common msg->hdr fields to host byteorder */
4336 msg->msg_hdr.type = type;
4337 msg->msg_hdr.src_nid = src_nid;
4338 msg->msg_hdr.src_pid = le32_to_cpu(msg->msg_hdr.src_pid);
4339 msg->msg_hdr.dest_nid = dest_nid;
4340 msg->msg_hdr.dest_pid = dest_pid;
4341 msg->msg_hdr.payload_length = payload_length;
4345 lpni = lnet_nid2peerni_locked(from_nid, ni->ni_nid, cpt);
4347 lnet_net_unlock(cpt);
4348 CERROR("%s, src %s: Dropping %s "
4349 "(error %ld looking up sender)\n",
4350 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4351 lnet_msgtyp2str(type), PTR_ERR(lpni));
4353 if (rc == -ESHUTDOWN)
4354 /* We are shutting down. Don't do anything more */
4358 msg->msg_rxpeer = lpni;
4360 lnet_ni_addref_locked(ni, cpt);
4361 /* Multi-Rail: Primary NID of source. */
4362 msg->msg_initiator = lnet_peer_primary_nid_locked(src_nid);
4364 if (lnet_isrouter(msg->msg_rxpeer)) {
4365 lnet_peer_set_alive(msg->msg_rxpeer);
4366 if (avoid_asym_router_failure &&
4367 LNET_NIDNET(src_nid) != LNET_NIDNET(from_nid)) {
4368 /* received a remote message from router, update
4369 * remote NI status on this router.
4370 * NB: multi-hop routed message will be ignored.
4372 lnet_router_ni_update_locked(msg->msg_rxpeer,
4373 LNET_NIDNET(src_nid));
4377 lnet_msg_commit(msg, cpt);
4379 /* message delay simulation */
4380 if (unlikely(!list_empty(&the_lnet.ln_delay_rules) &&
4381 lnet_delay_rule_match_locked(hdr, msg))) {
4382 lnet_net_unlock(cpt);
4387 rc = lnet_parse_forward_locked(ni, msg);
4388 lnet_net_unlock(cpt);
4393 if (rc == LNET_CREDIT_OK) {
4394 lnet_ni_recv(ni, msg->msg_private, msg, 0,
4395 0, payload_length, payload_length);
4400 lnet_net_unlock(cpt);
4402 rc = lnet_parse_local(ni, msg);
4408 LASSERT(msg->msg_md == NULL);
4409 lnet_finalize(msg, rc);
4412 lnet_drop_message(ni, cpt, private, payload_length, type);
4415 EXPORT_SYMBOL(lnet_parse);
4418 lnet_drop_delayed_msg_list(struct list_head *head, char *reason)
4420 while (!list_empty(head)) {
4421 struct lnet_process_id id = {0};
4422 struct lnet_msg *msg;
4424 msg = list_entry(head->next, struct lnet_msg, msg_list);
4425 list_del(&msg->msg_list);
4427 id.nid = msg->msg_hdr.src_nid;
4428 id.pid = msg->msg_hdr.src_pid;
4430 LASSERT(msg->msg_md == NULL);
4431 LASSERT(msg->msg_rx_delayed);
4432 LASSERT(msg->msg_rxpeer != NULL);
4433 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
4435 CWARN("Dropping delayed PUT from %s portal %d match %llu"
4436 " offset %d length %d: %s\n",
4438 msg->msg_hdr.msg.put.ptl_index,
4439 msg->msg_hdr.msg.put.match_bits,
4440 msg->msg_hdr.msg.put.offset,
4441 msg->msg_hdr.payload_length, reason);
4443 /* NB I can't drop msg's ref on msg_rxpeer until after I've
4444 * called lnet_drop_message(), so I just hang onto msg as well
4445 * until that's done */
4447 lnet_drop_message(msg->msg_rxni, msg->msg_rx_cpt,
4448 msg->msg_private, msg->msg_len,
4451 msg->msg_no_resend = true;
4453 * NB: message will not generate event because w/o attached MD,
4454 * but we still should give error code so lnet_msg_decommit()
4455 * can skip counters operations and other checks.
4457 lnet_finalize(msg, -ENOENT);
4462 lnet_recv_delayed_msg_list(struct list_head *head)
4464 while (!list_empty(head)) {
4465 struct lnet_msg *msg;
4466 struct lnet_process_id id;
4468 msg = list_entry(head->next, struct lnet_msg, msg_list);
4469 list_del(&msg->msg_list);
4471 /* md won't disappear under me, since each msg
4472 * holds a ref on it */
4474 id.nid = msg->msg_hdr.src_nid;
4475 id.pid = msg->msg_hdr.src_pid;
4477 LASSERT(msg->msg_rx_delayed);
4478 LASSERT(msg->msg_md != NULL);
4479 LASSERT(msg->msg_rxpeer != NULL);
4480 LASSERT(msg->msg_rxni != NULL);
4481 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
4483 CDEBUG(D_NET, "Resuming delayed PUT from %s portal %d "
4484 "match %llu offset %d length %d.\n",
4485 libcfs_id2str(id), msg->msg_hdr.msg.put.ptl_index,
4486 msg->msg_hdr.msg.put.match_bits,
4487 msg->msg_hdr.msg.put.offset,
4488 msg->msg_hdr.payload_length);
4490 lnet_recv_put(msg->msg_rxni, msg);
4495 lnet_attach_rsp_tracker(struct lnet_rsp_tracker *rspt, int cpt,
4496 struct lnet_libmd *md, struct lnet_handle_md mdh)
4499 bool new_entry = true;
4500 struct lnet_rsp_tracker *local_rspt;
4503 * MD has a refcount taken by message so it's not going away.
4504 * The MD however can be looked up. We need to secure the access
4505 * to the md_rspt_ptr by taking the res_lock.
4506 * The rspt can be accessed without protection up to when it gets
4507 * added to the list.
4511 local_rspt = md->md_rspt_ptr;
4512 timeout_ns = lnet_transaction_timeout * NSEC_PER_SEC;
4513 if (local_rspt != NULL) {
4515 * we already have an rspt attached to the md, so we'll
4516 * update the deadline on that one.
4518 LIBCFS_FREE(rspt, sizeof(*rspt));
4522 rspt->rspt_mdh = mdh;
4523 rspt->rspt_cpt = cpt;
4524 /* store the rspt so we can access it when we get the REPLY */
4525 md->md_rspt_ptr = rspt;
4528 local_rspt->rspt_deadline = ktime_add_ns(ktime_get(), timeout_ns);
4531 * add to the list of tracked responses. It's added to tail of the
4532 * list in order to expire all the older entries first.
4535 if (!new_entry && !list_empty(&local_rspt->rspt_on_list))
4536 list_del_init(&local_rspt->rspt_on_list);
4537 list_add_tail(&local_rspt->rspt_on_list, the_lnet.ln_mt_rstq[cpt]);
4538 lnet_net_unlock(cpt);
4539 lnet_res_unlock(cpt);
4543 * Initiate an asynchronous PUT operation.
4545 * There are several events associated with a PUT: completion of the send on
4546 * the initiator node (LNET_EVENT_SEND), and when the send completes
4547 * successfully, the receipt of an acknowledgment (LNET_EVENT_ACK) indicating
4548 * that the operation was accepted by the target. The event LNET_EVENT_PUT is
4549 * used at the target node to indicate the completion of incoming data
4552 * The local events will be logged in the EQ associated with the MD pointed to
4553 * by \a mdh handle. Using a MD without an associated EQ results in these
4554 * events being discarded. In this case, the caller must have another
4555 * mechanism (e.g., a higher level protocol) for determining when it is safe
4556 * to modify the memory region associated with the MD.
4558 * Note that LNet does not guarantee the order of LNET_EVENT_SEND and
4559 * LNET_EVENT_ACK, though intuitively ACK should happen after SEND.
4561 * \param self Indicates the NID of a local interface through which to send
4562 * the PUT request. Use LNET_NID_ANY to let LNet choose one by itself.
4563 * \param mdh A handle for the MD that describes the memory to be sent. The MD
4564 * must be "free floating" (See LNetMDBind()).
4565 * \param ack Controls whether an acknowledgment is requested.
4566 * Acknowledgments are only sent when they are requested by the initiating
4567 * process and the target MD enables them.
4568 * \param target A process identifier for the target process.
4569 * \param portal The index in the \a target's portal table.
4570 * \param match_bits The match bits to use for MD selection at the target
4572 * \param offset The offset into the target MD (only used when the target
4573 * MD has the LNET_MD_MANAGE_REMOTE option set).
4574 * \param hdr_data 64 bits of user data that can be included in the message
4575 * header. This data is written to an event queue entry at the target if an
4576 * EQ is present on the matching MD.
4578 * \retval 0 Success, and only in this case events will be generated
4579 * and logged to EQ (if it exists).
4580 * \retval -EIO Simulated failure.
4581 * \retval -ENOMEM Memory allocation failure.
4582 * \retval -ENOENT Invalid MD object.
4584 * \see struct lnet_event::hdr_data and lnet_event_kind_t.
4587 LNetPut(lnet_nid_t self, struct lnet_handle_md mdh, enum lnet_ack_req ack,
4588 struct lnet_process_id target, unsigned int portal,
4589 __u64 match_bits, unsigned int offset,
4592 struct lnet_msg *msg;
4593 struct lnet_libmd *md;
4596 struct lnet_rsp_tracker *rspt = NULL;
4598 LASSERT(the_lnet.ln_refcount > 0);
4600 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
4601 fail_peer(target.nid, 1)) { /* shall we now? */
4602 CERROR("Dropping PUT to %s: simulated failure\n",
4603 libcfs_id2str(target));
4607 msg = lnet_msg_alloc();
4609 CERROR("Dropping PUT to %s: ENOMEM on struct lnet_msg\n",
4610 libcfs_id2str(target));
4613 msg->msg_vmflush = !!memory_pressure_get();
4615 cpt = lnet_cpt_of_cookie(mdh.cookie);
4617 if (ack == LNET_ACK_REQ) {
4618 rspt = lnet_rspt_alloc(cpt);
4620 CERROR("Dropping PUT to %s: ENOMEM on response tracker\n",
4621 libcfs_id2str(target));
4624 INIT_LIST_HEAD(&rspt->rspt_on_list);
4629 md = lnet_handle2md(&mdh);
4630 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
4631 CERROR("Dropping PUT (%llu:%d:%s): MD (%d) invalid\n",
4632 match_bits, portal, libcfs_id2str(target),
4633 md == NULL ? -1 : md->md_threshold);
4634 if (md != NULL && md->md_me != NULL)
4635 CERROR("Source MD also attached to portal %d\n",
4636 md->md_me->me_portal);
4637 lnet_res_unlock(cpt);
4639 LIBCFS_FREE(rspt, sizeof(*rspt));
4644 CDEBUG(D_NET, "LNetPut -> %s\n", libcfs_id2str(target));
4646 lnet_msg_attach_md(msg, md, 0, 0);
4648 lnet_prep_send(msg, LNET_MSG_PUT, target, 0, md->md_length);
4650 msg->msg_hdr.msg.put.match_bits = cpu_to_le64(match_bits);
4651 msg->msg_hdr.msg.put.ptl_index = cpu_to_le32(portal);
4652 msg->msg_hdr.msg.put.offset = cpu_to_le32(offset);
4653 msg->msg_hdr.msg.put.hdr_data = hdr_data;
4655 /* NB handles only looked up by creator (no flips) */
4656 if (ack == LNET_ACK_REQ) {
4657 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
4658 the_lnet.ln_interface_cookie;
4659 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
4660 md->md_lh.lh_cookie;
4662 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
4663 LNET_WIRE_HANDLE_COOKIE_NONE;
4664 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
4665 LNET_WIRE_HANDLE_COOKIE_NONE;
4668 lnet_res_unlock(cpt);
4670 lnet_build_msg_event(msg, LNET_EVENT_SEND);
4672 if (ack == LNET_ACK_REQ)
4673 lnet_attach_rsp_tracker(rspt, cpt, md, mdh);
4675 if (CFS_FAIL_CHECK_ORSET(CFS_FAIL_PTLRPC_OST_BULK_CB2,
4679 rc = lnet_send(self, msg, LNET_NID_ANY);
4682 CNETERR("Error sending PUT to %s: %d\n",
4683 libcfs_id2str(target), rc);
4684 msg->msg_no_resend = true;
4685 lnet_finalize(msg, rc);
4688 /* completion will be signalled by an event */
4691 EXPORT_SYMBOL(LNetPut);
4694 * The LND can DMA direct to the GET md (i.e. no REPLY msg). This
4695 * returns a msg for the LND to pass to lnet_finalize() when the sink
4696 * data has been received.
4698 * CAVEAT EMPTOR: 'getmsg' is the original GET, which is freed when
4699 * lnet_finalize() is called on it, so the LND must call this first
4702 lnet_create_reply_msg(struct lnet_ni *ni, struct lnet_msg *getmsg)
4704 struct lnet_msg *msg = lnet_msg_alloc();
4705 struct lnet_libmd *getmd = getmsg->msg_md;
4706 struct lnet_process_id peer_id = getmsg->msg_target;
4709 LASSERT(!getmsg->msg_target_is_router);
4710 LASSERT(!getmsg->msg_routing);
4713 CERROR("%s: Dropping REPLY from %s: can't allocate msg\n",
4714 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id));
4718 cpt = lnet_cpt_of_cookie(getmd->md_lh.lh_cookie);
4721 LASSERT(getmd->md_refcount > 0);
4723 if (getmd->md_threshold == 0) {
4724 CERROR("%s: Dropping REPLY from %s for inactive MD %p\n",
4725 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id),
4727 lnet_res_unlock(cpt);
4731 LASSERT(getmd->md_offset == 0);
4733 CDEBUG(D_NET, "%s: Reply from %s md %p\n",
4734 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id), getmd);
4736 /* setup information for lnet_build_msg_event */
4737 msg->msg_initiator = getmsg->msg_txpeer->lpni_peer_net->lpn_peer->lp_primary_nid;
4738 msg->msg_from = peer_id.nid;
4739 msg->msg_type = LNET_MSG_GET; /* flag this msg as an "optimized" GET */
4740 msg->msg_hdr.src_nid = peer_id.nid;
4741 msg->msg_hdr.payload_length = getmd->md_length;
4742 msg->msg_receiving = 1; /* required by lnet_msg_attach_md */
4744 lnet_msg_attach_md(msg, getmd, getmd->md_offset, getmd->md_length);
4745 lnet_res_unlock(cpt);
4747 cpt = lnet_cpt_of_nid(peer_id.nid, ni);
4750 lnet_msg_commit(msg, cpt);
4751 lnet_net_unlock(cpt);
4753 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
4758 cpt = lnet_cpt_of_nid(peer_id.nid, ni);
4761 lnet_incr_stats(&ni->ni_stats, LNET_MSG_GET, LNET_STATS_TYPE_DROP);
4762 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
4763 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length +=
4765 lnet_net_unlock(cpt);
4772 EXPORT_SYMBOL(lnet_create_reply_msg);
4775 lnet_set_reply_msg_len(struct lnet_ni *ni, struct lnet_msg *reply,
4778 /* Set the REPLY length, now the RDMA that elides the REPLY message has
4779 * completed and I know it. */
4780 LASSERT(reply != NULL);
4781 LASSERT(reply->msg_type == LNET_MSG_GET);
4782 LASSERT(reply->msg_ev.type == LNET_EVENT_REPLY);
4784 /* NB I trusted my peer to RDMA. If she tells me she's written beyond
4785 * the end of my buffer, I might as well be dead. */
4786 LASSERT(len <= reply->msg_ev.mlength);
4788 reply->msg_ev.mlength = len;
4790 EXPORT_SYMBOL(lnet_set_reply_msg_len);
4793 * Initiate an asynchronous GET operation.
4795 * On the initiator node, an LNET_EVENT_SEND is logged when the GET request
4796 * is sent, and an LNET_EVENT_REPLY is logged when the data returned from
4797 * the target node in the REPLY has been written to local MD.
4799 * On the target node, an LNET_EVENT_GET is logged when the GET request
4800 * arrives and is accepted into a MD.
4802 * \param self,target,portal,match_bits,offset See the discussion in LNetPut().
4803 * \param mdh A handle for the MD that describes the memory into which the
4804 * requested data will be received. The MD must be "free floating" (See LNetMDBind()).
4806 * \retval 0 Success, and only in this case events will be generated
4807 * and logged to EQ (if it exists) of the MD.
4808 * \retval -EIO Simulated failure.
4809 * \retval -ENOMEM Memory allocation failure.
4810 * \retval -ENOENT Invalid MD object.
4813 LNetGet(lnet_nid_t self, struct lnet_handle_md mdh,
4814 struct lnet_process_id target, unsigned int portal,
4815 __u64 match_bits, unsigned int offset, bool recovery)
4817 struct lnet_msg *msg;
4818 struct lnet_libmd *md;
4819 struct lnet_rsp_tracker *rspt;
4823 LASSERT(the_lnet.ln_refcount > 0);
4825 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
4826 fail_peer(target.nid, 1)) /* shall we now? */
4828 CERROR("Dropping GET to %s: simulated failure\n",
4829 libcfs_id2str(target));
4833 msg = lnet_msg_alloc();
4835 CERROR("Dropping GET to %s: ENOMEM on struct lnet_msg\n",
4836 libcfs_id2str(target));
4840 cpt = lnet_cpt_of_cookie(mdh.cookie);
4842 rspt = lnet_rspt_alloc(cpt);
4844 CERROR("Dropping GET to %s: ENOMEM on response tracker\n",
4845 libcfs_id2str(target));
4848 INIT_LIST_HEAD(&rspt->rspt_on_list);
4850 msg->msg_recovery = recovery;
4854 md = lnet_handle2md(&mdh);
4855 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
4856 CERROR("Dropping GET (%llu:%d:%s): MD (%d) invalid\n",
4857 match_bits, portal, libcfs_id2str(target),
4858 md == NULL ? -1 : md->md_threshold);
4859 if (md != NULL && md->md_me != NULL)
4860 CERROR("REPLY MD also attached to portal %d\n",
4861 md->md_me->me_portal);
4863 lnet_res_unlock(cpt);
4866 LIBCFS_FREE(rspt, sizeof(*rspt));
4870 CDEBUG(D_NET, "LNetGet -> %s\n", libcfs_id2str(target));
4872 lnet_msg_attach_md(msg, md, 0, 0);
4874 lnet_prep_send(msg, LNET_MSG_GET, target, 0, 0);
4876 msg->msg_hdr.msg.get.match_bits = cpu_to_le64(match_bits);
4877 msg->msg_hdr.msg.get.ptl_index = cpu_to_le32(portal);
4878 msg->msg_hdr.msg.get.src_offset = cpu_to_le32(offset);
4879 msg->msg_hdr.msg.get.sink_length = cpu_to_le32(md->md_length);
4881 /* NB handles only looked up by creator (no flips) */
4882 msg->msg_hdr.msg.get.return_wmd.wh_interface_cookie =
4883 the_lnet.ln_interface_cookie;
4884 msg->msg_hdr.msg.get.return_wmd.wh_object_cookie =
4885 md->md_lh.lh_cookie;
4887 lnet_res_unlock(cpt);
4889 lnet_build_msg_event(msg, LNET_EVENT_SEND);
4891 lnet_attach_rsp_tracker(rspt, cpt, md, mdh);
4893 rc = lnet_send(self, msg, LNET_NID_ANY);
4895 CNETERR("Error sending GET to %s: %d\n",
4896 libcfs_id2str(target), rc);
4897 msg->msg_no_resend = true;
4898 lnet_finalize(msg, rc);
4901 /* completion will be signalled by an event */
4904 EXPORT_SYMBOL(LNetGet);
4907 * Calculate distance to node at \a dstnid.
4909 * \param dstnid Target NID.
4910 * \param srcnidp If not NULL, NID of the local interface to reach \a dstnid
4912 * \param orderp If not NULL, order of the route to reach \a dstnid is saved
4915 * \retval 0 If \a dstnid belongs to a local interface, and reserved option
4916 * local_nid_dist_zero is set, which is the default.
4917 * \retval positives Distance to target NID, i.e. number of hops plus one.
4918 * \retval -EHOSTUNREACH If \a dstnid is not reachable.
4921 LNetDist(lnet_nid_t dstnid, lnet_nid_t *srcnidp, __u32 *orderp)
4923 struct list_head *e;
4924 struct lnet_ni *ni = NULL;
4925 struct lnet_remotenet *rnet;
4926 __u32 dstnet = LNET_NIDNET(dstnid);
4930 struct list_head *rn_list;
4932 /* if !local_nid_dist_zero, I don't return a distance of 0 ever
4933 * (when lustre sees a distance of 0, it substitutes 0@lo), so I
4934 * keep order 0 free for 0@lo and order 1 free for a local NID
4937 LASSERT(the_lnet.ln_refcount > 0);
4939 cpt = lnet_net_lock_current();
4941 while ((ni = lnet_get_next_ni_locked(NULL, ni))) {
4942 if (ni->ni_nid == dstnid) {
4943 if (srcnidp != NULL)
4945 if (orderp != NULL) {
4946 if (LNET_NETTYP(LNET_NIDNET(dstnid)) == LOLND)
4951 lnet_net_unlock(cpt);
4953 return local_nid_dist_zero ? 0 : 1;
4956 if (LNET_NIDNET(ni->ni_nid) == dstnet) {
4957 /* Check if ni was originally created in
4958 * current net namespace.
4959 * If not, assign order above 0xffff0000,
4960 * to make this ni not a priority. */
4961 if (!net_eq(ni->ni_net_ns, current->nsproxy->net_ns))
4962 order += 0xffff0000;
4964 if (srcnidp != NULL)
4965 *srcnidp = ni->ni_nid;
4968 lnet_net_unlock(cpt);
4975 rn_list = lnet_net2rnethash(dstnet);
4976 list_for_each(e, rn_list) {
4977 rnet = list_entry(e, struct lnet_remotenet, lrn_list);
4979 if (rnet->lrn_net == dstnet) {
4980 struct lnet_route *route;
4981 struct lnet_route *shortest = NULL;
4982 __u32 shortest_hops = LNET_UNDEFINED_HOPS;
4985 LASSERT(!list_empty(&rnet->lrn_routes));
4987 list_for_each_entry(route, &rnet->lrn_routes,
4989 route_hops = route->lr_hops;
4990 if (route_hops == LNET_UNDEFINED_HOPS)
4992 if (shortest == NULL ||
4993 route_hops < shortest_hops) {
4995 shortest_hops = route_hops;
4999 LASSERT(shortest != NULL);
5000 hops = shortest_hops;
5001 if (srcnidp != NULL) {
5002 ni = lnet_get_next_ni_locked(
5003 shortest->lr_gateway->lpni_net,
5005 *srcnidp = ni->ni_nid;
5009 lnet_net_unlock(cpt);
5015 lnet_net_unlock(cpt);
5016 return -EHOSTUNREACH;
5018 EXPORT_SYMBOL(LNetDist);