4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lnet/lnet/lib-move.c
34 * Data movement routines
37 #define DEBUG_SUBSYSTEM S_LNET
39 #include <linux/pagemap.h>
41 #include <lnet/lib-lnet.h>
42 #include <linux/nsproxy.h>
43 #include <net/net_namespace.h>
45 static int local_nid_dist_zero = 1;
46 module_param(local_nid_dist_zero, int, 0444);
47 MODULE_PARM_DESC(local_nid_dist_zero, "Reserved");
49 struct lnet_send_data {
50 struct lnet_ni *sd_best_ni;
51 struct lnet_peer_ni *sd_best_lpni;
52 struct lnet_peer_ni *sd_final_dst_lpni;
53 struct lnet_peer *sd_peer;
54 struct lnet_peer *sd_gw_peer;
55 struct lnet_peer_ni *sd_gw_lpni;
56 struct lnet_peer_net *sd_peer_net;
57 struct lnet_msg *sd_msg;
58 lnet_nid_t sd_dst_nid;
59 lnet_nid_t sd_src_nid;
60 lnet_nid_t sd_rtr_nid;
67 lnet_msg_is_response(struct lnet_msg *msg)
69 return msg->msg_type == LNET_MSG_ACK || msg->msg_type == LNET_MSG_REPLY;
73 lnet_response_tracking_enabled(__u32 msg_type, unsigned int md_options)
75 if (md_options & LNET_MD_NO_TRACK_RESPONSE)
76 /* Explicitly disabled in MD options */
79 if (md_options & LNET_MD_TRACK_RESPONSE)
80 /* Explicity enabled in MD options */
83 if (lnet_response_tracking == 3)
84 /* Enabled for all message types */
87 if (msg_type == LNET_MSG_PUT)
88 return lnet_response_tracking == 2;
90 if (msg_type == LNET_MSG_GET)
91 return lnet_response_tracking == 1;
96 static inline struct lnet_comm_count *
97 get_stats_counts(struct lnet_element_stats *stats,
98 enum lnet_stats_type stats_type)
100 switch (stats_type) {
101 case LNET_STATS_TYPE_SEND:
102 return &stats->el_send_stats;
103 case LNET_STATS_TYPE_RECV:
104 return &stats->el_recv_stats;
105 case LNET_STATS_TYPE_DROP:
106 return &stats->el_drop_stats;
108 CERROR("Unknown stats type\n");
114 void lnet_incr_stats(struct lnet_element_stats *stats,
115 enum lnet_msg_type msg_type,
116 enum lnet_stats_type stats_type)
118 struct lnet_comm_count *counts = get_stats_counts(stats, stats_type);
124 atomic_inc(&counts->co_ack_count);
127 atomic_inc(&counts->co_put_count);
130 atomic_inc(&counts->co_get_count);
133 atomic_inc(&counts->co_reply_count);
136 atomic_inc(&counts->co_hello_count);
139 CERROR("There is a BUG in the code. Unknown message type\n");
144 __u32 lnet_sum_stats(struct lnet_element_stats *stats,
145 enum lnet_stats_type stats_type)
147 struct lnet_comm_count *counts = get_stats_counts(stats, stats_type);
151 return (atomic_read(&counts->co_ack_count) +
152 atomic_read(&counts->co_put_count) +
153 atomic_read(&counts->co_get_count) +
154 atomic_read(&counts->co_reply_count) +
155 atomic_read(&counts->co_hello_count));
158 static inline void assign_stats(struct lnet_ioctl_comm_count *msg_stats,
159 struct lnet_comm_count *counts)
161 msg_stats->ico_get_count = atomic_read(&counts->co_get_count);
162 msg_stats->ico_put_count = atomic_read(&counts->co_put_count);
163 msg_stats->ico_reply_count = atomic_read(&counts->co_reply_count);
164 msg_stats->ico_ack_count = atomic_read(&counts->co_ack_count);
165 msg_stats->ico_hello_count = atomic_read(&counts->co_hello_count);
168 void lnet_usr_translate_stats(struct lnet_ioctl_element_msg_stats *msg_stats,
169 struct lnet_element_stats *stats)
171 struct lnet_comm_count *counts;
176 counts = get_stats_counts(stats, LNET_STATS_TYPE_SEND);
179 assign_stats(&msg_stats->im_send_stats, counts);
181 counts = get_stats_counts(stats, LNET_STATS_TYPE_RECV);
184 assign_stats(&msg_stats->im_recv_stats, counts);
186 counts = get_stats_counts(stats, LNET_STATS_TYPE_DROP);
189 assign_stats(&msg_stats->im_drop_stats, counts);
193 lnet_fail_nid(lnet_nid_t nid, unsigned int threshold)
195 struct lnet_test_peer *tp;
196 struct list_head *el;
197 struct list_head *next;
200 /* NB: use lnet_net_lock(0) to serialize operations on test peers */
201 if (threshold != 0) {
202 /* Adding a new entry */
203 LIBCFS_ALLOC(tp, sizeof(*tp));
208 tp->tp_threshold = threshold;
211 list_add_tail(&tp->tp_list, &the_lnet.ln_test_peers);
218 list_for_each_safe(el, next, &the_lnet.ln_test_peers) {
219 tp = list_entry(el, struct lnet_test_peer, tp_list);
221 if (tp->tp_threshold == 0 || /* needs culling anyway */
222 nid == LNET_NID_ANY || /* removing all entries */
223 tp->tp_nid == nid) { /* matched this one */
224 list_move(&tp->tp_list, &cull);
230 while (!list_empty(&cull)) {
231 tp = list_entry(cull.next, struct lnet_test_peer, tp_list);
233 list_del(&tp->tp_list);
234 LIBCFS_FREE(tp, sizeof(*tp));
240 fail_peer (lnet_nid_t nid, int outgoing)
242 struct lnet_test_peer *tp;
243 struct list_head *el;
244 struct list_head *next;
248 /* NB: use lnet_net_lock(0) to serialize operations on test peers */
251 list_for_each_safe(el, next, &the_lnet.ln_test_peers) {
252 tp = list_entry(el, struct lnet_test_peer, tp_list);
254 if (tp->tp_threshold == 0) {
257 /* only cull zombies on outgoing tests,
258 * since we may be at interrupt priority on
259 * incoming messages. */
260 list_move(&tp->tp_list, &cull);
265 if (tp->tp_nid == LNET_NID_ANY || /* fail every peer */
266 nid == tp->tp_nid) { /* fail this peer */
269 if (tp->tp_threshold != LNET_MD_THRESH_INF) {
272 tp->tp_threshold == 0) {
274 list_move(&tp->tp_list, &cull);
283 while (!list_empty(&cull)) {
284 tp = list_entry(cull.next, struct lnet_test_peer, tp_list);
285 list_del(&tp->tp_list);
287 LIBCFS_FREE(tp, sizeof(*tp));
294 lnet_iov_nob(unsigned int niov, struct kvec *iov)
296 unsigned int nob = 0;
298 LASSERT(niov == 0 || iov != NULL);
300 nob += (iov++)->iov_len;
304 EXPORT_SYMBOL(lnet_iov_nob);
307 lnet_copy_iov2iov(unsigned int ndiov, struct kvec *diov, unsigned int doffset,
308 unsigned int nsiov, struct kvec *siov, unsigned int soffset,
311 /* NB diov, siov are READ-ONLY */
312 unsigned int this_nob;
317 /* skip complete frags before 'doffset' */
319 while (doffset >= diov->iov_len) {
320 doffset -= diov->iov_len;
326 /* skip complete frags before 'soffset' */
328 while (soffset >= siov->iov_len) {
329 soffset -= siov->iov_len;
338 this_nob = min3((unsigned int)diov->iov_len - doffset,
339 (unsigned int)siov->iov_len - soffset,
342 memcpy((char *)diov->iov_base + doffset,
343 (char *)siov->iov_base + soffset, this_nob);
346 if (diov->iov_len > doffset + this_nob) {
354 if (siov->iov_len > soffset + this_nob) {
363 EXPORT_SYMBOL(lnet_copy_iov2iov);
366 lnet_kiov_nob(unsigned int niov, struct bio_vec *kiov)
368 unsigned int nob = 0;
370 LASSERT(niov == 0 || kiov != NULL);
372 nob += (kiov++)->bv_len;
376 EXPORT_SYMBOL(lnet_kiov_nob);
379 lnet_copy_kiov2kiov(unsigned int ndiov, struct bio_vec *diov,
380 unsigned int doffset,
381 unsigned int nsiov, struct bio_vec *siov,
382 unsigned int soffset,
385 /* NB diov, siov are READ-ONLY */
386 unsigned int this_nob;
393 LASSERT (!in_interrupt ());
396 while (doffset >= diov->bv_len) {
397 doffset -= diov->bv_len;
404 while (soffset >= siov->bv_len) {
405 soffset -= siov->bv_len;
414 this_nob = min3(diov->bv_len - doffset,
415 siov->bv_len - soffset,
419 daddr = ((char *)kmap(diov->bv_page)) +
420 diov->bv_offset + doffset;
422 saddr = ((char *)kmap(siov->bv_page)) +
423 siov->bv_offset + soffset;
425 /* Vanishing risk of kmap deadlock when mapping 2 pages.
426 * However in practice at least one of the kiovs will be mapped
427 * kernel pages and the map/unmap will be NOOPs */
429 memcpy (daddr, saddr, this_nob);
432 if (diov->bv_len > doffset + this_nob) {
436 kunmap(diov->bv_page);
443 if (siov->bv_len > soffset + this_nob) {
447 kunmap(siov->bv_page);
456 kunmap(diov->bv_page);
458 kunmap(siov->bv_page);
460 EXPORT_SYMBOL(lnet_copy_kiov2kiov);
463 lnet_copy_kiov2iov (unsigned int niov, struct kvec *iov, unsigned int iovoffset,
464 unsigned int nkiov, struct bio_vec *kiov,
465 unsigned int kiovoffset,
468 /* NB iov, kiov are READ-ONLY */
469 unsigned int this_nob;
475 LASSERT (!in_interrupt ());
478 while (iovoffset >= iov->iov_len) {
479 iovoffset -= iov->iov_len;
486 while (kiovoffset >= kiov->bv_len) {
487 kiovoffset -= kiov->bv_len;
496 this_nob = min3((unsigned int)iov->iov_len - iovoffset,
497 (unsigned int)kiov->bv_len - kiovoffset,
501 addr = ((char *)kmap(kiov->bv_page)) +
502 kiov->bv_offset + kiovoffset;
504 memcpy((char *)iov->iov_base + iovoffset, addr, this_nob);
507 if (iov->iov_len > iovoffset + this_nob) {
508 iovoffset += this_nob;
515 if (kiov->bv_len > kiovoffset + this_nob) {
517 kiovoffset += this_nob;
519 kunmap(kiov->bv_page);
529 kunmap(kiov->bv_page);
531 EXPORT_SYMBOL(lnet_copy_kiov2iov);
534 lnet_copy_iov2kiov(unsigned int nkiov, struct bio_vec *kiov,
535 unsigned int kiovoffset,
536 unsigned int niov, struct kvec *iov, unsigned int iovoffset,
539 /* NB kiov, iov are READ-ONLY */
540 unsigned int this_nob;
546 LASSERT (!in_interrupt ());
549 while (kiovoffset >= kiov->bv_len) {
550 kiovoffset -= kiov->bv_len;
557 while (iovoffset >= iov->iov_len) {
558 iovoffset -= iov->iov_len;
567 this_nob = min3((unsigned int)kiov->bv_len - kiovoffset,
568 (unsigned int)iov->iov_len - iovoffset,
572 addr = ((char *)kmap(kiov->bv_page)) +
573 kiov->bv_offset + kiovoffset;
575 memcpy (addr, (char *)iov->iov_base + iovoffset, this_nob);
578 if (kiov->bv_len > kiovoffset + this_nob) {
580 kiovoffset += this_nob;
582 kunmap(kiov->bv_page);
589 if (iov->iov_len > iovoffset + this_nob) {
590 iovoffset += this_nob;
599 kunmap(kiov->bv_page);
601 EXPORT_SYMBOL(lnet_copy_iov2kiov);
604 lnet_extract_kiov(int dst_niov, struct bio_vec *dst,
605 int src_niov, struct bio_vec *src,
606 unsigned int offset, unsigned int len)
608 /* Initialise 'dst' to the subset of 'src' starting at 'offset',
609 * for exactly 'len' bytes, and return the number of entries.
610 * NB not destructive to 'src' */
611 unsigned int frag_len;
614 if (len == 0) /* no data => */
615 return (0); /* no frags */
617 LASSERT(src_niov > 0);
618 while (offset >= src->bv_len) { /* skip initial frags */
619 offset -= src->bv_len;
622 LASSERT(src_niov > 0);
627 LASSERT(src_niov > 0);
628 LASSERT((int)niov <= dst_niov);
630 frag_len = src->bv_len - offset;
631 dst->bv_page = src->bv_page;
632 dst->bv_offset = src->bv_offset + offset;
634 if (len <= frag_len) {
636 LASSERT(dst->bv_offset + dst->bv_len <= PAGE_SIZE);
640 dst->bv_len = frag_len;
641 LASSERT(dst->bv_offset + dst->bv_len <= PAGE_SIZE);
651 EXPORT_SYMBOL(lnet_extract_kiov);
654 lnet_ni_recv(struct lnet_ni *ni, void *private, struct lnet_msg *msg,
655 int delayed, unsigned int offset, unsigned int mlen,
658 unsigned int niov = 0;
659 struct kvec *iov = NULL;
660 struct bio_vec *kiov = NULL;
663 LASSERT (!in_interrupt ());
664 LASSERT (mlen == 0 || msg != NULL);
667 LASSERT(msg->msg_receiving);
668 LASSERT(!msg->msg_sending);
669 LASSERT(rlen == msg->msg_len);
670 LASSERT(mlen <= msg->msg_len);
671 LASSERT(msg->msg_offset == offset);
672 LASSERT(msg->msg_wanted == mlen);
674 msg->msg_receiving = 0;
677 niov = msg->msg_niov;
678 kiov = msg->msg_kiov;
681 LASSERT ((iov == NULL) != (kiov == NULL));
685 rc = (ni->ni_net->net_lnd->lnd_recv)(ni, private, msg, delayed,
686 niov, kiov, offset, mlen,
689 lnet_finalize(msg, rc);
693 lnet_setpayloadbuffer(struct lnet_msg *msg)
695 struct lnet_libmd *md = msg->msg_md;
697 LASSERT(msg->msg_len > 0);
698 LASSERT(!msg->msg_routing);
700 LASSERT(msg->msg_niov == 0);
701 LASSERT(msg->msg_kiov == NULL);
703 msg->msg_niov = md->md_niov;
704 msg->msg_kiov = md->md_kiov;
708 lnet_prep_send(struct lnet_msg *msg, int type, struct lnet_process_id target,
709 unsigned int offset, unsigned int len)
711 msg->msg_type = type;
712 msg->msg_target = target;
714 msg->msg_offset = offset;
717 lnet_setpayloadbuffer(msg);
719 memset (&msg->msg_hdr, 0, sizeof (msg->msg_hdr));
720 msg->msg_hdr.type = cpu_to_le32(type);
721 /* dest_nid will be overwritten by lnet_select_pathway() */
722 msg->msg_hdr.dest_nid = cpu_to_le64(target.nid);
723 msg->msg_hdr.dest_pid = cpu_to_le32(target.pid);
724 /* src_nid will be set later */
725 msg->msg_hdr.src_pid = cpu_to_le32(the_lnet.ln_pid);
726 msg->msg_hdr.payload_length = cpu_to_le32(len);
730 lnet_ni_send(struct lnet_ni *ni, struct lnet_msg *msg)
732 void *priv = msg->msg_private;
735 LASSERT(!in_interrupt());
736 LASSERT(ni->ni_nid == LNET_NID_LO_0 ||
737 (msg->msg_txcredit && msg->msg_peertxcredit));
739 rc = (ni->ni_net->net_lnd->lnd_send)(ni, priv, msg);
741 msg->msg_no_resend = true;
742 lnet_finalize(msg, rc);
747 lnet_ni_eager_recv(struct lnet_ni *ni, struct lnet_msg *msg)
751 LASSERT(!msg->msg_sending);
752 LASSERT(msg->msg_receiving);
753 LASSERT(!msg->msg_rx_ready_delay);
754 LASSERT(ni->ni_net->net_lnd->lnd_eager_recv != NULL);
756 msg->msg_rx_ready_delay = 1;
757 rc = (ni->ni_net->net_lnd->lnd_eager_recv)(ni, msg->msg_private, msg,
760 CERROR("recv from %s / send to %s aborted: "
761 "eager_recv failed %d\n",
762 libcfs_nid2str(msg->msg_rxpeer->lpni_nid),
763 libcfs_id2str(msg->msg_target), rc);
764 LASSERT(rc < 0); /* required by my callers */
771 lnet_is_peer_deadline_passed(struct lnet_peer_ni *lpni, time64_t now)
775 deadline = lpni->lpni_last_alive +
776 lpni->lpni_net->net_tunables.lct_peer_timeout;
779 * assume peer_ni is alive as long as we're within the configured
788 /* NB: returns 1 when alive, 0 when dead, negative when error;
789 * may drop the lnet_net_lock */
791 lnet_peer_alive_locked(struct lnet_ni *ni, struct lnet_peer_ni *lpni,
792 struct lnet_msg *msg)
794 time64_t now = ktime_get_seconds();
796 if (!lnet_peer_aliveness_enabled(lpni))
800 * If we're resending a message, let's attempt to send it even if
801 * the peer is down to fulfill our resend quota on the message
803 if (msg->msg_retry_count > 0)
806 /* try and send recovery messages irregardless */
807 if (msg->msg_recovery)
810 /* always send any responses */
811 if (lnet_msg_is_response(msg))
814 if (!lnet_is_peer_deadline_passed(lpni, now))
817 return lnet_is_peer_ni_alive(lpni);
821 * \param msg The message to be sent.
822 * \param do_send True if lnet_ni_send() should be called in this function.
823 * lnet_send() is going to lnet_net_unlock immediately after this, so
824 * it sets do_send FALSE and I don't do the unlock/send/lock bit.
826 * \retval LNET_CREDIT_OK If \a msg sent or OK to send.
827 * \retval LNET_CREDIT_WAIT If \a msg blocked for credit.
828 * \retval -EHOSTUNREACH If the next hop of the message appears dead.
829 * \retval -ECANCELED If the MD of the message has been unlinked.
832 lnet_post_send_locked(struct lnet_msg *msg, int do_send)
834 struct lnet_peer_ni *lp = msg->msg_txpeer;
835 struct lnet_ni *ni = msg->msg_txni;
836 int cpt = msg->msg_tx_cpt;
837 struct lnet_tx_queue *tq = ni->ni_tx_queues[cpt];
839 /* non-lnet_send() callers have checked before */
840 LASSERT(!do_send || msg->msg_tx_delayed);
841 LASSERT(!msg->msg_receiving);
842 LASSERT(msg->msg_tx_committed);
843 /* can't get here if we're sending to the loopback interface */
844 LASSERT(lp->lpni_nid != the_lnet.ln_loni->ni_nid);
846 /* NB 'lp' is always the next hop */
847 if ((msg->msg_target.pid & LNET_PID_USERFLAG) == 0 &&
848 lnet_peer_alive_locked(ni, lp, msg) == 0) {
849 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
850 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length +=
852 lnet_net_unlock(cpt);
854 lnet_incr_stats(&msg->msg_txpeer->lpni_stats,
856 LNET_STATS_TYPE_DROP);
858 lnet_incr_stats(&msg->msg_txni->ni_stats,
860 LNET_STATS_TYPE_DROP);
862 CNETERR("Dropping message for %s: peer not alive\n",
863 libcfs_id2str(msg->msg_target));
864 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_DROPPED;
866 lnet_finalize(msg, -EHOSTUNREACH);
869 return -EHOSTUNREACH;
872 if (msg->msg_md != NULL &&
873 (msg->msg_md->md_flags & LNET_MD_FLAG_ABORTED) != 0) {
874 lnet_net_unlock(cpt);
876 CNETERR("Aborting message for %s: LNetM[DE]Unlink() already "
877 "called on the MD/ME.\n",
878 libcfs_id2str(msg->msg_target));
880 msg->msg_no_resend = true;
881 CDEBUG(D_NET, "msg %p to %s canceled and will not be resent\n",
882 msg, libcfs_id2str(msg->msg_target));
883 lnet_finalize(msg, -ECANCELED);
890 if (!msg->msg_peertxcredit) {
891 spin_lock(&lp->lpni_lock);
892 LASSERT((lp->lpni_txcredits < 0) ==
893 !list_empty(&lp->lpni_txq));
895 msg->msg_peertxcredit = 1;
896 lp->lpni_txqnob += msg->msg_len + sizeof(struct lnet_hdr);
897 lp->lpni_txcredits--;
899 if (lp->lpni_txcredits < lp->lpni_mintxcredits)
900 lp->lpni_mintxcredits = lp->lpni_txcredits;
902 if (lp->lpni_txcredits < 0) {
903 msg->msg_tx_delayed = 1;
904 list_add_tail(&msg->msg_list, &lp->lpni_txq);
905 spin_unlock(&lp->lpni_lock);
906 return LNET_CREDIT_WAIT;
908 spin_unlock(&lp->lpni_lock);
911 if (!msg->msg_txcredit) {
912 LASSERT((tq->tq_credits < 0) ==
913 !list_empty(&tq->tq_delayed));
915 msg->msg_txcredit = 1;
917 atomic_dec(&ni->ni_tx_credits);
919 if (tq->tq_credits < tq->tq_credits_min)
920 tq->tq_credits_min = tq->tq_credits;
922 if (tq->tq_credits < 0) {
923 msg->msg_tx_delayed = 1;
924 list_add_tail(&msg->msg_list, &tq->tq_delayed);
925 return LNET_CREDIT_WAIT;
929 /* unset the tx_delay flag as we're going to send it now */
930 msg->msg_tx_delayed = 0;
933 lnet_net_unlock(cpt);
934 lnet_ni_send(ni, msg);
937 return LNET_CREDIT_OK;
941 static struct lnet_rtrbufpool *
942 lnet_msg2bufpool(struct lnet_msg *msg)
944 struct lnet_rtrbufpool *rbp;
947 LASSERT(msg->msg_rx_committed);
949 cpt = msg->msg_rx_cpt;
950 rbp = &the_lnet.ln_rtrpools[cpt][0];
952 LASSERT(msg->msg_len <= LNET_MTU);
953 while (msg->msg_len > (unsigned int)rbp->rbp_npages * PAGE_SIZE) {
955 LASSERT(rbp < &the_lnet.ln_rtrpools[cpt][LNET_NRBPOOLS]);
962 lnet_post_routed_recv_locked(struct lnet_msg *msg, int do_recv)
964 /* lnet_parse is going to lnet_net_unlock immediately after this, so it
965 * sets do_recv FALSE and I don't do the unlock/send/lock bit.
966 * I return LNET_CREDIT_WAIT if msg blocked and LNET_CREDIT_OK if
967 * received or OK to receive */
968 struct lnet_peer_ni *lpni = msg->msg_rxpeer;
969 struct lnet_peer *lp;
970 struct lnet_rtrbufpool *rbp;
971 struct lnet_rtrbuf *rb;
973 LASSERT(msg->msg_kiov == NULL);
974 LASSERT(msg->msg_niov == 0);
975 LASSERT(msg->msg_routing);
976 LASSERT(msg->msg_receiving);
977 LASSERT(!msg->msg_sending);
978 LASSERT(lpni->lpni_peer_net);
979 LASSERT(lpni->lpni_peer_net->lpn_peer);
981 lp = lpni->lpni_peer_net->lpn_peer;
983 /* non-lnet_parse callers only receive delayed messages */
984 LASSERT(!do_recv || msg->msg_rx_delayed);
986 if (!msg->msg_peerrtrcredit) {
987 /* lpni_lock protects the credit manipulation */
988 spin_lock(&lpni->lpni_lock);
990 msg->msg_peerrtrcredit = 1;
991 lpni->lpni_rtrcredits--;
992 if (lpni->lpni_rtrcredits < lpni->lpni_minrtrcredits)
993 lpni->lpni_minrtrcredits = lpni->lpni_rtrcredits;
995 if (lpni->lpni_rtrcredits < 0) {
996 spin_unlock(&lpni->lpni_lock);
997 /* must have checked eager_recv before here */
998 LASSERT(msg->msg_rx_ready_delay);
999 msg->msg_rx_delayed = 1;
1000 /* lp_lock protects the lp_rtrq */
1001 spin_lock(&lp->lp_lock);
1002 list_add_tail(&msg->msg_list, &lp->lp_rtrq);
1003 spin_unlock(&lp->lp_lock);
1004 return LNET_CREDIT_WAIT;
1006 spin_unlock(&lpni->lpni_lock);
1009 rbp = lnet_msg2bufpool(msg);
1011 if (!msg->msg_rtrcredit) {
1012 msg->msg_rtrcredit = 1;
1014 if (rbp->rbp_credits < rbp->rbp_mincredits)
1015 rbp->rbp_mincredits = rbp->rbp_credits;
1017 if (rbp->rbp_credits < 0) {
1018 /* must have checked eager_recv before here */
1019 LASSERT(msg->msg_rx_ready_delay);
1020 msg->msg_rx_delayed = 1;
1021 list_add_tail(&msg->msg_list, &rbp->rbp_msgs);
1022 return LNET_CREDIT_WAIT;
1026 LASSERT(!list_empty(&rbp->rbp_bufs));
1027 rb = list_entry(rbp->rbp_bufs.next, struct lnet_rtrbuf, rb_list);
1028 list_del(&rb->rb_list);
1030 msg->msg_niov = rbp->rbp_npages;
1031 msg->msg_kiov = &rb->rb_kiov[0];
1033 /* unset the msg-rx_delayed flag since we're receiving the message */
1034 msg->msg_rx_delayed = 0;
1037 int cpt = msg->msg_rx_cpt;
1039 lnet_net_unlock(cpt);
1040 lnet_ni_recv(msg->msg_rxni, msg->msg_private, msg, 1,
1041 0, msg->msg_len, msg->msg_len);
1044 return LNET_CREDIT_OK;
1048 lnet_return_tx_credits_locked(struct lnet_msg *msg)
1050 struct lnet_peer_ni *txpeer = msg->msg_txpeer;
1051 struct lnet_ni *txni = msg->msg_txni;
1052 struct lnet_msg *msg2;
1054 if (msg->msg_txcredit) {
1055 struct lnet_ni *ni = msg->msg_txni;
1056 struct lnet_tx_queue *tq = ni->ni_tx_queues[msg->msg_tx_cpt];
1058 /* give back NI txcredits */
1059 msg->msg_txcredit = 0;
1061 LASSERT((tq->tq_credits < 0) ==
1062 !list_empty(&tq->tq_delayed));
1065 atomic_inc(&ni->ni_tx_credits);
1066 if (tq->tq_credits <= 0) {
1067 msg2 = list_entry(tq->tq_delayed.next,
1068 struct lnet_msg, msg_list);
1069 list_del(&msg2->msg_list);
1071 LASSERT(msg2->msg_txni == ni);
1072 LASSERT(msg2->msg_tx_delayed);
1073 LASSERT(msg2->msg_tx_cpt == msg->msg_tx_cpt);
1075 (void) lnet_post_send_locked(msg2, 1);
1079 if (msg->msg_peertxcredit) {
1080 /* give back peer txcredits */
1081 msg->msg_peertxcredit = 0;
1083 spin_lock(&txpeer->lpni_lock);
1084 LASSERT((txpeer->lpni_txcredits < 0) ==
1085 !list_empty(&txpeer->lpni_txq));
1087 txpeer->lpni_txqnob -= msg->msg_len + sizeof(struct lnet_hdr);
1088 LASSERT(txpeer->lpni_txqnob >= 0);
1090 txpeer->lpni_txcredits++;
1091 if (txpeer->lpni_txcredits <= 0) {
1094 msg2 = list_entry(txpeer->lpni_txq.next,
1095 struct lnet_msg, msg_list);
1096 list_del(&msg2->msg_list);
1097 spin_unlock(&txpeer->lpni_lock);
1099 LASSERT(msg2->msg_txpeer == txpeer);
1100 LASSERT(msg2->msg_tx_delayed);
1102 msg2_cpt = msg2->msg_tx_cpt;
1105 * The msg_cpt can be different from the msg2_cpt
1106 * so we need to make sure we lock the correct cpt
1108 * Once we call lnet_post_send_locked() it is no
1109 * longer safe to access msg2, since it could've
1110 * been freed by lnet_finalize(), but we still
1111 * need to relock the correct cpt, so we cache the
1112 * msg2_cpt for the purpose of the check that
1113 * follows the call to lnet_pose_send_locked().
1115 if (msg2_cpt != msg->msg_tx_cpt) {
1116 lnet_net_unlock(msg->msg_tx_cpt);
1117 lnet_net_lock(msg2_cpt);
1119 (void) lnet_post_send_locked(msg2, 1);
1120 if (msg2_cpt != msg->msg_tx_cpt) {
1121 lnet_net_unlock(msg2_cpt);
1122 lnet_net_lock(msg->msg_tx_cpt);
1125 spin_unlock(&txpeer->lpni_lock);
1130 msg->msg_txni = NULL;
1131 lnet_ni_decref_locked(txni, msg->msg_tx_cpt);
1134 if (txpeer != NULL) {
1135 msg->msg_txpeer = NULL;
1136 lnet_peer_ni_decref_locked(txpeer);
1141 lnet_schedule_blocked_locked(struct lnet_rtrbufpool *rbp)
1143 struct lnet_msg *msg;
1145 if (list_empty(&rbp->rbp_msgs))
1147 msg = list_entry(rbp->rbp_msgs.next,
1148 struct lnet_msg, msg_list);
1149 list_del(&msg->msg_list);
1151 (void)lnet_post_routed_recv_locked(msg, 1);
1155 lnet_drop_routed_msgs_locked(struct list_head *list, int cpt)
1157 struct lnet_msg *msg;
1158 struct lnet_msg *tmp;
1160 lnet_net_unlock(cpt);
1162 list_for_each_entry_safe(msg, tmp, list, msg_list) {
1163 lnet_ni_recv(msg->msg_rxni, msg->msg_private, NULL,
1164 0, 0, 0, msg->msg_hdr.payload_length);
1165 list_del_init(&msg->msg_list);
1166 msg->msg_no_resend = true;
1167 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_ERROR;
1168 lnet_finalize(msg, -ECANCELED);
1175 lnet_return_rx_credits_locked(struct lnet_msg *msg)
1177 struct lnet_peer_ni *rxpeerni = msg->msg_rxpeer;
1178 struct lnet_peer *lp;
1179 struct lnet_ni *rxni = msg->msg_rxni;
1180 struct lnet_msg *msg2;
1182 if (msg->msg_rtrcredit) {
1183 /* give back global router credits */
1184 struct lnet_rtrbuf *rb;
1185 struct lnet_rtrbufpool *rbp;
1187 /* NB If a msg ever blocks for a buffer in rbp_msgs, it stays
1188 * there until it gets one allocated, or aborts the wait
1190 LASSERT(msg->msg_kiov != NULL);
1192 rb = list_entry(msg->msg_kiov, struct lnet_rtrbuf, rb_kiov[0]);
1195 msg->msg_kiov = NULL;
1196 msg->msg_rtrcredit = 0;
1198 LASSERT(rbp == lnet_msg2bufpool(msg));
1200 LASSERT((rbp->rbp_credits > 0) ==
1201 !list_empty(&rbp->rbp_bufs));
1203 /* If routing is now turned off, we just drop this buffer and
1204 * don't bother trying to return credits. */
1205 if (!the_lnet.ln_routing) {
1206 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1210 /* It is possible that a user has lowered the desired number of
1211 * buffers in this pool. Make sure we never put back
1212 * more buffers than the stated number. */
1213 if (unlikely(rbp->rbp_credits >= rbp->rbp_req_nbuffers)) {
1214 /* Discard this buffer so we don't have too
1216 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1217 rbp->rbp_nbuffers--;
1219 list_add(&rb->rb_list, &rbp->rbp_bufs);
1221 if (rbp->rbp_credits <= 0)
1222 lnet_schedule_blocked_locked(rbp);
1227 if (msg->msg_peerrtrcredit) {
1229 LASSERT(rxpeerni->lpni_peer_net);
1230 LASSERT(rxpeerni->lpni_peer_net->lpn_peer);
1232 /* give back peer router credits */
1233 msg->msg_peerrtrcredit = 0;
1235 spin_lock(&rxpeerni->lpni_lock);
1236 rxpeerni->lpni_rtrcredits++;
1237 spin_unlock(&rxpeerni->lpni_lock);
1239 lp = rxpeerni->lpni_peer_net->lpn_peer;
1240 spin_lock(&lp->lp_lock);
1242 /* drop all messages which are queued to be routed on that
1244 if (!the_lnet.ln_routing) {
1246 list_splice_init(&lp->lp_rtrq, &drop);
1247 spin_unlock(&lp->lp_lock);
1248 lnet_drop_routed_msgs_locked(&drop, msg->msg_rx_cpt);
1249 } else if (!list_empty(&lp->lp_rtrq)) {
1252 msg2 = list_entry(lp->lp_rtrq.next,
1253 struct lnet_msg, msg_list);
1254 list_del(&msg2->msg_list);
1255 msg2_cpt = msg2->msg_rx_cpt;
1256 spin_unlock(&lp->lp_lock);
1258 * messages on the lp_rtrq can be from any NID in
1259 * the peer, which means they might have different
1260 * cpts. We need to make sure we lock the right
1263 if (msg2_cpt != msg->msg_rx_cpt) {
1264 lnet_net_unlock(msg->msg_rx_cpt);
1265 lnet_net_lock(msg2_cpt);
1267 (void) lnet_post_routed_recv_locked(msg2, 1);
1268 if (msg2_cpt != msg->msg_rx_cpt) {
1269 lnet_net_unlock(msg2_cpt);
1270 lnet_net_lock(msg->msg_rx_cpt);
1273 spin_unlock(&lp->lp_lock);
1277 msg->msg_rxni = NULL;
1278 lnet_ni_decref_locked(rxni, msg->msg_rx_cpt);
1280 if (rxpeerni != NULL) {
1281 msg->msg_rxpeer = NULL;
1282 lnet_peer_ni_decref_locked(rxpeerni);
1287 lnet_compare_gw_lpnis(struct lnet_peer_ni *p1, struct lnet_peer_ni *p2)
1289 if (p1->lpni_txqnob < p2->lpni_txqnob)
1292 if (p1->lpni_txqnob > p2->lpni_txqnob)
1295 if (p1->lpni_txcredits > p2->lpni_txcredits)
1298 if (p1->lpni_txcredits < p2->lpni_txcredits)
1304 static struct lnet_peer_ni *
1305 lnet_select_peer_ni(struct lnet_ni *best_ni, lnet_nid_t dst_nid,
1306 struct lnet_peer *peer,
1307 struct lnet_peer_ni *best_lpni,
1308 struct lnet_peer_net *peer_net)
1311 * Look at the peer NIs for the destination peer that connect
1312 * to the chosen net. If a peer_ni is preferred when using the
1313 * best_ni to communicate, we use that one. If there is no
1314 * preferred peer_ni, or there are multiple preferred peer_ni,
1315 * the available transmit credits are used. If the transmit
1316 * credits are equal, we round-robin over the peer_ni.
1318 struct lnet_peer_ni *lpni = NULL;
1319 int best_lpni_credits = (best_lpni) ? best_lpni->lpni_txcredits :
1321 int best_lpni_healthv = (best_lpni) ?
1322 atomic_read(&best_lpni->lpni_healthv) : 0;
1323 bool preferred = false;
1327 while ((lpni = lnet_get_next_peer_ni_locked(peer, peer_net, lpni))) {
1329 * if the best_ni we've chosen aleady has this lpni
1330 * preferred, then let's use it
1333 ni_is_pref = lnet_peer_is_pref_nid_locked(lpni,
1335 CDEBUG(D_NET, "%s ni_is_pref = %d\n",
1336 libcfs_nid2str(best_ni->ni_nid), ni_is_pref);
1341 lpni_healthv = atomic_read(&lpni->lpni_healthv);
1344 CDEBUG(D_NET, "%s c:[%d, %d], s:[%d, %d]\n",
1345 libcfs_nid2str(lpni->lpni_nid),
1346 lpni->lpni_txcredits, best_lpni_credits,
1347 lpni->lpni_seq, best_lpni->lpni_seq);
1349 /* pick the healthiest peer ni */
1350 if (lpni_healthv < best_lpni_healthv) {
1352 } else if (lpni_healthv > best_lpni_healthv) {
1353 best_lpni_healthv = lpni_healthv;
1354 /* if this is a preferred peer use it */
1355 } else if (!preferred && ni_is_pref) {
1357 } else if (preferred && !ni_is_pref) {
1359 * this is not the preferred peer so let's ignore
1363 } else if (lpni->lpni_txcredits < best_lpni_credits) {
1365 * We already have a peer that has more credits
1366 * available than this one. No need to consider
1367 * this peer further.
1370 } else if (lpni->lpni_txcredits == best_lpni_credits) {
1372 * The best peer found so far and the current peer
1373 * have the same number of available credits let's
1374 * make sure to select between them using Round
1378 if (best_lpni->lpni_seq <= lpni->lpni_seq)
1384 best_lpni_credits = lpni->lpni_txcredits;
1387 /* if we still can't find a peer ni then we can't reach it */
1389 __u32 net_id = (peer_net) ? peer_net->lpn_net_id :
1390 LNET_NIDNET(dst_nid);
1391 CDEBUG(D_NET, "no peer_ni found on peer net %s\n",
1392 libcfs_net2str(net_id));
1396 CDEBUG(D_NET, "sd_best_lpni = %s\n",
1397 libcfs_nid2str(best_lpni->lpni_nid));
1403 * Prerequisite: the best_ni should already be set in the sd
1404 * Find the best lpni.
1405 * If the net id is provided then restrict lpni selection on
1406 * that particular net.
1407 * Otherwise find any reachable lpni. When dealing with an MR
1408 * gateway and it has multiple lpnis which we can use
1409 * we want to select the best one from the list of reachable
1412 static inline struct lnet_peer_ni *
1413 lnet_find_best_lpni(struct lnet_ni *lni, lnet_nid_t dst_nid,
1414 struct lnet_peer *peer, __u32 net_id)
1416 struct lnet_peer_net *peer_net;
1417 __u32 any_net = LNET_NIDNET(LNET_NID_ANY);
1419 /* find the best_lpni on any local network */
1420 if (net_id == any_net) {
1421 struct lnet_peer_ni *best_lpni = NULL;
1422 struct lnet_peer_net *lpn;
1423 list_for_each_entry(lpn, &peer->lp_peer_nets, lpn_peer_nets) {
1424 /* no net specified find any reachable peer ni */
1425 if (!lnet_islocalnet_locked(lpn->lpn_net_id))
1427 best_lpni = lnet_select_peer_ni(lni, dst_nid, peer,
1433 /* restrict on the specified net */
1434 peer_net = lnet_peer_get_net_locked(peer, net_id);
1436 return lnet_select_peer_ni(lni, dst_nid, peer, NULL, peer_net);
1441 /* Compare route priorities and hop counts */
1443 lnet_compare_routes(struct lnet_route *r1, struct lnet_route *r2)
1445 int r1_hops = (r1->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r1->lr_hops;
1446 int r2_hops = (r2->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r2->lr_hops;
1448 if (r1->lr_priority < r2->lr_priority)
1451 if (r1->lr_priority > r2->lr_priority)
1454 if (r1_hops < r2_hops)
1457 if (r1_hops > r2_hops)
1463 static struct lnet_route *
1464 lnet_find_route_locked(struct lnet_remotenet *rnet, __u32 src_net,
1465 struct lnet_route **prev_route,
1466 struct lnet_peer_ni **gwni)
1468 struct lnet_peer_ni *lpni, *best_gw_ni = NULL;
1469 struct lnet_route *best_route;
1470 struct lnet_route *last_route;
1471 struct lnet_route *route;
1474 CDEBUG(D_NET, "Looking up a route to %s, from %s\n",
1475 libcfs_net2str(rnet->lrn_net), libcfs_net2str(src_net));
1477 best_route = last_route = NULL;
1478 list_for_each_entry(route, &rnet->lrn_routes, lr_list) {
1479 if (!lnet_is_route_alive(route))
1483 * Restrict the selection of the router NI on the src_net
1484 * provided. If the src_net is LNET_NID_ANY, then select
1485 * the best interface available.
1488 lpni = lnet_find_best_lpni(NULL, LNET_NID_ANY,
1492 best_route = last_route = route;
1495 CDEBUG(D_NET, "Gateway %s does not have a peer NI on net %s\n",
1496 libcfs_nid2str(route->lr_gateway->lp_primary_nid),
1497 libcfs_net2str(src_net));
1503 /* no protection on below fields, but it's harmless */
1504 if (last_route->lr_seq - route->lr_seq < 0)
1507 rc = lnet_compare_routes(route, best_route);
1511 lpni = lnet_find_best_lpni(NULL, LNET_NID_ANY,
1514 /* restrict the lpni on the src_net if specified */
1516 CDEBUG(D_NET, "Gateway %s does not have a peer NI on net %s\n",
1517 libcfs_nid2str(route->lr_gateway->lp_primary_nid),
1518 libcfs_net2str(src_net));
1528 rc = lnet_compare_gw_lpnis(lpni, best_gw_ni);
1532 if (rc == 1 || route->lr_seq <= best_route->lr_seq) {
1539 *prev_route = last_route;
1545 static struct lnet_ni *
1546 lnet_get_best_ni(struct lnet_net *local_net, struct lnet_ni *best_ni,
1547 struct lnet_peer *peer, struct lnet_peer_net *peer_net,
1550 struct lnet_ni *ni = NULL;
1551 unsigned int shortest_distance;
1556 * If there is no peer_ni that we can send to on this network,
1557 * then there is no point in looking for a new best_ni here.
1559 if (!lnet_get_next_peer_ni_locked(peer, peer_net, NULL))
1562 if (best_ni == NULL) {
1563 shortest_distance = UINT_MAX;
1564 best_credits = INT_MIN;
1567 shortest_distance = cfs_cpt_distance(lnet_cpt_table(), md_cpt,
1568 best_ni->ni_dev_cpt);
1569 best_credits = atomic_read(&best_ni->ni_tx_credits);
1570 best_healthv = atomic_read(&best_ni->ni_healthv);
1573 while ((ni = lnet_get_next_ni_locked(local_net, ni))) {
1574 unsigned int distance;
1579 ni_credits = atomic_read(&ni->ni_tx_credits);
1580 ni_healthv = atomic_read(&ni->ni_healthv);
1581 ni_fatal = atomic_read(&ni->ni_fatal_error_on);
1584 * calculate the distance from the CPT on which
1585 * the message memory is allocated to the CPT of
1586 * the NI's physical device
1588 distance = cfs_cpt_distance(lnet_cpt_table(),
1592 CDEBUG(D_NET, "compare ni %s [c:%d, d:%d, s:%d] with best_ni %s [c:%d, d:%d, s:%d]\n",
1593 libcfs_nid2str(ni->ni_nid), ni_credits, distance,
1594 ni->ni_seq, (best_ni) ? libcfs_nid2str(best_ni->ni_nid)
1595 : "not seleced", best_credits, shortest_distance,
1596 (best_ni) ? best_ni->ni_seq : 0);
1599 * All distances smaller than the NUMA range
1600 * are treated equally.
1602 if (distance < lnet_numa_range)
1603 distance = lnet_numa_range;
1606 * Select on health, shorter distance, available
1607 * credits, then round-robin.
1611 } else if (ni_healthv < best_healthv) {
1613 } else if (ni_healthv > best_healthv) {
1614 best_healthv = ni_healthv;
1616 * If we're going to prefer this ni because it's
1617 * the healthiest, then we should set the
1618 * shortest_distance in the algorithm in case
1619 * there are multiple NIs with the same health but
1620 * different distances.
1622 if (distance < shortest_distance)
1623 shortest_distance = distance;
1624 } else if (distance > shortest_distance) {
1626 } else if (distance < shortest_distance) {
1627 shortest_distance = distance;
1628 } else if (ni_credits < best_credits) {
1630 } else if (ni_credits == best_credits) {
1631 if (best_ni && best_ni->ni_seq <= ni->ni_seq)
1635 best_credits = ni_credits;
1638 CDEBUG(D_NET, "selected best_ni %s\n",
1639 (best_ni) ? libcfs_nid2str(best_ni->ni_nid) : "no selection");
1645 * Traffic to the LNET_RESERVED_PORTAL may not trigger peer discovery,
1646 * because such traffic is required to perform discovery. We therefore
1647 * exclude all GET and PUT on that portal. We also exclude all ACK and
1648 * REPLY traffic, but that is because the portal is not tracked in the
1649 * message structure for these message types. We could restrict this
1650 * further by also checking for LNET_PROTO_PING_MATCHBITS.
1653 lnet_msg_discovery(struct lnet_msg *msg)
1655 if (msg->msg_type == LNET_MSG_PUT) {
1656 if (msg->msg_hdr.msg.put.ptl_index != LNET_RESERVED_PORTAL)
1658 } else if (msg->msg_type == LNET_MSG_GET) {
1659 if (msg->msg_hdr.msg.get.ptl_index != LNET_RESERVED_PORTAL)
1665 #define SRC_SPEC 0x0001
1666 #define SRC_ANY 0x0002
1667 #define LOCAL_DST 0x0004
1668 #define REMOTE_DST 0x0008
1669 #define MR_DST 0x0010
1670 #define NMR_DST 0x0020
1671 #define SND_RESP 0x0040
1673 /* The following to defines are used for return codes */
1674 #define REPEAT_SEND 0x1000
1675 #define PASS_THROUGH 0x2000
1677 /* The different cases lnet_select pathway needs to handle */
1678 #define SRC_SPEC_LOCAL_MR_DST (SRC_SPEC | LOCAL_DST | MR_DST)
1679 #define SRC_SPEC_ROUTER_MR_DST (SRC_SPEC | REMOTE_DST | MR_DST)
1680 #define SRC_SPEC_LOCAL_NMR_DST (SRC_SPEC | LOCAL_DST | NMR_DST)
1681 #define SRC_SPEC_ROUTER_NMR_DST (SRC_SPEC | REMOTE_DST | NMR_DST)
1682 #define SRC_ANY_LOCAL_MR_DST (SRC_ANY | LOCAL_DST | MR_DST)
1683 #define SRC_ANY_ROUTER_MR_DST (SRC_ANY | REMOTE_DST | MR_DST)
1684 #define SRC_ANY_LOCAL_NMR_DST (SRC_ANY | LOCAL_DST | NMR_DST)
1685 #define SRC_ANY_ROUTER_NMR_DST (SRC_ANY | REMOTE_DST | NMR_DST)
1688 lnet_handle_lo_send(struct lnet_send_data *sd)
1690 struct lnet_msg *msg = sd->sd_msg;
1691 int cpt = sd->sd_cpt;
1693 /* No send credit hassles with LOLND */
1694 lnet_ni_addref_locked(the_lnet.ln_loni, cpt);
1695 msg->msg_hdr.dest_nid = cpu_to_le64(the_lnet.ln_loni->ni_nid);
1696 if (!msg->msg_routing)
1697 msg->msg_hdr.src_nid =
1698 cpu_to_le64(the_lnet.ln_loni->ni_nid);
1699 msg->msg_target.nid = the_lnet.ln_loni->ni_nid;
1700 lnet_msg_commit(msg, cpt);
1701 msg->msg_txni = the_lnet.ln_loni;
1703 return LNET_CREDIT_OK;
1707 lnet_handle_send(struct lnet_send_data *sd)
1709 struct lnet_ni *best_ni = sd->sd_best_ni;
1710 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
1711 struct lnet_peer_ni *final_dst_lpni = sd->sd_final_dst_lpni;
1712 struct lnet_msg *msg = sd->sd_msg;
1714 __u32 send_case = sd->sd_send_case;
1716 __u32 routing = send_case & REMOTE_DST;
1717 struct lnet_rsp_tracker *rspt;
1720 * Increment sequence number of the selected peer so that we
1721 * pick the next one in Round Robin.
1723 best_lpni->lpni_seq++;
1726 * grab a reference on the peer_ni so it sticks around even if
1727 * we need to drop and relock the lnet_net_lock below.
1729 lnet_peer_ni_addref_locked(best_lpni);
1732 * Use lnet_cpt_of_nid() to determine the CPT used to commit the
1733 * message. This ensures that we get a CPT that is correct for
1734 * the NI when the NI has been restricted to a subset of all CPTs.
1735 * If the selected CPT differs from the one currently locked, we
1736 * must unlock and relock the lnet_net_lock(), and then check whether
1737 * the configuration has changed. We don't have a hold on the best_ni
1738 * yet, and it may have vanished.
1740 cpt2 = lnet_cpt_of_nid_locked(best_lpni->lpni_nid, best_ni);
1741 if (sd->sd_cpt != cpt2) {
1742 __u32 seq = lnet_get_dlc_seq_locked();
1743 lnet_net_unlock(sd->sd_cpt);
1745 lnet_net_lock(sd->sd_cpt);
1746 if (seq != lnet_get_dlc_seq_locked()) {
1747 lnet_peer_ni_decref_locked(best_lpni);
1753 * store the best_lpni in the message right away to avoid having
1754 * to do the same operation under different conditions
1756 msg->msg_txpeer = best_lpni;
1757 msg->msg_txni = best_ni;
1760 * grab a reference for the best_ni since now it's in use in this
1761 * send. The reference will be dropped in lnet_finalize()
1763 lnet_ni_addref_locked(msg->msg_txni, sd->sd_cpt);
1766 * Always set the target.nid to the best peer picked. Either the
1767 * NID will be one of the peer NIDs selected, or the same NID as
1768 * what was originally set in the target or it will be the NID of
1769 * a router if this message should be routed
1771 msg->msg_target.nid = msg->msg_txpeer->lpni_nid;
1774 * lnet_msg_commit assigns the correct cpt to the message, which
1775 * is used to decrement the correct refcount on the ni when it's
1776 * time to return the credits
1778 lnet_msg_commit(msg, sd->sd_cpt);
1781 * If we are routing the message then we keep the src_nid that was
1782 * set by the originator. If we are not routing then we are the
1783 * originator and set it here.
1785 if (!msg->msg_routing)
1786 msg->msg_hdr.src_nid = cpu_to_le64(msg->msg_txni->ni_nid);
1789 msg->msg_target_is_router = 1;
1790 msg->msg_target.pid = LNET_PID_LUSTRE;
1792 * since we're routing we want to ensure that the
1793 * msg_hdr.dest_nid is set to the final destination. When
1794 * the router receives this message it knows how to route
1797 * final_dst_lpni is set at the beginning of the
1798 * lnet_select_pathway() function and is never changed.
1799 * It's safe to use it here.
1801 msg->msg_hdr.dest_nid = cpu_to_le64(final_dst_lpni->lpni_nid);
1804 * if we're not routing set the dest_nid to the best peer
1805 * ni NID that we picked earlier in the algorithm.
1807 msg->msg_hdr.dest_nid = cpu_to_le64(msg->msg_txpeer->lpni_nid);
1811 * if we have response tracker block update it with the next hop
1815 rspt = msg->msg_md->md_rspt_ptr;
1817 rspt->rspt_next_hop_nid = msg->msg_txpeer->lpni_nid;
1818 CDEBUG(D_NET, "rspt_next_hop_nid = %s\n",
1819 libcfs_nid2str(rspt->rspt_next_hop_nid));
1823 rc = lnet_post_send_locked(msg, 0);
1826 CDEBUG(D_NET, "TRACE: %s(%s:%s) -> %s(%s:%s) %s : %s try# %d\n",
1827 libcfs_nid2str(msg->msg_hdr.src_nid),
1828 libcfs_nid2str(msg->msg_txni->ni_nid),
1829 libcfs_nid2str(sd->sd_src_nid),
1830 libcfs_nid2str(msg->msg_hdr.dest_nid),
1831 libcfs_nid2str(sd->sd_dst_nid),
1832 libcfs_nid2str(msg->msg_txpeer->lpni_nid),
1833 libcfs_nid2str(sd->sd_rtr_nid),
1834 lnet_msgtyp2str(msg->msg_type), msg->msg_retry_count);
1840 lnet_set_non_mr_pref_nid(struct lnet_peer_ni *lpni, struct lnet_ni *lni,
1841 struct lnet_msg *msg)
1843 if (!lnet_peer_is_multi_rail(lpni->lpni_peer_net->lpn_peer) &&
1844 !lnet_msg_is_response(msg) && lpni->lpni_pref_nnids == 0) {
1845 CDEBUG(D_NET, "Setting preferred local NID %s on NMR peer %s\n",
1846 libcfs_nid2str(lni->ni_nid),
1847 libcfs_nid2str(lpni->lpni_nid));
1848 lnet_peer_ni_set_non_mr_pref_nid(lpni, lni->ni_nid);
1857 * use the source and destination NIDs as the pathway
1860 lnet_handle_spec_local_nmr_dst(struct lnet_send_data *sd)
1862 /* the destination lpni is set before we get here. */
1865 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
1866 if (!sd->sd_best_ni) {
1867 CERROR("Can't send to %s: src %s is not a "
1868 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
1869 libcfs_nid2str(sd->sd_src_nid));
1873 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
1875 return lnet_handle_send(sd);
1883 * Don't run the selection algorithm on the peer NIs. By specifying the
1884 * local NID, we're also saying that we should always use the destination NID
1885 * provided. This handles the case where we should be using the same
1886 * destination NID for the all the messages which belong to the same RPC
1890 lnet_handle_spec_local_mr_dst(struct lnet_send_data *sd)
1892 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
1893 if (!sd->sd_best_ni) {
1894 CERROR("Can't send to %s: src %s is not a "
1895 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
1896 libcfs_nid2str(sd->sd_src_nid));
1900 if (sd->sd_best_lpni &&
1901 sd->sd_best_lpni->lpni_nid == the_lnet.ln_loni->ni_nid)
1902 return lnet_handle_lo_send(sd);
1903 else if (sd->sd_best_lpni)
1904 return lnet_handle_send(sd);
1906 CERROR("can't send to %s. no NI on %s\n",
1907 libcfs_nid2str(sd->sd_dst_nid),
1908 libcfs_net2str(sd->sd_best_ni->ni_net->net_id));
1910 return -EHOSTUNREACH;
1914 lnet_find_best_ni_on_spec_net(struct lnet_ni *cur_best_ni,
1915 struct lnet_peer *peer,
1916 struct lnet_peer_net *peer_net,
1920 struct lnet_net *local_net;
1921 struct lnet_ni *best_ni;
1923 local_net = lnet_get_net_locked(peer_net->lpn_net_id);
1928 * Iterate through the NIs in this local Net and select
1929 * the NI to send from. The selection is determined by
1930 * these 3 criterion in the following priority:
1932 * 2. NI available credits
1935 best_ni = lnet_get_best_ni(local_net, cur_best_ni,
1936 peer, peer_net, cpt);
1938 if (incr_seq && best_ni)
1945 lnet_initiate_peer_discovery(struct lnet_peer_ni *lpni, struct lnet_msg *msg,
1948 struct lnet_peer *peer;
1951 lnet_peer_ni_addref_locked(lpni);
1953 peer = lpni->lpni_peer_net->lpn_peer;
1955 if (lnet_peer_gw_discovery(peer)) {
1956 lnet_peer_ni_decref_locked(lpni);
1960 if (!lnet_msg_discovery(msg) || lnet_peer_is_uptodate(peer)) {
1961 lnet_peer_ni_decref_locked(lpni);
1965 rc = lnet_discover_peer_locked(lpni, cpt, false);
1967 lnet_peer_ni_decref_locked(lpni);
1970 /* The peer may have changed. */
1971 peer = lpni->lpni_peer_net->lpn_peer;
1972 spin_lock(&peer->lp_lock);
1973 if (lnet_peer_is_uptodate_locked(peer)) {
1974 spin_unlock(&peer->lp_lock);
1975 lnet_peer_ni_decref_locked(lpni);
1978 /* queue message and return */
1979 msg->msg_sending = 0;
1980 msg->msg_txpeer = NULL;
1981 list_add_tail(&msg->msg_list, &peer->lp_dc_pendq);
1982 spin_unlock(&peer->lp_lock);
1984 lnet_peer_ni_decref_locked(lpni);
1986 CDEBUG(D_NET, "msg %p delayed. %s pending discovery\n",
1987 msg, libcfs_nid2str(peer->lp_primary_nid));
1989 return LNET_DC_WAIT;
1993 lnet_handle_find_routed_path(struct lnet_send_data *sd,
1995 struct lnet_peer_ni **gw_lpni,
1996 struct lnet_peer **gw_peer)
2000 struct lnet_peer *gw;
2001 struct lnet_peer *lp;
2002 struct lnet_peer_net *lpn;
2003 struct lnet_peer_net *best_lpn = NULL;
2004 struct lnet_remotenet *rnet, *best_rnet = NULL;
2005 struct lnet_route *best_route = NULL;
2006 struct lnet_route *last_route = NULL;
2007 struct lnet_peer_ni *lpni = NULL;
2008 struct lnet_peer_ni *gwni = NULL;
2009 bool route_found = false;
2010 lnet_nid_t src_nid = (sd->sd_src_nid != LNET_NID_ANY) ? sd->sd_src_nid :
2011 (sd->sd_best_ni != NULL) ? sd->sd_best_ni->ni_nid :
2014 CDEBUG(D_NET, "using src nid %s for route restriction\n",
2015 libcfs_nid2str(src_nid));
2017 /* If a router nid was specified then we are replying to a GET or
2018 * sending an ACK. In this case we use the gateway associated with the
2019 * specified router nid.
2021 if (sd->sd_rtr_nid != LNET_NID_ANY) {
2022 gwni = lnet_find_peer_ni_locked(sd->sd_rtr_nid);
2024 gw = gwni->lpni_peer_net->lpn_peer;
2025 lnet_peer_ni_decref_locked(gwni);
2026 if (gw->lp_rtr_refcount) {
2027 local_lnet = LNET_NIDNET(sd->sd_rtr_nid);
2031 CWARN("No peer NI for gateway %s. Attempting to find an alternative route.\n",
2032 libcfs_nid2str(sd->sd_rtr_nid));
2037 /* we've already looked up the initial lpni using dst_nid */
2038 lpni = sd->sd_best_lpni;
2039 /* the peer tree must be in existence */
2040 LASSERT(lpni && lpni->lpni_peer_net &&
2041 lpni->lpni_peer_net->lpn_peer);
2042 lp = lpni->lpni_peer_net->lpn_peer;
2044 list_for_each_entry(lpn, &lp->lp_peer_nets, lpn_peer_nets) {
2045 /* is this remote network reachable? */
2046 rnet = lnet_find_rnet_locked(lpn->lpn_net_id);
2055 if (best_lpn->lpn_seq <= lpn->lpn_seq)
2063 CERROR("peer %s has no available nets\n",
2064 libcfs_nid2str(sd->sd_dst_nid));
2065 return -EHOSTUNREACH;
2068 sd->sd_best_lpni = lnet_find_best_lpni(sd->sd_best_ni,
2071 best_lpn->lpn_net_id);
2072 if (!sd->sd_best_lpni) {
2073 CERROR("peer %s is unreachable\n",
2074 libcfs_nid2str(sd->sd_dst_nid));
2075 return -EHOSTUNREACH;
2079 * We're attempting to round robin over the remote peer
2080 * NI's so update the final destination we selected
2082 sd->sd_final_dst_lpni = sd->sd_best_lpni;
2085 * find the best route. Restrict the selection on the net of the
2086 * local NI if we've already picked the local NI to send from.
2087 * Otherwise, let's pick any route we can find and then find
2088 * a local NI we can reach the route's gateway on. Any route we select
2089 * will be reachable by virtue of the restriction we have when
2092 best_route = lnet_find_route_locked(best_rnet,
2093 LNET_NIDNET(src_nid),
2094 &last_route, &gwni);
2097 CERROR("no route to %s from %s\n",
2098 libcfs_nid2str(dst_nid),
2099 libcfs_nid2str(src_nid));
2100 return -EHOSTUNREACH;
2104 CERROR("Internal Error. Route expected to %s from %s\n",
2105 libcfs_nid2str(dst_nid),
2106 libcfs_nid2str(src_nid));
2110 gw = best_route->lr_gateway;
2111 LASSERT(gw == gwni->lpni_peer_net->lpn_peer);
2112 local_lnet = best_route->lr_lnet;
2115 * Increment the sequence number of the remote lpni so we
2116 * can round robin over the different interfaces of the
2119 sd->sd_best_lpni->lpni_seq++;
2123 * Discover this gateway if it hasn't already been discovered.
2124 * This means we might delay the message until discovery has
2127 sd->sd_msg->msg_src_nid_param = sd->sd_src_nid;
2128 rc = lnet_initiate_peer_discovery(gwni, sd->sd_msg, sd->sd_cpt);
2132 if (!sd->sd_best_ni)
2133 sd->sd_best_ni = lnet_find_best_ni_on_spec_net(NULL, gw,
2134 lnet_peer_get_net_locked(gw,
2139 if (!sd->sd_best_ni) {
2140 CERROR("Internal Error. Expected local ni on %s but non found :%s\n",
2141 libcfs_net2str(local_lnet),
2142 libcfs_nid2str(sd->sd_src_nid));
2150 * increment the sequence numbers since now we're sure we're
2151 * going to use this path
2153 if (sd->sd_rtr_nid == LNET_NID_ANY) {
2154 LASSERT(best_route && last_route);
2155 best_route->lr_seq = last_route->lr_seq + 1;
2156 best_lpn->lpn_seq++;
2167 * Remote destination
2168 * Non-MR destination
2172 * Remote destination
2175 * The handling of these two cases is similar. Even though the destination
2176 * can be MR or non-MR, we'll deal directly with the router.
2179 lnet_handle_spec_router_dst(struct lnet_send_data *sd)
2182 struct lnet_peer_ni *gw_lpni = NULL;
2183 struct lnet_peer *gw_peer = NULL;
2186 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
2187 if (!sd->sd_best_ni) {
2188 CERROR("Can't send to %s: src %s is not a "
2189 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
2190 libcfs_nid2str(sd->sd_src_nid));
2194 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2199 if (sd->sd_send_case & NMR_DST)
2201 * since the final destination is non-MR let's set its preferred
2202 * NID before we send
2204 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni,
2208 * We're going to send to the gw found so let's set its
2211 sd->sd_peer = gw_peer;
2212 sd->sd_best_lpni = gw_lpni;
2214 return lnet_handle_send(sd);
2218 lnet_find_best_ni_on_local_net(struct lnet_peer *peer, int md_cpt,
2221 struct lnet_peer_net *peer_net = NULL;
2222 struct lnet_ni *best_ni = NULL;
2223 int lpn_healthv = 0;
2226 * The peer can have multiple interfaces, some of them can be on
2227 * the local network and others on a routed network. We should
2228 * prefer the local network. However if the local network is not
2229 * available then we need to try the routed network
2232 /* go through all the peer nets and find the best_ni */
2233 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_peer_nets) {
2235 * The peer's list of nets can contain non-local nets. We
2236 * want to only examine the local ones.
2238 if (!lnet_get_net_locked(peer_net->lpn_net_id))
2241 /* always select the lpn with the best health */
2242 if (lpn_healthv <= peer_net->lpn_healthv)
2243 lpn_healthv = peer_net->lpn_healthv;
2247 best_ni = lnet_find_best_ni_on_spec_net(best_ni, peer, peer_net,
2251 * if this is a discovery message and lp_disc_net_id is
2252 * specified then use that net to send the discovery on.
2254 if (peer->lp_disc_net_id == peer_net->lpn_net_id &&
2260 /* increment sequence number so we can round robin */
2266 static struct lnet_ni *
2267 lnet_find_existing_preferred_best_ni(struct lnet_peer_ni *lpni, int cpt)
2269 struct lnet_ni *best_ni = NULL;
2270 struct lnet_peer_net *peer_net = lpni->lpni_peer_net;
2271 struct lnet_peer_ni *lpni_entry;
2274 * We must use a consistent source address when sending to a
2275 * non-MR peer. However, a non-MR peer can have multiple NIDs
2276 * on multiple networks, and we may even need to talk to this
2277 * peer on multiple networks -- certain types of
2278 * load-balancing configuration do this.
2280 * So we need to pick the NI the peer prefers for this
2281 * particular network.
2284 list_for_each_entry(lpni_entry, &peer_net->lpn_peer_nis,
2286 if (lpni_entry->lpni_pref_nnids == 0)
2288 LASSERT(lpni_entry->lpni_pref_nnids == 1);
2289 best_ni = lnet_nid2ni_locked(lpni_entry->lpni_pref.nid, cpt);
2296 /* Prerequisite: sd->sd_peer and sd->sd_best_lpni should be set */
2298 lnet_select_preferred_best_ni(struct lnet_send_data *sd)
2300 struct lnet_ni *best_ni = NULL;
2301 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
2304 * We must use a consistent source address when sending to a
2305 * non-MR peer. However, a non-MR peer can have multiple NIDs
2306 * on multiple networks, and we may even need to talk to this
2307 * peer on multiple networks -- certain types of
2308 * load-balancing configuration do this.
2310 * So we need to pick the NI the peer prefers for this
2311 * particular network.
2314 best_ni = lnet_find_existing_preferred_best_ni(sd->sd_best_lpni,
2317 /* if best_ni is still not set just pick one */
2320 lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2321 sd->sd_best_lpni->lpni_peer_net,
2322 sd->sd_md_cpt, true);
2323 /* If there is no best_ni we don't have a route */
2325 CERROR("no path to %s from net %s\n",
2326 libcfs_nid2str(best_lpni->lpni_nid),
2327 libcfs_net2str(best_lpni->lpni_net->net_id));
2328 return -EHOSTUNREACH;
2332 sd->sd_best_ni = best_ni;
2334 /* Set preferred NI if necessary. */
2335 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
2342 * Source not specified
2346 * always use the same source NID for NMR peers
2347 * If we've talked to that peer before then we already have a preferred
2348 * source NI associated with it. Otherwise, we select a preferred local NI
2349 * and store it in the peer
2352 lnet_handle_any_local_nmr_dst(struct lnet_send_data *sd)
2356 /* sd->sd_best_lpni is already set to the final destination */
2359 * At this point we should've created the peer ni and peer. If we
2360 * can't find it, then something went wrong. Instead of assert
2361 * output a relevant message and fail the send
2363 if (!sd->sd_best_lpni) {
2364 CERROR("Internal fault. Unable to send msg %s to %s. "
2366 lnet_msgtyp2str(sd->sd_msg->msg_type),
2367 libcfs_nid2str(sd->sd_dst_nid));
2371 if (sd->sd_msg->msg_routing) {
2372 /* If I'm forwarding this message then I can choose any NI
2373 * on the destination peer net
2375 sd->sd_best_ni = lnet_find_best_ni_on_spec_net(NULL,
2377 sd->sd_best_lpni->lpni_peer_net,
2380 if (!sd->sd_best_ni) {
2381 CERROR("Unable to forward message to %s. No local NI available\n",
2382 libcfs_nid2str(sd->sd_dst_nid));
2386 rc = lnet_select_preferred_best_ni(sd);
2389 rc = lnet_handle_send(sd);
2395 lnet_handle_any_mr_dsta(struct lnet_send_data *sd)
2398 * NOTE we've already handled the remote peer case. So we only
2399 * need to worry about the local case here.
2401 * if we're sending a response, ACK or reply, we need to send it
2402 * to the destination NID given to us. At this point we already
2403 * have the peer_ni we're suppose to send to, so just find the
2404 * best_ni on the peer net and use that. Since we're sending to an
2405 * MR peer then we can just run the selection algorithm on our
2406 * local NIs and pick the best one.
2408 if (sd->sd_send_case & SND_RESP) {
2410 lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2411 sd->sd_best_lpni->lpni_peer_net,
2412 sd->sd_md_cpt, true);
2414 if (!sd->sd_best_ni) {
2416 * We're not going to deal with not able to send
2417 * a response to the provided final destination
2419 CERROR("Can't send response to %s. "
2420 "No local NI available\n",
2421 libcfs_nid2str(sd->sd_dst_nid));
2422 return -EHOSTUNREACH;
2425 return lnet_handle_send(sd);
2429 * If we get here that means we're sending a fresh request, PUT or
2430 * GET, so we need to run our standard selection algorithm.
2431 * First find the best local interface that's on any of the peer's
2434 sd->sd_best_ni = lnet_find_best_ni_on_local_net(sd->sd_peer,
2436 lnet_msg_discovery(sd->sd_msg));
2437 if (sd->sd_best_ni) {
2439 lnet_find_best_lpni(sd->sd_best_ni, sd->sd_dst_nid,
2441 sd->sd_best_ni->ni_net->net_id);
2444 * if we're successful in selecting a peer_ni on the local
2445 * network, then send to it. Otherwise fall through and
2446 * try and see if we can reach it over another routed
2449 if (sd->sd_best_lpni &&
2450 sd->sd_best_lpni->lpni_nid == the_lnet.ln_loni->ni_nid) {
2452 * in case we initially started with a routed
2453 * destination, let's reset to local
2455 sd->sd_send_case &= ~REMOTE_DST;
2456 sd->sd_send_case |= LOCAL_DST;
2457 return lnet_handle_lo_send(sd);
2458 } else if (sd->sd_best_lpni) {
2460 * in case we initially started with a routed
2461 * destination, let's reset to local
2463 sd->sd_send_case &= ~REMOTE_DST;
2464 sd->sd_send_case |= LOCAL_DST;
2465 return lnet_handle_send(sd);
2468 CERROR("Internal Error. Expected to have a best_lpni: "
2470 libcfs_nid2str(sd->sd_src_nid),
2471 libcfs_nid2str(sd->sd_dst_nid));
2477 * Peer doesn't have a local network. Let's see if there is
2478 * a remote network we can reach it on.
2480 return PASS_THROUGH;
2485 * Source NID not specified
2490 * Source NID not speified
2491 * Remote destination
2494 * In both of these cases if we're sending a response, ACK or REPLY, then
2495 * we need to send to the destination NID provided.
2497 * In the remote case let's deal with MR routers.
2502 lnet_handle_any_mr_dst(struct lnet_send_data *sd)
2505 struct lnet_peer *gw_peer = NULL;
2506 struct lnet_peer_ni *gw_lpni = NULL;
2509 * handle sending a response to a remote peer here so we don't
2510 * have to worry about it if we hit lnet_handle_any_mr_dsta()
2512 if (sd->sd_send_case & REMOTE_DST &&
2513 sd->sd_send_case & SND_RESP) {
2514 struct lnet_peer_ni *gw;
2515 struct lnet_peer *gw_peer;
2517 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw,
2520 CERROR("Can't send response to %s. "
2521 "No route available\n",
2522 libcfs_nid2str(sd->sd_dst_nid));
2523 return -EHOSTUNREACH;
2524 } else if (rc > 0) {
2528 sd->sd_best_lpni = gw;
2529 sd->sd_peer = gw_peer;
2531 return lnet_handle_send(sd);
2535 * Even though the NID for the peer might not be on a local network,
2536 * since the peer is MR there could be other interfaces on the
2537 * local network. In that case we'd still like to prefer the local
2538 * network over the routed network. If we're unable to do that
2539 * then we select the best router among the different routed networks,
2540 * and if the router is MR then we can deal with it as such.
2542 rc = lnet_handle_any_mr_dsta(sd);
2543 if (rc != PASS_THROUGH)
2547 * Now that we must route to the destination, we must consider the
2548 * MR case, where the destination has multiple interfaces, some of
2549 * which we can route to and others we do not. For this reason we
2550 * need to select the destination which we can route to and if
2551 * there are multiple, we need to round robin.
2553 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2558 sd->sd_send_case &= ~LOCAL_DST;
2559 sd->sd_send_case |= REMOTE_DST;
2561 sd->sd_peer = gw_peer;
2562 sd->sd_best_lpni = gw_lpni;
2564 return lnet_handle_send(sd);
2568 * Source not specified
2569 * Remote destination
2572 * Must send to the specified peer NID using the same source NID that
2573 * we've used before. If it's the first time to talk to that peer then
2574 * find the source NI and assign it as preferred to that peer
2577 lnet_handle_any_router_nmr_dst(struct lnet_send_data *sd)
2580 struct lnet_peer_ni *gw_lpni = NULL;
2581 struct lnet_peer *gw_peer = NULL;
2584 * Let's see if we have a preferred NI to talk to this NMR peer
2586 sd->sd_best_ni = lnet_find_existing_preferred_best_ni(sd->sd_best_lpni,
2590 * find the router and that'll find the best NI if we didn't find
2593 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2599 * set the best_ni we've chosen as the preferred one for
2602 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
2604 /* we'll be sending to the gw */
2605 sd->sd_best_lpni = gw_lpni;
2606 sd->sd_peer = gw_peer;
2608 return lnet_handle_send(sd);
2612 lnet_handle_send_case_locked(struct lnet_send_data *sd)
2615 * turn off the SND_RESP bit.
2616 * It will be checked in the case handling
2618 __u32 send_case = sd->sd_send_case &= ~SND_RESP ;
2620 CDEBUG(D_NET, "Source %s%s to %s %s %s destination\n",
2621 (send_case & SRC_SPEC) ? "Specified: " : "ANY",
2622 (send_case & SRC_SPEC) ? libcfs_nid2str(sd->sd_src_nid) : "",
2623 (send_case & MR_DST) ? "MR: " : "NMR: ",
2624 libcfs_nid2str(sd->sd_dst_nid),
2625 (send_case & LOCAL_DST) ? "local" : "routed");
2627 switch (send_case) {
2629 * For all cases where the source is specified, we should always
2630 * use the destination NID, whether it's an MR destination or not,
2631 * since we're continuing a series of related messages for the
2634 case SRC_SPEC_LOCAL_NMR_DST:
2635 return lnet_handle_spec_local_nmr_dst(sd);
2636 case SRC_SPEC_LOCAL_MR_DST:
2637 return lnet_handle_spec_local_mr_dst(sd);
2638 case SRC_SPEC_ROUTER_NMR_DST:
2639 case SRC_SPEC_ROUTER_MR_DST:
2640 return lnet_handle_spec_router_dst(sd);
2641 case SRC_ANY_LOCAL_NMR_DST:
2642 return lnet_handle_any_local_nmr_dst(sd);
2643 case SRC_ANY_LOCAL_MR_DST:
2644 case SRC_ANY_ROUTER_MR_DST:
2645 return lnet_handle_any_mr_dst(sd);
2646 case SRC_ANY_ROUTER_NMR_DST:
2647 return lnet_handle_any_router_nmr_dst(sd);
2649 CERROR("Unknown send case\n");
2655 lnet_select_pathway(lnet_nid_t src_nid, lnet_nid_t dst_nid,
2656 struct lnet_msg *msg, lnet_nid_t rtr_nid)
2658 struct lnet_peer_ni *lpni;
2659 struct lnet_peer *peer;
2660 struct lnet_send_data send_data;
2663 __u32 send_case = 0;
2665 bool mr_forwarding_allowed;
2667 memset(&send_data, 0, sizeof(send_data));
2670 * get an initial CPT to use for locking. The idea here is not to
2671 * serialize the calls to select_pathway, so that as many
2672 * operations can run concurrently as possible. To do that we use
2673 * the CPT where this call is being executed. Later on when we
2674 * determine the CPT to use in lnet_message_commit, we switch the
2675 * lock and check if there was any configuration change. If none,
2676 * then we proceed, if there is, then we restart the operation.
2678 cpt = lnet_net_lock_current();
2680 md_cpt = lnet_cpt_of_md(msg->msg_md, msg->msg_offset);
2681 if (md_cpt == CFS_CPT_ANY)
2687 * If we're being asked to send to the loopback interface, there
2688 * is no need to go through any selection. We can just shortcut
2689 * the entire process and send over lolnd
2691 send_data.sd_msg = msg;
2692 send_data.sd_cpt = cpt;
2693 if (dst_nid == LNET_NID_LO_0) {
2694 rc = lnet_handle_lo_send(&send_data);
2695 lnet_net_unlock(cpt);
2700 * find an existing peer_ni, or create one and mark it as having been
2701 * created due to network traffic. This call will create the
2702 * peer->peer_net->peer_ni tree.
2704 lpni = lnet_nid2peerni_locked(dst_nid, LNET_NID_ANY, cpt);
2706 lnet_net_unlock(cpt);
2707 return PTR_ERR(lpni);
2711 * Cache the original src_nid and rtr_nid. If we need to resend the
2712 * message then we'll need to know whether the src_nid was originally
2713 * specified for this message. If it was originally specified,
2714 * then we need to keep using the same src_nid since it's
2715 * continuing the same sequence of messages. Similarly, rtr_nid will
2716 * affect our choice of next hop.
2718 msg->msg_src_nid_param = src_nid;
2719 msg->msg_rtr_nid_param = rtr_nid;
2722 * If necessary, perform discovery on the peer that owns this peer_ni.
2723 * Note, this can result in the ownership of this peer_ni changing
2724 * to another peer object.
2726 rc = lnet_initiate_peer_discovery(lpni, msg, cpt);
2728 lnet_peer_ni_decref_locked(lpni);
2729 lnet_net_unlock(cpt);
2732 lnet_peer_ni_decref_locked(lpni);
2734 peer = lpni->lpni_peer_net->lpn_peer;
2737 * Identify the different send cases
2739 if (src_nid == LNET_NID_ANY)
2740 send_case |= SRC_ANY;
2742 send_case |= SRC_SPEC;
2744 if (lnet_get_net_locked(LNET_NIDNET(dst_nid)))
2745 send_case |= LOCAL_DST;
2747 send_case |= REMOTE_DST;
2750 if (msg->msg_routing && (send_case & LOCAL_DST))
2753 /* Determine whether to allow MR forwarding for this message.
2754 * NB: MR forwarding is allowed if the message originator and the
2755 * destination are both MR capable, and the destination lpni that was
2756 * originally chosen by the originator is unhealthy or down.
2757 * We check the MR capability of the destination further below
2759 mr_forwarding_allowed = false;
2761 struct lnet_peer *src_lp;
2762 struct lnet_peer_ni *src_lpni;
2764 src_lpni = lnet_nid2peerni_locked(msg->msg_hdr.src_nid,
2766 /* We don't fail the send if we hit any errors here. We'll just
2767 * try to send it via non-multi-rail criteria
2769 if (!IS_ERR(src_lpni)) {
2770 /* Drop ref taken by lnet_nid2peerni_locked() */
2771 lnet_peer_ni_decref_locked(src_lpni);
2772 src_lp = lpni->lpni_peer_net->lpn_peer;
2773 if (lnet_peer_is_multi_rail(src_lp) &&
2774 !lnet_is_peer_ni_alive(lpni))
2775 mr_forwarding_allowed = true;
2778 CDEBUG(D_NET, "msg %p MR forwarding %s\n", msg,
2779 mr_forwarding_allowed ? "allowed" : "not allowed");
2783 * Deal with the peer as NMR in the following cases:
2784 * 1. the peer is NMR
2785 * 2. We're trying to recover a specific peer NI
2786 * 3. I'm a router sending to the final destination and MR forwarding is
2787 * not allowed for this message (as determined above).
2788 * In this case the source of the message would've
2789 * already selected the final destination so my job
2790 * is to honor the selection.
2792 if (!lnet_peer_is_multi_rail(peer) || msg->msg_recovery ||
2793 (final_hop && !mr_forwarding_allowed))
2794 send_case |= NMR_DST;
2796 send_case |= MR_DST;
2798 if (lnet_msg_is_response(msg))
2799 send_case |= SND_RESP;
2801 /* assign parameters to the send_data */
2802 send_data.sd_rtr_nid = rtr_nid;
2803 send_data.sd_src_nid = src_nid;
2804 send_data.sd_dst_nid = dst_nid;
2805 send_data.sd_best_lpni = lpni;
2807 * keep a pointer to the final destination in case we're going to
2808 * route, so we'll need to access it later
2810 send_data.sd_final_dst_lpni = lpni;
2811 send_data.sd_peer = peer;
2812 send_data.sd_md_cpt = md_cpt;
2813 send_data.sd_send_case = send_case;
2815 rc = lnet_handle_send_case_locked(&send_data);
2818 * Update the local cpt since send_data.sd_cpt might've been
2819 * updated as a result of calling lnet_handle_send_case_locked().
2821 cpt = send_data.sd_cpt;
2823 if (rc == REPEAT_SEND)
2826 lnet_net_unlock(cpt);
2832 lnet_send(lnet_nid_t src_nid, struct lnet_msg *msg, lnet_nid_t rtr_nid)
2834 lnet_nid_t dst_nid = msg->msg_target.nid;
2838 * NB: rtr_nid is set to LNET_NID_ANY for all current use-cases,
2839 * but we might want to use pre-determined router for ACK/REPLY
2842 /* NB: ni != NULL == interface pre-determined (ACK/REPLY) */
2843 LASSERT(msg->msg_txpeer == NULL);
2844 LASSERT(msg->msg_txni == NULL);
2845 LASSERT(!msg->msg_sending);
2846 LASSERT(!msg->msg_target_is_router);
2847 LASSERT(!msg->msg_receiving);
2849 msg->msg_sending = 1;
2851 LASSERT(!msg->msg_tx_committed);
2853 rc = lnet_select_pathway(src_nid, dst_nid, msg, rtr_nid);
2855 if (rc == -EHOSTUNREACH)
2856 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_ERROR;
2858 msg->msg_health_status = LNET_MSG_STATUS_LOCAL_ERROR;
2862 if (rc == LNET_CREDIT_OK)
2863 lnet_ni_send(msg->msg_txni, msg);
2865 /* rc == LNET_CREDIT_OK or LNET_CREDIT_WAIT or LNET_DC_WAIT */
2869 enum lnet_mt_event_type {
2870 MT_TYPE_LOCAL_NI = 0,
2874 struct lnet_mt_event_info {
2875 enum lnet_mt_event_type mt_type;
2879 /* called with res_lock held */
2881 lnet_detach_rsp_tracker(struct lnet_libmd *md, int cpt)
2883 struct lnet_rsp_tracker *rspt;
2886 * msg has a refcount on the MD so the MD is not going away.
2887 * The rspt queue for the cpt is protected by
2888 * the lnet_net_lock(cpt). cpt is the cpt of the MD cookie.
2890 if (!md->md_rspt_ptr)
2893 rspt = md->md_rspt_ptr;
2896 LASSERT(rspt->rspt_cpt == cpt);
2898 md->md_rspt_ptr = NULL;
2900 if (LNetMDHandleIsInvalid(rspt->rspt_mdh)) {
2902 * The monitor thread has invalidated this handle because the
2903 * response timed out, but it failed to lookup the MD. That
2904 * means this response tracker is on the zombie list. We can
2905 * safely remove it under the resource lock (held by caller) and
2906 * free the response tracker block.
2908 list_del(&rspt->rspt_on_list);
2909 lnet_rspt_free(rspt, cpt);
2912 * invalidate the handle to indicate that a response has been
2913 * received, which will then lead the monitor thread to clean up
2916 LNetInvalidateMDHandle(&rspt->rspt_mdh);
2921 lnet_clean_zombie_rstqs(void)
2923 struct lnet_rsp_tracker *rspt, *tmp;
2926 cfs_cpt_for_each(i, lnet_cpt_table()) {
2927 list_for_each_entry_safe(rspt, tmp,
2928 the_lnet.ln_mt_zombie_rstqs[i],
2930 list_del(&rspt->rspt_on_list);
2931 lnet_rspt_free(rspt, i);
2935 cfs_percpt_free(the_lnet.ln_mt_zombie_rstqs);
2939 lnet_finalize_expired_responses(void)
2941 struct lnet_libmd *md;
2942 struct lnet_rsp_tracker *rspt, *tmp;
2946 if (the_lnet.ln_mt_rstq == NULL)
2949 cfs_cpt_for_each(i, lnet_cpt_table()) {
2950 LIST_HEAD(local_queue);
2953 if (!the_lnet.ln_mt_rstq[i]) {
2957 list_splice_init(the_lnet.ln_mt_rstq[i], &local_queue);
2962 list_for_each_entry_safe(rspt, tmp, &local_queue, rspt_on_list) {
2964 * The rspt mdh will be invalidated when a response
2965 * is received or whenever we want to discard the
2966 * block the monitor thread will walk the queue
2967 * and clean up any rsts with an invalid mdh.
2968 * The monitor thread will walk the queue until
2969 * the first unexpired rspt block. This means that
2970 * some rspt blocks which received their
2971 * corresponding responses will linger in the
2972 * queue until they are cleaned up eventually.
2975 if (LNetMDHandleIsInvalid(rspt->rspt_mdh)) {
2977 list_del(&rspt->rspt_on_list);
2978 lnet_rspt_free(rspt, i);
2982 if (ktime_compare(now, rspt->rspt_deadline) >= 0 ||
2983 the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN) {
2984 struct lnet_peer_ni *lpni;
2987 md = lnet_handle2md(&rspt->rspt_mdh);
2989 /* MD has been queued for unlink, but
2990 * rspt hasn't been detached (Note we've
2991 * checked above that the rspt_mdh is
2992 * valid). Since we cannot lookup the MD
2993 * we're unable to detach the rspt
2994 * ourselves. Thus, move the rspt to the
2995 * zombie list where we'll wait for
2997 * 1. The remaining operations on the
2998 * MD to complete. In this case the
2999 * final operation will result in
3000 * lnet_msg_detach_md()->
3001 * lnet_detach_rsp_tracker() where
3002 * we will clean up this response
3004 * 2. LNet to shutdown. In this case
3005 * we'll wait until after all LND Nets
3006 * have shutdown and then we can
3007 * safely free any remaining response
3008 * tracker blocks on the zombie list.
3009 * Note: We need to hold the resource
3010 * lock when adding to the zombie list
3011 * because we may have concurrent access
3012 * with lnet_detach_rsp_tracker().
3014 LNetInvalidateMDHandle(&rspt->rspt_mdh);
3015 list_move(&rspt->rspt_on_list,
3016 the_lnet.ln_mt_zombie_rstqs[i]);
3020 LASSERT(md->md_rspt_ptr == rspt);
3021 md->md_rspt_ptr = NULL;
3024 LNetMDUnlink(rspt->rspt_mdh);
3026 nid = rspt->rspt_next_hop_nid;
3028 list_del(&rspt->rspt_on_list);
3029 lnet_rspt_free(rspt, i);
3031 /* If we're shutting down we just want to clean
3032 * up the rspt blocks
3034 if (the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN)
3038 the_lnet.ln_counters[i]->lct_health.lch_response_timeout_count++;
3042 "Response timeout: md = %p: nid = %s\n",
3043 md, libcfs_nid2str(nid));
3046 * If there is a timeout on the response
3047 * from the next hop decrement its health
3048 * value so that we don't use it
3051 lpni = lnet_find_peer_ni_locked(nid);
3053 lnet_handle_remote_failure_locked(lpni);
3054 lnet_peer_ni_decref_locked(lpni);
3063 if (!list_empty(&local_queue)) {
3065 list_splice(&local_queue, the_lnet.ln_mt_rstq[i]);
3072 lnet_resend_pending_msgs_locked(struct list_head *resendq, int cpt)
3074 struct lnet_msg *msg;
3076 while (!list_empty(resendq)) {
3077 struct lnet_peer_ni *lpni;
3079 msg = list_entry(resendq->next, struct lnet_msg,
3082 list_del_init(&msg->msg_list);
3084 lpni = lnet_find_peer_ni_locked(msg->msg_hdr.dest_nid);
3086 lnet_net_unlock(cpt);
3087 CERROR("Expected that a peer is already created for %s\n",
3088 libcfs_nid2str(msg->msg_hdr.dest_nid));
3089 msg->msg_no_resend = true;
3090 lnet_finalize(msg, -EFAULT);
3095 lnet_peer_ni_decref_locked(lpni);
3097 lnet_net_unlock(cpt);
3098 CDEBUG(D_NET, "resending %s->%s: %s recovery %d try# %d\n",
3099 libcfs_nid2str(msg->msg_src_nid_param),
3100 libcfs_id2str(msg->msg_target),
3101 lnet_msgtyp2str(msg->msg_type),
3103 msg->msg_retry_count);
3104 rc = lnet_send(msg->msg_src_nid_param, msg,
3105 msg->msg_rtr_nid_param);
3107 CERROR("Error sending %s to %s: %d\n",
3108 lnet_msgtyp2str(msg->msg_type),
3109 libcfs_id2str(msg->msg_target), rc);
3110 msg->msg_no_resend = true;
3111 lnet_finalize(msg, rc);
3115 the_lnet.ln_counters[cpt]->lct_health.lch_resend_count++;
3121 lnet_resend_pending_msgs(void)
3125 cfs_cpt_for_each(i, lnet_cpt_table()) {
3127 lnet_resend_pending_msgs_locked(the_lnet.ln_mt_resendqs[i], i);
3132 /* called with cpt and ni_lock held */
3134 lnet_unlink_ni_recovery_mdh_locked(struct lnet_ni *ni, int cpt, bool force)
3136 struct lnet_handle_md recovery_mdh;
3138 LNetInvalidateMDHandle(&recovery_mdh);
3140 if (ni->ni_recovery_state & LNET_NI_RECOVERY_PENDING ||
3142 recovery_mdh = ni->ni_ping_mdh;
3143 LNetInvalidateMDHandle(&ni->ni_ping_mdh);
3146 lnet_net_unlock(cpt);
3147 if (!LNetMDHandleIsInvalid(recovery_mdh))
3148 LNetMDUnlink(recovery_mdh);
3154 lnet_recover_local_nis(void)
3156 struct lnet_mt_event_info *ev_info;
3157 LIST_HEAD(processed_list);
3158 LIST_HEAD(local_queue);
3159 struct lnet_handle_md mdh;
3160 struct lnet_ni *tmp;
3167 * splice the recovery queue on a local queue. We will iterate
3168 * through the local queue and update it as needed. Once we're
3169 * done with the traversal, we'll splice the local queue back on
3170 * the head of the ln_mt_localNIRecovq. Any newly added local NIs
3171 * will be traversed in the next iteration.
3174 list_splice_init(&the_lnet.ln_mt_localNIRecovq,
3178 list_for_each_entry_safe(ni, tmp, &local_queue, ni_recovery) {
3180 * if an NI is being deleted or it is now healthy, there
3181 * is no need to keep it around in the recovery queue.
3182 * The monitor thread is the only thread responsible for
3183 * removing the NI from the recovery queue.
3184 * Multiple threads can be adding NIs to the recovery
3187 healthv = atomic_read(&ni->ni_healthv);
3191 if (ni->ni_state != LNET_NI_STATE_ACTIVE ||
3192 healthv == LNET_MAX_HEALTH_VALUE) {
3193 list_del_init(&ni->ni_recovery);
3194 lnet_unlink_ni_recovery_mdh_locked(ni, 0, false);
3196 lnet_ni_decref_locked(ni, 0);
3202 * if the local NI failed recovery we must unlink the md.
3203 * But we want to keep the local_ni on the recovery queue
3204 * so we can continue the attempts to recover it.
3206 if (ni->ni_recovery_state & LNET_NI_RECOVERY_FAILED) {
3207 lnet_unlink_ni_recovery_mdh_locked(ni, 0, true);
3208 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_FAILED;
3215 CDEBUG(D_NET, "attempting to recover local ni: %s\n",
3216 libcfs_nid2str(ni->ni_nid));
3219 if (!(ni->ni_recovery_state & LNET_NI_RECOVERY_PENDING)) {
3220 ni->ni_recovery_state |= LNET_NI_RECOVERY_PENDING;
3223 LIBCFS_ALLOC(ev_info, sizeof(*ev_info));
3225 CERROR("out of memory. Can't recover %s\n",
3226 libcfs_nid2str(ni->ni_nid));
3228 ni->ni_recovery_state &=
3229 ~LNET_NI_RECOVERY_PENDING;
3234 mdh = ni->ni_ping_mdh;
3236 * Invalidate the ni mdh in case it's deleted.
3237 * We'll unlink the mdh in this case below.
3239 LNetInvalidateMDHandle(&ni->ni_ping_mdh);
3243 * remove the NI from the local queue and drop the
3244 * reference count to it while we're recovering
3245 * it. The reason for that, is that the NI could
3246 * be deleted, and the way the code is structured
3247 * is if we don't drop the NI, then the deletion
3248 * code will enter a loop waiting for the
3249 * reference count to be removed while holding the
3250 * ln_mutex_lock(). When we look up the peer to
3251 * send to in lnet_select_pathway() we will try to
3252 * lock the ln_mutex_lock() as well, leading to
3253 * a deadlock. By dropping the refcount and
3254 * removing it from the list, we allow for the NI
3255 * to be removed, then we use the cached NID to
3256 * look it up again. If it's gone, then we just
3257 * continue examining the rest of the queue.
3260 list_del_init(&ni->ni_recovery);
3261 lnet_ni_decref_locked(ni, 0);
3264 ev_info->mt_type = MT_TYPE_LOCAL_NI;
3265 ev_info->mt_nid = nid;
3266 rc = lnet_send_ping(nid, &mdh, LNET_INTERFACES_MIN,
3267 ev_info, the_lnet.ln_mt_handler,
3269 /* lookup the nid again */
3271 ni = lnet_nid2ni_locked(nid, 0);
3274 * the NI has been deleted when we dropped
3282 * Same note as in lnet_recover_peer_nis(). When
3283 * we're sending the ping, the NI is free to be
3284 * deleted or manipulated. By this point it
3285 * could've been added back on the recovery queue,
3286 * and a refcount taken on it.
3287 * So we can't just add it blindly again or we'll
3288 * corrupt the queue. We must check under lock if
3289 * it's not on any list and if not then add it
3290 * to the processed list, which will eventually be
3291 * spliced back on to the recovery queue.
3293 ni->ni_ping_mdh = mdh;
3294 if (list_empty(&ni->ni_recovery)) {
3295 list_add_tail(&ni->ni_recovery, &processed_list);
3296 lnet_ni_addref_locked(ni, 0);
3302 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_PENDING;
3308 * put back the remaining NIs on the ln_mt_localNIRecovq to be
3309 * reexamined in the next iteration.
3311 list_splice_init(&processed_list, &local_queue);
3313 list_splice(&local_queue, &the_lnet.ln_mt_localNIRecovq);
3318 lnet_resendqs_create(void)
3320 struct list_head **resendqs;
3321 resendqs = lnet_create_array_of_queues();
3326 lnet_net_lock(LNET_LOCK_EX);
3327 the_lnet.ln_mt_resendqs = resendqs;
3328 lnet_net_unlock(LNET_LOCK_EX);
3334 lnet_clean_local_ni_recoveryq(void)
3338 /* This is only called when the monitor thread has stopped */
3341 while (!list_empty(&the_lnet.ln_mt_localNIRecovq)) {
3342 ni = list_entry(the_lnet.ln_mt_localNIRecovq.next,
3343 struct lnet_ni, ni_recovery);
3344 list_del_init(&ni->ni_recovery);
3346 lnet_unlink_ni_recovery_mdh_locked(ni, 0, true);
3348 lnet_ni_decref_locked(ni, 0);
3355 lnet_unlink_lpni_recovery_mdh_locked(struct lnet_peer_ni *lpni, int cpt,
3358 struct lnet_handle_md recovery_mdh;
3360 LNetInvalidateMDHandle(&recovery_mdh);
3362 if (lpni->lpni_state & LNET_PEER_NI_RECOVERY_PENDING || force) {
3363 recovery_mdh = lpni->lpni_recovery_ping_mdh;
3364 LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
3366 spin_unlock(&lpni->lpni_lock);
3367 lnet_net_unlock(cpt);
3368 if (!LNetMDHandleIsInvalid(recovery_mdh))
3369 LNetMDUnlink(recovery_mdh);
3371 spin_lock(&lpni->lpni_lock);
3375 lnet_clean_peer_ni_recoveryq(void)
3377 struct lnet_peer_ni *lpni, *tmp;
3379 lnet_net_lock(LNET_LOCK_EX);
3381 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_mt_peerNIRecovq,
3383 list_del_init(&lpni->lpni_recovery);
3384 spin_lock(&lpni->lpni_lock);
3385 lnet_unlink_lpni_recovery_mdh_locked(lpni, LNET_LOCK_EX, true);
3386 spin_unlock(&lpni->lpni_lock);
3387 lnet_peer_ni_decref_locked(lpni);
3390 lnet_net_unlock(LNET_LOCK_EX);
3394 lnet_clean_resendqs(void)
3396 struct lnet_msg *msg, *tmp;
3400 cfs_cpt_for_each(i, lnet_cpt_table()) {
3402 list_splice_init(the_lnet.ln_mt_resendqs[i], &msgs);
3404 list_for_each_entry_safe(msg, tmp, &msgs, msg_list) {
3405 list_del_init(&msg->msg_list);
3406 msg->msg_no_resend = true;
3407 lnet_finalize(msg, -ESHUTDOWN);
3411 cfs_percpt_free(the_lnet.ln_mt_resendqs);
3415 lnet_recover_peer_nis(void)
3417 struct lnet_mt_event_info *ev_info;
3418 LIST_HEAD(processed_list);
3419 LIST_HEAD(local_queue);
3420 struct lnet_handle_md mdh;
3421 struct lnet_peer_ni *lpni;
3422 struct lnet_peer_ni *tmp;
3428 * Always use cpt 0 for locking across all interactions with
3429 * ln_mt_peerNIRecovq
3432 list_splice_init(&the_lnet.ln_mt_peerNIRecovq,
3436 list_for_each_entry_safe(lpni, tmp, &local_queue,
3439 * The same protection strategy is used here as is in the
3440 * local recovery case.
3443 healthv = atomic_read(&lpni->lpni_healthv);
3444 spin_lock(&lpni->lpni_lock);
3445 if (lpni->lpni_state & LNET_PEER_NI_DELETING ||
3446 healthv == LNET_MAX_HEALTH_VALUE) {
3447 list_del_init(&lpni->lpni_recovery);
3448 lnet_unlink_lpni_recovery_mdh_locked(lpni, 0, false);
3449 spin_unlock(&lpni->lpni_lock);
3450 lnet_peer_ni_decref_locked(lpni);
3456 * If the peer NI has failed recovery we must unlink the
3457 * md. But we want to keep the peer ni on the recovery
3458 * queue so we can try to continue recovering it
3460 if (lpni->lpni_state & LNET_PEER_NI_RECOVERY_FAILED) {
3461 lnet_unlink_lpni_recovery_mdh_locked(lpni, 0, true);
3462 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_FAILED;
3465 spin_unlock(&lpni->lpni_lock);
3469 * NOTE: we're racing with peer deletion from user space.
3470 * It's possible that a peer is deleted after we check its
3471 * state. In this case the recovery can create a new peer
3473 spin_lock(&lpni->lpni_lock);
3474 if (!(lpni->lpni_state & LNET_PEER_NI_RECOVERY_PENDING) &&
3475 !(lpni->lpni_state & LNET_PEER_NI_DELETING)) {
3476 lpni->lpni_state |= LNET_PEER_NI_RECOVERY_PENDING;
3477 spin_unlock(&lpni->lpni_lock);
3479 LIBCFS_ALLOC(ev_info, sizeof(*ev_info));
3481 CERROR("out of memory. Can't recover %s\n",
3482 libcfs_nid2str(lpni->lpni_nid));
3483 spin_lock(&lpni->lpni_lock);
3484 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3485 spin_unlock(&lpni->lpni_lock);
3489 /* look at the comments in lnet_recover_local_nis() */
3490 mdh = lpni->lpni_recovery_ping_mdh;
3491 LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
3492 nid = lpni->lpni_nid;
3494 list_del_init(&lpni->lpni_recovery);
3495 lnet_peer_ni_decref_locked(lpni);
3498 ev_info->mt_type = MT_TYPE_PEER_NI;
3499 ev_info->mt_nid = nid;
3500 rc = lnet_send_ping(nid, &mdh, LNET_INTERFACES_MIN,
3501 ev_info, the_lnet.ln_mt_handler,
3505 * lnet_find_peer_ni_locked() grabs a refcount for
3506 * us. No need to take it explicitly.
3508 lpni = lnet_find_peer_ni_locked(nid);
3515 lpni->lpni_recovery_ping_mdh = mdh;
3517 * While we're unlocked the lpni could've been
3518 * readded on the recovery queue. In this case we
3519 * don't need to add it to the local queue, since
3520 * it's already on there and the thread that added
3521 * it would've incremented the refcount on the
3522 * peer, which means we need to decref the refcount
3523 * that was implicitly grabbed by find_peer_ni_locked.
3524 * Otherwise, if the lpni is still not on
3525 * the recovery queue, then we'll add it to the
3528 if (list_empty(&lpni->lpni_recovery))
3529 list_add_tail(&lpni->lpni_recovery, &processed_list);
3531 lnet_peer_ni_decref_locked(lpni);
3534 spin_lock(&lpni->lpni_lock);
3536 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3538 spin_unlock(&lpni->lpni_lock);
3541 list_splice_init(&processed_list, &local_queue);
3543 list_splice(&local_queue, &the_lnet.ln_mt_peerNIRecovq);
3548 lnet_monitor_thread(void *arg)
3550 time64_t recovery_timeout = 0;
3551 time64_t rsp_timeout = 0;
3555 wait_for_completion(&the_lnet.ln_started);
3557 * The monitor thread takes care of the following:
3558 * 1. Checks the aliveness of routers
3559 * 2. Checks if there are messages on the resend queue to resend
3561 * 3. Check if there are any NIs on the local recovery queue and
3563 * 4. Checks if there are any NIs on the remote recovery queue
3566 while (the_lnet.ln_mt_state == LNET_MT_STATE_RUNNING) {
3567 now = ktime_get_real_seconds();
3569 if (lnet_router_checker_active())
3570 lnet_check_routers();
3572 lnet_resend_pending_msgs();
3574 if (now >= rsp_timeout) {
3575 lnet_finalize_expired_responses();
3576 rsp_timeout = now + (lnet_transaction_timeout / 2);
3579 if (now >= recovery_timeout) {
3580 lnet_recover_local_nis();
3581 lnet_recover_peer_nis();
3582 recovery_timeout = now + lnet_recovery_interval;
3586 * TODO do we need to check if we should sleep without
3587 * timeout? Technically, an active system will always
3588 * have messages in flight so this check will always
3589 * evaluate to false. And on an idle system do we care
3590 * if we wake up every 1 second? Although, we've seen
3591 * cases where we get a complaint that an idle thread
3592 * is waking up unnecessarily.
3594 * Take into account the current net_count when you wake
3595 * up for alive router checking, since we need to check
3596 * possibly as many networks as we have configured.
3598 interval = min(lnet_recovery_interval,
3599 min((unsigned int) alive_router_check_interval /
3600 lnet_current_net_count,
3601 lnet_transaction_timeout / 2));
3602 wait_for_completion_interruptible_timeout(
3603 &the_lnet.ln_mt_wait_complete,
3604 cfs_time_seconds(interval));
3605 /* Must re-init the completion before testing anything,
3606 * including ln_mt_state.
3608 reinit_completion(&the_lnet.ln_mt_wait_complete);
3612 lnet_net_lock(LNET_LOCK_EX);
3613 the_lnet.ln_mt_state = LNET_MT_STATE_SHUTDOWN;
3614 lnet_net_unlock(LNET_LOCK_EX);
3616 /* signal that the monitor thread is exiting */
3617 up(&the_lnet.ln_mt_signal);
3625 * Returns == 0 if success
3626 * Returns > 0 if LNetMDBind or prior fails
3627 * Returns < 0 if LNetGet fails
3630 lnet_send_ping(lnet_nid_t dest_nid,
3631 struct lnet_handle_md *mdh, int nnis,
3632 void *user_data, lnet_handler_t handler, bool recovery)
3634 struct lnet_md md = { NULL };
3635 struct lnet_process_id id;
3636 struct lnet_ping_buffer *pbuf;
3639 if (dest_nid == LNET_NID_ANY) {
3644 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
3650 /* initialize md content */
3651 md.start = &pbuf->pb_info;
3652 md.length = LNET_PING_INFO_SIZE(nnis);
3653 md.threshold = 2; /* GET/REPLY */
3655 md.options = LNET_MD_TRUNCATE | LNET_MD_TRACK_RESPONSE;
3656 md.user_ptr = user_data;
3657 md.handler = handler;
3659 rc = LNetMDBind(&md, LNET_UNLINK, mdh);
3661 lnet_ping_buffer_decref(pbuf);
3662 CERROR("Can't bind MD: %d\n", rc);
3663 rc = -rc; /* change the rc to positive */
3666 id.pid = LNET_PID_LUSTRE;
3669 rc = LNetGet(LNET_NID_ANY, *mdh, id,
3670 LNET_RESERVED_PORTAL,
3671 LNET_PROTO_PING_MATCHBITS, 0, recovery);
3674 goto fail_unlink_md;
3680 LNetInvalidateMDHandle(mdh);
3686 lnet_handle_recovery_reply(struct lnet_mt_event_info *ev_info,
3687 int status, bool send, bool unlink_event)
3689 lnet_nid_t nid = ev_info->mt_nid;
3691 if (ev_info->mt_type == MT_TYPE_LOCAL_NI) {
3695 ni = lnet_nid2ni_locked(nid, 0);
3701 if (!send || (send && status != 0))
3702 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_PENDING;
3704 ni->ni_recovery_state |= LNET_NI_RECOVERY_FAILED;
3709 CERROR("local NI (%s) recovery failed with %d\n",
3710 libcfs_nid2str(nid), status);
3714 * need to increment healthv for the ni here, because in
3715 * the lnet_finalize() path we don't have access to this
3716 * NI. And in order to get access to it, we'll need to
3717 * carry forward too much information.
3718 * In the peer case, it'll naturally be incremented
3721 lnet_inc_healthv(&ni->ni_healthv,
3722 lnet_health_sensitivity);
3724 struct lnet_peer_ni *lpni;
3727 cpt = lnet_net_lock_current();
3728 lpni = lnet_find_peer_ni_locked(nid);
3730 lnet_net_unlock(cpt);
3733 spin_lock(&lpni->lpni_lock);
3734 if (!send || (send && status != 0))
3735 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3737 lpni->lpni_state |= LNET_PEER_NI_RECOVERY_FAILED;
3738 spin_unlock(&lpni->lpni_lock);
3739 lnet_peer_ni_decref_locked(lpni);
3740 lnet_net_unlock(cpt);
3743 CERROR("peer NI (%s) recovery failed with %d\n",
3744 libcfs_nid2str(nid), status);
3749 lnet_mt_event_handler(struct lnet_event *event)
3751 struct lnet_mt_event_info *ev_info = event->md_user_ptr;
3752 struct lnet_ping_buffer *pbuf;
3754 /* TODO: remove assert */
3755 LASSERT(event->type == LNET_EVENT_REPLY ||
3756 event->type == LNET_EVENT_SEND ||
3757 event->type == LNET_EVENT_UNLINK);
3759 CDEBUG(D_NET, "Received event: %d status: %d\n", event->type,
3762 switch (event->type) {
3763 case LNET_EVENT_UNLINK:
3764 CDEBUG(D_NET, "%s recovery ping unlinked\n",
3765 libcfs_nid2str(ev_info->mt_nid));
3767 case LNET_EVENT_REPLY:
3768 lnet_handle_recovery_reply(ev_info, event->status, false,
3769 event->type == LNET_EVENT_UNLINK);
3771 case LNET_EVENT_SEND:
3772 CDEBUG(D_NET, "%s recovery message sent %s:%d\n",
3773 libcfs_nid2str(ev_info->mt_nid),
3774 (event->status) ? "unsuccessfully" :
3775 "successfully", event->status);
3776 lnet_handle_recovery_reply(ev_info, event->status, true, false);
3779 CERROR("Unexpected event: %d\n", event->type);
3782 if (event->unlinked) {
3783 LIBCFS_FREE(ev_info, sizeof(*ev_info));
3784 pbuf = LNET_PING_INFO_TO_BUFFER(event->md_start);
3785 lnet_ping_buffer_decref(pbuf);
3790 lnet_rsp_tracker_create(void)
3792 struct list_head **rstqs;
3793 rstqs = lnet_create_array_of_queues();
3798 the_lnet.ln_mt_rstq = rstqs;
3804 lnet_rsp_tracker_clean(void)
3806 lnet_finalize_expired_responses();
3808 cfs_percpt_free(the_lnet.ln_mt_rstq);
3809 the_lnet.ln_mt_rstq = NULL;
3812 int lnet_monitor_thr_start(void)
3815 struct task_struct *task;
3817 if (the_lnet.ln_mt_state != LNET_MT_STATE_SHUTDOWN)
3820 rc = lnet_resendqs_create();
3824 rc = lnet_rsp_tracker_create();
3828 sema_init(&the_lnet.ln_mt_signal, 0);
3830 lnet_net_lock(LNET_LOCK_EX);
3831 the_lnet.ln_mt_state = LNET_MT_STATE_RUNNING;
3832 lnet_net_unlock(LNET_LOCK_EX);
3833 task = kthread_run(lnet_monitor_thread, NULL, "monitor_thread");
3836 CERROR("Can't start monitor thread: %d\n", rc);
3843 lnet_net_lock(LNET_LOCK_EX);
3844 the_lnet.ln_mt_state = LNET_MT_STATE_STOPPING;
3845 lnet_net_unlock(LNET_LOCK_EX);
3846 /* block until event callback signals exit */
3847 down(&the_lnet.ln_mt_signal);
3849 lnet_net_lock(LNET_LOCK_EX);
3850 the_lnet.ln_mt_state = LNET_MT_STATE_SHUTDOWN;
3851 lnet_net_unlock(LNET_LOCK_EX);
3852 lnet_rsp_tracker_clean();
3853 lnet_clean_local_ni_recoveryq();
3854 lnet_clean_peer_ni_recoveryq();
3855 lnet_clean_resendqs();
3856 the_lnet.ln_mt_handler = NULL;
3859 lnet_rsp_tracker_clean();
3860 lnet_clean_local_ni_recoveryq();
3861 lnet_clean_peer_ni_recoveryq();
3862 lnet_clean_resendqs();
3866 void lnet_monitor_thr_stop(void)
3868 if (the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN)
3871 LASSERT(the_lnet.ln_mt_state == LNET_MT_STATE_RUNNING);
3872 lnet_net_lock(LNET_LOCK_EX);
3873 the_lnet.ln_mt_state = LNET_MT_STATE_STOPPING;
3874 lnet_net_unlock(LNET_LOCK_EX);
3876 /* tell the monitor thread that we're shutting down */
3877 complete(&the_lnet.ln_mt_wait_complete);
3879 /* block until monitor thread signals that it's done */
3880 down(&the_lnet.ln_mt_signal);
3881 LASSERT(the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN);
3883 /* perform cleanup tasks */
3884 lnet_rsp_tracker_clean();
3885 lnet_clean_local_ni_recoveryq();
3886 lnet_clean_peer_ni_recoveryq();
3887 lnet_clean_resendqs();
3891 lnet_drop_message(struct lnet_ni *ni, int cpt, void *private, unsigned int nob,
3895 lnet_incr_stats(&ni->ni_stats, msg_type, LNET_STATS_TYPE_DROP);
3896 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
3897 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length += nob;
3898 lnet_net_unlock(cpt);
3900 lnet_ni_recv(ni, private, NULL, 0, 0, 0, nob);
3904 lnet_recv_put(struct lnet_ni *ni, struct lnet_msg *msg)
3906 struct lnet_hdr *hdr = &msg->msg_hdr;
3908 if (msg->msg_wanted != 0)
3909 lnet_setpayloadbuffer(msg);
3911 lnet_build_msg_event(msg, LNET_EVENT_PUT);
3913 /* Must I ACK? If so I'll grab the ack_wmd out of the header and put
3914 * it back into the ACK during lnet_finalize() */
3915 msg->msg_ack = (!lnet_is_wire_handle_none(&hdr->msg.put.ack_wmd) &&
3916 (msg->msg_md->md_options & LNET_MD_ACK_DISABLE) == 0);
3918 lnet_ni_recv(ni, msg->msg_private, msg, msg->msg_rx_delayed,
3919 msg->msg_offset, msg->msg_wanted, hdr->payload_length);
3923 lnet_parse_put(struct lnet_ni *ni, struct lnet_msg *msg)
3925 struct lnet_hdr *hdr = &msg->msg_hdr;
3926 struct lnet_match_info info;
3930 /* Convert put fields to host byte order */
3931 hdr->msg.put.match_bits = le64_to_cpu(hdr->msg.put.match_bits);
3932 hdr->msg.put.ptl_index = le32_to_cpu(hdr->msg.put.ptl_index);
3933 hdr->msg.put.offset = le32_to_cpu(hdr->msg.put.offset);
3935 /* Primary peer NID. */
3936 info.mi_id.nid = msg->msg_initiator;
3937 info.mi_id.pid = hdr->src_pid;
3938 info.mi_opc = LNET_MD_OP_PUT;
3939 info.mi_portal = hdr->msg.put.ptl_index;
3940 info.mi_rlength = hdr->payload_length;
3941 info.mi_roffset = hdr->msg.put.offset;
3942 info.mi_mbits = hdr->msg.put.match_bits;
3943 info.mi_cpt = lnet_cpt_of_nid(msg->msg_initiator, ni);
3945 msg->msg_rx_ready_delay = ni->ni_net->net_lnd->lnd_eager_recv == NULL;
3946 ready_delay = msg->msg_rx_ready_delay;
3949 rc = lnet_ptl_match_md(&info, msg);
3954 case LNET_MATCHMD_OK:
3955 lnet_recv_put(ni, msg);
3958 case LNET_MATCHMD_NONE:
3960 /* no eager_recv or has already called it, should
3961 * have been attached on delayed list */
3964 rc = lnet_ni_eager_recv(ni, msg);
3971 case LNET_MATCHMD_DROP:
3972 CNETERR("Dropping PUT from %s portal %d match %llu"
3973 " offset %d length %d: %d\n",
3974 libcfs_id2str(info.mi_id), info.mi_portal,
3975 info.mi_mbits, info.mi_roffset, info.mi_rlength, rc);
3977 return -ENOENT; /* -ve: OK but no match */
3982 lnet_parse_get(struct lnet_ni *ni, struct lnet_msg *msg, int rdma_get)
3984 struct lnet_match_info info;
3985 struct lnet_hdr *hdr = &msg->msg_hdr;
3986 struct lnet_process_id source_id;
3987 struct lnet_handle_wire reply_wmd;
3990 /* Convert get fields to host byte order */
3991 hdr->msg.get.match_bits = le64_to_cpu(hdr->msg.get.match_bits);
3992 hdr->msg.get.ptl_index = le32_to_cpu(hdr->msg.get.ptl_index);
3993 hdr->msg.get.sink_length = le32_to_cpu(hdr->msg.get.sink_length);
3994 hdr->msg.get.src_offset = le32_to_cpu(hdr->msg.get.src_offset);
3996 source_id.nid = hdr->src_nid;
3997 source_id.pid = hdr->src_pid;
3998 /* Primary peer NID */
3999 info.mi_id.nid = msg->msg_initiator;
4000 info.mi_id.pid = hdr->src_pid;
4001 info.mi_opc = LNET_MD_OP_GET;
4002 info.mi_portal = hdr->msg.get.ptl_index;
4003 info.mi_rlength = hdr->msg.get.sink_length;
4004 info.mi_roffset = hdr->msg.get.src_offset;
4005 info.mi_mbits = hdr->msg.get.match_bits;
4006 info.mi_cpt = lnet_cpt_of_nid(msg->msg_initiator, ni);
4008 rc = lnet_ptl_match_md(&info, msg);
4009 if (rc == LNET_MATCHMD_DROP) {
4010 CNETERR("Dropping GET from %s portal %d match %llu"
4011 " offset %d length %d\n",
4012 libcfs_id2str(info.mi_id), info.mi_portal,
4013 info.mi_mbits, info.mi_roffset, info.mi_rlength);
4014 return -ENOENT; /* -ve: OK but no match */
4017 LASSERT(rc == LNET_MATCHMD_OK);
4019 lnet_build_msg_event(msg, LNET_EVENT_GET);
4021 reply_wmd = hdr->msg.get.return_wmd;
4023 lnet_prep_send(msg, LNET_MSG_REPLY, source_id,
4024 msg->msg_offset, msg->msg_wanted);
4026 msg->msg_hdr.msg.reply.dst_wmd = reply_wmd;
4029 /* The LND completes the REPLY from her recv procedure */
4030 lnet_ni_recv(ni, msg->msg_private, msg, 0,
4031 msg->msg_offset, msg->msg_len, msg->msg_len);
4035 lnet_ni_recv(ni, msg->msg_private, NULL, 0, 0, 0, 0);
4036 msg->msg_receiving = 0;
4038 rc = lnet_send(ni->ni_nid, msg, msg->msg_from);
4040 /* didn't get as far as lnet_ni_send() */
4041 CERROR("%s: Unable to send REPLY for GET from %s: %d\n",
4042 libcfs_nid2str(ni->ni_nid),
4043 libcfs_id2str(info.mi_id), rc);
4045 lnet_finalize(msg, rc);
4052 lnet_parse_reply(struct lnet_ni *ni, struct lnet_msg *msg)
4054 void *private = msg->msg_private;
4055 struct lnet_hdr *hdr = &msg->msg_hdr;
4056 struct lnet_process_id src = {0};
4057 struct lnet_libmd *md;
4058 unsigned int rlength;
4059 unsigned int mlength;
4062 cpt = lnet_cpt_of_cookie(hdr->msg.reply.dst_wmd.wh_object_cookie);
4065 src.nid = hdr->src_nid;
4066 src.pid = hdr->src_pid;
4068 /* NB handles only looked up by creator (no flips) */
4069 md = lnet_wire_handle2md(&hdr->msg.reply.dst_wmd);
4070 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
4071 CNETERR("%s: Dropping REPLY from %s for %s "
4073 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
4074 (md == NULL) ? "invalid" : "inactive",
4075 hdr->msg.reply.dst_wmd.wh_interface_cookie,
4076 hdr->msg.reply.dst_wmd.wh_object_cookie);
4077 if (md != NULL && md->md_me != NULL)
4078 CERROR("REPLY MD also attached to portal %d\n",
4079 md->md_me->me_portal);
4081 lnet_res_unlock(cpt);
4082 return -ENOENT; /* -ve: OK but no match */
4085 LASSERT(md->md_offset == 0);
4087 rlength = hdr->payload_length;
4088 mlength = min(rlength, md->md_length);
4090 if (mlength < rlength &&
4091 (md->md_options & LNET_MD_TRUNCATE) == 0) {
4092 CNETERR("%s: Dropping REPLY from %s length %d "
4093 "for MD %#llx would overflow (%d)\n",
4094 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
4095 rlength, hdr->msg.reply.dst_wmd.wh_object_cookie,
4097 lnet_res_unlock(cpt);
4098 return -ENOENT; /* -ve: OK but no match */
4101 CDEBUG(D_NET, "%s: Reply from %s of length %d/%d into md %#llx\n",
4102 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
4103 mlength, rlength, hdr->msg.reply.dst_wmd.wh_object_cookie);
4105 lnet_msg_attach_md(msg, md, 0, mlength);
4108 lnet_setpayloadbuffer(msg);
4110 lnet_res_unlock(cpt);
4112 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
4114 lnet_ni_recv(ni, private, msg, 0, 0, mlength, rlength);
4119 lnet_parse_ack(struct lnet_ni *ni, struct lnet_msg *msg)
4121 struct lnet_hdr *hdr = &msg->msg_hdr;
4122 struct lnet_process_id src = {0};
4123 struct lnet_libmd *md;
4126 src.nid = hdr->src_nid;
4127 src.pid = hdr->src_pid;
4129 /* Convert ack fields to host byte order */
4130 hdr->msg.ack.match_bits = le64_to_cpu(hdr->msg.ack.match_bits);
4131 hdr->msg.ack.mlength = le32_to_cpu(hdr->msg.ack.mlength);
4133 cpt = lnet_cpt_of_cookie(hdr->msg.ack.dst_wmd.wh_object_cookie);
4136 /* NB handles only looked up by creator (no flips) */
4137 md = lnet_wire_handle2md(&hdr->msg.ack.dst_wmd);
4138 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
4139 /* Don't moan; this is expected */
4141 "%s: Dropping ACK from %s to %s MD %#llx.%#llx\n",
4142 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
4143 (md == NULL) ? "invalid" : "inactive",
4144 hdr->msg.ack.dst_wmd.wh_interface_cookie,
4145 hdr->msg.ack.dst_wmd.wh_object_cookie);
4146 if (md != NULL && md->md_me != NULL)
4147 CERROR("Source MD also attached to portal %d\n",
4148 md->md_me->me_portal);
4150 lnet_res_unlock(cpt);
4151 return -ENOENT; /* -ve! */
4154 CDEBUG(D_NET, "%s: ACK from %s into md %#llx\n",
4155 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
4156 hdr->msg.ack.dst_wmd.wh_object_cookie);
4158 lnet_msg_attach_md(msg, md, 0, 0);
4160 lnet_res_unlock(cpt);
4162 lnet_build_msg_event(msg, LNET_EVENT_ACK);
4164 lnet_ni_recv(ni, msg->msg_private, msg, 0, 0, 0, msg->msg_len);
4169 * \retval LNET_CREDIT_OK If \a msg is forwarded
4170 * \retval LNET_CREDIT_WAIT If \a msg is blocked because w/o buffer
4171 * \retval -ve error code
4174 lnet_parse_forward_locked(struct lnet_ni *ni, struct lnet_msg *msg)
4178 if (!the_lnet.ln_routing)
4181 if (msg->msg_rxpeer->lpni_rtrcredits <= 0 ||
4182 lnet_msg2bufpool(msg)->rbp_credits <= 0) {
4183 if (ni->ni_net->net_lnd->lnd_eager_recv == NULL) {
4184 msg->msg_rx_ready_delay = 1;
4186 lnet_net_unlock(msg->msg_rx_cpt);
4187 rc = lnet_ni_eager_recv(ni, msg);
4188 lnet_net_lock(msg->msg_rx_cpt);
4193 rc = lnet_post_routed_recv_locked(msg, 0);
4198 lnet_parse_local(struct lnet_ni *ni, struct lnet_msg *msg)
4202 switch (msg->msg_type) {
4204 rc = lnet_parse_ack(ni, msg);
4207 rc = lnet_parse_put(ni, msg);
4210 rc = lnet_parse_get(ni, msg, msg->msg_rdma_get);
4212 case LNET_MSG_REPLY:
4213 rc = lnet_parse_reply(ni, msg);
4215 default: /* prevent an unused label if !kernel */
4220 LASSERT(rc == 0 || rc == -ENOENT);
4225 lnet_msgtyp2str (int type)
4234 case LNET_MSG_REPLY:
4236 case LNET_MSG_HELLO:
4239 return ("<UNKNOWN>");
4244 lnet_parse(struct lnet_ni *ni, struct lnet_hdr *hdr, lnet_nid_t from_nid,
4245 void *private, int rdma_req)
4247 struct lnet_peer_ni *lpni;
4248 struct lnet_msg *msg;
4249 __u32 payload_length;
4250 lnet_pid_t dest_pid;
4251 lnet_nid_t dest_nid;
4259 LASSERT (!in_interrupt ());
4261 type = le32_to_cpu(hdr->type);
4262 src_nid = le64_to_cpu(hdr->src_nid);
4263 dest_nid = le64_to_cpu(hdr->dest_nid);
4264 dest_pid = le32_to_cpu(hdr->dest_pid);
4265 payload_length = le32_to_cpu(hdr->payload_length);
4267 for_me = (ni->ni_nid == dest_nid);
4268 cpt = lnet_cpt_of_nid(from_nid, ni);
4270 CDEBUG(D_NET, "TRACE: %s(%s) <- %s : %s - %s\n",
4271 libcfs_nid2str(dest_nid),
4272 libcfs_nid2str(ni->ni_nid),
4273 libcfs_nid2str(src_nid),
4274 lnet_msgtyp2str(type),
4275 (for_me) ? "for me" : "routed");
4280 if (payload_length > 0) {
4281 CERROR("%s, src %s: bad %s payload %d (0 expected)\n",
4282 libcfs_nid2str(from_nid),
4283 libcfs_nid2str(src_nid),
4284 lnet_msgtyp2str(type), payload_length);
4290 case LNET_MSG_REPLY:
4291 if (payload_length >
4292 (__u32)(for_me ? LNET_MAX_PAYLOAD : LNET_MTU)) {
4293 CERROR("%s, src %s: bad %s payload %d "
4294 "(%d max expected)\n",
4295 libcfs_nid2str(from_nid),
4296 libcfs_nid2str(src_nid),
4297 lnet_msgtyp2str(type),
4299 for_me ? LNET_MAX_PAYLOAD : LNET_MTU);
4305 CERROR("%s, src %s: Bad message type 0x%x\n",
4306 libcfs_nid2str(from_nid),
4307 libcfs_nid2str(src_nid), type);
4311 if (the_lnet.ln_routing &&
4312 ni->ni_net->net_last_alive != ktime_get_real_seconds()) {
4314 spin_lock(&ni->ni_net->net_lock);
4315 ni->ni_net->net_last_alive = ktime_get_real_seconds();
4316 spin_unlock(&ni->ni_net->net_lock);
4317 push = lnet_ni_set_status_locked(ni, LNET_NI_STATUS_UP);
4322 lnet_push_update_to_peers(1);
4324 /* Regard a bad destination NID as a protocol error. Senders should
4325 * know what they're doing; if they don't they're misconfigured, buggy
4326 * or malicious so we chop them off at the knees :) */
4329 if (LNET_NIDNET(dest_nid) == LNET_NIDNET(ni->ni_nid)) {
4330 /* should have gone direct */
4331 CERROR("%s, src %s: Bad dest nid %s "
4332 "(should have been sent direct)\n",
4333 libcfs_nid2str(from_nid),
4334 libcfs_nid2str(src_nid),
4335 libcfs_nid2str(dest_nid));
4339 if (lnet_islocalnid(dest_nid)) {
4340 /* dest is another local NI; sender should have used
4341 * this node's NID on its own network */
4342 CERROR("%s, src %s: Bad dest nid %s "
4343 "(it's my nid but on a different network)\n",
4344 libcfs_nid2str(from_nid),
4345 libcfs_nid2str(src_nid),
4346 libcfs_nid2str(dest_nid));
4350 if (rdma_req && type == LNET_MSG_GET) {
4351 CERROR("%s, src %s: Bad optimized GET for %s "
4352 "(final destination must be me)\n",
4353 libcfs_nid2str(from_nid),
4354 libcfs_nid2str(src_nid),
4355 libcfs_nid2str(dest_nid));
4359 if (!the_lnet.ln_routing) {
4360 CERROR("%s, src %s: Dropping message for %s "
4361 "(routing not enabled)\n",
4362 libcfs_nid2str(from_nid),
4363 libcfs_nid2str(src_nid),
4364 libcfs_nid2str(dest_nid));
4369 /* Message looks OK; we're not going to return an error, so we MUST
4370 * call back lnd_recv() come what may... */
4372 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
4373 fail_peer(src_nid, 0)) { /* shall we now? */
4374 CERROR("%s, src %s: Dropping %s to simulate failure\n",
4375 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4376 lnet_msgtyp2str(type));
4380 if (!list_empty(&the_lnet.ln_drop_rules) &&
4381 lnet_drop_rule_match(hdr, ni->ni_nid, NULL)) {
4383 "%s, src %s, dst %s: Dropping %s to simulate silent message loss\n",
4384 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4385 libcfs_nid2str(dest_nid), lnet_msgtyp2str(type));
4389 if (lnet_drop_asym_route && for_me &&
4390 LNET_NIDNET(src_nid) != LNET_NIDNET(from_nid)) {
4391 struct lnet_net *net;
4392 struct lnet_remotenet *rnet;
4395 /* we are dealing with a routed message,
4396 * so see if route to reach src_nid goes through from_nid
4399 net = lnet_get_net_locked(LNET_NIDNET(ni->ni_nid));
4401 lnet_net_unlock(cpt);
4402 CERROR("net %s not found\n",
4403 libcfs_net2str(LNET_NIDNET(ni->ni_nid)));
4407 rnet = lnet_find_rnet_locked(LNET_NIDNET(src_nid));
4409 struct lnet_peer *gw = NULL;
4410 struct lnet_peer_ni *lpni = NULL;
4411 struct lnet_route *route;
4413 list_for_each_entry(route, &rnet->lrn_routes, lr_list) {
4415 gw = route->lr_gateway;
4416 if (route->lr_lnet != net->net_id)
4419 * if the nid is one of the gateway's NIDs
4420 * then this is a valid gateway
4422 while ((lpni = lnet_get_next_peer_ni_locked(gw,
4423 NULL, lpni)) != NULL) {
4424 if (lpni->lpni_nid == from_nid) {
4431 lnet_net_unlock(cpt);
4433 /* we would not use from_nid to route a message to
4435 * => asymmetric routing detected but forbidden
4437 CERROR("%s, src %s: Dropping asymmetrical route %s\n",
4438 libcfs_nid2str(from_nid),
4439 libcfs_nid2str(src_nid), lnet_msgtyp2str(type));
4444 msg = lnet_msg_alloc();
4446 CERROR("%s, src %s: Dropping %s (out of memory)\n",
4447 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4448 lnet_msgtyp2str(type));
4452 /* msg zeroed in lnet_msg_alloc; i.e. flags all clear,
4453 * pointers NULL etc */
4455 msg->msg_type = type;
4456 msg->msg_private = private;
4457 msg->msg_receiving = 1;
4458 msg->msg_rdma_get = rdma_req;
4459 msg->msg_len = msg->msg_wanted = payload_length;
4460 msg->msg_offset = 0;
4461 msg->msg_hdr = *hdr;
4462 /* for building message event */
4463 msg->msg_from = from_nid;
4465 msg->msg_target.pid = dest_pid;
4466 msg->msg_target.nid = dest_nid;
4467 msg->msg_routing = 1;
4470 /* convert common msg->hdr fields to host byteorder */
4471 msg->msg_hdr.type = type;
4472 msg->msg_hdr.src_nid = src_nid;
4473 msg->msg_hdr.src_pid = le32_to_cpu(msg->msg_hdr.src_pid);
4474 msg->msg_hdr.dest_nid = dest_nid;
4475 msg->msg_hdr.dest_pid = dest_pid;
4476 msg->msg_hdr.payload_length = payload_length;
4480 lpni = lnet_nid2peerni_locked(from_nid, ni->ni_nid, cpt);
4482 lnet_net_unlock(cpt);
4483 CERROR("%s, src %s: Dropping %s "
4484 "(error %ld looking up sender)\n",
4485 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4486 lnet_msgtyp2str(type), PTR_ERR(lpni));
4488 if (rc == -ESHUTDOWN)
4489 /* We are shutting down. Don't do anything more */
4494 if (the_lnet.ln_routing)
4495 lpni->lpni_last_alive = ktime_get_seconds();
4497 msg->msg_rxpeer = lpni;
4499 lnet_ni_addref_locked(ni, cpt);
4500 /* Multi-Rail: Primary NID of source. */
4501 msg->msg_initiator = lnet_peer_primary_nid_locked(src_nid);
4504 * mark the status of this lpni as UP since we received a message
4505 * from it. The ping response reports back the ns_status which is
4506 * marked on the remote as up or down and we cache it here.
4508 msg->msg_rxpeer->lpni_ns_status = LNET_NI_STATUS_UP;
4510 lnet_msg_commit(msg, cpt);
4512 /* message delay simulation */
4513 if (unlikely(!list_empty(&the_lnet.ln_delay_rules) &&
4514 lnet_delay_rule_match_locked(hdr, msg))) {
4515 lnet_net_unlock(cpt);
4520 rc = lnet_parse_forward_locked(ni, msg);
4521 lnet_net_unlock(cpt);
4526 if (rc == LNET_CREDIT_OK) {
4527 lnet_ni_recv(ni, msg->msg_private, msg, 0,
4528 0, payload_length, payload_length);
4533 lnet_net_unlock(cpt);
4535 rc = lnet_parse_local(ni, msg);
4541 LASSERT(msg->msg_md == NULL);
4542 lnet_finalize(msg, rc);
4545 lnet_drop_message(ni, cpt, private, payload_length, type);
4548 EXPORT_SYMBOL(lnet_parse);
4551 lnet_drop_delayed_msg_list(struct list_head *head, char *reason)
4553 while (!list_empty(head)) {
4554 struct lnet_process_id id = {0};
4555 struct lnet_msg *msg;
4557 msg = list_entry(head->next, struct lnet_msg, msg_list);
4558 list_del(&msg->msg_list);
4560 id.nid = msg->msg_hdr.src_nid;
4561 id.pid = msg->msg_hdr.src_pid;
4563 LASSERT(msg->msg_md == NULL);
4564 LASSERT(msg->msg_rx_delayed);
4565 LASSERT(msg->msg_rxpeer != NULL);
4566 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
4568 CWARN("Dropping delayed PUT from %s portal %d match %llu"
4569 " offset %d length %d: %s\n",
4571 msg->msg_hdr.msg.put.ptl_index,
4572 msg->msg_hdr.msg.put.match_bits,
4573 msg->msg_hdr.msg.put.offset,
4574 msg->msg_hdr.payload_length, reason);
4576 /* NB I can't drop msg's ref on msg_rxpeer until after I've
4577 * called lnet_drop_message(), so I just hang onto msg as well
4578 * until that's done */
4580 lnet_drop_message(msg->msg_rxni, msg->msg_rx_cpt,
4581 msg->msg_private, msg->msg_len,
4584 msg->msg_no_resend = true;
4586 * NB: message will not generate event because w/o attached MD,
4587 * but we still should give error code so lnet_msg_decommit()
4588 * can skip counters operations and other checks.
4590 lnet_finalize(msg, -ENOENT);
4595 lnet_recv_delayed_msg_list(struct list_head *head)
4597 while (!list_empty(head)) {
4598 struct lnet_msg *msg;
4599 struct lnet_process_id id;
4601 msg = list_entry(head->next, struct lnet_msg, msg_list);
4602 list_del(&msg->msg_list);
4604 /* md won't disappear under me, since each msg
4605 * holds a ref on it */
4607 id.nid = msg->msg_hdr.src_nid;
4608 id.pid = msg->msg_hdr.src_pid;
4610 LASSERT(msg->msg_rx_delayed);
4611 LASSERT(msg->msg_md != NULL);
4612 LASSERT(msg->msg_rxpeer != NULL);
4613 LASSERT(msg->msg_rxni != NULL);
4614 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
4616 CDEBUG(D_NET, "Resuming delayed PUT from %s portal %d "
4617 "match %llu offset %d length %d.\n",
4618 libcfs_id2str(id), msg->msg_hdr.msg.put.ptl_index,
4619 msg->msg_hdr.msg.put.match_bits,
4620 msg->msg_hdr.msg.put.offset,
4621 msg->msg_hdr.payload_length);
4623 lnet_recv_put(msg->msg_rxni, msg);
4628 lnet_attach_rsp_tracker(struct lnet_rsp_tracker *rspt, int cpt,
4629 struct lnet_libmd *md, struct lnet_handle_md mdh)
4632 struct lnet_rsp_tracker *local_rspt;
4635 * MD has a refcount taken by message so it's not going away.
4636 * The MD however can be looked up. We need to secure the access
4637 * to the md_rspt_ptr by taking the res_lock.
4638 * The rspt can be accessed without protection up to when it gets
4639 * added to the list.
4643 local_rspt = md->md_rspt_ptr;
4644 timeout_ns = lnet_transaction_timeout * NSEC_PER_SEC;
4645 if (local_rspt != NULL) {
4647 * we already have an rspt attached to the md, so we'll
4648 * update the deadline on that one.
4650 lnet_rspt_free(rspt, cpt);
4653 rspt->rspt_mdh = mdh;
4654 rspt->rspt_cpt = cpt;
4655 /* store the rspt so we can access it when we get the REPLY */
4656 md->md_rspt_ptr = rspt;
4659 local_rspt->rspt_deadline = ktime_add_ns(ktime_get(), timeout_ns);
4662 * add to the list of tracked responses. It's added to tail of the
4663 * list in order to expire all the older entries first.
4666 list_move_tail(&local_rspt->rspt_on_list, the_lnet.ln_mt_rstq[cpt]);
4667 lnet_net_unlock(cpt);
4668 lnet_res_unlock(cpt);
4672 * Initiate an asynchronous PUT operation.
4674 * There are several events associated with a PUT: completion of the send on
4675 * the initiator node (LNET_EVENT_SEND), and when the send completes
4676 * successfully, the receipt of an acknowledgment (LNET_EVENT_ACK) indicating
4677 * that the operation was accepted by the target. The event LNET_EVENT_PUT is
4678 * used at the target node to indicate the completion of incoming data
4681 * The local events will be logged in the EQ associated with the MD pointed to
4682 * by \a mdh handle. Using a MD without an associated EQ results in these
4683 * events being discarded. In this case, the caller must have another
4684 * mechanism (e.g., a higher level protocol) for determining when it is safe
4685 * to modify the memory region associated with the MD.
4687 * Note that LNet does not guarantee the order of LNET_EVENT_SEND and
4688 * LNET_EVENT_ACK, though intuitively ACK should happen after SEND.
4690 * \param self Indicates the NID of a local interface through which to send
4691 * the PUT request. Use LNET_NID_ANY to let LNet choose one by itself.
4692 * \param mdh A handle for the MD that describes the memory to be sent. The MD
4693 * must be "free floating" (See LNetMDBind()).
4694 * \param ack Controls whether an acknowledgment is requested.
4695 * Acknowledgments are only sent when they are requested by the initiating
4696 * process and the target MD enables them.
4697 * \param target A process identifier for the target process.
4698 * \param portal The index in the \a target's portal table.
4699 * \param match_bits The match bits to use for MD selection at the target
4701 * \param offset The offset into the target MD (only used when the target
4702 * MD has the LNET_MD_MANAGE_REMOTE option set).
4703 * \param hdr_data 64 bits of user data that can be included in the message
4704 * header. This data is written to an event queue entry at the target if an
4705 * EQ is present on the matching MD.
4707 * \retval 0 Success, and only in this case events will be generated
4708 * and logged to EQ (if it exists).
4709 * \retval -EIO Simulated failure.
4710 * \retval -ENOMEM Memory allocation failure.
4711 * \retval -ENOENT Invalid MD object.
4713 * \see struct lnet_event::hdr_data and lnet_event_kind_t.
4716 LNetPut(lnet_nid_t self, struct lnet_handle_md mdh, enum lnet_ack_req ack,
4717 struct lnet_process_id target, unsigned int portal,
4718 __u64 match_bits, unsigned int offset,
4721 struct lnet_msg *msg;
4722 struct lnet_libmd *md;
4725 struct lnet_rsp_tracker *rspt = NULL;
4727 LASSERT(the_lnet.ln_refcount > 0);
4729 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
4730 fail_peer(target.nid, 1)) { /* shall we now? */
4731 CERROR("Dropping PUT to %s: simulated failure\n",
4732 libcfs_id2str(target));
4736 msg = lnet_msg_alloc();
4738 CERROR("Dropping PUT to %s: ENOMEM on struct lnet_msg\n",
4739 libcfs_id2str(target));
4742 msg->msg_vmflush = !!(current->flags & PF_MEMALLOC);
4744 cpt = lnet_cpt_of_cookie(mdh.cookie);
4746 if (ack == LNET_ACK_REQ) {
4747 rspt = lnet_rspt_alloc(cpt);
4749 CERROR("Dropping PUT to %s: ENOMEM on response tracker\n",
4750 libcfs_id2str(target));
4753 INIT_LIST_HEAD(&rspt->rspt_on_list);
4758 md = lnet_handle2md(&mdh);
4759 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
4760 CERROR("Dropping PUT (%llu:%d:%s): MD (%d) invalid\n",
4761 match_bits, portal, libcfs_id2str(target),
4762 md == NULL ? -1 : md->md_threshold);
4763 if (md != NULL && md->md_me != NULL)
4764 CERROR("Source MD also attached to portal %d\n",
4765 md->md_me->me_portal);
4766 lnet_res_unlock(cpt);
4769 lnet_rspt_free(rspt, cpt);
4775 CDEBUG(D_NET, "LNetPut -> %s\n", libcfs_id2str(target));
4777 lnet_msg_attach_md(msg, md, 0, 0);
4779 lnet_prep_send(msg, LNET_MSG_PUT, target, 0, md->md_length);
4781 msg->msg_hdr.msg.put.match_bits = cpu_to_le64(match_bits);
4782 msg->msg_hdr.msg.put.ptl_index = cpu_to_le32(portal);
4783 msg->msg_hdr.msg.put.offset = cpu_to_le32(offset);
4784 msg->msg_hdr.msg.put.hdr_data = hdr_data;
4786 /* NB handles only looked up by creator (no flips) */
4787 if (ack == LNET_ACK_REQ) {
4788 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
4789 the_lnet.ln_interface_cookie;
4790 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
4791 md->md_lh.lh_cookie;
4793 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
4794 LNET_WIRE_HANDLE_COOKIE_NONE;
4795 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
4796 LNET_WIRE_HANDLE_COOKIE_NONE;
4799 lnet_res_unlock(cpt);
4801 lnet_build_msg_event(msg, LNET_EVENT_SEND);
4803 if (rspt && lnet_response_tracking_enabled(LNET_MSG_PUT,
4805 lnet_attach_rsp_tracker(rspt, cpt, md, mdh);
4807 lnet_rspt_free(rspt, cpt);
4809 if (CFS_FAIL_CHECK_ORSET(CFS_FAIL_PTLRPC_OST_BULK_CB2,
4813 rc = lnet_send(self, msg, LNET_NID_ANY);
4816 CNETERR("Error sending PUT to %s: %d\n",
4817 libcfs_id2str(target), rc);
4818 msg->msg_no_resend = true;
4819 lnet_finalize(msg, rc);
4822 /* completion will be signalled by an event */
4825 EXPORT_SYMBOL(LNetPut);
4828 * The LND can DMA direct to the GET md (i.e. no REPLY msg). This
4829 * returns a msg for the LND to pass to lnet_finalize() when the sink
4830 * data has been received.
4832 * CAVEAT EMPTOR: 'getmsg' is the original GET, which is freed when
4833 * lnet_finalize() is called on it, so the LND must call this first
4836 lnet_create_reply_msg(struct lnet_ni *ni, struct lnet_msg *getmsg)
4838 struct lnet_msg *msg = lnet_msg_alloc();
4839 struct lnet_libmd *getmd = getmsg->msg_md;
4840 struct lnet_process_id peer_id = getmsg->msg_target;
4843 LASSERT(!getmsg->msg_target_is_router);
4844 LASSERT(!getmsg->msg_routing);
4847 CERROR("%s: Dropping REPLY from %s: can't allocate msg\n",
4848 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id));
4852 cpt = lnet_cpt_of_cookie(getmd->md_lh.lh_cookie);
4855 LASSERT(getmd->md_refcount > 0);
4857 if (getmd->md_threshold == 0) {
4858 CERROR("%s: Dropping REPLY from %s for inactive MD %p\n",
4859 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id),
4861 lnet_res_unlock(cpt);
4865 LASSERT(getmd->md_offset == 0);
4867 CDEBUG(D_NET, "%s: Reply from %s md %p\n",
4868 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id), getmd);
4870 /* setup information for lnet_build_msg_event */
4871 msg->msg_initiator = getmsg->msg_txpeer->lpni_peer_net->lpn_peer->lp_primary_nid;
4872 msg->msg_from = peer_id.nid;
4873 msg->msg_type = LNET_MSG_GET; /* flag this msg as an "optimized" GET */
4874 msg->msg_hdr.src_nid = peer_id.nid;
4875 msg->msg_hdr.payload_length = getmd->md_length;
4876 msg->msg_receiving = 1; /* required by lnet_msg_attach_md */
4878 lnet_msg_attach_md(msg, getmd, getmd->md_offset, getmd->md_length);
4879 lnet_res_unlock(cpt);
4881 cpt = lnet_cpt_of_nid(peer_id.nid, ni);
4884 lnet_msg_commit(msg, cpt);
4885 lnet_net_unlock(cpt);
4887 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
4892 cpt = lnet_cpt_of_nid(peer_id.nid, ni);
4895 lnet_incr_stats(&ni->ni_stats, LNET_MSG_GET, LNET_STATS_TYPE_DROP);
4896 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
4897 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length +=
4899 lnet_net_unlock(cpt);
4906 EXPORT_SYMBOL(lnet_create_reply_msg);
4909 lnet_set_reply_msg_len(struct lnet_ni *ni, struct lnet_msg *reply,
4912 /* Set the REPLY length, now the RDMA that elides the REPLY message has
4913 * completed and I know it. */
4914 LASSERT(reply != NULL);
4915 LASSERT(reply->msg_type == LNET_MSG_GET);
4916 LASSERT(reply->msg_ev.type == LNET_EVENT_REPLY);
4918 /* NB I trusted my peer to RDMA. If she tells me she's written beyond
4919 * the end of my buffer, I might as well be dead. */
4920 LASSERT(len <= reply->msg_ev.mlength);
4922 reply->msg_ev.mlength = len;
4924 EXPORT_SYMBOL(lnet_set_reply_msg_len);
4927 * Initiate an asynchronous GET operation.
4929 * On the initiator node, an LNET_EVENT_SEND is logged when the GET request
4930 * is sent, and an LNET_EVENT_REPLY is logged when the data returned from
4931 * the target node in the REPLY has been written to local MD.
4933 * On the target node, an LNET_EVENT_GET is logged when the GET request
4934 * arrives and is accepted into a MD.
4936 * \param self,target,portal,match_bits,offset See the discussion in LNetPut().
4937 * \param mdh A handle for the MD that describes the memory into which the
4938 * requested data will be received. The MD must be "free floating" (See LNetMDBind()).
4940 * \retval 0 Success, and only in this case events will be generated
4941 * and logged to EQ (if it exists) of the MD.
4942 * \retval -EIO Simulated failure.
4943 * \retval -ENOMEM Memory allocation failure.
4944 * \retval -ENOENT Invalid MD object.
4947 LNetGet(lnet_nid_t self, struct lnet_handle_md mdh,
4948 struct lnet_process_id target, unsigned int portal,
4949 __u64 match_bits, unsigned int offset, bool recovery)
4951 struct lnet_msg *msg;
4952 struct lnet_libmd *md;
4953 struct lnet_rsp_tracker *rspt;
4957 LASSERT(the_lnet.ln_refcount > 0);
4959 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
4960 fail_peer(target.nid, 1)) /* shall we now? */
4962 CERROR("Dropping GET to %s: simulated failure\n",
4963 libcfs_id2str(target));
4967 msg = lnet_msg_alloc();
4969 CERROR("Dropping GET to %s: ENOMEM on struct lnet_msg\n",
4970 libcfs_id2str(target));
4974 cpt = lnet_cpt_of_cookie(mdh.cookie);
4976 rspt = lnet_rspt_alloc(cpt);
4978 CERROR("Dropping GET to %s: ENOMEM on response tracker\n",
4979 libcfs_id2str(target));
4982 INIT_LIST_HEAD(&rspt->rspt_on_list);
4984 msg->msg_recovery = recovery;
4988 md = lnet_handle2md(&mdh);
4989 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
4990 CERROR("Dropping GET (%llu:%d:%s): MD (%d) invalid\n",
4991 match_bits, portal, libcfs_id2str(target),
4992 md == NULL ? -1 : md->md_threshold);
4993 if (md != NULL && md->md_me != NULL)
4994 CERROR("REPLY MD also attached to portal %d\n",
4995 md->md_me->me_portal);
4997 lnet_res_unlock(cpt);
5000 lnet_rspt_free(rspt, cpt);
5004 CDEBUG(D_NET, "LNetGet -> %s\n", libcfs_id2str(target));
5006 lnet_msg_attach_md(msg, md, 0, 0);
5008 lnet_prep_send(msg, LNET_MSG_GET, target, 0, 0);
5010 msg->msg_hdr.msg.get.match_bits = cpu_to_le64(match_bits);
5011 msg->msg_hdr.msg.get.ptl_index = cpu_to_le32(portal);
5012 msg->msg_hdr.msg.get.src_offset = cpu_to_le32(offset);
5013 msg->msg_hdr.msg.get.sink_length = cpu_to_le32(md->md_length);
5015 /* NB handles only looked up by creator (no flips) */
5016 msg->msg_hdr.msg.get.return_wmd.wh_interface_cookie =
5017 the_lnet.ln_interface_cookie;
5018 msg->msg_hdr.msg.get.return_wmd.wh_object_cookie =
5019 md->md_lh.lh_cookie;
5021 lnet_res_unlock(cpt);
5023 lnet_build_msg_event(msg, LNET_EVENT_SEND);
5025 if (lnet_response_tracking_enabled(LNET_MSG_GET, md->md_options))
5026 lnet_attach_rsp_tracker(rspt, cpt, md, mdh);
5028 lnet_rspt_free(rspt, cpt);
5030 rc = lnet_send(self, msg, LNET_NID_ANY);
5032 CNETERR("Error sending GET to %s: %d\n",
5033 libcfs_id2str(target), rc);
5034 msg->msg_no_resend = true;
5035 lnet_finalize(msg, rc);
5038 /* completion will be signalled by an event */
5041 EXPORT_SYMBOL(LNetGet);
5044 * Calculate distance to node at \a dstnid.
5046 * \param dstnid Target NID.
5047 * \param srcnidp If not NULL, NID of the local interface to reach \a dstnid
5049 * \param orderp If not NULL, order of the route to reach \a dstnid is saved
5052 * \retval 0 If \a dstnid belongs to a local interface, and reserved option
5053 * local_nid_dist_zero is set, which is the default.
5054 * \retval positives Distance to target NID, i.e. number of hops plus one.
5055 * \retval -EHOSTUNREACH If \a dstnid is not reachable.
5058 LNetDist(lnet_nid_t dstnid, lnet_nid_t *srcnidp, __u32 *orderp)
5060 struct list_head *e;
5061 struct lnet_ni *ni = NULL;
5062 struct lnet_remotenet *rnet;
5063 __u32 dstnet = LNET_NIDNET(dstnid);
5067 struct list_head *rn_list;
5069 /* if !local_nid_dist_zero, I don't return a distance of 0 ever
5070 * (when lustre sees a distance of 0, it substitutes 0@lo), so I
5071 * keep order 0 free for 0@lo and order 1 free for a local NID
5074 LASSERT(the_lnet.ln_refcount > 0);
5076 cpt = lnet_net_lock_current();
5078 while ((ni = lnet_get_next_ni_locked(NULL, ni))) {
5079 if (ni->ni_nid == dstnid) {
5080 if (srcnidp != NULL)
5082 if (orderp != NULL) {
5083 if (dstnid == LNET_NID_LO_0)
5088 lnet_net_unlock(cpt);
5090 return local_nid_dist_zero ? 0 : 1;
5093 if (LNET_NIDNET(ni->ni_nid) == dstnet) {
5094 /* Check if ni was originally created in
5095 * current net namespace.
5096 * If not, assign order above 0xffff0000,
5097 * to make this ni not a priority. */
5098 if (current->nsproxy &&
5099 !net_eq(ni->ni_net_ns, current->nsproxy->net_ns))
5100 order += 0xffff0000;
5101 if (srcnidp != NULL)
5102 *srcnidp = ni->ni_nid;
5105 lnet_net_unlock(cpt);
5112 rn_list = lnet_net2rnethash(dstnet);
5113 list_for_each(e, rn_list) {
5114 rnet = list_entry(e, struct lnet_remotenet, lrn_list);
5116 if (rnet->lrn_net == dstnet) {
5117 struct lnet_route *route;
5118 struct lnet_route *shortest = NULL;
5119 __u32 shortest_hops = LNET_UNDEFINED_HOPS;
5122 LASSERT(!list_empty(&rnet->lrn_routes));
5124 list_for_each_entry(route, &rnet->lrn_routes,
5126 route_hops = route->lr_hops;
5127 if (route_hops == LNET_UNDEFINED_HOPS)
5129 if (shortest == NULL ||
5130 route_hops < shortest_hops) {
5132 shortest_hops = route_hops;
5136 LASSERT(shortest != NULL);
5137 hops = shortest_hops;
5138 if (srcnidp != NULL) {
5139 struct lnet_net *net;
5140 net = lnet_get_net_locked(shortest->lr_lnet);
5142 ni = lnet_get_next_ni_locked(net, NULL);
5143 *srcnidp = ni->ni_nid;
5147 lnet_net_unlock(cpt);
5153 lnet_net_unlock(cpt);
5154 return -EHOSTUNREACH;
5156 EXPORT_SYMBOL(LNetDist);