4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * lnet/lnet/lib-move.c
33 * Data movement routines
36 #define DEBUG_SUBSYSTEM S_LNET
38 #include <linux/pagemap.h>
40 #include <lnet/lib-lnet.h>
41 #include <linux/nsproxy.h>
42 #include <lnet/lnet_rdma.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 nid4, unsigned int threshold)
195 struct lnet_test_peer *tp;
196 struct list_head *el;
197 struct list_head *next;
201 lnet_nid4_to_nid(nid4, &nid);
202 /* NB: use lnet_net_lock(0) to serialize operations on test peers */
203 if (threshold != 0) {
204 /* Adding a new entry */
205 LIBCFS_ALLOC(tp, sizeof(*tp));
210 tp->tp_threshold = threshold;
213 list_add_tail(&tp->tp_list, &the_lnet.ln_test_peers);
220 list_for_each_safe(el, next, &the_lnet.ln_test_peers) {
221 tp = list_entry(el, struct lnet_test_peer, tp_list);
223 if (tp->tp_threshold == 0 || /* needs culling anyway */
224 LNET_NID_IS_ANY(&nid) || /* removing all entries */
225 nid_same(&tp->tp_nid, &nid)) { /* matched this one */
226 list_move(&tp->tp_list, &cull);
232 while (!list_empty(&cull)) {
233 tp = list_entry(cull.next, struct lnet_test_peer, tp_list);
235 list_del(&tp->tp_list);
236 LIBCFS_FREE(tp, sizeof(*tp));
242 fail_peer(lnet_nid_t nid4, int outgoing)
244 struct lnet_test_peer *tp;
245 struct list_head *el;
246 struct list_head *next;
251 lnet_nid4_to_nid(nid4, &nid);
252 /* NB: use lnet_net_lock(0) to serialize operations on test peers */
255 list_for_each_safe(el, next, &the_lnet.ln_test_peers) {
256 tp = list_entry(el, struct lnet_test_peer, tp_list);
258 if (tp->tp_threshold == 0) {
261 /* only cull zombies on outgoing tests,
262 * since we may be at interrupt priority on
263 * incoming messages. */
264 list_move(&tp->tp_list, &cull);
269 if (LNET_NID_IS_ANY(&tp->tp_nid) || /* fail every peer */
270 nid_same(&nid, &tp->tp_nid)) { /* fail this peer */
273 if (tp->tp_threshold != LNET_MD_THRESH_INF) {
276 tp->tp_threshold == 0) {
278 list_move(&tp->tp_list, &cull);
287 while (!list_empty(&cull)) {
288 tp = list_entry(cull.next, struct lnet_test_peer, tp_list);
289 list_del(&tp->tp_list);
291 LIBCFS_FREE(tp, sizeof(*tp));
298 lnet_iov_nob(unsigned int niov, struct kvec *iov)
300 unsigned int nob = 0;
302 LASSERT(niov == 0 || iov != NULL);
304 nob += (iov++)->iov_len;
308 EXPORT_SYMBOL(lnet_iov_nob);
311 lnet_copy_iov2iov(unsigned int ndiov, struct kvec *diov, unsigned int doffset,
312 unsigned int nsiov, struct kvec *siov, unsigned int soffset,
315 /* NB diov, siov are READ-ONLY */
316 unsigned int this_nob;
321 /* skip complete frags before 'doffset' */
323 while (doffset >= diov->iov_len) {
324 doffset -= diov->iov_len;
330 /* skip complete frags before 'soffset' */
332 while (soffset >= siov->iov_len) {
333 soffset -= siov->iov_len;
342 this_nob = min3((unsigned int)diov->iov_len - doffset,
343 (unsigned int)siov->iov_len - soffset,
346 memcpy((char *)diov->iov_base + doffset,
347 (char *)siov->iov_base + soffset, this_nob);
350 if (diov->iov_len > doffset + this_nob) {
358 if (siov->iov_len > soffset + this_nob) {
367 EXPORT_SYMBOL(lnet_copy_iov2iov);
370 lnet_kiov_nob(unsigned int niov, struct bio_vec *kiov)
372 unsigned int nob = 0;
374 LASSERT(niov == 0 || kiov != NULL);
376 nob += (kiov++)->bv_len;
380 EXPORT_SYMBOL(lnet_kiov_nob);
383 lnet_copy_kiov2kiov(unsigned int ndiov, struct bio_vec *diov,
384 unsigned int doffset,
385 unsigned int nsiov, struct bio_vec *siov,
386 unsigned int soffset,
389 /* NB diov, siov are READ-ONLY */
390 unsigned int this_nob;
397 LASSERT (!in_interrupt ());
400 while (doffset >= diov->bv_len) {
401 doffset -= diov->bv_len;
408 while (soffset >= siov->bv_len) {
409 soffset -= siov->bv_len;
418 this_nob = min3(diov->bv_len - doffset,
419 siov->bv_len - soffset,
423 daddr = ((char *)kmap(diov->bv_page)) +
424 diov->bv_offset + doffset;
426 saddr = ((char *)kmap(siov->bv_page)) +
427 siov->bv_offset + soffset;
429 /* Vanishing risk of kmap deadlock when mapping 2 pages.
430 * However in practice at least one of the kiovs will be mapped
431 * kernel pages and the map/unmap will be NOOPs */
433 memcpy (daddr, saddr, this_nob);
436 if (diov->bv_len > doffset + this_nob) {
440 kunmap(diov->bv_page);
447 if (siov->bv_len > soffset + this_nob) {
451 kunmap(siov->bv_page);
460 kunmap(diov->bv_page);
462 kunmap(siov->bv_page);
464 EXPORT_SYMBOL(lnet_copy_kiov2kiov);
467 lnet_copy_kiov2iov (unsigned int niov, struct kvec *iov, unsigned int iovoffset,
468 unsigned int nkiov, struct bio_vec *kiov,
469 unsigned int kiovoffset,
472 /* NB iov, kiov are READ-ONLY */
473 unsigned int this_nob;
479 LASSERT (!in_interrupt ());
482 while (iovoffset >= iov->iov_len) {
483 iovoffset -= iov->iov_len;
490 while (kiovoffset >= kiov->bv_len) {
491 kiovoffset -= kiov->bv_len;
500 this_nob = min3((unsigned int)iov->iov_len - iovoffset,
501 (unsigned int)kiov->bv_len - kiovoffset,
505 addr = ((char *)kmap(kiov->bv_page)) +
506 kiov->bv_offset + kiovoffset;
508 memcpy((char *)iov->iov_base + iovoffset, addr, this_nob);
511 if (iov->iov_len > iovoffset + this_nob) {
512 iovoffset += this_nob;
519 if (kiov->bv_len > kiovoffset + this_nob) {
521 kiovoffset += this_nob;
523 kunmap(kiov->bv_page);
533 kunmap(kiov->bv_page);
535 EXPORT_SYMBOL(lnet_copy_kiov2iov);
538 lnet_copy_iov2kiov(unsigned int nkiov, struct bio_vec *kiov,
539 unsigned int kiovoffset,
540 unsigned int niov, struct kvec *iov, unsigned int iovoffset,
543 /* NB kiov, iov are READ-ONLY */
544 unsigned int this_nob;
550 LASSERT (!in_interrupt ());
553 while (kiovoffset >= kiov->bv_len) {
554 kiovoffset -= kiov->bv_len;
561 while (iovoffset >= iov->iov_len) {
562 iovoffset -= iov->iov_len;
571 this_nob = min3((unsigned int)kiov->bv_len - kiovoffset,
572 (unsigned int)iov->iov_len - iovoffset,
576 addr = ((char *)kmap(kiov->bv_page)) +
577 kiov->bv_offset + kiovoffset;
579 memcpy (addr, (char *)iov->iov_base + iovoffset, this_nob);
582 if (kiov->bv_len > kiovoffset + this_nob) {
584 kiovoffset += this_nob;
586 kunmap(kiov->bv_page);
593 if (iov->iov_len > iovoffset + this_nob) {
594 iovoffset += this_nob;
603 kunmap(kiov->bv_page);
605 EXPORT_SYMBOL(lnet_copy_iov2kiov);
608 lnet_extract_kiov(int dst_niov, struct bio_vec *dst,
609 int src_niov, struct bio_vec *src,
610 unsigned int offset, unsigned int len)
612 /* Initialise 'dst' to the subset of 'src' starting at 'offset',
613 * for exactly 'len' bytes, and return the number of entries.
614 * NB not destructive to 'src' */
615 unsigned int frag_len;
618 if (len == 0) /* no data => */
619 return (0); /* no frags */
621 LASSERT(src_niov > 0);
622 while (offset >= src->bv_len) { /* skip initial frags */
623 offset -= src->bv_len;
626 LASSERT(src_niov > 0);
631 LASSERT(src_niov > 0);
632 LASSERT((int)niov <= dst_niov);
634 frag_len = src->bv_len - offset;
635 dst->bv_page = src->bv_page;
636 dst->bv_offset = src->bv_offset + offset;
638 if (len <= frag_len) {
640 LASSERT(dst->bv_offset + dst->bv_len <= PAGE_SIZE);
644 dst->bv_len = frag_len;
645 LASSERT(dst->bv_offset + dst->bv_len <= PAGE_SIZE);
655 EXPORT_SYMBOL(lnet_extract_kiov);
658 lnet_ni_recv(struct lnet_ni *ni, void *private, struct lnet_msg *msg,
659 int delayed, unsigned int offset, unsigned int mlen,
662 unsigned int niov = 0;
663 struct kvec *iov = NULL;
664 struct bio_vec *kiov = NULL;
667 LASSERT (!in_interrupt ());
668 LASSERT (mlen == 0 || msg != NULL);
671 LASSERT(msg->msg_receiving);
672 LASSERT(!msg->msg_sending);
673 LASSERT(rlen == msg->msg_len);
674 LASSERT(mlen <= msg->msg_len);
675 LASSERT(msg->msg_offset == offset);
676 LASSERT(msg->msg_wanted == mlen);
678 msg->msg_receiving = 0;
681 niov = msg->msg_niov;
682 kiov = msg->msg_kiov;
685 LASSERT ((iov == NULL) != (kiov == NULL));
689 rc = (ni->ni_net->net_lnd->lnd_recv)(ni, private, msg, delayed,
690 niov, kiov, offset, mlen,
693 lnet_finalize(msg, rc);
697 lnet_setpayloadbuffer(struct lnet_msg *msg)
699 struct lnet_libmd *md = msg->msg_md;
701 LASSERT(msg->msg_len > 0);
702 LASSERT(!msg->msg_routing);
704 LASSERT(msg->msg_niov == 0);
705 LASSERT(msg->msg_kiov == NULL);
707 msg->msg_niov = md->md_niov;
708 msg->msg_kiov = md->md_kiov;
712 lnet_prep_send(struct lnet_msg *msg, int type, struct lnet_process_id target,
713 unsigned int offset, unsigned int len)
715 msg->msg_type = type;
716 msg->msg_target = target;
718 msg->msg_offset = offset;
721 lnet_setpayloadbuffer(msg);
723 memset (&msg->msg_hdr, 0, sizeof (msg->msg_hdr));
724 msg->msg_hdr.type = cpu_to_le32(type);
725 /* dest_nid will be overwritten by lnet_select_pathway() */
726 msg->msg_hdr.dest_nid = cpu_to_le64(target.nid);
727 msg->msg_hdr.dest_pid = cpu_to_le32(target.pid);
728 /* src_nid will be set later */
729 msg->msg_hdr.src_pid = cpu_to_le32(the_lnet.ln_pid);
730 msg->msg_hdr.payload_length = cpu_to_le32(len);
734 lnet_ni_send(struct lnet_ni *ni, struct lnet_msg *msg)
736 void *priv = msg->msg_private;
739 LASSERT(!in_interrupt());
740 LASSERT(nid_is_lo0(&ni->ni_nid) ||
741 (msg->msg_txcredit && msg->msg_peertxcredit));
743 rc = (ni->ni_net->net_lnd->lnd_send)(ni, priv, msg);
745 msg->msg_no_resend = true;
746 lnet_finalize(msg, rc);
751 lnet_ni_eager_recv(struct lnet_ni *ni, struct lnet_msg *msg)
755 LASSERT(!msg->msg_sending);
756 LASSERT(msg->msg_receiving);
757 LASSERT(!msg->msg_rx_ready_delay);
758 LASSERT(ni->ni_net->net_lnd->lnd_eager_recv != NULL);
760 msg->msg_rx_ready_delay = 1;
761 rc = (ni->ni_net->net_lnd->lnd_eager_recv)(ni, msg->msg_private, msg,
764 CERROR("recv from %s / send to %s aborted: "
765 "eager_recv failed %d\n",
766 libcfs_nidstr(&msg->msg_rxpeer->lpni_nid),
767 libcfs_id2str(msg->msg_target), rc);
768 LASSERT(rc < 0); /* required by my callers */
775 lnet_is_peer_deadline_passed(struct lnet_peer_ni *lpni, time64_t now)
779 deadline = lpni->lpni_last_alive +
780 lpni->lpni_net->net_tunables.lct_peer_timeout;
783 * assume peer_ni is alive as long as we're within the configured
792 /* NB: returns 1 when alive, 0 when dead, negative when error;
793 * may drop the lnet_net_lock */
795 lnet_peer_alive_locked(struct lnet_ni *ni, struct lnet_peer_ni *lpni,
796 struct lnet_msg *msg)
798 time64_t now = ktime_get_seconds();
800 if (!lnet_peer_aliveness_enabled(lpni))
804 * If we're resending a message, let's attempt to send it even if
805 * the peer is down to fulfill our resend quota on the message
807 if (msg->msg_retry_count > 0)
810 /* try and send recovery messages irregardless */
811 if (msg->msg_recovery)
814 /* always send any responses */
815 if (lnet_msg_is_response(msg))
818 if (!lnet_is_peer_deadline_passed(lpni, now))
821 return lnet_is_peer_ni_alive(lpni);
825 * \param msg The message to be sent.
826 * \param do_send True if lnet_ni_send() should be called in this function.
827 * lnet_send() is going to lnet_net_unlock immediately after this, so
828 * it sets do_send FALSE and I don't do the unlock/send/lock bit.
830 * \retval LNET_CREDIT_OK If \a msg sent or OK to send.
831 * \retval LNET_CREDIT_WAIT If \a msg blocked for credit.
832 * \retval -EHOSTUNREACH If the next hop of the message appears dead.
833 * \retval -ECANCELED If the MD of the message has been unlinked.
836 lnet_post_send_locked(struct lnet_msg *msg, int do_send)
838 struct lnet_peer_ni *lp = msg->msg_txpeer;
839 struct lnet_ni *ni = msg->msg_txni;
840 int cpt = msg->msg_tx_cpt;
841 struct lnet_tx_queue *tq = ni->ni_tx_queues[cpt];
843 /* non-lnet_send() callers have checked before */
844 LASSERT(!do_send || msg->msg_tx_delayed);
845 LASSERT(!msg->msg_receiving);
846 LASSERT(msg->msg_tx_committed);
848 /* can't get here if we're sending to the loopback interface */
849 if (the_lnet.ln_loni)
850 LASSERT(!nid_same(&lp->lpni_nid, &the_lnet.ln_loni->ni_nid));
852 /* NB 'lp' is always the next hop */
853 if ((msg->msg_target.pid & LNET_PID_USERFLAG) == 0 &&
854 lnet_peer_alive_locked(ni, lp, msg) == 0) {
855 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
856 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length +=
858 lnet_net_unlock(cpt);
860 lnet_incr_stats(&msg->msg_txpeer->lpni_stats,
862 LNET_STATS_TYPE_DROP);
864 lnet_incr_stats(&msg->msg_txni->ni_stats,
866 LNET_STATS_TYPE_DROP);
868 CNETERR("Dropping message for %s: peer not alive\n",
869 libcfs_id2str(msg->msg_target));
870 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_DROPPED;
872 lnet_finalize(msg, -EHOSTUNREACH);
875 return -EHOSTUNREACH;
878 if (msg->msg_md != NULL &&
879 (msg->msg_md->md_flags & LNET_MD_FLAG_ABORTED) != 0) {
880 lnet_net_unlock(cpt);
882 CNETERR("Aborting message for %s: LNetM[DE]Unlink() already "
883 "called on the MD/ME.\n",
884 libcfs_id2str(msg->msg_target));
886 msg->msg_no_resend = true;
887 CDEBUG(D_NET, "msg %p to %s canceled and will not be resent\n",
888 msg, libcfs_id2str(msg->msg_target));
889 lnet_finalize(msg, -ECANCELED);
896 if (!msg->msg_peertxcredit) {
897 spin_lock(&lp->lpni_lock);
898 LASSERT((lp->lpni_txcredits < 0) ==
899 !list_empty(&lp->lpni_txq));
901 msg->msg_peertxcredit = 1;
902 lp->lpni_txqnob += msg->msg_len + sizeof(struct lnet_hdr);
903 lp->lpni_txcredits--;
905 if (lp->lpni_txcredits < lp->lpni_mintxcredits)
906 lp->lpni_mintxcredits = lp->lpni_txcredits;
908 if (lp->lpni_txcredits < 0) {
909 msg->msg_tx_delayed = 1;
910 list_add_tail(&msg->msg_list, &lp->lpni_txq);
911 spin_unlock(&lp->lpni_lock);
912 return LNET_CREDIT_WAIT;
914 spin_unlock(&lp->lpni_lock);
917 if (!msg->msg_txcredit) {
918 LASSERT((tq->tq_credits < 0) ==
919 !list_empty(&tq->tq_delayed));
921 msg->msg_txcredit = 1;
923 atomic_dec(&ni->ni_tx_credits);
925 if (tq->tq_credits < tq->tq_credits_min)
926 tq->tq_credits_min = tq->tq_credits;
928 if (tq->tq_credits < 0) {
929 msg->msg_tx_delayed = 1;
930 list_add_tail(&msg->msg_list, &tq->tq_delayed);
931 return LNET_CREDIT_WAIT;
935 if (unlikely(!list_empty(&the_lnet.ln_delay_rules)) &&
936 lnet_delay_rule_match_locked(&msg->msg_hdr, msg)) {
937 msg->msg_tx_delayed = 1;
938 return LNET_CREDIT_WAIT;
941 /* unset the tx_delay flag as we're going to send it now */
942 msg->msg_tx_delayed = 0;
945 lnet_net_unlock(cpt);
946 lnet_ni_send(ni, msg);
949 return LNET_CREDIT_OK;
953 static struct lnet_rtrbufpool *
954 lnet_msg2bufpool(struct lnet_msg *msg)
956 struct lnet_rtrbufpool *rbp;
959 LASSERT(msg->msg_rx_committed);
961 cpt = msg->msg_rx_cpt;
962 rbp = &the_lnet.ln_rtrpools[cpt][0];
964 LASSERT(msg->msg_len <= LNET_MTU);
965 while (msg->msg_len > (unsigned int)rbp->rbp_npages * PAGE_SIZE) {
967 LASSERT(rbp < &the_lnet.ln_rtrpools[cpt][LNET_NRBPOOLS]);
974 lnet_post_routed_recv_locked(struct lnet_msg *msg, int do_recv)
976 /* lnet_parse is going to lnet_net_unlock immediately after this, so it
977 * sets do_recv FALSE and I don't do the unlock/send/lock bit.
978 * I return LNET_CREDIT_WAIT if msg blocked and LNET_CREDIT_OK if
979 * received or OK to receive */
980 struct lnet_peer_ni *lpni = msg->msg_rxpeer;
981 struct lnet_peer *lp;
982 struct lnet_rtrbufpool *rbp;
983 struct lnet_rtrbuf *rb;
985 LASSERT(msg->msg_kiov == NULL);
986 LASSERT(msg->msg_niov == 0);
987 LASSERT(msg->msg_routing);
988 LASSERT(msg->msg_receiving);
989 LASSERT(!msg->msg_sending);
990 LASSERT(lpni->lpni_peer_net);
991 LASSERT(lpni->lpni_peer_net->lpn_peer);
993 lp = lpni->lpni_peer_net->lpn_peer;
995 /* non-lnet_parse callers only receive delayed messages */
996 LASSERT(!do_recv || msg->msg_rx_delayed);
998 if (!msg->msg_peerrtrcredit) {
999 /* lpni_lock protects the credit manipulation */
1000 spin_lock(&lpni->lpni_lock);
1002 msg->msg_peerrtrcredit = 1;
1003 lpni->lpni_rtrcredits--;
1004 if (lpni->lpni_rtrcredits < lpni->lpni_minrtrcredits)
1005 lpni->lpni_minrtrcredits = lpni->lpni_rtrcredits;
1007 if (lpni->lpni_rtrcredits < 0) {
1008 spin_unlock(&lpni->lpni_lock);
1009 /* must have checked eager_recv before here */
1010 LASSERT(msg->msg_rx_ready_delay);
1011 msg->msg_rx_delayed = 1;
1012 /* lp_lock protects the lp_rtrq */
1013 spin_lock(&lp->lp_lock);
1014 list_add_tail(&msg->msg_list, &lp->lp_rtrq);
1015 spin_unlock(&lp->lp_lock);
1016 return LNET_CREDIT_WAIT;
1018 spin_unlock(&lpni->lpni_lock);
1021 rbp = lnet_msg2bufpool(msg);
1023 if (!msg->msg_rtrcredit) {
1024 msg->msg_rtrcredit = 1;
1026 if (rbp->rbp_credits < rbp->rbp_mincredits)
1027 rbp->rbp_mincredits = rbp->rbp_credits;
1029 if (rbp->rbp_credits < 0) {
1030 /* must have checked eager_recv before here */
1031 LASSERT(msg->msg_rx_ready_delay);
1032 msg->msg_rx_delayed = 1;
1033 list_add_tail(&msg->msg_list, &rbp->rbp_msgs);
1034 return LNET_CREDIT_WAIT;
1038 LASSERT(!list_empty(&rbp->rbp_bufs));
1039 rb = list_entry(rbp->rbp_bufs.next, struct lnet_rtrbuf, rb_list);
1040 list_del(&rb->rb_list);
1042 msg->msg_niov = rbp->rbp_npages;
1043 msg->msg_kiov = &rb->rb_kiov[0];
1045 /* unset the msg-rx_delayed flag since we're receiving the message */
1046 msg->msg_rx_delayed = 0;
1049 int cpt = msg->msg_rx_cpt;
1051 lnet_net_unlock(cpt);
1052 lnet_ni_recv(msg->msg_rxni, msg->msg_private, msg, 1,
1053 0, msg->msg_len, msg->msg_len);
1056 return LNET_CREDIT_OK;
1060 lnet_return_tx_credits_locked(struct lnet_msg *msg)
1062 struct lnet_peer_ni *txpeer = msg->msg_txpeer;
1063 struct lnet_ni *txni = msg->msg_txni;
1064 struct lnet_msg *msg2;
1066 if (msg->msg_txcredit) {
1067 struct lnet_ni *ni = msg->msg_txni;
1068 struct lnet_tx_queue *tq = ni->ni_tx_queues[msg->msg_tx_cpt];
1070 /* give back NI txcredits */
1071 msg->msg_txcredit = 0;
1073 LASSERT((tq->tq_credits < 0) ==
1074 !list_empty(&tq->tq_delayed));
1077 atomic_inc(&ni->ni_tx_credits);
1078 if (tq->tq_credits <= 0) {
1079 msg2 = list_entry(tq->tq_delayed.next,
1080 struct lnet_msg, msg_list);
1081 list_del(&msg2->msg_list);
1083 LASSERT(msg2->msg_txni == ni);
1084 LASSERT(msg2->msg_tx_delayed);
1085 LASSERT(msg2->msg_tx_cpt == msg->msg_tx_cpt);
1087 (void) lnet_post_send_locked(msg2, 1);
1091 if (msg->msg_peertxcredit) {
1092 /* give back peer txcredits */
1093 msg->msg_peertxcredit = 0;
1095 spin_lock(&txpeer->lpni_lock);
1096 LASSERT((txpeer->lpni_txcredits < 0) ==
1097 !list_empty(&txpeer->lpni_txq));
1099 txpeer->lpni_txqnob -= msg->msg_len + sizeof(struct lnet_hdr);
1100 LASSERT(txpeer->lpni_txqnob >= 0);
1102 txpeer->lpni_txcredits++;
1103 if (txpeer->lpni_txcredits <= 0) {
1106 msg2 = list_entry(txpeer->lpni_txq.next,
1107 struct lnet_msg, msg_list);
1108 list_del(&msg2->msg_list);
1109 spin_unlock(&txpeer->lpni_lock);
1111 LASSERT(msg2->msg_txpeer == txpeer);
1112 LASSERT(msg2->msg_tx_delayed);
1114 msg2_cpt = msg2->msg_tx_cpt;
1117 * The msg_cpt can be different from the msg2_cpt
1118 * so we need to make sure we lock the correct cpt
1120 * Once we call lnet_post_send_locked() it is no
1121 * longer safe to access msg2, since it could've
1122 * been freed by lnet_finalize(), but we still
1123 * need to relock the correct cpt, so we cache the
1124 * msg2_cpt for the purpose of the check that
1125 * follows the call to lnet_pose_send_locked().
1127 if (msg2_cpt != msg->msg_tx_cpt) {
1128 lnet_net_unlock(msg->msg_tx_cpt);
1129 lnet_net_lock(msg2_cpt);
1131 (void) lnet_post_send_locked(msg2, 1);
1132 if (msg2_cpt != msg->msg_tx_cpt) {
1133 lnet_net_unlock(msg2_cpt);
1134 lnet_net_lock(msg->msg_tx_cpt);
1137 spin_unlock(&txpeer->lpni_lock);
1142 msg->msg_txni = NULL;
1143 lnet_ni_decref_locked(txni, msg->msg_tx_cpt);
1146 if (txpeer != NULL) {
1147 msg->msg_txpeer = NULL;
1148 lnet_peer_ni_decref_locked(txpeer);
1153 lnet_schedule_blocked_locked(struct lnet_rtrbufpool *rbp)
1155 struct lnet_msg *msg;
1157 if (list_empty(&rbp->rbp_msgs))
1159 msg = list_entry(rbp->rbp_msgs.next,
1160 struct lnet_msg, msg_list);
1161 list_del(&msg->msg_list);
1163 (void)lnet_post_routed_recv_locked(msg, 1);
1167 lnet_drop_routed_msgs_locked(struct list_head *list, int cpt)
1169 struct lnet_msg *msg;
1170 struct lnet_msg *tmp;
1172 lnet_net_unlock(cpt);
1174 list_for_each_entry_safe(msg, tmp, list, msg_list) {
1175 lnet_ni_recv(msg->msg_rxni, msg->msg_private, NULL,
1176 0, 0, 0, msg->msg_hdr.payload_length);
1177 list_del_init(&msg->msg_list);
1178 msg->msg_no_resend = true;
1179 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_ERROR;
1180 lnet_finalize(msg, -ECANCELED);
1187 lnet_return_rx_credits_locked(struct lnet_msg *msg)
1189 struct lnet_peer_ni *rxpeerni = msg->msg_rxpeer;
1190 struct lnet_peer *lp;
1191 struct lnet_ni *rxni = msg->msg_rxni;
1192 struct lnet_msg *msg2;
1194 if (msg->msg_rtrcredit) {
1195 /* give back global router credits */
1196 struct lnet_rtrbuf *rb;
1197 struct lnet_rtrbufpool *rbp;
1199 /* NB If a msg ever blocks for a buffer in rbp_msgs, it stays
1200 * there until it gets one allocated, or aborts the wait
1202 LASSERT(msg->msg_kiov != NULL);
1204 rb = list_entry(msg->msg_kiov, struct lnet_rtrbuf, rb_kiov[0]);
1207 msg->msg_kiov = NULL;
1208 msg->msg_rtrcredit = 0;
1210 LASSERT(rbp == lnet_msg2bufpool(msg));
1212 LASSERT((rbp->rbp_credits > 0) ==
1213 !list_empty(&rbp->rbp_bufs));
1215 /* If routing is now turned off, we just drop this buffer and
1216 * don't bother trying to return credits. */
1217 if (!the_lnet.ln_routing) {
1218 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1222 /* It is possible that a user has lowered the desired number of
1223 * buffers in this pool. Make sure we never put back
1224 * more buffers than the stated number. */
1225 if (unlikely(rbp->rbp_credits >= rbp->rbp_req_nbuffers)) {
1226 /* Discard this buffer so we don't have too
1228 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1229 rbp->rbp_nbuffers--;
1231 list_add(&rb->rb_list, &rbp->rbp_bufs);
1233 if (rbp->rbp_credits <= 0)
1234 lnet_schedule_blocked_locked(rbp);
1239 if (msg->msg_peerrtrcredit) {
1241 LASSERT(rxpeerni->lpni_peer_net);
1242 LASSERT(rxpeerni->lpni_peer_net->lpn_peer);
1244 /* give back peer router credits */
1245 msg->msg_peerrtrcredit = 0;
1247 spin_lock(&rxpeerni->lpni_lock);
1248 rxpeerni->lpni_rtrcredits++;
1249 spin_unlock(&rxpeerni->lpni_lock);
1251 lp = rxpeerni->lpni_peer_net->lpn_peer;
1252 spin_lock(&lp->lp_lock);
1254 /* drop all messages which are queued to be routed on that
1256 if (!the_lnet.ln_routing) {
1258 list_splice_init(&lp->lp_rtrq, &drop);
1259 spin_unlock(&lp->lp_lock);
1260 lnet_drop_routed_msgs_locked(&drop, msg->msg_rx_cpt);
1261 } else if (!list_empty(&lp->lp_rtrq)) {
1264 msg2 = list_entry(lp->lp_rtrq.next,
1265 struct lnet_msg, msg_list);
1266 list_del(&msg2->msg_list);
1267 msg2_cpt = msg2->msg_rx_cpt;
1268 spin_unlock(&lp->lp_lock);
1270 * messages on the lp_rtrq can be from any NID in
1271 * the peer, which means they might have different
1272 * cpts. We need to make sure we lock the right
1275 if (msg2_cpt != msg->msg_rx_cpt) {
1276 lnet_net_unlock(msg->msg_rx_cpt);
1277 lnet_net_lock(msg2_cpt);
1279 (void) lnet_post_routed_recv_locked(msg2, 1);
1280 if (msg2_cpt != msg->msg_rx_cpt) {
1281 lnet_net_unlock(msg2_cpt);
1282 lnet_net_lock(msg->msg_rx_cpt);
1285 spin_unlock(&lp->lp_lock);
1289 msg->msg_rxni = NULL;
1290 lnet_ni_decref_locked(rxni, msg->msg_rx_cpt);
1292 if (rxpeerni != NULL) {
1293 msg->msg_rxpeer = NULL;
1294 lnet_peer_ni_decref_locked(rxpeerni);
1298 static struct lnet_peer_ni *
1299 lnet_select_peer_ni(struct lnet_ni *best_ni, lnet_nid_t dst_nid,
1300 struct lnet_peer *peer,
1301 struct lnet_peer_ni *best_lpni,
1302 struct lnet_peer_net *peer_net)
1305 * Look at the peer NIs for the destination peer that connect
1306 * to the chosen net. If a peer_ni is preferred when using the
1307 * best_ni to communicate, we use that one. If there is no
1308 * preferred peer_ni, or there are multiple preferred peer_ni,
1309 * the available transmit credits are used. If the transmit
1310 * credits are equal, we round-robin over the peer_ni.
1312 struct lnet_peer_ni *lpni = NULL;
1313 int best_lpni_credits = (best_lpni) ? best_lpni->lpni_txcredits :
1315 int best_lpni_healthv = (best_lpni) ?
1316 atomic_read(&best_lpni->lpni_healthv) : 0;
1317 bool best_lpni_is_preferred = false;
1318 bool lpni_is_preferred;
1320 __u32 lpni_sel_prio;
1321 __u32 best_sel_prio = LNET_MAX_SELECTION_PRIORITY;
1323 while ((lpni = lnet_get_next_peer_ni_locked(peer, peer_net, lpni))) {
1325 * if the best_ni we've chosen aleady has this lpni
1326 * preferred, then let's use it
1329 /* FIXME need to handle large-addr nid */
1330 lpni_is_preferred = lnet_peer_is_pref_nid_locked(
1331 lpni, lnet_nid_to_nid4(&best_ni->ni_nid));
1332 CDEBUG(D_NET, "%s lpni_is_preferred = %d\n",
1333 libcfs_nidstr(&best_ni->ni_nid),
1336 lpni_is_preferred = false;
1339 lpni_healthv = atomic_read(&lpni->lpni_healthv);
1340 lpni_sel_prio = lpni->lpni_sel_priority;
1343 CDEBUG(D_NET, "n:[%s, %s] h:[%d, %d] p:[%d, %d] c:[%d, %d] s:[%d, %d]\n",
1344 libcfs_nidstr(&lpni->lpni_nid),
1345 libcfs_nidstr(&best_lpni->lpni_nid),
1346 lpni_healthv, best_lpni_healthv,
1347 lpni_sel_prio, best_sel_prio,
1348 lpni->lpni_txcredits, best_lpni_credits,
1349 lpni->lpni_seq, best_lpni->lpni_seq);
1353 /* pick the healthiest peer ni */
1354 if (lpni_healthv < best_lpni_healthv)
1356 else if (lpni_healthv > best_lpni_healthv) {
1357 if (best_lpni_is_preferred)
1358 best_lpni_is_preferred = false;
1362 if (lpni_sel_prio > best_sel_prio)
1364 else if (lpni_sel_prio < best_sel_prio) {
1365 if (best_lpni_is_preferred)
1366 best_lpni_is_preferred = false;
1370 /* if this is a preferred peer use it */
1371 if (!best_lpni_is_preferred && lpni_is_preferred) {
1372 best_lpni_is_preferred = true;
1374 } else if (best_lpni_is_preferred && !lpni_is_preferred) {
1375 /* this is not the preferred peer so let's ignore
1381 if (lpni->lpni_txcredits < best_lpni_credits)
1382 /* We already have a peer that has more credits
1383 * available than this one. No need to consider
1384 * this peer further.
1387 else if (lpni->lpni_txcredits > best_lpni_credits)
1390 /* The best peer found so far and the current peer
1391 * have the same number of available credits let's
1392 * make sure to select between them using Round Robin
1394 if (best_lpni && (best_lpni->lpni_seq <= lpni->lpni_seq))
1397 best_lpni_is_preferred = lpni_is_preferred;
1398 best_lpni_healthv = lpni_healthv;
1399 best_sel_prio = lpni_sel_prio;
1401 best_lpni_credits = lpni->lpni_txcredits;
1404 /* if we still can't find a peer ni then we can't reach it */
1406 __u32 net_id = (peer_net) ? peer_net->lpn_net_id :
1407 LNET_NIDNET(dst_nid);
1408 CDEBUG(D_NET, "no peer_ni found on peer net %s\n",
1409 libcfs_net2str(net_id));
1413 CDEBUG(D_NET, "sd_best_lpni = %s\n",
1414 libcfs_nidstr(&best_lpni->lpni_nid));
1420 * Prerequisite: the best_ni should already be set in the sd
1421 * Find the best lpni.
1422 * If the net id is provided then restrict lpni selection on
1423 * that particular net.
1424 * Otherwise find any reachable lpni. When dealing with an MR
1425 * gateway and it has multiple lpnis which we can use
1426 * we want to select the best one from the list of reachable
1429 static inline struct lnet_peer_ni *
1430 lnet_find_best_lpni(struct lnet_ni *lni, lnet_nid_t dst_nid,
1431 struct lnet_peer *peer, __u32 net_id)
1433 struct lnet_peer_net *peer_net;
1435 /* find the best_lpni on any local network */
1436 if (net_id == LNET_NET_ANY) {
1437 struct lnet_peer_ni *best_lpni = NULL;
1438 struct lnet_peer_net *lpn;
1439 list_for_each_entry(lpn, &peer->lp_peer_nets, lpn_peer_nets) {
1440 /* no net specified find any reachable peer ni */
1441 if (!lnet_islocalnet_locked(lpn->lpn_net_id))
1443 best_lpni = lnet_select_peer_ni(lni, dst_nid, peer,
1449 /* restrict on the specified net */
1450 peer_net = lnet_peer_get_net_locked(peer, net_id);
1452 return lnet_select_peer_ni(lni, dst_nid, peer, NULL, peer_net);
1458 lnet_compare_gw_lpnis(struct lnet_peer_ni *lpni1, struct lnet_peer_ni *lpni2)
1460 if (lpni1->lpni_txqnob < lpni2->lpni_txqnob)
1463 if (lpni1->lpni_txqnob > lpni2->lpni_txqnob)
1466 if (lpni1->lpni_txcredits > lpni2->lpni_txcredits)
1469 if (lpni1->lpni_txcredits < lpni2->lpni_txcredits)
1475 /* Compare route priorities and hop counts */
1477 lnet_compare_routes(struct lnet_route *r1, struct lnet_route *r2)
1479 int r1_hops = (r1->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r1->lr_hops;
1480 int r2_hops = (r2->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r2->lr_hops;
1482 if (r1->lr_priority < r2->lr_priority)
1485 if (r1->lr_priority > r2->lr_priority)
1488 if (r1_hops < r2_hops)
1491 if (r1_hops > r2_hops)
1497 static struct lnet_route *
1498 lnet_find_route_locked(struct lnet_remotenet *rnet, __u32 src_net,
1499 struct lnet_peer_ni *remote_lpni,
1500 struct lnet_route **prev_route,
1501 struct lnet_peer_ni **gwni)
1503 struct lnet_peer_ni *lpni, *best_gw_ni = NULL;
1504 struct lnet_route *best_route;
1505 struct lnet_route *last_route;
1506 struct lnet_route *route;
1508 bool best_rte_is_preferred = false;
1511 CDEBUG(D_NET, "Looking up a route to %s, from %s\n",
1512 libcfs_net2str(rnet->lrn_net), libcfs_net2str(src_net));
1514 best_route = last_route = NULL;
1515 list_for_each_entry(route, &rnet->lrn_routes, lr_list) {
1516 if (!lnet_is_route_alive(route))
1518 gw_pnid = lnet_nid_to_nid4(&route->lr_gateway->lp_primary_nid);
1520 /* no protection on below fields, but it's harmless */
1521 if (last_route && (last_route->lr_seq - route->lr_seq < 0))
1524 /* if the best route found is in the preferred list then
1525 * tag it as preferred and use it later on. But if we
1526 * didn't find any routes which are on the preferred list
1527 * then just use the best route possible.
1529 rc = lnet_peer_is_pref_rtr_locked(remote_lpni, gw_pnid);
1531 if (!best_route || (rc && !best_rte_is_preferred)) {
1532 /* Restrict the selection of the router NI on the
1533 * src_net provided. If the src_net is LNET_NID_ANY,
1534 * then select the best interface available.
1536 lpni = lnet_find_best_lpni(NULL, LNET_NID_ANY,
1541 "Gateway %s does not have a peer NI on net %s\n",
1542 libcfs_nid2str(gw_pnid),
1543 libcfs_net2str(src_net));
1548 if (rc && !best_rte_is_preferred) {
1549 /* This is the first preferred route we found,
1550 * so it beats any route found previously
1556 best_rte_is_preferred = true;
1557 CDEBUG(D_NET, "preferred gw = %s\n",
1558 libcfs_nid2str(gw_pnid));
1560 } else if ((!rc) && best_rte_is_preferred)
1561 /* The best route we found so far is in the preferred
1562 * list, so it beats any non-preferred route
1567 best_route = last_route = route;
1572 rc = lnet_compare_routes(route, best_route);
1576 /* Restrict the selection of the router NI on the
1577 * src_net provided. If the src_net is LNET_NID_ANY,
1578 * then select the best interface available.
1580 lpni = lnet_find_best_lpni(NULL, LNET_NID_ANY,
1585 "Gateway %s does not have a peer NI on net %s\n",
1586 libcfs_nid2str(gw_pnid),
1587 libcfs_net2str(src_net));
1597 rc = lnet_compare_gw_lpnis(lpni, best_gw_ni);
1601 if (rc == 1 || route->lr_seq <= best_route->lr_seq) {
1608 *prev_route = last_route;
1614 static inline unsigned int
1615 lnet_dev_prio_of_md(struct lnet_ni *ni, unsigned int dev_idx)
1617 if (dev_idx == UINT_MAX)
1620 if (!ni || !ni->ni_net || !ni->ni_net->net_lnd ||
1621 !ni->ni_net->net_lnd->lnd_get_dev_prio)
1624 return ni->ni_net->net_lnd->lnd_get_dev_prio(ni, dev_idx);
1627 static struct lnet_ni *
1628 lnet_get_best_ni(struct lnet_net *local_net, struct lnet_ni *best_ni,
1629 struct lnet_peer *peer, struct lnet_peer_net *peer_net,
1630 struct lnet_msg *msg, int md_cpt)
1632 struct lnet_libmd *md = msg->msg_md;
1633 unsigned int offset = msg->msg_offset;
1634 unsigned int shortest_distance;
1635 struct lnet_ni *ni = NULL;
1638 __u32 best_sel_prio;
1639 unsigned int best_dev_prio;
1640 unsigned int dev_idx = UINT_MAX;
1641 struct page *page = lnet_get_first_page(md, offset);
1642 msg->msg_rdma_force = lnet_is_rdma_only_page(page);
1644 if (msg->msg_rdma_force)
1645 dev_idx = lnet_get_dev_idx(page);
1648 * If there is no peer_ni that we can send to on this network,
1649 * then there is no point in looking for a new best_ni here.
1651 if (!lnet_get_next_peer_ni_locked(peer, peer_net, NULL))
1654 if (best_ni == NULL) {
1655 best_sel_prio = LNET_MAX_SELECTION_PRIORITY;
1656 shortest_distance = UINT_MAX;
1657 best_dev_prio = UINT_MAX;
1658 best_credits = INT_MIN;
1661 best_dev_prio = lnet_dev_prio_of_md(best_ni, dev_idx);
1662 shortest_distance = cfs_cpt_distance(lnet_cpt_table(), md_cpt,
1663 best_ni->ni_dev_cpt);
1664 best_credits = atomic_read(&best_ni->ni_tx_credits);
1665 best_healthv = atomic_read(&best_ni->ni_healthv);
1666 best_sel_prio = best_ni->ni_sel_priority;
1669 while ((ni = lnet_get_next_ni_locked(local_net, ni))) {
1670 unsigned int distance;
1675 unsigned int ni_dev_prio;
1677 ni_credits = atomic_read(&ni->ni_tx_credits);
1678 ni_healthv = atomic_read(&ni->ni_healthv);
1679 ni_fatal = atomic_read(&ni->ni_fatal_error_on);
1680 ni_sel_prio = ni->ni_sel_priority;
1683 * calculate the distance from the CPT on which
1684 * the message memory is allocated to the CPT of
1685 * the NI's physical device
1687 distance = cfs_cpt_distance(lnet_cpt_table(),
1691 ni_dev_prio = lnet_dev_prio_of_md(ni, dev_idx);
1694 * All distances smaller than the NUMA range
1695 * are treated equally.
1697 if (distance < lnet_numa_range)
1698 distance = lnet_numa_range;
1701 * Select on health, selection policy, direct dma prio,
1702 * shorter distance, available credits, then round-robin.
1708 CDEBUG(D_NET, "compare ni %s [c:%d, d:%d, s:%d, p:%u, g:%u] with best_ni %s [c:%d, d:%d, s:%d, p:%u, g:%u]\n",
1709 libcfs_nidstr(&ni->ni_nid), ni_credits, distance,
1710 ni->ni_seq, ni_sel_prio, ni_dev_prio,
1711 (best_ni) ? libcfs_nidstr(&best_ni->ni_nid)
1712 : "not selected", best_credits, shortest_distance,
1713 (best_ni) ? best_ni->ni_seq : 0,
1714 best_sel_prio, best_dev_prio);
1718 if (ni_healthv < best_healthv)
1720 else if (ni_healthv > best_healthv)
1723 if (ni_sel_prio > best_sel_prio)
1725 else if (ni_sel_prio < best_sel_prio)
1728 if (ni_dev_prio > best_dev_prio)
1730 else if (ni_dev_prio < best_dev_prio)
1733 if (distance > shortest_distance)
1735 else if (distance < shortest_distance)
1738 if (ni_credits < best_credits)
1740 else if (ni_credits > best_credits)
1743 if (best_ni && best_ni->ni_seq <= ni->ni_seq)
1747 best_sel_prio = ni_sel_prio;
1748 best_dev_prio = ni_dev_prio;
1749 shortest_distance = distance;
1750 best_healthv = ni_healthv;
1752 best_credits = ni_credits;
1755 CDEBUG(D_NET, "selected best_ni %s\n",
1756 (best_ni) ? libcfs_nidstr(&best_ni->ni_nid) : "no selection");
1762 * Traffic to the LNET_RESERVED_PORTAL may not trigger peer discovery,
1763 * because such traffic is required to perform discovery. We therefore
1764 * exclude all GET and PUT on that portal. We also exclude all ACK and
1765 * REPLY traffic, but that is because the portal is not tracked in the
1766 * message structure for these message types. We could restrict this
1767 * further by also checking for LNET_PROTO_PING_MATCHBITS.
1770 lnet_msg_discovery(struct lnet_msg *msg)
1772 if (msg->msg_type == LNET_MSG_PUT) {
1773 if (msg->msg_hdr.msg.put.ptl_index != LNET_RESERVED_PORTAL)
1775 } else if (msg->msg_type == LNET_MSG_GET) {
1776 if (msg->msg_hdr.msg.get.ptl_index != LNET_RESERVED_PORTAL)
1782 #define SRC_SPEC 0x0001
1783 #define SRC_ANY 0x0002
1784 #define LOCAL_DST 0x0004
1785 #define REMOTE_DST 0x0008
1786 #define MR_DST 0x0010
1787 #define NMR_DST 0x0020
1788 #define SND_RESP 0x0040
1790 /* The following to defines are used for return codes */
1791 #define REPEAT_SEND 0x1000
1792 #define PASS_THROUGH 0x2000
1794 /* The different cases lnet_select pathway needs to handle */
1795 #define SRC_SPEC_LOCAL_MR_DST (SRC_SPEC | LOCAL_DST | MR_DST)
1796 #define SRC_SPEC_ROUTER_MR_DST (SRC_SPEC | REMOTE_DST | MR_DST)
1797 #define SRC_SPEC_LOCAL_NMR_DST (SRC_SPEC | LOCAL_DST | NMR_DST)
1798 #define SRC_SPEC_ROUTER_NMR_DST (SRC_SPEC | REMOTE_DST | NMR_DST)
1799 #define SRC_ANY_LOCAL_MR_DST (SRC_ANY | LOCAL_DST | MR_DST)
1800 #define SRC_ANY_ROUTER_MR_DST (SRC_ANY | REMOTE_DST | MR_DST)
1801 #define SRC_ANY_LOCAL_NMR_DST (SRC_ANY | LOCAL_DST | NMR_DST)
1802 #define SRC_ANY_ROUTER_NMR_DST (SRC_ANY | REMOTE_DST | NMR_DST)
1805 lnet_handle_lo_send(struct lnet_send_data *sd)
1807 struct lnet_msg *msg = sd->sd_msg;
1808 int cpt = sd->sd_cpt;
1810 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1813 /* No send credit hassles with LOLND */
1814 lnet_ni_addref_locked(the_lnet.ln_loni, cpt);
1815 msg->msg_hdr.dest_nid =
1816 cpu_to_le64(lnet_nid_to_nid4(&the_lnet.ln_loni->ni_nid));
1817 if (!msg->msg_routing)
1818 msg->msg_hdr.src_nid =
1819 cpu_to_le64(lnet_nid_to_nid4(&the_lnet.ln_loni->ni_nid));
1820 msg->msg_target.nid = lnet_nid_to_nid4(&the_lnet.ln_loni->ni_nid);
1821 lnet_msg_commit(msg, cpt);
1822 msg->msg_txni = the_lnet.ln_loni;
1824 return LNET_CREDIT_OK;
1828 lnet_handle_send(struct lnet_send_data *sd)
1830 struct lnet_ni *best_ni = sd->sd_best_ni;
1831 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
1832 struct lnet_peer_ni *final_dst_lpni = sd->sd_final_dst_lpni;
1833 struct lnet_msg *msg = sd->sd_msg;
1835 __u32 send_case = sd->sd_send_case;
1837 __u32 routing = send_case & REMOTE_DST;
1838 struct lnet_rsp_tracker *rspt;
1840 /* Increment sequence number of the selected peer, peer net,
1841 * local ni and local net so that we pick the next ones
1844 best_lpni->lpni_peer_net->lpn_seq++;
1845 best_lpni->lpni_seq = best_lpni->lpni_peer_net->lpn_seq;
1846 best_ni->ni_net->net_seq++;
1847 best_ni->ni_seq = best_ni->ni_net->net_seq;
1849 CDEBUG(D_NET, "%s NI seq info: [%d:%d:%d:%u] %s LPNI seq info [%d:%d:%d:%u]\n",
1850 libcfs_nidstr(&best_ni->ni_nid),
1851 best_ni->ni_seq, best_ni->ni_net->net_seq,
1852 atomic_read(&best_ni->ni_tx_credits),
1853 best_ni->ni_sel_priority,
1854 libcfs_nidstr(&best_lpni->lpni_nid),
1855 best_lpni->lpni_seq, best_lpni->lpni_peer_net->lpn_seq,
1856 best_lpni->lpni_txcredits,
1857 best_lpni->lpni_sel_priority);
1860 * grab a reference on the peer_ni so it sticks around even if
1861 * we need to drop and relock the lnet_net_lock below.
1863 lnet_peer_ni_addref_locked(best_lpni);
1866 * Use lnet_cpt_of_nid() to determine the CPT used to commit the
1867 * message. This ensures that we get a CPT that is correct for
1868 * the NI when the NI has been restricted to a subset of all CPTs.
1869 * If the selected CPT differs from the one currently locked, we
1870 * must unlock and relock the lnet_net_lock(), and then check whether
1871 * the configuration has changed. We don't have a hold on the best_ni
1872 * yet, and it may have vanished.
1874 cpt2 = lnet_cpt_of_nid_locked(&best_lpni->lpni_nid, best_ni);
1875 if (sd->sd_cpt != cpt2) {
1876 __u32 seq = lnet_get_dlc_seq_locked();
1877 lnet_net_unlock(sd->sd_cpt);
1879 lnet_net_lock(sd->sd_cpt);
1880 if (seq != lnet_get_dlc_seq_locked()) {
1881 lnet_peer_ni_decref_locked(best_lpni);
1887 * store the best_lpni in the message right away to avoid having
1888 * to do the same operation under different conditions
1890 msg->msg_txpeer = best_lpni;
1891 msg->msg_txni = best_ni;
1894 * grab a reference for the best_ni since now it's in use in this
1895 * send. The reference will be dropped in lnet_finalize()
1897 lnet_ni_addref_locked(msg->msg_txni, sd->sd_cpt);
1900 * Always set the target.nid to the best peer picked. Either the
1901 * NID will be one of the peer NIDs selected, or the same NID as
1902 * what was originally set in the target or it will be the NID of
1903 * a router if this message should be routed
1905 /* FIXME handle large-addr nids */
1906 msg->msg_target.nid = lnet_nid_to_nid4(&msg->msg_txpeer->lpni_nid);
1909 * lnet_msg_commit assigns the correct cpt to the message, which
1910 * is used to decrement the correct refcount on the ni when it's
1911 * time to return the credits
1913 lnet_msg_commit(msg, sd->sd_cpt);
1916 * If we are routing the message then we keep the src_nid that was
1917 * set by the originator. If we are not routing then we are the
1918 * originator and set it here.
1920 if (!msg->msg_routing)
1921 msg->msg_hdr.src_nid =
1922 cpu_to_le64(lnet_nid_to_nid4(&msg->msg_txni->ni_nid));
1925 msg->msg_target_is_router = 1;
1926 msg->msg_target.pid = LNET_PID_LUSTRE;
1928 * since we're routing we want to ensure that the
1929 * msg_hdr.dest_nid is set to the final destination. When
1930 * the router receives this message it knows how to route
1933 * final_dst_lpni is set at the beginning of the
1934 * lnet_select_pathway() function and is never changed.
1935 * It's safe to use it here.
1937 /* FIXME handle large-addr nid */
1938 msg->msg_hdr.dest_nid =
1939 cpu_to_le64(lnet_nid_to_nid4(&final_dst_lpni->lpni_nid));
1942 * if we're not routing set the dest_nid to the best peer
1943 * ni NID that we picked earlier in the algorithm.
1945 msg->msg_hdr.dest_nid =
1946 cpu_to_le64(lnet_nid_to_nid4(&msg->msg_txpeer->lpni_nid));
1950 * if we have response tracker block update it with the next hop
1954 rspt = msg->msg_md->md_rspt_ptr;
1956 rspt->rspt_next_hop_nid =
1957 msg->msg_txpeer->lpni_nid;
1958 CDEBUG(D_NET, "rspt_next_hop_nid = %s\n",
1959 libcfs_nidstr(&rspt->rspt_next_hop_nid));
1963 rc = lnet_post_send_locked(msg, 0);
1966 CDEBUG(D_NET, "TRACE: %s(%s:%s) -> %s(%s:%s) %s : %s try# %d\n",
1967 libcfs_nid2str(msg->msg_hdr.src_nid),
1968 libcfs_nidstr(&msg->msg_txni->ni_nid),
1969 libcfs_nid2str(sd->sd_src_nid),
1970 libcfs_nid2str(msg->msg_hdr.dest_nid),
1971 libcfs_nid2str(sd->sd_dst_nid),
1972 libcfs_nidstr(&msg->msg_txpeer->lpni_nid),
1973 libcfs_nid2str(sd->sd_rtr_nid),
1974 lnet_msgtyp2str(msg->msg_type), msg->msg_retry_count);
1980 lnet_set_non_mr_pref_nid(struct lnet_peer_ni *lpni, struct lnet_ni *lni,
1981 struct lnet_msg *msg)
1983 if (!lnet_peer_is_multi_rail(lpni->lpni_peer_net->lpn_peer) &&
1984 !lnet_msg_is_response(msg) && lpni->lpni_pref_nnids == 0) {
1985 CDEBUG(D_NET, "Setting preferred local NID %s on NMR peer %s\n",
1986 libcfs_nidstr(&lni->ni_nid),
1987 libcfs_nidstr(&lpni->lpni_nid));
1988 lnet_peer_ni_set_non_mr_pref_nid(
1989 lpni, lnet_nid_to_nid4(&lni->ni_nid));
1998 * use the source and destination NIDs as the pathway
2001 lnet_handle_spec_local_nmr_dst(struct lnet_send_data *sd)
2003 /* the destination lpni is set before we get here. */
2006 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
2007 if (!sd->sd_best_ni) {
2008 CERROR("Can't send to %s: src %s is not a "
2009 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
2010 libcfs_nid2str(sd->sd_src_nid));
2014 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
2016 return lnet_handle_send(sd);
2024 * Don't run the selection algorithm on the peer NIs. By specifying the
2025 * local NID, we're also saying that we should always use the destination NID
2026 * provided. This handles the case where we should be using the same
2027 * destination NID for the all the messages which belong to the same RPC
2031 lnet_handle_spec_local_mr_dst(struct lnet_send_data *sd)
2033 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
2034 if (!sd->sd_best_ni) {
2035 CERROR("Can't send to %s: src %s is not a "
2036 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
2037 libcfs_nid2str(sd->sd_src_nid));
2041 if (sd->sd_best_lpni &&
2042 nid_same(&sd->sd_best_lpni->lpni_nid,
2043 &the_lnet.ln_loni->ni_nid))
2044 return lnet_handle_lo_send(sd);
2045 else if (sd->sd_best_lpni)
2046 return lnet_handle_send(sd);
2048 CERROR("can't send to %s. no NI on %s\n",
2049 libcfs_nid2str(sd->sd_dst_nid),
2050 libcfs_net2str(sd->sd_best_ni->ni_net->net_id));
2052 return -EHOSTUNREACH;
2056 lnet_find_best_ni_on_spec_net(struct lnet_ni *cur_best_ni,
2057 struct lnet_peer *peer,
2058 struct lnet_peer_net *peer_net,
2059 struct lnet_msg *msg,
2062 struct lnet_net *local_net;
2063 struct lnet_ni *best_ni;
2065 local_net = lnet_get_net_locked(peer_net->lpn_net_id);
2070 * Iterate through the NIs in this local Net and select
2071 * the NI to send from. The selection is determined by
2072 * these 3 criterion in the following priority:
2074 * 2. NI available credits
2077 best_ni = lnet_get_best_ni(local_net, cur_best_ni,
2078 peer, peer_net, msg, cpt);
2084 lnet_initiate_peer_discovery(struct lnet_peer_ni *lpni, struct lnet_msg *msg,
2087 struct lnet_peer *peer;
2088 struct lnet_peer_ni *new_lpni;
2091 lnet_peer_ni_addref_locked(lpni);
2093 peer = lpni->lpni_peer_net->lpn_peer;
2095 if (lnet_peer_gw_discovery(peer)) {
2096 lnet_peer_ni_decref_locked(lpni);
2100 if (!lnet_msg_discovery(msg) || lnet_peer_is_uptodate(peer)) {
2101 lnet_peer_ni_decref_locked(lpni);
2105 rc = lnet_discover_peer_locked(lpni, cpt, false);
2107 lnet_peer_ni_decref_locked(lpni);
2111 new_lpni = lnet_find_peer_ni_locked(lnet_nid_to_nid4(&lpni->lpni_nid));
2113 lnet_peer_ni_decref_locked(lpni);
2117 peer = new_lpni->lpni_peer_net->lpn_peer;
2118 spin_lock(&peer->lp_lock);
2119 if (lpni == new_lpni && lnet_peer_is_uptodate_locked(peer)) {
2120 /* The peer NI did not change and the peer is up to date.
2121 * Nothing more to do.
2123 spin_unlock(&peer->lp_lock);
2124 lnet_peer_ni_decref_locked(lpni);
2125 lnet_peer_ni_decref_locked(new_lpni);
2128 spin_unlock(&peer->lp_lock);
2130 /* Either the peer NI changed during discovery, or the peer isn't up
2131 * to date. In both cases we want to queue the message on the
2132 * (possibly new) peer's pending queue and queue the peer for discovery
2134 msg->msg_sending = 0;
2135 msg->msg_txpeer = NULL;
2136 lnet_net_unlock(cpt);
2137 lnet_peer_queue_message(peer, msg);
2140 lnet_peer_ni_decref_locked(lpni);
2141 lnet_peer_ni_decref_locked(new_lpni);
2143 CDEBUG(D_NET, "msg %p delayed. %s pending discovery\n",
2144 msg, libcfs_nidstr(&peer->lp_primary_nid));
2146 return LNET_DC_WAIT;
2150 lnet_handle_find_routed_path(struct lnet_send_data *sd,
2152 struct lnet_peer_ni **gw_lpni,
2153 struct lnet_peer **gw_peer)
2156 struct lnet_peer *gw;
2157 struct lnet_peer *lp;
2158 struct lnet_peer_net *lpn;
2159 struct lnet_peer_net *best_lpn = NULL;
2160 struct lnet_remotenet *rnet, *best_rnet = NULL;
2161 struct lnet_route *best_route = NULL;
2162 struct lnet_route *last_route = NULL;
2163 struct lnet_peer_ni *lpni = NULL;
2164 struct lnet_peer_ni *gwni = NULL;
2165 bool route_found = false;
2166 lnet_nid_t src_nid = (sd->sd_src_nid != LNET_NID_ANY) ? sd->sd_src_nid :
2167 (sd->sd_best_ni != NULL)
2168 ? lnet_nid_to_nid4(&sd->sd_best_ni->ni_nid)
2170 int best_lpn_healthv = 0;
2171 __u32 best_lpn_sel_prio = LNET_MAX_SELECTION_PRIORITY;
2173 CDEBUG(D_NET, "using src nid %s for route restriction\n",
2174 libcfs_nid2str(src_nid));
2176 /* If a router nid was specified then we are replying to a GET or
2177 * sending an ACK. In this case we use the gateway associated with the
2178 * specified router nid.
2180 if (sd->sd_rtr_nid != LNET_NID_ANY) {
2181 gwni = lnet_find_peer_ni_locked(sd->sd_rtr_nid);
2183 gw = gwni->lpni_peer_net->lpn_peer;
2184 lnet_peer_ni_decref_locked(gwni);
2185 if (gw->lp_rtr_refcount)
2188 CWARN("No peer NI for gateway %s. Attempting to find an alternative route.\n",
2189 libcfs_nid2str(sd->sd_rtr_nid));
2194 if (sd->sd_msg->msg_routing) {
2195 /* If I'm routing this message then I need to find the
2196 * next hop based on the destination NID
2198 best_rnet = lnet_find_rnet_locked(LNET_NIDNET(sd->sd_dst_nid));
2200 CERROR("Unable to route message to %s - Route table may be misconfigured\n",
2201 libcfs_nid2str(sd->sd_dst_nid));
2202 return -EHOSTUNREACH;
2205 /* we've already looked up the initial lpni using
2208 lpni = sd->sd_best_lpni;
2209 /* the peer tree must be in existence */
2210 LASSERT(lpni && lpni->lpni_peer_net &&
2211 lpni->lpni_peer_net->lpn_peer);
2212 lp = lpni->lpni_peer_net->lpn_peer;
2214 list_for_each_entry(lpn, &lp->lp_peer_nets, lpn_peer_nets) {
2215 /* is this remote network reachable? */
2216 rnet = lnet_find_rnet_locked(lpn->lpn_net_id);
2225 /* select the preferred peer net */
2226 if (best_lpn_healthv > lpn->lpn_healthv)
2228 else if (best_lpn_healthv < lpn->lpn_healthv)
2231 if (best_lpn_sel_prio < lpn->lpn_sel_priority)
2233 else if (best_lpn_sel_prio > lpn->lpn_sel_priority)
2236 if (best_lpn->lpn_seq <= lpn->lpn_seq)
2239 best_lpn_healthv = lpn->lpn_healthv;
2240 best_lpn_sel_prio = lpn->lpn_sel_priority;
2246 CERROR("peer %s has no available nets\n",
2247 libcfs_nid2str(sd->sd_dst_nid));
2248 return -EHOSTUNREACH;
2251 sd->sd_best_lpni = lnet_find_best_lpni(sd->sd_best_ni,
2254 best_lpn->lpn_net_id);
2255 if (!sd->sd_best_lpni) {
2256 CERROR("peer %s is unreachable\n",
2257 libcfs_nid2str(sd->sd_dst_nid));
2258 return -EHOSTUNREACH;
2261 /* We're attempting to round robin over the remote peer
2262 * NI's so update the final destination we selected
2264 sd->sd_final_dst_lpni = sd->sd_best_lpni;
2266 /* Increment the sequence number of the remote lpni so
2267 * we can round robin over the different interfaces of
2270 sd->sd_best_lpni->lpni_seq++;
2274 * find the best route. Restrict the selection on the net of the
2275 * local NI if we've already picked the local NI to send from.
2276 * Otherwise, let's pick any route we can find and then find
2277 * a local NI we can reach the route's gateway on. Any route we
2278 * select will be reachable by virtue of the restriction we have
2279 * when adding a route.
2281 best_route = lnet_find_route_locked(best_rnet,
2282 LNET_NIDNET(src_nid),
2284 &last_route, &gwni);
2287 CERROR("no route to %s from %s\n",
2288 libcfs_nid2str(dst_nid),
2289 libcfs_nid2str(src_nid));
2290 return -EHOSTUNREACH;
2294 CERROR("Internal Error. Route expected to %s from %s\n",
2295 libcfs_nid2str(dst_nid),
2296 libcfs_nid2str(src_nid));
2300 gw = best_route->lr_gateway;
2301 LASSERT(gw == gwni->lpni_peer_net->lpn_peer);
2305 * Discover this gateway if it hasn't already been discovered.
2306 * This means we might delay the message until discovery has
2309 rc = lnet_initiate_peer_discovery(gwni, sd->sd_msg, sd->sd_cpt);
2313 if (!sd->sd_best_ni) {
2314 lpn = gwni->lpni_peer_net;
2315 sd->sd_best_ni = lnet_find_best_ni_on_spec_net(NULL, gw, lpn,
2318 if (!sd->sd_best_ni) {
2319 CERROR("Internal Error. Expected local ni on %s but non found: %s\n",
2320 libcfs_net2str(lpn->lpn_net_id),
2321 libcfs_nid2str(sd->sd_src_nid));
2330 * increment the sequence numbers since now we're sure we're
2331 * going to use this path
2333 if (sd->sd_rtr_nid == LNET_NID_ANY) {
2334 LASSERT(best_route && last_route);
2335 best_route->lr_seq = last_route->lr_seq + 1;
2337 best_lpn->lpn_seq++;
2348 * Remote destination
2349 * Non-MR destination
2353 * Remote destination
2356 * The handling of these two cases is similar. Even though the destination
2357 * can be MR or non-MR, we'll deal directly with the router.
2360 lnet_handle_spec_router_dst(struct lnet_send_data *sd)
2363 struct lnet_peer_ni *gw_lpni = NULL;
2364 struct lnet_peer *gw_peer = NULL;
2367 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
2368 if (!sd->sd_best_ni) {
2369 CERROR("Can't send to %s: src %s is not a "
2370 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
2371 libcfs_nid2str(sd->sd_src_nid));
2375 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2380 if (sd->sd_send_case & NMR_DST)
2382 * since the final destination is non-MR let's set its preferred
2383 * NID before we send
2385 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni,
2389 * We're going to send to the gw found so let's set its
2392 sd->sd_peer = gw_peer;
2393 sd->sd_best_lpni = gw_lpni;
2395 return lnet_handle_send(sd);
2399 lnet_find_best_ni_on_local_net(struct lnet_peer *peer, int md_cpt,
2400 struct lnet_msg *msg, bool discovery)
2402 struct lnet_peer_net *lpn = NULL;
2403 struct lnet_peer_net *best_lpn = NULL;
2404 struct lnet_net *net = NULL;
2405 struct lnet_net *best_net = NULL;
2406 struct lnet_ni *best_ni = NULL;
2407 int best_lpn_healthv = 0;
2408 int best_net_healthv = 0;
2410 __u32 best_lpn_sel_prio = LNET_MAX_SELECTION_PRIORITY;
2412 __u32 best_net_sel_prio = LNET_MAX_SELECTION_PRIORITY;
2417 * The peer can have multiple interfaces, some of them can be on
2418 * the local network and others on a routed network. We should
2419 * prefer the local network. However if the local network is not
2420 * available then we need to try the routed network
2423 /* go through all the peer nets and find the best_ni */
2424 list_for_each_entry(lpn, &peer->lp_peer_nets, lpn_peer_nets) {
2426 * The peer's list of nets can contain non-local nets. We
2427 * want to only examine the local ones.
2429 net = lnet_get_net_locked(lpn->lpn_net_id);
2433 lpn_sel_prio = lpn->lpn_sel_priority;
2434 net_healthv = lnet_get_net_healthv_locked(net);
2435 net_sel_prio = net->net_sel_priority;
2438 * if this is a discovery message and lp_disc_net_id is
2439 * specified then use that net to send the discovery on.
2441 if (peer->lp_disc_net_id == lpn->lpn_net_id &&
2450 /* always select the lpn with the best health */
2451 if (best_lpn_healthv > lpn->lpn_healthv)
2453 else if (best_lpn_healthv < lpn->lpn_healthv)
2456 /* select the preferred peer and local nets */
2457 if (best_lpn_sel_prio < lpn_sel_prio)
2459 else if (best_lpn_sel_prio > lpn_sel_prio)
2462 if (best_net_healthv > net_healthv)
2464 else if (best_net_healthv < net_healthv)
2467 if (best_net_sel_prio < net_sel_prio)
2469 else if (best_net_sel_prio > net_sel_prio)
2472 if (best_lpn->lpn_seq < lpn->lpn_seq)
2474 else if (best_lpn->lpn_seq > lpn->lpn_seq)
2477 /* round robin over the local networks */
2478 if (best_net->net_seq <= net->net_seq)
2482 best_net_healthv = net_healthv;
2483 best_net_sel_prio = net_sel_prio;
2484 best_lpn_healthv = lpn->lpn_healthv;
2485 best_lpn_sel_prio = lpn_sel_prio;
2494 /* Select the best NI on the same net as best_lpn chosen
2497 best_ni = lnet_find_best_ni_on_spec_net(NULL, peer, best_lpn,
2504 static struct lnet_ni *
2505 lnet_find_existing_preferred_best_ni(struct lnet_peer_ni *lpni, int cpt)
2507 struct lnet_ni *best_ni = NULL;
2508 struct lnet_peer_net *peer_net = lpni->lpni_peer_net;
2509 struct lnet_peer_ni *lpni_entry;
2512 * We must use a consistent source address when sending to a
2513 * non-MR peer. However, a non-MR peer can have multiple NIDs
2514 * on multiple networks, and we may even need to talk to this
2515 * peer on multiple networks -- certain types of
2516 * load-balancing configuration do this.
2518 * So we need to pick the NI the peer prefers for this
2519 * particular network.
2522 list_for_each_entry(lpni_entry, &peer_net->lpn_peer_nis,
2524 if (lpni_entry->lpni_pref_nnids == 0)
2526 LASSERT(lpni_entry->lpni_pref_nnids == 1);
2527 best_ni = lnet_nid2ni_locked(lpni_entry->lpni_pref.nid, cpt);
2534 /* Prerequisite: sd->sd_peer and sd->sd_best_lpni should be set */
2536 lnet_select_preferred_best_ni(struct lnet_send_data *sd)
2538 struct lnet_ni *best_ni = NULL;
2539 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
2542 * We must use a consistent source address when sending to a
2543 * non-MR peer. However, a non-MR peer can have multiple NIDs
2544 * on multiple networks, and we may even need to talk to this
2545 * peer on multiple networks -- certain types of
2546 * load-balancing configuration do this.
2548 * So we need to pick the NI the peer prefers for this
2549 * particular network.
2552 best_ni = lnet_find_existing_preferred_best_ni(sd->sd_best_lpni,
2555 /* if best_ni is still not set just pick one */
2558 lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2559 sd->sd_best_lpni->lpni_peer_net,
2562 /* If there is no best_ni we don't have a route */
2564 CERROR("no path to %s from net %s\n",
2565 libcfs_nidstr(&best_lpni->lpni_nid),
2566 libcfs_net2str(best_lpni->lpni_net->net_id));
2567 return -EHOSTUNREACH;
2571 sd->sd_best_ni = best_ni;
2573 /* Set preferred NI if necessary. */
2574 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
2581 * Source not specified
2585 * always use the same source NID for NMR peers
2586 * If we've talked to that peer before then we already have a preferred
2587 * source NI associated with it. Otherwise, we select a preferred local NI
2588 * and store it in the peer
2591 lnet_handle_any_local_nmr_dst(struct lnet_send_data *sd)
2595 /* sd->sd_best_lpni is already set to the final destination */
2598 * At this point we should've created the peer ni and peer. If we
2599 * can't find it, then something went wrong. Instead of assert
2600 * output a relevant message and fail the send
2602 if (!sd->sd_best_lpni) {
2603 CERROR("Internal fault. Unable to send msg %s to %s. "
2605 lnet_msgtyp2str(sd->sd_msg->msg_type),
2606 libcfs_nid2str(sd->sd_dst_nid));
2610 if (sd->sd_msg->msg_routing) {
2611 /* If I'm forwarding this message then I can choose any NI
2612 * on the destination peer net
2614 sd->sd_best_ni = lnet_find_best_ni_on_spec_net(NULL,
2616 sd->sd_best_lpni->lpni_peer_net,
2619 if (!sd->sd_best_ni) {
2620 CERROR("Unable to forward message to %s. No local NI available\n",
2621 libcfs_nid2str(sd->sd_dst_nid));
2625 rc = lnet_select_preferred_best_ni(sd);
2628 rc = lnet_handle_send(sd);
2634 lnet_handle_any_mr_dsta(struct lnet_send_data *sd)
2637 * NOTE we've already handled the remote peer case. So we only
2638 * need to worry about the local case here.
2640 * if we're sending a response, ACK or reply, we need to send it
2641 * to the destination NID given to us. At this point we already
2642 * have the peer_ni we're suppose to send to, so just find the
2643 * best_ni on the peer net and use that. Since we're sending to an
2644 * MR peer then we can just run the selection algorithm on our
2645 * local NIs and pick the best one.
2647 if (sd->sd_send_case & SND_RESP) {
2649 lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2650 sd->sd_best_lpni->lpni_peer_net,
2654 if (!sd->sd_best_ni) {
2656 * We're not going to deal with not able to send
2657 * a response to the provided final destination
2659 CERROR("Can't send response to %s. "
2660 "No local NI available\n",
2661 libcfs_nid2str(sd->sd_dst_nid));
2662 return -EHOSTUNREACH;
2665 return lnet_handle_send(sd);
2669 * If we get here that means we're sending a fresh request, PUT or
2670 * GET, so we need to run our standard selection algorithm.
2671 * First find the best local interface that's on any of the peer's
2674 sd->sd_best_ni = lnet_find_best_ni_on_local_net(sd->sd_peer,
2677 lnet_msg_discovery(sd->sd_msg));
2678 if (sd->sd_best_ni) {
2680 lnet_find_best_lpni(sd->sd_best_ni, sd->sd_dst_nid,
2682 sd->sd_best_ni->ni_net->net_id);
2685 * if we're successful in selecting a peer_ni on the local
2686 * network, then send to it. Otherwise fall through and
2687 * try and see if we can reach it over another routed
2690 if (sd->sd_best_lpni &&
2691 nid_same(&sd->sd_best_lpni->lpni_nid,
2692 &the_lnet.ln_loni->ni_nid)) {
2694 * in case we initially started with a routed
2695 * destination, let's reset to local
2697 sd->sd_send_case &= ~REMOTE_DST;
2698 sd->sd_send_case |= LOCAL_DST;
2699 return lnet_handle_lo_send(sd);
2700 } else if (sd->sd_best_lpni) {
2702 * in case we initially started with a routed
2703 * destination, let's reset to local
2705 sd->sd_send_case &= ~REMOTE_DST;
2706 sd->sd_send_case |= LOCAL_DST;
2707 return lnet_handle_send(sd);
2710 CERROR("Internal Error. Expected to have a best_lpni: "
2712 libcfs_nid2str(sd->sd_src_nid),
2713 libcfs_nid2str(sd->sd_dst_nid));
2719 * Peer doesn't have a local network. Let's see if there is
2720 * a remote network we can reach it on.
2722 return PASS_THROUGH;
2727 * Source NID not specified
2732 * Source NID not speified
2733 * Remote destination
2736 * In both of these cases if we're sending a response, ACK or REPLY, then
2737 * we need to send to the destination NID provided.
2739 * In the remote case let's deal with MR routers.
2744 lnet_handle_any_mr_dst(struct lnet_send_data *sd)
2747 struct lnet_peer *gw_peer = NULL;
2748 struct lnet_peer_ni *gw_lpni = NULL;
2751 * handle sending a response to a remote peer here so we don't
2752 * have to worry about it if we hit lnet_handle_any_mr_dsta()
2754 if (sd->sd_send_case & REMOTE_DST &&
2755 sd->sd_send_case & SND_RESP) {
2756 struct lnet_peer_ni *gw;
2757 struct lnet_peer *gw_peer;
2759 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw,
2762 CERROR("Can't send response to %s. "
2763 "No route available\n",
2764 libcfs_nid2str(sd->sd_dst_nid));
2765 return -EHOSTUNREACH;
2766 } else if (rc > 0) {
2770 sd->sd_best_lpni = gw;
2771 sd->sd_peer = gw_peer;
2773 return lnet_handle_send(sd);
2777 * Even though the NID for the peer might not be on a local network,
2778 * since the peer is MR there could be other interfaces on the
2779 * local network. In that case we'd still like to prefer the local
2780 * network over the routed network. If we're unable to do that
2781 * then we select the best router among the different routed networks,
2782 * and if the router is MR then we can deal with it as such.
2784 rc = lnet_handle_any_mr_dsta(sd);
2785 if (rc != PASS_THROUGH)
2789 * Now that we must route to the destination, we must consider the
2790 * MR case, where the destination has multiple interfaces, some of
2791 * which we can route to and others we do not. For this reason we
2792 * need to select the destination which we can route to and if
2793 * there are multiple, we need to round robin.
2795 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2800 sd->sd_send_case &= ~LOCAL_DST;
2801 sd->sd_send_case |= REMOTE_DST;
2803 sd->sd_peer = gw_peer;
2804 sd->sd_best_lpni = gw_lpni;
2806 return lnet_handle_send(sd);
2810 * Source not specified
2811 * Remote destination
2814 * Must send to the specified peer NID using the same source NID that
2815 * we've used before. If it's the first time to talk to that peer then
2816 * find the source NI and assign it as preferred to that peer
2819 lnet_handle_any_router_nmr_dst(struct lnet_send_data *sd)
2822 struct lnet_peer_ni *gw_lpni = NULL;
2823 struct lnet_peer *gw_peer = NULL;
2826 * Let's see if we have a preferred NI to talk to this NMR peer
2828 sd->sd_best_ni = lnet_find_existing_preferred_best_ni(sd->sd_best_lpni,
2832 * find the router and that'll find the best NI if we didn't find
2835 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2841 * set the best_ni we've chosen as the preferred one for
2844 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
2846 /* we'll be sending to the gw */
2847 sd->sd_best_lpni = gw_lpni;
2848 sd->sd_peer = gw_peer;
2850 return lnet_handle_send(sd);
2854 lnet_handle_send_case_locked(struct lnet_send_data *sd)
2857 * turn off the SND_RESP bit.
2858 * It will be checked in the case handling
2860 __u32 send_case = sd->sd_send_case &= ~SND_RESP ;
2862 CDEBUG(D_NET, "Source %s%s to %s %s %s destination\n",
2863 (send_case & SRC_SPEC) ? "Specified: " : "ANY",
2864 (send_case & SRC_SPEC) ? libcfs_nid2str(sd->sd_src_nid) : "",
2865 (send_case & MR_DST) ? "MR: " : "NMR: ",
2866 libcfs_nid2str(sd->sd_dst_nid),
2867 (send_case & LOCAL_DST) ? "local" : "routed");
2869 switch (send_case) {
2871 * For all cases where the source is specified, we should always
2872 * use the destination NID, whether it's an MR destination or not,
2873 * since we're continuing a series of related messages for the
2876 case SRC_SPEC_LOCAL_NMR_DST:
2877 return lnet_handle_spec_local_nmr_dst(sd);
2878 case SRC_SPEC_LOCAL_MR_DST:
2879 return lnet_handle_spec_local_mr_dst(sd);
2880 case SRC_SPEC_ROUTER_NMR_DST:
2881 case SRC_SPEC_ROUTER_MR_DST:
2882 return lnet_handle_spec_router_dst(sd);
2883 case SRC_ANY_LOCAL_NMR_DST:
2884 return lnet_handle_any_local_nmr_dst(sd);
2885 case SRC_ANY_LOCAL_MR_DST:
2886 case SRC_ANY_ROUTER_MR_DST:
2887 return lnet_handle_any_mr_dst(sd);
2888 case SRC_ANY_ROUTER_NMR_DST:
2889 return lnet_handle_any_router_nmr_dst(sd);
2891 CERROR("Unknown send case\n");
2897 lnet_select_pathway(lnet_nid_t src_nid, lnet_nid_t dst_nid,
2898 struct lnet_msg *msg, lnet_nid_t rtr_nid)
2900 struct lnet_peer_ni *lpni;
2901 struct lnet_peer *peer;
2902 struct lnet_send_data send_data;
2905 __u32 send_case = 0;
2907 bool mr_forwarding_allowed;
2909 memset(&send_data, 0, sizeof(send_data));
2912 * get an initial CPT to use for locking. The idea here is not to
2913 * serialize the calls to select_pathway, so that as many
2914 * operations can run concurrently as possible. To do that we use
2915 * the CPT where this call is being executed. Later on when we
2916 * determine the CPT to use in lnet_message_commit, we switch the
2917 * lock and check if there was any configuration change. If none,
2918 * then we proceed, if there is, then we restart the operation.
2920 cpt = lnet_net_lock_current();
2922 md_cpt = lnet_cpt_of_md(msg->msg_md, msg->msg_offset);
2923 if (md_cpt == CFS_CPT_ANY)
2929 * If we're being asked to send to the loopback interface, there
2930 * is no need to go through any selection. We can just shortcut
2931 * the entire process and send over lolnd
2933 send_data.sd_msg = msg;
2934 send_data.sd_cpt = cpt;
2935 if (dst_nid == LNET_NID_LO_0) {
2936 rc = lnet_handle_lo_send(&send_data);
2937 lnet_net_unlock(cpt);
2942 * find an existing peer_ni, or create one and mark it as having been
2943 * created due to network traffic. This call will create the
2944 * peer->peer_net->peer_ni tree.
2946 lpni = lnet_nid2peerni_locked(dst_nid, LNET_NID_ANY, cpt);
2948 lnet_net_unlock(cpt);
2949 return PTR_ERR(lpni);
2953 * Cache the original src_nid and rtr_nid. If we need to resend the
2954 * message then we'll need to know whether the src_nid was originally
2955 * specified for this message. If it was originally specified,
2956 * then we need to keep using the same src_nid since it's
2957 * continuing the same sequence of messages. Similarly, rtr_nid will
2958 * affect our choice of next hop.
2960 msg->msg_src_nid_param = src_nid;
2961 msg->msg_rtr_nid_param = rtr_nid;
2964 * If necessary, perform discovery on the peer that owns this peer_ni.
2965 * Note, this can result in the ownership of this peer_ni changing
2966 * to another peer object.
2968 rc = lnet_initiate_peer_discovery(lpni, msg, cpt);
2970 lnet_peer_ni_decref_locked(lpni);
2971 lnet_net_unlock(cpt);
2974 lnet_peer_ni_decref_locked(lpni);
2976 peer = lpni->lpni_peer_net->lpn_peer;
2979 * Identify the different send cases
2981 if (src_nid == LNET_NID_ANY)
2982 send_case |= SRC_ANY;
2984 send_case |= SRC_SPEC;
2986 if (lnet_get_net_locked(LNET_NIDNET(dst_nid)))
2987 send_case |= LOCAL_DST;
2989 send_case |= REMOTE_DST;
2992 if (msg->msg_routing && (send_case & LOCAL_DST))
2995 /* Determine whether to allow MR forwarding for this message.
2996 * NB: MR forwarding is allowed if the message originator and the
2997 * destination are both MR capable, and the destination lpni that was
2998 * originally chosen by the originator is unhealthy or down.
2999 * We check the MR capability of the destination further below
3001 mr_forwarding_allowed = false;
3003 struct lnet_peer *src_lp;
3004 struct lnet_peer_ni *src_lpni;
3006 src_lpni = lnet_nid2peerni_locked(msg->msg_hdr.src_nid,
3008 /* We don't fail the send if we hit any errors here. We'll just
3009 * try to send it via non-multi-rail criteria
3011 if (!IS_ERR(src_lpni)) {
3012 /* Drop ref taken by lnet_nid2peerni_locked() */
3013 lnet_peer_ni_decref_locked(src_lpni);
3014 src_lp = lpni->lpni_peer_net->lpn_peer;
3015 if (lnet_peer_is_multi_rail(src_lp) &&
3016 !lnet_is_peer_ni_alive(lpni))
3017 mr_forwarding_allowed = true;
3020 CDEBUG(D_NET, "msg %p MR forwarding %s\n", msg,
3021 mr_forwarding_allowed ? "allowed" : "not allowed");
3025 * Deal with the peer as NMR in the following cases:
3026 * 1. the peer is NMR
3027 * 2. We're trying to recover a specific peer NI
3028 * 3. I'm a router sending to the final destination and MR forwarding is
3029 * not allowed for this message (as determined above).
3030 * In this case the source of the message would've
3031 * already selected the final destination so my job
3032 * is to honor the selection.
3034 if (!lnet_peer_is_multi_rail(peer) || msg->msg_recovery ||
3035 (final_hop && !mr_forwarding_allowed))
3036 send_case |= NMR_DST;
3038 send_case |= MR_DST;
3040 if (lnet_msg_is_response(msg))
3041 send_case |= SND_RESP;
3043 /* assign parameters to the send_data */
3044 send_data.sd_rtr_nid = rtr_nid;
3045 send_data.sd_src_nid = src_nid;
3046 send_data.sd_dst_nid = dst_nid;
3047 send_data.sd_best_lpni = lpni;
3049 * keep a pointer to the final destination in case we're going to
3050 * route, so we'll need to access it later
3052 send_data.sd_final_dst_lpni = lpni;
3053 send_data.sd_peer = peer;
3054 send_data.sd_md_cpt = md_cpt;
3055 send_data.sd_send_case = send_case;
3057 rc = lnet_handle_send_case_locked(&send_data);
3060 * Update the local cpt since send_data.sd_cpt might've been
3061 * updated as a result of calling lnet_handle_send_case_locked().
3063 cpt = send_data.sd_cpt;
3065 if (rc == REPEAT_SEND)
3068 lnet_net_unlock(cpt);
3074 lnet_send(lnet_nid_t src_nid, struct lnet_msg *msg, lnet_nid_t rtr_nid)
3076 lnet_nid_t dst_nid = msg->msg_target.nid;
3080 * NB: rtr_nid is set to LNET_NID_ANY for all current use-cases,
3081 * but we might want to use pre-determined router for ACK/REPLY
3084 /* NB: ni != NULL == interface pre-determined (ACK/REPLY) */
3085 LASSERT(msg->msg_txpeer == NULL);
3086 LASSERT(msg->msg_txni == NULL);
3087 LASSERT(!msg->msg_sending);
3088 LASSERT(!msg->msg_target_is_router);
3089 LASSERT(!msg->msg_receiving);
3091 msg->msg_sending = 1;
3093 LASSERT(!msg->msg_tx_committed);
3095 rc = lnet_select_pathway(src_nid, dst_nid, msg, rtr_nid);
3097 if (rc == -EHOSTUNREACH)
3098 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_ERROR;
3100 msg->msg_health_status = LNET_MSG_STATUS_LOCAL_ERROR;
3104 if (rc == LNET_CREDIT_OK)
3105 lnet_ni_send(msg->msg_txni, msg);
3107 /* rc == LNET_CREDIT_OK or LNET_CREDIT_WAIT or LNET_DC_WAIT */
3111 enum lnet_mt_event_type {
3112 MT_TYPE_LOCAL_NI = 0,
3116 struct lnet_mt_event_info {
3117 enum lnet_mt_event_type mt_type;
3121 /* called with res_lock held */
3123 lnet_detach_rsp_tracker(struct lnet_libmd *md, int cpt)
3125 struct lnet_rsp_tracker *rspt;
3128 * msg has a refcount on the MD so the MD is not going away.
3129 * The rspt queue for the cpt is protected by
3130 * the lnet_net_lock(cpt). cpt is the cpt of the MD cookie.
3132 if (!md->md_rspt_ptr)
3135 rspt = md->md_rspt_ptr;
3138 LASSERT(rspt->rspt_cpt == cpt);
3140 md->md_rspt_ptr = NULL;
3142 if (LNetMDHandleIsInvalid(rspt->rspt_mdh)) {
3144 * The monitor thread has invalidated this handle because the
3145 * response timed out, but it failed to lookup the MD. That
3146 * means this response tracker is on the zombie list. We can
3147 * safely remove it under the resource lock (held by caller) and
3148 * free the response tracker block.
3150 list_del(&rspt->rspt_on_list);
3151 lnet_rspt_free(rspt, cpt);
3154 * invalidate the handle to indicate that a response has been
3155 * received, which will then lead the monitor thread to clean up
3158 LNetInvalidateMDHandle(&rspt->rspt_mdh);
3163 lnet_clean_zombie_rstqs(void)
3165 struct lnet_rsp_tracker *rspt, *tmp;
3168 cfs_cpt_for_each(i, lnet_cpt_table()) {
3169 list_for_each_entry_safe(rspt, tmp,
3170 the_lnet.ln_mt_zombie_rstqs[i],
3172 list_del(&rspt->rspt_on_list);
3173 lnet_rspt_free(rspt, i);
3177 cfs_percpt_free(the_lnet.ln_mt_zombie_rstqs);
3181 lnet_finalize_expired_responses(void)
3183 struct lnet_libmd *md;
3184 struct lnet_rsp_tracker *rspt, *tmp;
3188 if (the_lnet.ln_mt_rstq == NULL)
3191 cfs_cpt_for_each(i, lnet_cpt_table()) {
3192 LIST_HEAD(local_queue);
3195 if (!the_lnet.ln_mt_rstq[i]) {
3199 list_splice_init(the_lnet.ln_mt_rstq[i], &local_queue);
3204 list_for_each_entry_safe(rspt, tmp, &local_queue, rspt_on_list) {
3206 * The rspt mdh will be invalidated when a response
3207 * is received or whenever we want to discard the
3208 * block the monitor thread will walk the queue
3209 * and clean up any rsts with an invalid mdh.
3210 * The monitor thread will walk the queue until
3211 * the first unexpired rspt block. This means that
3212 * some rspt blocks which received their
3213 * corresponding responses will linger in the
3214 * queue until they are cleaned up eventually.
3217 if (LNetMDHandleIsInvalid(rspt->rspt_mdh)) {
3219 list_del(&rspt->rspt_on_list);
3220 lnet_rspt_free(rspt, i);
3224 if (ktime_compare(now, rspt->rspt_deadline) >= 0 ||
3225 the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN) {
3226 struct lnet_peer_ni *lpni;
3227 struct lnet_nid nid;
3229 md = lnet_handle2md(&rspt->rspt_mdh);
3231 /* MD has been queued for unlink, but
3232 * rspt hasn't been detached (Note we've
3233 * checked above that the rspt_mdh is
3234 * valid). Since we cannot lookup the MD
3235 * we're unable to detach the rspt
3236 * ourselves. Thus, move the rspt to the
3237 * zombie list where we'll wait for
3239 * 1. The remaining operations on the
3240 * MD to complete. In this case the
3241 * final operation will result in
3242 * lnet_msg_detach_md()->
3243 * lnet_detach_rsp_tracker() where
3244 * we will clean up this response
3246 * 2. LNet to shutdown. In this case
3247 * we'll wait until after all LND Nets
3248 * have shutdown and then we can
3249 * safely free any remaining response
3250 * tracker blocks on the zombie list.
3251 * Note: We need to hold the resource
3252 * lock when adding to the zombie list
3253 * because we may have concurrent access
3254 * with lnet_detach_rsp_tracker().
3256 LNetInvalidateMDHandle(&rspt->rspt_mdh);
3257 list_move(&rspt->rspt_on_list,
3258 the_lnet.ln_mt_zombie_rstqs[i]);
3262 LASSERT(md->md_rspt_ptr == rspt);
3263 md->md_rspt_ptr = NULL;
3266 LNetMDUnlink(rspt->rspt_mdh);
3268 nid = rspt->rspt_next_hop_nid;
3270 list_del(&rspt->rspt_on_list);
3271 lnet_rspt_free(rspt, i);
3273 /* If we're shutting down we just want to clean
3274 * up the rspt blocks
3276 if (the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN)
3280 the_lnet.ln_counters[i]->lct_health.lch_response_timeout_count++;
3284 "Response timeout: md = %p: nid = %s\n",
3285 md, libcfs_nidstr(&nid));
3288 * If there is a timeout on the response
3289 * from the next hop decrement its health
3290 * value so that we don't use it
3293 lpni = lnet_peer_ni_find_locked(&nid);
3295 lnet_handle_remote_failure_locked(lpni);
3296 lnet_peer_ni_decref_locked(lpni);
3305 if (!list_empty(&local_queue)) {
3307 list_splice(&local_queue, the_lnet.ln_mt_rstq[i]);
3314 lnet_resend_pending_msgs_locked(struct list_head *resendq, int cpt)
3316 struct lnet_msg *msg;
3318 while (!list_empty(resendq)) {
3319 struct lnet_peer_ni *lpni;
3321 msg = list_entry(resendq->next, struct lnet_msg,
3324 list_del_init(&msg->msg_list);
3326 lpni = lnet_find_peer_ni_locked(msg->msg_hdr.dest_nid);
3328 lnet_net_unlock(cpt);
3329 CERROR("Expected that a peer is already created for %s\n",
3330 libcfs_nid2str(msg->msg_hdr.dest_nid));
3331 msg->msg_no_resend = true;
3332 lnet_finalize(msg, -EFAULT);
3337 lnet_peer_ni_decref_locked(lpni);
3339 lnet_net_unlock(cpt);
3340 CDEBUG(D_NET, "resending %s->%s: %s recovery %d try# %d\n",
3341 libcfs_nid2str(msg->msg_src_nid_param),
3342 libcfs_id2str(msg->msg_target),
3343 lnet_msgtyp2str(msg->msg_type),
3345 msg->msg_retry_count);
3346 rc = lnet_send(msg->msg_src_nid_param, msg,
3347 msg->msg_rtr_nid_param);
3349 CERROR("Error sending %s to %s: %d\n",
3350 lnet_msgtyp2str(msg->msg_type),
3351 libcfs_id2str(msg->msg_target), rc);
3352 msg->msg_no_resend = true;
3353 lnet_finalize(msg, rc);
3357 the_lnet.ln_counters[cpt]->lct_health.lch_resend_count++;
3363 lnet_resend_pending_msgs(void)
3367 cfs_cpt_for_each(i, lnet_cpt_table()) {
3369 lnet_resend_pending_msgs_locked(the_lnet.ln_mt_resendqs[i], i);
3374 /* called with cpt and ni_lock held */
3376 lnet_unlink_ni_recovery_mdh_locked(struct lnet_ni *ni, int cpt, bool force)
3378 struct lnet_handle_md recovery_mdh;
3380 LNetInvalidateMDHandle(&recovery_mdh);
3382 if (ni->ni_recovery_state & LNET_NI_RECOVERY_PENDING ||
3384 recovery_mdh = ni->ni_ping_mdh;
3385 LNetInvalidateMDHandle(&ni->ni_ping_mdh);
3388 lnet_net_unlock(cpt);
3389 if (!LNetMDHandleIsInvalid(recovery_mdh))
3390 LNetMDUnlink(recovery_mdh);
3396 lnet_recover_local_nis(void)
3398 struct lnet_mt_event_info *ev_info;
3399 LIST_HEAD(processed_list);
3400 LIST_HEAD(local_queue);
3401 struct lnet_handle_md mdh;
3402 struct lnet_ni *tmp;
3410 * splice the recovery queue on a local queue. We will iterate
3411 * through the local queue and update it as needed. Once we're
3412 * done with the traversal, we'll splice the local queue back on
3413 * the head of the ln_mt_localNIRecovq. Any newly added local NIs
3414 * will be traversed in the next iteration.
3417 list_splice_init(&the_lnet.ln_mt_localNIRecovq,
3421 now = ktime_get_seconds();
3423 list_for_each_entry_safe(ni, tmp, &local_queue, ni_recovery) {
3425 * if an NI is being deleted or it is now healthy, there
3426 * is no need to keep it around in the recovery queue.
3427 * The monitor thread is the only thread responsible for
3428 * removing the NI from the recovery queue.
3429 * Multiple threads can be adding NIs to the recovery
3432 healthv = atomic_read(&ni->ni_healthv);
3436 if (ni->ni_state != LNET_NI_STATE_ACTIVE ||
3437 healthv == LNET_MAX_HEALTH_VALUE) {
3438 list_del_init(&ni->ni_recovery);
3439 lnet_unlink_ni_recovery_mdh_locked(ni, 0, false);
3441 lnet_ni_decref_locked(ni, 0);
3447 * if the local NI failed recovery we must unlink the md.
3448 * But we want to keep the local_ni on the recovery queue
3449 * so we can continue the attempts to recover it.
3451 if (ni->ni_recovery_state & LNET_NI_RECOVERY_FAILED) {
3452 lnet_unlink_ni_recovery_mdh_locked(ni, 0, true);
3453 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_FAILED;
3459 if (now < ni->ni_next_ping) {
3466 CDEBUG(D_NET, "attempting to recover local ni: %s\n",
3467 libcfs_nidstr(&ni->ni_nid));
3470 if (!(ni->ni_recovery_state & LNET_NI_RECOVERY_PENDING)) {
3471 ni->ni_recovery_state |= LNET_NI_RECOVERY_PENDING;
3474 LIBCFS_ALLOC(ev_info, sizeof(*ev_info));
3476 CERROR("out of memory. Can't recover %s\n",
3477 libcfs_nidstr(&ni->ni_nid));
3479 ni->ni_recovery_state &=
3480 ~LNET_NI_RECOVERY_PENDING;
3485 mdh = ni->ni_ping_mdh;
3487 * Invalidate the ni mdh in case it's deleted.
3488 * We'll unlink the mdh in this case below.
3490 LNetInvalidateMDHandle(&ni->ni_ping_mdh);
3491 /* FIXME need to handle large-addr nid */
3492 nid = lnet_nid_to_nid4(&ni->ni_nid);
3495 * remove the NI from the local queue and drop the
3496 * reference count to it while we're recovering
3497 * it. The reason for that, is that the NI could
3498 * be deleted, and the way the code is structured
3499 * is if we don't drop the NI, then the deletion
3500 * code will enter a loop waiting for the
3501 * reference count to be removed while holding the
3502 * ln_mutex_lock(). When we look up the peer to
3503 * send to in lnet_select_pathway() we will try to
3504 * lock the ln_mutex_lock() as well, leading to
3505 * a deadlock. By dropping the refcount and
3506 * removing it from the list, we allow for the NI
3507 * to be removed, then we use the cached NID to
3508 * look it up again. If it's gone, then we just
3509 * continue examining the rest of the queue.
3512 list_del_init(&ni->ni_recovery);
3513 lnet_ni_decref_locked(ni, 0);
3516 ev_info->mt_type = MT_TYPE_LOCAL_NI;
3517 ev_info->mt_nid = nid;
3518 rc = lnet_send_ping(nid, &mdh, LNET_INTERFACES_MIN,
3519 ev_info, the_lnet.ln_mt_handler,
3521 /* lookup the nid again */
3523 ni = lnet_nid2ni_locked(nid, 0);
3526 * the NI has been deleted when we dropped
3533 ni->ni_ping_count++;
3535 ni->ni_ping_mdh = mdh;
3536 lnet_ni_add_to_recoveryq_locked(ni, &processed_list,
3541 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_PENDING;
3550 * put back the remaining NIs on the ln_mt_localNIRecovq to be
3551 * reexamined in the next iteration.
3553 list_splice_init(&processed_list, &local_queue);
3555 list_splice(&local_queue, &the_lnet.ln_mt_localNIRecovq);
3560 lnet_resendqs_create(void)
3562 struct list_head **resendqs;
3563 resendqs = lnet_create_array_of_queues();
3568 lnet_net_lock(LNET_LOCK_EX);
3569 the_lnet.ln_mt_resendqs = resendqs;
3570 lnet_net_unlock(LNET_LOCK_EX);
3576 lnet_clean_local_ni_recoveryq(void)
3580 /* This is only called when the monitor thread has stopped */
3583 while (!list_empty(&the_lnet.ln_mt_localNIRecovq)) {
3584 ni = list_entry(the_lnet.ln_mt_localNIRecovq.next,
3585 struct lnet_ni, ni_recovery);
3586 list_del_init(&ni->ni_recovery);
3588 lnet_unlink_ni_recovery_mdh_locked(ni, 0, true);
3590 lnet_ni_decref_locked(ni, 0);
3597 lnet_unlink_lpni_recovery_mdh_locked(struct lnet_peer_ni *lpni, int cpt,
3600 struct lnet_handle_md recovery_mdh;
3602 LNetInvalidateMDHandle(&recovery_mdh);
3604 if (lpni->lpni_state & LNET_PEER_NI_RECOVERY_PENDING || force) {
3605 recovery_mdh = lpni->lpni_recovery_ping_mdh;
3606 LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
3608 spin_unlock(&lpni->lpni_lock);
3609 lnet_net_unlock(cpt);
3610 if (!LNetMDHandleIsInvalid(recovery_mdh))
3611 LNetMDUnlink(recovery_mdh);
3613 spin_lock(&lpni->lpni_lock);
3617 lnet_clean_peer_ni_recoveryq(void)
3619 struct lnet_peer_ni *lpni, *tmp;
3621 lnet_net_lock(LNET_LOCK_EX);
3623 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_mt_peerNIRecovq,
3625 list_del_init(&lpni->lpni_recovery);
3626 spin_lock(&lpni->lpni_lock);
3627 lnet_unlink_lpni_recovery_mdh_locked(lpni, LNET_LOCK_EX, true);
3628 spin_unlock(&lpni->lpni_lock);
3629 lnet_peer_ni_decref_locked(lpni);
3632 lnet_net_unlock(LNET_LOCK_EX);
3636 lnet_clean_resendqs(void)
3638 struct lnet_msg *msg, *tmp;
3642 cfs_cpt_for_each(i, lnet_cpt_table()) {
3644 list_splice_init(the_lnet.ln_mt_resendqs[i], &msgs);
3646 list_for_each_entry_safe(msg, tmp, &msgs, msg_list) {
3647 list_del_init(&msg->msg_list);
3648 msg->msg_no_resend = true;
3649 lnet_finalize(msg, -ESHUTDOWN);
3653 cfs_percpt_free(the_lnet.ln_mt_resendqs);
3657 lnet_recover_peer_nis(void)
3659 struct lnet_mt_event_info *ev_info;
3660 LIST_HEAD(processed_list);
3661 LIST_HEAD(local_queue);
3662 struct lnet_handle_md mdh;
3663 struct lnet_peer_ni *lpni;
3664 struct lnet_peer_ni *tmp;
3671 * Always use cpt 0 for locking across all interactions with
3672 * ln_mt_peerNIRecovq
3675 list_splice_init(&the_lnet.ln_mt_peerNIRecovq,
3679 now = ktime_get_seconds();
3681 list_for_each_entry_safe(lpni, tmp, &local_queue,
3684 * The same protection strategy is used here as is in the
3685 * local recovery case.
3688 healthv = atomic_read(&lpni->lpni_healthv);
3689 spin_lock(&lpni->lpni_lock);
3690 if (lpni->lpni_state & LNET_PEER_NI_DELETING ||
3691 healthv == LNET_MAX_HEALTH_VALUE) {
3692 list_del_init(&lpni->lpni_recovery);
3693 lnet_unlink_lpni_recovery_mdh_locked(lpni, 0, false);
3694 spin_unlock(&lpni->lpni_lock);
3695 lnet_peer_ni_decref_locked(lpni);
3701 * If the peer NI has failed recovery we must unlink the
3702 * md. But we want to keep the peer ni on the recovery
3703 * queue so we can try to continue recovering it
3705 if (lpni->lpni_state & LNET_PEER_NI_RECOVERY_FAILED) {
3706 lnet_unlink_lpni_recovery_mdh_locked(lpni, 0, true);
3707 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_FAILED;
3710 spin_unlock(&lpni->lpni_lock);
3712 if (now < lpni->lpni_next_ping) {
3720 * NOTE: we're racing with peer deletion from user space.
3721 * It's possible that a peer is deleted after we check its
3722 * state. In this case the recovery can create a new peer
3724 spin_lock(&lpni->lpni_lock);
3725 if (!(lpni->lpni_state & LNET_PEER_NI_RECOVERY_PENDING) &&
3726 !(lpni->lpni_state & LNET_PEER_NI_DELETING)) {
3727 lpni->lpni_state |= LNET_PEER_NI_RECOVERY_PENDING;
3728 spin_unlock(&lpni->lpni_lock);
3730 LIBCFS_ALLOC(ev_info, sizeof(*ev_info));
3732 CERROR("out of memory. Can't recover %s\n",
3733 libcfs_nidstr(&lpni->lpni_nid));
3734 spin_lock(&lpni->lpni_lock);
3735 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3736 spin_unlock(&lpni->lpni_lock);
3740 /* look at the comments in lnet_recover_local_nis() */
3741 mdh = lpni->lpni_recovery_ping_mdh;
3742 LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
3743 /* FIXME handle large-addr nid */
3744 nid = lnet_nid_to_nid4(&lpni->lpni_nid);
3746 list_del_init(&lpni->lpni_recovery);
3747 lnet_peer_ni_decref_locked(lpni);
3750 ev_info->mt_type = MT_TYPE_PEER_NI;
3751 ev_info->mt_nid = nid;
3752 rc = lnet_send_ping(nid, &mdh, LNET_INTERFACES_MIN,
3753 ev_info, the_lnet.ln_mt_handler,
3757 * lnet_find_peer_ni_locked() grabs a refcount for
3758 * us. No need to take it explicitly.
3760 lpni = lnet_find_peer_ni_locked(nid);
3767 lpni->lpni_ping_count++;
3769 lpni->lpni_recovery_ping_mdh = mdh;
3771 lnet_peer_ni_add_to_recoveryq_locked(lpni,
3775 spin_lock(&lpni->lpni_lock);
3776 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3777 spin_unlock(&lpni->lpni_lock);
3780 /* Drop the ref taken by lnet_find_peer_ni_locked() */
3781 lnet_peer_ni_decref_locked(lpni);
3784 spin_unlock(&lpni->lpni_lock);
3787 list_splice_init(&processed_list, &local_queue);
3789 list_splice(&local_queue, &the_lnet.ln_mt_peerNIRecovq);
3794 lnet_monitor_thread(void *arg)
3796 time64_t rsp_timeout = 0;
3799 wait_for_completion(&the_lnet.ln_started);
3801 * The monitor thread takes care of the following:
3802 * 1. Checks the aliveness of routers
3803 * 2. Checks if there are messages on the resend queue to resend
3805 * 3. Check if there are any NIs on the local recovery queue and
3807 * 4. Checks if there are any NIs on the remote recovery queue
3810 while (the_lnet.ln_mt_state == LNET_MT_STATE_RUNNING) {
3811 now = ktime_get_real_seconds();
3813 if (lnet_router_checker_active())
3814 lnet_check_routers();
3816 lnet_resend_pending_msgs();
3818 if (now >= rsp_timeout) {
3819 lnet_finalize_expired_responses();
3820 rsp_timeout = now + (lnet_transaction_timeout / 2);
3823 lnet_recover_local_nis();
3824 lnet_recover_peer_nis();
3827 * TODO do we need to check if we should sleep without
3828 * timeout? Technically, an active system will always
3829 * have messages in flight so this check will always
3830 * evaluate to false. And on an idle system do we care
3831 * if we wake up every 1 second? Although, we've seen
3832 * cases where we get a complaint that an idle thread
3833 * is waking up unnecessarily.
3835 wait_for_completion_interruptible_timeout(
3836 &the_lnet.ln_mt_wait_complete,
3837 cfs_time_seconds(1));
3838 /* Must re-init the completion before testing anything,
3839 * including ln_mt_state.
3841 reinit_completion(&the_lnet.ln_mt_wait_complete);
3845 lnet_net_lock(LNET_LOCK_EX);
3846 the_lnet.ln_mt_state = LNET_MT_STATE_SHUTDOWN;
3847 lnet_net_unlock(LNET_LOCK_EX);
3849 /* signal that the monitor thread is exiting */
3850 up(&the_lnet.ln_mt_signal);
3858 * Returns == 0 if success
3859 * Returns > 0 if LNetMDBind or prior fails
3860 * Returns < 0 if LNetGet fails
3863 lnet_send_ping(lnet_nid_t dest_nid,
3864 struct lnet_handle_md *mdh, int nnis,
3865 void *user_data, lnet_handler_t handler, bool recovery)
3867 struct lnet_md md = { NULL };
3868 struct lnet_process_id id;
3869 struct lnet_ping_buffer *pbuf;
3872 if (dest_nid == LNET_NID_ANY) {
3877 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
3883 /* initialize md content */
3884 md.start = &pbuf->pb_info;
3885 md.length = LNET_PING_INFO_SIZE(nnis);
3886 md.threshold = 2; /* GET/REPLY */
3888 md.options = LNET_MD_TRUNCATE | LNET_MD_TRACK_RESPONSE;
3889 md.user_ptr = user_data;
3890 md.handler = handler;
3892 rc = LNetMDBind(&md, LNET_UNLINK, mdh);
3894 lnet_ping_buffer_decref(pbuf);
3895 CERROR("Can't bind MD: %d\n", rc);
3896 rc = -rc; /* change the rc to positive */
3899 id.pid = LNET_PID_LUSTRE;
3902 rc = LNetGet(LNET_NID_ANY, *mdh, id,
3903 LNET_RESERVED_PORTAL,
3904 LNET_PROTO_PING_MATCHBITS, 0, recovery);
3907 goto fail_unlink_md;
3913 LNetInvalidateMDHandle(mdh);
3919 lnet_handle_recovery_reply(struct lnet_mt_event_info *ev_info,
3920 int status, bool send, bool unlink_event)
3922 lnet_nid_t nid = ev_info->mt_nid;
3924 if (ev_info->mt_type == MT_TYPE_LOCAL_NI) {
3928 ni = lnet_nid2ni_locked(nid, 0);
3934 if (!send || (send && status != 0))
3935 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_PENDING;
3937 ni->ni_recovery_state |= LNET_NI_RECOVERY_FAILED;
3942 CERROR("local NI (%s) recovery failed with %d\n",
3943 libcfs_nid2str(nid), status);
3947 * need to increment healthv for the ni here, because in
3948 * the lnet_finalize() path we don't have access to this
3949 * NI. And in order to get access to it, we'll need to
3950 * carry forward too much information.
3951 * In the peer case, it'll naturally be incremented
3954 lnet_inc_healthv(&ni->ni_healthv,
3955 lnet_health_sensitivity);
3957 struct lnet_peer_ni *lpni;
3960 cpt = lnet_net_lock_current();
3961 lpni = lnet_find_peer_ni_locked(nid);
3963 lnet_net_unlock(cpt);
3966 spin_lock(&lpni->lpni_lock);
3967 if (!send || (send && status != 0))
3968 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3970 lpni->lpni_state |= LNET_PEER_NI_RECOVERY_FAILED;
3971 spin_unlock(&lpni->lpni_lock);
3972 lnet_peer_ni_decref_locked(lpni);
3973 lnet_net_unlock(cpt);
3976 CERROR("peer NI (%s) recovery failed with %d\n",
3977 libcfs_nid2str(nid), status);
3982 lnet_mt_event_handler(struct lnet_event *event)
3984 struct lnet_mt_event_info *ev_info = event->md_user_ptr;
3985 struct lnet_ping_buffer *pbuf;
3987 /* TODO: remove assert */
3988 LASSERT(event->type == LNET_EVENT_REPLY ||
3989 event->type == LNET_EVENT_SEND ||
3990 event->type == LNET_EVENT_UNLINK);
3992 CDEBUG(D_NET, "Received event: %d status: %d\n", event->type,
3995 switch (event->type) {
3996 case LNET_EVENT_UNLINK:
3997 CDEBUG(D_NET, "%s recovery ping unlinked\n",
3998 libcfs_nid2str(ev_info->mt_nid));
4000 case LNET_EVENT_REPLY:
4001 lnet_handle_recovery_reply(ev_info, event->status, false,
4002 event->type == LNET_EVENT_UNLINK);
4004 case LNET_EVENT_SEND:
4005 CDEBUG(D_NET, "%s recovery message sent %s:%d\n",
4006 libcfs_nid2str(ev_info->mt_nid),
4007 (event->status) ? "unsuccessfully" :
4008 "successfully", event->status);
4009 lnet_handle_recovery_reply(ev_info, event->status, true, false);
4012 CERROR("Unexpected event: %d\n", event->type);
4015 if (event->unlinked) {
4016 LIBCFS_FREE(ev_info, sizeof(*ev_info));
4017 pbuf = LNET_PING_INFO_TO_BUFFER(event->md_start);
4018 lnet_ping_buffer_decref(pbuf);
4023 lnet_rsp_tracker_create(void)
4025 struct list_head **rstqs;
4026 rstqs = lnet_create_array_of_queues();
4031 the_lnet.ln_mt_rstq = rstqs;
4037 lnet_rsp_tracker_clean(void)
4039 lnet_finalize_expired_responses();
4041 cfs_percpt_free(the_lnet.ln_mt_rstq);
4042 the_lnet.ln_mt_rstq = NULL;
4045 int lnet_monitor_thr_start(void)
4048 struct task_struct *task;
4050 if (the_lnet.ln_mt_state != LNET_MT_STATE_SHUTDOWN)
4053 rc = lnet_resendqs_create();
4057 rc = lnet_rsp_tracker_create();
4061 sema_init(&the_lnet.ln_mt_signal, 0);
4063 lnet_net_lock(LNET_LOCK_EX);
4064 the_lnet.ln_mt_state = LNET_MT_STATE_RUNNING;
4065 lnet_net_unlock(LNET_LOCK_EX);
4066 task = kthread_run(lnet_monitor_thread, NULL, "monitor_thread");
4069 CERROR("Can't start monitor thread: %d\n", rc);
4076 lnet_net_lock(LNET_LOCK_EX);
4077 the_lnet.ln_mt_state = LNET_MT_STATE_STOPPING;
4078 lnet_net_unlock(LNET_LOCK_EX);
4079 /* block until event callback signals exit */
4080 down(&the_lnet.ln_mt_signal);
4082 lnet_net_lock(LNET_LOCK_EX);
4083 the_lnet.ln_mt_state = LNET_MT_STATE_SHUTDOWN;
4084 lnet_net_unlock(LNET_LOCK_EX);
4085 lnet_rsp_tracker_clean();
4086 lnet_clean_local_ni_recoveryq();
4087 lnet_clean_peer_ni_recoveryq();
4088 lnet_clean_resendqs();
4089 the_lnet.ln_mt_handler = NULL;
4092 lnet_rsp_tracker_clean();
4093 lnet_clean_local_ni_recoveryq();
4094 lnet_clean_peer_ni_recoveryq();
4095 lnet_clean_resendqs();
4099 void lnet_monitor_thr_stop(void)
4101 if (the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN)
4104 LASSERT(the_lnet.ln_mt_state == LNET_MT_STATE_RUNNING);
4105 lnet_net_lock(LNET_LOCK_EX);
4106 the_lnet.ln_mt_state = LNET_MT_STATE_STOPPING;
4107 lnet_net_unlock(LNET_LOCK_EX);
4109 /* tell the monitor thread that we're shutting down */
4110 complete(&the_lnet.ln_mt_wait_complete);
4112 /* block until monitor thread signals that it's done */
4113 down(&the_lnet.ln_mt_signal);
4114 LASSERT(the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN);
4116 /* perform cleanup tasks */
4117 lnet_rsp_tracker_clean();
4118 lnet_clean_local_ni_recoveryq();
4119 lnet_clean_peer_ni_recoveryq();
4120 lnet_clean_resendqs();
4124 lnet_drop_message(struct lnet_ni *ni, int cpt, void *private, unsigned int nob,
4128 lnet_incr_stats(&ni->ni_stats, msg_type, LNET_STATS_TYPE_DROP);
4129 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
4130 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length += nob;
4131 lnet_net_unlock(cpt);
4133 lnet_ni_recv(ni, private, NULL, 0, 0, 0, nob);
4137 lnet_recv_put(struct lnet_ni *ni, struct lnet_msg *msg)
4139 struct lnet_hdr *hdr = &msg->msg_hdr;
4141 if (msg->msg_wanted != 0)
4142 lnet_setpayloadbuffer(msg);
4144 lnet_build_msg_event(msg, LNET_EVENT_PUT);
4146 /* Must I ACK? If so I'll grab the ack_wmd out of the header and put
4147 * it back into the ACK during lnet_finalize() */
4148 msg->msg_ack = (!lnet_is_wire_handle_none(&hdr->msg.put.ack_wmd) &&
4149 (msg->msg_md->md_options & LNET_MD_ACK_DISABLE) == 0);
4151 lnet_ni_recv(ni, msg->msg_private, msg, msg->msg_rx_delayed,
4152 msg->msg_offset, msg->msg_wanted, hdr->payload_length);
4156 lnet_parse_put(struct lnet_ni *ni, struct lnet_msg *msg)
4158 struct lnet_hdr *hdr = &msg->msg_hdr;
4159 struct lnet_match_info info;
4163 /* Convert put fields to host byte order */
4164 hdr->msg.put.match_bits = le64_to_cpu(hdr->msg.put.match_bits);
4165 hdr->msg.put.ptl_index = le32_to_cpu(hdr->msg.put.ptl_index);
4166 hdr->msg.put.offset = le32_to_cpu(hdr->msg.put.offset);
4168 /* Primary peer NID. */
4169 info.mi_id.nid = msg->msg_initiator;
4170 info.mi_id.pid = hdr->src_pid;
4171 info.mi_opc = LNET_MD_OP_PUT;
4172 info.mi_portal = hdr->msg.put.ptl_index;
4173 info.mi_rlength = hdr->payload_length;
4174 info.mi_roffset = hdr->msg.put.offset;
4175 info.mi_mbits = hdr->msg.put.match_bits;
4176 info.mi_cpt = lnet_cpt_of_nid(msg->msg_initiator, ni);
4178 msg->msg_rx_ready_delay = ni->ni_net->net_lnd->lnd_eager_recv == NULL;
4179 ready_delay = msg->msg_rx_ready_delay;
4182 rc = lnet_ptl_match_md(&info, msg);
4187 case LNET_MATCHMD_OK:
4188 lnet_recv_put(ni, msg);
4191 case LNET_MATCHMD_NONE:
4193 /* no eager_recv or has already called it, should
4194 * have been attached on delayed list */
4197 rc = lnet_ni_eager_recv(ni, msg);
4204 case LNET_MATCHMD_DROP:
4205 CNETERR("Dropping PUT from %s portal %d match %llu"
4206 " offset %d length %d: %d\n",
4207 libcfs_id2str(info.mi_id), info.mi_portal,
4208 info.mi_mbits, info.mi_roffset, info.mi_rlength, rc);
4210 return -ENOENT; /* -ve: OK but no match */
4215 lnet_parse_get(struct lnet_ni *ni, struct lnet_msg *msg, int rdma_get)
4217 struct lnet_match_info info;
4218 struct lnet_hdr *hdr = &msg->msg_hdr;
4219 struct lnet_process_id source_id;
4220 struct lnet_handle_wire reply_wmd;
4223 /* Convert get fields to host byte order */
4224 hdr->msg.get.match_bits = le64_to_cpu(hdr->msg.get.match_bits);
4225 hdr->msg.get.ptl_index = le32_to_cpu(hdr->msg.get.ptl_index);
4226 hdr->msg.get.sink_length = le32_to_cpu(hdr->msg.get.sink_length);
4227 hdr->msg.get.src_offset = le32_to_cpu(hdr->msg.get.src_offset);
4229 source_id.nid = hdr->src_nid;
4230 source_id.pid = hdr->src_pid;
4231 /* Primary peer NID */
4232 info.mi_id.nid = msg->msg_initiator;
4233 info.mi_id.pid = hdr->src_pid;
4234 info.mi_opc = LNET_MD_OP_GET;
4235 info.mi_portal = hdr->msg.get.ptl_index;
4236 info.mi_rlength = hdr->msg.get.sink_length;
4237 info.mi_roffset = hdr->msg.get.src_offset;
4238 info.mi_mbits = hdr->msg.get.match_bits;
4239 info.mi_cpt = lnet_cpt_of_nid(msg->msg_initiator, ni);
4241 rc = lnet_ptl_match_md(&info, msg);
4242 if (rc == LNET_MATCHMD_DROP) {
4243 CNETERR("Dropping GET from %s portal %d match %llu"
4244 " offset %d length %d\n",
4245 libcfs_id2str(info.mi_id), info.mi_portal,
4246 info.mi_mbits, info.mi_roffset, info.mi_rlength);
4247 return -ENOENT; /* -ve: OK but no match */
4250 LASSERT(rc == LNET_MATCHMD_OK);
4252 lnet_build_msg_event(msg, LNET_EVENT_GET);
4254 reply_wmd = hdr->msg.get.return_wmd;
4256 lnet_prep_send(msg, LNET_MSG_REPLY, source_id,
4257 msg->msg_offset, msg->msg_wanted);
4259 msg->msg_hdr.msg.reply.dst_wmd = reply_wmd;
4262 /* The LND completes the REPLY from her recv procedure */
4263 lnet_ni_recv(ni, msg->msg_private, msg, 0,
4264 msg->msg_offset, msg->msg_len, msg->msg_len);
4268 lnet_ni_recv(ni, msg->msg_private, NULL, 0, 0, 0, 0);
4269 msg->msg_receiving = 0;
4271 /* FIXME need to handle large-addr nid */
4272 rc = lnet_send(lnet_nid_to_nid4(&ni->ni_nid), msg, msg->msg_from);
4274 /* didn't get as far as lnet_ni_send() */
4275 CERROR("%s: Unable to send REPLY for GET from %s: %d\n",
4276 libcfs_nidstr(&ni->ni_nid),
4277 libcfs_id2str(info.mi_id), rc);
4279 lnet_finalize(msg, rc);
4286 lnet_parse_reply(struct lnet_ni *ni, struct lnet_msg *msg)
4288 void *private = msg->msg_private;
4289 struct lnet_hdr *hdr = &msg->msg_hdr;
4290 struct lnet_process_id src = {0};
4291 struct lnet_libmd *md;
4292 unsigned int rlength;
4293 unsigned int mlength;
4296 cpt = lnet_cpt_of_cookie(hdr->msg.reply.dst_wmd.wh_object_cookie);
4299 src.nid = hdr->src_nid;
4300 src.pid = hdr->src_pid;
4302 /* NB handles only looked up by creator (no flips) */
4303 md = lnet_wire_handle2md(&hdr->msg.reply.dst_wmd);
4304 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
4305 CNETERR("%s: Dropping REPLY from %s for %s "
4307 libcfs_nidstr(&ni->ni_nid), libcfs_id2str(src),
4308 (md == NULL) ? "invalid" : "inactive",
4309 hdr->msg.reply.dst_wmd.wh_interface_cookie,
4310 hdr->msg.reply.dst_wmd.wh_object_cookie);
4311 if (md != NULL && md->md_me != NULL)
4312 CERROR("REPLY MD also attached to portal %d\n",
4313 md->md_me->me_portal);
4315 lnet_res_unlock(cpt);
4316 return -ENOENT; /* -ve: OK but no match */
4319 LASSERT(md->md_offset == 0);
4321 rlength = hdr->payload_length;
4322 mlength = min(rlength, md->md_length);
4324 if (mlength < rlength &&
4325 (md->md_options & LNET_MD_TRUNCATE) == 0) {
4326 CNETERR("%s: Dropping REPLY from %s length %d "
4327 "for MD %#llx would overflow (%d)\n",
4328 libcfs_nidstr(&ni->ni_nid), libcfs_id2str(src),
4329 rlength, hdr->msg.reply.dst_wmd.wh_object_cookie,
4331 lnet_res_unlock(cpt);
4332 return -ENOENT; /* -ve: OK but no match */
4335 CDEBUG(D_NET, "%s: Reply from %s of length %d/%d into md %#llx\n",
4336 libcfs_nidstr(&ni->ni_nid), libcfs_id2str(src),
4337 mlength, rlength, hdr->msg.reply.dst_wmd.wh_object_cookie);
4339 lnet_msg_attach_md(msg, md, 0, mlength);
4342 lnet_setpayloadbuffer(msg);
4344 lnet_res_unlock(cpt);
4346 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
4348 lnet_ni_recv(ni, private, msg, 0, 0, mlength, rlength);
4353 lnet_parse_ack(struct lnet_ni *ni, struct lnet_msg *msg)
4355 struct lnet_hdr *hdr = &msg->msg_hdr;
4356 struct lnet_process_id src = {0};
4357 struct lnet_libmd *md;
4360 src.nid = hdr->src_nid;
4361 src.pid = hdr->src_pid;
4363 /* Convert ack fields to host byte order */
4364 hdr->msg.ack.match_bits = le64_to_cpu(hdr->msg.ack.match_bits);
4365 hdr->msg.ack.mlength = le32_to_cpu(hdr->msg.ack.mlength);
4367 cpt = lnet_cpt_of_cookie(hdr->msg.ack.dst_wmd.wh_object_cookie);
4370 /* NB handles only looked up by creator (no flips) */
4371 md = lnet_wire_handle2md(&hdr->msg.ack.dst_wmd);
4372 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
4373 /* Don't moan; this is expected */
4375 "%s: Dropping ACK from %s to %s MD %#llx.%#llx\n",
4376 libcfs_nidstr(&ni->ni_nid), libcfs_id2str(src),
4377 (md == NULL) ? "invalid" : "inactive",
4378 hdr->msg.ack.dst_wmd.wh_interface_cookie,
4379 hdr->msg.ack.dst_wmd.wh_object_cookie);
4380 if (md != NULL && md->md_me != NULL)
4381 CERROR("Source MD also attached to portal %d\n",
4382 md->md_me->me_portal);
4384 lnet_res_unlock(cpt);
4385 return -ENOENT; /* -ve! */
4388 CDEBUG(D_NET, "%s: ACK from %s into md %#llx\n",
4389 libcfs_nidstr(&ni->ni_nid), libcfs_id2str(src),
4390 hdr->msg.ack.dst_wmd.wh_object_cookie);
4392 lnet_msg_attach_md(msg, md, 0, 0);
4394 lnet_res_unlock(cpt);
4396 lnet_build_msg_event(msg, LNET_EVENT_ACK);
4398 lnet_ni_recv(ni, msg->msg_private, msg, 0, 0, 0, msg->msg_len);
4403 * \retval LNET_CREDIT_OK If \a msg is forwarded
4404 * \retval LNET_CREDIT_WAIT If \a msg is blocked because w/o buffer
4405 * \retval -ve error code
4408 lnet_parse_forward_locked(struct lnet_ni *ni, struct lnet_msg *msg)
4412 if (!the_lnet.ln_routing)
4415 if (msg->msg_rxpeer->lpni_rtrcredits <= 0 ||
4416 lnet_msg2bufpool(msg)->rbp_credits <= 0) {
4417 if (ni->ni_net->net_lnd->lnd_eager_recv == NULL) {
4418 msg->msg_rx_ready_delay = 1;
4420 lnet_net_unlock(msg->msg_rx_cpt);
4421 rc = lnet_ni_eager_recv(ni, msg);
4422 lnet_net_lock(msg->msg_rx_cpt);
4427 rc = lnet_post_routed_recv_locked(msg, 0);
4432 lnet_parse_local(struct lnet_ni *ni, struct lnet_msg *msg)
4436 switch (msg->msg_type) {
4438 rc = lnet_parse_ack(ni, msg);
4441 rc = lnet_parse_put(ni, msg);
4444 rc = lnet_parse_get(ni, msg, msg->msg_rdma_get);
4446 case LNET_MSG_REPLY:
4447 rc = lnet_parse_reply(ni, msg);
4449 default: /* prevent an unused label if !kernel */
4454 LASSERT(rc == 0 || rc == -ENOENT);
4459 lnet_msgtyp2str (int type)
4468 case LNET_MSG_REPLY:
4470 case LNET_MSG_HELLO:
4473 return ("<UNKNOWN>");
4478 lnet_parse(struct lnet_ni *ni, struct lnet_hdr *hdr, lnet_nid_t from_nid,
4479 void *private, int rdma_req)
4481 struct lnet_peer_ni *lpni;
4482 struct lnet_msg *msg;
4483 __u32 payload_length;
4484 lnet_pid_t dest_pid;
4485 lnet_nid_t dest_nid;
4493 LASSERT (!in_interrupt ());
4495 type = le32_to_cpu(hdr->type);
4496 src_nid = le64_to_cpu(hdr->src_nid);
4497 dest_nid = le64_to_cpu(hdr->dest_nid);
4498 dest_pid = le32_to_cpu(hdr->dest_pid);
4499 payload_length = le32_to_cpu(hdr->payload_length);
4501 /* FIXME handle large-addr nids */
4502 for_me = (lnet_nid_to_nid4(&ni->ni_nid) == dest_nid);
4503 cpt = lnet_cpt_of_nid(from_nid, ni);
4505 CDEBUG(D_NET, "TRACE: %s(%s) <- %s : %s - %s\n",
4506 libcfs_nid2str(dest_nid),
4507 libcfs_nidstr(&ni->ni_nid),
4508 libcfs_nid2str(src_nid),
4509 lnet_msgtyp2str(type),
4510 (for_me) ? "for me" : "routed");
4515 if (payload_length > 0) {
4516 CERROR("%s, src %s: bad %s payload %d (0 expected)\n",
4517 libcfs_nid2str(from_nid),
4518 libcfs_nid2str(src_nid),
4519 lnet_msgtyp2str(type), payload_length);
4525 case LNET_MSG_REPLY:
4526 if (payload_length >
4527 (__u32)(for_me ? LNET_MAX_PAYLOAD : LNET_MTU)) {
4528 CERROR("%s, src %s: bad %s payload %d "
4529 "(%d max expected)\n",
4530 libcfs_nid2str(from_nid),
4531 libcfs_nid2str(src_nid),
4532 lnet_msgtyp2str(type),
4534 for_me ? LNET_MAX_PAYLOAD : LNET_MTU);
4540 CERROR("%s, src %s: Bad message type 0x%x\n",
4541 libcfs_nid2str(from_nid),
4542 libcfs_nid2str(src_nid), type);
4546 if (the_lnet.ln_routing &&
4547 ni->ni_net->net_last_alive != ktime_get_real_seconds()) {
4549 spin_lock(&ni->ni_net->net_lock);
4550 ni->ni_net->net_last_alive = ktime_get_real_seconds();
4551 spin_unlock(&ni->ni_net->net_lock);
4552 push = lnet_ni_set_status_locked(ni, LNET_NI_STATUS_UP);
4557 lnet_push_update_to_peers(1);
4559 /* Regard a bad destination NID as a protocol error. Senders should
4560 * know what they're doing; if they don't they're misconfigured, buggy
4561 * or malicious so we chop them off at the knees :) */
4564 if (LNET_NIDNET(dest_nid) == LNET_NID_NET(&ni->ni_nid)) {
4565 /* should have gone direct */
4566 CERROR("%s, src %s: Bad dest nid %s "
4567 "(should have been sent direct)\n",
4568 libcfs_nid2str(from_nid),
4569 libcfs_nid2str(src_nid),
4570 libcfs_nid2str(dest_nid));
4574 if (lnet_islocalnid4(dest_nid)) {
4575 /* dest is another local NI; sender should have used
4576 * this node's NID on its own network */
4577 CERROR("%s, src %s: Bad dest nid %s "
4578 "(it's my nid but on a different network)\n",
4579 libcfs_nid2str(from_nid),
4580 libcfs_nid2str(src_nid),
4581 libcfs_nid2str(dest_nid));
4585 if (rdma_req && type == LNET_MSG_GET) {
4586 CERROR("%s, src %s: Bad optimized GET for %s "
4587 "(final destination must be me)\n",
4588 libcfs_nid2str(from_nid),
4589 libcfs_nid2str(src_nid),
4590 libcfs_nid2str(dest_nid));
4594 if (!the_lnet.ln_routing) {
4595 CERROR("%s, src %s: Dropping message for %s "
4596 "(routing not enabled)\n",
4597 libcfs_nid2str(from_nid),
4598 libcfs_nid2str(src_nid),
4599 libcfs_nid2str(dest_nid));
4604 /* Message looks OK; we're not going to return an error, so we MUST
4605 * call back lnd_recv() come what may... */
4607 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
4608 fail_peer(src_nid, 0)) { /* shall we now? */
4609 CERROR("%s, src %s: Dropping %s to simulate failure\n",
4610 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4611 lnet_msgtyp2str(type));
4615 /* FIXME need to support large-addr nid */
4616 if (!list_empty(&the_lnet.ln_drop_rules) &&
4617 lnet_drop_rule_match(hdr, lnet_nid_to_nid4(&ni->ni_nid), NULL)) {
4619 "%s, src %s, dst %s: Dropping %s to simulate silent message loss\n",
4620 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4621 libcfs_nid2str(dest_nid), lnet_msgtyp2str(type));
4625 msg = lnet_msg_alloc();
4627 CERROR("%s, src %s: Dropping %s (out of memory)\n",
4628 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4629 lnet_msgtyp2str(type));
4633 /* msg zeroed in lnet_msg_alloc; i.e. flags all clear,
4634 * pointers NULL etc */
4636 msg->msg_type = type;
4637 msg->msg_private = private;
4638 msg->msg_receiving = 1;
4639 msg->msg_rdma_get = rdma_req;
4640 msg->msg_len = msg->msg_wanted = payload_length;
4641 msg->msg_offset = 0;
4642 msg->msg_hdr = *hdr;
4643 /* for building message event */
4644 msg->msg_from = from_nid;
4646 msg->msg_target.pid = dest_pid;
4647 msg->msg_target.nid = dest_nid;
4648 msg->msg_routing = 1;
4651 /* convert common msg->hdr fields to host byteorder */
4652 msg->msg_hdr.type = type;
4653 msg->msg_hdr.src_nid = src_nid;
4654 msg->msg_hdr.src_pid = le32_to_cpu(msg->msg_hdr.src_pid);
4655 msg->msg_hdr.dest_nid = dest_nid;
4656 msg->msg_hdr.dest_pid = dest_pid;
4657 msg->msg_hdr.payload_length = payload_length;
4661 /* FIXME support large-addr nid */
4662 lpni = lnet_nid2peerni_locked(from_nid, lnet_nid_to_nid4(&ni->ni_nid),
4665 lnet_net_unlock(cpt);
4667 CERROR("%s, src %s: Dropping %s (error %d looking up sender)\n",
4668 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4669 lnet_msgtyp2str(type), rc);
4671 if (rc == -ESHUTDOWN)
4672 /* We are shutting down. Don't do anything more */
4677 /* If this message was forwarded to us from a router then we may need
4678 * to update router aliveness or check for an asymmetrical route
4681 if (((lnet_drop_asym_route && for_me) ||
4682 !lpni->lpni_peer_net->lpn_peer->lp_alive) &&
4683 LNET_NIDNET(src_nid) != LNET_NIDNET(from_nid)) {
4684 __u32 src_net_id = LNET_NIDNET(src_nid);
4685 struct lnet_peer *gw = lpni->lpni_peer_net->lpn_peer;
4686 struct lnet_route *route;
4689 list_for_each_entry(route, &gw->lp_routes, lr_gwlist) {
4690 if (route->lr_net == src_net_id) {
4692 /* If we're transitioning the gateway from
4693 * dead -> alive, and discovery is disabled
4694 * locally or on the gateway, then we need to
4695 * update the cached route aliveness for each
4696 * route to the src_nid's net.
4698 * Otherwise, we're only checking for
4699 * symmetrical route, and we can break the
4702 if (!gw->lp_alive &&
4703 lnet_is_discovery_disabled(gw))
4704 lnet_set_route_aliveness(route, true);
4709 if (lnet_drop_asym_route && for_me && !found) {
4710 /* Drop ref taken by lnet_nid2peerni_locked() */
4711 lnet_peer_ni_decref_locked(lpni);
4712 lnet_net_unlock(cpt);
4713 /* we would not use from_nid to route a message to
4715 * => asymmetric routing detected but forbidden
4717 CERROR("%s, src %s: Dropping asymmetrical route %s\n",
4718 libcfs_nid2str(from_nid),
4719 libcfs_nid2str(src_nid), lnet_msgtyp2str(type));
4723 if (!gw->lp_alive) {
4724 struct lnet_peer_net *lpn;
4725 struct lnet_peer_ni *lpni2;
4727 gw->lp_alive = true;
4728 /* Mark all remote NIs on src_nid's net UP */
4729 lpn = lnet_peer_get_net_locked(gw, src_net_id);
4731 list_for_each_entry(lpni2, &lpn->lpn_peer_nis,
4733 lpni2->lpni_ns_status = LNET_NI_STATUS_UP;
4737 lpni->lpni_last_alive = ktime_get_seconds();
4739 msg->msg_rxpeer = lpni;
4741 lnet_ni_addref_locked(ni, cpt);
4742 /* Multi-Rail: Primary NID of source. */
4743 msg->msg_initiator = lnet_peer_primary_nid_locked(src_nid);
4746 * mark the status of this lpni as UP since we received a message
4747 * from it. The ping response reports back the ns_status which is
4748 * marked on the remote as up or down and we cache it here.
4750 msg->msg_rxpeer->lpni_ns_status = LNET_NI_STATUS_UP;
4752 lnet_msg_commit(msg, cpt);
4754 /* message delay simulation */
4755 if (unlikely(!list_empty(&the_lnet.ln_delay_rules) &&
4756 lnet_delay_rule_match_locked(hdr, msg))) {
4757 lnet_net_unlock(cpt);
4762 rc = lnet_parse_forward_locked(ni, msg);
4763 lnet_net_unlock(cpt);
4768 if (rc == LNET_CREDIT_OK) {
4769 lnet_ni_recv(ni, msg->msg_private, msg, 0,
4770 0, payload_length, payload_length);
4775 lnet_net_unlock(cpt);
4777 rc = lnet_parse_local(ni, msg);
4783 LASSERT(msg->msg_md == NULL);
4784 lnet_finalize(msg, rc);
4787 lnet_drop_message(ni, cpt, private, payload_length, type);
4790 EXPORT_SYMBOL(lnet_parse);
4793 lnet_drop_delayed_msg_list(struct list_head *head, char *reason)
4795 while (!list_empty(head)) {
4796 struct lnet_process_id id = {0};
4797 struct lnet_msg *msg;
4799 msg = list_entry(head->next, struct lnet_msg, msg_list);
4800 list_del(&msg->msg_list);
4802 id.nid = msg->msg_hdr.src_nid;
4803 id.pid = msg->msg_hdr.src_pid;
4805 LASSERT(msg->msg_md == NULL);
4806 LASSERT(msg->msg_rx_delayed);
4807 LASSERT(msg->msg_rxpeer != NULL);
4808 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
4810 CWARN("Dropping delayed PUT from %s portal %d match %llu"
4811 " offset %d length %d: %s\n",
4813 msg->msg_hdr.msg.put.ptl_index,
4814 msg->msg_hdr.msg.put.match_bits,
4815 msg->msg_hdr.msg.put.offset,
4816 msg->msg_hdr.payload_length, reason);
4818 /* NB I can't drop msg's ref on msg_rxpeer until after I've
4819 * called lnet_drop_message(), so I just hang onto msg as well
4820 * until that's done */
4822 lnet_drop_message(msg->msg_rxni, msg->msg_rx_cpt,
4823 msg->msg_private, msg->msg_len,
4826 msg->msg_no_resend = true;
4828 * NB: message will not generate event because w/o attached MD,
4829 * but we still should give error code so lnet_msg_decommit()
4830 * can skip counters operations and other checks.
4832 lnet_finalize(msg, -ENOENT);
4837 lnet_recv_delayed_msg_list(struct list_head *head)
4839 while (!list_empty(head)) {
4840 struct lnet_msg *msg;
4841 struct lnet_process_id id;
4843 msg = list_entry(head->next, struct lnet_msg, msg_list);
4844 list_del(&msg->msg_list);
4846 /* md won't disappear under me, since each msg
4847 * holds a ref on it */
4849 id.nid = msg->msg_hdr.src_nid;
4850 id.pid = msg->msg_hdr.src_pid;
4852 LASSERT(msg->msg_rx_delayed);
4853 LASSERT(msg->msg_md != NULL);
4854 LASSERT(msg->msg_rxpeer != NULL);
4855 LASSERT(msg->msg_rxni != NULL);
4856 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
4858 CDEBUG(D_NET, "Resuming delayed PUT from %s portal %d "
4859 "match %llu offset %d length %d.\n",
4860 libcfs_id2str(id), msg->msg_hdr.msg.put.ptl_index,
4861 msg->msg_hdr.msg.put.match_bits,
4862 msg->msg_hdr.msg.put.offset,
4863 msg->msg_hdr.payload_length);
4865 lnet_recv_put(msg->msg_rxni, msg);
4870 lnet_attach_rsp_tracker(struct lnet_rsp_tracker *rspt, int cpt,
4871 struct lnet_libmd *md, struct lnet_handle_md mdh)
4874 struct lnet_rsp_tracker *local_rspt;
4877 * MD has a refcount taken by message so it's not going away.
4878 * The MD however can be looked up. We need to secure the access
4879 * to the md_rspt_ptr by taking the res_lock.
4880 * The rspt can be accessed without protection up to when it gets
4881 * added to the list.
4885 local_rspt = md->md_rspt_ptr;
4886 timeout_ns = lnet_transaction_timeout * NSEC_PER_SEC;
4887 if (local_rspt != NULL) {
4889 * we already have an rspt attached to the md, so we'll
4890 * update the deadline on that one.
4892 lnet_rspt_free(rspt, cpt);
4895 rspt->rspt_mdh = mdh;
4896 rspt->rspt_cpt = cpt;
4897 /* store the rspt so we can access it when we get the REPLY */
4898 md->md_rspt_ptr = rspt;
4901 local_rspt->rspt_deadline = ktime_add_ns(ktime_get(), timeout_ns);
4904 * add to the list of tracked responses. It's added to tail of the
4905 * list in order to expire all the older entries first.
4908 list_move_tail(&local_rspt->rspt_on_list, the_lnet.ln_mt_rstq[cpt]);
4909 lnet_net_unlock(cpt);
4910 lnet_res_unlock(cpt);
4914 * Initiate an asynchronous PUT operation.
4916 * There are several events associated with a PUT: completion of the send on
4917 * the initiator node (LNET_EVENT_SEND), and when the send completes
4918 * successfully, the receipt of an acknowledgment (LNET_EVENT_ACK) indicating
4919 * that the operation was accepted by the target. The event LNET_EVENT_PUT is
4920 * used at the target node to indicate the completion of incoming data
4923 * The local events will be logged in the EQ associated with the MD pointed to
4924 * by \a mdh handle. Using a MD without an associated EQ results in these
4925 * events being discarded. In this case, the caller must have another
4926 * mechanism (e.g., a higher level protocol) for determining when it is safe
4927 * to modify the memory region associated with the MD.
4929 * Note that LNet does not guarantee the order of LNET_EVENT_SEND and
4930 * LNET_EVENT_ACK, though intuitively ACK should happen after SEND.
4932 * \param self Indicates the NID of a local interface through which to send
4933 * the PUT request. Use LNET_NID_ANY to let LNet choose one by itself.
4934 * \param mdh A handle for the MD that describes the memory to be sent. The MD
4935 * must be "free floating" (See LNetMDBind()).
4936 * \param ack Controls whether an acknowledgment is requested.
4937 * Acknowledgments are only sent when they are requested by the initiating
4938 * process and the target MD enables them.
4939 * \param target A process identifier for the target process.
4940 * \param portal The index in the \a target's portal table.
4941 * \param match_bits The match bits to use for MD selection at the target
4943 * \param offset The offset into the target MD (only used when the target
4944 * MD has the LNET_MD_MANAGE_REMOTE option set).
4945 * \param hdr_data 64 bits of user data that can be included in the message
4946 * header. This data is written to an event queue entry at the target if an
4947 * EQ is present on the matching MD.
4949 * \retval 0 Success, and only in this case events will be generated
4950 * and logged to EQ (if it exists).
4951 * \retval -EIO Simulated failure.
4952 * \retval -ENOMEM Memory allocation failure.
4953 * \retval -ENOENT Invalid MD object.
4955 * \see struct lnet_event::hdr_data and lnet_event_kind_t.
4958 LNetPut(lnet_nid_t self, struct lnet_handle_md mdh, enum lnet_ack_req ack,
4959 struct lnet_process_id target, unsigned int portal,
4960 __u64 match_bits, unsigned int offset,
4963 struct lnet_msg *msg;
4964 struct lnet_libmd *md;
4967 struct lnet_rsp_tracker *rspt = NULL;
4969 LASSERT(the_lnet.ln_refcount > 0);
4971 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
4972 fail_peer(target.nid, 1)) { /* shall we now? */
4973 CERROR("Dropping PUT to %s: simulated failure\n",
4974 libcfs_id2str(target));
4978 msg = lnet_msg_alloc();
4980 CERROR("Dropping PUT to %s: ENOMEM on struct lnet_msg\n",
4981 libcfs_id2str(target));
4984 msg->msg_vmflush = !!(current->flags & PF_MEMALLOC);
4986 cpt = lnet_cpt_of_cookie(mdh.cookie);
4988 if (ack == LNET_ACK_REQ) {
4989 rspt = lnet_rspt_alloc(cpt);
4991 CERROR("Dropping PUT to %s: ENOMEM on response tracker\n",
4992 libcfs_id2str(target));
4995 INIT_LIST_HEAD(&rspt->rspt_on_list);
5000 md = lnet_handle2md(&mdh);
5001 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
5002 CERROR("Dropping PUT (%llu:%d:%s): MD (%d) invalid\n",
5003 match_bits, portal, libcfs_id2str(target),
5004 md == NULL ? -1 : md->md_threshold);
5005 if (md != NULL && md->md_me != NULL)
5006 CERROR("Source MD also attached to portal %d\n",
5007 md->md_me->me_portal);
5008 lnet_res_unlock(cpt);
5011 lnet_rspt_free(rspt, cpt);
5017 CDEBUG(D_NET, "LNetPut -> %s\n", libcfs_id2str(target));
5019 lnet_msg_attach_md(msg, md, 0, 0);
5021 lnet_prep_send(msg, LNET_MSG_PUT, target, 0, md->md_length);
5023 msg->msg_hdr.msg.put.match_bits = cpu_to_le64(match_bits);
5024 msg->msg_hdr.msg.put.ptl_index = cpu_to_le32(portal);
5025 msg->msg_hdr.msg.put.offset = cpu_to_le32(offset);
5026 msg->msg_hdr.msg.put.hdr_data = hdr_data;
5028 /* NB handles only looked up by creator (no flips) */
5029 if (ack == LNET_ACK_REQ) {
5030 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
5031 the_lnet.ln_interface_cookie;
5032 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
5033 md->md_lh.lh_cookie;
5035 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
5036 LNET_WIRE_HANDLE_COOKIE_NONE;
5037 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
5038 LNET_WIRE_HANDLE_COOKIE_NONE;
5041 lnet_res_unlock(cpt);
5043 lnet_build_msg_event(msg, LNET_EVENT_SEND);
5045 if (rspt && lnet_response_tracking_enabled(LNET_MSG_PUT,
5047 lnet_attach_rsp_tracker(rspt, cpt, md, mdh);
5049 lnet_rspt_free(rspt, cpt);
5051 if (CFS_FAIL_CHECK_ORSET(CFS_FAIL_PTLRPC_OST_BULK_CB2,
5055 rc = lnet_send(self, msg, LNET_NID_ANY);
5058 CNETERR("Error sending PUT to %s: %d\n",
5059 libcfs_id2str(target), rc);
5060 msg->msg_no_resend = true;
5061 lnet_finalize(msg, rc);
5064 /* completion will be signalled by an event */
5067 EXPORT_SYMBOL(LNetPut);
5070 * The LND can DMA direct to the GET md (i.e. no REPLY msg). This
5071 * returns a msg for the LND to pass to lnet_finalize() when the sink
5072 * data has been received.
5074 * CAVEAT EMPTOR: 'getmsg' is the original GET, which is freed when
5075 * lnet_finalize() is called on it, so the LND must call this first
5078 lnet_create_reply_msg(struct lnet_ni *ni, struct lnet_msg *getmsg)
5080 struct lnet_msg *msg = lnet_msg_alloc();
5081 struct lnet_libmd *getmd = getmsg->msg_md;
5082 struct lnet_process_id peer_id = getmsg->msg_target;
5085 LASSERT(!getmsg->msg_target_is_router);
5086 LASSERT(!getmsg->msg_routing);
5089 CERROR("%s: Dropping REPLY from %s: can't allocate msg\n",
5090 libcfs_nidstr(&ni->ni_nid), libcfs_id2str(peer_id));
5094 cpt = lnet_cpt_of_cookie(getmd->md_lh.lh_cookie);
5097 LASSERT(getmd->md_refcount > 0);
5099 if (getmd->md_threshold == 0) {
5100 CERROR("%s: Dropping REPLY from %s for inactive MD %p\n",
5101 libcfs_nidstr(&ni->ni_nid), libcfs_id2str(peer_id),
5103 lnet_res_unlock(cpt);
5107 LASSERT(getmd->md_offset == 0);
5109 CDEBUG(D_NET, "%s: Reply from %s md %p\n",
5110 libcfs_nidstr(&ni->ni_nid), libcfs_id2str(peer_id), getmd);
5112 /* setup information for lnet_build_msg_event */
5113 msg->msg_initiator =
5114 lnet_nid_to_nid4(&getmsg->msg_txpeer->lpni_peer_net->lpn_peer->lp_primary_nid);
5115 msg->msg_from = peer_id.nid;
5116 msg->msg_type = LNET_MSG_GET; /* flag this msg as an "optimized" GET */
5117 msg->msg_hdr.src_nid = peer_id.nid;
5118 msg->msg_hdr.payload_length = getmd->md_length;
5119 msg->msg_receiving = 1; /* required by lnet_msg_attach_md */
5121 lnet_msg_attach_md(msg, getmd, getmd->md_offset, getmd->md_length);
5122 lnet_res_unlock(cpt);
5124 cpt = lnet_cpt_of_nid(peer_id.nid, ni);
5127 lnet_msg_commit(msg, cpt);
5128 lnet_net_unlock(cpt);
5130 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
5135 cpt = lnet_cpt_of_nid(peer_id.nid, ni);
5138 lnet_incr_stats(&ni->ni_stats, LNET_MSG_GET, LNET_STATS_TYPE_DROP);
5139 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
5140 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length +=
5142 lnet_net_unlock(cpt);
5149 EXPORT_SYMBOL(lnet_create_reply_msg);
5152 lnet_set_reply_msg_len(struct lnet_ni *ni, struct lnet_msg *reply,
5155 /* Set the REPLY length, now the RDMA that elides the REPLY message has
5156 * completed and I know it. */
5157 LASSERT(reply != NULL);
5158 LASSERT(reply->msg_type == LNET_MSG_GET);
5159 LASSERT(reply->msg_ev.type == LNET_EVENT_REPLY);
5161 /* NB I trusted my peer to RDMA. If she tells me she's written beyond
5162 * the end of my buffer, I might as well be dead. */
5163 LASSERT(len <= reply->msg_ev.mlength);
5165 reply->msg_ev.mlength = len;
5167 EXPORT_SYMBOL(lnet_set_reply_msg_len);
5170 * Initiate an asynchronous GET operation.
5172 * On the initiator node, an LNET_EVENT_SEND is logged when the GET request
5173 * is sent, and an LNET_EVENT_REPLY is logged when the data returned from
5174 * the target node in the REPLY has been written to local MD.
5176 * On the target node, an LNET_EVENT_GET is logged when the GET request
5177 * arrives and is accepted into a MD.
5179 * \param self,target,portal,match_bits,offset See the discussion in LNetPut().
5180 * \param mdh A handle for the MD that describes the memory into which the
5181 * requested data will be received. The MD must be "free floating" (See LNetMDBind()).
5183 * \retval 0 Success, and only in this case events will be generated
5184 * and logged to EQ (if it exists) of the MD.
5185 * \retval -EIO Simulated failure.
5186 * \retval -ENOMEM Memory allocation failure.
5187 * \retval -ENOENT Invalid MD object.
5190 LNetGet(lnet_nid_t self, struct lnet_handle_md mdh,
5191 struct lnet_process_id target, unsigned int portal,
5192 __u64 match_bits, unsigned int offset, bool recovery)
5194 struct lnet_msg *msg;
5195 struct lnet_libmd *md;
5196 struct lnet_rsp_tracker *rspt;
5200 LASSERT(the_lnet.ln_refcount > 0);
5202 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
5203 fail_peer(target.nid, 1)) /* shall we now? */
5205 CERROR("Dropping GET to %s: simulated failure\n",
5206 libcfs_id2str(target));
5210 msg = lnet_msg_alloc();
5212 CERROR("Dropping GET to %s: ENOMEM on struct lnet_msg\n",
5213 libcfs_id2str(target));
5217 cpt = lnet_cpt_of_cookie(mdh.cookie);
5219 rspt = lnet_rspt_alloc(cpt);
5221 CERROR("Dropping GET to %s: ENOMEM on response tracker\n",
5222 libcfs_id2str(target));
5225 INIT_LIST_HEAD(&rspt->rspt_on_list);
5227 msg->msg_recovery = recovery;
5231 md = lnet_handle2md(&mdh);
5232 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
5233 CERROR("Dropping GET (%llu:%d:%s): MD (%d) invalid\n",
5234 match_bits, portal, libcfs_id2str(target),
5235 md == NULL ? -1 : md->md_threshold);
5236 if (md != NULL && md->md_me != NULL)
5237 CERROR("REPLY MD also attached to portal %d\n",
5238 md->md_me->me_portal);
5240 lnet_res_unlock(cpt);
5243 lnet_rspt_free(rspt, cpt);
5247 CDEBUG(D_NET, "LNetGet -> %s\n", libcfs_id2str(target));
5249 lnet_msg_attach_md(msg, md, 0, 0);
5251 lnet_prep_send(msg, LNET_MSG_GET, target, 0, 0);
5253 msg->msg_hdr.msg.get.match_bits = cpu_to_le64(match_bits);
5254 msg->msg_hdr.msg.get.ptl_index = cpu_to_le32(portal);
5255 msg->msg_hdr.msg.get.src_offset = cpu_to_le32(offset);
5256 msg->msg_hdr.msg.get.sink_length = cpu_to_le32(md->md_length);
5258 /* NB handles only looked up by creator (no flips) */
5259 msg->msg_hdr.msg.get.return_wmd.wh_interface_cookie =
5260 the_lnet.ln_interface_cookie;
5261 msg->msg_hdr.msg.get.return_wmd.wh_object_cookie =
5262 md->md_lh.lh_cookie;
5264 lnet_res_unlock(cpt);
5266 lnet_build_msg_event(msg, LNET_EVENT_SEND);
5268 if (lnet_response_tracking_enabled(LNET_MSG_GET, md->md_options))
5269 lnet_attach_rsp_tracker(rspt, cpt, md, mdh);
5271 lnet_rspt_free(rspt, cpt);
5273 rc = lnet_send(self, msg, LNET_NID_ANY);
5275 CNETERR("Error sending GET to %s: %d\n",
5276 libcfs_id2str(target), rc);
5277 msg->msg_no_resend = true;
5278 lnet_finalize(msg, rc);
5281 /* completion will be signalled by an event */
5284 EXPORT_SYMBOL(LNetGet);
5287 * Calculate distance to node at \a dstnid.
5289 * \param dstnid Target NID.
5290 * \param srcnidp If not NULL, NID of the local interface to reach \a dstnid
5292 * \param orderp If not NULL, order of the route to reach \a dstnid is saved
5295 * \retval 0 If \a dstnid belongs to a local interface, and reserved option
5296 * local_nid_dist_zero is set, which is the default.
5297 * \retval positives Distance to target NID, i.e. number of hops plus one.
5298 * \retval -EHOSTUNREACH If \a dstnid is not reachable.
5301 LNetDist(lnet_nid_t dstnid, lnet_nid_t *srcnidp, __u32 *orderp)
5303 struct list_head *e;
5304 struct lnet_ni *ni = NULL;
5305 struct lnet_remotenet *rnet;
5306 __u32 dstnet = LNET_NIDNET(dstnid);
5310 struct list_head *rn_list;
5311 bool matched_dstnet = false;
5313 /* if !local_nid_dist_zero, I don't return a distance of 0 ever
5314 * (when lustre sees a distance of 0, it substitutes 0@lo), so I
5315 * keep order 0 free for 0@lo and order 1 free for a local NID
5318 LASSERT(the_lnet.ln_refcount > 0);
5320 cpt = lnet_net_lock_current();
5322 while ((ni = lnet_get_next_ni_locked(NULL, ni))) {
5323 /* FIXME support large-addr nid */
5324 if (lnet_nid_to_nid4(&ni->ni_nid) == dstnid) {
5325 if (srcnidp != NULL)
5327 if (orderp != NULL) {
5328 if (dstnid == LNET_NID_LO_0)
5333 lnet_net_unlock(cpt);
5335 return local_nid_dist_zero ? 0 : 1;
5338 if (!matched_dstnet && LNET_NID_NET(&ni->ni_nid) == dstnet) {
5339 matched_dstnet = true;
5340 /* We matched the destination net, but we may have
5341 * additional local NIs to inspect.
5343 * We record the nid and order as appropriate, but
5344 * they may be overwritten if we match local NI above.
5347 /* FIXME support large-addr nids */
5348 *srcnidp = lnet_nid_to_nid4(&ni->ni_nid);
5351 /* Check if ni was originally created in
5352 * current net namespace.
5353 * If not, assign order above 0xffff0000,
5354 * to make this ni not a priority.
5356 if (current->nsproxy &&
5357 !net_eq(ni->ni_net_ns,
5358 current->nsproxy->net_ns))
5359 *orderp = order + 0xffff0000;
5368 if (matched_dstnet) {
5369 lnet_net_unlock(cpt);
5373 rn_list = lnet_net2rnethash(dstnet);
5374 list_for_each(e, rn_list) {
5375 rnet = list_entry(e, struct lnet_remotenet, lrn_list);
5377 if (rnet->lrn_net == dstnet) {
5378 struct lnet_route *route;
5379 struct lnet_route *shortest = NULL;
5380 __u32 shortest_hops = LNET_UNDEFINED_HOPS;
5383 LASSERT(!list_empty(&rnet->lrn_routes));
5385 list_for_each_entry(route, &rnet->lrn_routes,
5387 route_hops = route->lr_hops;
5388 if (route_hops == LNET_UNDEFINED_HOPS)
5390 if (shortest == NULL ||
5391 route_hops < shortest_hops) {
5393 shortest_hops = route_hops;
5397 LASSERT(shortest != NULL);
5398 hops = shortest_hops;
5399 if (srcnidp != NULL) {
5400 struct lnet_net *net;
5401 net = lnet_get_net_locked(shortest->lr_lnet);
5403 ni = lnet_get_next_ni_locked(net, NULL);
5404 /* FIXME support large-addr nids */
5405 *srcnidp = lnet_nid_to_nid4(&ni->ni_nid);
5409 lnet_net_unlock(cpt);
5415 lnet_net_unlock(cpt);
5416 return -EHOSTUNREACH;
5418 EXPORT_SYMBOL(LNetDist);