4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lnet/lnet/lib-move.c
34 * Data movement routines
37 #define DEBUG_SUBSYSTEM S_LNET
39 #include <linux/pagemap.h>
41 #include <lnet/lib-lnet.h>
42 #include <linux/nsproxy.h>
43 #include <net/net_namespace.h>
45 static int local_nid_dist_zero = 1;
46 module_param(local_nid_dist_zero, int, 0444);
47 MODULE_PARM_DESC(local_nid_dist_zero, "Reserved");
49 struct lnet_send_data {
50 struct lnet_ni *sd_best_ni;
51 struct lnet_peer_ni *sd_best_lpni;
52 struct lnet_peer_ni *sd_final_dst_lpni;
53 struct lnet_peer *sd_peer;
54 struct lnet_peer *sd_gw_peer;
55 struct lnet_peer_ni *sd_gw_lpni;
56 struct lnet_peer_net *sd_peer_net;
57 struct lnet_msg *sd_msg;
58 lnet_nid_t sd_dst_nid;
59 lnet_nid_t sd_src_nid;
60 lnet_nid_t sd_rtr_nid;
67 lnet_msg_is_response(struct lnet_msg *msg)
69 return msg->msg_type == LNET_MSG_ACK || msg->msg_type == LNET_MSG_REPLY;
72 static inline struct lnet_comm_count *
73 get_stats_counts(struct lnet_element_stats *stats,
74 enum lnet_stats_type stats_type)
77 case LNET_STATS_TYPE_SEND:
78 return &stats->el_send_stats;
79 case LNET_STATS_TYPE_RECV:
80 return &stats->el_recv_stats;
81 case LNET_STATS_TYPE_DROP:
82 return &stats->el_drop_stats;
84 CERROR("Unknown stats type\n");
90 void lnet_incr_stats(struct lnet_element_stats *stats,
91 enum lnet_msg_type msg_type,
92 enum lnet_stats_type stats_type)
94 struct lnet_comm_count *counts = get_stats_counts(stats, stats_type);
100 atomic_inc(&counts->co_ack_count);
103 atomic_inc(&counts->co_put_count);
106 atomic_inc(&counts->co_get_count);
109 atomic_inc(&counts->co_reply_count);
112 atomic_inc(&counts->co_hello_count);
115 CERROR("There is a BUG in the code. Unknown message type\n");
120 __u32 lnet_sum_stats(struct lnet_element_stats *stats,
121 enum lnet_stats_type stats_type)
123 struct lnet_comm_count *counts = get_stats_counts(stats, stats_type);
127 return (atomic_read(&counts->co_ack_count) +
128 atomic_read(&counts->co_put_count) +
129 atomic_read(&counts->co_get_count) +
130 atomic_read(&counts->co_reply_count) +
131 atomic_read(&counts->co_hello_count));
134 static inline void assign_stats(struct lnet_ioctl_comm_count *msg_stats,
135 struct lnet_comm_count *counts)
137 msg_stats->ico_get_count = atomic_read(&counts->co_get_count);
138 msg_stats->ico_put_count = atomic_read(&counts->co_put_count);
139 msg_stats->ico_reply_count = atomic_read(&counts->co_reply_count);
140 msg_stats->ico_ack_count = atomic_read(&counts->co_ack_count);
141 msg_stats->ico_hello_count = atomic_read(&counts->co_hello_count);
144 void lnet_usr_translate_stats(struct lnet_ioctl_element_msg_stats *msg_stats,
145 struct lnet_element_stats *stats)
147 struct lnet_comm_count *counts;
152 counts = get_stats_counts(stats, LNET_STATS_TYPE_SEND);
155 assign_stats(&msg_stats->im_send_stats, counts);
157 counts = get_stats_counts(stats, LNET_STATS_TYPE_RECV);
160 assign_stats(&msg_stats->im_recv_stats, counts);
162 counts = get_stats_counts(stats, LNET_STATS_TYPE_DROP);
165 assign_stats(&msg_stats->im_drop_stats, counts);
169 lnet_fail_nid(lnet_nid_t nid, unsigned int threshold)
171 struct lnet_test_peer *tp;
172 struct list_head *el;
173 struct list_head *next;
176 /* NB: use lnet_net_lock(0) to serialize operations on test peers */
177 if (threshold != 0) {
178 /* Adding a new entry */
179 LIBCFS_ALLOC(tp, sizeof(*tp));
184 tp->tp_threshold = threshold;
187 list_add_tail(&tp->tp_list, &the_lnet.ln_test_peers);
194 list_for_each_safe(el, next, &the_lnet.ln_test_peers) {
195 tp = list_entry(el, struct lnet_test_peer, tp_list);
197 if (tp->tp_threshold == 0 || /* needs culling anyway */
198 nid == LNET_NID_ANY || /* removing all entries */
199 tp->tp_nid == nid) { /* matched this one */
200 list_move(&tp->tp_list, &cull);
206 while (!list_empty(&cull)) {
207 tp = list_entry(cull.next, struct lnet_test_peer, tp_list);
209 list_del(&tp->tp_list);
210 LIBCFS_FREE(tp, sizeof(*tp));
216 fail_peer (lnet_nid_t nid, int outgoing)
218 struct lnet_test_peer *tp;
219 struct list_head *el;
220 struct list_head *next;
224 /* NB: use lnet_net_lock(0) to serialize operations on test peers */
227 list_for_each_safe(el, next, &the_lnet.ln_test_peers) {
228 tp = list_entry(el, struct lnet_test_peer, tp_list);
230 if (tp->tp_threshold == 0) {
233 /* only cull zombies on outgoing tests,
234 * since we may be at interrupt priority on
235 * incoming messages. */
236 list_move(&tp->tp_list, &cull);
241 if (tp->tp_nid == LNET_NID_ANY || /* fail every peer */
242 nid == tp->tp_nid) { /* fail this peer */
245 if (tp->tp_threshold != LNET_MD_THRESH_INF) {
248 tp->tp_threshold == 0) {
250 list_move(&tp->tp_list, &cull);
259 while (!list_empty(&cull)) {
260 tp = list_entry(cull.next, struct lnet_test_peer, tp_list);
261 list_del(&tp->tp_list);
263 LIBCFS_FREE(tp, sizeof(*tp));
270 lnet_iov_nob(unsigned int niov, struct kvec *iov)
272 unsigned int nob = 0;
274 LASSERT(niov == 0 || iov != NULL);
276 nob += (iov++)->iov_len;
280 EXPORT_SYMBOL(lnet_iov_nob);
283 lnet_copy_iov2iov(unsigned int ndiov, struct kvec *diov, unsigned int doffset,
284 unsigned int nsiov, struct kvec *siov, unsigned int soffset,
287 /* NB diov, siov are READ-ONLY */
288 unsigned int this_nob;
293 /* skip complete frags before 'doffset' */
295 while (doffset >= diov->iov_len) {
296 doffset -= diov->iov_len;
302 /* skip complete frags before 'soffset' */
304 while (soffset >= siov->iov_len) {
305 soffset -= siov->iov_len;
314 this_nob = min3((unsigned int)diov->iov_len - doffset,
315 (unsigned int)siov->iov_len - soffset,
318 memcpy((char *)diov->iov_base + doffset,
319 (char *)siov->iov_base + soffset, this_nob);
322 if (diov->iov_len > doffset + this_nob) {
330 if (siov->iov_len > soffset + this_nob) {
339 EXPORT_SYMBOL(lnet_copy_iov2iov);
342 lnet_kiov_nob(unsigned int niov, struct bio_vec *kiov)
344 unsigned int nob = 0;
346 LASSERT(niov == 0 || kiov != NULL);
348 nob += (kiov++)->bv_len;
352 EXPORT_SYMBOL(lnet_kiov_nob);
355 lnet_copy_kiov2kiov(unsigned int ndiov, struct bio_vec *diov,
356 unsigned int doffset,
357 unsigned int nsiov, struct bio_vec *siov,
358 unsigned int soffset,
361 /* NB diov, siov are READ-ONLY */
362 unsigned int this_nob;
369 LASSERT (!in_interrupt ());
372 while (doffset >= diov->bv_len) {
373 doffset -= diov->bv_len;
380 while (soffset >= siov->bv_len) {
381 soffset -= siov->bv_len;
390 this_nob = min3(diov->bv_len - doffset,
391 siov->bv_len - soffset,
395 daddr = ((char *)kmap(diov->bv_page)) +
396 diov->bv_offset + doffset;
398 saddr = ((char *)kmap(siov->bv_page)) +
399 siov->bv_offset + soffset;
401 /* Vanishing risk of kmap deadlock when mapping 2 pages.
402 * However in practice at least one of the kiovs will be mapped
403 * kernel pages and the map/unmap will be NOOPs */
405 memcpy (daddr, saddr, this_nob);
408 if (diov->bv_len > doffset + this_nob) {
412 kunmap(diov->bv_page);
419 if (siov->bv_len > soffset + this_nob) {
423 kunmap(siov->bv_page);
432 kunmap(diov->bv_page);
434 kunmap(siov->bv_page);
436 EXPORT_SYMBOL(lnet_copy_kiov2kiov);
439 lnet_copy_kiov2iov (unsigned int niov, struct kvec *iov, unsigned int iovoffset,
440 unsigned int nkiov, struct bio_vec *kiov,
441 unsigned int kiovoffset,
444 /* NB iov, kiov are READ-ONLY */
445 unsigned int this_nob;
451 LASSERT (!in_interrupt ());
454 while (iovoffset >= iov->iov_len) {
455 iovoffset -= iov->iov_len;
462 while (kiovoffset >= kiov->bv_len) {
463 kiovoffset -= kiov->bv_len;
472 this_nob = min3((unsigned int)iov->iov_len - iovoffset,
473 (unsigned int)kiov->bv_len - kiovoffset,
477 addr = ((char *)kmap(kiov->bv_page)) +
478 kiov->bv_offset + kiovoffset;
480 memcpy((char *)iov->iov_base + iovoffset, addr, this_nob);
483 if (iov->iov_len > iovoffset + this_nob) {
484 iovoffset += this_nob;
491 if (kiov->bv_len > kiovoffset + this_nob) {
493 kiovoffset += this_nob;
495 kunmap(kiov->bv_page);
505 kunmap(kiov->bv_page);
507 EXPORT_SYMBOL(lnet_copy_kiov2iov);
510 lnet_copy_iov2kiov(unsigned int nkiov, struct bio_vec *kiov,
511 unsigned int kiovoffset,
512 unsigned int niov, struct kvec *iov, unsigned int iovoffset,
515 /* NB kiov, iov are READ-ONLY */
516 unsigned int this_nob;
522 LASSERT (!in_interrupt ());
525 while (kiovoffset >= kiov->bv_len) {
526 kiovoffset -= kiov->bv_len;
533 while (iovoffset >= iov->iov_len) {
534 iovoffset -= iov->iov_len;
543 this_nob = min3((unsigned int)kiov->bv_len - kiovoffset,
544 (unsigned int)iov->iov_len - iovoffset,
548 addr = ((char *)kmap(kiov->bv_page)) +
549 kiov->bv_offset + kiovoffset;
551 memcpy (addr, (char *)iov->iov_base + iovoffset, this_nob);
554 if (kiov->bv_len > kiovoffset + this_nob) {
556 kiovoffset += this_nob;
558 kunmap(kiov->bv_page);
565 if (iov->iov_len > iovoffset + this_nob) {
566 iovoffset += this_nob;
575 kunmap(kiov->bv_page);
577 EXPORT_SYMBOL(lnet_copy_iov2kiov);
580 lnet_extract_kiov(int dst_niov, struct bio_vec *dst,
581 int src_niov, struct bio_vec *src,
582 unsigned int offset, unsigned int len)
584 /* Initialise 'dst' to the subset of 'src' starting at 'offset',
585 * for exactly 'len' bytes, and return the number of entries.
586 * NB not destructive to 'src' */
587 unsigned int frag_len;
590 if (len == 0) /* no data => */
591 return (0); /* no frags */
593 LASSERT(src_niov > 0);
594 while (offset >= src->bv_len) { /* skip initial frags */
595 offset -= src->bv_len;
598 LASSERT(src_niov > 0);
603 LASSERT(src_niov > 0);
604 LASSERT((int)niov <= dst_niov);
606 frag_len = src->bv_len - offset;
607 dst->bv_page = src->bv_page;
608 dst->bv_offset = src->bv_offset + offset;
610 if (len <= frag_len) {
612 LASSERT(dst->bv_offset + dst->bv_len <= PAGE_SIZE);
616 dst->bv_len = frag_len;
617 LASSERT(dst->bv_offset + dst->bv_len <= PAGE_SIZE);
627 EXPORT_SYMBOL(lnet_extract_kiov);
630 lnet_ni_recv(struct lnet_ni *ni, void *private, struct lnet_msg *msg,
631 int delayed, unsigned int offset, unsigned int mlen,
634 unsigned int niov = 0;
635 struct kvec *iov = NULL;
636 struct bio_vec *kiov = NULL;
639 LASSERT (!in_interrupt ());
640 LASSERT (mlen == 0 || msg != NULL);
643 LASSERT(msg->msg_receiving);
644 LASSERT(!msg->msg_sending);
645 LASSERT(rlen == msg->msg_len);
646 LASSERT(mlen <= msg->msg_len);
647 LASSERT(msg->msg_offset == offset);
648 LASSERT(msg->msg_wanted == mlen);
650 msg->msg_receiving = 0;
653 niov = msg->msg_niov;
654 kiov = msg->msg_kiov;
657 LASSERT ((iov == NULL) != (kiov == NULL));
661 rc = (ni->ni_net->net_lnd->lnd_recv)(ni, private, msg, delayed,
662 niov, kiov, offset, mlen,
665 lnet_finalize(msg, rc);
669 lnet_setpayloadbuffer(struct lnet_msg *msg)
671 struct lnet_libmd *md = msg->msg_md;
673 LASSERT(msg->msg_len > 0);
674 LASSERT(!msg->msg_routing);
676 LASSERT(msg->msg_niov == 0);
677 LASSERT(msg->msg_kiov == NULL);
679 msg->msg_niov = md->md_niov;
680 msg->msg_kiov = md->md_kiov;
684 lnet_prep_send(struct lnet_msg *msg, int type, struct lnet_process_id target,
685 unsigned int offset, unsigned int len)
687 msg->msg_type = type;
688 msg->msg_target = target;
690 msg->msg_offset = offset;
693 lnet_setpayloadbuffer(msg);
695 memset (&msg->msg_hdr, 0, sizeof (msg->msg_hdr));
696 msg->msg_hdr.type = cpu_to_le32(type);
697 /* dest_nid will be overwritten by lnet_select_pathway() */
698 msg->msg_hdr.dest_nid = cpu_to_le64(target.nid);
699 msg->msg_hdr.dest_pid = cpu_to_le32(target.pid);
700 /* src_nid will be set later */
701 msg->msg_hdr.src_pid = cpu_to_le32(the_lnet.ln_pid);
702 msg->msg_hdr.payload_length = cpu_to_le32(len);
706 lnet_ni_send(struct lnet_ni *ni, struct lnet_msg *msg)
708 void *priv = msg->msg_private;
711 LASSERT(!in_interrupt());
712 LASSERT(ni->ni_nid == LNET_NID_LO_0 ||
713 (msg->msg_txcredit && msg->msg_peertxcredit));
715 rc = (ni->ni_net->net_lnd->lnd_send)(ni, priv, msg);
717 msg->msg_no_resend = true;
718 lnet_finalize(msg, rc);
723 lnet_ni_eager_recv(struct lnet_ni *ni, struct lnet_msg *msg)
727 LASSERT(!msg->msg_sending);
728 LASSERT(msg->msg_receiving);
729 LASSERT(!msg->msg_rx_ready_delay);
730 LASSERT(ni->ni_net->net_lnd->lnd_eager_recv != NULL);
732 msg->msg_rx_ready_delay = 1;
733 rc = (ni->ni_net->net_lnd->lnd_eager_recv)(ni, msg->msg_private, msg,
736 CERROR("recv from %s / send to %s aborted: "
737 "eager_recv failed %d\n",
738 libcfs_nid2str(msg->msg_rxpeer->lpni_nid),
739 libcfs_id2str(msg->msg_target), rc);
740 LASSERT(rc < 0); /* required by my callers */
747 lnet_is_peer_deadline_passed(struct lnet_peer_ni *lpni, time64_t now)
751 deadline = lpni->lpni_last_alive +
752 lpni->lpni_net->net_tunables.lct_peer_timeout;
755 * assume peer_ni is alive as long as we're within the configured
764 /* NB: returns 1 when alive, 0 when dead, negative when error;
765 * may drop the lnet_net_lock */
767 lnet_peer_alive_locked(struct lnet_ni *ni, struct lnet_peer_ni *lpni,
768 struct lnet_msg *msg)
770 time64_t now = ktime_get_seconds();
772 if (!lnet_peer_aliveness_enabled(lpni))
776 * If we're resending a message, let's attempt to send it even if
777 * the peer is down to fulfill our resend quota on the message
779 if (msg->msg_retry_count > 0)
782 /* try and send recovery messages irregardless */
783 if (msg->msg_recovery)
786 /* always send any responses */
787 if (lnet_msg_is_response(msg))
790 if (!lnet_is_peer_deadline_passed(lpni, now))
793 return lnet_is_peer_ni_alive(lpni);
797 * \param msg The message to be sent.
798 * \param do_send True if lnet_ni_send() should be called in this function.
799 * lnet_send() is going to lnet_net_unlock immediately after this, so
800 * it sets do_send FALSE and I don't do the unlock/send/lock bit.
802 * \retval LNET_CREDIT_OK If \a msg sent or OK to send.
803 * \retval LNET_CREDIT_WAIT If \a msg blocked for credit.
804 * \retval -EHOSTUNREACH If the next hop of the message appears dead.
805 * \retval -ECANCELED If the MD of the message has been unlinked.
808 lnet_post_send_locked(struct lnet_msg *msg, int do_send)
810 struct lnet_peer_ni *lp = msg->msg_txpeer;
811 struct lnet_ni *ni = msg->msg_txni;
812 int cpt = msg->msg_tx_cpt;
813 struct lnet_tx_queue *tq = ni->ni_tx_queues[cpt];
815 /* non-lnet_send() callers have checked before */
816 LASSERT(!do_send || msg->msg_tx_delayed);
817 LASSERT(!msg->msg_receiving);
818 LASSERT(msg->msg_tx_committed);
819 /* can't get here if we're sending to the loopback interface */
820 LASSERT(lp->lpni_nid != the_lnet.ln_loni->ni_nid);
822 /* NB 'lp' is always the next hop */
823 if ((msg->msg_target.pid & LNET_PID_USERFLAG) == 0 &&
824 lnet_peer_alive_locked(ni, lp, msg) == 0) {
825 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
826 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length +=
828 lnet_net_unlock(cpt);
830 lnet_incr_stats(&msg->msg_txpeer->lpni_stats,
832 LNET_STATS_TYPE_DROP);
834 lnet_incr_stats(&msg->msg_txni->ni_stats,
836 LNET_STATS_TYPE_DROP);
838 CNETERR("Dropping message for %s: peer not alive\n",
839 libcfs_id2str(msg->msg_target));
840 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_DROPPED;
842 lnet_finalize(msg, -EHOSTUNREACH);
845 return -EHOSTUNREACH;
848 if (msg->msg_md != NULL &&
849 (msg->msg_md->md_flags & LNET_MD_FLAG_ABORTED) != 0) {
850 lnet_net_unlock(cpt);
852 CNETERR("Aborting message for %s: LNetM[DE]Unlink() already "
853 "called on the MD/ME.\n",
854 libcfs_id2str(msg->msg_target));
856 msg->msg_no_resend = true;
857 CDEBUG(D_NET, "msg %p to %s canceled and will not be resent\n",
858 msg, libcfs_id2str(msg->msg_target));
859 lnet_finalize(msg, -ECANCELED);
866 if (!msg->msg_peertxcredit) {
867 spin_lock(&lp->lpni_lock);
868 LASSERT((lp->lpni_txcredits < 0) ==
869 !list_empty(&lp->lpni_txq));
871 msg->msg_peertxcredit = 1;
872 lp->lpni_txqnob += msg->msg_len + sizeof(struct lnet_hdr);
873 lp->lpni_txcredits--;
875 if (lp->lpni_txcredits < lp->lpni_mintxcredits)
876 lp->lpni_mintxcredits = lp->lpni_txcredits;
878 if (lp->lpni_txcredits < 0) {
879 msg->msg_tx_delayed = 1;
880 list_add_tail(&msg->msg_list, &lp->lpni_txq);
881 spin_unlock(&lp->lpni_lock);
882 return LNET_CREDIT_WAIT;
884 spin_unlock(&lp->lpni_lock);
887 if (!msg->msg_txcredit) {
888 LASSERT((tq->tq_credits < 0) ==
889 !list_empty(&tq->tq_delayed));
891 msg->msg_txcredit = 1;
893 atomic_dec(&ni->ni_tx_credits);
895 if (tq->tq_credits < tq->tq_credits_min)
896 tq->tq_credits_min = tq->tq_credits;
898 if (tq->tq_credits < 0) {
899 msg->msg_tx_delayed = 1;
900 list_add_tail(&msg->msg_list, &tq->tq_delayed);
901 return LNET_CREDIT_WAIT;
905 /* unset the tx_delay flag as we're going to send it now */
906 msg->msg_tx_delayed = 0;
909 lnet_net_unlock(cpt);
910 lnet_ni_send(ni, msg);
913 return LNET_CREDIT_OK;
917 static struct lnet_rtrbufpool *
918 lnet_msg2bufpool(struct lnet_msg *msg)
920 struct lnet_rtrbufpool *rbp;
923 LASSERT(msg->msg_rx_committed);
925 cpt = msg->msg_rx_cpt;
926 rbp = &the_lnet.ln_rtrpools[cpt][0];
928 LASSERT(msg->msg_len <= LNET_MTU);
929 while (msg->msg_len > (unsigned int)rbp->rbp_npages * PAGE_SIZE) {
931 LASSERT(rbp < &the_lnet.ln_rtrpools[cpt][LNET_NRBPOOLS]);
938 lnet_post_routed_recv_locked(struct lnet_msg *msg, int do_recv)
940 /* lnet_parse is going to lnet_net_unlock immediately after this, so it
941 * sets do_recv FALSE and I don't do the unlock/send/lock bit.
942 * I return LNET_CREDIT_WAIT if msg blocked and LNET_CREDIT_OK if
943 * received or OK to receive */
944 struct lnet_peer_ni *lpni = msg->msg_rxpeer;
945 struct lnet_peer *lp;
946 struct lnet_rtrbufpool *rbp;
947 struct lnet_rtrbuf *rb;
949 LASSERT(msg->msg_kiov == NULL);
950 LASSERT(msg->msg_niov == 0);
951 LASSERT(msg->msg_routing);
952 LASSERT(msg->msg_receiving);
953 LASSERT(!msg->msg_sending);
954 LASSERT(lpni->lpni_peer_net);
955 LASSERT(lpni->lpni_peer_net->lpn_peer);
957 lp = lpni->lpni_peer_net->lpn_peer;
959 /* non-lnet_parse callers only receive delayed messages */
960 LASSERT(!do_recv || msg->msg_rx_delayed);
962 if (!msg->msg_peerrtrcredit) {
963 /* lpni_lock protects the credit manipulation */
964 spin_lock(&lpni->lpni_lock);
966 msg->msg_peerrtrcredit = 1;
967 lpni->lpni_rtrcredits--;
968 if (lpni->lpni_rtrcredits < lpni->lpni_minrtrcredits)
969 lpni->lpni_minrtrcredits = lpni->lpni_rtrcredits;
971 if (lpni->lpni_rtrcredits < 0) {
972 spin_unlock(&lpni->lpni_lock);
973 /* must have checked eager_recv before here */
974 LASSERT(msg->msg_rx_ready_delay);
975 msg->msg_rx_delayed = 1;
976 /* lp_lock protects the lp_rtrq */
977 spin_lock(&lp->lp_lock);
978 list_add_tail(&msg->msg_list, &lp->lp_rtrq);
979 spin_unlock(&lp->lp_lock);
980 return LNET_CREDIT_WAIT;
982 spin_unlock(&lpni->lpni_lock);
985 rbp = lnet_msg2bufpool(msg);
987 if (!msg->msg_rtrcredit) {
988 msg->msg_rtrcredit = 1;
990 if (rbp->rbp_credits < rbp->rbp_mincredits)
991 rbp->rbp_mincredits = rbp->rbp_credits;
993 if (rbp->rbp_credits < 0) {
994 /* must have checked eager_recv before here */
995 LASSERT(msg->msg_rx_ready_delay);
996 msg->msg_rx_delayed = 1;
997 list_add_tail(&msg->msg_list, &rbp->rbp_msgs);
998 return LNET_CREDIT_WAIT;
1002 LASSERT(!list_empty(&rbp->rbp_bufs));
1003 rb = list_entry(rbp->rbp_bufs.next, struct lnet_rtrbuf, rb_list);
1004 list_del(&rb->rb_list);
1006 msg->msg_niov = rbp->rbp_npages;
1007 msg->msg_kiov = &rb->rb_kiov[0];
1009 /* unset the msg-rx_delayed flag since we're receiving the message */
1010 msg->msg_rx_delayed = 0;
1013 int cpt = msg->msg_rx_cpt;
1015 lnet_net_unlock(cpt);
1016 lnet_ni_recv(msg->msg_rxni, msg->msg_private, msg, 1,
1017 0, msg->msg_len, msg->msg_len);
1020 return LNET_CREDIT_OK;
1024 lnet_return_tx_credits_locked(struct lnet_msg *msg)
1026 struct lnet_peer_ni *txpeer = msg->msg_txpeer;
1027 struct lnet_ni *txni = msg->msg_txni;
1028 struct lnet_msg *msg2;
1030 if (msg->msg_txcredit) {
1031 struct lnet_ni *ni = msg->msg_txni;
1032 struct lnet_tx_queue *tq = ni->ni_tx_queues[msg->msg_tx_cpt];
1034 /* give back NI txcredits */
1035 msg->msg_txcredit = 0;
1037 LASSERT((tq->tq_credits < 0) ==
1038 !list_empty(&tq->tq_delayed));
1041 atomic_inc(&ni->ni_tx_credits);
1042 if (tq->tq_credits <= 0) {
1043 msg2 = list_entry(tq->tq_delayed.next,
1044 struct lnet_msg, msg_list);
1045 list_del(&msg2->msg_list);
1047 LASSERT(msg2->msg_txni == ni);
1048 LASSERT(msg2->msg_tx_delayed);
1049 LASSERT(msg2->msg_tx_cpt == msg->msg_tx_cpt);
1051 (void) lnet_post_send_locked(msg2, 1);
1055 if (msg->msg_peertxcredit) {
1056 /* give back peer txcredits */
1057 msg->msg_peertxcredit = 0;
1059 spin_lock(&txpeer->lpni_lock);
1060 LASSERT((txpeer->lpni_txcredits < 0) ==
1061 !list_empty(&txpeer->lpni_txq));
1063 txpeer->lpni_txqnob -= msg->msg_len + sizeof(struct lnet_hdr);
1064 LASSERT(txpeer->lpni_txqnob >= 0);
1066 txpeer->lpni_txcredits++;
1067 if (txpeer->lpni_txcredits <= 0) {
1070 msg2 = list_entry(txpeer->lpni_txq.next,
1071 struct lnet_msg, msg_list);
1072 list_del(&msg2->msg_list);
1073 spin_unlock(&txpeer->lpni_lock);
1075 LASSERT(msg2->msg_txpeer == txpeer);
1076 LASSERT(msg2->msg_tx_delayed);
1078 msg2_cpt = msg2->msg_tx_cpt;
1081 * The msg_cpt can be different from the msg2_cpt
1082 * so we need to make sure we lock the correct cpt
1084 * Once we call lnet_post_send_locked() it is no
1085 * longer safe to access msg2, since it could've
1086 * been freed by lnet_finalize(), but we still
1087 * need to relock the correct cpt, so we cache the
1088 * msg2_cpt for the purpose of the check that
1089 * follows the call to lnet_pose_send_locked().
1091 if (msg2_cpt != msg->msg_tx_cpt) {
1092 lnet_net_unlock(msg->msg_tx_cpt);
1093 lnet_net_lock(msg2_cpt);
1095 (void) lnet_post_send_locked(msg2, 1);
1096 if (msg2_cpt != msg->msg_tx_cpt) {
1097 lnet_net_unlock(msg2_cpt);
1098 lnet_net_lock(msg->msg_tx_cpt);
1101 spin_unlock(&txpeer->lpni_lock);
1106 msg->msg_txni = NULL;
1107 lnet_ni_decref_locked(txni, msg->msg_tx_cpt);
1110 if (txpeer != NULL) {
1111 msg->msg_txpeer = NULL;
1112 lnet_peer_ni_decref_locked(txpeer);
1117 lnet_schedule_blocked_locked(struct lnet_rtrbufpool *rbp)
1119 struct lnet_msg *msg;
1121 if (list_empty(&rbp->rbp_msgs))
1123 msg = list_entry(rbp->rbp_msgs.next,
1124 struct lnet_msg, msg_list);
1125 list_del(&msg->msg_list);
1127 (void)lnet_post_routed_recv_locked(msg, 1);
1131 lnet_drop_routed_msgs_locked(struct list_head *list, int cpt)
1133 struct lnet_msg *msg;
1134 struct lnet_msg *tmp;
1136 lnet_net_unlock(cpt);
1138 list_for_each_entry_safe(msg, tmp, list, msg_list) {
1139 lnet_ni_recv(msg->msg_rxni, msg->msg_private, NULL,
1140 0, 0, 0, msg->msg_hdr.payload_length);
1141 list_del_init(&msg->msg_list);
1142 msg->msg_no_resend = true;
1143 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_ERROR;
1144 lnet_finalize(msg, -ECANCELED);
1151 lnet_return_rx_credits_locked(struct lnet_msg *msg)
1153 struct lnet_peer_ni *rxpeerni = msg->msg_rxpeer;
1154 struct lnet_peer *lp;
1155 struct lnet_ni *rxni = msg->msg_rxni;
1156 struct lnet_msg *msg2;
1158 if (msg->msg_rtrcredit) {
1159 /* give back global router credits */
1160 struct lnet_rtrbuf *rb;
1161 struct lnet_rtrbufpool *rbp;
1163 /* NB If a msg ever blocks for a buffer in rbp_msgs, it stays
1164 * there until it gets one allocated, or aborts the wait
1166 LASSERT(msg->msg_kiov != NULL);
1168 rb = list_entry(msg->msg_kiov, struct lnet_rtrbuf, rb_kiov[0]);
1171 msg->msg_kiov = NULL;
1172 msg->msg_rtrcredit = 0;
1174 LASSERT(rbp == lnet_msg2bufpool(msg));
1176 LASSERT((rbp->rbp_credits > 0) ==
1177 !list_empty(&rbp->rbp_bufs));
1179 /* If routing is now turned off, we just drop this buffer and
1180 * don't bother trying to return credits. */
1181 if (!the_lnet.ln_routing) {
1182 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1186 /* It is possible that a user has lowered the desired number of
1187 * buffers in this pool. Make sure we never put back
1188 * more buffers than the stated number. */
1189 if (unlikely(rbp->rbp_credits >= rbp->rbp_req_nbuffers)) {
1190 /* Discard this buffer so we don't have too
1192 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1193 rbp->rbp_nbuffers--;
1195 list_add(&rb->rb_list, &rbp->rbp_bufs);
1197 if (rbp->rbp_credits <= 0)
1198 lnet_schedule_blocked_locked(rbp);
1203 if (msg->msg_peerrtrcredit) {
1205 LASSERT(rxpeerni->lpni_peer_net);
1206 LASSERT(rxpeerni->lpni_peer_net->lpn_peer);
1208 /* give back peer router credits */
1209 msg->msg_peerrtrcredit = 0;
1211 spin_lock(&rxpeerni->lpni_lock);
1212 rxpeerni->lpni_rtrcredits++;
1213 spin_unlock(&rxpeerni->lpni_lock);
1215 lp = rxpeerni->lpni_peer_net->lpn_peer;
1216 spin_lock(&lp->lp_lock);
1218 /* drop all messages which are queued to be routed on that
1220 if (!the_lnet.ln_routing) {
1222 list_splice_init(&lp->lp_rtrq, &drop);
1223 spin_unlock(&lp->lp_lock);
1224 lnet_drop_routed_msgs_locked(&drop, msg->msg_rx_cpt);
1225 } else if (!list_empty(&lp->lp_rtrq)) {
1228 msg2 = list_entry(lp->lp_rtrq.next,
1229 struct lnet_msg, msg_list);
1230 list_del(&msg2->msg_list);
1231 msg2_cpt = msg2->msg_rx_cpt;
1232 spin_unlock(&lp->lp_lock);
1234 * messages on the lp_rtrq can be from any NID in
1235 * the peer, which means they might have different
1236 * cpts. We need to make sure we lock the right
1239 if (msg2_cpt != msg->msg_rx_cpt) {
1240 lnet_net_unlock(msg->msg_rx_cpt);
1241 lnet_net_lock(msg2_cpt);
1243 (void) lnet_post_routed_recv_locked(msg2, 1);
1244 if (msg2_cpt != msg->msg_rx_cpt) {
1245 lnet_net_unlock(msg2_cpt);
1246 lnet_net_lock(msg->msg_rx_cpt);
1249 spin_unlock(&lp->lp_lock);
1253 msg->msg_rxni = NULL;
1254 lnet_ni_decref_locked(rxni, msg->msg_rx_cpt);
1256 if (rxpeerni != NULL) {
1257 msg->msg_rxpeer = NULL;
1258 lnet_peer_ni_decref_locked(rxpeerni);
1263 lnet_compare_gw_lpnis(struct lnet_peer_ni *p1, struct lnet_peer_ni *p2)
1265 if (p1->lpni_txqnob < p2->lpni_txqnob)
1268 if (p1->lpni_txqnob > p2->lpni_txqnob)
1271 if (p1->lpni_txcredits > p2->lpni_txcredits)
1274 if (p1->lpni_txcredits < p2->lpni_txcredits)
1280 static struct lnet_peer_ni *
1281 lnet_select_peer_ni(struct lnet_ni *best_ni, lnet_nid_t dst_nid,
1282 struct lnet_peer *peer,
1283 struct lnet_peer_ni *best_lpni,
1284 struct lnet_peer_net *peer_net)
1287 * Look at the peer NIs for the destination peer that connect
1288 * to the chosen net. If a peer_ni is preferred when using the
1289 * best_ni to communicate, we use that one. If there is no
1290 * preferred peer_ni, or there are multiple preferred peer_ni,
1291 * the available transmit credits are used. If the transmit
1292 * credits are equal, we round-robin over the peer_ni.
1294 struct lnet_peer_ni *lpni = NULL;
1295 int best_lpni_credits = (best_lpni) ? best_lpni->lpni_txcredits :
1297 int best_lpni_healthv = (best_lpni) ?
1298 atomic_read(&best_lpni->lpni_healthv) : 0;
1299 bool preferred = false;
1303 while ((lpni = lnet_get_next_peer_ni_locked(peer, peer_net, lpni))) {
1305 * if the best_ni we've chosen aleady has this lpni
1306 * preferred, then let's use it
1309 ni_is_pref = lnet_peer_is_pref_nid_locked(lpni,
1311 CDEBUG(D_NET, "%s ni_is_pref = %d\n",
1312 libcfs_nid2str(best_ni->ni_nid), ni_is_pref);
1317 lpni_healthv = atomic_read(&lpni->lpni_healthv);
1320 CDEBUG(D_NET, "%s c:[%d, %d], s:[%d, %d]\n",
1321 libcfs_nid2str(lpni->lpni_nid),
1322 lpni->lpni_txcredits, best_lpni_credits,
1323 lpni->lpni_seq, best_lpni->lpni_seq);
1325 /* pick the healthiest peer ni */
1326 if (lpni_healthv < best_lpni_healthv) {
1328 } else if (lpni_healthv > best_lpni_healthv) {
1329 best_lpni_healthv = lpni_healthv;
1330 /* if this is a preferred peer use it */
1331 } else if (!preferred && ni_is_pref) {
1333 } else if (preferred && !ni_is_pref) {
1335 * this is not the preferred peer so let's ignore
1339 } else if (lpni->lpni_txcredits < best_lpni_credits) {
1341 * We already have a peer that has more credits
1342 * available than this one. No need to consider
1343 * this peer further.
1346 } else if (lpni->lpni_txcredits == best_lpni_credits) {
1348 * The best peer found so far and the current peer
1349 * have the same number of available credits let's
1350 * make sure to select between them using Round
1354 if (best_lpni->lpni_seq <= lpni->lpni_seq)
1360 best_lpni_credits = lpni->lpni_txcredits;
1363 /* if we still can't find a peer ni then we can't reach it */
1365 __u32 net_id = (peer_net) ? peer_net->lpn_net_id :
1366 LNET_NIDNET(dst_nid);
1367 CDEBUG(D_NET, "no peer_ni found on peer net %s\n",
1368 libcfs_net2str(net_id));
1372 CDEBUG(D_NET, "sd_best_lpni = %s\n",
1373 libcfs_nid2str(best_lpni->lpni_nid));
1379 * Prerequisite: the best_ni should already be set in the sd
1380 * Find the best lpni.
1381 * If the net id is provided then restrict lpni selection on
1382 * that particular net.
1383 * Otherwise find any reachable lpni. When dealing with an MR
1384 * gateway and it has multiple lpnis which we can use
1385 * we want to select the best one from the list of reachable
1388 static inline struct lnet_peer_ni *
1389 lnet_find_best_lpni(struct lnet_ni *lni, lnet_nid_t dst_nid,
1390 struct lnet_peer *peer, __u32 net_id)
1392 struct lnet_peer_net *peer_net;
1393 __u32 any_net = LNET_NIDNET(LNET_NID_ANY);
1395 /* find the best_lpni on any local network */
1396 if (net_id == any_net) {
1397 struct lnet_peer_ni *best_lpni = NULL;
1398 struct lnet_peer_net *lpn;
1399 list_for_each_entry(lpn, &peer->lp_peer_nets, lpn_peer_nets) {
1400 /* no net specified find any reachable peer ni */
1401 if (!lnet_islocalnet_locked(lpn->lpn_net_id))
1403 best_lpni = lnet_select_peer_ni(lni, dst_nid, peer,
1409 /* restrict on the specified net */
1410 peer_net = lnet_peer_get_net_locked(peer, net_id);
1412 return lnet_select_peer_ni(lni, dst_nid, peer, NULL, peer_net);
1417 /* Compare route priorities and hop counts */
1419 lnet_compare_routes(struct lnet_route *r1, struct lnet_route *r2)
1421 int r1_hops = (r1->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r1->lr_hops;
1422 int r2_hops = (r2->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r2->lr_hops;
1424 if (r1->lr_priority < r2->lr_priority)
1427 if (r1->lr_priority > r2->lr_priority)
1430 if (r1_hops < r2_hops)
1433 if (r1_hops > r2_hops)
1439 static struct lnet_route *
1440 lnet_find_route_locked(struct lnet_remotenet *rnet, __u32 src_net,
1441 struct lnet_route **prev_route,
1442 struct lnet_peer_ni **gwni)
1444 struct lnet_peer_ni *lpni, *best_gw_ni = NULL;
1445 struct lnet_route *best_route;
1446 struct lnet_route *last_route;
1447 struct lnet_route *route;
1450 CDEBUG(D_NET, "Looking up a route to %s, from %s\n",
1451 libcfs_net2str(rnet->lrn_net), libcfs_net2str(src_net));
1453 best_route = last_route = NULL;
1454 list_for_each_entry(route, &rnet->lrn_routes, lr_list) {
1455 if (!lnet_is_route_alive(route))
1459 * Restrict the selection of the router NI on the src_net
1460 * provided. If the src_net is LNET_NID_ANY, then select
1461 * the best interface available.
1464 lpni = lnet_find_best_lpni(NULL, LNET_NID_ANY,
1468 best_route = last_route = route;
1471 CDEBUG(D_NET, "Gateway %s does not have a peer NI on net %s\n",
1472 libcfs_nid2str(route->lr_gateway->lp_primary_nid),
1473 libcfs_net2str(src_net));
1479 /* no protection on below fields, but it's harmless */
1480 if (last_route->lr_seq - route->lr_seq < 0)
1483 rc = lnet_compare_routes(route, best_route);
1487 lpni = lnet_find_best_lpni(NULL, LNET_NID_ANY,
1490 /* restrict the lpni on the src_net if specified */
1492 CDEBUG(D_NET, "Gateway %s does not have a peer NI on net %s\n",
1493 libcfs_nid2str(route->lr_gateway->lp_primary_nid),
1494 libcfs_net2str(src_net));
1504 rc = lnet_compare_gw_lpnis(lpni, best_gw_ni);
1508 if (rc == 1 || route->lr_seq <= best_route->lr_seq) {
1515 *prev_route = last_route;
1521 static struct lnet_ni *
1522 lnet_get_best_ni(struct lnet_net *local_net, struct lnet_ni *best_ni,
1523 struct lnet_peer *peer, struct lnet_peer_net *peer_net,
1526 struct lnet_ni *ni = NULL;
1527 unsigned int shortest_distance;
1532 * If there is no peer_ni that we can send to on this network,
1533 * then there is no point in looking for a new best_ni here.
1535 if (!lnet_get_next_peer_ni_locked(peer, peer_net, NULL))
1538 if (best_ni == NULL) {
1539 shortest_distance = UINT_MAX;
1540 best_credits = INT_MIN;
1543 shortest_distance = cfs_cpt_distance(lnet_cpt_table(), md_cpt,
1544 best_ni->ni_dev_cpt);
1545 best_credits = atomic_read(&best_ni->ni_tx_credits);
1546 best_healthv = atomic_read(&best_ni->ni_healthv);
1549 while ((ni = lnet_get_next_ni_locked(local_net, ni))) {
1550 unsigned int distance;
1555 ni_credits = atomic_read(&ni->ni_tx_credits);
1556 ni_healthv = atomic_read(&ni->ni_healthv);
1557 ni_fatal = atomic_read(&ni->ni_fatal_error_on);
1560 * calculate the distance from the CPT on which
1561 * the message memory is allocated to the CPT of
1562 * the NI's physical device
1564 distance = cfs_cpt_distance(lnet_cpt_table(),
1568 CDEBUG(D_NET, "compare ni %s [c:%d, d:%d, s:%d] with best_ni %s [c:%d, d:%d, s:%d]\n",
1569 libcfs_nid2str(ni->ni_nid), ni_credits, distance,
1570 ni->ni_seq, (best_ni) ? libcfs_nid2str(best_ni->ni_nid)
1571 : "not seleced", best_credits, shortest_distance,
1572 (best_ni) ? best_ni->ni_seq : 0);
1575 * All distances smaller than the NUMA range
1576 * are treated equally.
1578 if (distance < lnet_numa_range)
1579 distance = lnet_numa_range;
1582 * Select on health, shorter distance, available
1583 * credits, then round-robin.
1587 } else if (ni_healthv < best_healthv) {
1589 } else if (ni_healthv > best_healthv) {
1590 best_healthv = ni_healthv;
1592 * If we're going to prefer this ni because it's
1593 * the healthiest, then we should set the
1594 * shortest_distance in the algorithm in case
1595 * there are multiple NIs with the same health but
1596 * different distances.
1598 if (distance < shortest_distance)
1599 shortest_distance = distance;
1600 } else if (distance > shortest_distance) {
1602 } else if (distance < shortest_distance) {
1603 shortest_distance = distance;
1604 } else if (ni_credits < best_credits) {
1606 } else if (ni_credits == best_credits) {
1607 if (best_ni && best_ni->ni_seq <= ni->ni_seq)
1611 best_credits = ni_credits;
1614 CDEBUG(D_NET, "selected best_ni %s\n",
1615 (best_ni) ? libcfs_nid2str(best_ni->ni_nid) : "no selection");
1621 * Traffic to the LNET_RESERVED_PORTAL may not trigger peer discovery,
1622 * because such traffic is required to perform discovery. We therefore
1623 * exclude all GET and PUT on that portal. We also exclude all ACK and
1624 * REPLY traffic, but that is because the portal is not tracked in the
1625 * message structure for these message types. We could restrict this
1626 * further by also checking for LNET_PROTO_PING_MATCHBITS.
1629 lnet_msg_discovery(struct lnet_msg *msg)
1631 if (msg->msg_type == LNET_MSG_PUT) {
1632 if (msg->msg_hdr.msg.put.ptl_index != LNET_RESERVED_PORTAL)
1634 } else if (msg->msg_type == LNET_MSG_GET) {
1635 if (msg->msg_hdr.msg.get.ptl_index != LNET_RESERVED_PORTAL)
1641 #define SRC_SPEC 0x0001
1642 #define SRC_ANY 0x0002
1643 #define LOCAL_DST 0x0004
1644 #define REMOTE_DST 0x0008
1645 #define MR_DST 0x0010
1646 #define NMR_DST 0x0020
1647 #define SND_RESP 0x0040
1649 /* The following to defines are used for return codes */
1650 #define REPEAT_SEND 0x1000
1651 #define PASS_THROUGH 0x2000
1653 /* The different cases lnet_select pathway needs to handle */
1654 #define SRC_SPEC_LOCAL_MR_DST (SRC_SPEC | LOCAL_DST | MR_DST)
1655 #define SRC_SPEC_ROUTER_MR_DST (SRC_SPEC | REMOTE_DST | MR_DST)
1656 #define SRC_SPEC_LOCAL_NMR_DST (SRC_SPEC | LOCAL_DST | NMR_DST)
1657 #define SRC_SPEC_ROUTER_NMR_DST (SRC_SPEC | REMOTE_DST | NMR_DST)
1658 #define SRC_ANY_LOCAL_MR_DST (SRC_ANY | LOCAL_DST | MR_DST)
1659 #define SRC_ANY_ROUTER_MR_DST (SRC_ANY | REMOTE_DST | MR_DST)
1660 #define SRC_ANY_LOCAL_NMR_DST (SRC_ANY | LOCAL_DST | NMR_DST)
1661 #define SRC_ANY_ROUTER_NMR_DST (SRC_ANY | REMOTE_DST | NMR_DST)
1664 lnet_handle_lo_send(struct lnet_send_data *sd)
1666 struct lnet_msg *msg = sd->sd_msg;
1667 int cpt = sd->sd_cpt;
1669 /* No send credit hassles with LOLND */
1670 lnet_ni_addref_locked(the_lnet.ln_loni, cpt);
1671 msg->msg_hdr.dest_nid = cpu_to_le64(the_lnet.ln_loni->ni_nid);
1672 if (!msg->msg_routing)
1673 msg->msg_hdr.src_nid =
1674 cpu_to_le64(the_lnet.ln_loni->ni_nid);
1675 msg->msg_target.nid = the_lnet.ln_loni->ni_nid;
1676 lnet_msg_commit(msg, cpt);
1677 msg->msg_txni = the_lnet.ln_loni;
1679 return LNET_CREDIT_OK;
1683 lnet_handle_send(struct lnet_send_data *sd)
1685 struct lnet_ni *best_ni = sd->sd_best_ni;
1686 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
1687 struct lnet_peer_ni *final_dst_lpni = sd->sd_final_dst_lpni;
1688 struct lnet_msg *msg = sd->sd_msg;
1690 __u32 send_case = sd->sd_send_case;
1692 __u32 routing = send_case & REMOTE_DST;
1693 struct lnet_rsp_tracker *rspt;
1696 * Increment sequence number of the selected peer so that we
1697 * pick the next one in Round Robin.
1699 best_lpni->lpni_seq++;
1702 * grab a reference on the peer_ni so it sticks around even if
1703 * we need to drop and relock the lnet_net_lock below.
1705 lnet_peer_ni_addref_locked(best_lpni);
1708 * Use lnet_cpt_of_nid() to determine the CPT used to commit the
1709 * message. This ensures that we get a CPT that is correct for
1710 * the NI when the NI has been restricted to a subset of all CPTs.
1711 * If the selected CPT differs from the one currently locked, we
1712 * must unlock and relock the lnet_net_lock(), and then check whether
1713 * the configuration has changed. We don't have a hold on the best_ni
1714 * yet, and it may have vanished.
1716 cpt2 = lnet_cpt_of_nid_locked(best_lpni->lpni_nid, best_ni);
1717 if (sd->sd_cpt != cpt2) {
1718 __u32 seq = lnet_get_dlc_seq_locked();
1719 lnet_net_unlock(sd->sd_cpt);
1721 lnet_net_lock(sd->sd_cpt);
1722 if (seq != lnet_get_dlc_seq_locked()) {
1723 lnet_peer_ni_decref_locked(best_lpni);
1729 * store the best_lpni in the message right away to avoid having
1730 * to do the same operation under different conditions
1732 msg->msg_txpeer = best_lpni;
1733 msg->msg_txni = best_ni;
1736 * grab a reference for the best_ni since now it's in use in this
1737 * send. The reference will be dropped in lnet_finalize()
1739 lnet_ni_addref_locked(msg->msg_txni, sd->sd_cpt);
1742 * Always set the target.nid to the best peer picked. Either the
1743 * NID will be one of the peer NIDs selected, or the same NID as
1744 * what was originally set in the target or it will be the NID of
1745 * a router if this message should be routed
1747 msg->msg_target.nid = msg->msg_txpeer->lpni_nid;
1750 * lnet_msg_commit assigns the correct cpt to the message, which
1751 * is used to decrement the correct refcount on the ni when it's
1752 * time to return the credits
1754 lnet_msg_commit(msg, sd->sd_cpt);
1757 * If we are routing the message then we keep the src_nid that was
1758 * set by the originator. If we are not routing then we are the
1759 * originator and set it here.
1761 if (!msg->msg_routing)
1762 msg->msg_hdr.src_nid = cpu_to_le64(msg->msg_txni->ni_nid);
1765 msg->msg_target_is_router = 1;
1766 msg->msg_target.pid = LNET_PID_LUSTRE;
1768 * since we're routing we want to ensure that the
1769 * msg_hdr.dest_nid is set to the final destination. When
1770 * the router receives this message it knows how to route
1773 * final_dst_lpni is set at the beginning of the
1774 * lnet_select_pathway() function and is never changed.
1775 * It's safe to use it here.
1777 msg->msg_hdr.dest_nid = cpu_to_le64(final_dst_lpni->lpni_nid);
1780 * if we're not routing set the dest_nid to the best peer
1781 * ni NID that we picked earlier in the algorithm.
1783 msg->msg_hdr.dest_nid = cpu_to_le64(msg->msg_txpeer->lpni_nid);
1787 * if we have response tracker block update it with the next hop
1791 rspt = msg->msg_md->md_rspt_ptr;
1793 rspt->rspt_next_hop_nid = msg->msg_txpeer->lpni_nid;
1794 CDEBUG(D_NET, "rspt_next_hop_nid = %s\n",
1795 libcfs_nid2str(rspt->rspt_next_hop_nid));
1799 rc = lnet_post_send_locked(msg, 0);
1802 CDEBUG(D_NET, "TRACE: %s(%s:%s) -> %s(%s:%s) %s : %s try# %d\n",
1803 libcfs_nid2str(msg->msg_hdr.src_nid),
1804 libcfs_nid2str(msg->msg_txni->ni_nid),
1805 libcfs_nid2str(sd->sd_src_nid),
1806 libcfs_nid2str(msg->msg_hdr.dest_nid),
1807 libcfs_nid2str(sd->sd_dst_nid),
1808 libcfs_nid2str(msg->msg_txpeer->lpni_nid),
1809 libcfs_nid2str(sd->sd_rtr_nid),
1810 lnet_msgtyp2str(msg->msg_type), msg->msg_retry_count);
1816 lnet_set_non_mr_pref_nid(struct lnet_peer_ni *lpni, struct lnet_ni *lni,
1817 struct lnet_msg *msg)
1819 if (!lnet_msg_is_response(msg) && lpni->lpni_pref_nnids == 0) {
1820 CDEBUG(D_NET, "Setting preferred local NID %s on NMR peer %s\n",
1821 libcfs_nid2str(lni->ni_nid),
1822 libcfs_nid2str(lpni->lpni_nid));
1823 lnet_peer_ni_set_non_mr_pref_nid(lpni, lni->ni_nid);
1832 * use the source and destination NIDs as the pathway
1835 lnet_handle_spec_local_nmr_dst(struct lnet_send_data *sd)
1837 /* the destination lpni is set before we get here. */
1840 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
1841 if (!sd->sd_best_ni) {
1842 CERROR("Can't send to %s: src %s is not a "
1843 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
1844 libcfs_nid2str(sd->sd_src_nid));
1848 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
1850 return lnet_handle_send(sd);
1858 * Don't run the selection algorithm on the peer NIs. By specifying the
1859 * local NID, we're also saying that we should always use the destination NID
1860 * provided. This handles the case where we should be using the same
1861 * destination NID for the all the messages which belong to the same RPC
1865 lnet_handle_spec_local_mr_dst(struct lnet_send_data *sd)
1867 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
1868 if (!sd->sd_best_ni) {
1869 CERROR("Can't send to %s: src %s is not a "
1870 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
1871 libcfs_nid2str(sd->sd_src_nid));
1875 if (sd->sd_best_lpni &&
1876 sd->sd_best_lpni->lpni_nid == the_lnet.ln_loni->ni_nid)
1877 return lnet_handle_lo_send(sd);
1878 else if (sd->sd_best_lpni)
1879 return lnet_handle_send(sd);
1881 CERROR("can't send to %s. no NI on %s\n",
1882 libcfs_nid2str(sd->sd_dst_nid),
1883 libcfs_net2str(sd->sd_best_ni->ni_net->net_id));
1885 return -EHOSTUNREACH;
1889 lnet_find_best_ni_on_spec_net(struct lnet_ni *cur_best_ni,
1890 struct lnet_peer *peer,
1891 struct lnet_peer_net *peer_net,
1895 struct lnet_net *local_net;
1896 struct lnet_ni *best_ni;
1898 local_net = lnet_get_net_locked(peer_net->lpn_net_id);
1903 * Iterate through the NIs in this local Net and select
1904 * the NI to send from. The selection is determined by
1905 * these 3 criterion in the following priority:
1907 * 2. NI available credits
1910 best_ni = lnet_get_best_ni(local_net, cur_best_ni,
1911 peer, peer_net, cpt);
1913 if (incr_seq && best_ni)
1920 lnet_initiate_peer_discovery(struct lnet_peer_ni *lpni, struct lnet_msg *msg,
1923 struct lnet_peer *peer;
1926 lnet_peer_ni_addref_locked(lpni);
1928 peer = lpni->lpni_peer_net->lpn_peer;
1930 if (lnet_peer_gw_discovery(peer)) {
1931 lnet_peer_ni_decref_locked(lpni);
1935 if (!lnet_msg_discovery(msg) || lnet_peer_is_uptodate(peer)) {
1936 lnet_peer_ni_decref_locked(lpni);
1940 rc = lnet_discover_peer_locked(lpni, cpt, false);
1942 lnet_peer_ni_decref_locked(lpni);
1945 /* The peer may have changed. */
1946 peer = lpni->lpni_peer_net->lpn_peer;
1947 spin_lock(&peer->lp_lock);
1948 if (lnet_peer_is_uptodate_locked(peer)) {
1949 spin_unlock(&peer->lp_lock);
1950 lnet_peer_ni_decref_locked(lpni);
1953 /* queue message and return */
1954 msg->msg_sending = 0;
1955 msg->msg_txpeer = NULL;
1956 list_add_tail(&msg->msg_list, &peer->lp_dc_pendq);
1957 spin_unlock(&peer->lp_lock);
1959 lnet_peer_ni_decref_locked(lpni);
1961 CDEBUG(D_NET, "msg %p delayed. %s pending discovery\n",
1962 msg, libcfs_nid2str(peer->lp_primary_nid));
1964 return LNET_DC_WAIT;
1968 lnet_handle_find_routed_path(struct lnet_send_data *sd,
1970 struct lnet_peer_ni **gw_lpni,
1971 struct lnet_peer **gw_peer)
1975 struct lnet_peer *gw;
1976 struct lnet_peer *lp;
1977 struct lnet_peer_net *lpn;
1978 struct lnet_peer_net *best_lpn = NULL;
1979 struct lnet_remotenet *rnet, *best_rnet = NULL;
1980 struct lnet_route *best_route = NULL;
1981 struct lnet_route *last_route = NULL;
1982 struct lnet_peer_ni *lpni = NULL;
1983 struct lnet_peer_ni *gwni = NULL;
1984 lnet_nid_t src_nid = (sd->sd_src_nid != LNET_NID_ANY) ? sd->sd_src_nid :
1985 (sd->sd_best_ni != NULL) ? sd->sd_best_ni->ni_nid :
1988 CDEBUG(D_NET, "using src nid %s for route restriction\n",
1989 libcfs_nid2str(src_nid));
1991 /* If a router nid was specified then we are replying to a GET or
1992 * sending an ACK. In this case we use the gateway associated with the
1993 * specified router nid.
1995 if (sd->sd_rtr_nid != LNET_NID_ANY) {
1996 gwni = lnet_find_peer_ni_locked(sd->sd_rtr_nid);
1998 CERROR("No peer NI for gateway %s\n",
1999 libcfs_nid2str(sd->sd_rtr_nid));
2000 return -EHOSTUNREACH;
2002 gw = gwni->lpni_peer_net->lpn_peer;
2003 lnet_peer_ni_decref_locked(gwni);
2004 local_lnet = LNET_NIDNET(sd->sd_rtr_nid);
2006 /* we've already looked up the initial lpni using dst_nid */
2007 lpni = sd->sd_best_lpni;
2008 /* the peer tree must be in existence */
2009 LASSERT(lpni && lpni->lpni_peer_net &&
2010 lpni->lpni_peer_net->lpn_peer);
2011 lp = lpni->lpni_peer_net->lpn_peer;
2013 list_for_each_entry(lpn, &lp->lp_peer_nets, lpn_peer_nets) {
2014 /* is this remote network reachable? */
2015 rnet = lnet_find_rnet_locked(lpn->lpn_net_id);
2024 if (best_lpn->lpn_seq <= lpn->lpn_seq)
2032 CERROR("peer %s has no available nets\n",
2033 libcfs_nid2str(sd->sd_dst_nid));
2034 return -EHOSTUNREACH;
2037 sd->sd_best_lpni = lnet_find_best_lpni(sd->sd_best_ni,
2040 best_lpn->lpn_net_id);
2041 if (!sd->sd_best_lpni) {
2042 CERROR("peer %s is unreachable\n",
2043 libcfs_nid2str(sd->sd_dst_nid));
2044 return -EHOSTUNREACH;
2048 * We're attempting to round robin over the remote peer
2049 * NI's so update the final destination we selected
2051 sd->sd_final_dst_lpni = sd->sd_best_lpni;
2054 * find the best route. Restrict the selection on the net of the
2055 * local NI if we've already picked the local NI to send from.
2056 * Otherwise, let's pick any route we can find and then find
2057 * a local NI we can reach the route's gateway on. Any route we select
2058 * will be reachable by virtue of the restriction we have when
2061 best_route = lnet_find_route_locked(best_rnet,
2062 LNET_NIDNET(src_nid),
2063 &last_route, &gwni);
2066 CERROR("no route to %s from %s\n",
2067 libcfs_nid2str(dst_nid),
2068 libcfs_nid2str(src_nid));
2069 return -EHOSTUNREACH;
2073 CERROR("Internal Error. Route expected to %s from %s\n",
2074 libcfs_nid2str(dst_nid),
2075 libcfs_nid2str(src_nid));
2079 gw = best_route->lr_gateway;
2080 LASSERT(gw == gwni->lpni_peer_net->lpn_peer);
2081 local_lnet = best_route->lr_lnet;
2084 * Increment the sequence number of the remote lpni so we
2085 * can round robin over the different interfaces of the
2088 sd->sd_best_lpni->lpni_seq++;
2092 * Discover this gateway if it hasn't already been discovered.
2093 * This means we might delay the message until discovery has
2096 sd->sd_msg->msg_src_nid_param = sd->sd_src_nid;
2097 rc = lnet_initiate_peer_discovery(gwni, sd->sd_msg, sd->sd_cpt);
2101 if (!sd->sd_best_ni)
2102 sd->sd_best_ni = lnet_find_best_ni_on_spec_net(NULL, gw,
2103 lnet_peer_get_net_locked(gw,
2108 if (!sd->sd_best_ni) {
2109 CERROR("Internal Error. Expected local ni on %s but non found :%s\n",
2110 libcfs_net2str(local_lnet),
2111 libcfs_nid2str(sd->sd_src_nid));
2119 * increment the sequence numbers since now we're sure we're
2120 * going to use this path
2122 if (sd->sd_rtr_nid == LNET_NID_ANY) {
2123 LASSERT(best_route && last_route);
2124 best_route->lr_seq = last_route->lr_seq + 1;
2125 best_lpn->lpn_seq++;
2136 * Remote destination
2137 * Non-MR destination
2141 * Remote destination
2144 * The handling of these two cases is similar. Even though the destination
2145 * can be MR or non-MR, we'll deal directly with the router.
2148 lnet_handle_spec_router_dst(struct lnet_send_data *sd)
2151 struct lnet_peer_ni *gw_lpni = NULL;
2152 struct lnet_peer *gw_peer = NULL;
2155 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
2156 if (!sd->sd_best_ni) {
2157 CERROR("Can't send to %s: src %s is not a "
2158 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
2159 libcfs_nid2str(sd->sd_src_nid));
2163 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2168 if (sd->sd_send_case & NMR_DST)
2170 * since the final destination is non-MR let's set its preferred
2171 * NID before we send
2173 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni,
2177 * We're going to send to the gw found so let's set its
2180 sd->sd_peer = gw_peer;
2181 sd->sd_best_lpni = gw_lpni;
2183 return lnet_handle_send(sd);
2187 lnet_find_best_ni_on_local_net(struct lnet_peer *peer, int md_cpt,
2190 struct lnet_peer_net *peer_net = NULL;
2191 struct lnet_ni *best_ni = NULL;
2192 int lpn_healthv = 0;
2195 * The peer can have multiple interfaces, some of them can be on
2196 * the local network and others on a routed network. We should
2197 * prefer the local network. However if the local network is not
2198 * available then we need to try the routed network
2201 /* go through all the peer nets and find the best_ni */
2202 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_peer_nets) {
2204 * The peer's list of nets can contain non-local nets. We
2205 * want to only examine the local ones.
2207 if (!lnet_get_net_locked(peer_net->lpn_net_id))
2210 /* always select the lpn with the best health */
2211 if (lpn_healthv <= peer_net->lpn_healthv)
2212 lpn_healthv = peer_net->lpn_healthv;
2216 best_ni = lnet_find_best_ni_on_spec_net(best_ni, peer, peer_net,
2220 * if this is a discovery message and lp_disc_net_id is
2221 * specified then use that net to send the discovery on.
2223 if (peer->lp_disc_net_id == peer_net->lpn_net_id &&
2229 /* increment sequence number so we can round robin */
2235 static struct lnet_ni *
2236 lnet_find_existing_preferred_best_ni(struct lnet_peer_ni *lpni, int cpt)
2238 struct lnet_ni *best_ni = NULL;
2239 struct lnet_peer_net *peer_net = lpni->lpni_peer_net;
2240 struct lnet_peer_ni *lpni_entry;
2243 * We must use a consistent source address when sending to a
2244 * non-MR peer. However, a non-MR peer can have multiple NIDs
2245 * on multiple networks, and we may even need to talk to this
2246 * peer on multiple networks -- certain types of
2247 * load-balancing configuration do this.
2249 * So we need to pick the NI the peer prefers for this
2250 * particular network.
2253 list_for_each_entry(lpni_entry, &peer_net->lpn_peer_nis,
2255 if (lpni_entry->lpni_pref_nnids == 0)
2257 LASSERT(lpni_entry->lpni_pref_nnids == 1);
2258 best_ni = lnet_nid2ni_locked(lpni_entry->lpni_pref.nid, cpt);
2265 /* Prerequisite: sd->sd_peer and sd->sd_best_lpni should be set */
2267 lnet_select_preferred_best_ni(struct lnet_send_data *sd)
2269 struct lnet_ni *best_ni = NULL;
2270 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
2273 * We must use a consistent source address when sending to a
2274 * non-MR peer. However, a non-MR peer can have multiple NIDs
2275 * on multiple networks, and we may even need to talk to this
2276 * peer on multiple networks -- certain types of
2277 * load-balancing configuration do this.
2279 * So we need to pick the NI the peer prefers for this
2280 * particular network.
2283 best_ni = lnet_find_existing_preferred_best_ni(sd->sd_best_lpni,
2286 /* if best_ni is still not set just pick one */
2289 lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2290 sd->sd_best_lpni->lpni_peer_net,
2291 sd->sd_md_cpt, true);
2292 /* If there is no best_ni we don't have a route */
2294 CERROR("no path to %s from net %s\n",
2295 libcfs_nid2str(best_lpni->lpni_nid),
2296 libcfs_net2str(best_lpni->lpni_net->net_id));
2297 return -EHOSTUNREACH;
2301 sd->sd_best_ni = best_ni;
2303 /* Set preferred NI if necessary. */
2304 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
2311 * Source not specified
2315 * always use the same source NID for NMR peers
2316 * If we've talked to that peer before then we already have a preferred
2317 * source NI associated with it. Otherwise, we select a preferred local NI
2318 * and store it in the peer
2321 lnet_handle_any_local_nmr_dst(struct lnet_send_data *sd)
2325 /* sd->sd_best_lpni is already set to the final destination */
2328 * At this point we should've created the peer ni and peer. If we
2329 * can't find it, then something went wrong. Instead of assert
2330 * output a relevant message and fail the send
2332 if (!sd->sd_best_lpni) {
2333 CERROR("Internal fault. Unable to send msg %s to %s. "
2335 lnet_msgtyp2str(sd->sd_msg->msg_type),
2336 libcfs_nid2str(sd->sd_dst_nid));
2340 rc = lnet_select_preferred_best_ni(sd);
2342 rc = lnet_handle_send(sd);
2348 lnet_handle_any_mr_dsta(struct lnet_send_data *sd)
2351 * NOTE we've already handled the remote peer case. So we only
2352 * need to worry about the local case here.
2354 * if we're sending a response, ACK or reply, we need to send it
2355 * to the destination NID given to us. At this point we already
2356 * have the peer_ni we're suppose to send to, so just find the
2357 * best_ni on the peer net and use that. Since we're sending to an
2358 * MR peer then we can just run the selection algorithm on our
2359 * local NIs and pick the best one.
2361 if (sd->sd_send_case & SND_RESP) {
2363 lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2364 sd->sd_best_lpni->lpni_peer_net,
2365 sd->sd_md_cpt, true);
2367 if (!sd->sd_best_ni) {
2369 * We're not going to deal with not able to send
2370 * a response to the provided final destination
2372 CERROR("Can't send response to %s. "
2373 "No local NI available\n",
2374 libcfs_nid2str(sd->sd_dst_nid));
2375 return -EHOSTUNREACH;
2378 return lnet_handle_send(sd);
2382 * If we get here that means we're sending a fresh request, PUT or
2383 * GET, so we need to run our standard selection algorithm.
2384 * First find the best local interface that's on any of the peer's
2387 sd->sd_best_ni = lnet_find_best_ni_on_local_net(sd->sd_peer,
2389 lnet_msg_discovery(sd->sd_msg));
2390 if (sd->sd_best_ni) {
2392 lnet_find_best_lpni(sd->sd_best_ni, sd->sd_dst_nid,
2394 sd->sd_best_ni->ni_net->net_id);
2397 * if we're successful in selecting a peer_ni on the local
2398 * network, then send to it. Otherwise fall through and
2399 * try and see if we can reach it over another routed
2402 if (sd->sd_best_lpni &&
2403 sd->sd_best_lpni->lpni_nid == the_lnet.ln_loni->ni_nid) {
2405 * in case we initially started with a routed
2406 * destination, let's reset to local
2408 sd->sd_send_case &= ~REMOTE_DST;
2409 sd->sd_send_case |= LOCAL_DST;
2410 return lnet_handle_lo_send(sd);
2411 } else if (sd->sd_best_lpni) {
2413 * in case we initially started with a routed
2414 * destination, let's reset to local
2416 sd->sd_send_case &= ~REMOTE_DST;
2417 sd->sd_send_case |= LOCAL_DST;
2418 return lnet_handle_send(sd);
2421 CERROR("Internal Error. Expected to have a best_lpni: "
2423 libcfs_nid2str(sd->sd_src_nid),
2424 libcfs_nid2str(sd->sd_dst_nid));
2430 * Peer doesn't have a local network. Let's see if there is
2431 * a remote network we can reach it on.
2433 return PASS_THROUGH;
2438 * Source NID not specified
2443 * Source NID not speified
2444 * Remote destination
2447 * In both of these cases if we're sending a response, ACK or REPLY, then
2448 * we need to send to the destination NID provided.
2450 * In the remote case let's deal with MR routers.
2455 lnet_handle_any_mr_dst(struct lnet_send_data *sd)
2458 struct lnet_peer *gw_peer = NULL;
2459 struct lnet_peer_ni *gw_lpni = NULL;
2462 * handle sending a response to a remote peer here so we don't
2463 * have to worry about it if we hit lnet_handle_any_mr_dsta()
2465 if (sd->sd_send_case & REMOTE_DST &&
2466 sd->sd_send_case & SND_RESP) {
2467 struct lnet_peer_ni *gw;
2468 struct lnet_peer *gw_peer;
2470 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw,
2473 CERROR("Can't send response to %s. "
2474 "No route available\n",
2475 libcfs_nid2str(sd->sd_dst_nid));
2476 return -EHOSTUNREACH;
2477 } else if (rc > 0) {
2481 sd->sd_best_lpni = gw;
2482 sd->sd_peer = gw_peer;
2484 return lnet_handle_send(sd);
2488 * Even though the NID for the peer might not be on a local network,
2489 * since the peer is MR there could be other interfaces on the
2490 * local network. In that case we'd still like to prefer the local
2491 * network over the routed network. If we're unable to do that
2492 * then we select the best router among the different routed networks,
2493 * and if the router is MR then we can deal with it as such.
2495 rc = lnet_handle_any_mr_dsta(sd);
2496 if (rc != PASS_THROUGH)
2500 * Now that we must route to the destination, we must consider the
2501 * MR case, where the destination has multiple interfaces, some of
2502 * which we can route to and others we do not. For this reason we
2503 * need to select the destination which we can route to and if
2504 * there are multiple, we need to round robin.
2506 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2511 sd->sd_send_case &= ~LOCAL_DST;
2512 sd->sd_send_case |= REMOTE_DST;
2514 sd->sd_peer = gw_peer;
2515 sd->sd_best_lpni = gw_lpni;
2517 return lnet_handle_send(sd);
2521 * Source not specified
2522 * Remote destination
2525 * Must send to the specified peer NID using the same source NID that
2526 * we've used before. If it's the first time to talk to that peer then
2527 * find the source NI and assign it as preferred to that peer
2530 lnet_handle_any_router_nmr_dst(struct lnet_send_data *sd)
2533 struct lnet_peer_ni *gw_lpni = NULL;
2534 struct lnet_peer *gw_peer = NULL;
2537 * Let's see if we have a preferred NI to talk to this NMR peer
2539 sd->sd_best_ni = lnet_find_existing_preferred_best_ni(sd->sd_best_lpni,
2543 * find the router and that'll find the best NI if we didn't find
2546 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2552 * set the best_ni we've chosen as the preferred one for
2555 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
2557 /* we'll be sending to the gw */
2558 sd->sd_best_lpni = gw_lpni;
2559 sd->sd_peer = gw_peer;
2561 return lnet_handle_send(sd);
2565 lnet_handle_send_case_locked(struct lnet_send_data *sd)
2568 * turn off the SND_RESP bit.
2569 * It will be checked in the case handling
2571 __u32 send_case = sd->sd_send_case &= ~SND_RESP ;
2573 CDEBUG(D_NET, "Source %s%s to %s %s %s destination\n",
2574 (send_case & SRC_SPEC) ? "Specified: " : "ANY",
2575 (send_case & SRC_SPEC) ? libcfs_nid2str(sd->sd_src_nid) : "",
2576 (send_case & MR_DST) ? "MR: " : "NMR: ",
2577 libcfs_nid2str(sd->sd_dst_nid),
2578 (send_case & LOCAL_DST) ? "local" : "routed");
2580 switch (send_case) {
2582 * For all cases where the source is specified, we should always
2583 * use the destination NID, whether it's an MR destination or not,
2584 * since we're continuing a series of related messages for the
2587 case SRC_SPEC_LOCAL_NMR_DST:
2588 return lnet_handle_spec_local_nmr_dst(sd);
2589 case SRC_SPEC_LOCAL_MR_DST:
2590 return lnet_handle_spec_local_mr_dst(sd);
2591 case SRC_SPEC_ROUTER_NMR_DST:
2592 case SRC_SPEC_ROUTER_MR_DST:
2593 return lnet_handle_spec_router_dst(sd);
2594 case SRC_ANY_LOCAL_NMR_DST:
2595 return lnet_handle_any_local_nmr_dst(sd);
2596 case SRC_ANY_LOCAL_MR_DST:
2597 case SRC_ANY_ROUTER_MR_DST:
2598 return lnet_handle_any_mr_dst(sd);
2599 case SRC_ANY_ROUTER_NMR_DST:
2600 return lnet_handle_any_router_nmr_dst(sd);
2602 CERROR("Unknown send case\n");
2608 lnet_select_pathway(lnet_nid_t src_nid, lnet_nid_t dst_nid,
2609 struct lnet_msg *msg, lnet_nid_t rtr_nid)
2611 struct lnet_peer_ni *lpni;
2612 struct lnet_peer *peer;
2613 struct lnet_send_data send_data;
2616 __u32 send_case = 0;
2618 memset(&send_data, 0, sizeof(send_data));
2621 * get an initial CPT to use for locking. The idea here is not to
2622 * serialize the calls to select_pathway, so that as many
2623 * operations can run concurrently as possible. To do that we use
2624 * the CPT where this call is being executed. Later on when we
2625 * determine the CPT to use in lnet_message_commit, we switch the
2626 * lock and check if there was any configuration change. If none,
2627 * then we proceed, if there is, then we restart the operation.
2629 cpt = lnet_net_lock_current();
2631 md_cpt = lnet_cpt_of_md(msg->msg_md, msg->msg_offset);
2632 if (md_cpt == CFS_CPT_ANY)
2638 * If we're being asked to send to the loopback interface, there
2639 * is no need to go through any selection. We can just shortcut
2640 * the entire process and send over lolnd
2642 send_data.sd_msg = msg;
2643 send_data.sd_cpt = cpt;
2644 if (dst_nid == LNET_NID_LO_0) {
2645 rc = lnet_handle_lo_send(&send_data);
2646 lnet_net_unlock(cpt);
2651 * find an existing peer_ni, or create one and mark it as having been
2652 * created due to network traffic. This call will create the
2653 * peer->peer_net->peer_ni tree.
2655 lpni = lnet_nid2peerni_locked(dst_nid, LNET_NID_ANY, cpt);
2657 lnet_net_unlock(cpt);
2658 return PTR_ERR(lpni);
2662 * Cache the original src_nid and rtr_nid. If we need to resend the
2663 * message then we'll need to know whether the src_nid was originally
2664 * specified for this message. If it was originally specified,
2665 * then we need to keep using the same src_nid since it's
2666 * continuing the same sequence of messages. Similarly, rtr_nid will
2667 * affect our choice of next hop.
2669 msg->msg_src_nid_param = src_nid;
2670 msg->msg_rtr_nid_param = rtr_nid;
2673 * If necessary, perform discovery on the peer that owns this peer_ni.
2674 * Note, this can result in the ownership of this peer_ni changing
2675 * to another peer object.
2677 rc = lnet_initiate_peer_discovery(lpni, msg, cpt);
2679 lnet_peer_ni_decref_locked(lpni);
2680 lnet_net_unlock(cpt);
2683 lnet_peer_ni_decref_locked(lpni);
2685 peer = lpni->lpni_peer_net->lpn_peer;
2688 * Identify the different send cases
2690 if (src_nid == LNET_NID_ANY)
2691 send_case |= SRC_ANY;
2693 send_case |= SRC_SPEC;
2695 if (lnet_get_net_locked(LNET_NIDNET(dst_nid)))
2696 send_case |= LOCAL_DST;
2698 send_case |= REMOTE_DST;
2701 * Deal with the peer as NMR in the following cases:
2702 * 1. the peer is NMR
2703 * 2. We're trying to recover a specific peer NI
2704 * 3. I'm a router sending to the final destination
2705 * In this case the source of the message would've
2706 * already selected the final destination so my job
2707 * is to honor the selection.
2709 if (!lnet_peer_is_multi_rail(peer) || msg->msg_recovery ||
2710 (msg->msg_routing && (send_case & LOCAL_DST)))
2711 send_case |= NMR_DST;
2713 send_case |= MR_DST;
2715 if (lnet_msg_is_response(msg))
2716 send_case |= SND_RESP;
2718 /* assign parameters to the send_data */
2719 send_data.sd_rtr_nid = rtr_nid;
2720 send_data.sd_src_nid = src_nid;
2721 send_data.sd_dst_nid = dst_nid;
2722 send_data.sd_best_lpni = lpni;
2724 * keep a pointer to the final destination in case we're going to
2725 * route, so we'll need to access it later
2727 send_data.sd_final_dst_lpni = lpni;
2728 send_data.sd_peer = peer;
2729 send_data.sd_md_cpt = md_cpt;
2730 send_data.sd_send_case = send_case;
2732 rc = lnet_handle_send_case_locked(&send_data);
2735 * Update the local cpt since send_data.sd_cpt might've been
2736 * updated as a result of calling lnet_handle_send_case_locked().
2738 cpt = send_data.sd_cpt;
2740 if (rc == REPEAT_SEND)
2743 lnet_net_unlock(cpt);
2749 lnet_send(lnet_nid_t src_nid, struct lnet_msg *msg, lnet_nid_t rtr_nid)
2751 lnet_nid_t dst_nid = msg->msg_target.nid;
2755 * NB: rtr_nid is set to LNET_NID_ANY for all current use-cases,
2756 * but we might want to use pre-determined router for ACK/REPLY
2759 /* NB: ni != NULL == interface pre-determined (ACK/REPLY) */
2760 LASSERT(msg->msg_txpeer == NULL);
2761 LASSERT(msg->msg_txni == NULL);
2762 LASSERT(!msg->msg_sending);
2763 LASSERT(!msg->msg_target_is_router);
2764 LASSERT(!msg->msg_receiving);
2766 msg->msg_sending = 1;
2768 LASSERT(!msg->msg_tx_committed);
2770 rc = lnet_select_pathway(src_nid, dst_nid, msg, rtr_nid);
2772 if (rc == -EHOSTUNREACH)
2773 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_ERROR;
2775 msg->msg_health_status = LNET_MSG_STATUS_LOCAL_ERROR;
2779 if (rc == LNET_CREDIT_OK)
2780 lnet_ni_send(msg->msg_txni, msg);
2782 /* rc == LNET_CREDIT_OK or LNET_CREDIT_WAIT or LNET_DC_WAIT */
2786 enum lnet_mt_event_type {
2787 MT_TYPE_LOCAL_NI = 0,
2791 struct lnet_mt_event_info {
2792 enum lnet_mt_event_type mt_type;
2796 /* called with res_lock held */
2798 lnet_detach_rsp_tracker(struct lnet_libmd *md, int cpt)
2800 struct lnet_rsp_tracker *rspt;
2803 * msg has a refcount on the MD so the MD is not going away.
2804 * The rspt queue for the cpt is protected by
2805 * the lnet_net_lock(cpt). cpt is the cpt of the MD cookie.
2807 if (!md->md_rspt_ptr)
2810 rspt = md->md_rspt_ptr;
2813 LASSERT(rspt->rspt_cpt == cpt);
2815 md->md_rspt_ptr = NULL;
2817 if (LNetMDHandleIsInvalid(rspt->rspt_mdh)) {
2819 * The monitor thread has invalidated this handle because the
2820 * response timed out, but it failed to lookup the MD. That
2821 * means this response tracker is on the zombie list. We can
2822 * safely remove it under the resource lock (held by caller) and
2823 * free the response tracker block.
2825 list_del(&rspt->rspt_on_list);
2826 lnet_rspt_free(rspt, cpt);
2829 * invalidate the handle to indicate that a response has been
2830 * received, which will then lead the monitor thread to clean up
2833 LNetInvalidateMDHandle(&rspt->rspt_mdh);
2838 lnet_clean_zombie_rstqs(void)
2840 struct lnet_rsp_tracker *rspt, *tmp;
2843 cfs_cpt_for_each(i, lnet_cpt_table()) {
2844 list_for_each_entry_safe(rspt, tmp,
2845 the_lnet.ln_mt_zombie_rstqs[i],
2847 list_del(&rspt->rspt_on_list);
2848 lnet_rspt_free(rspt, i);
2852 cfs_percpt_free(the_lnet.ln_mt_zombie_rstqs);
2856 lnet_finalize_expired_responses(void)
2858 struct lnet_libmd *md;
2859 struct lnet_rsp_tracker *rspt, *tmp;
2863 if (the_lnet.ln_mt_rstq == NULL)
2866 cfs_cpt_for_each(i, lnet_cpt_table()) {
2867 LIST_HEAD(local_queue);
2870 if (!the_lnet.ln_mt_rstq[i]) {
2874 list_splice_init(the_lnet.ln_mt_rstq[i], &local_queue);
2879 list_for_each_entry_safe(rspt, tmp, &local_queue, rspt_on_list) {
2881 * The rspt mdh will be invalidated when a response
2882 * is received or whenever we want to discard the
2883 * block the monitor thread will walk the queue
2884 * and clean up any rsts with an invalid mdh.
2885 * The monitor thread will walk the queue until
2886 * the first unexpired rspt block. This means that
2887 * some rspt blocks which received their
2888 * corresponding responses will linger in the
2889 * queue until they are cleaned up eventually.
2892 if (LNetMDHandleIsInvalid(rspt->rspt_mdh)) {
2894 list_del(&rspt->rspt_on_list);
2895 lnet_rspt_free(rspt, i);
2899 if (ktime_compare(now, rspt->rspt_deadline) >= 0 ||
2900 the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN) {
2901 struct lnet_peer_ni *lpni;
2904 md = lnet_handle2md(&rspt->rspt_mdh);
2906 /* MD has been queued for unlink, but
2907 * rspt hasn't been detached (Note we've
2908 * checked above that the rspt_mdh is
2909 * valid). Since we cannot lookup the MD
2910 * we're unable to detach the rspt
2911 * ourselves. Thus, move the rspt to the
2912 * zombie list where we'll wait for
2914 * 1. The remaining operations on the
2915 * MD to complete. In this case the
2916 * final operation will result in
2917 * lnet_msg_detach_md()->
2918 * lnet_detach_rsp_tracker() where
2919 * we will clean up this response
2921 * 2. LNet to shutdown. In this case
2922 * we'll wait until after all LND Nets
2923 * have shutdown and then we can
2924 * safely free any remaining response
2925 * tracker blocks on the zombie list.
2926 * Note: We need to hold the resource
2927 * lock when adding to the zombie list
2928 * because we may have concurrent access
2929 * with lnet_detach_rsp_tracker().
2931 LNetInvalidateMDHandle(&rspt->rspt_mdh);
2932 list_move(&rspt->rspt_on_list,
2933 the_lnet.ln_mt_zombie_rstqs[i]);
2937 LASSERT(md->md_rspt_ptr == rspt);
2938 md->md_rspt_ptr = NULL;
2941 LNetMDUnlink(rspt->rspt_mdh);
2943 nid = rspt->rspt_next_hop_nid;
2945 list_del(&rspt->rspt_on_list);
2946 lnet_rspt_free(rspt, i);
2948 /* If we're shutting down we just want to clean
2949 * up the rspt blocks
2951 if (the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN)
2955 the_lnet.ln_counters[i]->lct_health.lch_response_timeout_count++;
2959 "Response timeout: md = %p: nid = %s\n",
2960 md, libcfs_nid2str(nid));
2963 * If there is a timeout on the response
2964 * from the next hop decrement its health
2965 * value so that we don't use it
2968 lpni = lnet_find_peer_ni_locked(nid);
2970 lnet_handle_remote_failure_locked(lpni);
2971 lnet_peer_ni_decref_locked(lpni);
2980 if (!list_empty(&local_queue)) {
2982 list_splice(&local_queue, the_lnet.ln_mt_rstq[i]);
2989 lnet_resend_pending_msgs_locked(struct list_head *resendq, int cpt)
2991 struct lnet_msg *msg;
2993 while (!list_empty(resendq)) {
2994 struct lnet_peer_ni *lpni;
2996 msg = list_entry(resendq->next, struct lnet_msg,
2999 list_del_init(&msg->msg_list);
3001 lpni = lnet_find_peer_ni_locked(msg->msg_hdr.dest_nid);
3003 lnet_net_unlock(cpt);
3004 CERROR("Expected that a peer is already created for %s\n",
3005 libcfs_nid2str(msg->msg_hdr.dest_nid));
3006 msg->msg_no_resend = true;
3007 lnet_finalize(msg, -EFAULT);
3012 lnet_peer_ni_decref_locked(lpni);
3014 lnet_net_unlock(cpt);
3015 CDEBUG(D_NET, "resending %s->%s: %s recovery %d try# %d\n",
3016 libcfs_nid2str(msg->msg_src_nid_param),
3017 libcfs_id2str(msg->msg_target),
3018 lnet_msgtyp2str(msg->msg_type),
3020 msg->msg_retry_count);
3021 rc = lnet_send(msg->msg_src_nid_param, msg,
3022 msg->msg_rtr_nid_param);
3024 CERROR("Error sending %s to %s: %d\n",
3025 lnet_msgtyp2str(msg->msg_type),
3026 libcfs_id2str(msg->msg_target), rc);
3027 msg->msg_no_resend = true;
3028 lnet_finalize(msg, rc);
3032 the_lnet.ln_counters[cpt]->lct_health.lch_resend_count++;
3038 lnet_resend_pending_msgs(void)
3042 cfs_cpt_for_each(i, lnet_cpt_table()) {
3044 lnet_resend_pending_msgs_locked(the_lnet.ln_mt_resendqs[i], i);
3049 /* called with cpt and ni_lock held */
3051 lnet_unlink_ni_recovery_mdh_locked(struct lnet_ni *ni, int cpt, bool force)
3053 struct lnet_handle_md recovery_mdh;
3055 LNetInvalidateMDHandle(&recovery_mdh);
3057 if (ni->ni_recovery_state & LNET_NI_RECOVERY_PENDING ||
3059 recovery_mdh = ni->ni_ping_mdh;
3060 LNetInvalidateMDHandle(&ni->ni_ping_mdh);
3063 lnet_net_unlock(cpt);
3064 if (!LNetMDHandleIsInvalid(recovery_mdh))
3065 LNetMDUnlink(recovery_mdh);
3071 lnet_recover_local_nis(void)
3073 struct lnet_mt_event_info *ev_info;
3074 LIST_HEAD(processed_list);
3075 LIST_HEAD(local_queue);
3076 struct lnet_handle_md mdh;
3077 struct lnet_ni *tmp;
3084 * splice the recovery queue on a local queue. We will iterate
3085 * through the local queue and update it as needed. Once we're
3086 * done with the traversal, we'll splice the local queue back on
3087 * the head of the ln_mt_localNIRecovq. Any newly added local NIs
3088 * will be traversed in the next iteration.
3091 list_splice_init(&the_lnet.ln_mt_localNIRecovq,
3095 list_for_each_entry_safe(ni, tmp, &local_queue, ni_recovery) {
3097 * if an NI is being deleted or it is now healthy, there
3098 * is no need to keep it around in the recovery queue.
3099 * The monitor thread is the only thread responsible for
3100 * removing the NI from the recovery queue.
3101 * Multiple threads can be adding NIs to the recovery
3104 healthv = atomic_read(&ni->ni_healthv);
3108 if (ni->ni_state != LNET_NI_STATE_ACTIVE ||
3109 healthv == LNET_MAX_HEALTH_VALUE) {
3110 list_del_init(&ni->ni_recovery);
3111 lnet_unlink_ni_recovery_mdh_locked(ni, 0, false);
3113 lnet_ni_decref_locked(ni, 0);
3119 * if the local NI failed recovery we must unlink the md.
3120 * But we want to keep the local_ni on the recovery queue
3121 * so we can continue the attempts to recover it.
3123 if (ni->ni_recovery_state & LNET_NI_RECOVERY_FAILED) {
3124 lnet_unlink_ni_recovery_mdh_locked(ni, 0, true);
3125 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_FAILED;
3132 CDEBUG(D_NET, "attempting to recover local ni: %s\n",
3133 libcfs_nid2str(ni->ni_nid));
3136 if (!(ni->ni_recovery_state & LNET_NI_RECOVERY_PENDING)) {
3137 ni->ni_recovery_state |= LNET_NI_RECOVERY_PENDING;
3140 LIBCFS_ALLOC(ev_info, sizeof(*ev_info));
3142 CERROR("out of memory. Can't recover %s\n",
3143 libcfs_nid2str(ni->ni_nid));
3145 ni->ni_recovery_state &=
3146 ~LNET_NI_RECOVERY_PENDING;
3151 mdh = ni->ni_ping_mdh;
3153 * Invalidate the ni mdh in case it's deleted.
3154 * We'll unlink the mdh in this case below.
3156 LNetInvalidateMDHandle(&ni->ni_ping_mdh);
3160 * remove the NI from the local queue and drop the
3161 * reference count to it while we're recovering
3162 * it. The reason for that, is that the NI could
3163 * be deleted, and the way the code is structured
3164 * is if we don't drop the NI, then the deletion
3165 * code will enter a loop waiting for the
3166 * reference count to be removed while holding the
3167 * ln_mutex_lock(). When we look up the peer to
3168 * send to in lnet_select_pathway() we will try to
3169 * lock the ln_mutex_lock() as well, leading to
3170 * a deadlock. By dropping the refcount and
3171 * removing it from the list, we allow for the NI
3172 * to be removed, then we use the cached NID to
3173 * look it up again. If it's gone, then we just
3174 * continue examining the rest of the queue.
3177 list_del_init(&ni->ni_recovery);
3178 lnet_ni_decref_locked(ni, 0);
3181 ev_info->mt_type = MT_TYPE_LOCAL_NI;
3182 ev_info->mt_nid = nid;
3183 rc = lnet_send_ping(nid, &mdh, LNET_INTERFACES_MIN,
3184 ev_info, the_lnet.ln_mt_handler,
3186 /* lookup the nid again */
3188 ni = lnet_nid2ni_locked(nid, 0);
3191 * the NI has been deleted when we dropped
3199 * Same note as in lnet_recover_peer_nis(). When
3200 * we're sending the ping, the NI is free to be
3201 * deleted or manipulated. By this point it
3202 * could've been added back on the recovery queue,
3203 * and a refcount taken on it.
3204 * So we can't just add it blindly again or we'll
3205 * corrupt the queue. We must check under lock if
3206 * it's not on any list and if not then add it
3207 * to the processed list, which will eventually be
3208 * spliced back on to the recovery queue.
3210 ni->ni_ping_mdh = mdh;
3211 if (list_empty(&ni->ni_recovery)) {
3212 list_add_tail(&ni->ni_recovery, &processed_list);
3213 lnet_ni_addref_locked(ni, 0);
3219 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_PENDING;
3225 * put back the remaining NIs on the ln_mt_localNIRecovq to be
3226 * reexamined in the next iteration.
3228 list_splice_init(&processed_list, &local_queue);
3230 list_splice(&local_queue, &the_lnet.ln_mt_localNIRecovq);
3235 lnet_resendqs_create(void)
3237 struct list_head **resendqs;
3238 resendqs = lnet_create_array_of_queues();
3243 lnet_net_lock(LNET_LOCK_EX);
3244 the_lnet.ln_mt_resendqs = resendqs;
3245 lnet_net_unlock(LNET_LOCK_EX);
3251 lnet_clean_local_ni_recoveryq(void)
3255 /* This is only called when the monitor thread has stopped */
3258 while (!list_empty(&the_lnet.ln_mt_localNIRecovq)) {
3259 ni = list_entry(the_lnet.ln_mt_localNIRecovq.next,
3260 struct lnet_ni, ni_recovery);
3261 list_del_init(&ni->ni_recovery);
3263 lnet_unlink_ni_recovery_mdh_locked(ni, 0, true);
3265 lnet_ni_decref_locked(ni, 0);
3272 lnet_unlink_lpni_recovery_mdh_locked(struct lnet_peer_ni *lpni, int cpt,
3275 struct lnet_handle_md recovery_mdh;
3277 LNetInvalidateMDHandle(&recovery_mdh);
3279 if (lpni->lpni_state & LNET_PEER_NI_RECOVERY_PENDING || force) {
3280 recovery_mdh = lpni->lpni_recovery_ping_mdh;
3281 LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
3283 spin_unlock(&lpni->lpni_lock);
3284 lnet_net_unlock(cpt);
3285 if (!LNetMDHandleIsInvalid(recovery_mdh))
3286 LNetMDUnlink(recovery_mdh);
3288 spin_lock(&lpni->lpni_lock);
3292 lnet_clean_peer_ni_recoveryq(void)
3294 struct lnet_peer_ni *lpni, *tmp;
3296 lnet_net_lock(LNET_LOCK_EX);
3298 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_mt_peerNIRecovq,
3300 list_del_init(&lpni->lpni_recovery);
3301 spin_lock(&lpni->lpni_lock);
3302 lnet_unlink_lpni_recovery_mdh_locked(lpni, LNET_LOCK_EX, true);
3303 spin_unlock(&lpni->lpni_lock);
3304 lnet_peer_ni_decref_locked(lpni);
3307 lnet_net_unlock(LNET_LOCK_EX);
3311 lnet_clean_resendqs(void)
3313 struct lnet_msg *msg, *tmp;
3317 cfs_cpt_for_each(i, lnet_cpt_table()) {
3319 list_splice_init(the_lnet.ln_mt_resendqs[i], &msgs);
3321 list_for_each_entry_safe(msg, tmp, &msgs, msg_list) {
3322 list_del_init(&msg->msg_list);
3323 msg->msg_no_resend = true;
3324 lnet_finalize(msg, -ESHUTDOWN);
3328 cfs_percpt_free(the_lnet.ln_mt_resendqs);
3332 lnet_recover_peer_nis(void)
3334 struct lnet_mt_event_info *ev_info;
3335 LIST_HEAD(processed_list);
3336 LIST_HEAD(local_queue);
3337 struct lnet_handle_md mdh;
3338 struct lnet_peer_ni *lpni;
3339 struct lnet_peer_ni *tmp;
3345 * Always use cpt 0 for locking across all interactions with
3346 * ln_mt_peerNIRecovq
3349 list_splice_init(&the_lnet.ln_mt_peerNIRecovq,
3353 list_for_each_entry_safe(lpni, tmp, &local_queue,
3356 * The same protection strategy is used here as is in the
3357 * local recovery case.
3360 healthv = atomic_read(&lpni->lpni_healthv);
3361 spin_lock(&lpni->lpni_lock);
3362 if (lpni->lpni_state & LNET_PEER_NI_DELETING ||
3363 healthv == LNET_MAX_HEALTH_VALUE) {
3364 list_del_init(&lpni->lpni_recovery);
3365 lnet_unlink_lpni_recovery_mdh_locked(lpni, 0, false);
3366 spin_unlock(&lpni->lpni_lock);
3367 lnet_peer_ni_decref_locked(lpni);
3373 * If the peer NI has failed recovery we must unlink the
3374 * md. But we want to keep the peer ni on the recovery
3375 * queue so we can try to continue recovering it
3377 if (lpni->lpni_state & LNET_PEER_NI_RECOVERY_FAILED) {
3378 lnet_unlink_lpni_recovery_mdh_locked(lpni, 0, true);
3379 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_FAILED;
3382 spin_unlock(&lpni->lpni_lock);
3386 * NOTE: we're racing with peer deletion from user space.
3387 * It's possible that a peer is deleted after we check its
3388 * state. In this case the recovery can create a new peer
3390 spin_lock(&lpni->lpni_lock);
3391 if (!(lpni->lpni_state & LNET_PEER_NI_RECOVERY_PENDING) &&
3392 !(lpni->lpni_state & LNET_PEER_NI_DELETING)) {
3393 lpni->lpni_state |= LNET_PEER_NI_RECOVERY_PENDING;
3394 spin_unlock(&lpni->lpni_lock);
3396 LIBCFS_ALLOC(ev_info, sizeof(*ev_info));
3398 CERROR("out of memory. Can't recover %s\n",
3399 libcfs_nid2str(lpni->lpni_nid));
3400 spin_lock(&lpni->lpni_lock);
3401 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3402 spin_unlock(&lpni->lpni_lock);
3406 /* look at the comments in lnet_recover_local_nis() */
3407 mdh = lpni->lpni_recovery_ping_mdh;
3408 LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
3409 nid = lpni->lpni_nid;
3411 list_del_init(&lpni->lpni_recovery);
3412 lnet_peer_ni_decref_locked(lpni);
3415 ev_info->mt_type = MT_TYPE_PEER_NI;
3416 ev_info->mt_nid = nid;
3417 rc = lnet_send_ping(nid, &mdh, LNET_INTERFACES_MIN,
3418 ev_info, the_lnet.ln_mt_handler,
3422 * lnet_find_peer_ni_locked() grabs a refcount for
3423 * us. No need to take it explicitly.
3425 lpni = lnet_find_peer_ni_locked(nid);
3432 lpni->lpni_recovery_ping_mdh = mdh;
3434 * While we're unlocked the lpni could've been
3435 * readded on the recovery queue. In this case we
3436 * don't need to add it to the local queue, since
3437 * it's already on there and the thread that added
3438 * it would've incremented the refcount on the
3439 * peer, which means we need to decref the refcount
3440 * that was implicitly grabbed by find_peer_ni_locked.
3441 * Otherwise, if the lpni is still not on
3442 * the recovery queue, then we'll add it to the
3445 if (list_empty(&lpni->lpni_recovery))
3446 list_add_tail(&lpni->lpni_recovery, &processed_list);
3448 lnet_peer_ni_decref_locked(lpni);
3451 spin_lock(&lpni->lpni_lock);
3453 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3455 spin_unlock(&lpni->lpni_lock);
3458 list_splice_init(&processed_list, &local_queue);
3460 list_splice(&local_queue, &the_lnet.ln_mt_peerNIRecovq);
3465 lnet_monitor_thread(void *arg)
3467 time64_t recovery_timeout = 0;
3468 time64_t rsp_timeout = 0;
3472 wait_for_completion(&the_lnet.ln_started);
3474 * The monitor thread takes care of the following:
3475 * 1. Checks the aliveness of routers
3476 * 2. Checks if there are messages on the resend queue to resend
3478 * 3. Check if there are any NIs on the local recovery queue and
3480 * 4. Checks if there are any NIs on the remote recovery queue
3483 while (the_lnet.ln_mt_state == LNET_MT_STATE_RUNNING) {
3484 now = ktime_get_real_seconds();
3486 if (lnet_router_checker_active())
3487 lnet_check_routers();
3489 lnet_resend_pending_msgs();
3491 if (now >= rsp_timeout) {
3492 lnet_finalize_expired_responses();
3493 rsp_timeout = now + (lnet_transaction_timeout / 2);
3496 if (now >= recovery_timeout) {
3497 lnet_recover_local_nis();
3498 lnet_recover_peer_nis();
3499 recovery_timeout = now + lnet_recovery_interval;
3503 * TODO do we need to check if we should sleep without
3504 * timeout? Technically, an active system will always
3505 * have messages in flight so this check will always
3506 * evaluate to false. And on an idle system do we care
3507 * if we wake up every 1 second? Although, we've seen
3508 * cases where we get a complaint that an idle thread
3509 * is waking up unnecessarily.
3511 * Take into account the current net_count when you wake
3512 * up for alive router checking, since we need to check
3513 * possibly as many networks as we have configured.
3515 interval = min(lnet_recovery_interval,
3516 min((unsigned int) alive_router_check_interval /
3517 lnet_current_net_count,
3518 lnet_transaction_timeout / 2));
3519 wait_for_completion_interruptible_timeout(
3520 &the_lnet.ln_mt_wait_complete,
3521 cfs_time_seconds(interval));
3522 /* Must re-init the completion before testing anything,
3523 * including ln_mt_state.
3525 reinit_completion(&the_lnet.ln_mt_wait_complete);
3529 lnet_net_lock(LNET_LOCK_EX);
3530 the_lnet.ln_mt_state = LNET_MT_STATE_SHUTDOWN;
3531 lnet_net_unlock(LNET_LOCK_EX);
3533 /* signal that the monitor thread is exiting */
3534 up(&the_lnet.ln_mt_signal);
3542 * Returns == 0 if success
3543 * Returns > 0 if LNetMDBind or prior fails
3544 * Returns < 0 if LNetGet fails
3547 lnet_send_ping(lnet_nid_t dest_nid,
3548 struct lnet_handle_md *mdh, int nnis,
3549 void *user_data, lnet_handler_t handler, bool recovery)
3551 struct lnet_md md = { NULL };
3552 struct lnet_process_id id;
3553 struct lnet_ping_buffer *pbuf;
3556 if (dest_nid == LNET_NID_ANY) {
3561 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
3567 /* initialize md content */
3568 md.start = &pbuf->pb_info;
3569 md.length = LNET_PING_INFO_SIZE(nnis);
3570 md.threshold = 2; /* GET/REPLY */
3572 md.options = LNET_MD_TRUNCATE;
3573 md.user_ptr = user_data;
3574 md.handler = handler;
3576 rc = LNetMDBind(&md, LNET_UNLINK, mdh);
3578 lnet_ping_buffer_decref(pbuf);
3579 CERROR("Can't bind MD: %d\n", rc);
3580 rc = -rc; /* change the rc to positive */
3583 id.pid = LNET_PID_LUSTRE;
3586 rc = LNetGet(LNET_NID_ANY, *mdh, id,
3587 LNET_RESERVED_PORTAL,
3588 LNET_PROTO_PING_MATCHBITS, 0, recovery);
3591 goto fail_unlink_md;
3597 LNetInvalidateMDHandle(mdh);
3603 lnet_handle_recovery_reply(struct lnet_mt_event_info *ev_info,
3604 int status, bool send, bool unlink_event)
3606 lnet_nid_t nid = ev_info->mt_nid;
3608 if (ev_info->mt_type == MT_TYPE_LOCAL_NI) {
3612 ni = lnet_nid2ni_locked(nid, 0);
3618 if (!send || (send && status != 0))
3619 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_PENDING;
3621 ni->ni_recovery_state |= LNET_NI_RECOVERY_FAILED;
3626 CERROR("local NI (%s) recovery failed with %d\n",
3627 libcfs_nid2str(nid), status);
3631 * need to increment healthv for the ni here, because in
3632 * the lnet_finalize() path we don't have access to this
3633 * NI. And in order to get access to it, we'll need to
3634 * carry forward too much information.
3635 * In the peer case, it'll naturally be incremented
3638 lnet_inc_healthv(&ni->ni_healthv,
3639 lnet_health_sensitivity);
3641 struct lnet_peer_ni *lpni;
3644 cpt = lnet_net_lock_current();
3645 lpni = lnet_find_peer_ni_locked(nid);
3647 lnet_net_unlock(cpt);
3650 spin_lock(&lpni->lpni_lock);
3651 if (!send || (send && status != 0))
3652 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3654 lpni->lpni_state |= LNET_PEER_NI_RECOVERY_FAILED;
3655 spin_unlock(&lpni->lpni_lock);
3656 lnet_peer_ni_decref_locked(lpni);
3657 lnet_net_unlock(cpt);
3660 CERROR("peer NI (%s) recovery failed with %d\n",
3661 libcfs_nid2str(nid), status);
3666 lnet_mt_event_handler(struct lnet_event *event)
3668 struct lnet_mt_event_info *ev_info = event->md_user_ptr;
3669 struct lnet_ping_buffer *pbuf;
3671 /* TODO: remove assert */
3672 LASSERT(event->type == LNET_EVENT_REPLY ||
3673 event->type == LNET_EVENT_SEND ||
3674 event->type == LNET_EVENT_UNLINK);
3676 CDEBUG(D_NET, "Received event: %d status: %d\n", event->type,
3679 switch (event->type) {
3680 case LNET_EVENT_UNLINK:
3681 CDEBUG(D_NET, "%s recovery ping unlinked\n",
3682 libcfs_nid2str(ev_info->mt_nid));
3684 case LNET_EVENT_REPLY:
3685 lnet_handle_recovery_reply(ev_info, event->status, false,
3686 event->type == LNET_EVENT_UNLINK);
3688 case LNET_EVENT_SEND:
3689 CDEBUG(D_NET, "%s recovery message sent %s:%d\n",
3690 libcfs_nid2str(ev_info->mt_nid),
3691 (event->status) ? "unsuccessfully" :
3692 "successfully", event->status);
3693 lnet_handle_recovery_reply(ev_info, event->status, true, false);
3696 CERROR("Unexpected event: %d\n", event->type);
3699 if (event->unlinked) {
3700 LIBCFS_FREE(ev_info, sizeof(*ev_info));
3701 pbuf = LNET_PING_INFO_TO_BUFFER(event->md_start);
3702 lnet_ping_buffer_decref(pbuf);
3707 lnet_rsp_tracker_create(void)
3709 struct list_head **rstqs;
3710 rstqs = lnet_create_array_of_queues();
3715 the_lnet.ln_mt_rstq = rstqs;
3721 lnet_rsp_tracker_clean(void)
3723 lnet_finalize_expired_responses();
3725 cfs_percpt_free(the_lnet.ln_mt_rstq);
3726 the_lnet.ln_mt_rstq = NULL;
3729 int lnet_monitor_thr_start(void)
3732 struct task_struct *task;
3734 if (the_lnet.ln_mt_state != LNET_MT_STATE_SHUTDOWN)
3737 rc = lnet_resendqs_create();
3741 rc = lnet_rsp_tracker_create();
3745 sema_init(&the_lnet.ln_mt_signal, 0);
3747 lnet_net_lock(LNET_LOCK_EX);
3748 the_lnet.ln_mt_state = LNET_MT_STATE_RUNNING;
3749 lnet_net_unlock(LNET_LOCK_EX);
3750 task = kthread_run(lnet_monitor_thread, NULL, "monitor_thread");
3753 CERROR("Can't start monitor thread: %d\n", rc);
3760 lnet_net_lock(LNET_LOCK_EX);
3761 the_lnet.ln_mt_state = LNET_MT_STATE_STOPPING;
3762 lnet_net_unlock(LNET_LOCK_EX);
3763 /* block until event callback signals exit */
3764 down(&the_lnet.ln_mt_signal);
3766 lnet_net_lock(LNET_LOCK_EX);
3767 the_lnet.ln_mt_state = LNET_MT_STATE_SHUTDOWN;
3768 lnet_net_unlock(LNET_LOCK_EX);
3769 lnet_rsp_tracker_clean();
3770 lnet_clean_local_ni_recoveryq();
3771 lnet_clean_peer_ni_recoveryq();
3772 lnet_clean_resendqs();
3773 the_lnet.ln_mt_handler = NULL;
3776 lnet_rsp_tracker_clean();
3777 lnet_clean_local_ni_recoveryq();
3778 lnet_clean_peer_ni_recoveryq();
3779 lnet_clean_resendqs();
3783 void lnet_monitor_thr_stop(void)
3785 if (the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN)
3788 LASSERT(the_lnet.ln_mt_state == LNET_MT_STATE_RUNNING);
3789 lnet_net_lock(LNET_LOCK_EX);
3790 the_lnet.ln_mt_state = LNET_MT_STATE_STOPPING;
3791 lnet_net_unlock(LNET_LOCK_EX);
3793 /* tell the monitor thread that we're shutting down */
3794 complete(&the_lnet.ln_mt_wait_complete);
3796 /* block until monitor thread signals that it's done */
3797 down(&the_lnet.ln_mt_signal);
3798 LASSERT(the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN);
3800 /* perform cleanup tasks */
3801 lnet_rsp_tracker_clean();
3802 lnet_clean_local_ni_recoveryq();
3803 lnet_clean_peer_ni_recoveryq();
3804 lnet_clean_resendqs();
3808 lnet_drop_message(struct lnet_ni *ni, int cpt, void *private, unsigned int nob,
3812 lnet_incr_stats(&ni->ni_stats, msg_type, LNET_STATS_TYPE_DROP);
3813 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
3814 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length += nob;
3815 lnet_net_unlock(cpt);
3817 lnet_ni_recv(ni, private, NULL, 0, 0, 0, nob);
3821 lnet_recv_put(struct lnet_ni *ni, struct lnet_msg *msg)
3823 struct lnet_hdr *hdr = &msg->msg_hdr;
3825 if (msg->msg_wanted != 0)
3826 lnet_setpayloadbuffer(msg);
3828 lnet_build_msg_event(msg, LNET_EVENT_PUT);
3830 /* Must I ACK? If so I'll grab the ack_wmd out of the header and put
3831 * it back into the ACK during lnet_finalize() */
3832 msg->msg_ack = (!lnet_is_wire_handle_none(&hdr->msg.put.ack_wmd) &&
3833 (msg->msg_md->md_options & LNET_MD_ACK_DISABLE) == 0);
3835 lnet_ni_recv(ni, msg->msg_private, msg, msg->msg_rx_delayed,
3836 msg->msg_offset, msg->msg_wanted, hdr->payload_length);
3840 lnet_parse_put(struct lnet_ni *ni, struct lnet_msg *msg)
3842 struct lnet_hdr *hdr = &msg->msg_hdr;
3843 struct lnet_match_info info;
3847 /* Convert put fields to host byte order */
3848 hdr->msg.put.match_bits = le64_to_cpu(hdr->msg.put.match_bits);
3849 hdr->msg.put.ptl_index = le32_to_cpu(hdr->msg.put.ptl_index);
3850 hdr->msg.put.offset = le32_to_cpu(hdr->msg.put.offset);
3852 /* Primary peer NID. */
3853 info.mi_id.nid = msg->msg_initiator;
3854 info.mi_id.pid = hdr->src_pid;
3855 info.mi_opc = LNET_MD_OP_PUT;
3856 info.mi_portal = hdr->msg.put.ptl_index;
3857 info.mi_rlength = hdr->payload_length;
3858 info.mi_roffset = hdr->msg.put.offset;
3859 info.mi_mbits = hdr->msg.put.match_bits;
3860 info.mi_cpt = lnet_cpt_of_nid(msg->msg_initiator, ni);
3862 msg->msg_rx_ready_delay = ni->ni_net->net_lnd->lnd_eager_recv == NULL;
3863 ready_delay = msg->msg_rx_ready_delay;
3866 rc = lnet_ptl_match_md(&info, msg);
3871 case LNET_MATCHMD_OK:
3872 lnet_recv_put(ni, msg);
3875 case LNET_MATCHMD_NONE:
3877 /* no eager_recv or has already called it, should
3878 * have been attached on delayed list */
3881 rc = lnet_ni_eager_recv(ni, msg);
3888 case LNET_MATCHMD_DROP:
3889 CNETERR("Dropping PUT from %s portal %d match %llu"
3890 " offset %d length %d: %d\n",
3891 libcfs_id2str(info.mi_id), info.mi_portal,
3892 info.mi_mbits, info.mi_roffset, info.mi_rlength, rc);
3894 return -ENOENT; /* -ve: OK but no match */
3899 lnet_parse_get(struct lnet_ni *ni, struct lnet_msg *msg, int rdma_get)
3901 struct lnet_match_info info;
3902 struct lnet_hdr *hdr = &msg->msg_hdr;
3903 struct lnet_process_id source_id;
3904 struct lnet_handle_wire reply_wmd;
3907 /* Convert get fields to host byte order */
3908 hdr->msg.get.match_bits = le64_to_cpu(hdr->msg.get.match_bits);
3909 hdr->msg.get.ptl_index = le32_to_cpu(hdr->msg.get.ptl_index);
3910 hdr->msg.get.sink_length = le32_to_cpu(hdr->msg.get.sink_length);
3911 hdr->msg.get.src_offset = le32_to_cpu(hdr->msg.get.src_offset);
3913 source_id.nid = hdr->src_nid;
3914 source_id.pid = hdr->src_pid;
3915 /* Primary peer NID */
3916 info.mi_id.nid = msg->msg_initiator;
3917 info.mi_id.pid = hdr->src_pid;
3918 info.mi_opc = LNET_MD_OP_GET;
3919 info.mi_portal = hdr->msg.get.ptl_index;
3920 info.mi_rlength = hdr->msg.get.sink_length;
3921 info.mi_roffset = hdr->msg.get.src_offset;
3922 info.mi_mbits = hdr->msg.get.match_bits;
3923 info.mi_cpt = lnet_cpt_of_nid(msg->msg_initiator, ni);
3925 rc = lnet_ptl_match_md(&info, msg);
3926 if (rc == LNET_MATCHMD_DROP) {
3927 CNETERR("Dropping GET from %s portal %d match %llu"
3928 " offset %d length %d\n",
3929 libcfs_id2str(info.mi_id), info.mi_portal,
3930 info.mi_mbits, info.mi_roffset, info.mi_rlength);
3931 return -ENOENT; /* -ve: OK but no match */
3934 LASSERT(rc == LNET_MATCHMD_OK);
3936 lnet_build_msg_event(msg, LNET_EVENT_GET);
3938 reply_wmd = hdr->msg.get.return_wmd;
3940 lnet_prep_send(msg, LNET_MSG_REPLY, source_id,
3941 msg->msg_offset, msg->msg_wanted);
3943 msg->msg_hdr.msg.reply.dst_wmd = reply_wmd;
3946 /* The LND completes the REPLY from her recv procedure */
3947 lnet_ni_recv(ni, msg->msg_private, msg, 0,
3948 msg->msg_offset, msg->msg_len, msg->msg_len);
3952 lnet_ni_recv(ni, msg->msg_private, NULL, 0, 0, 0, 0);
3953 msg->msg_receiving = 0;
3955 rc = lnet_send(ni->ni_nid, msg, msg->msg_from);
3957 /* didn't get as far as lnet_ni_send() */
3958 CERROR("%s: Unable to send REPLY for GET from %s: %d\n",
3959 libcfs_nid2str(ni->ni_nid),
3960 libcfs_id2str(info.mi_id), rc);
3962 lnet_finalize(msg, rc);
3969 lnet_parse_reply(struct lnet_ni *ni, struct lnet_msg *msg)
3971 void *private = msg->msg_private;
3972 struct lnet_hdr *hdr = &msg->msg_hdr;
3973 struct lnet_process_id src = {0};
3974 struct lnet_libmd *md;
3975 unsigned int rlength;
3976 unsigned int mlength;
3979 cpt = lnet_cpt_of_cookie(hdr->msg.reply.dst_wmd.wh_object_cookie);
3982 src.nid = hdr->src_nid;
3983 src.pid = hdr->src_pid;
3985 /* NB handles only looked up by creator (no flips) */
3986 md = lnet_wire_handle2md(&hdr->msg.reply.dst_wmd);
3987 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
3988 CNETERR("%s: Dropping REPLY from %s for %s "
3990 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
3991 (md == NULL) ? "invalid" : "inactive",
3992 hdr->msg.reply.dst_wmd.wh_interface_cookie,
3993 hdr->msg.reply.dst_wmd.wh_object_cookie);
3994 if (md != NULL && md->md_me != NULL)
3995 CERROR("REPLY MD also attached to portal %d\n",
3996 md->md_me->me_portal);
3998 lnet_res_unlock(cpt);
3999 return -ENOENT; /* -ve: OK but no match */
4002 LASSERT(md->md_offset == 0);
4004 rlength = hdr->payload_length;
4005 mlength = min(rlength, md->md_length);
4007 if (mlength < rlength &&
4008 (md->md_options & LNET_MD_TRUNCATE) == 0) {
4009 CNETERR("%s: Dropping REPLY from %s length %d "
4010 "for MD %#llx would overflow (%d)\n",
4011 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
4012 rlength, hdr->msg.reply.dst_wmd.wh_object_cookie,
4014 lnet_res_unlock(cpt);
4015 return -ENOENT; /* -ve: OK but no match */
4018 CDEBUG(D_NET, "%s: Reply from %s of length %d/%d into md %#llx\n",
4019 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
4020 mlength, rlength, hdr->msg.reply.dst_wmd.wh_object_cookie);
4022 lnet_msg_attach_md(msg, md, 0, mlength);
4025 lnet_setpayloadbuffer(msg);
4027 lnet_res_unlock(cpt);
4029 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
4031 lnet_ni_recv(ni, private, msg, 0, 0, mlength, rlength);
4036 lnet_parse_ack(struct lnet_ni *ni, struct lnet_msg *msg)
4038 struct lnet_hdr *hdr = &msg->msg_hdr;
4039 struct lnet_process_id src = {0};
4040 struct lnet_libmd *md;
4043 src.nid = hdr->src_nid;
4044 src.pid = hdr->src_pid;
4046 /* Convert ack fields to host byte order */
4047 hdr->msg.ack.match_bits = le64_to_cpu(hdr->msg.ack.match_bits);
4048 hdr->msg.ack.mlength = le32_to_cpu(hdr->msg.ack.mlength);
4050 cpt = lnet_cpt_of_cookie(hdr->msg.ack.dst_wmd.wh_object_cookie);
4053 /* NB handles only looked up by creator (no flips) */
4054 md = lnet_wire_handle2md(&hdr->msg.ack.dst_wmd);
4055 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
4056 /* Don't moan; this is expected */
4058 "%s: Dropping ACK from %s to %s MD %#llx.%#llx\n",
4059 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
4060 (md == NULL) ? "invalid" : "inactive",
4061 hdr->msg.ack.dst_wmd.wh_interface_cookie,
4062 hdr->msg.ack.dst_wmd.wh_object_cookie);
4063 if (md != NULL && md->md_me != NULL)
4064 CERROR("Source MD also attached to portal %d\n",
4065 md->md_me->me_portal);
4067 lnet_res_unlock(cpt);
4068 return -ENOENT; /* -ve! */
4071 CDEBUG(D_NET, "%s: ACK from %s into md %#llx\n",
4072 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
4073 hdr->msg.ack.dst_wmd.wh_object_cookie);
4075 lnet_msg_attach_md(msg, md, 0, 0);
4077 lnet_res_unlock(cpt);
4079 lnet_build_msg_event(msg, LNET_EVENT_ACK);
4081 lnet_ni_recv(ni, msg->msg_private, msg, 0, 0, 0, msg->msg_len);
4086 * \retval LNET_CREDIT_OK If \a msg is forwarded
4087 * \retval LNET_CREDIT_WAIT If \a msg is blocked because w/o buffer
4088 * \retval -ve error code
4091 lnet_parse_forward_locked(struct lnet_ni *ni, struct lnet_msg *msg)
4095 if (!the_lnet.ln_routing)
4098 if (msg->msg_rxpeer->lpni_rtrcredits <= 0 ||
4099 lnet_msg2bufpool(msg)->rbp_credits <= 0) {
4100 if (ni->ni_net->net_lnd->lnd_eager_recv == NULL) {
4101 msg->msg_rx_ready_delay = 1;
4103 lnet_net_unlock(msg->msg_rx_cpt);
4104 rc = lnet_ni_eager_recv(ni, msg);
4105 lnet_net_lock(msg->msg_rx_cpt);
4110 rc = lnet_post_routed_recv_locked(msg, 0);
4115 lnet_parse_local(struct lnet_ni *ni, struct lnet_msg *msg)
4119 switch (msg->msg_type) {
4121 rc = lnet_parse_ack(ni, msg);
4124 rc = lnet_parse_put(ni, msg);
4127 rc = lnet_parse_get(ni, msg, msg->msg_rdma_get);
4129 case LNET_MSG_REPLY:
4130 rc = lnet_parse_reply(ni, msg);
4132 default: /* prevent an unused label if !kernel */
4137 LASSERT(rc == 0 || rc == -ENOENT);
4142 lnet_msgtyp2str (int type)
4151 case LNET_MSG_REPLY:
4153 case LNET_MSG_HELLO:
4156 return ("<UNKNOWN>");
4161 lnet_print_hdr(struct lnet_hdr *hdr)
4163 struct lnet_process_id src = {
4164 .nid = hdr->src_nid,
4165 .pid = hdr->src_pid,
4167 struct lnet_process_id dst = {
4168 .nid = hdr->dest_nid,
4169 .pid = hdr->dest_pid,
4171 char *type_str = lnet_msgtyp2str(hdr->type);
4173 CWARN("P3 Header at %p of type %s\n", hdr, type_str);
4174 CWARN(" From %s\n", libcfs_id2str(src));
4175 CWARN(" To %s\n", libcfs_id2str(dst));
4177 switch (hdr->type) {
4182 CWARN(" Ptl index %d, ack md %#llx.%#llx, "
4183 "match bits %llu\n",
4184 hdr->msg.put.ptl_index,
4185 hdr->msg.put.ack_wmd.wh_interface_cookie,
4186 hdr->msg.put.ack_wmd.wh_object_cookie,
4187 hdr->msg.put.match_bits);
4188 CWARN(" Length %d, offset %d, hdr data %#llx\n",
4189 hdr->payload_length, hdr->msg.put.offset,
4190 hdr->msg.put.hdr_data);
4194 CWARN(" Ptl index %d, return md %#llx.%#llx, "
4195 "match bits %llu\n", hdr->msg.get.ptl_index,
4196 hdr->msg.get.return_wmd.wh_interface_cookie,
4197 hdr->msg.get.return_wmd.wh_object_cookie,
4198 hdr->msg.get.match_bits);
4199 CWARN(" Length %d, src offset %d\n",
4200 hdr->msg.get.sink_length,
4201 hdr->msg.get.src_offset);
4205 CWARN(" dst md %#llx.%#llx, "
4206 "manipulated length %d\n",
4207 hdr->msg.ack.dst_wmd.wh_interface_cookie,
4208 hdr->msg.ack.dst_wmd.wh_object_cookie,
4209 hdr->msg.ack.mlength);
4212 case LNET_MSG_REPLY:
4213 CWARN(" dst md %#llx.%#llx, "
4215 hdr->msg.reply.dst_wmd.wh_interface_cookie,
4216 hdr->msg.reply.dst_wmd.wh_object_cookie,
4217 hdr->payload_length);
4223 lnet_parse(struct lnet_ni *ni, struct lnet_hdr *hdr, lnet_nid_t from_nid,
4224 void *private, int rdma_req)
4226 struct lnet_peer_ni *lpni;
4227 struct lnet_msg *msg;
4228 __u32 payload_length;
4229 lnet_pid_t dest_pid;
4230 lnet_nid_t dest_nid;
4238 LASSERT (!in_interrupt ());
4240 type = le32_to_cpu(hdr->type);
4241 src_nid = le64_to_cpu(hdr->src_nid);
4242 dest_nid = le64_to_cpu(hdr->dest_nid);
4243 dest_pid = le32_to_cpu(hdr->dest_pid);
4244 payload_length = le32_to_cpu(hdr->payload_length);
4246 for_me = (ni->ni_nid == dest_nid);
4247 cpt = lnet_cpt_of_nid(from_nid, ni);
4249 CDEBUG(D_NET, "TRACE: %s(%s) <- %s : %s - %s\n",
4250 libcfs_nid2str(dest_nid),
4251 libcfs_nid2str(ni->ni_nid),
4252 libcfs_nid2str(src_nid),
4253 lnet_msgtyp2str(type),
4254 (for_me) ? "for me" : "routed");
4259 if (payload_length > 0) {
4260 CERROR("%s, src %s: bad %s payload %d (0 expected)\n",
4261 libcfs_nid2str(from_nid),
4262 libcfs_nid2str(src_nid),
4263 lnet_msgtyp2str(type), payload_length);
4269 case LNET_MSG_REPLY:
4270 if (payload_length >
4271 (__u32)(for_me ? LNET_MAX_PAYLOAD : LNET_MTU)) {
4272 CERROR("%s, src %s: bad %s payload %d "
4273 "(%d max expected)\n",
4274 libcfs_nid2str(from_nid),
4275 libcfs_nid2str(src_nid),
4276 lnet_msgtyp2str(type),
4278 for_me ? LNET_MAX_PAYLOAD : LNET_MTU);
4284 CERROR("%s, src %s: Bad message type 0x%x\n",
4285 libcfs_nid2str(from_nid),
4286 libcfs_nid2str(src_nid), type);
4290 if (the_lnet.ln_routing &&
4291 ni->ni_net->net_last_alive != ktime_get_real_seconds()) {
4293 spin_lock(&ni->ni_net->net_lock);
4294 ni->ni_net->net_last_alive = ktime_get_real_seconds();
4295 spin_unlock(&ni->ni_net->net_lock);
4296 if (ni->ni_status != NULL &&
4297 ni->ni_status->ns_status == LNET_NI_STATUS_DOWN) {
4298 ni->ni_status->ns_status = LNET_NI_STATUS_UP;
4305 lnet_push_update_to_peers(1);
4307 /* Regard a bad destination NID as a protocol error. Senders should
4308 * know what they're doing; if they don't they're misconfigured, buggy
4309 * or malicious so we chop them off at the knees :) */
4312 if (LNET_NIDNET(dest_nid) == LNET_NIDNET(ni->ni_nid)) {
4313 /* should have gone direct */
4314 CERROR("%s, src %s: Bad dest nid %s "
4315 "(should have been sent direct)\n",
4316 libcfs_nid2str(from_nid),
4317 libcfs_nid2str(src_nid),
4318 libcfs_nid2str(dest_nid));
4322 if (lnet_islocalnid(dest_nid)) {
4323 /* dest is another local NI; sender should have used
4324 * this node's NID on its own network */
4325 CERROR("%s, src %s: Bad dest nid %s "
4326 "(it's my nid but on a different network)\n",
4327 libcfs_nid2str(from_nid),
4328 libcfs_nid2str(src_nid),
4329 libcfs_nid2str(dest_nid));
4333 if (rdma_req && type == LNET_MSG_GET) {
4334 CERROR("%s, src %s: Bad optimized GET for %s "
4335 "(final destination must be me)\n",
4336 libcfs_nid2str(from_nid),
4337 libcfs_nid2str(src_nid),
4338 libcfs_nid2str(dest_nid));
4342 if (!the_lnet.ln_routing) {
4343 CERROR("%s, src %s: Dropping message for %s "
4344 "(routing not enabled)\n",
4345 libcfs_nid2str(from_nid),
4346 libcfs_nid2str(src_nid),
4347 libcfs_nid2str(dest_nid));
4352 /* Message looks OK; we're not going to return an error, so we MUST
4353 * call back lnd_recv() come what may... */
4355 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
4356 fail_peer(src_nid, 0)) { /* shall we now? */
4357 CERROR("%s, src %s: Dropping %s to simulate failure\n",
4358 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4359 lnet_msgtyp2str(type));
4363 if (!list_empty(&the_lnet.ln_drop_rules) &&
4364 lnet_drop_rule_match(hdr, ni->ni_nid, NULL)) {
4366 "%s, src %s, dst %s: Dropping %s to simulate silent message loss\n",
4367 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4368 libcfs_nid2str(dest_nid), lnet_msgtyp2str(type));
4372 if (lnet_drop_asym_route && for_me &&
4373 LNET_NIDNET(src_nid) != LNET_NIDNET(from_nid)) {
4374 struct lnet_net *net;
4375 struct lnet_remotenet *rnet;
4378 /* we are dealing with a routed message,
4379 * so see if route to reach src_nid goes through from_nid
4382 net = lnet_get_net_locked(LNET_NIDNET(ni->ni_nid));
4384 lnet_net_unlock(cpt);
4385 CERROR("net %s not found\n",
4386 libcfs_net2str(LNET_NIDNET(ni->ni_nid)));
4390 rnet = lnet_find_rnet_locked(LNET_NIDNET(src_nid));
4392 struct lnet_peer *gw = NULL;
4393 struct lnet_peer_ni *lpni = NULL;
4394 struct lnet_route *route;
4396 list_for_each_entry(route, &rnet->lrn_routes, lr_list) {
4398 gw = route->lr_gateway;
4399 if (route->lr_lnet != net->net_id)
4402 * if the nid is one of the gateway's NIDs
4403 * then this is a valid gateway
4405 while ((lpni = lnet_get_next_peer_ni_locked(gw,
4406 NULL, lpni)) != NULL) {
4407 if (lpni->lpni_nid == from_nid) {
4414 lnet_net_unlock(cpt);
4416 /* we would not use from_nid to route a message to
4418 * => asymmetric routing detected but forbidden
4420 CERROR("%s, src %s: Dropping asymmetrical route %s\n",
4421 libcfs_nid2str(from_nid),
4422 libcfs_nid2str(src_nid), lnet_msgtyp2str(type));
4427 msg = lnet_msg_alloc();
4429 CERROR("%s, src %s: Dropping %s (out of memory)\n",
4430 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4431 lnet_msgtyp2str(type));
4435 /* msg zeroed in lnet_msg_alloc; i.e. flags all clear,
4436 * pointers NULL etc */
4438 msg->msg_type = type;
4439 msg->msg_private = private;
4440 msg->msg_receiving = 1;
4441 msg->msg_rdma_get = rdma_req;
4442 msg->msg_len = msg->msg_wanted = payload_length;
4443 msg->msg_offset = 0;
4444 msg->msg_hdr = *hdr;
4445 /* for building message event */
4446 msg->msg_from = from_nid;
4448 msg->msg_target.pid = dest_pid;
4449 msg->msg_target.nid = dest_nid;
4450 msg->msg_routing = 1;
4453 /* convert common msg->hdr fields to host byteorder */
4454 msg->msg_hdr.type = type;
4455 msg->msg_hdr.src_nid = src_nid;
4456 msg->msg_hdr.src_pid = le32_to_cpu(msg->msg_hdr.src_pid);
4457 msg->msg_hdr.dest_nid = dest_nid;
4458 msg->msg_hdr.dest_pid = dest_pid;
4459 msg->msg_hdr.payload_length = payload_length;
4463 lpni = lnet_nid2peerni_locked(from_nid, ni->ni_nid, cpt);
4465 lnet_net_unlock(cpt);
4466 CERROR("%s, src %s: Dropping %s "
4467 "(error %ld looking up sender)\n",
4468 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4469 lnet_msgtyp2str(type), PTR_ERR(lpni));
4471 if (rc == -ESHUTDOWN)
4472 /* We are shutting down. Don't do anything more */
4477 if (the_lnet.ln_routing)
4478 lpni->lpni_last_alive = ktime_get_seconds();
4480 msg->msg_rxpeer = lpni;
4482 lnet_ni_addref_locked(ni, cpt);
4483 /* Multi-Rail: Primary NID of source. */
4484 msg->msg_initiator = lnet_peer_primary_nid_locked(src_nid);
4487 * mark the status of this lpni as UP since we received a message
4488 * from it. The ping response reports back the ns_status which is
4489 * marked on the remote as up or down and we cache it here.
4491 msg->msg_rxpeer->lpni_ns_status = LNET_NI_STATUS_UP;
4493 lnet_msg_commit(msg, cpt);
4495 /* message delay simulation */
4496 if (unlikely(!list_empty(&the_lnet.ln_delay_rules) &&
4497 lnet_delay_rule_match_locked(hdr, msg))) {
4498 lnet_net_unlock(cpt);
4503 rc = lnet_parse_forward_locked(ni, msg);
4504 lnet_net_unlock(cpt);
4509 if (rc == LNET_CREDIT_OK) {
4510 lnet_ni_recv(ni, msg->msg_private, msg, 0,
4511 0, payload_length, payload_length);
4516 lnet_net_unlock(cpt);
4518 rc = lnet_parse_local(ni, msg);
4524 LASSERT(msg->msg_md == NULL);
4525 lnet_finalize(msg, rc);
4528 lnet_drop_message(ni, cpt, private, payload_length, type);
4531 EXPORT_SYMBOL(lnet_parse);
4534 lnet_drop_delayed_msg_list(struct list_head *head, char *reason)
4536 while (!list_empty(head)) {
4537 struct lnet_process_id id = {0};
4538 struct lnet_msg *msg;
4540 msg = list_entry(head->next, struct lnet_msg, msg_list);
4541 list_del(&msg->msg_list);
4543 id.nid = msg->msg_hdr.src_nid;
4544 id.pid = msg->msg_hdr.src_pid;
4546 LASSERT(msg->msg_md == NULL);
4547 LASSERT(msg->msg_rx_delayed);
4548 LASSERT(msg->msg_rxpeer != NULL);
4549 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
4551 CWARN("Dropping delayed PUT from %s portal %d match %llu"
4552 " offset %d length %d: %s\n",
4554 msg->msg_hdr.msg.put.ptl_index,
4555 msg->msg_hdr.msg.put.match_bits,
4556 msg->msg_hdr.msg.put.offset,
4557 msg->msg_hdr.payload_length, reason);
4559 /* NB I can't drop msg's ref on msg_rxpeer until after I've
4560 * called lnet_drop_message(), so I just hang onto msg as well
4561 * until that's done */
4563 lnet_drop_message(msg->msg_rxni, msg->msg_rx_cpt,
4564 msg->msg_private, msg->msg_len,
4567 msg->msg_no_resend = true;
4569 * NB: message will not generate event because w/o attached MD,
4570 * but we still should give error code so lnet_msg_decommit()
4571 * can skip counters operations and other checks.
4573 lnet_finalize(msg, -ENOENT);
4578 lnet_recv_delayed_msg_list(struct list_head *head)
4580 while (!list_empty(head)) {
4581 struct lnet_msg *msg;
4582 struct lnet_process_id id;
4584 msg = list_entry(head->next, struct lnet_msg, msg_list);
4585 list_del(&msg->msg_list);
4587 /* md won't disappear under me, since each msg
4588 * holds a ref on it */
4590 id.nid = msg->msg_hdr.src_nid;
4591 id.pid = msg->msg_hdr.src_pid;
4593 LASSERT(msg->msg_rx_delayed);
4594 LASSERT(msg->msg_md != NULL);
4595 LASSERT(msg->msg_rxpeer != NULL);
4596 LASSERT(msg->msg_rxni != NULL);
4597 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
4599 CDEBUG(D_NET, "Resuming delayed PUT from %s portal %d "
4600 "match %llu offset %d length %d.\n",
4601 libcfs_id2str(id), msg->msg_hdr.msg.put.ptl_index,
4602 msg->msg_hdr.msg.put.match_bits,
4603 msg->msg_hdr.msg.put.offset,
4604 msg->msg_hdr.payload_length);
4606 lnet_recv_put(msg->msg_rxni, msg);
4611 lnet_attach_rsp_tracker(struct lnet_rsp_tracker *rspt, int cpt,
4612 struct lnet_libmd *md, struct lnet_handle_md mdh)
4615 struct lnet_rsp_tracker *local_rspt;
4618 * MD has a refcount taken by message so it's not going away.
4619 * The MD however can be looked up. We need to secure the access
4620 * to the md_rspt_ptr by taking the res_lock.
4621 * The rspt can be accessed without protection up to when it gets
4622 * added to the list.
4626 local_rspt = md->md_rspt_ptr;
4627 timeout_ns = lnet_transaction_timeout * NSEC_PER_SEC;
4628 if (local_rspt != NULL) {
4630 * we already have an rspt attached to the md, so we'll
4631 * update the deadline on that one.
4633 lnet_rspt_free(rspt, cpt);
4636 rspt->rspt_mdh = mdh;
4637 rspt->rspt_cpt = cpt;
4638 /* store the rspt so we can access it when we get the REPLY */
4639 md->md_rspt_ptr = rspt;
4642 local_rspt->rspt_deadline = ktime_add_ns(ktime_get(), timeout_ns);
4645 * add to the list of tracked responses. It's added to tail of the
4646 * list in order to expire all the older entries first.
4649 list_move_tail(&local_rspt->rspt_on_list, the_lnet.ln_mt_rstq[cpt]);
4650 lnet_net_unlock(cpt);
4651 lnet_res_unlock(cpt);
4655 * Initiate an asynchronous PUT operation.
4657 * There are several events associated with a PUT: completion of the send on
4658 * the initiator node (LNET_EVENT_SEND), and when the send completes
4659 * successfully, the receipt of an acknowledgment (LNET_EVENT_ACK) indicating
4660 * that the operation was accepted by the target. The event LNET_EVENT_PUT is
4661 * used at the target node to indicate the completion of incoming data
4664 * The local events will be logged in the EQ associated with the MD pointed to
4665 * by \a mdh handle. Using a MD without an associated EQ results in these
4666 * events being discarded. In this case, the caller must have another
4667 * mechanism (e.g., a higher level protocol) for determining when it is safe
4668 * to modify the memory region associated with the MD.
4670 * Note that LNet does not guarantee the order of LNET_EVENT_SEND and
4671 * LNET_EVENT_ACK, though intuitively ACK should happen after SEND.
4673 * \param self Indicates the NID of a local interface through which to send
4674 * the PUT request. Use LNET_NID_ANY to let LNet choose one by itself.
4675 * \param mdh A handle for the MD that describes the memory to be sent. The MD
4676 * must be "free floating" (See LNetMDBind()).
4677 * \param ack Controls whether an acknowledgment is requested.
4678 * Acknowledgments are only sent when they are requested by the initiating
4679 * process and the target MD enables them.
4680 * \param target A process identifier for the target process.
4681 * \param portal The index in the \a target's portal table.
4682 * \param match_bits The match bits to use for MD selection at the target
4684 * \param offset The offset into the target MD (only used when the target
4685 * MD has the LNET_MD_MANAGE_REMOTE option set).
4686 * \param hdr_data 64 bits of user data that can be included in the message
4687 * header. This data is written to an event queue entry at the target if an
4688 * EQ is present on the matching MD.
4690 * \retval 0 Success, and only in this case events will be generated
4691 * and logged to EQ (if it exists).
4692 * \retval -EIO Simulated failure.
4693 * \retval -ENOMEM Memory allocation failure.
4694 * \retval -ENOENT Invalid MD object.
4696 * \see struct lnet_event::hdr_data and lnet_event_kind_t.
4699 LNetPut(lnet_nid_t self, struct lnet_handle_md mdh, enum lnet_ack_req ack,
4700 struct lnet_process_id target, unsigned int portal,
4701 __u64 match_bits, unsigned int offset,
4704 struct lnet_msg *msg;
4705 struct lnet_libmd *md;
4708 struct lnet_rsp_tracker *rspt = NULL;
4710 LASSERT(the_lnet.ln_refcount > 0);
4712 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
4713 fail_peer(target.nid, 1)) { /* shall we now? */
4714 CERROR("Dropping PUT to %s: simulated failure\n",
4715 libcfs_id2str(target));
4719 msg = lnet_msg_alloc();
4721 CERROR("Dropping PUT to %s: ENOMEM on struct lnet_msg\n",
4722 libcfs_id2str(target));
4725 msg->msg_vmflush = !!(current->flags & PF_MEMALLOC);
4727 cpt = lnet_cpt_of_cookie(mdh.cookie);
4729 if (ack == LNET_ACK_REQ) {
4730 rspt = lnet_rspt_alloc(cpt);
4732 CERROR("Dropping PUT to %s: ENOMEM on response tracker\n",
4733 libcfs_id2str(target));
4736 INIT_LIST_HEAD(&rspt->rspt_on_list);
4741 md = lnet_handle2md(&mdh);
4742 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
4743 CERROR("Dropping PUT (%llu:%d:%s): MD (%d) invalid\n",
4744 match_bits, portal, libcfs_id2str(target),
4745 md == NULL ? -1 : md->md_threshold);
4746 if (md != NULL && md->md_me != NULL)
4747 CERROR("Source MD also attached to portal %d\n",
4748 md->md_me->me_portal);
4749 lnet_res_unlock(cpt);
4751 lnet_rspt_free(rspt, cpt);
4756 CDEBUG(D_NET, "LNetPut -> %s\n", libcfs_id2str(target));
4758 lnet_msg_attach_md(msg, md, 0, 0);
4760 lnet_prep_send(msg, LNET_MSG_PUT, target, 0, md->md_length);
4762 msg->msg_hdr.msg.put.match_bits = cpu_to_le64(match_bits);
4763 msg->msg_hdr.msg.put.ptl_index = cpu_to_le32(portal);
4764 msg->msg_hdr.msg.put.offset = cpu_to_le32(offset);
4765 msg->msg_hdr.msg.put.hdr_data = hdr_data;
4767 /* NB handles only looked up by creator (no flips) */
4768 if (ack == LNET_ACK_REQ) {
4769 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
4770 the_lnet.ln_interface_cookie;
4771 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
4772 md->md_lh.lh_cookie;
4774 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
4775 LNET_WIRE_HANDLE_COOKIE_NONE;
4776 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
4777 LNET_WIRE_HANDLE_COOKIE_NONE;
4780 lnet_res_unlock(cpt);
4782 lnet_build_msg_event(msg, LNET_EVENT_SEND);
4784 if (ack == LNET_ACK_REQ)
4785 lnet_attach_rsp_tracker(rspt, cpt, md, mdh);
4787 if (CFS_FAIL_CHECK_ORSET(CFS_FAIL_PTLRPC_OST_BULK_CB2,
4791 rc = lnet_send(self, msg, LNET_NID_ANY);
4794 CNETERR("Error sending PUT to %s: %d\n",
4795 libcfs_id2str(target), rc);
4796 msg->msg_no_resend = true;
4797 lnet_finalize(msg, rc);
4800 /* completion will be signalled by an event */
4803 EXPORT_SYMBOL(LNetPut);
4806 * The LND can DMA direct to the GET md (i.e. no REPLY msg). This
4807 * returns a msg for the LND to pass to lnet_finalize() when the sink
4808 * data has been received.
4810 * CAVEAT EMPTOR: 'getmsg' is the original GET, which is freed when
4811 * lnet_finalize() is called on it, so the LND must call this first
4814 lnet_create_reply_msg(struct lnet_ni *ni, struct lnet_msg *getmsg)
4816 struct lnet_msg *msg = lnet_msg_alloc();
4817 struct lnet_libmd *getmd = getmsg->msg_md;
4818 struct lnet_process_id peer_id = getmsg->msg_target;
4821 LASSERT(!getmsg->msg_target_is_router);
4822 LASSERT(!getmsg->msg_routing);
4825 CERROR("%s: Dropping REPLY from %s: can't allocate msg\n",
4826 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id));
4830 cpt = lnet_cpt_of_cookie(getmd->md_lh.lh_cookie);
4833 LASSERT(getmd->md_refcount > 0);
4835 if (getmd->md_threshold == 0) {
4836 CERROR("%s: Dropping REPLY from %s for inactive MD %p\n",
4837 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id),
4839 lnet_res_unlock(cpt);
4843 LASSERT(getmd->md_offset == 0);
4845 CDEBUG(D_NET, "%s: Reply from %s md %p\n",
4846 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id), getmd);
4848 /* setup information for lnet_build_msg_event */
4849 msg->msg_initiator = getmsg->msg_txpeer->lpni_peer_net->lpn_peer->lp_primary_nid;
4850 msg->msg_from = peer_id.nid;
4851 msg->msg_type = LNET_MSG_GET; /* flag this msg as an "optimized" GET */
4852 msg->msg_hdr.src_nid = peer_id.nid;
4853 msg->msg_hdr.payload_length = getmd->md_length;
4854 msg->msg_receiving = 1; /* required by lnet_msg_attach_md */
4856 lnet_msg_attach_md(msg, getmd, getmd->md_offset, getmd->md_length);
4857 lnet_res_unlock(cpt);
4859 cpt = lnet_cpt_of_nid(peer_id.nid, ni);
4862 lnet_msg_commit(msg, cpt);
4863 lnet_net_unlock(cpt);
4865 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
4870 cpt = lnet_cpt_of_nid(peer_id.nid, ni);
4873 lnet_incr_stats(&ni->ni_stats, LNET_MSG_GET, LNET_STATS_TYPE_DROP);
4874 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
4875 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length +=
4877 lnet_net_unlock(cpt);
4884 EXPORT_SYMBOL(lnet_create_reply_msg);
4887 lnet_set_reply_msg_len(struct lnet_ni *ni, struct lnet_msg *reply,
4890 /* Set the REPLY length, now the RDMA that elides the REPLY message has
4891 * completed and I know it. */
4892 LASSERT(reply != NULL);
4893 LASSERT(reply->msg_type == LNET_MSG_GET);
4894 LASSERT(reply->msg_ev.type == LNET_EVENT_REPLY);
4896 /* NB I trusted my peer to RDMA. If she tells me she's written beyond
4897 * the end of my buffer, I might as well be dead. */
4898 LASSERT(len <= reply->msg_ev.mlength);
4900 reply->msg_ev.mlength = len;
4902 EXPORT_SYMBOL(lnet_set_reply_msg_len);
4905 * Initiate an asynchronous GET operation.
4907 * On the initiator node, an LNET_EVENT_SEND is logged when the GET request
4908 * is sent, and an LNET_EVENT_REPLY is logged when the data returned from
4909 * the target node in the REPLY has been written to local MD.
4911 * On the target node, an LNET_EVENT_GET is logged when the GET request
4912 * arrives and is accepted into a MD.
4914 * \param self,target,portal,match_bits,offset See the discussion in LNetPut().
4915 * \param mdh A handle for the MD that describes the memory into which the
4916 * requested data will be received. The MD must be "free floating" (See LNetMDBind()).
4918 * \retval 0 Success, and only in this case events will be generated
4919 * and logged to EQ (if it exists) of the MD.
4920 * \retval -EIO Simulated failure.
4921 * \retval -ENOMEM Memory allocation failure.
4922 * \retval -ENOENT Invalid MD object.
4925 LNetGet(lnet_nid_t self, struct lnet_handle_md mdh,
4926 struct lnet_process_id target, unsigned int portal,
4927 __u64 match_bits, unsigned int offset, bool recovery)
4929 struct lnet_msg *msg;
4930 struct lnet_libmd *md;
4931 struct lnet_rsp_tracker *rspt;
4935 LASSERT(the_lnet.ln_refcount > 0);
4937 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
4938 fail_peer(target.nid, 1)) /* shall we now? */
4940 CERROR("Dropping GET to %s: simulated failure\n",
4941 libcfs_id2str(target));
4945 msg = lnet_msg_alloc();
4947 CERROR("Dropping GET to %s: ENOMEM on struct lnet_msg\n",
4948 libcfs_id2str(target));
4952 cpt = lnet_cpt_of_cookie(mdh.cookie);
4954 rspt = lnet_rspt_alloc(cpt);
4956 CERROR("Dropping GET to %s: ENOMEM on response tracker\n",
4957 libcfs_id2str(target));
4960 INIT_LIST_HEAD(&rspt->rspt_on_list);
4962 msg->msg_recovery = recovery;
4966 md = lnet_handle2md(&mdh);
4967 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
4968 CERROR("Dropping GET (%llu:%d:%s): MD (%d) invalid\n",
4969 match_bits, portal, libcfs_id2str(target),
4970 md == NULL ? -1 : md->md_threshold);
4971 if (md != NULL && md->md_me != NULL)
4972 CERROR("REPLY MD also attached to portal %d\n",
4973 md->md_me->me_portal);
4975 lnet_res_unlock(cpt);
4978 lnet_rspt_free(rspt, cpt);
4982 CDEBUG(D_NET, "LNetGet -> %s\n", libcfs_id2str(target));
4984 lnet_msg_attach_md(msg, md, 0, 0);
4986 lnet_prep_send(msg, LNET_MSG_GET, target, 0, 0);
4988 msg->msg_hdr.msg.get.match_bits = cpu_to_le64(match_bits);
4989 msg->msg_hdr.msg.get.ptl_index = cpu_to_le32(portal);
4990 msg->msg_hdr.msg.get.src_offset = cpu_to_le32(offset);
4991 msg->msg_hdr.msg.get.sink_length = cpu_to_le32(md->md_length);
4993 /* NB handles only looked up by creator (no flips) */
4994 msg->msg_hdr.msg.get.return_wmd.wh_interface_cookie =
4995 the_lnet.ln_interface_cookie;
4996 msg->msg_hdr.msg.get.return_wmd.wh_object_cookie =
4997 md->md_lh.lh_cookie;
4999 lnet_res_unlock(cpt);
5001 lnet_build_msg_event(msg, LNET_EVENT_SEND);
5003 lnet_attach_rsp_tracker(rspt, cpt, md, mdh);
5005 rc = lnet_send(self, msg, LNET_NID_ANY);
5007 CNETERR("Error sending GET to %s: %d\n",
5008 libcfs_id2str(target), rc);
5009 msg->msg_no_resend = true;
5010 lnet_finalize(msg, rc);
5013 /* completion will be signalled by an event */
5016 EXPORT_SYMBOL(LNetGet);
5019 * Calculate distance to node at \a dstnid.
5021 * \param dstnid Target NID.
5022 * \param srcnidp If not NULL, NID of the local interface to reach \a dstnid
5024 * \param orderp If not NULL, order of the route to reach \a dstnid is saved
5027 * \retval 0 If \a dstnid belongs to a local interface, and reserved option
5028 * local_nid_dist_zero is set, which is the default.
5029 * \retval positives Distance to target NID, i.e. number of hops plus one.
5030 * \retval -EHOSTUNREACH If \a dstnid is not reachable.
5033 LNetDist(lnet_nid_t dstnid, lnet_nid_t *srcnidp, __u32 *orderp)
5035 struct list_head *e;
5036 struct lnet_ni *ni = NULL;
5037 struct lnet_remotenet *rnet;
5038 __u32 dstnet = LNET_NIDNET(dstnid);
5042 struct list_head *rn_list;
5044 /* if !local_nid_dist_zero, I don't return a distance of 0 ever
5045 * (when lustre sees a distance of 0, it substitutes 0@lo), so I
5046 * keep order 0 free for 0@lo and order 1 free for a local NID
5049 LASSERT(the_lnet.ln_refcount > 0);
5051 cpt = lnet_net_lock_current();
5053 while ((ni = lnet_get_next_ni_locked(NULL, ni))) {
5054 if (ni->ni_nid == dstnid) {
5055 if (srcnidp != NULL)
5057 if (orderp != NULL) {
5058 if (dstnid == LNET_NID_LO_0)
5063 lnet_net_unlock(cpt);
5065 return local_nid_dist_zero ? 0 : 1;
5068 if (LNET_NIDNET(ni->ni_nid) == dstnet) {
5069 /* Check if ni was originally created in
5070 * current net namespace.
5071 * If not, assign order above 0xffff0000,
5072 * to make this ni not a priority. */
5073 if (current->nsproxy &&
5074 !net_eq(ni->ni_net_ns, current->nsproxy->net_ns))
5075 order += 0xffff0000;
5076 if (srcnidp != NULL)
5077 *srcnidp = ni->ni_nid;
5080 lnet_net_unlock(cpt);
5087 rn_list = lnet_net2rnethash(dstnet);
5088 list_for_each(e, rn_list) {
5089 rnet = list_entry(e, struct lnet_remotenet, lrn_list);
5091 if (rnet->lrn_net == dstnet) {
5092 struct lnet_route *route;
5093 struct lnet_route *shortest = NULL;
5094 __u32 shortest_hops = LNET_UNDEFINED_HOPS;
5097 LASSERT(!list_empty(&rnet->lrn_routes));
5099 list_for_each_entry(route, &rnet->lrn_routes,
5101 route_hops = route->lr_hops;
5102 if (route_hops == LNET_UNDEFINED_HOPS)
5104 if (shortest == NULL ||
5105 route_hops < shortest_hops) {
5107 shortest_hops = route_hops;
5111 LASSERT(shortest != NULL);
5112 hops = shortest_hops;
5113 if (srcnidp != NULL) {
5114 struct lnet_net *net;
5115 net = lnet_get_net_locked(shortest->lr_lnet);
5117 ni = lnet_get_next_ni_locked(net, NULL);
5118 *srcnidp = ni->ni_nid;
5122 lnet_net_unlock(cpt);
5128 lnet_net_unlock(cpt);
5129 return -EHOSTUNREACH;
5131 EXPORT_SYMBOL(LNetDist);