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/
31 * lnet/lnet/lib-move.c
33 * Data movement routines
36 #define DEBUG_SUBSYSTEM S_LNET
38 #include <linux/pagemap.h>
40 #include <lnet/lib-lnet.h>
41 #include <linux/nsproxy.h>
42 #include <net/net_namespace.h>
44 static int local_nid_dist_zero = 1;
45 module_param(local_nid_dist_zero, int, 0444);
46 MODULE_PARM_DESC(local_nid_dist_zero, "Reserved");
48 struct lnet_send_data {
49 struct lnet_ni *sd_best_ni;
50 struct lnet_peer_ni *sd_best_lpni;
51 struct lnet_peer_ni *sd_final_dst_lpni;
52 struct lnet_peer *sd_peer;
53 struct lnet_peer *sd_gw_peer;
54 struct lnet_peer_ni *sd_gw_lpni;
55 struct lnet_peer_net *sd_peer_net;
56 struct lnet_msg *sd_msg;
57 lnet_nid_t sd_dst_nid;
58 lnet_nid_t sd_src_nid;
59 lnet_nid_t sd_rtr_nid;
66 lnet_msg_is_response(struct lnet_msg *msg)
68 return msg->msg_type == LNET_MSG_ACK || msg->msg_type == LNET_MSG_REPLY;
72 lnet_response_tracking_enabled(__u32 msg_type, unsigned int md_options)
74 if (md_options & LNET_MD_NO_TRACK_RESPONSE)
75 /* Explicitly disabled in MD options */
78 if (md_options & LNET_MD_TRACK_RESPONSE)
79 /* Explicity enabled in MD options */
82 if (lnet_response_tracking == 3)
83 /* Enabled for all message types */
86 if (msg_type == LNET_MSG_PUT)
87 return lnet_response_tracking == 2;
89 if (msg_type == LNET_MSG_GET)
90 return lnet_response_tracking == 1;
95 static inline struct lnet_comm_count *
96 get_stats_counts(struct lnet_element_stats *stats,
97 enum lnet_stats_type stats_type)
100 case LNET_STATS_TYPE_SEND:
101 return &stats->el_send_stats;
102 case LNET_STATS_TYPE_RECV:
103 return &stats->el_recv_stats;
104 case LNET_STATS_TYPE_DROP:
105 return &stats->el_drop_stats;
107 CERROR("Unknown stats type\n");
113 void lnet_incr_stats(struct lnet_element_stats *stats,
114 enum lnet_msg_type msg_type,
115 enum lnet_stats_type stats_type)
117 struct lnet_comm_count *counts = get_stats_counts(stats, stats_type);
123 atomic_inc(&counts->co_ack_count);
126 atomic_inc(&counts->co_put_count);
129 atomic_inc(&counts->co_get_count);
132 atomic_inc(&counts->co_reply_count);
135 atomic_inc(&counts->co_hello_count);
138 CERROR("There is a BUG in the code. Unknown message type\n");
143 __u32 lnet_sum_stats(struct lnet_element_stats *stats,
144 enum lnet_stats_type stats_type)
146 struct lnet_comm_count *counts = get_stats_counts(stats, stats_type);
150 return (atomic_read(&counts->co_ack_count) +
151 atomic_read(&counts->co_put_count) +
152 atomic_read(&counts->co_get_count) +
153 atomic_read(&counts->co_reply_count) +
154 atomic_read(&counts->co_hello_count));
157 static inline void assign_stats(struct lnet_ioctl_comm_count *msg_stats,
158 struct lnet_comm_count *counts)
160 msg_stats->ico_get_count = atomic_read(&counts->co_get_count);
161 msg_stats->ico_put_count = atomic_read(&counts->co_put_count);
162 msg_stats->ico_reply_count = atomic_read(&counts->co_reply_count);
163 msg_stats->ico_ack_count = atomic_read(&counts->co_ack_count);
164 msg_stats->ico_hello_count = atomic_read(&counts->co_hello_count);
167 void lnet_usr_translate_stats(struct lnet_ioctl_element_msg_stats *msg_stats,
168 struct lnet_element_stats *stats)
170 struct lnet_comm_count *counts;
175 counts = get_stats_counts(stats, LNET_STATS_TYPE_SEND);
178 assign_stats(&msg_stats->im_send_stats, counts);
180 counts = get_stats_counts(stats, LNET_STATS_TYPE_RECV);
183 assign_stats(&msg_stats->im_recv_stats, counts);
185 counts = get_stats_counts(stats, LNET_STATS_TYPE_DROP);
188 assign_stats(&msg_stats->im_drop_stats, counts);
192 lnet_fail_nid(lnet_nid_t nid, unsigned int threshold)
194 struct lnet_test_peer *tp;
195 struct list_head *el;
196 struct list_head *next;
199 /* NB: use lnet_net_lock(0) to serialize operations on test peers */
200 if (threshold != 0) {
201 /* Adding a new entry */
202 LIBCFS_ALLOC(tp, sizeof(*tp));
207 tp->tp_threshold = threshold;
210 list_add_tail(&tp->tp_list, &the_lnet.ln_test_peers);
217 list_for_each_safe(el, next, &the_lnet.ln_test_peers) {
218 tp = list_entry(el, struct lnet_test_peer, tp_list);
220 if (tp->tp_threshold == 0 || /* needs culling anyway */
221 nid == LNET_NID_ANY || /* removing all entries */
222 tp->tp_nid == nid) { /* matched this one */
223 list_move(&tp->tp_list, &cull);
229 while (!list_empty(&cull)) {
230 tp = list_entry(cull.next, struct lnet_test_peer, tp_list);
232 list_del(&tp->tp_list);
233 LIBCFS_FREE(tp, sizeof(*tp));
239 fail_peer (lnet_nid_t nid, int outgoing)
241 struct lnet_test_peer *tp;
242 struct list_head *el;
243 struct list_head *next;
247 /* NB: use lnet_net_lock(0) to serialize operations on test peers */
250 list_for_each_safe(el, next, &the_lnet.ln_test_peers) {
251 tp = list_entry(el, struct lnet_test_peer, tp_list);
253 if (tp->tp_threshold == 0) {
256 /* only cull zombies on outgoing tests,
257 * since we may be at interrupt priority on
258 * incoming messages. */
259 list_move(&tp->tp_list, &cull);
264 if (tp->tp_nid == LNET_NID_ANY || /* fail every peer */
265 nid == tp->tp_nid) { /* fail this peer */
268 if (tp->tp_threshold != LNET_MD_THRESH_INF) {
271 tp->tp_threshold == 0) {
273 list_move(&tp->tp_list, &cull);
282 while (!list_empty(&cull)) {
283 tp = list_entry(cull.next, struct lnet_test_peer, tp_list);
284 list_del(&tp->tp_list);
286 LIBCFS_FREE(tp, sizeof(*tp));
293 lnet_iov_nob(unsigned int niov, struct kvec *iov)
295 unsigned int nob = 0;
297 LASSERT(niov == 0 || iov != NULL);
299 nob += (iov++)->iov_len;
303 EXPORT_SYMBOL(lnet_iov_nob);
306 lnet_copy_iov2iov(unsigned int ndiov, struct kvec *diov, unsigned int doffset,
307 unsigned int nsiov, struct kvec *siov, unsigned int soffset,
310 /* NB diov, siov are READ-ONLY */
311 unsigned int this_nob;
316 /* skip complete frags before 'doffset' */
318 while (doffset >= diov->iov_len) {
319 doffset -= diov->iov_len;
325 /* skip complete frags before 'soffset' */
327 while (soffset >= siov->iov_len) {
328 soffset -= siov->iov_len;
337 this_nob = min3((unsigned int)diov->iov_len - doffset,
338 (unsigned int)siov->iov_len - soffset,
341 memcpy((char *)diov->iov_base + doffset,
342 (char *)siov->iov_base + soffset, this_nob);
345 if (diov->iov_len > doffset + this_nob) {
353 if (siov->iov_len > soffset + this_nob) {
362 EXPORT_SYMBOL(lnet_copy_iov2iov);
365 lnet_kiov_nob(unsigned int niov, struct bio_vec *kiov)
367 unsigned int nob = 0;
369 LASSERT(niov == 0 || kiov != NULL);
371 nob += (kiov++)->bv_len;
375 EXPORT_SYMBOL(lnet_kiov_nob);
378 lnet_copy_kiov2kiov(unsigned int ndiov, struct bio_vec *diov,
379 unsigned int doffset,
380 unsigned int nsiov, struct bio_vec *siov,
381 unsigned int soffset,
384 /* NB diov, siov are READ-ONLY */
385 unsigned int this_nob;
392 LASSERT (!in_interrupt ());
395 while (doffset >= diov->bv_len) {
396 doffset -= diov->bv_len;
403 while (soffset >= siov->bv_len) {
404 soffset -= siov->bv_len;
413 this_nob = min3(diov->bv_len - doffset,
414 siov->bv_len - soffset,
418 daddr = ((char *)kmap(diov->bv_page)) +
419 diov->bv_offset + doffset;
421 saddr = ((char *)kmap(siov->bv_page)) +
422 siov->bv_offset + soffset;
424 /* Vanishing risk of kmap deadlock when mapping 2 pages.
425 * However in practice at least one of the kiovs will be mapped
426 * kernel pages and the map/unmap will be NOOPs */
428 memcpy (daddr, saddr, this_nob);
431 if (diov->bv_len > doffset + this_nob) {
435 kunmap(diov->bv_page);
442 if (siov->bv_len > soffset + this_nob) {
446 kunmap(siov->bv_page);
455 kunmap(diov->bv_page);
457 kunmap(siov->bv_page);
459 EXPORT_SYMBOL(lnet_copy_kiov2kiov);
462 lnet_copy_kiov2iov (unsigned int niov, struct kvec *iov, unsigned int iovoffset,
463 unsigned int nkiov, struct bio_vec *kiov,
464 unsigned int kiovoffset,
467 /* NB iov, kiov are READ-ONLY */
468 unsigned int this_nob;
474 LASSERT (!in_interrupt ());
477 while (iovoffset >= iov->iov_len) {
478 iovoffset -= iov->iov_len;
485 while (kiovoffset >= kiov->bv_len) {
486 kiovoffset -= kiov->bv_len;
495 this_nob = min3((unsigned int)iov->iov_len - iovoffset,
496 (unsigned int)kiov->bv_len - kiovoffset,
500 addr = ((char *)kmap(kiov->bv_page)) +
501 kiov->bv_offset + kiovoffset;
503 memcpy((char *)iov->iov_base + iovoffset, addr, this_nob);
506 if (iov->iov_len > iovoffset + this_nob) {
507 iovoffset += this_nob;
514 if (kiov->bv_len > kiovoffset + this_nob) {
516 kiovoffset += this_nob;
518 kunmap(kiov->bv_page);
528 kunmap(kiov->bv_page);
530 EXPORT_SYMBOL(lnet_copy_kiov2iov);
533 lnet_copy_iov2kiov(unsigned int nkiov, struct bio_vec *kiov,
534 unsigned int kiovoffset,
535 unsigned int niov, struct kvec *iov, unsigned int iovoffset,
538 /* NB kiov, iov are READ-ONLY */
539 unsigned int this_nob;
545 LASSERT (!in_interrupt ());
548 while (kiovoffset >= kiov->bv_len) {
549 kiovoffset -= kiov->bv_len;
556 while (iovoffset >= iov->iov_len) {
557 iovoffset -= iov->iov_len;
566 this_nob = min3((unsigned int)kiov->bv_len - kiovoffset,
567 (unsigned int)iov->iov_len - iovoffset,
571 addr = ((char *)kmap(kiov->bv_page)) +
572 kiov->bv_offset + kiovoffset;
574 memcpy (addr, (char *)iov->iov_base + iovoffset, this_nob);
577 if (kiov->bv_len > kiovoffset + this_nob) {
579 kiovoffset += this_nob;
581 kunmap(kiov->bv_page);
588 if (iov->iov_len > iovoffset + this_nob) {
589 iovoffset += this_nob;
598 kunmap(kiov->bv_page);
600 EXPORT_SYMBOL(lnet_copy_iov2kiov);
603 lnet_extract_kiov(int dst_niov, struct bio_vec *dst,
604 int src_niov, struct bio_vec *src,
605 unsigned int offset, unsigned int len)
607 /* Initialise 'dst' to the subset of 'src' starting at 'offset',
608 * for exactly 'len' bytes, and return the number of entries.
609 * NB not destructive to 'src' */
610 unsigned int frag_len;
613 if (len == 0) /* no data => */
614 return (0); /* no frags */
616 LASSERT(src_niov > 0);
617 while (offset >= src->bv_len) { /* skip initial frags */
618 offset -= src->bv_len;
621 LASSERT(src_niov > 0);
626 LASSERT(src_niov > 0);
627 LASSERT((int)niov <= dst_niov);
629 frag_len = src->bv_len - offset;
630 dst->bv_page = src->bv_page;
631 dst->bv_offset = src->bv_offset + offset;
633 if (len <= frag_len) {
635 LASSERT(dst->bv_offset + dst->bv_len <= PAGE_SIZE);
639 dst->bv_len = frag_len;
640 LASSERT(dst->bv_offset + dst->bv_len <= PAGE_SIZE);
650 EXPORT_SYMBOL(lnet_extract_kiov);
653 lnet_ni_recv(struct lnet_ni *ni, void *private, struct lnet_msg *msg,
654 int delayed, unsigned int offset, unsigned int mlen,
657 unsigned int niov = 0;
658 struct kvec *iov = NULL;
659 struct bio_vec *kiov = NULL;
662 LASSERT (!in_interrupt ());
663 LASSERT (mlen == 0 || msg != NULL);
666 LASSERT(msg->msg_receiving);
667 LASSERT(!msg->msg_sending);
668 LASSERT(rlen == msg->msg_len);
669 LASSERT(mlen <= msg->msg_len);
670 LASSERT(msg->msg_offset == offset);
671 LASSERT(msg->msg_wanted == mlen);
673 msg->msg_receiving = 0;
676 niov = msg->msg_niov;
677 kiov = msg->msg_kiov;
680 LASSERT ((iov == NULL) != (kiov == NULL));
684 rc = (ni->ni_net->net_lnd->lnd_recv)(ni, private, msg, delayed,
685 niov, kiov, offset, mlen,
688 lnet_finalize(msg, rc);
692 lnet_setpayloadbuffer(struct lnet_msg *msg)
694 struct lnet_libmd *md = msg->msg_md;
696 LASSERT(msg->msg_len > 0);
697 LASSERT(!msg->msg_routing);
699 LASSERT(msg->msg_niov == 0);
700 LASSERT(msg->msg_kiov == NULL);
702 msg->msg_niov = md->md_niov;
703 msg->msg_kiov = md->md_kiov;
707 lnet_prep_send(struct lnet_msg *msg, int type, struct lnet_process_id target,
708 unsigned int offset, unsigned int len)
710 msg->msg_type = type;
711 msg->msg_target = target;
713 msg->msg_offset = offset;
716 lnet_setpayloadbuffer(msg);
718 memset (&msg->msg_hdr, 0, sizeof (msg->msg_hdr));
719 msg->msg_hdr.type = cpu_to_le32(type);
720 /* dest_nid will be overwritten by lnet_select_pathway() */
721 msg->msg_hdr.dest_nid = cpu_to_le64(target.nid);
722 msg->msg_hdr.dest_pid = cpu_to_le32(target.pid);
723 /* src_nid will be set later */
724 msg->msg_hdr.src_pid = cpu_to_le32(the_lnet.ln_pid);
725 msg->msg_hdr.payload_length = cpu_to_le32(len);
729 lnet_ni_send(struct lnet_ni *ni, struct lnet_msg *msg)
731 void *priv = msg->msg_private;
734 LASSERT(!in_interrupt());
735 LASSERT(ni->ni_nid == LNET_NID_LO_0 ||
736 (msg->msg_txcredit && msg->msg_peertxcredit));
738 rc = (ni->ni_net->net_lnd->lnd_send)(ni, priv, msg);
740 msg->msg_no_resend = true;
741 lnet_finalize(msg, rc);
746 lnet_ni_eager_recv(struct lnet_ni *ni, struct lnet_msg *msg)
750 LASSERT(!msg->msg_sending);
751 LASSERT(msg->msg_receiving);
752 LASSERT(!msg->msg_rx_ready_delay);
753 LASSERT(ni->ni_net->net_lnd->lnd_eager_recv != NULL);
755 msg->msg_rx_ready_delay = 1;
756 rc = (ni->ni_net->net_lnd->lnd_eager_recv)(ni, msg->msg_private, msg,
759 CERROR("recv from %s / send to %s aborted: "
760 "eager_recv failed %d\n",
761 libcfs_nid2str(msg->msg_rxpeer->lpni_nid),
762 libcfs_id2str(msg->msg_target), rc);
763 LASSERT(rc < 0); /* required by my callers */
770 lnet_is_peer_deadline_passed(struct lnet_peer_ni *lpni, time64_t now)
774 deadline = lpni->lpni_last_alive +
775 lpni->lpni_net->net_tunables.lct_peer_timeout;
778 * assume peer_ni is alive as long as we're within the configured
787 /* NB: returns 1 when alive, 0 when dead, negative when error;
788 * may drop the lnet_net_lock */
790 lnet_peer_alive_locked(struct lnet_ni *ni, struct lnet_peer_ni *lpni,
791 struct lnet_msg *msg)
793 time64_t now = ktime_get_seconds();
795 if (!lnet_peer_aliveness_enabled(lpni))
799 * If we're resending a message, let's attempt to send it even if
800 * the peer is down to fulfill our resend quota on the message
802 if (msg->msg_retry_count > 0)
805 /* try and send recovery messages irregardless */
806 if (msg->msg_recovery)
809 /* always send any responses */
810 if (lnet_msg_is_response(msg))
813 if (!lnet_is_peer_deadline_passed(lpni, now))
816 return lnet_is_peer_ni_alive(lpni);
820 * \param msg The message to be sent.
821 * \param do_send True if lnet_ni_send() should be called in this function.
822 * lnet_send() is going to lnet_net_unlock immediately after this, so
823 * it sets do_send FALSE and I don't do the unlock/send/lock bit.
825 * \retval LNET_CREDIT_OK If \a msg sent or OK to send.
826 * \retval LNET_CREDIT_WAIT If \a msg blocked for credit.
827 * \retval -EHOSTUNREACH If the next hop of the message appears dead.
828 * \retval -ECANCELED If the MD of the message has been unlinked.
831 lnet_post_send_locked(struct lnet_msg *msg, int do_send)
833 struct lnet_peer_ni *lp = msg->msg_txpeer;
834 struct lnet_ni *ni = msg->msg_txni;
835 int cpt = msg->msg_tx_cpt;
836 struct lnet_tx_queue *tq = ni->ni_tx_queues[cpt];
838 /* non-lnet_send() callers have checked before */
839 LASSERT(!do_send || msg->msg_tx_delayed);
840 LASSERT(!msg->msg_receiving);
841 LASSERT(msg->msg_tx_committed);
843 /* can't get here if we're sending to the loopback interface */
844 if (the_lnet.ln_loni)
845 LASSERT(lp->lpni_nid != the_lnet.ln_loni->ni_nid);
847 /* NB 'lp' is always the next hop */
848 if ((msg->msg_target.pid & LNET_PID_USERFLAG) == 0 &&
849 lnet_peer_alive_locked(ni, lp, msg) == 0) {
850 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
851 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length +=
853 lnet_net_unlock(cpt);
855 lnet_incr_stats(&msg->msg_txpeer->lpni_stats,
857 LNET_STATS_TYPE_DROP);
859 lnet_incr_stats(&msg->msg_txni->ni_stats,
861 LNET_STATS_TYPE_DROP);
863 CNETERR("Dropping message for %s: peer not alive\n",
864 libcfs_id2str(msg->msg_target));
865 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_DROPPED;
867 lnet_finalize(msg, -EHOSTUNREACH);
870 return -EHOSTUNREACH;
873 if (msg->msg_md != NULL &&
874 (msg->msg_md->md_flags & LNET_MD_FLAG_ABORTED) != 0) {
875 lnet_net_unlock(cpt);
877 CNETERR("Aborting message for %s: LNetM[DE]Unlink() already "
878 "called on the MD/ME.\n",
879 libcfs_id2str(msg->msg_target));
881 msg->msg_no_resend = true;
882 CDEBUG(D_NET, "msg %p to %s canceled and will not be resent\n",
883 msg, libcfs_id2str(msg->msg_target));
884 lnet_finalize(msg, -ECANCELED);
891 if (!msg->msg_peertxcredit) {
892 spin_lock(&lp->lpni_lock);
893 LASSERT((lp->lpni_txcredits < 0) ==
894 !list_empty(&lp->lpni_txq));
896 msg->msg_peertxcredit = 1;
897 lp->lpni_txqnob += msg->msg_len + sizeof(struct lnet_hdr);
898 lp->lpni_txcredits--;
900 if (lp->lpni_txcredits < lp->lpni_mintxcredits)
901 lp->lpni_mintxcredits = lp->lpni_txcredits;
903 if (lp->lpni_txcredits < 0) {
904 msg->msg_tx_delayed = 1;
905 list_add_tail(&msg->msg_list, &lp->lpni_txq);
906 spin_unlock(&lp->lpni_lock);
907 return LNET_CREDIT_WAIT;
909 spin_unlock(&lp->lpni_lock);
912 if (!msg->msg_txcredit) {
913 LASSERT((tq->tq_credits < 0) ==
914 !list_empty(&tq->tq_delayed));
916 msg->msg_txcredit = 1;
918 atomic_dec(&ni->ni_tx_credits);
920 if (tq->tq_credits < tq->tq_credits_min)
921 tq->tq_credits_min = tq->tq_credits;
923 if (tq->tq_credits < 0) {
924 msg->msg_tx_delayed = 1;
925 list_add_tail(&msg->msg_list, &tq->tq_delayed);
926 return LNET_CREDIT_WAIT;
930 if (unlikely(!list_empty(&the_lnet.ln_delay_rules)) &&
931 lnet_delay_rule_match_locked(&msg->msg_hdr, msg)) {
932 msg->msg_tx_delayed = 1;
933 return LNET_CREDIT_WAIT;
936 /* unset the tx_delay flag as we're going to send it now */
937 msg->msg_tx_delayed = 0;
940 lnet_net_unlock(cpt);
941 lnet_ni_send(ni, msg);
944 return LNET_CREDIT_OK;
948 static struct lnet_rtrbufpool *
949 lnet_msg2bufpool(struct lnet_msg *msg)
951 struct lnet_rtrbufpool *rbp;
954 LASSERT(msg->msg_rx_committed);
956 cpt = msg->msg_rx_cpt;
957 rbp = &the_lnet.ln_rtrpools[cpt][0];
959 LASSERT(msg->msg_len <= LNET_MTU);
960 while (msg->msg_len > (unsigned int)rbp->rbp_npages * PAGE_SIZE) {
962 LASSERT(rbp < &the_lnet.ln_rtrpools[cpt][LNET_NRBPOOLS]);
969 lnet_post_routed_recv_locked(struct lnet_msg *msg, int do_recv)
971 /* lnet_parse is going to lnet_net_unlock immediately after this, so it
972 * sets do_recv FALSE and I don't do the unlock/send/lock bit.
973 * I return LNET_CREDIT_WAIT if msg blocked and LNET_CREDIT_OK if
974 * received or OK to receive */
975 struct lnet_peer_ni *lpni = msg->msg_rxpeer;
976 struct lnet_peer *lp;
977 struct lnet_rtrbufpool *rbp;
978 struct lnet_rtrbuf *rb;
980 LASSERT(msg->msg_kiov == NULL);
981 LASSERT(msg->msg_niov == 0);
982 LASSERT(msg->msg_routing);
983 LASSERT(msg->msg_receiving);
984 LASSERT(!msg->msg_sending);
985 LASSERT(lpni->lpni_peer_net);
986 LASSERT(lpni->lpni_peer_net->lpn_peer);
988 lp = lpni->lpni_peer_net->lpn_peer;
990 /* non-lnet_parse callers only receive delayed messages */
991 LASSERT(!do_recv || msg->msg_rx_delayed);
993 if (!msg->msg_peerrtrcredit) {
994 /* lpni_lock protects the credit manipulation */
995 spin_lock(&lpni->lpni_lock);
997 msg->msg_peerrtrcredit = 1;
998 lpni->lpni_rtrcredits--;
999 if (lpni->lpni_rtrcredits < lpni->lpni_minrtrcredits)
1000 lpni->lpni_minrtrcredits = lpni->lpni_rtrcredits;
1002 if (lpni->lpni_rtrcredits < 0) {
1003 spin_unlock(&lpni->lpni_lock);
1004 /* must have checked eager_recv before here */
1005 LASSERT(msg->msg_rx_ready_delay);
1006 msg->msg_rx_delayed = 1;
1007 /* lp_lock protects the lp_rtrq */
1008 spin_lock(&lp->lp_lock);
1009 list_add_tail(&msg->msg_list, &lp->lp_rtrq);
1010 spin_unlock(&lp->lp_lock);
1011 return LNET_CREDIT_WAIT;
1013 spin_unlock(&lpni->lpni_lock);
1016 rbp = lnet_msg2bufpool(msg);
1018 if (!msg->msg_rtrcredit) {
1019 msg->msg_rtrcredit = 1;
1021 if (rbp->rbp_credits < rbp->rbp_mincredits)
1022 rbp->rbp_mincredits = rbp->rbp_credits;
1024 if (rbp->rbp_credits < 0) {
1025 /* must have checked eager_recv before here */
1026 LASSERT(msg->msg_rx_ready_delay);
1027 msg->msg_rx_delayed = 1;
1028 list_add_tail(&msg->msg_list, &rbp->rbp_msgs);
1029 return LNET_CREDIT_WAIT;
1033 LASSERT(!list_empty(&rbp->rbp_bufs));
1034 rb = list_entry(rbp->rbp_bufs.next, struct lnet_rtrbuf, rb_list);
1035 list_del(&rb->rb_list);
1037 msg->msg_niov = rbp->rbp_npages;
1038 msg->msg_kiov = &rb->rb_kiov[0];
1040 /* unset the msg-rx_delayed flag since we're receiving the message */
1041 msg->msg_rx_delayed = 0;
1044 int cpt = msg->msg_rx_cpt;
1046 lnet_net_unlock(cpt);
1047 lnet_ni_recv(msg->msg_rxni, msg->msg_private, msg, 1,
1048 0, msg->msg_len, msg->msg_len);
1051 return LNET_CREDIT_OK;
1055 lnet_return_tx_credits_locked(struct lnet_msg *msg)
1057 struct lnet_peer_ni *txpeer = msg->msg_txpeer;
1058 struct lnet_ni *txni = msg->msg_txni;
1059 struct lnet_msg *msg2;
1061 if (msg->msg_txcredit) {
1062 struct lnet_ni *ni = msg->msg_txni;
1063 struct lnet_tx_queue *tq = ni->ni_tx_queues[msg->msg_tx_cpt];
1065 /* give back NI txcredits */
1066 msg->msg_txcredit = 0;
1068 LASSERT((tq->tq_credits < 0) ==
1069 !list_empty(&tq->tq_delayed));
1072 atomic_inc(&ni->ni_tx_credits);
1073 if (tq->tq_credits <= 0) {
1074 msg2 = list_entry(tq->tq_delayed.next,
1075 struct lnet_msg, msg_list);
1076 list_del(&msg2->msg_list);
1078 LASSERT(msg2->msg_txni == ni);
1079 LASSERT(msg2->msg_tx_delayed);
1080 LASSERT(msg2->msg_tx_cpt == msg->msg_tx_cpt);
1082 (void) lnet_post_send_locked(msg2, 1);
1086 if (msg->msg_peertxcredit) {
1087 /* give back peer txcredits */
1088 msg->msg_peertxcredit = 0;
1090 spin_lock(&txpeer->lpni_lock);
1091 LASSERT((txpeer->lpni_txcredits < 0) ==
1092 !list_empty(&txpeer->lpni_txq));
1094 txpeer->lpni_txqnob -= msg->msg_len + sizeof(struct lnet_hdr);
1095 LASSERT(txpeer->lpni_txqnob >= 0);
1097 txpeer->lpni_txcredits++;
1098 if (txpeer->lpni_txcredits <= 0) {
1101 msg2 = list_entry(txpeer->lpni_txq.next,
1102 struct lnet_msg, msg_list);
1103 list_del(&msg2->msg_list);
1104 spin_unlock(&txpeer->lpni_lock);
1106 LASSERT(msg2->msg_txpeer == txpeer);
1107 LASSERT(msg2->msg_tx_delayed);
1109 msg2_cpt = msg2->msg_tx_cpt;
1112 * The msg_cpt can be different from the msg2_cpt
1113 * so we need to make sure we lock the correct cpt
1115 * Once we call lnet_post_send_locked() it is no
1116 * longer safe to access msg2, since it could've
1117 * been freed by lnet_finalize(), but we still
1118 * need to relock the correct cpt, so we cache the
1119 * msg2_cpt for the purpose of the check that
1120 * follows the call to lnet_pose_send_locked().
1122 if (msg2_cpt != msg->msg_tx_cpt) {
1123 lnet_net_unlock(msg->msg_tx_cpt);
1124 lnet_net_lock(msg2_cpt);
1126 (void) lnet_post_send_locked(msg2, 1);
1127 if (msg2_cpt != msg->msg_tx_cpt) {
1128 lnet_net_unlock(msg2_cpt);
1129 lnet_net_lock(msg->msg_tx_cpt);
1132 spin_unlock(&txpeer->lpni_lock);
1137 msg->msg_txni = NULL;
1138 lnet_ni_decref_locked(txni, msg->msg_tx_cpt);
1141 if (txpeer != NULL) {
1142 msg->msg_txpeer = NULL;
1143 lnet_peer_ni_decref_locked(txpeer);
1148 lnet_schedule_blocked_locked(struct lnet_rtrbufpool *rbp)
1150 struct lnet_msg *msg;
1152 if (list_empty(&rbp->rbp_msgs))
1154 msg = list_entry(rbp->rbp_msgs.next,
1155 struct lnet_msg, msg_list);
1156 list_del(&msg->msg_list);
1158 (void)lnet_post_routed_recv_locked(msg, 1);
1162 lnet_drop_routed_msgs_locked(struct list_head *list, int cpt)
1164 struct lnet_msg *msg;
1165 struct lnet_msg *tmp;
1167 lnet_net_unlock(cpt);
1169 list_for_each_entry_safe(msg, tmp, list, msg_list) {
1170 lnet_ni_recv(msg->msg_rxni, msg->msg_private, NULL,
1171 0, 0, 0, msg->msg_hdr.payload_length);
1172 list_del_init(&msg->msg_list);
1173 msg->msg_no_resend = true;
1174 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_ERROR;
1175 lnet_finalize(msg, -ECANCELED);
1182 lnet_return_rx_credits_locked(struct lnet_msg *msg)
1184 struct lnet_peer_ni *rxpeerni = msg->msg_rxpeer;
1185 struct lnet_peer *lp;
1186 struct lnet_ni *rxni = msg->msg_rxni;
1187 struct lnet_msg *msg2;
1189 if (msg->msg_rtrcredit) {
1190 /* give back global router credits */
1191 struct lnet_rtrbuf *rb;
1192 struct lnet_rtrbufpool *rbp;
1194 /* NB If a msg ever blocks for a buffer in rbp_msgs, it stays
1195 * there until it gets one allocated, or aborts the wait
1197 LASSERT(msg->msg_kiov != NULL);
1199 rb = list_entry(msg->msg_kiov, struct lnet_rtrbuf, rb_kiov[0]);
1202 msg->msg_kiov = NULL;
1203 msg->msg_rtrcredit = 0;
1205 LASSERT(rbp == lnet_msg2bufpool(msg));
1207 LASSERT((rbp->rbp_credits > 0) ==
1208 !list_empty(&rbp->rbp_bufs));
1210 /* If routing is now turned off, we just drop this buffer and
1211 * don't bother trying to return credits. */
1212 if (!the_lnet.ln_routing) {
1213 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1217 /* It is possible that a user has lowered the desired number of
1218 * buffers in this pool. Make sure we never put back
1219 * more buffers than the stated number. */
1220 if (unlikely(rbp->rbp_credits >= rbp->rbp_req_nbuffers)) {
1221 /* Discard this buffer so we don't have too
1223 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1224 rbp->rbp_nbuffers--;
1226 list_add(&rb->rb_list, &rbp->rbp_bufs);
1228 if (rbp->rbp_credits <= 0)
1229 lnet_schedule_blocked_locked(rbp);
1234 if (msg->msg_peerrtrcredit) {
1236 LASSERT(rxpeerni->lpni_peer_net);
1237 LASSERT(rxpeerni->lpni_peer_net->lpn_peer);
1239 /* give back peer router credits */
1240 msg->msg_peerrtrcredit = 0;
1242 spin_lock(&rxpeerni->lpni_lock);
1243 rxpeerni->lpni_rtrcredits++;
1244 spin_unlock(&rxpeerni->lpni_lock);
1246 lp = rxpeerni->lpni_peer_net->lpn_peer;
1247 spin_lock(&lp->lp_lock);
1249 /* drop all messages which are queued to be routed on that
1251 if (!the_lnet.ln_routing) {
1253 list_splice_init(&lp->lp_rtrq, &drop);
1254 spin_unlock(&lp->lp_lock);
1255 lnet_drop_routed_msgs_locked(&drop, msg->msg_rx_cpt);
1256 } else if (!list_empty(&lp->lp_rtrq)) {
1259 msg2 = list_entry(lp->lp_rtrq.next,
1260 struct lnet_msg, msg_list);
1261 list_del(&msg2->msg_list);
1262 msg2_cpt = msg2->msg_rx_cpt;
1263 spin_unlock(&lp->lp_lock);
1265 * messages on the lp_rtrq can be from any NID in
1266 * the peer, which means they might have different
1267 * cpts. We need to make sure we lock the right
1270 if (msg2_cpt != msg->msg_rx_cpt) {
1271 lnet_net_unlock(msg->msg_rx_cpt);
1272 lnet_net_lock(msg2_cpt);
1274 (void) lnet_post_routed_recv_locked(msg2, 1);
1275 if (msg2_cpt != msg->msg_rx_cpt) {
1276 lnet_net_unlock(msg2_cpt);
1277 lnet_net_lock(msg->msg_rx_cpt);
1280 spin_unlock(&lp->lp_lock);
1284 msg->msg_rxni = NULL;
1285 lnet_ni_decref_locked(rxni, msg->msg_rx_cpt);
1287 if (rxpeerni != NULL) {
1288 msg->msg_rxpeer = NULL;
1289 lnet_peer_ni_decref_locked(rxpeerni);
1293 static struct lnet_peer_ni *
1294 lnet_select_peer_ni(struct lnet_ni *best_ni, lnet_nid_t dst_nid,
1295 struct lnet_peer *peer,
1296 struct lnet_peer_ni *best_lpni,
1297 struct lnet_peer_net *peer_net)
1300 * Look at the peer NIs for the destination peer that connect
1301 * to the chosen net. If a peer_ni is preferred when using the
1302 * best_ni to communicate, we use that one. If there is no
1303 * preferred peer_ni, or there are multiple preferred peer_ni,
1304 * the available transmit credits are used. If the transmit
1305 * credits are equal, we round-robin over the peer_ni.
1307 struct lnet_peer_ni *lpni = NULL;
1308 int best_lpni_credits = (best_lpni) ? best_lpni->lpni_txcredits :
1310 int best_lpni_healthv = (best_lpni) ?
1311 atomic_read(&best_lpni->lpni_healthv) : 0;
1312 bool best_lpni_is_preferred = false;
1313 bool lpni_is_preferred;
1315 __u32 lpni_sel_prio;
1316 __u32 best_sel_prio = LNET_MAX_SELECTION_PRIORITY;
1318 while ((lpni = lnet_get_next_peer_ni_locked(peer, peer_net, lpni))) {
1320 * if the best_ni we've chosen aleady has this lpni
1321 * preferred, then let's use it
1324 lpni_is_preferred = lnet_peer_is_pref_nid_locked(lpni,
1326 CDEBUG(D_NET, "%s lpni_is_preferred = %d\n",
1327 libcfs_nid2str(best_ni->ni_nid),
1330 lpni_is_preferred = false;
1333 lpni_healthv = atomic_read(&lpni->lpni_healthv);
1334 lpni_sel_prio = lpni->lpni_sel_priority;
1337 CDEBUG(D_NET, "n:[%s, %s] h:[%d, %d] p:[%d, %d] c:[%d, %d] s:[%d, %d]\n",
1338 libcfs_nid2str(lpni->lpni_nid),
1339 libcfs_nid2str(best_lpni->lpni_nid),
1340 lpni_healthv, best_lpni_healthv,
1341 lpni_sel_prio, best_sel_prio,
1342 lpni->lpni_txcredits, best_lpni_credits,
1343 lpni->lpni_seq, best_lpni->lpni_seq);
1347 /* pick the healthiest peer ni */
1348 if (lpni_healthv < best_lpni_healthv)
1350 else if (lpni_healthv > best_lpni_healthv) {
1351 if (best_lpni_is_preferred)
1352 best_lpni_is_preferred = false;
1356 if (lpni_sel_prio > best_sel_prio)
1358 else if (lpni_sel_prio < best_sel_prio) {
1359 if (best_lpni_is_preferred)
1360 best_lpni_is_preferred = false;
1364 /* if this is a preferred peer use it */
1365 if (!best_lpni_is_preferred && lpni_is_preferred) {
1366 best_lpni_is_preferred = true;
1368 } else if (best_lpni_is_preferred && !lpni_is_preferred) {
1369 /* this is not the preferred peer so let's ignore
1375 if (lpni->lpni_txcredits < best_lpni_credits)
1376 /* We already have a peer that has more credits
1377 * available than this one. No need to consider
1378 * this peer further.
1381 else if (lpni->lpni_txcredits > best_lpni_credits)
1384 /* The best peer found so far and the current peer
1385 * have the same number of available credits let's
1386 * make sure to select between them using Round Robin
1388 if (best_lpni && (best_lpni->lpni_seq <= lpni->lpni_seq))
1391 best_lpni_is_preferred = lpni_is_preferred;
1392 best_lpni_healthv = lpni_healthv;
1393 best_sel_prio = lpni_sel_prio;
1395 best_lpni_credits = lpni->lpni_txcredits;
1398 /* if we still can't find a peer ni then we can't reach it */
1400 __u32 net_id = (peer_net) ? peer_net->lpn_net_id :
1401 LNET_NIDNET(dst_nid);
1402 CDEBUG(D_NET, "no peer_ni found on peer net %s\n",
1403 libcfs_net2str(net_id));
1407 CDEBUG(D_NET, "sd_best_lpni = %s\n",
1408 libcfs_nid2str(best_lpni->lpni_nid));
1414 * Prerequisite: the best_ni should already be set in the sd
1415 * Find the best lpni.
1416 * If the net id is provided then restrict lpni selection on
1417 * that particular net.
1418 * Otherwise find any reachable lpni. When dealing with an MR
1419 * gateway and it has multiple lpnis which we can use
1420 * we want to select the best one from the list of reachable
1423 static inline struct lnet_peer_ni *
1424 lnet_find_best_lpni(struct lnet_ni *lni, lnet_nid_t dst_nid,
1425 struct lnet_peer *peer, __u32 net_id)
1427 struct lnet_peer_net *peer_net;
1429 /* find the best_lpni on any local network */
1430 if (net_id == LNET_NET_ANY) {
1431 struct lnet_peer_ni *best_lpni = NULL;
1432 struct lnet_peer_net *lpn;
1433 list_for_each_entry(lpn, &peer->lp_peer_nets, lpn_peer_nets) {
1434 /* no net specified find any reachable peer ni */
1435 if (!lnet_islocalnet_locked(lpn->lpn_net_id))
1437 best_lpni = lnet_select_peer_ni(lni, dst_nid, peer,
1443 /* restrict on the specified net */
1444 peer_net = lnet_peer_get_net_locked(peer, net_id);
1446 return lnet_select_peer_ni(lni, dst_nid, peer, NULL, peer_net);
1452 lnet_compare_gw_lpnis(struct lnet_peer_ni *lpni1, struct lnet_peer_ni *lpni2)
1454 if (lpni1->lpni_txqnob < lpni2->lpni_txqnob)
1457 if (lpni1->lpni_txqnob > lpni2->lpni_txqnob)
1460 if (lpni1->lpni_txcredits > lpni2->lpni_txcredits)
1463 if (lpni1->lpni_txcredits < lpni2->lpni_txcredits)
1469 /* Compare route priorities and hop counts */
1471 lnet_compare_routes(struct lnet_route *r1, struct lnet_route *r2)
1473 int r1_hops = (r1->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r1->lr_hops;
1474 int r2_hops = (r2->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r2->lr_hops;
1476 if (r1->lr_priority < r2->lr_priority)
1479 if (r1->lr_priority > r2->lr_priority)
1482 if (r1_hops < r2_hops)
1485 if (r1_hops > r2_hops)
1491 static struct lnet_route *
1492 lnet_find_route_locked(struct lnet_remotenet *rnet, __u32 src_net,
1493 struct lnet_peer_ni *remote_lpni,
1494 struct lnet_route **prev_route,
1495 struct lnet_peer_ni **gwni)
1497 struct lnet_peer_ni *lpni, *best_gw_ni = NULL;
1498 struct lnet_route *best_route;
1499 struct lnet_route *last_route;
1500 struct lnet_route *route;
1502 bool best_rte_is_preferred = false;
1505 CDEBUG(D_NET, "Looking up a route to %s, from %s\n",
1506 libcfs_net2str(rnet->lrn_net), libcfs_net2str(src_net));
1508 best_route = last_route = NULL;
1509 list_for_each_entry(route, &rnet->lrn_routes, lr_list) {
1510 if (!lnet_is_route_alive(route))
1512 gw_pnid = route->lr_gateway->lp_primary_nid;
1514 /* no protection on below fields, but it's harmless */
1515 if (last_route && (last_route->lr_seq - route->lr_seq < 0))
1518 /* if the best route found is in the preferred list then
1519 * tag it as preferred and use it later on. But if we
1520 * didn't find any routes which are on the preferred list
1521 * then just use the best route possible.
1523 rc = lnet_peer_is_pref_rtr_locked(remote_lpni, gw_pnid);
1525 if (!best_route || (rc && !best_rte_is_preferred)) {
1526 /* Restrict the selection of the router NI on the
1527 * src_net provided. If the src_net is LNET_NID_ANY,
1528 * then select the best interface available.
1530 lpni = lnet_find_best_lpni(NULL, LNET_NID_ANY,
1535 "Gateway %s does not have a peer NI on net %s\n",
1536 libcfs_nid2str(gw_pnid),
1537 libcfs_net2str(src_net));
1542 if (rc && !best_rte_is_preferred) {
1543 /* This is the first preferred route we found,
1544 * so it beats any route found previously
1550 best_rte_is_preferred = true;
1551 CDEBUG(D_NET, "preferred gw = %s\n",
1552 libcfs_nid2str(gw_pnid));
1554 } else if ((!rc) && best_rte_is_preferred)
1555 /* The best route we found so far is in the preferred
1556 * list, so it beats any non-preferred route
1561 best_route = last_route = route;
1566 rc = lnet_compare_routes(route, best_route);
1570 /* Restrict the selection of the router NI on the
1571 * src_net provided. If the src_net is LNET_NID_ANY,
1572 * then select the best interface available.
1574 lpni = lnet_find_best_lpni(NULL, LNET_NID_ANY,
1579 "Gateway %s does not have a peer NI on net %s\n",
1580 libcfs_nid2str(gw_pnid),
1581 libcfs_net2str(src_net));
1591 rc = lnet_compare_gw_lpnis(lpni, best_gw_ni);
1595 if (rc == 1 || route->lr_seq <= best_route->lr_seq) {
1602 *prev_route = last_route;
1608 static struct lnet_ni *
1609 lnet_get_best_ni(struct lnet_net *local_net, struct lnet_ni *best_ni,
1610 struct lnet_peer *peer, struct lnet_peer_net *peer_net,
1613 struct lnet_ni *ni = NULL;
1614 unsigned int shortest_distance;
1617 __u32 best_sel_prio;
1620 * If there is no peer_ni that we can send to on this network,
1621 * then there is no point in looking for a new best_ni here.
1623 if (!lnet_get_next_peer_ni_locked(peer, peer_net, NULL))
1626 if (best_ni == NULL) {
1627 best_sel_prio = LNET_MAX_SELECTION_PRIORITY;
1628 shortest_distance = UINT_MAX;
1629 best_credits = INT_MIN;
1632 shortest_distance = cfs_cpt_distance(lnet_cpt_table(), md_cpt,
1633 best_ni->ni_dev_cpt);
1634 best_credits = atomic_read(&best_ni->ni_tx_credits);
1635 best_healthv = atomic_read(&best_ni->ni_healthv);
1636 best_sel_prio = best_ni->ni_sel_priority;
1639 while ((ni = lnet_get_next_ni_locked(local_net, ni))) {
1640 unsigned int distance;
1646 ni_credits = atomic_read(&ni->ni_tx_credits);
1647 ni_healthv = atomic_read(&ni->ni_healthv);
1648 ni_fatal = atomic_read(&ni->ni_fatal_error_on);
1649 ni_sel_prio = ni->ni_sel_priority;
1652 * calculate the distance from the CPT on which
1653 * the message memory is allocated to the CPT of
1654 * the NI's physical device
1656 distance = cfs_cpt_distance(lnet_cpt_table(),
1661 * All distances smaller than the NUMA range
1662 * are treated equally.
1664 if (distance < lnet_numa_range)
1665 distance = lnet_numa_range;
1668 * Select on health, shorter distance, available
1669 * credits, then round-robin.
1675 CDEBUG(D_NET, "compare ni %s [c:%d, d:%d, s:%d, p:%u] with best_ni %s [c:%d, d:%d, s:%d, p:%u]\n",
1676 libcfs_nid2str(ni->ni_nid), ni_credits, distance,
1677 ni->ni_seq, ni_sel_prio,
1678 (best_ni) ? libcfs_nid2str(best_ni->ni_nid)
1679 : "not selected", best_credits, shortest_distance,
1680 (best_ni) ? best_ni->ni_seq : 0,
1685 if (ni_healthv < best_healthv)
1687 else if (ni_healthv > best_healthv)
1690 if (ni_sel_prio > best_sel_prio)
1692 else if (ni_sel_prio < best_sel_prio)
1695 if (distance > shortest_distance)
1697 else if (distance < shortest_distance)
1700 if (ni_credits < best_credits)
1702 else if (ni_credits > best_credits)
1705 if (best_ni && best_ni->ni_seq <= ni->ni_seq)
1709 best_sel_prio = ni_sel_prio;
1710 shortest_distance = distance;
1711 best_healthv = ni_healthv;
1713 best_credits = ni_credits;
1716 CDEBUG(D_NET, "selected best_ni %s\n",
1717 (best_ni) ? libcfs_nid2str(best_ni->ni_nid) : "no selection");
1723 * Traffic to the LNET_RESERVED_PORTAL may not trigger peer discovery,
1724 * because such traffic is required to perform discovery. We therefore
1725 * exclude all GET and PUT on that portal. We also exclude all ACK and
1726 * REPLY traffic, but that is because the portal is not tracked in the
1727 * message structure for these message types. We could restrict this
1728 * further by also checking for LNET_PROTO_PING_MATCHBITS.
1731 lnet_msg_discovery(struct lnet_msg *msg)
1733 if (msg->msg_type == LNET_MSG_PUT) {
1734 if (msg->msg_hdr.msg.put.ptl_index != LNET_RESERVED_PORTAL)
1736 } else if (msg->msg_type == LNET_MSG_GET) {
1737 if (msg->msg_hdr.msg.get.ptl_index != LNET_RESERVED_PORTAL)
1743 #define SRC_SPEC 0x0001
1744 #define SRC_ANY 0x0002
1745 #define LOCAL_DST 0x0004
1746 #define REMOTE_DST 0x0008
1747 #define MR_DST 0x0010
1748 #define NMR_DST 0x0020
1749 #define SND_RESP 0x0040
1751 /* The following to defines are used for return codes */
1752 #define REPEAT_SEND 0x1000
1753 #define PASS_THROUGH 0x2000
1755 /* The different cases lnet_select pathway needs to handle */
1756 #define SRC_SPEC_LOCAL_MR_DST (SRC_SPEC | LOCAL_DST | MR_DST)
1757 #define SRC_SPEC_ROUTER_MR_DST (SRC_SPEC | REMOTE_DST | MR_DST)
1758 #define SRC_SPEC_LOCAL_NMR_DST (SRC_SPEC | LOCAL_DST | NMR_DST)
1759 #define SRC_SPEC_ROUTER_NMR_DST (SRC_SPEC | REMOTE_DST | NMR_DST)
1760 #define SRC_ANY_LOCAL_MR_DST (SRC_ANY | LOCAL_DST | MR_DST)
1761 #define SRC_ANY_ROUTER_MR_DST (SRC_ANY | REMOTE_DST | MR_DST)
1762 #define SRC_ANY_LOCAL_NMR_DST (SRC_ANY | LOCAL_DST | NMR_DST)
1763 #define SRC_ANY_ROUTER_NMR_DST (SRC_ANY | REMOTE_DST | NMR_DST)
1766 lnet_handle_lo_send(struct lnet_send_data *sd)
1768 struct lnet_msg *msg = sd->sd_msg;
1769 int cpt = sd->sd_cpt;
1771 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1774 /* No send credit hassles with LOLND */
1775 lnet_ni_addref_locked(the_lnet.ln_loni, cpt);
1776 msg->msg_hdr.dest_nid = cpu_to_le64(the_lnet.ln_loni->ni_nid);
1777 if (!msg->msg_routing)
1778 msg->msg_hdr.src_nid =
1779 cpu_to_le64(the_lnet.ln_loni->ni_nid);
1780 msg->msg_target.nid = the_lnet.ln_loni->ni_nid;
1781 lnet_msg_commit(msg, cpt);
1782 msg->msg_txni = the_lnet.ln_loni;
1784 return LNET_CREDIT_OK;
1788 lnet_handle_send(struct lnet_send_data *sd)
1790 struct lnet_ni *best_ni = sd->sd_best_ni;
1791 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
1792 struct lnet_peer_ni *final_dst_lpni = sd->sd_final_dst_lpni;
1793 struct lnet_msg *msg = sd->sd_msg;
1795 __u32 send_case = sd->sd_send_case;
1797 __u32 routing = send_case & REMOTE_DST;
1798 struct lnet_rsp_tracker *rspt;
1800 /* Increment sequence number of the selected peer, peer net,
1801 * local ni and local net so that we pick the next ones
1804 best_lpni->lpni_seq++;
1805 best_lpni->lpni_peer_net->lpn_seq++;
1807 best_ni->ni_net->net_seq++;
1809 CDEBUG(D_NET, "%s NI seq info: [%d:%d:%d:%u] %s LPNI seq info [%d:%d:%d:%u]\n",
1810 libcfs_nid2str(best_ni->ni_nid),
1811 best_ni->ni_seq, best_ni->ni_net->net_seq,
1812 atomic_read(&best_ni->ni_tx_credits),
1813 best_ni->ni_sel_priority,
1814 libcfs_nid2str(best_lpni->lpni_nid),
1815 best_lpni->lpni_seq, best_lpni->lpni_peer_net->lpn_seq,
1816 best_lpni->lpni_txcredits,
1817 best_lpni->lpni_sel_priority);
1820 * grab a reference on the peer_ni so it sticks around even if
1821 * we need to drop and relock the lnet_net_lock below.
1823 lnet_peer_ni_addref_locked(best_lpni);
1826 * Use lnet_cpt_of_nid() to determine the CPT used to commit the
1827 * message. This ensures that we get a CPT that is correct for
1828 * the NI when the NI has been restricted to a subset of all CPTs.
1829 * If the selected CPT differs from the one currently locked, we
1830 * must unlock and relock the lnet_net_lock(), and then check whether
1831 * the configuration has changed. We don't have a hold on the best_ni
1832 * yet, and it may have vanished.
1834 cpt2 = lnet_cpt_of_nid_locked(best_lpni->lpni_nid, best_ni);
1835 if (sd->sd_cpt != cpt2) {
1836 __u32 seq = lnet_get_dlc_seq_locked();
1837 lnet_net_unlock(sd->sd_cpt);
1839 lnet_net_lock(sd->sd_cpt);
1840 if (seq != lnet_get_dlc_seq_locked()) {
1841 lnet_peer_ni_decref_locked(best_lpni);
1847 * store the best_lpni in the message right away to avoid having
1848 * to do the same operation under different conditions
1850 msg->msg_txpeer = best_lpni;
1851 msg->msg_txni = best_ni;
1854 * grab a reference for the best_ni since now it's in use in this
1855 * send. The reference will be dropped in lnet_finalize()
1857 lnet_ni_addref_locked(msg->msg_txni, sd->sd_cpt);
1860 * Always set the target.nid to the best peer picked. Either the
1861 * NID will be one of the peer NIDs selected, or the same NID as
1862 * what was originally set in the target or it will be the NID of
1863 * a router if this message should be routed
1865 msg->msg_target.nid = msg->msg_txpeer->lpni_nid;
1868 * lnet_msg_commit assigns the correct cpt to the message, which
1869 * is used to decrement the correct refcount on the ni when it's
1870 * time to return the credits
1872 lnet_msg_commit(msg, sd->sd_cpt);
1875 * If we are routing the message then we keep the src_nid that was
1876 * set by the originator. If we are not routing then we are the
1877 * originator and set it here.
1879 if (!msg->msg_routing)
1880 msg->msg_hdr.src_nid = cpu_to_le64(msg->msg_txni->ni_nid);
1883 msg->msg_target_is_router = 1;
1884 msg->msg_target.pid = LNET_PID_LUSTRE;
1886 * since we're routing we want to ensure that the
1887 * msg_hdr.dest_nid is set to the final destination. When
1888 * the router receives this message it knows how to route
1891 * final_dst_lpni is set at the beginning of the
1892 * lnet_select_pathway() function and is never changed.
1893 * It's safe to use it here.
1895 msg->msg_hdr.dest_nid = cpu_to_le64(final_dst_lpni->lpni_nid);
1898 * if we're not routing set the dest_nid to the best peer
1899 * ni NID that we picked earlier in the algorithm.
1901 msg->msg_hdr.dest_nid = cpu_to_le64(msg->msg_txpeer->lpni_nid);
1905 * if we have response tracker block update it with the next hop
1909 rspt = msg->msg_md->md_rspt_ptr;
1911 rspt->rspt_next_hop_nid = msg->msg_txpeer->lpni_nid;
1912 CDEBUG(D_NET, "rspt_next_hop_nid = %s\n",
1913 libcfs_nid2str(rspt->rspt_next_hop_nid));
1917 rc = lnet_post_send_locked(msg, 0);
1920 CDEBUG(D_NET, "TRACE: %s(%s:%s) -> %s(%s:%s) %s : %s try# %d\n",
1921 libcfs_nid2str(msg->msg_hdr.src_nid),
1922 libcfs_nid2str(msg->msg_txni->ni_nid),
1923 libcfs_nid2str(sd->sd_src_nid),
1924 libcfs_nid2str(msg->msg_hdr.dest_nid),
1925 libcfs_nid2str(sd->sd_dst_nid),
1926 libcfs_nid2str(msg->msg_txpeer->lpni_nid),
1927 libcfs_nid2str(sd->sd_rtr_nid),
1928 lnet_msgtyp2str(msg->msg_type), msg->msg_retry_count);
1934 lnet_set_non_mr_pref_nid(struct lnet_peer_ni *lpni, struct lnet_ni *lni,
1935 struct lnet_msg *msg)
1937 if (!lnet_peer_is_multi_rail(lpni->lpni_peer_net->lpn_peer) &&
1938 !lnet_msg_is_response(msg) && lpni->lpni_pref_nnids == 0) {
1939 CDEBUG(D_NET, "Setting preferred local NID %s on NMR peer %s\n",
1940 libcfs_nid2str(lni->ni_nid),
1941 libcfs_nid2str(lpni->lpni_nid));
1942 lnet_peer_ni_set_non_mr_pref_nid(lpni, lni->ni_nid);
1951 * use the source and destination NIDs as the pathway
1954 lnet_handle_spec_local_nmr_dst(struct lnet_send_data *sd)
1956 /* the destination lpni is set before we get here. */
1959 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
1960 if (!sd->sd_best_ni) {
1961 CERROR("Can't send to %s: src %s is not a "
1962 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
1963 libcfs_nid2str(sd->sd_src_nid));
1967 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
1969 return lnet_handle_send(sd);
1977 * Don't run the selection algorithm on the peer NIs. By specifying the
1978 * local NID, we're also saying that we should always use the destination NID
1979 * provided. This handles the case where we should be using the same
1980 * destination NID for the all the messages which belong to the same RPC
1984 lnet_handle_spec_local_mr_dst(struct lnet_send_data *sd)
1986 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
1987 if (!sd->sd_best_ni) {
1988 CERROR("Can't send to %s: src %s is not a "
1989 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
1990 libcfs_nid2str(sd->sd_src_nid));
1994 if (sd->sd_best_lpni &&
1995 sd->sd_best_lpni->lpni_nid == the_lnet.ln_loni->ni_nid)
1996 return lnet_handle_lo_send(sd);
1997 else if (sd->sd_best_lpni)
1998 return lnet_handle_send(sd);
2000 CERROR("can't send to %s. no NI on %s\n",
2001 libcfs_nid2str(sd->sd_dst_nid),
2002 libcfs_net2str(sd->sd_best_ni->ni_net->net_id));
2004 return -EHOSTUNREACH;
2008 lnet_find_best_ni_on_spec_net(struct lnet_ni *cur_best_ni,
2009 struct lnet_peer *peer,
2010 struct lnet_peer_net *peer_net,
2013 struct lnet_net *local_net;
2014 struct lnet_ni *best_ni;
2016 local_net = lnet_get_net_locked(peer_net->lpn_net_id);
2021 * Iterate through the NIs in this local Net and select
2022 * the NI to send from. The selection is determined by
2023 * these 3 criterion in the following priority:
2025 * 2. NI available credits
2028 best_ni = lnet_get_best_ni(local_net, cur_best_ni,
2029 peer, peer_net, cpt);
2035 lnet_initiate_peer_discovery(struct lnet_peer_ni *lpni, struct lnet_msg *msg,
2038 struct lnet_peer *peer;
2039 struct lnet_peer_ni *new_lpni;
2042 lnet_peer_ni_addref_locked(lpni);
2044 peer = lpni->lpni_peer_net->lpn_peer;
2046 if (lnet_peer_gw_discovery(peer)) {
2047 lnet_peer_ni_decref_locked(lpni);
2051 if (!lnet_msg_discovery(msg) || lnet_peer_is_uptodate(peer)) {
2052 lnet_peer_ni_decref_locked(lpni);
2056 rc = lnet_discover_peer_locked(lpni, cpt, false);
2058 lnet_peer_ni_decref_locked(lpni);
2062 new_lpni = lnet_find_peer_ni_locked(lpni->lpni_nid);
2064 lnet_peer_ni_decref_locked(lpni);
2068 peer = new_lpni->lpni_peer_net->lpn_peer;
2069 spin_lock(&peer->lp_lock);
2070 if (lpni == new_lpni && lnet_peer_is_uptodate_locked(peer)) {
2071 /* The peer NI did not change and the peer is up to date.
2072 * Nothing more to do.
2074 spin_unlock(&peer->lp_lock);
2075 lnet_peer_ni_decref_locked(lpni);
2076 lnet_peer_ni_decref_locked(new_lpni);
2079 spin_unlock(&peer->lp_lock);
2081 /* Either the peer NI changed during discovery, or the peer isn't up
2082 * to date. In both cases we want to queue the message on the
2083 * (possibly new) peer's pending queue and queue the peer for discovery
2085 msg->msg_sending = 0;
2086 msg->msg_txpeer = NULL;
2087 lnet_net_unlock(cpt);
2088 lnet_peer_queue_message(peer, msg);
2091 lnet_peer_ni_decref_locked(lpni);
2092 lnet_peer_ni_decref_locked(new_lpni);
2094 CDEBUG(D_NET, "msg %p delayed. %s pending discovery\n",
2095 msg, libcfs_nid2str(peer->lp_primary_nid));
2097 return LNET_DC_WAIT;
2101 lnet_handle_find_routed_path(struct lnet_send_data *sd,
2103 struct lnet_peer_ni **gw_lpni,
2104 struct lnet_peer **gw_peer)
2108 struct lnet_peer *gw;
2109 struct lnet_peer *lp;
2110 struct lnet_peer_net *lpn;
2111 struct lnet_peer_net *best_lpn = NULL;
2112 struct lnet_remotenet *rnet, *best_rnet = NULL;
2113 struct lnet_route *best_route = NULL;
2114 struct lnet_route *last_route = NULL;
2115 struct lnet_peer_ni *lpni = NULL;
2116 struct lnet_peer_ni *gwni = NULL;
2117 bool route_found = false;
2118 lnet_nid_t src_nid = (sd->sd_src_nid != LNET_NID_ANY) ? sd->sd_src_nid :
2119 (sd->sd_best_ni != NULL) ? sd->sd_best_ni->ni_nid :
2121 int best_lpn_healthv = 0;
2122 __u32 best_lpn_sel_prio = LNET_MAX_SELECTION_PRIORITY;
2124 CDEBUG(D_NET, "using src nid %s for route restriction\n",
2125 libcfs_nid2str(src_nid));
2127 /* If a router nid was specified then we are replying to a GET or
2128 * sending an ACK. In this case we use the gateway associated with the
2129 * specified router nid.
2131 if (sd->sd_rtr_nid != LNET_NID_ANY) {
2132 gwni = lnet_find_peer_ni_locked(sd->sd_rtr_nid);
2134 gw = gwni->lpni_peer_net->lpn_peer;
2135 lnet_peer_ni_decref_locked(gwni);
2136 if (gw->lp_rtr_refcount) {
2137 local_lnet = LNET_NIDNET(sd->sd_rtr_nid);
2141 CWARN("No peer NI for gateway %s. Attempting to find an alternative route.\n",
2142 libcfs_nid2str(sd->sd_rtr_nid));
2147 if (sd->sd_msg->msg_routing) {
2148 /* If I'm routing this message then I need to find the
2149 * next hop based on the destination NID
2151 best_rnet = lnet_find_rnet_locked(LNET_NIDNET(sd->sd_dst_nid));
2153 CERROR("Unable to route message to %s - Route table may be misconfigured\n",
2154 libcfs_nid2str(sd->sd_dst_nid));
2155 return -EHOSTUNREACH;
2158 /* we've already looked up the initial lpni using
2161 lpni = sd->sd_best_lpni;
2162 /* the peer tree must be in existence */
2163 LASSERT(lpni && lpni->lpni_peer_net &&
2164 lpni->lpni_peer_net->lpn_peer);
2165 lp = lpni->lpni_peer_net->lpn_peer;
2167 list_for_each_entry(lpn, &lp->lp_peer_nets, lpn_peer_nets) {
2168 /* is this remote network reachable? */
2169 rnet = lnet_find_rnet_locked(lpn->lpn_net_id);
2178 /* select the preferred peer net */
2179 if (best_lpn_healthv > lpn->lpn_healthv)
2181 else if (best_lpn_healthv < lpn->lpn_healthv)
2184 if (best_lpn_sel_prio < lpn->lpn_sel_priority)
2186 else if (best_lpn_sel_prio > lpn->lpn_sel_priority)
2189 if (best_lpn->lpn_seq <= lpn->lpn_seq)
2192 best_lpn_healthv = lpn->lpn_healthv;
2193 best_lpn_sel_prio = lpn->lpn_sel_priority;
2199 CERROR("peer %s has no available nets\n",
2200 libcfs_nid2str(sd->sd_dst_nid));
2201 return -EHOSTUNREACH;
2204 sd->sd_best_lpni = lnet_find_best_lpni(sd->sd_best_ni,
2207 best_lpn->lpn_net_id);
2208 if (!sd->sd_best_lpni) {
2209 CERROR("peer %s is unreachable\n",
2210 libcfs_nid2str(sd->sd_dst_nid));
2211 return -EHOSTUNREACH;
2214 /* We're attempting to round robin over the remote peer
2215 * NI's so update the final destination we selected
2217 sd->sd_final_dst_lpni = sd->sd_best_lpni;
2219 /* Increment the sequence number of the remote lpni so
2220 * we can round robin over the different interfaces of
2223 sd->sd_best_lpni->lpni_seq++;
2227 * find the best route. Restrict the selection on the net of the
2228 * local NI if we've already picked the local NI to send from.
2229 * Otherwise, let's pick any route we can find and then find
2230 * a local NI we can reach the route's gateway on. Any route we
2231 * select will be reachable by virtue of the restriction we have
2232 * when adding a route.
2234 best_route = lnet_find_route_locked(best_rnet,
2235 LNET_NIDNET(src_nid),
2237 &last_route, &gwni);
2240 CERROR("no route to %s from %s\n",
2241 libcfs_nid2str(dst_nid),
2242 libcfs_nid2str(src_nid));
2243 return -EHOSTUNREACH;
2247 CERROR("Internal Error. Route expected to %s from %s\n",
2248 libcfs_nid2str(dst_nid),
2249 libcfs_nid2str(src_nid));
2253 gw = best_route->lr_gateway;
2254 LASSERT(gw == gwni->lpni_peer_net->lpn_peer);
2255 local_lnet = best_route->lr_lnet;
2259 * Discover this gateway if it hasn't already been discovered.
2260 * This means we might delay the message until discovery has
2263 sd->sd_msg->msg_src_nid_param = sd->sd_src_nid;
2264 rc = lnet_initiate_peer_discovery(gwni, sd->sd_msg, sd->sd_cpt);
2268 if (!sd->sd_best_ni)
2269 sd->sd_best_ni = lnet_find_best_ni_on_spec_net(NULL, gw,
2270 lnet_peer_get_net_locked(gw,
2274 if (!sd->sd_best_ni) {
2275 CERROR("Internal Error. Expected local ni on %s but non found :%s\n",
2276 libcfs_net2str(local_lnet),
2277 libcfs_nid2str(sd->sd_src_nid));
2285 * increment the sequence numbers since now we're sure we're
2286 * going to use this path
2288 if (sd->sd_rtr_nid == LNET_NID_ANY) {
2289 LASSERT(best_route && last_route);
2290 best_route->lr_seq = last_route->lr_seq + 1;
2292 best_lpn->lpn_seq++;
2303 * Remote destination
2304 * Non-MR destination
2308 * Remote destination
2311 * The handling of these two cases is similar. Even though the destination
2312 * can be MR or non-MR, we'll deal directly with the router.
2315 lnet_handle_spec_router_dst(struct lnet_send_data *sd)
2318 struct lnet_peer_ni *gw_lpni = NULL;
2319 struct lnet_peer *gw_peer = NULL;
2322 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
2323 if (!sd->sd_best_ni) {
2324 CERROR("Can't send to %s: src %s is not a "
2325 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
2326 libcfs_nid2str(sd->sd_src_nid));
2330 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2335 if (sd->sd_send_case & NMR_DST)
2337 * since the final destination is non-MR let's set its preferred
2338 * NID before we send
2340 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni,
2344 * We're going to send to the gw found so let's set its
2347 sd->sd_peer = gw_peer;
2348 sd->sd_best_lpni = gw_lpni;
2350 return lnet_handle_send(sd);
2354 lnet_find_best_ni_on_local_net(struct lnet_peer *peer, int md_cpt,
2357 struct lnet_peer_net *lpn = NULL;
2358 struct lnet_peer_net *best_lpn = NULL;
2359 struct lnet_net *net = NULL;
2360 struct lnet_net *best_net = NULL;
2361 struct lnet_ni *best_ni = NULL;
2362 int best_lpn_healthv = 0;
2363 int best_net_healthv = 0;
2365 __u32 best_lpn_sel_prio = LNET_MAX_SELECTION_PRIORITY;
2367 __u32 best_net_sel_prio = LNET_MAX_SELECTION_PRIORITY;
2372 * The peer can have multiple interfaces, some of them can be on
2373 * the local network and others on a routed network. We should
2374 * prefer the local network. However if the local network is not
2375 * available then we need to try the routed network
2378 /* go through all the peer nets and find the best_ni */
2379 list_for_each_entry(lpn, &peer->lp_peer_nets, lpn_peer_nets) {
2381 * The peer's list of nets can contain non-local nets. We
2382 * want to only examine the local ones.
2384 net = lnet_get_net_locked(lpn->lpn_net_id);
2388 lpn_sel_prio = lpn->lpn_sel_priority;
2389 net_healthv = lnet_get_net_healthv_locked(net);
2390 net_sel_prio = net->net_sel_priority;
2393 * if this is a discovery message and lp_disc_net_id is
2394 * specified then use that net to send the discovery on.
2396 if (peer->lp_disc_net_id == lpn->lpn_net_id &&
2405 /* always select the lpn with the best health */
2406 if (best_lpn_healthv > lpn->lpn_healthv)
2408 else if (best_lpn_healthv < lpn->lpn_healthv)
2411 /* select the preferred peer and local nets */
2412 if (best_lpn_sel_prio < lpn_sel_prio)
2414 else if (best_lpn_sel_prio > lpn_sel_prio)
2417 if (best_net_healthv > net_healthv)
2419 else if (best_net_healthv < net_healthv)
2422 if (best_net_sel_prio < net_sel_prio)
2424 else if (best_net_sel_prio > net_sel_prio)
2427 if (best_lpn->lpn_seq < lpn->lpn_seq)
2429 else if (best_lpn->lpn_seq > lpn->lpn_seq)
2432 /* round robin over the local networks */
2433 if (best_net->net_seq <= net->net_seq)
2437 best_net_healthv = net_healthv;
2438 best_net_sel_prio = net_sel_prio;
2439 best_lpn_healthv = lpn->lpn_healthv;
2440 best_lpn_sel_prio = lpn_sel_prio;
2449 /* Select the best NI on the same net as best_lpn chosen
2452 best_ni = lnet_find_best_ni_on_spec_net(NULL, peer,
2459 static struct lnet_ni *
2460 lnet_find_existing_preferred_best_ni(struct lnet_peer_ni *lpni, int cpt)
2462 struct lnet_ni *best_ni = NULL;
2463 struct lnet_peer_net *peer_net = lpni->lpni_peer_net;
2464 struct lnet_peer_ni *lpni_entry;
2467 * We must use a consistent source address when sending to a
2468 * non-MR peer. However, a non-MR peer can have multiple NIDs
2469 * on multiple networks, and we may even need to talk to this
2470 * peer on multiple networks -- certain types of
2471 * load-balancing configuration do this.
2473 * So we need to pick the NI the peer prefers for this
2474 * particular network.
2477 list_for_each_entry(lpni_entry, &peer_net->lpn_peer_nis,
2479 if (lpni_entry->lpni_pref_nnids == 0)
2481 LASSERT(lpni_entry->lpni_pref_nnids == 1);
2482 best_ni = lnet_nid2ni_locked(lpni_entry->lpni_pref.nid, cpt);
2489 /* Prerequisite: sd->sd_peer and sd->sd_best_lpni should be set */
2491 lnet_select_preferred_best_ni(struct lnet_send_data *sd)
2493 struct lnet_ni *best_ni = NULL;
2494 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
2497 * We must use a consistent source address when sending to a
2498 * non-MR peer. However, a non-MR peer can have multiple NIDs
2499 * on multiple networks, and we may even need to talk to this
2500 * peer on multiple networks -- certain types of
2501 * load-balancing configuration do this.
2503 * So we need to pick the NI the peer prefers for this
2504 * particular network.
2507 best_ni = lnet_find_existing_preferred_best_ni(sd->sd_best_lpni,
2510 /* if best_ni is still not set just pick one */
2513 lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2514 sd->sd_best_lpni->lpni_peer_net,
2516 /* If there is no best_ni we don't have a route */
2518 CERROR("no path to %s from net %s\n",
2519 libcfs_nid2str(best_lpni->lpni_nid),
2520 libcfs_net2str(best_lpni->lpni_net->net_id));
2521 return -EHOSTUNREACH;
2525 sd->sd_best_ni = best_ni;
2527 /* Set preferred NI if necessary. */
2528 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
2535 * Source not specified
2539 * always use the same source NID for NMR peers
2540 * If we've talked to that peer before then we already have a preferred
2541 * source NI associated with it. Otherwise, we select a preferred local NI
2542 * and store it in the peer
2545 lnet_handle_any_local_nmr_dst(struct lnet_send_data *sd)
2549 /* sd->sd_best_lpni is already set to the final destination */
2552 * At this point we should've created the peer ni and peer. If we
2553 * can't find it, then something went wrong. Instead of assert
2554 * output a relevant message and fail the send
2556 if (!sd->sd_best_lpni) {
2557 CERROR("Internal fault. Unable to send msg %s to %s. "
2559 lnet_msgtyp2str(sd->sd_msg->msg_type),
2560 libcfs_nid2str(sd->sd_dst_nid));
2564 if (sd->sd_msg->msg_routing) {
2565 /* If I'm forwarding this message then I can choose any NI
2566 * on the destination peer net
2568 sd->sd_best_ni = lnet_find_best_ni_on_spec_net(NULL,
2570 sd->sd_best_lpni->lpni_peer_net,
2572 if (!sd->sd_best_ni) {
2573 CERROR("Unable to forward message to %s. No local NI available\n",
2574 libcfs_nid2str(sd->sd_dst_nid));
2578 rc = lnet_select_preferred_best_ni(sd);
2581 rc = lnet_handle_send(sd);
2587 lnet_handle_any_mr_dsta(struct lnet_send_data *sd)
2590 * NOTE we've already handled the remote peer case. So we only
2591 * need to worry about the local case here.
2593 * if we're sending a response, ACK or reply, we need to send it
2594 * to the destination NID given to us. At this point we already
2595 * have the peer_ni we're suppose to send to, so just find the
2596 * best_ni on the peer net and use that. Since we're sending to an
2597 * MR peer then we can just run the selection algorithm on our
2598 * local NIs and pick the best one.
2600 if (sd->sd_send_case & SND_RESP) {
2602 lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2603 sd->sd_best_lpni->lpni_peer_net,
2606 if (!sd->sd_best_ni) {
2608 * We're not going to deal with not able to send
2609 * a response to the provided final destination
2611 CERROR("Can't send response to %s. "
2612 "No local NI available\n",
2613 libcfs_nid2str(sd->sd_dst_nid));
2614 return -EHOSTUNREACH;
2617 return lnet_handle_send(sd);
2621 * If we get here that means we're sending a fresh request, PUT or
2622 * GET, so we need to run our standard selection algorithm.
2623 * First find the best local interface that's on any of the peer's
2626 sd->sd_best_ni = lnet_find_best_ni_on_local_net(sd->sd_peer,
2628 lnet_msg_discovery(sd->sd_msg));
2629 if (sd->sd_best_ni) {
2631 lnet_find_best_lpni(sd->sd_best_ni, sd->sd_dst_nid,
2633 sd->sd_best_ni->ni_net->net_id);
2636 * if we're successful in selecting a peer_ni on the local
2637 * network, then send to it. Otherwise fall through and
2638 * try and see if we can reach it over another routed
2641 if (sd->sd_best_lpni &&
2642 sd->sd_best_lpni->lpni_nid == the_lnet.ln_loni->ni_nid) {
2644 * in case we initially started with a routed
2645 * destination, let's reset to local
2647 sd->sd_send_case &= ~REMOTE_DST;
2648 sd->sd_send_case |= LOCAL_DST;
2649 return lnet_handle_lo_send(sd);
2650 } else if (sd->sd_best_lpni) {
2652 * in case we initially started with a routed
2653 * destination, let's reset to local
2655 sd->sd_send_case &= ~REMOTE_DST;
2656 sd->sd_send_case |= LOCAL_DST;
2657 return lnet_handle_send(sd);
2660 CERROR("Internal Error. Expected to have a best_lpni: "
2662 libcfs_nid2str(sd->sd_src_nid),
2663 libcfs_nid2str(sd->sd_dst_nid));
2669 * Peer doesn't have a local network. Let's see if there is
2670 * a remote network we can reach it on.
2672 return PASS_THROUGH;
2677 * Source NID not specified
2682 * Source NID not speified
2683 * Remote destination
2686 * In both of these cases if we're sending a response, ACK or REPLY, then
2687 * we need to send to the destination NID provided.
2689 * In the remote case let's deal with MR routers.
2694 lnet_handle_any_mr_dst(struct lnet_send_data *sd)
2697 struct lnet_peer *gw_peer = NULL;
2698 struct lnet_peer_ni *gw_lpni = NULL;
2701 * handle sending a response to a remote peer here so we don't
2702 * have to worry about it if we hit lnet_handle_any_mr_dsta()
2704 if (sd->sd_send_case & REMOTE_DST &&
2705 sd->sd_send_case & SND_RESP) {
2706 struct lnet_peer_ni *gw;
2707 struct lnet_peer *gw_peer;
2709 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw,
2712 CERROR("Can't send response to %s. "
2713 "No route available\n",
2714 libcfs_nid2str(sd->sd_dst_nid));
2715 return -EHOSTUNREACH;
2716 } else if (rc > 0) {
2720 sd->sd_best_lpni = gw;
2721 sd->sd_peer = gw_peer;
2723 return lnet_handle_send(sd);
2727 * Even though the NID for the peer might not be on a local network,
2728 * since the peer is MR there could be other interfaces on the
2729 * local network. In that case we'd still like to prefer the local
2730 * network over the routed network. If we're unable to do that
2731 * then we select the best router among the different routed networks,
2732 * and if the router is MR then we can deal with it as such.
2734 rc = lnet_handle_any_mr_dsta(sd);
2735 if (rc != PASS_THROUGH)
2739 * Now that we must route to the destination, we must consider the
2740 * MR case, where the destination has multiple interfaces, some of
2741 * which we can route to and others we do not. For this reason we
2742 * need to select the destination which we can route to and if
2743 * there are multiple, we need to round robin.
2745 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2750 sd->sd_send_case &= ~LOCAL_DST;
2751 sd->sd_send_case |= REMOTE_DST;
2753 sd->sd_peer = gw_peer;
2754 sd->sd_best_lpni = gw_lpni;
2756 return lnet_handle_send(sd);
2760 * Source not specified
2761 * Remote destination
2764 * Must send to the specified peer NID using the same source NID that
2765 * we've used before. If it's the first time to talk to that peer then
2766 * find the source NI and assign it as preferred to that peer
2769 lnet_handle_any_router_nmr_dst(struct lnet_send_data *sd)
2772 struct lnet_peer_ni *gw_lpni = NULL;
2773 struct lnet_peer *gw_peer = NULL;
2776 * Let's see if we have a preferred NI to talk to this NMR peer
2778 sd->sd_best_ni = lnet_find_existing_preferred_best_ni(sd->sd_best_lpni,
2782 * find the router and that'll find the best NI if we didn't find
2785 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2791 * set the best_ni we've chosen as the preferred one for
2794 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
2796 /* we'll be sending to the gw */
2797 sd->sd_best_lpni = gw_lpni;
2798 sd->sd_peer = gw_peer;
2800 return lnet_handle_send(sd);
2804 lnet_handle_send_case_locked(struct lnet_send_data *sd)
2807 * turn off the SND_RESP bit.
2808 * It will be checked in the case handling
2810 __u32 send_case = sd->sd_send_case &= ~SND_RESP ;
2812 CDEBUG(D_NET, "Source %s%s to %s %s %s destination\n",
2813 (send_case & SRC_SPEC) ? "Specified: " : "ANY",
2814 (send_case & SRC_SPEC) ? libcfs_nid2str(sd->sd_src_nid) : "",
2815 (send_case & MR_DST) ? "MR: " : "NMR: ",
2816 libcfs_nid2str(sd->sd_dst_nid),
2817 (send_case & LOCAL_DST) ? "local" : "routed");
2819 switch (send_case) {
2821 * For all cases where the source is specified, we should always
2822 * use the destination NID, whether it's an MR destination or not,
2823 * since we're continuing a series of related messages for the
2826 case SRC_SPEC_LOCAL_NMR_DST:
2827 return lnet_handle_spec_local_nmr_dst(sd);
2828 case SRC_SPEC_LOCAL_MR_DST:
2829 return lnet_handle_spec_local_mr_dst(sd);
2830 case SRC_SPEC_ROUTER_NMR_DST:
2831 case SRC_SPEC_ROUTER_MR_DST:
2832 return lnet_handle_spec_router_dst(sd);
2833 case SRC_ANY_LOCAL_NMR_DST:
2834 return lnet_handle_any_local_nmr_dst(sd);
2835 case SRC_ANY_LOCAL_MR_DST:
2836 case SRC_ANY_ROUTER_MR_DST:
2837 return lnet_handle_any_mr_dst(sd);
2838 case SRC_ANY_ROUTER_NMR_DST:
2839 return lnet_handle_any_router_nmr_dst(sd);
2841 CERROR("Unknown send case\n");
2847 lnet_select_pathway(lnet_nid_t src_nid, lnet_nid_t dst_nid,
2848 struct lnet_msg *msg, lnet_nid_t rtr_nid)
2850 struct lnet_peer_ni *lpni;
2851 struct lnet_peer *peer;
2852 struct lnet_send_data send_data;
2855 __u32 send_case = 0;
2857 bool mr_forwarding_allowed;
2859 memset(&send_data, 0, sizeof(send_data));
2862 * get an initial CPT to use for locking. The idea here is not to
2863 * serialize the calls to select_pathway, so that as many
2864 * operations can run concurrently as possible. To do that we use
2865 * the CPT where this call is being executed. Later on when we
2866 * determine the CPT to use in lnet_message_commit, we switch the
2867 * lock and check if there was any configuration change. If none,
2868 * then we proceed, if there is, then we restart the operation.
2870 cpt = lnet_net_lock_current();
2872 md_cpt = lnet_cpt_of_md(msg->msg_md, msg->msg_offset);
2873 if (md_cpt == CFS_CPT_ANY)
2879 * If we're being asked to send to the loopback interface, there
2880 * is no need to go through any selection. We can just shortcut
2881 * the entire process and send over lolnd
2883 send_data.sd_msg = msg;
2884 send_data.sd_cpt = cpt;
2885 if (dst_nid == LNET_NID_LO_0) {
2886 rc = lnet_handle_lo_send(&send_data);
2887 lnet_net_unlock(cpt);
2892 * find an existing peer_ni, or create one and mark it as having been
2893 * created due to network traffic. This call will create the
2894 * peer->peer_net->peer_ni tree.
2896 lpni = lnet_nid2peerni_locked(dst_nid, LNET_NID_ANY, cpt);
2898 lnet_net_unlock(cpt);
2899 return PTR_ERR(lpni);
2903 * Cache the original src_nid and rtr_nid. If we need to resend the
2904 * message then we'll need to know whether the src_nid was originally
2905 * specified for this message. If it was originally specified,
2906 * then we need to keep using the same src_nid since it's
2907 * continuing the same sequence of messages. Similarly, rtr_nid will
2908 * affect our choice of next hop.
2910 msg->msg_src_nid_param = src_nid;
2911 msg->msg_rtr_nid_param = rtr_nid;
2914 * If necessary, perform discovery on the peer that owns this peer_ni.
2915 * Note, this can result in the ownership of this peer_ni changing
2916 * to another peer object.
2918 rc = lnet_initiate_peer_discovery(lpni, msg, cpt);
2920 lnet_peer_ni_decref_locked(lpni);
2921 lnet_net_unlock(cpt);
2924 lnet_peer_ni_decref_locked(lpni);
2926 peer = lpni->lpni_peer_net->lpn_peer;
2929 * Identify the different send cases
2931 if (src_nid == LNET_NID_ANY)
2932 send_case |= SRC_ANY;
2934 send_case |= SRC_SPEC;
2936 if (lnet_get_net_locked(LNET_NIDNET(dst_nid)))
2937 send_case |= LOCAL_DST;
2939 send_case |= REMOTE_DST;
2942 if (msg->msg_routing && (send_case & LOCAL_DST))
2945 /* Determine whether to allow MR forwarding for this message.
2946 * NB: MR forwarding is allowed if the message originator and the
2947 * destination are both MR capable, and the destination lpni that was
2948 * originally chosen by the originator is unhealthy or down.
2949 * We check the MR capability of the destination further below
2951 mr_forwarding_allowed = false;
2953 struct lnet_peer *src_lp;
2954 struct lnet_peer_ni *src_lpni;
2956 src_lpni = lnet_nid2peerni_locked(msg->msg_hdr.src_nid,
2958 /* We don't fail the send if we hit any errors here. We'll just
2959 * try to send it via non-multi-rail criteria
2961 if (!IS_ERR(src_lpni)) {
2962 /* Drop ref taken by lnet_nid2peerni_locked() */
2963 lnet_peer_ni_decref_locked(src_lpni);
2964 src_lp = lpni->lpni_peer_net->lpn_peer;
2965 if (lnet_peer_is_multi_rail(src_lp) &&
2966 !lnet_is_peer_ni_alive(lpni))
2967 mr_forwarding_allowed = true;
2970 CDEBUG(D_NET, "msg %p MR forwarding %s\n", msg,
2971 mr_forwarding_allowed ? "allowed" : "not allowed");
2975 * Deal with the peer as NMR in the following cases:
2976 * 1. the peer is NMR
2977 * 2. We're trying to recover a specific peer NI
2978 * 3. I'm a router sending to the final destination and MR forwarding is
2979 * not allowed for this message (as determined above).
2980 * In this case the source of the message would've
2981 * already selected the final destination so my job
2982 * is to honor the selection.
2984 if (!lnet_peer_is_multi_rail(peer) || msg->msg_recovery ||
2985 (final_hop && !mr_forwarding_allowed))
2986 send_case |= NMR_DST;
2988 send_case |= MR_DST;
2990 if (lnet_msg_is_response(msg))
2991 send_case |= SND_RESP;
2993 /* assign parameters to the send_data */
2994 send_data.sd_rtr_nid = rtr_nid;
2995 send_data.sd_src_nid = src_nid;
2996 send_data.sd_dst_nid = dst_nid;
2997 send_data.sd_best_lpni = lpni;
2999 * keep a pointer to the final destination in case we're going to
3000 * route, so we'll need to access it later
3002 send_data.sd_final_dst_lpni = lpni;
3003 send_data.sd_peer = peer;
3004 send_data.sd_md_cpt = md_cpt;
3005 send_data.sd_send_case = send_case;
3007 rc = lnet_handle_send_case_locked(&send_data);
3010 * Update the local cpt since send_data.sd_cpt might've been
3011 * updated as a result of calling lnet_handle_send_case_locked().
3013 cpt = send_data.sd_cpt;
3015 if (rc == REPEAT_SEND)
3018 lnet_net_unlock(cpt);
3024 lnet_send(lnet_nid_t src_nid, struct lnet_msg *msg, lnet_nid_t rtr_nid)
3026 lnet_nid_t dst_nid = msg->msg_target.nid;
3030 * NB: rtr_nid is set to LNET_NID_ANY for all current use-cases,
3031 * but we might want to use pre-determined router for ACK/REPLY
3034 /* NB: ni != NULL == interface pre-determined (ACK/REPLY) */
3035 LASSERT(msg->msg_txpeer == NULL);
3036 LASSERT(msg->msg_txni == NULL);
3037 LASSERT(!msg->msg_sending);
3038 LASSERT(!msg->msg_target_is_router);
3039 LASSERT(!msg->msg_receiving);
3041 msg->msg_sending = 1;
3043 LASSERT(!msg->msg_tx_committed);
3045 rc = lnet_select_pathway(src_nid, dst_nid, msg, rtr_nid);
3047 if (rc == -EHOSTUNREACH)
3048 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_ERROR;
3050 msg->msg_health_status = LNET_MSG_STATUS_LOCAL_ERROR;
3054 if (rc == LNET_CREDIT_OK)
3055 lnet_ni_send(msg->msg_txni, msg);
3057 /* rc == LNET_CREDIT_OK or LNET_CREDIT_WAIT or LNET_DC_WAIT */
3061 enum lnet_mt_event_type {
3062 MT_TYPE_LOCAL_NI = 0,
3066 struct lnet_mt_event_info {
3067 enum lnet_mt_event_type mt_type;
3071 /* called with res_lock held */
3073 lnet_detach_rsp_tracker(struct lnet_libmd *md, int cpt)
3075 struct lnet_rsp_tracker *rspt;
3078 * msg has a refcount on the MD so the MD is not going away.
3079 * The rspt queue for the cpt is protected by
3080 * the lnet_net_lock(cpt). cpt is the cpt of the MD cookie.
3082 if (!md->md_rspt_ptr)
3085 rspt = md->md_rspt_ptr;
3088 LASSERT(rspt->rspt_cpt == cpt);
3090 md->md_rspt_ptr = NULL;
3092 if (LNetMDHandleIsInvalid(rspt->rspt_mdh)) {
3094 * The monitor thread has invalidated this handle because the
3095 * response timed out, but it failed to lookup the MD. That
3096 * means this response tracker is on the zombie list. We can
3097 * safely remove it under the resource lock (held by caller) and
3098 * free the response tracker block.
3100 list_del(&rspt->rspt_on_list);
3101 lnet_rspt_free(rspt, cpt);
3104 * invalidate the handle to indicate that a response has been
3105 * received, which will then lead the monitor thread to clean up
3108 LNetInvalidateMDHandle(&rspt->rspt_mdh);
3113 lnet_clean_zombie_rstqs(void)
3115 struct lnet_rsp_tracker *rspt, *tmp;
3118 cfs_cpt_for_each(i, lnet_cpt_table()) {
3119 list_for_each_entry_safe(rspt, tmp,
3120 the_lnet.ln_mt_zombie_rstqs[i],
3122 list_del(&rspt->rspt_on_list);
3123 lnet_rspt_free(rspt, i);
3127 cfs_percpt_free(the_lnet.ln_mt_zombie_rstqs);
3131 lnet_finalize_expired_responses(void)
3133 struct lnet_libmd *md;
3134 struct lnet_rsp_tracker *rspt, *tmp;
3138 if (the_lnet.ln_mt_rstq == NULL)
3141 cfs_cpt_for_each(i, lnet_cpt_table()) {
3142 LIST_HEAD(local_queue);
3145 if (!the_lnet.ln_mt_rstq[i]) {
3149 list_splice_init(the_lnet.ln_mt_rstq[i], &local_queue);
3154 list_for_each_entry_safe(rspt, tmp, &local_queue, rspt_on_list) {
3156 * The rspt mdh will be invalidated when a response
3157 * is received or whenever we want to discard the
3158 * block the monitor thread will walk the queue
3159 * and clean up any rsts with an invalid mdh.
3160 * The monitor thread will walk the queue until
3161 * the first unexpired rspt block. This means that
3162 * some rspt blocks which received their
3163 * corresponding responses will linger in the
3164 * queue until they are cleaned up eventually.
3167 if (LNetMDHandleIsInvalid(rspt->rspt_mdh)) {
3169 list_del(&rspt->rspt_on_list);
3170 lnet_rspt_free(rspt, i);
3174 if (ktime_compare(now, rspt->rspt_deadline) >= 0 ||
3175 the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN) {
3176 struct lnet_peer_ni *lpni;
3179 md = lnet_handle2md(&rspt->rspt_mdh);
3181 /* MD has been queued for unlink, but
3182 * rspt hasn't been detached (Note we've
3183 * checked above that the rspt_mdh is
3184 * valid). Since we cannot lookup the MD
3185 * we're unable to detach the rspt
3186 * ourselves. Thus, move the rspt to the
3187 * zombie list where we'll wait for
3189 * 1. The remaining operations on the
3190 * MD to complete. In this case the
3191 * final operation will result in
3192 * lnet_msg_detach_md()->
3193 * lnet_detach_rsp_tracker() where
3194 * we will clean up this response
3196 * 2. LNet to shutdown. In this case
3197 * we'll wait until after all LND Nets
3198 * have shutdown and then we can
3199 * safely free any remaining response
3200 * tracker blocks on the zombie list.
3201 * Note: We need to hold the resource
3202 * lock when adding to the zombie list
3203 * because we may have concurrent access
3204 * with lnet_detach_rsp_tracker().
3206 LNetInvalidateMDHandle(&rspt->rspt_mdh);
3207 list_move(&rspt->rspt_on_list,
3208 the_lnet.ln_mt_zombie_rstqs[i]);
3212 LASSERT(md->md_rspt_ptr == rspt);
3213 md->md_rspt_ptr = NULL;
3216 LNetMDUnlink(rspt->rspt_mdh);
3218 nid = rspt->rspt_next_hop_nid;
3220 list_del(&rspt->rspt_on_list);
3221 lnet_rspt_free(rspt, i);
3223 /* If we're shutting down we just want to clean
3224 * up the rspt blocks
3226 if (the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN)
3230 the_lnet.ln_counters[i]->lct_health.lch_response_timeout_count++;
3234 "Response timeout: md = %p: nid = %s\n",
3235 md, libcfs_nid2str(nid));
3238 * If there is a timeout on the response
3239 * from the next hop decrement its health
3240 * value so that we don't use it
3243 lpni = lnet_find_peer_ni_locked(nid);
3245 lnet_handle_remote_failure_locked(lpni);
3246 lnet_peer_ni_decref_locked(lpni);
3255 if (!list_empty(&local_queue)) {
3257 list_splice(&local_queue, the_lnet.ln_mt_rstq[i]);
3264 lnet_resend_pending_msgs_locked(struct list_head *resendq, int cpt)
3266 struct lnet_msg *msg;
3268 while (!list_empty(resendq)) {
3269 struct lnet_peer_ni *lpni;
3271 msg = list_entry(resendq->next, struct lnet_msg,
3274 list_del_init(&msg->msg_list);
3276 lpni = lnet_find_peer_ni_locked(msg->msg_hdr.dest_nid);
3278 lnet_net_unlock(cpt);
3279 CERROR("Expected that a peer is already created for %s\n",
3280 libcfs_nid2str(msg->msg_hdr.dest_nid));
3281 msg->msg_no_resend = true;
3282 lnet_finalize(msg, -EFAULT);
3287 lnet_peer_ni_decref_locked(lpni);
3289 lnet_net_unlock(cpt);
3290 CDEBUG(D_NET, "resending %s->%s: %s recovery %d try# %d\n",
3291 libcfs_nid2str(msg->msg_src_nid_param),
3292 libcfs_id2str(msg->msg_target),
3293 lnet_msgtyp2str(msg->msg_type),
3295 msg->msg_retry_count);
3296 rc = lnet_send(msg->msg_src_nid_param, msg,
3297 msg->msg_rtr_nid_param);
3299 CERROR("Error sending %s to %s: %d\n",
3300 lnet_msgtyp2str(msg->msg_type),
3301 libcfs_id2str(msg->msg_target), rc);
3302 msg->msg_no_resend = true;
3303 lnet_finalize(msg, rc);
3307 the_lnet.ln_counters[cpt]->lct_health.lch_resend_count++;
3313 lnet_resend_pending_msgs(void)
3317 cfs_cpt_for_each(i, lnet_cpt_table()) {
3319 lnet_resend_pending_msgs_locked(the_lnet.ln_mt_resendqs[i], i);
3324 /* called with cpt and ni_lock held */
3326 lnet_unlink_ni_recovery_mdh_locked(struct lnet_ni *ni, int cpt, bool force)
3328 struct lnet_handle_md recovery_mdh;
3330 LNetInvalidateMDHandle(&recovery_mdh);
3332 if (ni->ni_recovery_state & LNET_NI_RECOVERY_PENDING ||
3334 recovery_mdh = ni->ni_ping_mdh;
3335 LNetInvalidateMDHandle(&ni->ni_ping_mdh);
3338 lnet_net_unlock(cpt);
3339 if (!LNetMDHandleIsInvalid(recovery_mdh))
3340 LNetMDUnlink(recovery_mdh);
3346 lnet_recover_local_nis(void)
3348 struct lnet_mt_event_info *ev_info;
3349 LIST_HEAD(processed_list);
3350 LIST_HEAD(local_queue);
3351 struct lnet_handle_md mdh;
3352 struct lnet_ni *tmp;
3360 * splice the recovery queue on a local queue. We will iterate
3361 * through the local queue and update it as needed. Once we're
3362 * done with the traversal, we'll splice the local queue back on
3363 * the head of the ln_mt_localNIRecovq. Any newly added local NIs
3364 * will be traversed in the next iteration.
3367 list_splice_init(&the_lnet.ln_mt_localNIRecovq,
3371 now = ktime_get_seconds();
3373 list_for_each_entry_safe(ni, tmp, &local_queue, ni_recovery) {
3375 * if an NI is being deleted or it is now healthy, there
3376 * is no need to keep it around in the recovery queue.
3377 * The monitor thread is the only thread responsible for
3378 * removing the NI from the recovery queue.
3379 * Multiple threads can be adding NIs to the recovery
3382 healthv = atomic_read(&ni->ni_healthv);
3386 if (ni->ni_state != LNET_NI_STATE_ACTIVE ||
3387 healthv == LNET_MAX_HEALTH_VALUE) {
3388 list_del_init(&ni->ni_recovery);
3389 lnet_unlink_ni_recovery_mdh_locked(ni, 0, false);
3391 lnet_ni_decref_locked(ni, 0);
3397 * if the local NI failed recovery we must unlink the md.
3398 * But we want to keep the local_ni on the recovery queue
3399 * so we can continue the attempts to recover it.
3401 if (ni->ni_recovery_state & LNET_NI_RECOVERY_FAILED) {
3402 lnet_unlink_ni_recovery_mdh_locked(ni, 0, true);
3403 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_FAILED;
3409 if (now < ni->ni_next_ping) {
3416 CDEBUG(D_NET, "attempting to recover local ni: %s\n",
3417 libcfs_nid2str(ni->ni_nid));
3420 if (!(ni->ni_recovery_state & LNET_NI_RECOVERY_PENDING)) {
3421 ni->ni_recovery_state |= LNET_NI_RECOVERY_PENDING;
3424 LIBCFS_ALLOC(ev_info, sizeof(*ev_info));
3426 CERROR("out of memory. Can't recover %s\n",
3427 libcfs_nid2str(ni->ni_nid));
3429 ni->ni_recovery_state &=
3430 ~LNET_NI_RECOVERY_PENDING;
3435 mdh = ni->ni_ping_mdh;
3437 * Invalidate the ni mdh in case it's deleted.
3438 * We'll unlink the mdh in this case below.
3440 LNetInvalidateMDHandle(&ni->ni_ping_mdh);
3444 * remove the NI from the local queue and drop the
3445 * reference count to it while we're recovering
3446 * it. The reason for that, is that the NI could
3447 * be deleted, and the way the code is structured
3448 * is if we don't drop the NI, then the deletion
3449 * code will enter a loop waiting for the
3450 * reference count to be removed while holding the
3451 * ln_mutex_lock(). When we look up the peer to
3452 * send to in lnet_select_pathway() we will try to
3453 * lock the ln_mutex_lock() as well, leading to
3454 * a deadlock. By dropping the refcount and
3455 * removing it from the list, we allow for the NI
3456 * to be removed, then we use the cached NID to
3457 * look it up again. If it's gone, then we just
3458 * continue examining the rest of the queue.
3461 list_del_init(&ni->ni_recovery);
3462 lnet_ni_decref_locked(ni, 0);
3465 ev_info->mt_type = MT_TYPE_LOCAL_NI;
3466 ev_info->mt_nid = nid;
3467 rc = lnet_send_ping(nid, &mdh, LNET_INTERFACES_MIN,
3468 ev_info, the_lnet.ln_mt_handler,
3470 /* lookup the nid again */
3472 ni = lnet_nid2ni_locked(nid, 0);
3475 * the NI has been deleted when we dropped
3482 ni->ni_ping_count++;
3484 ni->ni_ping_mdh = mdh;
3485 lnet_ni_add_to_recoveryq_locked(ni, &processed_list,
3490 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_PENDING;
3499 * put back the remaining NIs on the ln_mt_localNIRecovq to be
3500 * reexamined in the next iteration.
3502 list_splice_init(&processed_list, &local_queue);
3504 list_splice(&local_queue, &the_lnet.ln_mt_localNIRecovq);
3509 lnet_resendqs_create(void)
3511 struct list_head **resendqs;
3512 resendqs = lnet_create_array_of_queues();
3517 lnet_net_lock(LNET_LOCK_EX);
3518 the_lnet.ln_mt_resendqs = resendqs;
3519 lnet_net_unlock(LNET_LOCK_EX);
3525 lnet_clean_local_ni_recoveryq(void)
3529 /* This is only called when the monitor thread has stopped */
3532 while (!list_empty(&the_lnet.ln_mt_localNIRecovq)) {
3533 ni = list_entry(the_lnet.ln_mt_localNIRecovq.next,
3534 struct lnet_ni, ni_recovery);
3535 list_del_init(&ni->ni_recovery);
3537 lnet_unlink_ni_recovery_mdh_locked(ni, 0, true);
3539 lnet_ni_decref_locked(ni, 0);
3546 lnet_unlink_lpni_recovery_mdh_locked(struct lnet_peer_ni *lpni, int cpt,
3549 struct lnet_handle_md recovery_mdh;
3551 LNetInvalidateMDHandle(&recovery_mdh);
3553 if (lpni->lpni_state & LNET_PEER_NI_RECOVERY_PENDING || force) {
3554 recovery_mdh = lpni->lpni_recovery_ping_mdh;
3555 LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
3557 spin_unlock(&lpni->lpni_lock);
3558 lnet_net_unlock(cpt);
3559 if (!LNetMDHandleIsInvalid(recovery_mdh))
3560 LNetMDUnlink(recovery_mdh);
3562 spin_lock(&lpni->lpni_lock);
3566 lnet_clean_peer_ni_recoveryq(void)
3568 struct lnet_peer_ni *lpni, *tmp;
3570 lnet_net_lock(LNET_LOCK_EX);
3572 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_mt_peerNIRecovq,
3574 list_del_init(&lpni->lpni_recovery);
3575 spin_lock(&lpni->lpni_lock);
3576 lnet_unlink_lpni_recovery_mdh_locked(lpni, LNET_LOCK_EX, true);
3577 spin_unlock(&lpni->lpni_lock);
3578 lnet_peer_ni_decref_locked(lpni);
3581 lnet_net_unlock(LNET_LOCK_EX);
3585 lnet_clean_resendqs(void)
3587 struct lnet_msg *msg, *tmp;
3591 cfs_cpt_for_each(i, lnet_cpt_table()) {
3593 list_splice_init(the_lnet.ln_mt_resendqs[i], &msgs);
3595 list_for_each_entry_safe(msg, tmp, &msgs, msg_list) {
3596 list_del_init(&msg->msg_list);
3597 msg->msg_no_resend = true;
3598 lnet_finalize(msg, -ESHUTDOWN);
3602 cfs_percpt_free(the_lnet.ln_mt_resendqs);
3606 lnet_recover_peer_nis(void)
3608 struct lnet_mt_event_info *ev_info;
3609 LIST_HEAD(processed_list);
3610 LIST_HEAD(local_queue);
3611 struct lnet_handle_md mdh;
3612 struct lnet_peer_ni *lpni;
3613 struct lnet_peer_ni *tmp;
3620 * Always use cpt 0 for locking across all interactions with
3621 * ln_mt_peerNIRecovq
3624 list_splice_init(&the_lnet.ln_mt_peerNIRecovq,
3628 now = ktime_get_seconds();
3630 list_for_each_entry_safe(lpni, tmp, &local_queue,
3633 * The same protection strategy is used here as is in the
3634 * local recovery case.
3637 healthv = atomic_read(&lpni->lpni_healthv);
3638 spin_lock(&lpni->lpni_lock);
3639 if (lpni->lpni_state & LNET_PEER_NI_DELETING ||
3640 healthv == LNET_MAX_HEALTH_VALUE) {
3641 list_del_init(&lpni->lpni_recovery);
3642 lnet_unlink_lpni_recovery_mdh_locked(lpni, 0, false);
3643 spin_unlock(&lpni->lpni_lock);
3644 lnet_peer_ni_decref_locked(lpni);
3650 * If the peer NI has failed recovery we must unlink the
3651 * md. But we want to keep the peer ni on the recovery
3652 * queue so we can try to continue recovering it
3654 if (lpni->lpni_state & LNET_PEER_NI_RECOVERY_FAILED) {
3655 lnet_unlink_lpni_recovery_mdh_locked(lpni, 0, true);
3656 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_FAILED;
3659 spin_unlock(&lpni->lpni_lock);
3661 if (now < lpni->lpni_next_ping) {
3669 * NOTE: we're racing with peer deletion from user space.
3670 * It's possible that a peer is deleted after we check its
3671 * state. In this case the recovery can create a new peer
3673 spin_lock(&lpni->lpni_lock);
3674 if (!(lpni->lpni_state & LNET_PEER_NI_RECOVERY_PENDING) &&
3675 !(lpni->lpni_state & LNET_PEER_NI_DELETING)) {
3676 lpni->lpni_state |= LNET_PEER_NI_RECOVERY_PENDING;
3677 spin_unlock(&lpni->lpni_lock);
3679 LIBCFS_ALLOC(ev_info, sizeof(*ev_info));
3681 CERROR("out of memory. Can't recover %s\n",
3682 libcfs_nid2str(lpni->lpni_nid));
3683 spin_lock(&lpni->lpni_lock);
3684 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3685 spin_unlock(&lpni->lpni_lock);
3689 /* look at the comments in lnet_recover_local_nis() */
3690 mdh = lpni->lpni_recovery_ping_mdh;
3691 LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
3692 nid = lpni->lpni_nid;
3694 list_del_init(&lpni->lpni_recovery);
3695 lnet_peer_ni_decref_locked(lpni);
3698 ev_info->mt_type = MT_TYPE_PEER_NI;
3699 ev_info->mt_nid = nid;
3700 rc = lnet_send_ping(nid, &mdh, LNET_INTERFACES_MIN,
3701 ev_info, the_lnet.ln_mt_handler,
3705 * lnet_find_peer_ni_locked() grabs a refcount for
3706 * us. No need to take it explicitly.
3708 lpni = lnet_find_peer_ni_locked(nid);
3715 lpni->lpni_ping_count++;
3717 lpni->lpni_recovery_ping_mdh = mdh;
3719 lnet_peer_ni_add_to_recoveryq_locked(lpni,
3723 spin_lock(&lpni->lpni_lock);
3724 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3725 spin_unlock(&lpni->lpni_lock);
3728 /* Drop the ref taken by lnet_find_peer_ni_locked() */
3729 lnet_peer_ni_decref_locked(lpni);
3732 spin_unlock(&lpni->lpni_lock);
3735 list_splice_init(&processed_list, &local_queue);
3737 list_splice(&local_queue, &the_lnet.ln_mt_peerNIRecovq);
3742 lnet_monitor_thread(void *arg)
3744 time64_t rsp_timeout = 0;
3747 wait_for_completion(&the_lnet.ln_started);
3749 * The monitor thread takes care of the following:
3750 * 1. Checks the aliveness of routers
3751 * 2. Checks if there are messages on the resend queue to resend
3753 * 3. Check if there are any NIs on the local recovery queue and
3755 * 4. Checks if there are any NIs on the remote recovery queue
3758 while (the_lnet.ln_mt_state == LNET_MT_STATE_RUNNING) {
3759 now = ktime_get_real_seconds();
3761 if (lnet_router_checker_active())
3762 lnet_check_routers();
3764 lnet_resend_pending_msgs();
3766 if (now >= rsp_timeout) {
3767 lnet_finalize_expired_responses();
3768 rsp_timeout = now + (lnet_transaction_timeout / 2);
3771 lnet_recover_local_nis();
3772 lnet_recover_peer_nis();
3775 * TODO do we need to check if we should sleep without
3776 * timeout? Technically, an active system will always
3777 * have messages in flight so this check will always
3778 * evaluate to false. And on an idle system do we care
3779 * if we wake up every 1 second? Although, we've seen
3780 * cases where we get a complaint that an idle thread
3781 * is waking up unnecessarily.
3783 wait_for_completion_interruptible_timeout(
3784 &the_lnet.ln_mt_wait_complete,
3785 cfs_time_seconds(1));
3786 /* Must re-init the completion before testing anything,
3787 * including ln_mt_state.
3789 reinit_completion(&the_lnet.ln_mt_wait_complete);
3793 lnet_net_lock(LNET_LOCK_EX);
3794 the_lnet.ln_mt_state = LNET_MT_STATE_SHUTDOWN;
3795 lnet_net_unlock(LNET_LOCK_EX);
3797 /* signal that the monitor thread is exiting */
3798 up(&the_lnet.ln_mt_signal);
3806 * Returns == 0 if success
3807 * Returns > 0 if LNetMDBind or prior fails
3808 * Returns < 0 if LNetGet fails
3811 lnet_send_ping(lnet_nid_t dest_nid,
3812 struct lnet_handle_md *mdh, int nnis,
3813 void *user_data, lnet_handler_t handler, bool recovery)
3815 struct lnet_md md = { NULL };
3816 struct lnet_process_id id;
3817 struct lnet_ping_buffer *pbuf;
3820 if (dest_nid == LNET_NID_ANY) {
3825 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
3831 /* initialize md content */
3832 md.start = &pbuf->pb_info;
3833 md.length = LNET_PING_INFO_SIZE(nnis);
3834 md.threshold = 2; /* GET/REPLY */
3836 md.options = LNET_MD_TRUNCATE | LNET_MD_TRACK_RESPONSE;
3837 md.user_ptr = user_data;
3838 md.handler = handler;
3840 rc = LNetMDBind(&md, LNET_UNLINK, mdh);
3842 lnet_ping_buffer_decref(pbuf);
3843 CERROR("Can't bind MD: %d\n", rc);
3844 rc = -rc; /* change the rc to positive */
3847 id.pid = LNET_PID_LUSTRE;
3850 rc = LNetGet(LNET_NID_ANY, *mdh, id,
3851 LNET_RESERVED_PORTAL,
3852 LNET_PROTO_PING_MATCHBITS, 0, recovery);
3855 goto fail_unlink_md;
3861 LNetInvalidateMDHandle(mdh);
3867 lnet_handle_recovery_reply(struct lnet_mt_event_info *ev_info,
3868 int status, bool send, bool unlink_event)
3870 lnet_nid_t nid = ev_info->mt_nid;
3872 if (ev_info->mt_type == MT_TYPE_LOCAL_NI) {
3876 ni = lnet_nid2ni_locked(nid, 0);
3882 if (!send || (send && status != 0))
3883 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_PENDING;
3885 ni->ni_recovery_state |= LNET_NI_RECOVERY_FAILED;
3890 CERROR("local NI (%s) recovery failed with %d\n",
3891 libcfs_nid2str(nid), status);
3895 * need to increment healthv for the ni here, because in
3896 * the lnet_finalize() path we don't have access to this
3897 * NI. And in order to get access to it, we'll need to
3898 * carry forward too much information.
3899 * In the peer case, it'll naturally be incremented
3902 lnet_inc_healthv(&ni->ni_healthv,
3903 lnet_health_sensitivity);
3905 struct lnet_peer_ni *lpni;
3908 cpt = lnet_net_lock_current();
3909 lpni = lnet_find_peer_ni_locked(nid);
3911 lnet_net_unlock(cpt);
3914 spin_lock(&lpni->lpni_lock);
3915 if (!send || (send && status != 0))
3916 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3918 lpni->lpni_state |= LNET_PEER_NI_RECOVERY_FAILED;
3919 spin_unlock(&lpni->lpni_lock);
3920 lnet_peer_ni_decref_locked(lpni);
3921 lnet_net_unlock(cpt);
3924 CERROR("peer NI (%s) recovery failed with %d\n",
3925 libcfs_nid2str(nid), status);
3930 lnet_mt_event_handler(struct lnet_event *event)
3932 struct lnet_mt_event_info *ev_info = event->md_user_ptr;
3933 struct lnet_ping_buffer *pbuf;
3935 /* TODO: remove assert */
3936 LASSERT(event->type == LNET_EVENT_REPLY ||
3937 event->type == LNET_EVENT_SEND ||
3938 event->type == LNET_EVENT_UNLINK);
3940 CDEBUG(D_NET, "Received event: %d status: %d\n", event->type,
3943 switch (event->type) {
3944 case LNET_EVENT_UNLINK:
3945 CDEBUG(D_NET, "%s recovery ping unlinked\n",
3946 libcfs_nid2str(ev_info->mt_nid));
3948 case LNET_EVENT_REPLY:
3949 lnet_handle_recovery_reply(ev_info, event->status, false,
3950 event->type == LNET_EVENT_UNLINK);
3952 case LNET_EVENT_SEND:
3953 CDEBUG(D_NET, "%s recovery message sent %s:%d\n",
3954 libcfs_nid2str(ev_info->mt_nid),
3955 (event->status) ? "unsuccessfully" :
3956 "successfully", event->status);
3957 lnet_handle_recovery_reply(ev_info, event->status, true, false);
3960 CERROR("Unexpected event: %d\n", event->type);
3963 if (event->unlinked) {
3964 LIBCFS_FREE(ev_info, sizeof(*ev_info));
3965 pbuf = LNET_PING_INFO_TO_BUFFER(event->md_start);
3966 lnet_ping_buffer_decref(pbuf);
3971 lnet_rsp_tracker_create(void)
3973 struct list_head **rstqs;
3974 rstqs = lnet_create_array_of_queues();
3979 the_lnet.ln_mt_rstq = rstqs;
3985 lnet_rsp_tracker_clean(void)
3987 lnet_finalize_expired_responses();
3989 cfs_percpt_free(the_lnet.ln_mt_rstq);
3990 the_lnet.ln_mt_rstq = NULL;
3993 int lnet_monitor_thr_start(void)
3996 struct task_struct *task;
3998 if (the_lnet.ln_mt_state != LNET_MT_STATE_SHUTDOWN)
4001 rc = lnet_resendqs_create();
4005 rc = lnet_rsp_tracker_create();
4009 sema_init(&the_lnet.ln_mt_signal, 0);
4011 lnet_net_lock(LNET_LOCK_EX);
4012 the_lnet.ln_mt_state = LNET_MT_STATE_RUNNING;
4013 lnet_net_unlock(LNET_LOCK_EX);
4014 task = kthread_run(lnet_monitor_thread, NULL, "monitor_thread");
4017 CERROR("Can't start monitor thread: %d\n", rc);
4024 lnet_net_lock(LNET_LOCK_EX);
4025 the_lnet.ln_mt_state = LNET_MT_STATE_STOPPING;
4026 lnet_net_unlock(LNET_LOCK_EX);
4027 /* block until event callback signals exit */
4028 down(&the_lnet.ln_mt_signal);
4030 lnet_net_lock(LNET_LOCK_EX);
4031 the_lnet.ln_mt_state = LNET_MT_STATE_SHUTDOWN;
4032 lnet_net_unlock(LNET_LOCK_EX);
4033 lnet_rsp_tracker_clean();
4034 lnet_clean_local_ni_recoveryq();
4035 lnet_clean_peer_ni_recoveryq();
4036 lnet_clean_resendqs();
4037 the_lnet.ln_mt_handler = NULL;
4040 lnet_rsp_tracker_clean();
4041 lnet_clean_local_ni_recoveryq();
4042 lnet_clean_peer_ni_recoveryq();
4043 lnet_clean_resendqs();
4047 void lnet_monitor_thr_stop(void)
4049 if (the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN)
4052 LASSERT(the_lnet.ln_mt_state == LNET_MT_STATE_RUNNING);
4053 lnet_net_lock(LNET_LOCK_EX);
4054 the_lnet.ln_mt_state = LNET_MT_STATE_STOPPING;
4055 lnet_net_unlock(LNET_LOCK_EX);
4057 /* tell the monitor thread that we're shutting down */
4058 complete(&the_lnet.ln_mt_wait_complete);
4060 /* block until monitor thread signals that it's done */
4061 down(&the_lnet.ln_mt_signal);
4062 LASSERT(the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN);
4064 /* perform cleanup tasks */
4065 lnet_rsp_tracker_clean();
4066 lnet_clean_local_ni_recoveryq();
4067 lnet_clean_peer_ni_recoveryq();
4068 lnet_clean_resendqs();
4072 lnet_drop_message(struct lnet_ni *ni, int cpt, void *private, unsigned int nob,
4076 lnet_incr_stats(&ni->ni_stats, msg_type, LNET_STATS_TYPE_DROP);
4077 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
4078 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length += nob;
4079 lnet_net_unlock(cpt);
4081 lnet_ni_recv(ni, private, NULL, 0, 0, 0, nob);
4085 lnet_recv_put(struct lnet_ni *ni, struct lnet_msg *msg)
4087 struct lnet_hdr *hdr = &msg->msg_hdr;
4089 if (msg->msg_wanted != 0)
4090 lnet_setpayloadbuffer(msg);
4092 lnet_build_msg_event(msg, LNET_EVENT_PUT);
4094 /* Must I ACK? If so I'll grab the ack_wmd out of the header and put
4095 * it back into the ACK during lnet_finalize() */
4096 msg->msg_ack = (!lnet_is_wire_handle_none(&hdr->msg.put.ack_wmd) &&
4097 (msg->msg_md->md_options & LNET_MD_ACK_DISABLE) == 0);
4099 lnet_ni_recv(ni, msg->msg_private, msg, msg->msg_rx_delayed,
4100 msg->msg_offset, msg->msg_wanted, hdr->payload_length);
4104 lnet_parse_put(struct lnet_ni *ni, struct lnet_msg *msg)
4106 struct lnet_hdr *hdr = &msg->msg_hdr;
4107 struct lnet_match_info info;
4111 /* Convert put fields to host byte order */
4112 hdr->msg.put.match_bits = le64_to_cpu(hdr->msg.put.match_bits);
4113 hdr->msg.put.ptl_index = le32_to_cpu(hdr->msg.put.ptl_index);
4114 hdr->msg.put.offset = le32_to_cpu(hdr->msg.put.offset);
4116 /* Primary peer NID. */
4117 info.mi_id.nid = msg->msg_initiator;
4118 info.mi_id.pid = hdr->src_pid;
4119 info.mi_opc = LNET_MD_OP_PUT;
4120 info.mi_portal = hdr->msg.put.ptl_index;
4121 info.mi_rlength = hdr->payload_length;
4122 info.mi_roffset = hdr->msg.put.offset;
4123 info.mi_mbits = hdr->msg.put.match_bits;
4124 info.mi_cpt = lnet_cpt_of_nid(msg->msg_initiator, ni);
4126 msg->msg_rx_ready_delay = ni->ni_net->net_lnd->lnd_eager_recv == NULL;
4127 ready_delay = msg->msg_rx_ready_delay;
4130 rc = lnet_ptl_match_md(&info, msg);
4135 case LNET_MATCHMD_OK:
4136 lnet_recv_put(ni, msg);
4139 case LNET_MATCHMD_NONE:
4141 /* no eager_recv or has already called it, should
4142 * have been attached on delayed list */
4145 rc = lnet_ni_eager_recv(ni, msg);
4152 case LNET_MATCHMD_DROP:
4153 CNETERR("Dropping PUT from %s portal %d match %llu"
4154 " offset %d length %d: %d\n",
4155 libcfs_id2str(info.mi_id), info.mi_portal,
4156 info.mi_mbits, info.mi_roffset, info.mi_rlength, rc);
4158 return -ENOENT; /* -ve: OK but no match */
4163 lnet_parse_get(struct lnet_ni *ni, struct lnet_msg *msg, int rdma_get)
4165 struct lnet_match_info info;
4166 struct lnet_hdr *hdr = &msg->msg_hdr;
4167 struct lnet_process_id source_id;
4168 struct lnet_handle_wire reply_wmd;
4171 /* Convert get fields to host byte order */
4172 hdr->msg.get.match_bits = le64_to_cpu(hdr->msg.get.match_bits);
4173 hdr->msg.get.ptl_index = le32_to_cpu(hdr->msg.get.ptl_index);
4174 hdr->msg.get.sink_length = le32_to_cpu(hdr->msg.get.sink_length);
4175 hdr->msg.get.src_offset = le32_to_cpu(hdr->msg.get.src_offset);
4177 source_id.nid = hdr->src_nid;
4178 source_id.pid = hdr->src_pid;
4179 /* Primary peer NID */
4180 info.mi_id.nid = msg->msg_initiator;
4181 info.mi_id.pid = hdr->src_pid;
4182 info.mi_opc = LNET_MD_OP_GET;
4183 info.mi_portal = hdr->msg.get.ptl_index;
4184 info.mi_rlength = hdr->msg.get.sink_length;
4185 info.mi_roffset = hdr->msg.get.src_offset;
4186 info.mi_mbits = hdr->msg.get.match_bits;
4187 info.mi_cpt = lnet_cpt_of_nid(msg->msg_initiator, ni);
4189 rc = lnet_ptl_match_md(&info, msg);
4190 if (rc == LNET_MATCHMD_DROP) {
4191 CNETERR("Dropping GET from %s portal %d match %llu"
4192 " offset %d length %d\n",
4193 libcfs_id2str(info.mi_id), info.mi_portal,
4194 info.mi_mbits, info.mi_roffset, info.mi_rlength);
4195 return -ENOENT; /* -ve: OK but no match */
4198 LASSERT(rc == LNET_MATCHMD_OK);
4200 lnet_build_msg_event(msg, LNET_EVENT_GET);
4202 reply_wmd = hdr->msg.get.return_wmd;
4204 lnet_prep_send(msg, LNET_MSG_REPLY, source_id,
4205 msg->msg_offset, msg->msg_wanted);
4207 msg->msg_hdr.msg.reply.dst_wmd = reply_wmd;
4210 /* The LND completes the REPLY from her recv procedure */
4211 lnet_ni_recv(ni, msg->msg_private, msg, 0,
4212 msg->msg_offset, msg->msg_len, msg->msg_len);
4216 lnet_ni_recv(ni, msg->msg_private, NULL, 0, 0, 0, 0);
4217 msg->msg_receiving = 0;
4219 rc = lnet_send(ni->ni_nid, msg, msg->msg_from);
4221 /* didn't get as far as lnet_ni_send() */
4222 CERROR("%s: Unable to send REPLY for GET from %s: %d\n",
4223 libcfs_nid2str(ni->ni_nid),
4224 libcfs_id2str(info.mi_id), rc);
4226 lnet_finalize(msg, rc);
4233 lnet_parse_reply(struct lnet_ni *ni, struct lnet_msg *msg)
4235 void *private = msg->msg_private;
4236 struct lnet_hdr *hdr = &msg->msg_hdr;
4237 struct lnet_process_id src = {0};
4238 struct lnet_libmd *md;
4239 unsigned int rlength;
4240 unsigned int mlength;
4243 cpt = lnet_cpt_of_cookie(hdr->msg.reply.dst_wmd.wh_object_cookie);
4246 src.nid = hdr->src_nid;
4247 src.pid = hdr->src_pid;
4249 /* NB handles only looked up by creator (no flips) */
4250 md = lnet_wire_handle2md(&hdr->msg.reply.dst_wmd);
4251 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
4252 CNETERR("%s: Dropping REPLY from %s for %s "
4254 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
4255 (md == NULL) ? "invalid" : "inactive",
4256 hdr->msg.reply.dst_wmd.wh_interface_cookie,
4257 hdr->msg.reply.dst_wmd.wh_object_cookie);
4258 if (md != NULL && md->md_me != NULL)
4259 CERROR("REPLY MD also attached to portal %d\n",
4260 md->md_me->me_portal);
4262 lnet_res_unlock(cpt);
4263 return -ENOENT; /* -ve: OK but no match */
4266 LASSERT(md->md_offset == 0);
4268 rlength = hdr->payload_length;
4269 mlength = min(rlength, md->md_length);
4271 if (mlength < rlength &&
4272 (md->md_options & LNET_MD_TRUNCATE) == 0) {
4273 CNETERR("%s: Dropping REPLY from %s length %d "
4274 "for MD %#llx would overflow (%d)\n",
4275 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
4276 rlength, hdr->msg.reply.dst_wmd.wh_object_cookie,
4278 lnet_res_unlock(cpt);
4279 return -ENOENT; /* -ve: OK but no match */
4282 CDEBUG(D_NET, "%s: Reply from %s of length %d/%d into md %#llx\n",
4283 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
4284 mlength, rlength, hdr->msg.reply.dst_wmd.wh_object_cookie);
4286 lnet_msg_attach_md(msg, md, 0, mlength);
4289 lnet_setpayloadbuffer(msg);
4291 lnet_res_unlock(cpt);
4293 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
4295 lnet_ni_recv(ni, private, msg, 0, 0, mlength, rlength);
4300 lnet_parse_ack(struct lnet_ni *ni, struct lnet_msg *msg)
4302 struct lnet_hdr *hdr = &msg->msg_hdr;
4303 struct lnet_process_id src = {0};
4304 struct lnet_libmd *md;
4307 src.nid = hdr->src_nid;
4308 src.pid = hdr->src_pid;
4310 /* Convert ack fields to host byte order */
4311 hdr->msg.ack.match_bits = le64_to_cpu(hdr->msg.ack.match_bits);
4312 hdr->msg.ack.mlength = le32_to_cpu(hdr->msg.ack.mlength);
4314 cpt = lnet_cpt_of_cookie(hdr->msg.ack.dst_wmd.wh_object_cookie);
4317 /* NB handles only looked up by creator (no flips) */
4318 md = lnet_wire_handle2md(&hdr->msg.ack.dst_wmd);
4319 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
4320 /* Don't moan; this is expected */
4322 "%s: Dropping ACK from %s to %s MD %#llx.%#llx\n",
4323 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
4324 (md == NULL) ? "invalid" : "inactive",
4325 hdr->msg.ack.dst_wmd.wh_interface_cookie,
4326 hdr->msg.ack.dst_wmd.wh_object_cookie);
4327 if (md != NULL && md->md_me != NULL)
4328 CERROR("Source MD also attached to portal %d\n",
4329 md->md_me->me_portal);
4331 lnet_res_unlock(cpt);
4332 return -ENOENT; /* -ve! */
4335 CDEBUG(D_NET, "%s: ACK from %s into md %#llx\n",
4336 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
4337 hdr->msg.ack.dst_wmd.wh_object_cookie);
4339 lnet_msg_attach_md(msg, md, 0, 0);
4341 lnet_res_unlock(cpt);
4343 lnet_build_msg_event(msg, LNET_EVENT_ACK);
4345 lnet_ni_recv(ni, msg->msg_private, msg, 0, 0, 0, msg->msg_len);
4350 * \retval LNET_CREDIT_OK If \a msg is forwarded
4351 * \retval LNET_CREDIT_WAIT If \a msg is blocked because w/o buffer
4352 * \retval -ve error code
4355 lnet_parse_forward_locked(struct lnet_ni *ni, struct lnet_msg *msg)
4359 if (!the_lnet.ln_routing)
4362 if (msg->msg_rxpeer->lpni_rtrcredits <= 0 ||
4363 lnet_msg2bufpool(msg)->rbp_credits <= 0) {
4364 if (ni->ni_net->net_lnd->lnd_eager_recv == NULL) {
4365 msg->msg_rx_ready_delay = 1;
4367 lnet_net_unlock(msg->msg_rx_cpt);
4368 rc = lnet_ni_eager_recv(ni, msg);
4369 lnet_net_lock(msg->msg_rx_cpt);
4374 rc = lnet_post_routed_recv_locked(msg, 0);
4379 lnet_parse_local(struct lnet_ni *ni, struct lnet_msg *msg)
4383 switch (msg->msg_type) {
4385 rc = lnet_parse_ack(ni, msg);
4388 rc = lnet_parse_put(ni, msg);
4391 rc = lnet_parse_get(ni, msg, msg->msg_rdma_get);
4393 case LNET_MSG_REPLY:
4394 rc = lnet_parse_reply(ni, msg);
4396 default: /* prevent an unused label if !kernel */
4401 LASSERT(rc == 0 || rc == -ENOENT);
4406 lnet_msgtyp2str (int type)
4415 case LNET_MSG_REPLY:
4417 case LNET_MSG_HELLO:
4420 return ("<UNKNOWN>");
4425 lnet_parse(struct lnet_ni *ni, struct lnet_hdr *hdr, lnet_nid_t from_nid,
4426 void *private, int rdma_req)
4428 struct lnet_peer_ni *lpni;
4429 struct lnet_msg *msg;
4430 __u32 payload_length;
4431 lnet_pid_t dest_pid;
4432 lnet_nid_t dest_nid;
4440 LASSERT (!in_interrupt ());
4442 type = le32_to_cpu(hdr->type);
4443 src_nid = le64_to_cpu(hdr->src_nid);
4444 dest_nid = le64_to_cpu(hdr->dest_nid);
4445 dest_pid = le32_to_cpu(hdr->dest_pid);
4446 payload_length = le32_to_cpu(hdr->payload_length);
4448 for_me = (ni->ni_nid == dest_nid);
4449 cpt = lnet_cpt_of_nid(from_nid, ni);
4451 CDEBUG(D_NET, "TRACE: %s(%s) <- %s : %s - %s\n",
4452 libcfs_nid2str(dest_nid),
4453 libcfs_nid2str(ni->ni_nid),
4454 libcfs_nid2str(src_nid),
4455 lnet_msgtyp2str(type),
4456 (for_me) ? "for me" : "routed");
4461 if (payload_length > 0) {
4462 CERROR("%s, src %s: bad %s payload %d (0 expected)\n",
4463 libcfs_nid2str(from_nid),
4464 libcfs_nid2str(src_nid),
4465 lnet_msgtyp2str(type), payload_length);
4471 case LNET_MSG_REPLY:
4472 if (payload_length >
4473 (__u32)(for_me ? LNET_MAX_PAYLOAD : LNET_MTU)) {
4474 CERROR("%s, src %s: bad %s payload %d "
4475 "(%d max expected)\n",
4476 libcfs_nid2str(from_nid),
4477 libcfs_nid2str(src_nid),
4478 lnet_msgtyp2str(type),
4480 for_me ? LNET_MAX_PAYLOAD : LNET_MTU);
4486 CERROR("%s, src %s: Bad message type 0x%x\n",
4487 libcfs_nid2str(from_nid),
4488 libcfs_nid2str(src_nid), type);
4492 if (the_lnet.ln_routing &&
4493 ni->ni_net->net_last_alive != ktime_get_real_seconds()) {
4495 spin_lock(&ni->ni_net->net_lock);
4496 ni->ni_net->net_last_alive = ktime_get_real_seconds();
4497 spin_unlock(&ni->ni_net->net_lock);
4498 push = lnet_ni_set_status_locked(ni, LNET_NI_STATUS_UP);
4503 lnet_push_update_to_peers(1);
4505 /* Regard a bad destination NID as a protocol error. Senders should
4506 * know what they're doing; if they don't they're misconfigured, buggy
4507 * or malicious so we chop them off at the knees :) */
4510 if (LNET_NIDNET(dest_nid) == LNET_NIDNET(ni->ni_nid)) {
4511 /* should have gone direct */
4512 CERROR("%s, src %s: Bad dest nid %s "
4513 "(should have been sent direct)\n",
4514 libcfs_nid2str(from_nid),
4515 libcfs_nid2str(src_nid),
4516 libcfs_nid2str(dest_nid));
4520 if (lnet_islocalnid(dest_nid)) {
4521 /* dest is another local NI; sender should have used
4522 * this node's NID on its own network */
4523 CERROR("%s, src %s: Bad dest nid %s "
4524 "(it's my nid but on a different network)\n",
4525 libcfs_nid2str(from_nid),
4526 libcfs_nid2str(src_nid),
4527 libcfs_nid2str(dest_nid));
4531 if (rdma_req && type == LNET_MSG_GET) {
4532 CERROR("%s, src %s: Bad optimized GET for %s "
4533 "(final destination must be me)\n",
4534 libcfs_nid2str(from_nid),
4535 libcfs_nid2str(src_nid),
4536 libcfs_nid2str(dest_nid));
4540 if (!the_lnet.ln_routing) {
4541 CERROR("%s, src %s: Dropping message for %s "
4542 "(routing not enabled)\n",
4543 libcfs_nid2str(from_nid),
4544 libcfs_nid2str(src_nid),
4545 libcfs_nid2str(dest_nid));
4550 /* Message looks OK; we're not going to return an error, so we MUST
4551 * call back lnd_recv() come what may... */
4553 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
4554 fail_peer(src_nid, 0)) { /* shall we now? */
4555 CERROR("%s, src %s: Dropping %s to simulate failure\n",
4556 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4557 lnet_msgtyp2str(type));
4561 if (!list_empty(&the_lnet.ln_drop_rules) &&
4562 lnet_drop_rule_match(hdr, ni->ni_nid, NULL)) {
4564 "%s, src %s, dst %s: Dropping %s to simulate silent message loss\n",
4565 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4566 libcfs_nid2str(dest_nid), lnet_msgtyp2str(type));
4570 msg = lnet_msg_alloc();
4572 CERROR("%s, src %s: Dropping %s (out of memory)\n",
4573 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4574 lnet_msgtyp2str(type));
4578 /* msg zeroed in lnet_msg_alloc; i.e. flags all clear,
4579 * pointers NULL etc */
4581 msg->msg_type = type;
4582 msg->msg_private = private;
4583 msg->msg_receiving = 1;
4584 msg->msg_rdma_get = rdma_req;
4585 msg->msg_len = msg->msg_wanted = payload_length;
4586 msg->msg_offset = 0;
4587 msg->msg_hdr = *hdr;
4588 /* for building message event */
4589 msg->msg_from = from_nid;
4591 msg->msg_target.pid = dest_pid;
4592 msg->msg_target.nid = dest_nid;
4593 msg->msg_routing = 1;
4596 /* convert common msg->hdr fields to host byteorder */
4597 msg->msg_hdr.type = type;
4598 msg->msg_hdr.src_nid = src_nid;
4599 msg->msg_hdr.src_pid = le32_to_cpu(msg->msg_hdr.src_pid);
4600 msg->msg_hdr.dest_nid = dest_nid;
4601 msg->msg_hdr.dest_pid = dest_pid;
4602 msg->msg_hdr.payload_length = payload_length;
4606 lpni = lnet_nid2peerni_locked(from_nid, ni->ni_nid, cpt);
4608 lnet_net_unlock(cpt);
4609 CERROR("%s, src %s: Dropping %s "
4610 "(error %ld looking up sender)\n",
4611 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4612 lnet_msgtyp2str(type), PTR_ERR(lpni));
4614 if (rc == -ESHUTDOWN)
4615 /* We are shutting down. Don't do anything more */
4620 /* If this message was forwarded to us from a router then we may need
4621 * to update router aliveness or check for an asymmetrical route
4624 if (((lnet_drop_asym_route && for_me) ||
4625 !lpni->lpni_peer_net->lpn_peer->lp_alive) &&
4626 LNET_NIDNET(src_nid) != LNET_NIDNET(from_nid)) {
4627 __u32 src_net_id = LNET_NIDNET(src_nid);
4628 struct lnet_peer *gw = lpni->lpni_peer_net->lpn_peer;
4629 struct lnet_route *route;
4632 list_for_each_entry(route, &gw->lp_routes, lr_gwlist) {
4633 if (route->lr_net == src_net_id) {
4635 /* If we're transitioning the gateway from
4636 * dead -> alive, and discovery is disabled
4637 * locally or on the gateway, then we need to
4638 * update the cached route aliveness for each
4639 * route to the src_nid's net.
4641 * Otherwise, we're only checking for
4642 * symmetrical route, and we can break the
4645 if (!gw->lp_alive &&
4646 lnet_is_discovery_disabled(gw))
4647 lnet_set_route_aliveness(route, true);
4652 if (lnet_drop_asym_route && for_me && !found) {
4653 lnet_net_unlock(cpt);
4654 /* we would not use from_nid to route a message to
4656 * => asymmetric routing detected but forbidden
4658 CERROR("%s, src %s: Dropping asymmetrical route %s\n",
4659 libcfs_nid2str(from_nid),
4660 libcfs_nid2str(src_nid), lnet_msgtyp2str(type));
4664 if (!gw->lp_alive) {
4665 struct lnet_peer_net *lpn;
4666 struct lnet_peer_ni *lpni2;
4668 gw->lp_alive = true;
4669 /* Mark all remote NIs on src_nid's net UP */
4670 lpn = lnet_peer_get_net_locked(gw, src_net_id);
4672 list_for_each_entry(lpni2, &lpn->lpn_peer_nis,
4674 lpni2->lpni_ns_status = LNET_NI_STATUS_UP;
4678 lpni->lpni_last_alive = ktime_get_seconds();
4680 msg->msg_rxpeer = lpni;
4682 lnet_ni_addref_locked(ni, cpt);
4683 /* Multi-Rail: Primary NID of source. */
4684 msg->msg_initiator = lnet_peer_primary_nid_locked(src_nid);
4687 * mark the status of this lpni as UP since we received a message
4688 * from it. The ping response reports back the ns_status which is
4689 * marked on the remote as up or down and we cache it here.
4691 msg->msg_rxpeer->lpni_ns_status = LNET_NI_STATUS_UP;
4693 lnet_msg_commit(msg, cpt);
4695 /* message delay simulation */
4696 if (unlikely(!list_empty(&the_lnet.ln_delay_rules) &&
4697 lnet_delay_rule_match_locked(hdr, msg))) {
4698 lnet_net_unlock(cpt);
4703 rc = lnet_parse_forward_locked(ni, msg);
4704 lnet_net_unlock(cpt);
4709 if (rc == LNET_CREDIT_OK) {
4710 lnet_ni_recv(ni, msg->msg_private, msg, 0,
4711 0, payload_length, payload_length);
4716 lnet_net_unlock(cpt);
4718 rc = lnet_parse_local(ni, msg);
4724 LASSERT(msg->msg_md == NULL);
4725 lnet_finalize(msg, rc);
4728 lnet_drop_message(ni, cpt, private, payload_length, type);
4731 EXPORT_SYMBOL(lnet_parse);
4734 lnet_drop_delayed_msg_list(struct list_head *head, char *reason)
4736 while (!list_empty(head)) {
4737 struct lnet_process_id id = {0};
4738 struct lnet_msg *msg;
4740 msg = list_entry(head->next, struct lnet_msg, msg_list);
4741 list_del(&msg->msg_list);
4743 id.nid = msg->msg_hdr.src_nid;
4744 id.pid = msg->msg_hdr.src_pid;
4746 LASSERT(msg->msg_md == NULL);
4747 LASSERT(msg->msg_rx_delayed);
4748 LASSERT(msg->msg_rxpeer != NULL);
4749 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
4751 CWARN("Dropping delayed PUT from %s portal %d match %llu"
4752 " offset %d length %d: %s\n",
4754 msg->msg_hdr.msg.put.ptl_index,
4755 msg->msg_hdr.msg.put.match_bits,
4756 msg->msg_hdr.msg.put.offset,
4757 msg->msg_hdr.payload_length, reason);
4759 /* NB I can't drop msg's ref on msg_rxpeer until after I've
4760 * called lnet_drop_message(), so I just hang onto msg as well
4761 * until that's done */
4763 lnet_drop_message(msg->msg_rxni, msg->msg_rx_cpt,
4764 msg->msg_private, msg->msg_len,
4767 msg->msg_no_resend = true;
4769 * NB: message will not generate event because w/o attached MD,
4770 * but we still should give error code so lnet_msg_decommit()
4771 * can skip counters operations and other checks.
4773 lnet_finalize(msg, -ENOENT);
4778 lnet_recv_delayed_msg_list(struct list_head *head)
4780 while (!list_empty(head)) {
4781 struct lnet_msg *msg;
4782 struct lnet_process_id id;
4784 msg = list_entry(head->next, struct lnet_msg, msg_list);
4785 list_del(&msg->msg_list);
4787 /* md won't disappear under me, since each msg
4788 * holds a ref on it */
4790 id.nid = msg->msg_hdr.src_nid;
4791 id.pid = msg->msg_hdr.src_pid;
4793 LASSERT(msg->msg_rx_delayed);
4794 LASSERT(msg->msg_md != NULL);
4795 LASSERT(msg->msg_rxpeer != NULL);
4796 LASSERT(msg->msg_rxni != NULL);
4797 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
4799 CDEBUG(D_NET, "Resuming delayed PUT from %s portal %d "
4800 "match %llu offset %d length %d.\n",
4801 libcfs_id2str(id), msg->msg_hdr.msg.put.ptl_index,
4802 msg->msg_hdr.msg.put.match_bits,
4803 msg->msg_hdr.msg.put.offset,
4804 msg->msg_hdr.payload_length);
4806 lnet_recv_put(msg->msg_rxni, msg);
4811 lnet_attach_rsp_tracker(struct lnet_rsp_tracker *rspt, int cpt,
4812 struct lnet_libmd *md, struct lnet_handle_md mdh)
4815 struct lnet_rsp_tracker *local_rspt;
4818 * MD has a refcount taken by message so it's not going away.
4819 * The MD however can be looked up. We need to secure the access
4820 * to the md_rspt_ptr by taking the res_lock.
4821 * The rspt can be accessed without protection up to when it gets
4822 * added to the list.
4826 local_rspt = md->md_rspt_ptr;
4827 timeout_ns = lnet_transaction_timeout * NSEC_PER_SEC;
4828 if (local_rspt != NULL) {
4830 * we already have an rspt attached to the md, so we'll
4831 * update the deadline on that one.
4833 lnet_rspt_free(rspt, cpt);
4836 rspt->rspt_mdh = mdh;
4837 rspt->rspt_cpt = cpt;
4838 /* store the rspt so we can access it when we get the REPLY */
4839 md->md_rspt_ptr = rspt;
4842 local_rspt->rspt_deadline = ktime_add_ns(ktime_get(), timeout_ns);
4845 * add to the list of tracked responses. It's added to tail of the
4846 * list in order to expire all the older entries first.
4849 list_move_tail(&local_rspt->rspt_on_list, the_lnet.ln_mt_rstq[cpt]);
4850 lnet_net_unlock(cpt);
4851 lnet_res_unlock(cpt);
4855 * Initiate an asynchronous PUT operation.
4857 * There are several events associated with a PUT: completion of the send on
4858 * the initiator node (LNET_EVENT_SEND), and when the send completes
4859 * successfully, the receipt of an acknowledgment (LNET_EVENT_ACK) indicating
4860 * that the operation was accepted by the target. The event LNET_EVENT_PUT is
4861 * used at the target node to indicate the completion of incoming data
4864 * The local events will be logged in the EQ associated with the MD pointed to
4865 * by \a mdh handle. Using a MD without an associated EQ results in these
4866 * events being discarded. In this case, the caller must have another
4867 * mechanism (e.g., a higher level protocol) for determining when it is safe
4868 * to modify the memory region associated with the MD.
4870 * Note that LNet does not guarantee the order of LNET_EVENT_SEND and
4871 * LNET_EVENT_ACK, though intuitively ACK should happen after SEND.
4873 * \param self Indicates the NID of a local interface through which to send
4874 * the PUT request. Use LNET_NID_ANY to let LNet choose one by itself.
4875 * \param mdh A handle for the MD that describes the memory to be sent. The MD
4876 * must be "free floating" (See LNetMDBind()).
4877 * \param ack Controls whether an acknowledgment is requested.
4878 * Acknowledgments are only sent when they are requested by the initiating
4879 * process and the target MD enables them.
4880 * \param target A process identifier for the target process.
4881 * \param portal The index in the \a target's portal table.
4882 * \param match_bits The match bits to use for MD selection at the target
4884 * \param offset The offset into the target MD (only used when the target
4885 * MD has the LNET_MD_MANAGE_REMOTE option set).
4886 * \param hdr_data 64 bits of user data that can be included in the message
4887 * header. This data is written to an event queue entry at the target if an
4888 * EQ is present on the matching MD.
4890 * \retval 0 Success, and only in this case events will be generated
4891 * and logged to EQ (if it exists).
4892 * \retval -EIO Simulated failure.
4893 * \retval -ENOMEM Memory allocation failure.
4894 * \retval -ENOENT Invalid MD object.
4896 * \see struct lnet_event::hdr_data and lnet_event_kind_t.
4899 LNetPut(lnet_nid_t self, struct lnet_handle_md mdh, enum lnet_ack_req ack,
4900 struct lnet_process_id target, unsigned int portal,
4901 __u64 match_bits, unsigned int offset,
4904 struct lnet_msg *msg;
4905 struct lnet_libmd *md;
4908 struct lnet_rsp_tracker *rspt = NULL;
4910 LASSERT(the_lnet.ln_refcount > 0);
4912 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
4913 fail_peer(target.nid, 1)) { /* shall we now? */
4914 CERROR("Dropping PUT to %s: simulated failure\n",
4915 libcfs_id2str(target));
4919 msg = lnet_msg_alloc();
4921 CERROR("Dropping PUT to %s: ENOMEM on struct lnet_msg\n",
4922 libcfs_id2str(target));
4925 msg->msg_vmflush = !!(current->flags & PF_MEMALLOC);
4927 cpt = lnet_cpt_of_cookie(mdh.cookie);
4929 if (ack == LNET_ACK_REQ) {
4930 rspt = lnet_rspt_alloc(cpt);
4932 CERROR("Dropping PUT to %s: ENOMEM on response tracker\n",
4933 libcfs_id2str(target));
4936 INIT_LIST_HEAD(&rspt->rspt_on_list);
4941 md = lnet_handle2md(&mdh);
4942 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
4943 CERROR("Dropping PUT (%llu:%d:%s): MD (%d) invalid\n",
4944 match_bits, portal, libcfs_id2str(target),
4945 md == NULL ? -1 : md->md_threshold);
4946 if (md != NULL && md->md_me != NULL)
4947 CERROR("Source MD also attached to portal %d\n",
4948 md->md_me->me_portal);
4949 lnet_res_unlock(cpt);
4952 lnet_rspt_free(rspt, cpt);
4958 CDEBUG(D_NET, "LNetPut -> %s\n", libcfs_id2str(target));
4960 lnet_msg_attach_md(msg, md, 0, 0);
4962 lnet_prep_send(msg, LNET_MSG_PUT, target, 0, md->md_length);
4964 msg->msg_hdr.msg.put.match_bits = cpu_to_le64(match_bits);
4965 msg->msg_hdr.msg.put.ptl_index = cpu_to_le32(portal);
4966 msg->msg_hdr.msg.put.offset = cpu_to_le32(offset);
4967 msg->msg_hdr.msg.put.hdr_data = hdr_data;
4969 /* NB handles only looked up by creator (no flips) */
4970 if (ack == LNET_ACK_REQ) {
4971 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
4972 the_lnet.ln_interface_cookie;
4973 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
4974 md->md_lh.lh_cookie;
4976 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
4977 LNET_WIRE_HANDLE_COOKIE_NONE;
4978 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
4979 LNET_WIRE_HANDLE_COOKIE_NONE;
4982 lnet_res_unlock(cpt);
4984 lnet_build_msg_event(msg, LNET_EVENT_SEND);
4986 if (rspt && lnet_response_tracking_enabled(LNET_MSG_PUT,
4988 lnet_attach_rsp_tracker(rspt, cpt, md, mdh);
4990 lnet_rspt_free(rspt, cpt);
4992 if (CFS_FAIL_CHECK_ORSET(CFS_FAIL_PTLRPC_OST_BULK_CB2,
4996 rc = lnet_send(self, msg, LNET_NID_ANY);
4999 CNETERR("Error sending PUT to %s: %d\n",
5000 libcfs_id2str(target), rc);
5001 msg->msg_no_resend = true;
5002 lnet_finalize(msg, rc);
5005 /* completion will be signalled by an event */
5008 EXPORT_SYMBOL(LNetPut);
5011 * The LND can DMA direct to the GET md (i.e. no REPLY msg). This
5012 * returns a msg for the LND to pass to lnet_finalize() when the sink
5013 * data has been received.
5015 * CAVEAT EMPTOR: 'getmsg' is the original GET, which is freed when
5016 * lnet_finalize() is called on it, so the LND must call this first
5019 lnet_create_reply_msg(struct lnet_ni *ni, struct lnet_msg *getmsg)
5021 struct lnet_msg *msg = lnet_msg_alloc();
5022 struct lnet_libmd *getmd = getmsg->msg_md;
5023 struct lnet_process_id peer_id = getmsg->msg_target;
5026 LASSERT(!getmsg->msg_target_is_router);
5027 LASSERT(!getmsg->msg_routing);
5030 CERROR("%s: Dropping REPLY from %s: can't allocate msg\n",
5031 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id));
5035 cpt = lnet_cpt_of_cookie(getmd->md_lh.lh_cookie);
5038 LASSERT(getmd->md_refcount > 0);
5040 if (getmd->md_threshold == 0) {
5041 CERROR("%s: Dropping REPLY from %s for inactive MD %p\n",
5042 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id),
5044 lnet_res_unlock(cpt);
5048 LASSERT(getmd->md_offset == 0);
5050 CDEBUG(D_NET, "%s: Reply from %s md %p\n",
5051 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id), getmd);
5053 /* setup information for lnet_build_msg_event */
5054 msg->msg_initiator = getmsg->msg_txpeer->lpni_peer_net->lpn_peer->lp_primary_nid;
5055 msg->msg_from = peer_id.nid;
5056 msg->msg_type = LNET_MSG_GET; /* flag this msg as an "optimized" GET */
5057 msg->msg_hdr.src_nid = peer_id.nid;
5058 msg->msg_hdr.payload_length = getmd->md_length;
5059 msg->msg_receiving = 1; /* required by lnet_msg_attach_md */
5061 lnet_msg_attach_md(msg, getmd, getmd->md_offset, getmd->md_length);
5062 lnet_res_unlock(cpt);
5064 cpt = lnet_cpt_of_nid(peer_id.nid, ni);
5067 lnet_msg_commit(msg, cpt);
5068 lnet_net_unlock(cpt);
5070 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
5075 cpt = lnet_cpt_of_nid(peer_id.nid, ni);
5078 lnet_incr_stats(&ni->ni_stats, LNET_MSG_GET, LNET_STATS_TYPE_DROP);
5079 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
5080 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length +=
5082 lnet_net_unlock(cpt);
5089 EXPORT_SYMBOL(lnet_create_reply_msg);
5092 lnet_set_reply_msg_len(struct lnet_ni *ni, struct lnet_msg *reply,
5095 /* Set the REPLY length, now the RDMA that elides the REPLY message has
5096 * completed and I know it. */
5097 LASSERT(reply != NULL);
5098 LASSERT(reply->msg_type == LNET_MSG_GET);
5099 LASSERT(reply->msg_ev.type == LNET_EVENT_REPLY);
5101 /* NB I trusted my peer to RDMA. If she tells me she's written beyond
5102 * the end of my buffer, I might as well be dead. */
5103 LASSERT(len <= reply->msg_ev.mlength);
5105 reply->msg_ev.mlength = len;
5107 EXPORT_SYMBOL(lnet_set_reply_msg_len);
5110 * Initiate an asynchronous GET operation.
5112 * On the initiator node, an LNET_EVENT_SEND is logged when the GET request
5113 * is sent, and an LNET_EVENT_REPLY is logged when the data returned from
5114 * the target node in the REPLY has been written to local MD.
5116 * On the target node, an LNET_EVENT_GET is logged when the GET request
5117 * arrives and is accepted into a MD.
5119 * \param self,target,portal,match_bits,offset See the discussion in LNetPut().
5120 * \param mdh A handle for the MD that describes the memory into which the
5121 * requested data will be received. The MD must be "free floating" (See LNetMDBind()).
5123 * \retval 0 Success, and only in this case events will be generated
5124 * and logged to EQ (if it exists) of the MD.
5125 * \retval -EIO Simulated failure.
5126 * \retval -ENOMEM Memory allocation failure.
5127 * \retval -ENOENT Invalid MD object.
5130 LNetGet(lnet_nid_t self, struct lnet_handle_md mdh,
5131 struct lnet_process_id target, unsigned int portal,
5132 __u64 match_bits, unsigned int offset, bool recovery)
5134 struct lnet_msg *msg;
5135 struct lnet_libmd *md;
5136 struct lnet_rsp_tracker *rspt;
5140 LASSERT(the_lnet.ln_refcount > 0);
5142 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
5143 fail_peer(target.nid, 1)) /* shall we now? */
5145 CERROR("Dropping GET to %s: simulated failure\n",
5146 libcfs_id2str(target));
5150 msg = lnet_msg_alloc();
5152 CERROR("Dropping GET to %s: ENOMEM on struct lnet_msg\n",
5153 libcfs_id2str(target));
5157 cpt = lnet_cpt_of_cookie(mdh.cookie);
5159 rspt = lnet_rspt_alloc(cpt);
5161 CERROR("Dropping GET to %s: ENOMEM on response tracker\n",
5162 libcfs_id2str(target));
5165 INIT_LIST_HEAD(&rspt->rspt_on_list);
5167 msg->msg_recovery = recovery;
5171 md = lnet_handle2md(&mdh);
5172 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
5173 CERROR("Dropping GET (%llu:%d:%s): MD (%d) invalid\n",
5174 match_bits, portal, libcfs_id2str(target),
5175 md == NULL ? -1 : md->md_threshold);
5176 if (md != NULL && md->md_me != NULL)
5177 CERROR("REPLY MD also attached to portal %d\n",
5178 md->md_me->me_portal);
5180 lnet_res_unlock(cpt);
5183 lnet_rspt_free(rspt, cpt);
5187 CDEBUG(D_NET, "LNetGet -> %s\n", libcfs_id2str(target));
5189 lnet_msg_attach_md(msg, md, 0, 0);
5191 lnet_prep_send(msg, LNET_MSG_GET, target, 0, 0);
5193 msg->msg_hdr.msg.get.match_bits = cpu_to_le64(match_bits);
5194 msg->msg_hdr.msg.get.ptl_index = cpu_to_le32(portal);
5195 msg->msg_hdr.msg.get.src_offset = cpu_to_le32(offset);
5196 msg->msg_hdr.msg.get.sink_length = cpu_to_le32(md->md_length);
5198 /* NB handles only looked up by creator (no flips) */
5199 msg->msg_hdr.msg.get.return_wmd.wh_interface_cookie =
5200 the_lnet.ln_interface_cookie;
5201 msg->msg_hdr.msg.get.return_wmd.wh_object_cookie =
5202 md->md_lh.lh_cookie;
5204 lnet_res_unlock(cpt);
5206 lnet_build_msg_event(msg, LNET_EVENT_SEND);
5208 if (lnet_response_tracking_enabled(LNET_MSG_GET, md->md_options))
5209 lnet_attach_rsp_tracker(rspt, cpt, md, mdh);
5211 lnet_rspt_free(rspt, cpt);
5213 rc = lnet_send(self, msg, LNET_NID_ANY);
5215 CNETERR("Error sending GET to %s: %d\n",
5216 libcfs_id2str(target), rc);
5217 msg->msg_no_resend = true;
5218 lnet_finalize(msg, rc);
5221 /* completion will be signalled by an event */
5224 EXPORT_SYMBOL(LNetGet);
5227 * Calculate distance to node at \a dstnid.
5229 * \param dstnid Target NID.
5230 * \param srcnidp If not NULL, NID of the local interface to reach \a dstnid
5232 * \param orderp If not NULL, order of the route to reach \a dstnid is saved
5235 * \retval 0 If \a dstnid belongs to a local interface, and reserved option
5236 * local_nid_dist_zero is set, which is the default.
5237 * \retval positives Distance to target NID, i.e. number of hops plus one.
5238 * \retval -EHOSTUNREACH If \a dstnid is not reachable.
5241 LNetDist(lnet_nid_t dstnid, lnet_nid_t *srcnidp, __u32 *orderp)
5243 struct list_head *e;
5244 struct lnet_ni *ni = NULL;
5245 struct lnet_remotenet *rnet;
5246 __u32 dstnet = LNET_NIDNET(dstnid);
5250 struct list_head *rn_list;
5252 /* if !local_nid_dist_zero, I don't return a distance of 0 ever
5253 * (when lustre sees a distance of 0, it substitutes 0@lo), so I
5254 * keep order 0 free for 0@lo and order 1 free for a local NID
5257 LASSERT(the_lnet.ln_refcount > 0);
5259 cpt = lnet_net_lock_current();
5261 while ((ni = lnet_get_next_ni_locked(NULL, ni))) {
5262 if (ni->ni_nid == dstnid) {
5263 if (srcnidp != NULL)
5265 if (orderp != NULL) {
5266 if (dstnid == LNET_NID_LO_0)
5271 lnet_net_unlock(cpt);
5273 return local_nid_dist_zero ? 0 : 1;
5276 if (LNET_NIDNET(ni->ni_nid) == dstnet) {
5277 /* Check if ni was originally created in
5278 * current net namespace.
5279 * If not, assign order above 0xffff0000,
5280 * to make this ni not a priority. */
5281 if (current->nsproxy &&
5282 !net_eq(ni->ni_net_ns, current->nsproxy->net_ns))
5283 order += 0xffff0000;
5284 if (srcnidp != NULL)
5285 *srcnidp = ni->ni_nid;
5288 lnet_net_unlock(cpt);
5295 rn_list = lnet_net2rnethash(dstnet);
5296 list_for_each(e, rn_list) {
5297 rnet = list_entry(e, struct lnet_remotenet, lrn_list);
5299 if (rnet->lrn_net == dstnet) {
5300 struct lnet_route *route;
5301 struct lnet_route *shortest = NULL;
5302 __u32 shortest_hops = LNET_UNDEFINED_HOPS;
5305 LASSERT(!list_empty(&rnet->lrn_routes));
5307 list_for_each_entry(route, &rnet->lrn_routes,
5309 route_hops = route->lr_hops;
5310 if (route_hops == LNET_UNDEFINED_HOPS)
5312 if (shortest == NULL ||
5313 route_hops < shortest_hops) {
5315 shortest_hops = route_hops;
5319 LASSERT(shortest != NULL);
5320 hops = shortest_hops;
5321 if (srcnidp != NULL) {
5322 struct lnet_net *net;
5323 net = lnet_get_net_locked(shortest->lr_lnet);
5325 ni = lnet_get_next_ni_locked(net, NULL);
5326 *srcnidp = ni->ni_nid;
5330 lnet_net_unlock(cpt);
5336 lnet_net_unlock(cpt);
5337 return -EHOSTUNREACH;
5339 EXPORT_SYMBOL(LNetDist);