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_extract_iov(int dst_niov, struct kvec *dst,
343 int src_niov, struct kvec *src,
344 unsigned int offset, unsigned int len)
346 /* Initialise 'dst' to the subset of 'src' starting at 'offset',
347 * for exactly 'len' bytes, and return the number of entries.
348 * NB not destructive to 'src' */
349 unsigned int frag_len;
352 if (len == 0) /* no data => */
353 return (0); /* no frags */
355 LASSERT(src_niov > 0);
356 while (offset >= src->iov_len) { /* skip initial frags */
357 offset -= src->iov_len;
360 LASSERT(src_niov > 0);
365 LASSERT(src_niov > 0);
366 LASSERT((int)niov <= dst_niov);
368 frag_len = src->iov_len - offset;
369 dst->iov_base = ((char *)src->iov_base) + offset;
371 if (len <= frag_len) {
376 dst->iov_len = frag_len;
386 EXPORT_SYMBOL(lnet_extract_iov);
390 lnet_kiov_nob(unsigned int niov, struct bio_vec *kiov)
392 unsigned int nob = 0;
394 LASSERT(niov == 0 || kiov != NULL);
396 nob += (kiov++)->bv_len;
400 EXPORT_SYMBOL(lnet_kiov_nob);
403 lnet_copy_kiov2kiov(unsigned int ndiov, struct bio_vec *diov,
404 unsigned int doffset,
405 unsigned int nsiov, struct bio_vec *siov,
406 unsigned int soffset,
409 /* NB diov, siov are READ-ONLY */
410 unsigned int this_nob;
417 LASSERT (!in_interrupt ());
420 while (doffset >= diov->bv_len) {
421 doffset -= diov->bv_len;
428 while (soffset >= siov->bv_len) {
429 soffset -= siov->bv_len;
438 this_nob = min3(diov->bv_len - doffset,
439 siov->bv_len - soffset,
443 daddr = ((char *)kmap(diov->bv_page)) +
444 diov->bv_offset + doffset;
446 saddr = ((char *)kmap(siov->bv_page)) +
447 siov->bv_offset + soffset;
449 /* Vanishing risk of kmap deadlock when mapping 2 pages.
450 * However in practice at least one of the kiovs will be mapped
451 * kernel pages and the map/unmap will be NOOPs */
453 memcpy (daddr, saddr, this_nob);
456 if (diov->bv_len > doffset + this_nob) {
460 kunmap(diov->bv_page);
467 if (siov->bv_len > soffset + this_nob) {
471 kunmap(siov->bv_page);
480 kunmap(diov->bv_page);
482 kunmap(siov->bv_page);
484 EXPORT_SYMBOL(lnet_copy_kiov2kiov);
487 lnet_copy_kiov2iov (unsigned int niov, struct kvec *iov, unsigned int iovoffset,
488 unsigned int nkiov, struct bio_vec *kiov,
489 unsigned int kiovoffset,
492 /* NB iov, kiov are READ-ONLY */
493 unsigned int this_nob;
499 LASSERT (!in_interrupt ());
502 while (iovoffset >= iov->iov_len) {
503 iovoffset -= iov->iov_len;
510 while (kiovoffset >= kiov->bv_len) {
511 kiovoffset -= kiov->bv_len;
520 this_nob = min3((unsigned int)iov->iov_len - iovoffset,
521 (unsigned int)kiov->bv_len - kiovoffset,
525 addr = ((char *)kmap(kiov->bv_page)) +
526 kiov->bv_offset + kiovoffset;
528 memcpy((char *)iov->iov_base + iovoffset, addr, this_nob);
531 if (iov->iov_len > iovoffset + this_nob) {
532 iovoffset += this_nob;
539 if (kiov->bv_len > kiovoffset + this_nob) {
541 kiovoffset += this_nob;
543 kunmap(kiov->bv_page);
553 kunmap(kiov->bv_page);
555 EXPORT_SYMBOL(lnet_copy_kiov2iov);
558 lnet_copy_iov2kiov(unsigned int nkiov, struct bio_vec *kiov,
559 unsigned int kiovoffset,
560 unsigned int niov, struct kvec *iov, unsigned int iovoffset,
563 /* NB kiov, iov are READ-ONLY */
564 unsigned int this_nob;
570 LASSERT (!in_interrupt ());
573 while (kiovoffset >= kiov->bv_len) {
574 kiovoffset -= kiov->bv_len;
581 while (iovoffset >= iov->iov_len) {
582 iovoffset -= iov->iov_len;
591 this_nob = min3((unsigned int)kiov->bv_len - kiovoffset,
592 (unsigned int)iov->iov_len - iovoffset,
596 addr = ((char *)kmap(kiov->bv_page)) +
597 kiov->bv_offset + kiovoffset;
599 memcpy (addr, (char *)iov->iov_base + iovoffset, this_nob);
602 if (kiov->bv_len > kiovoffset + this_nob) {
604 kiovoffset += this_nob;
606 kunmap(kiov->bv_page);
613 if (iov->iov_len > iovoffset + this_nob) {
614 iovoffset += this_nob;
623 kunmap(kiov->bv_page);
625 EXPORT_SYMBOL(lnet_copy_iov2kiov);
628 lnet_extract_kiov(int dst_niov, struct bio_vec *dst,
629 int src_niov, struct bio_vec *src,
630 unsigned int offset, unsigned int len)
632 /* Initialise 'dst' to the subset of 'src' starting at 'offset',
633 * for exactly 'len' bytes, and return the number of entries.
634 * NB not destructive to 'src' */
635 unsigned int frag_len;
638 if (len == 0) /* no data => */
639 return (0); /* no frags */
641 LASSERT(src_niov > 0);
642 while (offset >= src->bv_len) { /* skip initial frags */
643 offset -= src->bv_len;
646 LASSERT(src_niov > 0);
651 LASSERT(src_niov > 0);
652 LASSERT((int)niov <= dst_niov);
654 frag_len = src->bv_len - offset;
655 dst->bv_page = src->bv_page;
656 dst->bv_offset = src->bv_offset + offset;
658 if (len <= frag_len) {
660 LASSERT(dst->bv_offset + dst->bv_len <= PAGE_SIZE);
664 dst->bv_len = frag_len;
665 LASSERT(dst->bv_offset + dst->bv_len <= PAGE_SIZE);
675 EXPORT_SYMBOL(lnet_extract_kiov);
678 lnet_ni_recv(struct lnet_ni *ni, void *private, struct lnet_msg *msg,
679 int delayed, unsigned int offset, unsigned int mlen,
682 unsigned int niov = 0;
683 struct kvec *iov = NULL;
684 struct bio_vec *kiov = NULL;
687 LASSERT (!in_interrupt ());
688 LASSERT (mlen == 0 || msg != NULL);
691 LASSERT(msg->msg_receiving);
692 LASSERT(!msg->msg_sending);
693 LASSERT(rlen == msg->msg_len);
694 LASSERT(mlen <= msg->msg_len);
695 LASSERT(msg->msg_offset == offset);
696 LASSERT(msg->msg_wanted == mlen);
698 msg->msg_receiving = 0;
701 niov = msg->msg_niov;
702 kiov = msg->msg_kiov;
705 LASSERT ((iov == NULL) != (kiov == NULL));
709 rc = (ni->ni_net->net_lnd->lnd_recv)(ni, private, msg, delayed,
710 niov, kiov, offset, mlen,
713 lnet_finalize(msg, rc);
717 lnet_setpayloadbuffer(struct lnet_msg *msg)
719 struct lnet_libmd *md = msg->msg_md;
721 LASSERT(msg->msg_len > 0);
722 LASSERT(!msg->msg_routing);
724 LASSERT(msg->msg_niov == 0);
725 LASSERT(msg->msg_kiov == NULL);
727 msg->msg_niov = md->md_niov;
728 msg->msg_kiov = md->md_kiov;
732 lnet_prep_send(struct lnet_msg *msg, int type, struct lnet_process_id target,
733 unsigned int offset, unsigned int len)
735 msg->msg_type = type;
736 msg->msg_target = target;
738 msg->msg_offset = offset;
741 lnet_setpayloadbuffer(msg);
743 memset (&msg->msg_hdr, 0, sizeof (msg->msg_hdr));
744 msg->msg_hdr.type = cpu_to_le32(type);
745 /* dest_nid will be overwritten by lnet_select_pathway() */
746 msg->msg_hdr.dest_nid = cpu_to_le64(target.nid);
747 msg->msg_hdr.dest_pid = cpu_to_le32(target.pid);
748 /* src_nid will be set later */
749 msg->msg_hdr.src_pid = cpu_to_le32(the_lnet.ln_pid);
750 msg->msg_hdr.payload_length = cpu_to_le32(len);
754 lnet_ni_send(struct lnet_ni *ni, struct lnet_msg *msg)
756 void *priv = msg->msg_private;
759 LASSERT(!in_interrupt());
760 LASSERT(ni->ni_nid == LNET_NID_LO_0 ||
761 (msg->msg_txcredit && msg->msg_peertxcredit));
763 rc = (ni->ni_net->net_lnd->lnd_send)(ni, priv, msg);
765 msg->msg_no_resend = true;
766 lnet_finalize(msg, rc);
771 lnet_ni_eager_recv(struct lnet_ni *ni, struct lnet_msg *msg)
775 LASSERT(!msg->msg_sending);
776 LASSERT(msg->msg_receiving);
777 LASSERT(!msg->msg_rx_ready_delay);
778 LASSERT(ni->ni_net->net_lnd->lnd_eager_recv != NULL);
780 msg->msg_rx_ready_delay = 1;
781 rc = (ni->ni_net->net_lnd->lnd_eager_recv)(ni, msg->msg_private, msg,
784 CERROR("recv from %s / send to %s aborted: "
785 "eager_recv failed %d\n",
786 libcfs_nid2str(msg->msg_rxpeer->lpni_nid),
787 libcfs_id2str(msg->msg_target), rc);
788 LASSERT(rc < 0); /* required by my callers */
795 lnet_is_peer_deadline_passed(struct lnet_peer_ni *lpni, time64_t now)
799 deadline = lpni->lpni_last_alive +
800 lpni->lpni_net->net_tunables.lct_peer_timeout;
803 * assume peer_ni is alive as long as we're within the configured
812 /* NB: returns 1 when alive, 0 when dead, negative when error;
813 * may drop the lnet_net_lock */
815 lnet_peer_alive_locked(struct lnet_ni *ni, struct lnet_peer_ni *lpni,
816 struct lnet_msg *msg)
818 time64_t now = ktime_get_seconds();
820 if (!lnet_peer_aliveness_enabled(lpni))
824 * If we're resending a message, let's attempt to send it even if
825 * the peer is down to fulfill our resend quota on the message
827 if (msg->msg_retry_count > 0)
830 /* try and send recovery messages irregardless */
831 if (msg->msg_recovery)
834 /* always send any responses */
835 if (lnet_msg_is_response(msg))
838 if (!lnet_is_peer_deadline_passed(lpni, now))
841 return lnet_is_peer_ni_alive(lpni);
845 * \param msg The message to be sent.
846 * \param do_send True if lnet_ni_send() should be called in this function.
847 * lnet_send() is going to lnet_net_unlock immediately after this, so
848 * it sets do_send FALSE and I don't do the unlock/send/lock bit.
850 * \retval LNET_CREDIT_OK If \a msg sent or OK to send.
851 * \retval LNET_CREDIT_WAIT If \a msg blocked for credit.
852 * \retval -EHOSTUNREACH If the next hop of the message appears dead.
853 * \retval -ECANCELED If the MD of the message has been unlinked.
856 lnet_post_send_locked(struct lnet_msg *msg, int do_send)
858 struct lnet_peer_ni *lp = msg->msg_txpeer;
859 struct lnet_ni *ni = msg->msg_txni;
860 int cpt = msg->msg_tx_cpt;
861 struct lnet_tx_queue *tq = ni->ni_tx_queues[cpt];
863 /* non-lnet_send() callers have checked before */
864 LASSERT(!do_send || msg->msg_tx_delayed);
865 LASSERT(!msg->msg_receiving);
866 LASSERT(msg->msg_tx_committed);
867 /* can't get here if we're sending to the loopback interface */
868 LASSERT(lp->lpni_nid != the_lnet.ln_loni->ni_nid);
870 /* NB 'lp' is always the next hop */
871 if ((msg->msg_target.pid & LNET_PID_USERFLAG) == 0 &&
872 lnet_peer_alive_locked(ni, lp, msg) == 0) {
873 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
874 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length +=
876 lnet_net_unlock(cpt);
878 lnet_incr_stats(&msg->msg_txpeer->lpni_stats,
880 LNET_STATS_TYPE_DROP);
882 lnet_incr_stats(&msg->msg_txni->ni_stats,
884 LNET_STATS_TYPE_DROP);
886 CNETERR("Dropping message for %s: peer not alive\n",
887 libcfs_id2str(msg->msg_target));
888 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_DROPPED;
890 lnet_finalize(msg, -EHOSTUNREACH);
893 return -EHOSTUNREACH;
896 if (msg->msg_md != NULL &&
897 (msg->msg_md->md_flags & LNET_MD_FLAG_ABORTED) != 0) {
898 lnet_net_unlock(cpt);
900 CNETERR("Aborting message for %s: LNetM[DE]Unlink() already "
901 "called on the MD/ME.\n",
902 libcfs_id2str(msg->msg_target));
904 msg->msg_no_resend = true;
905 CDEBUG(D_NET, "msg %p to %s canceled and will not be resent\n",
906 msg, libcfs_id2str(msg->msg_target));
907 lnet_finalize(msg, -ECANCELED);
914 if (!msg->msg_peertxcredit) {
915 spin_lock(&lp->lpni_lock);
916 LASSERT((lp->lpni_txcredits < 0) ==
917 !list_empty(&lp->lpni_txq));
919 msg->msg_peertxcredit = 1;
920 lp->lpni_txqnob += msg->msg_len + sizeof(struct lnet_hdr);
921 lp->lpni_txcredits--;
923 if (lp->lpni_txcredits < lp->lpni_mintxcredits)
924 lp->lpni_mintxcredits = lp->lpni_txcredits;
926 if (lp->lpni_txcredits < 0) {
927 msg->msg_tx_delayed = 1;
928 list_add_tail(&msg->msg_list, &lp->lpni_txq);
929 spin_unlock(&lp->lpni_lock);
930 return LNET_CREDIT_WAIT;
932 spin_unlock(&lp->lpni_lock);
935 if (!msg->msg_txcredit) {
936 LASSERT((tq->tq_credits < 0) ==
937 !list_empty(&tq->tq_delayed));
939 msg->msg_txcredit = 1;
941 atomic_dec(&ni->ni_tx_credits);
943 if (tq->tq_credits < tq->tq_credits_min)
944 tq->tq_credits_min = tq->tq_credits;
946 if (tq->tq_credits < 0) {
947 msg->msg_tx_delayed = 1;
948 list_add_tail(&msg->msg_list, &tq->tq_delayed);
949 return LNET_CREDIT_WAIT;
953 /* unset the tx_delay flag as we're going to send it now */
954 msg->msg_tx_delayed = 0;
957 lnet_net_unlock(cpt);
958 lnet_ni_send(ni, msg);
961 return LNET_CREDIT_OK;
965 static struct lnet_rtrbufpool *
966 lnet_msg2bufpool(struct lnet_msg *msg)
968 struct lnet_rtrbufpool *rbp;
971 LASSERT(msg->msg_rx_committed);
973 cpt = msg->msg_rx_cpt;
974 rbp = &the_lnet.ln_rtrpools[cpt][0];
976 LASSERT(msg->msg_len <= LNET_MTU);
977 while (msg->msg_len > (unsigned int)rbp->rbp_npages * PAGE_SIZE) {
979 LASSERT(rbp < &the_lnet.ln_rtrpools[cpt][LNET_NRBPOOLS]);
986 lnet_post_routed_recv_locked(struct lnet_msg *msg, int do_recv)
988 /* lnet_parse is going to lnet_net_unlock immediately after this, so it
989 * sets do_recv FALSE and I don't do the unlock/send/lock bit.
990 * I return LNET_CREDIT_WAIT if msg blocked and LNET_CREDIT_OK if
991 * received or OK to receive */
992 struct lnet_peer_ni *lpni = msg->msg_rxpeer;
993 struct lnet_peer *lp;
994 struct lnet_rtrbufpool *rbp;
995 struct lnet_rtrbuf *rb;
997 LASSERT(msg->msg_kiov == NULL);
998 LASSERT(msg->msg_niov == 0);
999 LASSERT(msg->msg_routing);
1000 LASSERT(msg->msg_receiving);
1001 LASSERT(!msg->msg_sending);
1002 LASSERT(lpni->lpni_peer_net);
1003 LASSERT(lpni->lpni_peer_net->lpn_peer);
1005 lp = lpni->lpni_peer_net->lpn_peer;
1007 /* non-lnet_parse callers only receive delayed messages */
1008 LASSERT(!do_recv || msg->msg_rx_delayed);
1010 if (!msg->msg_peerrtrcredit) {
1011 /* lpni_lock protects the credit manipulation */
1012 spin_lock(&lpni->lpni_lock);
1014 msg->msg_peerrtrcredit = 1;
1015 lpni->lpni_rtrcredits--;
1016 if (lpni->lpni_rtrcredits < lpni->lpni_minrtrcredits)
1017 lpni->lpni_minrtrcredits = lpni->lpni_rtrcredits;
1019 if (lpni->lpni_rtrcredits < 0) {
1020 spin_unlock(&lpni->lpni_lock);
1021 /* must have checked eager_recv before here */
1022 LASSERT(msg->msg_rx_ready_delay);
1023 msg->msg_rx_delayed = 1;
1024 /* lp_lock protects the lp_rtrq */
1025 spin_lock(&lp->lp_lock);
1026 list_add_tail(&msg->msg_list, &lp->lp_rtrq);
1027 spin_unlock(&lp->lp_lock);
1028 return LNET_CREDIT_WAIT;
1030 spin_unlock(&lpni->lpni_lock);
1033 rbp = lnet_msg2bufpool(msg);
1035 if (!msg->msg_rtrcredit) {
1036 msg->msg_rtrcredit = 1;
1038 if (rbp->rbp_credits < rbp->rbp_mincredits)
1039 rbp->rbp_mincredits = rbp->rbp_credits;
1041 if (rbp->rbp_credits < 0) {
1042 /* must have checked eager_recv before here */
1043 LASSERT(msg->msg_rx_ready_delay);
1044 msg->msg_rx_delayed = 1;
1045 list_add_tail(&msg->msg_list, &rbp->rbp_msgs);
1046 return LNET_CREDIT_WAIT;
1050 LASSERT(!list_empty(&rbp->rbp_bufs));
1051 rb = list_entry(rbp->rbp_bufs.next, struct lnet_rtrbuf, rb_list);
1052 list_del(&rb->rb_list);
1054 msg->msg_niov = rbp->rbp_npages;
1055 msg->msg_kiov = &rb->rb_kiov[0];
1057 /* unset the msg-rx_delayed flag since we're receiving the message */
1058 msg->msg_rx_delayed = 0;
1061 int cpt = msg->msg_rx_cpt;
1063 lnet_net_unlock(cpt);
1064 lnet_ni_recv(msg->msg_rxni, msg->msg_private, msg, 1,
1065 0, msg->msg_len, msg->msg_len);
1068 return LNET_CREDIT_OK;
1072 lnet_return_tx_credits_locked(struct lnet_msg *msg)
1074 struct lnet_peer_ni *txpeer = msg->msg_txpeer;
1075 struct lnet_ni *txni = msg->msg_txni;
1076 struct lnet_msg *msg2;
1078 if (msg->msg_txcredit) {
1079 struct lnet_ni *ni = msg->msg_txni;
1080 struct lnet_tx_queue *tq = ni->ni_tx_queues[msg->msg_tx_cpt];
1082 /* give back NI txcredits */
1083 msg->msg_txcredit = 0;
1085 LASSERT((tq->tq_credits < 0) ==
1086 !list_empty(&tq->tq_delayed));
1089 atomic_inc(&ni->ni_tx_credits);
1090 if (tq->tq_credits <= 0) {
1091 msg2 = list_entry(tq->tq_delayed.next,
1092 struct lnet_msg, msg_list);
1093 list_del(&msg2->msg_list);
1095 LASSERT(msg2->msg_txni == ni);
1096 LASSERT(msg2->msg_tx_delayed);
1097 LASSERT(msg2->msg_tx_cpt == msg->msg_tx_cpt);
1099 (void) lnet_post_send_locked(msg2, 1);
1103 if (msg->msg_peertxcredit) {
1104 /* give back peer txcredits */
1105 msg->msg_peertxcredit = 0;
1107 spin_lock(&txpeer->lpni_lock);
1108 LASSERT((txpeer->lpni_txcredits < 0) ==
1109 !list_empty(&txpeer->lpni_txq));
1111 txpeer->lpni_txqnob -= msg->msg_len + sizeof(struct lnet_hdr);
1112 LASSERT(txpeer->lpni_txqnob >= 0);
1114 txpeer->lpni_txcredits++;
1115 if (txpeer->lpni_txcredits <= 0) {
1118 msg2 = list_entry(txpeer->lpni_txq.next,
1119 struct lnet_msg, msg_list);
1120 list_del(&msg2->msg_list);
1121 spin_unlock(&txpeer->lpni_lock);
1123 LASSERT(msg2->msg_txpeer == txpeer);
1124 LASSERT(msg2->msg_tx_delayed);
1126 msg2_cpt = msg2->msg_tx_cpt;
1129 * The msg_cpt can be different from the msg2_cpt
1130 * so we need to make sure we lock the correct cpt
1132 * Once we call lnet_post_send_locked() it is no
1133 * longer safe to access msg2, since it could've
1134 * been freed by lnet_finalize(), but we still
1135 * need to relock the correct cpt, so we cache the
1136 * msg2_cpt for the purpose of the check that
1137 * follows the call to lnet_pose_send_locked().
1139 if (msg2_cpt != msg->msg_tx_cpt) {
1140 lnet_net_unlock(msg->msg_tx_cpt);
1141 lnet_net_lock(msg2_cpt);
1143 (void) lnet_post_send_locked(msg2, 1);
1144 if (msg2_cpt != msg->msg_tx_cpt) {
1145 lnet_net_unlock(msg2_cpt);
1146 lnet_net_lock(msg->msg_tx_cpt);
1149 spin_unlock(&txpeer->lpni_lock);
1154 msg->msg_txni = NULL;
1155 lnet_ni_decref_locked(txni, msg->msg_tx_cpt);
1158 if (txpeer != NULL) {
1159 msg->msg_txpeer = NULL;
1160 lnet_peer_ni_decref_locked(txpeer);
1165 lnet_schedule_blocked_locked(struct lnet_rtrbufpool *rbp)
1167 struct lnet_msg *msg;
1169 if (list_empty(&rbp->rbp_msgs))
1171 msg = list_entry(rbp->rbp_msgs.next,
1172 struct lnet_msg, msg_list);
1173 list_del(&msg->msg_list);
1175 (void)lnet_post_routed_recv_locked(msg, 1);
1179 lnet_drop_routed_msgs_locked(struct list_head *list, int cpt)
1181 struct lnet_msg *msg;
1182 struct lnet_msg *tmp;
1184 lnet_net_unlock(cpt);
1186 list_for_each_entry_safe(msg, tmp, list, msg_list) {
1187 lnet_ni_recv(msg->msg_rxni, msg->msg_private, NULL,
1188 0, 0, 0, msg->msg_hdr.payload_length);
1189 list_del_init(&msg->msg_list);
1190 msg->msg_no_resend = true;
1191 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_ERROR;
1192 lnet_finalize(msg, -ECANCELED);
1199 lnet_return_rx_credits_locked(struct lnet_msg *msg)
1201 struct lnet_peer_ni *rxpeerni = msg->msg_rxpeer;
1202 struct lnet_peer *lp;
1203 struct lnet_ni *rxni = msg->msg_rxni;
1204 struct lnet_msg *msg2;
1206 if (msg->msg_rtrcredit) {
1207 /* give back global router credits */
1208 struct lnet_rtrbuf *rb;
1209 struct lnet_rtrbufpool *rbp;
1211 /* NB If a msg ever blocks for a buffer in rbp_msgs, it stays
1212 * there until it gets one allocated, or aborts the wait
1214 LASSERT(msg->msg_kiov != NULL);
1216 rb = list_entry(msg->msg_kiov, struct lnet_rtrbuf, rb_kiov[0]);
1219 msg->msg_kiov = NULL;
1220 msg->msg_rtrcredit = 0;
1222 LASSERT(rbp == lnet_msg2bufpool(msg));
1224 LASSERT((rbp->rbp_credits > 0) ==
1225 !list_empty(&rbp->rbp_bufs));
1227 /* If routing is now turned off, we just drop this buffer and
1228 * don't bother trying to return credits. */
1229 if (!the_lnet.ln_routing) {
1230 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1234 /* It is possible that a user has lowered the desired number of
1235 * buffers in this pool. Make sure we never put back
1236 * more buffers than the stated number. */
1237 if (unlikely(rbp->rbp_credits >= rbp->rbp_req_nbuffers)) {
1238 /* Discard this buffer so we don't have too
1240 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1241 rbp->rbp_nbuffers--;
1243 list_add(&rb->rb_list, &rbp->rbp_bufs);
1245 if (rbp->rbp_credits <= 0)
1246 lnet_schedule_blocked_locked(rbp);
1251 if (msg->msg_peerrtrcredit) {
1253 LASSERT(rxpeerni->lpni_peer_net);
1254 LASSERT(rxpeerni->lpni_peer_net->lpn_peer);
1256 /* give back peer router credits */
1257 msg->msg_peerrtrcredit = 0;
1259 spin_lock(&rxpeerni->lpni_lock);
1260 rxpeerni->lpni_rtrcredits++;
1261 spin_unlock(&rxpeerni->lpni_lock);
1263 lp = rxpeerni->lpni_peer_net->lpn_peer;
1264 spin_lock(&lp->lp_lock);
1266 /* drop all messages which are queued to be routed on that
1268 if (!the_lnet.ln_routing) {
1270 list_splice_init(&lp->lp_rtrq, &drop);
1271 spin_unlock(&lp->lp_lock);
1272 lnet_drop_routed_msgs_locked(&drop, msg->msg_rx_cpt);
1273 } else if (!list_empty(&lp->lp_rtrq)) {
1276 msg2 = list_entry(lp->lp_rtrq.next,
1277 struct lnet_msg, msg_list);
1278 list_del(&msg2->msg_list);
1279 msg2_cpt = msg2->msg_rx_cpt;
1280 spin_unlock(&lp->lp_lock);
1282 * messages on the lp_rtrq can be from any NID in
1283 * the peer, which means they might have different
1284 * cpts. We need to make sure we lock the right
1287 if (msg2_cpt != msg->msg_rx_cpt) {
1288 lnet_net_unlock(msg->msg_rx_cpt);
1289 lnet_net_lock(msg2_cpt);
1291 (void) lnet_post_routed_recv_locked(msg2, 1);
1292 if (msg2_cpt != msg->msg_rx_cpt) {
1293 lnet_net_unlock(msg2_cpt);
1294 lnet_net_lock(msg->msg_rx_cpt);
1297 spin_unlock(&lp->lp_lock);
1301 msg->msg_rxni = NULL;
1302 lnet_ni_decref_locked(rxni, msg->msg_rx_cpt);
1304 if (rxpeerni != NULL) {
1305 msg->msg_rxpeer = NULL;
1306 lnet_peer_ni_decref_locked(rxpeerni);
1311 lnet_compare_gw_lpnis(struct lnet_peer_ni *p1, struct lnet_peer_ni *p2)
1313 if (p1->lpni_txqnob < p2->lpni_txqnob)
1316 if (p1->lpni_txqnob > p2->lpni_txqnob)
1319 if (p1->lpni_txcredits > p2->lpni_txcredits)
1322 if (p1->lpni_txcredits < p2->lpni_txcredits)
1328 static struct lnet_peer_ni *
1329 lnet_select_peer_ni(struct lnet_ni *best_ni, lnet_nid_t dst_nid,
1330 struct lnet_peer *peer,
1331 struct lnet_peer_ni *best_lpni,
1332 struct lnet_peer_net *peer_net)
1335 * Look at the peer NIs for the destination peer that connect
1336 * to the chosen net. If a peer_ni is preferred when using the
1337 * best_ni to communicate, we use that one. If there is no
1338 * preferred peer_ni, or there are multiple preferred peer_ni,
1339 * the available transmit credits are used. If the transmit
1340 * credits are equal, we round-robin over the peer_ni.
1342 struct lnet_peer_ni *lpni = NULL;
1343 int best_lpni_credits = (best_lpni) ? best_lpni->lpni_txcredits :
1345 int best_lpni_healthv = (best_lpni) ?
1346 atomic_read(&best_lpni->lpni_healthv) : 0;
1347 bool preferred = false;
1351 while ((lpni = lnet_get_next_peer_ni_locked(peer, peer_net, lpni))) {
1353 * if the best_ni we've chosen aleady has this lpni
1354 * preferred, then let's use it
1357 ni_is_pref = lnet_peer_is_pref_nid_locked(lpni,
1359 CDEBUG(D_NET, "%s ni_is_pref = %d\n",
1360 libcfs_nid2str(best_ni->ni_nid), ni_is_pref);
1365 lpni_healthv = atomic_read(&lpni->lpni_healthv);
1368 CDEBUG(D_NET, "%s c:[%d, %d], s:[%d, %d]\n",
1369 libcfs_nid2str(lpni->lpni_nid),
1370 lpni->lpni_txcredits, best_lpni_credits,
1371 lpni->lpni_seq, best_lpni->lpni_seq);
1373 /* pick the healthiest peer ni */
1374 if (lpni_healthv < best_lpni_healthv) {
1376 } else if (lpni_healthv > best_lpni_healthv) {
1377 best_lpni_healthv = lpni_healthv;
1378 /* if this is a preferred peer use it */
1379 } else if (!preferred && ni_is_pref) {
1381 } else if (preferred && !ni_is_pref) {
1383 * this is not the preferred peer so let's ignore
1387 } else if (lpni->lpni_txcredits < best_lpni_credits) {
1389 * We already have a peer that has more credits
1390 * available than this one. No need to consider
1391 * this peer further.
1394 } else if (lpni->lpni_txcredits == best_lpni_credits) {
1396 * The best peer found so far and the current peer
1397 * have the same number of available credits let's
1398 * make sure to select between them using Round
1402 if (best_lpni->lpni_seq <= lpni->lpni_seq)
1408 best_lpni_credits = lpni->lpni_txcredits;
1411 /* if we still can't find a peer ni then we can't reach it */
1413 __u32 net_id = (peer_net) ? peer_net->lpn_net_id :
1414 LNET_NIDNET(dst_nid);
1415 CDEBUG(D_NET, "no peer_ni found on peer net %s\n",
1416 libcfs_net2str(net_id));
1420 CDEBUG(D_NET, "sd_best_lpni = %s\n",
1421 libcfs_nid2str(best_lpni->lpni_nid));
1427 * Prerequisite: the best_ni should already be set in the sd
1428 * Find the best lpni.
1429 * If the net id is provided then restrict lpni selection on
1430 * that particular net.
1431 * Otherwise find any reachable lpni. When dealing with an MR
1432 * gateway and it has multiple lpnis which we can use
1433 * we want to select the best one from the list of reachable
1436 static inline struct lnet_peer_ni *
1437 lnet_find_best_lpni(struct lnet_ni *lni, lnet_nid_t dst_nid,
1438 struct lnet_peer *peer, __u32 net_id)
1440 struct lnet_peer_net *peer_net;
1441 __u32 any_net = LNET_NIDNET(LNET_NID_ANY);
1443 /* find the best_lpni on any local network */
1444 if (net_id == any_net) {
1445 struct lnet_peer_ni *best_lpni = NULL;
1446 struct lnet_peer_net *lpn;
1447 list_for_each_entry(lpn, &peer->lp_peer_nets, lpn_peer_nets) {
1448 /* no net specified find any reachable peer ni */
1449 if (!lnet_islocalnet_locked(lpn->lpn_net_id))
1451 best_lpni = lnet_select_peer_ni(lni, dst_nid, peer,
1457 /* restrict on the specified net */
1458 peer_net = lnet_peer_get_net_locked(peer, net_id);
1460 return lnet_select_peer_ni(lni, dst_nid, peer, NULL, peer_net);
1465 /* Compare route priorities and hop counts */
1467 lnet_compare_routes(struct lnet_route *r1, struct lnet_route *r2)
1469 int r1_hops = (r1->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r1->lr_hops;
1470 int r2_hops = (r2->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r2->lr_hops;
1472 if (r1->lr_priority < r2->lr_priority)
1475 if (r1->lr_priority > r2->lr_priority)
1478 if (r1_hops < r2_hops)
1481 if (r1_hops > r2_hops)
1487 static struct lnet_route *
1488 lnet_find_route_locked(struct lnet_remotenet *rnet, __u32 src_net,
1489 struct lnet_route **prev_route,
1490 struct lnet_peer_ni **gwni)
1492 struct lnet_peer_ni *lpni, *best_gw_ni = NULL;
1493 struct lnet_route *best_route;
1494 struct lnet_route *last_route;
1495 struct lnet_route *route;
1498 CDEBUG(D_NET, "Looking up a route to %s, from %s\n",
1499 libcfs_net2str(rnet->lrn_net), libcfs_net2str(src_net));
1501 best_route = last_route = NULL;
1502 list_for_each_entry(route, &rnet->lrn_routes, lr_list) {
1503 if (!lnet_is_route_alive(route))
1507 * Restrict the selection of the router NI on the src_net
1508 * provided. If the src_net is LNET_NID_ANY, then select
1509 * the best interface available.
1512 lpni = lnet_find_best_lpni(NULL, LNET_NID_ANY,
1516 best_route = last_route = route;
1519 CDEBUG(D_NET, "Gateway %s does not have a peer NI on net %s\n",
1520 libcfs_nid2str(route->lr_gateway->lp_primary_nid),
1521 libcfs_net2str(src_net));
1527 /* no protection on below fields, but it's harmless */
1528 if (last_route->lr_seq - route->lr_seq < 0)
1531 rc = lnet_compare_routes(route, best_route);
1535 lpni = lnet_find_best_lpni(NULL, LNET_NID_ANY,
1538 /* restrict the lpni on the src_net if specified */
1540 CDEBUG(D_NET, "Gateway %s does not have a peer NI on net %s\n",
1541 libcfs_nid2str(route->lr_gateway->lp_primary_nid),
1542 libcfs_net2str(src_net));
1552 rc = lnet_compare_gw_lpnis(lpni, best_gw_ni);
1556 if (rc == 1 || route->lr_seq <= best_route->lr_seq) {
1563 *prev_route = last_route;
1569 static struct lnet_ni *
1570 lnet_get_best_ni(struct lnet_net *local_net, struct lnet_ni *best_ni,
1571 struct lnet_peer *peer, struct lnet_peer_net *peer_net,
1574 struct lnet_ni *ni = NULL;
1575 unsigned int shortest_distance;
1580 * If there is no peer_ni that we can send to on this network,
1581 * then there is no point in looking for a new best_ni here.
1583 if (!lnet_get_next_peer_ni_locked(peer, peer_net, NULL))
1586 if (best_ni == NULL) {
1587 shortest_distance = UINT_MAX;
1588 best_credits = INT_MIN;
1591 shortest_distance = cfs_cpt_distance(lnet_cpt_table(), md_cpt,
1592 best_ni->ni_dev_cpt);
1593 best_credits = atomic_read(&best_ni->ni_tx_credits);
1594 best_healthv = atomic_read(&best_ni->ni_healthv);
1597 while ((ni = lnet_get_next_ni_locked(local_net, ni))) {
1598 unsigned int distance;
1603 ni_credits = atomic_read(&ni->ni_tx_credits);
1604 ni_healthv = atomic_read(&ni->ni_healthv);
1605 ni_fatal = atomic_read(&ni->ni_fatal_error_on);
1608 * calculate the distance from the CPT on which
1609 * the message memory is allocated to the CPT of
1610 * the NI's physical device
1612 distance = cfs_cpt_distance(lnet_cpt_table(),
1616 CDEBUG(D_NET, "compare ni %s [c:%d, d:%d, s:%d] with best_ni %s [c:%d, d:%d, s:%d]\n",
1617 libcfs_nid2str(ni->ni_nid), ni_credits, distance,
1618 ni->ni_seq, (best_ni) ? libcfs_nid2str(best_ni->ni_nid)
1619 : "not seleced", best_credits, shortest_distance,
1620 (best_ni) ? best_ni->ni_seq : 0);
1623 * All distances smaller than the NUMA range
1624 * are treated equally.
1626 if (distance < lnet_numa_range)
1627 distance = lnet_numa_range;
1630 * Select on health, shorter distance, available
1631 * credits, then round-robin.
1635 } else if (ni_healthv < best_healthv) {
1637 } else if (ni_healthv > best_healthv) {
1638 best_healthv = ni_healthv;
1640 * If we're going to prefer this ni because it's
1641 * the healthiest, then we should set the
1642 * shortest_distance in the algorithm in case
1643 * there are multiple NIs with the same health but
1644 * different distances.
1646 if (distance < shortest_distance)
1647 shortest_distance = distance;
1648 } else if (distance > shortest_distance) {
1650 } else if (distance < shortest_distance) {
1651 shortest_distance = distance;
1652 } else if (ni_credits < best_credits) {
1654 } else if (ni_credits == best_credits) {
1655 if (best_ni && best_ni->ni_seq <= ni->ni_seq)
1659 best_credits = ni_credits;
1662 CDEBUG(D_NET, "selected best_ni %s\n",
1663 (best_ni) ? libcfs_nid2str(best_ni->ni_nid) : "no selection");
1669 * Traffic to the LNET_RESERVED_PORTAL may not trigger peer discovery,
1670 * because such traffic is required to perform discovery. We therefore
1671 * exclude all GET and PUT on that portal. We also exclude all ACK and
1672 * REPLY traffic, but that is because the portal is not tracked in the
1673 * message structure for these message types. We could restrict this
1674 * further by also checking for LNET_PROTO_PING_MATCHBITS.
1677 lnet_msg_discovery(struct lnet_msg *msg)
1679 if (msg->msg_type == LNET_MSG_PUT) {
1680 if (msg->msg_hdr.msg.put.ptl_index != LNET_RESERVED_PORTAL)
1682 } else if (msg->msg_type == LNET_MSG_GET) {
1683 if (msg->msg_hdr.msg.get.ptl_index != LNET_RESERVED_PORTAL)
1689 #define SRC_SPEC 0x0001
1690 #define SRC_ANY 0x0002
1691 #define LOCAL_DST 0x0004
1692 #define REMOTE_DST 0x0008
1693 #define MR_DST 0x0010
1694 #define NMR_DST 0x0020
1695 #define SND_RESP 0x0040
1697 /* The following to defines are used for return codes */
1698 #define REPEAT_SEND 0x1000
1699 #define PASS_THROUGH 0x2000
1701 /* The different cases lnet_select pathway needs to handle */
1702 #define SRC_SPEC_LOCAL_MR_DST (SRC_SPEC | LOCAL_DST | MR_DST)
1703 #define SRC_SPEC_ROUTER_MR_DST (SRC_SPEC | REMOTE_DST | MR_DST)
1704 #define SRC_SPEC_LOCAL_NMR_DST (SRC_SPEC | LOCAL_DST | NMR_DST)
1705 #define SRC_SPEC_ROUTER_NMR_DST (SRC_SPEC | REMOTE_DST | NMR_DST)
1706 #define SRC_ANY_LOCAL_MR_DST (SRC_ANY | LOCAL_DST | MR_DST)
1707 #define SRC_ANY_ROUTER_MR_DST (SRC_ANY | REMOTE_DST | MR_DST)
1708 #define SRC_ANY_LOCAL_NMR_DST (SRC_ANY | LOCAL_DST | NMR_DST)
1709 #define SRC_ANY_ROUTER_NMR_DST (SRC_ANY | REMOTE_DST | NMR_DST)
1712 lnet_handle_lo_send(struct lnet_send_data *sd)
1714 struct lnet_msg *msg = sd->sd_msg;
1715 int cpt = sd->sd_cpt;
1717 /* No send credit hassles with LOLND */
1718 lnet_ni_addref_locked(the_lnet.ln_loni, cpt);
1719 msg->msg_hdr.dest_nid = cpu_to_le64(the_lnet.ln_loni->ni_nid);
1720 if (!msg->msg_routing)
1721 msg->msg_hdr.src_nid =
1722 cpu_to_le64(the_lnet.ln_loni->ni_nid);
1723 msg->msg_target.nid = the_lnet.ln_loni->ni_nid;
1724 lnet_msg_commit(msg, cpt);
1725 msg->msg_txni = the_lnet.ln_loni;
1727 return LNET_CREDIT_OK;
1731 lnet_handle_send(struct lnet_send_data *sd)
1733 struct lnet_ni *best_ni = sd->sd_best_ni;
1734 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
1735 struct lnet_peer_ni *final_dst_lpni = sd->sd_final_dst_lpni;
1736 struct lnet_msg *msg = sd->sd_msg;
1738 __u32 send_case = sd->sd_send_case;
1740 __u32 routing = send_case & REMOTE_DST;
1741 struct lnet_rsp_tracker *rspt;
1744 * Increment sequence number of the selected peer so that we
1745 * pick the next one in Round Robin.
1747 best_lpni->lpni_seq++;
1750 * grab a reference on the peer_ni so it sticks around even if
1751 * we need to drop and relock the lnet_net_lock below.
1753 lnet_peer_ni_addref_locked(best_lpni);
1756 * Use lnet_cpt_of_nid() to determine the CPT used to commit the
1757 * message. This ensures that we get a CPT that is correct for
1758 * the NI when the NI has been restricted to a subset of all CPTs.
1759 * If the selected CPT differs from the one currently locked, we
1760 * must unlock and relock the lnet_net_lock(), and then check whether
1761 * the configuration has changed. We don't have a hold on the best_ni
1762 * yet, and it may have vanished.
1764 cpt2 = lnet_cpt_of_nid_locked(best_lpni->lpni_nid, best_ni);
1765 if (sd->sd_cpt != cpt2) {
1766 __u32 seq = lnet_get_dlc_seq_locked();
1767 lnet_net_unlock(sd->sd_cpt);
1769 lnet_net_lock(sd->sd_cpt);
1770 if (seq != lnet_get_dlc_seq_locked()) {
1771 lnet_peer_ni_decref_locked(best_lpni);
1777 * store the best_lpni in the message right away to avoid having
1778 * to do the same operation under different conditions
1780 msg->msg_txpeer = best_lpni;
1781 msg->msg_txni = best_ni;
1784 * grab a reference for the best_ni since now it's in use in this
1785 * send. The reference will be dropped in lnet_finalize()
1787 lnet_ni_addref_locked(msg->msg_txni, sd->sd_cpt);
1790 * Always set the target.nid to the best peer picked. Either the
1791 * NID will be one of the peer NIDs selected, or the same NID as
1792 * what was originally set in the target or it will be the NID of
1793 * a router if this message should be routed
1795 msg->msg_target.nid = msg->msg_txpeer->lpni_nid;
1798 * lnet_msg_commit assigns the correct cpt to the message, which
1799 * is used to decrement the correct refcount on the ni when it's
1800 * time to return the credits
1802 lnet_msg_commit(msg, sd->sd_cpt);
1805 * If we are routing the message then we keep the src_nid that was
1806 * set by the originator. If we are not routing then we are the
1807 * originator and set it here.
1809 if (!msg->msg_routing)
1810 msg->msg_hdr.src_nid = cpu_to_le64(msg->msg_txni->ni_nid);
1813 msg->msg_target_is_router = 1;
1814 msg->msg_target.pid = LNET_PID_LUSTRE;
1816 * since we're routing we want to ensure that the
1817 * msg_hdr.dest_nid is set to the final destination. When
1818 * the router receives this message it knows how to route
1821 * final_dst_lpni is set at the beginning of the
1822 * lnet_select_pathway() function and is never changed.
1823 * It's safe to use it here.
1825 msg->msg_hdr.dest_nid = cpu_to_le64(final_dst_lpni->lpni_nid);
1828 * if we're not routing set the dest_nid to the best peer
1829 * ni NID that we picked earlier in the algorithm.
1831 msg->msg_hdr.dest_nid = cpu_to_le64(msg->msg_txpeer->lpni_nid);
1835 * if we have response tracker block update it with the next hop
1839 rspt = msg->msg_md->md_rspt_ptr;
1841 rspt->rspt_next_hop_nid = msg->msg_txpeer->lpni_nid;
1842 CDEBUG(D_NET, "rspt_next_hop_nid = %s\n",
1843 libcfs_nid2str(rspt->rspt_next_hop_nid));
1847 rc = lnet_post_send_locked(msg, 0);
1850 CDEBUG(D_NET, "TRACE: %s(%s:%s) -> %s(%s:%s) %s : %s try# %d\n",
1851 libcfs_nid2str(msg->msg_hdr.src_nid),
1852 libcfs_nid2str(msg->msg_txni->ni_nid),
1853 libcfs_nid2str(sd->sd_src_nid),
1854 libcfs_nid2str(msg->msg_hdr.dest_nid),
1855 libcfs_nid2str(sd->sd_dst_nid),
1856 libcfs_nid2str(msg->msg_txpeer->lpni_nid),
1857 libcfs_nid2str(sd->sd_rtr_nid),
1858 lnet_msgtyp2str(msg->msg_type), msg->msg_retry_count);
1864 lnet_set_non_mr_pref_nid(struct lnet_peer_ni *lpni, struct lnet_ni *lni,
1865 struct lnet_msg *msg)
1867 if (!lnet_msg_is_response(msg) && lpni->lpni_pref_nnids == 0) {
1868 CDEBUG(D_NET, "Setting preferred local NID %s on NMR peer %s\n",
1869 libcfs_nid2str(lni->ni_nid),
1870 libcfs_nid2str(lpni->lpni_nid));
1871 lnet_peer_ni_set_non_mr_pref_nid(lpni, lni->ni_nid);
1880 * use the source and destination NIDs as the pathway
1883 lnet_handle_spec_local_nmr_dst(struct lnet_send_data *sd)
1885 /* the destination lpni is set before we get here. */
1888 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
1889 if (!sd->sd_best_ni) {
1890 CERROR("Can't send to %s: src %s is not a "
1891 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
1892 libcfs_nid2str(sd->sd_src_nid));
1896 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
1898 return lnet_handle_send(sd);
1906 * Don't run the selection algorithm on the peer NIs. By specifying the
1907 * local NID, we're also saying that we should always use the destination NID
1908 * provided. This handles the case where we should be using the same
1909 * destination NID for the all the messages which belong to the same RPC
1913 lnet_handle_spec_local_mr_dst(struct lnet_send_data *sd)
1915 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
1916 if (!sd->sd_best_ni) {
1917 CERROR("Can't send to %s: src %s is not a "
1918 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
1919 libcfs_nid2str(sd->sd_src_nid));
1923 if (sd->sd_best_lpni &&
1924 sd->sd_best_lpni->lpni_nid == the_lnet.ln_loni->ni_nid)
1925 return lnet_handle_lo_send(sd);
1926 else if (sd->sd_best_lpni)
1927 return lnet_handle_send(sd);
1929 CERROR("can't send to %s. no NI on %s\n",
1930 libcfs_nid2str(sd->sd_dst_nid),
1931 libcfs_net2str(sd->sd_best_ni->ni_net->net_id));
1933 return -EHOSTUNREACH;
1937 lnet_find_best_ni_on_spec_net(struct lnet_ni *cur_best_ni,
1938 struct lnet_peer *peer,
1939 struct lnet_peer_net *peer_net,
1943 struct lnet_net *local_net;
1944 struct lnet_ni *best_ni;
1946 local_net = lnet_get_net_locked(peer_net->lpn_net_id);
1951 * Iterate through the NIs in this local Net and select
1952 * the NI to send from. The selection is determined by
1953 * these 3 criterion in the following priority:
1955 * 2. NI available credits
1958 best_ni = lnet_get_best_ni(local_net, cur_best_ni,
1959 peer, peer_net, cpt);
1961 if (incr_seq && best_ni)
1968 lnet_initiate_peer_discovery(struct lnet_peer_ni *lpni, struct lnet_msg *msg,
1971 struct lnet_peer *peer;
1974 lnet_peer_ni_addref_locked(lpni);
1976 peer = lpni->lpni_peer_net->lpn_peer;
1978 if (lnet_peer_gw_discovery(peer)) {
1979 lnet_peer_ni_decref_locked(lpni);
1983 if (!lnet_msg_discovery(msg) || lnet_peer_is_uptodate(peer)) {
1984 lnet_peer_ni_decref_locked(lpni);
1988 rc = lnet_discover_peer_locked(lpni, cpt, false);
1990 lnet_peer_ni_decref_locked(lpni);
1993 /* The peer may have changed. */
1994 peer = lpni->lpni_peer_net->lpn_peer;
1995 spin_lock(&peer->lp_lock);
1996 if (lnet_peer_is_uptodate_locked(peer)) {
1997 spin_unlock(&peer->lp_lock);
1998 lnet_peer_ni_decref_locked(lpni);
2001 /* queue message and return */
2002 msg->msg_sending = 0;
2003 msg->msg_txpeer = NULL;
2004 list_add_tail(&msg->msg_list, &peer->lp_dc_pendq);
2005 spin_unlock(&peer->lp_lock);
2007 lnet_peer_ni_decref_locked(lpni);
2009 CDEBUG(D_NET, "msg %p delayed. %s pending discovery\n",
2010 msg, libcfs_nid2str(peer->lp_primary_nid));
2012 return LNET_DC_WAIT;
2016 lnet_handle_find_routed_path(struct lnet_send_data *sd,
2018 struct lnet_peer_ni **gw_lpni,
2019 struct lnet_peer **gw_peer)
2023 struct lnet_peer *gw;
2024 struct lnet_peer *lp;
2025 struct lnet_peer_net *lpn;
2026 struct lnet_peer_net *best_lpn = NULL;
2027 struct lnet_remotenet *rnet, *best_rnet = NULL;
2028 struct lnet_route *best_route = NULL;
2029 struct lnet_route *last_route = NULL;
2030 struct lnet_peer_ni *lpni = NULL;
2031 struct lnet_peer_ni *gwni = NULL;
2032 lnet_nid_t src_nid = (sd->sd_src_nid != LNET_NID_ANY) ? sd->sd_src_nid :
2033 (sd->sd_best_ni != NULL) ? sd->sd_best_ni->ni_nid :
2036 CDEBUG(D_NET, "using src nid %s for route restriction\n",
2037 libcfs_nid2str(src_nid));
2039 /* If a router nid was specified then we are replying to a GET or
2040 * sending an ACK. In this case we use the gateway associated with the
2041 * specified router nid.
2043 if (sd->sd_rtr_nid != LNET_NID_ANY) {
2044 gwni = lnet_find_peer_ni_locked(sd->sd_rtr_nid);
2046 CERROR("No peer NI for gateway %s\n",
2047 libcfs_nid2str(sd->sd_rtr_nid));
2048 return -EHOSTUNREACH;
2050 gw = gwni->lpni_peer_net->lpn_peer;
2051 lnet_peer_ni_decref_locked(gwni);
2052 local_lnet = LNET_NIDNET(sd->sd_rtr_nid);
2054 /* we've already looked up the initial lpni using dst_nid */
2055 lpni = sd->sd_best_lpni;
2056 /* the peer tree must be in existence */
2057 LASSERT(lpni && lpni->lpni_peer_net &&
2058 lpni->lpni_peer_net->lpn_peer);
2059 lp = lpni->lpni_peer_net->lpn_peer;
2061 list_for_each_entry(lpn, &lp->lp_peer_nets, lpn_peer_nets) {
2062 /* is this remote network reachable? */
2063 rnet = lnet_find_rnet_locked(lpn->lpn_net_id);
2072 if (best_lpn->lpn_seq <= lpn->lpn_seq)
2080 CERROR("peer %s has no available nets\n",
2081 libcfs_nid2str(sd->sd_dst_nid));
2082 return -EHOSTUNREACH;
2085 sd->sd_best_lpni = lnet_find_best_lpni(sd->sd_best_ni,
2088 best_lpn->lpn_net_id);
2089 if (!sd->sd_best_lpni) {
2090 CERROR("peer %s is unreachable\n",
2091 libcfs_nid2str(sd->sd_dst_nid));
2092 return -EHOSTUNREACH;
2096 * We're attempting to round robin over the remote peer
2097 * NI's so update the final destination we selected
2099 sd->sd_final_dst_lpni = sd->sd_best_lpni;
2102 * find the best route. Restrict the selection on the net of the
2103 * local NI if we've already picked the local NI to send from.
2104 * Otherwise, let's pick any route we can find and then find
2105 * a local NI we can reach the route's gateway on. Any route we select
2106 * will be reachable by virtue of the restriction we have when
2109 best_route = lnet_find_route_locked(best_rnet,
2110 LNET_NIDNET(src_nid),
2111 &last_route, &gwni);
2114 CERROR("no route to %s from %s\n",
2115 libcfs_nid2str(dst_nid),
2116 libcfs_nid2str(src_nid));
2117 return -EHOSTUNREACH;
2121 CERROR("Internal Error. Route expected to %s from %s\n",
2122 libcfs_nid2str(dst_nid),
2123 libcfs_nid2str(src_nid));
2127 gw = best_route->lr_gateway;
2128 LASSERT(gw == gwni->lpni_peer_net->lpn_peer);
2129 local_lnet = best_route->lr_lnet;
2132 * Increment the sequence number of the remote lpni so we
2133 * can round robin over the different interfaces of the
2136 sd->sd_best_lpni->lpni_seq++;
2140 * Discover this gateway if it hasn't already been discovered.
2141 * This means we might delay the message until discovery has
2144 sd->sd_msg->msg_src_nid_param = sd->sd_src_nid;
2145 rc = lnet_initiate_peer_discovery(gwni, sd->sd_msg, sd->sd_cpt);
2149 if (!sd->sd_best_ni)
2150 sd->sd_best_ni = lnet_find_best_ni_on_spec_net(NULL, gw,
2151 lnet_peer_get_net_locked(gw,
2156 if (!sd->sd_best_ni) {
2157 CERROR("Internal Error. Expected local ni on %s but non found :%s\n",
2158 libcfs_net2str(local_lnet),
2159 libcfs_nid2str(sd->sd_src_nid));
2167 * increment the sequence numbers since now we're sure we're
2168 * going to use this path
2170 if (sd->sd_rtr_nid == LNET_NID_ANY) {
2171 LASSERT(best_route && last_route);
2172 best_route->lr_seq = last_route->lr_seq + 1;
2173 best_lpn->lpn_seq++;
2184 * Remote destination
2185 * Non-MR destination
2189 * Remote destination
2192 * The handling of these two cases is similar. Even though the destination
2193 * can be MR or non-MR, we'll deal directly with the router.
2196 lnet_handle_spec_router_dst(struct lnet_send_data *sd)
2199 struct lnet_peer_ni *gw_lpni = NULL;
2200 struct lnet_peer *gw_peer = NULL;
2203 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
2204 if (!sd->sd_best_ni) {
2205 CERROR("Can't send to %s: src %s is not a "
2206 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
2207 libcfs_nid2str(sd->sd_src_nid));
2211 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2216 if (sd->sd_send_case & NMR_DST)
2218 * since the final destination is non-MR let's set its preferred
2219 * NID before we send
2221 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni,
2225 * We're going to send to the gw found so let's set its
2228 sd->sd_peer = gw_peer;
2229 sd->sd_best_lpni = gw_lpni;
2231 return lnet_handle_send(sd);
2235 lnet_find_best_ni_on_local_net(struct lnet_peer *peer, int md_cpt,
2238 struct lnet_peer_net *peer_net = NULL;
2239 struct lnet_ni *best_ni = NULL;
2240 int lpn_healthv = 0;
2243 * The peer can have multiple interfaces, some of them can be on
2244 * the local network and others on a routed network. We should
2245 * prefer the local network. However if the local network is not
2246 * available then we need to try the routed network
2249 /* go through all the peer nets and find the best_ni */
2250 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_peer_nets) {
2252 * The peer's list of nets can contain non-local nets. We
2253 * want to only examine the local ones.
2255 if (!lnet_get_net_locked(peer_net->lpn_net_id))
2258 /* always select the lpn with the best health */
2259 if (lpn_healthv <= peer_net->lpn_healthv)
2260 lpn_healthv = peer_net->lpn_healthv;
2264 best_ni = lnet_find_best_ni_on_spec_net(best_ni, peer, peer_net,
2268 * if this is a discovery message and lp_disc_net_id is
2269 * specified then use that net to send the discovery on.
2271 if (peer->lp_disc_net_id == peer_net->lpn_net_id &&
2277 /* increment sequence number so we can round robin */
2283 static struct lnet_ni *
2284 lnet_find_existing_preferred_best_ni(struct lnet_peer_ni *lpni, int cpt)
2286 struct lnet_ni *best_ni = NULL;
2287 struct lnet_peer_net *peer_net = lpni->lpni_peer_net;
2288 struct lnet_peer_ni *lpni_entry;
2291 * We must use a consistent source address when sending to a
2292 * non-MR peer. However, a non-MR peer can have multiple NIDs
2293 * on multiple networks, and we may even need to talk to this
2294 * peer on multiple networks -- certain types of
2295 * load-balancing configuration do this.
2297 * So we need to pick the NI the peer prefers for this
2298 * particular network.
2301 list_for_each_entry(lpni_entry, &peer_net->lpn_peer_nis,
2303 if (lpni_entry->lpni_pref_nnids == 0)
2305 LASSERT(lpni_entry->lpni_pref_nnids == 1);
2306 best_ni = lnet_nid2ni_locked(lpni_entry->lpni_pref.nid, cpt);
2313 /* Prerequisite: sd->sd_peer and sd->sd_best_lpni should be set */
2315 lnet_select_preferred_best_ni(struct lnet_send_data *sd)
2317 struct lnet_ni *best_ni = NULL;
2318 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
2321 * We must use a consistent source address when sending to a
2322 * non-MR peer. However, a non-MR peer can have multiple NIDs
2323 * on multiple networks, and we may even need to talk to this
2324 * peer on multiple networks -- certain types of
2325 * load-balancing configuration do this.
2327 * So we need to pick the NI the peer prefers for this
2328 * particular network.
2331 best_ni = lnet_find_existing_preferred_best_ni(sd->sd_best_lpni,
2334 /* if best_ni is still not set just pick one */
2337 lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2338 sd->sd_best_lpni->lpni_peer_net,
2339 sd->sd_md_cpt, true);
2340 /* If there is no best_ni we don't have a route */
2342 CERROR("no path to %s from net %s\n",
2343 libcfs_nid2str(best_lpni->lpni_nid),
2344 libcfs_net2str(best_lpni->lpni_net->net_id));
2345 return -EHOSTUNREACH;
2349 sd->sd_best_ni = best_ni;
2351 /* Set preferred NI if necessary. */
2352 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
2359 * Source not specified
2363 * always use the same source NID for NMR peers
2364 * If we've talked to that peer before then we already have a preferred
2365 * source NI associated with it. Otherwise, we select a preferred local NI
2366 * and store it in the peer
2369 lnet_handle_any_local_nmr_dst(struct lnet_send_data *sd)
2373 /* sd->sd_best_lpni is already set to the final destination */
2376 * At this point we should've created the peer ni and peer. If we
2377 * can't find it, then something went wrong. Instead of assert
2378 * output a relevant message and fail the send
2380 if (!sd->sd_best_lpni) {
2381 CERROR("Internal fault. Unable to send msg %s to %s. "
2383 lnet_msgtyp2str(sd->sd_msg->msg_type),
2384 libcfs_nid2str(sd->sd_dst_nid));
2388 rc = lnet_select_preferred_best_ni(sd);
2390 rc = lnet_handle_send(sd);
2396 lnet_handle_any_mr_dsta(struct lnet_send_data *sd)
2399 * NOTE we've already handled the remote peer case. So we only
2400 * need to worry about the local case here.
2402 * if we're sending a response, ACK or reply, we need to send it
2403 * to the destination NID given to us. At this point we already
2404 * have the peer_ni we're suppose to send to, so just find the
2405 * best_ni on the peer net and use that. Since we're sending to an
2406 * MR peer then we can just run the selection algorithm on our
2407 * local NIs and pick the best one.
2409 if (sd->sd_send_case & SND_RESP) {
2411 lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2412 sd->sd_best_lpni->lpni_peer_net,
2413 sd->sd_md_cpt, true);
2415 if (!sd->sd_best_ni) {
2417 * We're not going to deal with not able to send
2418 * a response to the provided final destination
2420 CERROR("Can't send response to %s. "
2421 "No local NI available\n",
2422 libcfs_nid2str(sd->sd_dst_nid));
2423 return -EHOSTUNREACH;
2426 return lnet_handle_send(sd);
2430 * If we get here that means we're sending a fresh request, PUT or
2431 * GET, so we need to run our standard selection algorithm.
2432 * First find the best local interface that's on any of the peer's
2435 sd->sd_best_ni = lnet_find_best_ni_on_local_net(sd->sd_peer,
2437 lnet_msg_discovery(sd->sd_msg));
2438 if (sd->sd_best_ni) {
2440 lnet_find_best_lpni(sd->sd_best_ni, sd->sd_dst_nid,
2442 sd->sd_best_ni->ni_net->net_id);
2445 * if we're successful in selecting a peer_ni on the local
2446 * network, then send to it. Otherwise fall through and
2447 * try and see if we can reach it over another routed
2450 if (sd->sd_best_lpni &&
2451 sd->sd_best_lpni->lpni_nid == the_lnet.ln_loni->ni_nid) {
2453 * in case we initially started with a routed
2454 * destination, let's reset to local
2456 sd->sd_send_case &= ~REMOTE_DST;
2457 sd->sd_send_case |= LOCAL_DST;
2458 return lnet_handle_lo_send(sd);
2459 } else if (sd->sd_best_lpni) {
2461 * in case we initially started with a routed
2462 * destination, let's reset to local
2464 sd->sd_send_case &= ~REMOTE_DST;
2465 sd->sd_send_case |= LOCAL_DST;
2466 return lnet_handle_send(sd);
2469 CERROR("Internal Error. Expected to have a best_lpni: "
2471 libcfs_nid2str(sd->sd_src_nid),
2472 libcfs_nid2str(sd->sd_dst_nid));
2478 * Peer doesn't have a local network. Let's see if there is
2479 * a remote network we can reach it on.
2481 return PASS_THROUGH;
2486 * Source NID not specified
2491 * Source NID not speified
2492 * Remote destination
2495 * In both of these cases if we're sending a response, ACK or REPLY, then
2496 * we need to send to the destination NID provided.
2498 * In the remote case let's deal with MR routers.
2503 lnet_handle_any_mr_dst(struct lnet_send_data *sd)
2506 struct lnet_peer *gw_peer = NULL;
2507 struct lnet_peer_ni *gw_lpni = NULL;
2510 * handle sending a response to a remote peer here so we don't
2511 * have to worry about it if we hit lnet_handle_any_mr_dsta()
2513 if (sd->sd_send_case & REMOTE_DST &&
2514 sd->sd_send_case & SND_RESP) {
2515 struct lnet_peer_ni *gw;
2516 struct lnet_peer *gw_peer;
2518 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw,
2521 CERROR("Can't send response to %s. "
2522 "No route available\n",
2523 libcfs_nid2str(sd->sd_dst_nid));
2524 return -EHOSTUNREACH;
2525 } else if (rc > 0) {
2529 sd->sd_best_lpni = gw;
2530 sd->sd_peer = gw_peer;
2532 return lnet_handle_send(sd);
2536 * Even though the NID for the peer might not be on a local network,
2537 * since the peer is MR there could be other interfaces on the
2538 * local network. In that case we'd still like to prefer the local
2539 * network over the routed network. If we're unable to do that
2540 * then we select the best router among the different routed networks,
2541 * and if the router is MR then we can deal with it as such.
2543 rc = lnet_handle_any_mr_dsta(sd);
2544 if (rc != PASS_THROUGH)
2548 * Now that we must route to the destination, we must consider the
2549 * MR case, where the destination has multiple interfaces, some of
2550 * which we can route to and others we do not. For this reason we
2551 * need to select the destination which we can route to and if
2552 * there are multiple, we need to round robin.
2554 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2559 sd->sd_send_case &= ~LOCAL_DST;
2560 sd->sd_send_case |= REMOTE_DST;
2562 sd->sd_peer = gw_peer;
2563 sd->sd_best_lpni = gw_lpni;
2565 return lnet_handle_send(sd);
2569 * Source not specified
2570 * Remote destination
2573 * Must send to the specified peer NID using the same source NID that
2574 * we've used before. If it's the first time to talk to that peer then
2575 * find the source NI and assign it as preferred to that peer
2578 lnet_handle_any_router_nmr_dst(struct lnet_send_data *sd)
2581 struct lnet_peer_ni *gw_lpni = NULL;
2582 struct lnet_peer *gw_peer = NULL;
2585 * Let's see if we have a preferred NI to talk to this NMR peer
2587 sd->sd_best_ni = lnet_find_existing_preferred_best_ni(sd->sd_best_lpni,
2591 * find the router and that'll find the best NI if we didn't find
2594 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2600 * set the best_ni we've chosen as the preferred one for
2603 lnet_set_non_mr_pref_nid(sd->sd_best_lpni, sd->sd_best_ni, sd->sd_msg);
2605 /* we'll be sending to the gw */
2606 sd->sd_best_lpni = gw_lpni;
2607 sd->sd_peer = gw_peer;
2609 return lnet_handle_send(sd);
2613 lnet_handle_send_case_locked(struct lnet_send_data *sd)
2616 * turn off the SND_RESP bit.
2617 * It will be checked in the case handling
2619 __u32 send_case = sd->sd_send_case &= ~SND_RESP ;
2621 CDEBUG(D_NET, "Source %s%s to %s %s %s destination\n",
2622 (send_case & SRC_SPEC) ? "Specified: " : "ANY",
2623 (send_case & SRC_SPEC) ? libcfs_nid2str(sd->sd_src_nid) : "",
2624 (send_case & MR_DST) ? "MR: " : "NMR: ",
2625 libcfs_nid2str(sd->sd_dst_nid),
2626 (send_case & LOCAL_DST) ? "local" : "routed");
2628 switch (send_case) {
2630 * For all cases where the source is specified, we should always
2631 * use the destination NID, whether it's an MR destination or not,
2632 * since we're continuing a series of related messages for the
2635 case SRC_SPEC_LOCAL_NMR_DST:
2636 return lnet_handle_spec_local_nmr_dst(sd);
2637 case SRC_SPEC_LOCAL_MR_DST:
2638 return lnet_handle_spec_local_mr_dst(sd);
2639 case SRC_SPEC_ROUTER_NMR_DST:
2640 case SRC_SPEC_ROUTER_MR_DST:
2641 return lnet_handle_spec_router_dst(sd);
2642 case SRC_ANY_LOCAL_NMR_DST:
2643 return lnet_handle_any_local_nmr_dst(sd);
2644 case SRC_ANY_LOCAL_MR_DST:
2645 case SRC_ANY_ROUTER_MR_DST:
2646 return lnet_handle_any_mr_dst(sd);
2647 case SRC_ANY_ROUTER_NMR_DST:
2648 return lnet_handle_any_router_nmr_dst(sd);
2650 CERROR("Unknown send case\n");
2656 lnet_select_pathway(lnet_nid_t src_nid, lnet_nid_t dst_nid,
2657 struct lnet_msg *msg, lnet_nid_t rtr_nid)
2659 struct lnet_peer_ni *lpni;
2660 struct lnet_peer *peer;
2661 struct lnet_send_data send_data;
2664 __u32 send_case = 0;
2666 memset(&send_data, 0, sizeof(send_data));
2669 * get an initial CPT to use for locking. The idea here is not to
2670 * serialize the calls to select_pathway, so that as many
2671 * operations can run concurrently as possible. To do that we use
2672 * the CPT where this call is being executed. Later on when we
2673 * determine the CPT to use in lnet_message_commit, we switch the
2674 * lock and check if there was any configuration change. If none,
2675 * then we proceed, if there is, then we restart the operation.
2677 cpt = lnet_net_lock_current();
2679 md_cpt = lnet_cpt_of_md(msg->msg_md, msg->msg_offset);
2680 if (md_cpt == CFS_CPT_ANY)
2686 * If we're being asked to send to the loopback interface, there
2687 * is no need to go through any selection. We can just shortcut
2688 * the entire process and send over lolnd
2690 send_data.sd_msg = msg;
2691 send_data.sd_cpt = cpt;
2692 if (dst_nid == LNET_NID_LO_0) {
2693 rc = lnet_handle_lo_send(&send_data);
2694 lnet_net_unlock(cpt);
2699 * find an existing peer_ni, or create one and mark it as having been
2700 * created due to network traffic. This call will create the
2701 * peer->peer_net->peer_ni tree.
2703 lpni = lnet_nid2peerni_locked(dst_nid, LNET_NID_ANY, cpt);
2705 lnet_net_unlock(cpt);
2706 return PTR_ERR(lpni);
2710 * Cache the original src_nid and rtr_nid. If we need to resend the
2711 * message then we'll need to know whether the src_nid was originally
2712 * specified for this message. If it was originally specified,
2713 * then we need to keep using the same src_nid since it's
2714 * continuing the same sequence of messages. Similarly, rtr_nid will
2715 * affect our choice of next hop.
2717 msg->msg_src_nid_param = src_nid;
2718 msg->msg_rtr_nid_param = rtr_nid;
2721 * If necessary, perform discovery on the peer that owns this peer_ni.
2722 * Note, this can result in the ownership of this peer_ni changing
2723 * to another peer object.
2725 rc = lnet_initiate_peer_discovery(lpni, msg, cpt);
2727 lnet_peer_ni_decref_locked(lpni);
2728 lnet_net_unlock(cpt);
2731 lnet_peer_ni_decref_locked(lpni);
2733 peer = lpni->lpni_peer_net->lpn_peer;
2736 * Identify the different send cases
2738 if (src_nid == LNET_NID_ANY)
2739 send_case |= SRC_ANY;
2741 send_case |= SRC_SPEC;
2743 if (lnet_get_net_locked(LNET_NIDNET(dst_nid)))
2744 send_case |= LOCAL_DST;
2746 send_case |= REMOTE_DST;
2749 * Deal with the peer as NMR in the following cases:
2750 * 1. the peer is NMR
2751 * 2. We're trying to recover a specific peer NI
2752 * 3. I'm a router sending to the final destination
2753 * In this case the source of the message would've
2754 * already selected the final destination so my job
2755 * is to honor the selection.
2757 if (!lnet_peer_is_multi_rail(peer) || msg->msg_recovery ||
2758 (msg->msg_routing && (send_case & LOCAL_DST)))
2759 send_case |= NMR_DST;
2761 send_case |= MR_DST;
2763 if (lnet_msg_is_response(msg))
2764 send_case |= SND_RESP;
2766 /* assign parameters to the send_data */
2767 send_data.sd_rtr_nid = rtr_nid;
2768 send_data.sd_src_nid = src_nid;
2769 send_data.sd_dst_nid = dst_nid;
2770 send_data.sd_best_lpni = lpni;
2772 * keep a pointer to the final destination in case we're going to
2773 * route, so we'll need to access it later
2775 send_data.sd_final_dst_lpni = lpni;
2776 send_data.sd_peer = peer;
2777 send_data.sd_md_cpt = md_cpt;
2778 send_data.sd_send_case = send_case;
2780 rc = lnet_handle_send_case_locked(&send_data);
2783 * Update the local cpt since send_data.sd_cpt might've been
2784 * updated as a result of calling lnet_handle_send_case_locked().
2786 cpt = send_data.sd_cpt;
2788 if (rc == REPEAT_SEND)
2791 lnet_net_unlock(cpt);
2797 lnet_send(lnet_nid_t src_nid, struct lnet_msg *msg, lnet_nid_t rtr_nid)
2799 lnet_nid_t dst_nid = msg->msg_target.nid;
2803 * NB: rtr_nid is set to LNET_NID_ANY for all current use-cases,
2804 * but we might want to use pre-determined router for ACK/REPLY
2807 /* NB: ni != NULL == interface pre-determined (ACK/REPLY) */
2808 LASSERT(msg->msg_txpeer == NULL);
2809 LASSERT(msg->msg_txni == NULL);
2810 LASSERT(!msg->msg_sending);
2811 LASSERT(!msg->msg_target_is_router);
2812 LASSERT(!msg->msg_receiving);
2814 msg->msg_sending = 1;
2816 LASSERT(!msg->msg_tx_committed);
2818 rc = lnet_select_pathway(src_nid, dst_nid, msg, rtr_nid);
2820 if (rc == -EHOSTUNREACH)
2821 msg->msg_health_status = LNET_MSG_STATUS_REMOTE_ERROR;
2823 msg->msg_health_status = LNET_MSG_STATUS_LOCAL_ERROR;
2827 if (rc == LNET_CREDIT_OK)
2828 lnet_ni_send(msg->msg_txni, msg);
2830 /* rc == LNET_CREDIT_OK or LNET_CREDIT_WAIT or LNET_DC_WAIT */
2834 enum lnet_mt_event_type {
2835 MT_TYPE_LOCAL_NI = 0,
2839 struct lnet_mt_event_info {
2840 enum lnet_mt_event_type mt_type;
2844 /* called with res_lock held */
2846 lnet_detach_rsp_tracker(struct lnet_libmd *md, int cpt)
2848 struct lnet_rsp_tracker *rspt;
2851 * msg has a refcount on the MD so the MD is not going away.
2852 * The rspt queue for the cpt is protected by
2853 * the lnet_net_lock(cpt). cpt is the cpt of the MD cookie.
2855 if (!md->md_rspt_ptr)
2858 rspt = md->md_rspt_ptr;
2861 LASSERT(rspt->rspt_cpt == cpt);
2863 md->md_rspt_ptr = NULL;
2865 if (LNetMDHandleIsInvalid(rspt->rspt_mdh)) {
2867 * The monitor thread has invalidated this handle because the
2868 * response timed out, but it failed to lookup the MD. That
2869 * means this response tracker is on the zombie list. We can
2870 * safely remove it under the resource lock (held by caller) and
2871 * free the response tracker block.
2873 list_del(&rspt->rspt_on_list);
2874 lnet_rspt_free(rspt, cpt);
2877 * invalidate the handle to indicate that a response has been
2878 * received, which will then lead the monitor thread to clean up
2881 LNetInvalidateMDHandle(&rspt->rspt_mdh);
2886 lnet_clean_zombie_rstqs(void)
2888 struct lnet_rsp_tracker *rspt, *tmp;
2891 cfs_cpt_for_each(i, lnet_cpt_table()) {
2892 list_for_each_entry_safe(rspt, tmp,
2893 the_lnet.ln_mt_zombie_rstqs[i],
2895 list_del(&rspt->rspt_on_list);
2896 lnet_rspt_free(rspt, i);
2900 cfs_percpt_free(the_lnet.ln_mt_zombie_rstqs);
2904 lnet_finalize_expired_responses(void)
2906 struct lnet_libmd *md;
2907 struct lnet_rsp_tracker *rspt, *tmp;
2911 if (the_lnet.ln_mt_rstq == NULL)
2914 cfs_cpt_for_each(i, lnet_cpt_table()) {
2915 LIST_HEAD(local_queue);
2918 if (!the_lnet.ln_mt_rstq[i]) {
2922 list_splice_init(the_lnet.ln_mt_rstq[i], &local_queue);
2927 list_for_each_entry_safe(rspt, tmp, &local_queue, rspt_on_list) {
2929 * The rspt mdh will be invalidated when a response
2930 * is received or whenever we want to discard the
2931 * block the monitor thread will walk the queue
2932 * and clean up any rsts with an invalid mdh.
2933 * The monitor thread will walk the queue until
2934 * the first unexpired rspt block. This means that
2935 * some rspt blocks which received their
2936 * corresponding responses will linger in the
2937 * queue until they are cleaned up eventually.
2940 if (LNetMDHandleIsInvalid(rspt->rspt_mdh)) {
2942 list_del(&rspt->rspt_on_list);
2943 lnet_rspt_free(rspt, i);
2947 if (ktime_compare(now, rspt->rspt_deadline) >= 0 ||
2948 the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN) {
2949 struct lnet_peer_ni *lpni;
2952 md = lnet_handle2md(&rspt->rspt_mdh);
2954 /* MD has been queued for unlink, but
2955 * rspt hasn't been detached (Note we've
2956 * checked above that the rspt_mdh is
2957 * valid). Since we cannot lookup the MD
2958 * we're unable to detach the rspt
2959 * ourselves. Thus, move the rspt to the
2960 * zombie list where we'll wait for
2962 * 1. The remaining operations on the
2963 * MD to complete. In this case the
2964 * final operation will result in
2965 * lnet_msg_detach_md()->
2966 * lnet_detach_rsp_tracker() where
2967 * we will clean up this response
2969 * 2. LNet to shutdown. In this case
2970 * we'll wait until after all LND Nets
2971 * have shutdown and then we can
2972 * safely free any remaining response
2973 * tracker blocks on the zombie list.
2974 * Note: We need to hold the resource
2975 * lock when adding to the zombie list
2976 * because we may have concurrent access
2977 * with lnet_detach_rsp_tracker().
2979 LNetInvalidateMDHandle(&rspt->rspt_mdh);
2980 list_move(&rspt->rspt_on_list,
2981 the_lnet.ln_mt_zombie_rstqs[i]);
2985 LASSERT(md->md_rspt_ptr == rspt);
2986 md->md_rspt_ptr = NULL;
2989 LNetMDUnlink(rspt->rspt_mdh);
2991 nid = rspt->rspt_next_hop_nid;
2993 list_del(&rspt->rspt_on_list);
2994 lnet_rspt_free(rspt, i);
2996 /* If we're shutting down we just want to clean
2997 * up the rspt blocks
2999 if (the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN)
3003 the_lnet.ln_counters[i]->lct_health.lch_response_timeout_count++;
3007 "Response timeout: md = %p: nid = %s\n",
3008 md, libcfs_nid2str(nid));
3011 * If there is a timeout on the response
3012 * from the next hop decrement its health
3013 * value so that we don't use it
3016 lpni = lnet_find_peer_ni_locked(nid);
3018 lnet_handle_remote_failure_locked(lpni);
3019 lnet_peer_ni_decref_locked(lpni);
3028 if (!list_empty(&local_queue)) {
3030 list_splice(&local_queue, the_lnet.ln_mt_rstq[i]);
3037 lnet_resend_pending_msgs_locked(struct list_head *resendq, int cpt)
3039 struct lnet_msg *msg;
3041 while (!list_empty(resendq)) {
3042 struct lnet_peer_ni *lpni;
3044 msg = list_entry(resendq->next, struct lnet_msg,
3047 list_del_init(&msg->msg_list);
3049 lpni = lnet_find_peer_ni_locked(msg->msg_hdr.dest_nid);
3051 lnet_net_unlock(cpt);
3052 CERROR("Expected that a peer is already created for %s\n",
3053 libcfs_nid2str(msg->msg_hdr.dest_nid));
3054 msg->msg_no_resend = true;
3055 lnet_finalize(msg, -EFAULT);
3060 lnet_peer_ni_decref_locked(lpni);
3062 lnet_net_unlock(cpt);
3063 CDEBUG(D_NET, "resending %s->%s: %s recovery %d try# %d\n",
3064 libcfs_nid2str(msg->msg_src_nid_param),
3065 libcfs_id2str(msg->msg_target),
3066 lnet_msgtyp2str(msg->msg_type),
3068 msg->msg_retry_count);
3069 rc = lnet_send(msg->msg_src_nid_param, msg,
3070 msg->msg_rtr_nid_param);
3072 CERROR("Error sending %s to %s: %d\n",
3073 lnet_msgtyp2str(msg->msg_type),
3074 libcfs_id2str(msg->msg_target), rc);
3075 msg->msg_no_resend = true;
3076 lnet_finalize(msg, rc);
3080 the_lnet.ln_counters[cpt]->lct_health.lch_resend_count++;
3086 lnet_resend_pending_msgs(void)
3090 cfs_cpt_for_each(i, lnet_cpt_table()) {
3092 lnet_resend_pending_msgs_locked(the_lnet.ln_mt_resendqs[i], i);
3097 /* called with cpt and ni_lock held */
3099 lnet_unlink_ni_recovery_mdh_locked(struct lnet_ni *ni, int cpt, bool force)
3101 struct lnet_handle_md recovery_mdh;
3103 LNetInvalidateMDHandle(&recovery_mdh);
3105 if (ni->ni_recovery_state & LNET_NI_RECOVERY_PENDING ||
3107 recovery_mdh = ni->ni_ping_mdh;
3108 LNetInvalidateMDHandle(&ni->ni_ping_mdh);
3111 lnet_net_unlock(cpt);
3112 if (!LNetMDHandleIsInvalid(recovery_mdh))
3113 LNetMDUnlink(recovery_mdh);
3119 lnet_recover_local_nis(void)
3121 struct lnet_mt_event_info *ev_info;
3122 LIST_HEAD(processed_list);
3123 LIST_HEAD(local_queue);
3124 struct lnet_handle_md mdh;
3125 struct lnet_ni *tmp;
3132 * splice the recovery queue on a local queue. We will iterate
3133 * through the local queue and update it as needed. Once we're
3134 * done with the traversal, we'll splice the local queue back on
3135 * the head of the ln_mt_localNIRecovq. Any newly added local NIs
3136 * will be traversed in the next iteration.
3139 list_splice_init(&the_lnet.ln_mt_localNIRecovq,
3143 list_for_each_entry_safe(ni, tmp, &local_queue, ni_recovery) {
3145 * if an NI is being deleted or it is now healthy, there
3146 * is no need to keep it around in the recovery queue.
3147 * The monitor thread is the only thread responsible for
3148 * removing the NI from the recovery queue.
3149 * Multiple threads can be adding NIs to the recovery
3152 healthv = atomic_read(&ni->ni_healthv);
3156 if (ni->ni_state != LNET_NI_STATE_ACTIVE ||
3157 healthv == LNET_MAX_HEALTH_VALUE) {
3158 list_del_init(&ni->ni_recovery);
3159 lnet_unlink_ni_recovery_mdh_locked(ni, 0, false);
3161 lnet_ni_decref_locked(ni, 0);
3167 * if the local NI failed recovery we must unlink the md.
3168 * But we want to keep the local_ni on the recovery queue
3169 * so we can continue the attempts to recover it.
3171 if (ni->ni_recovery_state & LNET_NI_RECOVERY_FAILED) {
3172 lnet_unlink_ni_recovery_mdh_locked(ni, 0, true);
3173 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_FAILED;
3180 CDEBUG(D_NET, "attempting to recover local ni: %s\n",
3181 libcfs_nid2str(ni->ni_nid));
3184 if (!(ni->ni_recovery_state & LNET_NI_RECOVERY_PENDING)) {
3185 ni->ni_recovery_state |= LNET_NI_RECOVERY_PENDING;
3188 LIBCFS_ALLOC(ev_info, sizeof(*ev_info));
3190 CERROR("out of memory. Can't recover %s\n",
3191 libcfs_nid2str(ni->ni_nid));
3193 ni->ni_recovery_state &=
3194 ~LNET_NI_RECOVERY_PENDING;
3199 mdh = ni->ni_ping_mdh;
3201 * Invalidate the ni mdh in case it's deleted.
3202 * We'll unlink the mdh in this case below.
3204 LNetInvalidateMDHandle(&ni->ni_ping_mdh);
3208 * remove the NI from the local queue and drop the
3209 * reference count to it while we're recovering
3210 * it. The reason for that, is that the NI could
3211 * be deleted, and the way the code is structured
3212 * is if we don't drop the NI, then the deletion
3213 * code will enter a loop waiting for the
3214 * reference count to be removed while holding the
3215 * ln_mutex_lock(). When we look up the peer to
3216 * send to in lnet_select_pathway() we will try to
3217 * lock the ln_mutex_lock() as well, leading to
3218 * a deadlock. By dropping the refcount and
3219 * removing it from the list, we allow for the NI
3220 * to be removed, then we use the cached NID to
3221 * look it up again. If it's gone, then we just
3222 * continue examining the rest of the queue.
3225 list_del_init(&ni->ni_recovery);
3226 lnet_ni_decref_locked(ni, 0);
3229 ev_info->mt_type = MT_TYPE_LOCAL_NI;
3230 ev_info->mt_nid = nid;
3231 rc = lnet_send_ping(nid, &mdh, LNET_INTERFACES_MIN,
3232 ev_info, the_lnet.ln_mt_handler,
3234 /* lookup the nid again */
3236 ni = lnet_nid2ni_locked(nid, 0);
3239 * the NI has been deleted when we dropped
3247 * Same note as in lnet_recover_peer_nis(). When
3248 * we're sending the ping, the NI is free to be
3249 * deleted or manipulated. By this point it
3250 * could've been added back on the recovery queue,
3251 * and a refcount taken on it.
3252 * So we can't just add it blindly again or we'll
3253 * corrupt the queue. We must check under lock if
3254 * it's not on any list and if not then add it
3255 * to the processed list, which will eventually be
3256 * spliced back on to the recovery queue.
3258 ni->ni_ping_mdh = mdh;
3259 if (list_empty(&ni->ni_recovery)) {
3260 list_add_tail(&ni->ni_recovery, &processed_list);
3261 lnet_ni_addref_locked(ni, 0);
3267 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_PENDING;
3273 * put back the remaining NIs on the ln_mt_localNIRecovq to be
3274 * reexamined in the next iteration.
3276 list_splice_init(&processed_list, &local_queue);
3278 list_splice(&local_queue, &the_lnet.ln_mt_localNIRecovq);
3283 lnet_resendqs_create(void)
3285 struct list_head **resendqs;
3286 resendqs = lnet_create_array_of_queues();
3291 lnet_net_lock(LNET_LOCK_EX);
3292 the_lnet.ln_mt_resendqs = resendqs;
3293 lnet_net_unlock(LNET_LOCK_EX);
3299 lnet_clean_local_ni_recoveryq(void)
3303 /* This is only called when the monitor thread has stopped */
3306 while (!list_empty(&the_lnet.ln_mt_localNIRecovq)) {
3307 ni = list_entry(the_lnet.ln_mt_localNIRecovq.next,
3308 struct lnet_ni, ni_recovery);
3309 list_del_init(&ni->ni_recovery);
3311 lnet_unlink_ni_recovery_mdh_locked(ni, 0, true);
3313 lnet_ni_decref_locked(ni, 0);
3320 lnet_unlink_lpni_recovery_mdh_locked(struct lnet_peer_ni *lpni, int cpt,
3323 struct lnet_handle_md recovery_mdh;
3325 LNetInvalidateMDHandle(&recovery_mdh);
3327 if (lpni->lpni_state & LNET_PEER_NI_RECOVERY_PENDING || force) {
3328 recovery_mdh = lpni->lpni_recovery_ping_mdh;
3329 LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
3331 spin_unlock(&lpni->lpni_lock);
3332 lnet_net_unlock(cpt);
3333 if (!LNetMDHandleIsInvalid(recovery_mdh))
3334 LNetMDUnlink(recovery_mdh);
3336 spin_lock(&lpni->lpni_lock);
3340 lnet_clean_peer_ni_recoveryq(void)
3342 struct lnet_peer_ni *lpni, *tmp;
3344 lnet_net_lock(LNET_LOCK_EX);
3346 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_mt_peerNIRecovq,
3348 list_del_init(&lpni->lpni_recovery);
3349 spin_lock(&lpni->lpni_lock);
3350 lnet_unlink_lpni_recovery_mdh_locked(lpni, LNET_LOCK_EX, true);
3351 spin_unlock(&lpni->lpni_lock);
3352 lnet_peer_ni_decref_locked(lpni);
3355 lnet_net_unlock(LNET_LOCK_EX);
3359 lnet_clean_resendqs(void)
3361 struct lnet_msg *msg, *tmp;
3365 cfs_cpt_for_each(i, lnet_cpt_table()) {
3367 list_splice_init(the_lnet.ln_mt_resendqs[i], &msgs);
3369 list_for_each_entry_safe(msg, tmp, &msgs, msg_list) {
3370 list_del_init(&msg->msg_list);
3371 msg->msg_no_resend = true;
3372 lnet_finalize(msg, -ESHUTDOWN);
3376 cfs_percpt_free(the_lnet.ln_mt_resendqs);
3380 lnet_recover_peer_nis(void)
3382 struct lnet_mt_event_info *ev_info;
3383 LIST_HEAD(processed_list);
3384 LIST_HEAD(local_queue);
3385 struct lnet_handle_md mdh;
3386 struct lnet_peer_ni *lpni;
3387 struct lnet_peer_ni *tmp;
3393 * Always use cpt 0 for locking across all interactions with
3394 * ln_mt_peerNIRecovq
3397 list_splice_init(&the_lnet.ln_mt_peerNIRecovq,
3401 list_for_each_entry_safe(lpni, tmp, &local_queue,
3404 * The same protection strategy is used here as is in the
3405 * local recovery case.
3408 healthv = atomic_read(&lpni->lpni_healthv);
3409 spin_lock(&lpni->lpni_lock);
3410 if (lpni->lpni_state & LNET_PEER_NI_DELETING ||
3411 healthv == LNET_MAX_HEALTH_VALUE) {
3412 list_del_init(&lpni->lpni_recovery);
3413 lnet_unlink_lpni_recovery_mdh_locked(lpni, 0, false);
3414 spin_unlock(&lpni->lpni_lock);
3415 lnet_peer_ni_decref_locked(lpni);
3421 * If the peer NI has failed recovery we must unlink the
3422 * md. But we want to keep the peer ni on the recovery
3423 * queue so we can try to continue recovering it
3425 if (lpni->lpni_state & LNET_PEER_NI_RECOVERY_FAILED) {
3426 lnet_unlink_lpni_recovery_mdh_locked(lpni, 0, true);
3427 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_FAILED;
3430 spin_unlock(&lpni->lpni_lock);
3434 * NOTE: we're racing with peer deletion from user space.
3435 * It's possible that a peer is deleted after we check its
3436 * state. In this case the recovery can create a new peer
3438 spin_lock(&lpni->lpni_lock);
3439 if (!(lpni->lpni_state & LNET_PEER_NI_RECOVERY_PENDING) &&
3440 !(lpni->lpni_state & LNET_PEER_NI_DELETING)) {
3441 lpni->lpni_state |= LNET_PEER_NI_RECOVERY_PENDING;
3442 spin_unlock(&lpni->lpni_lock);
3444 LIBCFS_ALLOC(ev_info, sizeof(*ev_info));
3446 CERROR("out of memory. Can't recover %s\n",
3447 libcfs_nid2str(lpni->lpni_nid));
3448 spin_lock(&lpni->lpni_lock);
3449 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3450 spin_unlock(&lpni->lpni_lock);
3454 /* look at the comments in lnet_recover_local_nis() */
3455 mdh = lpni->lpni_recovery_ping_mdh;
3456 LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
3457 nid = lpni->lpni_nid;
3459 list_del_init(&lpni->lpni_recovery);
3460 lnet_peer_ni_decref_locked(lpni);
3463 ev_info->mt_type = MT_TYPE_PEER_NI;
3464 ev_info->mt_nid = nid;
3465 rc = lnet_send_ping(nid, &mdh, LNET_INTERFACES_MIN,
3466 ev_info, the_lnet.ln_mt_handler,
3470 * lnet_find_peer_ni_locked() grabs a refcount for
3471 * us. No need to take it explicitly.
3473 lpni = lnet_find_peer_ni_locked(nid);
3480 lpni->lpni_recovery_ping_mdh = mdh;
3482 * While we're unlocked the lpni could've been
3483 * readded on the recovery queue. In this case we
3484 * don't need to add it to the local queue, since
3485 * it's already on there and the thread that added
3486 * it would've incremented the refcount on the
3487 * peer, which means we need to decref the refcount
3488 * that was implicitly grabbed by find_peer_ni_locked.
3489 * Otherwise, if the lpni is still not on
3490 * the recovery queue, then we'll add it to the
3493 if (list_empty(&lpni->lpni_recovery))
3494 list_add_tail(&lpni->lpni_recovery, &processed_list);
3496 lnet_peer_ni_decref_locked(lpni);
3499 spin_lock(&lpni->lpni_lock);
3501 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3503 spin_unlock(&lpni->lpni_lock);
3506 list_splice_init(&processed_list, &local_queue);
3508 list_splice(&local_queue, &the_lnet.ln_mt_peerNIRecovq);
3513 lnet_monitor_thread(void *arg)
3515 time64_t recovery_timeout = 0;
3516 time64_t rsp_timeout = 0;
3520 wait_for_completion(&the_lnet.ln_started);
3522 * The monitor thread takes care of the following:
3523 * 1. Checks the aliveness of routers
3524 * 2. Checks if there are messages on the resend queue to resend
3526 * 3. Check if there are any NIs on the local recovery queue and
3528 * 4. Checks if there are any NIs on the remote recovery queue
3531 while (the_lnet.ln_mt_state == LNET_MT_STATE_RUNNING) {
3532 now = ktime_get_real_seconds();
3534 if (lnet_router_checker_active())
3535 lnet_check_routers();
3537 lnet_resend_pending_msgs();
3539 if (now >= rsp_timeout) {
3540 lnet_finalize_expired_responses();
3541 rsp_timeout = now + (lnet_transaction_timeout / 2);
3544 if (now >= recovery_timeout) {
3545 lnet_recover_local_nis();
3546 lnet_recover_peer_nis();
3547 recovery_timeout = now + lnet_recovery_interval;
3551 * TODO do we need to check if we should sleep without
3552 * timeout? Technically, an active system will always
3553 * have messages in flight so this check will always
3554 * evaluate to false. And on an idle system do we care
3555 * if we wake up every 1 second? Although, we've seen
3556 * cases where we get a complaint that an idle thread
3557 * is waking up unnecessarily.
3559 * Take into account the current net_count when you wake
3560 * up for alive router checking, since we need to check
3561 * possibly as many networks as we have configured.
3563 interval = min(lnet_recovery_interval,
3564 min((unsigned int) alive_router_check_interval /
3565 lnet_current_net_count,
3566 lnet_transaction_timeout / 2));
3567 wait_for_completion_interruptible_timeout(
3568 &the_lnet.ln_mt_wait_complete,
3569 cfs_time_seconds(interval));
3570 /* Must re-init the completion before testing anything,
3571 * including ln_mt_state.
3573 reinit_completion(&the_lnet.ln_mt_wait_complete);
3577 lnet_net_lock(LNET_LOCK_EX);
3578 the_lnet.ln_mt_state = LNET_MT_STATE_SHUTDOWN;
3579 lnet_net_unlock(LNET_LOCK_EX);
3581 /* signal that the monitor thread is exiting */
3582 up(&the_lnet.ln_mt_signal);
3590 * Returns == 0 if success
3591 * Returns > 0 if LNetMDBind or prior fails
3592 * Returns < 0 if LNetGet fails
3595 lnet_send_ping(lnet_nid_t dest_nid,
3596 struct lnet_handle_md *mdh, int nnis,
3597 void *user_data, lnet_handler_t handler, bool recovery)
3599 struct lnet_md md = { NULL };
3600 struct lnet_process_id id;
3601 struct lnet_ping_buffer *pbuf;
3604 if (dest_nid == LNET_NID_ANY) {
3609 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
3615 /* initialize md content */
3616 md.start = &pbuf->pb_info;
3617 md.length = LNET_PING_INFO_SIZE(nnis);
3618 md.threshold = 2; /* GET/REPLY */
3620 md.options = LNET_MD_TRUNCATE;
3621 md.user_ptr = user_data;
3622 md.handler = handler;
3624 rc = LNetMDBind(md, LNET_UNLINK, mdh);
3626 lnet_ping_buffer_decref(pbuf);
3627 CERROR("Can't bind MD: %d\n", rc);
3628 rc = -rc; /* change the rc to positive */
3631 id.pid = LNET_PID_LUSTRE;
3634 rc = LNetGet(LNET_NID_ANY, *mdh, id,
3635 LNET_RESERVED_PORTAL,
3636 LNET_PROTO_PING_MATCHBITS, 0, recovery);
3639 goto fail_unlink_md;
3645 LNetInvalidateMDHandle(mdh);
3651 lnet_handle_recovery_reply(struct lnet_mt_event_info *ev_info,
3652 int status, bool send, bool unlink_event)
3654 lnet_nid_t nid = ev_info->mt_nid;
3656 if (ev_info->mt_type == MT_TYPE_LOCAL_NI) {
3660 ni = lnet_nid2ni_locked(nid, 0);
3666 if (!send || (send && status != 0))
3667 ni->ni_recovery_state &= ~LNET_NI_RECOVERY_PENDING;
3669 ni->ni_recovery_state |= LNET_NI_RECOVERY_FAILED;
3674 CERROR("local NI (%s) recovery failed with %d\n",
3675 libcfs_nid2str(nid), status);
3679 * need to increment healthv for the ni here, because in
3680 * the lnet_finalize() path we don't have access to this
3681 * NI. And in order to get access to it, we'll need to
3682 * carry forward too much information.
3683 * In the peer case, it'll naturally be incremented
3686 lnet_inc_healthv(&ni->ni_healthv,
3687 lnet_health_sensitivity);
3689 struct lnet_peer_ni *lpni;
3692 cpt = lnet_net_lock_current();
3693 lpni = lnet_find_peer_ni_locked(nid);
3695 lnet_net_unlock(cpt);
3698 spin_lock(&lpni->lpni_lock);
3699 if (!send || (send && status != 0))
3700 lpni->lpni_state &= ~LNET_PEER_NI_RECOVERY_PENDING;
3702 lpni->lpni_state |= LNET_PEER_NI_RECOVERY_FAILED;
3703 spin_unlock(&lpni->lpni_lock);
3704 lnet_peer_ni_decref_locked(lpni);
3705 lnet_net_unlock(cpt);
3708 CERROR("peer NI (%s) recovery failed with %d\n",
3709 libcfs_nid2str(nid), status);
3714 lnet_mt_event_handler(struct lnet_event *event)
3716 struct lnet_mt_event_info *ev_info = event->md_user_ptr;
3717 struct lnet_ping_buffer *pbuf;
3719 /* TODO: remove assert */
3720 LASSERT(event->type == LNET_EVENT_REPLY ||
3721 event->type == LNET_EVENT_SEND ||
3722 event->type == LNET_EVENT_UNLINK);
3724 CDEBUG(D_NET, "Received event: %d status: %d\n", event->type,
3727 switch (event->type) {
3728 case LNET_EVENT_UNLINK:
3729 CDEBUG(D_NET, "%s recovery ping unlinked\n",
3730 libcfs_nid2str(ev_info->mt_nid));
3732 case LNET_EVENT_REPLY:
3733 lnet_handle_recovery_reply(ev_info, event->status, false,
3734 event->type == LNET_EVENT_UNLINK);
3736 case LNET_EVENT_SEND:
3737 CDEBUG(D_NET, "%s recovery message sent %s:%d\n",
3738 libcfs_nid2str(ev_info->mt_nid),
3739 (event->status) ? "unsuccessfully" :
3740 "successfully", event->status);
3741 lnet_handle_recovery_reply(ev_info, event->status, true, false);
3744 CERROR("Unexpected event: %d\n", event->type);
3747 if (event->unlinked) {
3748 LIBCFS_FREE(ev_info, sizeof(*ev_info));
3749 pbuf = LNET_PING_INFO_TO_BUFFER(event->md_start);
3750 lnet_ping_buffer_decref(pbuf);
3755 lnet_rsp_tracker_create(void)
3757 struct list_head **rstqs;
3758 rstqs = lnet_create_array_of_queues();
3763 the_lnet.ln_mt_rstq = rstqs;
3769 lnet_rsp_tracker_clean(void)
3771 lnet_finalize_expired_responses();
3773 cfs_percpt_free(the_lnet.ln_mt_rstq);
3774 the_lnet.ln_mt_rstq = NULL;
3777 int lnet_monitor_thr_start(void)
3780 struct task_struct *task;
3782 if (the_lnet.ln_mt_state != LNET_MT_STATE_SHUTDOWN)
3785 rc = lnet_resendqs_create();
3789 rc = lnet_rsp_tracker_create();
3793 sema_init(&the_lnet.ln_mt_signal, 0);
3795 lnet_net_lock(LNET_LOCK_EX);
3796 the_lnet.ln_mt_state = LNET_MT_STATE_RUNNING;
3797 lnet_net_unlock(LNET_LOCK_EX);
3798 task = kthread_run(lnet_monitor_thread, NULL, "monitor_thread");
3801 CERROR("Can't start monitor thread: %d\n", rc);
3808 lnet_net_lock(LNET_LOCK_EX);
3809 the_lnet.ln_mt_state = LNET_MT_STATE_STOPPING;
3810 lnet_net_unlock(LNET_LOCK_EX);
3811 /* block until event callback signals exit */
3812 down(&the_lnet.ln_mt_signal);
3814 lnet_net_lock(LNET_LOCK_EX);
3815 the_lnet.ln_mt_state = LNET_MT_STATE_SHUTDOWN;
3816 lnet_net_unlock(LNET_LOCK_EX);
3817 lnet_rsp_tracker_clean();
3818 lnet_clean_local_ni_recoveryq();
3819 lnet_clean_peer_ni_recoveryq();
3820 lnet_clean_resendqs();
3821 the_lnet.ln_mt_handler = NULL;
3824 lnet_rsp_tracker_clean();
3825 lnet_clean_local_ni_recoveryq();
3826 lnet_clean_peer_ni_recoveryq();
3827 lnet_clean_resendqs();
3831 void lnet_monitor_thr_stop(void)
3833 if (the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN)
3836 LASSERT(the_lnet.ln_mt_state == LNET_MT_STATE_RUNNING);
3837 lnet_net_lock(LNET_LOCK_EX);
3838 the_lnet.ln_mt_state = LNET_MT_STATE_STOPPING;
3839 lnet_net_unlock(LNET_LOCK_EX);
3841 /* tell the monitor thread that we're shutting down */
3842 complete(&the_lnet.ln_mt_wait_complete);
3844 /* block until monitor thread signals that it's done */
3845 down(&the_lnet.ln_mt_signal);
3846 LASSERT(the_lnet.ln_mt_state == LNET_MT_STATE_SHUTDOWN);
3848 /* perform cleanup tasks */
3849 lnet_rsp_tracker_clean();
3850 lnet_clean_local_ni_recoveryq();
3851 lnet_clean_peer_ni_recoveryq();
3852 lnet_clean_resendqs();
3856 lnet_drop_message(struct lnet_ni *ni, int cpt, void *private, unsigned int nob,
3860 lnet_incr_stats(&ni->ni_stats, msg_type, LNET_STATS_TYPE_DROP);
3861 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
3862 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length += nob;
3863 lnet_net_unlock(cpt);
3865 lnet_ni_recv(ni, private, NULL, 0, 0, 0, nob);
3869 lnet_recv_put(struct lnet_ni *ni, struct lnet_msg *msg)
3871 struct lnet_hdr *hdr = &msg->msg_hdr;
3873 if (msg->msg_wanted != 0)
3874 lnet_setpayloadbuffer(msg);
3876 lnet_build_msg_event(msg, LNET_EVENT_PUT);
3878 /* Must I ACK? If so I'll grab the ack_wmd out of the header and put
3879 * it back into the ACK during lnet_finalize() */
3880 msg->msg_ack = (!lnet_is_wire_handle_none(&hdr->msg.put.ack_wmd) &&
3881 (msg->msg_md->md_options & LNET_MD_ACK_DISABLE) == 0);
3883 lnet_ni_recv(ni, msg->msg_private, msg, msg->msg_rx_delayed,
3884 msg->msg_offset, msg->msg_wanted, hdr->payload_length);
3888 lnet_parse_put(struct lnet_ni *ni, struct lnet_msg *msg)
3890 struct lnet_hdr *hdr = &msg->msg_hdr;
3891 struct lnet_match_info info;
3895 /* Convert put fields to host byte order */
3896 hdr->msg.put.match_bits = le64_to_cpu(hdr->msg.put.match_bits);
3897 hdr->msg.put.ptl_index = le32_to_cpu(hdr->msg.put.ptl_index);
3898 hdr->msg.put.offset = le32_to_cpu(hdr->msg.put.offset);
3900 /* Primary peer NID. */
3901 info.mi_id.nid = msg->msg_initiator;
3902 info.mi_id.pid = hdr->src_pid;
3903 info.mi_opc = LNET_MD_OP_PUT;
3904 info.mi_portal = hdr->msg.put.ptl_index;
3905 info.mi_rlength = hdr->payload_length;
3906 info.mi_roffset = hdr->msg.put.offset;
3907 info.mi_mbits = hdr->msg.put.match_bits;
3908 info.mi_cpt = lnet_cpt_of_nid(msg->msg_initiator, ni);
3910 msg->msg_rx_ready_delay = ni->ni_net->net_lnd->lnd_eager_recv == NULL;
3911 ready_delay = msg->msg_rx_ready_delay;
3914 rc = lnet_ptl_match_md(&info, msg);
3919 case LNET_MATCHMD_OK:
3920 lnet_recv_put(ni, msg);
3923 case LNET_MATCHMD_NONE:
3925 /* no eager_recv or has already called it, should
3926 * have been attached on delayed list */
3929 rc = lnet_ni_eager_recv(ni, msg);
3936 case LNET_MATCHMD_DROP:
3937 CNETERR("Dropping PUT from %s portal %d match %llu"
3938 " offset %d length %d: %d\n",
3939 libcfs_id2str(info.mi_id), info.mi_portal,
3940 info.mi_mbits, info.mi_roffset, info.mi_rlength, rc);
3942 return -ENOENT; /* -ve: OK but no match */
3947 lnet_parse_get(struct lnet_ni *ni, struct lnet_msg *msg, int rdma_get)
3949 struct lnet_match_info info;
3950 struct lnet_hdr *hdr = &msg->msg_hdr;
3951 struct lnet_process_id source_id;
3952 struct lnet_handle_wire reply_wmd;
3955 /* Convert get fields to host byte order */
3956 hdr->msg.get.match_bits = le64_to_cpu(hdr->msg.get.match_bits);
3957 hdr->msg.get.ptl_index = le32_to_cpu(hdr->msg.get.ptl_index);
3958 hdr->msg.get.sink_length = le32_to_cpu(hdr->msg.get.sink_length);
3959 hdr->msg.get.src_offset = le32_to_cpu(hdr->msg.get.src_offset);
3961 source_id.nid = hdr->src_nid;
3962 source_id.pid = hdr->src_pid;
3963 /* Primary peer NID */
3964 info.mi_id.nid = msg->msg_initiator;
3965 info.mi_id.pid = hdr->src_pid;
3966 info.mi_opc = LNET_MD_OP_GET;
3967 info.mi_portal = hdr->msg.get.ptl_index;
3968 info.mi_rlength = hdr->msg.get.sink_length;
3969 info.mi_roffset = hdr->msg.get.src_offset;
3970 info.mi_mbits = hdr->msg.get.match_bits;
3971 info.mi_cpt = lnet_cpt_of_nid(msg->msg_initiator, ni);
3973 rc = lnet_ptl_match_md(&info, msg);
3974 if (rc == LNET_MATCHMD_DROP) {
3975 CNETERR("Dropping GET from %s portal %d match %llu"
3976 " offset %d length %d\n",
3977 libcfs_id2str(info.mi_id), info.mi_portal,
3978 info.mi_mbits, info.mi_roffset, info.mi_rlength);
3979 return -ENOENT; /* -ve: OK but no match */
3982 LASSERT(rc == LNET_MATCHMD_OK);
3984 lnet_build_msg_event(msg, LNET_EVENT_GET);
3986 reply_wmd = hdr->msg.get.return_wmd;
3988 lnet_prep_send(msg, LNET_MSG_REPLY, source_id,
3989 msg->msg_offset, msg->msg_wanted);
3991 msg->msg_hdr.msg.reply.dst_wmd = reply_wmd;
3994 /* The LND completes the REPLY from her recv procedure */
3995 lnet_ni_recv(ni, msg->msg_private, msg, 0,
3996 msg->msg_offset, msg->msg_len, msg->msg_len);
4000 lnet_ni_recv(ni, msg->msg_private, NULL, 0, 0, 0, 0);
4001 msg->msg_receiving = 0;
4003 rc = lnet_send(ni->ni_nid, msg, msg->msg_from);
4005 /* didn't get as far as lnet_ni_send() */
4006 CERROR("%s: Unable to send REPLY for GET from %s: %d\n",
4007 libcfs_nid2str(ni->ni_nid),
4008 libcfs_id2str(info.mi_id), rc);
4010 lnet_finalize(msg, rc);
4017 lnet_parse_reply(struct lnet_ni *ni, struct lnet_msg *msg)
4019 void *private = msg->msg_private;
4020 struct lnet_hdr *hdr = &msg->msg_hdr;
4021 struct lnet_process_id src = {0};
4022 struct lnet_libmd *md;
4023 unsigned int rlength;
4024 unsigned int mlength;
4027 cpt = lnet_cpt_of_cookie(hdr->msg.reply.dst_wmd.wh_object_cookie);
4030 src.nid = hdr->src_nid;
4031 src.pid = hdr->src_pid;
4033 /* NB handles only looked up by creator (no flips) */
4034 md = lnet_wire_handle2md(&hdr->msg.reply.dst_wmd);
4035 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
4036 CNETERR("%s: Dropping REPLY from %s for %s "
4038 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
4039 (md == NULL) ? "invalid" : "inactive",
4040 hdr->msg.reply.dst_wmd.wh_interface_cookie,
4041 hdr->msg.reply.dst_wmd.wh_object_cookie);
4042 if (md != NULL && md->md_me != NULL)
4043 CERROR("REPLY MD also attached to portal %d\n",
4044 md->md_me->me_portal);
4046 lnet_res_unlock(cpt);
4047 return -ENOENT; /* -ve: OK but no match */
4050 LASSERT(md->md_offset == 0);
4052 rlength = hdr->payload_length;
4053 mlength = min(rlength, md->md_length);
4055 if (mlength < rlength &&
4056 (md->md_options & LNET_MD_TRUNCATE) == 0) {
4057 CNETERR("%s: Dropping REPLY from %s length %d "
4058 "for MD %#llx would overflow (%d)\n",
4059 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
4060 rlength, hdr->msg.reply.dst_wmd.wh_object_cookie,
4062 lnet_res_unlock(cpt);
4063 return -ENOENT; /* -ve: OK but no match */
4066 CDEBUG(D_NET, "%s: Reply from %s of length %d/%d into md %#llx\n",
4067 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
4068 mlength, rlength, hdr->msg.reply.dst_wmd.wh_object_cookie);
4070 lnet_msg_attach_md(msg, md, 0, mlength);
4073 lnet_setpayloadbuffer(msg);
4075 lnet_res_unlock(cpt);
4077 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
4079 lnet_ni_recv(ni, private, msg, 0, 0, mlength, rlength);
4084 lnet_parse_ack(struct lnet_ni *ni, struct lnet_msg *msg)
4086 struct lnet_hdr *hdr = &msg->msg_hdr;
4087 struct lnet_process_id src = {0};
4088 struct lnet_libmd *md;
4091 src.nid = hdr->src_nid;
4092 src.pid = hdr->src_pid;
4094 /* Convert ack fields to host byte order */
4095 hdr->msg.ack.match_bits = le64_to_cpu(hdr->msg.ack.match_bits);
4096 hdr->msg.ack.mlength = le32_to_cpu(hdr->msg.ack.mlength);
4098 cpt = lnet_cpt_of_cookie(hdr->msg.ack.dst_wmd.wh_object_cookie);
4101 /* NB handles only looked up by creator (no flips) */
4102 md = lnet_wire_handle2md(&hdr->msg.ack.dst_wmd);
4103 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
4104 /* Don't moan; this is expected */
4106 "%s: Dropping ACK from %s to %s MD %#llx.%#llx\n",
4107 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
4108 (md == NULL) ? "invalid" : "inactive",
4109 hdr->msg.ack.dst_wmd.wh_interface_cookie,
4110 hdr->msg.ack.dst_wmd.wh_object_cookie);
4111 if (md != NULL && md->md_me != NULL)
4112 CERROR("Source MD also attached to portal %d\n",
4113 md->md_me->me_portal);
4115 lnet_res_unlock(cpt);
4116 return -ENOENT; /* -ve! */
4119 CDEBUG(D_NET, "%s: ACK from %s into md %#llx\n",
4120 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
4121 hdr->msg.ack.dst_wmd.wh_object_cookie);
4123 lnet_msg_attach_md(msg, md, 0, 0);
4125 lnet_res_unlock(cpt);
4127 lnet_build_msg_event(msg, LNET_EVENT_ACK);
4129 lnet_ni_recv(ni, msg->msg_private, msg, 0, 0, 0, msg->msg_len);
4134 * \retval LNET_CREDIT_OK If \a msg is forwarded
4135 * \retval LNET_CREDIT_WAIT If \a msg is blocked because w/o buffer
4136 * \retval -ve error code
4139 lnet_parse_forward_locked(struct lnet_ni *ni, struct lnet_msg *msg)
4143 if (!the_lnet.ln_routing)
4146 if (msg->msg_rxpeer->lpni_rtrcredits <= 0 ||
4147 lnet_msg2bufpool(msg)->rbp_credits <= 0) {
4148 if (ni->ni_net->net_lnd->lnd_eager_recv == NULL) {
4149 msg->msg_rx_ready_delay = 1;
4151 lnet_net_unlock(msg->msg_rx_cpt);
4152 rc = lnet_ni_eager_recv(ni, msg);
4153 lnet_net_lock(msg->msg_rx_cpt);
4158 rc = lnet_post_routed_recv_locked(msg, 0);
4163 lnet_parse_local(struct lnet_ni *ni, struct lnet_msg *msg)
4167 switch (msg->msg_type) {
4169 rc = lnet_parse_ack(ni, msg);
4172 rc = lnet_parse_put(ni, msg);
4175 rc = lnet_parse_get(ni, msg, msg->msg_rdma_get);
4177 case LNET_MSG_REPLY:
4178 rc = lnet_parse_reply(ni, msg);
4180 default: /* prevent an unused label if !kernel */
4185 LASSERT(rc == 0 || rc == -ENOENT);
4190 lnet_msgtyp2str (int type)
4199 case LNET_MSG_REPLY:
4201 case LNET_MSG_HELLO:
4204 return ("<UNKNOWN>");
4209 lnet_print_hdr(struct lnet_hdr *hdr)
4211 struct lnet_process_id src = {
4212 .nid = hdr->src_nid,
4213 .pid = hdr->src_pid,
4215 struct lnet_process_id dst = {
4216 .nid = hdr->dest_nid,
4217 .pid = hdr->dest_pid,
4219 char *type_str = lnet_msgtyp2str(hdr->type);
4221 CWARN("P3 Header at %p of type %s\n", hdr, type_str);
4222 CWARN(" From %s\n", libcfs_id2str(src));
4223 CWARN(" To %s\n", libcfs_id2str(dst));
4225 switch (hdr->type) {
4230 CWARN(" Ptl index %d, ack md %#llx.%#llx, "
4231 "match bits %llu\n",
4232 hdr->msg.put.ptl_index,
4233 hdr->msg.put.ack_wmd.wh_interface_cookie,
4234 hdr->msg.put.ack_wmd.wh_object_cookie,
4235 hdr->msg.put.match_bits);
4236 CWARN(" Length %d, offset %d, hdr data %#llx\n",
4237 hdr->payload_length, hdr->msg.put.offset,
4238 hdr->msg.put.hdr_data);
4242 CWARN(" Ptl index %d, return md %#llx.%#llx, "
4243 "match bits %llu\n", hdr->msg.get.ptl_index,
4244 hdr->msg.get.return_wmd.wh_interface_cookie,
4245 hdr->msg.get.return_wmd.wh_object_cookie,
4246 hdr->msg.get.match_bits);
4247 CWARN(" Length %d, src offset %d\n",
4248 hdr->msg.get.sink_length,
4249 hdr->msg.get.src_offset);
4253 CWARN(" dst md %#llx.%#llx, "
4254 "manipulated length %d\n",
4255 hdr->msg.ack.dst_wmd.wh_interface_cookie,
4256 hdr->msg.ack.dst_wmd.wh_object_cookie,
4257 hdr->msg.ack.mlength);
4260 case LNET_MSG_REPLY:
4261 CWARN(" dst md %#llx.%#llx, "
4263 hdr->msg.reply.dst_wmd.wh_interface_cookie,
4264 hdr->msg.reply.dst_wmd.wh_object_cookie,
4265 hdr->payload_length);
4271 lnet_parse(struct lnet_ni *ni, struct lnet_hdr *hdr, lnet_nid_t from_nid,
4272 void *private, int rdma_req)
4274 struct lnet_peer_ni *lpni;
4275 struct lnet_msg *msg;
4276 __u32 payload_length;
4277 lnet_pid_t dest_pid;
4278 lnet_nid_t dest_nid;
4286 LASSERT (!in_interrupt ());
4288 type = le32_to_cpu(hdr->type);
4289 src_nid = le64_to_cpu(hdr->src_nid);
4290 dest_nid = le64_to_cpu(hdr->dest_nid);
4291 dest_pid = le32_to_cpu(hdr->dest_pid);
4292 payload_length = le32_to_cpu(hdr->payload_length);
4294 for_me = (ni->ni_nid == dest_nid);
4295 cpt = lnet_cpt_of_nid(from_nid, ni);
4297 CDEBUG(D_NET, "TRACE: %s(%s) <- %s : %s - %s\n",
4298 libcfs_nid2str(dest_nid),
4299 libcfs_nid2str(ni->ni_nid),
4300 libcfs_nid2str(src_nid),
4301 lnet_msgtyp2str(type),
4302 (for_me) ? "for me" : "routed");
4307 if (payload_length > 0) {
4308 CERROR("%s, src %s: bad %s payload %d (0 expected)\n",
4309 libcfs_nid2str(from_nid),
4310 libcfs_nid2str(src_nid),
4311 lnet_msgtyp2str(type), payload_length);
4317 case LNET_MSG_REPLY:
4318 if (payload_length >
4319 (__u32)(for_me ? LNET_MAX_PAYLOAD : LNET_MTU)) {
4320 CERROR("%s, src %s: bad %s payload %d "
4321 "(%d max expected)\n",
4322 libcfs_nid2str(from_nid),
4323 libcfs_nid2str(src_nid),
4324 lnet_msgtyp2str(type),
4326 for_me ? LNET_MAX_PAYLOAD : LNET_MTU);
4332 CERROR("%s, src %s: Bad message type 0x%x\n",
4333 libcfs_nid2str(from_nid),
4334 libcfs_nid2str(src_nid), type);
4338 if (the_lnet.ln_routing &&
4339 ni->ni_net->net_last_alive != ktime_get_real_seconds()) {
4341 spin_lock(&ni->ni_net->net_lock);
4342 ni->ni_net->net_last_alive = ktime_get_real_seconds();
4343 spin_unlock(&ni->ni_net->net_lock);
4344 if (ni->ni_status != NULL &&
4345 ni->ni_status->ns_status == LNET_NI_STATUS_DOWN) {
4346 ni->ni_status->ns_status = LNET_NI_STATUS_UP;
4353 lnet_push_update_to_peers(1);
4355 /* Regard a bad destination NID as a protocol error. Senders should
4356 * know what they're doing; if they don't they're misconfigured, buggy
4357 * or malicious so we chop them off at the knees :) */
4360 if (LNET_NIDNET(dest_nid) == LNET_NIDNET(ni->ni_nid)) {
4361 /* should have gone direct */
4362 CERROR("%s, src %s: Bad dest nid %s "
4363 "(should have been sent direct)\n",
4364 libcfs_nid2str(from_nid),
4365 libcfs_nid2str(src_nid),
4366 libcfs_nid2str(dest_nid));
4370 if (lnet_islocalnid(dest_nid)) {
4371 /* dest is another local NI; sender should have used
4372 * this node's NID on its own network */
4373 CERROR("%s, src %s: Bad dest nid %s "
4374 "(it's my nid but on a different network)\n",
4375 libcfs_nid2str(from_nid),
4376 libcfs_nid2str(src_nid),
4377 libcfs_nid2str(dest_nid));
4381 if (rdma_req && type == LNET_MSG_GET) {
4382 CERROR("%s, src %s: Bad optimized GET for %s "
4383 "(final destination must be me)\n",
4384 libcfs_nid2str(from_nid),
4385 libcfs_nid2str(src_nid),
4386 libcfs_nid2str(dest_nid));
4390 if (!the_lnet.ln_routing) {
4391 CERROR("%s, src %s: Dropping message for %s "
4392 "(routing not enabled)\n",
4393 libcfs_nid2str(from_nid),
4394 libcfs_nid2str(src_nid),
4395 libcfs_nid2str(dest_nid));
4400 /* Message looks OK; we're not going to return an error, so we MUST
4401 * call back lnd_recv() come what may... */
4403 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
4404 fail_peer(src_nid, 0)) { /* shall we now? */
4405 CERROR("%s, src %s: Dropping %s to simulate failure\n",
4406 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4407 lnet_msgtyp2str(type));
4411 if (!list_empty(&the_lnet.ln_drop_rules) &&
4412 lnet_drop_rule_match(hdr, ni->ni_nid, NULL)) {
4414 "%s, src %s, dst %s: Dropping %s to simulate silent message loss\n",
4415 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4416 libcfs_nid2str(dest_nid), lnet_msgtyp2str(type));
4420 if (lnet_drop_asym_route && for_me &&
4421 LNET_NIDNET(src_nid) != LNET_NIDNET(from_nid)) {
4422 struct lnet_net *net;
4423 struct lnet_remotenet *rnet;
4426 /* we are dealing with a routed message,
4427 * so see if route to reach src_nid goes through from_nid
4430 net = lnet_get_net_locked(LNET_NIDNET(ni->ni_nid));
4432 lnet_net_unlock(cpt);
4433 CERROR("net %s not found\n",
4434 libcfs_net2str(LNET_NIDNET(ni->ni_nid)));
4438 rnet = lnet_find_rnet_locked(LNET_NIDNET(src_nid));
4440 struct lnet_peer *gw = NULL;
4441 struct lnet_peer_ni *lpni = NULL;
4442 struct lnet_route *route;
4444 list_for_each_entry(route, &rnet->lrn_routes, lr_list) {
4446 gw = route->lr_gateway;
4447 if (route->lr_lnet != net->net_id)
4450 * if the nid is one of the gateway's NIDs
4451 * then this is a valid gateway
4453 while ((lpni = lnet_get_next_peer_ni_locked(gw,
4454 NULL, lpni)) != NULL) {
4455 if (lpni->lpni_nid == from_nid) {
4462 lnet_net_unlock(cpt);
4464 /* we would not use from_nid to route a message to
4466 * => asymmetric routing detected but forbidden
4468 CERROR("%s, src %s: Dropping asymmetrical route %s\n",
4469 libcfs_nid2str(from_nid),
4470 libcfs_nid2str(src_nid), lnet_msgtyp2str(type));
4475 msg = lnet_msg_alloc();
4477 CERROR("%s, src %s: Dropping %s (out of memory)\n",
4478 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4479 lnet_msgtyp2str(type));
4483 /* msg zeroed in lnet_msg_alloc; i.e. flags all clear,
4484 * pointers NULL etc */
4486 msg->msg_type = type;
4487 msg->msg_private = private;
4488 msg->msg_receiving = 1;
4489 msg->msg_rdma_get = rdma_req;
4490 msg->msg_len = msg->msg_wanted = payload_length;
4491 msg->msg_offset = 0;
4492 msg->msg_hdr = *hdr;
4493 /* for building message event */
4494 msg->msg_from = from_nid;
4496 msg->msg_target.pid = dest_pid;
4497 msg->msg_target.nid = dest_nid;
4498 msg->msg_routing = 1;
4501 /* convert common msg->hdr fields to host byteorder */
4502 msg->msg_hdr.type = type;
4503 msg->msg_hdr.src_nid = src_nid;
4504 msg->msg_hdr.src_pid = le32_to_cpu(msg->msg_hdr.src_pid);
4505 msg->msg_hdr.dest_nid = dest_nid;
4506 msg->msg_hdr.dest_pid = dest_pid;
4507 msg->msg_hdr.payload_length = payload_length;
4511 lpni = lnet_nid2peerni_locked(from_nid, ni->ni_nid, cpt);
4513 lnet_net_unlock(cpt);
4514 CERROR("%s, src %s: Dropping %s "
4515 "(error %ld looking up sender)\n",
4516 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
4517 lnet_msgtyp2str(type), PTR_ERR(lpni));
4519 if (rc == -ESHUTDOWN)
4520 /* We are shutting down. Don't do anything more */
4525 if (the_lnet.ln_routing)
4526 lpni->lpni_last_alive = ktime_get_seconds();
4528 msg->msg_rxpeer = lpni;
4530 lnet_ni_addref_locked(ni, cpt);
4531 /* Multi-Rail: Primary NID of source. */
4532 msg->msg_initiator = lnet_peer_primary_nid_locked(src_nid);
4535 * mark the status of this lpni as UP since we received a message
4536 * from it. The ping response reports back the ns_status which is
4537 * marked on the remote as up or down and we cache it here.
4539 msg->msg_rxpeer->lpni_ns_status = LNET_NI_STATUS_UP;
4541 lnet_msg_commit(msg, cpt);
4543 /* message delay simulation */
4544 if (unlikely(!list_empty(&the_lnet.ln_delay_rules) &&
4545 lnet_delay_rule_match_locked(hdr, msg))) {
4546 lnet_net_unlock(cpt);
4551 rc = lnet_parse_forward_locked(ni, msg);
4552 lnet_net_unlock(cpt);
4557 if (rc == LNET_CREDIT_OK) {
4558 lnet_ni_recv(ni, msg->msg_private, msg, 0,
4559 0, payload_length, payload_length);
4564 lnet_net_unlock(cpt);
4566 rc = lnet_parse_local(ni, msg);
4572 LASSERT(msg->msg_md == NULL);
4573 lnet_finalize(msg, rc);
4576 lnet_drop_message(ni, cpt, private, payload_length, type);
4579 EXPORT_SYMBOL(lnet_parse);
4582 lnet_drop_delayed_msg_list(struct list_head *head, char *reason)
4584 while (!list_empty(head)) {
4585 struct lnet_process_id id = {0};
4586 struct lnet_msg *msg;
4588 msg = list_entry(head->next, struct lnet_msg, msg_list);
4589 list_del(&msg->msg_list);
4591 id.nid = msg->msg_hdr.src_nid;
4592 id.pid = msg->msg_hdr.src_pid;
4594 LASSERT(msg->msg_md == NULL);
4595 LASSERT(msg->msg_rx_delayed);
4596 LASSERT(msg->msg_rxpeer != NULL);
4597 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
4599 CWARN("Dropping delayed PUT from %s portal %d match %llu"
4600 " offset %d length %d: %s\n",
4602 msg->msg_hdr.msg.put.ptl_index,
4603 msg->msg_hdr.msg.put.match_bits,
4604 msg->msg_hdr.msg.put.offset,
4605 msg->msg_hdr.payload_length, reason);
4607 /* NB I can't drop msg's ref on msg_rxpeer until after I've
4608 * called lnet_drop_message(), so I just hang onto msg as well
4609 * until that's done */
4611 lnet_drop_message(msg->msg_rxni, msg->msg_rx_cpt,
4612 msg->msg_private, msg->msg_len,
4615 msg->msg_no_resend = true;
4617 * NB: message will not generate event because w/o attached MD,
4618 * but we still should give error code so lnet_msg_decommit()
4619 * can skip counters operations and other checks.
4621 lnet_finalize(msg, -ENOENT);
4626 lnet_recv_delayed_msg_list(struct list_head *head)
4628 while (!list_empty(head)) {
4629 struct lnet_msg *msg;
4630 struct lnet_process_id id;
4632 msg = list_entry(head->next, struct lnet_msg, msg_list);
4633 list_del(&msg->msg_list);
4635 /* md won't disappear under me, since each msg
4636 * holds a ref on it */
4638 id.nid = msg->msg_hdr.src_nid;
4639 id.pid = msg->msg_hdr.src_pid;
4641 LASSERT(msg->msg_rx_delayed);
4642 LASSERT(msg->msg_md != NULL);
4643 LASSERT(msg->msg_rxpeer != NULL);
4644 LASSERT(msg->msg_rxni != NULL);
4645 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
4647 CDEBUG(D_NET, "Resuming delayed PUT from %s portal %d "
4648 "match %llu offset %d length %d.\n",
4649 libcfs_id2str(id), msg->msg_hdr.msg.put.ptl_index,
4650 msg->msg_hdr.msg.put.match_bits,
4651 msg->msg_hdr.msg.put.offset,
4652 msg->msg_hdr.payload_length);
4654 lnet_recv_put(msg->msg_rxni, msg);
4659 lnet_attach_rsp_tracker(struct lnet_rsp_tracker *rspt, int cpt,
4660 struct lnet_libmd *md, struct lnet_handle_md mdh)
4663 struct lnet_rsp_tracker *local_rspt;
4666 * MD has a refcount taken by message so it's not going away.
4667 * The MD however can be looked up. We need to secure the access
4668 * to the md_rspt_ptr by taking the res_lock.
4669 * The rspt can be accessed without protection up to when it gets
4670 * added to the list.
4674 local_rspt = md->md_rspt_ptr;
4675 timeout_ns = lnet_transaction_timeout * NSEC_PER_SEC;
4676 if (local_rspt != NULL) {
4678 * we already have an rspt attached to the md, so we'll
4679 * update the deadline on that one.
4681 lnet_rspt_free(rspt, cpt);
4684 rspt->rspt_mdh = mdh;
4685 rspt->rspt_cpt = cpt;
4686 /* store the rspt so we can access it when we get the REPLY */
4687 md->md_rspt_ptr = rspt;
4690 local_rspt->rspt_deadline = ktime_add_ns(ktime_get(), timeout_ns);
4693 * add to the list of tracked responses. It's added to tail of the
4694 * list in order to expire all the older entries first.
4697 list_move_tail(&local_rspt->rspt_on_list, the_lnet.ln_mt_rstq[cpt]);
4698 lnet_net_unlock(cpt);
4699 lnet_res_unlock(cpt);
4703 * Initiate an asynchronous PUT operation.
4705 * There are several events associated with a PUT: completion of the send on
4706 * the initiator node (LNET_EVENT_SEND), and when the send completes
4707 * successfully, the receipt of an acknowledgment (LNET_EVENT_ACK) indicating
4708 * that the operation was accepted by the target. The event LNET_EVENT_PUT is
4709 * used at the target node to indicate the completion of incoming data
4712 * The local events will be logged in the EQ associated with the MD pointed to
4713 * by \a mdh handle. Using a MD without an associated EQ results in these
4714 * events being discarded. In this case, the caller must have another
4715 * mechanism (e.g., a higher level protocol) for determining when it is safe
4716 * to modify the memory region associated with the MD.
4718 * Note that LNet does not guarantee the order of LNET_EVENT_SEND and
4719 * LNET_EVENT_ACK, though intuitively ACK should happen after SEND.
4721 * \param self Indicates the NID of a local interface through which to send
4722 * the PUT request. Use LNET_NID_ANY to let LNet choose one by itself.
4723 * \param mdh A handle for the MD that describes the memory to be sent. The MD
4724 * must be "free floating" (See LNetMDBind()).
4725 * \param ack Controls whether an acknowledgment is requested.
4726 * Acknowledgments are only sent when they are requested by the initiating
4727 * process and the target MD enables them.
4728 * \param target A process identifier for the target process.
4729 * \param portal The index in the \a target's portal table.
4730 * \param match_bits The match bits to use for MD selection at the target
4732 * \param offset The offset into the target MD (only used when the target
4733 * MD has the LNET_MD_MANAGE_REMOTE option set).
4734 * \param hdr_data 64 bits of user data that can be included in the message
4735 * header. This data is written to an event queue entry at the target if an
4736 * EQ is present on the matching MD.
4738 * \retval 0 Success, and only in this case events will be generated
4739 * and logged to EQ (if it exists).
4740 * \retval -EIO Simulated failure.
4741 * \retval -ENOMEM Memory allocation failure.
4742 * \retval -ENOENT Invalid MD object.
4744 * \see struct lnet_event::hdr_data and lnet_event_kind_t.
4747 LNetPut(lnet_nid_t self, struct lnet_handle_md mdh, enum lnet_ack_req ack,
4748 struct lnet_process_id target, unsigned int portal,
4749 __u64 match_bits, unsigned int offset,
4752 struct lnet_msg *msg;
4753 struct lnet_libmd *md;
4756 struct lnet_rsp_tracker *rspt = NULL;
4758 LASSERT(the_lnet.ln_refcount > 0);
4760 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
4761 fail_peer(target.nid, 1)) { /* shall we now? */
4762 CERROR("Dropping PUT to %s: simulated failure\n",
4763 libcfs_id2str(target));
4767 msg = lnet_msg_alloc();
4769 CERROR("Dropping PUT to %s: ENOMEM on struct lnet_msg\n",
4770 libcfs_id2str(target));
4773 msg->msg_vmflush = !!(current->flags & PF_MEMALLOC);
4775 cpt = lnet_cpt_of_cookie(mdh.cookie);
4777 if (ack == LNET_ACK_REQ) {
4778 rspt = lnet_rspt_alloc(cpt);
4780 CERROR("Dropping PUT to %s: ENOMEM on response tracker\n",
4781 libcfs_id2str(target));
4784 INIT_LIST_HEAD(&rspt->rspt_on_list);
4789 md = lnet_handle2md(&mdh);
4790 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
4791 CERROR("Dropping PUT (%llu:%d:%s): MD (%d) invalid\n",
4792 match_bits, portal, libcfs_id2str(target),
4793 md == NULL ? -1 : md->md_threshold);
4794 if (md != NULL && md->md_me != NULL)
4795 CERROR("Source MD also attached to portal %d\n",
4796 md->md_me->me_portal);
4797 lnet_res_unlock(cpt);
4799 lnet_rspt_free(rspt, cpt);
4804 CDEBUG(D_NET, "LNetPut -> %s\n", libcfs_id2str(target));
4806 lnet_msg_attach_md(msg, md, 0, 0);
4808 lnet_prep_send(msg, LNET_MSG_PUT, target, 0, md->md_length);
4810 msg->msg_hdr.msg.put.match_bits = cpu_to_le64(match_bits);
4811 msg->msg_hdr.msg.put.ptl_index = cpu_to_le32(portal);
4812 msg->msg_hdr.msg.put.offset = cpu_to_le32(offset);
4813 msg->msg_hdr.msg.put.hdr_data = hdr_data;
4815 /* NB handles only looked up by creator (no flips) */
4816 if (ack == LNET_ACK_REQ) {
4817 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
4818 the_lnet.ln_interface_cookie;
4819 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
4820 md->md_lh.lh_cookie;
4822 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
4823 LNET_WIRE_HANDLE_COOKIE_NONE;
4824 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
4825 LNET_WIRE_HANDLE_COOKIE_NONE;
4828 lnet_res_unlock(cpt);
4830 lnet_build_msg_event(msg, LNET_EVENT_SEND);
4832 if (ack == LNET_ACK_REQ)
4833 lnet_attach_rsp_tracker(rspt, cpt, md, mdh);
4835 if (CFS_FAIL_CHECK_ORSET(CFS_FAIL_PTLRPC_OST_BULK_CB2,
4839 rc = lnet_send(self, msg, LNET_NID_ANY);
4842 CNETERR("Error sending PUT to %s: %d\n",
4843 libcfs_id2str(target), rc);
4844 msg->msg_no_resend = true;
4845 lnet_finalize(msg, rc);
4848 /* completion will be signalled by an event */
4851 EXPORT_SYMBOL(LNetPut);
4854 * The LND can DMA direct to the GET md (i.e. no REPLY msg). This
4855 * returns a msg for the LND to pass to lnet_finalize() when the sink
4856 * data has been received.
4858 * CAVEAT EMPTOR: 'getmsg' is the original GET, which is freed when
4859 * lnet_finalize() is called on it, so the LND must call this first
4862 lnet_create_reply_msg(struct lnet_ni *ni, struct lnet_msg *getmsg)
4864 struct lnet_msg *msg = lnet_msg_alloc();
4865 struct lnet_libmd *getmd = getmsg->msg_md;
4866 struct lnet_process_id peer_id = getmsg->msg_target;
4869 LASSERT(!getmsg->msg_target_is_router);
4870 LASSERT(!getmsg->msg_routing);
4873 CERROR("%s: Dropping REPLY from %s: can't allocate msg\n",
4874 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id));
4878 cpt = lnet_cpt_of_cookie(getmd->md_lh.lh_cookie);
4881 LASSERT(getmd->md_refcount > 0);
4883 if (getmd->md_threshold == 0) {
4884 CERROR("%s: Dropping REPLY from %s for inactive MD %p\n",
4885 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id),
4887 lnet_res_unlock(cpt);
4891 LASSERT(getmd->md_offset == 0);
4893 CDEBUG(D_NET, "%s: Reply from %s md %p\n",
4894 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id), getmd);
4896 /* setup information for lnet_build_msg_event */
4897 msg->msg_initiator = getmsg->msg_txpeer->lpni_peer_net->lpn_peer->lp_primary_nid;
4898 msg->msg_from = peer_id.nid;
4899 msg->msg_type = LNET_MSG_GET; /* flag this msg as an "optimized" GET */
4900 msg->msg_hdr.src_nid = peer_id.nid;
4901 msg->msg_hdr.payload_length = getmd->md_length;
4902 msg->msg_receiving = 1; /* required by lnet_msg_attach_md */
4904 lnet_msg_attach_md(msg, getmd, getmd->md_offset, getmd->md_length);
4905 lnet_res_unlock(cpt);
4907 cpt = lnet_cpt_of_nid(peer_id.nid, ni);
4910 lnet_msg_commit(msg, cpt);
4911 lnet_net_unlock(cpt);
4913 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
4918 cpt = lnet_cpt_of_nid(peer_id.nid, ni);
4921 lnet_incr_stats(&ni->ni_stats, LNET_MSG_GET, LNET_STATS_TYPE_DROP);
4922 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_count++;
4923 the_lnet.ln_counters[cpt]->lct_common.lcc_drop_length +=
4925 lnet_net_unlock(cpt);
4932 EXPORT_SYMBOL(lnet_create_reply_msg);
4935 lnet_set_reply_msg_len(struct lnet_ni *ni, struct lnet_msg *reply,
4938 /* Set the REPLY length, now the RDMA that elides the REPLY message has
4939 * completed and I know it. */
4940 LASSERT(reply != NULL);
4941 LASSERT(reply->msg_type == LNET_MSG_GET);
4942 LASSERT(reply->msg_ev.type == LNET_EVENT_REPLY);
4944 /* NB I trusted my peer to RDMA. If she tells me she's written beyond
4945 * the end of my buffer, I might as well be dead. */
4946 LASSERT(len <= reply->msg_ev.mlength);
4948 reply->msg_ev.mlength = len;
4950 EXPORT_SYMBOL(lnet_set_reply_msg_len);
4953 * Initiate an asynchronous GET operation.
4955 * On the initiator node, an LNET_EVENT_SEND is logged when the GET request
4956 * is sent, and an LNET_EVENT_REPLY is logged when the data returned from
4957 * the target node in the REPLY has been written to local MD.
4959 * On the target node, an LNET_EVENT_GET is logged when the GET request
4960 * arrives and is accepted into a MD.
4962 * \param self,target,portal,match_bits,offset See the discussion in LNetPut().
4963 * \param mdh A handle for the MD that describes the memory into which the
4964 * requested data will be received. The MD must be "free floating" (See LNetMDBind()).
4966 * \retval 0 Success, and only in this case events will be generated
4967 * and logged to EQ (if it exists) of the MD.
4968 * \retval -EIO Simulated failure.
4969 * \retval -ENOMEM Memory allocation failure.
4970 * \retval -ENOENT Invalid MD object.
4973 LNetGet(lnet_nid_t self, struct lnet_handle_md mdh,
4974 struct lnet_process_id target, unsigned int portal,
4975 __u64 match_bits, unsigned int offset, bool recovery)
4977 struct lnet_msg *msg;
4978 struct lnet_libmd *md;
4979 struct lnet_rsp_tracker *rspt;
4983 LASSERT(the_lnet.ln_refcount > 0);
4985 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
4986 fail_peer(target.nid, 1)) /* shall we now? */
4988 CERROR("Dropping GET to %s: simulated failure\n",
4989 libcfs_id2str(target));
4993 msg = lnet_msg_alloc();
4995 CERROR("Dropping GET to %s: ENOMEM on struct lnet_msg\n",
4996 libcfs_id2str(target));
5000 cpt = lnet_cpt_of_cookie(mdh.cookie);
5002 rspt = lnet_rspt_alloc(cpt);
5004 CERROR("Dropping GET to %s: ENOMEM on response tracker\n",
5005 libcfs_id2str(target));
5008 INIT_LIST_HEAD(&rspt->rspt_on_list);
5010 msg->msg_recovery = recovery;
5014 md = lnet_handle2md(&mdh);
5015 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
5016 CERROR("Dropping GET (%llu:%d:%s): MD (%d) invalid\n",
5017 match_bits, portal, libcfs_id2str(target),
5018 md == NULL ? -1 : md->md_threshold);
5019 if (md != NULL && md->md_me != NULL)
5020 CERROR("REPLY MD also attached to portal %d\n",
5021 md->md_me->me_portal);
5023 lnet_res_unlock(cpt);
5026 lnet_rspt_free(rspt, cpt);
5030 CDEBUG(D_NET, "LNetGet -> %s\n", libcfs_id2str(target));
5032 lnet_msg_attach_md(msg, md, 0, 0);
5034 lnet_prep_send(msg, LNET_MSG_GET, target, 0, 0);
5036 msg->msg_hdr.msg.get.match_bits = cpu_to_le64(match_bits);
5037 msg->msg_hdr.msg.get.ptl_index = cpu_to_le32(portal);
5038 msg->msg_hdr.msg.get.src_offset = cpu_to_le32(offset);
5039 msg->msg_hdr.msg.get.sink_length = cpu_to_le32(md->md_length);
5041 /* NB handles only looked up by creator (no flips) */
5042 msg->msg_hdr.msg.get.return_wmd.wh_interface_cookie =
5043 the_lnet.ln_interface_cookie;
5044 msg->msg_hdr.msg.get.return_wmd.wh_object_cookie =
5045 md->md_lh.lh_cookie;
5047 lnet_res_unlock(cpt);
5049 lnet_build_msg_event(msg, LNET_EVENT_SEND);
5051 lnet_attach_rsp_tracker(rspt, cpt, md, mdh);
5053 rc = lnet_send(self, msg, LNET_NID_ANY);
5055 CNETERR("Error sending GET to %s: %d\n",
5056 libcfs_id2str(target), rc);
5057 msg->msg_no_resend = true;
5058 lnet_finalize(msg, rc);
5061 /* completion will be signalled by an event */
5064 EXPORT_SYMBOL(LNetGet);
5067 * Calculate distance to node at \a dstnid.
5069 * \param dstnid Target NID.
5070 * \param srcnidp If not NULL, NID of the local interface to reach \a dstnid
5072 * \param orderp If not NULL, order of the route to reach \a dstnid is saved
5075 * \retval 0 If \a dstnid belongs to a local interface, and reserved option
5076 * local_nid_dist_zero is set, which is the default.
5077 * \retval positives Distance to target NID, i.e. number of hops plus one.
5078 * \retval -EHOSTUNREACH If \a dstnid is not reachable.
5081 LNetDist(lnet_nid_t dstnid, lnet_nid_t *srcnidp, __u32 *orderp)
5083 struct list_head *e;
5084 struct lnet_ni *ni = NULL;
5085 struct lnet_remotenet *rnet;
5086 __u32 dstnet = LNET_NIDNET(dstnid);
5090 struct list_head *rn_list;
5092 /* if !local_nid_dist_zero, I don't return a distance of 0 ever
5093 * (when lustre sees a distance of 0, it substitutes 0@lo), so I
5094 * keep order 0 free for 0@lo and order 1 free for a local NID
5097 LASSERT(the_lnet.ln_refcount > 0);
5099 cpt = lnet_net_lock_current();
5101 while ((ni = lnet_get_next_ni_locked(NULL, ni))) {
5102 if (ni->ni_nid == dstnid) {
5103 if (srcnidp != NULL)
5105 if (orderp != NULL) {
5106 if (dstnid == LNET_NID_LO_0)
5111 lnet_net_unlock(cpt);
5113 return local_nid_dist_zero ? 0 : 1;
5116 if (LNET_NIDNET(ni->ni_nid) == dstnet) {
5117 /* Check if ni was originally created in
5118 * current net namespace.
5119 * If not, assign order above 0xffff0000,
5120 * to make this ni not a priority. */
5121 if (current->nsproxy &&
5122 !net_eq(ni->ni_net_ns, current->nsproxy->net_ns))
5123 order += 0xffff0000;
5124 if (srcnidp != NULL)
5125 *srcnidp = ni->ni_nid;
5128 lnet_net_unlock(cpt);
5135 rn_list = lnet_net2rnethash(dstnet);
5136 list_for_each(e, rn_list) {
5137 rnet = list_entry(e, struct lnet_remotenet, lrn_list);
5139 if (rnet->lrn_net == dstnet) {
5140 struct lnet_route *route;
5141 struct lnet_route *shortest = NULL;
5142 __u32 shortest_hops = LNET_UNDEFINED_HOPS;
5145 LASSERT(!list_empty(&rnet->lrn_routes));
5147 list_for_each_entry(route, &rnet->lrn_routes,
5149 route_hops = route->lr_hops;
5150 if (route_hops == LNET_UNDEFINED_HOPS)
5152 if (shortest == NULL ||
5153 route_hops < shortest_hops) {
5155 shortest_hops = route_hops;
5159 LASSERT(shortest != NULL);
5160 hops = shortest_hops;
5161 if (srcnidp != NULL) {
5162 struct lnet_net *net;
5163 net = lnet_get_net_locked(shortest->lr_lnet);
5165 ni = lnet_get_next_ni_locked(net, NULL);
5166 *srcnidp = ni->ni_nid;
5170 lnet_net_unlock(cpt);
5176 lnet_net_unlock(cpt);
5177 return -EHOSTUNREACH;
5179 EXPORT_SYMBOL(LNetDist);