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);
844 /* can't get here if we're sending to the loopback interface */
845 if (the_lnet.ln_loni)
846 LASSERT(lp->lpni_nid != the_lnet.ln_loni->ni_nid);
848 /* NB 'lp' is always the next hop */
849 if ((msg->msg_target.pid & LNET_PID_USERFLAG) == 0 &&
850 lnet_peer_alive_locked(ni, lp, msg) == 0) {
851 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
852 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length +=
854 lnet_net_unlock(cpt);
856 lnet_incr_stats(&msg->msg_txpeer->lpni_stats,
858 LNET_STATS_TYPE_DROP);
860 lnet_incr_stats(&msg->msg_txni->ni_stats,
862 LNET_STATS_TYPE_DROP);
864 CNETERR("Dropping message for %s: peer not alive\n",
865 libcfs_id2str(msg->msg_target));
866 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_DROPPED;
868 lnet_finalize(msg, -EHOSTUNREACH);
871 return -EHOSTUNREACH;
874 if (msg->msg_md != NULL &&
875 (msg->msg_md->md_flags & LNET_MD_FLAG_ABORTED) != 0) {
876 lnet_net_unlock(cpt);
878 CNETERR("Aborting message for %s: LNetM[DE]Unlink() already "
879 "called on the MD/ME.\n",
880 libcfs_id2str(msg->msg_target));
882 msg->msg_no_resend = true;
883 CDEBUG(D_NET, "msg %p to %s canceled and will not be resent\n",
884 msg, libcfs_id2str(msg->msg_target));
885 lnet_finalize(msg, -ECANCELED);
892 if (!msg->msg_peertxcredit) {
893 spin_lock(&lp->lpni_lock);
894 LASSERT((lp->lpni_txcredits < 0) ==
895 !list_empty(&lp->lpni_txq));
897 msg->msg_peertxcredit = 1;
898 lp->lpni_txqnob += msg->msg_len + sizeof(struct lnet_hdr);
899 lp->lpni_txcredits--;
901 if (lp->lpni_txcredits < lp->lpni_mintxcredits)
902 lp->lpni_mintxcredits = lp->lpni_txcredits;
904 if (lp->lpni_txcredits < 0) {
905 msg->msg_tx_delayed = 1;
906 list_add_tail(&msg->msg_list, &lp->lpni_txq);
907 spin_unlock(&lp->lpni_lock);
908 return LNET_CREDIT_WAIT;
910 spin_unlock(&lp->lpni_lock);
913 if (!msg->msg_txcredit) {
914 LASSERT((tq->tq_credits < 0) ==
915 !list_empty(&tq->tq_delayed));
917 msg->msg_txcredit = 1;
919 atomic_dec(&ni->ni_tx_credits);
921 if (tq->tq_credits < tq->tq_credits_min)
922 tq->tq_credits_min = tq->tq_credits;
924 if (tq->tq_credits < 0) {
925 msg->msg_tx_delayed = 1;
926 list_add_tail(&msg->msg_list, &tq->tq_delayed);
927 return LNET_CREDIT_WAIT;
931 /* unset the tx_delay flag as we're going to send it now */
932 msg->msg_tx_delayed = 0;
935 lnet_net_unlock(cpt);
936 lnet_ni_send(ni, msg);
939 return LNET_CREDIT_OK;
943 static struct lnet_rtrbufpool *
944 lnet_msg2bufpool(struct lnet_msg *msg)
946 struct lnet_rtrbufpool *rbp;
949 LASSERT(msg->msg_rx_committed);
951 cpt = msg->msg_rx_cpt;
952 rbp = &the_lnet.ln_rtrpools[cpt][0];
954 LASSERT(msg->msg_len <= LNET_MTU);
955 while (msg->msg_len > (unsigned int)rbp->rbp_npages * PAGE_SIZE) {
957 LASSERT(rbp < &the_lnet.ln_rtrpools[cpt][LNET_NRBPOOLS]);
964 lnet_post_routed_recv_locked(struct lnet_msg *msg, int do_recv)
966 /* lnet_parse is going to lnet_net_unlock immediately after this, so it
967 * sets do_recv FALSE and I don't do the unlock/send/lock bit.
968 * I return LNET_CREDIT_WAIT if msg blocked and LNET_CREDIT_OK if
969 * received or OK to receive */
970 struct lnet_peer_ni *lpni = msg->msg_rxpeer;
971 struct lnet_peer *lp;
972 struct lnet_rtrbufpool *rbp;
973 struct lnet_rtrbuf *rb;
975 LASSERT(msg->msg_kiov == NULL);
976 LASSERT(msg->msg_niov == 0);
977 LASSERT(msg->msg_routing);
978 LASSERT(msg->msg_receiving);
979 LASSERT(!msg->msg_sending);
980 LASSERT(lpni->lpni_peer_net);
981 LASSERT(lpni->lpni_peer_net->lpn_peer);
983 lp = lpni->lpni_peer_net->lpn_peer;
985 /* non-lnet_parse callers only receive delayed messages */
986 LASSERT(!do_recv || msg->msg_rx_delayed);
988 if (!msg->msg_peerrtrcredit) {
989 /* lpni_lock protects the credit manipulation */
990 spin_lock(&lpni->lpni_lock);
992 msg->msg_peerrtrcredit = 1;
993 lpni->lpni_rtrcredits--;
994 if (lpni->lpni_rtrcredits < lpni->lpni_minrtrcredits)
995 lpni->lpni_minrtrcredits = lpni->lpni_rtrcredits;
997 if (lpni->lpni_rtrcredits < 0) {
998 spin_unlock(&lpni->lpni_lock);
999 /* must have checked eager_recv before here */
1000 LASSERT(msg->msg_rx_ready_delay);
1001 msg->msg_rx_delayed = 1;
1002 /* lp_lock protects the lp_rtrq */
1003 spin_lock(&lp->lp_lock);
1004 list_add_tail(&msg->msg_list, &lp->lp_rtrq);
1005 spin_unlock(&lp->lp_lock);
1006 return LNET_CREDIT_WAIT;
1008 spin_unlock(&lpni->lpni_lock);
1011 rbp = lnet_msg2bufpool(msg);
1013 if (!msg->msg_rtrcredit) {
1014 msg->msg_rtrcredit = 1;
1016 if (rbp->rbp_credits < rbp->rbp_mincredits)
1017 rbp->rbp_mincredits = rbp->rbp_credits;
1019 if (rbp->rbp_credits < 0) {
1020 /* must have checked eager_recv before here */
1021 LASSERT(msg->msg_rx_ready_delay);
1022 msg->msg_rx_delayed = 1;
1023 list_add_tail(&msg->msg_list, &rbp->rbp_msgs);
1024 return LNET_CREDIT_WAIT;
1028 LASSERT(!list_empty(&rbp->rbp_bufs));
1029 rb = list_entry(rbp->rbp_bufs.next, struct lnet_rtrbuf, rb_list);
1030 list_del(&rb->rb_list);
1032 msg->msg_niov = rbp->rbp_npages;
1033 msg->msg_kiov = &rb->rb_kiov[0];
1035 /* unset the msg-rx_delayed flag since we're receiving the message */
1036 msg->msg_rx_delayed = 0;
1039 int cpt = msg->msg_rx_cpt;
1041 lnet_net_unlock(cpt);
1042 lnet_ni_recv(msg->msg_rxni, msg->msg_private, msg, 1,
1043 0, msg->msg_len, msg->msg_len);
1046 return LNET_CREDIT_OK;
1050 lnet_return_tx_credits_locked(struct lnet_msg *msg)
1052 struct lnet_peer_ni *txpeer = msg->msg_txpeer;
1053 struct lnet_ni *txni = msg->msg_txni;
1054 struct lnet_msg *msg2;
1056 if (msg->msg_txcredit) {
1057 struct lnet_ni *ni = msg->msg_txni;
1058 struct lnet_tx_queue *tq = ni->ni_tx_queues[msg->msg_tx_cpt];
1060 /* give back NI txcredits */
1061 msg->msg_txcredit = 0;
1063 LASSERT((tq->tq_credits < 0) ==
1064 !list_empty(&tq->tq_delayed));
1067 atomic_inc(&ni->ni_tx_credits);
1068 if (tq->tq_credits <= 0) {
1069 msg2 = list_entry(tq->tq_delayed.next,
1070 struct lnet_msg, msg_list);
1071 list_del(&msg2->msg_list);
1073 LASSERT(msg2->msg_txni == ni);
1074 LASSERT(msg2->msg_tx_delayed);
1075 LASSERT(msg2->msg_tx_cpt == msg->msg_tx_cpt);
1077 (void) lnet_post_send_locked(msg2, 1);
1081 if (msg->msg_peertxcredit) {
1082 /* give back peer txcredits */
1083 msg->msg_peertxcredit = 0;
1085 spin_lock(&txpeer->lpni_lock);
1086 LASSERT((txpeer->lpni_txcredits < 0) ==
1087 !list_empty(&txpeer->lpni_txq));
1089 txpeer->lpni_txqnob -= msg->msg_len + sizeof(struct lnet_hdr);
1090 LASSERT(txpeer->lpni_txqnob >= 0);
1092 txpeer->lpni_txcredits++;
1093 if (txpeer->lpni_txcredits <= 0) {
1096 msg2 = list_entry(txpeer->lpni_txq.next,
1097 struct lnet_msg, msg_list);
1098 list_del(&msg2->msg_list);
1099 spin_unlock(&txpeer->lpni_lock);
1101 LASSERT(msg2->msg_txpeer == txpeer);
1102 LASSERT(msg2->msg_tx_delayed);
1104 msg2_cpt = msg2->msg_tx_cpt;
1107 * The msg_cpt can be different from the msg2_cpt
1108 * so we need to make sure we lock the correct cpt
1110 * Once we call lnet_post_send_locked() it is no
1111 * longer safe to access msg2, since it could've
1112 * been freed by lnet_finalize(), but we still
1113 * need to relock the correct cpt, so we cache the
1114 * msg2_cpt for the purpose of the check that
1115 * follows the call to lnet_pose_send_locked().
1117 if (msg2_cpt != msg->msg_tx_cpt) {
1118 lnet_net_unlock(msg->msg_tx_cpt);
1119 lnet_net_lock(msg2_cpt);
1121 (void) lnet_post_send_locked(msg2, 1);
1122 if (msg2_cpt != msg->msg_tx_cpt) {
1123 lnet_net_unlock(msg2_cpt);
1124 lnet_net_lock(msg->msg_tx_cpt);
1127 spin_unlock(&txpeer->lpni_lock);
1132 msg->msg_txni = NULL;
1133 lnet_ni_decref_locked(txni, msg->msg_tx_cpt);
1136 if (txpeer != NULL) {
1137 msg->msg_txpeer = NULL;
1138 lnet_peer_ni_decref_locked(txpeer);
1143 lnet_schedule_blocked_locked(struct lnet_rtrbufpool *rbp)
1145 struct lnet_msg *msg;
1147 if (list_empty(&rbp->rbp_msgs))
1149 msg = list_entry(rbp->rbp_msgs.next,
1150 struct lnet_msg, msg_list);
1151 list_del(&msg->msg_list);
1153 (void)lnet_post_routed_recv_locked(msg, 1);
1157 lnet_drop_routed_msgs_locked(struct list_head *list, int cpt)
1159 struct lnet_msg *msg;
1160 struct lnet_msg *tmp;
1162 lnet_net_unlock(cpt);
1164 list_for_each_entry_safe(msg, tmp, list, msg_list) {
1165 lnet_ni_recv(msg->msg_rxni, msg->msg_private, NULL,
1166 0, 0, 0, msg->msg_hdr.payload_length);
1167 list_del_init(&msg->msg_list);
1168 msg->msg_no_resend = true;
1169 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_ERROR;
1170 lnet_finalize(msg, -ECANCELED);
1177 lnet_return_rx_credits_locked(struct lnet_msg *msg)
1179 struct lnet_peer_ni *rxpeerni = msg->msg_rxpeer;
1180 struct lnet_peer *lp;
1181 struct lnet_ni *rxni = msg->msg_rxni;
1182 struct lnet_msg *msg2;
1184 if (msg->msg_rtrcredit) {
1185 /* give back global router credits */
1186 struct lnet_rtrbuf *rb;
1187 struct lnet_rtrbufpool *rbp;
1189 /* NB If a msg ever blocks for a buffer in rbp_msgs, it stays
1190 * there until it gets one allocated, or aborts the wait
1192 LASSERT(msg->msg_kiov != NULL);
1194 rb = list_entry(msg->msg_kiov, struct lnet_rtrbuf, rb_kiov[0]);
1197 msg->msg_kiov = NULL;
1198 msg->msg_rtrcredit = 0;
1200 LASSERT(rbp == lnet_msg2bufpool(msg));
1202 LASSERT((rbp->rbp_credits > 0) ==
1203 !list_empty(&rbp->rbp_bufs));
1205 /* If routing is now turned off, we just drop this buffer and
1206 * don't bother trying to return credits. */
1207 if (!the_lnet.ln_routing) {
1208 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1212 /* It is possible that a user has lowered the desired number of
1213 * buffers in this pool. Make sure we never put back
1214 * more buffers than the stated number. */
1215 if (unlikely(rbp->rbp_credits >= rbp->rbp_req_nbuffers)) {
1216 /* Discard this buffer so we don't have too
1218 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1219 rbp->rbp_nbuffers--;
1221 list_add(&rb->rb_list, &rbp->rbp_bufs);
1223 if (rbp->rbp_credits <= 0)
1224 lnet_schedule_blocked_locked(rbp);
1229 if (msg->msg_peerrtrcredit) {
1231 LASSERT(rxpeerni->lpni_peer_net);
1232 LASSERT(rxpeerni->lpni_peer_net->lpn_peer);
1234 /* give back peer router credits */
1235 msg->msg_peerrtrcredit = 0;
1237 spin_lock(&rxpeerni->lpni_lock);
1238 rxpeerni->lpni_rtrcredits++;
1239 spin_unlock(&rxpeerni->lpni_lock);
1241 lp = rxpeerni->lpni_peer_net->lpn_peer;
1242 spin_lock(&lp->lp_lock);
1244 /* drop all messages which are queued to be routed on that
1246 if (!the_lnet.ln_routing) {
1248 list_splice_init(&lp->lp_rtrq, &drop);
1249 spin_unlock(&lp->lp_lock);
1250 lnet_drop_routed_msgs_locked(&drop, msg->msg_rx_cpt);
1251 } else if (!list_empty(&lp->lp_rtrq)) {
1254 msg2 = list_entry(lp->lp_rtrq.next,
1255 struct lnet_msg, msg_list);
1256 list_del(&msg2->msg_list);
1257 msg2_cpt = msg2->msg_rx_cpt;
1258 spin_unlock(&lp->lp_lock);
1260 * messages on the lp_rtrq can be from any NID in
1261 * the peer, which means they might have different
1262 * cpts. We need to make sure we lock the right
1265 if (msg2_cpt != msg->msg_rx_cpt) {
1266 lnet_net_unlock(msg->msg_rx_cpt);
1267 lnet_net_lock(msg2_cpt);
1269 (void) lnet_post_routed_recv_locked(msg2, 1);
1270 if (msg2_cpt != msg->msg_rx_cpt) {
1271 lnet_net_unlock(msg2_cpt);
1272 lnet_net_lock(msg->msg_rx_cpt);
1275 spin_unlock(&lp->lp_lock);
1279 msg->msg_rxni = NULL;
1280 lnet_ni_decref_locked(rxni, msg->msg_rx_cpt);
1282 if (rxpeerni != NULL) {
1283 msg->msg_rxpeer = NULL;
1284 lnet_peer_ni_decref_locked(rxpeerni);
1288 static struct lnet_peer_ni *
1289 lnet_select_peer_ni(struct lnet_ni *best_ni, lnet_nid_t dst_nid,
1290 struct lnet_peer *peer,
1291 struct lnet_peer_ni *best_lpni,
1292 struct lnet_peer_net *peer_net)
1295 * Look at the peer NIs for the destination peer that connect
1296 * to the chosen net. If a peer_ni is preferred when using the
1297 * best_ni to communicate, we use that one. If there is no
1298 * preferred peer_ni, or there are multiple preferred peer_ni,
1299 * the available transmit credits are used. If the transmit
1300 * credits are equal, we round-robin over the peer_ni.
1302 struct lnet_peer_ni *lpni = NULL;
1303 int best_lpni_credits = (best_lpni) ? best_lpni->lpni_txcredits :
1305 int best_lpni_healthv = (best_lpni) ?
1306 atomic_read(&best_lpni->lpni_healthv) : 0;
1307 bool best_lpni_is_preferred = false;
1308 bool lpni_is_preferred;
1310 __u32 lpni_sel_prio;
1311 __u32 best_sel_prio = LNET_MAX_SELECTION_PRIORITY;
1313 while ((lpni = lnet_get_next_peer_ni_locked(peer, peer_net, lpni))) {
1315 * if the best_ni we've chosen aleady has this lpni
1316 * preferred, then let's use it
1319 lpni_is_preferred = lnet_peer_is_pref_nid_locked(lpni,
1321 CDEBUG(D_NET, "%s lpni_is_preferred = %d\n",
1322 libcfs_nid2str(best_ni->ni_nid),
1325 lpni_is_preferred = false;
1328 lpni_healthv = atomic_read(&lpni->lpni_healthv);
1329 lpni_sel_prio = lpni->lpni_sel_priority;
1332 CDEBUG(D_NET, "n:[%s, %s] h:[%d, %d] p:[%d, %d] c:[%d, %d] s:[%d, %d]\n",
1333 libcfs_nid2str(lpni->lpni_nid),
1334 libcfs_nid2str(best_lpni->lpni_nid),
1335 lpni_healthv, best_lpni_healthv,
1336 lpni_sel_prio, best_sel_prio,
1337 lpni->lpni_txcredits, best_lpni_credits,
1338 lpni->lpni_seq, best_lpni->lpni_seq);
1342 /* pick the healthiest peer ni */
1343 if (lpni_healthv < best_lpni_healthv)
1345 else if (lpni_healthv > best_lpni_healthv) {
1346 if (best_lpni_is_preferred)
1347 best_lpni_is_preferred = false;
1351 if (lpni_sel_prio > best_sel_prio)
1353 else if (lpni_sel_prio < best_sel_prio) {
1354 if (best_lpni_is_preferred)
1355 best_lpni_is_preferred = false;
1359 /* if this is a preferred peer use it */
1360 if (!best_lpni_is_preferred && lpni_is_preferred) {
1361 best_lpni_is_preferred = true;
1363 } else if (best_lpni_is_preferred && !lpni_is_preferred) {
1364 /* this is not the preferred peer so let's ignore
1370 if (lpni->lpni_txcredits < best_lpni_credits)
1371 /* We already have a peer that has more credits
1372 * available than this one. No need to consider
1373 * this peer further.
1376 else if (lpni->lpni_txcredits > best_lpni_credits)
1379 /* The best peer found so far and the current peer
1380 * have the same number of available credits let's
1381 * make sure to select between them using Round Robin
1383 if (best_lpni && (best_lpni->lpni_seq <= lpni->lpni_seq))
1386 best_lpni_is_preferred = lpni_is_preferred;
1387 best_lpni_healthv = lpni_healthv;
1388 best_sel_prio = lpni_sel_prio;
1390 best_lpni_credits = lpni->lpni_txcredits;
1393 /* if we still can't find a peer ni then we can't reach it */
1395 __u32 net_id = (peer_net) ? peer_net->lpn_net_id :
1396 LNET_NIDNET(dst_nid);
1397 CDEBUG(D_NET, "no peer_ni found on peer net %s\n",
1398 libcfs_net2str(net_id));
1402 CDEBUG(D_NET, "sd_best_lpni = %s\n",
1403 libcfs_nid2str(best_lpni->lpni_nid));
1409 * Prerequisite: the best_ni should already be set in the sd
1410 * Find the best lpni.
1411 * If the net id is provided then restrict lpni selection on
1412 * that particular net.
1413 * Otherwise find any reachable lpni. When dealing with an MR
1414 * gateway and it has multiple lpnis which we can use
1415 * we want to select the best one from the list of reachable
1418 static inline struct lnet_peer_ni *
1419 lnet_find_best_lpni(struct lnet_ni *lni, lnet_nid_t dst_nid,
1420 struct lnet_peer *peer, __u32 net_id)
1422 struct lnet_peer_net *peer_net;
1424 /* find the best_lpni on any local network */
1425 if (net_id == LNET_NET_ANY) {
1426 struct lnet_peer_ni *best_lpni = NULL;
1427 struct lnet_peer_net *lpn;
1428 list_for_each_entry(lpn, &peer->lp_peer_nets, lpn_peer_nets) {
1429 /* no net specified find any reachable peer ni */
1430 if (!lnet_islocalnet_locked(lpn->lpn_net_id))
1432 best_lpni = lnet_select_peer_ni(lni, dst_nid, peer,
1438 /* restrict on the specified net */
1439 peer_net = lnet_peer_get_net_locked(peer, net_id);
1441 return lnet_select_peer_ni(lni, dst_nid, peer, NULL, peer_net);
1447 lnet_compare_gw_lpnis(struct lnet_peer_ni *lpni1, struct lnet_peer_ni *lpni2)
1449 if (lpni1->lpni_txqnob < lpni2->lpni_txqnob)
1452 if (lpni1->lpni_txqnob > lpni2->lpni_txqnob)
1455 if (lpni1->lpni_txcredits > lpni2->lpni_txcredits)
1458 if (lpni1->lpni_txcredits < lpni2->lpni_txcredits)
1464 /* Compare route priorities and hop counts */
1466 lnet_compare_routes(struct lnet_route *r1, struct lnet_route *r2)
1468 int r1_hops = (r1->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r1->lr_hops;
1469 int r2_hops = (r2->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r2->lr_hops;
1471 if (r1->lr_priority < r2->lr_priority)
1474 if (r1->lr_priority > r2->lr_priority)
1477 if (r1_hops < r2_hops)
1480 if (r1_hops > r2_hops)
1486 static struct lnet_route *
1487 lnet_find_route_locked(struct lnet_remotenet *rnet, __u32 src_net,
1488 struct lnet_peer_ni *remote_lpni,
1489 struct lnet_route **prev_route,
1490 struct lnet_peer_ni **gwni)
1492 struct lnet_peer_ni *lpni, *best_gw_ni = NULL;
1493 struct lnet_route *best_route;
1494 struct lnet_route *last_route;
1495 struct lnet_route *route;
1497 bool best_rte_is_preferred = false;
1500 CDEBUG(D_NET, "Looking up a route to %s, from %s\n",
1501 libcfs_net2str(rnet->lrn_net), libcfs_net2str(src_net));
1503 best_route = last_route = NULL;
1504 list_for_each_entry(route, &rnet->lrn_routes, lr_list) {
1505 if (!lnet_is_route_alive(route))
1507 gw_pnid = route->lr_gateway->lp_primary_nid;
1509 /* no protection on below fields, but it's harmless */
1510 if (last_route && (last_route->lr_seq - route->lr_seq < 0))
1513 /* if the best route found is in the preferred list then
1514 * tag it as preferred and use it later on. But if we
1515 * didn't find any routes which are on the preferred list
1516 * then just use the best route possible.
1518 rc = lnet_peer_is_pref_rtr_locked(remote_lpni, gw_pnid);
1520 if (!best_route || (rc && !best_rte_is_preferred)) {
1521 /* Restrict the selection of the router NI on the
1522 * src_net provided. If the src_net is LNET_NID_ANY,
1523 * then select the best interface available.
1525 lpni = lnet_find_best_lpni(NULL, LNET_NID_ANY,
1530 "Gateway %s does not have a peer NI on net %s\n",
1531 libcfs_nid2str(gw_pnid),
1532 libcfs_net2str(src_net));
1537 if (rc && !best_rte_is_preferred) {
1538 /* This is the first preferred route we found,
1539 * so it beats any route found previously
1545 best_rte_is_preferred = true;
1546 CDEBUG(D_NET, "preferred gw = %s\n",
1547 libcfs_nid2str(gw_pnid));
1549 } else if ((!rc) && best_rte_is_preferred)
1550 /* The best route we found so far is in the preferred
1551 * list, so it beats any non-preferred route
1556 best_route = last_route = route;
1561 rc = lnet_compare_routes(route, best_route);
1565 /* Restrict the selection of the router NI on the
1566 * src_net provided. If the src_net is LNET_NID_ANY,
1567 * then select the best interface available.
1569 lpni = lnet_find_best_lpni(NULL, LNET_NID_ANY,
1574 "Gateway %s does not have a peer NI on net %s\n",
1575 libcfs_nid2str(gw_pnid),
1576 libcfs_net2str(src_net));
1586 rc = lnet_compare_gw_lpnis(lpni, best_gw_ni);
1590 if (rc == 1 || route->lr_seq <= best_route->lr_seq) {
1597 *prev_route = last_route;
1603 static struct lnet_ni *
1604 lnet_get_best_ni(struct lnet_net *local_net, struct lnet_ni *best_ni,
1605 struct lnet_peer *peer, struct lnet_peer_net *peer_net,
1608 struct lnet_ni *ni = NULL;
1609 unsigned int shortest_distance;
1612 __u32 best_sel_prio;
1615 * If there is no peer_ni that we can send to on this network,
1616 * then there is no point in looking for a new best_ni here.
1618 if (!lnet_get_next_peer_ni_locked(peer, peer_net, NULL))
1621 if (best_ni == NULL) {
1622 best_sel_prio = LNET_MAX_SELECTION_PRIORITY;
1623 shortest_distance = UINT_MAX;
1624 best_credits = INT_MIN;
1627 shortest_distance = cfs_cpt_distance(lnet_cpt_table(), md_cpt,
1628 best_ni->ni_dev_cpt);
1629 best_credits = atomic_read(&best_ni->ni_tx_credits);
1630 best_healthv = atomic_read(&best_ni->ni_healthv);
1631 best_sel_prio = best_ni->ni_sel_priority;
1634 while ((ni = lnet_get_next_ni_locked(local_net, ni))) {
1635 unsigned int distance;
1641 ni_credits = atomic_read(&ni->ni_tx_credits);
1642 ni_healthv = atomic_read(&ni->ni_healthv);
1643 ni_fatal = atomic_read(&ni->ni_fatal_error_on);
1644 ni_sel_prio = ni->ni_sel_priority;
1647 * calculate the distance from the CPT on which
1648 * the message memory is allocated to the CPT of
1649 * the NI's physical device
1651 distance = cfs_cpt_distance(lnet_cpt_table(),
1656 * All distances smaller than the NUMA range
1657 * are treated equally.
1659 if (distance < lnet_numa_range)
1660 distance = lnet_numa_range;
1663 * Select on health, shorter distance, available
1664 * credits, then round-robin.
1670 CDEBUG(D_NET, "compare ni %s [c:%d, d:%d, s:%d, p:%u] with best_ni %s [c:%d, d:%d, s:%d, p:%u]\n",
1671 libcfs_nid2str(ni->ni_nid), ni_credits, distance,
1672 ni->ni_seq, ni_sel_prio,
1673 (best_ni) ? libcfs_nid2str(best_ni->ni_nid)
1674 : "not selected", best_credits, shortest_distance,
1675 (best_ni) ? best_ni->ni_seq : 0,
1680 if (ni_healthv < best_healthv)
1682 else if (ni_healthv > best_healthv)
1685 if (ni_sel_prio > best_sel_prio)
1687 else if (ni_sel_prio < best_sel_prio)
1690 if (distance > shortest_distance)
1692 else if (distance < shortest_distance)
1695 if (ni_credits < best_credits)
1697 else if (ni_credits > best_credits)
1700 if (best_ni && best_ni->ni_seq <= ni->ni_seq)
1704 best_sel_prio = ni_sel_prio;
1705 shortest_distance = distance;
1706 best_healthv = ni_healthv;
1708 best_credits = ni_credits;
1711 CDEBUG(D_NET, "selected best_ni %s\n",
1712 (best_ni) ? libcfs_nid2str(best_ni->ni_nid) : "no selection");
1718 * Traffic to the LNET_RESERVED_PORTAL may not trigger peer discovery,
1719 * because such traffic is required to perform discovery. We therefore
1720 * exclude all GET and PUT on that portal. We also exclude all ACK and
1721 * REPLY traffic, but that is because the portal is not tracked in the
1722 * message structure for these message types. We could restrict this
1723 * further by also checking for LNET_PROTO_PING_MATCHBITS.
1726 lnet_msg_discovery(struct lnet_msg *msg)
1728 if (msg->msg_type == LNET_MSG_PUT) {
1729 if (msg->msg_hdr.msg.put.ptl_index != LNET_RESERVED_PORTAL)
1731 } else if (msg->msg_type == LNET_MSG_GET) {
1732 if (msg->msg_hdr.msg.get.ptl_index != LNET_RESERVED_PORTAL)
1738 #define SRC_SPEC 0x0001
1739 #define SRC_ANY 0x0002
1740 #define LOCAL_DST 0x0004
1741 #define REMOTE_DST 0x0008
1742 #define MR_DST 0x0010
1743 #define NMR_DST 0x0020
1744 #define SND_RESP 0x0040
1746 /* The following to defines are used for return codes */
1747 #define REPEAT_SEND 0x1000
1748 #define PASS_THROUGH 0x2000
1750 /* The different cases lnet_select pathway needs to handle */
1751 #define SRC_SPEC_LOCAL_MR_DST (SRC_SPEC | LOCAL_DST | MR_DST)
1752 #define SRC_SPEC_ROUTER_MR_DST (SRC_SPEC | REMOTE_DST | MR_DST)
1753 #define SRC_SPEC_LOCAL_NMR_DST (SRC_SPEC | LOCAL_DST | NMR_DST)
1754 #define SRC_SPEC_ROUTER_NMR_DST (SRC_SPEC | REMOTE_DST | NMR_DST)
1755 #define SRC_ANY_LOCAL_MR_DST (SRC_ANY | LOCAL_DST | MR_DST)
1756 #define SRC_ANY_ROUTER_MR_DST (SRC_ANY | REMOTE_DST | MR_DST)
1757 #define SRC_ANY_LOCAL_NMR_DST (SRC_ANY | LOCAL_DST | NMR_DST)
1758 #define SRC_ANY_ROUTER_NMR_DST (SRC_ANY | REMOTE_DST | NMR_DST)
1761 lnet_handle_lo_send(struct lnet_send_data *sd)
1763 struct lnet_msg *msg = sd->sd_msg;
1764 int cpt = sd->sd_cpt;
1766 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1769 /* No send credit hassles with LOLND */
1770 lnet_ni_addref_locked(the_lnet.ln_loni, cpt);
1771 msg->msg_hdr.dest_nid = cpu_to_le64(the_lnet.ln_loni->ni_nid);
1772 if (!msg->msg_routing)
1773 msg->msg_hdr.src_nid =
1774 cpu_to_le64(the_lnet.ln_loni->ni_nid);
1775 msg->msg_target.nid = the_lnet.ln_loni->ni_nid;
1776 lnet_msg_commit(msg, cpt);
1777 msg->msg_txni = the_lnet.ln_loni;
1779 return LNET_CREDIT_OK;
1783 lnet_handle_send(struct lnet_send_data *sd)
1785 struct lnet_ni *best_ni = sd->sd_best_ni;
1786 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
1787 struct lnet_peer_ni *final_dst_lpni = sd->sd_final_dst_lpni;
1788 struct lnet_msg *msg = sd->sd_msg;
1790 __u32 send_case = sd->sd_send_case;
1792 __u32 routing = send_case & REMOTE_DST;
1793 struct lnet_rsp_tracker *rspt;
1795 /* Increment sequence number of the selected peer, peer net,
1796 * local ni and local net so that we pick the next ones
1799 best_lpni->lpni_seq++;
1800 best_lpni->lpni_peer_net->lpn_seq++;
1802 best_ni->ni_net->net_seq++;
1804 CDEBUG(D_NET, "%s NI seq info: [%d:%d:%d:%u] %s LPNI seq info [%d:%d:%d:%u]\n",
1805 libcfs_nid2str(best_ni->ni_nid),
1806 best_ni->ni_seq, best_ni->ni_net->net_seq,
1807 atomic_read(&best_ni->ni_tx_credits),
1808 best_ni->ni_sel_priority,
1809 libcfs_nid2str(best_lpni->lpni_nid),
1810 best_lpni->lpni_seq, best_lpni->lpni_peer_net->lpn_seq,
1811 best_lpni->lpni_txcredits,
1812 best_lpni->lpni_sel_priority);
1815 * grab a reference on the peer_ni so it sticks around even if
1816 * we need to drop and relock the lnet_net_lock below.
1818 lnet_peer_ni_addref_locked(best_lpni);
1821 * Use lnet_cpt_of_nid() to determine the CPT used to commit the
1822 * message. This ensures that we get a CPT that is correct for
1823 * the NI when the NI has been restricted to a subset of all CPTs.
1824 * If the selected CPT differs from the one currently locked, we
1825 * must unlock and relock the lnet_net_lock(), and then check whether
1826 * the configuration has changed. We don't have a hold on the best_ni
1827 * yet, and it may have vanished.
1829 cpt2 = lnet_cpt_of_nid_locked(best_lpni->lpni_nid, best_ni);
1830 if (sd->sd_cpt != cpt2) {
1831 __u32 seq = lnet_get_dlc_seq_locked();
1832 lnet_net_unlock(sd->sd_cpt);
1834 lnet_net_lock(sd->sd_cpt);
1835 if (seq != lnet_get_dlc_seq_locked()) {
1836 lnet_peer_ni_decref_locked(best_lpni);
1842 * store the best_lpni in the message right away to avoid having
1843 * to do the same operation under different conditions
1845 msg->msg_txpeer = best_lpni;
1846 msg->msg_txni = best_ni;
1849 * grab a reference for the best_ni since now it's in use in this
1850 * send. The reference will be dropped in lnet_finalize()
1852 lnet_ni_addref_locked(msg->msg_txni, sd->sd_cpt);
1855 * Always set the target.nid to the best peer picked. Either the
1856 * NID will be one of the peer NIDs selected, or the same NID as
1857 * what was originally set in the target or it will be the NID of
1858 * a router if this message should be routed
1860 msg->msg_target.nid = msg->msg_txpeer->lpni_nid;
1863 * lnet_msg_commit assigns the correct cpt to the message, which
1864 * is used to decrement the correct refcount on the ni when it's
1865 * time to return the credits
1867 lnet_msg_commit(msg, sd->sd_cpt);
1870 * If we are routing the message then we keep the src_nid that was
1871 * set by the originator. If we are not routing then we are the
1872 * originator and set it here.
1874 if (!msg->msg_routing)
1875 msg->msg_hdr.src_nid = cpu_to_le64(msg->msg_txni->ni_nid);
1878 msg->msg_target_is_router = 1;
1879 msg->msg_target.pid = LNET_PID_LUSTRE;
1881 * since we're routing we want to ensure that the
1882 * msg_hdr.dest_nid is set to the final destination. When
1883 * the router receives this message it knows how to route
1886 * final_dst_lpni is set at the beginning of the
1887 * lnet_select_pathway() function and is never changed.
1888 * It's safe to use it here.
1890 msg->msg_hdr.dest_nid = cpu_to_le64(final_dst_lpni->lpni_nid);
1893 * if we're not routing set the dest_nid to the best peer
1894 * ni NID that we picked earlier in the algorithm.
1896 msg->msg_hdr.dest_nid = cpu_to_le64(msg->msg_txpeer->lpni_nid);
1900 * if we have response tracker block update it with the next hop
1904 rspt = msg->msg_md->md_rspt_ptr;
1906 rspt->rspt_next_hop_nid = msg->msg_txpeer->lpni_nid;
1907 CDEBUG(D_NET, "rspt_next_hop_nid = %s\n",
1908 libcfs_nid2str(rspt->rspt_next_hop_nid));
1912 rc = lnet_post_send_locked(msg, 0);
1915 CDEBUG(D_NET, "TRACE: %s(%s:%s) -> %s(%s:%s) %s : %s try# %d\n",
1916 libcfs_nid2str(msg->msg_hdr.src_nid),
1917 libcfs_nid2str(msg->msg_txni->ni_nid),
1918 libcfs_nid2str(sd->sd_src_nid),
1919 libcfs_nid2str(msg->msg_hdr.dest_nid),
1920 libcfs_nid2str(sd->sd_dst_nid),
1921 libcfs_nid2str(msg->msg_txpeer->lpni_nid),
1922 libcfs_nid2str(sd->sd_rtr_nid),
1923 lnet_msgtyp2str(msg->msg_type), msg->msg_retry_count);
1929 lnet_set_non_mr_pref_nid(struct lnet_peer_ni *lpni, struct lnet_ni *lni,
1930 struct lnet_msg *msg)
1932 if (!lnet_peer_is_multi_rail(lpni->lpni_peer_net->lpn_peer) &&
1933 !lnet_msg_is_response(msg) && lpni->lpni_pref_nnids == 0) {
1934 CDEBUG(D_NET, "Setting preferred local NID %s on NMR peer %s\n",
1935 libcfs_nid2str(lni->ni_nid),
1936 libcfs_nid2str(lpni->lpni_nid));
1937 lnet_peer_ni_set_non_mr_pref_nid(lpni, lni->ni_nid);
1946 * use the source and destination NIDs as the pathway
1949 lnet_handle_spec_local_nmr_dst(struct lnet_send_data *sd)
1951 /* the destination lpni is set before we get here. */
1954 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
1955 if (!sd->sd_best_ni) {
1956 CERROR("Can't send to %s: src %s is not a "
1957 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
1958 libcfs_nid2str(sd->sd_src_nid));
1962 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
1964 return lnet_handle_send(sd);
1972 * Don't run the selection algorithm on the peer NIs. By specifying the
1973 * local NID, we're also saying that we should always use the destination NID
1974 * provided. This handles the case where we should be using the same
1975 * destination NID for the all the messages which belong to the same RPC
1979 lnet_handle_spec_local_mr_dst(struct lnet_send_data *sd)
1981 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
1982 if (!sd->sd_best_ni) {
1983 CERROR("Can't send to %s: src %s is not a "
1984 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
1985 libcfs_nid2str(sd->sd_src_nid));
1989 if (sd->sd_best_lpni &&
1990 sd->sd_best_lpni->lpni_nid == the_lnet.ln_loni->ni_nid)
1991 return lnet_handle_lo_send(sd);
1992 else if (sd->sd_best_lpni)
1993 return lnet_handle_send(sd);
1995 CERROR("can't send to %s. no NI on %s\n",
1996 libcfs_nid2str(sd->sd_dst_nid),
1997 libcfs_net2str(sd->sd_best_ni->ni_net->net_id));
1999 return -EHOSTUNREACH;
2003 lnet_find_best_ni_on_spec_net(struct lnet_ni *cur_best_ni,
2004 struct lnet_peer *peer,
2005 struct lnet_peer_net *peer_net,
2008 struct lnet_net *local_net;
2009 struct lnet_ni *best_ni;
2011 local_net = lnet_get_net_locked(peer_net->lpn_net_id);
2016 * Iterate through the NIs in this local Net and select
2017 * the NI to send from. The selection is determined by
2018 * these 3 criterion in the following priority:
2020 * 2. NI available credits
2023 best_ni = lnet_get_best_ni(local_net, cur_best_ni,
2024 peer, peer_net, cpt);
2030 lnet_initiate_peer_discovery(struct lnet_peer_ni *lpni, struct lnet_msg *msg,
2033 struct lnet_peer *peer;
2034 struct lnet_peer_ni *new_lpni;
2037 lnet_peer_ni_addref_locked(lpni);
2039 peer = lpni->lpni_peer_net->lpn_peer;
2041 if (lnet_peer_gw_discovery(peer)) {
2042 lnet_peer_ni_decref_locked(lpni);
2046 if (!lnet_msg_discovery(msg) || lnet_peer_is_uptodate(peer)) {
2047 lnet_peer_ni_decref_locked(lpni);
2051 rc = lnet_discover_peer_locked(lpni, cpt, false);
2053 lnet_peer_ni_decref_locked(lpni);
2057 new_lpni = lnet_find_peer_ni_locked(lpni->lpni_nid);
2059 lnet_peer_ni_decref_locked(lpni);
2063 peer = new_lpni->lpni_peer_net->lpn_peer;
2064 spin_lock(&peer->lp_lock);
2065 if (lpni == new_lpni && lnet_peer_is_uptodate_locked(peer)) {
2066 /* The peer NI did not change and the peer is up to date.
2067 * Nothing more to do.
2069 spin_unlock(&peer->lp_lock);
2070 lnet_peer_ni_decref_locked(lpni);
2071 lnet_peer_ni_decref_locked(new_lpni);
2074 spin_unlock(&peer->lp_lock);
2076 /* Either the peer NI changed during discovery, or the peer isn't up
2077 * to date. In both cases we want to queue the message on the
2078 * (possibly new) peer's pending queue and queue the peer for discovery
2080 msg->msg_sending = 0;
2081 msg->msg_txpeer = NULL;
2082 lnet_net_unlock(cpt);
2083 lnet_peer_queue_message(peer, msg);
2086 lnet_peer_ni_decref_locked(lpni);
2087 lnet_peer_ni_decref_locked(new_lpni);
2089 CDEBUG(D_NET, "msg %p delayed. %s pending discovery\n",
2090 msg, libcfs_nid2str(peer->lp_primary_nid));
2092 return LNET_DC_WAIT;
2096 lnet_handle_find_routed_path(struct lnet_send_data *sd,
2098 struct lnet_peer_ni **gw_lpni,
2099 struct lnet_peer **gw_peer)
2103 struct lnet_peer *gw;
2104 struct lnet_peer *lp;
2105 struct lnet_peer_net *lpn;
2106 struct lnet_peer_net *best_lpn = NULL;
2107 struct lnet_remotenet *rnet, *best_rnet = NULL;
2108 struct lnet_route *best_route = NULL;
2109 struct lnet_route *last_route = NULL;
2110 struct lnet_peer_ni *lpni = NULL;
2111 struct lnet_peer_ni *gwni = NULL;
2112 bool route_found = false;
2113 lnet_nid_t src_nid = (sd->sd_src_nid != LNET_NID_ANY) ? sd->sd_src_nid :
2114 (sd->sd_best_ni != NULL) ? sd->sd_best_ni->ni_nid :
2116 int best_lpn_healthv = 0;
2117 __u32 best_lpn_sel_prio = LNET_MAX_SELECTION_PRIORITY;
2119 CDEBUG(D_NET, "using src nid %s for route restriction\n",
2120 libcfs_nid2str(src_nid));
2122 /* If a router nid was specified then we are replying to a GET or
2123 * sending an ACK. In this case we use the gateway associated with the
2124 * specified router nid.
2126 if (sd->sd_rtr_nid != LNET_NID_ANY) {
2127 gwni = lnet_find_peer_ni_locked(sd->sd_rtr_nid);
2129 gw = gwni->lpni_peer_net->lpn_peer;
2130 lnet_peer_ni_decref_locked(gwni);
2131 if (gw->lp_rtr_refcount) {
2132 local_lnet = LNET_NIDNET(sd->sd_rtr_nid);
2136 CWARN("No peer NI for gateway %s. Attempting to find an alternative route.\n",
2137 libcfs_nid2str(sd->sd_rtr_nid));
2142 if (sd->sd_msg->msg_routing) {
2143 /* If I'm routing this message then I need to find the
2144 * next hop based on the destination NID
2146 best_rnet = lnet_find_rnet_locked(LNET_NIDNET(sd->sd_dst_nid));
2148 CERROR("Unable to route message to %s - Route table may be misconfigured\n",
2149 libcfs_nid2str(sd->sd_dst_nid));
2150 return -EHOSTUNREACH;
2153 /* we've already looked up the initial lpni using
2156 lpni = sd->sd_best_lpni;
2157 /* the peer tree must be in existence */
2158 LASSERT(lpni && lpni->lpni_peer_net &&
2159 lpni->lpni_peer_net->lpn_peer);
2160 lp = lpni->lpni_peer_net->lpn_peer;
2162 list_for_each_entry(lpn, &lp->lp_peer_nets, lpn_peer_nets) {
2163 /* is this remote network reachable? */
2164 rnet = lnet_find_rnet_locked(lpn->lpn_net_id);
2173 /* select the preferred peer net */
2174 if (best_lpn_healthv > lpn->lpn_healthv)
2176 else if (best_lpn_healthv < lpn->lpn_healthv)
2179 if (best_lpn_sel_prio < lpn->lpn_sel_priority)
2181 else if (best_lpn_sel_prio > lpn->lpn_sel_priority)
2184 if (best_lpn->lpn_seq <= lpn->lpn_seq)
2187 best_lpn_healthv = lpn->lpn_healthv;
2188 best_lpn_sel_prio = lpn->lpn_sel_priority;
2194 CERROR("peer %s has no available nets\n",
2195 libcfs_nid2str(sd->sd_dst_nid));
2196 return -EHOSTUNREACH;
2199 sd->sd_best_lpni = lnet_find_best_lpni(sd->sd_best_ni,
2202 best_lpn->lpn_net_id);
2203 if (!sd->sd_best_lpni) {
2204 CERROR("peer %s is unreachable\n",
2205 libcfs_nid2str(sd->sd_dst_nid));
2206 return -EHOSTUNREACH;
2209 /* We're attempting to round robin over the remote peer
2210 * NI's so update the final destination we selected
2212 sd->sd_final_dst_lpni = sd->sd_best_lpni;
2214 /* Increment the sequence number of the remote lpni so
2215 * we can round robin over the different interfaces of
2218 sd->sd_best_lpni->lpni_seq++;
2222 * find the best route. Restrict the selection on the net of the
2223 * local NI if we've already picked the local NI to send from.
2224 * Otherwise, let's pick any route we can find and then find
2225 * a local NI we can reach the route's gateway on. Any route we
2226 * select will be reachable by virtue of the restriction we have
2227 * when adding a route.
2229 best_route = lnet_find_route_locked(best_rnet,
2230 LNET_NIDNET(src_nid),
2232 &last_route, &gwni);
2235 CERROR("no route to %s from %s\n",
2236 libcfs_nid2str(dst_nid),
2237 libcfs_nid2str(src_nid));
2238 return -EHOSTUNREACH;
2242 CERROR("Internal Error. Route expected to %s from %s\n",
2243 libcfs_nid2str(dst_nid),
2244 libcfs_nid2str(src_nid));
2248 gw = best_route->lr_gateway;
2249 LASSERT(gw == gwni->lpni_peer_net->lpn_peer);
2250 local_lnet = best_route->lr_lnet;
2254 * Discover this gateway if it hasn't already been discovered.
2255 * This means we might delay the message until discovery has
2258 sd->sd_msg->msg_src_nid_param = sd->sd_src_nid;
2259 rc = lnet_initiate_peer_discovery(gwni, sd->sd_msg, sd->sd_cpt);
2263 if (!sd->sd_best_ni)
2264 sd->sd_best_ni = lnet_find_best_ni_on_spec_net(NULL, gw,
2265 lnet_peer_get_net_locked(gw,
2269 if (!sd->sd_best_ni) {
2270 CERROR("Internal Error. Expected local ni on %s but non found :%s\n",
2271 libcfs_net2str(local_lnet),
2272 libcfs_nid2str(sd->sd_src_nid));
2280 * increment the sequence numbers since now we're sure we're
2281 * going to use this path
2283 if (sd->sd_rtr_nid == LNET_NID_ANY) {
2284 LASSERT(best_route && last_route);
2285 best_route->lr_seq = last_route->lr_seq + 1;
2287 best_lpn->lpn_seq++;
2298 * Remote destination
2299 * Non-MR destination
2303 * Remote destination
2306 * The handling of these two cases is similar. Even though the destination
2307 * can be MR or non-MR, we'll deal directly with the router.
2310 lnet_handle_spec_router_dst(struct lnet_send_data *sd)
2313 struct lnet_peer_ni *gw_lpni = NULL;
2314 struct lnet_peer *gw_peer = NULL;
2317 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
2318 if (!sd->sd_best_ni) {
2319 CERROR("Can't send to %s: src %s is not a "
2320 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
2321 libcfs_nid2str(sd->sd_src_nid));
2325 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2330 if (sd->sd_send_case & NMR_DST)
2332 * since the final destination is non-MR let's set its preferred
2333 * NID before we send
2335 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni,
2339 * We're going to send to the gw found so let's set its
2342 sd->sd_peer = gw_peer;
2343 sd->sd_best_lpni = gw_lpni;
2345 return lnet_handle_send(sd);
2349 lnet_find_best_ni_on_local_net(struct lnet_peer *peer, int md_cpt,
2352 struct lnet_peer_net *lpn = NULL;
2353 struct lnet_peer_net *best_lpn = NULL;
2354 struct lnet_net *net = NULL;
2355 struct lnet_net *best_net = NULL;
2356 struct lnet_ni *best_ni = NULL;
2357 int best_lpn_healthv = 0;
2358 int best_net_healthv = 0;
2360 __u32 best_lpn_sel_prio = LNET_MAX_SELECTION_PRIORITY;
2362 __u32 best_net_sel_prio = LNET_MAX_SELECTION_PRIORITY;
2367 * The peer can have multiple interfaces, some of them can be on
2368 * the local network and others on a routed network. We should
2369 * prefer the local network. However if the local network is not
2370 * available then we need to try the routed network
2373 /* go through all the peer nets and find the best_ni */
2374 list_for_each_entry(lpn, &peer->lp_peer_nets, lpn_peer_nets) {
2376 * The peer's list of nets can contain non-local nets. We
2377 * want to only examine the local ones.
2379 net = lnet_get_net_locked(lpn->lpn_net_id);
2383 lpn_sel_prio = lpn->lpn_sel_priority;
2384 net_healthv = lnet_get_net_healthv_locked(net);
2385 net_sel_prio = net->net_sel_priority;
2388 * if this is a discovery message and lp_disc_net_id is
2389 * specified then use that net to send the discovery on.
2391 if (peer->lp_disc_net_id == lpn->lpn_net_id &&
2400 /* always select the lpn with the best health */
2401 if (best_lpn_healthv > lpn->lpn_healthv)
2403 else if (best_lpn_healthv < lpn->lpn_healthv)
2406 /* select the preferred peer and local nets */
2407 if (best_lpn_sel_prio < lpn_sel_prio)
2409 else if (best_lpn_sel_prio > lpn_sel_prio)
2412 if (best_net_healthv > net_healthv)
2414 else if (best_net_healthv < net_healthv)
2417 if (best_net_sel_prio < net_sel_prio)
2419 else if (best_net_sel_prio > net_sel_prio)
2422 if (best_lpn->lpn_seq < lpn->lpn_seq)
2424 else if (best_lpn->lpn_seq > lpn->lpn_seq)
2427 /* round robin over the local networks */
2428 if (best_net->net_seq <= net->net_seq)
2432 best_net_healthv = net_healthv;
2433 best_net_sel_prio = net_sel_prio;
2434 best_lpn_healthv = lpn->lpn_healthv;
2435 best_lpn_sel_prio = lpn_sel_prio;
2444 /* Select the best NI on the same net as best_lpn chosen
2447 best_ni = lnet_find_best_ni_on_spec_net(NULL, peer,
2454 static struct lnet_ni *
2455 lnet_find_existing_preferred_best_ni(struct lnet_peer_ni *lpni, int cpt)
2457 struct lnet_ni *best_ni = NULL;
2458 struct lnet_peer_net *peer_net = lpni->lpni_peer_net;
2459 struct lnet_peer_ni *lpni_entry;
2462 * We must use a consistent source address when sending to a
2463 * non-MR peer. However, a non-MR peer can have multiple NIDs
2464 * on multiple networks, and we may even need to talk to this
2465 * peer on multiple networks -- certain types of
2466 * load-balancing configuration do this.
2468 * So we need to pick the NI the peer prefers for this
2469 * particular network.
2472 list_for_each_entry(lpni_entry, &peer_net->lpn_peer_nis,
2474 if (lpni_entry->lpni_pref_nnids == 0)
2476 LASSERT(lpni_entry->lpni_pref_nnids == 1);
2477 best_ni = lnet_nid2ni_locked(lpni_entry->lpni_pref.nid, cpt);
2484 /* Prerequisite: sd->sd_peer and sd->sd_best_lpni should be set */
2486 lnet_select_preferred_best_ni(struct lnet_send_data *sd)
2488 struct lnet_ni *best_ni = NULL;
2489 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
2492 * We must use a consistent source address when sending to a
2493 * non-MR peer. However, a non-MR peer can have multiple NIDs
2494 * on multiple networks, and we may even need to talk to this
2495 * peer on multiple networks -- certain types of
2496 * load-balancing configuration do this.
2498 * So we need to pick the NI the peer prefers for this
2499 * particular network.
2502 best_ni = lnet_find_existing_preferred_best_ni(sd->sd_best_lpni,
2505 /* if best_ni is still not set just pick one */
2508 lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2509 sd->sd_best_lpni->lpni_peer_net,
2511 /* If there is no best_ni we don't have a route */
2513 CERROR("no path to %s from net %s\n",
2514 libcfs_nid2str(best_lpni->lpni_nid),
2515 libcfs_net2str(best_lpni->lpni_net->net_id));
2516 return -EHOSTUNREACH;
2520 sd->sd_best_ni = best_ni;
2522 /* Set preferred NI if necessary. */
2523 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
2530 * Source not specified
2534 * always use the same source NID for NMR peers
2535 * If we've talked to that peer before then we already have a preferred
2536 * source NI associated with it. Otherwise, we select a preferred local NI
2537 * and store it in the peer
2540 lnet_handle_any_local_nmr_dst(struct lnet_send_data *sd)
2544 /* sd->sd_best_lpni is already set to the final destination */
2547 * At this point we should've created the peer ni and peer. If we
2548 * can't find it, then something went wrong. Instead of assert
2549 * output a relevant message and fail the send
2551 if (!sd->sd_best_lpni) {
2552 CERROR("Internal fault. Unable to send msg %s to %s. "
2554 lnet_msgtyp2str(sd->sd_msg->msg_type),
2555 libcfs_nid2str(sd->sd_dst_nid));
2559 if (sd->sd_msg->msg_routing) {
2560 /* If I'm forwarding this message then I can choose any NI
2561 * on the destination peer net
2563 sd->sd_best_ni = lnet_find_best_ni_on_spec_net(NULL,
2565 sd->sd_best_lpni->lpni_peer_net,
2567 if (!sd->sd_best_ni) {
2568 CERROR("Unable to forward message to %s. No local NI available\n",
2569 libcfs_nid2str(sd->sd_dst_nid));
2573 rc = lnet_select_preferred_best_ni(sd);
2576 rc = lnet_handle_send(sd);
2582 lnet_handle_any_mr_dsta(struct lnet_send_data *sd)
2585 * NOTE we've already handled the remote peer case. So we only
2586 * need to worry about the local case here.
2588 * if we're sending a response, ACK or reply, we need to send it
2589 * to the destination NID given to us. At this point we already
2590 * have the peer_ni we're suppose to send to, so just find the
2591 * best_ni on the peer net and use that. Since we're sending to an
2592 * MR peer then we can just run the selection algorithm on our
2593 * local NIs and pick the best one.
2595 if (sd->sd_send_case & SND_RESP) {
2597 lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2598 sd->sd_best_lpni->lpni_peer_net,
2601 if (!sd->sd_best_ni) {
2603 * We're not going to deal with not able to send
2604 * a response to the provided final destination
2606 CERROR("Can't send response to %s. "
2607 "No local NI available\n",
2608 libcfs_nid2str(sd->sd_dst_nid));
2609 return -EHOSTUNREACH;
2612 return lnet_handle_send(sd);
2616 * If we get here that means we're sending a fresh request, PUT or
2617 * GET, so we need to run our standard selection algorithm.
2618 * First find the best local interface that's on any of the peer's
2621 sd->sd_best_ni = lnet_find_best_ni_on_local_net(sd->sd_peer,
2623 lnet_msg_discovery(sd->sd_msg));
2624 if (sd->sd_best_ni) {
2626 lnet_find_best_lpni(sd->sd_best_ni, sd->sd_dst_nid,
2628 sd->sd_best_ni->ni_net->net_id);
2631 * if we're successful in selecting a peer_ni on the local
2632 * network, then send to it. Otherwise fall through and
2633 * try and see if we can reach it over another routed
2636 if (sd->sd_best_lpni &&
2637 sd->sd_best_lpni->lpni_nid == the_lnet.ln_loni->ni_nid) {
2639 * in case we initially started with a routed
2640 * destination, let's reset to local
2642 sd->sd_send_case &= ~REMOTE_DST;
2643 sd->sd_send_case |= LOCAL_DST;
2644 return lnet_handle_lo_send(sd);
2645 } else if (sd->sd_best_lpni) {
2647 * in case we initially started with a routed
2648 * destination, let's reset to local
2650 sd->sd_send_case &= ~REMOTE_DST;
2651 sd->sd_send_case |= LOCAL_DST;
2652 return lnet_handle_send(sd);
2655 CERROR("Internal Error. Expected to have a best_lpni: "
2657 libcfs_nid2str(sd->sd_src_nid),
2658 libcfs_nid2str(sd->sd_dst_nid));
2664 * Peer doesn't have a local network. Let's see if there is
2665 * a remote network we can reach it on.
2667 return PASS_THROUGH;
2672 * Source NID not specified
2677 * Source NID not speified
2678 * Remote destination
2681 * In both of these cases if we're sending a response, ACK or REPLY, then
2682 * we need to send to the destination NID provided.
2684 * In the remote case let's deal with MR routers.
2689 lnet_handle_any_mr_dst(struct lnet_send_data *sd)
2692 struct lnet_peer *gw_peer = NULL;
2693 struct lnet_peer_ni *gw_lpni = NULL;
2696 * handle sending a response to a remote peer here so we don't
2697 * have to worry about it if we hit lnet_handle_any_mr_dsta()
2699 if (sd->sd_send_case & REMOTE_DST &&
2700 sd->sd_send_case & SND_RESP) {
2701 struct lnet_peer_ni *gw;
2702 struct lnet_peer *gw_peer;
2704 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw,
2707 CERROR("Can't send response to %s. "
2708 "No route available\n",
2709 libcfs_nid2str(sd->sd_dst_nid));
2710 return -EHOSTUNREACH;
2711 } else if (rc > 0) {
2715 sd->sd_best_lpni = gw;
2716 sd->sd_peer = gw_peer;
2718 return lnet_handle_send(sd);
2722 * Even though the NID for the peer might not be on a local network,
2723 * since the peer is MR there could be other interfaces on the
2724 * local network. In that case we'd still like to prefer the local
2725 * network over the routed network. If we're unable to do that
2726 * then we select the best router among the different routed networks,
2727 * and if the router is MR then we can deal with it as such.
2729 rc = lnet_handle_any_mr_dsta(sd);
2730 if (rc != PASS_THROUGH)
2734 * Now that we must route to the destination, we must consider the
2735 * MR case, where the destination has multiple interfaces, some of
2736 * which we can route to and others we do not. For this reason we
2737 * need to select the destination which we can route to and if
2738 * there are multiple, we need to round robin.
2740 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2745 sd->sd_send_case &= ~LOCAL_DST;
2746 sd->sd_send_case |= REMOTE_DST;
2748 sd->sd_peer = gw_peer;
2749 sd->sd_best_lpni = gw_lpni;
2751 return lnet_handle_send(sd);
2755 * Source not specified
2756 * Remote destination
2759 * Must send to the specified peer NID using the same source NID that
2760 * we've used before. If it's the first time to talk to that peer then
2761 * find the source NI and assign it as preferred to that peer
2764 lnet_handle_any_router_nmr_dst(struct lnet_send_data *sd)
2767 struct lnet_peer_ni *gw_lpni = NULL;
2768 struct lnet_peer *gw_peer = NULL;
2771 * Let's see if we have a preferred NI to talk to this NMR peer
2773 sd->sd_best_ni = lnet_find_existing_preferred_best_ni(sd->sd_best_lpni,
2777 * find the router and that'll find the best NI if we didn't find
2780 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2786 * set the best_ni we've chosen as the preferred one for
2789 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
2791 /* we'll be sending to the gw */
2792 sd->sd_best_lpni = gw_lpni;
2793 sd->sd_peer = gw_peer;
2795 return lnet_handle_send(sd);
2799 lnet_handle_send_case_locked(struct lnet_send_data *sd)
2802 * turn off the SND_RESP bit.
2803 * It will be checked in the case handling
2805 __u32 send_case = sd->sd_send_case &= ~SND_RESP ;
2807 CDEBUG(D_NET, "Source %s%s to %s %s %s destination\n",
2808 (send_case & SRC_SPEC) ? "Specified: " : "ANY",
2809 (send_case & SRC_SPEC) ? libcfs_nid2str(sd->sd_src_nid) : "",
2810 (send_case & MR_DST) ? "MR: " : "NMR: ",
2811 libcfs_nid2str(sd->sd_dst_nid),
2812 (send_case & LOCAL_DST) ? "local" : "routed");
2814 switch (send_case) {
2816 * For all cases where the source is specified, we should always
2817 * use the destination NID, whether it's an MR destination or not,
2818 * since we're continuing a series of related messages for the
2821 case SRC_SPEC_LOCAL_NMR_DST:
2822 return lnet_handle_spec_local_nmr_dst(sd);
2823 case SRC_SPEC_LOCAL_MR_DST:
2824 return lnet_handle_spec_local_mr_dst(sd);
2825 case SRC_SPEC_ROUTER_NMR_DST:
2826 case SRC_SPEC_ROUTER_MR_DST:
2827 return lnet_handle_spec_router_dst(sd);
2828 case SRC_ANY_LOCAL_NMR_DST:
2829 return lnet_handle_any_local_nmr_dst(sd);
2830 case SRC_ANY_LOCAL_MR_DST:
2831 case SRC_ANY_ROUTER_MR_DST:
2832 return lnet_handle_any_mr_dst(sd);
2833 case SRC_ANY_ROUTER_NMR_DST:
2834 return lnet_handle_any_router_nmr_dst(sd);
2836 CERROR("Unknown send case\n");
2842 lnet_select_pathway(lnet_nid_t src_nid, lnet_nid_t dst_nid,
2843 struct lnet_msg *msg, lnet_nid_t rtr_nid)
2845 struct lnet_peer_ni *lpni;
2846 struct lnet_peer *peer;
2847 struct lnet_send_data send_data;
2850 __u32 send_case = 0;
2852 bool mr_forwarding_allowed;
2854 memset(&send_data, 0, sizeof(send_data));
2857 * get an initial CPT to use for locking. The idea here is not to
2858 * serialize the calls to select_pathway, so that as many
2859 * operations can run concurrently as possible. To do that we use
2860 * the CPT where this call is being executed. Later on when we
2861 * determine the CPT to use in lnet_message_commit, we switch the
2862 * lock and check if there was any configuration change. If none,
2863 * then we proceed, if there is, then we restart the operation.
2865 cpt = lnet_net_lock_current();
2867 md_cpt = lnet_cpt_of_md(msg->msg_md, msg->msg_offset);
2868 if (md_cpt == CFS_CPT_ANY)
2874 * If we're being asked to send to the loopback interface, there
2875 * is no need to go through any selection. We can just shortcut
2876 * the entire process and send over lolnd
2878 send_data.sd_msg = msg;
2879 send_data.sd_cpt = cpt;
2880 if (dst_nid == LNET_NID_LO_0) {
2881 rc = lnet_handle_lo_send(&send_data);
2882 lnet_net_unlock(cpt);
2887 * find an existing peer_ni, or create one and mark it as having been
2888 * created due to network traffic. This call will create the
2889 * peer->peer_net->peer_ni tree.
2891 lpni = lnet_nid2peerni_locked(dst_nid, LNET_NID_ANY, cpt);
2893 lnet_net_unlock(cpt);
2894 return PTR_ERR(lpni);
2898 * Cache the original src_nid and rtr_nid. If we need to resend the
2899 * message then we'll need to know whether the src_nid was originally
2900 * specified for this message. If it was originally specified,
2901 * then we need to keep using the same src_nid since it's
2902 * continuing the same sequence of messages. Similarly, rtr_nid will
2903 * affect our choice of next hop.
2905 msg->msg_src_nid_param = src_nid;
2906 msg->msg_rtr_nid_param = rtr_nid;
2909 * If necessary, perform discovery on the peer that owns this peer_ni.
2910 * Note, this can result in the ownership of this peer_ni changing
2911 * to another peer object.
2913 rc = lnet_initiate_peer_discovery(lpni, msg, cpt);
2915 lnet_peer_ni_decref_locked(lpni);
2916 lnet_net_unlock(cpt);
2919 lnet_peer_ni_decref_locked(lpni);
2921 peer = lpni->lpni_peer_net->lpn_peer;
2924 * Identify the different send cases
2926 if (src_nid == LNET_NID_ANY)
2927 send_case |= SRC_ANY;
2929 send_case |= SRC_SPEC;
2931 if (lnet_get_net_locked(LNET_NIDNET(dst_nid)))
2932 send_case |= LOCAL_DST;
2934 send_case |= REMOTE_DST;
2937 if (msg->msg_routing && (send_case & LOCAL_DST))
2940 /* Determine whether to allow MR forwarding for this message.
2941 * NB: MR forwarding is allowed if the message originator and the
2942 * destination are both MR capable, and the destination lpni that was
2943 * originally chosen by the originator is unhealthy or down.
2944 * We check the MR capability of the destination further below
2946 mr_forwarding_allowed = false;
2948 struct lnet_peer *src_lp;
2949 struct lnet_peer_ni *src_lpni;
2951 src_lpni = lnet_nid2peerni_locked(msg->msg_hdr.src_nid,
2953 /* We don't fail the send if we hit any errors here. We'll just
2954 * try to send it via non-multi-rail criteria
2956 if (!IS_ERR(src_lpni)) {
2957 /* Drop ref taken by lnet_nid2peerni_locked() */
2958 lnet_peer_ni_decref_locked(src_lpni);
2959 src_lp = lpni->lpni_peer_net->lpn_peer;
2960 if (lnet_peer_is_multi_rail(src_lp) &&
2961 !lnet_is_peer_ni_alive(lpni))
2962 mr_forwarding_allowed = true;
2965 CDEBUG(D_NET, "msg %p MR forwarding %s\n", msg,
2966 mr_forwarding_allowed ? "allowed" : "not allowed");
2970 * Deal with the peer as NMR in the following cases:
2971 * 1. the peer is NMR
2972 * 2. We're trying to recover a specific peer NI
2973 * 3. I'm a router sending to the final destination and MR forwarding is
2974 * not allowed for this message (as determined above).
2975 * In this case the source of the message would've
2976 * already selected the final destination so my job
2977 * is to honor the selection.
2979 if (!lnet_peer_is_multi_rail(peer) || msg->msg_recovery ||
2980 (final_hop && !mr_forwarding_allowed))
2981 send_case |= NMR_DST;
2983 send_case |= MR_DST;
2985 if (lnet_msg_is_response(msg))
2986 send_case |= SND_RESP;
2988 /* assign parameters to the send_data */
2989 send_data.sd_rtr_nid = rtr_nid;
2990 send_data.sd_src_nid = src_nid;
2991 send_data.sd_dst_nid = dst_nid;
2992 send_data.sd_best_lpni = lpni;
2994 * keep a pointer to the final destination in case we're going to
2995 * route, so we'll need to access it later
2997 send_data.sd_final_dst_lpni = lpni;
2998 send_data.sd_peer = peer;
2999 send_data.sd_md_cpt = md_cpt;
3000 send_data.sd_send_case = send_case;
3002 rc = lnet_handle_send_case_locked(&send_data);
3005 * Update the local cpt since send_data.sd_cpt might've been
3006 * updated as a result of calling lnet_handle_send_case_locked().
3008 cpt = send_data.sd_cpt;
3010 if (rc == REPEAT_SEND)
3013 lnet_net_unlock(cpt);
3019 lnet_send(lnet_nid_t src_nid, struct lnet_msg *msg, lnet_nid_t rtr_nid)
3021 lnet_nid_t dst_nid = msg->msg_target.nid;
3025 * NB: rtr_nid is set to LNET_NID_ANY for all current use-cases,
3026 * but we might want to use pre-determined router for ACK/REPLY
3029 /* NB: ni != NULL == interface pre-determined (ACK/REPLY) */
3030 LASSERT(msg->msg_txpeer == NULL);
3031 LASSERT(msg->msg_txni == NULL);
3032 LASSERT(!msg->msg_sending);
3033 LASSERT(!msg->msg_target_is_router);
3034 LASSERT(!msg->msg_receiving);
3036 msg->msg_sending = 1;
3038 LASSERT(!msg->msg_tx_committed);
3040 rc = lnet_select_pathway(src_nid, dst_nid, msg, rtr_nid);
3042 if (rc == -EHOSTUNREACH)
3043 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_ERROR;
3045 msg->msg_health_status = LNET_MSG_STATUS_LOCAL_ERROR;
3049 if (rc == LNET_CREDIT_OK)
3050 lnet_ni_send(msg->msg_txni, msg);
3052 /* rc == LNET_CREDIT_OK or LNET_CREDIT_WAIT or LNET_DC_WAIT */
3056 enum lnet_mt_event_type {
3057 MT_TYPE_LOCAL_NI = 0,
3061 struct lnet_mt_event_info {
3062 enum lnet_mt_event_type mt_type;
3066 /* called with res_lock held */
3068 lnet_detach_rsp_tracker(struct lnet_libmd *md, int cpt)
3070 struct lnet_rsp_tracker *rspt;
3073 * msg has a refcount on the MD so the MD is not going away.
3074 * The rspt queue for the cpt is protected by
3075 * the lnet_net_lock(cpt). cpt is the cpt of the MD cookie.
3077 if (!md->md_rspt_ptr)
3080 rspt = md->md_rspt_ptr;
3083 LASSERT(rspt->rspt_cpt == cpt);
3085 md->md_rspt_ptr = NULL;
3087 if (LNetMDHandleIsInvalid(rspt->rspt_mdh)) {
3089 * The monitor thread has invalidated this handle because the
3090 * response timed out, but it failed to lookup the MD. That
3091 * means this response tracker is on the zombie list. We can
3092 * safely remove it under the resource lock (held by caller) and
3093 * free the response tracker block.
3095 list_del(&rspt->rspt_on_list);
3096 lnet_rspt_free(rspt, cpt);
3099 * invalidate the handle to indicate that a response has been
3100 * received, which will then lead the monitor thread to clean up
3103 LNetInvalidateMDHandle(&rspt->rspt_mdh);
3108 lnet_clean_zombie_rstqs(void)
3110 struct lnet_rsp_tracker *rspt, *tmp;
3113 cfs_cpt_for_each(i, lnet_cpt_table()) {
3114 list_for_each_entry_safe(rspt, tmp,
3115 the_lnet.ln_mt_zombie_rstqs[i],
3117 list_del(&rspt->rspt_on_list);
3118 lnet_rspt_free(rspt, i);
3122 cfs_percpt_free(the_lnet.ln_mt_zombie_rstqs);
3126 lnet_finalize_expired_responses(void)
3128 struct lnet_libmd *md;
3129 struct lnet_rsp_tracker *rspt, *tmp;
3133 if (the_lnet.ln_mt_rstq == NULL)
3136 cfs_cpt_for_each(i, lnet_cpt_table()) {
3137 LIST_HEAD(local_queue);
3140 if (!the_lnet.ln_mt_rstq[i]) {
3144 list_splice_init(the_lnet.ln_mt_rstq[i], &local_queue);
3149 list_for_each_entry_safe(rspt, tmp, &local_queue, rspt_on_list) {
3151 * The rspt mdh will be invalidated when a response
3152 * is received or whenever we want to discard the
3153 * block the monitor thread will walk the queue
3154 * and clean up any rsts with an invalid mdh.
3155 * The monitor thread will walk the queue until
3156 * the first unexpired rspt block. This means that
3157 * some rspt blocks which received their
3158 * corresponding responses will linger in the
3159 * queue until they are cleaned up eventually.
3162 if (LNetMDHandleIsInvalid(rspt->rspt_mdh)) {
3164 list_del(&rspt->rspt_on_list);
3165 lnet_rspt_free(rspt, i);
3169 if (ktime_compare(now, rspt->rspt_deadline) >= 0 ||
3170 the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN) {
3171 struct lnet_peer_ni *lpni;
3174 md = lnet_handle2md(&rspt->rspt_mdh);
3176 /* MD has been queued for unlink, but
3177 * rspt hasn't been detached (Note we've
3178 * checked above that the rspt_mdh is
3179 * valid). Since we cannot lookup the MD
3180 * we're unable to detach the rspt
3181 * ourselves. Thus, move the rspt to the
3182 * zombie list where we'll wait for
3184 * 1. The remaining operations on the
3185 * MD to complete. In this case the
3186 * final operation will result in
3187 * lnet_msg_detach_md()->
3188 * lnet_detach_rsp_tracker() where
3189 * we will clean up this response
3191 * 2. LNet to shutdown. In this case
3192 * we'll wait until after all LND Nets
3193 * have shutdown and then we can
3194 * safely free any remaining response
3195 * tracker blocks on the zombie list.
3196 * Note: We need to hold the resource
3197 * lock when adding to the zombie list
3198 * because we may have concurrent access
3199 * with lnet_detach_rsp_tracker().
3201 LNetInvalidateMDHandle(&rspt->rspt_mdh);
3202 list_move(&rspt->rspt_on_list,
3203 the_lnet.ln_mt_zombie_rstqs[i]);
3207 LASSERT(md->md_rspt_ptr == rspt);
3208 md->md_rspt_ptr = NULL;
3211 LNetMDUnlink(rspt->rspt_mdh);
3213 nid = rspt->rspt_next_hop_nid;
3215 list_del(&rspt->rspt_on_list);
3216 lnet_rspt_free(rspt, i);
3218 /* If we're shutting down we just want to clean
3219 * up the rspt blocks
3221 if (the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN)
3225 the_lnet.ln_counters[i]->lct_health.lch_response_timeout_count++;
3229 "Response timeout: md = %p: nid = %s\n",
3230 md, libcfs_nid2str(nid));
3233 * If there is a timeout on the response
3234 * from the next hop decrement its health
3235 * value so that we don't use it
3238 lpni = lnet_find_peer_ni_locked(nid);
3240 lnet_handle_remote_failure_locked(lpni);
3241 lnet_peer_ni_decref_locked(lpni);
3250 if (!list_empty(&local_queue)) {
3252 list_splice(&local_queue, the_lnet.ln_mt_rstq[i]);
3259 lnet_resend_pending_msgs_locked(struct list_head *resendq, int cpt)
3261 struct lnet_msg *msg;
3263 while (!list_empty(resendq)) {
3264 struct lnet_peer_ni *lpni;
3266 msg = list_entry(resendq->next, struct lnet_msg,
3269 list_del_init(&msg->msg_list);
3271 lpni = lnet_find_peer_ni_locked(msg->msg_hdr.dest_nid);
3273 lnet_net_unlock(cpt);
3274 CERROR("Expected that a peer is already created for %s\n",
3275 libcfs_nid2str(msg->msg_hdr.dest_nid));
3276 msg->msg_no_resend = true;
3277 lnet_finalize(msg, -EFAULT);
3282 lnet_peer_ni_decref_locked(lpni);
3284 lnet_net_unlock(cpt);
3285 CDEBUG(D_NET, "resending %s->%s: %s recovery %d try# %d\n",
3286 libcfs_nid2str(msg->msg_src_nid_param),
3287 libcfs_id2str(msg->msg_target),
3288 lnet_msgtyp2str(msg->msg_type),
3290 msg->msg_retry_count);
3291 rc = lnet_send(msg->msg_src_nid_param, msg,
3292 msg->msg_rtr_nid_param);
3294 CERROR("Error sending %s to %s: %d\n",
3295 lnet_msgtyp2str(msg->msg_type),
3296 libcfs_id2str(msg->msg_target), rc);
3297 msg->msg_no_resend = true;
3298 lnet_finalize(msg, rc);
3302 the_lnet.ln_counters[cpt]->lct_health.lch_resend_count++;
3308 lnet_resend_pending_msgs(void)
3312 cfs_cpt_for_each(i, lnet_cpt_table()) {
3314 lnet_resend_pending_msgs_locked(the_lnet.ln_mt_resendqs[i], i);
3319 /* called with cpt and ni_lock held */
3321 lnet_unlink_ni_recovery_mdh_locked(struct lnet_ni *ni, int cpt, bool force)
3323 struct lnet_handle_md recovery_mdh;
3325 LNetInvalidateMDHandle(&recovery_mdh);
3327 if (ni->ni_recovery_state & LNET_NI_RECOVERY_PENDING ||
3329 recovery_mdh = ni->ni_ping_mdh;
3330 LNetInvalidateMDHandle(&ni->ni_ping_mdh);
3333 lnet_net_unlock(cpt);
3334 if (!LNetMDHandleIsInvalid(recovery_mdh))
3335 LNetMDUnlink(recovery_mdh);
3341 lnet_recover_local_nis(void)
3343 struct lnet_mt_event_info *ev_info;
3344 LIST_HEAD(processed_list);
3345 LIST_HEAD(local_queue);
3346 struct lnet_handle_md mdh;
3347 struct lnet_ni *tmp;
3354 * splice the recovery queue on a local queue. We will iterate
3355 * through the local queue and update it as needed. Once we're
3356 * done with the traversal, we'll splice the local queue back on
3357 * the head of the ln_mt_localNIRecovq. Any newly added local NIs
3358 * will be traversed in the next iteration.
3361 list_splice_init(&the_lnet.ln_mt_localNIRecovq,
3365 list_for_each_entry_safe(ni, tmp, &local_queue, ni_recovery) {
3367 * if an NI is being deleted or it is now healthy, there
3368 * is no need to keep it around in the recovery queue.
3369 * The monitor thread is the only thread responsible for
3370 * removing the NI from the recovery queue.
3371 * Multiple threads can be adding NIs to the recovery
3374 healthv = atomic_read(&ni->ni_healthv);
3378 if (ni->ni_state != LNET_NI_STATE_ACTIVE ||
3379 healthv == LNET_MAX_HEALTH_VALUE) {
3380 list_del_init(&ni->ni_recovery);
3381 lnet_unlink_ni_recovery_mdh_locked(ni, 0, false);
3383 lnet_ni_decref_locked(ni, 0);
3389 * if the local NI failed recovery we must unlink the md.
3390 * But we want to keep the local_ni on the recovery queue
3391 * so we can continue the attempts to recover it.
3393 if (ni->ni_recovery_state & LNET_NI_RECOVERY_FAILED) {
3394 lnet_unlink_ni_recovery_mdh_locked(ni, 0, true);
3395 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_FAILED;
3402 CDEBUG(D_NET, "attempting to recover local ni: %s\n",
3403 libcfs_nid2str(ni->ni_nid));
3406 if (!(ni->ni_recovery_state & LNET_NI_RECOVERY_PENDING)) {
3407 ni->ni_recovery_state |= LNET_NI_RECOVERY_PENDING;
3410 LIBCFS_ALLOC(ev_info, sizeof(*ev_info));
3412 CERROR("out of memory. Can't recover %s\n",
3413 libcfs_nid2str(ni->ni_nid));
3415 ni->ni_recovery_state &=
3416 ~LNET_NI_RECOVERY_PENDING;
3421 mdh = ni->ni_ping_mdh;
3423 * Invalidate the ni mdh in case it's deleted.
3424 * We'll unlink the mdh in this case below.
3426 LNetInvalidateMDHandle(&ni->ni_ping_mdh);
3430 * remove the NI from the local queue and drop the
3431 * reference count to it while we're recovering
3432 * it. The reason for that, is that the NI could
3433 * be deleted, and the way the code is structured
3434 * is if we don't drop the NI, then the deletion
3435 * code will enter a loop waiting for the
3436 * reference count to be removed while holding the
3437 * ln_mutex_lock(). When we look up the peer to
3438 * send to in lnet_select_pathway() we will try to
3439 * lock the ln_mutex_lock() as well, leading to
3440 * a deadlock. By dropping the refcount and
3441 * removing it from the list, we allow for the NI
3442 * to be removed, then we use the cached NID to
3443 * look it up again. If it's gone, then we just
3444 * continue examining the rest of the queue.
3447 list_del_init(&ni->ni_recovery);
3448 lnet_ni_decref_locked(ni, 0);
3451 ev_info->mt_type = MT_TYPE_LOCAL_NI;
3452 ev_info->mt_nid = nid;
3453 rc = lnet_send_ping(nid, &mdh, LNET_INTERFACES_MIN,
3454 ev_info, the_lnet.ln_mt_handler,
3456 /* lookup the nid again */
3458 ni = lnet_nid2ni_locked(nid, 0);
3461 * the NI has been deleted when we dropped
3469 * Same note as in lnet_recover_peer_nis(). When
3470 * we're sending the ping, the NI is free to be
3471 * deleted or manipulated. By this point it
3472 * could've been added back on the recovery queue,
3473 * and a refcount taken on it.
3474 * So we can't just add it blindly again or we'll
3475 * corrupt the queue. We must check under lock if
3476 * it's not on any list and if not then add it
3477 * to the processed list, which will eventually be
3478 * spliced back on to the recovery queue.
3480 ni->ni_ping_mdh = mdh;
3481 if (list_empty(&ni->ni_recovery)) {
3482 list_add_tail(&ni->ni_recovery, &processed_list);
3483 lnet_ni_addref_locked(ni, 0);
3489 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_PENDING;
3495 * put back the remaining NIs on the ln_mt_localNIRecovq to be
3496 * reexamined in the next iteration.
3498 list_splice_init(&processed_list, &local_queue);
3500 list_splice(&local_queue, &the_lnet.ln_mt_localNIRecovq);
3505 lnet_resendqs_create(void)
3507 struct list_head **resendqs;
3508 resendqs = lnet_create_array_of_queues();
3513 lnet_net_lock(LNET_LOCK_EX);
3514 the_lnet.ln_mt_resendqs = resendqs;
3515 lnet_net_unlock(LNET_LOCK_EX);
3521 lnet_clean_local_ni_recoveryq(void)
3525 /* This is only called when the monitor thread has stopped */
3528 while (!list_empty(&the_lnet.ln_mt_localNIRecovq)) {
3529 ni = list_entry(the_lnet.ln_mt_localNIRecovq.next,
3530 struct lnet_ni, ni_recovery);
3531 list_del_init(&ni->ni_recovery);
3533 lnet_unlink_ni_recovery_mdh_locked(ni, 0, true);
3535 lnet_ni_decref_locked(ni, 0);
3542 lnet_unlink_lpni_recovery_mdh_locked(struct lnet_peer_ni *lpni, int cpt,
3545 struct lnet_handle_md recovery_mdh;
3547 LNetInvalidateMDHandle(&recovery_mdh);
3549 if (lpni->lpni_state & LNET_PEER_NI_RECOVERY_PENDING || force) {
3550 recovery_mdh = lpni->lpni_recovery_ping_mdh;
3551 LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
3553 spin_unlock(&lpni->lpni_lock);
3554 lnet_net_unlock(cpt);
3555 if (!LNetMDHandleIsInvalid(recovery_mdh))
3556 LNetMDUnlink(recovery_mdh);
3558 spin_lock(&lpni->lpni_lock);
3562 lnet_clean_peer_ni_recoveryq(void)
3564 struct lnet_peer_ni *lpni, *tmp;
3566 lnet_net_lock(LNET_LOCK_EX);
3568 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_mt_peerNIRecovq,
3570 list_del_init(&lpni->lpni_recovery);
3571 spin_lock(&lpni->lpni_lock);
3572 lnet_unlink_lpni_recovery_mdh_locked(lpni, LNET_LOCK_EX, true);
3573 spin_unlock(&lpni->lpni_lock);
3574 lnet_peer_ni_decref_locked(lpni);
3577 lnet_net_unlock(LNET_LOCK_EX);
3581 lnet_clean_resendqs(void)
3583 struct lnet_msg *msg, *tmp;
3587 cfs_cpt_for_each(i, lnet_cpt_table()) {
3589 list_splice_init(the_lnet.ln_mt_resendqs[i], &msgs);
3591 list_for_each_entry_safe(msg, tmp, &msgs, msg_list) {
3592 list_del_init(&msg->msg_list);
3593 msg->msg_no_resend = true;
3594 lnet_finalize(msg, -ESHUTDOWN);
3598 cfs_percpt_free(the_lnet.ln_mt_resendqs);
3602 lnet_recover_peer_nis(void)
3604 struct lnet_mt_event_info *ev_info;
3605 LIST_HEAD(processed_list);
3606 LIST_HEAD(local_queue);
3607 struct lnet_handle_md mdh;
3608 struct lnet_peer_ni *lpni;
3609 struct lnet_peer_ni *tmp;
3616 * Always use cpt 0 for locking across all interactions with
3617 * ln_mt_peerNIRecovq
3620 list_splice_init(&the_lnet.ln_mt_peerNIRecovq,
3624 now = ktime_get_seconds();
3626 list_for_each_entry_safe(lpni, tmp, &local_queue,
3629 * The same protection strategy is used here as is in the
3630 * local recovery case.
3633 healthv = atomic_read(&lpni->lpni_healthv);
3634 spin_lock(&lpni->lpni_lock);
3635 if (lpni->lpni_state & LNET_PEER_NI_DELETING ||
3636 healthv == LNET_MAX_HEALTH_VALUE) {
3637 list_del_init(&lpni->lpni_recovery);
3638 lnet_unlink_lpni_recovery_mdh_locked(lpni, 0, false);
3639 spin_unlock(&lpni->lpni_lock);
3640 lnet_peer_ni_decref_locked(lpni);
3646 * If the peer NI has failed recovery we must unlink the
3647 * md. But we want to keep the peer ni on the recovery
3648 * queue so we can try to continue recovering it
3650 if (lpni->lpni_state & LNET_PEER_NI_RECOVERY_FAILED) {
3651 lnet_unlink_lpni_recovery_mdh_locked(lpni, 0, true);
3652 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_FAILED;
3655 spin_unlock(&lpni->lpni_lock);
3657 if (now < lpni->lpni_next_ping) {
3665 * NOTE: we're racing with peer deletion from user space.
3666 * It's possible that a peer is deleted after we check its
3667 * state. In this case the recovery can create a new peer
3669 spin_lock(&lpni->lpni_lock);
3670 if (!(lpni->lpni_state & LNET_PEER_NI_RECOVERY_PENDING) &&
3671 !(lpni->lpni_state & LNET_PEER_NI_DELETING)) {
3672 lpni->lpni_state |= LNET_PEER_NI_RECOVERY_PENDING;
3673 spin_unlock(&lpni->lpni_lock);
3675 LIBCFS_ALLOC(ev_info, sizeof(*ev_info));
3677 CERROR("out of memory. Can't recover %s\n",
3678 libcfs_nid2str(lpni->lpni_nid));
3679 spin_lock(&lpni->lpni_lock);
3680 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3681 spin_unlock(&lpni->lpni_lock);
3685 /* look at the comments in lnet_recover_local_nis() */
3686 mdh = lpni->lpni_recovery_ping_mdh;
3687 LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
3688 nid = lpni->lpni_nid;
3690 list_del_init(&lpni->lpni_recovery);
3691 lnet_peer_ni_decref_locked(lpni);
3694 ev_info->mt_type = MT_TYPE_PEER_NI;
3695 ev_info->mt_nid = nid;
3696 rc = lnet_send_ping(nid, &mdh, LNET_INTERFACES_MIN,
3697 ev_info, the_lnet.ln_mt_handler,
3701 * lnet_find_peer_ni_locked() grabs a refcount for
3702 * us. No need to take it explicitly.
3704 lpni = lnet_find_peer_ni_locked(nid);
3711 lpni->lpni_ping_count++;
3713 lpni->lpni_recovery_ping_mdh = mdh;
3715 lnet_peer_ni_add_to_recoveryq_locked(lpni,
3719 spin_lock(&lpni->lpni_lock);
3720 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3721 spin_unlock(&lpni->lpni_lock);
3724 /* Drop the ref taken by lnet_find_peer_ni_locked() */
3725 lnet_peer_ni_decref_locked(lpni);
3728 spin_unlock(&lpni->lpni_lock);
3731 list_splice_init(&processed_list, &local_queue);
3733 list_splice(&local_queue, &the_lnet.ln_mt_peerNIRecovq);
3738 lnet_monitor_thread(void *arg)
3740 time64_t recovery_timeout = 0;
3741 time64_t rsp_timeout = 0;
3745 wait_for_completion(&the_lnet.ln_started);
3747 * The monitor thread takes care of the following:
3748 * 1. Checks the aliveness of routers
3749 * 2. Checks if there are messages on the resend queue to resend
3751 * 3. Check if there are any NIs on the local recovery queue and
3753 * 4. Checks if there are any NIs on the remote recovery queue
3756 while (the_lnet.ln_mt_state == LNET_MT_STATE_RUNNING) {
3757 now = ktime_get_real_seconds();
3759 if (lnet_router_checker_active())
3760 lnet_check_routers();
3762 lnet_resend_pending_msgs();
3764 if (now >= rsp_timeout) {
3765 lnet_finalize_expired_responses();
3766 rsp_timeout = now + (lnet_transaction_timeout / 2);
3769 if (now >= recovery_timeout) {
3770 lnet_recover_local_nis();
3771 lnet_recover_peer_nis();
3772 recovery_timeout = now + lnet_recovery_interval;
3776 * TODO do we need to check if we should sleep without
3777 * timeout? Technically, an active system will always
3778 * have messages in flight so this check will always
3779 * evaluate to false. And on an idle system do we care
3780 * if we wake up every 1 second? Although, we've seen
3781 * cases where we get a complaint that an idle thread
3782 * is waking up unnecessarily.
3784 * Take into account the current net_count when you wake
3785 * up for alive router checking, since we need to check
3786 * possibly as many networks as we have configured.
3788 interval = min(lnet_recovery_interval,
3789 min((unsigned int) alive_router_check_interval /
3790 lnet_current_net_count,
3791 lnet_transaction_timeout / 2));
3792 wait_for_completion_interruptible_timeout(
3793 &the_lnet.ln_mt_wait_complete,
3794 cfs_time_seconds(interval));
3795 /* Must re-init the completion before testing anything,
3796 * including ln_mt_state.
3798 reinit_completion(&the_lnet.ln_mt_wait_complete);
3802 lnet_net_lock(LNET_LOCK_EX);
3803 the_lnet.ln_mt_state = LNET_MT_STATE_SHUTDOWN;
3804 lnet_net_unlock(LNET_LOCK_EX);
3806 /* signal that the monitor thread is exiting */
3807 up(&the_lnet.ln_mt_signal);
3815 * Returns == 0 if success
3816 * Returns > 0 if LNetMDBind or prior fails
3817 * Returns < 0 if LNetGet fails
3820 lnet_send_ping(lnet_nid_t dest_nid,
3821 struct lnet_handle_md *mdh, int nnis,
3822 void *user_data, lnet_handler_t handler, bool recovery)
3824 struct lnet_md md = { NULL };
3825 struct lnet_process_id id;
3826 struct lnet_ping_buffer *pbuf;
3829 if (dest_nid == LNET_NID_ANY) {
3834 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
3840 /* initialize md content */
3841 md.start = &pbuf->pb_info;
3842 md.length = LNET_PING_INFO_SIZE(nnis);
3843 md.threshold = 2; /* GET/REPLY */
3845 md.options = LNET_MD_TRUNCATE | LNET_MD_TRACK_RESPONSE;
3846 md.user_ptr = user_data;
3847 md.handler = handler;
3849 rc = LNetMDBind(&md, LNET_UNLINK, mdh);
3851 lnet_ping_buffer_decref(pbuf);
3852 CERROR("Can't bind MD: %d\n", rc);
3853 rc = -rc; /* change the rc to positive */
3856 id.pid = LNET_PID_LUSTRE;
3859 rc = LNetGet(LNET_NID_ANY, *mdh, id,
3860 LNET_RESERVED_PORTAL,
3861 LNET_PROTO_PING_MATCHBITS, 0, recovery);
3864 goto fail_unlink_md;
3870 LNetInvalidateMDHandle(mdh);
3876 lnet_handle_recovery_reply(struct lnet_mt_event_info *ev_info,
3877 int status, bool send, bool unlink_event)
3879 lnet_nid_t nid = ev_info->mt_nid;
3881 if (ev_info->mt_type == MT_TYPE_LOCAL_NI) {
3885 ni = lnet_nid2ni_locked(nid, 0);
3891 if (!send || (send && status != 0))
3892 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_PENDING;
3894 ni->ni_recovery_state |= LNET_NI_RECOVERY_FAILED;
3899 CERROR("local NI (%s) recovery failed with %d\n",
3900 libcfs_nid2str(nid), status);
3904 * need to increment healthv for the ni here, because in
3905 * the lnet_finalize() path we don't have access to this
3906 * NI. And in order to get access to it, we'll need to
3907 * carry forward too much information.
3908 * In the peer case, it'll naturally be incremented
3911 lnet_inc_healthv(&ni->ni_healthv,
3912 lnet_health_sensitivity);
3914 struct lnet_peer_ni *lpni;
3917 cpt = lnet_net_lock_current();
3918 lpni = lnet_find_peer_ni_locked(nid);
3920 lnet_net_unlock(cpt);
3923 spin_lock(&lpni->lpni_lock);
3924 if (!send || (send && status != 0))
3925 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3927 lpni->lpni_state |= LNET_PEER_NI_RECOVERY_FAILED;
3928 spin_unlock(&lpni->lpni_lock);
3929 lnet_peer_ni_decref_locked(lpni);
3930 lnet_net_unlock(cpt);
3933 CERROR("peer NI (%s) recovery failed with %d\n",
3934 libcfs_nid2str(nid), status);
3939 lnet_mt_event_handler(struct lnet_event *event)
3941 struct lnet_mt_event_info *ev_info = event->md_user_ptr;
3942 struct lnet_ping_buffer *pbuf;
3944 /* TODO: remove assert */
3945 LASSERT(event->type == LNET_EVENT_REPLY ||
3946 event->type == LNET_EVENT_SEND ||
3947 event->type == LNET_EVENT_UNLINK);
3949 CDEBUG(D_NET, "Received event: %d status: %d\n", event->type,
3952 switch (event->type) {
3953 case LNET_EVENT_UNLINK:
3954 CDEBUG(D_NET, "%s recovery ping unlinked\n",
3955 libcfs_nid2str(ev_info->mt_nid));
3957 case LNET_EVENT_REPLY:
3958 lnet_handle_recovery_reply(ev_info, event->status, false,
3959 event->type == LNET_EVENT_UNLINK);
3961 case LNET_EVENT_SEND:
3962 CDEBUG(D_NET, "%s recovery message sent %s:%d\n",
3963 libcfs_nid2str(ev_info->mt_nid),
3964 (event->status) ? "unsuccessfully" :
3965 "successfully", event->status);
3966 lnet_handle_recovery_reply(ev_info, event->status, true, false);
3969 CERROR("Unexpected event: %d\n", event->type);
3972 if (event->unlinked) {
3973 LIBCFS_FREE(ev_info, sizeof(*ev_info));
3974 pbuf = LNET_PING_INFO_TO_BUFFER(event->md_start);
3975 lnet_ping_buffer_decref(pbuf);
3980 lnet_rsp_tracker_create(void)
3982 struct list_head **rstqs;
3983 rstqs = lnet_create_array_of_queues();
3988 the_lnet.ln_mt_rstq = rstqs;
3994 lnet_rsp_tracker_clean(void)
3996 lnet_finalize_expired_responses();
3998 cfs_percpt_free(the_lnet.ln_mt_rstq);
3999 the_lnet.ln_mt_rstq = NULL;
4002 int lnet_monitor_thr_start(void)
4005 struct task_struct *task;
4007 if (the_lnet.ln_mt_state != LNET_MT_STATE_SHUTDOWN)
4010 rc = lnet_resendqs_create();
4014 rc = lnet_rsp_tracker_create();
4018 sema_init(&the_lnet.ln_mt_signal, 0);
4020 lnet_net_lock(LNET_LOCK_EX);
4021 the_lnet.ln_mt_state = LNET_MT_STATE_RUNNING;
4022 lnet_net_unlock(LNET_LOCK_EX);
4023 task = kthread_run(lnet_monitor_thread, NULL, "monitor_thread");
4026 CERROR("Can't start monitor thread: %d\n", rc);
4033 lnet_net_lock(LNET_LOCK_EX);
4034 the_lnet.ln_mt_state = LNET_MT_STATE_STOPPING;
4035 lnet_net_unlock(LNET_LOCK_EX);
4036 /* block until event callback signals exit */
4037 down(&the_lnet.ln_mt_signal);
4039 lnet_net_lock(LNET_LOCK_EX);
4040 the_lnet.ln_mt_state = LNET_MT_STATE_SHUTDOWN;
4041 lnet_net_unlock(LNET_LOCK_EX);
4042 lnet_rsp_tracker_clean();
4043 lnet_clean_local_ni_recoveryq();
4044 lnet_clean_peer_ni_recoveryq();
4045 lnet_clean_resendqs();
4046 the_lnet.ln_mt_handler = NULL;
4049 lnet_rsp_tracker_clean();
4050 lnet_clean_local_ni_recoveryq();
4051 lnet_clean_peer_ni_recoveryq();
4052 lnet_clean_resendqs();
4056 void lnet_monitor_thr_stop(void)
4058 if (the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN)
4061 LASSERT(the_lnet.ln_mt_state == LNET_MT_STATE_RUNNING);
4062 lnet_net_lock(LNET_LOCK_EX);
4063 the_lnet.ln_mt_state = LNET_MT_STATE_STOPPING;
4064 lnet_net_unlock(LNET_LOCK_EX);
4066 /* tell the monitor thread that we're shutting down */
4067 complete(&the_lnet.ln_mt_wait_complete);
4069 /* block until monitor thread signals that it's done */
4070 down(&the_lnet.ln_mt_signal);
4071 LASSERT(the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN);
4073 /* perform cleanup tasks */
4074 lnet_rsp_tracker_clean();
4075 lnet_clean_local_ni_recoveryq();
4076 lnet_clean_peer_ni_recoveryq();
4077 lnet_clean_resendqs();
4081 lnet_drop_message(struct lnet_ni *ni, int cpt, void *private, unsigned int nob,
4085 lnet_incr_stats(&ni->ni_stats, msg_type, LNET_STATS_TYPE_DROP);
4086 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
4087 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length += nob;
4088 lnet_net_unlock(cpt);
4090 lnet_ni_recv(ni, private, NULL, 0, 0, 0, nob);
4094 lnet_recv_put(struct lnet_ni *ni, struct lnet_msg *msg)
4096 struct lnet_hdr *hdr = &msg->msg_hdr;
4098 if (msg->msg_wanted != 0)
4099 lnet_setpayloadbuffer(msg);
4101 lnet_build_msg_event(msg, LNET_EVENT_PUT);
4103 /* Must I ACK? If so I'll grab the ack_wmd out of the header and put
4104 * it back into the ACK during lnet_finalize() */
4105 msg->msg_ack = (!lnet_is_wire_handle_none(&hdr->msg.put.ack_wmd) &&
4106 (msg->msg_md->md_options & LNET_MD_ACK_DISABLE) == 0);
4108 lnet_ni_recv(ni, msg->msg_private, msg, msg->msg_rx_delayed,
4109 msg->msg_offset, msg->msg_wanted, hdr->payload_length);
4113 lnet_parse_put(struct lnet_ni *ni, struct lnet_msg *msg)
4115 struct lnet_hdr *hdr = &msg->msg_hdr;
4116 struct lnet_match_info info;
4120 /* Convert put fields to host byte order */
4121 hdr->msg.put.match_bits = le64_to_cpu(hdr->msg.put.match_bits);
4122 hdr->msg.put.ptl_index = le32_to_cpu(hdr->msg.put.ptl_index);
4123 hdr->msg.put.offset = le32_to_cpu(hdr->msg.put.offset);
4125 /* Primary peer NID. */
4126 info.mi_id.nid = msg->msg_initiator;
4127 info.mi_id.pid = hdr->src_pid;
4128 info.mi_opc = LNET_MD_OP_PUT;
4129 info.mi_portal = hdr->msg.put.ptl_index;
4130 info.mi_rlength = hdr->payload_length;
4131 info.mi_roffset = hdr->msg.put.offset;
4132 info.mi_mbits = hdr->msg.put.match_bits;
4133 info.mi_cpt = lnet_cpt_of_nid(msg->msg_initiator, ni);
4135 msg->msg_rx_ready_delay = ni->ni_net->net_lnd->lnd_eager_recv == NULL;
4136 ready_delay = msg->msg_rx_ready_delay;
4139 rc = lnet_ptl_match_md(&info, msg);
4144 case LNET_MATCHMD_OK:
4145 lnet_recv_put(ni, msg);
4148 case LNET_MATCHMD_NONE:
4150 /* no eager_recv or has already called it, should
4151 * have been attached on delayed list */
4154 rc = lnet_ni_eager_recv(ni, msg);
4161 case LNET_MATCHMD_DROP:
4162 CNETERR("Dropping PUT from %s portal %d match %llu"
4163 " offset %d length %d: %d\n",
4164 libcfs_id2str(info.mi_id), info.mi_portal,
4165 info.mi_mbits, info.mi_roffset, info.mi_rlength, rc);
4167 return -ENOENT; /* -ve: OK but no match */
4172 lnet_parse_get(struct lnet_ni *ni, struct lnet_msg *msg, int rdma_get)
4174 struct lnet_match_info info;
4175 struct lnet_hdr *hdr = &msg->msg_hdr;
4176 struct lnet_process_id source_id;
4177 struct lnet_handle_wire reply_wmd;
4180 /* Convert get fields to host byte order */
4181 hdr->msg.get.match_bits = le64_to_cpu(hdr->msg.get.match_bits);
4182 hdr->msg.get.ptl_index = le32_to_cpu(hdr->msg.get.ptl_index);
4183 hdr->msg.get.sink_length = le32_to_cpu(hdr->msg.get.sink_length);
4184 hdr->msg.get.src_offset = le32_to_cpu(hdr->msg.get.src_offset);
4186 source_id.nid = hdr->src_nid;
4187 source_id.pid = hdr->src_pid;
4188 /* Primary peer NID */
4189 info.mi_id.nid = msg->msg_initiator;
4190 info.mi_id.pid = hdr->src_pid;
4191 info.mi_opc = LNET_MD_OP_GET;
4192 info.mi_portal = hdr->msg.get.ptl_index;
4193 info.mi_rlength = hdr->msg.get.sink_length;
4194 info.mi_roffset = hdr->msg.get.src_offset;
4195 info.mi_mbits = hdr->msg.get.match_bits;
4196 info.mi_cpt = lnet_cpt_of_nid(msg->msg_initiator, ni);
4198 rc = lnet_ptl_match_md(&info, msg);
4199 if (rc == LNET_MATCHMD_DROP) {
4200 CNETERR("Dropping GET from %s portal %d match %llu"
4201 " offset %d length %d\n",
4202 libcfs_id2str(info.mi_id), info.mi_portal,
4203 info.mi_mbits, info.mi_roffset, info.mi_rlength);
4204 return -ENOENT; /* -ve: OK but no match */
4207 LASSERT(rc == LNET_MATCHMD_OK);
4209 lnet_build_msg_event(msg, LNET_EVENT_GET);
4211 reply_wmd = hdr->msg.get.return_wmd;
4213 lnet_prep_send(msg, LNET_MSG_REPLY, source_id,
4214 msg->msg_offset, msg->msg_wanted);
4216 msg->msg_hdr.msg.reply.dst_wmd = reply_wmd;
4219 /* The LND completes the REPLY from her recv procedure */
4220 lnet_ni_recv(ni, msg->msg_private, msg, 0,
4221 msg->msg_offset, msg->msg_len, msg->msg_len);
4225 lnet_ni_recv(ni, msg->msg_private, NULL, 0, 0, 0, 0);
4226 msg->msg_receiving = 0;
4228 rc = lnet_send(ni->ni_nid, msg, msg->msg_from);
4230 /* didn't get as far as lnet_ni_send() */
4231 CERROR("%s: Unable to send REPLY for GET from %s: %d\n",
4232 libcfs_nid2str(ni->ni_nid),
4233 libcfs_id2str(info.mi_id), rc);
4235 lnet_finalize(msg, rc);
4242 lnet_parse_reply(struct lnet_ni *ni, struct lnet_msg *msg)
4244 void *private = msg->msg_private;
4245 struct lnet_hdr *hdr = &msg->msg_hdr;
4246 struct lnet_process_id src = {0};
4247 struct lnet_libmd *md;
4248 unsigned int rlength;
4249 unsigned int mlength;
4252 cpt = lnet_cpt_of_cookie(hdr->msg.reply.dst_wmd.wh_object_cookie);
4255 src.nid = hdr->src_nid;
4256 src.pid = hdr->src_pid;
4258 /* NB handles only looked up by creator (no flips) */
4259 md = lnet_wire_handle2md(&hdr->msg.reply.dst_wmd);
4260 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
4261 CNETERR("%s: Dropping REPLY from %s for %s "
4263 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
4264 (md == NULL) ? "invalid" : "inactive",
4265 hdr->msg.reply.dst_wmd.wh_interface_cookie,
4266 hdr->msg.reply.dst_wmd.wh_object_cookie);
4267 if (md != NULL && md->md_me != NULL)
4268 CERROR("REPLY MD also attached to portal %d\n",
4269 md->md_me->me_portal);
4271 lnet_res_unlock(cpt);
4272 return -ENOENT; /* -ve: OK but no match */
4275 LASSERT(md->md_offset == 0);
4277 rlength = hdr->payload_length;
4278 mlength = min(rlength, md->md_length);
4280 if (mlength < rlength &&
4281 (md->md_options & LNET_MD_TRUNCATE) == 0) {
4282 CNETERR("%s: Dropping REPLY from %s length %d "
4283 "for MD %#llx would overflow (%d)\n",
4284 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
4285 rlength, hdr->msg.reply.dst_wmd.wh_object_cookie,
4287 lnet_res_unlock(cpt);
4288 return -ENOENT; /* -ve: OK but no match */
4291 CDEBUG(D_NET, "%s: Reply from %s of length %d/%d into md %#llx\n",
4292 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
4293 mlength, rlength, hdr->msg.reply.dst_wmd.wh_object_cookie);
4295 lnet_msg_attach_md(msg, md, 0, mlength);
4298 lnet_setpayloadbuffer(msg);
4300 lnet_res_unlock(cpt);
4302 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
4304 lnet_ni_recv(ni, private, msg, 0, 0, mlength, rlength);
4309 lnet_parse_ack(struct lnet_ni *ni, struct lnet_msg *msg)
4311 struct lnet_hdr *hdr = &msg->msg_hdr;
4312 struct lnet_process_id src = {0};
4313 struct lnet_libmd *md;
4316 src.nid = hdr->src_nid;
4317 src.pid = hdr->src_pid;
4319 /* Convert ack fields to host byte order */
4320 hdr->msg.ack.match_bits = le64_to_cpu(hdr->msg.ack.match_bits);
4321 hdr->msg.ack.mlength = le32_to_cpu(hdr->msg.ack.mlength);
4323 cpt = lnet_cpt_of_cookie(hdr->msg.ack.dst_wmd.wh_object_cookie);
4326 /* NB handles only looked up by creator (no flips) */
4327 md = lnet_wire_handle2md(&hdr->msg.ack.dst_wmd);
4328 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
4329 /* Don't moan; this is expected */
4331 "%s: Dropping ACK from %s to %s MD %#llx.%#llx\n",
4332 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
4333 (md == NULL) ? "invalid" : "inactive",
4334 hdr->msg.ack.dst_wmd.wh_interface_cookie,
4335 hdr->msg.ack.dst_wmd.wh_object_cookie);
4336 if (md != NULL && md->md_me != NULL)
4337 CERROR("Source MD also attached to portal %d\n",
4338 md->md_me->me_portal);
4340 lnet_res_unlock(cpt);
4341 return -ENOENT; /* -ve! */
4344 CDEBUG(D_NET, "%s: ACK from %s into md %#llx\n",
4345 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
4346 hdr->msg.ack.dst_wmd.wh_object_cookie);
4348 lnet_msg_attach_md(msg, md, 0, 0);
4350 lnet_res_unlock(cpt);
4352 lnet_build_msg_event(msg, LNET_EVENT_ACK);
4354 lnet_ni_recv(ni, msg->msg_private, msg, 0, 0, 0, msg->msg_len);
4359 * \retval LNET_CREDIT_OK If \a msg is forwarded
4360 * \retval LNET_CREDIT_WAIT If \a msg is blocked because w/o buffer
4361 * \retval -ve error code
4364 lnet_parse_forward_locked(struct lnet_ni *ni, struct lnet_msg *msg)
4368 if (!the_lnet.ln_routing)
4371 if (msg->msg_rxpeer->lpni_rtrcredits <= 0 ||
4372 lnet_msg2bufpool(msg)->rbp_credits <= 0) {
4373 if (ni->ni_net->net_lnd->lnd_eager_recv == NULL) {
4374 msg->msg_rx_ready_delay = 1;
4376 lnet_net_unlock(msg->msg_rx_cpt);
4377 rc = lnet_ni_eager_recv(ni, msg);
4378 lnet_net_lock(msg->msg_rx_cpt);
4383 rc = lnet_post_routed_recv_locked(msg, 0);
4388 lnet_parse_local(struct lnet_ni *ni, struct lnet_msg *msg)
4392 switch (msg->msg_type) {
4394 rc = lnet_parse_ack(ni, msg);
4397 rc = lnet_parse_put(ni, msg);
4400 rc = lnet_parse_get(ni, msg, msg->msg_rdma_get);
4402 case LNET_MSG_REPLY:
4403 rc = lnet_parse_reply(ni, msg);
4405 default: /* prevent an unused label if !kernel */
4410 LASSERT(rc == 0 || rc == -ENOENT);
4415 lnet_msgtyp2str (int type)
4424 case LNET_MSG_REPLY:
4426 case LNET_MSG_HELLO:
4429 return ("<UNKNOWN>");
4434 lnet_parse(struct lnet_ni *ni, struct lnet_hdr *hdr, lnet_nid_t from_nid,
4435 void *private, int rdma_req)
4437 struct lnet_peer_ni *lpni;
4438 struct lnet_msg *msg;
4439 __u32 payload_length;
4440 lnet_pid_t dest_pid;
4441 lnet_nid_t dest_nid;
4449 LASSERT (!in_interrupt ());
4451 type = le32_to_cpu(hdr->type);
4452 src_nid = le64_to_cpu(hdr->src_nid);
4453 dest_nid = le64_to_cpu(hdr->dest_nid);
4454 dest_pid = le32_to_cpu(hdr->dest_pid);
4455 payload_length = le32_to_cpu(hdr->payload_length);
4457 for_me = (ni->ni_nid == dest_nid);
4458 cpt = lnet_cpt_of_nid(from_nid, ni);
4460 CDEBUG(D_NET, "TRACE: %s(%s) <- %s : %s - %s\n",
4461 libcfs_nid2str(dest_nid),
4462 libcfs_nid2str(ni->ni_nid),
4463 libcfs_nid2str(src_nid),
4464 lnet_msgtyp2str(type),
4465 (for_me) ? "for me" : "routed");
4470 if (payload_length > 0) {
4471 CERROR("%s, src %s: bad %s payload %d (0 expected)\n",
4472 libcfs_nid2str(from_nid),
4473 libcfs_nid2str(src_nid),
4474 lnet_msgtyp2str(type), payload_length);
4480 case LNET_MSG_REPLY:
4481 if (payload_length >
4482 (__u32)(for_me ? LNET_MAX_PAYLOAD : LNET_MTU)) {
4483 CERROR("%s, src %s: bad %s payload %d "
4484 "(%d max expected)\n",
4485 libcfs_nid2str(from_nid),
4486 libcfs_nid2str(src_nid),
4487 lnet_msgtyp2str(type),
4489 for_me ? LNET_MAX_PAYLOAD : LNET_MTU);
4495 CERROR("%s, src %s: Bad message type 0x%x\n",
4496 libcfs_nid2str(from_nid),
4497 libcfs_nid2str(src_nid), type);
4501 if (the_lnet.ln_routing &&
4502 ni->ni_net->net_last_alive != ktime_get_real_seconds()) {
4504 spin_lock(&ni->ni_net->net_lock);
4505 ni->ni_net->net_last_alive = ktime_get_real_seconds();
4506 spin_unlock(&ni->ni_net->net_lock);
4507 push = lnet_ni_set_status_locked(ni, LNET_NI_STATUS_UP);
4512 lnet_push_update_to_peers(1);
4514 /* Regard a bad destination NID as a protocol error. Senders should
4515 * know what they're doing; if they don't they're misconfigured, buggy
4516 * or malicious so we chop them off at the knees :) */
4519 if (LNET_NIDNET(dest_nid) == LNET_NIDNET(ni->ni_nid)) {
4520 /* should have gone direct */
4521 CERROR("%s, src %s: Bad dest nid %s "
4522 "(should have been sent direct)\n",
4523 libcfs_nid2str(from_nid),
4524 libcfs_nid2str(src_nid),
4525 libcfs_nid2str(dest_nid));
4529 if (lnet_islocalnid(dest_nid)) {
4530 /* dest is another local NI; sender should have used
4531 * this node's NID on its own network */
4532 CERROR("%s, src %s: Bad dest nid %s "
4533 "(it's my nid but on a different network)\n",
4534 libcfs_nid2str(from_nid),
4535 libcfs_nid2str(src_nid),
4536 libcfs_nid2str(dest_nid));
4540 if (rdma_req && type == LNET_MSG_GET) {
4541 CERROR("%s, src %s: Bad optimized GET for %s "
4542 "(final destination must be me)\n",
4543 libcfs_nid2str(from_nid),
4544 libcfs_nid2str(src_nid),
4545 libcfs_nid2str(dest_nid));
4549 if (!the_lnet.ln_routing) {
4550 CERROR("%s, src %s: Dropping message for %s "
4551 "(routing not enabled)\n",
4552 libcfs_nid2str(from_nid),
4553 libcfs_nid2str(src_nid),
4554 libcfs_nid2str(dest_nid));
4559 /* Message looks OK; we're not going to return an error, so we MUST
4560 * call back lnd_recv() come what may... */
4562 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
4563 fail_peer(src_nid, 0)) { /* shall we now? */
4564 CERROR("%s, src %s: Dropping %s to simulate failure\n",
4565 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4566 lnet_msgtyp2str(type));
4570 if (!list_empty(&the_lnet.ln_drop_rules) &&
4571 lnet_drop_rule_match(hdr, ni->ni_nid, NULL)) {
4573 "%s, src %s, dst %s: Dropping %s to simulate silent message loss\n",
4574 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4575 libcfs_nid2str(dest_nid), lnet_msgtyp2str(type));
4579 msg = lnet_msg_alloc();
4581 CERROR("%s, src %s: Dropping %s (out of memory)\n",
4582 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4583 lnet_msgtyp2str(type));
4587 /* msg zeroed in lnet_msg_alloc; i.e. flags all clear,
4588 * pointers NULL etc */
4590 msg->msg_type = type;
4591 msg->msg_private = private;
4592 msg->msg_receiving = 1;
4593 msg->msg_rdma_get = rdma_req;
4594 msg->msg_len = msg->msg_wanted = payload_length;
4595 msg->msg_offset = 0;
4596 msg->msg_hdr = *hdr;
4597 /* for building message event */
4598 msg->msg_from = from_nid;
4600 msg->msg_target.pid = dest_pid;
4601 msg->msg_target.nid = dest_nid;
4602 msg->msg_routing = 1;
4605 /* convert common msg->hdr fields to host byteorder */
4606 msg->msg_hdr.type = type;
4607 msg->msg_hdr.src_nid = src_nid;
4608 msg->msg_hdr.src_pid = le32_to_cpu(msg->msg_hdr.src_pid);
4609 msg->msg_hdr.dest_nid = dest_nid;
4610 msg->msg_hdr.dest_pid = dest_pid;
4611 msg->msg_hdr.payload_length = payload_length;
4615 lpni = lnet_nid2peerni_locked(from_nid, ni->ni_nid, cpt);
4617 lnet_net_unlock(cpt);
4618 CERROR("%s, src %s: Dropping %s "
4619 "(error %ld looking up sender)\n",
4620 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4621 lnet_msgtyp2str(type), PTR_ERR(lpni));
4623 if (rc == -ESHUTDOWN)
4624 /* We are shutting down. Don't do anything more */
4629 if (lnet_drop_asym_route && for_me &&
4630 LNET_NIDNET(src_nid) != LNET_NIDNET(from_nid)) {
4631 __u32 src_net_id = LNET_NIDNET(src_nid);
4632 struct lnet_peer *gw = lpni->lpni_peer_net->lpn_peer;
4633 struct lnet_route *route;
4636 list_for_each_entry(route, &gw->lp_routes, lr_gwlist) {
4637 if (route->lr_net == src_net_id) {
4643 lnet_net_unlock(cpt);
4644 /* we would not use from_nid to route a message to
4646 * => asymmetric routing detected but forbidden
4648 CERROR("%s, src %s: Dropping asymmetrical route %s\n",
4649 libcfs_nid2str(from_nid),
4650 libcfs_nid2str(src_nid), lnet_msgtyp2str(type));
4656 lpni->lpni_last_alive = ktime_get_seconds();
4658 msg->msg_rxpeer = lpni;
4660 lnet_ni_addref_locked(ni, cpt);
4661 /* Multi-Rail: Primary NID of source. */
4662 msg->msg_initiator = lnet_peer_primary_nid_locked(src_nid);
4665 * mark the status of this lpni as UP since we received a message
4666 * from it. The ping response reports back the ns_status which is
4667 * marked on the remote as up or down and we cache it here.
4669 msg->msg_rxpeer->lpni_ns_status = LNET_NI_STATUS_UP;
4671 lnet_msg_commit(msg, cpt);
4673 /* message delay simulation */
4674 if (unlikely(!list_empty(&the_lnet.ln_delay_rules) &&
4675 lnet_delay_rule_match_locked(hdr, msg))) {
4676 lnet_net_unlock(cpt);
4681 rc = lnet_parse_forward_locked(ni, msg);
4682 lnet_net_unlock(cpt);
4687 if (rc == LNET_CREDIT_OK) {
4688 lnet_ni_recv(ni, msg->msg_private, msg, 0,
4689 0, payload_length, payload_length);
4694 lnet_net_unlock(cpt);
4696 rc = lnet_parse_local(ni, msg);
4702 LASSERT(msg->msg_md == NULL);
4703 lnet_finalize(msg, rc);
4706 lnet_drop_message(ni, cpt, private, payload_length, type);
4709 EXPORT_SYMBOL(lnet_parse);
4712 lnet_drop_delayed_msg_list(struct list_head *head, char *reason)
4714 while (!list_empty(head)) {
4715 struct lnet_process_id id = {0};
4716 struct lnet_msg *msg;
4718 msg = list_entry(head->next, struct lnet_msg, msg_list);
4719 list_del(&msg->msg_list);
4721 id.nid = msg->msg_hdr.src_nid;
4722 id.pid = msg->msg_hdr.src_pid;
4724 LASSERT(msg->msg_md == NULL);
4725 LASSERT(msg->msg_rx_delayed);
4726 LASSERT(msg->msg_rxpeer != NULL);
4727 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
4729 CWARN("Dropping delayed PUT from %s portal %d match %llu"
4730 " offset %d length %d: %s\n",
4732 msg->msg_hdr.msg.put.ptl_index,
4733 msg->msg_hdr.msg.put.match_bits,
4734 msg->msg_hdr.msg.put.offset,
4735 msg->msg_hdr.payload_length, reason);
4737 /* NB I can't drop msg's ref on msg_rxpeer until after I've
4738 * called lnet_drop_message(), so I just hang onto msg as well
4739 * until that's done */
4741 lnet_drop_message(msg->msg_rxni, msg->msg_rx_cpt,
4742 msg->msg_private, msg->msg_len,
4745 msg->msg_no_resend = true;
4747 * NB: message will not generate event because w/o attached MD,
4748 * but we still should give error code so lnet_msg_decommit()
4749 * can skip counters operations and other checks.
4751 lnet_finalize(msg, -ENOENT);
4756 lnet_recv_delayed_msg_list(struct list_head *head)
4758 while (!list_empty(head)) {
4759 struct lnet_msg *msg;
4760 struct lnet_process_id id;
4762 msg = list_entry(head->next, struct lnet_msg, msg_list);
4763 list_del(&msg->msg_list);
4765 /* md won't disappear under me, since each msg
4766 * holds a ref on it */
4768 id.nid = msg->msg_hdr.src_nid;
4769 id.pid = msg->msg_hdr.src_pid;
4771 LASSERT(msg->msg_rx_delayed);
4772 LASSERT(msg->msg_md != NULL);
4773 LASSERT(msg->msg_rxpeer != NULL);
4774 LASSERT(msg->msg_rxni != NULL);
4775 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
4777 CDEBUG(D_NET, "Resuming delayed PUT from %s portal %d "
4778 "match %llu offset %d length %d.\n",
4779 libcfs_id2str(id), msg->msg_hdr.msg.put.ptl_index,
4780 msg->msg_hdr.msg.put.match_bits,
4781 msg->msg_hdr.msg.put.offset,
4782 msg->msg_hdr.payload_length);
4784 lnet_recv_put(msg->msg_rxni, msg);
4789 lnet_attach_rsp_tracker(struct lnet_rsp_tracker *rspt, int cpt,
4790 struct lnet_libmd *md, struct lnet_handle_md mdh)
4793 struct lnet_rsp_tracker *local_rspt;
4796 * MD has a refcount taken by message so it's not going away.
4797 * The MD however can be looked up. We need to secure the access
4798 * to the md_rspt_ptr by taking the res_lock.
4799 * The rspt can be accessed without protection up to when it gets
4800 * added to the list.
4804 local_rspt = md->md_rspt_ptr;
4805 timeout_ns = lnet_transaction_timeout * NSEC_PER_SEC;
4806 if (local_rspt != NULL) {
4808 * we already have an rspt attached to the md, so we'll
4809 * update the deadline on that one.
4811 lnet_rspt_free(rspt, cpt);
4814 rspt->rspt_mdh = mdh;
4815 rspt->rspt_cpt = cpt;
4816 /* store the rspt so we can access it when we get the REPLY */
4817 md->md_rspt_ptr = rspt;
4820 local_rspt->rspt_deadline = ktime_add_ns(ktime_get(), timeout_ns);
4823 * add to the list of tracked responses. It's added to tail of the
4824 * list in order to expire all the older entries first.
4827 list_move_tail(&local_rspt->rspt_on_list, the_lnet.ln_mt_rstq[cpt]);
4828 lnet_net_unlock(cpt);
4829 lnet_res_unlock(cpt);
4833 * Initiate an asynchronous PUT operation.
4835 * There are several events associated with a PUT: completion of the send on
4836 * the initiator node (LNET_EVENT_SEND), and when the send completes
4837 * successfully, the receipt of an acknowledgment (LNET_EVENT_ACK) indicating
4838 * that the operation was accepted by the target. The event LNET_EVENT_PUT is
4839 * used at the target node to indicate the completion of incoming data
4842 * The local events will be logged in the EQ associated with the MD pointed to
4843 * by \a mdh handle. Using a MD without an associated EQ results in these
4844 * events being discarded. In this case, the caller must have another
4845 * mechanism (e.g., a higher level protocol) for determining when it is safe
4846 * to modify the memory region associated with the MD.
4848 * Note that LNet does not guarantee the order of LNET_EVENT_SEND and
4849 * LNET_EVENT_ACK, though intuitively ACK should happen after SEND.
4851 * \param self Indicates the NID of a local interface through which to send
4852 * the PUT request. Use LNET_NID_ANY to let LNet choose one by itself.
4853 * \param mdh A handle for the MD that describes the memory to be sent. The MD
4854 * must be "free floating" (See LNetMDBind()).
4855 * \param ack Controls whether an acknowledgment is requested.
4856 * Acknowledgments are only sent when they are requested by the initiating
4857 * process and the target MD enables them.
4858 * \param target A process identifier for the target process.
4859 * \param portal The index in the \a target's portal table.
4860 * \param match_bits The match bits to use for MD selection at the target
4862 * \param offset The offset into the target MD (only used when the target
4863 * MD has the LNET_MD_MANAGE_REMOTE option set).
4864 * \param hdr_data 64 bits of user data that can be included in the message
4865 * header. This data is written to an event queue entry at the target if an
4866 * EQ is present on the matching MD.
4868 * \retval 0 Success, and only in this case events will be generated
4869 * and logged to EQ (if it exists).
4870 * \retval -EIO Simulated failure.
4871 * \retval -ENOMEM Memory allocation failure.
4872 * \retval -ENOENT Invalid MD object.
4874 * \see struct lnet_event::hdr_data and lnet_event_kind_t.
4877 LNetPut(lnet_nid_t self, struct lnet_handle_md mdh, enum lnet_ack_req ack,
4878 struct lnet_process_id target, unsigned int portal,
4879 __u64 match_bits, unsigned int offset,
4882 struct lnet_msg *msg;
4883 struct lnet_libmd *md;
4886 struct lnet_rsp_tracker *rspt = NULL;
4888 LASSERT(the_lnet.ln_refcount > 0);
4890 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
4891 fail_peer(target.nid, 1)) { /* shall we now? */
4892 CERROR("Dropping PUT to %s: simulated failure\n",
4893 libcfs_id2str(target));
4897 msg = lnet_msg_alloc();
4899 CERROR("Dropping PUT to %s: ENOMEM on struct lnet_msg\n",
4900 libcfs_id2str(target));
4903 msg->msg_vmflush = !!(current->flags & PF_MEMALLOC);
4905 cpt = lnet_cpt_of_cookie(mdh.cookie);
4907 if (ack == LNET_ACK_REQ) {
4908 rspt = lnet_rspt_alloc(cpt);
4910 CERROR("Dropping PUT to %s: ENOMEM on response tracker\n",
4911 libcfs_id2str(target));
4914 INIT_LIST_HEAD(&rspt->rspt_on_list);
4919 md = lnet_handle2md(&mdh);
4920 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
4921 CERROR("Dropping PUT (%llu:%d:%s): MD (%d) invalid\n",
4922 match_bits, portal, libcfs_id2str(target),
4923 md == NULL ? -1 : md->md_threshold);
4924 if (md != NULL && md->md_me != NULL)
4925 CERROR("Source MD also attached to portal %d\n",
4926 md->md_me->me_portal);
4927 lnet_res_unlock(cpt);
4930 lnet_rspt_free(rspt, cpt);
4936 CDEBUG(D_NET, "LNetPut -> %s\n", libcfs_id2str(target));
4938 lnet_msg_attach_md(msg, md, 0, 0);
4940 lnet_prep_send(msg, LNET_MSG_PUT, target, 0, md->md_length);
4942 msg->msg_hdr.msg.put.match_bits = cpu_to_le64(match_bits);
4943 msg->msg_hdr.msg.put.ptl_index = cpu_to_le32(portal);
4944 msg->msg_hdr.msg.put.offset = cpu_to_le32(offset);
4945 msg->msg_hdr.msg.put.hdr_data = hdr_data;
4947 /* NB handles only looked up by creator (no flips) */
4948 if (ack == LNET_ACK_REQ) {
4949 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
4950 the_lnet.ln_interface_cookie;
4951 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
4952 md->md_lh.lh_cookie;
4954 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
4955 LNET_WIRE_HANDLE_COOKIE_NONE;
4956 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
4957 LNET_WIRE_HANDLE_COOKIE_NONE;
4960 lnet_res_unlock(cpt);
4962 lnet_build_msg_event(msg, LNET_EVENT_SEND);
4964 if (rspt && lnet_response_tracking_enabled(LNET_MSG_PUT,
4966 lnet_attach_rsp_tracker(rspt, cpt, md, mdh);
4968 lnet_rspt_free(rspt, cpt);
4970 if (CFS_FAIL_CHECK_ORSET(CFS_FAIL_PTLRPC_OST_BULK_CB2,
4974 rc = lnet_send(self, msg, LNET_NID_ANY);
4977 CNETERR("Error sending PUT to %s: %d\n",
4978 libcfs_id2str(target), rc);
4979 msg->msg_no_resend = true;
4980 lnet_finalize(msg, rc);
4983 /* completion will be signalled by an event */
4986 EXPORT_SYMBOL(LNetPut);
4989 * The LND can DMA direct to the GET md (i.e. no REPLY msg). This
4990 * returns a msg for the LND to pass to lnet_finalize() when the sink
4991 * data has been received.
4993 * CAVEAT EMPTOR: 'getmsg' is the original GET, which is freed when
4994 * lnet_finalize() is called on it, so the LND must call this first
4997 lnet_create_reply_msg(struct lnet_ni *ni, struct lnet_msg *getmsg)
4999 struct lnet_msg *msg = lnet_msg_alloc();
5000 struct lnet_libmd *getmd = getmsg->msg_md;
5001 struct lnet_process_id peer_id = getmsg->msg_target;
5004 LASSERT(!getmsg->msg_target_is_router);
5005 LASSERT(!getmsg->msg_routing);
5008 CERROR("%s: Dropping REPLY from %s: can't allocate msg\n",
5009 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id));
5013 cpt = lnet_cpt_of_cookie(getmd->md_lh.lh_cookie);
5016 LASSERT(getmd->md_refcount > 0);
5018 if (getmd->md_threshold == 0) {
5019 CERROR("%s: Dropping REPLY from %s for inactive MD %p\n",
5020 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id),
5022 lnet_res_unlock(cpt);
5026 LASSERT(getmd->md_offset == 0);
5028 CDEBUG(D_NET, "%s: Reply from %s md %p\n",
5029 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id), getmd);
5031 /* setup information for lnet_build_msg_event */
5032 msg->msg_initiator = getmsg->msg_txpeer->lpni_peer_net->lpn_peer->lp_primary_nid;
5033 msg->msg_from = peer_id.nid;
5034 msg->msg_type = LNET_MSG_GET; /* flag this msg as an "optimized" GET */
5035 msg->msg_hdr.src_nid = peer_id.nid;
5036 msg->msg_hdr.payload_length = getmd->md_length;
5037 msg->msg_receiving = 1; /* required by lnet_msg_attach_md */
5039 lnet_msg_attach_md(msg, getmd, getmd->md_offset, getmd->md_length);
5040 lnet_res_unlock(cpt);
5042 cpt = lnet_cpt_of_nid(peer_id.nid, ni);
5045 lnet_msg_commit(msg, cpt);
5046 lnet_net_unlock(cpt);
5048 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
5053 cpt = lnet_cpt_of_nid(peer_id.nid, ni);
5056 lnet_incr_stats(&ni->ni_stats, LNET_MSG_GET, LNET_STATS_TYPE_DROP);
5057 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
5058 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length +=
5060 lnet_net_unlock(cpt);
5067 EXPORT_SYMBOL(lnet_create_reply_msg);
5070 lnet_set_reply_msg_len(struct lnet_ni *ni, struct lnet_msg *reply,
5073 /* Set the REPLY length, now the RDMA that elides the REPLY message has
5074 * completed and I know it. */
5075 LASSERT(reply != NULL);
5076 LASSERT(reply->msg_type == LNET_MSG_GET);
5077 LASSERT(reply->msg_ev.type == LNET_EVENT_REPLY);
5079 /* NB I trusted my peer to RDMA. If she tells me she's written beyond
5080 * the end of my buffer, I might as well be dead. */
5081 LASSERT(len <= reply->msg_ev.mlength);
5083 reply->msg_ev.mlength = len;
5085 EXPORT_SYMBOL(lnet_set_reply_msg_len);
5088 * Initiate an asynchronous GET operation.
5090 * On the initiator node, an LNET_EVENT_SEND is logged when the GET request
5091 * is sent, and an LNET_EVENT_REPLY is logged when the data returned from
5092 * the target node in the REPLY has been written to local MD.
5094 * On the target node, an LNET_EVENT_GET is logged when the GET request
5095 * arrives and is accepted into a MD.
5097 * \param self,target,portal,match_bits,offset See the discussion in LNetPut().
5098 * \param mdh A handle for the MD that describes the memory into which the
5099 * requested data will be received. The MD must be "free floating" (See LNetMDBind()).
5101 * \retval 0 Success, and only in this case events will be generated
5102 * and logged to EQ (if it exists) of the MD.
5103 * \retval -EIO Simulated failure.
5104 * \retval -ENOMEM Memory allocation failure.
5105 * \retval -ENOENT Invalid MD object.
5108 LNetGet(lnet_nid_t self, struct lnet_handle_md mdh,
5109 struct lnet_process_id target, unsigned int portal,
5110 __u64 match_bits, unsigned int offset, bool recovery)
5112 struct lnet_msg *msg;
5113 struct lnet_libmd *md;
5114 struct lnet_rsp_tracker *rspt;
5118 LASSERT(the_lnet.ln_refcount > 0);
5120 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
5121 fail_peer(target.nid, 1)) /* shall we now? */
5123 CERROR("Dropping GET to %s: simulated failure\n",
5124 libcfs_id2str(target));
5128 msg = lnet_msg_alloc();
5130 CERROR("Dropping GET to %s: ENOMEM on struct lnet_msg\n",
5131 libcfs_id2str(target));
5135 cpt = lnet_cpt_of_cookie(mdh.cookie);
5137 rspt = lnet_rspt_alloc(cpt);
5139 CERROR("Dropping GET to %s: ENOMEM on response tracker\n",
5140 libcfs_id2str(target));
5143 INIT_LIST_HEAD(&rspt->rspt_on_list);
5145 msg->msg_recovery = recovery;
5149 md = lnet_handle2md(&mdh);
5150 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
5151 CERROR("Dropping GET (%llu:%d:%s): MD (%d) invalid\n",
5152 match_bits, portal, libcfs_id2str(target),
5153 md == NULL ? -1 : md->md_threshold);
5154 if (md != NULL && md->md_me != NULL)
5155 CERROR("REPLY MD also attached to portal %d\n",
5156 md->md_me->me_portal);
5158 lnet_res_unlock(cpt);
5161 lnet_rspt_free(rspt, cpt);
5165 CDEBUG(D_NET, "LNetGet -> %s\n", libcfs_id2str(target));
5167 lnet_msg_attach_md(msg, md, 0, 0);
5169 lnet_prep_send(msg, LNET_MSG_GET, target, 0, 0);
5171 msg->msg_hdr.msg.get.match_bits = cpu_to_le64(match_bits);
5172 msg->msg_hdr.msg.get.ptl_index = cpu_to_le32(portal);
5173 msg->msg_hdr.msg.get.src_offset = cpu_to_le32(offset);
5174 msg->msg_hdr.msg.get.sink_length = cpu_to_le32(md->md_length);
5176 /* NB handles only looked up by creator (no flips) */
5177 msg->msg_hdr.msg.get.return_wmd.wh_interface_cookie =
5178 the_lnet.ln_interface_cookie;
5179 msg->msg_hdr.msg.get.return_wmd.wh_object_cookie =
5180 md->md_lh.lh_cookie;
5182 lnet_res_unlock(cpt);
5184 lnet_build_msg_event(msg, LNET_EVENT_SEND);
5186 if (lnet_response_tracking_enabled(LNET_MSG_GET, md->md_options))
5187 lnet_attach_rsp_tracker(rspt, cpt, md, mdh);
5189 lnet_rspt_free(rspt, cpt);
5191 rc = lnet_send(self, msg, LNET_NID_ANY);
5193 CNETERR("Error sending GET to %s: %d\n",
5194 libcfs_id2str(target), rc);
5195 msg->msg_no_resend = true;
5196 lnet_finalize(msg, rc);
5199 /* completion will be signalled by an event */
5202 EXPORT_SYMBOL(LNetGet);
5205 * Calculate distance to node at \a dstnid.
5207 * \param dstnid Target NID.
5208 * \param srcnidp If not NULL, NID of the local interface to reach \a dstnid
5210 * \param orderp If not NULL, order of the route to reach \a dstnid is saved
5213 * \retval 0 If \a dstnid belongs to a local interface, and reserved option
5214 * local_nid_dist_zero is set, which is the default.
5215 * \retval positives Distance to target NID, i.e. number of hops plus one.
5216 * \retval -EHOSTUNREACH If \a dstnid is not reachable.
5219 LNetDist(lnet_nid_t dstnid, lnet_nid_t *srcnidp, __u32 *orderp)
5221 struct list_head *e;
5222 struct lnet_ni *ni = NULL;
5223 struct lnet_remotenet *rnet;
5224 __u32 dstnet = LNET_NIDNET(dstnid);
5228 struct list_head *rn_list;
5230 /* if !local_nid_dist_zero, I don't return a distance of 0 ever
5231 * (when lustre sees a distance of 0, it substitutes 0@lo), so I
5232 * keep order 0 free for 0@lo and order 1 free for a local NID
5235 LASSERT(the_lnet.ln_refcount > 0);
5237 cpt = lnet_net_lock_current();
5239 while ((ni = lnet_get_next_ni_locked(NULL, ni))) {
5240 if (ni->ni_nid == dstnid) {
5241 if (srcnidp != NULL)
5243 if (orderp != NULL) {
5244 if (dstnid == LNET_NID_LO_0)
5249 lnet_net_unlock(cpt);
5251 return local_nid_dist_zero ? 0 : 1;
5254 if (LNET_NIDNET(ni->ni_nid) == dstnet) {
5255 /* Check if ni was originally created in
5256 * current net namespace.
5257 * If not, assign order above 0xffff0000,
5258 * to make this ni not a priority. */
5259 if (current->nsproxy &&
5260 !net_eq(ni->ni_net_ns, current->nsproxy->net_ns))
5261 order += 0xffff0000;
5262 if (srcnidp != NULL)
5263 *srcnidp = ni->ni_nid;
5266 lnet_net_unlock(cpt);
5273 rn_list = lnet_net2rnethash(dstnet);
5274 list_for_each(e, rn_list) {
5275 rnet = list_entry(e, struct lnet_remotenet, lrn_list);
5277 if (rnet->lrn_net == dstnet) {
5278 struct lnet_route *route;
5279 struct lnet_route *shortest = NULL;
5280 __u32 shortest_hops = LNET_UNDEFINED_HOPS;
5283 LASSERT(!list_empty(&rnet->lrn_routes));
5285 list_for_each_entry(route, &rnet->lrn_routes,
5287 route_hops = route->lr_hops;
5288 if (route_hops == LNET_UNDEFINED_HOPS)
5290 if (shortest == NULL ||
5291 route_hops < shortest_hops) {
5293 shortest_hops = route_hops;
5297 LASSERT(shortest != NULL);
5298 hops = shortest_hops;
5299 if (srcnidp != NULL) {
5300 struct lnet_net *net;
5301 net = lnet_get_net_locked(shortest->lr_lnet);
5303 ni = lnet_get_next_ni_locked(net, NULL);
5304 *srcnidp = ni->ni_nid;
5308 lnet_net_unlock(cpt);
5314 lnet_net_unlock(cpt);
5315 return -EHOSTUNREACH;
5317 EXPORT_SYMBOL(LNetDist);