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, 2016, 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 <lnet/lib-lnet.h>
40 #include <linux/nsproxy.h>
41 #include <net/net_namespace.h>
43 static int local_nid_dist_zero = 1;
44 module_param(local_nid_dist_zero, int, 0444);
45 MODULE_PARM_DESC(local_nid_dist_zero, "Reserved");
48 lnet_fail_nid(lnet_nid_t nid, unsigned int threshold)
50 struct lnet_test_peer *tp;
52 struct list_head *next;
53 struct list_head cull;
55 /* NB: use lnet_net_lock(0) to serialize operations on test peers */
57 /* Adding a new entry */
58 LIBCFS_ALLOC(tp, sizeof(*tp));
63 tp->tp_threshold = threshold;
66 list_add_tail(&tp->tp_list, &the_lnet.ln_test_peers);
71 /* removing entries */
72 INIT_LIST_HEAD(&cull);
76 list_for_each_safe(el, next, &the_lnet.ln_test_peers) {
77 tp = list_entry(el, struct lnet_test_peer, tp_list);
79 if (tp->tp_threshold == 0 || /* needs culling anyway */
80 nid == LNET_NID_ANY || /* removing all entries */
81 tp->tp_nid == nid) { /* matched this one */
82 list_del(&tp->tp_list);
83 list_add(&tp->tp_list, &cull);
89 while (!list_empty(&cull)) {
90 tp = list_entry(cull.next, struct lnet_test_peer, tp_list);
92 list_del(&tp->tp_list);
93 LIBCFS_FREE(tp, sizeof(*tp));
99 fail_peer (lnet_nid_t nid, int outgoing)
101 struct lnet_test_peer *tp;
102 struct list_head *el;
103 struct list_head *next;
104 struct list_head cull;
107 INIT_LIST_HEAD(&cull);
109 /* NB: use lnet_net_lock(0) to serialize operations on test peers */
112 list_for_each_safe(el, next, &the_lnet.ln_test_peers) {
113 tp = list_entry(el, struct lnet_test_peer, tp_list);
115 if (tp->tp_threshold == 0) {
118 /* only cull zombies on outgoing tests,
119 * since we may be at interrupt priority on
120 * incoming messages. */
121 list_del(&tp->tp_list);
122 list_add(&tp->tp_list, &cull);
127 if (tp->tp_nid == LNET_NID_ANY || /* fail every peer */
128 nid == tp->tp_nid) { /* fail this peer */
131 if (tp->tp_threshold != LNET_MD_THRESH_INF) {
134 tp->tp_threshold == 0) {
136 list_del(&tp->tp_list);
137 list_add(&tp->tp_list, &cull);
146 while (!list_empty(&cull)) {
147 tp = list_entry(cull.next, struct lnet_test_peer, tp_list);
148 list_del(&tp->tp_list);
150 LIBCFS_FREE(tp, sizeof(*tp));
157 lnet_iov_nob(unsigned int niov, struct kvec *iov)
159 unsigned int nob = 0;
161 LASSERT(niov == 0 || iov != NULL);
163 nob += (iov++)->iov_len;
167 EXPORT_SYMBOL(lnet_iov_nob);
170 lnet_copy_iov2iov(unsigned int ndiov, struct kvec *diov, unsigned int doffset,
171 unsigned int nsiov, struct kvec *siov, unsigned int soffset,
174 /* NB diov, siov are READ-ONLY */
175 unsigned int this_nob;
180 /* skip complete frags before 'doffset' */
182 while (doffset >= diov->iov_len) {
183 doffset -= diov->iov_len;
189 /* skip complete frags before 'soffset' */
191 while (soffset >= siov->iov_len) {
192 soffset -= siov->iov_len;
201 this_nob = MIN(diov->iov_len - doffset,
202 siov->iov_len - soffset);
203 this_nob = MIN(this_nob, nob);
205 memcpy((char *)diov->iov_base + doffset,
206 (char *)siov->iov_base + soffset, this_nob);
209 if (diov->iov_len > doffset + this_nob) {
217 if (siov->iov_len > soffset + this_nob) {
226 EXPORT_SYMBOL(lnet_copy_iov2iov);
229 lnet_extract_iov(int dst_niov, struct kvec *dst,
230 int src_niov, struct kvec *src,
231 unsigned int offset, unsigned int len)
233 /* Initialise 'dst' to the subset of 'src' starting at 'offset',
234 * for exactly 'len' bytes, and return the number of entries.
235 * NB not destructive to 'src' */
236 unsigned int frag_len;
239 if (len == 0) /* no data => */
240 return (0); /* no frags */
242 LASSERT(src_niov > 0);
243 while (offset >= src->iov_len) { /* skip initial frags */
244 offset -= src->iov_len;
247 LASSERT(src_niov > 0);
252 LASSERT(src_niov > 0);
253 LASSERT((int)niov <= dst_niov);
255 frag_len = src->iov_len - offset;
256 dst->iov_base = ((char *)src->iov_base) + offset;
258 if (len <= frag_len) {
263 dst->iov_len = frag_len;
273 EXPORT_SYMBOL(lnet_extract_iov);
277 lnet_kiov_nob(unsigned int niov, lnet_kiov_t *kiov)
279 unsigned int nob = 0;
281 LASSERT(niov == 0 || kiov != NULL);
283 nob += (kiov++)->kiov_len;
287 EXPORT_SYMBOL(lnet_kiov_nob);
290 lnet_copy_kiov2kiov(unsigned int ndiov, lnet_kiov_t *diov, unsigned int doffset,
291 unsigned int nsiov, lnet_kiov_t *siov, unsigned int soffset,
294 /* NB diov, siov are READ-ONLY */
295 unsigned int this_nob;
302 LASSERT (!in_interrupt ());
305 while (doffset >= diov->kiov_len) {
306 doffset -= diov->kiov_len;
313 while (soffset >= siov->kiov_len) {
314 soffset -= siov->kiov_len;
323 this_nob = MIN(diov->kiov_len - doffset,
324 siov->kiov_len - soffset);
325 this_nob = MIN(this_nob, nob);
328 daddr = ((char *)kmap(diov->kiov_page)) +
329 diov->kiov_offset + doffset;
331 saddr = ((char *)kmap(siov->kiov_page)) +
332 siov->kiov_offset + soffset;
334 /* Vanishing risk of kmap deadlock when mapping 2 pages.
335 * However in practice at least one of the kiovs will be mapped
336 * kernel pages and the map/unmap will be NOOPs */
338 memcpy (daddr, saddr, this_nob);
341 if (diov->kiov_len > doffset + this_nob) {
345 kunmap(diov->kiov_page);
352 if (siov->kiov_len > soffset + this_nob) {
356 kunmap(siov->kiov_page);
365 kunmap(diov->kiov_page);
367 kunmap(siov->kiov_page);
369 EXPORT_SYMBOL(lnet_copy_kiov2kiov);
372 lnet_copy_kiov2iov (unsigned int niov, struct kvec *iov, unsigned int iovoffset,
373 unsigned int nkiov, lnet_kiov_t *kiov, unsigned int kiovoffset,
376 /* NB iov, kiov are READ-ONLY */
377 unsigned int this_nob;
383 LASSERT (!in_interrupt ());
386 while (iovoffset >= iov->iov_len) {
387 iovoffset -= iov->iov_len;
394 while (kiovoffset >= kiov->kiov_len) {
395 kiovoffset -= kiov->kiov_len;
404 this_nob = MIN(iov->iov_len - iovoffset,
405 kiov->kiov_len - kiovoffset);
406 this_nob = MIN(this_nob, nob);
409 addr = ((char *)kmap(kiov->kiov_page)) +
410 kiov->kiov_offset + kiovoffset;
412 memcpy((char *)iov->iov_base + iovoffset, addr, this_nob);
415 if (iov->iov_len > iovoffset + this_nob) {
416 iovoffset += this_nob;
423 if (kiov->kiov_len > kiovoffset + this_nob) {
425 kiovoffset += this_nob;
427 kunmap(kiov->kiov_page);
437 kunmap(kiov->kiov_page);
439 EXPORT_SYMBOL(lnet_copy_kiov2iov);
442 lnet_copy_iov2kiov(unsigned int nkiov, lnet_kiov_t *kiov, unsigned int kiovoffset,
443 unsigned int niov, struct kvec *iov, unsigned int iovoffset,
446 /* NB kiov, iov are READ-ONLY */
447 unsigned int this_nob;
453 LASSERT (!in_interrupt ());
456 while (kiovoffset >= kiov->kiov_len) {
457 kiovoffset -= kiov->kiov_len;
464 while (iovoffset >= iov->iov_len) {
465 iovoffset -= iov->iov_len;
474 this_nob = MIN(kiov->kiov_len - kiovoffset,
475 iov->iov_len - iovoffset);
476 this_nob = MIN(this_nob, nob);
479 addr = ((char *)kmap(kiov->kiov_page)) +
480 kiov->kiov_offset + kiovoffset;
482 memcpy (addr, (char *)iov->iov_base + iovoffset, this_nob);
485 if (kiov->kiov_len > kiovoffset + this_nob) {
487 kiovoffset += this_nob;
489 kunmap(kiov->kiov_page);
496 if (iov->iov_len > iovoffset + this_nob) {
497 iovoffset += this_nob;
506 kunmap(kiov->kiov_page);
508 EXPORT_SYMBOL(lnet_copy_iov2kiov);
511 lnet_extract_kiov(int dst_niov, lnet_kiov_t *dst,
512 int src_niov, lnet_kiov_t *src,
513 unsigned int offset, unsigned int len)
515 /* Initialise 'dst' to the subset of 'src' starting at 'offset',
516 * for exactly 'len' bytes, and return the number of entries.
517 * NB not destructive to 'src' */
518 unsigned int frag_len;
521 if (len == 0) /* no data => */
522 return (0); /* no frags */
524 LASSERT(src_niov > 0);
525 while (offset >= src->kiov_len) { /* skip initial frags */
526 offset -= src->kiov_len;
529 LASSERT(src_niov > 0);
534 LASSERT(src_niov > 0);
535 LASSERT((int)niov <= dst_niov);
537 frag_len = src->kiov_len - offset;
538 dst->kiov_page = src->kiov_page;
539 dst->kiov_offset = src->kiov_offset + offset;
541 if (len <= frag_len) {
543 LASSERT(dst->kiov_offset + dst->kiov_len <= PAGE_SIZE);
547 dst->kiov_len = frag_len;
548 LASSERT(dst->kiov_offset + dst->kiov_len <= PAGE_SIZE);
558 EXPORT_SYMBOL(lnet_extract_kiov);
561 lnet_ni_recv(struct lnet_ni *ni, void *private, struct lnet_msg *msg,
562 int delayed, unsigned int offset, unsigned int mlen,
565 unsigned int niov = 0;
566 struct kvec *iov = NULL;
567 lnet_kiov_t *kiov = NULL;
570 LASSERT (!in_interrupt ());
571 LASSERT (mlen == 0 || msg != NULL);
574 LASSERT(msg->msg_receiving);
575 LASSERT(!msg->msg_sending);
576 LASSERT(rlen == msg->msg_len);
577 LASSERT(mlen <= msg->msg_len);
578 LASSERT(msg->msg_offset == offset);
579 LASSERT(msg->msg_wanted == mlen);
581 msg->msg_receiving = 0;
584 niov = msg->msg_niov;
586 kiov = msg->msg_kiov;
589 LASSERT ((iov == NULL) != (kiov == NULL));
593 rc = (ni->ni_net->net_lnd->lnd_recv)(ni, private, msg, delayed,
594 niov, iov, kiov, offset, mlen,
597 lnet_finalize(msg, rc);
601 lnet_setpayloadbuffer(struct lnet_msg *msg)
603 struct lnet_libmd *md = msg->msg_md;
605 LASSERT(msg->msg_len > 0);
606 LASSERT(!msg->msg_routing);
608 LASSERT(msg->msg_niov == 0);
609 LASSERT(msg->msg_iov == NULL);
610 LASSERT(msg->msg_kiov == NULL);
612 msg->msg_niov = md->md_niov;
613 if ((md->md_options & LNET_MD_KIOV) != 0)
614 msg->msg_kiov = md->md_iov.kiov;
616 msg->msg_iov = md->md_iov.iov;
620 lnet_prep_send(struct lnet_msg *msg, int type, struct lnet_process_id target,
621 unsigned int offset, unsigned int len)
623 msg->msg_type = type;
624 msg->msg_target = target;
626 msg->msg_offset = offset;
629 lnet_setpayloadbuffer(msg);
631 memset (&msg->msg_hdr, 0, sizeof (msg->msg_hdr));
632 msg->msg_hdr.type = cpu_to_le32(type);
633 msg->msg_hdr.dest_pid = cpu_to_le32(target.pid);
634 /* src_nid will be set later */
635 msg->msg_hdr.src_pid = cpu_to_le32(the_lnet.ln_pid);
636 msg->msg_hdr.payload_length = cpu_to_le32(len);
640 lnet_ni_send(struct lnet_ni *ni, struct lnet_msg *msg)
642 void *priv = msg->msg_private;
645 LASSERT (!in_interrupt ());
646 LASSERT (LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND ||
647 (msg->msg_txcredit && msg->msg_peertxcredit));
649 rc = (ni->ni_net->net_lnd->lnd_send)(ni, priv, msg);
651 lnet_finalize(msg, rc);
655 lnet_ni_eager_recv(struct lnet_ni *ni, struct lnet_msg *msg)
659 LASSERT(!msg->msg_sending);
660 LASSERT(msg->msg_receiving);
661 LASSERT(!msg->msg_rx_ready_delay);
662 LASSERT(ni->ni_net->net_lnd->lnd_eager_recv != NULL);
664 msg->msg_rx_ready_delay = 1;
665 rc = (ni->ni_net->net_lnd->lnd_eager_recv)(ni, msg->msg_private, msg,
668 CERROR("recv from %s / send to %s aborted: "
669 "eager_recv failed %d\n",
670 libcfs_nid2str(msg->msg_rxpeer->lpni_nid),
671 libcfs_id2str(msg->msg_target), rc);
672 LASSERT(rc < 0); /* required by my callers */
679 * This function can be called from two paths:
680 * 1. when sending a message
681 * 2. when decommiting a message (lnet_msg_decommit_tx())
682 * In both these cases the peer_ni should have it's reference count
683 * acquired by the caller and therefore it is safe to drop the spin
684 * lock before calling lnd_query()
687 lnet_ni_query_locked(struct lnet_ni *ni, struct lnet_peer_ni *lp)
689 cfs_time_t last_alive = 0;
690 int cpt = lnet_cpt_of_nid_locked(lp->lpni_nid, ni);
692 LASSERT(lnet_peer_aliveness_enabled(lp));
693 LASSERT(ni->ni_net->net_lnd->lnd_query != NULL);
695 lnet_net_unlock(cpt);
696 (ni->ni_net->net_lnd->lnd_query)(ni, lp->lpni_nid, &last_alive);
699 lp->lpni_last_query = cfs_time_current();
701 if (last_alive != 0) /* NI has updated timestamp */
702 lp->lpni_last_alive = last_alive;
705 /* NB: always called with lnet_net_lock held */
707 lnet_peer_is_alive (struct lnet_peer_ni *lp, cfs_time_t now)
712 LASSERT (lnet_peer_aliveness_enabled(lp));
715 * Trust lnet_notify() if it has more recent aliveness news, but
716 * ignore the initial assumed death (see lnet_peers_start_down()).
718 spin_lock(&lp->lpni_lock);
719 if (!lp->lpni_alive && lp->lpni_alive_count > 0 &&
720 cfs_time_aftereq(lp->lpni_timestamp, lp->lpni_last_alive)) {
721 spin_unlock(&lp->lpni_lock);
726 cfs_time_add(lp->lpni_last_alive,
727 cfs_time_seconds(lp->lpni_net->net_tunables.
729 alive = cfs_time_after(deadline, now);
732 * Update obsolete lp_alive except for routers assumed to be dead
733 * initially, because router checker would update aliveness in this
734 * case, and moreover lpni_last_alive at peer creation is assumed.
736 if (alive && !lp->lpni_alive &&
737 !(lnet_isrouter(lp) && lp->lpni_alive_count == 0)) {
738 spin_unlock(&lp->lpni_lock);
739 lnet_notify_locked(lp, 0, 1, lp->lpni_last_alive);
741 spin_unlock(&lp->lpni_lock);
748 /* NB: returns 1 when alive, 0 when dead, negative when error;
749 * may drop the lnet_net_lock */
751 lnet_peer_alive_locked (struct lnet_ni *ni, struct lnet_peer_ni *lp)
753 cfs_time_t now = cfs_time_current();
755 if (!lnet_peer_aliveness_enabled(lp))
758 if (lnet_peer_is_alive(lp, now))
762 * Peer appears dead, but we should avoid frequent NI queries (at
763 * most once per lnet_queryinterval seconds).
765 if (lp->lpni_last_query != 0) {
766 static const int lnet_queryinterval = 1;
768 cfs_time_t next_query =
769 cfs_time_add(lp->lpni_last_query,
770 cfs_time_seconds(lnet_queryinterval));
772 if (cfs_time_before(now, next_query)) {
774 CWARN("Unexpected aliveness of peer %s: "
776 libcfs_nid2str(lp->lpni_nid),
777 (int)now, (int)next_query,
779 lp->lpni_net->net_tunables.lct_peer_timeout);
784 /* query NI for latest aliveness news */
785 lnet_ni_query_locked(ni, lp);
787 if (lnet_peer_is_alive(lp, now))
790 lnet_notify_locked(lp, 0, 0, lp->lpni_last_alive);
795 * \param msg The message to be sent.
796 * \param do_send True if lnet_ni_send() should be called in this function.
797 * lnet_send() is going to lnet_net_unlock immediately after this, so
798 * it sets do_send FALSE and I don't do the unlock/send/lock bit.
800 * \retval LNET_CREDIT_OK If \a msg sent or OK to send.
801 * \retval LNET_CREDIT_WAIT If \a msg blocked for credit.
802 * \retval -EHOSTUNREACH If the next hop of the message appears dead.
803 * \retval -ECANCELED If the MD of the message has been unlinked.
806 lnet_post_send_locked(struct lnet_msg *msg, int do_send)
808 struct lnet_peer_ni *lp = msg->msg_txpeer;
809 struct lnet_ni *ni = msg->msg_txni;
810 int cpt = msg->msg_tx_cpt;
811 struct lnet_tx_queue *tq = ni->ni_tx_queues[cpt];
813 /* non-lnet_send() callers have checked before */
814 LASSERT(!do_send || msg->msg_tx_delayed);
815 LASSERT(!msg->msg_receiving);
816 LASSERT(msg->msg_tx_committed);
818 /* NB 'lp' is always the next hop */
819 if ((msg->msg_target.pid & LNET_PID_USERFLAG) == 0 &&
820 lnet_peer_alive_locked(ni, lp) == 0) {
821 the_lnet.ln_counters[cpt]->drop_count++;
822 the_lnet.ln_counters[cpt]->drop_length += msg->msg_len;
823 lnet_net_unlock(cpt);
825 atomic_inc(&msg->msg_txpeer->lpni_stats.drop_count);
827 atomic_inc(&msg->msg_txni->ni_stats.drop_count);
829 CNETERR("Dropping message for %s: peer not alive\n",
830 libcfs_id2str(msg->msg_target));
832 lnet_finalize(msg, -EHOSTUNREACH);
835 return -EHOSTUNREACH;
838 if (msg->msg_md != NULL &&
839 (msg->msg_md->md_flags & LNET_MD_FLAG_ABORTED) != 0) {
840 lnet_net_unlock(cpt);
842 CNETERR("Aborting message for %s: LNetM[DE]Unlink() already "
843 "called on the MD/ME.\n",
844 libcfs_id2str(msg->msg_target));
846 lnet_finalize(msg, -ECANCELED);
852 if (!msg->msg_peertxcredit) {
853 spin_lock(&lp->lpni_lock);
854 LASSERT((lp->lpni_txcredits < 0) ==
855 !list_empty(&lp->lpni_txq));
857 msg->msg_peertxcredit = 1;
858 lp->lpni_txqnob += msg->msg_len + sizeof(struct lnet_hdr);
859 lp->lpni_txcredits--;
861 if (lp->lpni_txcredits < lp->lpni_mintxcredits)
862 lp->lpni_mintxcredits = lp->lpni_txcredits;
864 if (lp->lpni_txcredits < 0) {
865 msg->msg_tx_delayed = 1;
866 list_add_tail(&msg->msg_list, &lp->lpni_txq);
867 spin_unlock(&lp->lpni_lock);
868 return LNET_CREDIT_WAIT;
870 spin_unlock(&lp->lpni_lock);
873 if (!msg->msg_txcredit) {
874 LASSERT((tq->tq_credits < 0) ==
875 !list_empty(&tq->tq_delayed));
877 msg->msg_txcredit = 1;
879 atomic_dec(&ni->ni_tx_credits);
881 if (tq->tq_credits < tq->tq_credits_min)
882 tq->tq_credits_min = tq->tq_credits;
884 if (tq->tq_credits < 0) {
885 msg->msg_tx_delayed = 1;
886 list_add_tail(&msg->msg_list, &tq->tq_delayed);
887 return LNET_CREDIT_WAIT;
892 lnet_net_unlock(cpt);
893 lnet_ni_send(ni, msg);
896 return LNET_CREDIT_OK;
900 static struct lnet_rtrbufpool *
901 lnet_msg2bufpool(struct lnet_msg *msg)
903 struct lnet_rtrbufpool *rbp;
906 LASSERT(msg->msg_rx_committed);
908 cpt = msg->msg_rx_cpt;
909 rbp = &the_lnet.ln_rtrpools[cpt][0];
911 LASSERT(msg->msg_len <= LNET_MTU);
912 while (msg->msg_len > (unsigned int)rbp->rbp_npages * PAGE_SIZE) {
914 LASSERT(rbp < &the_lnet.ln_rtrpools[cpt][LNET_NRBPOOLS]);
921 lnet_post_routed_recv_locked(struct lnet_msg *msg, int do_recv)
923 /* lnet_parse is going to lnet_net_unlock immediately after this, so it
924 * sets do_recv FALSE and I don't do the unlock/send/lock bit.
925 * I return LNET_CREDIT_WAIT if msg blocked and LNET_CREDIT_OK if
926 * received or OK to receive */
927 struct lnet_peer_ni *lp = msg->msg_rxpeer;
928 struct lnet_rtrbufpool *rbp;
929 struct lnet_rtrbuf *rb;
931 LASSERT (msg->msg_iov == NULL);
932 LASSERT (msg->msg_kiov == NULL);
933 LASSERT (msg->msg_niov == 0);
934 LASSERT (msg->msg_routing);
935 LASSERT (msg->msg_receiving);
936 LASSERT (!msg->msg_sending);
938 /* non-lnet_parse callers only receive delayed messages */
939 LASSERT(!do_recv || msg->msg_rx_delayed);
941 if (!msg->msg_peerrtrcredit) {
942 spin_lock(&lp->lpni_lock);
943 LASSERT((lp->lpni_rtrcredits < 0) ==
944 !list_empty(&lp->lpni_rtrq));
946 msg->msg_peerrtrcredit = 1;
947 lp->lpni_rtrcredits--;
948 if (lp->lpni_rtrcredits < lp->lpni_minrtrcredits)
949 lp->lpni_minrtrcredits = lp->lpni_rtrcredits;
951 if (lp->lpni_rtrcredits < 0) {
952 /* must have checked eager_recv before here */
953 LASSERT(msg->msg_rx_ready_delay);
954 msg->msg_rx_delayed = 1;
955 list_add_tail(&msg->msg_list, &lp->lpni_rtrq);
956 spin_unlock(&lp->lpni_lock);
957 return LNET_CREDIT_WAIT;
959 spin_unlock(&lp->lpni_lock);
962 rbp = lnet_msg2bufpool(msg);
964 if (!msg->msg_rtrcredit) {
965 msg->msg_rtrcredit = 1;
967 if (rbp->rbp_credits < rbp->rbp_mincredits)
968 rbp->rbp_mincredits = rbp->rbp_credits;
970 if (rbp->rbp_credits < 0) {
971 /* must have checked eager_recv before here */
972 LASSERT(msg->msg_rx_ready_delay);
973 msg->msg_rx_delayed = 1;
974 list_add_tail(&msg->msg_list, &rbp->rbp_msgs);
975 return LNET_CREDIT_WAIT;
979 LASSERT(!list_empty(&rbp->rbp_bufs));
980 rb = list_entry(rbp->rbp_bufs.next, struct lnet_rtrbuf, rb_list);
981 list_del(&rb->rb_list);
983 msg->msg_niov = rbp->rbp_npages;
984 msg->msg_kiov = &rb->rb_kiov[0];
987 int cpt = msg->msg_rx_cpt;
989 lnet_net_unlock(cpt);
990 lnet_ni_recv(msg->msg_rxni, msg->msg_private, msg, 1,
991 0, msg->msg_len, msg->msg_len);
994 return LNET_CREDIT_OK;
998 lnet_return_tx_credits_locked(struct lnet_msg *msg)
1000 struct lnet_peer_ni *txpeer = msg->msg_txpeer;
1001 struct lnet_ni *txni = msg->msg_txni;
1002 struct lnet_msg *msg2;
1004 if (msg->msg_txcredit) {
1005 struct lnet_ni *ni = msg->msg_txni;
1006 struct lnet_tx_queue *tq = ni->ni_tx_queues[msg->msg_tx_cpt];
1008 /* give back NI txcredits */
1009 msg->msg_txcredit = 0;
1011 LASSERT((tq->tq_credits < 0) ==
1012 !list_empty(&tq->tq_delayed));
1015 atomic_inc(&ni->ni_tx_credits);
1016 if (tq->tq_credits <= 0) {
1017 msg2 = list_entry(tq->tq_delayed.next,
1018 struct lnet_msg, msg_list);
1019 list_del(&msg2->msg_list);
1021 LASSERT(msg2->msg_txni == ni);
1022 LASSERT(msg2->msg_tx_delayed);
1023 LASSERT(msg2->msg_tx_cpt == msg->msg_tx_cpt);
1025 (void) lnet_post_send_locked(msg2, 1);
1029 if (msg->msg_peertxcredit) {
1030 /* give back peer txcredits */
1031 msg->msg_peertxcredit = 0;
1033 spin_lock(&txpeer->lpni_lock);
1034 LASSERT((txpeer->lpni_txcredits < 0) ==
1035 !list_empty(&txpeer->lpni_txq));
1037 txpeer->lpni_txqnob -= msg->msg_len + sizeof(struct lnet_hdr);
1038 LASSERT(txpeer->lpni_txqnob >= 0);
1040 txpeer->lpni_txcredits++;
1041 if (txpeer->lpni_txcredits <= 0) {
1044 msg2 = list_entry(txpeer->lpni_txq.next,
1045 struct lnet_msg, msg_list);
1046 list_del(&msg2->msg_list);
1047 spin_unlock(&txpeer->lpni_lock);
1049 LASSERT(msg2->msg_txpeer == txpeer);
1050 LASSERT(msg2->msg_tx_delayed);
1052 msg2_cpt = msg2->msg_tx_cpt;
1055 * The msg_cpt can be different from the msg2_cpt
1056 * so we need to make sure we lock the correct cpt
1058 * Once we call lnet_post_send_locked() it is no
1059 * longer safe to access msg2, since it could've
1060 * been freed by lnet_finalize(), but we still
1061 * need to relock the correct cpt, so we cache the
1062 * msg2_cpt for the purpose of the check that
1063 * follows the call to lnet_pose_send_locked().
1065 if (msg2_cpt != msg->msg_tx_cpt) {
1066 lnet_net_unlock(msg->msg_tx_cpt);
1067 lnet_net_lock(msg2_cpt);
1069 (void) lnet_post_send_locked(msg2, 1);
1070 if (msg2_cpt != msg->msg_tx_cpt) {
1071 lnet_net_unlock(msg2_cpt);
1072 lnet_net_lock(msg->msg_tx_cpt);
1075 spin_unlock(&txpeer->lpni_lock);
1080 msg->msg_txni = NULL;
1081 lnet_ni_decref_locked(txni, msg->msg_tx_cpt);
1084 if (txpeer != NULL) {
1087 * Once the patch for the health comes in we need to set
1088 * the health of the peer ni to bad when we fail to send
1090 * int status = msg->msg_ev.status;
1092 * lnet_set_peer_ni_health_locked(txpeer, false)
1094 msg->msg_txpeer = NULL;
1095 lnet_peer_ni_decref_locked(txpeer);
1100 lnet_schedule_blocked_locked(struct lnet_rtrbufpool *rbp)
1102 struct lnet_msg *msg;
1104 if (list_empty(&rbp->rbp_msgs))
1106 msg = list_entry(rbp->rbp_msgs.next,
1107 struct lnet_msg, msg_list);
1108 list_del(&msg->msg_list);
1110 (void)lnet_post_routed_recv_locked(msg, 1);
1114 lnet_drop_routed_msgs_locked(struct list_head *list, int cpt)
1116 struct lnet_msg *msg;
1117 struct lnet_msg *tmp;
1119 lnet_net_unlock(cpt);
1121 list_for_each_entry_safe(msg, tmp, list, msg_list) {
1122 lnet_ni_recv(msg->msg_rxni, msg->msg_private, NULL,
1123 0, 0, 0, msg->msg_hdr.payload_length);
1124 list_del_init(&msg->msg_list);
1125 lnet_finalize(msg, -ECANCELED);
1132 lnet_return_rx_credits_locked(struct lnet_msg *msg)
1134 struct lnet_peer_ni *rxpeer = msg->msg_rxpeer;
1135 struct lnet_ni *rxni = msg->msg_rxni;
1136 struct lnet_msg *msg2;
1138 if (msg->msg_rtrcredit) {
1139 /* give back global router credits */
1140 struct lnet_rtrbuf *rb;
1141 struct lnet_rtrbufpool *rbp;
1143 /* NB If a msg ever blocks for a buffer in rbp_msgs, it stays
1144 * there until it gets one allocated, or aborts the wait
1146 LASSERT(msg->msg_kiov != NULL);
1148 rb = list_entry(msg->msg_kiov, struct lnet_rtrbuf, rb_kiov[0]);
1151 msg->msg_kiov = NULL;
1152 msg->msg_rtrcredit = 0;
1154 LASSERT(rbp == lnet_msg2bufpool(msg));
1156 LASSERT((rbp->rbp_credits > 0) ==
1157 !list_empty(&rbp->rbp_bufs));
1159 /* If routing is now turned off, we just drop this buffer and
1160 * don't bother trying to return credits. */
1161 if (!the_lnet.ln_routing) {
1162 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1166 /* It is possible that a user has lowered the desired number of
1167 * buffers in this pool. Make sure we never put back
1168 * more buffers than the stated number. */
1169 if (unlikely(rbp->rbp_credits >= rbp->rbp_req_nbuffers)) {
1170 /* Discard this buffer so we don't have too
1172 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1173 rbp->rbp_nbuffers--;
1175 list_add(&rb->rb_list, &rbp->rbp_bufs);
1177 if (rbp->rbp_credits <= 0)
1178 lnet_schedule_blocked_locked(rbp);
1183 if (msg->msg_peerrtrcredit) {
1184 /* give back peer router credits */
1185 msg->msg_peerrtrcredit = 0;
1187 spin_lock(&rxpeer->lpni_lock);
1188 LASSERT((rxpeer->lpni_rtrcredits < 0) ==
1189 !list_empty(&rxpeer->lpni_rtrq));
1191 rxpeer->lpni_rtrcredits++;
1193 /* drop all messages which are queued to be routed on that
1195 if (!the_lnet.ln_routing) {
1196 struct list_head drop;
1197 INIT_LIST_HEAD(&drop);
1198 list_splice_init(&rxpeer->lpni_rtrq, &drop);
1199 spin_unlock(&rxpeer->lpni_lock);
1200 lnet_drop_routed_msgs_locked(&drop, msg->msg_rx_cpt);
1201 } else if (rxpeer->lpni_rtrcredits <= 0) {
1202 msg2 = list_entry(rxpeer->lpni_rtrq.next,
1203 struct lnet_msg, msg_list);
1204 list_del(&msg2->msg_list);
1205 spin_unlock(&rxpeer->lpni_lock);
1206 (void) lnet_post_routed_recv_locked(msg2, 1);
1208 spin_unlock(&rxpeer->lpni_lock);
1212 msg->msg_rxni = NULL;
1213 lnet_ni_decref_locked(rxni, msg->msg_rx_cpt);
1215 if (rxpeer != NULL) {
1216 msg->msg_rxpeer = NULL;
1217 lnet_peer_ni_decref_locked(rxpeer);
1222 lnet_compare_peers(struct lnet_peer_ni *p1, struct lnet_peer_ni *p2)
1224 if (p1->lpni_txqnob < p2->lpni_txqnob)
1227 if (p1->lpni_txqnob > p2->lpni_txqnob)
1230 if (p1->lpni_txcredits > p2->lpni_txcredits)
1233 if (p1->lpni_txcredits < p2->lpni_txcredits)
1240 lnet_compare_routes(struct lnet_route *r1, struct lnet_route *r2)
1242 struct lnet_peer_ni *p1 = r1->lr_gateway;
1243 struct lnet_peer_ni *p2 = r2->lr_gateway;
1244 int r1_hops = (r1->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r1->lr_hops;
1245 int r2_hops = (r2->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r2->lr_hops;
1248 if (r1->lr_priority < r2->lr_priority)
1251 if (r1->lr_priority > r2->lr_priority)
1254 if (r1_hops < r2_hops)
1257 if (r1_hops > r2_hops)
1260 rc = lnet_compare_peers(p1, p2);
1264 if (r1->lr_seq - r2->lr_seq <= 0)
1270 static struct lnet_peer_ni *
1271 lnet_find_route_locked(struct lnet_net *net, lnet_nid_t target,
1274 struct lnet_remotenet *rnet;
1275 struct lnet_route *route;
1276 struct lnet_route *best_route;
1277 struct lnet_route *last_route;
1278 struct lnet_peer_ni *lpni_best;
1279 struct lnet_peer_ni *lp;
1282 /* If @rtr_nid is not LNET_NID_ANY, return the gateway with
1283 * rtr_nid nid, otherwise find the best gateway I can use */
1285 rnet = lnet_find_rnet_locked(LNET_NIDNET(target));
1290 best_route = last_route = NULL;
1291 list_for_each_entry(route, &rnet->lrn_routes, lr_list) {
1292 lp = route->lr_gateway;
1294 if (!lnet_is_route_alive(route))
1297 if (net != NULL && lp->lpni_net != net)
1300 if (lp->lpni_nid == rtr_nid) /* it's pre-determined router */
1303 if (lpni_best == NULL) {
1304 best_route = last_route = route;
1309 /* no protection on below fields, but it's harmless */
1310 if (last_route->lr_seq - route->lr_seq < 0)
1313 rc = lnet_compare_routes(route, best_route);
1321 /* set sequence number on the best router to the latest sequence + 1
1322 * so we can round-robin all routers, it's race and inaccurate but
1323 * harmless and functional */
1324 if (best_route != NULL)
1325 best_route->lr_seq = last_route->lr_seq + 1;
1329 static struct lnet_ni *
1330 lnet_get_best_ni(struct lnet_net *local_net, struct lnet_ni *cur_ni,
1333 struct lnet_ni *ni = NULL, *best_ni = cur_ni;
1334 unsigned int shortest_distance;
1337 if (best_ni == NULL) {
1338 shortest_distance = UINT_MAX;
1339 best_credits = INT_MIN;
1341 shortest_distance = cfs_cpt_distance(lnet_cpt_table(), md_cpt,
1342 best_ni->ni_dev_cpt);
1343 best_credits = atomic_read(&best_ni->ni_tx_credits);
1346 while ((ni = lnet_get_next_ni_locked(local_net, ni))) {
1347 unsigned int distance;
1350 if (!lnet_is_ni_healthy_locked(ni))
1353 ni_credits = atomic_read(&ni->ni_tx_credits);
1356 * calculate the distance from the CPT on which
1357 * the message memory is allocated to the CPT of
1358 * the NI's physical device
1360 distance = cfs_cpt_distance(lnet_cpt_table(),
1365 * All distances smaller than the NUMA range
1366 * are treated equally.
1368 if (distance < lnet_numa_range)
1369 distance = lnet_numa_range;
1372 * Select on shorter distance, then available
1373 * credits, then round-robin.
1375 if (distance > shortest_distance) {
1377 } else if (distance < shortest_distance) {
1378 shortest_distance = distance;
1379 } else if (ni_credits < best_credits) {
1381 } else if (ni_credits == best_credits) {
1382 if (best_ni && (best_ni)->ni_seq <= ni->ni_seq)
1386 best_credits = ni_credits;
1393 lnet_select_pathway(lnet_nid_t src_nid, lnet_nid_t dst_nid,
1394 struct lnet_msg *msg, lnet_nid_t rtr_nid)
1396 struct lnet_ni *best_ni;
1397 struct lnet_peer_ni *best_lpni;
1398 struct lnet_peer_ni *best_gw;
1399 struct lnet_peer_ni *lpni;
1400 struct lnet_peer_ni *final_dst;
1401 struct lnet_peer *peer;
1402 struct lnet_peer_net *peer_net;
1403 struct lnet_net *local_net;
1411 int best_lpni_credits;
1415 * get an initial CPT to use for locking. The idea here is not to
1416 * serialize the calls to select_pathway, so that as many
1417 * operations can run concurrently as possible. To do that we use
1418 * the CPT where this call is being executed. Later on when we
1419 * determine the CPT to use in lnet_message_commit, we switch the
1420 * lock and check if there was any configuration change. If none,
1421 * then we proceed, if there is, then we restart the operation.
1423 cpt = lnet_net_lock_current();
1425 md_cpt = lnet_cpt_of_md(msg->msg_md, msg->msg_offset);
1426 if (md_cpt == CFS_CPT_ANY)
1437 local_found = false;
1439 seq = lnet_get_dlc_seq_locked();
1441 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1442 lnet_net_unlock(cpt);
1447 * lnet_nid2peerni_locked() is the path that will find an
1448 * existing peer_ni, or create one and mark it as having been
1449 * created due to network traffic.
1451 lpni = lnet_nid2peerni_locked(dst_nid, cpt);
1453 lnet_net_unlock(cpt);
1454 return PTR_ERR(lpni);
1456 peer = lpni->lpni_peer_net->lpn_peer;
1457 lnet_peer_ni_decref_locked(lpni);
1459 /* If peer is not healthy then can not send anything to it */
1460 if (!lnet_is_peer_healthy_locked(peer)) {
1461 lnet_net_unlock(cpt);
1462 return -EHOSTUNREACH;
1465 if (!lnet_peer_is_multi_rail(peer) &&
1466 lnet_get_num_peer_nis(peer) > 1) {
1467 CERROR("peer %s is declared to be non MR capable, "
1468 "yet configured with more than one NID\n",
1469 libcfs_nid2str(dst_nid));
1474 * STEP 1: first jab at determining best_ni
1475 * if src_nid is explicitly specified, then best_ni is already
1476 * pre-determiend for us. Otherwise we need to select the best
1477 * one to use later on
1479 if (src_nid != LNET_NID_ANY) {
1480 best_ni = lnet_nid2ni_locked(src_nid, cpt);
1482 lnet_net_unlock(cpt);
1483 LCONSOLE_WARN("Can't send to %s: src %s is not a "
1484 "local nid\n", libcfs_nid2str(dst_nid),
1485 libcfs_nid2str(src_nid));
1490 if (msg->msg_type == LNET_MSG_REPLY ||
1491 msg->msg_type == LNET_MSG_ACK ||
1492 !lnet_peer_is_multi_rail(peer) ||
1495 * for replies we want to respond on the same peer_ni we
1496 * received the message on if possible. If not, then pick
1497 * a peer_ni to send to
1499 * if the peer is non-multi-rail then you want to send to
1500 * the dst_nid provided as well.
1502 * If the best_ni has already been determined, IE the
1503 * src_nid has been specified, then use the
1504 * destination_nid provided as well, since we're
1505 * continuing a series of related messages for the same
1508 * It is expected to find the lpni using dst_nid, since we
1509 * created it earlier.
1511 best_lpni = lnet_find_peer_ni_locked(dst_nid);
1513 lnet_peer_ni_decref_locked(best_lpni);
1515 if (best_lpni && !lnet_get_net_locked(LNET_NIDNET(dst_nid))) {
1517 * this lpni is not on a local network so we need
1518 * to route this reply.
1520 best_gw = lnet_find_route_locked(NULL,
1521 best_lpni->lpni_nid,
1525 * RULE: Each node considers only the next-hop
1527 * We're going to route the message, so change the peer to
1530 LASSERT(best_gw->lpni_peer_net);
1531 LASSERT(best_gw->lpni_peer_net->lpn_peer);
1532 peer = best_gw->lpni_peer_net->lpn_peer;
1535 * if the router is not multi-rail then use the best_gw
1536 * found to send the message to
1538 if (!lnet_peer_is_multi_rail(peer))
1539 best_lpni = best_gw;
1547 } else if (!best_lpni) {
1548 lnet_net_unlock(cpt);
1549 CERROR("unable to send msg_type %d to "
1550 "originating %s. Destination NID not in DB\n",
1551 msg->msg_type, libcfs_nid2str(dst_nid));
1557 * if the peer is not MR capable, then we should always send to it
1558 * using the first NI in the NET we determined.
1560 if (!lnet_peer_is_multi_rail(peer)) {
1562 lnet_net_unlock(cpt);
1563 CERROR("no route to %s\n",
1564 libcfs_nid2str(dst_nid));
1565 return -EHOSTUNREACH;
1568 /* best ni could be set because src_nid was provided */
1570 best_ni = lnet_net2ni_locked(best_lpni->lpni_net->net_id, cpt);
1572 lnet_net_unlock(cpt);
1573 CERROR("no path to %s from net %s\n",
1574 libcfs_nid2str(best_lpni->lpni_nid),
1575 libcfs_net2str(best_lpni->lpni_net->net_id));
1576 return -EHOSTUNREACH;
1582 * if we already found a best_ni because src_nid is specified and
1583 * best_lpni because we are replying to a message then just send
1586 if (best_ni && best_lpni)
1590 * If we already found a best_ni because src_nid is specified then
1591 * pick the peer then send the message
1597 * pick the best_ni by going through all the possible networks of
1598 * that peer and see which local NI is best suited to talk to that
1601 * Locally connected networks will always be preferred over
1602 * a routed network. If there are only routed paths to the peer,
1603 * then the best route is chosen. If all routes are equal then
1604 * they are used in round robin.
1606 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_on_peer_list) {
1607 if (!lnet_is_peer_net_healthy_locked(peer_net))
1610 local_net = lnet_get_net_locked(peer_net->lpn_net_id);
1611 if (!local_net && !routing && !local_found) {
1612 struct lnet_peer_ni *net_gw;
1614 lpni = list_entry(peer_net->lpn_peer_nis.next,
1615 struct lnet_peer_ni,
1616 lpni_on_peer_net_list);
1618 net_gw = lnet_find_route_locked(NULL,
1626 * lnet_find_route_locked() call
1627 * will return the best_Gw on the
1628 * lpni->lpni_nid network.
1629 * However, best_gw and net_gw can
1630 * be on different networks.
1631 * Therefore need to compare them
1632 * to pick the better of either.
1634 if (lnet_compare_peers(best_gw, net_gw) > 0)
1636 if (best_gw->lpni_gw_seq <= net_gw->lpni_gw_seq)
1651 * a gw on this network is found, but there could be
1652 * other better gateways on other networks. So don't pick
1653 * the best_ni until we determine the best_gw.
1658 /* if no local_net found continue */
1663 * Iterate through the NIs in this local Net and select
1664 * the NI to send from. The selection is determined by
1665 * these 3 criterion in the following priority:
1667 * 2. NI available credits
1670 best_ni = lnet_get_best_ni(local_net, best_ni, md_cpt);
1673 if (!best_ni && !best_gw) {
1674 lnet_net_unlock(cpt);
1675 LCONSOLE_WARN("No local ni found to send from to %s\n",
1676 libcfs_nid2str(dst_nid));
1681 best_ni = lnet_get_best_ni(best_gw->lpni_net, best_ni, md_cpt);
1682 LASSERT(best_gw && best_ni);
1685 * We're going to route the message, so change the peer to
1688 LASSERT(best_gw->lpni_peer_net);
1689 LASSERT(best_gw->lpni_peer_net->lpn_peer);
1690 best_gw->lpni_gw_seq++;
1691 peer = best_gw->lpni_peer_net->lpn_peer;
1695 * Now that we selected the NI to use increment its sequence
1696 * number so the Round Robin algorithm will detect that it has
1697 * been used and pick the next NI.
1703 * At this point the best_ni is on a local network on which
1704 * the peer has a peer_ni as well
1706 peer_net = lnet_peer_get_net_locked(peer,
1707 best_ni->ni_net->net_id);
1709 * peer_net is not available or the src_nid is explicitly defined
1710 * and the peer_net for that src_nid is unhealthy. find a route to
1711 * the destination nid.
1714 (src_nid != LNET_NID_ANY &&
1715 !lnet_is_peer_net_healthy_locked(peer_net))) {
1716 best_gw = lnet_find_route_locked(best_ni->ni_net,
1720 * if no route is found for that network then
1721 * move onto the next peer_ni in the peer
1724 lnet_net_unlock(cpt);
1725 LCONSOLE_WARN("No route to peer from %s\n",
1726 libcfs_nid2str(best_ni->ni_nid));
1727 return -EHOSTUNREACH;
1730 CDEBUG(D_NET, "Best route to %s via %s for %s %d\n",
1731 libcfs_nid2str(dst_nid),
1732 libcfs_nid2str(best_gw->lpni_nid),
1733 lnet_msgtyp2str(msg->msg_type), msg->msg_len);
1737 * RULE: Each node considers only the next-hop
1739 * We're going to route the message, so change the peer to
1742 LASSERT(best_gw->lpni_peer_net);
1743 LASSERT(best_gw->lpni_peer_net->lpn_peer);
1744 peer = best_gw->lpni_peer_net->lpn_peer;
1745 } else if (!lnet_is_peer_net_healthy_locked(peer_net)) {
1747 * this peer_net is unhealthy but we still have an opportunity
1748 * to find another peer_net that we can use
1750 __u32 net_id = peer_net->lpn_net_id;
1751 LCONSOLE_WARN("peer net %s unhealthy\n",
1752 libcfs_net2str(net_id));
1757 * Look at the peer NIs for the destination peer that connect
1758 * to the chosen net. If a peer_ni is preferred when using the
1759 * best_ni to communicate, we use that one. If there is no
1760 * preferred peer_ni, or there are multiple preferred peer_ni,
1761 * the available transmit credits are used. If the transmit
1762 * credits are equal, we round-robin over the peer_ni.
1765 best_lpni_credits = INT_MIN;
1768 while ((lpni = lnet_get_next_peer_ni_locked(peer, peer_net, lpni))) {
1770 * if this peer ni is not healthy just skip it, no point in
1771 * examining it further
1773 if (!lnet_is_peer_ni_healthy_locked(lpni))
1775 ni_is_pref = lnet_peer_is_ni_pref_locked(lpni, best_ni);
1777 /* if this is a preferred peer use it */
1778 if (!preferred && ni_is_pref) {
1780 } else if (preferred && !ni_is_pref) {
1782 * this is not the preferred peer so let's ignore
1786 } else if (lpni->lpni_txcredits < best_lpni_credits) {
1788 * We already have a peer that has more credits
1789 * available than this one. No need to consider
1790 * this peer further.
1793 } else if (lpni->lpni_txcredits == best_lpni_credits) {
1795 * The best peer found so far and the current peer
1796 * have the same number of available credits let's
1797 * make sure to select between them using Round
1801 if (best_lpni->lpni_seq <= lpni->lpni_seq)
1807 best_lpni_credits = lpni->lpni_txcredits;
1810 /* if we still can't find a peer ni then we can't reach it */
1812 __u32 net_id = (peer_net) ? peer_net->lpn_net_id :
1813 LNET_NIDNET(dst_nid);
1814 lnet_net_unlock(cpt);
1815 LCONSOLE_WARN("no peer_ni found on peer net %s\n",
1816 libcfs_net2str(net_id));
1817 return -EHOSTUNREACH;
1822 /* Shortcut for loopback. */
1823 if (best_ni == the_lnet.ln_loni) {
1824 /* No send credit hassles with LOLND */
1825 lnet_ni_addref_locked(best_ni, cpt);
1826 msg->msg_hdr.dest_nid = cpu_to_le64(best_ni->ni_nid);
1827 if (!msg->msg_routing)
1828 msg->msg_hdr.src_nid = cpu_to_le64(best_ni->ni_nid);
1829 msg->msg_target.nid = best_ni->ni_nid;
1830 lnet_msg_commit(msg, cpt);
1831 msg->msg_txni = best_ni;
1832 lnet_net_unlock(cpt);
1834 return LNET_CREDIT_OK;
1837 routing = routing || routing2;
1840 * Increment sequence number of the peer selected so that we
1841 * pick the next one in Round Robin.
1843 best_lpni->lpni_seq++;
1846 * grab a reference on the peer_ni so it sticks around even if
1847 * we need to drop and relock the lnet_net_lock below.
1849 lnet_peer_ni_addref_locked(best_lpni);
1852 * Use lnet_cpt_of_nid() to determine the CPT used to commit the
1853 * message. This ensures that we get a CPT that is correct for
1854 * the NI when the NI has been restricted to a subset of all CPTs.
1855 * If the selected CPT differs from the one currently locked, we
1856 * must unlock and relock the lnet_net_lock(), and then check whether
1857 * the configuration has changed. We don't have a hold on the best_ni
1858 * yet, and it may have vanished.
1860 cpt2 = lnet_cpt_of_nid_locked(best_lpni->lpni_nid, best_ni);
1862 lnet_net_unlock(cpt);
1865 if (seq != lnet_get_dlc_seq_locked()) {
1866 lnet_peer_ni_decref_locked(best_lpni);
1872 * store the best_lpni in the message right away to avoid having
1873 * to do the same operation under different conditions
1875 msg->msg_txpeer = best_lpni;
1876 msg->msg_txni = best_ni;
1879 * grab a reference for the best_ni since now it's in use in this
1880 * send. the reference will need to be dropped when the message is
1881 * finished in lnet_finalize()
1883 lnet_ni_addref_locked(msg->msg_txni, cpt);
1886 * Always set the target.nid to the best peer picked. Either the
1887 * nid will be one of the preconfigured NIDs, or the same NID as
1888 * what was originally set in the target or it will be the NID of
1889 * a router if this message should be routed
1891 msg->msg_target.nid = msg->msg_txpeer->lpni_nid;
1894 * lnet_msg_commit assigns the correct cpt to the message, which
1895 * is used to decrement the correct refcount on the ni when it's
1896 * time to return the credits
1898 lnet_msg_commit(msg, cpt);
1901 * If we are routing the message then we don't need to overwrite
1902 * the src_nid since it would've been set at the origin. Otherwise
1903 * we are the originator so we need to set it.
1905 if (!msg->msg_routing)
1906 msg->msg_hdr.src_nid = cpu_to_le64(msg->msg_txni->ni_nid);
1909 msg->msg_target_is_router = 1;
1910 msg->msg_target.pid = LNET_PID_LUSTRE;
1912 * since we're routing we want to ensure that the
1913 * msg_hdr.dest_nid is set to the final destination. When
1914 * the router receives this message it knows how to route
1917 msg->msg_hdr.dest_nid =
1918 cpu_to_le64(final_dst ? final_dst->lpni_nid : dst_nid);
1921 * if we're not routing set the dest_nid to the best peer
1922 * ni that we picked earlier in the algorithm.
1924 msg->msg_hdr.dest_nid = cpu_to_le64(msg->msg_txpeer->lpni_nid);
1927 rc = lnet_post_send_locked(msg, 0);
1930 CDEBUG(D_NET, "TRACE: %s(%s:%s) -> %s(%s:%s) : %s\n",
1931 libcfs_nid2str(msg->msg_hdr.src_nid),
1932 libcfs_nid2str(msg->msg_txni->ni_nid),
1933 libcfs_nid2str(src_nid),
1934 libcfs_nid2str(msg->msg_hdr.dest_nid),
1935 libcfs_nid2str(dst_nid),
1936 libcfs_nid2str(msg->msg_txpeer->lpni_nid),
1937 lnet_msgtyp2str(msg->msg_type));
1939 lnet_net_unlock(cpt);
1945 lnet_send(lnet_nid_t src_nid, struct lnet_msg *msg, lnet_nid_t rtr_nid)
1947 lnet_nid_t dst_nid = msg->msg_target.nid;
1951 * NB: rtr_nid is set to LNET_NID_ANY for all current use-cases,
1952 * but we might want to use pre-determined router for ACK/REPLY
1955 /* NB: ni != NULL == interface pre-determined (ACK/REPLY) */
1956 LASSERT (msg->msg_txpeer == NULL);
1957 LASSERT (!msg->msg_sending);
1958 LASSERT (!msg->msg_target_is_router);
1959 LASSERT (!msg->msg_receiving);
1961 msg->msg_sending = 1;
1963 LASSERT(!msg->msg_tx_committed);
1965 rc = lnet_select_pathway(src_nid, dst_nid, msg, rtr_nid);
1969 if (rc == LNET_CREDIT_OK)
1970 lnet_ni_send(msg->msg_txni, msg);
1972 /* rc == LNET_CREDIT_OK or LNET_CREDIT_WAIT */
1977 lnet_drop_message(struct lnet_ni *ni, int cpt, void *private, unsigned int nob)
1980 the_lnet.ln_counters[cpt]->drop_count++;
1981 the_lnet.ln_counters[cpt]->drop_length += nob;
1982 lnet_net_unlock(cpt);
1984 lnet_ni_recv(ni, private, NULL, 0, 0, 0, nob);
1988 lnet_recv_put(struct lnet_ni *ni, struct lnet_msg *msg)
1990 struct lnet_hdr *hdr = &msg->msg_hdr;
1992 if (msg->msg_wanted != 0)
1993 lnet_setpayloadbuffer(msg);
1995 lnet_build_msg_event(msg, LNET_EVENT_PUT);
1997 /* Must I ACK? If so I'll grab the ack_wmd out of the header and put
1998 * it back into the ACK during lnet_finalize() */
1999 msg->msg_ack = (!lnet_is_wire_handle_none(&hdr->msg.put.ack_wmd) &&
2000 (msg->msg_md->md_options & LNET_MD_ACK_DISABLE) == 0);
2002 lnet_ni_recv(ni, msg->msg_private, msg, msg->msg_rx_delayed,
2003 msg->msg_offset, msg->msg_wanted, hdr->payload_length);
2007 lnet_parse_put(struct lnet_ni *ni, struct lnet_msg *msg)
2009 struct lnet_hdr *hdr = &msg->msg_hdr;
2010 struct lnet_match_info info;
2014 /* Convert put fields to host byte order */
2015 hdr->msg.put.match_bits = le64_to_cpu(hdr->msg.put.match_bits);
2016 hdr->msg.put.ptl_index = le32_to_cpu(hdr->msg.put.ptl_index);
2017 hdr->msg.put.offset = le32_to_cpu(hdr->msg.put.offset);
2019 /* Primary peer NID. */
2020 info.mi_id.nid = msg->msg_initiator;
2021 info.mi_id.pid = hdr->src_pid;
2022 info.mi_opc = LNET_MD_OP_PUT;
2023 info.mi_portal = hdr->msg.put.ptl_index;
2024 info.mi_rlength = hdr->payload_length;
2025 info.mi_roffset = hdr->msg.put.offset;
2026 info.mi_mbits = hdr->msg.put.match_bits;
2027 info.mi_cpt = lnet_cpt_of_nid(msg->msg_rxpeer->lpni_nid, ni);
2029 msg->msg_rx_ready_delay = ni->ni_net->net_lnd->lnd_eager_recv == NULL;
2030 ready_delay = msg->msg_rx_ready_delay;
2033 rc = lnet_ptl_match_md(&info, msg);
2038 case LNET_MATCHMD_OK:
2039 lnet_recv_put(ni, msg);
2042 case LNET_MATCHMD_NONE:
2044 /* no eager_recv or has already called it, should
2045 * have been attached on delayed list */
2048 rc = lnet_ni_eager_recv(ni, msg);
2055 case LNET_MATCHMD_DROP:
2056 CNETERR("Dropping PUT from %s portal %d match %llu"
2057 " offset %d length %d: %d\n",
2058 libcfs_id2str(info.mi_id), info.mi_portal,
2059 info.mi_mbits, info.mi_roffset, info.mi_rlength, rc);
2061 return -ENOENT; /* -ve: OK but no match */
2066 lnet_parse_get(struct lnet_ni *ni, struct lnet_msg *msg, int rdma_get)
2068 struct lnet_match_info info;
2069 struct lnet_hdr *hdr = &msg->msg_hdr;
2070 struct lnet_process_id source_id;
2071 struct lnet_handle_wire reply_wmd;
2074 /* Convert get fields to host byte order */
2075 hdr->msg.get.match_bits = le64_to_cpu(hdr->msg.get.match_bits);
2076 hdr->msg.get.ptl_index = le32_to_cpu(hdr->msg.get.ptl_index);
2077 hdr->msg.get.sink_length = le32_to_cpu(hdr->msg.get.sink_length);
2078 hdr->msg.get.src_offset = le32_to_cpu(hdr->msg.get.src_offset);
2080 source_id.nid = hdr->src_nid;
2081 source_id.pid = hdr->src_pid;
2082 /* Primary peer NID */
2083 info.mi_id.nid = msg->msg_initiator;
2084 info.mi_id.pid = hdr->src_pid;
2085 info.mi_opc = LNET_MD_OP_GET;
2086 info.mi_portal = hdr->msg.get.ptl_index;
2087 info.mi_rlength = hdr->msg.get.sink_length;
2088 info.mi_roffset = hdr->msg.get.src_offset;
2089 info.mi_mbits = hdr->msg.get.match_bits;
2090 info.mi_cpt = lnet_cpt_of_nid(msg->msg_rxpeer->lpni_nid, ni);
2092 rc = lnet_ptl_match_md(&info, msg);
2093 if (rc == LNET_MATCHMD_DROP) {
2094 CNETERR("Dropping GET from %s portal %d match %llu"
2095 " offset %d length %d\n",
2096 libcfs_id2str(info.mi_id), info.mi_portal,
2097 info.mi_mbits, info.mi_roffset, info.mi_rlength);
2098 return -ENOENT; /* -ve: OK but no match */
2101 LASSERT(rc == LNET_MATCHMD_OK);
2103 lnet_build_msg_event(msg, LNET_EVENT_GET);
2105 reply_wmd = hdr->msg.get.return_wmd;
2107 lnet_prep_send(msg, LNET_MSG_REPLY, source_id,
2108 msg->msg_offset, msg->msg_wanted);
2110 msg->msg_hdr.msg.reply.dst_wmd = reply_wmd;
2113 /* The LND completes the REPLY from her recv procedure */
2114 lnet_ni_recv(ni, msg->msg_private, msg, 0,
2115 msg->msg_offset, msg->msg_len, msg->msg_len);
2119 lnet_ni_recv(ni, msg->msg_private, NULL, 0, 0, 0, 0);
2120 msg->msg_receiving = 0;
2122 rc = lnet_send(ni->ni_nid, msg, LNET_NID_ANY);
2124 /* didn't get as far as lnet_ni_send() */
2125 CERROR("%s: Unable to send REPLY for GET from %s: %d\n",
2126 libcfs_nid2str(ni->ni_nid),
2127 libcfs_id2str(info.mi_id), rc);
2129 lnet_finalize(msg, rc);
2136 lnet_parse_reply(struct lnet_ni *ni, struct lnet_msg *msg)
2138 void *private = msg->msg_private;
2139 struct lnet_hdr *hdr = &msg->msg_hdr;
2140 struct lnet_process_id src = {0};
2141 struct lnet_libmd *md;
2146 cpt = lnet_cpt_of_cookie(hdr->msg.reply.dst_wmd.wh_object_cookie);
2149 src.nid = hdr->src_nid;
2150 src.pid = hdr->src_pid;
2152 /* NB handles only looked up by creator (no flips) */
2153 md = lnet_wire_handle2md(&hdr->msg.reply.dst_wmd);
2154 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
2155 CNETERR("%s: Dropping REPLY from %s for %s "
2157 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
2158 (md == NULL) ? "invalid" : "inactive",
2159 hdr->msg.reply.dst_wmd.wh_interface_cookie,
2160 hdr->msg.reply.dst_wmd.wh_object_cookie);
2161 if (md != NULL && md->md_me != NULL)
2162 CERROR("REPLY MD also attached to portal %d\n",
2163 md->md_me->me_portal);
2165 lnet_res_unlock(cpt);
2166 return -ENOENT; /* -ve: OK but no match */
2169 LASSERT(md->md_offset == 0);
2171 rlength = hdr->payload_length;
2172 mlength = MIN(rlength, (int)md->md_length);
2174 if (mlength < rlength &&
2175 (md->md_options & LNET_MD_TRUNCATE) == 0) {
2176 CNETERR("%s: Dropping REPLY from %s length %d "
2177 "for MD %#llx would overflow (%d)\n",
2178 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
2179 rlength, hdr->msg.reply.dst_wmd.wh_object_cookie,
2181 lnet_res_unlock(cpt);
2182 return -ENOENT; /* -ve: OK but no match */
2185 CDEBUG(D_NET, "%s: Reply from %s of length %d/%d into md %#llx\n",
2186 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
2187 mlength, rlength, hdr->msg.reply.dst_wmd.wh_object_cookie);
2189 lnet_msg_attach_md(msg, md, 0, mlength);
2192 lnet_setpayloadbuffer(msg);
2194 lnet_res_unlock(cpt);
2196 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
2198 lnet_ni_recv(ni, private, msg, 0, 0, mlength, rlength);
2203 lnet_parse_ack(struct lnet_ni *ni, struct lnet_msg *msg)
2205 struct lnet_hdr *hdr = &msg->msg_hdr;
2206 struct lnet_process_id src = {0};
2207 struct lnet_libmd *md;
2210 src.nid = hdr->src_nid;
2211 src.pid = hdr->src_pid;
2213 /* Convert ack fields to host byte order */
2214 hdr->msg.ack.match_bits = le64_to_cpu(hdr->msg.ack.match_bits);
2215 hdr->msg.ack.mlength = le32_to_cpu(hdr->msg.ack.mlength);
2217 cpt = lnet_cpt_of_cookie(hdr->msg.ack.dst_wmd.wh_object_cookie);
2220 /* NB handles only looked up by creator (no flips) */
2221 md = lnet_wire_handle2md(&hdr->msg.ack.dst_wmd);
2222 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
2223 /* Don't moan; this is expected */
2225 "%s: Dropping ACK from %s to %s MD %#llx.%#llx\n",
2226 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
2227 (md == NULL) ? "invalid" : "inactive",
2228 hdr->msg.ack.dst_wmd.wh_interface_cookie,
2229 hdr->msg.ack.dst_wmd.wh_object_cookie);
2230 if (md != NULL && md->md_me != NULL)
2231 CERROR("Source MD also attached to portal %d\n",
2232 md->md_me->me_portal);
2234 lnet_res_unlock(cpt);
2235 return -ENOENT; /* -ve! */
2238 CDEBUG(D_NET, "%s: ACK from %s into md %#llx\n",
2239 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
2240 hdr->msg.ack.dst_wmd.wh_object_cookie);
2242 lnet_msg_attach_md(msg, md, 0, 0);
2244 lnet_res_unlock(cpt);
2246 lnet_build_msg_event(msg, LNET_EVENT_ACK);
2248 lnet_ni_recv(ni, msg->msg_private, msg, 0, 0, 0, msg->msg_len);
2253 * \retval LNET_CREDIT_OK If \a msg is forwarded
2254 * \retval LNET_CREDIT_WAIT If \a msg is blocked because w/o buffer
2255 * \retval -ve error code
2258 lnet_parse_forward_locked(struct lnet_ni *ni, struct lnet_msg *msg)
2262 if (!the_lnet.ln_routing)
2265 if (msg->msg_rxpeer->lpni_rtrcredits <= 0 ||
2266 lnet_msg2bufpool(msg)->rbp_credits <= 0) {
2267 if (ni->ni_net->net_lnd->lnd_eager_recv == NULL) {
2268 msg->msg_rx_ready_delay = 1;
2270 lnet_net_unlock(msg->msg_rx_cpt);
2271 rc = lnet_ni_eager_recv(ni, msg);
2272 lnet_net_lock(msg->msg_rx_cpt);
2277 rc = lnet_post_routed_recv_locked(msg, 0);
2282 lnet_parse_local(struct lnet_ni *ni, struct lnet_msg *msg)
2286 switch (msg->msg_type) {
2288 rc = lnet_parse_ack(ni, msg);
2291 rc = lnet_parse_put(ni, msg);
2294 rc = lnet_parse_get(ni, msg, msg->msg_rdma_get);
2296 case LNET_MSG_REPLY:
2297 rc = lnet_parse_reply(ni, msg);
2299 default: /* prevent an unused label if !kernel */
2304 LASSERT(rc == 0 || rc == -ENOENT);
2309 lnet_msgtyp2str (int type)
2318 case LNET_MSG_REPLY:
2320 case LNET_MSG_HELLO:
2323 return ("<UNKNOWN>");
2328 lnet_print_hdr(struct lnet_hdr *hdr)
2330 struct lnet_process_id src = {
2331 .nid = hdr->src_nid,
2332 .pid = hdr->src_pid,
2334 struct lnet_process_id dst = {
2335 .nid = hdr->dest_nid,
2336 .pid = hdr->dest_pid,
2338 char *type_str = lnet_msgtyp2str(hdr->type);
2340 CWARN("P3 Header at %p of type %s\n", hdr, type_str);
2341 CWARN(" From %s\n", libcfs_id2str(src));
2342 CWARN(" To %s\n", libcfs_id2str(dst));
2344 switch (hdr->type) {
2349 CWARN(" Ptl index %d, ack md %#llx.%#llx, "
2350 "match bits %llu\n",
2351 hdr->msg.put.ptl_index,
2352 hdr->msg.put.ack_wmd.wh_interface_cookie,
2353 hdr->msg.put.ack_wmd.wh_object_cookie,
2354 hdr->msg.put.match_bits);
2355 CWARN(" Length %d, offset %d, hdr data %#llx\n",
2356 hdr->payload_length, hdr->msg.put.offset,
2357 hdr->msg.put.hdr_data);
2361 CWARN(" Ptl index %d, return md %#llx.%#llx, "
2362 "match bits %llu\n", hdr->msg.get.ptl_index,
2363 hdr->msg.get.return_wmd.wh_interface_cookie,
2364 hdr->msg.get.return_wmd.wh_object_cookie,
2365 hdr->msg.get.match_bits);
2366 CWARN(" Length %d, src offset %d\n",
2367 hdr->msg.get.sink_length,
2368 hdr->msg.get.src_offset);
2372 CWARN(" dst md %#llx.%#llx, "
2373 "manipulated length %d\n",
2374 hdr->msg.ack.dst_wmd.wh_interface_cookie,
2375 hdr->msg.ack.dst_wmd.wh_object_cookie,
2376 hdr->msg.ack.mlength);
2379 case LNET_MSG_REPLY:
2380 CWARN(" dst md %#llx.%#llx, "
2382 hdr->msg.reply.dst_wmd.wh_interface_cookie,
2383 hdr->msg.reply.dst_wmd.wh_object_cookie,
2384 hdr->payload_length);
2390 lnet_parse(struct lnet_ni *ni, struct lnet_hdr *hdr, lnet_nid_t from_nid,
2391 void *private, int rdma_req)
2396 struct lnet_msg *msg;
2397 lnet_pid_t dest_pid;
2398 lnet_nid_t dest_nid;
2400 struct lnet_peer_ni *lpni;
2401 __u32 payload_length;
2404 LASSERT (!in_interrupt ());
2406 type = le32_to_cpu(hdr->type);
2407 src_nid = le64_to_cpu(hdr->src_nid);
2408 dest_nid = le64_to_cpu(hdr->dest_nid);
2409 dest_pid = le32_to_cpu(hdr->dest_pid);
2410 payload_length = le32_to_cpu(hdr->payload_length);
2412 for_me = (ni->ni_nid == dest_nid);
2413 cpt = lnet_cpt_of_nid(from_nid, ni);
2415 CDEBUG(D_NET, "TRACE: %s(%s) <- %s : %s\n",
2416 libcfs_nid2str(dest_nid),
2417 libcfs_nid2str(ni->ni_nid),
2418 libcfs_nid2str(src_nid),
2419 lnet_msgtyp2str(type));
2424 if (payload_length > 0) {
2425 CERROR("%s, src %s: bad %s payload %d (0 expected)\n",
2426 libcfs_nid2str(from_nid),
2427 libcfs_nid2str(src_nid),
2428 lnet_msgtyp2str(type), payload_length);
2434 case LNET_MSG_REPLY:
2435 if (payload_length >
2436 (__u32)(for_me ? LNET_MAX_PAYLOAD : LNET_MTU)) {
2437 CERROR("%s, src %s: bad %s payload %d "
2438 "(%d max expected)\n",
2439 libcfs_nid2str(from_nid),
2440 libcfs_nid2str(src_nid),
2441 lnet_msgtyp2str(type),
2443 for_me ? LNET_MAX_PAYLOAD : LNET_MTU);
2449 CERROR("%s, src %s: Bad message type 0x%x\n",
2450 libcfs_nid2str(from_nid),
2451 libcfs_nid2str(src_nid), type);
2455 if (the_lnet.ln_routing &&
2456 ni->ni_last_alive != ktime_get_real_seconds()) {
2457 /* NB: so far here is the only place to set NI status to "up */
2459 ni->ni_last_alive = ktime_get_real_seconds();
2460 if (ni->ni_status != NULL &&
2461 ni->ni_status->ns_status == LNET_NI_STATUS_DOWN)
2462 ni->ni_status->ns_status = LNET_NI_STATUS_UP;
2466 /* Regard a bad destination NID as a protocol error. Senders should
2467 * know what they're doing; if they don't they're misconfigured, buggy
2468 * or malicious so we chop them off at the knees :) */
2471 if (LNET_NIDNET(dest_nid) == LNET_NIDNET(ni->ni_nid)) {
2472 /* should have gone direct */
2473 CERROR("%s, src %s: Bad dest nid %s "
2474 "(should have been sent direct)\n",
2475 libcfs_nid2str(from_nid),
2476 libcfs_nid2str(src_nid),
2477 libcfs_nid2str(dest_nid));
2481 if (lnet_islocalnid(dest_nid)) {
2482 /* dest is another local NI; sender should have used
2483 * this node's NID on its own network */
2484 CERROR("%s, src %s: Bad dest nid %s "
2485 "(it's my nid but on a different network)\n",
2486 libcfs_nid2str(from_nid),
2487 libcfs_nid2str(src_nid),
2488 libcfs_nid2str(dest_nid));
2492 if (rdma_req && type == LNET_MSG_GET) {
2493 CERROR("%s, src %s: Bad optimized GET for %s "
2494 "(final destination must be me)\n",
2495 libcfs_nid2str(from_nid),
2496 libcfs_nid2str(src_nid),
2497 libcfs_nid2str(dest_nid));
2501 if (!the_lnet.ln_routing) {
2502 CERROR("%s, src %s: Dropping message for %s "
2503 "(routing not enabled)\n",
2504 libcfs_nid2str(from_nid),
2505 libcfs_nid2str(src_nid),
2506 libcfs_nid2str(dest_nid));
2511 /* Message looks OK; we're not going to return an error, so we MUST
2512 * call back lnd_recv() come what may... */
2514 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
2515 fail_peer(src_nid, 0)) { /* shall we now? */
2516 CERROR("%s, src %s: Dropping %s to simulate failure\n",
2517 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
2518 lnet_msgtyp2str(type));
2522 if (!list_empty(&the_lnet.ln_drop_rules) &&
2523 lnet_drop_rule_match(hdr)) {
2524 CDEBUG(D_NET, "%s, src %s, dst %s: Dropping %s to simulate"
2525 "silent message loss\n",
2526 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
2527 libcfs_nid2str(dest_nid), lnet_msgtyp2str(type));
2532 msg = lnet_msg_alloc();
2534 CERROR("%s, src %s: Dropping %s (out of memory)\n",
2535 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
2536 lnet_msgtyp2str(type));
2540 /* msg zeroed in lnet_msg_alloc; i.e. flags all clear,
2541 * pointers NULL etc */
2543 msg->msg_type = type;
2544 msg->msg_private = private;
2545 msg->msg_receiving = 1;
2546 msg->msg_rdma_get = rdma_req;
2547 msg->msg_len = msg->msg_wanted = payload_length;
2548 msg->msg_offset = 0;
2549 msg->msg_hdr = *hdr;
2550 /* for building message event */
2551 msg->msg_from = from_nid;
2553 msg->msg_target.pid = dest_pid;
2554 msg->msg_target.nid = dest_nid;
2555 msg->msg_routing = 1;
2558 /* convert common msg->hdr fields to host byteorder */
2559 msg->msg_hdr.type = type;
2560 msg->msg_hdr.src_nid = src_nid;
2561 msg->msg_hdr.src_pid = le32_to_cpu(msg->msg_hdr.src_pid);
2562 msg->msg_hdr.dest_nid = dest_nid;
2563 msg->msg_hdr.dest_pid = dest_pid;
2564 msg->msg_hdr.payload_length = payload_length;
2568 lpni = lnet_nid2peerni_locked(from_nid, cpt);
2570 lnet_net_unlock(cpt);
2571 CERROR("%s, src %s: Dropping %s "
2572 "(error %ld looking up sender)\n",
2573 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
2574 lnet_msgtyp2str(type), PTR_ERR(lpni));
2576 if (rc == -ESHUTDOWN)
2577 /* We are shutting down. Don't do anything more */
2581 msg->msg_rxpeer = lpni;
2583 lnet_ni_addref_locked(ni, cpt);
2584 /* Multi-Rail: Primary NID of source. */
2585 msg->msg_initiator = lnet_peer_primary_nid_locked(src_nid);
2587 if (lnet_isrouter(msg->msg_rxpeer)) {
2588 lnet_peer_set_alive(msg->msg_rxpeer);
2589 if (avoid_asym_router_failure &&
2590 LNET_NIDNET(src_nid) != LNET_NIDNET(from_nid)) {
2591 /* received a remote message from router, update
2592 * remote NI status on this router.
2593 * NB: multi-hop routed message will be ignored.
2595 lnet_router_ni_update_locked(msg->msg_rxpeer,
2596 LNET_NIDNET(src_nid));
2600 lnet_msg_commit(msg, cpt);
2602 /* message delay simulation */
2603 if (unlikely(!list_empty(&the_lnet.ln_delay_rules) &&
2604 lnet_delay_rule_match_locked(hdr, msg))) {
2605 lnet_net_unlock(cpt);
2610 rc = lnet_parse_forward_locked(ni, msg);
2611 lnet_net_unlock(cpt);
2616 if (rc == LNET_CREDIT_OK) {
2617 lnet_ni_recv(ni, msg->msg_private, msg, 0,
2618 0, payload_length, payload_length);
2623 lnet_net_unlock(cpt);
2625 rc = lnet_parse_local(ni, msg);
2631 LASSERT(msg->msg_md == NULL);
2632 lnet_finalize(msg, rc);
2635 lnet_drop_message(ni, cpt, private, payload_length);
2638 EXPORT_SYMBOL(lnet_parse);
2641 lnet_drop_delayed_msg_list(struct list_head *head, char *reason)
2643 while (!list_empty(head)) {
2644 struct lnet_process_id id = {0};
2645 struct lnet_msg *msg;
2647 msg = list_entry(head->next, struct lnet_msg, msg_list);
2648 list_del(&msg->msg_list);
2650 id.nid = msg->msg_hdr.src_nid;
2651 id.pid = msg->msg_hdr.src_pid;
2653 LASSERT(msg->msg_md == NULL);
2654 LASSERT(msg->msg_rx_delayed);
2655 LASSERT(msg->msg_rxpeer != NULL);
2656 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
2658 CWARN("Dropping delayed PUT from %s portal %d match %llu"
2659 " offset %d length %d: %s\n",
2661 msg->msg_hdr.msg.put.ptl_index,
2662 msg->msg_hdr.msg.put.match_bits,
2663 msg->msg_hdr.msg.put.offset,
2664 msg->msg_hdr.payload_length, reason);
2666 /* NB I can't drop msg's ref on msg_rxpeer until after I've
2667 * called lnet_drop_message(), so I just hang onto msg as well
2668 * until that's done */
2670 lnet_drop_message(msg->msg_rxni, msg->msg_rx_cpt,
2671 msg->msg_private, msg->msg_len);
2673 * NB: message will not generate event because w/o attached MD,
2674 * but we still should give error code so lnet_msg_decommit()
2675 * can skip counters operations and other checks.
2677 lnet_finalize(msg, -ENOENT);
2682 lnet_recv_delayed_msg_list(struct list_head *head)
2684 while (!list_empty(head)) {
2685 struct lnet_msg *msg;
2686 struct lnet_process_id id;
2688 msg = list_entry(head->next, struct lnet_msg, msg_list);
2689 list_del(&msg->msg_list);
2691 /* md won't disappear under me, since each msg
2692 * holds a ref on it */
2694 id.nid = msg->msg_hdr.src_nid;
2695 id.pid = msg->msg_hdr.src_pid;
2697 LASSERT(msg->msg_rx_delayed);
2698 LASSERT(msg->msg_md != NULL);
2699 LASSERT(msg->msg_rxpeer != NULL);
2700 LASSERT(msg->msg_rxni != NULL);
2701 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
2703 CDEBUG(D_NET, "Resuming delayed PUT from %s portal %d "
2704 "match %llu offset %d length %d.\n",
2705 libcfs_id2str(id), msg->msg_hdr.msg.put.ptl_index,
2706 msg->msg_hdr.msg.put.match_bits,
2707 msg->msg_hdr.msg.put.offset,
2708 msg->msg_hdr.payload_length);
2710 lnet_recv_put(msg->msg_rxni, msg);
2715 * Initiate an asynchronous PUT operation.
2717 * There are several events associated with a PUT: completion of the send on
2718 * the initiator node (LNET_EVENT_SEND), and when the send completes
2719 * successfully, the receipt of an acknowledgment (LNET_EVENT_ACK) indicating
2720 * that the operation was accepted by the target. The event LNET_EVENT_PUT is
2721 * used at the target node to indicate the completion of incoming data
2724 * The local events will be logged in the EQ associated with the MD pointed to
2725 * by \a mdh handle. Using a MD without an associated EQ results in these
2726 * events being discarded. In this case, the caller must have another
2727 * mechanism (e.g., a higher level protocol) for determining when it is safe
2728 * to modify the memory region associated with the MD.
2730 * Note that LNet does not guarantee the order of LNET_EVENT_SEND and
2731 * LNET_EVENT_ACK, though intuitively ACK should happen after SEND.
2733 * \param self Indicates the NID of a local interface through which to send
2734 * the PUT request. Use LNET_NID_ANY to let LNet choose one by itself.
2735 * \param mdh A handle for the MD that describes the memory to be sent. The MD
2736 * must be "free floating" (See LNetMDBind()).
2737 * \param ack Controls whether an acknowledgment is requested.
2738 * Acknowledgments are only sent when they are requested by the initiating
2739 * process and the target MD enables them.
2740 * \param target A process identifier for the target process.
2741 * \param portal The index in the \a target's portal table.
2742 * \param match_bits The match bits to use for MD selection at the target
2744 * \param offset The offset into the target MD (only used when the target
2745 * MD has the LNET_MD_MANAGE_REMOTE option set).
2746 * \param hdr_data 64 bits of user data that can be included in the message
2747 * header. This data is written to an event queue entry at the target if an
2748 * EQ is present on the matching MD.
2750 * \retval 0 Success, and only in this case events will be generated
2751 * and logged to EQ (if it exists).
2752 * \retval -EIO Simulated failure.
2753 * \retval -ENOMEM Memory allocation failure.
2754 * \retval -ENOENT Invalid MD object.
2756 * \see struct lnet_event::hdr_data and lnet_event_kind_t.
2759 LNetPut(lnet_nid_t self, struct lnet_handle_md mdh, enum lnet_ack_req ack,
2760 struct lnet_process_id target, unsigned int portal,
2761 __u64 match_bits, unsigned int offset,
2764 struct lnet_msg *msg;
2765 struct lnet_libmd *md;
2769 LASSERT(the_lnet.ln_refcount > 0);
2771 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
2772 fail_peer(target.nid, 1)) { /* shall we now? */
2773 CERROR("Dropping PUT to %s: simulated failure\n",
2774 libcfs_id2str(target));
2778 msg = lnet_msg_alloc();
2780 CERROR("Dropping PUT to %s: ENOMEM on struct lnet_msg\n",
2781 libcfs_id2str(target));
2784 msg->msg_vmflush = !!memory_pressure_get();
2786 cpt = lnet_cpt_of_cookie(mdh.cookie);
2789 md = lnet_handle2md(&mdh);
2790 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
2791 CERROR("Dropping PUT (%llu:%d:%s): MD (%d) invalid\n",
2792 match_bits, portal, libcfs_id2str(target),
2793 md == NULL ? -1 : md->md_threshold);
2794 if (md != NULL && md->md_me != NULL)
2795 CERROR("Source MD also attached to portal %d\n",
2796 md->md_me->me_portal);
2797 lnet_res_unlock(cpt);
2803 CDEBUG(D_NET, "LNetPut -> %s\n", libcfs_id2str(target));
2805 lnet_msg_attach_md(msg, md, 0, 0);
2807 lnet_prep_send(msg, LNET_MSG_PUT, target, 0, md->md_length);
2809 msg->msg_hdr.msg.put.match_bits = cpu_to_le64(match_bits);
2810 msg->msg_hdr.msg.put.ptl_index = cpu_to_le32(portal);
2811 msg->msg_hdr.msg.put.offset = cpu_to_le32(offset);
2812 msg->msg_hdr.msg.put.hdr_data = hdr_data;
2814 /* NB handles only looked up by creator (no flips) */
2815 if (ack == LNET_ACK_REQ) {
2816 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
2817 the_lnet.ln_interface_cookie;
2818 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
2819 md->md_lh.lh_cookie;
2821 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
2822 LNET_WIRE_HANDLE_COOKIE_NONE;
2823 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
2824 LNET_WIRE_HANDLE_COOKIE_NONE;
2827 lnet_res_unlock(cpt);
2829 lnet_build_msg_event(msg, LNET_EVENT_SEND);
2831 rc = lnet_send(self, msg, LNET_NID_ANY);
2833 CNETERR("Error sending PUT to %s: %d\n",
2834 libcfs_id2str(target), rc);
2835 lnet_finalize(msg, rc);
2838 /* completion will be signalled by an event */
2841 EXPORT_SYMBOL(LNetPut);
2844 * The LND can DMA direct to the GET md (i.e. no REPLY msg). This
2845 * returns a msg for the LND to pass to lnet_finalize() when the sink
2846 * data has been received.
2848 * CAVEAT EMPTOR: 'getmsg' is the original GET, which is freed when
2849 * lnet_finalize() is called on it, so the LND must call this first
2852 lnet_create_reply_msg(struct lnet_ni *ni, struct lnet_msg *getmsg)
2854 struct lnet_msg *msg = lnet_msg_alloc();
2855 struct lnet_libmd *getmd = getmsg->msg_md;
2856 struct lnet_process_id peer_id = getmsg->msg_target;
2859 LASSERT(!getmsg->msg_target_is_router);
2860 LASSERT(!getmsg->msg_routing);
2863 CERROR("%s: Dropping REPLY from %s: can't allocate msg\n",
2864 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id));
2868 cpt = lnet_cpt_of_cookie(getmd->md_lh.lh_cookie);
2871 LASSERT(getmd->md_refcount > 0);
2873 if (getmd->md_threshold == 0) {
2874 CERROR("%s: Dropping REPLY from %s for inactive MD %p\n",
2875 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id),
2877 lnet_res_unlock(cpt);
2881 LASSERT(getmd->md_offset == 0);
2883 CDEBUG(D_NET, "%s: Reply from %s md %p\n",
2884 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id), getmd);
2886 /* setup information for lnet_build_msg_event */
2887 msg->msg_initiator = getmsg->msg_txpeer->lpni_peer_net->lpn_peer->lp_primary_nid;
2888 msg->msg_from = peer_id.nid;
2889 msg->msg_type = LNET_MSG_GET; /* flag this msg as an "optimized" GET */
2890 msg->msg_hdr.src_nid = peer_id.nid;
2891 msg->msg_hdr.payload_length = getmd->md_length;
2892 msg->msg_receiving = 1; /* required by lnet_msg_attach_md */
2894 lnet_msg_attach_md(msg, getmd, getmd->md_offset, getmd->md_length);
2895 lnet_res_unlock(cpt);
2897 cpt = lnet_cpt_of_nid(peer_id.nid, ni);
2900 lnet_msg_commit(msg, cpt);
2901 lnet_net_unlock(cpt);
2903 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
2908 cpt = lnet_cpt_of_nid(peer_id.nid, ni);
2911 the_lnet.ln_counters[cpt]->drop_count++;
2912 the_lnet.ln_counters[cpt]->drop_length += getmd->md_length;
2913 lnet_net_unlock(cpt);
2920 EXPORT_SYMBOL(lnet_create_reply_msg);
2923 lnet_set_reply_msg_len(struct lnet_ni *ni, struct lnet_msg *reply,
2926 /* Set the REPLY length, now the RDMA that elides the REPLY message has
2927 * completed and I know it. */
2928 LASSERT(reply != NULL);
2929 LASSERT(reply->msg_type == LNET_MSG_GET);
2930 LASSERT(reply->msg_ev.type == LNET_EVENT_REPLY);
2932 /* NB I trusted my peer to RDMA. If she tells me she's written beyond
2933 * the end of my buffer, I might as well be dead. */
2934 LASSERT(len <= reply->msg_ev.mlength);
2936 reply->msg_ev.mlength = len;
2938 EXPORT_SYMBOL(lnet_set_reply_msg_len);
2941 * Initiate an asynchronous GET operation.
2943 * On the initiator node, an LNET_EVENT_SEND is logged when the GET request
2944 * is sent, and an LNET_EVENT_REPLY is logged when the data returned from
2945 * the target node in the REPLY has been written to local MD.
2947 * On the target node, an LNET_EVENT_GET is logged when the GET request
2948 * arrives and is accepted into a MD.
2950 * \param self,target,portal,match_bits,offset See the discussion in LNetPut().
2951 * \param mdh A handle for the MD that describes the memory into which the
2952 * requested data will be received. The MD must be "free floating" (See LNetMDBind()).
2954 * \retval 0 Success, and only in this case events will be generated
2955 * and logged to EQ (if it exists) of the MD.
2956 * \retval -EIO Simulated failure.
2957 * \retval -ENOMEM Memory allocation failure.
2958 * \retval -ENOENT Invalid MD object.
2961 LNetGet(lnet_nid_t self, struct lnet_handle_md mdh,
2962 struct lnet_process_id target, unsigned int portal,
2963 __u64 match_bits, unsigned int offset)
2965 struct lnet_msg *msg;
2966 struct lnet_libmd *md;
2970 LASSERT(the_lnet.ln_refcount > 0);
2972 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
2973 fail_peer(target.nid, 1)) /* shall we now? */
2975 CERROR("Dropping GET to %s: simulated failure\n",
2976 libcfs_id2str(target));
2980 msg = lnet_msg_alloc();
2982 CERROR("Dropping GET to %s: ENOMEM on struct lnet_msg\n",
2983 libcfs_id2str(target));
2987 cpt = lnet_cpt_of_cookie(mdh.cookie);
2990 md = lnet_handle2md(&mdh);
2991 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
2992 CERROR("Dropping GET (%llu:%d:%s): MD (%d) invalid\n",
2993 match_bits, portal, libcfs_id2str(target),
2994 md == NULL ? -1 : md->md_threshold);
2995 if (md != NULL && md->md_me != NULL)
2996 CERROR("REPLY MD also attached to portal %d\n",
2997 md->md_me->me_portal);
2999 lnet_res_unlock(cpt);
3005 CDEBUG(D_NET, "LNetGet -> %s\n", libcfs_id2str(target));
3007 lnet_msg_attach_md(msg, md, 0, 0);
3009 lnet_prep_send(msg, LNET_MSG_GET, target, 0, 0);
3011 msg->msg_hdr.msg.get.match_bits = cpu_to_le64(match_bits);
3012 msg->msg_hdr.msg.get.ptl_index = cpu_to_le32(portal);
3013 msg->msg_hdr.msg.get.src_offset = cpu_to_le32(offset);
3014 msg->msg_hdr.msg.get.sink_length = cpu_to_le32(md->md_length);
3016 /* NB handles only looked up by creator (no flips) */
3017 msg->msg_hdr.msg.get.return_wmd.wh_interface_cookie =
3018 the_lnet.ln_interface_cookie;
3019 msg->msg_hdr.msg.get.return_wmd.wh_object_cookie =
3020 md->md_lh.lh_cookie;
3022 lnet_res_unlock(cpt);
3024 lnet_build_msg_event(msg, LNET_EVENT_SEND);
3026 rc = lnet_send(self, msg, LNET_NID_ANY);
3028 CNETERR("Error sending GET to %s: %d\n",
3029 libcfs_id2str(target), rc);
3030 lnet_finalize(msg, rc);
3033 /* completion will be signalled by an event */
3036 EXPORT_SYMBOL(LNetGet);
3039 * Calculate distance to node at \a dstnid.
3041 * \param dstnid Target NID.
3042 * \param srcnidp If not NULL, NID of the local interface to reach \a dstnid
3044 * \param orderp If not NULL, order of the route to reach \a dstnid is saved
3047 * \retval 0 If \a dstnid belongs to a local interface, and reserved option
3048 * local_nid_dist_zero is set, which is the default.
3049 * \retval positives Distance to target NID, i.e. number of hops plus one.
3050 * \retval -EHOSTUNREACH If \a dstnid is not reachable.
3053 LNetDist(lnet_nid_t dstnid, lnet_nid_t *srcnidp, __u32 *orderp)
3055 struct list_head *e;
3056 struct lnet_ni *ni = NULL;
3057 struct lnet_remotenet *rnet;
3058 __u32 dstnet = LNET_NIDNET(dstnid);
3062 struct list_head *rn_list;
3064 /* if !local_nid_dist_zero, I don't return a distance of 0 ever
3065 * (when lustre sees a distance of 0, it substitutes 0@lo), so I
3066 * keep order 0 free for 0@lo and order 1 free for a local NID
3069 LASSERT(the_lnet.ln_refcount > 0);
3071 cpt = lnet_net_lock_current();
3073 while ((ni = lnet_get_next_ni_locked(NULL, ni))) {
3074 if (ni->ni_nid == dstnid) {
3075 if (srcnidp != NULL)
3077 if (orderp != NULL) {
3078 if (LNET_NETTYP(LNET_NIDNET(dstnid)) == LOLND)
3083 lnet_net_unlock(cpt);
3085 return local_nid_dist_zero ? 0 : 1;
3088 if (LNET_NIDNET(ni->ni_nid) == dstnet) {
3089 /* Check if ni was originally created in
3090 * current net namespace.
3091 * If not, assign order above 0xffff0000,
3092 * to make this ni not a priority. */
3093 if (!net_eq(ni->ni_net_ns, current->nsproxy->net_ns))
3094 order += 0xffff0000;
3096 if (srcnidp != NULL)
3097 *srcnidp = ni->ni_nid;
3100 lnet_net_unlock(cpt);
3107 rn_list = lnet_net2rnethash(dstnet);
3108 list_for_each(e, rn_list) {
3109 rnet = list_entry(e, struct lnet_remotenet, lrn_list);
3111 if (rnet->lrn_net == dstnet) {
3112 struct lnet_route *route;
3113 struct lnet_route *shortest = NULL;
3114 __u32 shortest_hops = LNET_UNDEFINED_HOPS;
3117 LASSERT(!list_empty(&rnet->lrn_routes));
3119 list_for_each_entry(route, &rnet->lrn_routes,
3121 route_hops = route->lr_hops;
3122 if (route_hops == LNET_UNDEFINED_HOPS)
3124 if (shortest == NULL ||
3125 route_hops < shortest_hops) {
3127 shortest_hops = route_hops;
3131 LASSERT(shortest != NULL);
3132 hops = shortest_hops;
3133 if (srcnidp != NULL) {
3134 ni = lnet_get_next_ni_locked(
3135 shortest->lr_gateway->lpni_net,
3137 *srcnidp = ni->ni_nid;
3141 lnet_net_unlock(cpt);
3147 lnet_net_unlock(cpt);
3148 return -EHOSTUNREACH;
3150 EXPORT_SYMBOL(LNetDist);