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);
1286 static struct lnet_peer_ni *
1287 lnet_select_peer_ni(struct lnet_ni *best_ni, lnet_nid_t dst_nid,
1288 struct lnet_peer *peer,
1289 struct lnet_peer_ni *best_lpni,
1290 struct lnet_peer_net *peer_net)
1293 * Look at the peer NIs for the destination peer that connect
1294 * to the chosen net. If a peer_ni is preferred when using the
1295 * best_ni to communicate, we use that one. If there is no
1296 * preferred peer_ni, or there are multiple preferred peer_ni,
1297 * the available transmit credits are used. If the transmit
1298 * credits are equal, we round-robin over the peer_ni.
1300 struct lnet_peer_ni *lpni = NULL;
1301 int best_lpni_credits = (best_lpni) ? best_lpni->lpni_txcredits :
1303 int best_lpni_healthv = (best_lpni) ?
1304 atomic_read(&best_lpni->lpni_healthv) : 0;
1305 bool preferred = false;
1309 while ((lpni = lnet_get_next_peer_ni_locked(peer, peer_net, lpni))) {
1311 * if the best_ni we've chosen aleady has this lpni
1312 * preferred, then let's use it
1315 ni_is_pref = lnet_peer_is_pref_nid_locked(lpni,
1317 CDEBUG(D_NET, "%s ni_is_pref = %d\n",
1318 libcfs_nid2str(best_ni->ni_nid), ni_is_pref);
1323 lpni_healthv = atomic_read(&lpni->lpni_healthv);
1326 CDEBUG(D_NET, "%s c:[%d, %d], s:[%d, %d]\n",
1327 libcfs_nid2str(lpni->lpni_nid),
1328 lpni->lpni_txcredits, best_lpni_credits,
1329 lpni->lpni_seq, best_lpni->lpni_seq);
1331 /* pick the healthiest peer ni */
1332 if (lpni_healthv < best_lpni_healthv) {
1334 } else if (lpni_healthv > best_lpni_healthv) {
1335 best_lpni_healthv = lpni_healthv;
1336 /* if this is a preferred peer use it */
1337 } else if (!preferred && ni_is_pref) {
1339 } else if (preferred && !ni_is_pref) {
1341 * this is not the preferred peer so let's ignore
1345 } else if (lpni->lpni_txcredits < best_lpni_credits) {
1347 * We already have a peer that has more credits
1348 * available than this one. No need to consider
1349 * this peer further.
1352 } else if (lpni->lpni_txcredits == best_lpni_credits) {
1354 * The best peer found so far and the current peer
1355 * have the same number of available credits let's
1356 * make sure to select between them using Round
1360 if (best_lpni->lpni_seq <= lpni->lpni_seq)
1366 best_lpni_credits = lpni->lpni_txcredits;
1369 /* if we still can't find a peer ni then we can't reach it */
1371 __u32 net_id = (peer_net) ? peer_net->lpn_net_id :
1372 LNET_NIDNET(dst_nid);
1373 CDEBUG(D_NET, "no peer_ni found on peer net %s\n",
1374 libcfs_net2str(net_id));
1378 CDEBUG(D_NET, "sd_best_lpni = %s\n",
1379 libcfs_nid2str(best_lpni->lpni_nid));
1385 * Prerequisite: the best_ni should already be set in the sd
1386 * Find the best lpni.
1387 * If the net id is provided then restrict lpni selection on
1388 * that particular net.
1389 * Otherwise find any reachable lpni. When dealing with an MR
1390 * gateway and it has multiple lpnis which we can use
1391 * we want to select the best one from the list of reachable
1394 static inline struct lnet_peer_ni *
1395 lnet_find_best_lpni(struct lnet_ni *lni, lnet_nid_t dst_nid,
1396 struct lnet_peer *peer, __u32 net_id)
1398 struct lnet_peer_net *peer_net;
1400 /* find the best_lpni on any local network */
1401 if (net_id == LNET_NET_ANY) {
1402 struct lnet_peer_ni *best_lpni = NULL;
1403 struct lnet_peer_net *lpn;
1404 list_for_each_entry(lpn, &peer->lp_peer_nets, lpn_peer_nets) {
1405 /* no net specified find any reachable peer ni */
1406 if (!lnet_islocalnet_locked(lpn->lpn_net_id))
1408 best_lpni = lnet_select_peer_ni(lni, dst_nid, peer,
1414 /* restrict on the specified net */
1415 peer_net = lnet_peer_get_net_locked(peer, net_id);
1417 return lnet_select_peer_ni(lni, dst_nid, peer, NULL, peer_net);
1423 lnet_compare_gw_lpnis(struct lnet_peer_ni *lpni1, struct lnet_peer_ni *lpni2)
1425 if (lpni1->lpni_txqnob < lpni2->lpni_txqnob)
1428 if (lpni1->lpni_txqnob > lpni2->lpni_txqnob)
1431 if (lpni1->lpni_txcredits > lpni2->lpni_txcredits)
1434 if (lpni1->lpni_txcredits < lpni2->lpni_txcredits)
1440 /* Compare route priorities and hop counts */
1442 lnet_compare_routes(struct lnet_route *r1, struct lnet_route *r2)
1444 int r1_hops = (r1->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r1->lr_hops;
1445 int r2_hops = (r2->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r2->lr_hops;
1447 if (r1->lr_priority < r2->lr_priority)
1450 if (r1->lr_priority > r2->lr_priority)
1453 if (r1_hops < r2_hops)
1456 if (r1_hops > r2_hops)
1462 static struct lnet_route *
1463 lnet_find_route_locked(struct lnet_remotenet *rnet, __u32 src_net,
1464 struct lnet_peer_ni *remote_lpni,
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;
1473 bool best_rte_is_preferred = false;
1476 CDEBUG(D_NET, "Looking up a route to %s, from %s\n",
1477 libcfs_net2str(rnet->lrn_net), libcfs_net2str(src_net));
1479 best_route = last_route = NULL;
1480 list_for_each_entry(route, &rnet->lrn_routes, lr_list) {
1481 if (!lnet_is_route_alive(route))
1483 gw_pnid = route->lr_gateway->lp_primary_nid;
1485 /* no protection on below fields, but it's harmless */
1486 if (last_route && (last_route->lr_seq - route->lr_seq < 0))
1489 /* if the best route found is in the preferred list then
1490 * tag it as preferred and use it later on. But if we
1491 * didn't find any routes which are on the preferred list
1492 * then just use the best route possible.
1494 rc = lnet_peer_is_pref_rtr_locked(remote_lpni, gw_pnid);
1496 if (!best_route || (rc && !best_rte_is_preferred)) {
1497 /* Restrict the selection of the router NI on the
1498 * src_net provided. If the src_net is LNET_NID_ANY,
1499 * then select the best interface available.
1501 lpni = lnet_find_best_lpni(NULL, LNET_NID_ANY,
1506 "Gateway %s does not have a peer NI on net %s\n",
1507 libcfs_nid2str(gw_pnid),
1508 libcfs_net2str(src_net));
1513 if (rc && !best_rte_is_preferred) {
1514 /* This is the first preferred route we found,
1515 * so it beats any route found previously
1521 best_rte_is_preferred = true;
1522 CDEBUG(D_NET, "preferred gw = %s\n",
1523 libcfs_nid2str(gw_pnid));
1525 } else if ((!rc) && best_rte_is_preferred)
1526 /* The best route we found so far is in the preferred
1527 * list, so it beats any non-preferred route
1532 best_route = last_route = route;
1537 rc = lnet_compare_routes(route, best_route);
1541 /* Restrict the selection of the router NI on the
1542 * src_net provided. If the src_net is LNET_NID_ANY,
1543 * then select the best interface available.
1545 lpni = lnet_find_best_lpni(NULL, LNET_NID_ANY,
1550 "Gateway %s does not have a peer NI on net %s\n",
1551 libcfs_nid2str(gw_pnid),
1552 libcfs_net2str(src_net));
1562 rc = lnet_compare_gw_lpnis(lpni, best_gw_ni);
1566 if (rc == 1 || route->lr_seq <= best_route->lr_seq) {
1573 *prev_route = last_route;
1579 static struct lnet_ni *
1580 lnet_get_best_ni(struct lnet_net *local_net, struct lnet_ni *best_ni,
1581 struct lnet_peer *peer, struct lnet_peer_net *peer_net,
1584 struct lnet_ni *ni = NULL;
1585 unsigned int shortest_distance;
1590 * If there is no peer_ni that we can send to on this network,
1591 * then there is no point in looking for a new best_ni here.
1593 if (!lnet_get_next_peer_ni_locked(peer, peer_net, NULL))
1596 if (best_ni == NULL) {
1597 shortest_distance = UINT_MAX;
1598 best_credits = INT_MIN;
1601 shortest_distance = cfs_cpt_distance(lnet_cpt_table(), md_cpt,
1602 best_ni->ni_dev_cpt);
1603 best_credits = atomic_read(&best_ni->ni_tx_credits);
1604 best_healthv = atomic_read(&best_ni->ni_healthv);
1607 while ((ni = lnet_get_next_ni_locked(local_net, ni))) {
1608 unsigned int distance;
1613 ni_credits = atomic_read(&ni->ni_tx_credits);
1614 ni_healthv = atomic_read(&ni->ni_healthv);
1615 ni_fatal = atomic_read(&ni->ni_fatal_error_on);
1618 * calculate the distance from the CPT on which
1619 * the message memory is allocated to the CPT of
1620 * the NI's physical device
1622 distance = cfs_cpt_distance(lnet_cpt_table(),
1626 CDEBUG(D_NET, "compare ni %s [c:%d, d:%d, s:%d] with best_ni %s [c:%d, d:%d, s:%d]\n",
1627 libcfs_nid2str(ni->ni_nid), ni_credits, distance,
1628 ni->ni_seq, (best_ni) ? libcfs_nid2str(best_ni->ni_nid)
1629 : "not seleced", best_credits, shortest_distance,
1630 (best_ni) ? best_ni->ni_seq : 0);
1633 * All distances smaller than the NUMA range
1634 * are treated equally.
1636 if (distance < lnet_numa_range)
1637 distance = lnet_numa_range;
1640 * Select on health, shorter distance, available
1641 * credits, then round-robin.
1645 } else if (ni_healthv < best_healthv) {
1647 } else if (ni_healthv > best_healthv) {
1648 best_healthv = ni_healthv;
1650 * If we're going to prefer this ni because it's
1651 * the healthiest, then we should set the
1652 * shortest_distance in the algorithm in case
1653 * there are multiple NIs with the same health but
1654 * different distances.
1656 if (distance < shortest_distance)
1657 shortest_distance = distance;
1658 } else if (distance > shortest_distance) {
1660 } else if (distance < shortest_distance) {
1661 shortest_distance = distance;
1662 } else if (ni_credits < best_credits) {
1664 } else if (ni_credits == best_credits) {
1665 if (best_ni && best_ni->ni_seq <= ni->ni_seq)
1669 best_credits = ni_credits;
1672 CDEBUG(D_NET, "selected best_ni %s\n",
1673 (best_ni) ? libcfs_nid2str(best_ni->ni_nid) : "no selection");
1679 * Traffic to the LNET_RESERVED_PORTAL may not trigger peer discovery,
1680 * because such traffic is required to perform discovery. We therefore
1681 * exclude all GET and PUT on that portal. We also exclude all ACK and
1682 * REPLY traffic, but that is because the portal is not tracked in the
1683 * message structure for these message types. We could restrict this
1684 * further by also checking for LNET_PROTO_PING_MATCHBITS.
1687 lnet_msg_discovery(struct lnet_msg *msg)
1689 if (msg->msg_type == LNET_MSG_PUT) {
1690 if (msg->msg_hdr.msg.put.ptl_index != LNET_RESERVED_PORTAL)
1692 } else if (msg->msg_type == LNET_MSG_GET) {
1693 if (msg->msg_hdr.msg.get.ptl_index != LNET_RESERVED_PORTAL)
1699 #define SRC_SPEC 0x0001
1700 #define SRC_ANY 0x0002
1701 #define LOCAL_DST 0x0004
1702 #define REMOTE_DST 0x0008
1703 #define MR_DST 0x0010
1704 #define NMR_DST 0x0020
1705 #define SND_RESP 0x0040
1707 /* The following to defines are used for return codes */
1708 #define REPEAT_SEND 0x1000
1709 #define PASS_THROUGH 0x2000
1711 /* The different cases lnet_select pathway needs to handle */
1712 #define SRC_SPEC_LOCAL_MR_DST (SRC_SPEC | LOCAL_DST | MR_DST)
1713 #define SRC_SPEC_ROUTER_MR_DST (SRC_SPEC | REMOTE_DST | MR_DST)
1714 #define SRC_SPEC_LOCAL_NMR_DST (SRC_SPEC | LOCAL_DST | NMR_DST)
1715 #define SRC_SPEC_ROUTER_NMR_DST (SRC_SPEC | REMOTE_DST | NMR_DST)
1716 #define SRC_ANY_LOCAL_MR_DST (SRC_ANY | LOCAL_DST | MR_DST)
1717 #define SRC_ANY_ROUTER_MR_DST (SRC_ANY | REMOTE_DST | MR_DST)
1718 #define SRC_ANY_LOCAL_NMR_DST (SRC_ANY | LOCAL_DST | NMR_DST)
1719 #define SRC_ANY_ROUTER_NMR_DST (SRC_ANY | REMOTE_DST | NMR_DST)
1722 lnet_handle_lo_send(struct lnet_send_data *sd)
1724 struct lnet_msg *msg = sd->sd_msg;
1725 int cpt = sd->sd_cpt;
1727 /* No send credit hassles with LOLND */
1728 lnet_ni_addref_locked(the_lnet.ln_loni, cpt);
1729 msg->msg_hdr.dest_nid = cpu_to_le64(the_lnet.ln_loni->ni_nid);
1730 if (!msg->msg_routing)
1731 msg->msg_hdr.src_nid =
1732 cpu_to_le64(the_lnet.ln_loni->ni_nid);
1733 msg->msg_target.nid = the_lnet.ln_loni->ni_nid;
1734 lnet_msg_commit(msg, cpt);
1735 msg->msg_txni = the_lnet.ln_loni;
1737 return LNET_CREDIT_OK;
1741 lnet_handle_send(struct lnet_send_data *sd)
1743 struct lnet_ni *best_ni = sd->sd_best_ni;
1744 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
1745 struct lnet_peer_ni *final_dst_lpni = sd->sd_final_dst_lpni;
1746 struct lnet_msg *msg = sd->sd_msg;
1748 __u32 send_case = sd->sd_send_case;
1750 __u32 routing = send_case & REMOTE_DST;
1751 struct lnet_rsp_tracker *rspt;
1754 * Increment sequence number of the selected peer so that we
1755 * pick the next one in Round Robin.
1757 best_lpni->lpni_seq++;
1760 * grab a reference on the peer_ni so it sticks around even if
1761 * we need to drop and relock the lnet_net_lock below.
1763 lnet_peer_ni_addref_locked(best_lpni);
1766 * Use lnet_cpt_of_nid() to determine the CPT used to commit the
1767 * message. This ensures that we get a CPT that is correct for
1768 * the NI when the NI has been restricted to a subset of all CPTs.
1769 * If the selected CPT differs from the one currently locked, we
1770 * must unlock and relock the lnet_net_lock(), and then check whether
1771 * the configuration has changed. We don't have a hold on the best_ni
1772 * yet, and it may have vanished.
1774 cpt2 = lnet_cpt_of_nid_locked(best_lpni->lpni_nid, best_ni);
1775 if (sd->sd_cpt != cpt2) {
1776 __u32 seq = lnet_get_dlc_seq_locked();
1777 lnet_net_unlock(sd->sd_cpt);
1779 lnet_net_lock(sd->sd_cpt);
1780 if (seq != lnet_get_dlc_seq_locked()) {
1781 lnet_peer_ni_decref_locked(best_lpni);
1787 * store the best_lpni in the message right away to avoid having
1788 * to do the same operation under different conditions
1790 msg->msg_txpeer = best_lpni;
1791 msg->msg_txni = best_ni;
1794 * grab a reference for the best_ni since now it's in use in this
1795 * send. The reference will be dropped in lnet_finalize()
1797 lnet_ni_addref_locked(msg->msg_txni, sd->sd_cpt);
1800 * Always set the target.nid to the best peer picked. Either the
1801 * NID will be one of the peer NIDs selected, or the same NID as
1802 * what was originally set in the target or it will be the NID of
1803 * a router if this message should be routed
1805 msg->msg_target.nid = msg->msg_txpeer->lpni_nid;
1808 * lnet_msg_commit assigns the correct cpt to the message, which
1809 * is used to decrement the correct refcount on the ni when it's
1810 * time to return the credits
1812 lnet_msg_commit(msg, sd->sd_cpt);
1815 * If we are routing the message then we keep the src_nid that was
1816 * set by the originator. If we are not routing then we are the
1817 * originator and set it here.
1819 if (!msg->msg_routing)
1820 msg->msg_hdr.src_nid = cpu_to_le64(msg->msg_txni->ni_nid);
1823 msg->msg_target_is_router = 1;
1824 msg->msg_target.pid = LNET_PID_LUSTRE;
1826 * since we're routing we want to ensure that the
1827 * msg_hdr.dest_nid is set to the final destination. When
1828 * the router receives this message it knows how to route
1831 * final_dst_lpni is set at the beginning of the
1832 * lnet_select_pathway() function and is never changed.
1833 * It's safe to use it here.
1835 msg->msg_hdr.dest_nid = cpu_to_le64(final_dst_lpni->lpni_nid);
1838 * if we're not routing set the dest_nid to the best peer
1839 * ni NID that we picked earlier in the algorithm.
1841 msg->msg_hdr.dest_nid = cpu_to_le64(msg->msg_txpeer->lpni_nid);
1845 * if we have response tracker block update it with the next hop
1849 rspt = msg->msg_md->md_rspt_ptr;
1851 rspt->rspt_next_hop_nid = msg->msg_txpeer->lpni_nid;
1852 CDEBUG(D_NET, "rspt_next_hop_nid = %s\n",
1853 libcfs_nid2str(rspt->rspt_next_hop_nid));
1857 rc = lnet_post_send_locked(msg, 0);
1860 CDEBUG(D_NET, "TRACE: %s(%s:%s) -> %s(%s:%s) %s : %s try# %d\n",
1861 libcfs_nid2str(msg->msg_hdr.src_nid),
1862 libcfs_nid2str(msg->msg_txni->ni_nid),
1863 libcfs_nid2str(sd->sd_src_nid),
1864 libcfs_nid2str(msg->msg_hdr.dest_nid),
1865 libcfs_nid2str(sd->sd_dst_nid),
1866 libcfs_nid2str(msg->msg_txpeer->lpni_nid),
1867 libcfs_nid2str(sd->sd_rtr_nid),
1868 lnet_msgtyp2str(msg->msg_type), msg->msg_retry_count);
1874 lnet_set_non_mr_pref_nid(struct lnet_peer_ni *lpni, struct lnet_ni *lni,
1875 struct lnet_msg *msg)
1877 if (!lnet_peer_is_multi_rail(lpni->lpni_peer_net->lpn_peer) &&
1878 !lnet_msg_is_response(msg) && lpni->lpni_pref_nnids == 0) {
1879 CDEBUG(D_NET, "Setting preferred local NID %s on NMR peer %s\n",
1880 libcfs_nid2str(lni->ni_nid),
1881 libcfs_nid2str(lpni->lpni_nid));
1882 lnet_peer_ni_set_non_mr_pref_nid(lpni, lni->ni_nid);
1891 * use the source and destination NIDs as the pathway
1894 lnet_handle_spec_local_nmr_dst(struct lnet_send_data *sd)
1896 /* the destination lpni is set before we get here. */
1899 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
1900 if (!sd->sd_best_ni) {
1901 CERROR("Can't send to %s: src %s is not a "
1902 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
1903 libcfs_nid2str(sd->sd_src_nid));
1907 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
1909 return lnet_handle_send(sd);
1917 * Don't run the selection algorithm on the peer NIs. By specifying the
1918 * local NID, we're also saying that we should always use the destination NID
1919 * provided. This handles the case where we should be using the same
1920 * destination NID for the all the messages which belong to the same RPC
1924 lnet_handle_spec_local_mr_dst(struct lnet_send_data *sd)
1926 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
1927 if (!sd->sd_best_ni) {
1928 CERROR("Can't send to %s: src %s is not a "
1929 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
1930 libcfs_nid2str(sd->sd_src_nid));
1934 if (sd->sd_best_lpni &&
1935 sd->sd_best_lpni->lpni_nid == the_lnet.ln_loni->ni_nid)
1936 return lnet_handle_lo_send(sd);
1937 else if (sd->sd_best_lpni)
1938 return lnet_handle_send(sd);
1940 CERROR("can't send to %s. no NI on %s\n",
1941 libcfs_nid2str(sd->sd_dst_nid),
1942 libcfs_net2str(sd->sd_best_ni->ni_net->net_id));
1944 return -EHOSTUNREACH;
1948 lnet_find_best_ni_on_spec_net(struct lnet_ni *cur_best_ni,
1949 struct lnet_peer *peer,
1950 struct lnet_peer_net *peer_net,
1954 struct lnet_net *local_net;
1955 struct lnet_ni *best_ni;
1957 local_net = lnet_get_net_locked(peer_net->lpn_net_id);
1962 * Iterate through the NIs in this local Net and select
1963 * the NI to send from. The selection is determined by
1964 * these 3 criterion in the following priority:
1966 * 2. NI available credits
1969 best_ni = lnet_get_best_ni(local_net, cur_best_ni,
1970 peer, peer_net, cpt);
1972 if (incr_seq && best_ni)
1979 lnet_initiate_peer_discovery(struct lnet_peer_ni *lpni, struct lnet_msg *msg,
1982 struct lnet_peer *peer;
1985 lnet_peer_ni_addref_locked(lpni);
1987 peer = lpni->lpni_peer_net->lpn_peer;
1989 if (lnet_peer_gw_discovery(peer)) {
1990 lnet_peer_ni_decref_locked(lpni);
1994 if (!lnet_msg_discovery(msg) || lnet_peer_is_uptodate(peer)) {
1995 lnet_peer_ni_decref_locked(lpni);
1999 rc = lnet_discover_peer_locked(lpni, cpt, false);
2001 lnet_peer_ni_decref_locked(lpni);
2004 /* The peer may have changed. */
2005 peer = lpni->lpni_peer_net->lpn_peer;
2006 spin_lock(&peer->lp_lock);
2007 if (lnet_peer_is_uptodate_locked(peer)) {
2008 spin_unlock(&peer->lp_lock);
2009 lnet_peer_ni_decref_locked(lpni);
2012 /* queue message and return */
2013 msg->msg_sending = 0;
2014 msg->msg_txpeer = NULL;
2015 list_add_tail(&msg->msg_list, &peer->lp_dc_pendq);
2016 spin_unlock(&peer->lp_lock);
2018 lnet_peer_ni_decref_locked(lpni);
2020 CDEBUG(D_NET, "msg %p delayed. %s pending discovery\n",
2021 msg, libcfs_nid2str(peer->lp_primary_nid));
2023 return LNET_DC_WAIT;
2027 lnet_handle_find_routed_path(struct lnet_send_data *sd,
2029 struct lnet_peer_ni **gw_lpni,
2030 struct lnet_peer **gw_peer)
2034 struct lnet_peer *gw;
2035 struct lnet_peer *lp;
2036 struct lnet_peer_net *lpn;
2037 struct lnet_peer_net *best_lpn = NULL;
2038 struct lnet_remotenet *rnet, *best_rnet = NULL;
2039 struct lnet_route *best_route = NULL;
2040 struct lnet_route *last_route = NULL;
2041 struct lnet_peer_ni *lpni = NULL;
2042 struct lnet_peer_ni *gwni = NULL;
2043 bool route_found = false;
2044 lnet_nid_t src_nid = (sd->sd_src_nid != LNET_NID_ANY) ? sd->sd_src_nid :
2045 (sd->sd_best_ni != NULL) ? sd->sd_best_ni->ni_nid :
2047 int best_lpn_healthv = 0;
2048 __u32 best_lpn_sel_prio = LNET_MAX_SELECTION_PRIORITY;
2050 CDEBUG(D_NET, "using src nid %s for route restriction\n",
2051 libcfs_nid2str(src_nid));
2053 /* If a router nid was specified then we are replying to a GET or
2054 * sending an ACK. In this case we use the gateway associated with the
2055 * specified router nid.
2057 if (sd->sd_rtr_nid != LNET_NID_ANY) {
2058 gwni = lnet_find_peer_ni_locked(sd->sd_rtr_nid);
2060 gw = gwni->lpni_peer_net->lpn_peer;
2061 lnet_peer_ni_decref_locked(gwni);
2062 if (gw->lp_rtr_refcount) {
2063 local_lnet = LNET_NIDNET(sd->sd_rtr_nid);
2067 CWARN("No peer NI for gateway %s. Attempting to find an alternative route.\n",
2068 libcfs_nid2str(sd->sd_rtr_nid));
2073 if (sd->sd_msg->msg_routing) {
2074 /* If I'm routing this message then I need to find the
2075 * next hop based on the destination NID
2077 best_rnet = lnet_find_rnet_locked(LNET_NIDNET(sd->sd_dst_nid));
2079 CERROR("Unable to route message to %s - Route table may be misconfigured\n",
2080 libcfs_nid2str(sd->sd_dst_nid));
2081 return -EHOSTUNREACH;
2084 /* we've already looked up the initial lpni using
2087 lpni = sd->sd_best_lpni;
2088 /* the peer tree must be in existence */
2089 LASSERT(lpni && lpni->lpni_peer_net &&
2090 lpni->lpni_peer_net->lpn_peer);
2091 lp = lpni->lpni_peer_net->lpn_peer;
2093 list_for_each_entry(lpn, &lp->lp_peer_nets, lpn_peer_nets) {
2094 /* is this remote network reachable? */
2095 rnet = lnet_find_rnet_locked(lpn->lpn_net_id);
2104 /* select the preferred peer net */
2105 if (best_lpn_healthv > lpn->lpn_healthv)
2107 else if (best_lpn_healthv < lpn->lpn_healthv)
2110 if (best_lpn_sel_prio < lpn->lpn_sel_priority)
2112 else if (best_lpn_sel_prio > lpn->lpn_sel_priority)
2115 if (best_lpn->lpn_seq <= lpn->lpn_seq)
2118 best_lpn_healthv = lpn->lpn_healthv;
2119 best_lpn_sel_prio = lpn->lpn_sel_priority;
2125 CERROR("peer %s has no available nets\n",
2126 libcfs_nid2str(sd->sd_dst_nid));
2127 return -EHOSTUNREACH;
2130 sd->sd_best_lpni = lnet_find_best_lpni(sd->sd_best_ni,
2133 best_lpn->lpn_net_id);
2134 if (!sd->sd_best_lpni) {
2135 CERROR("peer %s is unreachable\n",
2136 libcfs_nid2str(sd->sd_dst_nid));
2137 return -EHOSTUNREACH;
2140 /* We're attempting to round robin over the remote peer
2141 * NI's so update the final destination we selected
2143 sd->sd_final_dst_lpni = sd->sd_best_lpni;
2145 /* Increment the sequence number of the remote lpni so
2146 * we can round robin over the different interfaces of
2149 sd->sd_best_lpni->lpni_seq++;
2153 * find the best route. Restrict the selection on the net of the
2154 * local NI if we've already picked the local NI to send from.
2155 * Otherwise, let's pick any route we can find and then find
2156 * a local NI we can reach the route's gateway on. Any route we
2157 * select will be reachable by virtue of the restriction we have
2158 * when adding a route.
2160 best_route = lnet_find_route_locked(best_rnet,
2161 LNET_NIDNET(src_nid),
2163 &last_route, &gwni);
2166 CERROR("no route to %s from %s\n",
2167 libcfs_nid2str(dst_nid),
2168 libcfs_nid2str(src_nid));
2169 return -EHOSTUNREACH;
2173 CERROR("Internal Error. Route expected to %s from %s\n",
2174 libcfs_nid2str(dst_nid),
2175 libcfs_nid2str(src_nid));
2179 gw = best_route->lr_gateway;
2180 LASSERT(gw == gwni->lpni_peer_net->lpn_peer);
2181 local_lnet = best_route->lr_lnet;
2185 * Discover this gateway if it hasn't already been discovered.
2186 * This means we might delay the message until discovery has
2189 sd->sd_msg->msg_src_nid_param = sd->sd_src_nid;
2190 rc = lnet_initiate_peer_discovery(gwni, sd->sd_msg, sd->sd_cpt);
2194 if (!sd->sd_best_ni)
2195 sd->sd_best_ni = lnet_find_best_ni_on_spec_net(NULL, gw,
2196 lnet_peer_get_net_locked(gw,
2201 if (!sd->sd_best_ni) {
2202 CERROR("Internal Error. Expected local ni on %s but non found :%s\n",
2203 libcfs_net2str(local_lnet),
2204 libcfs_nid2str(sd->sd_src_nid));
2212 * increment the sequence numbers since now we're sure we're
2213 * going to use this path
2215 if (sd->sd_rtr_nid == LNET_NID_ANY) {
2216 LASSERT(best_route && last_route);
2217 best_route->lr_seq = last_route->lr_seq + 1;
2219 best_lpn->lpn_seq++;
2230 * Remote destination
2231 * Non-MR destination
2235 * Remote destination
2238 * The handling of these two cases is similar. Even though the destination
2239 * can be MR or non-MR, we'll deal directly with the router.
2242 lnet_handle_spec_router_dst(struct lnet_send_data *sd)
2245 struct lnet_peer_ni *gw_lpni = NULL;
2246 struct lnet_peer *gw_peer = NULL;
2249 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
2250 if (!sd->sd_best_ni) {
2251 CERROR("Can't send to %s: src %s is not a "
2252 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
2253 libcfs_nid2str(sd->sd_src_nid));
2257 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2262 if (sd->sd_send_case & NMR_DST)
2264 * since the final destination is non-MR let's set its preferred
2265 * NID before we send
2267 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni,
2271 * We're going to send to the gw found so let's set its
2274 sd->sd_peer = gw_peer;
2275 sd->sd_best_lpni = gw_lpni;
2277 return lnet_handle_send(sd);
2281 lnet_find_best_ni_on_local_net(struct lnet_peer *peer, int md_cpt,
2284 struct lnet_peer_net *peer_net = NULL;
2285 struct lnet_ni *best_ni = NULL;
2286 int lpn_healthv = 0;
2289 * The peer can have multiple interfaces, some of them can be on
2290 * the local network and others on a routed network. We should
2291 * prefer the local network. However if the local network is not
2292 * available then we need to try the routed network
2295 /* go through all the peer nets and find the best_ni */
2296 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_peer_nets) {
2298 * The peer's list of nets can contain non-local nets. We
2299 * want to only examine the local ones.
2301 if (!lnet_get_net_locked(peer_net->lpn_net_id))
2304 /* always select the lpn with the best health */
2305 if (lpn_healthv <= peer_net->lpn_healthv)
2306 lpn_healthv = peer_net->lpn_healthv;
2310 best_ni = lnet_find_best_ni_on_spec_net(best_ni, peer, peer_net,
2314 * if this is a discovery message and lp_disc_net_id is
2315 * specified then use that net to send the discovery on.
2317 if (peer->lp_disc_net_id == peer_net->lpn_net_id &&
2323 /* increment sequence number so we can round robin */
2329 static struct lnet_ni *
2330 lnet_find_existing_preferred_best_ni(struct lnet_peer_ni *lpni, int cpt)
2332 struct lnet_ni *best_ni = NULL;
2333 struct lnet_peer_net *peer_net = lpni->lpni_peer_net;
2334 struct lnet_peer_ni *lpni_entry;
2337 * We must use a consistent source address when sending to a
2338 * non-MR peer. However, a non-MR peer can have multiple NIDs
2339 * on multiple networks, and we may even need to talk to this
2340 * peer on multiple networks -- certain types of
2341 * load-balancing configuration do this.
2343 * So we need to pick the NI the peer prefers for this
2344 * particular network.
2347 list_for_each_entry(lpni_entry, &peer_net->lpn_peer_nis,
2349 if (lpni_entry->lpni_pref_nnids == 0)
2351 LASSERT(lpni_entry->lpni_pref_nnids == 1);
2352 best_ni = lnet_nid2ni_locked(lpni_entry->lpni_pref.nid, cpt);
2359 /* Prerequisite: sd->sd_peer and sd->sd_best_lpni should be set */
2361 lnet_select_preferred_best_ni(struct lnet_send_data *sd)
2363 struct lnet_ni *best_ni = NULL;
2364 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
2367 * We must use a consistent source address when sending to a
2368 * non-MR peer. However, a non-MR peer can have multiple NIDs
2369 * on multiple networks, and we may even need to talk to this
2370 * peer on multiple networks -- certain types of
2371 * load-balancing configuration do this.
2373 * So we need to pick the NI the peer prefers for this
2374 * particular network.
2377 best_ni = lnet_find_existing_preferred_best_ni(sd->sd_best_lpni,
2380 /* if best_ni is still not set just pick one */
2383 lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2384 sd->sd_best_lpni->lpni_peer_net,
2385 sd->sd_md_cpt, true);
2386 /* If there is no best_ni we don't have a route */
2388 CERROR("no path to %s from net %s\n",
2389 libcfs_nid2str(best_lpni->lpni_nid),
2390 libcfs_net2str(best_lpni->lpni_net->net_id));
2391 return -EHOSTUNREACH;
2395 sd->sd_best_ni = best_ni;
2397 /* Set preferred NI if necessary. */
2398 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
2405 * Source not specified
2409 * always use the same source NID for NMR peers
2410 * If we've talked to that peer before then we already have a preferred
2411 * source NI associated with it. Otherwise, we select a preferred local NI
2412 * and store it in the peer
2415 lnet_handle_any_local_nmr_dst(struct lnet_send_data *sd)
2419 /* sd->sd_best_lpni is already set to the final destination */
2422 * At this point we should've created the peer ni and peer. If we
2423 * can't find it, then something went wrong. Instead of assert
2424 * output a relevant message and fail the send
2426 if (!sd->sd_best_lpni) {
2427 CERROR("Internal fault. Unable to send msg %s to %s. "
2429 lnet_msgtyp2str(sd->sd_msg->msg_type),
2430 libcfs_nid2str(sd->sd_dst_nid));
2434 if (sd->sd_msg->msg_routing) {
2435 /* If I'm forwarding this message then I can choose any NI
2436 * on the destination peer net
2438 sd->sd_best_ni = lnet_find_best_ni_on_spec_net(NULL,
2440 sd->sd_best_lpni->lpni_peer_net,
2443 if (!sd->sd_best_ni) {
2444 CERROR("Unable to forward message to %s. No local NI available\n",
2445 libcfs_nid2str(sd->sd_dst_nid));
2449 rc = lnet_select_preferred_best_ni(sd);
2452 rc = lnet_handle_send(sd);
2458 lnet_handle_any_mr_dsta(struct lnet_send_data *sd)
2461 * NOTE we've already handled the remote peer case. So we only
2462 * need to worry about the local case here.
2464 * if we're sending a response, ACK or reply, we need to send it
2465 * to the destination NID given to us. At this point we already
2466 * have the peer_ni we're suppose to send to, so just find the
2467 * best_ni on the peer net and use that. Since we're sending to an
2468 * MR peer then we can just run the selection algorithm on our
2469 * local NIs and pick the best one.
2471 if (sd->sd_send_case & SND_RESP) {
2473 lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2474 sd->sd_best_lpni->lpni_peer_net,
2475 sd->sd_md_cpt, true);
2477 if (!sd->sd_best_ni) {
2479 * We're not going to deal with not able to send
2480 * a response to the provided final destination
2482 CERROR("Can't send response to %s. "
2483 "No local NI available\n",
2484 libcfs_nid2str(sd->sd_dst_nid));
2485 return -EHOSTUNREACH;
2488 return lnet_handle_send(sd);
2492 * If we get here that means we're sending a fresh request, PUT or
2493 * GET, so we need to run our standard selection algorithm.
2494 * First find the best local interface that's on any of the peer's
2497 sd->sd_best_ni = lnet_find_best_ni_on_local_net(sd->sd_peer,
2499 lnet_msg_discovery(sd->sd_msg));
2500 if (sd->sd_best_ni) {
2502 lnet_find_best_lpni(sd->sd_best_ni, sd->sd_dst_nid,
2504 sd->sd_best_ni->ni_net->net_id);
2507 * if we're successful in selecting a peer_ni on the local
2508 * network, then send to it. Otherwise fall through and
2509 * try and see if we can reach it over another routed
2512 if (sd->sd_best_lpni &&
2513 sd->sd_best_lpni->lpni_nid == the_lnet.ln_loni->ni_nid) {
2515 * in case we initially started with a routed
2516 * destination, let's reset to local
2518 sd->sd_send_case &= ~REMOTE_DST;
2519 sd->sd_send_case |= LOCAL_DST;
2520 return lnet_handle_lo_send(sd);
2521 } else if (sd->sd_best_lpni) {
2523 * in case we initially started with a routed
2524 * destination, let's reset to local
2526 sd->sd_send_case &= ~REMOTE_DST;
2527 sd->sd_send_case |= LOCAL_DST;
2528 return lnet_handle_send(sd);
2531 CERROR("Internal Error. Expected to have a best_lpni: "
2533 libcfs_nid2str(sd->sd_src_nid),
2534 libcfs_nid2str(sd->sd_dst_nid));
2540 * Peer doesn't have a local network. Let's see if there is
2541 * a remote network we can reach it on.
2543 return PASS_THROUGH;
2548 * Source NID not specified
2553 * Source NID not speified
2554 * Remote destination
2557 * In both of these cases if we're sending a response, ACK or REPLY, then
2558 * we need to send to the destination NID provided.
2560 * In the remote case let's deal with MR routers.
2565 lnet_handle_any_mr_dst(struct lnet_send_data *sd)
2568 struct lnet_peer *gw_peer = NULL;
2569 struct lnet_peer_ni *gw_lpni = NULL;
2572 * handle sending a response to a remote peer here so we don't
2573 * have to worry about it if we hit lnet_handle_any_mr_dsta()
2575 if (sd->sd_send_case & REMOTE_DST &&
2576 sd->sd_send_case & SND_RESP) {
2577 struct lnet_peer_ni *gw;
2578 struct lnet_peer *gw_peer;
2580 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw,
2583 CERROR("Can't send response to %s. "
2584 "No route available\n",
2585 libcfs_nid2str(sd->sd_dst_nid));
2586 return -EHOSTUNREACH;
2587 } else if (rc > 0) {
2591 sd->sd_best_lpni = gw;
2592 sd->sd_peer = gw_peer;
2594 return lnet_handle_send(sd);
2598 * Even though the NID for the peer might not be on a local network,
2599 * since the peer is MR there could be other interfaces on the
2600 * local network. In that case we'd still like to prefer the local
2601 * network over the routed network. If we're unable to do that
2602 * then we select the best router among the different routed networks,
2603 * and if the router is MR then we can deal with it as such.
2605 rc = lnet_handle_any_mr_dsta(sd);
2606 if (rc != PASS_THROUGH)
2610 * Now that we must route to the destination, we must consider the
2611 * MR case, where the destination has multiple interfaces, some of
2612 * which we can route to and others we do not. For this reason we
2613 * need to select the destination which we can route to and if
2614 * there are multiple, we need to round robin.
2616 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2621 sd->sd_send_case &= ~LOCAL_DST;
2622 sd->sd_send_case |= REMOTE_DST;
2624 sd->sd_peer = gw_peer;
2625 sd->sd_best_lpni = gw_lpni;
2627 return lnet_handle_send(sd);
2631 * Source not specified
2632 * Remote destination
2635 * Must send to the specified peer NID using the same source NID that
2636 * we've used before. If it's the first time to talk to that peer then
2637 * find the source NI and assign it as preferred to that peer
2640 lnet_handle_any_router_nmr_dst(struct lnet_send_data *sd)
2643 struct lnet_peer_ni *gw_lpni = NULL;
2644 struct lnet_peer *gw_peer = NULL;
2647 * Let's see if we have a preferred NI to talk to this NMR peer
2649 sd->sd_best_ni = lnet_find_existing_preferred_best_ni(sd->sd_best_lpni,
2653 * find the router and that'll find the best NI if we didn't find
2656 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2662 * set the best_ni we've chosen as the preferred one for
2665 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
2667 /* we'll be sending to the gw */
2668 sd->sd_best_lpni = gw_lpni;
2669 sd->sd_peer = gw_peer;
2671 return lnet_handle_send(sd);
2675 lnet_handle_send_case_locked(struct lnet_send_data *sd)
2678 * turn off the SND_RESP bit.
2679 * It will be checked in the case handling
2681 __u32 send_case = sd->sd_send_case &= ~SND_RESP ;
2683 CDEBUG(D_NET, "Source %s%s to %s %s %s destination\n",
2684 (send_case & SRC_SPEC) ? "Specified: " : "ANY",
2685 (send_case & SRC_SPEC) ? libcfs_nid2str(sd->sd_src_nid) : "",
2686 (send_case & MR_DST) ? "MR: " : "NMR: ",
2687 libcfs_nid2str(sd->sd_dst_nid),
2688 (send_case & LOCAL_DST) ? "local" : "routed");
2690 switch (send_case) {
2692 * For all cases where the source is specified, we should always
2693 * use the destination NID, whether it's an MR destination or not,
2694 * since we're continuing a series of related messages for the
2697 case SRC_SPEC_LOCAL_NMR_DST:
2698 return lnet_handle_spec_local_nmr_dst(sd);
2699 case SRC_SPEC_LOCAL_MR_DST:
2700 return lnet_handle_spec_local_mr_dst(sd);
2701 case SRC_SPEC_ROUTER_NMR_DST:
2702 case SRC_SPEC_ROUTER_MR_DST:
2703 return lnet_handle_spec_router_dst(sd);
2704 case SRC_ANY_LOCAL_NMR_DST:
2705 return lnet_handle_any_local_nmr_dst(sd);
2706 case SRC_ANY_LOCAL_MR_DST:
2707 case SRC_ANY_ROUTER_MR_DST:
2708 return lnet_handle_any_mr_dst(sd);
2709 case SRC_ANY_ROUTER_NMR_DST:
2710 return lnet_handle_any_router_nmr_dst(sd);
2712 CERROR("Unknown send case\n");
2718 lnet_select_pathway(lnet_nid_t src_nid, lnet_nid_t dst_nid,
2719 struct lnet_msg *msg, lnet_nid_t rtr_nid)
2721 struct lnet_peer_ni *lpni;
2722 struct lnet_peer *peer;
2723 struct lnet_send_data send_data;
2726 __u32 send_case = 0;
2728 bool mr_forwarding_allowed;
2730 memset(&send_data, 0, sizeof(send_data));
2733 * get an initial CPT to use for locking. The idea here is not to
2734 * serialize the calls to select_pathway, so that as many
2735 * operations can run concurrently as possible. To do that we use
2736 * the CPT where this call is being executed. Later on when we
2737 * determine the CPT to use in lnet_message_commit, we switch the
2738 * lock and check if there was any configuration change. If none,
2739 * then we proceed, if there is, then we restart the operation.
2741 cpt = lnet_net_lock_current();
2743 md_cpt = lnet_cpt_of_md(msg->msg_md, msg->msg_offset);
2744 if (md_cpt == CFS_CPT_ANY)
2750 * If we're being asked to send to the loopback interface, there
2751 * is no need to go through any selection. We can just shortcut
2752 * the entire process and send over lolnd
2754 send_data.sd_msg = msg;
2755 send_data.sd_cpt = cpt;
2756 if (dst_nid == LNET_NID_LO_0) {
2757 rc = lnet_handle_lo_send(&send_data);
2758 lnet_net_unlock(cpt);
2763 * find an existing peer_ni, or create one and mark it as having been
2764 * created due to network traffic. This call will create the
2765 * peer->peer_net->peer_ni tree.
2767 lpni = lnet_nid2peerni_locked(dst_nid, LNET_NID_ANY, cpt);
2769 lnet_net_unlock(cpt);
2770 return PTR_ERR(lpni);
2774 * Cache the original src_nid and rtr_nid. If we need to resend the
2775 * message then we'll need to know whether the src_nid was originally
2776 * specified for this message. If it was originally specified,
2777 * then we need to keep using the same src_nid since it's
2778 * continuing the same sequence of messages. Similarly, rtr_nid will
2779 * affect our choice of next hop.
2781 msg->msg_src_nid_param = src_nid;
2782 msg->msg_rtr_nid_param = rtr_nid;
2785 * If necessary, perform discovery on the peer that owns this peer_ni.
2786 * Note, this can result in the ownership of this peer_ni changing
2787 * to another peer object.
2789 rc = lnet_initiate_peer_discovery(lpni, msg, cpt);
2791 lnet_peer_ni_decref_locked(lpni);
2792 lnet_net_unlock(cpt);
2795 lnet_peer_ni_decref_locked(lpni);
2797 peer = lpni->lpni_peer_net->lpn_peer;
2800 * Identify the different send cases
2802 if (src_nid == LNET_NID_ANY)
2803 send_case |= SRC_ANY;
2805 send_case |= SRC_SPEC;
2807 if (lnet_get_net_locked(LNET_NIDNET(dst_nid)))
2808 send_case |= LOCAL_DST;
2810 send_case |= REMOTE_DST;
2813 if (msg->msg_routing && (send_case & LOCAL_DST))
2816 /* Determine whether to allow MR forwarding for this message.
2817 * NB: MR forwarding is allowed if the message originator and the
2818 * destination are both MR capable, and the destination lpni that was
2819 * originally chosen by the originator is unhealthy or down.
2820 * We check the MR capability of the destination further below
2822 mr_forwarding_allowed = false;
2824 struct lnet_peer *src_lp;
2825 struct lnet_peer_ni *src_lpni;
2827 src_lpni = lnet_nid2peerni_locked(msg->msg_hdr.src_nid,
2829 /* We don't fail the send if we hit any errors here. We'll just
2830 * try to send it via non-multi-rail criteria
2832 if (!IS_ERR(src_lpni)) {
2833 /* Drop ref taken by lnet_nid2peerni_locked() */
2834 lnet_peer_ni_decref_locked(src_lpni);
2835 src_lp = lpni->lpni_peer_net->lpn_peer;
2836 if (lnet_peer_is_multi_rail(src_lp) &&
2837 !lnet_is_peer_ni_alive(lpni))
2838 mr_forwarding_allowed = true;
2841 CDEBUG(D_NET, "msg %p MR forwarding %s\n", msg,
2842 mr_forwarding_allowed ? "allowed" : "not allowed");
2846 * Deal with the peer as NMR in the following cases:
2847 * 1. the peer is NMR
2848 * 2. We're trying to recover a specific peer NI
2849 * 3. I'm a router sending to the final destination and MR forwarding is
2850 * not allowed for this message (as determined above).
2851 * In this case the source of the message would've
2852 * already selected the final destination so my job
2853 * is to honor the selection.
2855 if (!lnet_peer_is_multi_rail(peer) || msg->msg_recovery ||
2856 (final_hop && !mr_forwarding_allowed))
2857 send_case |= NMR_DST;
2859 send_case |= MR_DST;
2861 if (lnet_msg_is_response(msg))
2862 send_case |= SND_RESP;
2864 /* assign parameters to the send_data */
2865 send_data.sd_rtr_nid = rtr_nid;
2866 send_data.sd_src_nid = src_nid;
2867 send_data.sd_dst_nid = dst_nid;
2868 send_data.sd_best_lpni = lpni;
2870 * keep a pointer to the final destination in case we're going to
2871 * route, so we'll need to access it later
2873 send_data.sd_final_dst_lpni = lpni;
2874 send_data.sd_peer = peer;
2875 send_data.sd_md_cpt = md_cpt;
2876 send_data.sd_send_case = send_case;
2878 rc = lnet_handle_send_case_locked(&send_data);
2881 * Update the local cpt since send_data.sd_cpt might've been
2882 * updated as a result of calling lnet_handle_send_case_locked().
2884 cpt = send_data.sd_cpt;
2886 if (rc == REPEAT_SEND)
2889 lnet_net_unlock(cpt);
2895 lnet_send(lnet_nid_t src_nid, struct lnet_msg *msg, lnet_nid_t rtr_nid)
2897 lnet_nid_t dst_nid = msg->msg_target.nid;
2901 * NB: rtr_nid is set to LNET_NID_ANY for all current use-cases,
2902 * but we might want to use pre-determined router for ACK/REPLY
2905 /* NB: ni != NULL == interface pre-determined (ACK/REPLY) */
2906 LASSERT(msg->msg_txpeer == NULL);
2907 LASSERT(msg->msg_txni == NULL);
2908 LASSERT(!msg->msg_sending);
2909 LASSERT(!msg->msg_target_is_router);
2910 LASSERT(!msg->msg_receiving);
2912 msg->msg_sending = 1;
2914 LASSERT(!msg->msg_tx_committed);
2916 rc = lnet_select_pathway(src_nid, dst_nid, msg, rtr_nid);
2918 if (rc == -EHOSTUNREACH)
2919 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_ERROR;
2921 msg->msg_health_status = LNET_MSG_STATUS_LOCAL_ERROR;
2925 if (rc == LNET_CREDIT_OK)
2926 lnet_ni_send(msg->msg_txni, msg);
2928 /* rc == LNET_CREDIT_OK or LNET_CREDIT_WAIT or LNET_DC_WAIT */
2932 enum lnet_mt_event_type {
2933 MT_TYPE_LOCAL_NI = 0,
2937 struct lnet_mt_event_info {
2938 enum lnet_mt_event_type mt_type;
2942 /* called with res_lock held */
2944 lnet_detach_rsp_tracker(struct lnet_libmd *md, int cpt)
2946 struct lnet_rsp_tracker *rspt;
2949 * msg has a refcount on the MD so the MD is not going away.
2950 * The rspt queue for the cpt is protected by
2951 * the lnet_net_lock(cpt). cpt is the cpt of the MD cookie.
2953 if (!md->md_rspt_ptr)
2956 rspt = md->md_rspt_ptr;
2959 LASSERT(rspt->rspt_cpt == cpt);
2961 md->md_rspt_ptr = NULL;
2963 if (LNetMDHandleIsInvalid(rspt->rspt_mdh)) {
2965 * The monitor thread has invalidated this handle because the
2966 * response timed out, but it failed to lookup the MD. That
2967 * means this response tracker is on the zombie list. We can
2968 * safely remove it under the resource lock (held by caller) and
2969 * free the response tracker block.
2971 list_del(&rspt->rspt_on_list);
2972 lnet_rspt_free(rspt, cpt);
2975 * invalidate the handle to indicate that a response has been
2976 * received, which will then lead the monitor thread to clean up
2979 LNetInvalidateMDHandle(&rspt->rspt_mdh);
2984 lnet_clean_zombie_rstqs(void)
2986 struct lnet_rsp_tracker *rspt, *tmp;
2989 cfs_cpt_for_each(i, lnet_cpt_table()) {
2990 list_for_each_entry_safe(rspt, tmp,
2991 the_lnet.ln_mt_zombie_rstqs[i],
2993 list_del(&rspt->rspt_on_list);
2994 lnet_rspt_free(rspt, i);
2998 cfs_percpt_free(the_lnet.ln_mt_zombie_rstqs);
3002 lnet_finalize_expired_responses(void)
3004 struct lnet_libmd *md;
3005 struct lnet_rsp_tracker *rspt, *tmp;
3009 if (the_lnet.ln_mt_rstq == NULL)
3012 cfs_cpt_for_each(i, lnet_cpt_table()) {
3013 LIST_HEAD(local_queue);
3016 if (!the_lnet.ln_mt_rstq[i]) {
3020 list_splice_init(the_lnet.ln_mt_rstq[i], &local_queue);
3025 list_for_each_entry_safe(rspt, tmp, &local_queue, rspt_on_list) {
3027 * The rspt mdh will be invalidated when a response
3028 * is received or whenever we want to discard the
3029 * block the monitor thread will walk the queue
3030 * and clean up any rsts with an invalid mdh.
3031 * The monitor thread will walk the queue until
3032 * the first unexpired rspt block. This means that
3033 * some rspt blocks which received their
3034 * corresponding responses will linger in the
3035 * queue until they are cleaned up eventually.
3038 if (LNetMDHandleIsInvalid(rspt->rspt_mdh)) {
3040 list_del(&rspt->rspt_on_list);
3041 lnet_rspt_free(rspt, i);
3045 if (ktime_compare(now, rspt->rspt_deadline) >= 0 ||
3046 the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN) {
3047 struct lnet_peer_ni *lpni;
3050 md = lnet_handle2md(&rspt->rspt_mdh);
3052 /* MD has been queued for unlink, but
3053 * rspt hasn't been detached (Note we've
3054 * checked above that the rspt_mdh is
3055 * valid). Since we cannot lookup the MD
3056 * we're unable to detach the rspt
3057 * ourselves. Thus, move the rspt to the
3058 * zombie list where we'll wait for
3060 * 1. The remaining operations on the
3061 * MD to complete. In this case the
3062 * final operation will result in
3063 * lnet_msg_detach_md()->
3064 * lnet_detach_rsp_tracker() where
3065 * we will clean up this response
3067 * 2. LNet to shutdown. In this case
3068 * we'll wait until after all LND Nets
3069 * have shutdown and then we can
3070 * safely free any remaining response
3071 * tracker blocks on the zombie list.
3072 * Note: We need to hold the resource
3073 * lock when adding to the zombie list
3074 * because we may have concurrent access
3075 * with lnet_detach_rsp_tracker().
3077 LNetInvalidateMDHandle(&rspt->rspt_mdh);
3078 list_move(&rspt->rspt_on_list,
3079 the_lnet.ln_mt_zombie_rstqs[i]);
3083 LASSERT(md->md_rspt_ptr == rspt);
3084 md->md_rspt_ptr = NULL;
3087 LNetMDUnlink(rspt->rspt_mdh);
3089 nid = rspt->rspt_next_hop_nid;
3091 list_del(&rspt->rspt_on_list);
3092 lnet_rspt_free(rspt, i);
3094 /* If we're shutting down we just want to clean
3095 * up the rspt blocks
3097 if (the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN)
3101 the_lnet.ln_counters[i]->lct_health.lch_response_timeout_count++;
3105 "Response timeout: md = %p: nid = %s\n",
3106 md, libcfs_nid2str(nid));
3109 * If there is a timeout on the response
3110 * from the next hop decrement its health
3111 * value so that we don't use it
3114 lpni = lnet_find_peer_ni_locked(nid);
3116 lnet_handle_remote_failure_locked(lpni);
3117 lnet_peer_ni_decref_locked(lpni);
3126 if (!list_empty(&local_queue)) {
3128 list_splice(&local_queue, the_lnet.ln_mt_rstq[i]);
3135 lnet_resend_pending_msgs_locked(struct list_head *resendq, int cpt)
3137 struct lnet_msg *msg;
3139 while (!list_empty(resendq)) {
3140 struct lnet_peer_ni *lpni;
3142 msg = list_entry(resendq->next, struct lnet_msg,
3145 list_del_init(&msg->msg_list);
3147 lpni = lnet_find_peer_ni_locked(msg->msg_hdr.dest_nid);
3149 lnet_net_unlock(cpt);
3150 CERROR("Expected that a peer is already created for %s\n",
3151 libcfs_nid2str(msg->msg_hdr.dest_nid));
3152 msg->msg_no_resend = true;
3153 lnet_finalize(msg, -EFAULT);
3158 lnet_peer_ni_decref_locked(lpni);
3160 lnet_net_unlock(cpt);
3161 CDEBUG(D_NET, "resending %s->%s: %s recovery %d try# %d\n",
3162 libcfs_nid2str(msg->msg_src_nid_param),
3163 libcfs_id2str(msg->msg_target),
3164 lnet_msgtyp2str(msg->msg_type),
3166 msg->msg_retry_count);
3167 rc = lnet_send(msg->msg_src_nid_param, msg,
3168 msg->msg_rtr_nid_param);
3170 CERROR("Error sending %s to %s: %d\n",
3171 lnet_msgtyp2str(msg->msg_type),
3172 libcfs_id2str(msg->msg_target), rc);
3173 msg->msg_no_resend = true;
3174 lnet_finalize(msg, rc);
3178 the_lnet.ln_counters[cpt]->lct_health.lch_resend_count++;
3184 lnet_resend_pending_msgs(void)
3188 cfs_cpt_for_each(i, lnet_cpt_table()) {
3190 lnet_resend_pending_msgs_locked(the_lnet.ln_mt_resendqs[i], i);
3195 /* called with cpt and ni_lock held */
3197 lnet_unlink_ni_recovery_mdh_locked(struct lnet_ni *ni, int cpt, bool force)
3199 struct lnet_handle_md recovery_mdh;
3201 LNetInvalidateMDHandle(&recovery_mdh);
3203 if (ni->ni_recovery_state & LNET_NI_RECOVERY_PENDING ||
3205 recovery_mdh = ni->ni_ping_mdh;
3206 LNetInvalidateMDHandle(&ni->ni_ping_mdh);
3209 lnet_net_unlock(cpt);
3210 if (!LNetMDHandleIsInvalid(recovery_mdh))
3211 LNetMDUnlink(recovery_mdh);
3217 lnet_recover_local_nis(void)
3219 struct lnet_mt_event_info *ev_info;
3220 LIST_HEAD(processed_list);
3221 LIST_HEAD(local_queue);
3222 struct lnet_handle_md mdh;
3223 struct lnet_ni *tmp;
3230 * splice the recovery queue on a local queue. We will iterate
3231 * through the local queue and update it as needed. Once we're
3232 * done with the traversal, we'll splice the local queue back on
3233 * the head of the ln_mt_localNIRecovq. Any newly added local NIs
3234 * will be traversed in the next iteration.
3237 list_splice_init(&the_lnet.ln_mt_localNIRecovq,
3241 list_for_each_entry_safe(ni, tmp, &local_queue, ni_recovery) {
3243 * if an NI is being deleted or it is now healthy, there
3244 * is no need to keep it around in the recovery queue.
3245 * The monitor thread is the only thread responsible for
3246 * removing the NI from the recovery queue.
3247 * Multiple threads can be adding NIs to the recovery
3250 healthv = atomic_read(&ni->ni_healthv);
3254 if (ni->ni_state != LNET_NI_STATE_ACTIVE ||
3255 healthv == LNET_MAX_HEALTH_VALUE) {
3256 list_del_init(&ni->ni_recovery);
3257 lnet_unlink_ni_recovery_mdh_locked(ni, 0, false);
3259 lnet_ni_decref_locked(ni, 0);
3265 * if the local NI failed recovery we must unlink the md.
3266 * But we want to keep the local_ni on the recovery queue
3267 * so we can continue the attempts to recover it.
3269 if (ni->ni_recovery_state & LNET_NI_RECOVERY_FAILED) {
3270 lnet_unlink_ni_recovery_mdh_locked(ni, 0, true);
3271 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_FAILED;
3278 CDEBUG(D_NET, "attempting to recover local ni: %s\n",
3279 libcfs_nid2str(ni->ni_nid));
3282 if (!(ni->ni_recovery_state & LNET_NI_RECOVERY_PENDING)) {
3283 ni->ni_recovery_state |= LNET_NI_RECOVERY_PENDING;
3286 LIBCFS_ALLOC(ev_info, sizeof(*ev_info));
3288 CERROR("out of memory. Can't recover %s\n",
3289 libcfs_nid2str(ni->ni_nid));
3291 ni->ni_recovery_state &=
3292 ~LNET_NI_RECOVERY_PENDING;
3297 mdh = ni->ni_ping_mdh;
3299 * Invalidate the ni mdh in case it's deleted.
3300 * We'll unlink the mdh in this case below.
3302 LNetInvalidateMDHandle(&ni->ni_ping_mdh);
3306 * remove the NI from the local queue and drop the
3307 * reference count to it while we're recovering
3308 * it. The reason for that, is that the NI could
3309 * be deleted, and the way the code is structured
3310 * is if we don't drop the NI, then the deletion
3311 * code will enter a loop waiting for the
3312 * reference count to be removed while holding the
3313 * ln_mutex_lock(). When we look up the peer to
3314 * send to in lnet_select_pathway() we will try to
3315 * lock the ln_mutex_lock() as well, leading to
3316 * a deadlock. By dropping the refcount and
3317 * removing it from the list, we allow for the NI
3318 * to be removed, then we use the cached NID to
3319 * look it up again. If it's gone, then we just
3320 * continue examining the rest of the queue.
3323 list_del_init(&ni->ni_recovery);
3324 lnet_ni_decref_locked(ni, 0);
3327 ev_info->mt_type = MT_TYPE_LOCAL_NI;
3328 ev_info->mt_nid = nid;
3329 rc = lnet_send_ping(nid, &mdh, LNET_INTERFACES_MIN,
3330 ev_info, the_lnet.ln_mt_handler,
3332 /* lookup the nid again */
3334 ni = lnet_nid2ni_locked(nid, 0);
3337 * the NI has been deleted when we dropped
3345 * Same note as in lnet_recover_peer_nis(). When
3346 * we're sending the ping, the NI is free to be
3347 * deleted or manipulated. By this point it
3348 * could've been added back on the recovery queue,
3349 * and a refcount taken on it.
3350 * So we can't just add it blindly again or we'll
3351 * corrupt the queue. We must check under lock if
3352 * it's not on any list and if not then add it
3353 * to the processed list, which will eventually be
3354 * spliced back on to the recovery queue.
3356 ni->ni_ping_mdh = mdh;
3357 if (list_empty(&ni->ni_recovery)) {
3358 list_add_tail(&ni->ni_recovery, &processed_list);
3359 lnet_ni_addref_locked(ni, 0);
3365 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_PENDING;
3371 * put back the remaining NIs on the ln_mt_localNIRecovq to be
3372 * reexamined in the next iteration.
3374 list_splice_init(&processed_list, &local_queue);
3376 list_splice(&local_queue, &the_lnet.ln_mt_localNIRecovq);
3381 lnet_resendqs_create(void)
3383 struct list_head **resendqs;
3384 resendqs = lnet_create_array_of_queues();
3389 lnet_net_lock(LNET_LOCK_EX);
3390 the_lnet.ln_mt_resendqs = resendqs;
3391 lnet_net_unlock(LNET_LOCK_EX);
3397 lnet_clean_local_ni_recoveryq(void)
3401 /* This is only called when the monitor thread has stopped */
3404 while (!list_empty(&the_lnet.ln_mt_localNIRecovq)) {
3405 ni = list_entry(the_lnet.ln_mt_localNIRecovq.next,
3406 struct lnet_ni, ni_recovery);
3407 list_del_init(&ni->ni_recovery);
3409 lnet_unlink_ni_recovery_mdh_locked(ni, 0, true);
3411 lnet_ni_decref_locked(ni, 0);
3418 lnet_unlink_lpni_recovery_mdh_locked(struct lnet_peer_ni *lpni, int cpt,
3421 struct lnet_handle_md recovery_mdh;
3423 LNetInvalidateMDHandle(&recovery_mdh);
3425 if (lpni->lpni_state & LNET_PEER_NI_RECOVERY_PENDING || force) {
3426 recovery_mdh = lpni->lpni_recovery_ping_mdh;
3427 LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
3429 spin_unlock(&lpni->lpni_lock);
3430 lnet_net_unlock(cpt);
3431 if (!LNetMDHandleIsInvalid(recovery_mdh))
3432 LNetMDUnlink(recovery_mdh);
3434 spin_lock(&lpni->lpni_lock);
3438 lnet_clean_peer_ni_recoveryq(void)
3440 struct lnet_peer_ni *lpni, *tmp;
3442 lnet_net_lock(LNET_LOCK_EX);
3444 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_mt_peerNIRecovq,
3446 list_del_init(&lpni->lpni_recovery);
3447 spin_lock(&lpni->lpni_lock);
3448 lnet_unlink_lpni_recovery_mdh_locked(lpni, LNET_LOCK_EX, true);
3449 spin_unlock(&lpni->lpni_lock);
3450 lnet_peer_ni_decref_locked(lpni);
3453 lnet_net_unlock(LNET_LOCK_EX);
3457 lnet_clean_resendqs(void)
3459 struct lnet_msg *msg, *tmp;
3463 cfs_cpt_for_each(i, lnet_cpt_table()) {
3465 list_splice_init(the_lnet.ln_mt_resendqs[i], &msgs);
3467 list_for_each_entry_safe(msg, tmp, &msgs, msg_list) {
3468 list_del_init(&msg->msg_list);
3469 msg->msg_no_resend = true;
3470 lnet_finalize(msg, -ESHUTDOWN);
3474 cfs_percpt_free(the_lnet.ln_mt_resendqs);
3478 lnet_recover_peer_nis(void)
3480 struct lnet_mt_event_info *ev_info;
3481 LIST_HEAD(processed_list);
3482 LIST_HEAD(local_queue);
3483 struct lnet_handle_md mdh;
3484 struct lnet_peer_ni *lpni;
3485 struct lnet_peer_ni *tmp;
3491 * Always use cpt 0 for locking across all interactions with
3492 * ln_mt_peerNIRecovq
3495 list_splice_init(&the_lnet.ln_mt_peerNIRecovq,
3499 list_for_each_entry_safe(lpni, tmp, &local_queue,
3502 * The same protection strategy is used here as is in the
3503 * local recovery case.
3506 healthv = atomic_read(&lpni->lpni_healthv);
3507 spin_lock(&lpni->lpni_lock);
3508 if (lpni->lpni_state & LNET_PEER_NI_DELETING ||
3509 healthv == LNET_MAX_HEALTH_VALUE) {
3510 list_del_init(&lpni->lpni_recovery);
3511 lnet_unlink_lpni_recovery_mdh_locked(lpni, 0, false);
3512 spin_unlock(&lpni->lpni_lock);
3513 lnet_peer_ni_decref_locked(lpni);
3519 * If the peer NI has failed recovery we must unlink the
3520 * md. But we want to keep the peer ni on the recovery
3521 * queue so we can try to continue recovering it
3523 if (lpni->lpni_state & LNET_PEER_NI_RECOVERY_FAILED) {
3524 lnet_unlink_lpni_recovery_mdh_locked(lpni, 0, true);
3525 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_FAILED;
3528 spin_unlock(&lpni->lpni_lock);
3532 * NOTE: we're racing with peer deletion from user space.
3533 * It's possible that a peer is deleted after we check its
3534 * state. In this case the recovery can create a new peer
3536 spin_lock(&lpni->lpni_lock);
3537 if (!(lpni->lpni_state & LNET_PEER_NI_RECOVERY_PENDING) &&
3538 !(lpni->lpni_state & LNET_PEER_NI_DELETING)) {
3539 lpni->lpni_state |= LNET_PEER_NI_RECOVERY_PENDING;
3540 spin_unlock(&lpni->lpni_lock);
3542 LIBCFS_ALLOC(ev_info, sizeof(*ev_info));
3544 CERROR("out of memory. Can't recover %s\n",
3545 libcfs_nid2str(lpni->lpni_nid));
3546 spin_lock(&lpni->lpni_lock);
3547 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3548 spin_unlock(&lpni->lpni_lock);
3552 /* look at the comments in lnet_recover_local_nis() */
3553 mdh = lpni->lpni_recovery_ping_mdh;
3554 LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
3555 nid = lpni->lpni_nid;
3557 list_del_init(&lpni->lpni_recovery);
3558 lnet_peer_ni_decref_locked(lpni);
3561 ev_info->mt_type = MT_TYPE_PEER_NI;
3562 ev_info->mt_nid = nid;
3563 rc = lnet_send_ping(nid, &mdh, LNET_INTERFACES_MIN,
3564 ev_info, the_lnet.ln_mt_handler,
3568 * lnet_find_peer_ni_locked() grabs a refcount for
3569 * us. No need to take it explicitly.
3571 lpni = lnet_find_peer_ni_locked(nid);
3578 lpni->lpni_recovery_ping_mdh = mdh;
3580 * While we're unlocked the lpni could've been
3581 * readded on the recovery queue. In this case we
3582 * don't need to add it to the local queue, since
3583 * it's already on there and the thread that added
3584 * it would've incremented the refcount on the
3585 * peer, which means we need to decref the refcount
3586 * that was implicitly grabbed by find_peer_ni_locked.
3587 * Otherwise, if the lpni is still not on
3588 * the recovery queue, then we'll add it to the
3591 if (list_empty(&lpni->lpni_recovery))
3592 list_add_tail(&lpni->lpni_recovery, &processed_list);
3594 lnet_peer_ni_decref_locked(lpni);
3597 spin_lock(&lpni->lpni_lock);
3599 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3601 spin_unlock(&lpni->lpni_lock);
3604 list_splice_init(&processed_list, &local_queue);
3606 list_splice(&local_queue, &the_lnet.ln_mt_peerNIRecovq);
3611 lnet_monitor_thread(void *arg)
3613 time64_t recovery_timeout = 0;
3614 time64_t rsp_timeout = 0;
3618 wait_for_completion(&the_lnet.ln_started);
3620 * The monitor thread takes care of the following:
3621 * 1. Checks the aliveness of routers
3622 * 2. Checks if there are messages on the resend queue to resend
3624 * 3. Check if there are any NIs on the local recovery queue and
3626 * 4. Checks if there are any NIs on the remote recovery queue
3629 while (the_lnet.ln_mt_state == LNET_MT_STATE_RUNNING) {
3630 now = ktime_get_real_seconds();
3632 if (lnet_router_checker_active())
3633 lnet_check_routers();
3635 lnet_resend_pending_msgs();
3637 if (now >= rsp_timeout) {
3638 lnet_finalize_expired_responses();
3639 rsp_timeout = now + (lnet_transaction_timeout / 2);
3642 if (now >= recovery_timeout) {
3643 lnet_recover_local_nis();
3644 lnet_recover_peer_nis();
3645 recovery_timeout = now + lnet_recovery_interval;
3649 * TODO do we need to check if we should sleep without
3650 * timeout? Technically, an active system will always
3651 * have messages in flight so this check will always
3652 * evaluate to false. And on an idle system do we care
3653 * if we wake up every 1 second? Although, we've seen
3654 * cases where we get a complaint that an idle thread
3655 * is waking up unnecessarily.
3657 * Take into account the current net_count when you wake
3658 * up for alive router checking, since we need to check
3659 * possibly as many networks as we have configured.
3661 interval = min(lnet_recovery_interval,
3662 min((unsigned int) alive_router_check_interval /
3663 lnet_current_net_count,
3664 lnet_transaction_timeout / 2));
3665 wait_for_completion_interruptible_timeout(
3666 &the_lnet.ln_mt_wait_complete,
3667 cfs_time_seconds(interval));
3668 /* Must re-init the completion before testing anything,
3669 * including ln_mt_state.
3671 reinit_completion(&the_lnet.ln_mt_wait_complete);
3675 lnet_net_lock(LNET_LOCK_EX);
3676 the_lnet.ln_mt_state = LNET_MT_STATE_SHUTDOWN;
3677 lnet_net_unlock(LNET_LOCK_EX);
3679 /* signal that the monitor thread is exiting */
3680 up(&the_lnet.ln_mt_signal);
3688 * Returns == 0 if success
3689 * Returns > 0 if LNetMDBind or prior fails
3690 * Returns < 0 if LNetGet fails
3693 lnet_send_ping(lnet_nid_t dest_nid,
3694 struct lnet_handle_md *mdh, int nnis,
3695 void *user_data, lnet_handler_t handler, bool recovery)
3697 struct lnet_md md = { NULL };
3698 struct lnet_process_id id;
3699 struct lnet_ping_buffer *pbuf;
3702 if (dest_nid == LNET_NID_ANY) {
3707 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
3713 /* initialize md content */
3714 md.start = &pbuf->pb_info;
3715 md.length = LNET_PING_INFO_SIZE(nnis);
3716 md.threshold = 2; /* GET/REPLY */
3718 md.options = LNET_MD_TRUNCATE | LNET_MD_TRACK_RESPONSE;
3719 md.user_ptr = user_data;
3720 md.handler = handler;
3722 rc = LNetMDBind(&md, LNET_UNLINK, mdh);
3724 lnet_ping_buffer_decref(pbuf);
3725 CERROR("Can't bind MD: %d\n", rc);
3726 rc = -rc; /* change the rc to positive */
3729 id.pid = LNET_PID_LUSTRE;
3732 rc = LNetGet(LNET_NID_ANY, *mdh, id,
3733 LNET_RESERVED_PORTAL,
3734 LNET_PROTO_PING_MATCHBITS, 0, recovery);
3737 goto fail_unlink_md;
3743 LNetInvalidateMDHandle(mdh);
3749 lnet_handle_recovery_reply(struct lnet_mt_event_info *ev_info,
3750 int status, bool send, bool unlink_event)
3752 lnet_nid_t nid = ev_info->mt_nid;
3754 if (ev_info->mt_type == MT_TYPE_LOCAL_NI) {
3758 ni = lnet_nid2ni_locked(nid, 0);
3764 if (!send || (send && status != 0))
3765 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_PENDING;
3767 ni->ni_recovery_state |= LNET_NI_RECOVERY_FAILED;
3772 CERROR("local NI (%s) recovery failed with %d\n",
3773 libcfs_nid2str(nid), status);
3777 * need to increment healthv for the ni here, because in
3778 * the lnet_finalize() path we don't have access to this
3779 * NI. And in order to get access to it, we'll need to
3780 * carry forward too much information.
3781 * In the peer case, it'll naturally be incremented
3784 lnet_inc_healthv(&ni->ni_healthv,
3785 lnet_health_sensitivity);
3787 struct lnet_peer_ni *lpni;
3790 cpt = lnet_net_lock_current();
3791 lpni = lnet_find_peer_ni_locked(nid);
3793 lnet_net_unlock(cpt);
3796 spin_lock(&lpni->lpni_lock);
3797 if (!send || (send && status != 0))
3798 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3800 lpni->lpni_state |= LNET_PEER_NI_RECOVERY_FAILED;
3801 spin_unlock(&lpni->lpni_lock);
3802 lnet_peer_ni_decref_locked(lpni);
3803 lnet_net_unlock(cpt);
3806 CERROR("peer NI (%s) recovery failed with %d\n",
3807 libcfs_nid2str(nid), status);
3812 lnet_mt_event_handler(struct lnet_event *event)
3814 struct lnet_mt_event_info *ev_info = event->md_user_ptr;
3815 struct lnet_ping_buffer *pbuf;
3817 /* TODO: remove assert */
3818 LASSERT(event->type == LNET_EVENT_REPLY ||
3819 event->type == LNET_EVENT_SEND ||
3820 event->type == LNET_EVENT_UNLINK);
3822 CDEBUG(D_NET, "Received event: %d status: %d\n", event->type,
3825 switch (event->type) {
3826 case LNET_EVENT_UNLINK:
3827 CDEBUG(D_NET, "%s recovery ping unlinked\n",
3828 libcfs_nid2str(ev_info->mt_nid));
3830 case LNET_EVENT_REPLY:
3831 lnet_handle_recovery_reply(ev_info, event->status, false,
3832 event->type == LNET_EVENT_UNLINK);
3834 case LNET_EVENT_SEND:
3835 CDEBUG(D_NET, "%s recovery message sent %s:%d\n",
3836 libcfs_nid2str(ev_info->mt_nid),
3837 (event->status) ? "unsuccessfully" :
3838 "successfully", event->status);
3839 lnet_handle_recovery_reply(ev_info, event->status, true, false);
3842 CERROR("Unexpected event: %d\n", event->type);
3845 if (event->unlinked) {
3846 LIBCFS_FREE(ev_info, sizeof(*ev_info));
3847 pbuf = LNET_PING_INFO_TO_BUFFER(event->md_start);
3848 lnet_ping_buffer_decref(pbuf);
3853 lnet_rsp_tracker_create(void)
3855 struct list_head **rstqs;
3856 rstqs = lnet_create_array_of_queues();
3861 the_lnet.ln_mt_rstq = rstqs;
3867 lnet_rsp_tracker_clean(void)
3869 lnet_finalize_expired_responses();
3871 cfs_percpt_free(the_lnet.ln_mt_rstq);
3872 the_lnet.ln_mt_rstq = NULL;
3875 int lnet_monitor_thr_start(void)
3878 struct task_struct *task;
3880 if (the_lnet.ln_mt_state != LNET_MT_STATE_SHUTDOWN)
3883 rc = lnet_resendqs_create();
3887 rc = lnet_rsp_tracker_create();
3891 sema_init(&the_lnet.ln_mt_signal, 0);
3893 lnet_net_lock(LNET_LOCK_EX);
3894 the_lnet.ln_mt_state = LNET_MT_STATE_RUNNING;
3895 lnet_net_unlock(LNET_LOCK_EX);
3896 task = kthread_run(lnet_monitor_thread, NULL, "monitor_thread");
3899 CERROR("Can't start monitor thread: %d\n", rc);
3906 lnet_net_lock(LNET_LOCK_EX);
3907 the_lnet.ln_mt_state = LNET_MT_STATE_STOPPING;
3908 lnet_net_unlock(LNET_LOCK_EX);
3909 /* block until event callback signals exit */
3910 down(&the_lnet.ln_mt_signal);
3912 lnet_net_lock(LNET_LOCK_EX);
3913 the_lnet.ln_mt_state = LNET_MT_STATE_SHUTDOWN;
3914 lnet_net_unlock(LNET_LOCK_EX);
3915 lnet_rsp_tracker_clean();
3916 lnet_clean_local_ni_recoveryq();
3917 lnet_clean_peer_ni_recoveryq();
3918 lnet_clean_resendqs();
3919 the_lnet.ln_mt_handler = NULL;
3922 lnet_rsp_tracker_clean();
3923 lnet_clean_local_ni_recoveryq();
3924 lnet_clean_peer_ni_recoveryq();
3925 lnet_clean_resendqs();
3929 void lnet_monitor_thr_stop(void)
3931 if (the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN)
3934 LASSERT(the_lnet.ln_mt_state == LNET_MT_STATE_RUNNING);
3935 lnet_net_lock(LNET_LOCK_EX);
3936 the_lnet.ln_mt_state = LNET_MT_STATE_STOPPING;
3937 lnet_net_unlock(LNET_LOCK_EX);
3939 /* tell the monitor thread that we're shutting down */
3940 complete(&the_lnet.ln_mt_wait_complete);
3942 /* block until monitor thread signals that it's done */
3943 down(&the_lnet.ln_mt_signal);
3944 LASSERT(the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN);
3946 /* perform cleanup tasks */
3947 lnet_rsp_tracker_clean();
3948 lnet_clean_local_ni_recoveryq();
3949 lnet_clean_peer_ni_recoveryq();
3950 lnet_clean_resendqs();
3954 lnet_drop_message(struct lnet_ni *ni, int cpt, void *private, unsigned int nob,
3958 lnet_incr_stats(&ni->ni_stats, msg_type, LNET_STATS_TYPE_DROP);
3959 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
3960 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length += nob;
3961 lnet_net_unlock(cpt);
3963 lnet_ni_recv(ni, private, NULL, 0, 0, 0, nob);
3967 lnet_recv_put(struct lnet_ni *ni, struct lnet_msg *msg)
3969 struct lnet_hdr *hdr = &msg->msg_hdr;
3971 if (msg->msg_wanted != 0)
3972 lnet_setpayloadbuffer(msg);
3974 lnet_build_msg_event(msg, LNET_EVENT_PUT);
3976 /* Must I ACK? If so I'll grab the ack_wmd out of the header and put
3977 * it back into the ACK during lnet_finalize() */
3978 msg->msg_ack = (!lnet_is_wire_handle_none(&hdr->msg.put.ack_wmd) &&
3979 (msg->msg_md->md_options & LNET_MD_ACK_DISABLE) == 0);
3981 lnet_ni_recv(ni, msg->msg_private, msg, msg->msg_rx_delayed,
3982 msg->msg_offset, msg->msg_wanted, hdr->payload_length);
3986 lnet_parse_put(struct lnet_ni *ni, struct lnet_msg *msg)
3988 struct lnet_hdr *hdr = &msg->msg_hdr;
3989 struct lnet_match_info info;
3993 /* Convert put fields to host byte order */
3994 hdr->msg.put.match_bits = le64_to_cpu(hdr->msg.put.match_bits);
3995 hdr->msg.put.ptl_index = le32_to_cpu(hdr->msg.put.ptl_index);
3996 hdr->msg.put.offset = le32_to_cpu(hdr->msg.put.offset);
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_PUT;
4002 info.mi_portal = hdr->msg.put.ptl_index;
4003 info.mi_rlength = hdr->payload_length;
4004 info.mi_roffset = hdr->msg.put.offset;
4005 info.mi_mbits = hdr->msg.put.match_bits;
4006 info.mi_cpt = lnet_cpt_of_nid(msg->msg_initiator, ni);
4008 msg->msg_rx_ready_delay = ni->ni_net->net_lnd->lnd_eager_recv == NULL;
4009 ready_delay = msg->msg_rx_ready_delay;
4012 rc = lnet_ptl_match_md(&info, msg);
4017 case LNET_MATCHMD_OK:
4018 lnet_recv_put(ni, msg);
4021 case LNET_MATCHMD_NONE:
4023 /* no eager_recv or has already called it, should
4024 * have been attached on delayed list */
4027 rc = lnet_ni_eager_recv(ni, msg);
4034 case LNET_MATCHMD_DROP:
4035 CNETERR("Dropping PUT from %s portal %d match %llu"
4036 " offset %d length %d: %d\n",
4037 libcfs_id2str(info.mi_id), info.mi_portal,
4038 info.mi_mbits, info.mi_roffset, info.mi_rlength, rc);
4040 return -ENOENT; /* -ve: OK but no match */
4045 lnet_parse_get(struct lnet_ni *ni, struct lnet_msg *msg, int rdma_get)
4047 struct lnet_match_info info;
4048 struct lnet_hdr *hdr = &msg->msg_hdr;
4049 struct lnet_process_id source_id;
4050 struct lnet_handle_wire reply_wmd;
4053 /* Convert get fields to host byte order */
4054 hdr->msg.get.match_bits = le64_to_cpu(hdr->msg.get.match_bits);
4055 hdr->msg.get.ptl_index = le32_to_cpu(hdr->msg.get.ptl_index);
4056 hdr->msg.get.sink_length = le32_to_cpu(hdr->msg.get.sink_length);
4057 hdr->msg.get.src_offset = le32_to_cpu(hdr->msg.get.src_offset);
4059 source_id.nid = hdr->src_nid;
4060 source_id.pid = hdr->src_pid;
4061 /* Primary peer NID */
4062 info.mi_id.nid = msg->msg_initiator;
4063 info.mi_id.pid = hdr->src_pid;
4064 info.mi_opc = LNET_MD_OP_GET;
4065 info.mi_portal = hdr->msg.get.ptl_index;
4066 info.mi_rlength = hdr->msg.get.sink_length;
4067 info.mi_roffset = hdr->msg.get.src_offset;
4068 info.mi_mbits = hdr->msg.get.match_bits;
4069 info.mi_cpt = lnet_cpt_of_nid(msg->msg_initiator, ni);
4071 rc = lnet_ptl_match_md(&info, msg);
4072 if (rc == LNET_MATCHMD_DROP) {
4073 CNETERR("Dropping GET from %s portal %d match %llu"
4074 " offset %d length %d\n",
4075 libcfs_id2str(info.mi_id), info.mi_portal,
4076 info.mi_mbits, info.mi_roffset, info.mi_rlength);
4077 return -ENOENT; /* -ve: OK but no match */
4080 LASSERT(rc == LNET_MATCHMD_OK);
4082 lnet_build_msg_event(msg, LNET_EVENT_GET);
4084 reply_wmd = hdr->msg.get.return_wmd;
4086 lnet_prep_send(msg, LNET_MSG_REPLY, source_id,
4087 msg->msg_offset, msg->msg_wanted);
4089 msg->msg_hdr.msg.reply.dst_wmd = reply_wmd;
4092 /* The LND completes the REPLY from her recv procedure */
4093 lnet_ni_recv(ni, msg->msg_private, msg, 0,
4094 msg->msg_offset, msg->msg_len, msg->msg_len);
4098 lnet_ni_recv(ni, msg->msg_private, NULL, 0, 0, 0, 0);
4099 msg->msg_receiving = 0;
4101 rc = lnet_send(ni->ni_nid, msg, msg->msg_from);
4103 /* didn't get as far as lnet_ni_send() */
4104 CERROR("%s: Unable to send REPLY for GET from %s: %d\n",
4105 libcfs_nid2str(ni->ni_nid),
4106 libcfs_id2str(info.mi_id), rc);
4108 lnet_finalize(msg, rc);
4115 lnet_parse_reply(struct lnet_ni *ni, struct lnet_msg *msg)
4117 void *private = msg->msg_private;
4118 struct lnet_hdr *hdr = &msg->msg_hdr;
4119 struct lnet_process_id src = {0};
4120 struct lnet_libmd *md;
4121 unsigned int rlength;
4122 unsigned int mlength;
4125 cpt = lnet_cpt_of_cookie(hdr->msg.reply.dst_wmd.wh_object_cookie);
4128 src.nid = hdr->src_nid;
4129 src.pid = hdr->src_pid;
4131 /* NB handles only looked up by creator (no flips) */
4132 md = lnet_wire_handle2md(&hdr->msg.reply.dst_wmd);
4133 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
4134 CNETERR("%s: Dropping REPLY from %s for %s "
4136 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
4137 (md == NULL) ? "invalid" : "inactive",
4138 hdr->msg.reply.dst_wmd.wh_interface_cookie,
4139 hdr->msg.reply.dst_wmd.wh_object_cookie);
4140 if (md != NULL && md->md_me != NULL)
4141 CERROR("REPLY MD also attached to portal %d\n",
4142 md->md_me->me_portal);
4144 lnet_res_unlock(cpt);
4145 return -ENOENT; /* -ve: OK but no match */
4148 LASSERT(md->md_offset == 0);
4150 rlength = hdr->payload_length;
4151 mlength = min(rlength, md->md_length);
4153 if (mlength < rlength &&
4154 (md->md_options & LNET_MD_TRUNCATE) == 0) {
4155 CNETERR("%s: Dropping REPLY from %s length %d "
4156 "for MD %#llx would overflow (%d)\n",
4157 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
4158 rlength, hdr->msg.reply.dst_wmd.wh_object_cookie,
4160 lnet_res_unlock(cpt);
4161 return -ENOENT; /* -ve: OK but no match */
4164 CDEBUG(D_NET, "%s: Reply from %s of length %d/%d into md %#llx\n",
4165 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
4166 mlength, rlength, hdr->msg.reply.dst_wmd.wh_object_cookie);
4168 lnet_msg_attach_md(msg, md, 0, mlength);
4171 lnet_setpayloadbuffer(msg);
4173 lnet_res_unlock(cpt);
4175 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
4177 lnet_ni_recv(ni, private, msg, 0, 0, mlength, rlength);
4182 lnet_parse_ack(struct lnet_ni *ni, struct lnet_msg *msg)
4184 struct lnet_hdr *hdr = &msg->msg_hdr;
4185 struct lnet_process_id src = {0};
4186 struct lnet_libmd *md;
4189 src.nid = hdr->src_nid;
4190 src.pid = hdr->src_pid;
4192 /* Convert ack fields to host byte order */
4193 hdr->msg.ack.match_bits = le64_to_cpu(hdr->msg.ack.match_bits);
4194 hdr->msg.ack.mlength = le32_to_cpu(hdr->msg.ack.mlength);
4196 cpt = lnet_cpt_of_cookie(hdr->msg.ack.dst_wmd.wh_object_cookie);
4199 /* NB handles only looked up by creator (no flips) */
4200 md = lnet_wire_handle2md(&hdr->msg.ack.dst_wmd);
4201 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
4202 /* Don't moan; this is expected */
4204 "%s: Dropping ACK from %s to %s MD %#llx.%#llx\n",
4205 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
4206 (md == NULL) ? "invalid" : "inactive",
4207 hdr->msg.ack.dst_wmd.wh_interface_cookie,
4208 hdr->msg.ack.dst_wmd.wh_object_cookie);
4209 if (md != NULL && md->md_me != NULL)
4210 CERROR("Source MD also attached to portal %d\n",
4211 md->md_me->me_portal);
4213 lnet_res_unlock(cpt);
4214 return -ENOENT; /* -ve! */
4217 CDEBUG(D_NET, "%s: ACK from %s into md %#llx\n",
4218 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
4219 hdr->msg.ack.dst_wmd.wh_object_cookie);
4221 lnet_msg_attach_md(msg, md, 0, 0);
4223 lnet_res_unlock(cpt);
4225 lnet_build_msg_event(msg, LNET_EVENT_ACK);
4227 lnet_ni_recv(ni, msg->msg_private, msg, 0, 0, 0, msg->msg_len);
4232 * \retval LNET_CREDIT_OK If \a msg is forwarded
4233 * \retval LNET_CREDIT_WAIT If \a msg is blocked because w/o buffer
4234 * \retval -ve error code
4237 lnet_parse_forward_locked(struct lnet_ni *ni, struct lnet_msg *msg)
4241 if (!the_lnet.ln_routing)
4244 if (msg->msg_rxpeer->lpni_rtrcredits <= 0 ||
4245 lnet_msg2bufpool(msg)->rbp_credits <= 0) {
4246 if (ni->ni_net->net_lnd->lnd_eager_recv == NULL) {
4247 msg->msg_rx_ready_delay = 1;
4249 lnet_net_unlock(msg->msg_rx_cpt);
4250 rc = lnet_ni_eager_recv(ni, msg);
4251 lnet_net_lock(msg->msg_rx_cpt);
4256 rc = lnet_post_routed_recv_locked(msg, 0);
4261 lnet_parse_local(struct lnet_ni *ni, struct lnet_msg *msg)
4265 switch (msg->msg_type) {
4267 rc = lnet_parse_ack(ni, msg);
4270 rc = lnet_parse_put(ni, msg);
4273 rc = lnet_parse_get(ni, msg, msg->msg_rdma_get);
4275 case LNET_MSG_REPLY:
4276 rc = lnet_parse_reply(ni, msg);
4278 default: /* prevent an unused label if !kernel */
4283 LASSERT(rc == 0 || rc == -ENOENT);
4288 lnet_msgtyp2str (int type)
4297 case LNET_MSG_REPLY:
4299 case LNET_MSG_HELLO:
4302 return ("<UNKNOWN>");
4307 lnet_parse(struct lnet_ni *ni, struct lnet_hdr *hdr, lnet_nid_t from_nid,
4308 void *private, int rdma_req)
4310 struct lnet_peer_ni *lpni;
4311 struct lnet_msg *msg;
4312 __u32 payload_length;
4313 lnet_pid_t dest_pid;
4314 lnet_nid_t dest_nid;
4322 LASSERT (!in_interrupt ());
4324 type = le32_to_cpu(hdr->type);
4325 src_nid = le64_to_cpu(hdr->src_nid);
4326 dest_nid = le64_to_cpu(hdr->dest_nid);
4327 dest_pid = le32_to_cpu(hdr->dest_pid);
4328 payload_length = le32_to_cpu(hdr->payload_length);
4330 for_me = (ni->ni_nid == dest_nid);
4331 cpt = lnet_cpt_of_nid(from_nid, ni);
4333 CDEBUG(D_NET, "TRACE: %s(%s) <- %s : %s - %s\n",
4334 libcfs_nid2str(dest_nid),
4335 libcfs_nid2str(ni->ni_nid),
4336 libcfs_nid2str(src_nid),
4337 lnet_msgtyp2str(type),
4338 (for_me) ? "for me" : "routed");
4343 if (payload_length > 0) {
4344 CERROR("%s, src %s: bad %s payload %d (0 expected)\n",
4345 libcfs_nid2str(from_nid),
4346 libcfs_nid2str(src_nid),
4347 lnet_msgtyp2str(type), payload_length);
4353 case LNET_MSG_REPLY:
4354 if (payload_length >
4355 (__u32)(for_me ? LNET_MAX_PAYLOAD : LNET_MTU)) {
4356 CERROR("%s, src %s: bad %s payload %d "
4357 "(%d max expected)\n",
4358 libcfs_nid2str(from_nid),
4359 libcfs_nid2str(src_nid),
4360 lnet_msgtyp2str(type),
4362 for_me ? LNET_MAX_PAYLOAD : LNET_MTU);
4368 CERROR("%s, src %s: Bad message type 0x%x\n",
4369 libcfs_nid2str(from_nid),
4370 libcfs_nid2str(src_nid), type);
4374 if (the_lnet.ln_routing &&
4375 ni->ni_net->net_last_alive != ktime_get_real_seconds()) {
4377 spin_lock(&ni->ni_net->net_lock);
4378 ni->ni_net->net_last_alive = ktime_get_real_seconds();
4379 spin_unlock(&ni->ni_net->net_lock);
4380 push = lnet_ni_set_status_locked(ni, LNET_NI_STATUS_UP);
4385 lnet_push_update_to_peers(1);
4387 /* Regard a bad destination NID as a protocol error. Senders should
4388 * know what they're doing; if they don't they're misconfigured, buggy
4389 * or malicious so we chop them off at the knees :) */
4392 if (LNET_NIDNET(dest_nid) == LNET_NIDNET(ni->ni_nid)) {
4393 /* should have gone direct */
4394 CERROR("%s, src %s: Bad dest nid %s "
4395 "(should have been sent direct)\n",
4396 libcfs_nid2str(from_nid),
4397 libcfs_nid2str(src_nid),
4398 libcfs_nid2str(dest_nid));
4402 if (lnet_islocalnid(dest_nid)) {
4403 /* dest is another local NI; sender should have used
4404 * this node's NID on its own network */
4405 CERROR("%s, src %s: Bad dest nid %s "
4406 "(it's my nid but on a different network)\n",
4407 libcfs_nid2str(from_nid),
4408 libcfs_nid2str(src_nid),
4409 libcfs_nid2str(dest_nid));
4413 if (rdma_req && type == LNET_MSG_GET) {
4414 CERROR("%s, src %s: Bad optimized GET for %s "
4415 "(final destination must be me)\n",
4416 libcfs_nid2str(from_nid),
4417 libcfs_nid2str(src_nid),
4418 libcfs_nid2str(dest_nid));
4422 if (!the_lnet.ln_routing) {
4423 CERROR("%s, src %s: Dropping message for %s "
4424 "(routing not enabled)\n",
4425 libcfs_nid2str(from_nid),
4426 libcfs_nid2str(src_nid),
4427 libcfs_nid2str(dest_nid));
4432 /* Message looks OK; we're not going to return an error, so we MUST
4433 * call back lnd_recv() come what may... */
4435 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
4436 fail_peer(src_nid, 0)) { /* shall we now? */
4437 CERROR("%s, src %s: Dropping %s to simulate failure\n",
4438 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4439 lnet_msgtyp2str(type));
4443 if (!list_empty(&the_lnet.ln_drop_rules) &&
4444 lnet_drop_rule_match(hdr, ni->ni_nid, NULL)) {
4446 "%s, src %s, dst %s: Dropping %s to simulate silent message loss\n",
4447 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4448 libcfs_nid2str(dest_nid), lnet_msgtyp2str(type));
4452 if (lnet_drop_asym_route && for_me &&
4453 LNET_NIDNET(src_nid) != LNET_NIDNET(from_nid)) {
4454 struct lnet_net *net;
4455 struct lnet_remotenet *rnet;
4458 /* we are dealing with a routed message,
4459 * so see if route to reach src_nid goes through from_nid
4462 net = lnet_get_net_locked(LNET_NIDNET(ni->ni_nid));
4464 lnet_net_unlock(cpt);
4465 CERROR("net %s not found\n",
4466 libcfs_net2str(LNET_NIDNET(ni->ni_nid)));
4470 rnet = lnet_find_rnet_locked(LNET_NIDNET(src_nid));
4472 struct lnet_peer *gw = NULL;
4473 struct lnet_peer_ni *lpni = NULL;
4474 struct lnet_route *route;
4476 list_for_each_entry(route, &rnet->lrn_routes, lr_list) {
4478 gw = route->lr_gateway;
4479 if (route->lr_lnet != net->net_id)
4482 * if the nid is one of the gateway's NIDs
4483 * then this is a valid gateway
4485 while ((lpni = lnet_get_next_peer_ni_locked(gw,
4486 NULL, lpni)) != NULL) {
4487 if (lpni->lpni_nid == from_nid) {
4494 lnet_net_unlock(cpt);
4496 /* we would not use from_nid to route a message to
4498 * => asymmetric routing detected but forbidden
4500 CERROR("%s, src %s: Dropping asymmetrical route %s\n",
4501 libcfs_nid2str(from_nid),
4502 libcfs_nid2str(src_nid), lnet_msgtyp2str(type));
4507 msg = lnet_msg_alloc();
4509 CERROR("%s, src %s: Dropping %s (out of memory)\n",
4510 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4511 lnet_msgtyp2str(type));
4515 /* msg zeroed in lnet_msg_alloc; i.e. flags all clear,
4516 * pointers NULL etc */
4518 msg->msg_type = type;
4519 msg->msg_private = private;
4520 msg->msg_receiving = 1;
4521 msg->msg_rdma_get = rdma_req;
4522 msg->msg_len = msg->msg_wanted = payload_length;
4523 msg->msg_offset = 0;
4524 msg->msg_hdr = *hdr;
4525 /* for building message event */
4526 msg->msg_from = from_nid;
4528 msg->msg_target.pid = dest_pid;
4529 msg->msg_target.nid = dest_nid;
4530 msg->msg_routing = 1;
4533 /* convert common msg->hdr fields to host byteorder */
4534 msg->msg_hdr.type = type;
4535 msg->msg_hdr.src_nid = src_nid;
4536 msg->msg_hdr.src_pid = le32_to_cpu(msg->msg_hdr.src_pid);
4537 msg->msg_hdr.dest_nid = dest_nid;
4538 msg->msg_hdr.dest_pid = dest_pid;
4539 msg->msg_hdr.payload_length = payload_length;
4543 lpni = lnet_nid2peerni_locked(from_nid, ni->ni_nid, cpt);
4545 lnet_net_unlock(cpt);
4546 CERROR("%s, src %s: Dropping %s "
4547 "(error %ld looking up sender)\n",
4548 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4549 lnet_msgtyp2str(type), PTR_ERR(lpni));
4551 if (rc == -ESHUTDOWN)
4552 /* We are shutting down. Don't do anything more */
4557 if (the_lnet.ln_routing)
4558 lpni->lpni_last_alive = ktime_get_seconds();
4560 msg->msg_rxpeer = lpni;
4562 lnet_ni_addref_locked(ni, cpt);
4563 /* Multi-Rail: Primary NID of source. */
4564 msg->msg_initiator = lnet_peer_primary_nid_locked(src_nid);
4567 * mark the status of this lpni as UP since we received a message
4568 * from it. The ping response reports back the ns_status which is
4569 * marked on the remote as up or down and we cache it here.
4571 msg->msg_rxpeer->lpni_ns_status = LNET_NI_STATUS_UP;
4573 lnet_msg_commit(msg, cpt);
4575 /* message delay simulation */
4576 if (unlikely(!list_empty(&the_lnet.ln_delay_rules) &&
4577 lnet_delay_rule_match_locked(hdr, msg))) {
4578 lnet_net_unlock(cpt);
4583 rc = lnet_parse_forward_locked(ni, msg);
4584 lnet_net_unlock(cpt);
4589 if (rc == LNET_CREDIT_OK) {
4590 lnet_ni_recv(ni, msg->msg_private, msg, 0,
4591 0, payload_length, payload_length);
4596 lnet_net_unlock(cpt);
4598 rc = lnet_parse_local(ni, msg);
4604 LASSERT(msg->msg_md == NULL);
4605 lnet_finalize(msg, rc);
4608 lnet_drop_message(ni, cpt, private, payload_length, type);
4611 EXPORT_SYMBOL(lnet_parse);
4614 lnet_drop_delayed_msg_list(struct list_head *head, char *reason)
4616 while (!list_empty(head)) {
4617 struct lnet_process_id id = {0};
4618 struct lnet_msg *msg;
4620 msg = list_entry(head->next, struct lnet_msg, msg_list);
4621 list_del(&msg->msg_list);
4623 id.nid = msg->msg_hdr.src_nid;
4624 id.pid = msg->msg_hdr.src_pid;
4626 LASSERT(msg->msg_md == NULL);
4627 LASSERT(msg->msg_rx_delayed);
4628 LASSERT(msg->msg_rxpeer != NULL);
4629 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
4631 CWARN("Dropping delayed PUT from %s portal %d match %llu"
4632 " offset %d length %d: %s\n",
4634 msg->msg_hdr.msg.put.ptl_index,
4635 msg->msg_hdr.msg.put.match_bits,
4636 msg->msg_hdr.msg.put.offset,
4637 msg->msg_hdr.payload_length, reason);
4639 /* NB I can't drop msg's ref on msg_rxpeer until after I've
4640 * called lnet_drop_message(), so I just hang onto msg as well
4641 * until that's done */
4643 lnet_drop_message(msg->msg_rxni, msg->msg_rx_cpt,
4644 msg->msg_private, msg->msg_len,
4647 msg->msg_no_resend = true;
4649 * NB: message will not generate event because w/o attached MD,
4650 * but we still should give error code so lnet_msg_decommit()
4651 * can skip counters operations and other checks.
4653 lnet_finalize(msg, -ENOENT);
4658 lnet_recv_delayed_msg_list(struct list_head *head)
4660 while (!list_empty(head)) {
4661 struct lnet_msg *msg;
4662 struct lnet_process_id id;
4664 msg = list_entry(head->next, struct lnet_msg, msg_list);
4665 list_del(&msg->msg_list);
4667 /* md won't disappear under me, since each msg
4668 * holds a ref on it */
4670 id.nid = msg->msg_hdr.src_nid;
4671 id.pid = msg->msg_hdr.src_pid;
4673 LASSERT(msg->msg_rx_delayed);
4674 LASSERT(msg->msg_md != NULL);
4675 LASSERT(msg->msg_rxpeer != NULL);
4676 LASSERT(msg->msg_rxni != NULL);
4677 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
4679 CDEBUG(D_NET, "Resuming delayed PUT from %s portal %d "
4680 "match %llu offset %d length %d.\n",
4681 libcfs_id2str(id), msg->msg_hdr.msg.put.ptl_index,
4682 msg->msg_hdr.msg.put.match_bits,
4683 msg->msg_hdr.msg.put.offset,
4684 msg->msg_hdr.payload_length);
4686 lnet_recv_put(msg->msg_rxni, msg);
4691 lnet_attach_rsp_tracker(struct lnet_rsp_tracker *rspt, int cpt,
4692 struct lnet_libmd *md, struct lnet_handle_md mdh)
4695 struct lnet_rsp_tracker *local_rspt;
4698 * MD has a refcount taken by message so it's not going away.
4699 * The MD however can be looked up. We need to secure the access
4700 * to the md_rspt_ptr by taking the res_lock.
4701 * The rspt can be accessed without protection up to when it gets
4702 * added to the list.
4706 local_rspt = md->md_rspt_ptr;
4707 timeout_ns = lnet_transaction_timeout * NSEC_PER_SEC;
4708 if (local_rspt != NULL) {
4710 * we already have an rspt attached to the md, so we'll
4711 * update the deadline on that one.
4713 lnet_rspt_free(rspt, cpt);
4716 rspt->rspt_mdh = mdh;
4717 rspt->rspt_cpt = cpt;
4718 /* store the rspt so we can access it when we get the REPLY */
4719 md->md_rspt_ptr = rspt;
4722 local_rspt->rspt_deadline = ktime_add_ns(ktime_get(), timeout_ns);
4725 * add to the list of tracked responses. It's added to tail of the
4726 * list in order to expire all the older entries first.
4729 list_move_tail(&local_rspt->rspt_on_list, the_lnet.ln_mt_rstq[cpt]);
4730 lnet_net_unlock(cpt);
4731 lnet_res_unlock(cpt);
4735 * Initiate an asynchronous PUT operation.
4737 * There are several events associated with a PUT: completion of the send on
4738 * the initiator node (LNET_EVENT_SEND), and when the send completes
4739 * successfully, the receipt of an acknowledgment (LNET_EVENT_ACK) indicating
4740 * that the operation was accepted by the target. The event LNET_EVENT_PUT is
4741 * used at the target node to indicate the completion of incoming data
4744 * The local events will be logged in the EQ associated with the MD pointed to
4745 * by \a mdh handle. Using a MD without an associated EQ results in these
4746 * events being discarded. In this case, the caller must have another
4747 * mechanism (e.g., a higher level protocol) for determining when it is safe
4748 * to modify the memory region associated with the MD.
4750 * Note that LNet does not guarantee the order of LNET_EVENT_SEND and
4751 * LNET_EVENT_ACK, though intuitively ACK should happen after SEND.
4753 * \param self Indicates the NID of a local interface through which to send
4754 * the PUT request. Use LNET_NID_ANY to let LNet choose one by itself.
4755 * \param mdh A handle for the MD that describes the memory to be sent. The MD
4756 * must be "free floating" (See LNetMDBind()).
4757 * \param ack Controls whether an acknowledgment is requested.
4758 * Acknowledgments are only sent when they are requested by the initiating
4759 * process and the target MD enables them.
4760 * \param target A process identifier for the target process.
4761 * \param portal The index in the \a target's portal table.
4762 * \param match_bits The match bits to use for MD selection at the target
4764 * \param offset The offset into the target MD (only used when the target
4765 * MD has the LNET_MD_MANAGE_REMOTE option set).
4766 * \param hdr_data 64 bits of user data that can be included in the message
4767 * header. This data is written to an event queue entry at the target if an
4768 * EQ is present on the matching MD.
4770 * \retval 0 Success, and only in this case events will be generated
4771 * and logged to EQ (if it exists).
4772 * \retval -EIO Simulated failure.
4773 * \retval -ENOMEM Memory allocation failure.
4774 * \retval -ENOENT Invalid MD object.
4776 * \see struct lnet_event::hdr_data and lnet_event_kind_t.
4779 LNetPut(lnet_nid_t self, struct lnet_handle_md mdh, enum lnet_ack_req ack,
4780 struct lnet_process_id target, unsigned int portal,
4781 __u64 match_bits, unsigned int offset,
4784 struct lnet_msg *msg;
4785 struct lnet_libmd *md;
4788 struct lnet_rsp_tracker *rspt = NULL;
4790 LASSERT(the_lnet.ln_refcount > 0);
4792 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
4793 fail_peer(target.nid, 1)) { /* shall we now? */
4794 CERROR("Dropping PUT to %s: simulated failure\n",
4795 libcfs_id2str(target));
4799 msg = lnet_msg_alloc();
4801 CERROR("Dropping PUT to %s: ENOMEM on struct lnet_msg\n",
4802 libcfs_id2str(target));
4805 msg->msg_vmflush = !!(current->flags & PF_MEMALLOC);
4807 cpt = lnet_cpt_of_cookie(mdh.cookie);
4809 if (ack == LNET_ACK_REQ) {
4810 rspt = lnet_rspt_alloc(cpt);
4812 CERROR("Dropping PUT to %s: ENOMEM on response tracker\n",
4813 libcfs_id2str(target));
4816 INIT_LIST_HEAD(&rspt->rspt_on_list);
4821 md = lnet_handle2md(&mdh);
4822 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
4823 CERROR("Dropping PUT (%llu:%d:%s): MD (%d) invalid\n",
4824 match_bits, portal, libcfs_id2str(target),
4825 md == NULL ? -1 : md->md_threshold);
4826 if (md != NULL && md->md_me != NULL)
4827 CERROR("Source MD also attached to portal %d\n",
4828 md->md_me->me_portal);
4829 lnet_res_unlock(cpt);
4832 lnet_rspt_free(rspt, cpt);
4838 CDEBUG(D_NET, "LNetPut -> %s\n", libcfs_id2str(target));
4840 lnet_msg_attach_md(msg, md, 0, 0);
4842 lnet_prep_send(msg, LNET_MSG_PUT, target, 0, md->md_length);
4844 msg->msg_hdr.msg.put.match_bits = cpu_to_le64(match_bits);
4845 msg->msg_hdr.msg.put.ptl_index = cpu_to_le32(portal);
4846 msg->msg_hdr.msg.put.offset = cpu_to_le32(offset);
4847 msg->msg_hdr.msg.put.hdr_data = hdr_data;
4849 /* NB handles only looked up by creator (no flips) */
4850 if (ack == LNET_ACK_REQ) {
4851 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
4852 the_lnet.ln_interface_cookie;
4853 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
4854 md->md_lh.lh_cookie;
4856 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
4857 LNET_WIRE_HANDLE_COOKIE_NONE;
4858 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
4859 LNET_WIRE_HANDLE_COOKIE_NONE;
4862 lnet_res_unlock(cpt);
4864 lnet_build_msg_event(msg, LNET_EVENT_SEND);
4866 if (rspt && lnet_response_tracking_enabled(LNET_MSG_PUT,
4868 lnet_attach_rsp_tracker(rspt, cpt, md, mdh);
4870 lnet_rspt_free(rspt, cpt);
4872 if (CFS_FAIL_CHECK_ORSET(CFS_FAIL_PTLRPC_OST_BULK_CB2,
4876 rc = lnet_send(self, msg, LNET_NID_ANY);
4879 CNETERR("Error sending PUT to %s: %d\n",
4880 libcfs_id2str(target), rc);
4881 msg->msg_no_resend = true;
4882 lnet_finalize(msg, rc);
4885 /* completion will be signalled by an event */
4888 EXPORT_SYMBOL(LNetPut);
4891 * The LND can DMA direct to the GET md (i.e. no REPLY msg). This
4892 * returns a msg for the LND to pass to lnet_finalize() when the sink
4893 * data has been received.
4895 * CAVEAT EMPTOR: 'getmsg' is the original GET, which is freed when
4896 * lnet_finalize() is called on it, so the LND must call this first
4899 lnet_create_reply_msg(struct lnet_ni *ni, struct lnet_msg *getmsg)
4901 struct lnet_msg *msg = lnet_msg_alloc();
4902 struct lnet_libmd *getmd = getmsg->msg_md;
4903 struct lnet_process_id peer_id = getmsg->msg_target;
4906 LASSERT(!getmsg->msg_target_is_router);
4907 LASSERT(!getmsg->msg_routing);
4910 CERROR("%s: Dropping REPLY from %s: can't allocate msg\n",
4911 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id));
4915 cpt = lnet_cpt_of_cookie(getmd->md_lh.lh_cookie);
4918 LASSERT(getmd->md_refcount > 0);
4920 if (getmd->md_threshold == 0) {
4921 CERROR("%s: Dropping REPLY from %s for inactive MD %p\n",
4922 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id),
4924 lnet_res_unlock(cpt);
4928 LASSERT(getmd->md_offset == 0);
4930 CDEBUG(D_NET, "%s: Reply from %s md %p\n",
4931 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id), getmd);
4933 /* setup information for lnet_build_msg_event */
4934 msg->msg_initiator = getmsg->msg_txpeer->lpni_peer_net->lpn_peer->lp_primary_nid;
4935 msg->msg_from = peer_id.nid;
4936 msg->msg_type = LNET_MSG_GET; /* flag this msg as an "optimized" GET */
4937 msg->msg_hdr.src_nid = peer_id.nid;
4938 msg->msg_hdr.payload_length = getmd->md_length;
4939 msg->msg_receiving = 1; /* required by lnet_msg_attach_md */
4941 lnet_msg_attach_md(msg, getmd, getmd->md_offset, getmd->md_length);
4942 lnet_res_unlock(cpt);
4944 cpt = lnet_cpt_of_nid(peer_id.nid, ni);
4947 lnet_msg_commit(msg, cpt);
4948 lnet_net_unlock(cpt);
4950 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
4955 cpt = lnet_cpt_of_nid(peer_id.nid, ni);
4958 lnet_incr_stats(&ni->ni_stats, LNET_MSG_GET, LNET_STATS_TYPE_DROP);
4959 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
4960 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length +=
4962 lnet_net_unlock(cpt);
4969 EXPORT_SYMBOL(lnet_create_reply_msg);
4972 lnet_set_reply_msg_len(struct lnet_ni *ni, struct lnet_msg *reply,
4975 /* Set the REPLY length, now the RDMA that elides the REPLY message has
4976 * completed and I know it. */
4977 LASSERT(reply != NULL);
4978 LASSERT(reply->msg_type == LNET_MSG_GET);
4979 LASSERT(reply->msg_ev.type == LNET_EVENT_REPLY);
4981 /* NB I trusted my peer to RDMA. If she tells me she's written beyond
4982 * the end of my buffer, I might as well be dead. */
4983 LASSERT(len <= reply->msg_ev.mlength);
4985 reply->msg_ev.mlength = len;
4987 EXPORT_SYMBOL(lnet_set_reply_msg_len);
4990 * Initiate an asynchronous GET operation.
4992 * On the initiator node, an LNET_EVENT_SEND is logged when the GET request
4993 * is sent, and an LNET_EVENT_REPLY is logged when the data returned from
4994 * the target node in the REPLY has been written to local MD.
4996 * On the target node, an LNET_EVENT_GET is logged when the GET request
4997 * arrives and is accepted into a MD.
4999 * \param self,target,portal,match_bits,offset See the discussion in LNetPut().
5000 * \param mdh A handle for the MD that describes the memory into which the
5001 * requested data will be received. The MD must be "free floating" (See LNetMDBind()).
5003 * \retval 0 Success, and only in this case events will be generated
5004 * and logged to EQ (if it exists) of the MD.
5005 * \retval -EIO Simulated failure.
5006 * \retval -ENOMEM Memory allocation failure.
5007 * \retval -ENOENT Invalid MD object.
5010 LNetGet(lnet_nid_t self, struct lnet_handle_md mdh,
5011 struct lnet_process_id target, unsigned int portal,
5012 __u64 match_bits, unsigned int offset, bool recovery)
5014 struct lnet_msg *msg;
5015 struct lnet_libmd *md;
5016 struct lnet_rsp_tracker *rspt;
5020 LASSERT(the_lnet.ln_refcount > 0);
5022 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
5023 fail_peer(target.nid, 1)) /* shall we now? */
5025 CERROR("Dropping GET to %s: simulated failure\n",
5026 libcfs_id2str(target));
5030 msg = lnet_msg_alloc();
5032 CERROR("Dropping GET to %s: ENOMEM on struct lnet_msg\n",
5033 libcfs_id2str(target));
5037 cpt = lnet_cpt_of_cookie(mdh.cookie);
5039 rspt = lnet_rspt_alloc(cpt);
5041 CERROR("Dropping GET to %s: ENOMEM on response tracker\n",
5042 libcfs_id2str(target));
5045 INIT_LIST_HEAD(&rspt->rspt_on_list);
5047 msg->msg_recovery = recovery;
5051 md = lnet_handle2md(&mdh);
5052 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
5053 CERROR("Dropping GET (%llu:%d:%s): MD (%d) invalid\n",
5054 match_bits, portal, libcfs_id2str(target),
5055 md == NULL ? -1 : md->md_threshold);
5056 if (md != NULL && md->md_me != NULL)
5057 CERROR("REPLY MD also attached to portal %d\n",
5058 md->md_me->me_portal);
5060 lnet_res_unlock(cpt);
5063 lnet_rspt_free(rspt, cpt);
5067 CDEBUG(D_NET, "LNetGet -> %s\n", libcfs_id2str(target));
5069 lnet_msg_attach_md(msg, md, 0, 0);
5071 lnet_prep_send(msg, LNET_MSG_GET, target, 0, 0);
5073 msg->msg_hdr.msg.get.match_bits = cpu_to_le64(match_bits);
5074 msg->msg_hdr.msg.get.ptl_index = cpu_to_le32(portal);
5075 msg->msg_hdr.msg.get.src_offset = cpu_to_le32(offset);
5076 msg->msg_hdr.msg.get.sink_length = cpu_to_le32(md->md_length);
5078 /* NB handles only looked up by creator (no flips) */
5079 msg->msg_hdr.msg.get.return_wmd.wh_interface_cookie =
5080 the_lnet.ln_interface_cookie;
5081 msg->msg_hdr.msg.get.return_wmd.wh_object_cookie =
5082 md->md_lh.lh_cookie;
5084 lnet_res_unlock(cpt);
5086 lnet_build_msg_event(msg, LNET_EVENT_SEND);
5088 if (lnet_response_tracking_enabled(LNET_MSG_GET, md->md_options))
5089 lnet_attach_rsp_tracker(rspt, cpt, md, mdh);
5091 lnet_rspt_free(rspt, cpt);
5093 rc = lnet_send(self, msg, LNET_NID_ANY);
5095 CNETERR("Error sending GET to %s: %d\n",
5096 libcfs_id2str(target), rc);
5097 msg->msg_no_resend = true;
5098 lnet_finalize(msg, rc);
5101 /* completion will be signalled by an event */
5104 EXPORT_SYMBOL(LNetGet);
5107 * Calculate distance to node at \a dstnid.
5109 * \param dstnid Target NID.
5110 * \param srcnidp If not NULL, NID of the local interface to reach \a dstnid
5112 * \param orderp If not NULL, order of the route to reach \a dstnid is saved
5115 * \retval 0 If \a dstnid belongs to a local interface, and reserved option
5116 * local_nid_dist_zero is set, which is the default.
5117 * \retval positives Distance to target NID, i.e. number of hops plus one.
5118 * \retval -EHOSTUNREACH If \a dstnid is not reachable.
5121 LNetDist(lnet_nid_t dstnid, lnet_nid_t *srcnidp, __u32 *orderp)
5123 struct list_head *e;
5124 struct lnet_ni *ni = NULL;
5125 struct lnet_remotenet *rnet;
5126 __u32 dstnet = LNET_NIDNET(dstnid);
5130 struct list_head *rn_list;
5132 /* if !local_nid_dist_zero, I don't return a distance of 0 ever
5133 * (when lustre sees a distance of 0, it substitutes 0@lo), so I
5134 * keep order 0 free for 0@lo and order 1 free for a local NID
5137 LASSERT(the_lnet.ln_refcount > 0);
5139 cpt = lnet_net_lock_current();
5141 while ((ni = lnet_get_next_ni_locked(NULL, ni))) {
5142 if (ni->ni_nid == dstnid) {
5143 if (srcnidp != NULL)
5145 if (orderp != NULL) {
5146 if (dstnid == LNET_NID_LO_0)
5151 lnet_net_unlock(cpt);
5153 return local_nid_dist_zero ? 0 : 1;
5156 if (LNET_NIDNET(ni->ni_nid) == dstnet) {
5157 /* Check if ni was originally created in
5158 * current net namespace.
5159 * If not, assign order above 0xffff0000,
5160 * to make this ni not a priority. */
5161 if (current->nsproxy &&
5162 !net_eq(ni->ni_net_ns, current->nsproxy->net_ns))
5163 order += 0xffff0000;
5164 if (srcnidp != NULL)
5165 *srcnidp = ni->ni_nid;
5168 lnet_net_unlock(cpt);
5175 rn_list = lnet_net2rnethash(dstnet);
5176 list_for_each(e, rn_list) {
5177 rnet = list_entry(e, struct lnet_remotenet, lrn_list);
5179 if (rnet->lrn_net == dstnet) {
5180 struct lnet_route *route;
5181 struct lnet_route *shortest = NULL;
5182 __u32 shortest_hops = LNET_UNDEFINED_HOPS;
5185 LASSERT(!list_empty(&rnet->lrn_routes));
5187 list_for_each_entry(route, &rnet->lrn_routes,
5189 route_hops = route->lr_hops;
5190 if (route_hops == LNET_UNDEFINED_HOPS)
5192 if (shortest == NULL ||
5193 route_hops < shortest_hops) {
5195 shortest_hops = route_hops;
5199 LASSERT(shortest != NULL);
5200 hops = shortest_hops;
5201 if (srcnidp != NULL) {
5202 struct lnet_net *net;
5203 net = lnet_get_net_locked(shortest->lr_lnet);
5205 ni = lnet_get_next_ni_locked(net, NULL);
5206 *srcnidp = ni->ni_nid;
5210 lnet_net_unlock(cpt);
5216 lnet_net_unlock(cpt);
5217 return -EHOSTUNREACH;
5219 EXPORT_SYMBOL(LNetDist);