4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lnet/lnet/lib-move.c
34 * Data movement routines
37 #define DEBUG_SUBSYSTEM S_LNET
39 #include <linux/pagemap.h>
41 #include <lnet/lib-lnet.h>
42 #include <linux/nsproxy.h>
43 #include <net/net_namespace.h>
45 static int local_nid_dist_zero = 1;
46 module_param(local_nid_dist_zero, int, 0444);
47 MODULE_PARM_DESC(local_nid_dist_zero, "Reserved");
49 struct lnet_send_data {
50 struct lnet_ni *sd_best_ni;
51 struct lnet_peer_ni *sd_best_lpni;
52 struct lnet_peer_ni *sd_final_dst_lpni;
53 struct lnet_peer *sd_peer;
54 struct lnet_peer *sd_gw_peer;
55 struct lnet_peer_ni *sd_gw_lpni;
56 struct lnet_peer_net *sd_peer_net;
57 struct lnet_msg *sd_msg;
58 lnet_nid_t sd_dst_nid;
59 lnet_nid_t sd_src_nid;
60 lnet_nid_t sd_rtr_nid;
66 static inline struct lnet_comm_count *
67 get_stats_counts(struct lnet_element_stats *stats,
68 enum lnet_stats_type stats_type)
71 case LNET_STATS_TYPE_SEND:
72 return &stats->el_send_stats;
73 case LNET_STATS_TYPE_RECV:
74 return &stats->el_recv_stats;
75 case LNET_STATS_TYPE_DROP:
76 return &stats->el_drop_stats;
78 CERROR("Unknown stats type\n");
84 void lnet_incr_stats(struct lnet_element_stats *stats,
85 enum lnet_msg_type msg_type,
86 enum lnet_stats_type stats_type)
88 struct lnet_comm_count *counts = get_stats_counts(stats, stats_type);
94 atomic_inc(&counts->co_ack_count);
97 atomic_inc(&counts->co_put_count);
100 atomic_inc(&counts->co_get_count);
103 atomic_inc(&counts->co_reply_count);
106 atomic_inc(&counts->co_hello_count);
109 CERROR("There is a BUG in the code. Unknown message type\n");
114 __u32 lnet_sum_stats(struct lnet_element_stats *stats,
115 enum lnet_stats_type stats_type)
117 struct lnet_comm_count *counts = get_stats_counts(stats, stats_type);
121 return (atomic_read(&counts->co_ack_count) +
122 atomic_read(&counts->co_put_count) +
123 atomic_read(&counts->co_get_count) +
124 atomic_read(&counts->co_reply_count) +
125 atomic_read(&counts->co_hello_count));
128 static inline void assign_stats(struct lnet_ioctl_comm_count *msg_stats,
129 struct lnet_comm_count *counts)
131 msg_stats->ico_get_count = atomic_read(&counts->co_get_count);
132 msg_stats->ico_put_count = atomic_read(&counts->co_put_count);
133 msg_stats->ico_reply_count = atomic_read(&counts->co_reply_count);
134 msg_stats->ico_ack_count = atomic_read(&counts->co_ack_count);
135 msg_stats->ico_hello_count = atomic_read(&counts->co_hello_count);
138 void lnet_usr_translate_stats(struct lnet_ioctl_element_msg_stats *msg_stats,
139 struct lnet_element_stats *stats)
141 struct lnet_comm_count *counts;
146 counts = get_stats_counts(stats, LNET_STATS_TYPE_SEND);
149 assign_stats(&msg_stats->im_send_stats, counts);
151 counts = get_stats_counts(stats, LNET_STATS_TYPE_RECV);
154 assign_stats(&msg_stats->im_recv_stats, counts);
156 counts = get_stats_counts(stats, LNET_STATS_TYPE_DROP);
159 assign_stats(&msg_stats->im_drop_stats, counts);
163 lnet_fail_nid(lnet_nid_t nid, unsigned int threshold)
165 struct lnet_test_peer *tp;
166 struct list_head *el;
167 struct list_head *next;
168 struct list_head cull;
170 /* NB: use lnet_net_lock(0) to serialize operations on test peers */
171 if (threshold != 0) {
172 /* Adding a new entry */
173 LIBCFS_ALLOC(tp, sizeof(*tp));
178 tp->tp_threshold = threshold;
181 list_add_tail(&tp->tp_list, &the_lnet.ln_test_peers);
186 /* removing entries */
187 INIT_LIST_HEAD(&cull);
191 list_for_each_safe(el, next, &the_lnet.ln_test_peers) {
192 tp = list_entry(el, struct lnet_test_peer, tp_list);
194 if (tp->tp_threshold == 0 || /* needs culling anyway */
195 nid == LNET_NID_ANY || /* removing all entries */
196 tp->tp_nid == nid) { /* matched this one */
197 list_move(&tp->tp_list, &cull);
203 while (!list_empty(&cull)) {
204 tp = list_entry(cull.next, struct lnet_test_peer, tp_list);
206 list_del(&tp->tp_list);
207 LIBCFS_FREE(tp, sizeof(*tp));
213 fail_peer (lnet_nid_t nid, int outgoing)
215 struct lnet_test_peer *tp;
216 struct list_head *el;
217 struct list_head *next;
218 struct list_head cull;
221 INIT_LIST_HEAD(&cull);
223 /* NB: use lnet_net_lock(0) to serialize operations on test peers */
226 list_for_each_safe(el, next, &the_lnet.ln_test_peers) {
227 tp = list_entry(el, struct lnet_test_peer, tp_list);
229 if (tp->tp_threshold == 0) {
232 /* only cull zombies on outgoing tests,
233 * since we may be at interrupt priority on
234 * incoming messages. */
235 list_move(&tp->tp_list, &cull);
240 if (tp->tp_nid == LNET_NID_ANY || /* fail every peer */
241 nid == tp->tp_nid) { /* fail this peer */
244 if (tp->tp_threshold != LNET_MD_THRESH_INF) {
247 tp->tp_threshold == 0) {
249 list_move(&tp->tp_list, &cull);
258 while (!list_empty(&cull)) {
259 tp = list_entry(cull.next, struct lnet_test_peer, tp_list);
260 list_del(&tp->tp_list);
262 LIBCFS_FREE(tp, sizeof(*tp));
269 lnet_iov_nob(unsigned int niov, struct kvec *iov)
271 unsigned int nob = 0;
273 LASSERT(niov == 0 || iov != NULL);
275 nob += (iov++)->iov_len;
279 EXPORT_SYMBOL(lnet_iov_nob);
282 lnet_copy_iov2iov(unsigned int ndiov, struct kvec *diov, unsigned int doffset,
283 unsigned int nsiov, struct kvec *siov, unsigned int soffset,
286 /* NB diov, siov are READ-ONLY */
287 unsigned int this_nob;
292 /* skip complete frags before 'doffset' */
294 while (doffset >= diov->iov_len) {
295 doffset -= diov->iov_len;
301 /* skip complete frags before 'soffset' */
303 while (soffset >= siov->iov_len) {
304 soffset -= siov->iov_len;
313 this_nob = MIN(diov->iov_len - doffset,
314 siov->iov_len - soffset);
315 this_nob = MIN(this_nob, nob);
317 memcpy((char *)diov->iov_base + doffset,
318 (char *)siov->iov_base + soffset, this_nob);
321 if (diov->iov_len > doffset + this_nob) {
329 if (siov->iov_len > soffset + this_nob) {
338 EXPORT_SYMBOL(lnet_copy_iov2iov);
341 lnet_extract_iov(int dst_niov, struct kvec *dst,
342 int src_niov, struct kvec *src,
343 unsigned int offset, unsigned int len)
345 /* Initialise 'dst' to the subset of 'src' starting at 'offset',
346 * for exactly 'len' bytes, and return the number of entries.
347 * NB not destructive to 'src' */
348 unsigned int frag_len;
351 if (len == 0) /* no data => */
352 return (0); /* no frags */
354 LASSERT(src_niov > 0);
355 while (offset >= src->iov_len) { /* skip initial frags */
356 offset -= src->iov_len;
359 LASSERT(src_niov > 0);
364 LASSERT(src_niov > 0);
365 LASSERT((int)niov <= dst_niov);
367 frag_len = src->iov_len - offset;
368 dst->iov_base = ((char *)src->iov_base) + offset;
370 if (len <= frag_len) {
375 dst->iov_len = frag_len;
385 EXPORT_SYMBOL(lnet_extract_iov);
389 lnet_kiov_nob(unsigned int niov, lnet_kiov_t *kiov)
391 unsigned int nob = 0;
393 LASSERT(niov == 0 || kiov != NULL);
395 nob += (kiov++)->kiov_len;
399 EXPORT_SYMBOL(lnet_kiov_nob);
402 lnet_copy_kiov2kiov(unsigned int ndiov, lnet_kiov_t *diov, unsigned int doffset,
403 unsigned int nsiov, lnet_kiov_t *siov, unsigned int soffset,
406 /* NB diov, siov are READ-ONLY */
407 unsigned int this_nob;
414 LASSERT (!in_interrupt ());
417 while (doffset >= diov->kiov_len) {
418 doffset -= diov->kiov_len;
425 while (soffset >= siov->kiov_len) {
426 soffset -= siov->kiov_len;
435 this_nob = MIN(diov->kiov_len - doffset,
436 siov->kiov_len - soffset);
437 this_nob = MIN(this_nob, nob);
440 daddr = ((char *)kmap(diov->kiov_page)) +
441 diov->kiov_offset + doffset;
443 saddr = ((char *)kmap(siov->kiov_page)) +
444 siov->kiov_offset + soffset;
446 /* Vanishing risk of kmap deadlock when mapping 2 pages.
447 * However in practice at least one of the kiovs will be mapped
448 * kernel pages and the map/unmap will be NOOPs */
450 memcpy (daddr, saddr, this_nob);
453 if (diov->kiov_len > doffset + this_nob) {
457 kunmap(diov->kiov_page);
464 if (siov->kiov_len > soffset + this_nob) {
468 kunmap(siov->kiov_page);
477 kunmap(diov->kiov_page);
479 kunmap(siov->kiov_page);
481 EXPORT_SYMBOL(lnet_copy_kiov2kiov);
484 lnet_copy_kiov2iov (unsigned int niov, struct kvec *iov, unsigned int iovoffset,
485 unsigned int nkiov, lnet_kiov_t *kiov, unsigned int kiovoffset,
488 /* NB iov, kiov are READ-ONLY */
489 unsigned int this_nob;
495 LASSERT (!in_interrupt ());
498 while (iovoffset >= iov->iov_len) {
499 iovoffset -= iov->iov_len;
506 while (kiovoffset >= kiov->kiov_len) {
507 kiovoffset -= kiov->kiov_len;
516 this_nob = MIN(iov->iov_len - iovoffset,
517 kiov->kiov_len - kiovoffset);
518 this_nob = MIN(this_nob, nob);
521 addr = ((char *)kmap(kiov->kiov_page)) +
522 kiov->kiov_offset + kiovoffset;
524 memcpy((char *)iov->iov_base + iovoffset, addr, this_nob);
527 if (iov->iov_len > iovoffset + this_nob) {
528 iovoffset += this_nob;
535 if (kiov->kiov_len > kiovoffset + this_nob) {
537 kiovoffset += this_nob;
539 kunmap(kiov->kiov_page);
549 kunmap(kiov->kiov_page);
551 EXPORT_SYMBOL(lnet_copy_kiov2iov);
554 lnet_copy_iov2kiov(unsigned int nkiov, lnet_kiov_t *kiov, unsigned int kiovoffset,
555 unsigned int niov, struct kvec *iov, unsigned int iovoffset,
558 /* NB kiov, iov are READ-ONLY */
559 unsigned int this_nob;
565 LASSERT (!in_interrupt ());
568 while (kiovoffset >= kiov->kiov_len) {
569 kiovoffset -= kiov->kiov_len;
576 while (iovoffset >= iov->iov_len) {
577 iovoffset -= iov->iov_len;
586 this_nob = MIN(kiov->kiov_len - kiovoffset,
587 iov->iov_len - iovoffset);
588 this_nob = MIN(this_nob, nob);
591 addr = ((char *)kmap(kiov->kiov_page)) +
592 kiov->kiov_offset + kiovoffset;
594 memcpy (addr, (char *)iov->iov_base + iovoffset, this_nob);
597 if (kiov->kiov_len > kiovoffset + this_nob) {
599 kiovoffset += this_nob;
601 kunmap(kiov->kiov_page);
608 if (iov->iov_len > iovoffset + this_nob) {
609 iovoffset += this_nob;
618 kunmap(kiov->kiov_page);
620 EXPORT_SYMBOL(lnet_copy_iov2kiov);
623 lnet_extract_kiov(int dst_niov, lnet_kiov_t *dst,
624 int src_niov, lnet_kiov_t *src,
625 unsigned int offset, unsigned int len)
627 /* Initialise 'dst' to the subset of 'src' starting at 'offset',
628 * for exactly 'len' bytes, and return the number of entries.
629 * NB not destructive to 'src' */
630 unsigned int frag_len;
633 if (len == 0) /* no data => */
634 return (0); /* no frags */
636 LASSERT(src_niov > 0);
637 while (offset >= src->kiov_len) { /* skip initial frags */
638 offset -= src->kiov_len;
641 LASSERT(src_niov > 0);
646 LASSERT(src_niov > 0);
647 LASSERT((int)niov <= dst_niov);
649 frag_len = src->kiov_len - offset;
650 dst->kiov_page = src->kiov_page;
651 dst->kiov_offset = src->kiov_offset + offset;
653 if (len <= frag_len) {
655 LASSERT(dst->kiov_offset + dst->kiov_len <= PAGE_SIZE);
659 dst->kiov_len = frag_len;
660 LASSERT(dst->kiov_offset + dst->kiov_len <= PAGE_SIZE);
670 EXPORT_SYMBOL(lnet_extract_kiov);
673 lnet_ni_recv(struct lnet_ni *ni, void *private, struct lnet_msg *msg,
674 int delayed, unsigned int offset, unsigned int mlen,
677 unsigned int niov = 0;
678 struct kvec *iov = NULL;
679 lnet_kiov_t *kiov = NULL;
682 LASSERT (!in_interrupt ());
683 LASSERT (mlen == 0 || msg != NULL);
686 LASSERT(msg->msg_receiving);
687 LASSERT(!msg->msg_sending);
688 LASSERT(rlen == msg->msg_len);
689 LASSERT(mlen <= msg->msg_len);
690 LASSERT(msg->msg_offset == offset);
691 LASSERT(msg->msg_wanted == mlen);
693 msg->msg_receiving = 0;
696 niov = msg->msg_niov;
698 kiov = msg->msg_kiov;
701 LASSERT ((iov == NULL) != (kiov == NULL));
705 rc = (ni->ni_net->net_lnd->lnd_recv)(ni, private, msg, delayed,
706 niov, iov, kiov, offset, mlen,
709 lnet_finalize(msg, rc);
713 lnet_setpayloadbuffer(struct lnet_msg *msg)
715 struct lnet_libmd *md = msg->msg_md;
717 LASSERT(msg->msg_len > 0);
718 LASSERT(!msg->msg_routing);
720 LASSERT(msg->msg_niov == 0);
721 LASSERT(msg->msg_iov == NULL);
722 LASSERT(msg->msg_kiov == NULL);
724 msg->msg_niov = md->md_niov;
725 if ((md->md_options & LNET_MD_KIOV) != 0)
726 msg->msg_kiov = md->md_iov.kiov;
728 msg->msg_iov = md->md_iov.iov;
732 lnet_prep_send(struct lnet_msg *msg, int type, struct lnet_process_id target,
733 unsigned int offset, unsigned int len)
735 msg->msg_type = type;
736 msg->msg_target = target;
738 msg->msg_offset = offset;
741 lnet_setpayloadbuffer(msg);
743 memset (&msg->msg_hdr, 0, sizeof (msg->msg_hdr));
744 msg->msg_hdr.type = cpu_to_le32(type);
745 /* dest_nid will be overwritten by lnet_select_pathway() */
746 msg->msg_hdr.dest_nid = cpu_to_le64(target.nid);
747 msg->msg_hdr.dest_pid = cpu_to_le32(target.pid);
748 /* src_nid will be set later */
749 msg->msg_hdr.src_pid = cpu_to_le32(the_lnet.ln_pid);
750 msg->msg_hdr.payload_length = cpu_to_le32(len);
754 lnet_ni_send(struct lnet_ni *ni, struct lnet_msg *msg)
756 void *priv = msg->msg_private;
759 LASSERT (!in_interrupt ());
760 LASSERT (LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND ||
761 (msg->msg_txcredit && msg->msg_peertxcredit));
763 rc = (ni->ni_net->net_lnd->lnd_send)(ni, priv, msg);
765 msg->msg_no_resend = true;
766 lnet_finalize(msg, rc);
771 lnet_ni_eager_recv(struct lnet_ni *ni, struct lnet_msg *msg)
775 LASSERT(!msg->msg_sending);
776 LASSERT(msg->msg_receiving);
777 LASSERT(!msg->msg_rx_ready_delay);
778 LASSERT(ni->ni_net->net_lnd->lnd_eager_recv != NULL);
780 msg->msg_rx_ready_delay = 1;
781 rc = (ni->ni_net->net_lnd->lnd_eager_recv)(ni, msg->msg_private, msg,
784 CERROR("recv from %s / send to %s aborted: "
785 "eager_recv failed %d\n",
786 libcfs_nid2str(msg->msg_rxpeer->lpni_nid),
787 libcfs_id2str(msg->msg_target), rc);
788 LASSERT(rc < 0); /* required by my callers */
795 lnet_is_peer_deadline_passed(struct lnet_peer_ni *lpni, time64_t now)
799 deadline = lpni->lpni_last_alive +
800 lpni->lpni_net->net_tunables.lct_peer_timeout;
803 * assume peer_ni is alive as long as we're within the configured
812 /* NB: returns 1 when alive, 0 when dead, negative when error;
813 * may drop the lnet_net_lock */
815 lnet_peer_alive_locked(struct lnet_ni *ni, struct lnet_peer_ni *lpni,
816 struct lnet_msg *msg)
818 time64_t now = ktime_get_seconds();
820 if (!lnet_peer_aliveness_enabled(lpni))
824 * If we're resending a message, let's attempt to send it even if
825 * the peer is down to fulfill our resend quota on the message
827 if (msg->msg_retry_count > 0)
830 /* try and send recovery messages irregardless */
831 if (msg->msg_recovery)
834 /* always send any responses */
835 if (msg->msg_type == LNET_MSG_ACK ||
836 msg->msg_type == LNET_MSG_REPLY)
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) {
1272 struct list_head drop;
1273 INIT_LIST_HEAD(&drop);
1274 list_splice_init(&lp->lp_rtrq, &drop);
1275 spin_unlock(&lp->lp_lock);
1276 spin_unlock(&rxpeerni->lpni_lock);
1277 lnet_drop_routed_msgs_locked(&drop, msg->msg_rx_cpt);
1278 } else if (!list_empty(&lp->lp_rtrq)) {
1281 msg2 = list_entry(lp->lp_rtrq.next,
1282 struct lnet_msg, msg_list);
1283 list_del(&msg2->msg_list);
1284 msg2_cpt = msg2->msg_rx_cpt;
1285 spin_unlock(&lp->lp_lock);
1286 spin_unlock(&rxpeerni->lpni_lock);
1288 * messages on the lp_rtrq can be from any NID in
1289 * the peer, which means they might have different
1290 * cpts. We need to make sure we lock the right
1293 if (msg2_cpt != msg->msg_rx_cpt) {
1294 lnet_net_unlock(msg->msg_rx_cpt);
1295 lnet_net_lock(msg2_cpt);
1297 (void) lnet_post_routed_recv_locked(msg2, 1);
1298 if (msg2_cpt != msg->msg_rx_cpt) {
1299 lnet_net_unlock(msg2_cpt);
1300 lnet_net_lock(msg->msg_rx_cpt);
1303 spin_unlock(&lp->lp_lock);
1304 spin_unlock(&rxpeerni->lpni_lock);
1308 msg->msg_rxni = NULL;
1309 lnet_ni_decref_locked(rxni, msg->msg_rx_cpt);
1311 if (rxpeerni != NULL) {
1312 msg->msg_rxpeer = NULL;
1313 lnet_peer_ni_decref_locked(rxpeerni);
1318 lnet_compare_gw_lpnis(struct lnet_peer_ni *p1, struct lnet_peer_ni *p2)
1320 if (p1->lpni_txqnob < p2->lpni_txqnob)
1323 if (p1->lpni_txqnob > p2->lpni_txqnob)
1326 if (p1->lpni_txcredits > p2->lpni_txcredits)
1329 if (p1->lpni_txcredits < p2->lpni_txcredits)
1335 static struct lnet_peer_ni *
1336 lnet_select_peer_ni(struct lnet_ni *best_ni, lnet_nid_t dst_nid,
1337 struct lnet_peer *peer,
1338 struct lnet_peer_net *peer_net)
1341 * Look at the peer NIs for the destination peer that connect
1342 * to the chosen net. If a peer_ni is preferred when using the
1343 * best_ni to communicate, we use that one. If there is no
1344 * preferred peer_ni, or there are multiple preferred peer_ni,
1345 * the available transmit credits are used. If the transmit
1346 * credits are equal, we round-robin over the peer_ni.
1348 struct lnet_peer_ni *lpni = NULL;
1349 struct lnet_peer_ni *best_lpni = NULL;
1350 int best_lpni_credits = INT_MIN;
1351 bool preferred = false;
1353 int best_lpni_healthv = 0;
1356 while ((lpni = lnet_get_next_peer_ni_locked(peer, peer_net, lpni))) {
1358 * if the best_ni we've chosen aleady has this lpni
1359 * preferred, then let's use it
1362 ni_is_pref = lnet_peer_is_pref_nid_locked(lpni,
1364 CDEBUG(D_NET, "%s ni_is_pref = %d\n",
1365 libcfs_nid2str(best_ni->ni_nid), ni_is_pref);
1370 lpni_healthv = atomic_read(&lpni->lpni_healthv);
1373 CDEBUG(D_NET, "%s c:[%d, %d], s:[%d, %d]\n",
1374 libcfs_nid2str(lpni->lpni_nid),
1375 lpni->lpni_txcredits, best_lpni_credits,
1376 lpni->lpni_seq, best_lpni->lpni_seq);
1378 /* pick the healthiest peer ni */
1379 if (lpni_healthv < best_lpni_healthv) {
1381 } else if (lpni_healthv > best_lpni_healthv) {
1382 best_lpni_healthv = lpni_healthv;
1383 /* if this is a preferred peer use it */
1384 } else if (!preferred && ni_is_pref) {
1386 } else if (preferred && !ni_is_pref) {
1388 * this is not the preferred peer so let's ignore
1392 } else if (lpni->lpni_txcredits < best_lpni_credits) {
1394 * We already have a peer that has more credits
1395 * available than this one. No need to consider
1396 * this peer further.
1399 } else if (lpni->lpni_txcredits == best_lpni_credits) {
1401 * The best peer found so far and the current peer
1402 * have the same number of available credits let's
1403 * make sure to select between them using Round
1407 if (best_lpni->lpni_seq <= lpni->lpni_seq)
1413 best_lpni_credits = lpni->lpni_txcredits;
1416 /* if we still can't find a peer ni then we can't reach it */
1418 __u32 net_id = (peer_net) ? peer_net->lpn_net_id :
1419 LNET_NIDNET(dst_nid);
1420 CDEBUG(D_NET, "no peer_ni found on peer net %s\n",
1421 libcfs_net2str(net_id));
1425 CDEBUG(D_NET, "sd_best_lpni = %s\n",
1426 libcfs_nid2str(best_lpni->lpni_nid));
1432 * Prerequisite: the best_ni should already be set in the sd
1434 static inline struct lnet_peer_ni *
1435 lnet_find_best_lpni_on_net(struct lnet_ni *lni, lnet_nid_t dst_nid,
1436 struct lnet_peer *peer, __u32 net_id)
1438 struct lnet_peer_net *peer_net;
1441 * The gateway is Multi-Rail capable so now we must select the
1444 peer_net = lnet_peer_get_net_locked(peer, net_id);
1447 CERROR("gateway peer %s has no NI on net %s\n",
1448 libcfs_nid2str(peer->lp_primary_nid),
1449 libcfs_net2str(net_id));
1453 return lnet_select_peer_ni(lni, dst_nid, peer, peer_net);
1456 /* Compare route priorities and hop counts */
1458 lnet_compare_routes(struct lnet_route *r1, struct lnet_route *r2)
1460 int r1_hops = (r1->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r1->lr_hops;
1461 int r2_hops = (r2->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r2->lr_hops;
1463 if (r1->lr_priority < r2->lr_priority)
1466 if (r1->lr_priority > r2->lr_priority)
1469 if (r1_hops < r2_hops)
1472 if (r1_hops > r2_hops)
1478 static struct lnet_route *
1479 lnet_find_route_locked(struct lnet_remotenet *rnet,
1480 struct lnet_route **prev_route,
1481 struct lnet_peer_ni **gwni)
1483 struct lnet_peer_ni *lpni, *best_gw_ni = NULL;
1484 struct lnet_route *best_route;
1485 struct lnet_route *last_route;
1486 struct lnet_route *route;
1489 best_route = last_route = NULL;
1490 list_for_each_entry(route, &rnet->lrn_routes, lr_list) {
1491 if (!lnet_is_route_alive(route))
1495 best_route = last_route = route;
1496 best_gw_ni = lnet_find_best_lpni_on_net(NULL,
1500 LASSERT(best_gw_ni);
1504 /* no protection on below fields, but it's harmless */
1505 if (last_route->lr_seq - route->lr_seq < 0)
1508 rc = lnet_compare_routes(route, best_route);
1512 lpni = lnet_find_best_lpni_on_net(NULL, LNET_NID_ANY,
1523 rc = lnet_compare_gw_lpnis(lpni, best_gw_ni);
1527 if (rc == 1 || route->lr_seq <= best_route->lr_seq) {
1534 *prev_route = last_route;
1540 static struct lnet_ni *
1541 lnet_get_best_ni(struct lnet_net *local_net, struct lnet_ni *best_ni,
1542 struct lnet_peer *peer, struct lnet_peer_net *peer_net,
1545 struct lnet_ni *ni = NULL;
1546 unsigned int shortest_distance;
1551 * If there is no peer_ni that we can send to on this network,
1552 * then there is no point in looking for a new best_ni here.
1554 if (!lnet_get_next_peer_ni_locked(peer, peer_net, NULL))
1557 if (best_ni == NULL) {
1558 shortest_distance = UINT_MAX;
1559 best_credits = INT_MIN;
1562 shortest_distance = cfs_cpt_distance(lnet_cpt_table(), md_cpt,
1563 best_ni->ni_dev_cpt);
1564 best_credits = atomic_read(&best_ni->ni_tx_credits);
1565 best_healthv = atomic_read(&best_ni->ni_healthv);
1568 while ((ni = lnet_get_next_ni_locked(local_net, ni))) {
1569 unsigned int distance;
1574 ni_credits = atomic_read(&ni->ni_tx_credits);
1575 ni_healthv = atomic_read(&ni->ni_healthv);
1576 ni_fatal = atomic_read(&ni->ni_fatal_error_on);
1579 * calculate the distance from the CPT on which
1580 * the message memory is allocated to the CPT of
1581 * the NI's physical device
1583 distance = cfs_cpt_distance(lnet_cpt_table(),
1587 CDEBUG(D_NET, "compare ni %s [c:%d, d:%d, s:%d] with best_ni %s [c:%d, d:%d, s:%d]\n",
1588 libcfs_nid2str(ni->ni_nid), ni_credits, distance,
1589 ni->ni_seq, (best_ni) ? libcfs_nid2str(best_ni->ni_nid)
1590 : "not seleced", best_credits, shortest_distance,
1591 (best_ni) ? best_ni->ni_seq : 0);
1594 * All distances smaller than the NUMA range
1595 * are treated equally.
1597 if (distance < lnet_numa_range)
1598 distance = lnet_numa_range;
1601 * Select on health, shorter distance, available
1602 * credits, then round-robin.
1606 } else if (ni_healthv < best_healthv) {
1608 } else if (ni_healthv > best_healthv) {
1609 best_healthv = ni_healthv;
1611 * If we're going to prefer this ni because it's
1612 * the healthiest, then we should set the
1613 * shortest_distance in the algorithm in case
1614 * there are multiple NIs with the same health but
1615 * different distances.
1617 if (distance < shortest_distance)
1618 shortest_distance = distance;
1619 } else if (distance > shortest_distance) {
1621 } else if (distance < shortest_distance) {
1622 shortest_distance = distance;
1623 } else if (ni_credits < best_credits) {
1625 } else if (ni_credits == best_credits) {
1626 if (best_ni && best_ni->ni_seq <= ni->ni_seq)
1630 best_credits = ni_credits;
1633 CDEBUG(D_NET, "selected best_ni %s\n",
1634 (best_ni) ? libcfs_nid2str(best_ni->ni_nid) : "no selection");
1640 * Traffic to the LNET_RESERVED_PORTAL may not trigger peer discovery,
1641 * because such traffic is required to perform discovery. We therefore
1642 * exclude all GET and PUT on that portal. We also exclude all ACK and
1643 * REPLY traffic, but that is because the portal is not tracked in the
1644 * message structure for these message types. We could restrict this
1645 * further by also checking for LNET_PROTO_PING_MATCHBITS.
1648 lnet_msg_discovery(struct lnet_msg *msg)
1650 if (msg->msg_type == LNET_MSG_PUT) {
1651 if (msg->msg_hdr.msg.put.ptl_index != LNET_RESERVED_PORTAL)
1653 } else if (msg->msg_type == LNET_MSG_GET) {
1654 if (msg->msg_hdr.msg.get.ptl_index != LNET_RESERVED_PORTAL)
1660 #define SRC_SPEC 0x0001
1661 #define SRC_ANY 0x0002
1662 #define LOCAL_DST 0x0004
1663 #define REMOTE_DST 0x0008
1664 #define MR_DST 0x0010
1665 #define NMR_DST 0x0020
1666 #define SND_RESP 0x0040
1668 /* The following to defines are used for return codes */
1669 #define REPEAT_SEND 0x1000
1670 #define PASS_THROUGH 0x2000
1672 /* The different cases lnet_select pathway needs to handle */
1673 #define SRC_SPEC_LOCAL_MR_DST (SRC_SPEC | LOCAL_DST | MR_DST)
1674 #define SRC_SPEC_ROUTER_MR_DST (SRC_SPEC | REMOTE_DST | MR_DST)
1675 #define SRC_SPEC_LOCAL_NMR_DST (SRC_SPEC | LOCAL_DST | NMR_DST)
1676 #define SRC_SPEC_ROUTER_NMR_DST (SRC_SPEC | REMOTE_DST | NMR_DST)
1677 #define SRC_ANY_LOCAL_MR_DST (SRC_ANY | LOCAL_DST | MR_DST)
1678 #define SRC_ANY_ROUTER_MR_DST (SRC_ANY | REMOTE_DST | MR_DST)
1679 #define SRC_ANY_LOCAL_NMR_DST (SRC_ANY | LOCAL_DST | NMR_DST)
1680 #define SRC_ANY_ROUTER_NMR_DST (SRC_ANY | REMOTE_DST | NMR_DST)
1683 lnet_handle_lo_send(struct lnet_send_data *sd)
1685 struct lnet_msg *msg = sd->sd_msg;
1686 int cpt = sd->sd_cpt;
1688 /* No send credit hassles with LOLND */
1689 lnet_ni_addref_locked(the_lnet.ln_loni, cpt);
1690 msg->msg_hdr.dest_nid = cpu_to_le64(the_lnet.ln_loni->ni_nid);
1691 if (!msg->msg_routing)
1692 msg->msg_hdr.src_nid =
1693 cpu_to_le64(the_lnet.ln_loni->ni_nid);
1694 msg->msg_target.nid = the_lnet.ln_loni->ni_nid;
1695 lnet_msg_commit(msg, cpt);
1696 msg->msg_txni = the_lnet.ln_loni;
1698 return LNET_CREDIT_OK;
1702 lnet_handle_send(struct lnet_send_data *sd)
1704 struct lnet_ni *best_ni = sd->sd_best_ni;
1705 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
1706 struct lnet_peer_ni *final_dst_lpni = sd->sd_final_dst_lpni;
1707 struct lnet_msg *msg = sd->sd_msg;
1709 __u32 send_case = sd->sd_send_case;
1711 __u32 routing = send_case & REMOTE_DST;
1712 struct lnet_rsp_tracker *rspt;
1715 * Increment sequence number of the selected peer so that we
1716 * pick the next one in Round Robin.
1718 best_lpni->lpni_seq++;
1721 * grab a reference on the peer_ni so it sticks around even if
1722 * we need to drop and relock the lnet_net_lock below.
1724 lnet_peer_ni_addref_locked(best_lpni);
1727 * Use lnet_cpt_of_nid() to determine the CPT used to commit the
1728 * message. This ensures that we get a CPT that is correct for
1729 * the NI when the NI has been restricted to a subset of all CPTs.
1730 * If the selected CPT differs from the one currently locked, we
1731 * must unlock and relock the lnet_net_lock(), and then check whether
1732 * the configuration has changed. We don't have a hold on the best_ni
1733 * yet, and it may have vanished.
1735 cpt2 = lnet_cpt_of_nid_locked(best_lpni->lpni_nid, best_ni);
1736 if (sd->sd_cpt != cpt2) {
1737 __u32 seq = lnet_get_dlc_seq_locked();
1738 lnet_net_unlock(sd->sd_cpt);
1740 lnet_net_lock(sd->sd_cpt);
1741 if (seq != lnet_get_dlc_seq_locked()) {
1742 lnet_peer_ni_decref_locked(best_lpni);
1748 * store the best_lpni in the message right away to avoid having
1749 * to do the same operation under different conditions
1751 msg->msg_txpeer = best_lpni;
1752 msg->msg_txni = best_ni;
1755 * grab a reference for the best_ni since now it's in use in this
1756 * send. The reference will be dropped in lnet_finalize()
1758 lnet_ni_addref_locked(msg->msg_txni, sd->sd_cpt);
1761 * Always set the target.nid to the best peer picked. Either the
1762 * NID will be one of the peer NIDs selected, or the same NID as
1763 * what was originally set in the target or it will be the NID of
1764 * a router if this message should be routed
1766 msg->msg_target.nid = msg->msg_txpeer->lpni_nid;
1769 * lnet_msg_commit assigns the correct cpt to the message, which
1770 * is used to decrement the correct refcount on the ni when it's
1771 * time to return the credits
1773 lnet_msg_commit(msg, sd->sd_cpt);
1776 * If we are routing the message then we keep the src_nid that was
1777 * set by the originator. If we are not routing then we are the
1778 * originator and set it here.
1780 if (!msg->msg_routing)
1781 msg->msg_hdr.src_nid = cpu_to_le64(msg->msg_txni->ni_nid);
1784 msg->msg_target_is_router = 1;
1785 msg->msg_target.pid = LNET_PID_LUSTRE;
1787 * since we're routing we want to ensure that the
1788 * msg_hdr.dest_nid is set to the final destination. When
1789 * the router receives this message it knows how to route
1792 * final_dst_lpni is set at the beginning of the
1793 * lnet_select_pathway() function and is never changed.
1794 * It's safe to use it here.
1796 msg->msg_hdr.dest_nid = cpu_to_le64(final_dst_lpni->lpni_nid);
1799 * if we're not routing set the dest_nid to the best peer
1800 * ni NID that we picked earlier in the algorithm.
1802 msg->msg_hdr.dest_nid = cpu_to_le64(msg->msg_txpeer->lpni_nid);
1806 * if we have response tracker block update it with the next hop
1810 rspt = msg->msg_md->md_rspt_ptr;
1812 rspt->rspt_next_hop_nid = msg->msg_txpeer->lpni_nid;
1813 CDEBUG(D_NET, "rspt_next_hop_nid = %s\n",
1814 libcfs_nid2str(rspt->rspt_next_hop_nid));
1818 rc = lnet_post_send_locked(msg, 0);
1821 CDEBUG(D_NET, "TRACE: %s(%s:%s) -> %s(%s:%s) %s : %s try# %d\n",
1822 libcfs_nid2str(msg->msg_hdr.src_nid),
1823 libcfs_nid2str(msg->msg_txni->ni_nid),
1824 libcfs_nid2str(sd->sd_src_nid),
1825 libcfs_nid2str(msg->msg_hdr.dest_nid),
1826 libcfs_nid2str(sd->sd_dst_nid),
1827 libcfs_nid2str(msg->msg_txpeer->lpni_nid),
1828 libcfs_nid2str(sd->sd_rtr_nid),
1829 lnet_msgtyp2str(msg->msg_type), msg->msg_retry_count);
1835 lnet_set_non_mr_pref_nid(struct lnet_send_data *sd)
1837 if (sd->sd_send_case & NMR_DST &&
1838 sd->sd_msg->msg_type != LNET_MSG_REPLY &&
1839 sd->sd_msg->msg_type != LNET_MSG_ACK &&
1840 sd->sd_best_lpni->lpni_pref_nnids == 0) {
1841 CDEBUG(D_NET, "Setting preferred local NID %s on NMR peer %s\n",
1842 libcfs_nid2str(sd->sd_best_ni->ni_nid),
1843 libcfs_nid2str(sd->sd_best_lpni->lpni_nid));
1844 lnet_peer_ni_set_non_mr_pref_nid(sd->sd_best_lpni,
1845 sd->sd_best_ni->ni_nid);
1854 * use the source and destination NIDs as the pathway
1857 lnet_handle_spec_local_nmr_dst(struct lnet_send_data *sd)
1859 /* the destination lpni is set before we get here. */
1862 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
1863 if (!sd->sd_best_ni) {
1864 CERROR("Can't send to %s: src %s is not a "
1865 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
1866 libcfs_nid2str(sd->sd_src_nid));
1871 * the preferred NID will only be set for NMR peers
1873 lnet_set_non_mr_pref_nid(sd);
1875 return lnet_handle_send(sd);
1883 * Don't run the selection algorithm on the peer NIs. By specifying the
1884 * local NID, we're also saying that we should always use the destination NID
1885 * provided. This handles the case where we should be using the same
1886 * destination NID for the all the messages which belong to the same RPC
1890 lnet_handle_spec_local_mr_dst(struct lnet_send_data *sd)
1892 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
1893 if (!sd->sd_best_ni) {
1894 CERROR("Can't send to %s: src %s is not a "
1895 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
1896 libcfs_nid2str(sd->sd_src_nid));
1900 if (sd->sd_best_lpni &&
1901 sd->sd_best_lpni->lpni_nid == the_lnet.ln_loni->ni_nid)
1902 return lnet_handle_lo_send(sd);
1903 else if (sd->sd_best_lpni)
1904 return lnet_handle_send(sd);
1906 CERROR("can't send to %s. no NI on %s\n",
1907 libcfs_nid2str(sd->sd_dst_nid),
1908 libcfs_net2str(sd->sd_best_ni->ni_net->net_id));
1910 return -EHOSTUNREACH;
1914 lnet_find_best_ni_on_spec_net(struct lnet_ni *cur_best_ni,
1915 struct lnet_peer *peer,
1916 struct lnet_peer_net *peer_net,
1920 struct lnet_net *local_net;
1921 struct lnet_ni *best_ni;
1923 local_net = lnet_get_net_locked(peer_net->lpn_net_id);
1928 * Iterate through the NIs in this local Net and select
1929 * the NI to send from. The selection is determined by
1930 * these 3 criterion in the following priority:
1932 * 2. NI available credits
1935 best_ni = lnet_get_best_ni(local_net, cur_best_ni,
1936 peer, peer_net, cpt);
1938 if (incr_seq && best_ni)
1945 lnet_initiate_peer_discovery(struct lnet_peer_ni *lpni,
1946 struct lnet_msg *msg, lnet_nid_t rtr_nid,
1949 struct lnet_peer *peer;
1950 lnet_nid_t primary_nid;
1953 lnet_peer_ni_addref_locked(lpni);
1955 peer = lpni->lpni_peer_net->lpn_peer;
1957 if (lnet_peer_gw_discovery(peer)) {
1958 lnet_peer_ni_decref_locked(lpni);
1962 if (!lnet_msg_discovery(msg) || lnet_peer_is_uptodate(peer)) {
1963 lnet_peer_ni_decref_locked(lpni);
1967 rc = lnet_discover_peer_locked(lpni, cpt, false);
1969 lnet_peer_ni_decref_locked(lpni);
1972 /* The peer may have changed. */
1973 peer = lpni->lpni_peer_net->lpn_peer;
1974 spin_lock(&peer->lp_lock);
1975 if (lnet_peer_is_uptodate_locked(peer)) {
1976 spin_unlock(&peer->lp_lock);
1977 lnet_peer_ni_decref_locked(lpni);
1980 /* queue message and return */
1981 msg->msg_rtr_nid_param = rtr_nid;
1982 msg->msg_sending = 0;
1983 msg->msg_txpeer = NULL;
1984 list_add_tail(&msg->msg_list, &peer->lp_dc_pendq);
1985 primary_nid = peer->lp_primary_nid;
1986 spin_unlock(&peer->lp_lock);
1988 lnet_peer_ni_decref_locked(lpni);
1990 CDEBUG(D_NET, "msg %p delayed. %s pending discovery\n",
1991 msg, libcfs_nid2str(primary_nid));
1993 return LNET_DC_WAIT;
1997 lnet_handle_find_routed_path(struct lnet_send_data *sd,
1999 struct lnet_peer_ni **gw_lpni,
2000 struct lnet_peer **gw_peer)
2004 struct lnet_peer *gw;
2005 struct lnet_peer *lp;
2006 struct lnet_peer_net *lpn;
2007 struct lnet_peer_net *best_lpn = NULL;
2008 struct lnet_remotenet *rnet, *best_rnet = NULL;
2009 struct lnet_route *best_route = NULL;
2010 struct lnet_route *last_route = NULL;
2011 struct lnet_peer_ni *lpni = NULL;
2012 struct lnet_peer_ni *gwni = NULL;
2013 lnet_nid_t src_nid = sd->sd_src_nid;
2015 /* If a router nid was specified then we are replying to a GET or
2016 * sending an ACK. In this case we use the gateway associated with the
2017 * specified router nid.
2019 if (sd->sd_rtr_nid != LNET_NID_ANY) {
2020 gwni = lnet_find_peer_ni_locked(sd->sd_rtr_nid);
2022 CERROR("No peer NI for gateway %s\n",
2023 libcfs_nid2str(sd->sd_rtr_nid));
2024 return -EHOSTUNREACH;
2026 gw = gwni->lpni_peer_net->lpn_peer;
2027 lnet_peer_ni_decref_locked(gwni);
2028 local_lnet = LNET_NIDNET(sd->sd_rtr_nid);
2030 /* we've already looked up the initial lpni using dst_nid */
2031 lpni = sd->sd_best_lpni;
2032 /* the peer tree must be in existence */
2033 LASSERT(lpni && lpni->lpni_peer_net &&
2034 lpni->lpni_peer_net->lpn_peer);
2035 lp = lpni->lpni_peer_net->lpn_peer;
2037 list_for_each_entry(lpn, &lp->lp_peer_nets, lpn_peer_nets) {
2038 /* is this remote network reachable? */
2039 rnet = lnet_find_rnet_locked(lpn->lpn_net_id);
2048 if (best_lpn->lpn_seq <= lpn->lpn_seq)
2056 CERROR("peer %s has no available nets\n",
2057 libcfs_nid2str(sd->sd_dst_nid));
2058 return -EHOSTUNREACH;
2061 sd->sd_best_lpni = lnet_find_best_lpni_on_net(sd->sd_best_ni,
2064 best_lpn->lpn_net_id);
2065 if (!sd->sd_best_lpni) {
2066 CERROR("peer %s down\n",
2067 libcfs_nid2str(sd->sd_dst_nid));
2068 return -EHOSTUNREACH;
2071 best_route = lnet_find_route_locked(best_rnet, &last_route,
2074 CERROR("no route to %s from %s\n",
2075 libcfs_nid2str(dst_nid),
2076 libcfs_nid2str(src_nid));
2077 return -EHOSTUNREACH;
2081 CERROR("Internal Error. Route expected to %s from %s\n",
2082 libcfs_nid2str(dst_nid),
2083 libcfs_nid2str(src_nid));
2087 gw = best_route->lr_gateway;
2088 LASSERT(gw == gwni->lpni_peer_net->lpn_peer);
2089 local_lnet = best_route->lr_lnet;
2094 * Discover this gateway if it hasn't already been discovered.
2095 * This means we might delay the message until discovery has
2098 sd->sd_msg->msg_src_nid_param = sd->sd_src_nid;
2099 rc = lnet_initiate_peer_discovery(gwni, sd->sd_msg, sd->sd_rtr_nid,
2104 if (!sd->sd_best_ni)
2105 sd->sd_best_ni = lnet_find_best_ni_on_spec_net(NULL, gw,
2106 lnet_peer_get_net_locked(gw,
2111 if (!sd->sd_best_ni) {
2112 CERROR("Internal Error. Expected local ni on %s but non found :%s\n",
2113 libcfs_net2str(local_lnet),
2114 libcfs_nid2str(sd->sd_src_nid));
2122 * increment the sequence numbers since now we're sure we're
2123 * going to use this path
2125 if (sd->sd_rtr_nid == LNET_NID_ANY) {
2126 LASSERT(best_route && last_route);
2127 best_route->lr_seq = last_route->lr_seq + 1;
2128 best_lpn->lpn_seq++;
2139 * Remote destination
2140 * Non-MR destination
2144 * Remote destination
2147 * The handling of these two cases is similar. Even though the destination
2148 * can be MR or non-MR, we'll deal directly with the router.
2151 lnet_handle_spec_router_dst(struct lnet_send_data *sd)
2154 struct lnet_peer_ni *gw_lpni = NULL;
2155 struct lnet_peer *gw_peer = NULL;
2158 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
2159 if (!sd->sd_best_ni) {
2160 CERROR("Can't send to %s: src %s is not a "
2161 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
2162 libcfs_nid2str(sd->sd_src_nid));
2166 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2171 if (sd->sd_send_case & NMR_DST)
2173 * since the final destination is non-MR let's set its preferred
2174 * NID before we send
2176 lnet_set_non_mr_pref_nid(sd);
2179 * We're going to send to the gw found so let's set its
2182 sd->sd_peer = gw_peer;
2183 sd->sd_best_lpni = gw_lpni;
2185 return lnet_handle_send(sd);
2189 lnet_find_best_ni_on_local_net(struct lnet_peer *peer, int md_cpt,
2192 struct lnet_peer_net *peer_net = NULL;
2193 struct lnet_ni *best_ni = NULL;
2196 * The peer can have multiple interfaces, some of them can be on
2197 * the local network and others on a routed network. We should
2198 * prefer the local network. However if the local network is not
2199 * available then we need to try the routed network
2202 /* go through all the peer nets and find the best_ni */
2203 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_peer_nets) {
2205 * The peer's list of nets can contain non-local nets. We
2206 * want to only examine the local ones.
2208 if (!lnet_get_net_locked(peer_net->lpn_net_id))
2210 best_ni = lnet_find_best_ni_on_spec_net(best_ni, peer,
2211 peer_net, md_cpt, false);
2214 * if this is a discovery message and lp_disc_net_id is
2215 * specified then use that net to send the discovery on.
2217 if (peer->lp_disc_net_id == peer_net->lpn_net_id &&
2223 /* increment sequence number so we can round robin */
2229 static struct lnet_ni *
2230 lnet_find_existing_preferred_best_ni(struct lnet_send_data *sd)
2232 struct lnet_ni *best_ni = NULL;
2233 struct lnet_peer_net *peer_net;
2234 struct lnet_peer *peer = sd->sd_peer;
2235 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
2236 struct lnet_peer_ni *lpni;
2237 int cpt = sd->sd_cpt;
2240 * We must use a consistent source address when sending to a
2241 * non-MR peer. However, a non-MR peer can have multiple NIDs
2242 * on multiple networks, and we may even need to talk to this
2243 * peer on multiple networks -- certain types of
2244 * load-balancing configuration do this.
2246 * So we need to pick the NI the peer prefers for this
2247 * particular network.
2250 /* Get the target peer_ni */
2251 peer_net = lnet_peer_get_net_locked(peer,
2252 LNET_NIDNET(best_lpni->lpni_nid));
2253 LASSERT(peer_net != NULL);
2254 list_for_each_entry(lpni, &peer_net->lpn_peer_nis,
2256 if (lpni->lpni_pref_nnids == 0)
2258 LASSERT(lpni->lpni_pref_nnids == 1);
2259 best_ni = lnet_nid2ni_locked(
2260 lpni->lpni_pref.nid, cpt);
2267 /* Prerequisite: sd->sd_peer and sd->sd_best_lpni should be set */
2269 lnet_select_preferred_best_ni(struct lnet_send_data *sd)
2271 struct lnet_ni *best_ni = NULL;
2272 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
2275 * We must use a consistent source address when sending to a
2276 * non-MR peer. However, a non-MR peer can have multiple NIDs
2277 * on multiple networks, and we may even need to talk to this
2278 * peer on multiple networks -- certain types of
2279 * load-balancing configuration do this.
2281 * So we need to pick the NI the peer prefers for this
2282 * particular network.
2285 best_ni = lnet_find_existing_preferred_best_ni(sd);
2287 /* if best_ni is still not set just pick one */
2290 lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2291 sd->sd_best_lpni->lpni_peer_net,
2292 sd->sd_md_cpt, true);
2293 /* If there is no best_ni we don't have a route */
2295 CERROR("no path to %s from net %s\n",
2296 libcfs_nid2str(best_lpni->lpni_nid),
2297 libcfs_net2str(best_lpni->lpni_net->net_id));
2298 return -EHOSTUNREACH;
2302 sd->sd_best_ni = best_ni;
2304 /* Set preferred NI if necessary. */
2305 lnet_set_non_mr_pref_nid(sd);
2312 * Source not specified
2316 * always use the same source NID for NMR peers
2317 * If we've talked to that peer before then we already have a preferred
2318 * source NI associated with it. Otherwise, we select a preferred local NI
2319 * and store it in the peer
2322 lnet_handle_any_local_nmr_dst(struct lnet_send_data *sd)
2326 /* sd->sd_best_lpni is already set to the final destination */
2329 * At this point we should've created the peer ni and peer. If we
2330 * can't find it, then something went wrong. Instead of assert
2331 * output a relevant message and fail the send
2333 if (!sd->sd_best_lpni) {
2334 CERROR("Internal fault. Unable to send msg %s to %s. "
2336 lnet_msgtyp2str(sd->sd_msg->msg_type),
2337 libcfs_nid2str(sd->sd_dst_nid));
2341 rc = lnet_select_preferred_best_ni(sd);
2343 rc = lnet_handle_send(sd);
2349 lnet_handle_any_mr_dsta(struct lnet_send_data *sd)
2352 * NOTE we've already handled the remote peer case. So we only
2353 * need to worry about the local case here.
2355 * if we're sending a response, ACK or reply, we need to send it
2356 * to the destination NID given to us. At this point we already
2357 * have the peer_ni we're suppose to send to, so just find the
2358 * best_ni on the peer net and use that. Since we're sending to an
2359 * MR peer then we can just run the selection algorithm on our
2360 * local NIs and pick the best one.
2362 if (sd->sd_send_case & SND_RESP) {
2364 lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2365 sd->sd_best_lpni->lpni_peer_net,
2366 sd->sd_md_cpt, true);
2368 if (!sd->sd_best_ni) {
2370 * We're not going to deal with not able to send
2371 * a response to the provided final destination
2373 CERROR("Can't send response to %s. "
2374 "No local NI available\n",
2375 libcfs_nid2str(sd->sd_dst_nid));
2376 return -EHOSTUNREACH;
2379 return lnet_handle_send(sd);
2383 * If we get here that means we're sending a fresh request, PUT or
2384 * GET, so we need to run our standard selection algorithm.
2385 * First find the best local interface that's on any of the peer's
2388 sd->sd_best_ni = lnet_find_best_ni_on_local_net(sd->sd_peer,
2390 lnet_msg_discovery(sd->sd_msg));
2391 if (sd->sd_best_ni) {
2393 lnet_find_best_lpni_on_net(sd->sd_best_ni, sd->sd_dst_nid,
2395 sd->sd_best_ni->ni_net->net_id);
2398 * if we're successful in selecting a peer_ni on the local
2399 * network, then send to it. Otherwise fall through and
2400 * try and see if we can reach it over another routed
2403 if (sd->sd_best_lpni &&
2404 sd->sd_best_lpni->lpni_nid == the_lnet.ln_loni->ni_nid) {
2406 * in case we initially started with a routed
2407 * destination, let's reset to local
2409 sd->sd_send_case &= ~REMOTE_DST;
2410 sd->sd_send_case |= LOCAL_DST;
2411 return lnet_handle_lo_send(sd);
2412 } else if (sd->sd_best_lpni) {
2414 * in case we initially started with a routed
2415 * destination, let's reset to local
2417 sd->sd_send_case &= ~REMOTE_DST;
2418 sd->sd_send_case |= LOCAL_DST;
2419 return lnet_handle_send(sd);
2422 CERROR("Internal Error. Expected to have a best_lpni: "
2424 libcfs_nid2str(sd->sd_src_nid),
2425 libcfs_nid2str(sd->sd_dst_nid));
2431 * Peer doesn't have a local network. Let's see if there is
2432 * a remote network we can reach it on.
2434 return PASS_THROUGH;
2439 * Source NID not specified
2444 * Source NID not speified
2445 * Remote destination
2448 * In both of these cases if we're sending a response, ACK or REPLY, then
2449 * we need to send to the destination NID provided.
2451 * In the remote case let's deal with MR routers.
2456 lnet_handle_any_mr_dst(struct lnet_send_data *sd)
2459 struct lnet_peer *gw_peer = NULL;
2460 struct lnet_peer_ni *gw_lpni = NULL;
2463 * handle sending a response to a remote peer here so we don't
2464 * have to worry about it if we hit lnet_handle_any_mr_dsta()
2466 if (sd->sd_send_case & REMOTE_DST &&
2467 sd->sd_send_case & SND_RESP) {
2468 struct lnet_peer_ni *gw;
2469 struct lnet_peer *gw_peer;
2471 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw,
2474 CERROR("Can't send response to %s. "
2475 "No route available\n",
2476 libcfs_nid2str(sd->sd_dst_nid));
2477 return -EHOSTUNREACH;
2478 } else if (rc > 0) {
2482 sd->sd_best_lpni = gw;
2483 sd->sd_peer = gw_peer;
2485 return lnet_handle_send(sd);
2489 * Even though the NID for the peer might not be on a local network,
2490 * since the peer is MR there could be other interfaces on the
2491 * local network. In that case we'd still like to prefer the local
2492 * network over the routed network. If we're unable to do that
2493 * then we select the best router among the different routed networks,
2494 * and if the router is MR then we can deal with it as such.
2496 rc = lnet_handle_any_mr_dsta(sd);
2497 if (rc != PASS_THROUGH)
2501 * Now that we must route to the destination, we must consider the
2502 * MR case, where the destination has multiple interfaces, some of
2503 * which we can route to and others we do not. For this reason we
2504 * need to select the destination which we can route to and if
2505 * there are multiple, we need to round robin.
2507 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2512 sd->sd_send_case &= ~LOCAL_DST;
2513 sd->sd_send_case |= REMOTE_DST;
2515 sd->sd_peer = gw_peer;
2516 sd->sd_best_lpni = gw_lpni;
2518 return lnet_handle_send(sd);
2522 * Source not specified
2523 * Remote destination
2526 * Must send to the specified peer NID using the same source NID that
2527 * we've used before. If it's the first time to talk to that peer then
2528 * find the source NI and assign it as preferred to that peer
2531 lnet_handle_any_router_nmr_dst(struct lnet_send_data *sd)
2534 struct lnet_peer_ni *gw_lpni = NULL;
2535 struct lnet_peer *gw_peer = NULL;
2538 * Let's set if we have a preferred NI to talk to this NMR peer
2540 sd->sd_best_ni = lnet_find_existing_preferred_best_ni(sd);
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);
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 sending to ourselves then there is no need to go through
2639 * any selection. We can shortcut the entire process and send over
2642 * However, we make two exceptions to this rule:
2643 * 1. If the src_nid is specified then our API defines that we must send
2644 * via that interface.
2645 * 2. Recovery messages must be sent to the lnet_ni that is being
2648 send_data.sd_msg = msg;
2649 send_data.sd_cpt = cpt;
2650 if (src_nid == LNET_NID_ANY && !msg->msg_recovery &&
2651 lnet_nid2ni_locked(dst_nid, cpt)) {
2652 rc = lnet_handle_lo_send(&send_data);
2653 lnet_net_unlock(cpt);
2658 * find an existing peer_ni, or create one and mark it as having been
2659 * created due to network traffic. This call will create the
2660 * peer->peer_net->peer_ni tree.
2662 lpni = lnet_nid2peerni_locked(dst_nid, LNET_NID_ANY, cpt);
2664 lnet_net_unlock(cpt);
2665 return PTR_ERR(lpni);
2669 * Cache the original src_nid. If we need to resend the message
2670 * then we'll need to know whether the src_nid was originally
2671 * specified for this message. If it was originally specified,
2672 * then we need to keep using the same src_nid since it's
2673 * continuing the same sequence of messages.
2675 msg->msg_src_nid_param = src_nid;
2678 * If necessary, perform discovery on the peer that owns this peer_ni.
2679 * Note, this can result in the ownership of this peer_ni changing
2680 * to another peer object.
2682 rc = lnet_initiate_peer_discovery(lpni, msg, rtr_nid, cpt);
2684 lnet_peer_ni_decref_locked(lpni);
2685 lnet_net_unlock(cpt);
2688 lnet_peer_ni_decref_locked(lpni);
2690 peer = lpni->lpni_peer_net->lpn_peer;
2693 * Identify the different send cases
2695 if (src_nid == LNET_NID_ANY)
2696 send_case |= SRC_ANY;
2698 send_case |= SRC_SPEC;
2700 if (lnet_get_net_locked(LNET_NIDNET(dst_nid)))
2701 send_case |= LOCAL_DST;
2703 send_case |= REMOTE_DST;
2706 * if this is a non-MR peer or if we're recovering a peer ni then
2707 * let's consider this an NMR case so we can hit the destination
2710 if (!lnet_peer_is_multi_rail(peer) || msg->msg_recovery)
2711 send_case |= NMR_DST;
2713 send_case |= MR_DST;
2715 if (msg->msg_type == LNET_MSG_REPLY ||
2716 msg->msg_type == LNET_MSG_ACK)
2717 send_case |= SND_RESP;
2719 /* assign parameters to the send_data */
2720 send_data.sd_rtr_nid = rtr_nid;
2721 send_data.sd_src_nid = src_nid;
2722 send_data.sd_dst_nid = dst_nid;
2723 send_data.sd_best_lpni = lpni;
2725 * keep a pointer to the final destination in case we're going to
2726 * route, so we'll need to access it later
2728 send_data.sd_final_dst_lpni = lpni;
2729 send_data.sd_peer = peer;
2730 send_data.sd_md_cpt = md_cpt;
2731 send_data.sd_send_case = send_case;
2733 rc = lnet_handle_send_case_locked(&send_data);
2736 * Update the local cpt since send_data.sd_cpt might've been
2737 * updated as a result of calling lnet_handle_send_case_locked().
2739 cpt = send_data.sd_cpt;
2741 if (rc == REPEAT_SEND)
2744 lnet_net_unlock(cpt);
2750 lnet_send(lnet_nid_t src_nid, struct lnet_msg *msg, lnet_nid_t rtr_nid)
2752 lnet_nid_t dst_nid = msg->msg_target.nid;
2756 * NB: rtr_nid is set to LNET_NID_ANY for all current use-cases,
2757 * but we might want to use pre-determined router for ACK/REPLY
2760 /* NB: ni != NULL == interface pre-determined (ACK/REPLY) */
2761 LASSERT(msg->msg_txpeer == NULL);
2762 LASSERT(msg->msg_txni == NULL);
2763 LASSERT(!msg->msg_sending);
2764 LASSERT(!msg->msg_target_is_router);
2765 LASSERT(!msg->msg_receiving);
2767 msg->msg_sending = 1;
2769 LASSERT(!msg->msg_tx_committed);
2771 rc = lnet_select_pathway(src_nid, dst_nid, msg, rtr_nid);
2773 if (rc == -EHOSTUNREACH)
2774 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_ERROR;
2776 msg->msg_health_status = LNET_MSG_STATUS_LOCAL_ERROR;
2780 if (rc == LNET_CREDIT_OK)
2781 lnet_ni_send(msg->msg_txni, msg);
2783 /* rc == LNET_CREDIT_OK or LNET_CREDIT_WAIT or LNET_DC_WAIT */
2787 enum lnet_mt_event_type {
2788 MT_TYPE_LOCAL_NI = 0,
2792 struct lnet_mt_event_info {
2793 enum lnet_mt_event_type mt_type;
2797 /* called with res_lock held */
2799 lnet_detach_rsp_tracker(struct lnet_libmd *md, int cpt)
2801 struct lnet_rsp_tracker *rspt;
2804 * msg has a refcount on the MD so the MD is not going away.
2805 * The rspt queue for the cpt is protected by
2806 * the lnet_net_lock(cpt). cpt is the cpt of the MD cookie.
2808 if (!md->md_rspt_ptr)
2811 rspt = md->md_rspt_ptr;
2814 LASSERT(rspt->rspt_cpt == cpt);
2816 md->md_rspt_ptr = NULL;
2818 if (LNetMDHandleIsInvalid(rspt->rspt_mdh)) {
2820 * The monitor thread has invalidated this handle because the
2821 * response timed out, but it failed to lookup the MD. That
2822 * means this response tracker is on the zombie list. We can
2823 * safely remove it under the resource lock (held by caller) and
2824 * free the response tracker block.
2826 list_del(&rspt->rspt_on_list);
2827 lnet_rspt_free(rspt, cpt);
2830 * invalidate the handle to indicate that a response has been
2831 * received, which will then lead the monitor thread to clean up
2834 LNetInvalidateMDHandle(&rspt->rspt_mdh);
2839 lnet_clean_zombie_rstqs(void)
2841 struct lnet_rsp_tracker *rspt, *tmp;
2844 cfs_cpt_for_each(i, lnet_cpt_table()) {
2845 list_for_each_entry_safe(rspt, tmp,
2846 the_lnet.ln_mt_zombie_rstqs[i],
2848 list_del(&rspt->rspt_on_list);
2849 lnet_rspt_free(rspt, i);
2853 cfs_percpt_free(the_lnet.ln_mt_zombie_rstqs);
2857 lnet_finalize_expired_responses(void)
2859 struct lnet_libmd *md;
2860 struct list_head local_queue;
2861 struct lnet_rsp_tracker *rspt, *tmp;
2865 if (the_lnet.ln_mt_rstq == NULL)
2868 cfs_cpt_for_each(i, lnet_cpt_table()) {
2869 INIT_LIST_HEAD(&local_queue);
2872 if (!the_lnet.ln_mt_rstq[i]) {
2876 list_splice_init(the_lnet.ln_mt_rstq[i], &local_queue);
2881 list_for_each_entry_safe(rspt, tmp, &local_queue, rspt_on_list) {
2883 * The rspt mdh will be invalidated when a response
2884 * is received or whenever we want to discard the
2885 * block the monitor thread will walk the queue
2886 * and clean up any rsts with an invalid mdh.
2887 * The monitor thread will walk the queue until
2888 * the first unexpired rspt block. This means that
2889 * some rspt blocks which received their
2890 * corresponding responses will linger in the
2891 * queue until they are cleaned up eventually.
2894 if (LNetMDHandleIsInvalid(rspt->rspt_mdh)) {
2896 list_del(&rspt->rspt_on_list);
2897 lnet_rspt_free(rspt, i);
2901 if (ktime_compare(now, rspt->rspt_deadline) >= 0 ||
2902 the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN) {
2903 struct lnet_peer_ni *lpni;
2906 md = lnet_handle2md(&rspt->rspt_mdh);
2908 /* MD has been queued for unlink, but
2909 * rspt hasn't been detached (Note we've
2910 * checked above that the rspt_mdh is
2911 * valid). Since we cannot lookup the MD
2912 * we're unable to detach the rspt
2913 * ourselves. Thus, move the rspt to the
2914 * zombie list where we'll wait for
2916 * 1. The remaining operations on the
2917 * MD to complete. In this case the
2918 * final operation will result in
2919 * lnet_msg_detach_md()->
2920 * lnet_detach_rsp_tracker() where
2921 * we will clean up this response
2923 * 2. LNet to shutdown. In this case
2924 * we'll wait until after all LND Nets
2925 * have shutdown and then we can
2926 * safely free any remaining response
2927 * tracker blocks on the zombie list.
2928 * Note: We need to hold the resource
2929 * lock when adding to the zombie list
2930 * because we may have concurrent access
2931 * with lnet_detach_rsp_tracker().
2933 LNetInvalidateMDHandle(&rspt->rspt_mdh);
2934 list_move(&rspt->rspt_on_list,
2935 the_lnet.ln_mt_zombie_rstqs[i]);
2939 LASSERT(md->md_rspt_ptr == rspt);
2940 md->md_rspt_ptr = NULL;
2943 LNetMDUnlink(rspt->rspt_mdh);
2945 nid = rspt->rspt_next_hop_nid;
2947 list_del(&rspt->rspt_on_list);
2948 lnet_rspt_free(rspt, i);
2950 /* If we're shutting down we just want to clean
2951 * up the rspt blocks
2953 if (the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN)
2957 the_lnet.ln_counters[i]->lct_health.lch_response_timeout_count++;
2961 "Response timeout: md = %p: nid = %s\n",
2962 md, libcfs_nid2str(nid));
2965 * If there is a timeout on the response
2966 * from the next hop decrement its health
2967 * value so that we don't use it
2970 lpni = lnet_find_peer_ni_locked(nid);
2972 lnet_handle_remote_failure_locked(lpni);
2973 lnet_peer_ni_decref_locked(lpni);
2982 if (!list_empty(&local_queue)) {
2984 list_splice(&local_queue, the_lnet.ln_mt_rstq[i]);
2991 lnet_resend_pending_msgs_locked(struct list_head *resendq, int cpt)
2993 struct lnet_msg *msg;
2995 while (!list_empty(resendq)) {
2996 struct lnet_peer_ni *lpni;
2998 msg = list_entry(resendq->next, struct lnet_msg,
3001 list_del_init(&msg->msg_list);
3003 lpni = lnet_find_peer_ni_locked(msg->msg_hdr.dest_nid);
3005 lnet_net_unlock(cpt);
3006 CERROR("Expected that a peer is already created for %s\n",
3007 libcfs_nid2str(msg->msg_hdr.dest_nid));
3008 msg->msg_no_resend = true;
3009 lnet_finalize(msg, -EFAULT);
3012 struct lnet_peer *peer;
3014 lnet_nid_t src_nid = LNET_NID_ANY;
3017 * if this message is not being routed and the
3018 * peer is non-MR then we must use the same
3019 * src_nid that was used in the original send.
3020 * Otherwise if we're routing the message (IE
3021 * we're a router) then we can use any of our
3022 * local interfaces. It doesn't matter to the
3023 * final destination.
3025 peer = lpni->lpni_peer_net->lpn_peer;
3026 if (!msg->msg_routing &&
3027 !lnet_peer_is_multi_rail(peer))
3028 src_nid = le64_to_cpu(msg->msg_hdr.src_nid);
3031 * If we originally specified a src NID, then we
3032 * must attempt to reuse it in the resend as well.
3034 if (msg->msg_src_nid_param != LNET_NID_ANY)
3035 src_nid = msg->msg_src_nid_param;
3036 lnet_peer_ni_decref_locked(lpni);
3038 lnet_net_unlock(cpt);
3039 CDEBUG(D_NET, "resending %s->%s: %s recovery %d try# %d\n",
3040 libcfs_nid2str(src_nid),
3041 libcfs_id2str(msg->msg_target),
3042 lnet_msgtyp2str(msg->msg_type),
3044 msg->msg_retry_count);
3045 rc = lnet_send(src_nid, msg, LNET_NID_ANY);
3047 CERROR("Error sending %s to %s: %d\n",
3048 lnet_msgtyp2str(msg->msg_type),
3049 libcfs_id2str(msg->msg_target), rc);
3050 msg->msg_no_resend = true;
3051 lnet_finalize(msg, rc);
3055 the_lnet.ln_counters[cpt]->lct_health.lch_resend_count++;
3061 lnet_resend_pending_msgs(void)
3065 cfs_cpt_for_each(i, lnet_cpt_table()) {
3067 lnet_resend_pending_msgs_locked(the_lnet.ln_mt_resendqs[i], i);
3072 /* called with cpt and ni_lock held */
3074 lnet_unlink_ni_recovery_mdh_locked(struct lnet_ni *ni, int cpt, bool force)
3076 struct lnet_handle_md recovery_mdh;
3078 LNetInvalidateMDHandle(&recovery_mdh);
3080 if (ni->ni_recovery_state & LNET_NI_RECOVERY_PENDING ||
3082 recovery_mdh = ni->ni_ping_mdh;
3083 LNetInvalidateMDHandle(&ni->ni_ping_mdh);
3086 lnet_net_unlock(cpt);
3087 if (!LNetMDHandleIsInvalid(recovery_mdh))
3088 LNetMDUnlink(recovery_mdh);
3094 lnet_recover_local_nis(void)
3096 struct lnet_mt_event_info *ev_info;
3097 struct list_head processed_list;
3098 struct list_head local_queue;
3099 struct lnet_handle_md mdh;
3100 struct lnet_ni *tmp;
3106 INIT_LIST_HEAD(&local_queue);
3107 INIT_LIST_HEAD(&processed_list);
3110 * splice the recovery queue on a local queue. We will iterate
3111 * through the local queue and update it as needed. Once we're
3112 * done with the traversal, we'll splice the local queue back on
3113 * the head of the ln_mt_localNIRecovq. Any newly added local NIs
3114 * will be traversed in the next iteration.
3117 list_splice_init(&the_lnet.ln_mt_localNIRecovq,
3121 list_for_each_entry_safe(ni, tmp, &local_queue, ni_recovery) {
3123 * if an NI is being deleted or it is now healthy, there
3124 * is no need to keep it around in the recovery queue.
3125 * The monitor thread is the only thread responsible for
3126 * removing the NI from the recovery queue.
3127 * Multiple threads can be adding NIs to the recovery
3130 healthv = atomic_read(&ni->ni_healthv);
3134 if (ni->ni_state != LNET_NI_STATE_ACTIVE ||
3135 healthv == LNET_MAX_HEALTH_VALUE) {
3136 list_del_init(&ni->ni_recovery);
3137 lnet_unlink_ni_recovery_mdh_locked(ni, 0, false);
3139 lnet_ni_decref_locked(ni, 0);
3145 * if the local NI failed recovery we must unlink the md.
3146 * But we want to keep the local_ni on the recovery queue
3147 * so we can continue the attempts to recover it.
3149 if (ni->ni_recovery_state & LNET_NI_RECOVERY_FAILED) {
3150 lnet_unlink_ni_recovery_mdh_locked(ni, 0, true);
3151 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_FAILED;
3158 CDEBUG(D_NET, "attempting to recover local ni: %s\n",
3159 libcfs_nid2str(ni->ni_nid));
3162 if (!(ni->ni_recovery_state & LNET_NI_RECOVERY_PENDING)) {
3163 ni->ni_recovery_state |= LNET_NI_RECOVERY_PENDING;
3166 LIBCFS_ALLOC(ev_info, sizeof(*ev_info));
3168 CERROR("out of memory. Can't recover %s\n",
3169 libcfs_nid2str(ni->ni_nid));
3171 ni->ni_recovery_state &=
3172 ~LNET_NI_RECOVERY_PENDING;
3177 mdh = ni->ni_ping_mdh;
3179 * Invalidate the ni mdh in case it's deleted.
3180 * We'll unlink the mdh in this case below.
3182 LNetInvalidateMDHandle(&ni->ni_ping_mdh);
3186 * remove the NI from the local queue and drop the
3187 * reference count to it while we're recovering
3188 * it. The reason for that, is that the NI could
3189 * be deleted, and the way the code is structured
3190 * is if we don't drop the NI, then the deletion
3191 * code will enter a loop waiting for the
3192 * reference count to be removed while holding the
3193 * ln_mutex_lock(). When we look up the peer to
3194 * send to in lnet_select_pathway() we will try to
3195 * lock the ln_mutex_lock() as well, leading to
3196 * a deadlock. By dropping the refcount and
3197 * removing it from the list, we allow for the NI
3198 * to be removed, then we use the cached NID to
3199 * look it up again. If it's gone, then we just
3200 * continue examining the rest of the queue.
3203 list_del_init(&ni->ni_recovery);
3204 lnet_ni_decref_locked(ni, 0);
3207 ev_info->mt_type = MT_TYPE_LOCAL_NI;
3208 ev_info->mt_nid = nid;
3209 rc = lnet_send_ping(nid, &mdh, LNET_INTERFACES_MIN,
3210 ev_info, the_lnet.ln_mt_eqh, true);
3211 /* lookup the nid again */
3213 ni = lnet_nid2ni_locked(nid, 0);
3216 * the NI has been deleted when we dropped
3224 * Same note as in lnet_recover_peer_nis(). When
3225 * we're sending the ping, the NI is free to be
3226 * deleted or manipulated. By this point it
3227 * could've been added back on the recovery queue,
3228 * and a refcount taken on it.
3229 * So we can't just add it blindly again or we'll
3230 * corrupt the queue. We must check under lock if
3231 * it's not on any list and if not then add it
3232 * to the processed list, which will eventually be
3233 * spliced back on to the recovery queue.
3235 ni->ni_ping_mdh = mdh;
3236 if (list_empty(&ni->ni_recovery)) {
3237 list_add_tail(&ni->ni_recovery, &processed_list);
3238 lnet_ni_addref_locked(ni, 0);
3244 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_PENDING;
3250 * put back the remaining NIs on the ln_mt_localNIRecovq to be
3251 * reexamined in the next iteration.
3253 list_splice_init(&processed_list, &local_queue);
3255 list_splice(&local_queue, &the_lnet.ln_mt_localNIRecovq);
3260 lnet_resendqs_create(void)
3262 struct list_head **resendqs;
3263 resendqs = lnet_create_array_of_queues();
3268 lnet_net_lock(LNET_LOCK_EX);
3269 the_lnet.ln_mt_resendqs = resendqs;
3270 lnet_net_unlock(LNET_LOCK_EX);
3276 lnet_clean_local_ni_recoveryq(void)
3280 /* This is only called when the monitor thread has stopped */
3283 while (!list_empty(&the_lnet.ln_mt_localNIRecovq)) {
3284 ni = list_entry(the_lnet.ln_mt_localNIRecovq.next,
3285 struct lnet_ni, ni_recovery);
3286 list_del_init(&ni->ni_recovery);
3288 lnet_unlink_ni_recovery_mdh_locked(ni, 0, true);
3290 lnet_ni_decref_locked(ni, 0);
3297 lnet_unlink_lpni_recovery_mdh_locked(struct lnet_peer_ni *lpni, int cpt,
3300 struct lnet_handle_md recovery_mdh;
3302 LNetInvalidateMDHandle(&recovery_mdh);
3304 if (lpni->lpni_state & LNET_PEER_NI_RECOVERY_PENDING || force) {
3305 recovery_mdh = lpni->lpni_recovery_ping_mdh;
3306 LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
3308 spin_unlock(&lpni->lpni_lock);
3309 lnet_net_unlock(cpt);
3310 if (!LNetMDHandleIsInvalid(recovery_mdh))
3311 LNetMDUnlink(recovery_mdh);
3313 spin_lock(&lpni->lpni_lock);
3317 lnet_clean_peer_ni_recoveryq(void)
3319 struct lnet_peer_ni *lpni, *tmp;
3321 lnet_net_lock(LNET_LOCK_EX);
3323 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_mt_peerNIRecovq,
3325 list_del_init(&lpni->lpni_recovery);
3326 spin_lock(&lpni->lpni_lock);
3327 lnet_unlink_lpni_recovery_mdh_locked(lpni, LNET_LOCK_EX, true);
3328 spin_unlock(&lpni->lpni_lock);
3329 lnet_peer_ni_decref_locked(lpni);
3332 lnet_net_unlock(LNET_LOCK_EX);
3336 lnet_clean_resendqs(void)
3338 struct lnet_msg *msg, *tmp;
3339 struct list_head msgs;
3342 INIT_LIST_HEAD(&msgs);
3344 cfs_cpt_for_each(i, lnet_cpt_table()) {
3346 list_splice_init(the_lnet.ln_mt_resendqs[i], &msgs);
3348 list_for_each_entry_safe(msg, tmp, &msgs, msg_list) {
3349 list_del_init(&msg->msg_list);
3350 msg->msg_no_resend = true;
3351 lnet_finalize(msg, -ESHUTDOWN);
3355 cfs_percpt_free(the_lnet.ln_mt_resendqs);
3359 lnet_recover_peer_nis(void)
3361 struct lnet_mt_event_info *ev_info;
3362 struct list_head processed_list;
3363 struct list_head local_queue;
3364 struct lnet_handle_md mdh;
3365 struct lnet_peer_ni *lpni;
3366 struct lnet_peer_ni *tmp;
3371 INIT_LIST_HEAD(&local_queue);
3372 INIT_LIST_HEAD(&processed_list);
3375 * Always use cpt 0 for locking across all interactions with
3376 * ln_mt_peerNIRecovq
3379 list_splice_init(&the_lnet.ln_mt_peerNIRecovq,
3383 list_for_each_entry_safe(lpni, tmp, &local_queue,
3386 * The same protection strategy is used here as is in the
3387 * local recovery case.
3390 healthv = atomic_read(&lpni->lpni_healthv);
3391 spin_lock(&lpni->lpni_lock);
3392 if (lpni->lpni_state & LNET_PEER_NI_DELETING ||
3393 healthv == LNET_MAX_HEALTH_VALUE) {
3394 list_del_init(&lpni->lpni_recovery);
3395 lnet_unlink_lpni_recovery_mdh_locked(lpni, 0, false);
3396 spin_unlock(&lpni->lpni_lock);
3397 lnet_peer_ni_decref_locked(lpni);
3403 * If the peer NI has failed recovery we must unlink the
3404 * md. But we want to keep the peer ni on the recovery
3405 * queue so we can try to continue recovering it
3407 if (lpni->lpni_state & LNET_PEER_NI_RECOVERY_FAILED) {
3408 lnet_unlink_lpni_recovery_mdh_locked(lpni, 0, true);
3409 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_FAILED;
3412 spin_unlock(&lpni->lpni_lock);
3416 * NOTE: we're racing with peer deletion from user space.
3417 * It's possible that a peer is deleted after we check its
3418 * state. In this case the recovery can create a new peer
3420 spin_lock(&lpni->lpni_lock);
3421 if (!(lpni->lpni_state & LNET_PEER_NI_RECOVERY_PENDING) &&
3422 !(lpni->lpni_state & LNET_PEER_NI_DELETING)) {
3423 lpni->lpni_state |= LNET_PEER_NI_RECOVERY_PENDING;
3424 spin_unlock(&lpni->lpni_lock);
3426 LIBCFS_ALLOC(ev_info, sizeof(*ev_info));
3428 CERROR("out of memory. Can't recover %s\n",
3429 libcfs_nid2str(lpni->lpni_nid));
3430 spin_lock(&lpni->lpni_lock);
3431 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3432 spin_unlock(&lpni->lpni_lock);
3436 /* look at the comments in lnet_recover_local_nis() */
3437 mdh = lpni->lpni_recovery_ping_mdh;
3438 LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
3439 nid = lpni->lpni_nid;
3441 list_del_init(&lpni->lpni_recovery);
3442 lnet_peer_ni_decref_locked(lpni);
3445 ev_info->mt_type = MT_TYPE_PEER_NI;
3446 ev_info->mt_nid = nid;
3447 rc = lnet_send_ping(nid, &mdh, LNET_INTERFACES_MIN,
3448 ev_info, the_lnet.ln_mt_eqh, true);
3451 * lnet_find_peer_ni_locked() grabs a refcount for
3452 * us. No need to take it explicitly.
3454 lpni = lnet_find_peer_ni_locked(nid);
3461 lpni->lpni_recovery_ping_mdh = mdh;
3463 * While we're unlocked the lpni could've been
3464 * readded on the recovery queue. In this case we
3465 * don't need to add it to the local queue, since
3466 * it's already on there and the thread that added
3467 * it would've incremented the refcount on the
3468 * peer, which means we need to decref the refcount
3469 * that was implicitly grabbed by find_peer_ni_locked.
3470 * Otherwise, if the lpni is still not on
3471 * the recovery queue, then we'll add it to the
3474 if (list_empty(&lpni->lpni_recovery))
3475 list_add_tail(&lpni->lpni_recovery, &processed_list);
3477 lnet_peer_ni_decref_locked(lpni);
3480 spin_lock(&lpni->lpni_lock);
3482 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3484 spin_unlock(&lpni->lpni_lock);
3487 list_splice_init(&processed_list, &local_queue);
3489 list_splice(&local_queue, &the_lnet.ln_mt_peerNIRecovq);
3494 lnet_monitor_thread(void *arg)
3496 time64_t recovery_timeout = 0;
3497 time64_t rsp_timeout = 0;
3501 wait_for_completion(&the_lnet.ln_started);
3503 * The monitor thread takes care of the following:
3504 * 1. Checks the aliveness of routers
3505 * 2. Checks if there are messages on the resend queue to resend
3507 * 3. Check if there are any NIs on the local recovery queue and
3509 * 4. Checks if there are any NIs on the remote recovery queue
3512 cfs_block_allsigs();
3514 while (the_lnet.ln_mt_state == LNET_MT_STATE_RUNNING) {
3515 now = ktime_get_real_seconds();
3517 if (lnet_router_checker_active())
3518 lnet_check_routers();
3520 lnet_resend_pending_msgs();
3522 if (now >= rsp_timeout) {
3523 lnet_finalize_expired_responses();
3524 rsp_timeout = now + (lnet_transaction_timeout / 2);
3527 if (now >= recovery_timeout) {
3528 lnet_recover_local_nis();
3529 lnet_recover_peer_nis();
3530 recovery_timeout = now + lnet_recovery_interval;
3534 * TODO do we need to check if we should sleep without
3535 * timeout? Technically, an active system will always
3536 * have messages in flight so this check will always
3537 * evaluate to false. And on an idle system do we care
3538 * if we wake up every 1 second? Although, we've seen
3539 * cases where we get a complaint that an idle thread
3540 * is waking up unnecessarily.
3542 * Take into account the current net_count when you wake
3543 * up for alive router checking, since we need to check
3544 * possibly as many networks as we have configured.
3546 interval = min(lnet_recovery_interval,
3547 min((unsigned int) alive_router_check_interval /
3548 lnet_current_net_count,
3549 lnet_transaction_timeout / 2));
3550 wait_for_completion_interruptible_timeout(
3551 &the_lnet.ln_mt_wait_complete,
3552 cfs_time_seconds(interval));
3553 /* Must re-init the completion before testing anything,
3554 * including ln_mt_state.
3556 reinit_completion(&the_lnet.ln_mt_wait_complete);
3560 lnet_net_lock(LNET_LOCK_EX);
3561 the_lnet.ln_mt_state = LNET_MT_STATE_SHUTDOWN;
3562 lnet_net_unlock(LNET_LOCK_EX);
3564 /* signal that the monitor thread is exiting */
3565 up(&the_lnet.ln_mt_signal);
3573 * Returns == 0 if success
3574 * Returns > 0 if LNetMDBind or prior fails
3575 * Returns < 0 if LNetGet fails
3578 lnet_send_ping(lnet_nid_t dest_nid,
3579 struct lnet_handle_md *mdh, int nnis,
3580 void *user_data, struct lnet_handle_eq eqh, bool recovery)
3582 struct lnet_md md = { NULL };
3583 struct lnet_process_id id;
3584 struct lnet_ping_buffer *pbuf;
3587 if (dest_nid == LNET_NID_ANY) {
3592 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
3598 /* initialize md content */
3599 md.start = &pbuf->pb_info;
3600 md.length = LNET_PING_INFO_SIZE(nnis);
3601 md.threshold = 2; /* GET/REPLY */
3603 md.options = LNET_MD_TRUNCATE;
3604 md.user_ptr = user_data;
3607 rc = LNetMDBind(md, LNET_UNLINK, mdh);
3609 lnet_ping_buffer_decref(pbuf);
3610 CERROR("Can't bind MD: %d\n", rc);
3611 rc = -rc; /* change the rc to positive */
3614 id.pid = LNET_PID_LUSTRE;
3617 rc = LNetGet(LNET_NID_ANY, *mdh, id,
3618 LNET_RESERVED_PORTAL,
3619 LNET_PROTO_PING_MATCHBITS, 0, recovery);
3622 goto fail_unlink_md;
3628 LNetInvalidateMDHandle(mdh);
3634 lnet_handle_recovery_reply(struct lnet_mt_event_info *ev_info,
3635 int status, bool unlink_event)
3637 lnet_nid_t nid = ev_info->mt_nid;
3639 if (ev_info->mt_type == MT_TYPE_LOCAL_NI) {
3643 ni = lnet_nid2ni_locked(nid, 0);
3649 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_PENDING;
3651 ni->ni_recovery_state |= LNET_NI_RECOVERY_FAILED;
3656 CERROR("local NI (%s) recovery failed with %d\n",
3657 libcfs_nid2str(nid), status);
3661 * need to increment healthv for the ni here, because in
3662 * the lnet_finalize() path we don't have access to this
3663 * NI. And in order to get access to it, we'll need to
3664 * carry forward too much information.
3665 * In the peer case, it'll naturally be incremented
3668 lnet_inc_healthv(&ni->ni_healthv);
3670 struct lnet_peer_ni *lpni;
3673 cpt = lnet_net_lock_current();
3674 lpni = lnet_find_peer_ni_locked(nid);
3676 lnet_net_unlock(cpt);
3679 spin_lock(&lpni->lpni_lock);
3680 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3682 lpni->lpni_state |= LNET_PEER_NI_RECOVERY_FAILED;
3683 spin_unlock(&lpni->lpni_lock);
3684 lnet_peer_ni_decref_locked(lpni);
3685 lnet_net_unlock(cpt);
3688 CERROR("peer NI (%s) recovery failed with %d\n",
3689 libcfs_nid2str(nid), status);
3694 lnet_mt_event_handler(struct lnet_event *event)
3696 struct lnet_mt_event_info *ev_info = event->md.user_ptr;
3697 struct lnet_ping_buffer *pbuf;
3699 /* TODO: remove assert */
3700 LASSERT(event->type == LNET_EVENT_REPLY ||
3701 event->type == LNET_EVENT_SEND ||
3702 event->type == LNET_EVENT_UNLINK);
3704 CDEBUG(D_NET, "Received event: %d status: %d\n", event->type,
3707 switch (event->type) {
3708 case LNET_EVENT_UNLINK:
3709 CDEBUG(D_NET, "%s recovery ping unlinked\n",
3710 libcfs_nid2str(ev_info->mt_nid));
3712 case LNET_EVENT_REPLY:
3713 lnet_handle_recovery_reply(ev_info, event->status,
3714 event->type == LNET_EVENT_UNLINK);
3716 case LNET_EVENT_SEND:
3717 CDEBUG(D_NET, "%s recovery message sent %s:%d\n",
3718 libcfs_nid2str(ev_info->mt_nid),
3719 (event->status) ? "unsuccessfully" :
3720 "successfully", event->status);
3723 CERROR("Unexpected event: %d\n", event->type);
3726 if (event->unlinked) {
3727 LIBCFS_FREE(ev_info, sizeof(*ev_info));
3728 pbuf = LNET_PING_INFO_TO_BUFFER(event->md.start);
3729 lnet_ping_buffer_decref(pbuf);
3734 lnet_rsp_tracker_create(void)
3736 struct list_head **rstqs;
3737 rstqs = lnet_create_array_of_queues();
3742 the_lnet.ln_mt_rstq = rstqs;
3748 lnet_rsp_tracker_clean(void)
3750 lnet_finalize_expired_responses();
3752 cfs_percpt_free(the_lnet.ln_mt_rstq);
3753 the_lnet.ln_mt_rstq = NULL;
3756 int lnet_monitor_thr_start(void)
3759 struct task_struct *task;
3761 if (the_lnet.ln_mt_state != LNET_MT_STATE_SHUTDOWN)
3764 rc = lnet_resendqs_create();
3768 rc = lnet_rsp_tracker_create();
3772 sema_init(&the_lnet.ln_mt_signal, 0);
3774 lnet_net_lock(LNET_LOCK_EX);
3775 the_lnet.ln_mt_state = LNET_MT_STATE_RUNNING;
3776 lnet_net_unlock(LNET_LOCK_EX);
3777 task = kthread_run(lnet_monitor_thread, NULL, "monitor_thread");
3780 CERROR("Can't start monitor thread: %d\n", rc);
3787 lnet_net_lock(LNET_LOCK_EX);
3788 the_lnet.ln_mt_state = LNET_MT_STATE_STOPPING;
3789 lnet_net_unlock(LNET_LOCK_EX);
3790 /* block until event callback signals exit */
3791 down(&the_lnet.ln_mt_signal);
3793 lnet_net_lock(LNET_LOCK_EX);
3794 the_lnet.ln_mt_state = LNET_MT_STATE_SHUTDOWN;
3795 lnet_net_unlock(LNET_LOCK_EX);
3796 lnet_rsp_tracker_clean();
3797 lnet_clean_local_ni_recoveryq();
3798 lnet_clean_peer_ni_recoveryq();
3799 lnet_clean_resendqs();
3800 LNetInvalidateEQHandle(&the_lnet.ln_mt_eqh);
3803 lnet_rsp_tracker_clean();
3804 lnet_clean_local_ni_recoveryq();
3805 lnet_clean_peer_ni_recoveryq();
3806 lnet_clean_resendqs();
3810 void lnet_monitor_thr_stop(void)
3812 if (the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN)
3815 LASSERT(the_lnet.ln_mt_state == LNET_MT_STATE_RUNNING);
3816 lnet_net_lock(LNET_LOCK_EX);
3817 the_lnet.ln_mt_state = LNET_MT_STATE_STOPPING;
3818 lnet_net_unlock(LNET_LOCK_EX);
3820 /* tell the monitor thread that we're shutting down */
3821 complete(&the_lnet.ln_mt_wait_complete);
3823 /* block until monitor thread signals that it's done */
3824 down(&the_lnet.ln_mt_signal);
3825 LASSERT(the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN);
3827 /* perform cleanup tasks */
3828 lnet_rsp_tracker_clean();
3829 lnet_clean_local_ni_recoveryq();
3830 lnet_clean_peer_ni_recoveryq();
3831 lnet_clean_resendqs();
3835 lnet_drop_message(struct lnet_ni *ni, int cpt, void *private, unsigned int nob,
3839 lnet_incr_stats(&ni->ni_stats, msg_type, LNET_STATS_TYPE_DROP);
3840 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
3841 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length += nob;
3842 lnet_net_unlock(cpt);
3844 lnet_ni_recv(ni, private, NULL, 0, 0, 0, nob);
3848 lnet_recv_put(struct lnet_ni *ni, struct lnet_msg *msg)
3850 struct lnet_hdr *hdr = &msg->msg_hdr;
3852 if (msg->msg_wanted != 0)
3853 lnet_setpayloadbuffer(msg);
3855 lnet_build_msg_event(msg, LNET_EVENT_PUT);
3857 /* Must I ACK? If so I'll grab the ack_wmd out of the header and put
3858 * it back into the ACK during lnet_finalize() */
3859 msg->msg_ack = (!lnet_is_wire_handle_none(&hdr->msg.put.ack_wmd) &&
3860 (msg->msg_md->md_options & LNET_MD_ACK_DISABLE) == 0);
3862 lnet_ni_recv(ni, msg->msg_private, msg, msg->msg_rx_delayed,
3863 msg->msg_offset, msg->msg_wanted, hdr->payload_length);
3867 lnet_parse_put(struct lnet_ni *ni, struct lnet_msg *msg)
3869 struct lnet_hdr *hdr = &msg->msg_hdr;
3870 struct lnet_match_info info;
3874 /* Convert put fields to host byte order */
3875 hdr->msg.put.match_bits = le64_to_cpu(hdr->msg.put.match_bits);
3876 hdr->msg.put.ptl_index = le32_to_cpu(hdr->msg.put.ptl_index);
3877 hdr->msg.put.offset = le32_to_cpu(hdr->msg.put.offset);
3879 /* Primary peer NID. */
3880 info.mi_id.nid = msg->msg_initiator;
3881 info.mi_id.pid = hdr->src_pid;
3882 info.mi_opc = LNET_MD_OP_PUT;
3883 info.mi_portal = hdr->msg.put.ptl_index;
3884 info.mi_rlength = hdr->payload_length;
3885 info.mi_roffset = hdr->msg.put.offset;
3886 info.mi_mbits = hdr->msg.put.match_bits;
3887 info.mi_cpt = lnet_cpt_of_nid(msg->msg_initiator, ni);
3889 msg->msg_rx_ready_delay = ni->ni_net->net_lnd->lnd_eager_recv == NULL;
3890 ready_delay = msg->msg_rx_ready_delay;
3893 rc = lnet_ptl_match_md(&info, msg);
3898 case LNET_MATCHMD_OK:
3899 lnet_recv_put(ni, msg);
3902 case LNET_MATCHMD_NONE:
3904 /* no eager_recv or has already called it, should
3905 * have been attached on delayed list */
3908 rc = lnet_ni_eager_recv(ni, msg);
3915 case LNET_MATCHMD_DROP:
3916 CNETERR("Dropping PUT from %s portal %d match %llu"
3917 " offset %d length %d: %d\n",
3918 libcfs_id2str(info.mi_id), info.mi_portal,
3919 info.mi_mbits, info.mi_roffset, info.mi_rlength, rc);
3921 return -ENOENT; /* -ve: OK but no match */
3926 lnet_parse_get(struct lnet_ni *ni, struct lnet_msg *msg, int rdma_get)
3928 struct lnet_match_info info;
3929 struct lnet_hdr *hdr = &msg->msg_hdr;
3930 struct lnet_process_id source_id;
3931 struct lnet_handle_wire reply_wmd;
3934 /* Convert get fields to host byte order */
3935 hdr->msg.get.match_bits = le64_to_cpu(hdr->msg.get.match_bits);
3936 hdr->msg.get.ptl_index = le32_to_cpu(hdr->msg.get.ptl_index);
3937 hdr->msg.get.sink_length = le32_to_cpu(hdr->msg.get.sink_length);
3938 hdr->msg.get.src_offset = le32_to_cpu(hdr->msg.get.src_offset);
3940 source_id.nid = hdr->src_nid;
3941 source_id.pid = hdr->src_pid;
3942 /* Primary peer NID */
3943 info.mi_id.nid = msg->msg_initiator;
3944 info.mi_id.pid = hdr->src_pid;
3945 info.mi_opc = LNET_MD_OP_GET;
3946 info.mi_portal = hdr->msg.get.ptl_index;
3947 info.mi_rlength = hdr->msg.get.sink_length;
3948 info.mi_roffset = hdr->msg.get.src_offset;
3949 info.mi_mbits = hdr->msg.get.match_bits;
3950 info.mi_cpt = lnet_cpt_of_nid(msg->msg_initiator, ni);
3952 rc = lnet_ptl_match_md(&info, msg);
3953 if (rc == LNET_MATCHMD_DROP) {
3954 CNETERR("Dropping GET from %s portal %d match %llu"
3955 " offset %d length %d\n",
3956 libcfs_id2str(info.mi_id), info.mi_portal,
3957 info.mi_mbits, info.mi_roffset, info.mi_rlength);
3958 return -ENOENT; /* -ve: OK but no match */
3961 LASSERT(rc == LNET_MATCHMD_OK);
3963 lnet_build_msg_event(msg, LNET_EVENT_GET);
3965 reply_wmd = hdr->msg.get.return_wmd;
3967 lnet_prep_send(msg, LNET_MSG_REPLY, source_id,
3968 msg->msg_offset, msg->msg_wanted);
3970 msg->msg_hdr.msg.reply.dst_wmd = reply_wmd;
3973 /* The LND completes the REPLY from her recv procedure */
3974 lnet_ni_recv(ni, msg->msg_private, msg, 0,
3975 msg->msg_offset, msg->msg_len, msg->msg_len);
3979 lnet_ni_recv(ni, msg->msg_private, NULL, 0, 0, 0, 0);
3980 msg->msg_receiving = 0;
3982 rc = lnet_send(ni->ni_nid, msg, msg->msg_from);
3984 /* didn't get as far as lnet_ni_send() */
3985 CERROR("%s: Unable to send REPLY for GET from %s: %d\n",
3986 libcfs_nid2str(ni->ni_nid),
3987 libcfs_id2str(info.mi_id), rc);
3989 lnet_finalize(msg, rc);
3996 lnet_parse_reply(struct lnet_ni *ni, struct lnet_msg *msg)
3998 void *private = msg->msg_private;
3999 struct lnet_hdr *hdr = &msg->msg_hdr;
4000 struct lnet_process_id src = {0};
4001 struct lnet_libmd *md;
4006 cpt = lnet_cpt_of_cookie(hdr->msg.reply.dst_wmd.wh_object_cookie);
4009 src.nid = hdr->src_nid;
4010 src.pid = hdr->src_pid;
4012 /* NB handles only looked up by creator (no flips) */
4013 md = lnet_wire_handle2md(&hdr->msg.reply.dst_wmd);
4014 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
4015 CNETERR("%s: Dropping REPLY from %s for %s "
4017 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
4018 (md == NULL) ? "invalid" : "inactive",
4019 hdr->msg.reply.dst_wmd.wh_interface_cookie,
4020 hdr->msg.reply.dst_wmd.wh_object_cookie);
4021 if (md != NULL && md->md_me != NULL)
4022 CERROR("REPLY MD also attached to portal %d\n",
4023 md->md_me->me_portal);
4025 lnet_res_unlock(cpt);
4026 return -ENOENT; /* -ve: OK but no match */
4029 LASSERT(md->md_offset == 0);
4031 rlength = hdr->payload_length;
4032 mlength = MIN(rlength, (int)md->md_length);
4034 if (mlength < rlength &&
4035 (md->md_options & LNET_MD_TRUNCATE) == 0) {
4036 CNETERR("%s: Dropping REPLY from %s length %d "
4037 "for MD %#llx would overflow (%d)\n",
4038 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
4039 rlength, hdr->msg.reply.dst_wmd.wh_object_cookie,
4041 lnet_res_unlock(cpt);
4042 return -ENOENT; /* -ve: OK but no match */
4045 CDEBUG(D_NET, "%s: Reply from %s of length %d/%d into md %#llx\n",
4046 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
4047 mlength, rlength, hdr->msg.reply.dst_wmd.wh_object_cookie);
4049 lnet_msg_attach_md(msg, md, 0, mlength);
4052 lnet_setpayloadbuffer(msg);
4054 lnet_res_unlock(cpt);
4056 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
4058 lnet_ni_recv(ni, private, msg, 0, 0, mlength, rlength);
4063 lnet_parse_ack(struct lnet_ni *ni, struct lnet_msg *msg)
4065 struct lnet_hdr *hdr = &msg->msg_hdr;
4066 struct lnet_process_id src = {0};
4067 struct lnet_libmd *md;
4070 src.nid = hdr->src_nid;
4071 src.pid = hdr->src_pid;
4073 /* Convert ack fields to host byte order */
4074 hdr->msg.ack.match_bits = le64_to_cpu(hdr->msg.ack.match_bits);
4075 hdr->msg.ack.mlength = le32_to_cpu(hdr->msg.ack.mlength);
4077 cpt = lnet_cpt_of_cookie(hdr->msg.ack.dst_wmd.wh_object_cookie);
4080 /* NB handles only looked up by creator (no flips) */
4081 md = lnet_wire_handle2md(&hdr->msg.ack.dst_wmd);
4082 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
4083 /* Don't moan; this is expected */
4085 "%s: Dropping ACK from %s to %s MD %#llx.%#llx\n",
4086 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
4087 (md == NULL) ? "invalid" : "inactive",
4088 hdr->msg.ack.dst_wmd.wh_interface_cookie,
4089 hdr->msg.ack.dst_wmd.wh_object_cookie);
4090 if (md != NULL && md->md_me != NULL)
4091 CERROR("Source MD also attached to portal %d\n",
4092 md->md_me->me_portal);
4094 lnet_res_unlock(cpt);
4095 return -ENOENT; /* -ve! */
4098 CDEBUG(D_NET, "%s: ACK from %s into md %#llx\n",
4099 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
4100 hdr->msg.ack.dst_wmd.wh_object_cookie);
4102 lnet_msg_attach_md(msg, md, 0, 0);
4104 lnet_res_unlock(cpt);
4106 lnet_build_msg_event(msg, LNET_EVENT_ACK);
4108 lnet_ni_recv(ni, msg->msg_private, msg, 0, 0, 0, msg->msg_len);
4113 * \retval LNET_CREDIT_OK If \a msg is forwarded
4114 * \retval LNET_CREDIT_WAIT If \a msg is blocked because w/o buffer
4115 * \retval -ve error code
4118 lnet_parse_forward_locked(struct lnet_ni *ni, struct lnet_msg *msg)
4122 if (!the_lnet.ln_routing)
4125 if (msg->msg_rxpeer->lpni_rtrcredits <= 0 ||
4126 lnet_msg2bufpool(msg)->rbp_credits <= 0) {
4127 if (ni->ni_net->net_lnd->lnd_eager_recv == NULL) {
4128 msg->msg_rx_ready_delay = 1;
4130 lnet_net_unlock(msg->msg_rx_cpt);
4131 rc = lnet_ni_eager_recv(ni, msg);
4132 lnet_net_lock(msg->msg_rx_cpt);
4137 rc = lnet_post_routed_recv_locked(msg, 0);
4142 lnet_parse_local(struct lnet_ni *ni, struct lnet_msg *msg)
4146 switch (msg->msg_type) {
4148 rc = lnet_parse_ack(ni, msg);
4151 rc = lnet_parse_put(ni, msg);
4154 rc = lnet_parse_get(ni, msg, msg->msg_rdma_get);
4156 case LNET_MSG_REPLY:
4157 rc = lnet_parse_reply(ni, msg);
4159 default: /* prevent an unused label if !kernel */
4164 LASSERT(rc == 0 || rc == -ENOENT);
4169 lnet_msgtyp2str (int type)
4178 case LNET_MSG_REPLY:
4180 case LNET_MSG_HELLO:
4183 return ("<UNKNOWN>");
4188 lnet_print_hdr(struct lnet_hdr *hdr)
4190 struct lnet_process_id src = {
4191 .nid = hdr->src_nid,
4192 .pid = hdr->src_pid,
4194 struct lnet_process_id dst = {
4195 .nid = hdr->dest_nid,
4196 .pid = hdr->dest_pid,
4198 char *type_str = lnet_msgtyp2str(hdr->type);
4200 CWARN("P3 Header at %p of type %s\n", hdr, type_str);
4201 CWARN(" From %s\n", libcfs_id2str(src));
4202 CWARN(" To %s\n", libcfs_id2str(dst));
4204 switch (hdr->type) {
4209 CWARN(" Ptl index %d, ack md %#llx.%#llx, "
4210 "match bits %llu\n",
4211 hdr->msg.put.ptl_index,
4212 hdr->msg.put.ack_wmd.wh_interface_cookie,
4213 hdr->msg.put.ack_wmd.wh_object_cookie,
4214 hdr->msg.put.match_bits);
4215 CWARN(" Length %d, offset %d, hdr data %#llx\n",
4216 hdr->payload_length, hdr->msg.put.offset,
4217 hdr->msg.put.hdr_data);
4221 CWARN(" Ptl index %d, return md %#llx.%#llx, "
4222 "match bits %llu\n", hdr->msg.get.ptl_index,
4223 hdr->msg.get.return_wmd.wh_interface_cookie,
4224 hdr->msg.get.return_wmd.wh_object_cookie,
4225 hdr->msg.get.match_bits);
4226 CWARN(" Length %d, src offset %d\n",
4227 hdr->msg.get.sink_length,
4228 hdr->msg.get.src_offset);
4232 CWARN(" dst md %#llx.%#llx, "
4233 "manipulated length %d\n",
4234 hdr->msg.ack.dst_wmd.wh_interface_cookie,
4235 hdr->msg.ack.dst_wmd.wh_object_cookie,
4236 hdr->msg.ack.mlength);
4239 case LNET_MSG_REPLY:
4240 CWARN(" dst md %#llx.%#llx, "
4242 hdr->msg.reply.dst_wmd.wh_interface_cookie,
4243 hdr->msg.reply.dst_wmd.wh_object_cookie,
4244 hdr->payload_length);
4250 lnet_parse(struct lnet_ni *ni, struct lnet_hdr *hdr, lnet_nid_t from_nid,
4251 void *private, int rdma_req)
4253 struct lnet_peer_ni *lpni;
4254 struct lnet_msg *msg;
4255 __u32 payload_length;
4256 lnet_pid_t dest_pid;
4257 lnet_nid_t dest_nid;
4265 LASSERT (!in_interrupt ());
4267 type = le32_to_cpu(hdr->type);
4268 src_nid = le64_to_cpu(hdr->src_nid);
4269 dest_nid = le64_to_cpu(hdr->dest_nid);
4270 dest_pid = le32_to_cpu(hdr->dest_pid);
4271 payload_length = le32_to_cpu(hdr->payload_length);
4273 for_me = (ni->ni_nid == dest_nid);
4274 cpt = lnet_cpt_of_nid(from_nid, ni);
4276 CDEBUG(D_NET, "TRACE: %s(%s) <- %s : %s - %s\n",
4277 libcfs_nid2str(dest_nid),
4278 libcfs_nid2str(ni->ni_nid),
4279 libcfs_nid2str(src_nid),
4280 lnet_msgtyp2str(type),
4281 (for_me) ? "for me" : "routed");
4286 if (payload_length > 0) {
4287 CERROR("%s, src %s: bad %s payload %d (0 expected)\n",
4288 libcfs_nid2str(from_nid),
4289 libcfs_nid2str(src_nid),
4290 lnet_msgtyp2str(type), payload_length);
4296 case LNET_MSG_REPLY:
4297 if (payload_length >
4298 (__u32)(for_me ? LNET_MAX_PAYLOAD : LNET_MTU)) {
4299 CERROR("%s, src %s: bad %s payload %d "
4300 "(%d max expected)\n",
4301 libcfs_nid2str(from_nid),
4302 libcfs_nid2str(src_nid),
4303 lnet_msgtyp2str(type),
4305 for_me ? LNET_MAX_PAYLOAD : LNET_MTU);
4311 CERROR("%s, src %s: Bad message type 0x%x\n",
4312 libcfs_nid2str(from_nid),
4313 libcfs_nid2str(src_nid), type);
4317 if (the_lnet.ln_routing &&
4318 ni->ni_net->net_last_alive != ktime_get_real_seconds()) {
4320 spin_lock(&ni->ni_net->net_lock);
4321 ni->ni_net->net_last_alive = ktime_get_real_seconds();
4322 spin_unlock(&ni->ni_net->net_lock);
4323 if (ni->ni_status != NULL &&
4324 ni->ni_status->ns_status == LNET_NI_STATUS_DOWN) {
4325 ni->ni_status->ns_status = LNET_NI_STATUS_UP;
4332 lnet_push_update_to_peers(1);
4334 /* Regard a bad destination NID as a protocol error. Senders should
4335 * know what they're doing; if they don't they're misconfigured, buggy
4336 * or malicious so we chop them off at the knees :) */
4339 if (LNET_NIDNET(dest_nid) == LNET_NIDNET(ni->ni_nid)) {
4340 /* should have gone direct */
4341 CERROR("%s, src %s: Bad dest nid %s "
4342 "(should have been sent direct)\n",
4343 libcfs_nid2str(from_nid),
4344 libcfs_nid2str(src_nid),
4345 libcfs_nid2str(dest_nid));
4349 if (lnet_islocalnid(dest_nid)) {
4350 /* dest is another local NI; sender should have used
4351 * this node's NID on its own network */
4352 CERROR("%s, src %s: Bad dest nid %s "
4353 "(it's my nid but on a different network)\n",
4354 libcfs_nid2str(from_nid),
4355 libcfs_nid2str(src_nid),
4356 libcfs_nid2str(dest_nid));
4360 if (rdma_req && type == LNET_MSG_GET) {
4361 CERROR("%s, src %s: Bad optimized GET for %s "
4362 "(final destination must be me)\n",
4363 libcfs_nid2str(from_nid),
4364 libcfs_nid2str(src_nid),
4365 libcfs_nid2str(dest_nid));
4369 if (!the_lnet.ln_routing) {
4370 CERROR("%s, src %s: Dropping message for %s "
4371 "(routing not enabled)\n",
4372 libcfs_nid2str(from_nid),
4373 libcfs_nid2str(src_nid),
4374 libcfs_nid2str(dest_nid));
4379 /* Message looks OK; we're not going to return an error, so we MUST
4380 * call back lnd_recv() come what may... */
4382 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
4383 fail_peer(src_nid, 0)) { /* shall we now? */
4384 CERROR("%s, src %s: Dropping %s to simulate failure\n",
4385 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4386 lnet_msgtyp2str(type));
4390 if (!list_empty(&the_lnet.ln_drop_rules) &&
4391 lnet_drop_rule_match(hdr, ni->ni_nid, NULL)) {
4392 CDEBUG(D_NET, "%s, src %s, dst %s: Dropping %s to simulate"
4393 "silent message loss\n",
4394 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4395 libcfs_nid2str(dest_nid), lnet_msgtyp2str(type));
4399 if (lnet_drop_asym_route && for_me &&
4400 LNET_NIDNET(src_nid) != LNET_NIDNET(from_nid)) {
4401 struct lnet_net *net;
4402 struct lnet_remotenet *rnet;
4405 /* we are dealing with a routed message,
4406 * so see if route to reach src_nid goes through from_nid
4409 net = lnet_get_net_locked(LNET_NIDNET(ni->ni_nid));
4411 lnet_net_unlock(cpt);
4412 CERROR("net %s not found\n",
4413 libcfs_net2str(LNET_NIDNET(ni->ni_nid)));
4417 rnet = lnet_find_rnet_locked(LNET_NIDNET(src_nid));
4419 struct lnet_peer *gw = NULL;
4420 struct lnet_peer_ni *lpni = NULL;
4421 struct lnet_route *route;
4423 list_for_each_entry(route, &rnet->lrn_routes, lr_list) {
4425 gw = route->lr_gateway;
4426 if (route->lr_lnet != net->net_id)
4429 * if the nid is one of the gateway's NIDs
4430 * then this is a valid gateway
4432 while ((lpni = lnet_get_next_peer_ni_locked(gw,
4433 NULL, lpni)) != NULL) {
4434 if (lpni->lpni_nid == from_nid) {
4441 lnet_net_unlock(cpt);
4443 /* we would not use from_nid to route a message to
4445 * => asymmetric routing detected but forbidden
4447 CERROR("%s, src %s: Dropping asymmetrical route %s\n",
4448 libcfs_nid2str(from_nid),
4449 libcfs_nid2str(src_nid), lnet_msgtyp2str(type));
4454 msg = lnet_msg_alloc();
4456 CERROR("%s, src %s: Dropping %s (out of memory)\n",
4457 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4458 lnet_msgtyp2str(type));
4462 /* msg zeroed in lnet_msg_alloc; i.e. flags all clear,
4463 * pointers NULL etc */
4465 msg->msg_type = type;
4466 msg->msg_private = private;
4467 msg->msg_receiving = 1;
4468 msg->msg_rdma_get = rdma_req;
4469 msg->msg_len = msg->msg_wanted = payload_length;
4470 msg->msg_offset = 0;
4471 msg->msg_hdr = *hdr;
4472 /* for building message event */
4473 msg->msg_from = from_nid;
4475 msg->msg_target.pid = dest_pid;
4476 msg->msg_target.nid = dest_nid;
4477 msg->msg_routing = 1;
4480 /* convert common msg->hdr fields to host byteorder */
4481 msg->msg_hdr.type = type;
4482 msg->msg_hdr.src_nid = src_nid;
4483 msg->msg_hdr.src_pid = le32_to_cpu(msg->msg_hdr.src_pid);
4484 msg->msg_hdr.dest_nid = dest_nid;
4485 msg->msg_hdr.dest_pid = dest_pid;
4486 msg->msg_hdr.payload_length = payload_length;
4490 lpni = lnet_nid2peerni_locked(from_nid, ni->ni_nid, cpt);
4492 lnet_net_unlock(cpt);
4493 CERROR("%s, src %s: Dropping %s "
4494 "(error %ld looking up sender)\n",
4495 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4496 lnet_msgtyp2str(type), PTR_ERR(lpni));
4498 if (rc == -ESHUTDOWN)
4499 /* We are shutting down. Don't do anything more */
4504 if (the_lnet.ln_routing)
4505 lpni->lpni_last_alive = ktime_get_seconds();
4507 msg->msg_rxpeer = lpni;
4509 lnet_ni_addref_locked(ni, cpt);
4510 /* Multi-Rail: Primary NID of source. */
4511 msg->msg_initiator = lnet_peer_primary_nid_locked(src_nid);
4514 * mark the status of this lpni as UP since we received a message
4515 * from it. The ping response reports back the ns_status which is
4516 * marked on the remote as up or down and we cache it here.
4518 msg->msg_rxpeer->lpni_ns_status = LNET_NI_STATUS_UP;
4520 lnet_msg_commit(msg, cpt);
4522 /* message delay simulation */
4523 if (unlikely(!list_empty(&the_lnet.ln_delay_rules) &&
4524 lnet_delay_rule_match_locked(hdr, msg))) {
4525 lnet_net_unlock(cpt);
4530 rc = lnet_parse_forward_locked(ni, msg);
4531 lnet_net_unlock(cpt);
4536 if (rc == LNET_CREDIT_OK) {
4537 lnet_ni_recv(ni, msg->msg_private, msg, 0,
4538 0, payload_length, payload_length);
4543 lnet_net_unlock(cpt);
4545 rc = lnet_parse_local(ni, msg);
4551 LASSERT(msg->msg_md == NULL);
4552 lnet_finalize(msg, rc);
4555 lnet_drop_message(ni, cpt, private, payload_length, type);
4558 EXPORT_SYMBOL(lnet_parse);
4561 lnet_drop_delayed_msg_list(struct list_head *head, char *reason)
4563 while (!list_empty(head)) {
4564 struct lnet_process_id id = {0};
4565 struct lnet_msg *msg;
4567 msg = list_entry(head->next, struct lnet_msg, msg_list);
4568 list_del(&msg->msg_list);
4570 id.nid = msg->msg_hdr.src_nid;
4571 id.pid = msg->msg_hdr.src_pid;
4573 LASSERT(msg->msg_md == NULL);
4574 LASSERT(msg->msg_rx_delayed);
4575 LASSERT(msg->msg_rxpeer != NULL);
4576 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
4578 CWARN("Dropping delayed PUT from %s portal %d match %llu"
4579 " offset %d length %d: %s\n",
4581 msg->msg_hdr.msg.put.ptl_index,
4582 msg->msg_hdr.msg.put.match_bits,
4583 msg->msg_hdr.msg.put.offset,
4584 msg->msg_hdr.payload_length, reason);
4586 /* NB I can't drop msg's ref on msg_rxpeer until after I've
4587 * called lnet_drop_message(), so I just hang onto msg as well
4588 * until that's done */
4590 lnet_drop_message(msg->msg_rxni, msg->msg_rx_cpt,
4591 msg->msg_private, msg->msg_len,
4594 msg->msg_no_resend = true;
4596 * NB: message will not generate event because w/o attached MD,
4597 * but we still should give error code so lnet_msg_decommit()
4598 * can skip counters operations and other checks.
4600 lnet_finalize(msg, -ENOENT);
4605 lnet_recv_delayed_msg_list(struct list_head *head)
4607 while (!list_empty(head)) {
4608 struct lnet_msg *msg;
4609 struct lnet_process_id id;
4611 msg = list_entry(head->next, struct lnet_msg, msg_list);
4612 list_del(&msg->msg_list);
4614 /* md won't disappear under me, since each msg
4615 * holds a ref on it */
4617 id.nid = msg->msg_hdr.src_nid;
4618 id.pid = msg->msg_hdr.src_pid;
4620 LASSERT(msg->msg_rx_delayed);
4621 LASSERT(msg->msg_md != NULL);
4622 LASSERT(msg->msg_rxpeer != NULL);
4623 LASSERT(msg->msg_rxni != NULL);
4624 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
4626 CDEBUG(D_NET, "Resuming delayed PUT from %s portal %d "
4627 "match %llu offset %d length %d.\n",
4628 libcfs_id2str(id), msg->msg_hdr.msg.put.ptl_index,
4629 msg->msg_hdr.msg.put.match_bits,
4630 msg->msg_hdr.msg.put.offset,
4631 msg->msg_hdr.payload_length);
4633 lnet_recv_put(msg->msg_rxni, msg);
4638 lnet_attach_rsp_tracker(struct lnet_rsp_tracker *rspt, int cpt,
4639 struct lnet_libmd *md, struct lnet_handle_md mdh)
4642 bool new_entry = true;
4643 struct lnet_rsp_tracker *local_rspt;
4646 * MD has a refcount taken by message so it's not going away.
4647 * The MD however can be looked up. We need to secure the access
4648 * to the md_rspt_ptr by taking the res_lock.
4649 * The rspt can be accessed without protection up to when it gets
4650 * added to the list.
4654 local_rspt = md->md_rspt_ptr;
4655 timeout_ns = lnet_transaction_timeout * NSEC_PER_SEC;
4656 if (local_rspt != NULL) {
4658 * we already have an rspt attached to the md, so we'll
4659 * update the deadline on that one.
4661 lnet_rspt_free(rspt, cpt);
4665 rspt->rspt_mdh = mdh;
4666 rspt->rspt_cpt = cpt;
4667 /* store the rspt so we can access it when we get the REPLY */
4668 md->md_rspt_ptr = rspt;
4671 local_rspt->rspt_deadline = ktime_add_ns(ktime_get(), timeout_ns);
4674 * add to the list of tracked responses. It's added to tail of the
4675 * list in order to expire all the older entries first.
4678 if (!new_entry && !list_empty(&local_rspt->rspt_on_list))
4679 list_del_init(&local_rspt->rspt_on_list);
4680 list_add_tail(&local_rspt->rspt_on_list, the_lnet.ln_mt_rstq[cpt]);
4681 lnet_net_unlock(cpt);
4682 lnet_res_unlock(cpt);
4686 * Initiate an asynchronous PUT operation.
4688 * There are several events associated with a PUT: completion of the send on
4689 * the initiator node (LNET_EVENT_SEND), and when the send completes
4690 * successfully, the receipt of an acknowledgment (LNET_EVENT_ACK) indicating
4691 * that the operation was accepted by the target. The event LNET_EVENT_PUT is
4692 * used at the target node to indicate the completion of incoming data
4695 * The local events will be logged in the EQ associated with the MD pointed to
4696 * by \a mdh handle. Using a MD without an associated EQ results in these
4697 * events being discarded. In this case, the caller must have another
4698 * mechanism (e.g., a higher level protocol) for determining when it is safe
4699 * to modify the memory region associated with the MD.
4701 * Note that LNet does not guarantee the order of LNET_EVENT_SEND and
4702 * LNET_EVENT_ACK, though intuitively ACK should happen after SEND.
4704 * \param self Indicates the NID of a local interface through which to send
4705 * the PUT request. Use LNET_NID_ANY to let LNet choose one by itself.
4706 * \param mdh A handle for the MD that describes the memory to be sent. The MD
4707 * must be "free floating" (See LNetMDBind()).
4708 * \param ack Controls whether an acknowledgment is requested.
4709 * Acknowledgments are only sent when they are requested by the initiating
4710 * process and the target MD enables them.
4711 * \param target A process identifier for the target process.
4712 * \param portal The index in the \a target's portal table.
4713 * \param match_bits The match bits to use for MD selection at the target
4715 * \param offset The offset into the target MD (only used when the target
4716 * MD has the LNET_MD_MANAGE_REMOTE option set).
4717 * \param hdr_data 64 bits of user data that can be included in the message
4718 * header. This data is written to an event queue entry at the target if an
4719 * EQ is present on the matching MD.
4721 * \retval 0 Success, and only in this case events will be generated
4722 * and logged to EQ (if it exists).
4723 * \retval -EIO Simulated failure.
4724 * \retval -ENOMEM Memory allocation failure.
4725 * \retval -ENOENT Invalid MD object.
4727 * \see struct lnet_event::hdr_data and lnet_event_kind_t.
4730 LNetPut(lnet_nid_t self, struct lnet_handle_md mdh, enum lnet_ack_req ack,
4731 struct lnet_process_id target, unsigned int portal,
4732 __u64 match_bits, unsigned int offset,
4735 struct lnet_msg *msg;
4736 struct lnet_libmd *md;
4739 struct lnet_rsp_tracker *rspt = NULL;
4741 LASSERT(the_lnet.ln_refcount > 0);
4743 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
4744 fail_peer(target.nid, 1)) { /* shall we now? */
4745 CERROR("Dropping PUT to %s: simulated failure\n",
4746 libcfs_id2str(target));
4750 msg = lnet_msg_alloc();
4752 CERROR("Dropping PUT to %s: ENOMEM on struct lnet_msg\n",
4753 libcfs_id2str(target));
4756 msg->msg_vmflush = !!memory_pressure_get();
4758 cpt = lnet_cpt_of_cookie(mdh.cookie);
4760 if (ack == LNET_ACK_REQ) {
4761 rspt = lnet_rspt_alloc(cpt);
4763 CERROR("Dropping PUT to %s: ENOMEM on response tracker\n",
4764 libcfs_id2str(target));
4767 INIT_LIST_HEAD(&rspt->rspt_on_list);
4772 md = lnet_handle2md(&mdh);
4773 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
4774 CERROR("Dropping PUT (%llu:%d:%s): MD (%d) invalid\n",
4775 match_bits, portal, libcfs_id2str(target),
4776 md == NULL ? -1 : md->md_threshold);
4777 if (md != NULL && md->md_me != NULL)
4778 CERROR("Source MD also attached to portal %d\n",
4779 md->md_me->me_portal);
4780 lnet_res_unlock(cpt);
4782 lnet_rspt_free(rspt, cpt);
4787 CDEBUG(D_NET, "LNetPut -> %s\n", libcfs_id2str(target));
4789 lnet_msg_attach_md(msg, md, 0, 0);
4791 lnet_prep_send(msg, LNET_MSG_PUT, target, 0, md->md_length);
4793 msg->msg_hdr.msg.put.match_bits = cpu_to_le64(match_bits);
4794 msg->msg_hdr.msg.put.ptl_index = cpu_to_le32(portal);
4795 msg->msg_hdr.msg.put.offset = cpu_to_le32(offset);
4796 msg->msg_hdr.msg.put.hdr_data = hdr_data;
4798 /* NB handles only looked up by creator (no flips) */
4799 if (ack == LNET_ACK_REQ) {
4800 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
4801 the_lnet.ln_interface_cookie;
4802 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
4803 md->md_lh.lh_cookie;
4805 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
4806 LNET_WIRE_HANDLE_COOKIE_NONE;
4807 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
4808 LNET_WIRE_HANDLE_COOKIE_NONE;
4811 lnet_res_unlock(cpt);
4813 lnet_build_msg_event(msg, LNET_EVENT_SEND);
4815 if (ack == LNET_ACK_REQ)
4816 lnet_attach_rsp_tracker(rspt, cpt, md, mdh);
4818 if (CFS_FAIL_CHECK_ORSET(CFS_FAIL_PTLRPC_OST_BULK_CB2,
4822 rc = lnet_send(self, msg, LNET_NID_ANY);
4825 CNETERR("Error sending PUT to %s: %d\n",
4826 libcfs_id2str(target), rc);
4827 msg->msg_no_resend = true;
4828 lnet_finalize(msg, rc);
4831 /* completion will be signalled by an event */
4834 EXPORT_SYMBOL(LNetPut);
4837 * The LND can DMA direct to the GET md (i.e. no REPLY msg). This
4838 * returns a msg for the LND to pass to lnet_finalize() when the sink
4839 * data has been received.
4841 * CAVEAT EMPTOR: 'getmsg' is the original GET, which is freed when
4842 * lnet_finalize() is called on it, so the LND must call this first
4845 lnet_create_reply_msg(struct lnet_ni *ni, struct lnet_msg *getmsg)
4847 struct lnet_msg *msg = lnet_msg_alloc();
4848 struct lnet_libmd *getmd = getmsg->msg_md;
4849 struct lnet_process_id peer_id = getmsg->msg_target;
4852 LASSERT(!getmsg->msg_target_is_router);
4853 LASSERT(!getmsg->msg_routing);
4856 CERROR("%s: Dropping REPLY from %s: can't allocate msg\n",
4857 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id));
4861 cpt = lnet_cpt_of_cookie(getmd->md_lh.lh_cookie);
4864 LASSERT(getmd->md_refcount > 0);
4866 if (getmd->md_threshold == 0) {
4867 CERROR("%s: Dropping REPLY from %s for inactive MD %p\n",
4868 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id),
4870 lnet_res_unlock(cpt);
4874 LASSERT(getmd->md_offset == 0);
4876 CDEBUG(D_NET, "%s: Reply from %s md %p\n",
4877 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id), getmd);
4879 /* setup information for lnet_build_msg_event */
4880 msg->msg_initiator = getmsg->msg_txpeer->lpni_peer_net->lpn_peer->lp_primary_nid;
4881 msg->msg_from = peer_id.nid;
4882 msg->msg_type = LNET_MSG_GET; /* flag this msg as an "optimized" GET */
4883 msg->msg_hdr.src_nid = peer_id.nid;
4884 msg->msg_hdr.payload_length = getmd->md_length;
4885 msg->msg_receiving = 1; /* required by lnet_msg_attach_md */
4887 lnet_msg_attach_md(msg, getmd, getmd->md_offset, getmd->md_length);
4888 lnet_res_unlock(cpt);
4890 cpt = lnet_cpt_of_nid(peer_id.nid, ni);
4893 lnet_msg_commit(msg, cpt);
4894 lnet_net_unlock(cpt);
4896 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
4901 cpt = lnet_cpt_of_nid(peer_id.nid, ni);
4904 lnet_incr_stats(&ni->ni_stats, LNET_MSG_GET, LNET_STATS_TYPE_DROP);
4905 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
4906 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length +=
4908 lnet_net_unlock(cpt);
4915 EXPORT_SYMBOL(lnet_create_reply_msg);
4918 lnet_set_reply_msg_len(struct lnet_ni *ni, struct lnet_msg *reply,
4921 /* Set the REPLY length, now the RDMA that elides the REPLY message has
4922 * completed and I know it. */
4923 LASSERT(reply != NULL);
4924 LASSERT(reply->msg_type == LNET_MSG_GET);
4925 LASSERT(reply->msg_ev.type == LNET_EVENT_REPLY);
4927 /* NB I trusted my peer to RDMA. If she tells me she's written beyond
4928 * the end of my buffer, I might as well be dead. */
4929 LASSERT(len <= reply->msg_ev.mlength);
4931 reply->msg_ev.mlength = len;
4933 EXPORT_SYMBOL(lnet_set_reply_msg_len);
4936 * Initiate an asynchronous GET operation.
4938 * On the initiator node, an LNET_EVENT_SEND is logged when the GET request
4939 * is sent, and an LNET_EVENT_REPLY is logged when the data returned from
4940 * the target node in the REPLY has been written to local MD.
4942 * On the target node, an LNET_EVENT_GET is logged when the GET request
4943 * arrives and is accepted into a MD.
4945 * \param self,target,portal,match_bits,offset See the discussion in LNetPut().
4946 * \param mdh A handle for the MD that describes the memory into which the
4947 * requested data will be received. The MD must be "free floating" (See LNetMDBind()).
4949 * \retval 0 Success, and only in this case events will be generated
4950 * and logged to EQ (if it exists) of the MD.
4951 * \retval -EIO Simulated failure.
4952 * \retval -ENOMEM Memory allocation failure.
4953 * \retval -ENOENT Invalid MD object.
4956 LNetGet(lnet_nid_t self, struct lnet_handle_md mdh,
4957 struct lnet_process_id target, unsigned int portal,
4958 __u64 match_bits, unsigned int offset, bool recovery)
4960 struct lnet_msg *msg;
4961 struct lnet_libmd *md;
4962 struct lnet_rsp_tracker *rspt;
4966 LASSERT(the_lnet.ln_refcount > 0);
4968 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
4969 fail_peer(target.nid, 1)) /* shall we now? */
4971 CERROR("Dropping GET to %s: simulated failure\n",
4972 libcfs_id2str(target));
4976 msg = lnet_msg_alloc();
4978 CERROR("Dropping GET to %s: ENOMEM on struct lnet_msg\n",
4979 libcfs_id2str(target));
4983 cpt = lnet_cpt_of_cookie(mdh.cookie);
4985 rspt = lnet_rspt_alloc(cpt);
4987 CERROR("Dropping GET to %s: ENOMEM on response tracker\n",
4988 libcfs_id2str(target));
4991 INIT_LIST_HEAD(&rspt->rspt_on_list);
4993 msg->msg_recovery = recovery;
4997 md = lnet_handle2md(&mdh);
4998 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
4999 CERROR("Dropping GET (%llu:%d:%s): MD (%d) invalid\n",
5000 match_bits, portal, libcfs_id2str(target),
5001 md == NULL ? -1 : md->md_threshold);
5002 if (md != NULL && md->md_me != NULL)
5003 CERROR("REPLY MD also attached to portal %d\n",
5004 md->md_me->me_portal);
5006 lnet_res_unlock(cpt);
5009 lnet_rspt_free(rspt, cpt);
5013 CDEBUG(D_NET, "LNetGet -> %s\n", libcfs_id2str(target));
5015 lnet_msg_attach_md(msg, md, 0, 0);
5017 lnet_prep_send(msg, LNET_MSG_GET, target, 0, 0);
5019 msg->msg_hdr.msg.get.match_bits = cpu_to_le64(match_bits);
5020 msg->msg_hdr.msg.get.ptl_index = cpu_to_le32(portal);
5021 msg->msg_hdr.msg.get.src_offset = cpu_to_le32(offset);
5022 msg->msg_hdr.msg.get.sink_length = cpu_to_le32(md->md_length);
5024 /* NB handles only looked up by creator (no flips) */
5025 msg->msg_hdr.msg.get.return_wmd.wh_interface_cookie =
5026 the_lnet.ln_interface_cookie;
5027 msg->msg_hdr.msg.get.return_wmd.wh_object_cookie =
5028 md->md_lh.lh_cookie;
5030 lnet_res_unlock(cpt);
5032 lnet_build_msg_event(msg, LNET_EVENT_SEND);
5034 lnet_attach_rsp_tracker(rspt, cpt, md, mdh);
5036 rc = lnet_send(self, msg, LNET_NID_ANY);
5038 CNETERR("Error sending GET to %s: %d\n",
5039 libcfs_id2str(target), rc);
5040 msg->msg_no_resend = true;
5041 lnet_finalize(msg, rc);
5044 /* completion will be signalled by an event */
5047 EXPORT_SYMBOL(LNetGet);
5050 * Calculate distance to node at \a dstnid.
5052 * \param dstnid Target NID.
5053 * \param srcnidp If not NULL, NID of the local interface to reach \a dstnid
5055 * \param orderp If not NULL, order of the route to reach \a dstnid is saved
5058 * \retval 0 If \a dstnid belongs to a local interface, and reserved option
5059 * local_nid_dist_zero is set, which is the default.
5060 * \retval positives Distance to target NID, i.e. number of hops plus one.
5061 * \retval -EHOSTUNREACH If \a dstnid is not reachable.
5064 LNetDist(lnet_nid_t dstnid, lnet_nid_t *srcnidp, __u32 *orderp)
5066 struct list_head *e;
5067 struct lnet_ni *ni = NULL;
5068 struct lnet_remotenet *rnet;
5069 __u32 dstnet = LNET_NIDNET(dstnid);
5073 struct list_head *rn_list;
5075 /* if !local_nid_dist_zero, I don't return a distance of 0 ever
5076 * (when lustre sees a distance of 0, it substitutes 0@lo), so I
5077 * keep order 0 free for 0@lo and order 1 free for a local NID
5080 LASSERT(the_lnet.ln_refcount > 0);
5082 cpt = lnet_net_lock_current();
5084 while ((ni = lnet_get_next_ni_locked(NULL, ni))) {
5085 if (ni->ni_nid == dstnid) {
5086 if (srcnidp != NULL)
5088 if (orderp != NULL) {
5089 if (LNET_NETTYP(LNET_NIDNET(dstnid)) == LOLND)
5094 lnet_net_unlock(cpt);
5096 return local_nid_dist_zero ? 0 : 1;
5099 if (LNET_NIDNET(ni->ni_nid) == dstnet) {
5100 /* Check if ni was originally created in
5101 * current net namespace.
5102 * If not, assign order above 0xffff0000,
5103 * to make this ni not a priority. */
5104 if (current->nsproxy &&
5105 !net_eq(ni->ni_net_ns, current->nsproxy->net_ns))
5106 order += 0xffff0000;
5107 if (srcnidp != NULL)
5108 *srcnidp = ni->ni_nid;
5111 lnet_net_unlock(cpt);
5118 rn_list = lnet_net2rnethash(dstnet);
5119 list_for_each(e, rn_list) {
5120 rnet = list_entry(e, struct lnet_remotenet, lrn_list);
5122 if (rnet->lrn_net == dstnet) {
5123 struct lnet_route *route;
5124 struct lnet_route *shortest = NULL;
5125 __u32 shortest_hops = LNET_UNDEFINED_HOPS;
5128 LASSERT(!list_empty(&rnet->lrn_routes));
5130 list_for_each_entry(route, &rnet->lrn_routes,
5132 route_hops = route->lr_hops;
5133 if (route_hops == LNET_UNDEFINED_HOPS)
5135 if (shortest == NULL ||
5136 route_hops < shortest_hops) {
5138 shortest_hops = route_hops;
5142 LASSERT(shortest != NULL);
5143 hops = shortest_hops;
5144 if (srcnidp != NULL) {
5145 struct lnet_net *net;
5146 net = lnet_get_net_locked(shortest->lr_lnet);
5148 ni = lnet_get_next_ni_locked(net, NULL);
5149 *srcnidp = ni->ni_nid;
5153 lnet_net_unlock(cpt);
5159 lnet_net_unlock(cpt);
5160 return -EHOSTUNREACH;
5162 EXPORT_SYMBOL(LNetDist);