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;
67 lnet_msg_is_response(struct lnet_msg *msg)
69 return msg->msg_type == LNET_MSG_ACK || msg->msg_type == LNET_MSG_REPLY;
72 static inline struct lnet_comm_count *
73 get_stats_counts(struct lnet_element_stats *stats,
74 enum lnet_stats_type stats_type)
77 case LNET_STATS_TYPE_SEND:
78 return &stats->el_send_stats;
79 case LNET_STATS_TYPE_RECV:
80 return &stats->el_recv_stats;
81 case LNET_STATS_TYPE_DROP:
82 return &stats->el_drop_stats;
84 CERROR("Unknown stats type\n");
90 void lnet_incr_stats(struct lnet_element_stats *stats,
91 enum lnet_msg_type msg_type,
92 enum lnet_stats_type stats_type)
94 struct lnet_comm_count *counts = get_stats_counts(stats, stats_type);
100 atomic_inc(&counts->co_ack_count);
103 atomic_inc(&counts->co_put_count);
106 atomic_inc(&counts->co_get_count);
109 atomic_inc(&counts->co_reply_count);
112 atomic_inc(&counts->co_hello_count);
115 CERROR("There is a BUG in the code. Unknown message type\n");
120 __u32 lnet_sum_stats(struct lnet_element_stats *stats,
121 enum lnet_stats_type stats_type)
123 struct lnet_comm_count *counts = get_stats_counts(stats, stats_type);
127 return (atomic_read(&counts->co_ack_count) +
128 atomic_read(&counts->co_put_count) +
129 atomic_read(&counts->co_get_count) +
130 atomic_read(&counts->co_reply_count) +
131 atomic_read(&counts->co_hello_count));
134 static inline void assign_stats(struct lnet_ioctl_comm_count *msg_stats,
135 struct lnet_comm_count *counts)
137 msg_stats->ico_get_count = atomic_read(&counts->co_get_count);
138 msg_stats->ico_put_count = atomic_read(&counts->co_put_count);
139 msg_stats->ico_reply_count = atomic_read(&counts->co_reply_count);
140 msg_stats->ico_ack_count = atomic_read(&counts->co_ack_count);
141 msg_stats->ico_hello_count = atomic_read(&counts->co_hello_count);
144 void lnet_usr_translate_stats(struct lnet_ioctl_element_msg_stats *msg_stats,
145 struct lnet_element_stats *stats)
147 struct lnet_comm_count *counts;
152 counts = get_stats_counts(stats, LNET_STATS_TYPE_SEND);
155 assign_stats(&msg_stats->im_send_stats, counts);
157 counts = get_stats_counts(stats, LNET_STATS_TYPE_RECV);
160 assign_stats(&msg_stats->im_recv_stats, counts);
162 counts = get_stats_counts(stats, LNET_STATS_TYPE_DROP);
165 assign_stats(&msg_stats->im_drop_stats, counts);
169 lnet_fail_nid(lnet_nid_t nid, unsigned int threshold)
171 struct lnet_test_peer *tp;
172 struct list_head *el;
173 struct list_head *next;
176 /* NB: use lnet_net_lock(0) to serialize operations on test peers */
177 if (threshold != 0) {
178 /* Adding a new entry */
179 LIBCFS_ALLOC(tp, sizeof(*tp));
184 tp->tp_threshold = threshold;
187 list_add_tail(&tp->tp_list, &the_lnet.ln_test_peers);
194 list_for_each_safe(el, next, &the_lnet.ln_test_peers) {
195 tp = list_entry(el, struct lnet_test_peer, tp_list);
197 if (tp->tp_threshold == 0 || /* needs culling anyway */
198 nid == LNET_NID_ANY || /* removing all entries */
199 tp->tp_nid == nid) { /* matched this one */
200 list_move(&tp->tp_list, &cull);
206 while (!list_empty(&cull)) {
207 tp = list_entry(cull.next, struct lnet_test_peer, tp_list);
209 list_del(&tp->tp_list);
210 LIBCFS_FREE(tp, sizeof(*tp));
216 fail_peer (lnet_nid_t nid, int outgoing)
218 struct lnet_test_peer *tp;
219 struct list_head *el;
220 struct list_head *next;
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_move(&tp->tp_list, &cull);
241 if (tp->tp_nid == LNET_NID_ANY || /* fail every peer */
242 nid == tp->tp_nid) { /* fail this peer */
245 if (tp->tp_threshold != LNET_MD_THRESH_INF) {
248 tp->tp_threshold == 0) {
250 list_move(&tp->tp_list, &cull);
259 while (!list_empty(&cull)) {
260 tp = list_entry(cull.next, struct lnet_test_peer, tp_list);
261 list_del(&tp->tp_list);
263 LIBCFS_FREE(tp, sizeof(*tp));
270 lnet_iov_nob(unsigned int niov, struct kvec *iov)
272 unsigned int nob = 0;
274 LASSERT(niov == 0 || iov != NULL);
276 nob += (iov++)->iov_len;
280 EXPORT_SYMBOL(lnet_iov_nob);
283 lnet_copy_iov2iov(unsigned int ndiov, struct kvec *diov, unsigned int doffset,
284 unsigned int nsiov, struct kvec *siov, unsigned int soffset,
287 /* NB diov, siov are READ-ONLY */
288 unsigned int this_nob;
293 /* skip complete frags before 'doffset' */
295 while (doffset >= diov->iov_len) {
296 doffset -= diov->iov_len;
302 /* skip complete frags before 'soffset' */
304 while (soffset >= siov->iov_len) {
305 soffset -= siov->iov_len;
314 this_nob = min3((unsigned int)diov->iov_len - doffset,
315 (unsigned int)siov->iov_len - soffset,
318 memcpy((char *)diov->iov_base + doffset,
319 (char *)siov->iov_base + soffset, this_nob);
322 if (diov->iov_len > doffset + this_nob) {
330 if (siov->iov_len > soffset + this_nob) {
339 EXPORT_SYMBOL(lnet_copy_iov2iov);
342 lnet_extract_iov(int dst_niov, struct kvec *dst,
343 int src_niov, struct kvec *src,
344 unsigned int offset, unsigned int len)
346 /* Initialise 'dst' to the subset of 'src' starting at 'offset',
347 * for exactly 'len' bytes, and return the number of entries.
348 * NB not destructive to 'src' */
349 unsigned int frag_len;
352 if (len == 0) /* no data => */
353 return (0); /* no frags */
355 LASSERT(src_niov > 0);
356 while (offset >= src->iov_len) { /* skip initial frags */
357 offset -= src->iov_len;
360 LASSERT(src_niov > 0);
365 LASSERT(src_niov > 0);
366 LASSERT((int)niov <= dst_niov);
368 frag_len = src->iov_len - offset;
369 dst->iov_base = ((char *)src->iov_base) + offset;
371 if (len <= frag_len) {
376 dst->iov_len = frag_len;
386 EXPORT_SYMBOL(lnet_extract_iov);
390 lnet_kiov_nob(unsigned int niov, lnet_kiov_t *kiov)
392 unsigned int nob = 0;
394 LASSERT(niov == 0 || kiov != NULL);
396 nob += (kiov++)->kiov_len;
400 EXPORT_SYMBOL(lnet_kiov_nob);
403 lnet_copy_kiov2kiov(unsigned int ndiov, lnet_kiov_t *diov, unsigned int doffset,
404 unsigned int nsiov, lnet_kiov_t *siov, unsigned int soffset,
407 /* NB diov, siov are READ-ONLY */
408 unsigned int this_nob;
415 LASSERT (!in_interrupt ());
418 while (doffset >= diov->kiov_len) {
419 doffset -= diov->kiov_len;
426 while (soffset >= siov->kiov_len) {
427 soffset -= siov->kiov_len;
436 this_nob = min3(diov->kiov_len - doffset,
437 siov->kiov_len - soffset,
441 daddr = ((char *)kmap(diov->kiov_page)) +
442 diov->kiov_offset + doffset;
444 saddr = ((char *)kmap(siov->kiov_page)) +
445 siov->kiov_offset + soffset;
447 /* Vanishing risk of kmap deadlock when mapping 2 pages.
448 * However in practice at least one of the kiovs will be mapped
449 * kernel pages and the map/unmap will be NOOPs */
451 memcpy (daddr, saddr, this_nob);
454 if (diov->kiov_len > doffset + this_nob) {
458 kunmap(diov->kiov_page);
465 if (siov->kiov_len > soffset + this_nob) {
469 kunmap(siov->kiov_page);
478 kunmap(diov->kiov_page);
480 kunmap(siov->kiov_page);
482 EXPORT_SYMBOL(lnet_copy_kiov2kiov);
485 lnet_copy_kiov2iov (unsigned int niov, struct kvec *iov, unsigned int iovoffset,
486 unsigned int nkiov, lnet_kiov_t *kiov, unsigned int kiovoffset,
489 /* NB iov, kiov are READ-ONLY */
490 unsigned int this_nob;
496 LASSERT (!in_interrupt ());
499 while (iovoffset >= iov->iov_len) {
500 iovoffset -= iov->iov_len;
507 while (kiovoffset >= kiov->kiov_len) {
508 kiovoffset -= kiov->kiov_len;
517 this_nob = min3((unsigned int)iov->iov_len - iovoffset,
518 (unsigned int)kiov->kiov_len - kiovoffset,
522 addr = ((char *)kmap(kiov->kiov_page)) +
523 kiov->kiov_offset + kiovoffset;
525 memcpy((char *)iov->iov_base + iovoffset, addr, this_nob);
528 if (iov->iov_len > iovoffset + this_nob) {
529 iovoffset += this_nob;
536 if (kiov->kiov_len > kiovoffset + this_nob) {
538 kiovoffset += this_nob;
540 kunmap(kiov->kiov_page);
550 kunmap(kiov->kiov_page);
552 EXPORT_SYMBOL(lnet_copy_kiov2iov);
555 lnet_copy_iov2kiov(unsigned int nkiov, lnet_kiov_t *kiov, unsigned int kiovoffset,
556 unsigned int niov, struct kvec *iov, unsigned int iovoffset,
559 /* NB kiov, iov are READ-ONLY */
560 unsigned int this_nob;
566 LASSERT (!in_interrupt ());
569 while (kiovoffset >= kiov->kiov_len) {
570 kiovoffset -= kiov->kiov_len;
577 while (iovoffset >= iov->iov_len) {
578 iovoffset -= iov->iov_len;
587 this_nob = min3((unsigned int)kiov->kiov_len - kiovoffset,
588 (unsigned int)iov->iov_len - iovoffset,
592 addr = ((char *)kmap(kiov->kiov_page)) +
593 kiov->kiov_offset + kiovoffset;
595 memcpy (addr, (char *)iov->iov_base + iovoffset, this_nob);
598 if (kiov->kiov_len > kiovoffset + this_nob) {
600 kiovoffset += this_nob;
602 kunmap(kiov->kiov_page);
609 if (iov->iov_len > iovoffset + this_nob) {
610 iovoffset += this_nob;
619 kunmap(kiov->kiov_page);
621 EXPORT_SYMBOL(lnet_copy_iov2kiov);
624 lnet_extract_kiov(int dst_niov, lnet_kiov_t *dst,
625 int src_niov, lnet_kiov_t *src,
626 unsigned int offset, unsigned int len)
628 /* Initialise 'dst' to the subset of 'src' starting at 'offset',
629 * for exactly 'len' bytes, and return the number of entries.
630 * NB not destructive to 'src' */
631 unsigned int frag_len;
634 if (len == 0) /* no data => */
635 return (0); /* no frags */
637 LASSERT(src_niov > 0);
638 while (offset >= src->kiov_len) { /* skip initial frags */
639 offset -= src->kiov_len;
642 LASSERT(src_niov > 0);
647 LASSERT(src_niov > 0);
648 LASSERT((int)niov <= dst_niov);
650 frag_len = src->kiov_len - offset;
651 dst->kiov_page = src->kiov_page;
652 dst->kiov_offset = src->kiov_offset + offset;
654 if (len <= frag_len) {
656 LASSERT(dst->kiov_offset + dst->kiov_len <= PAGE_SIZE);
660 dst->kiov_len = frag_len;
661 LASSERT(dst->kiov_offset + dst->kiov_len <= PAGE_SIZE);
671 EXPORT_SYMBOL(lnet_extract_kiov);
674 lnet_ni_recv(struct lnet_ni *ni, void *private, struct lnet_msg *msg,
675 int delayed, unsigned int offset, unsigned int mlen,
678 unsigned int niov = 0;
679 struct kvec *iov = NULL;
680 lnet_kiov_t *kiov = NULL;
683 LASSERT (!in_interrupt ());
684 LASSERT (mlen == 0 || msg != NULL);
687 LASSERT(msg->msg_receiving);
688 LASSERT(!msg->msg_sending);
689 LASSERT(rlen == msg->msg_len);
690 LASSERT(mlen <= msg->msg_len);
691 LASSERT(msg->msg_offset == offset);
692 LASSERT(msg->msg_wanted == mlen);
694 msg->msg_receiving = 0;
697 niov = msg->msg_niov;
699 kiov = msg->msg_kiov;
702 LASSERT ((iov == NULL) != (kiov == NULL));
706 rc = (ni->ni_net->net_lnd->lnd_recv)(ni, private, msg, delayed,
707 niov, iov, kiov, offset, mlen,
710 lnet_finalize(msg, rc);
714 lnet_setpayloadbuffer(struct lnet_msg *msg)
716 struct lnet_libmd *md = msg->msg_md;
718 LASSERT(msg->msg_len > 0);
719 LASSERT(!msg->msg_routing);
721 LASSERT(msg->msg_niov == 0);
722 LASSERT(msg->msg_iov == NULL);
723 LASSERT(msg->msg_kiov == NULL);
725 msg->msg_niov = md->md_niov;
726 if ((md->md_options & LNET_MD_KIOV) != 0)
727 msg->msg_kiov = md->md_iov.kiov;
729 msg->msg_iov = md->md_iov.iov;
733 lnet_prep_send(struct lnet_msg *msg, int type, struct lnet_process_id target,
734 unsigned int offset, unsigned int len)
736 msg->msg_type = type;
737 msg->msg_target = target;
739 msg->msg_offset = offset;
742 lnet_setpayloadbuffer(msg);
744 memset (&msg->msg_hdr, 0, sizeof (msg->msg_hdr));
745 msg->msg_hdr.type = cpu_to_le32(type);
746 /* dest_nid will be overwritten by lnet_select_pathway() */
747 msg->msg_hdr.dest_nid = cpu_to_le64(target.nid);
748 msg->msg_hdr.dest_pid = cpu_to_le32(target.pid);
749 /* src_nid will be set later */
750 msg->msg_hdr.src_pid = cpu_to_le32(the_lnet.ln_pid);
751 msg->msg_hdr.payload_length = cpu_to_le32(len);
755 lnet_ni_send(struct lnet_ni *ni, struct lnet_msg *msg)
757 void *priv = msg->msg_private;
760 LASSERT (!in_interrupt ());
761 LASSERT (LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND ||
762 (msg->msg_txcredit && msg->msg_peertxcredit));
764 rc = (ni->ni_net->net_lnd->lnd_send)(ni, priv, msg);
766 msg->msg_no_resend = true;
767 lnet_finalize(msg, rc);
772 lnet_ni_eager_recv(struct lnet_ni *ni, struct lnet_msg *msg)
776 LASSERT(!msg->msg_sending);
777 LASSERT(msg->msg_receiving);
778 LASSERT(!msg->msg_rx_ready_delay);
779 LASSERT(ni->ni_net->net_lnd->lnd_eager_recv != NULL);
781 msg->msg_rx_ready_delay = 1;
782 rc = (ni->ni_net->net_lnd->lnd_eager_recv)(ni, msg->msg_private, msg,
785 CERROR("recv from %s / send to %s aborted: "
786 "eager_recv failed %d\n",
787 libcfs_nid2str(msg->msg_rxpeer->lpni_nid),
788 libcfs_id2str(msg->msg_target), rc);
789 LASSERT(rc < 0); /* required by my callers */
796 lnet_is_peer_deadline_passed(struct lnet_peer_ni *lpni, time64_t now)
800 deadline = lpni->lpni_last_alive +
801 lpni->lpni_net->net_tunables.lct_peer_timeout;
804 * assume peer_ni is alive as long as we're within the configured
813 /* NB: returns 1 when alive, 0 when dead, negative when error;
814 * may drop the lnet_net_lock */
816 lnet_peer_alive_locked(struct lnet_ni *ni, struct lnet_peer_ni *lpni,
817 struct lnet_msg *msg)
819 time64_t now = ktime_get_seconds();
821 if (!lnet_peer_aliveness_enabled(lpni))
825 * If we're resending a message, let's attempt to send it even if
826 * the peer is down to fulfill our resend quota on the message
828 if (msg->msg_retry_count > 0)
831 /* try and send recovery messages irregardless */
832 if (msg->msg_recovery)
835 /* always send any responses */
836 if (lnet_msg_is_response(msg))
839 if (!lnet_is_peer_deadline_passed(lpni, now))
842 return lnet_is_peer_ni_alive(lpni);
846 * \param msg The message to be sent.
847 * \param do_send True if lnet_ni_send() should be called in this function.
848 * lnet_send() is going to lnet_net_unlock immediately after this, so
849 * it sets do_send FALSE and I don't do the unlock/send/lock bit.
851 * \retval LNET_CREDIT_OK If \a msg sent or OK to send.
852 * \retval LNET_CREDIT_WAIT If \a msg blocked for credit.
853 * \retval -EHOSTUNREACH If the next hop of the message appears dead.
854 * \retval -ECANCELED If the MD of the message has been unlinked.
857 lnet_post_send_locked(struct lnet_msg *msg, int do_send)
859 struct lnet_peer_ni *lp = msg->msg_txpeer;
860 struct lnet_ni *ni = msg->msg_txni;
861 int cpt = msg->msg_tx_cpt;
862 struct lnet_tx_queue *tq = ni->ni_tx_queues[cpt];
864 /* non-lnet_send() callers have checked before */
865 LASSERT(!do_send || msg->msg_tx_delayed);
866 LASSERT(!msg->msg_receiving);
867 LASSERT(msg->msg_tx_committed);
868 /* can't get here if we're sending to the loopback interface */
869 LASSERT(lp->lpni_nid != the_lnet.ln_loni->ni_nid);
871 /* NB 'lp' is always the next hop */
872 if ((msg->msg_target.pid & LNET_PID_USERFLAG) == 0 &&
873 lnet_peer_alive_locked(ni, lp, msg) == 0) {
874 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
875 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length +=
877 lnet_net_unlock(cpt);
879 lnet_incr_stats(&msg->msg_txpeer->lpni_stats,
881 LNET_STATS_TYPE_DROP);
883 lnet_incr_stats(&msg->msg_txni->ni_stats,
885 LNET_STATS_TYPE_DROP);
887 CNETERR("Dropping message for %s: peer not alive\n",
888 libcfs_id2str(msg->msg_target));
889 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_DROPPED;
891 lnet_finalize(msg, -EHOSTUNREACH);
894 return -EHOSTUNREACH;
897 if (msg->msg_md != NULL &&
898 (msg->msg_md->md_flags & LNET_MD_FLAG_ABORTED) != 0) {
899 lnet_net_unlock(cpt);
901 CNETERR("Aborting message for %s: LNetM[DE]Unlink() already "
902 "called on the MD/ME.\n",
903 libcfs_id2str(msg->msg_target));
905 msg->msg_no_resend = true;
906 CDEBUG(D_NET, "msg %p to %s canceled and will not be resent\n",
907 msg, libcfs_id2str(msg->msg_target));
908 lnet_finalize(msg, -ECANCELED);
915 if (!msg->msg_peertxcredit) {
916 spin_lock(&lp->lpni_lock);
917 LASSERT((lp->lpni_txcredits < 0) ==
918 !list_empty(&lp->lpni_txq));
920 msg->msg_peertxcredit = 1;
921 lp->lpni_txqnob += msg->msg_len + sizeof(struct lnet_hdr);
922 lp->lpni_txcredits--;
924 if (lp->lpni_txcredits < lp->lpni_mintxcredits)
925 lp->lpni_mintxcredits = lp->lpni_txcredits;
927 if (lp->lpni_txcredits < 0) {
928 msg->msg_tx_delayed = 1;
929 list_add_tail(&msg->msg_list, &lp->lpni_txq);
930 spin_unlock(&lp->lpni_lock);
931 return LNET_CREDIT_WAIT;
933 spin_unlock(&lp->lpni_lock);
936 if (!msg->msg_txcredit) {
937 LASSERT((tq->tq_credits < 0) ==
938 !list_empty(&tq->tq_delayed));
940 msg->msg_txcredit = 1;
942 atomic_dec(&ni->ni_tx_credits);
944 if (tq->tq_credits < tq->tq_credits_min)
945 tq->tq_credits_min = tq->tq_credits;
947 if (tq->tq_credits < 0) {
948 msg->msg_tx_delayed = 1;
949 list_add_tail(&msg->msg_list, &tq->tq_delayed);
950 return LNET_CREDIT_WAIT;
954 /* unset the tx_delay flag as we're going to send it now */
955 msg->msg_tx_delayed = 0;
958 lnet_net_unlock(cpt);
959 lnet_ni_send(ni, msg);
962 return LNET_CREDIT_OK;
966 static struct lnet_rtrbufpool *
967 lnet_msg2bufpool(struct lnet_msg *msg)
969 struct lnet_rtrbufpool *rbp;
972 LASSERT(msg->msg_rx_committed);
974 cpt = msg->msg_rx_cpt;
975 rbp = &the_lnet.ln_rtrpools[cpt][0];
977 LASSERT(msg->msg_len <= LNET_MTU);
978 while (msg->msg_len > (unsigned int)rbp->rbp_npages * PAGE_SIZE) {
980 LASSERT(rbp < &the_lnet.ln_rtrpools[cpt][LNET_NRBPOOLS]);
987 lnet_post_routed_recv_locked(struct lnet_msg *msg, int do_recv)
989 /* lnet_parse is going to lnet_net_unlock immediately after this, so it
990 * sets do_recv FALSE and I don't do the unlock/send/lock bit.
991 * I return LNET_CREDIT_WAIT if msg blocked and LNET_CREDIT_OK if
992 * received or OK to receive */
993 struct lnet_peer_ni *lpni = msg->msg_rxpeer;
994 struct lnet_peer *lp;
995 struct lnet_rtrbufpool *rbp;
996 struct lnet_rtrbuf *rb;
998 LASSERT(msg->msg_iov == NULL);
999 LASSERT(msg->msg_kiov == NULL);
1000 LASSERT(msg->msg_niov == 0);
1001 LASSERT(msg->msg_routing);
1002 LASSERT(msg->msg_receiving);
1003 LASSERT(!msg->msg_sending);
1004 LASSERT(lpni->lpni_peer_net);
1005 LASSERT(lpni->lpni_peer_net->lpn_peer);
1007 lp = lpni->lpni_peer_net->lpn_peer;
1009 /* non-lnet_parse callers only receive delayed messages */
1010 LASSERT(!do_recv || msg->msg_rx_delayed);
1012 if (!msg->msg_peerrtrcredit) {
1013 /* lpni_lock protects the credit manipulation */
1014 spin_lock(&lpni->lpni_lock);
1015 /* lp_lock protects the lp_rtrq */
1016 spin_lock(&lp->lp_lock);
1018 msg->msg_peerrtrcredit = 1;
1019 lpni->lpni_rtrcredits--;
1020 if (lpni->lpni_rtrcredits < lpni->lpni_minrtrcredits)
1021 lpni->lpni_minrtrcredits = lpni->lpni_rtrcredits;
1023 if (lpni->lpni_rtrcredits < 0) {
1024 /* must have checked eager_recv before here */
1025 LASSERT(msg->msg_rx_ready_delay);
1026 msg->msg_rx_delayed = 1;
1027 list_add_tail(&msg->msg_list, &lp->lp_rtrq);
1028 spin_unlock(&lp->lp_lock);
1029 spin_unlock(&lpni->lpni_lock);
1030 return LNET_CREDIT_WAIT;
1032 spin_unlock(&lp->lp_lock);
1033 spin_unlock(&lpni->lpni_lock);
1036 rbp = lnet_msg2bufpool(msg);
1038 if (!msg->msg_rtrcredit) {
1039 msg->msg_rtrcredit = 1;
1041 if (rbp->rbp_credits < rbp->rbp_mincredits)
1042 rbp->rbp_mincredits = rbp->rbp_credits;
1044 if (rbp->rbp_credits < 0) {
1045 /* must have checked eager_recv before here */
1046 LASSERT(msg->msg_rx_ready_delay);
1047 msg->msg_rx_delayed = 1;
1048 list_add_tail(&msg->msg_list, &rbp->rbp_msgs);
1049 return LNET_CREDIT_WAIT;
1053 LASSERT(!list_empty(&rbp->rbp_bufs));
1054 rb = list_entry(rbp->rbp_bufs.next, struct lnet_rtrbuf, rb_list);
1055 list_del(&rb->rb_list);
1057 msg->msg_niov = rbp->rbp_npages;
1058 msg->msg_kiov = &rb->rb_kiov[0];
1060 /* unset the msg-rx_delayed flag since we're receiving the message */
1061 msg->msg_rx_delayed = 0;
1064 int cpt = msg->msg_rx_cpt;
1066 lnet_net_unlock(cpt);
1067 lnet_ni_recv(msg->msg_rxni, msg->msg_private, msg, 1,
1068 0, msg->msg_len, msg->msg_len);
1071 return LNET_CREDIT_OK;
1075 lnet_return_tx_credits_locked(struct lnet_msg *msg)
1077 struct lnet_peer_ni *txpeer = msg->msg_txpeer;
1078 struct lnet_ni *txni = msg->msg_txni;
1079 struct lnet_msg *msg2;
1081 if (msg->msg_txcredit) {
1082 struct lnet_ni *ni = msg->msg_txni;
1083 struct lnet_tx_queue *tq = ni->ni_tx_queues[msg->msg_tx_cpt];
1085 /* give back NI txcredits */
1086 msg->msg_txcredit = 0;
1088 LASSERT((tq->tq_credits < 0) ==
1089 !list_empty(&tq->tq_delayed));
1092 atomic_inc(&ni->ni_tx_credits);
1093 if (tq->tq_credits <= 0) {
1094 msg2 = list_entry(tq->tq_delayed.next,
1095 struct lnet_msg, msg_list);
1096 list_del(&msg2->msg_list);
1098 LASSERT(msg2->msg_txni == ni);
1099 LASSERT(msg2->msg_tx_delayed);
1100 LASSERT(msg2->msg_tx_cpt == msg->msg_tx_cpt);
1102 (void) lnet_post_send_locked(msg2, 1);
1106 if (msg->msg_peertxcredit) {
1107 /* give back peer txcredits */
1108 msg->msg_peertxcredit = 0;
1110 spin_lock(&txpeer->lpni_lock);
1111 LASSERT((txpeer->lpni_txcredits < 0) ==
1112 !list_empty(&txpeer->lpni_txq));
1114 txpeer->lpni_txqnob -= msg->msg_len + sizeof(struct lnet_hdr);
1115 LASSERT(txpeer->lpni_txqnob >= 0);
1117 txpeer->lpni_txcredits++;
1118 if (txpeer->lpni_txcredits <= 0) {
1121 msg2 = list_entry(txpeer->lpni_txq.next,
1122 struct lnet_msg, msg_list);
1123 list_del(&msg2->msg_list);
1124 spin_unlock(&txpeer->lpni_lock);
1126 LASSERT(msg2->msg_txpeer == txpeer);
1127 LASSERT(msg2->msg_tx_delayed);
1129 msg2_cpt = msg2->msg_tx_cpt;
1132 * The msg_cpt can be different from the msg2_cpt
1133 * so we need to make sure we lock the correct cpt
1135 * Once we call lnet_post_send_locked() it is no
1136 * longer safe to access msg2, since it could've
1137 * been freed by lnet_finalize(), but we still
1138 * need to relock the correct cpt, so we cache the
1139 * msg2_cpt for the purpose of the check that
1140 * follows the call to lnet_pose_send_locked().
1142 if (msg2_cpt != msg->msg_tx_cpt) {
1143 lnet_net_unlock(msg->msg_tx_cpt);
1144 lnet_net_lock(msg2_cpt);
1146 (void) lnet_post_send_locked(msg2, 1);
1147 if (msg2_cpt != msg->msg_tx_cpt) {
1148 lnet_net_unlock(msg2_cpt);
1149 lnet_net_lock(msg->msg_tx_cpt);
1152 spin_unlock(&txpeer->lpni_lock);
1157 msg->msg_txni = NULL;
1158 lnet_ni_decref_locked(txni, msg->msg_tx_cpt);
1161 if (txpeer != NULL) {
1162 msg->msg_txpeer = NULL;
1163 lnet_peer_ni_decref_locked(txpeer);
1168 lnet_schedule_blocked_locked(struct lnet_rtrbufpool *rbp)
1170 struct lnet_msg *msg;
1172 if (list_empty(&rbp->rbp_msgs))
1174 msg = list_entry(rbp->rbp_msgs.next,
1175 struct lnet_msg, msg_list);
1176 list_del(&msg->msg_list);
1178 (void)lnet_post_routed_recv_locked(msg, 1);
1182 lnet_drop_routed_msgs_locked(struct list_head *list, int cpt)
1184 struct lnet_msg *msg;
1185 struct lnet_msg *tmp;
1187 lnet_net_unlock(cpt);
1189 list_for_each_entry_safe(msg, tmp, list, msg_list) {
1190 lnet_ni_recv(msg->msg_rxni, msg->msg_private, NULL,
1191 0, 0, 0, msg->msg_hdr.payload_length);
1192 list_del_init(&msg->msg_list);
1193 msg->msg_no_resend = true;
1194 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_ERROR;
1195 lnet_finalize(msg, -ECANCELED);
1202 lnet_return_rx_credits_locked(struct lnet_msg *msg)
1204 struct lnet_peer_ni *rxpeerni = msg->msg_rxpeer;
1205 struct lnet_peer *lp;
1206 struct lnet_ni *rxni = msg->msg_rxni;
1207 struct lnet_msg *msg2;
1209 if (msg->msg_rtrcredit) {
1210 /* give back global router credits */
1211 struct lnet_rtrbuf *rb;
1212 struct lnet_rtrbufpool *rbp;
1214 /* NB If a msg ever blocks for a buffer in rbp_msgs, it stays
1215 * there until it gets one allocated, or aborts the wait
1217 LASSERT(msg->msg_kiov != NULL);
1219 rb = list_entry(msg->msg_kiov, struct lnet_rtrbuf, rb_kiov[0]);
1222 msg->msg_kiov = NULL;
1223 msg->msg_rtrcredit = 0;
1225 LASSERT(rbp == lnet_msg2bufpool(msg));
1227 LASSERT((rbp->rbp_credits > 0) ==
1228 !list_empty(&rbp->rbp_bufs));
1230 /* If routing is now turned off, we just drop this buffer and
1231 * don't bother trying to return credits. */
1232 if (!the_lnet.ln_routing) {
1233 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1237 /* It is possible that a user has lowered the desired number of
1238 * buffers in this pool. Make sure we never put back
1239 * more buffers than the stated number. */
1240 if (unlikely(rbp->rbp_credits >= rbp->rbp_req_nbuffers)) {
1241 /* Discard this buffer so we don't have too
1243 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1244 rbp->rbp_nbuffers--;
1246 list_add(&rb->rb_list, &rbp->rbp_bufs);
1248 if (rbp->rbp_credits <= 0)
1249 lnet_schedule_blocked_locked(rbp);
1254 if (msg->msg_peerrtrcredit) {
1256 LASSERT(rxpeerni->lpni_peer_net);
1257 LASSERT(rxpeerni->lpni_peer_net->lpn_peer);
1259 lp = rxpeerni->lpni_peer_net->lpn_peer;
1261 /* give back peer router credits */
1262 msg->msg_peerrtrcredit = 0;
1264 spin_lock(&rxpeerni->lpni_lock);
1265 spin_lock(&lp->lp_lock);
1267 rxpeerni->lpni_rtrcredits++;
1269 /* drop all messages which are queued to be routed on that
1271 if (!the_lnet.ln_routing) {
1273 list_splice_init(&lp->lp_rtrq, &drop);
1274 spin_unlock(&lp->lp_lock);
1275 spin_unlock(&rxpeerni->lpni_lock);
1276 lnet_drop_routed_msgs_locked(&drop, msg->msg_rx_cpt);
1277 } else if (!list_empty(&lp->lp_rtrq)) {
1280 msg2 = list_entry(lp->lp_rtrq.next,
1281 struct lnet_msg, msg_list);
1282 list_del(&msg2->msg_list);
1283 msg2_cpt = msg2->msg_rx_cpt;
1284 spin_unlock(&lp->lp_lock);
1285 spin_unlock(&rxpeerni->lpni_lock);
1287 * messages on the lp_rtrq can be from any NID in
1288 * the peer, which means they might have different
1289 * cpts. We need to make sure we lock the right
1292 if (msg2_cpt != msg->msg_rx_cpt) {
1293 lnet_net_unlock(msg->msg_rx_cpt);
1294 lnet_net_lock(msg2_cpt);
1296 (void) lnet_post_routed_recv_locked(msg2, 1);
1297 if (msg2_cpt != msg->msg_rx_cpt) {
1298 lnet_net_unlock(msg2_cpt);
1299 lnet_net_lock(msg->msg_rx_cpt);
1302 spin_unlock(&lp->lp_lock);
1303 spin_unlock(&rxpeerni->lpni_lock);
1307 msg->msg_rxni = NULL;
1308 lnet_ni_decref_locked(rxni, msg->msg_rx_cpt);
1310 if (rxpeerni != NULL) {
1311 msg->msg_rxpeer = NULL;
1312 lnet_peer_ni_decref_locked(rxpeerni);
1317 lnet_compare_gw_lpnis(struct lnet_peer_ni *p1, struct lnet_peer_ni *p2)
1319 if (p1->lpni_txqnob < p2->lpni_txqnob)
1322 if (p1->lpni_txqnob > p2->lpni_txqnob)
1325 if (p1->lpni_txcredits > p2->lpni_txcredits)
1328 if (p1->lpni_txcredits < p2->lpni_txcredits)
1334 static struct lnet_peer_ni *
1335 lnet_select_peer_ni(struct lnet_ni *best_ni, lnet_nid_t dst_nid,
1336 struct lnet_peer *peer,
1337 struct lnet_peer_net *peer_net)
1340 * Look at the peer NIs for the destination peer that connect
1341 * to the chosen net. If a peer_ni is preferred when using the
1342 * best_ni to communicate, we use that one. If there is no
1343 * preferred peer_ni, or there are multiple preferred peer_ni,
1344 * the available transmit credits are used. If the transmit
1345 * credits are equal, we round-robin over the peer_ni.
1347 struct lnet_peer_ni *lpni = NULL;
1348 struct lnet_peer_ni *best_lpni = NULL;
1349 int best_lpni_credits = INT_MIN;
1350 bool preferred = false;
1352 int best_lpni_healthv = 0;
1355 while ((lpni = lnet_get_next_peer_ni_locked(peer, peer_net, lpni))) {
1357 * if the best_ni we've chosen aleady has this lpni
1358 * preferred, then let's use it
1361 ni_is_pref = lnet_peer_is_pref_nid_locked(lpni,
1363 CDEBUG(D_NET, "%s ni_is_pref = %d\n",
1364 libcfs_nid2str(best_ni->ni_nid), ni_is_pref);
1369 lpni_healthv = atomic_read(&lpni->lpni_healthv);
1372 CDEBUG(D_NET, "%s c:[%d, %d], s:[%d, %d]\n",
1373 libcfs_nid2str(lpni->lpni_nid),
1374 lpni->lpni_txcredits, best_lpni_credits,
1375 lpni->lpni_seq, best_lpni->lpni_seq);
1377 /* pick the healthiest peer ni */
1378 if (lpni_healthv < best_lpni_healthv) {
1380 } else if (lpni_healthv > best_lpni_healthv) {
1381 best_lpni_healthv = lpni_healthv;
1382 /* if this is a preferred peer use it */
1383 } else if (!preferred && ni_is_pref) {
1385 } else if (preferred && !ni_is_pref) {
1387 * this is not the preferred peer so let's ignore
1391 } else if (lpni->lpni_txcredits < best_lpni_credits) {
1393 * We already have a peer that has more credits
1394 * available than this one. No need to consider
1395 * this peer further.
1398 } else if (lpni->lpni_txcredits == best_lpni_credits) {
1400 * The best peer found so far and the current peer
1401 * have the same number of available credits let's
1402 * make sure to select between them using Round
1406 if (best_lpni->lpni_seq <= lpni->lpni_seq)
1412 best_lpni_credits = lpni->lpni_txcredits;
1415 /* if we still can't find a peer ni then we can't reach it */
1417 __u32 net_id = (peer_net) ? peer_net->lpn_net_id :
1418 LNET_NIDNET(dst_nid);
1419 CDEBUG(D_NET, "no peer_ni found on peer net %s\n",
1420 libcfs_net2str(net_id));
1424 CDEBUG(D_NET, "sd_best_lpni = %s\n",
1425 libcfs_nid2str(best_lpni->lpni_nid));
1431 * Prerequisite: the best_ni should already be set in the sd
1433 static inline struct lnet_peer_ni *
1434 lnet_find_best_lpni_on_net(struct lnet_ni *lni, lnet_nid_t dst_nid,
1435 struct lnet_peer *peer, __u32 net_id)
1437 struct lnet_peer_net *peer_net;
1440 * The gateway is Multi-Rail capable so now we must select the
1443 peer_net = lnet_peer_get_net_locked(peer, net_id);
1445 return lnet_select_peer_ni(lni, dst_nid, peer, peer_net);
1448 /* Compare route priorities and hop counts */
1450 lnet_compare_routes(struct lnet_route *r1, struct lnet_route *r2)
1452 int r1_hops = (r1->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r1->lr_hops;
1453 int r2_hops = (r2->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r2->lr_hops;
1455 if (r1->lr_priority < r2->lr_priority)
1458 if (r1->lr_priority > r2->lr_priority)
1461 if (r1_hops < r2_hops)
1464 if (r1_hops > r2_hops)
1470 static struct lnet_route *
1471 lnet_find_route_locked(struct lnet_remotenet *rnet, __u32 src_net,
1472 struct lnet_route **prev_route,
1473 struct lnet_peer_ni **gwni)
1475 struct lnet_peer_ni *lpni, *best_gw_ni = NULL;
1476 struct lnet_route *best_route;
1477 struct lnet_route *last_route;
1478 struct lnet_route *route;
1481 best_route = last_route = NULL;
1482 list_for_each_entry(route, &rnet->lrn_routes, lr_list) {
1483 if (!lnet_is_route_alive(route))
1487 * Restrict the selection of the router NI on the src_net
1488 * provided. If the src_net is LNET_NID_ANY, then select
1489 * the best interface available.
1492 lpni = lnet_find_best_lpni_on_net(NULL, LNET_NID_ANY,
1496 best_route = last_route = route;
1499 CERROR("Gateway %s does not have a peer NI on net %s\n",
1500 libcfs_nid2str(route->lr_gateway->lp_primary_nid),
1501 libcfs_net2str(src_net));
1506 /* no protection on below fields, but it's harmless */
1507 if (last_route->lr_seq - route->lr_seq < 0)
1510 rc = lnet_compare_routes(route, best_route);
1514 lpni = lnet_find_best_lpni_on_net(NULL, LNET_NID_ANY,
1518 CERROR("Gateway %s does not have a peer NI on net %s\n",
1519 libcfs_nid2str(route->lr_gateway->lp_primary_nid),
1520 libcfs_net2str(src_net));
1530 rc = lnet_compare_gw_lpnis(lpni, best_gw_ni);
1534 if (rc == 1 || route->lr_seq <= best_route->lr_seq) {
1541 *prev_route = last_route;
1547 static struct lnet_ni *
1548 lnet_get_best_ni(struct lnet_net *local_net, struct lnet_ni *best_ni,
1549 struct lnet_peer *peer, struct lnet_peer_net *peer_net,
1552 struct lnet_ni *ni = NULL;
1553 unsigned int shortest_distance;
1558 * If there is no peer_ni that we can send to on this network,
1559 * then there is no point in looking for a new best_ni here.
1561 if (!lnet_get_next_peer_ni_locked(peer, peer_net, NULL))
1564 if (best_ni == NULL) {
1565 shortest_distance = UINT_MAX;
1566 best_credits = INT_MIN;
1569 shortest_distance = cfs_cpt_distance(lnet_cpt_table(), md_cpt,
1570 best_ni->ni_dev_cpt);
1571 best_credits = atomic_read(&best_ni->ni_tx_credits);
1572 best_healthv = atomic_read(&best_ni->ni_healthv);
1575 while ((ni = lnet_get_next_ni_locked(local_net, ni))) {
1576 unsigned int distance;
1581 ni_credits = atomic_read(&ni->ni_tx_credits);
1582 ni_healthv = atomic_read(&ni->ni_healthv);
1583 ni_fatal = atomic_read(&ni->ni_fatal_error_on);
1586 * calculate the distance from the CPT on which
1587 * the message memory is allocated to the CPT of
1588 * the NI's physical device
1590 distance = cfs_cpt_distance(lnet_cpt_table(),
1594 CDEBUG(D_NET, "compare ni %s [c:%d, d:%d, s:%d] with best_ni %s [c:%d, d:%d, s:%d]\n",
1595 libcfs_nid2str(ni->ni_nid), ni_credits, distance,
1596 ni->ni_seq, (best_ni) ? libcfs_nid2str(best_ni->ni_nid)
1597 : "not seleced", best_credits, shortest_distance,
1598 (best_ni) ? best_ni->ni_seq : 0);
1601 * All distances smaller than the NUMA range
1602 * are treated equally.
1604 if (distance < lnet_numa_range)
1605 distance = lnet_numa_range;
1608 * Select on health, shorter distance, available
1609 * credits, then round-robin.
1613 } else if (ni_healthv < best_healthv) {
1615 } else if (ni_healthv > best_healthv) {
1616 best_healthv = ni_healthv;
1618 * If we're going to prefer this ni because it's
1619 * the healthiest, then we should set the
1620 * shortest_distance in the algorithm in case
1621 * there are multiple NIs with the same health but
1622 * different distances.
1624 if (distance < shortest_distance)
1625 shortest_distance = distance;
1626 } else if (distance > shortest_distance) {
1628 } else if (distance < shortest_distance) {
1629 shortest_distance = distance;
1630 } else if (ni_credits < best_credits) {
1632 } else if (ni_credits == best_credits) {
1633 if (best_ni && best_ni->ni_seq <= ni->ni_seq)
1637 best_credits = ni_credits;
1640 CDEBUG(D_NET, "selected best_ni %s\n",
1641 (best_ni) ? libcfs_nid2str(best_ni->ni_nid) : "no selection");
1647 * Traffic to the LNET_RESERVED_PORTAL may not trigger peer discovery,
1648 * because such traffic is required to perform discovery. We therefore
1649 * exclude all GET and PUT on that portal. We also exclude all ACK and
1650 * REPLY traffic, but that is because the portal is not tracked in the
1651 * message structure for these message types. We could restrict this
1652 * further by also checking for LNET_PROTO_PING_MATCHBITS.
1655 lnet_msg_discovery(struct lnet_msg *msg)
1657 if (msg->msg_type == LNET_MSG_PUT) {
1658 if (msg->msg_hdr.msg.put.ptl_index != LNET_RESERVED_PORTAL)
1660 } else if (msg->msg_type == LNET_MSG_GET) {
1661 if (msg->msg_hdr.msg.get.ptl_index != LNET_RESERVED_PORTAL)
1667 #define SRC_SPEC 0x0001
1668 #define SRC_ANY 0x0002
1669 #define LOCAL_DST 0x0004
1670 #define REMOTE_DST 0x0008
1671 #define MR_DST 0x0010
1672 #define NMR_DST 0x0020
1673 #define SND_RESP 0x0040
1675 /* The following to defines are used for return codes */
1676 #define REPEAT_SEND 0x1000
1677 #define PASS_THROUGH 0x2000
1679 /* The different cases lnet_select pathway needs to handle */
1680 #define SRC_SPEC_LOCAL_MR_DST (SRC_SPEC | LOCAL_DST | MR_DST)
1681 #define SRC_SPEC_ROUTER_MR_DST (SRC_SPEC | REMOTE_DST | MR_DST)
1682 #define SRC_SPEC_LOCAL_NMR_DST (SRC_SPEC | LOCAL_DST | NMR_DST)
1683 #define SRC_SPEC_ROUTER_NMR_DST (SRC_SPEC | REMOTE_DST | NMR_DST)
1684 #define SRC_ANY_LOCAL_MR_DST (SRC_ANY | LOCAL_DST | MR_DST)
1685 #define SRC_ANY_ROUTER_MR_DST (SRC_ANY | REMOTE_DST | MR_DST)
1686 #define SRC_ANY_LOCAL_NMR_DST (SRC_ANY | LOCAL_DST | NMR_DST)
1687 #define SRC_ANY_ROUTER_NMR_DST (SRC_ANY | REMOTE_DST | NMR_DST)
1690 lnet_handle_lo_send(struct lnet_send_data *sd)
1692 struct lnet_msg *msg = sd->sd_msg;
1693 int cpt = sd->sd_cpt;
1695 /* No send credit hassles with LOLND */
1696 lnet_ni_addref_locked(the_lnet.ln_loni, cpt);
1697 msg->msg_hdr.dest_nid = cpu_to_le64(the_lnet.ln_loni->ni_nid);
1698 if (!msg->msg_routing)
1699 msg->msg_hdr.src_nid =
1700 cpu_to_le64(the_lnet.ln_loni->ni_nid);
1701 msg->msg_target.nid = the_lnet.ln_loni->ni_nid;
1702 lnet_msg_commit(msg, cpt);
1703 msg->msg_txni = the_lnet.ln_loni;
1705 return LNET_CREDIT_OK;
1709 lnet_handle_send(struct lnet_send_data *sd)
1711 struct lnet_ni *best_ni = sd->sd_best_ni;
1712 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
1713 struct lnet_peer_ni *final_dst_lpni = sd->sd_final_dst_lpni;
1714 struct lnet_msg *msg = sd->sd_msg;
1716 __u32 send_case = sd->sd_send_case;
1718 __u32 routing = send_case & REMOTE_DST;
1719 struct lnet_rsp_tracker *rspt;
1722 * Increment sequence number of the selected peer so that we
1723 * pick the next one in Round Robin.
1725 best_lpni->lpni_seq++;
1728 * grab a reference on the peer_ni so it sticks around even if
1729 * we need to drop and relock the lnet_net_lock below.
1731 lnet_peer_ni_addref_locked(best_lpni);
1734 * Use lnet_cpt_of_nid() to determine the CPT used to commit the
1735 * message. This ensures that we get a CPT that is correct for
1736 * the NI when the NI has been restricted to a subset of all CPTs.
1737 * If the selected CPT differs from the one currently locked, we
1738 * must unlock and relock the lnet_net_lock(), and then check whether
1739 * the configuration has changed. We don't have a hold on the best_ni
1740 * yet, and it may have vanished.
1742 cpt2 = lnet_cpt_of_nid_locked(best_lpni->lpni_nid, best_ni);
1743 if (sd->sd_cpt != cpt2) {
1744 __u32 seq = lnet_get_dlc_seq_locked();
1745 lnet_net_unlock(sd->sd_cpt);
1747 lnet_net_lock(sd->sd_cpt);
1748 if (seq != lnet_get_dlc_seq_locked()) {
1749 lnet_peer_ni_decref_locked(best_lpni);
1755 * store the best_lpni in the message right away to avoid having
1756 * to do the same operation under different conditions
1758 msg->msg_txpeer = best_lpni;
1759 msg->msg_txni = best_ni;
1762 * grab a reference for the best_ni since now it's in use in this
1763 * send. The reference will be dropped in lnet_finalize()
1765 lnet_ni_addref_locked(msg->msg_txni, sd->sd_cpt);
1768 * Always set the target.nid to the best peer picked. Either the
1769 * NID will be one of the peer NIDs selected, or the same NID as
1770 * what was originally set in the target or it will be the NID of
1771 * a router if this message should be routed
1773 msg->msg_target.nid = msg->msg_txpeer->lpni_nid;
1776 * lnet_msg_commit assigns the correct cpt to the message, which
1777 * is used to decrement the correct refcount on the ni when it's
1778 * time to return the credits
1780 lnet_msg_commit(msg, sd->sd_cpt);
1783 * If we are routing the message then we keep the src_nid that was
1784 * set by the originator. If we are not routing then we are the
1785 * originator and set it here.
1787 if (!msg->msg_routing)
1788 msg->msg_hdr.src_nid = cpu_to_le64(msg->msg_txni->ni_nid);
1791 msg->msg_target_is_router = 1;
1792 msg->msg_target.pid = LNET_PID_LUSTRE;
1794 * since we're routing we want to ensure that the
1795 * msg_hdr.dest_nid is set to the final destination. When
1796 * the router receives this message it knows how to route
1799 * final_dst_lpni is set at the beginning of the
1800 * lnet_select_pathway() function and is never changed.
1801 * It's safe to use it here.
1803 msg->msg_hdr.dest_nid = cpu_to_le64(final_dst_lpni->lpni_nid);
1806 * if we're not routing set the dest_nid to the best peer
1807 * ni NID that we picked earlier in the algorithm.
1809 msg->msg_hdr.dest_nid = cpu_to_le64(msg->msg_txpeer->lpni_nid);
1813 * if we have response tracker block update it with the next hop
1817 rspt = msg->msg_md->md_rspt_ptr;
1819 rspt->rspt_next_hop_nid = msg->msg_txpeer->lpni_nid;
1820 CDEBUG(D_NET, "rspt_next_hop_nid = %s\n",
1821 libcfs_nid2str(rspt->rspt_next_hop_nid));
1825 rc = lnet_post_send_locked(msg, 0);
1828 CDEBUG(D_NET, "TRACE: %s(%s:%s) -> %s(%s:%s) %s : %s try# %d\n",
1829 libcfs_nid2str(msg->msg_hdr.src_nid),
1830 libcfs_nid2str(msg->msg_txni->ni_nid),
1831 libcfs_nid2str(sd->sd_src_nid),
1832 libcfs_nid2str(msg->msg_hdr.dest_nid),
1833 libcfs_nid2str(sd->sd_dst_nid),
1834 libcfs_nid2str(msg->msg_txpeer->lpni_nid),
1835 libcfs_nid2str(sd->sd_rtr_nid),
1836 lnet_msgtyp2str(msg->msg_type), msg->msg_retry_count);
1842 lnet_set_non_mr_pref_nid(struct lnet_peer_ni *lpni, struct lnet_ni *lni,
1843 struct lnet_msg *msg)
1845 if (!lnet_msg_is_response(msg) && lpni->lpni_pref_nnids == 0) {
1846 CDEBUG(D_NET, "Setting preferred local NID %s on NMR peer %s\n",
1847 libcfs_nid2str(lni->ni_nid),
1848 libcfs_nid2str(lpni->lpni_nid));
1849 lnet_peer_ni_set_non_mr_pref_nid(lpni, lni->ni_nid);
1858 * use the source and destination NIDs as the pathway
1861 lnet_handle_spec_local_nmr_dst(struct lnet_send_data *sd)
1863 /* the destination lpni is set before we get here. */
1866 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
1867 if (!sd->sd_best_ni) {
1868 CERROR("Can't send to %s: src %s is not a "
1869 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
1870 libcfs_nid2str(sd->sd_src_nid));
1874 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
1876 return lnet_handle_send(sd);
1884 * Don't run the selection algorithm on the peer NIs. By specifying the
1885 * local NID, we're also saying that we should always use the destination NID
1886 * provided. This handles the case where we should be using the same
1887 * destination NID for the all the messages which belong to the same RPC
1891 lnet_handle_spec_local_mr_dst(struct lnet_send_data *sd)
1893 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
1894 if (!sd->sd_best_ni) {
1895 CERROR("Can't send to %s: src %s is not a "
1896 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
1897 libcfs_nid2str(sd->sd_src_nid));
1901 if (sd->sd_best_lpni &&
1902 sd->sd_best_lpni->lpni_nid == the_lnet.ln_loni->ni_nid)
1903 return lnet_handle_lo_send(sd);
1904 else if (sd->sd_best_lpni)
1905 return lnet_handle_send(sd);
1907 CERROR("can't send to %s. no NI on %s\n",
1908 libcfs_nid2str(sd->sd_dst_nid),
1909 libcfs_net2str(sd->sd_best_ni->ni_net->net_id));
1911 return -EHOSTUNREACH;
1915 lnet_find_best_ni_on_spec_net(struct lnet_ni *cur_best_ni,
1916 struct lnet_peer *peer,
1917 struct lnet_peer_net *peer_net,
1921 struct lnet_net *local_net;
1922 struct lnet_ni *best_ni;
1924 local_net = lnet_get_net_locked(peer_net->lpn_net_id);
1929 * Iterate through the NIs in this local Net and select
1930 * the NI to send from. The selection is determined by
1931 * these 3 criterion in the following priority:
1933 * 2. NI available credits
1936 best_ni = lnet_get_best_ni(local_net, cur_best_ni,
1937 peer, peer_net, cpt);
1939 if (incr_seq && best_ni)
1946 lnet_initiate_peer_discovery(struct lnet_peer_ni *lpni, struct lnet_msg *msg,
1949 struct lnet_peer *peer;
1952 lnet_peer_ni_addref_locked(lpni);
1954 peer = lpni->lpni_peer_net->lpn_peer;
1956 if (lnet_peer_gw_discovery(peer)) {
1957 lnet_peer_ni_decref_locked(lpni);
1961 if (!lnet_msg_discovery(msg) || lnet_peer_is_uptodate(peer)) {
1962 lnet_peer_ni_decref_locked(lpni);
1966 rc = lnet_discover_peer_locked(lpni, cpt, false);
1968 lnet_peer_ni_decref_locked(lpni);
1971 /* The peer may have changed. */
1972 peer = lpni->lpni_peer_net->lpn_peer;
1973 spin_lock(&peer->lp_lock);
1974 if (lnet_peer_is_uptodate_locked(peer)) {
1975 spin_unlock(&peer->lp_lock);
1976 lnet_peer_ni_decref_locked(lpni);
1979 /* queue message and return */
1980 msg->msg_sending = 0;
1981 msg->msg_txpeer = NULL;
1982 list_add_tail(&msg->msg_list, &peer->lp_dc_pendq);
1983 spin_unlock(&peer->lp_lock);
1985 lnet_peer_ni_decref_locked(lpni);
1987 CDEBUG(D_NET, "msg %p delayed. %s pending discovery\n",
1988 msg, libcfs_nid2str(peer->lp_primary_nid));
1990 return LNET_DC_WAIT;
1994 lnet_handle_find_routed_path(struct lnet_send_data *sd,
1996 struct lnet_peer_ni **gw_lpni,
1997 struct lnet_peer **gw_peer)
2001 struct lnet_peer *gw;
2002 struct lnet_peer *lp;
2003 struct lnet_peer_net *lpn;
2004 struct lnet_peer_net *best_lpn = NULL;
2005 struct lnet_remotenet *rnet, *best_rnet = NULL;
2006 struct lnet_route *best_route = NULL;
2007 struct lnet_route *last_route = NULL;
2008 struct lnet_peer_ni *lpni = NULL;
2009 struct lnet_peer_ni *gwni = NULL;
2010 lnet_nid_t src_nid = sd->sd_src_nid;
2012 /* If a router nid was specified then we are replying to a GET or
2013 * sending an ACK. In this case we use the gateway associated with the
2014 * specified router nid.
2016 if (sd->sd_rtr_nid != LNET_NID_ANY) {
2017 gwni = lnet_find_peer_ni_locked(sd->sd_rtr_nid);
2019 CERROR("No peer NI for gateway %s\n",
2020 libcfs_nid2str(sd->sd_rtr_nid));
2021 return -EHOSTUNREACH;
2023 gw = gwni->lpni_peer_net->lpn_peer;
2024 lnet_peer_ni_decref_locked(gwni);
2025 local_lnet = LNET_NIDNET(sd->sd_rtr_nid);
2027 /* we've already looked up the initial lpni using dst_nid */
2028 lpni = sd->sd_best_lpni;
2029 /* the peer tree must be in existence */
2030 LASSERT(lpni && lpni->lpni_peer_net &&
2031 lpni->lpni_peer_net->lpn_peer);
2032 lp = lpni->lpni_peer_net->lpn_peer;
2034 list_for_each_entry(lpn, &lp->lp_peer_nets, lpn_peer_nets) {
2035 /* is this remote network reachable? */
2036 rnet = lnet_find_rnet_locked(lpn->lpn_net_id);
2045 if (best_lpn->lpn_seq <= lpn->lpn_seq)
2053 CERROR("peer %s has no available nets\n",
2054 libcfs_nid2str(sd->sd_dst_nid));
2055 return -EHOSTUNREACH;
2058 sd->sd_best_lpni = lnet_find_best_lpni_on_net(sd->sd_best_ni,
2061 best_lpn->lpn_net_id);
2062 if (!sd->sd_best_lpni) {
2063 CERROR("peer %s down\n",
2064 libcfs_nid2str(sd->sd_dst_nid));
2065 return -EHOSTUNREACH;
2068 best_route = lnet_find_route_locked(best_rnet,
2069 LNET_NIDNET(src_nid),
2070 &last_route, &gwni);
2072 CERROR("no route to %s from %s\n",
2073 libcfs_nid2str(dst_nid),
2074 libcfs_nid2str(src_nid));
2075 return -EHOSTUNREACH;
2079 CERROR("Internal Error. Route expected to %s from %s\n",
2080 libcfs_nid2str(dst_nid),
2081 libcfs_nid2str(src_nid));
2085 gw = best_route->lr_gateway;
2086 LASSERT(gw == gwni->lpni_peer_net->lpn_peer);
2087 local_lnet = best_route->lr_lnet;
2092 * Discover this gateway if it hasn't already been discovered.
2093 * This means we might delay the message until discovery has
2096 sd->sd_msg->msg_src_nid_param = sd->sd_src_nid;
2097 rc = lnet_initiate_peer_discovery(gwni, sd->sd_msg, sd->sd_cpt);
2101 if (!sd->sd_best_ni)
2102 sd->sd_best_ni = lnet_find_best_ni_on_spec_net(NULL, gw,
2103 lnet_peer_get_net_locked(gw,
2108 if (!sd->sd_best_ni) {
2109 CERROR("Internal Error. Expected local ni on %s but non found :%s\n",
2110 libcfs_net2str(local_lnet),
2111 libcfs_nid2str(sd->sd_src_nid));
2119 * increment the sequence numbers since now we're sure we're
2120 * going to use this path
2122 if (sd->sd_rtr_nid == LNET_NID_ANY) {
2123 LASSERT(best_route && last_route);
2124 best_route->lr_seq = last_route->lr_seq + 1;
2125 best_lpn->lpn_seq++;
2136 * Remote destination
2137 * Non-MR destination
2141 * Remote destination
2144 * The handling of these two cases is similar. Even though the destination
2145 * can be MR or non-MR, we'll deal directly with the router.
2148 lnet_handle_spec_router_dst(struct lnet_send_data *sd)
2151 struct lnet_peer_ni *gw_lpni = NULL;
2152 struct lnet_peer *gw_peer = NULL;
2155 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
2156 if (!sd->sd_best_ni) {
2157 CERROR("Can't send to %s: src %s is not a "
2158 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
2159 libcfs_nid2str(sd->sd_src_nid));
2163 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2168 if (sd->sd_send_case & NMR_DST)
2170 * since the final destination is non-MR let's set its preferred
2171 * NID before we send
2173 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni,
2177 * We're going to send to the gw found so let's set its
2180 sd->sd_peer = gw_peer;
2181 sd->sd_best_lpni = gw_lpni;
2183 return lnet_handle_send(sd);
2187 lnet_find_best_ni_on_local_net(struct lnet_peer *peer, int md_cpt,
2190 struct lnet_peer_net *peer_net = NULL;
2191 struct lnet_ni *best_ni = NULL;
2192 int lpn_healthv = 0;
2195 * The peer can have multiple interfaces, some of them can be on
2196 * the local network and others on a routed network. We should
2197 * prefer the local network. However if the local network is not
2198 * available then we need to try the routed network
2201 /* go through all the peer nets and find the best_ni */
2202 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_peer_nets) {
2204 * The peer's list of nets can contain non-local nets. We
2205 * want to only examine the local ones.
2207 if (!lnet_get_net_locked(peer_net->lpn_net_id))
2210 /* always select the lpn with the best health */
2211 if (lpn_healthv <= peer_net->lpn_healthv)
2212 lpn_healthv = peer_net->lpn_healthv;
2216 best_ni = lnet_find_best_ni_on_spec_net(best_ni, peer, peer_net,
2220 * if this is a discovery message and lp_disc_net_id is
2221 * specified then use that net to send the discovery on.
2223 if (peer->lp_disc_net_id == peer_net->lpn_net_id &&
2229 /* increment sequence number so we can round robin */
2235 static struct lnet_ni *
2236 lnet_find_existing_preferred_best_ni(struct lnet_peer_ni *lpni, int cpt)
2238 struct lnet_ni *best_ni = NULL;
2239 struct lnet_peer_net *peer_net = lpni->lpni_peer_net;
2240 struct lnet_peer_ni *lpni_entry;
2243 * We must use a consistent source address when sending to a
2244 * non-MR peer. However, a non-MR peer can have multiple NIDs
2245 * on multiple networks, and we may even need to talk to this
2246 * peer on multiple networks -- certain types of
2247 * load-balancing configuration do this.
2249 * So we need to pick the NI the peer prefers for this
2250 * particular network.
2253 list_for_each_entry(lpni_entry, &peer_net->lpn_peer_nis,
2255 if (lpni_entry->lpni_pref_nnids == 0)
2257 LASSERT(lpni_entry->lpni_pref_nnids == 1);
2258 best_ni = lnet_nid2ni_locked(lpni_entry->lpni_pref.nid, cpt);
2265 /* Prerequisite: sd->sd_peer and sd->sd_best_lpni should be set */
2267 lnet_select_preferred_best_ni(struct lnet_send_data *sd)
2269 struct lnet_ni *best_ni = NULL;
2270 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
2273 * We must use a consistent source address when sending to a
2274 * non-MR peer. However, a non-MR peer can have multiple NIDs
2275 * on multiple networks, and we may even need to talk to this
2276 * peer on multiple networks -- certain types of
2277 * load-balancing configuration do this.
2279 * So we need to pick the NI the peer prefers for this
2280 * particular network.
2283 best_ni = lnet_find_existing_preferred_best_ni(sd->sd_best_lpni,
2286 /* if best_ni is still not set just pick one */
2289 lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2290 sd->sd_best_lpni->lpni_peer_net,
2291 sd->sd_md_cpt, true);
2292 /* If there is no best_ni we don't have a route */
2294 CERROR("no path to %s from net %s\n",
2295 libcfs_nid2str(best_lpni->lpni_nid),
2296 libcfs_net2str(best_lpni->lpni_net->net_id));
2297 return -EHOSTUNREACH;
2301 sd->sd_best_ni = best_ni;
2303 /* Set preferred NI if necessary. */
2304 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
2311 * Source not specified
2315 * always use the same source NID for NMR peers
2316 * If we've talked to that peer before then we already have a preferred
2317 * source NI associated with it. Otherwise, we select a preferred local NI
2318 * and store it in the peer
2321 lnet_handle_any_local_nmr_dst(struct lnet_send_data *sd)
2325 /* sd->sd_best_lpni is already set to the final destination */
2328 * At this point we should've created the peer ni and peer. If we
2329 * can't find it, then something went wrong. Instead of assert
2330 * output a relevant message and fail the send
2332 if (!sd->sd_best_lpni) {
2333 CERROR("Internal fault. Unable to send msg %s to %s. "
2335 lnet_msgtyp2str(sd->sd_msg->msg_type),
2336 libcfs_nid2str(sd->sd_dst_nid));
2340 rc = lnet_select_preferred_best_ni(sd);
2342 rc = lnet_handle_send(sd);
2348 lnet_handle_any_mr_dsta(struct lnet_send_data *sd)
2351 * NOTE we've already handled the remote peer case. So we only
2352 * need to worry about the local case here.
2354 * if we're sending a response, ACK or reply, we need to send it
2355 * to the destination NID given to us. At this point we already
2356 * have the peer_ni we're suppose to send to, so just find the
2357 * best_ni on the peer net and use that. Since we're sending to an
2358 * MR peer then we can just run the selection algorithm on our
2359 * local NIs and pick the best one.
2361 if (sd->sd_send_case & SND_RESP) {
2363 lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2364 sd->sd_best_lpni->lpni_peer_net,
2365 sd->sd_md_cpt, true);
2367 if (!sd->sd_best_ni) {
2369 * We're not going to deal with not able to send
2370 * a response to the provided final destination
2372 CERROR("Can't send response to %s. "
2373 "No local NI available\n",
2374 libcfs_nid2str(sd->sd_dst_nid));
2375 return -EHOSTUNREACH;
2378 return lnet_handle_send(sd);
2382 * If we get here that means we're sending a fresh request, PUT or
2383 * GET, so we need to run our standard selection algorithm.
2384 * First find the best local interface that's on any of the peer's
2387 sd->sd_best_ni = lnet_find_best_ni_on_local_net(sd->sd_peer,
2389 lnet_msg_discovery(sd->sd_msg));
2390 if (sd->sd_best_ni) {
2392 lnet_find_best_lpni_on_net(sd->sd_best_ni, sd->sd_dst_nid,
2394 sd->sd_best_ni->ni_net->net_id);
2397 * if we're successful in selecting a peer_ni on the local
2398 * network, then send to it. Otherwise fall through and
2399 * try and see if we can reach it over another routed
2402 if (sd->sd_best_lpni &&
2403 sd->sd_best_lpni->lpni_nid == the_lnet.ln_loni->ni_nid) {
2405 * in case we initially started with a routed
2406 * destination, let's reset to local
2408 sd->sd_send_case &= ~REMOTE_DST;
2409 sd->sd_send_case |= LOCAL_DST;
2410 return lnet_handle_lo_send(sd);
2411 } else if (sd->sd_best_lpni) {
2413 * in case we initially started with a routed
2414 * destination, let's reset to local
2416 sd->sd_send_case &= ~REMOTE_DST;
2417 sd->sd_send_case |= LOCAL_DST;
2418 return lnet_handle_send(sd);
2421 CERROR("Internal Error. Expected to have a best_lpni: "
2423 libcfs_nid2str(sd->sd_src_nid),
2424 libcfs_nid2str(sd->sd_dst_nid));
2430 * Peer doesn't have a local network. Let's see if there is
2431 * a remote network we can reach it on.
2433 return PASS_THROUGH;
2438 * Source NID not specified
2443 * Source NID not speified
2444 * Remote destination
2447 * In both of these cases if we're sending a response, ACK or REPLY, then
2448 * we need to send to the destination NID provided.
2450 * In the remote case let's deal with MR routers.
2455 lnet_handle_any_mr_dst(struct lnet_send_data *sd)
2458 struct lnet_peer *gw_peer = NULL;
2459 struct lnet_peer_ni *gw_lpni = NULL;
2462 * handle sending a response to a remote peer here so we don't
2463 * have to worry about it if we hit lnet_handle_any_mr_dsta()
2465 if (sd->sd_send_case & REMOTE_DST &&
2466 sd->sd_send_case & SND_RESP) {
2467 struct lnet_peer_ni *gw;
2468 struct lnet_peer *gw_peer;
2470 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw,
2473 CERROR("Can't send response to %s. "
2474 "No route available\n",
2475 libcfs_nid2str(sd->sd_dst_nid));
2476 return -EHOSTUNREACH;
2477 } else if (rc > 0) {
2481 sd->sd_best_lpni = gw;
2482 sd->sd_peer = gw_peer;
2484 return lnet_handle_send(sd);
2488 * Even though the NID for the peer might not be on a local network,
2489 * since the peer is MR there could be other interfaces on the
2490 * local network. In that case we'd still like to prefer the local
2491 * network over the routed network. If we're unable to do that
2492 * then we select the best router among the different routed networks,
2493 * and if the router is MR then we can deal with it as such.
2495 rc = lnet_handle_any_mr_dsta(sd);
2496 if (rc != PASS_THROUGH)
2500 * Now that we must route to the destination, we must consider the
2501 * MR case, where the destination has multiple interfaces, some of
2502 * which we can route to and others we do not. For this reason we
2503 * need to select the destination which we can route to and if
2504 * there are multiple, we need to round robin.
2506 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2511 sd->sd_send_case &= ~LOCAL_DST;
2512 sd->sd_send_case |= REMOTE_DST;
2514 sd->sd_peer = gw_peer;
2515 sd->sd_best_lpni = gw_lpni;
2517 return lnet_handle_send(sd);
2521 * Source not specified
2522 * Remote destination
2525 * Must send to the specified peer NID using the same source NID that
2526 * we've used before. If it's the first time to talk to that peer then
2527 * find the source NI and assign it as preferred to that peer
2530 lnet_handle_any_router_nmr_dst(struct lnet_send_data *sd)
2533 struct lnet_peer_ni *gw_lpni = NULL;
2534 struct lnet_peer *gw_peer = NULL;
2537 * Let's see if we have a preferred NI to talk to this NMR peer
2539 sd->sd_best_ni = lnet_find_existing_preferred_best_ni(sd->sd_best_lpni,
2543 * find the router and that'll find the best NI if we didn't find
2546 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2552 * set the best_ni we've chosen as the preferred one for
2555 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
2557 /* we'll be sending to the gw */
2558 sd->sd_best_lpni = gw_lpni;
2559 sd->sd_peer = gw_peer;
2561 return lnet_handle_send(sd);
2565 lnet_handle_send_case_locked(struct lnet_send_data *sd)
2568 * turn off the SND_RESP bit.
2569 * It will be checked in the case handling
2571 __u32 send_case = sd->sd_send_case &= ~SND_RESP ;
2573 CDEBUG(D_NET, "Source %s%s to %s %s %s destination\n",
2574 (send_case & SRC_SPEC) ? "Specified: " : "ANY",
2575 (send_case & SRC_SPEC) ? libcfs_nid2str(sd->sd_src_nid) : "",
2576 (send_case & MR_DST) ? "MR: " : "NMR: ",
2577 libcfs_nid2str(sd->sd_dst_nid),
2578 (send_case & LOCAL_DST) ? "local" : "routed");
2580 switch (send_case) {
2582 * For all cases where the source is specified, we should always
2583 * use the destination NID, whether it's an MR destination or not,
2584 * since we're continuing a series of related messages for the
2587 case SRC_SPEC_LOCAL_NMR_DST:
2588 return lnet_handle_spec_local_nmr_dst(sd);
2589 case SRC_SPEC_LOCAL_MR_DST:
2590 return lnet_handle_spec_local_mr_dst(sd);
2591 case SRC_SPEC_ROUTER_NMR_DST:
2592 case SRC_SPEC_ROUTER_MR_DST:
2593 return lnet_handle_spec_router_dst(sd);
2594 case SRC_ANY_LOCAL_NMR_DST:
2595 return lnet_handle_any_local_nmr_dst(sd);
2596 case SRC_ANY_LOCAL_MR_DST:
2597 case SRC_ANY_ROUTER_MR_DST:
2598 return lnet_handle_any_mr_dst(sd);
2599 case SRC_ANY_ROUTER_NMR_DST:
2600 return lnet_handle_any_router_nmr_dst(sd);
2602 CERROR("Unknown send case\n");
2608 lnet_select_pathway(lnet_nid_t src_nid, lnet_nid_t dst_nid,
2609 struct lnet_msg *msg, lnet_nid_t rtr_nid)
2611 struct lnet_peer_ni *lpni;
2612 struct lnet_peer *peer;
2613 struct lnet_send_data send_data;
2616 __u32 send_case = 0;
2618 memset(&send_data, 0, sizeof(send_data));
2621 * get an initial CPT to use for locking. The idea here is not to
2622 * serialize the calls to select_pathway, so that as many
2623 * operations can run concurrently as possible. To do that we use
2624 * the CPT where this call is being executed. Later on when we
2625 * determine the CPT to use in lnet_message_commit, we switch the
2626 * lock and check if there was any configuration change. If none,
2627 * then we proceed, if there is, then we restart the operation.
2629 cpt = lnet_net_lock_current();
2631 md_cpt = lnet_cpt_of_md(msg->msg_md, msg->msg_offset);
2632 if (md_cpt == CFS_CPT_ANY)
2638 * If we're being asked to send to the loopback interface, there
2639 * is no need to go through any selection. We can just shortcut
2640 * the entire process and send over lolnd
2642 send_data.sd_msg = msg;
2643 send_data.sd_cpt = cpt;
2644 if (LNET_NETTYP(LNET_NIDNET(dst_nid)) == LOLND) {
2645 rc = lnet_handle_lo_send(&send_data);
2646 lnet_net_unlock(cpt);
2651 * find an existing peer_ni, or create one and mark it as having been
2652 * created due to network traffic. This call will create the
2653 * peer->peer_net->peer_ni tree.
2655 lpni = lnet_nid2peerni_locked(dst_nid, LNET_NID_ANY, cpt);
2657 lnet_net_unlock(cpt);
2658 return PTR_ERR(lpni);
2662 * Cache the original src_nid and rtr_nid. If we need to resend the
2663 * message then we'll need to know whether the src_nid was originally
2664 * specified for this message. If it was originally specified,
2665 * then we need to keep using the same src_nid since it's
2666 * continuing the same sequence of messages. Similarly, rtr_nid will
2667 * affect our choice of next hop.
2669 msg->msg_src_nid_param = src_nid;
2670 msg->msg_rtr_nid_param = rtr_nid;
2673 * If necessary, perform discovery on the peer that owns this peer_ni.
2674 * Note, this can result in the ownership of this peer_ni changing
2675 * to another peer object.
2677 rc = lnet_initiate_peer_discovery(lpni, msg, cpt);
2679 lnet_peer_ni_decref_locked(lpni);
2680 lnet_net_unlock(cpt);
2683 lnet_peer_ni_decref_locked(lpni);
2685 peer = lpni->lpni_peer_net->lpn_peer;
2688 * Identify the different send cases
2690 if (src_nid == LNET_NID_ANY)
2691 send_case |= SRC_ANY;
2693 send_case |= SRC_SPEC;
2695 if (lnet_get_net_locked(LNET_NIDNET(dst_nid)))
2696 send_case |= LOCAL_DST;
2698 send_case |= REMOTE_DST;
2701 * if this is a non-MR peer or if we're recovering a peer ni then
2702 * let's consider this an NMR case so we can hit the destination
2705 if (!lnet_peer_is_multi_rail(peer) || msg->msg_recovery)
2706 send_case |= NMR_DST;
2708 send_case |= MR_DST;
2710 if (lnet_msg_is_response(msg))
2711 send_case |= SND_RESP;
2713 /* assign parameters to the send_data */
2714 send_data.sd_rtr_nid = rtr_nid;
2715 send_data.sd_src_nid = src_nid;
2716 send_data.sd_dst_nid = dst_nid;
2717 send_data.sd_best_lpni = lpni;
2719 * keep a pointer to the final destination in case we're going to
2720 * route, so we'll need to access it later
2722 send_data.sd_final_dst_lpni = lpni;
2723 send_data.sd_peer = peer;
2724 send_data.sd_md_cpt = md_cpt;
2725 send_data.sd_send_case = send_case;
2727 rc = lnet_handle_send_case_locked(&send_data);
2730 * Update the local cpt since send_data.sd_cpt might've been
2731 * updated as a result of calling lnet_handle_send_case_locked().
2733 cpt = send_data.sd_cpt;
2735 if (rc == REPEAT_SEND)
2738 lnet_net_unlock(cpt);
2744 lnet_send(lnet_nid_t src_nid, struct lnet_msg *msg, lnet_nid_t rtr_nid)
2746 lnet_nid_t dst_nid = msg->msg_target.nid;
2750 * NB: rtr_nid is set to LNET_NID_ANY for all current use-cases,
2751 * but we might want to use pre-determined router for ACK/REPLY
2754 /* NB: ni != NULL == interface pre-determined (ACK/REPLY) */
2755 LASSERT(msg->msg_txpeer == NULL);
2756 LASSERT(msg->msg_txni == NULL);
2757 LASSERT(!msg->msg_sending);
2758 LASSERT(!msg->msg_target_is_router);
2759 LASSERT(!msg->msg_receiving);
2761 msg->msg_sending = 1;
2763 LASSERT(!msg->msg_tx_committed);
2765 rc = lnet_select_pathway(src_nid, dst_nid, msg, rtr_nid);
2767 if (rc == -EHOSTUNREACH)
2768 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_ERROR;
2770 msg->msg_health_status = LNET_MSG_STATUS_LOCAL_ERROR;
2774 if (rc == LNET_CREDIT_OK)
2775 lnet_ni_send(msg->msg_txni, msg);
2777 /* rc == LNET_CREDIT_OK or LNET_CREDIT_WAIT or LNET_DC_WAIT */
2781 enum lnet_mt_event_type {
2782 MT_TYPE_LOCAL_NI = 0,
2786 struct lnet_mt_event_info {
2787 enum lnet_mt_event_type mt_type;
2791 /* called with res_lock held */
2793 lnet_detach_rsp_tracker(struct lnet_libmd *md, int cpt)
2795 struct lnet_rsp_tracker *rspt;
2798 * msg has a refcount on the MD so the MD is not going away.
2799 * The rspt queue for the cpt is protected by
2800 * the lnet_net_lock(cpt). cpt is the cpt of the MD cookie.
2802 if (!md->md_rspt_ptr)
2805 rspt = md->md_rspt_ptr;
2808 LASSERT(rspt->rspt_cpt == cpt);
2810 md->md_rspt_ptr = NULL;
2812 if (LNetMDHandleIsInvalid(rspt->rspt_mdh)) {
2814 * The monitor thread has invalidated this handle because the
2815 * response timed out, but it failed to lookup the MD. That
2816 * means this response tracker is on the zombie list. We can
2817 * safely remove it under the resource lock (held by caller) and
2818 * free the response tracker block.
2820 list_del(&rspt->rspt_on_list);
2821 lnet_rspt_free(rspt, cpt);
2824 * invalidate the handle to indicate that a response has been
2825 * received, which will then lead the monitor thread to clean up
2828 LNetInvalidateMDHandle(&rspt->rspt_mdh);
2833 lnet_clean_zombie_rstqs(void)
2835 struct lnet_rsp_tracker *rspt, *tmp;
2838 cfs_cpt_for_each(i, lnet_cpt_table()) {
2839 list_for_each_entry_safe(rspt, tmp,
2840 the_lnet.ln_mt_zombie_rstqs[i],
2842 list_del(&rspt->rspt_on_list);
2843 lnet_rspt_free(rspt, i);
2847 cfs_percpt_free(the_lnet.ln_mt_zombie_rstqs);
2851 lnet_finalize_expired_responses(void)
2853 struct lnet_libmd *md;
2854 struct lnet_rsp_tracker *rspt, *tmp;
2858 if (the_lnet.ln_mt_rstq == NULL)
2861 cfs_cpt_for_each(i, lnet_cpt_table()) {
2862 LIST_HEAD(local_queue);
2865 if (!the_lnet.ln_mt_rstq[i]) {
2869 list_splice_init(the_lnet.ln_mt_rstq[i], &local_queue);
2874 list_for_each_entry_safe(rspt, tmp, &local_queue, rspt_on_list) {
2876 * The rspt mdh will be invalidated when a response
2877 * is received or whenever we want to discard the
2878 * block the monitor thread will walk the queue
2879 * and clean up any rsts with an invalid mdh.
2880 * The monitor thread will walk the queue until
2881 * the first unexpired rspt block. This means that
2882 * some rspt blocks which received their
2883 * corresponding responses will linger in the
2884 * queue until they are cleaned up eventually.
2887 if (LNetMDHandleIsInvalid(rspt->rspt_mdh)) {
2889 list_del(&rspt->rspt_on_list);
2890 lnet_rspt_free(rspt, i);
2894 if (ktime_compare(now, rspt->rspt_deadline) >= 0 ||
2895 the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN) {
2896 struct lnet_peer_ni *lpni;
2899 md = lnet_handle2md(&rspt->rspt_mdh);
2901 /* MD has been queued for unlink, but
2902 * rspt hasn't been detached (Note we've
2903 * checked above that the rspt_mdh is
2904 * valid). Since we cannot lookup the MD
2905 * we're unable to detach the rspt
2906 * ourselves. Thus, move the rspt to the
2907 * zombie list where we'll wait for
2909 * 1. The remaining operations on the
2910 * MD to complete. In this case the
2911 * final operation will result in
2912 * lnet_msg_detach_md()->
2913 * lnet_detach_rsp_tracker() where
2914 * we will clean up this response
2916 * 2. LNet to shutdown. In this case
2917 * we'll wait until after all LND Nets
2918 * have shutdown and then we can
2919 * safely free any remaining response
2920 * tracker blocks on the zombie list.
2921 * Note: We need to hold the resource
2922 * lock when adding to the zombie list
2923 * because we may have concurrent access
2924 * with lnet_detach_rsp_tracker().
2926 LNetInvalidateMDHandle(&rspt->rspt_mdh);
2927 list_move(&rspt->rspt_on_list,
2928 the_lnet.ln_mt_zombie_rstqs[i]);
2932 LASSERT(md->md_rspt_ptr == rspt);
2933 md->md_rspt_ptr = NULL;
2936 LNetMDUnlink(rspt->rspt_mdh);
2938 nid = rspt->rspt_next_hop_nid;
2940 list_del(&rspt->rspt_on_list);
2941 lnet_rspt_free(rspt, i);
2943 /* If we're shutting down we just want to clean
2944 * up the rspt blocks
2946 if (the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN)
2950 the_lnet.ln_counters[i]->lct_health.lch_response_timeout_count++;
2954 "Response timeout: md = %p: nid = %s\n",
2955 md, libcfs_nid2str(nid));
2958 * If there is a timeout on the response
2959 * from the next hop decrement its health
2960 * value so that we don't use it
2963 lpni = lnet_find_peer_ni_locked(nid);
2965 lnet_handle_remote_failure_locked(lpni);
2966 lnet_peer_ni_decref_locked(lpni);
2975 if (!list_empty(&local_queue)) {
2977 list_splice(&local_queue, the_lnet.ln_mt_rstq[i]);
2984 lnet_resend_pending_msgs_locked(struct list_head *resendq, int cpt)
2986 struct lnet_msg *msg;
2988 while (!list_empty(resendq)) {
2989 struct lnet_peer_ni *lpni;
2991 msg = list_entry(resendq->next, struct lnet_msg,
2994 list_del_init(&msg->msg_list);
2996 lpni = lnet_find_peer_ni_locked(msg->msg_hdr.dest_nid);
2998 lnet_net_unlock(cpt);
2999 CERROR("Expected that a peer is already created for %s\n",
3000 libcfs_nid2str(msg->msg_hdr.dest_nid));
3001 msg->msg_no_resend = true;
3002 lnet_finalize(msg, -EFAULT);
3007 lnet_peer_ni_decref_locked(lpni);
3009 lnet_net_unlock(cpt);
3010 CDEBUG(D_NET, "resending %s->%s: %s recovery %d try# %d\n",
3011 libcfs_nid2str(msg->msg_src_nid_param),
3012 libcfs_id2str(msg->msg_target),
3013 lnet_msgtyp2str(msg->msg_type),
3015 msg->msg_retry_count);
3016 rc = lnet_send(msg->msg_src_nid_param, msg,
3017 msg->msg_rtr_nid_param);
3019 CERROR("Error sending %s to %s: %d\n",
3020 lnet_msgtyp2str(msg->msg_type),
3021 libcfs_id2str(msg->msg_target), rc);
3022 msg->msg_no_resend = true;
3023 lnet_finalize(msg, rc);
3027 the_lnet.ln_counters[cpt]->lct_health.lch_resend_count++;
3033 lnet_resend_pending_msgs(void)
3037 cfs_cpt_for_each(i, lnet_cpt_table()) {
3039 lnet_resend_pending_msgs_locked(the_lnet.ln_mt_resendqs[i], i);
3044 /* called with cpt and ni_lock held */
3046 lnet_unlink_ni_recovery_mdh_locked(struct lnet_ni *ni, int cpt, bool force)
3048 struct lnet_handle_md recovery_mdh;
3050 LNetInvalidateMDHandle(&recovery_mdh);
3052 if (ni->ni_recovery_state & LNET_NI_RECOVERY_PENDING ||
3054 recovery_mdh = ni->ni_ping_mdh;
3055 LNetInvalidateMDHandle(&ni->ni_ping_mdh);
3058 lnet_net_unlock(cpt);
3059 if (!LNetMDHandleIsInvalid(recovery_mdh))
3060 LNetMDUnlink(recovery_mdh);
3066 lnet_recover_local_nis(void)
3068 struct lnet_mt_event_info *ev_info;
3069 LIST_HEAD(processed_list);
3070 LIST_HEAD(local_queue);
3071 struct lnet_handle_md mdh;
3072 struct lnet_ni *tmp;
3079 * splice the recovery queue on a local queue. We will iterate
3080 * through the local queue and update it as needed. Once we're
3081 * done with the traversal, we'll splice the local queue back on
3082 * the head of the ln_mt_localNIRecovq. Any newly added local NIs
3083 * will be traversed in the next iteration.
3086 list_splice_init(&the_lnet.ln_mt_localNIRecovq,
3090 list_for_each_entry_safe(ni, tmp, &local_queue, ni_recovery) {
3092 * if an NI is being deleted or it is now healthy, there
3093 * is no need to keep it around in the recovery queue.
3094 * The monitor thread is the only thread responsible for
3095 * removing the NI from the recovery queue.
3096 * Multiple threads can be adding NIs to the recovery
3099 healthv = atomic_read(&ni->ni_healthv);
3103 if (ni->ni_state != LNET_NI_STATE_ACTIVE ||
3104 healthv == LNET_MAX_HEALTH_VALUE) {
3105 list_del_init(&ni->ni_recovery);
3106 lnet_unlink_ni_recovery_mdh_locked(ni, 0, false);
3108 lnet_ni_decref_locked(ni, 0);
3114 * if the local NI failed recovery we must unlink the md.
3115 * But we want to keep the local_ni on the recovery queue
3116 * so we can continue the attempts to recover it.
3118 if (ni->ni_recovery_state & LNET_NI_RECOVERY_FAILED) {
3119 lnet_unlink_ni_recovery_mdh_locked(ni, 0, true);
3120 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_FAILED;
3127 CDEBUG(D_NET, "attempting to recover local ni: %s\n",
3128 libcfs_nid2str(ni->ni_nid));
3131 if (!(ni->ni_recovery_state & LNET_NI_RECOVERY_PENDING)) {
3132 ni->ni_recovery_state |= LNET_NI_RECOVERY_PENDING;
3135 LIBCFS_ALLOC(ev_info, sizeof(*ev_info));
3137 CERROR("out of memory. Can't recover %s\n",
3138 libcfs_nid2str(ni->ni_nid));
3140 ni->ni_recovery_state &=
3141 ~LNET_NI_RECOVERY_PENDING;
3146 mdh = ni->ni_ping_mdh;
3148 * Invalidate the ni mdh in case it's deleted.
3149 * We'll unlink the mdh in this case below.
3151 LNetInvalidateMDHandle(&ni->ni_ping_mdh);
3155 * remove the NI from the local queue and drop the
3156 * reference count to it while we're recovering
3157 * it. The reason for that, is that the NI could
3158 * be deleted, and the way the code is structured
3159 * is if we don't drop the NI, then the deletion
3160 * code will enter a loop waiting for the
3161 * reference count to be removed while holding the
3162 * ln_mutex_lock(). When we look up the peer to
3163 * send to in lnet_select_pathway() we will try to
3164 * lock the ln_mutex_lock() as well, leading to
3165 * a deadlock. By dropping the refcount and
3166 * removing it from the list, we allow for the NI
3167 * to be removed, then we use the cached NID to
3168 * look it up again. If it's gone, then we just
3169 * continue examining the rest of the queue.
3172 list_del_init(&ni->ni_recovery);
3173 lnet_ni_decref_locked(ni, 0);
3176 ev_info->mt_type = MT_TYPE_LOCAL_NI;
3177 ev_info->mt_nid = nid;
3178 rc = lnet_send_ping(nid, &mdh, LNET_INTERFACES_MIN,
3179 ev_info, the_lnet.ln_mt_eq, true);
3180 /* lookup the nid again */
3182 ni = lnet_nid2ni_locked(nid, 0);
3185 * the NI has been deleted when we dropped
3193 * Same note as in lnet_recover_peer_nis(). When
3194 * we're sending the ping, the NI is free to be
3195 * deleted or manipulated. By this point it
3196 * could've been added back on the recovery queue,
3197 * and a refcount taken on it.
3198 * So we can't just add it blindly again or we'll
3199 * corrupt the queue. We must check under lock if
3200 * it's not on any list and if not then add it
3201 * to the processed list, which will eventually be
3202 * spliced back on to the recovery queue.
3204 ni->ni_ping_mdh = mdh;
3205 if (list_empty(&ni->ni_recovery)) {
3206 list_add_tail(&ni->ni_recovery, &processed_list);
3207 lnet_ni_addref_locked(ni, 0);
3213 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_PENDING;
3219 * put back the remaining NIs on the ln_mt_localNIRecovq to be
3220 * reexamined in the next iteration.
3222 list_splice_init(&processed_list, &local_queue);
3224 list_splice(&local_queue, &the_lnet.ln_mt_localNIRecovq);
3229 lnet_resendqs_create(void)
3231 struct list_head **resendqs;
3232 resendqs = lnet_create_array_of_queues();
3237 lnet_net_lock(LNET_LOCK_EX);
3238 the_lnet.ln_mt_resendqs = resendqs;
3239 lnet_net_unlock(LNET_LOCK_EX);
3245 lnet_clean_local_ni_recoveryq(void)
3249 /* This is only called when the monitor thread has stopped */
3252 while (!list_empty(&the_lnet.ln_mt_localNIRecovq)) {
3253 ni = list_entry(the_lnet.ln_mt_localNIRecovq.next,
3254 struct lnet_ni, ni_recovery);
3255 list_del_init(&ni->ni_recovery);
3257 lnet_unlink_ni_recovery_mdh_locked(ni, 0, true);
3259 lnet_ni_decref_locked(ni, 0);
3266 lnet_unlink_lpni_recovery_mdh_locked(struct lnet_peer_ni *lpni, int cpt,
3269 struct lnet_handle_md recovery_mdh;
3271 LNetInvalidateMDHandle(&recovery_mdh);
3273 if (lpni->lpni_state & LNET_PEER_NI_RECOVERY_PENDING || force) {
3274 recovery_mdh = lpni->lpni_recovery_ping_mdh;
3275 LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
3277 spin_unlock(&lpni->lpni_lock);
3278 lnet_net_unlock(cpt);
3279 if (!LNetMDHandleIsInvalid(recovery_mdh))
3280 LNetMDUnlink(recovery_mdh);
3282 spin_lock(&lpni->lpni_lock);
3286 lnet_clean_peer_ni_recoveryq(void)
3288 struct lnet_peer_ni *lpni, *tmp;
3290 lnet_net_lock(LNET_LOCK_EX);
3292 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_mt_peerNIRecovq,
3294 list_del_init(&lpni->lpni_recovery);
3295 spin_lock(&lpni->lpni_lock);
3296 lnet_unlink_lpni_recovery_mdh_locked(lpni, LNET_LOCK_EX, true);
3297 spin_unlock(&lpni->lpni_lock);
3298 lnet_peer_ni_decref_locked(lpni);
3301 lnet_net_unlock(LNET_LOCK_EX);
3305 lnet_clean_resendqs(void)
3307 struct lnet_msg *msg, *tmp;
3311 cfs_cpt_for_each(i, lnet_cpt_table()) {
3313 list_splice_init(the_lnet.ln_mt_resendqs[i], &msgs);
3315 list_for_each_entry_safe(msg, tmp, &msgs, msg_list) {
3316 list_del_init(&msg->msg_list);
3317 msg->msg_no_resend = true;
3318 lnet_finalize(msg, -ESHUTDOWN);
3322 cfs_percpt_free(the_lnet.ln_mt_resendqs);
3326 lnet_recover_peer_nis(void)
3328 struct lnet_mt_event_info *ev_info;
3329 LIST_HEAD(processed_list);
3330 LIST_HEAD(local_queue);
3331 struct lnet_handle_md mdh;
3332 struct lnet_peer_ni *lpni;
3333 struct lnet_peer_ni *tmp;
3339 * Always use cpt 0 for locking across all interactions with
3340 * ln_mt_peerNIRecovq
3343 list_splice_init(&the_lnet.ln_mt_peerNIRecovq,
3347 list_for_each_entry_safe(lpni, tmp, &local_queue,
3350 * The same protection strategy is used here as is in the
3351 * local recovery case.
3354 healthv = atomic_read(&lpni->lpni_healthv);
3355 spin_lock(&lpni->lpni_lock);
3356 if (lpni->lpni_state & LNET_PEER_NI_DELETING ||
3357 healthv == LNET_MAX_HEALTH_VALUE) {
3358 list_del_init(&lpni->lpni_recovery);
3359 lnet_unlink_lpni_recovery_mdh_locked(lpni, 0, false);
3360 spin_unlock(&lpni->lpni_lock);
3361 lnet_peer_ni_decref_locked(lpni);
3367 * If the peer NI has failed recovery we must unlink the
3368 * md. But we want to keep the peer ni on the recovery
3369 * queue so we can try to continue recovering it
3371 if (lpni->lpni_state & LNET_PEER_NI_RECOVERY_FAILED) {
3372 lnet_unlink_lpni_recovery_mdh_locked(lpni, 0, true);
3373 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_FAILED;
3376 spin_unlock(&lpni->lpni_lock);
3380 * NOTE: we're racing with peer deletion from user space.
3381 * It's possible that a peer is deleted after we check its
3382 * state. In this case the recovery can create a new peer
3384 spin_lock(&lpni->lpni_lock);
3385 if (!(lpni->lpni_state & LNET_PEER_NI_RECOVERY_PENDING) &&
3386 !(lpni->lpni_state & LNET_PEER_NI_DELETING)) {
3387 lpni->lpni_state |= LNET_PEER_NI_RECOVERY_PENDING;
3388 spin_unlock(&lpni->lpni_lock);
3390 LIBCFS_ALLOC(ev_info, sizeof(*ev_info));
3392 CERROR("out of memory. Can't recover %s\n",
3393 libcfs_nid2str(lpni->lpni_nid));
3394 spin_lock(&lpni->lpni_lock);
3395 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3396 spin_unlock(&lpni->lpni_lock);
3400 /* look at the comments in lnet_recover_local_nis() */
3401 mdh = lpni->lpni_recovery_ping_mdh;
3402 LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
3403 nid = lpni->lpni_nid;
3405 list_del_init(&lpni->lpni_recovery);
3406 lnet_peer_ni_decref_locked(lpni);
3409 ev_info->mt_type = MT_TYPE_PEER_NI;
3410 ev_info->mt_nid = nid;
3411 rc = lnet_send_ping(nid, &mdh, LNET_INTERFACES_MIN,
3412 ev_info, the_lnet.ln_mt_eq, true);
3415 * lnet_find_peer_ni_locked() grabs a refcount for
3416 * us. No need to take it explicitly.
3418 lpni = lnet_find_peer_ni_locked(nid);
3425 lpni->lpni_recovery_ping_mdh = mdh;
3427 * While we're unlocked the lpni could've been
3428 * readded on the recovery queue. In this case we
3429 * don't need to add it to the local queue, since
3430 * it's already on there and the thread that added
3431 * it would've incremented the refcount on the
3432 * peer, which means we need to decref the refcount
3433 * that was implicitly grabbed by find_peer_ni_locked.
3434 * Otherwise, if the lpni is still not on
3435 * the recovery queue, then we'll add it to the
3438 if (list_empty(&lpni->lpni_recovery))
3439 list_add_tail(&lpni->lpni_recovery, &processed_list);
3441 lnet_peer_ni_decref_locked(lpni);
3444 spin_lock(&lpni->lpni_lock);
3446 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3448 spin_unlock(&lpni->lpni_lock);
3451 list_splice_init(&processed_list, &local_queue);
3453 list_splice(&local_queue, &the_lnet.ln_mt_peerNIRecovq);
3458 lnet_monitor_thread(void *arg)
3460 time64_t recovery_timeout = 0;
3461 time64_t rsp_timeout = 0;
3465 wait_for_completion(&the_lnet.ln_started);
3467 * The monitor thread takes care of the following:
3468 * 1. Checks the aliveness of routers
3469 * 2. Checks if there are messages on the resend queue to resend
3471 * 3. Check if there are any NIs on the local recovery queue and
3473 * 4. Checks if there are any NIs on the remote recovery queue
3476 while (the_lnet.ln_mt_state == LNET_MT_STATE_RUNNING) {
3477 now = ktime_get_real_seconds();
3479 if (lnet_router_checker_active())
3480 lnet_check_routers();
3482 lnet_resend_pending_msgs();
3484 if (now >= rsp_timeout) {
3485 lnet_finalize_expired_responses();
3486 rsp_timeout = now + (lnet_transaction_timeout / 2);
3489 if (now >= recovery_timeout) {
3490 lnet_recover_local_nis();
3491 lnet_recover_peer_nis();
3492 recovery_timeout = now + lnet_recovery_interval;
3496 * TODO do we need to check if we should sleep without
3497 * timeout? Technically, an active system will always
3498 * have messages in flight so this check will always
3499 * evaluate to false. And on an idle system do we care
3500 * if we wake up every 1 second? Although, we've seen
3501 * cases where we get a complaint that an idle thread
3502 * is waking up unnecessarily.
3504 * Take into account the current net_count when you wake
3505 * up for alive router checking, since we need to check
3506 * possibly as many networks as we have configured.
3508 interval = min(lnet_recovery_interval,
3509 min((unsigned int) alive_router_check_interval /
3510 lnet_current_net_count,
3511 lnet_transaction_timeout / 2));
3512 wait_for_completion_interruptible_timeout(
3513 &the_lnet.ln_mt_wait_complete,
3514 cfs_time_seconds(interval));
3515 /* Must re-init the completion before testing anything,
3516 * including ln_mt_state.
3518 reinit_completion(&the_lnet.ln_mt_wait_complete);
3522 lnet_net_lock(LNET_LOCK_EX);
3523 the_lnet.ln_mt_state = LNET_MT_STATE_SHUTDOWN;
3524 lnet_net_unlock(LNET_LOCK_EX);
3526 /* signal that the monitor thread is exiting */
3527 up(&the_lnet.ln_mt_signal);
3535 * Returns == 0 if success
3536 * Returns > 0 if LNetMDBind or prior fails
3537 * Returns < 0 if LNetGet fails
3540 lnet_send_ping(lnet_nid_t dest_nid,
3541 struct lnet_handle_md *mdh, int nnis,
3542 void *user_data, struct lnet_eq *eq, bool recovery)
3544 struct lnet_md md = { NULL };
3545 struct lnet_process_id id;
3546 struct lnet_ping_buffer *pbuf;
3549 if (dest_nid == LNET_NID_ANY) {
3554 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
3560 /* initialize md content */
3561 md.start = &pbuf->pb_info;
3562 md.length = LNET_PING_INFO_SIZE(nnis);
3563 md.threshold = 2; /* GET/REPLY */
3565 md.options = LNET_MD_TRUNCATE;
3566 md.user_ptr = user_data;
3569 rc = LNetMDBind(md, LNET_UNLINK, mdh);
3571 lnet_ping_buffer_decref(pbuf);
3572 CERROR("Can't bind MD: %d\n", rc);
3573 rc = -rc; /* change the rc to positive */
3576 id.pid = LNET_PID_LUSTRE;
3579 rc = LNetGet(LNET_NID_ANY, *mdh, id,
3580 LNET_RESERVED_PORTAL,
3581 LNET_PROTO_PING_MATCHBITS, 0, recovery);
3584 goto fail_unlink_md;
3590 LNetInvalidateMDHandle(mdh);
3596 lnet_handle_recovery_reply(struct lnet_mt_event_info *ev_info,
3597 int status, bool unlink_event)
3599 lnet_nid_t nid = ev_info->mt_nid;
3601 if (ev_info->mt_type == MT_TYPE_LOCAL_NI) {
3605 ni = lnet_nid2ni_locked(nid, 0);
3611 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_PENDING;
3613 ni->ni_recovery_state |= LNET_NI_RECOVERY_FAILED;
3618 CERROR("local NI (%s) recovery failed with %d\n",
3619 libcfs_nid2str(nid), status);
3623 * need to increment healthv for the ni here, because in
3624 * the lnet_finalize() path we don't have access to this
3625 * NI. And in order to get access to it, we'll need to
3626 * carry forward too much information.
3627 * In the peer case, it'll naturally be incremented
3630 lnet_inc_healthv(&ni->ni_healthv);
3632 struct lnet_peer_ni *lpni;
3635 cpt = lnet_net_lock_current();
3636 lpni = lnet_find_peer_ni_locked(nid);
3638 lnet_net_unlock(cpt);
3641 spin_lock(&lpni->lpni_lock);
3642 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3644 lpni->lpni_state |= LNET_PEER_NI_RECOVERY_FAILED;
3645 spin_unlock(&lpni->lpni_lock);
3646 lnet_peer_ni_decref_locked(lpni);
3647 lnet_net_unlock(cpt);
3650 CERROR("peer NI (%s) recovery failed with %d\n",
3651 libcfs_nid2str(nid), status);
3656 lnet_mt_event_handler(struct lnet_event *event)
3658 struct lnet_mt_event_info *ev_info = event->md.user_ptr;
3659 struct lnet_ping_buffer *pbuf;
3661 /* TODO: remove assert */
3662 LASSERT(event->type == LNET_EVENT_REPLY ||
3663 event->type == LNET_EVENT_SEND ||
3664 event->type == LNET_EVENT_UNLINK);
3666 CDEBUG(D_NET, "Received event: %d status: %d\n", event->type,
3669 switch (event->type) {
3670 case LNET_EVENT_UNLINK:
3671 CDEBUG(D_NET, "%s recovery ping unlinked\n",
3672 libcfs_nid2str(ev_info->mt_nid));
3674 case LNET_EVENT_REPLY:
3675 lnet_handle_recovery_reply(ev_info, event->status,
3676 event->type == LNET_EVENT_UNLINK);
3678 case LNET_EVENT_SEND:
3679 CDEBUG(D_NET, "%s recovery message sent %s:%d\n",
3680 libcfs_nid2str(ev_info->mt_nid),
3681 (event->status) ? "unsuccessfully" :
3682 "successfully", event->status);
3685 CERROR("Unexpected event: %d\n", event->type);
3688 if (event->unlinked) {
3689 LIBCFS_FREE(ev_info, sizeof(*ev_info));
3690 pbuf = LNET_PING_INFO_TO_BUFFER(event->md.start);
3691 lnet_ping_buffer_decref(pbuf);
3696 lnet_rsp_tracker_create(void)
3698 struct list_head **rstqs;
3699 rstqs = lnet_create_array_of_queues();
3704 the_lnet.ln_mt_rstq = rstqs;
3710 lnet_rsp_tracker_clean(void)
3712 lnet_finalize_expired_responses();
3714 cfs_percpt_free(the_lnet.ln_mt_rstq);
3715 the_lnet.ln_mt_rstq = NULL;
3718 int lnet_monitor_thr_start(void)
3721 struct task_struct *task;
3723 if (the_lnet.ln_mt_state != LNET_MT_STATE_SHUTDOWN)
3726 rc = lnet_resendqs_create();
3730 rc = lnet_rsp_tracker_create();
3734 sema_init(&the_lnet.ln_mt_signal, 0);
3736 lnet_net_lock(LNET_LOCK_EX);
3737 the_lnet.ln_mt_state = LNET_MT_STATE_RUNNING;
3738 lnet_net_unlock(LNET_LOCK_EX);
3739 task = kthread_run(lnet_monitor_thread, NULL, "monitor_thread");
3742 CERROR("Can't start monitor thread: %d\n", rc);
3749 lnet_net_lock(LNET_LOCK_EX);
3750 the_lnet.ln_mt_state = LNET_MT_STATE_STOPPING;
3751 lnet_net_unlock(LNET_LOCK_EX);
3752 /* block until event callback signals exit */
3753 down(&the_lnet.ln_mt_signal);
3755 lnet_net_lock(LNET_LOCK_EX);
3756 the_lnet.ln_mt_state = LNET_MT_STATE_SHUTDOWN;
3757 lnet_net_unlock(LNET_LOCK_EX);
3758 lnet_rsp_tracker_clean();
3759 lnet_clean_local_ni_recoveryq();
3760 lnet_clean_peer_ni_recoveryq();
3761 lnet_clean_resendqs();
3762 the_lnet.ln_mt_eq = NULL;
3765 lnet_rsp_tracker_clean();
3766 lnet_clean_local_ni_recoveryq();
3767 lnet_clean_peer_ni_recoveryq();
3768 lnet_clean_resendqs();
3772 void lnet_monitor_thr_stop(void)
3774 if (the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN)
3777 LASSERT(the_lnet.ln_mt_state == LNET_MT_STATE_RUNNING);
3778 lnet_net_lock(LNET_LOCK_EX);
3779 the_lnet.ln_mt_state = LNET_MT_STATE_STOPPING;
3780 lnet_net_unlock(LNET_LOCK_EX);
3782 /* tell the monitor thread that we're shutting down */
3783 complete(&the_lnet.ln_mt_wait_complete);
3785 /* block until monitor thread signals that it's done */
3786 down(&the_lnet.ln_mt_signal);
3787 LASSERT(the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN);
3789 /* perform cleanup tasks */
3790 lnet_rsp_tracker_clean();
3791 lnet_clean_local_ni_recoveryq();
3792 lnet_clean_peer_ni_recoveryq();
3793 lnet_clean_resendqs();
3797 lnet_drop_message(struct lnet_ni *ni, int cpt, void *private, unsigned int nob,
3801 lnet_incr_stats(&ni->ni_stats, msg_type, LNET_STATS_TYPE_DROP);
3802 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
3803 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length += nob;
3804 lnet_net_unlock(cpt);
3806 lnet_ni_recv(ni, private, NULL, 0, 0, 0, nob);
3810 lnet_recv_put(struct lnet_ni *ni, struct lnet_msg *msg)
3812 struct lnet_hdr *hdr = &msg->msg_hdr;
3814 if (msg->msg_wanted != 0)
3815 lnet_setpayloadbuffer(msg);
3817 lnet_build_msg_event(msg, LNET_EVENT_PUT);
3819 /* Must I ACK? If so I'll grab the ack_wmd out of the header and put
3820 * it back into the ACK during lnet_finalize() */
3821 msg->msg_ack = (!lnet_is_wire_handle_none(&hdr->msg.put.ack_wmd) &&
3822 (msg->msg_md->md_options & LNET_MD_ACK_DISABLE) == 0);
3824 lnet_ni_recv(ni, msg->msg_private, msg, msg->msg_rx_delayed,
3825 msg->msg_offset, msg->msg_wanted, hdr->payload_length);
3829 lnet_parse_put(struct lnet_ni *ni, struct lnet_msg *msg)
3831 struct lnet_hdr *hdr = &msg->msg_hdr;
3832 struct lnet_match_info info;
3836 /* Convert put fields to host byte order */
3837 hdr->msg.put.match_bits = le64_to_cpu(hdr->msg.put.match_bits);
3838 hdr->msg.put.ptl_index = le32_to_cpu(hdr->msg.put.ptl_index);
3839 hdr->msg.put.offset = le32_to_cpu(hdr->msg.put.offset);
3841 /* Primary peer NID. */
3842 info.mi_id.nid = msg->msg_initiator;
3843 info.mi_id.pid = hdr->src_pid;
3844 info.mi_opc = LNET_MD_OP_PUT;
3845 info.mi_portal = hdr->msg.put.ptl_index;
3846 info.mi_rlength = hdr->payload_length;
3847 info.mi_roffset = hdr->msg.put.offset;
3848 info.mi_mbits = hdr->msg.put.match_bits;
3849 info.mi_cpt = lnet_cpt_of_nid(msg->msg_initiator, ni);
3851 msg->msg_rx_ready_delay = ni->ni_net->net_lnd->lnd_eager_recv == NULL;
3852 ready_delay = msg->msg_rx_ready_delay;
3855 rc = lnet_ptl_match_md(&info, msg);
3860 case LNET_MATCHMD_OK:
3861 lnet_recv_put(ni, msg);
3864 case LNET_MATCHMD_NONE:
3866 /* no eager_recv or has already called it, should
3867 * have been attached on delayed list */
3870 rc = lnet_ni_eager_recv(ni, msg);
3877 case LNET_MATCHMD_DROP:
3878 CNETERR("Dropping PUT from %s portal %d match %llu"
3879 " offset %d length %d: %d\n",
3880 libcfs_id2str(info.mi_id), info.mi_portal,
3881 info.mi_mbits, info.mi_roffset, info.mi_rlength, rc);
3883 return -ENOENT; /* -ve: OK but no match */
3888 lnet_parse_get(struct lnet_ni *ni, struct lnet_msg *msg, int rdma_get)
3890 struct lnet_match_info info;
3891 struct lnet_hdr *hdr = &msg->msg_hdr;
3892 struct lnet_process_id source_id;
3893 struct lnet_handle_wire reply_wmd;
3896 /* Convert get fields to host byte order */
3897 hdr->msg.get.match_bits = le64_to_cpu(hdr->msg.get.match_bits);
3898 hdr->msg.get.ptl_index = le32_to_cpu(hdr->msg.get.ptl_index);
3899 hdr->msg.get.sink_length = le32_to_cpu(hdr->msg.get.sink_length);
3900 hdr->msg.get.src_offset = le32_to_cpu(hdr->msg.get.src_offset);
3902 source_id.nid = hdr->src_nid;
3903 source_id.pid = hdr->src_pid;
3904 /* Primary peer NID */
3905 info.mi_id.nid = msg->msg_initiator;
3906 info.mi_id.pid = hdr->src_pid;
3907 info.mi_opc = LNET_MD_OP_GET;
3908 info.mi_portal = hdr->msg.get.ptl_index;
3909 info.mi_rlength = hdr->msg.get.sink_length;
3910 info.mi_roffset = hdr->msg.get.src_offset;
3911 info.mi_mbits = hdr->msg.get.match_bits;
3912 info.mi_cpt = lnet_cpt_of_nid(msg->msg_initiator, ni);
3914 rc = lnet_ptl_match_md(&info, msg);
3915 if (rc == LNET_MATCHMD_DROP) {
3916 CNETERR("Dropping GET from %s portal %d match %llu"
3917 " offset %d length %d\n",
3918 libcfs_id2str(info.mi_id), info.mi_portal,
3919 info.mi_mbits, info.mi_roffset, info.mi_rlength);
3920 return -ENOENT; /* -ve: OK but no match */
3923 LASSERT(rc == LNET_MATCHMD_OK);
3925 lnet_build_msg_event(msg, LNET_EVENT_GET);
3927 reply_wmd = hdr->msg.get.return_wmd;
3929 lnet_prep_send(msg, LNET_MSG_REPLY, source_id,
3930 msg->msg_offset, msg->msg_wanted);
3932 msg->msg_hdr.msg.reply.dst_wmd = reply_wmd;
3935 /* The LND completes the REPLY from her recv procedure */
3936 lnet_ni_recv(ni, msg->msg_private, msg, 0,
3937 msg->msg_offset, msg->msg_len, msg->msg_len);
3941 lnet_ni_recv(ni, msg->msg_private, NULL, 0, 0, 0, 0);
3942 msg->msg_receiving = 0;
3944 rc = lnet_send(ni->ni_nid, msg, msg->msg_from);
3946 /* didn't get as far as lnet_ni_send() */
3947 CERROR("%s: Unable to send REPLY for GET from %s: %d\n",
3948 libcfs_nid2str(ni->ni_nid),
3949 libcfs_id2str(info.mi_id), rc);
3951 lnet_finalize(msg, rc);
3958 lnet_parse_reply(struct lnet_ni *ni, struct lnet_msg *msg)
3960 void *private = msg->msg_private;
3961 struct lnet_hdr *hdr = &msg->msg_hdr;
3962 struct lnet_process_id src = {0};
3963 struct lnet_libmd *md;
3964 unsigned int rlength;
3965 unsigned int mlength;
3968 cpt = lnet_cpt_of_cookie(hdr->msg.reply.dst_wmd.wh_object_cookie);
3971 src.nid = hdr->src_nid;
3972 src.pid = hdr->src_pid;
3974 /* NB handles only looked up by creator (no flips) */
3975 md = lnet_wire_handle2md(&hdr->msg.reply.dst_wmd);
3976 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
3977 CNETERR("%s: Dropping REPLY from %s for %s "
3979 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
3980 (md == NULL) ? "invalid" : "inactive",
3981 hdr->msg.reply.dst_wmd.wh_interface_cookie,
3982 hdr->msg.reply.dst_wmd.wh_object_cookie);
3983 if (md != NULL && md->md_me != NULL)
3984 CERROR("REPLY MD also attached to portal %d\n",
3985 md->md_me->me_portal);
3987 lnet_res_unlock(cpt);
3988 return -ENOENT; /* -ve: OK but no match */
3991 LASSERT(md->md_offset == 0);
3993 rlength = hdr->payload_length;
3994 mlength = min(rlength, md->md_length);
3996 if (mlength < rlength &&
3997 (md->md_options & LNET_MD_TRUNCATE) == 0) {
3998 CNETERR("%s: Dropping REPLY from %s length %d "
3999 "for MD %#llx would overflow (%d)\n",
4000 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
4001 rlength, hdr->msg.reply.dst_wmd.wh_object_cookie,
4003 lnet_res_unlock(cpt);
4004 return -ENOENT; /* -ve: OK but no match */
4007 CDEBUG(D_NET, "%s: Reply from %s of length %d/%d into md %#llx\n",
4008 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
4009 mlength, rlength, hdr->msg.reply.dst_wmd.wh_object_cookie);
4011 lnet_msg_attach_md(msg, md, 0, mlength);
4014 lnet_setpayloadbuffer(msg);
4016 lnet_res_unlock(cpt);
4018 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
4020 lnet_ni_recv(ni, private, msg, 0, 0, mlength, rlength);
4025 lnet_parse_ack(struct lnet_ni *ni, struct lnet_msg *msg)
4027 struct lnet_hdr *hdr = &msg->msg_hdr;
4028 struct lnet_process_id src = {0};
4029 struct lnet_libmd *md;
4032 src.nid = hdr->src_nid;
4033 src.pid = hdr->src_pid;
4035 /* Convert ack fields to host byte order */
4036 hdr->msg.ack.match_bits = le64_to_cpu(hdr->msg.ack.match_bits);
4037 hdr->msg.ack.mlength = le32_to_cpu(hdr->msg.ack.mlength);
4039 cpt = lnet_cpt_of_cookie(hdr->msg.ack.dst_wmd.wh_object_cookie);
4042 /* NB handles only looked up by creator (no flips) */
4043 md = lnet_wire_handle2md(&hdr->msg.ack.dst_wmd);
4044 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
4045 /* Don't moan; this is expected */
4047 "%s: Dropping ACK from %s to %s MD %#llx.%#llx\n",
4048 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
4049 (md == NULL) ? "invalid" : "inactive",
4050 hdr->msg.ack.dst_wmd.wh_interface_cookie,
4051 hdr->msg.ack.dst_wmd.wh_object_cookie);
4052 if (md != NULL && md->md_me != NULL)
4053 CERROR("Source MD also attached to portal %d\n",
4054 md->md_me->me_portal);
4056 lnet_res_unlock(cpt);
4057 return -ENOENT; /* -ve! */
4060 CDEBUG(D_NET, "%s: ACK from %s into md %#llx\n",
4061 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
4062 hdr->msg.ack.dst_wmd.wh_object_cookie);
4064 lnet_msg_attach_md(msg, md, 0, 0);
4066 lnet_res_unlock(cpt);
4068 lnet_build_msg_event(msg, LNET_EVENT_ACK);
4070 lnet_ni_recv(ni, msg->msg_private, msg, 0, 0, 0, msg->msg_len);
4075 * \retval LNET_CREDIT_OK If \a msg is forwarded
4076 * \retval LNET_CREDIT_WAIT If \a msg is blocked because w/o buffer
4077 * \retval -ve error code
4080 lnet_parse_forward_locked(struct lnet_ni *ni, struct lnet_msg *msg)
4084 if (!the_lnet.ln_routing)
4087 if (msg->msg_rxpeer->lpni_rtrcredits <= 0 ||
4088 lnet_msg2bufpool(msg)->rbp_credits <= 0) {
4089 if (ni->ni_net->net_lnd->lnd_eager_recv == NULL) {
4090 msg->msg_rx_ready_delay = 1;
4092 lnet_net_unlock(msg->msg_rx_cpt);
4093 rc = lnet_ni_eager_recv(ni, msg);
4094 lnet_net_lock(msg->msg_rx_cpt);
4099 rc = lnet_post_routed_recv_locked(msg, 0);
4104 lnet_parse_local(struct lnet_ni *ni, struct lnet_msg *msg)
4108 switch (msg->msg_type) {
4110 rc = lnet_parse_ack(ni, msg);
4113 rc = lnet_parse_put(ni, msg);
4116 rc = lnet_parse_get(ni, msg, msg->msg_rdma_get);
4118 case LNET_MSG_REPLY:
4119 rc = lnet_parse_reply(ni, msg);
4121 default: /* prevent an unused label if !kernel */
4126 LASSERT(rc == 0 || rc == -ENOENT);
4131 lnet_msgtyp2str (int type)
4140 case LNET_MSG_REPLY:
4142 case LNET_MSG_HELLO:
4145 return ("<UNKNOWN>");
4150 lnet_print_hdr(struct lnet_hdr *hdr)
4152 struct lnet_process_id src = {
4153 .nid = hdr->src_nid,
4154 .pid = hdr->src_pid,
4156 struct lnet_process_id dst = {
4157 .nid = hdr->dest_nid,
4158 .pid = hdr->dest_pid,
4160 char *type_str = lnet_msgtyp2str(hdr->type);
4162 CWARN("P3 Header at %p of type %s\n", hdr, type_str);
4163 CWARN(" From %s\n", libcfs_id2str(src));
4164 CWARN(" To %s\n", libcfs_id2str(dst));
4166 switch (hdr->type) {
4171 CWARN(" Ptl index %d, ack md %#llx.%#llx, "
4172 "match bits %llu\n",
4173 hdr->msg.put.ptl_index,
4174 hdr->msg.put.ack_wmd.wh_interface_cookie,
4175 hdr->msg.put.ack_wmd.wh_object_cookie,
4176 hdr->msg.put.match_bits);
4177 CWARN(" Length %d, offset %d, hdr data %#llx\n",
4178 hdr->payload_length, hdr->msg.put.offset,
4179 hdr->msg.put.hdr_data);
4183 CWARN(" Ptl index %d, return md %#llx.%#llx, "
4184 "match bits %llu\n", hdr->msg.get.ptl_index,
4185 hdr->msg.get.return_wmd.wh_interface_cookie,
4186 hdr->msg.get.return_wmd.wh_object_cookie,
4187 hdr->msg.get.match_bits);
4188 CWARN(" Length %d, src offset %d\n",
4189 hdr->msg.get.sink_length,
4190 hdr->msg.get.src_offset);
4194 CWARN(" dst md %#llx.%#llx, "
4195 "manipulated length %d\n",
4196 hdr->msg.ack.dst_wmd.wh_interface_cookie,
4197 hdr->msg.ack.dst_wmd.wh_object_cookie,
4198 hdr->msg.ack.mlength);
4201 case LNET_MSG_REPLY:
4202 CWARN(" dst md %#llx.%#llx, "
4204 hdr->msg.reply.dst_wmd.wh_interface_cookie,
4205 hdr->msg.reply.dst_wmd.wh_object_cookie,
4206 hdr->payload_length);
4212 lnet_parse(struct lnet_ni *ni, struct lnet_hdr *hdr, lnet_nid_t from_nid,
4213 void *private, int rdma_req)
4215 struct lnet_peer_ni *lpni;
4216 struct lnet_msg *msg;
4217 __u32 payload_length;
4218 lnet_pid_t dest_pid;
4219 lnet_nid_t dest_nid;
4227 LASSERT (!in_interrupt ());
4229 type = le32_to_cpu(hdr->type);
4230 src_nid = le64_to_cpu(hdr->src_nid);
4231 dest_nid = le64_to_cpu(hdr->dest_nid);
4232 dest_pid = le32_to_cpu(hdr->dest_pid);
4233 payload_length = le32_to_cpu(hdr->payload_length);
4235 for_me = (ni->ni_nid == dest_nid);
4236 cpt = lnet_cpt_of_nid(from_nid, ni);
4238 CDEBUG(D_NET, "TRACE: %s(%s) <- %s : %s - %s\n",
4239 libcfs_nid2str(dest_nid),
4240 libcfs_nid2str(ni->ni_nid),
4241 libcfs_nid2str(src_nid),
4242 lnet_msgtyp2str(type),
4243 (for_me) ? "for me" : "routed");
4248 if (payload_length > 0) {
4249 CERROR("%s, src %s: bad %s payload %d (0 expected)\n",
4250 libcfs_nid2str(from_nid),
4251 libcfs_nid2str(src_nid),
4252 lnet_msgtyp2str(type), payload_length);
4258 case LNET_MSG_REPLY:
4259 if (payload_length >
4260 (__u32)(for_me ? LNET_MAX_PAYLOAD : LNET_MTU)) {
4261 CERROR("%s, src %s: bad %s payload %d "
4262 "(%d max expected)\n",
4263 libcfs_nid2str(from_nid),
4264 libcfs_nid2str(src_nid),
4265 lnet_msgtyp2str(type),
4267 for_me ? LNET_MAX_PAYLOAD : LNET_MTU);
4273 CERROR("%s, src %s: Bad message type 0x%x\n",
4274 libcfs_nid2str(from_nid),
4275 libcfs_nid2str(src_nid), type);
4279 if (the_lnet.ln_routing &&
4280 ni->ni_net->net_last_alive != ktime_get_real_seconds()) {
4282 spin_lock(&ni->ni_net->net_lock);
4283 ni->ni_net->net_last_alive = ktime_get_real_seconds();
4284 spin_unlock(&ni->ni_net->net_lock);
4285 if (ni->ni_status != NULL &&
4286 ni->ni_status->ns_status == LNET_NI_STATUS_DOWN) {
4287 ni->ni_status->ns_status = LNET_NI_STATUS_UP;
4294 lnet_push_update_to_peers(1);
4296 /* Regard a bad destination NID as a protocol error. Senders should
4297 * know what they're doing; if they don't they're misconfigured, buggy
4298 * or malicious so we chop them off at the knees :) */
4301 if (LNET_NIDNET(dest_nid) == LNET_NIDNET(ni->ni_nid)) {
4302 /* should have gone direct */
4303 CERROR("%s, src %s: Bad dest nid %s "
4304 "(should have been sent direct)\n",
4305 libcfs_nid2str(from_nid),
4306 libcfs_nid2str(src_nid),
4307 libcfs_nid2str(dest_nid));
4311 if (lnet_islocalnid(dest_nid)) {
4312 /* dest is another local NI; sender should have used
4313 * this node's NID on its own network */
4314 CERROR("%s, src %s: Bad dest nid %s "
4315 "(it's my nid but on a different network)\n",
4316 libcfs_nid2str(from_nid),
4317 libcfs_nid2str(src_nid),
4318 libcfs_nid2str(dest_nid));
4322 if (rdma_req && type == LNET_MSG_GET) {
4323 CERROR("%s, src %s: Bad optimized GET for %s "
4324 "(final destination must be me)\n",
4325 libcfs_nid2str(from_nid),
4326 libcfs_nid2str(src_nid),
4327 libcfs_nid2str(dest_nid));
4331 if (!the_lnet.ln_routing) {
4332 CERROR("%s, src %s: Dropping message for %s "
4333 "(routing not enabled)\n",
4334 libcfs_nid2str(from_nid),
4335 libcfs_nid2str(src_nid),
4336 libcfs_nid2str(dest_nid));
4341 /* Message looks OK; we're not going to return an error, so we MUST
4342 * call back lnd_recv() come what may... */
4344 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
4345 fail_peer(src_nid, 0)) { /* shall we now? */
4346 CERROR("%s, src %s: Dropping %s to simulate failure\n",
4347 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4348 lnet_msgtyp2str(type));
4352 if (!list_empty(&the_lnet.ln_drop_rules) &&
4353 lnet_drop_rule_match(hdr, ni->ni_nid, NULL)) {
4355 "%s, src %s, dst %s: Dropping %s to simulate silent message loss\n",
4356 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4357 libcfs_nid2str(dest_nid), lnet_msgtyp2str(type));
4361 if (lnet_drop_asym_route && for_me &&
4362 LNET_NIDNET(src_nid) != LNET_NIDNET(from_nid)) {
4363 struct lnet_net *net;
4364 struct lnet_remotenet *rnet;
4367 /* we are dealing with a routed message,
4368 * so see if route to reach src_nid goes through from_nid
4371 net = lnet_get_net_locked(LNET_NIDNET(ni->ni_nid));
4373 lnet_net_unlock(cpt);
4374 CERROR("net %s not found\n",
4375 libcfs_net2str(LNET_NIDNET(ni->ni_nid)));
4379 rnet = lnet_find_rnet_locked(LNET_NIDNET(src_nid));
4381 struct lnet_peer *gw = NULL;
4382 struct lnet_peer_ni *lpni = NULL;
4383 struct lnet_route *route;
4385 list_for_each_entry(route, &rnet->lrn_routes, lr_list) {
4387 gw = route->lr_gateway;
4388 if (route->lr_lnet != net->net_id)
4391 * if the nid is one of the gateway's NIDs
4392 * then this is a valid gateway
4394 while ((lpni = lnet_get_next_peer_ni_locked(gw,
4395 NULL, lpni)) != NULL) {
4396 if (lpni->lpni_nid == from_nid) {
4403 lnet_net_unlock(cpt);
4405 /* we would not use from_nid to route a message to
4407 * => asymmetric routing detected but forbidden
4409 CERROR("%s, src %s: Dropping asymmetrical route %s\n",
4410 libcfs_nid2str(from_nid),
4411 libcfs_nid2str(src_nid), lnet_msgtyp2str(type));
4416 msg = lnet_msg_alloc();
4418 CERROR("%s, src %s: Dropping %s (out of memory)\n",
4419 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4420 lnet_msgtyp2str(type));
4424 /* msg zeroed in lnet_msg_alloc; i.e. flags all clear,
4425 * pointers NULL etc */
4427 msg->msg_type = type;
4428 msg->msg_private = private;
4429 msg->msg_receiving = 1;
4430 msg->msg_rdma_get = rdma_req;
4431 msg->msg_len = msg->msg_wanted = payload_length;
4432 msg->msg_offset = 0;
4433 msg->msg_hdr = *hdr;
4434 /* for building message event */
4435 msg->msg_from = from_nid;
4437 msg->msg_target.pid = dest_pid;
4438 msg->msg_target.nid = dest_nid;
4439 msg->msg_routing = 1;
4442 /* convert common msg->hdr fields to host byteorder */
4443 msg->msg_hdr.type = type;
4444 msg->msg_hdr.src_nid = src_nid;
4445 msg->msg_hdr.src_pid = le32_to_cpu(msg->msg_hdr.src_pid);
4446 msg->msg_hdr.dest_nid = dest_nid;
4447 msg->msg_hdr.dest_pid = dest_pid;
4448 msg->msg_hdr.payload_length = payload_length;
4452 lpni = lnet_nid2peerni_locked(from_nid, ni->ni_nid, cpt);
4454 lnet_net_unlock(cpt);
4455 CERROR("%s, src %s: Dropping %s "
4456 "(error %ld looking up sender)\n",
4457 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4458 lnet_msgtyp2str(type), PTR_ERR(lpni));
4460 if (rc == -ESHUTDOWN)
4461 /* We are shutting down. Don't do anything more */
4466 if (the_lnet.ln_routing)
4467 lpni->lpni_last_alive = ktime_get_seconds();
4469 msg->msg_rxpeer = lpni;
4471 lnet_ni_addref_locked(ni, cpt);
4472 /* Multi-Rail: Primary NID of source. */
4473 msg->msg_initiator = lnet_peer_primary_nid_locked(src_nid);
4476 * mark the status of this lpni as UP since we received a message
4477 * from it. The ping response reports back the ns_status which is
4478 * marked on the remote as up or down and we cache it here.
4480 msg->msg_rxpeer->lpni_ns_status = LNET_NI_STATUS_UP;
4482 lnet_msg_commit(msg, cpt);
4484 /* message delay simulation */
4485 if (unlikely(!list_empty(&the_lnet.ln_delay_rules) &&
4486 lnet_delay_rule_match_locked(hdr, msg))) {
4487 lnet_net_unlock(cpt);
4492 rc = lnet_parse_forward_locked(ni, msg);
4493 lnet_net_unlock(cpt);
4498 if (rc == LNET_CREDIT_OK) {
4499 lnet_ni_recv(ni, msg->msg_private, msg, 0,
4500 0, payload_length, payload_length);
4505 lnet_net_unlock(cpt);
4507 rc = lnet_parse_local(ni, msg);
4513 LASSERT(msg->msg_md == NULL);
4514 lnet_finalize(msg, rc);
4517 lnet_drop_message(ni, cpt, private, payload_length, type);
4520 EXPORT_SYMBOL(lnet_parse);
4523 lnet_drop_delayed_msg_list(struct list_head *head, char *reason)
4525 while (!list_empty(head)) {
4526 struct lnet_process_id id = {0};
4527 struct lnet_msg *msg;
4529 msg = list_entry(head->next, struct lnet_msg, msg_list);
4530 list_del(&msg->msg_list);
4532 id.nid = msg->msg_hdr.src_nid;
4533 id.pid = msg->msg_hdr.src_pid;
4535 LASSERT(msg->msg_md == NULL);
4536 LASSERT(msg->msg_rx_delayed);
4537 LASSERT(msg->msg_rxpeer != NULL);
4538 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
4540 CWARN("Dropping delayed PUT from %s portal %d match %llu"
4541 " offset %d length %d: %s\n",
4543 msg->msg_hdr.msg.put.ptl_index,
4544 msg->msg_hdr.msg.put.match_bits,
4545 msg->msg_hdr.msg.put.offset,
4546 msg->msg_hdr.payload_length, reason);
4548 /* NB I can't drop msg's ref on msg_rxpeer until after I've
4549 * called lnet_drop_message(), so I just hang onto msg as well
4550 * until that's done */
4552 lnet_drop_message(msg->msg_rxni, msg->msg_rx_cpt,
4553 msg->msg_private, msg->msg_len,
4556 msg->msg_no_resend = true;
4558 * NB: message will not generate event because w/o attached MD,
4559 * but we still should give error code so lnet_msg_decommit()
4560 * can skip counters operations and other checks.
4562 lnet_finalize(msg, -ENOENT);
4567 lnet_recv_delayed_msg_list(struct list_head *head)
4569 while (!list_empty(head)) {
4570 struct lnet_msg *msg;
4571 struct lnet_process_id id;
4573 msg = list_entry(head->next, struct lnet_msg, msg_list);
4574 list_del(&msg->msg_list);
4576 /* md won't disappear under me, since each msg
4577 * holds a ref on it */
4579 id.nid = msg->msg_hdr.src_nid;
4580 id.pid = msg->msg_hdr.src_pid;
4582 LASSERT(msg->msg_rx_delayed);
4583 LASSERT(msg->msg_md != NULL);
4584 LASSERT(msg->msg_rxpeer != NULL);
4585 LASSERT(msg->msg_rxni != NULL);
4586 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
4588 CDEBUG(D_NET, "Resuming delayed PUT from %s portal %d "
4589 "match %llu offset %d length %d.\n",
4590 libcfs_id2str(id), msg->msg_hdr.msg.put.ptl_index,
4591 msg->msg_hdr.msg.put.match_bits,
4592 msg->msg_hdr.msg.put.offset,
4593 msg->msg_hdr.payload_length);
4595 lnet_recv_put(msg->msg_rxni, msg);
4600 lnet_attach_rsp_tracker(struct lnet_rsp_tracker *rspt, int cpt,
4601 struct lnet_libmd *md, struct lnet_handle_md mdh)
4604 struct lnet_rsp_tracker *local_rspt;
4607 * MD has a refcount taken by message so it's not going away.
4608 * The MD however can be looked up. We need to secure the access
4609 * to the md_rspt_ptr by taking the res_lock.
4610 * The rspt can be accessed without protection up to when it gets
4611 * added to the list.
4615 local_rspt = md->md_rspt_ptr;
4616 timeout_ns = lnet_transaction_timeout * NSEC_PER_SEC;
4617 if (local_rspt != NULL) {
4619 * we already have an rspt attached to the md, so we'll
4620 * update the deadline on that one.
4622 lnet_rspt_free(rspt, cpt);
4625 rspt->rspt_mdh = mdh;
4626 rspt->rspt_cpt = cpt;
4627 /* store the rspt so we can access it when we get the REPLY */
4628 md->md_rspt_ptr = rspt;
4631 local_rspt->rspt_deadline = ktime_add_ns(ktime_get(), timeout_ns);
4634 * add to the list of tracked responses. It's added to tail of the
4635 * list in order to expire all the older entries first.
4638 list_move_tail(&local_rspt->rspt_on_list, the_lnet.ln_mt_rstq[cpt]);
4639 lnet_net_unlock(cpt);
4640 lnet_res_unlock(cpt);
4644 * Initiate an asynchronous PUT operation.
4646 * There are several events associated with a PUT: completion of the send on
4647 * the initiator node (LNET_EVENT_SEND), and when the send completes
4648 * successfully, the receipt of an acknowledgment (LNET_EVENT_ACK) indicating
4649 * that the operation was accepted by the target. The event LNET_EVENT_PUT is
4650 * used at the target node to indicate the completion of incoming data
4653 * The local events will be logged in the EQ associated with the MD pointed to
4654 * by \a mdh handle. Using a MD without an associated EQ results in these
4655 * events being discarded. In this case, the caller must have another
4656 * mechanism (e.g., a higher level protocol) for determining when it is safe
4657 * to modify the memory region associated with the MD.
4659 * Note that LNet does not guarantee the order of LNET_EVENT_SEND and
4660 * LNET_EVENT_ACK, though intuitively ACK should happen after SEND.
4662 * \param self Indicates the NID of a local interface through which to send
4663 * the PUT request. Use LNET_NID_ANY to let LNet choose one by itself.
4664 * \param mdh A handle for the MD that describes the memory to be sent. The MD
4665 * must be "free floating" (See LNetMDBind()).
4666 * \param ack Controls whether an acknowledgment is requested.
4667 * Acknowledgments are only sent when they are requested by the initiating
4668 * process and the target MD enables them.
4669 * \param target A process identifier for the target process.
4670 * \param portal The index in the \a target's portal table.
4671 * \param match_bits The match bits to use for MD selection at the target
4673 * \param offset The offset into the target MD (only used when the target
4674 * MD has the LNET_MD_MANAGE_REMOTE option set).
4675 * \param hdr_data 64 bits of user data that can be included in the message
4676 * header. This data is written to an event queue entry at the target if an
4677 * EQ is present on the matching MD.
4679 * \retval 0 Success, and only in this case events will be generated
4680 * and logged to EQ (if it exists).
4681 * \retval -EIO Simulated failure.
4682 * \retval -ENOMEM Memory allocation failure.
4683 * \retval -ENOENT Invalid MD object.
4685 * \see struct lnet_event::hdr_data and lnet_event_kind_t.
4688 LNetPut(lnet_nid_t self, struct lnet_handle_md mdh, enum lnet_ack_req ack,
4689 struct lnet_process_id target, unsigned int portal,
4690 __u64 match_bits, unsigned int offset,
4693 struct lnet_msg *msg;
4694 struct lnet_libmd *md;
4697 struct lnet_rsp_tracker *rspt = NULL;
4699 LASSERT(the_lnet.ln_refcount > 0);
4701 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
4702 fail_peer(target.nid, 1)) { /* shall we now? */
4703 CERROR("Dropping PUT to %s: simulated failure\n",
4704 libcfs_id2str(target));
4708 msg = lnet_msg_alloc();
4710 CERROR("Dropping PUT to %s: ENOMEM on struct lnet_msg\n",
4711 libcfs_id2str(target));
4714 msg->msg_vmflush = !!memory_pressure_get();
4716 cpt = lnet_cpt_of_cookie(mdh.cookie);
4718 if (ack == LNET_ACK_REQ) {
4719 rspt = lnet_rspt_alloc(cpt);
4721 CERROR("Dropping PUT to %s: ENOMEM on response tracker\n",
4722 libcfs_id2str(target));
4725 INIT_LIST_HEAD(&rspt->rspt_on_list);
4730 md = lnet_handle2md(&mdh);
4731 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
4732 CERROR("Dropping PUT (%llu:%d:%s): MD (%d) invalid\n",
4733 match_bits, portal, libcfs_id2str(target),
4734 md == NULL ? -1 : md->md_threshold);
4735 if (md != NULL && md->md_me != NULL)
4736 CERROR("Source MD also attached to portal %d\n",
4737 md->md_me->me_portal);
4738 lnet_res_unlock(cpt);
4740 lnet_rspt_free(rspt, cpt);
4745 CDEBUG(D_NET, "LNetPut -> %s\n", libcfs_id2str(target));
4747 lnet_msg_attach_md(msg, md, 0, 0);
4749 lnet_prep_send(msg, LNET_MSG_PUT, target, 0, md->md_length);
4751 msg->msg_hdr.msg.put.match_bits = cpu_to_le64(match_bits);
4752 msg->msg_hdr.msg.put.ptl_index = cpu_to_le32(portal);
4753 msg->msg_hdr.msg.put.offset = cpu_to_le32(offset);
4754 msg->msg_hdr.msg.put.hdr_data = hdr_data;
4756 /* NB handles only looked up by creator (no flips) */
4757 if (ack == LNET_ACK_REQ) {
4758 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
4759 the_lnet.ln_interface_cookie;
4760 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
4761 md->md_lh.lh_cookie;
4763 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
4764 LNET_WIRE_HANDLE_COOKIE_NONE;
4765 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
4766 LNET_WIRE_HANDLE_COOKIE_NONE;
4769 lnet_res_unlock(cpt);
4771 lnet_build_msg_event(msg, LNET_EVENT_SEND);
4773 if (ack == LNET_ACK_REQ)
4774 lnet_attach_rsp_tracker(rspt, cpt, md, mdh);
4776 if (CFS_FAIL_CHECK_ORSET(CFS_FAIL_PTLRPC_OST_BULK_CB2,
4780 rc = lnet_send(self, msg, LNET_NID_ANY);
4783 CNETERR("Error sending PUT to %s: %d\n",
4784 libcfs_id2str(target), rc);
4785 msg->msg_no_resend = true;
4786 lnet_finalize(msg, rc);
4789 /* completion will be signalled by an event */
4792 EXPORT_SYMBOL(LNetPut);
4795 * The LND can DMA direct to the GET md (i.e. no REPLY msg). This
4796 * returns a msg for the LND to pass to lnet_finalize() when the sink
4797 * data has been received.
4799 * CAVEAT EMPTOR: 'getmsg' is the original GET, which is freed when
4800 * lnet_finalize() is called on it, so the LND must call this first
4803 lnet_create_reply_msg(struct lnet_ni *ni, struct lnet_msg *getmsg)
4805 struct lnet_msg *msg = lnet_msg_alloc();
4806 struct lnet_libmd *getmd = getmsg->msg_md;
4807 struct lnet_process_id peer_id = getmsg->msg_target;
4810 LASSERT(!getmsg->msg_target_is_router);
4811 LASSERT(!getmsg->msg_routing);
4814 CERROR("%s: Dropping REPLY from %s: can't allocate msg\n",
4815 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id));
4819 cpt = lnet_cpt_of_cookie(getmd->md_lh.lh_cookie);
4822 LASSERT(getmd->md_refcount > 0);
4824 if (getmd->md_threshold == 0) {
4825 CERROR("%s: Dropping REPLY from %s for inactive MD %p\n",
4826 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id),
4828 lnet_res_unlock(cpt);
4832 LASSERT(getmd->md_offset == 0);
4834 CDEBUG(D_NET, "%s: Reply from %s md %p\n",
4835 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id), getmd);
4837 /* setup information for lnet_build_msg_event */
4838 msg->msg_initiator = getmsg->msg_txpeer->lpni_peer_net->lpn_peer->lp_primary_nid;
4839 msg->msg_from = peer_id.nid;
4840 msg->msg_type = LNET_MSG_GET; /* flag this msg as an "optimized" GET */
4841 msg->msg_hdr.src_nid = peer_id.nid;
4842 msg->msg_hdr.payload_length = getmd->md_length;
4843 msg->msg_receiving = 1; /* required by lnet_msg_attach_md */
4845 lnet_msg_attach_md(msg, getmd, getmd->md_offset, getmd->md_length);
4846 lnet_res_unlock(cpt);
4848 cpt = lnet_cpt_of_nid(peer_id.nid, ni);
4851 lnet_msg_commit(msg, cpt);
4852 lnet_net_unlock(cpt);
4854 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
4859 cpt = lnet_cpt_of_nid(peer_id.nid, ni);
4862 lnet_incr_stats(&ni->ni_stats, LNET_MSG_GET, LNET_STATS_TYPE_DROP);
4863 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
4864 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length +=
4866 lnet_net_unlock(cpt);
4873 EXPORT_SYMBOL(lnet_create_reply_msg);
4876 lnet_set_reply_msg_len(struct lnet_ni *ni, struct lnet_msg *reply,
4879 /* Set the REPLY length, now the RDMA that elides the REPLY message has
4880 * completed and I know it. */
4881 LASSERT(reply != NULL);
4882 LASSERT(reply->msg_type == LNET_MSG_GET);
4883 LASSERT(reply->msg_ev.type == LNET_EVENT_REPLY);
4885 /* NB I trusted my peer to RDMA. If she tells me she's written beyond
4886 * the end of my buffer, I might as well be dead. */
4887 LASSERT(len <= reply->msg_ev.mlength);
4889 reply->msg_ev.mlength = len;
4891 EXPORT_SYMBOL(lnet_set_reply_msg_len);
4894 * Initiate an asynchronous GET operation.
4896 * On the initiator node, an LNET_EVENT_SEND is logged when the GET request
4897 * is sent, and an LNET_EVENT_REPLY is logged when the data returned from
4898 * the target node in the REPLY has been written to local MD.
4900 * On the target node, an LNET_EVENT_GET is logged when the GET request
4901 * arrives and is accepted into a MD.
4903 * \param self,target,portal,match_bits,offset See the discussion in LNetPut().
4904 * \param mdh A handle for the MD that describes the memory into which the
4905 * requested data will be received. The MD must be "free floating" (See LNetMDBind()).
4907 * \retval 0 Success, and only in this case events will be generated
4908 * and logged to EQ (if it exists) of the MD.
4909 * \retval -EIO Simulated failure.
4910 * \retval -ENOMEM Memory allocation failure.
4911 * \retval -ENOENT Invalid MD object.
4914 LNetGet(lnet_nid_t self, struct lnet_handle_md mdh,
4915 struct lnet_process_id target, unsigned int portal,
4916 __u64 match_bits, unsigned int offset, bool recovery)
4918 struct lnet_msg *msg;
4919 struct lnet_libmd *md;
4920 struct lnet_rsp_tracker *rspt;
4924 LASSERT(the_lnet.ln_refcount > 0);
4926 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
4927 fail_peer(target.nid, 1)) /* shall we now? */
4929 CERROR("Dropping GET to %s: simulated failure\n",
4930 libcfs_id2str(target));
4934 msg = lnet_msg_alloc();
4936 CERROR("Dropping GET to %s: ENOMEM on struct lnet_msg\n",
4937 libcfs_id2str(target));
4941 cpt = lnet_cpt_of_cookie(mdh.cookie);
4943 rspt = lnet_rspt_alloc(cpt);
4945 CERROR("Dropping GET to %s: ENOMEM on response tracker\n",
4946 libcfs_id2str(target));
4949 INIT_LIST_HEAD(&rspt->rspt_on_list);
4951 msg->msg_recovery = recovery;
4955 md = lnet_handle2md(&mdh);
4956 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
4957 CERROR("Dropping GET (%llu:%d:%s): MD (%d) invalid\n",
4958 match_bits, portal, libcfs_id2str(target),
4959 md == NULL ? -1 : md->md_threshold);
4960 if (md != NULL && md->md_me != NULL)
4961 CERROR("REPLY MD also attached to portal %d\n",
4962 md->md_me->me_portal);
4964 lnet_res_unlock(cpt);
4967 lnet_rspt_free(rspt, cpt);
4971 CDEBUG(D_NET, "LNetGet -> %s\n", libcfs_id2str(target));
4973 lnet_msg_attach_md(msg, md, 0, 0);
4975 lnet_prep_send(msg, LNET_MSG_GET, target, 0, 0);
4977 msg->msg_hdr.msg.get.match_bits = cpu_to_le64(match_bits);
4978 msg->msg_hdr.msg.get.ptl_index = cpu_to_le32(portal);
4979 msg->msg_hdr.msg.get.src_offset = cpu_to_le32(offset);
4980 msg->msg_hdr.msg.get.sink_length = cpu_to_le32(md->md_length);
4982 /* NB handles only looked up by creator (no flips) */
4983 msg->msg_hdr.msg.get.return_wmd.wh_interface_cookie =
4984 the_lnet.ln_interface_cookie;
4985 msg->msg_hdr.msg.get.return_wmd.wh_object_cookie =
4986 md->md_lh.lh_cookie;
4988 lnet_res_unlock(cpt);
4990 lnet_build_msg_event(msg, LNET_EVENT_SEND);
4992 lnet_attach_rsp_tracker(rspt, cpt, md, mdh);
4994 rc = lnet_send(self, msg, LNET_NID_ANY);
4996 CNETERR("Error sending GET to %s: %d\n",
4997 libcfs_id2str(target), rc);
4998 msg->msg_no_resend = true;
4999 lnet_finalize(msg, rc);
5002 /* completion will be signalled by an event */
5005 EXPORT_SYMBOL(LNetGet);
5008 * Calculate distance to node at \a dstnid.
5010 * \param dstnid Target NID.
5011 * \param srcnidp If not NULL, NID of the local interface to reach \a dstnid
5013 * \param orderp If not NULL, order of the route to reach \a dstnid is saved
5016 * \retval 0 If \a dstnid belongs to a local interface, and reserved option
5017 * local_nid_dist_zero is set, which is the default.
5018 * \retval positives Distance to target NID, i.e. number of hops plus one.
5019 * \retval -EHOSTUNREACH If \a dstnid is not reachable.
5022 LNetDist(lnet_nid_t dstnid, lnet_nid_t *srcnidp, __u32 *orderp)
5024 struct list_head *e;
5025 struct lnet_ni *ni = NULL;
5026 struct lnet_remotenet *rnet;
5027 __u32 dstnet = LNET_NIDNET(dstnid);
5031 struct list_head *rn_list;
5033 /* if !local_nid_dist_zero, I don't return a distance of 0 ever
5034 * (when lustre sees a distance of 0, it substitutes 0@lo), so I
5035 * keep order 0 free for 0@lo and order 1 free for a local NID
5038 LASSERT(the_lnet.ln_refcount > 0);
5040 cpt = lnet_net_lock_current();
5042 while ((ni = lnet_get_next_ni_locked(NULL, ni))) {
5043 if (ni->ni_nid == dstnid) {
5044 if (srcnidp != NULL)
5046 if (orderp != NULL) {
5047 if (LNET_NETTYP(LNET_NIDNET(dstnid)) == LOLND)
5052 lnet_net_unlock(cpt);
5054 return local_nid_dist_zero ? 0 : 1;
5057 if (LNET_NIDNET(ni->ni_nid) == dstnet) {
5058 /* Check if ni was originally created in
5059 * current net namespace.
5060 * If not, assign order above 0xffff0000,
5061 * to make this ni not a priority. */
5062 if (current->nsproxy &&
5063 !net_eq(ni->ni_net_ns, current->nsproxy->net_ns))
5064 order += 0xffff0000;
5065 if (srcnidp != NULL)
5066 *srcnidp = ni->ni_nid;
5069 lnet_net_unlock(cpt);
5076 rn_list = lnet_net2rnethash(dstnet);
5077 list_for_each(e, rn_list) {
5078 rnet = list_entry(e, struct lnet_remotenet, lrn_list);
5080 if (rnet->lrn_net == dstnet) {
5081 struct lnet_route *route;
5082 struct lnet_route *shortest = NULL;
5083 __u32 shortest_hops = LNET_UNDEFINED_HOPS;
5086 LASSERT(!list_empty(&rnet->lrn_routes));
5088 list_for_each_entry(route, &rnet->lrn_routes,
5090 route_hops = route->lr_hops;
5091 if (route_hops == LNET_UNDEFINED_HOPS)
5093 if (shortest == NULL ||
5094 route_hops < shortest_hops) {
5096 shortest_hops = route_hops;
5100 LASSERT(shortest != NULL);
5101 hops = shortest_hops;
5102 if (srcnidp != NULL) {
5103 struct lnet_net *net;
5104 net = lnet_get_net_locked(shortest->lr_lnet);
5106 ni = lnet_get_next_ni_locked(net, NULL);
5107 *srcnidp = ni->ni_nid;
5111 lnet_net_unlock(cpt);
5117 lnet_net_unlock(cpt);
5118 return -EHOSTUNREACH;
5120 EXPORT_SYMBOL(LNetDist);