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)
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, lnet_test_peer_t, 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, lnet_test_peer_t, tp_list);
92 list_del(&tp->tp_list);
93 LIBCFS_FREE(tp, sizeof(*tp));
99 fail_peer (lnet_nid_t nid, int outgoing)
101 lnet_test_peer_t *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, lnet_test_peer_t, 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, lnet_test_peer_t, 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(lnet_ni_t *ni, void *private, lnet_msg_t *msg, int delayed,
562 unsigned int offset, unsigned int mlen, unsigned int rlen)
564 unsigned int niov = 0;
565 struct kvec *iov = NULL;
566 lnet_kiov_t *kiov = NULL;
569 LASSERT (!in_interrupt ());
570 LASSERT (mlen == 0 || msg != NULL);
573 LASSERT(msg->msg_receiving);
574 LASSERT(!msg->msg_sending);
575 LASSERT(rlen == msg->msg_len);
576 LASSERT(mlen <= msg->msg_len);
577 LASSERT(msg->msg_offset == offset);
578 LASSERT(msg->msg_wanted == mlen);
580 msg->msg_receiving = 0;
583 niov = msg->msg_niov;
585 kiov = msg->msg_kiov;
588 LASSERT ((iov == NULL) != (kiov == NULL));
592 rc = (ni->ni_net->net_lnd->lnd_recv)(ni, private, msg, delayed,
593 niov, iov, kiov, offset, mlen,
596 lnet_finalize(ni, msg, rc);
600 lnet_setpayloadbuffer(lnet_msg_t *msg)
602 lnet_libmd_t *md = msg->msg_md;
604 LASSERT(msg->msg_len > 0);
605 LASSERT(!msg->msg_routing);
607 LASSERT(msg->msg_niov == 0);
608 LASSERT(msg->msg_iov == NULL);
609 LASSERT(msg->msg_kiov == NULL);
611 msg->msg_niov = md->md_niov;
612 if ((md->md_options & LNET_MD_KIOV) != 0)
613 msg->msg_kiov = md->md_iov.kiov;
615 msg->msg_iov = md->md_iov.iov;
619 lnet_prep_send(lnet_msg_t *msg, int type, lnet_process_id_t target,
620 unsigned int offset, unsigned int len)
622 msg->msg_type = type;
623 msg->msg_target = target;
625 msg->msg_offset = offset;
628 lnet_setpayloadbuffer(msg);
630 memset (&msg->msg_hdr, 0, sizeof (msg->msg_hdr));
631 msg->msg_hdr.type = cpu_to_le32(type);
632 msg->msg_hdr.dest_pid = cpu_to_le32(target.pid);
633 /* src_nid will be set later */
634 msg->msg_hdr.src_pid = cpu_to_le32(the_lnet.ln_pid);
635 msg->msg_hdr.payload_length = cpu_to_le32(len);
639 lnet_ni_send(lnet_ni_t *ni, lnet_msg_t *msg)
641 void *priv = msg->msg_private;
644 LASSERT (!in_interrupt ());
645 LASSERT (LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND ||
646 (msg->msg_txcredit && msg->msg_peertxcredit));
648 rc = (ni->ni_net->net_lnd->lnd_send)(ni, priv, msg);
650 lnet_finalize(ni, msg, rc);
654 lnet_ni_eager_recv(lnet_ni_t *ni, lnet_msg_t *msg)
658 LASSERT(!msg->msg_sending);
659 LASSERT(msg->msg_receiving);
660 LASSERT(!msg->msg_rx_ready_delay);
661 LASSERT(ni->ni_net->net_lnd->lnd_eager_recv != NULL);
663 msg->msg_rx_ready_delay = 1;
664 rc = (ni->ni_net->net_lnd->lnd_eager_recv)(ni, msg->msg_private, msg,
667 CERROR("recv from %s / send to %s aborted: "
668 "eager_recv failed %d\n",
669 libcfs_nid2str(msg->msg_rxpeer->lpni_nid),
670 libcfs_id2str(msg->msg_target), rc);
671 LASSERT(rc < 0); /* required by my callers */
678 * This function can be called from two paths:
679 * 1. when sending a message
680 * 2. when decommiting a message (lnet_msg_decommit_tx())
681 * In both these cases the peer_ni should have it's reference count
682 * acquired by the caller and therefore it is safe to drop the spin
683 * lock before calling lnd_query()
686 lnet_ni_query_locked(lnet_ni_t *ni, struct lnet_peer_ni *lp)
688 cfs_time_t last_alive = 0;
689 int cpt = lnet_cpt_of_nid_locked(lp->lpni_nid, ni);
691 LASSERT(lnet_peer_aliveness_enabled(lp));
692 LASSERT(ni->ni_net->net_lnd->lnd_query != NULL);
694 lnet_net_unlock(cpt);
695 (ni->ni_net->net_lnd->lnd_query)(ni, lp->lpni_nid, &last_alive);
698 lp->lpni_last_query = cfs_time_current();
700 if (last_alive != 0) /* NI has updated timestamp */
701 lp->lpni_last_alive = last_alive;
704 /* NB: always called with lnet_net_lock held */
706 lnet_peer_is_alive (struct lnet_peer_ni *lp, cfs_time_t now)
711 LASSERT (lnet_peer_aliveness_enabled(lp));
714 * Trust lnet_notify() if it has more recent aliveness news, but
715 * ignore the initial assumed death (see lnet_peers_start_down()).
717 spin_lock(&lp->lpni_lock);
718 if (!lp->lpni_alive && lp->lpni_alive_count > 0 &&
719 cfs_time_aftereq(lp->lpni_timestamp, lp->lpni_last_alive)) {
720 spin_unlock(&lp->lpni_lock);
725 cfs_time_add(lp->lpni_last_alive,
726 cfs_time_seconds(lp->lpni_net->net_tunables.
728 alive = cfs_time_after(deadline, now);
731 * Update obsolete lp_alive except for routers assumed to be dead
732 * initially, because router checker would update aliveness in this
733 * case, and moreover lpni_last_alive at peer creation is assumed.
735 if (alive && !lp->lpni_alive &&
736 !(lnet_isrouter(lp) && lp->lpni_alive_count == 0)) {
737 spin_unlock(&lp->lpni_lock);
738 lnet_notify_locked(lp, 0, 1, lp->lpni_last_alive);
740 spin_unlock(&lp->lpni_lock);
747 /* NB: returns 1 when alive, 0 when dead, negative when error;
748 * may drop the lnet_net_lock */
750 lnet_peer_alive_locked (struct lnet_ni *ni, struct lnet_peer_ni *lp)
752 cfs_time_t now = cfs_time_current();
754 if (!lnet_peer_aliveness_enabled(lp))
757 if (lnet_peer_is_alive(lp, now))
761 * Peer appears dead, but we should avoid frequent NI queries (at
762 * most once per lnet_queryinterval seconds).
764 if (lp->lpni_last_query != 0) {
765 static const int lnet_queryinterval = 1;
767 cfs_time_t next_query =
768 cfs_time_add(lp->lpni_last_query,
769 cfs_time_seconds(lnet_queryinterval));
771 if (cfs_time_before(now, next_query)) {
773 CWARN("Unexpected aliveness of peer %s: "
775 libcfs_nid2str(lp->lpni_nid),
776 (int)now, (int)next_query,
778 lp->lpni_net->net_tunables.lct_peer_timeout);
783 /* query NI for latest aliveness news */
784 lnet_ni_query_locked(ni, lp);
786 if (lnet_peer_is_alive(lp, now))
789 lnet_notify_locked(lp, 0, 0, lp->lpni_last_alive);
794 * \param msg The message to be sent.
795 * \param do_send True if lnet_ni_send() should be called in this function.
796 * lnet_send() is going to lnet_net_unlock immediately after this, so
797 * it sets do_send FALSE and I don't do the unlock/send/lock bit.
799 * \retval LNET_CREDIT_OK If \a msg sent or OK to send.
800 * \retval LNET_CREDIT_WAIT If \a msg blocked for credit.
801 * \retval -EHOSTUNREACH If the next hop of the message appears dead.
802 * \retval -ECANCELED If the MD of the message has been unlinked.
805 lnet_post_send_locked(lnet_msg_t *msg, int do_send)
807 struct lnet_peer_ni *lp = msg->msg_txpeer;
808 struct lnet_ni *ni = msg->msg_txni;
809 int cpt = msg->msg_tx_cpt;
810 struct lnet_tx_queue *tq = ni->ni_tx_queues[cpt];
812 /* non-lnet_send() callers have checked before */
813 LASSERT(!do_send || msg->msg_tx_delayed);
814 LASSERT(!msg->msg_receiving);
815 LASSERT(msg->msg_tx_committed);
817 /* NB 'lp' is always the next hop */
818 if ((msg->msg_target.pid & LNET_PID_USERFLAG) == 0 &&
819 lnet_peer_alive_locked(ni, lp) == 0) {
820 the_lnet.ln_counters[cpt]->drop_count++;
821 the_lnet.ln_counters[cpt]->drop_length += msg->msg_len;
822 lnet_net_unlock(cpt);
824 atomic_inc(&msg->msg_txpeer->lpni_stats.drop_count);
826 atomic_inc(&msg->msg_txni->ni_stats.drop_count);
828 CNETERR("Dropping message for %s: peer not alive\n",
829 libcfs_id2str(msg->msg_target));
831 lnet_finalize(ni, msg, -EHOSTUNREACH);
834 return -EHOSTUNREACH;
837 if (msg->msg_md != NULL &&
838 (msg->msg_md->md_flags & LNET_MD_FLAG_ABORTED) != 0) {
839 lnet_net_unlock(cpt);
841 CNETERR("Aborting message for %s: LNetM[DE]Unlink() already "
842 "called on the MD/ME.\n",
843 libcfs_id2str(msg->msg_target));
845 lnet_finalize(ni, msg, -ECANCELED);
851 if (!msg->msg_peertxcredit) {
852 spin_lock(&lp->lpni_lock);
853 LASSERT((lp->lpni_txcredits < 0) ==
854 !list_empty(&lp->lpni_txq));
856 msg->msg_peertxcredit = 1;
857 lp->lpni_txqnob += msg->msg_len + sizeof(lnet_hdr_t);
858 lp->lpni_txcredits--;
860 if (lp->lpni_txcredits < lp->lpni_mintxcredits)
861 lp->lpni_mintxcredits = lp->lpni_txcredits;
863 if (lp->lpni_txcredits < 0) {
864 msg->msg_tx_delayed = 1;
865 list_add_tail(&msg->msg_list, &lp->lpni_txq);
866 spin_unlock(&lp->lpni_lock);
867 return LNET_CREDIT_WAIT;
869 spin_unlock(&lp->lpni_lock);
872 if (!msg->msg_txcredit) {
873 LASSERT((tq->tq_credits < 0) ==
874 !list_empty(&tq->tq_delayed));
876 msg->msg_txcredit = 1;
878 atomic_dec(&ni->ni_tx_credits);
880 if (tq->tq_credits < tq->tq_credits_min)
881 tq->tq_credits_min = tq->tq_credits;
883 if (tq->tq_credits < 0) {
884 msg->msg_tx_delayed = 1;
885 list_add_tail(&msg->msg_list, &tq->tq_delayed);
886 return LNET_CREDIT_WAIT;
891 lnet_net_unlock(cpt);
892 lnet_ni_send(ni, msg);
895 return LNET_CREDIT_OK;
899 static lnet_rtrbufpool_t *
900 lnet_msg2bufpool(lnet_msg_t *msg)
902 lnet_rtrbufpool_t *rbp;
905 LASSERT(msg->msg_rx_committed);
907 cpt = msg->msg_rx_cpt;
908 rbp = &the_lnet.ln_rtrpools[cpt][0];
910 LASSERT(msg->msg_len <= LNET_MTU);
911 while (msg->msg_len > (unsigned int)rbp->rbp_npages * PAGE_SIZE) {
913 LASSERT(rbp < &the_lnet.ln_rtrpools[cpt][LNET_NRBPOOLS]);
920 lnet_post_routed_recv_locked (lnet_msg_t *msg, int do_recv)
922 /* lnet_parse is going to lnet_net_unlock immediately after this, so it
923 * sets do_recv FALSE and I don't do the unlock/send/lock bit.
924 * I return LNET_CREDIT_WAIT if msg blocked and LNET_CREDIT_OK if
925 * received or OK to receive */
926 struct lnet_peer_ni *lp = msg->msg_rxpeer;
927 lnet_rtrbufpool_t *rbp;
930 LASSERT (msg->msg_iov == NULL);
931 LASSERT (msg->msg_kiov == NULL);
932 LASSERT (msg->msg_niov == 0);
933 LASSERT (msg->msg_routing);
934 LASSERT (msg->msg_receiving);
935 LASSERT (!msg->msg_sending);
937 /* non-lnet_parse callers only receive delayed messages */
938 LASSERT(!do_recv || msg->msg_rx_delayed);
940 if (!msg->msg_peerrtrcredit) {
941 spin_lock(&lp->lpni_lock);
942 LASSERT((lp->lpni_rtrcredits < 0) ==
943 !list_empty(&lp->lpni_rtrq));
945 msg->msg_peerrtrcredit = 1;
946 lp->lpni_rtrcredits--;
947 if (lp->lpni_rtrcredits < lp->lpni_minrtrcredits)
948 lp->lpni_minrtrcredits = lp->lpni_rtrcredits;
950 if (lp->lpni_rtrcredits < 0) {
951 /* must have checked eager_recv before here */
952 LASSERT(msg->msg_rx_ready_delay);
953 msg->msg_rx_delayed = 1;
954 list_add_tail(&msg->msg_list, &lp->lpni_rtrq);
955 spin_unlock(&lp->lpni_lock);
956 return LNET_CREDIT_WAIT;
958 spin_unlock(&lp->lpni_lock);
961 rbp = lnet_msg2bufpool(msg);
963 if (!msg->msg_rtrcredit) {
964 msg->msg_rtrcredit = 1;
966 if (rbp->rbp_credits < rbp->rbp_mincredits)
967 rbp->rbp_mincredits = rbp->rbp_credits;
969 if (rbp->rbp_credits < 0) {
970 /* must have checked eager_recv before here */
971 LASSERT(msg->msg_rx_ready_delay);
972 msg->msg_rx_delayed = 1;
973 list_add_tail(&msg->msg_list, &rbp->rbp_msgs);
974 return LNET_CREDIT_WAIT;
978 LASSERT(!list_empty(&rbp->rbp_bufs));
979 rb = list_entry(rbp->rbp_bufs.next, lnet_rtrbuf_t, rb_list);
980 list_del(&rb->rb_list);
982 msg->msg_niov = rbp->rbp_npages;
983 msg->msg_kiov = &rb->rb_kiov[0];
986 int cpt = msg->msg_rx_cpt;
988 lnet_net_unlock(cpt);
989 lnet_ni_recv(msg->msg_rxni, msg->msg_private, msg, 1,
990 0, msg->msg_len, msg->msg_len);
993 return LNET_CREDIT_OK;
997 lnet_return_tx_credits_locked(lnet_msg_t *msg)
999 struct lnet_peer_ni *txpeer = msg->msg_txpeer;
1000 struct lnet_ni *txni = msg->msg_txni;
1003 if (msg->msg_txcredit) {
1004 struct lnet_ni *ni = msg->msg_txni;
1005 struct lnet_tx_queue *tq = ni->ni_tx_queues[msg->msg_tx_cpt];
1007 /* give back NI txcredits */
1008 msg->msg_txcredit = 0;
1010 LASSERT((tq->tq_credits < 0) ==
1011 !list_empty(&tq->tq_delayed));
1014 atomic_inc(&ni->ni_tx_credits);
1015 if (tq->tq_credits <= 0) {
1016 msg2 = list_entry(tq->tq_delayed.next,
1017 lnet_msg_t, msg_list);
1018 list_del(&msg2->msg_list);
1020 LASSERT(msg2->msg_txni == ni);
1021 LASSERT(msg2->msg_tx_delayed);
1022 LASSERT(msg2->msg_tx_cpt == msg->msg_tx_cpt);
1024 (void) lnet_post_send_locked(msg2, 1);
1028 if (msg->msg_peertxcredit) {
1029 /* give back peer txcredits */
1030 msg->msg_peertxcredit = 0;
1032 spin_lock(&txpeer->lpni_lock);
1033 LASSERT((txpeer->lpni_txcredits < 0) ==
1034 !list_empty(&txpeer->lpni_txq));
1036 txpeer->lpni_txqnob -= msg->msg_len + sizeof(lnet_hdr_t);
1037 LASSERT(txpeer->lpni_txqnob >= 0);
1039 txpeer->lpni_txcredits++;
1040 if (txpeer->lpni_txcredits <= 0) {
1041 msg2 = list_entry(txpeer->lpni_txq.next,
1042 lnet_msg_t, msg_list);
1043 list_del(&msg2->msg_list);
1044 spin_unlock(&txpeer->lpni_lock);
1046 LASSERT(msg2->msg_txpeer == txpeer);
1047 LASSERT(msg2->msg_tx_delayed);
1049 if (msg2->msg_tx_cpt != msg->msg_tx_cpt) {
1050 lnet_net_unlock(msg->msg_tx_cpt);
1051 lnet_net_lock(msg2->msg_tx_cpt);
1053 (void) lnet_post_send_locked(msg2, 1);
1054 if (msg2->msg_tx_cpt != msg->msg_tx_cpt) {
1055 lnet_net_unlock(msg2->msg_tx_cpt);
1056 lnet_net_lock(msg->msg_tx_cpt);
1059 spin_unlock(&txpeer->lpni_lock);
1064 msg->msg_txni = NULL;
1065 lnet_ni_decref_locked(txni, msg->msg_tx_cpt);
1068 if (txpeer != NULL) {
1071 * Once the patch for the health comes in we need to set
1072 * the health of the peer ni to bad when we fail to send
1074 * int status = msg->msg_ev.status;
1076 * lnet_set_peer_ni_health_locked(txpeer, false)
1078 msg->msg_txpeer = NULL;
1079 lnet_peer_ni_decref_locked(txpeer);
1084 lnet_schedule_blocked_locked(lnet_rtrbufpool_t *rbp)
1088 if (list_empty(&rbp->rbp_msgs))
1090 msg = list_entry(rbp->rbp_msgs.next,
1091 lnet_msg_t, msg_list);
1092 list_del(&msg->msg_list);
1094 (void)lnet_post_routed_recv_locked(msg, 1);
1098 lnet_drop_routed_msgs_locked(struct list_head *list, int cpt)
1103 lnet_net_unlock(cpt);
1105 list_for_each_entry_safe(msg, tmp, list, msg_list) {
1106 lnet_ni_recv(msg->msg_rxni, msg->msg_private, NULL,
1107 0, 0, 0, msg->msg_hdr.payload_length);
1108 list_del_init(&msg->msg_list);
1109 lnet_finalize(NULL, msg, -ECANCELED);
1116 lnet_return_rx_credits_locked(lnet_msg_t *msg)
1118 struct lnet_peer_ni *rxpeer = msg->msg_rxpeer;
1119 struct lnet_ni *rxni = msg->msg_rxni;
1122 if (msg->msg_rtrcredit) {
1123 /* give back global router credits */
1125 lnet_rtrbufpool_t *rbp;
1127 /* NB If a msg ever blocks for a buffer in rbp_msgs, it stays
1128 * there until it gets one allocated, or aborts the wait
1130 LASSERT(msg->msg_kiov != NULL);
1132 rb = list_entry(msg->msg_kiov, lnet_rtrbuf_t, rb_kiov[0]);
1135 msg->msg_kiov = NULL;
1136 msg->msg_rtrcredit = 0;
1138 LASSERT(rbp == lnet_msg2bufpool(msg));
1140 LASSERT((rbp->rbp_credits > 0) ==
1141 !list_empty(&rbp->rbp_bufs));
1143 /* If routing is now turned off, we just drop this buffer and
1144 * don't bother trying to return credits. */
1145 if (!the_lnet.ln_routing) {
1146 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1150 /* It is possible that a user has lowered the desired number of
1151 * buffers in this pool. Make sure we never put back
1152 * more buffers than the stated number. */
1153 if (unlikely(rbp->rbp_credits >= rbp->rbp_req_nbuffers)) {
1154 /* Discard this buffer so we don't have too
1156 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1157 rbp->rbp_nbuffers--;
1159 list_add(&rb->rb_list, &rbp->rbp_bufs);
1161 if (rbp->rbp_credits <= 0)
1162 lnet_schedule_blocked_locked(rbp);
1167 if (msg->msg_peerrtrcredit) {
1168 /* give back peer router credits */
1169 msg->msg_peerrtrcredit = 0;
1171 spin_lock(&rxpeer->lpni_lock);
1172 LASSERT((rxpeer->lpni_rtrcredits < 0) ==
1173 !list_empty(&rxpeer->lpni_rtrq));
1175 rxpeer->lpni_rtrcredits++;
1177 /* drop all messages which are queued to be routed on that
1179 if (!the_lnet.ln_routing) {
1180 struct list_head drop;
1181 INIT_LIST_HEAD(&drop);
1182 list_splice_init(&rxpeer->lpni_rtrq, &drop);
1183 spin_unlock(&rxpeer->lpni_lock);
1184 lnet_drop_routed_msgs_locked(&drop, msg->msg_rx_cpt);
1185 } else if (rxpeer->lpni_rtrcredits <= 0) {
1186 msg2 = list_entry(rxpeer->lpni_rtrq.next,
1187 lnet_msg_t, msg_list);
1188 list_del(&msg2->msg_list);
1189 spin_unlock(&rxpeer->lpni_lock);
1190 (void) lnet_post_routed_recv_locked(msg2, 1);
1192 spin_unlock(&rxpeer->lpni_lock);
1196 msg->msg_rxni = NULL;
1197 lnet_ni_decref_locked(rxni, msg->msg_rx_cpt);
1199 if (rxpeer != NULL) {
1200 msg->msg_rxpeer = NULL;
1201 lnet_peer_ni_decref_locked(rxpeer);
1206 lnet_compare_peers(struct lnet_peer_ni *p1, struct lnet_peer_ni *p2)
1208 if (p1->lpni_txqnob < p2->lpni_txqnob)
1211 if (p1->lpni_txqnob > p2->lpni_txqnob)
1214 if (p1->lpni_txcredits > p2->lpni_txcredits)
1217 if (p1->lpni_txcredits < p2->lpni_txcredits)
1224 lnet_compare_routes(lnet_route_t *r1, lnet_route_t *r2)
1226 struct lnet_peer_ni *p1 = r1->lr_gateway;
1227 struct lnet_peer_ni *p2 = r2->lr_gateway;
1228 int r1_hops = (r1->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r1->lr_hops;
1229 int r2_hops = (r2->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r2->lr_hops;
1232 if (r1->lr_priority < r2->lr_priority)
1235 if (r1->lr_priority > r2->lr_priority)
1238 if (r1_hops < r2_hops)
1241 if (r1_hops > r2_hops)
1244 rc = lnet_compare_peers(p1, p2);
1248 if (r1->lr_seq - r2->lr_seq <= 0)
1254 static struct lnet_peer_ni *
1255 lnet_find_route_locked(struct lnet_net *net, lnet_nid_t target,
1258 lnet_remotenet_t *rnet;
1259 lnet_route_t *route;
1260 lnet_route_t *best_route;
1261 lnet_route_t *last_route;
1262 struct lnet_peer_ni *lpni_best;
1263 struct lnet_peer_ni *lp;
1266 /* If @rtr_nid is not LNET_NID_ANY, return the gateway with
1267 * rtr_nid nid, otherwise find the best gateway I can use */
1269 rnet = lnet_find_rnet_locked(LNET_NIDNET(target));
1274 best_route = last_route = NULL;
1275 list_for_each_entry(route, &rnet->lrn_routes, lr_list) {
1276 lp = route->lr_gateway;
1278 if (!lnet_is_route_alive(route))
1281 if (net != NULL && lp->lpni_net != net)
1284 if (lp->lpni_nid == rtr_nid) /* it's pre-determined router */
1287 if (lpni_best == NULL) {
1288 best_route = last_route = route;
1293 /* no protection on below fields, but it's harmless */
1294 if (last_route->lr_seq - route->lr_seq < 0)
1297 rc = lnet_compare_routes(route, best_route);
1305 /* set sequence number on the best router to the latest sequence + 1
1306 * so we can round-robin all routers, it's race and inaccurate but
1307 * harmless and functional */
1308 if (best_route != NULL)
1309 best_route->lr_seq = last_route->lr_seq + 1;
1314 lnet_select_pathway(lnet_nid_t src_nid, lnet_nid_t dst_nid,
1315 struct lnet_msg *msg, lnet_nid_t rtr_nid)
1317 struct lnet_ni *best_ni;
1318 struct lnet_peer_ni *best_lpni;
1319 struct lnet_peer_ni *best_gw;
1320 struct lnet_peer_ni *lpni;
1321 struct lnet_peer *peer;
1322 struct lnet_peer_net *peer_net;
1323 struct lnet_net *local_net;
1332 int best_lpni_credits;
1334 int shortest_distance;
1337 * get an initial CPT to use for locking. The idea here is not to
1338 * serialize the calls to select_pathway, so that as many
1339 * operations can run concurrently as possible. To do that we use
1340 * the CPT where this call is being executed. Later on when we
1341 * determine the CPT to use in lnet_message_commit, we switch the
1342 * lock and check if there was any configuration change. If none,
1343 * then we proceed, if there is, then we restart the operation.
1345 cpt = lnet_net_lock_current();
1354 seq = lnet_get_dlc_seq_locked();
1356 if (the_lnet.ln_shutdown) {
1357 lnet_net_unlock(cpt);
1361 if (msg->msg_md != NULL)
1362 /* get the cpt of the MD, used during NUMA based selection */
1363 md_cpt = lnet_cpt_of_cookie(msg->msg_md->md_lh.lh_cookie);
1365 md_cpt = CFS_CPT_ANY;
1367 peer = lnet_find_or_create_peer_locked(dst_nid, cpt);
1369 lnet_net_unlock(cpt);
1370 return PTR_ERR(peer);
1373 /* If peer is not healthy then can not send anything to it */
1374 if (!lnet_is_peer_healthy_locked(peer)) {
1375 lnet_net_unlock(cpt);
1376 return -EHOSTUNREACH;
1379 if (!peer->lp_multi_rail && lnet_get_num_peer_nis(peer) > 1) {
1380 CERROR("peer %s is declared to be non MR capable, "
1381 "yet configured with more than one NID\n",
1382 libcfs_nid2str(dst_nid));
1387 * STEP 1: first jab at determining best_ni
1388 * if src_nid is explicitly specified, then best_ni is already
1389 * pre-determiend for us. Otherwise we need to select the best
1390 * one to use later on
1392 if (src_nid != LNET_NID_ANY) {
1393 best_ni = lnet_nid2ni_locked(src_nid, cpt);
1395 lnet_net_unlock(cpt);
1396 LCONSOLE_WARN("Can't send to %s: src %s is not a "
1397 "local nid\n", libcfs_nid2str(dst_nid),
1398 libcfs_nid2str(src_nid));
1403 if (msg->msg_type == LNET_MSG_REPLY ||
1404 msg->msg_type == LNET_MSG_ACK ||
1405 !peer->lp_multi_rail) {
1407 * for replies we want to respond on the same peer_ni we
1408 * received the message on if possible. If not, then pick
1409 * a peer_ni to send to
1411 * if the peer is non-multi-rail then you want to send to
1412 * the dst_nid provided as well.
1414 * It is expected to find the lpni using dst_nid, since we
1415 * created it earlier.
1417 best_lpni = lnet_find_peer_ni_locked(dst_nid);
1419 lnet_peer_ni_decref_locked(best_lpni);
1421 if (best_lpni && !lnet_get_net_locked(LNET_NIDNET(dst_nid))) {
1423 * this lpni is not on a local network so we need
1424 * to route this reply.
1426 best_gw = lnet_find_route_locked(NULL,
1427 best_lpni->lpni_nid,
1431 * RULE: Each node considers only the next-hop
1433 * We're going to route the message, so change the peer to
1436 LASSERT(best_gw->lpni_peer_net);
1437 LASSERT(best_gw->lpni_peer_net->lpn_peer);
1438 peer = best_gw->lpni_peer_net->lpn_peer;
1441 * if the router is not multi-rail then use the best_gw
1442 * found to send the message to
1444 if (!peer->lp_multi_rail)
1445 best_lpni = best_gw;
1453 } else if (!best_lpni) {
1454 lnet_net_unlock(cpt);
1455 CERROR("unable to send msg_type %d to "
1456 "originating %s. Destination NID not in DB\n",
1457 msg->msg_type, libcfs_nid2str(dst_nid));
1463 * if the peer is not MR capable, then we should always send to it
1464 * using the first NI in the NET we determined.
1466 if (!peer->lp_multi_rail) {
1468 lnet_net_unlock(cpt);
1469 CERROR("no route to %s\n",
1470 libcfs_nid2str(dst_nid));
1471 return -EHOSTUNREACH;
1474 /* best ni could be set because src_nid was provided */
1476 best_ni = lnet_net2ni_locked(best_lpni->lpni_net->net_id, cpt);
1478 lnet_net_unlock(cpt);
1479 CERROR("no path to %s from net %s\n",
1480 libcfs_nid2str(best_lpni->lpni_nid),
1481 libcfs_net2str(best_lpni->lpni_net->net_id));
1482 return -EHOSTUNREACH;
1487 if (best_ni == the_lnet.ln_loni) {
1488 /* No send credit hassles with LOLND */
1489 lnet_ni_addref_locked(best_ni, cpt);
1490 msg->msg_hdr.dest_nid = cpu_to_le64(best_ni->ni_nid);
1491 if (!msg->msg_routing)
1492 msg->msg_hdr.src_nid = cpu_to_le64(best_ni->ni_nid);
1493 msg->msg_target.nid = best_ni->ni_nid;
1494 lnet_msg_commit(msg, cpt);
1495 msg->msg_txni = best_ni;
1496 lnet_net_unlock(cpt);
1498 return LNET_CREDIT_OK;
1502 * if we already found a best_ni because src_nid is specified and
1503 * best_lpni because we are replying to a message then just send
1506 if (best_ni && best_lpni)
1510 * If we already found a best_ni because src_nid is specified then
1511 * pick the peer then send the message
1517 * Decide whether we need to route to peer_ni.
1518 * Get the local net that I need to be on to be able to directly
1519 * send to that peer.
1521 * a. Find the peer which the dst_nid belongs to.
1522 * b. Iterate through each of the peer_nets/nis to decide
1523 * the best peer/local_ni pair to use
1525 shortest_distance = INT_MAX;
1526 best_credits = INT_MIN;
1527 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_on_peer_list) {
1528 if (!lnet_is_peer_net_healthy_locked(peer_net))
1531 local_net = lnet_get_net_locked(peer_net->lpn_net_id);
1532 if (!local_net && !routing) {
1533 struct lnet_peer_ni *net_gw;
1535 * go through each peer_ni on that peer_net and
1536 * determine the best possible gw to go through
1538 list_for_each_entry(lpni, &peer_net->lpn_peer_nis,
1539 lpni_on_peer_net_list) {
1540 net_gw = lnet_find_route_locked(NULL,
1545 * if no route is found for that network then
1546 * move onto the next peer_ni in the peer
1554 rc = lnet_compare_peers(net_gw,
1564 local_net = lnet_get_net_locked
1565 (LNET_NIDNET(best_gw->lpni_nid));
1572 /* no routable net found go on to a different net */
1577 * Iterate through the NIs in this local Net and select
1578 * the NI to send from. The selection is determined by
1579 * these 3 criterion in the following priority:
1581 * 2. NI available credits
1585 while ((ni = lnet_get_next_ni_locked(local_net, ni))) {
1589 if (!lnet_is_ni_healthy_locked(ni))
1592 ni_credits = atomic_read(&ni->ni_tx_credits);
1595 * calculate the distance from the CPT on which
1596 * the message memory is allocated to the CPT of
1597 * the NI's physical device
1599 distance = cfs_cpt_distance(lnet_cpt_table(),
1604 * All distances smaller than the NUMA range
1605 * are treated equally.
1607 if (distance < lnet_get_numa_range())
1608 distance = lnet_get_numa_range();
1611 * Select on shorter distance, then available
1612 * credits, then round-robin.
1614 if (distance > shortest_distance) {
1616 } else if (distance < shortest_distance) {
1617 shortest_distance = distance;
1618 } else if (ni_credits < best_credits) {
1620 } else if (ni_credits == best_credits) {
1621 if (best_ni && best_ni->ni_seq <= ni->ni_seq)
1625 best_credits = ni_credits;
1631 * RULE: Each node considers only the next-hop
1633 * We're going to route the message, so change the peer to
1636 LASSERT(best_gw->lpni_peer_net);
1637 LASSERT(best_gw->lpni_peer_net->lpn_peer);
1638 peer = best_gw->lpni_peer_net->lpn_peer;
1642 lnet_net_unlock(cpt);
1643 LCONSOLE_WARN("No local ni found to send from to %s\n",
1644 libcfs_nid2str(dst_nid));
1649 * Now that we selected the NI to use increment its sequence
1650 * number so the Round Robin algorithm will detect that it has
1651 * been used and pick the next NI.
1657 * At this point the best_ni is on a local network on which
1658 * the peer has a peer_ni as well
1660 peer_net = lnet_peer_get_net_locked(peer,
1661 best_ni->ni_net->net_id);
1663 * peer_net is not available or the src_nid is explicitly defined
1664 * and the peer_net for that src_nid is unhealthy. find a route to
1665 * the destination nid.
1668 (src_nid != LNET_NID_ANY &&
1669 !lnet_is_peer_net_healthy_locked(peer_net))) {
1670 best_gw = lnet_find_route_locked(best_ni->ni_net,
1674 * if no route is found for that network then
1675 * move onto the next peer_ni in the peer
1678 lnet_net_unlock(cpt);
1679 LCONSOLE_WARN("No route to peer from %s\n",
1680 libcfs_nid2str(best_ni->ni_nid));
1681 return -EHOSTUNREACH;
1684 CDEBUG(D_NET, "Best route to %s via %s for %s %d\n",
1685 libcfs_nid2str(dst_nid),
1686 libcfs_nid2str(best_gw->lpni_nid),
1687 lnet_msgtyp2str(msg->msg_type), msg->msg_len);
1691 * RULE: Each node considers only the next-hop
1693 * We're going to route the message, so change the peer to
1696 LASSERT(best_gw->lpni_peer_net);
1697 LASSERT(best_gw->lpni_peer_net->lpn_peer);
1698 peer = best_gw->lpni_peer_net->lpn_peer;
1699 } else if (!lnet_is_peer_net_healthy_locked(peer_net)) {
1701 * this peer_net is unhealthy but we still have an opportunity
1702 * to find another peer_net that we can use
1704 __u32 net_id = peer_net->lpn_net_id;
1705 LCONSOLE_WARN("peer net %s unhealthy\n",
1706 libcfs_net2str(net_id));
1711 * Look at the peer NIs for the destination peer that connect
1712 * to the chosen net. If a peer_ni is preferred when using the
1713 * best_ni to communicate, we use that one. If there is no
1714 * preferred peer_ni, or there are multiple preferred peer_ni,
1715 * the available transmit credits are used. If the transmit
1716 * credits are equal, we round-robin over the peer_ni.
1719 best_lpni_credits = INT_MIN;
1722 while ((lpni = lnet_get_next_peer_ni_locked(peer, peer_net, lpni))) {
1724 * if this peer ni is not healthy just skip it, no point in
1725 * examining it further
1727 if (!lnet_is_peer_ni_healthy_locked(lpni))
1729 ni_is_pref = lnet_peer_is_ni_pref_locked(lpni, best_ni);
1731 /* if this is a preferred peer use it */
1732 if (!preferred && ni_is_pref) {
1734 } else if (preferred && !ni_is_pref) {
1736 * this is not the preferred peer so let's ignore
1740 } else if (lpni->lpni_txcredits < best_lpni_credits) {
1742 * We already have a peer that has more credits
1743 * available than this one. No need to consider
1744 * this peer further.
1747 } else if (lpni->lpni_txcredits == best_lpni_credits) {
1749 * The best peer found so far and the current peer
1750 * have the same number of available credits let's
1751 * make sure to select between them using Round
1755 if (best_lpni->lpni_seq <= lpni->lpni_seq)
1761 best_lpni_credits = lpni->lpni_txcredits;
1764 /* if we still can't find a peer ni then we can't reach it */
1766 __u32 net_id = peer_net->lpn_net_id;
1767 lnet_net_unlock(cpt);
1768 LCONSOLE_WARN("no peer_ni found on peer net %s\n",
1769 libcfs_net2str(net_id));
1770 return -EHOSTUNREACH;
1774 routing = routing || routing2;
1777 * Increment sequence number of the peer selected so that we
1778 * pick the next one in Round Robin.
1780 best_lpni->lpni_seq++;
1783 * grab a reference on the peer_ni so it sticks around even if
1784 * we need to drop and relock the lnet_net_lock below.
1786 lnet_peer_ni_addref_locked(best_lpni);
1789 * Use lnet_cpt_of_nid() to determine the CPT used to commit the
1790 * message. This ensures that we get a CPT that is correct for
1791 * the NI when the NI has been restricted to a subset of all CPTs.
1792 * If the selected CPT differs from the one currently locked, we
1793 * must unlock and relock the lnet_net_lock(), and then check whether
1794 * the configuration has changed. We don't have a hold on the best_ni
1795 * yet, and it may have vanished.
1797 cpt2 = lnet_cpt_of_nid_locked(best_lpni->lpni_nid, best_ni);
1799 lnet_net_unlock(cpt);
1802 if (seq != lnet_get_dlc_seq_locked()) {
1803 lnet_peer_ni_decref_locked(best_lpni);
1809 * store the best_lpni in the message right away to avoid having
1810 * to do the same operation under different conditions
1812 msg->msg_txpeer = best_lpni;
1813 msg->msg_txni = best_ni;
1816 * grab a reference for the best_ni since now it's in use in this
1817 * send. the reference will need to be dropped when the message is
1818 * finished in lnet_finalize()
1820 lnet_ni_addref_locked(msg->msg_txni, cpt);
1823 * Always set the target.nid to the best peer picked. Either the
1824 * nid will be one of the preconfigured NIDs, or the same NID as
1825 * what was originally set in the target or it will be the NID of
1826 * a router if this message should be routed
1828 msg->msg_target.nid = msg->msg_txpeer->lpni_nid;
1831 * lnet_msg_commit assigns the correct cpt to the message, which
1832 * is used to decrement the correct refcount on the ni when it's
1833 * time to return the credits
1835 lnet_msg_commit(msg, cpt);
1838 * If we are routing the message then we don't need to overwrite
1839 * the src_nid since it would've been set at the origin. Otherwise
1840 * we are the originator so we need to set it.
1842 if (!msg->msg_routing)
1843 msg->msg_hdr.src_nid = cpu_to_le64(msg->msg_txni->ni_nid);
1846 msg->msg_target_is_router = 1;
1847 msg->msg_target.pid = LNET_PID_LUSTRE;
1849 * since we're routing we want to ensure that the
1850 * msg_hdr.dest_nid is set to the final destination. When
1851 * the router receives this message it knows how to route
1854 msg->msg_hdr.dest_nid = cpu_to_le64(dst_nid);
1857 * if we're not routing set the dest_nid to the best peer
1858 * ni that we picked earlier in the algorithm.
1860 msg->msg_hdr.dest_nid = cpu_to_le64(msg->msg_txpeer->lpni_nid);
1863 rc = lnet_post_send_locked(msg, 0);
1865 lnet_net_unlock(cpt);
1871 lnet_send(lnet_nid_t src_nid, lnet_msg_t *msg, lnet_nid_t rtr_nid)
1873 lnet_nid_t dst_nid = msg->msg_target.nid;
1877 * NB: rtr_nid is set to LNET_NID_ANY for all current use-cases,
1878 * but we might want to use pre-determined router for ACK/REPLY
1881 /* NB: ni != NULL == interface pre-determined (ACK/REPLY) */
1882 LASSERT (msg->msg_txpeer == NULL);
1883 LASSERT (!msg->msg_sending);
1884 LASSERT (!msg->msg_target_is_router);
1885 LASSERT (!msg->msg_receiving);
1887 msg->msg_sending = 1;
1889 LASSERT(!msg->msg_tx_committed);
1891 rc = lnet_select_pathway(src_nid, dst_nid, msg, rtr_nid);
1895 if (rc == LNET_CREDIT_OK)
1896 lnet_ni_send(msg->msg_txni, msg);
1898 /* rc == LNET_CREDIT_OK or LNET_CREDIT_WAIT */
1903 lnet_drop_message(lnet_ni_t *ni, int cpt, void *private, unsigned int nob)
1906 the_lnet.ln_counters[cpt]->drop_count++;
1907 the_lnet.ln_counters[cpt]->drop_length += nob;
1908 lnet_net_unlock(cpt);
1910 lnet_ni_recv(ni, private, NULL, 0, 0, 0, nob);
1914 lnet_recv_put(lnet_ni_t *ni, lnet_msg_t *msg)
1916 lnet_hdr_t *hdr = &msg->msg_hdr;
1918 if (msg->msg_wanted != 0)
1919 lnet_setpayloadbuffer(msg);
1921 lnet_build_msg_event(msg, LNET_EVENT_PUT);
1923 /* Must I ACK? If so I'll grab the ack_wmd out of the header and put
1924 * it back into the ACK during lnet_finalize() */
1925 msg->msg_ack = (!lnet_is_wire_handle_none(&hdr->msg.put.ack_wmd) &&
1926 (msg->msg_md->md_options & LNET_MD_ACK_DISABLE) == 0);
1928 lnet_ni_recv(ni, msg->msg_private, msg, msg->msg_rx_delayed,
1929 msg->msg_offset, msg->msg_wanted, hdr->payload_length);
1933 lnet_parse_put(lnet_ni_t *ni, lnet_msg_t *msg)
1935 lnet_hdr_t *hdr = &msg->msg_hdr;
1936 struct lnet_match_info info;
1940 /* Convert put fields to host byte order */
1941 hdr->msg.put.match_bits = le64_to_cpu(hdr->msg.put.match_bits);
1942 hdr->msg.put.ptl_index = le32_to_cpu(hdr->msg.put.ptl_index);
1943 hdr->msg.put.offset = le32_to_cpu(hdr->msg.put.offset);
1945 /* Primary peer NID. */
1946 info.mi_id.nid = msg->msg_initiator;
1947 info.mi_id.pid = hdr->src_pid;
1948 info.mi_opc = LNET_MD_OP_PUT;
1949 info.mi_portal = hdr->msg.put.ptl_index;
1950 info.mi_rlength = hdr->payload_length;
1951 info.mi_roffset = hdr->msg.put.offset;
1952 info.mi_mbits = hdr->msg.put.match_bits;
1953 info.mi_cpt = lnet_cpt_of_nid(msg->msg_rxpeer->lpni_nid, ni);
1955 msg->msg_rx_ready_delay = ni->ni_net->net_lnd->lnd_eager_recv == NULL;
1956 ready_delay = msg->msg_rx_ready_delay;
1959 rc = lnet_ptl_match_md(&info, msg);
1964 case LNET_MATCHMD_OK:
1965 lnet_recv_put(ni, msg);
1968 case LNET_MATCHMD_NONE:
1970 /* no eager_recv or has already called it, should
1971 * have been attached on delayed list */
1974 rc = lnet_ni_eager_recv(ni, msg);
1981 case LNET_MATCHMD_DROP:
1982 CNETERR("Dropping PUT from %s portal %d match %llu"
1983 " offset %d length %d: %d\n",
1984 libcfs_id2str(info.mi_id), info.mi_portal,
1985 info.mi_mbits, info.mi_roffset, info.mi_rlength, rc);
1987 return -ENOENT; /* -ve: OK but no match */
1992 lnet_parse_get(lnet_ni_t *ni, lnet_msg_t *msg, int rdma_get)
1994 struct lnet_match_info info;
1995 lnet_hdr_t *hdr = &msg->msg_hdr;
1996 lnet_process_id_t source_id;
1997 struct lnet_handle_wire reply_wmd;
2000 /* Convert get fields to host byte order */
2001 hdr->msg.get.match_bits = le64_to_cpu(hdr->msg.get.match_bits);
2002 hdr->msg.get.ptl_index = le32_to_cpu(hdr->msg.get.ptl_index);
2003 hdr->msg.get.sink_length = le32_to_cpu(hdr->msg.get.sink_length);
2004 hdr->msg.get.src_offset = le32_to_cpu(hdr->msg.get.src_offset);
2006 source_id.nid = hdr->src_nid;
2007 source_id.pid = hdr->src_pid;
2008 /* Primary peer NID */
2009 info.mi_id.nid = msg->msg_initiator;
2010 info.mi_id.pid = hdr->src_pid;
2011 info.mi_opc = LNET_MD_OP_GET;
2012 info.mi_portal = hdr->msg.get.ptl_index;
2013 info.mi_rlength = hdr->msg.get.sink_length;
2014 info.mi_roffset = hdr->msg.get.src_offset;
2015 info.mi_mbits = hdr->msg.get.match_bits;
2016 info.mi_cpt = lnet_cpt_of_nid(msg->msg_rxpeer->lpni_nid, ni);
2018 rc = lnet_ptl_match_md(&info, msg);
2019 if (rc == LNET_MATCHMD_DROP) {
2020 CNETERR("Dropping GET from %s portal %d match %llu"
2021 " offset %d length %d\n",
2022 libcfs_id2str(info.mi_id), info.mi_portal,
2023 info.mi_mbits, info.mi_roffset, info.mi_rlength);
2024 return -ENOENT; /* -ve: OK but no match */
2027 LASSERT(rc == LNET_MATCHMD_OK);
2029 lnet_build_msg_event(msg, LNET_EVENT_GET);
2031 reply_wmd = hdr->msg.get.return_wmd;
2033 lnet_prep_send(msg, LNET_MSG_REPLY, source_id,
2034 msg->msg_offset, msg->msg_wanted);
2036 msg->msg_hdr.msg.reply.dst_wmd = reply_wmd;
2039 /* The LND completes the REPLY from her recv procedure */
2040 lnet_ni_recv(ni, msg->msg_private, msg, 0,
2041 msg->msg_offset, msg->msg_len, msg->msg_len);
2045 lnet_ni_recv(ni, msg->msg_private, NULL, 0, 0, 0, 0);
2046 msg->msg_receiving = 0;
2048 rc = lnet_send(ni->ni_nid, msg, LNET_NID_ANY);
2050 /* didn't get as far as lnet_ni_send() */
2051 CERROR("%s: Unable to send REPLY for GET from %s: %d\n",
2052 libcfs_nid2str(ni->ni_nid),
2053 libcfs_id2str(info.mi_id), rc);
2055 lnet_finalize(ni, msg, rc);
2062 lnet_parse_reply(lnet_ni_t *ni, lnet_msg_t *msg)
2064 void *private = msg->msg_private;
2065 lnet_hdr_t *hdr = &msg->msg_hdr;
2066 lnet_process_id_t src = {0};
2072 cpt = lnet_cpt_of_cookie(hdr->msg.reply.dst_wmd.wh_object_cookie);
2075 src.nid = hdr->src_nid;
2076 src.pid = hdr->src_pid;
2078 /* NB handles only looked up by creator (no flips) */
2079 md = lnet_wire_handle2md(&hdr->msg.reply.dst_wmd);
2080 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
2081 CNETERR("%s: Dropping REPLY from %s for %s "
2083 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
2084 (md == NULL) ? "invalid" : "inactive",
2085 hdr->msg.reply.dst_wmd.wh_interface_cookie,
2086 hdr->msg.reply.dst_wmd.wh_object_cookie);
2087 if (md != NULL && md->md_me != NULL)
2088 CERROR("REPLY MD also attached to portal %d\n",
2089 md->md_me->me_portal);
2091 lnet_res_unlock(cpt);
2092 return -ENOENT; /* -ve: OK but no match */
2095 LASSERT(md->md_offset == 0);
2097 rlength = hdr->payload_length;
2098 mlength = MIN(rlength, (int)md->md_length);
2100 if (mlength < rlength &&
2101 (md->md_options & LNET_MD_TRUNCATE) == 0) {
2102 CNETERR("%s: Dropping REPLY from %s length %d "
2103 "for MD %#llx would overflow (%d)\n",
2104 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
2105 rlength, hdr->msg.reply.dst_wmd.wh_object_cookie,
2107 lnet_res_unlock(cpt);
2108 return -ENOENT; /* -ve: OK but no match */
2111 CDEBUG(D_NET, "%s: Reply from %s of length %d/%d into md %#llx\n",
2112 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
2113 mlength, rlength, hdr->msg.reply.dst_wmd.wh_object_cookie);
2115 lnet_msg_attach_md(msg, md, 0, mlength);
2118 lnet_setpayloadbuffer(msg);
2120 lnet_res_unlock(cpt);
2122 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
2124 lnet_ni_recv(ni, private, msg, 0, 0, mlength, rlength);
2129 lnet_parse_ack(lnet_ni_t *ni, lnet_msg_t *msg)
2131 lnet_hdr_t *hdr = &msg->msg_hdr;
2132 lnet_process_id_t src = {0};
2136 src.nid = hdr->src_nid;
2137 src.pid = hdr->src_pid;
2139 /* Convert ack fields to host byte order */
2140 hdr->msg.ack.match_bits = le64_to_cpu(hdr->msg.ack.match_bits);
2141 hdr->msg.ack.mlength = le32_to_cpu(hdr->msg.ack.mlength);
2143 cpt = lnet_cpt_of_cookie(hdr->msg.ack.dst_wmd.wh_object_cookie);
2146 /* NB handles only looked up by creator (no flips) */
2147 md = lnet_wire_handle2md(&hdr->msg.ack.dst_wmd);
2148 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
2149 /* Don't moan; this is expected */
2151 "%s: Dropping ACK from %s to %s MD %#llx.%#llx\n",
2152 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
2153 (md == NULL) ? "invalid" : "inactive",
2154 hdr->msg.ack.dst_wmd.wh_interface_cookie,
2155 hdr->msg.ack.dst_wmd.wh_object_cookie);
2156 if (md != NULL && md->md_me != NULL)
2157 CERROR("Source MD also attached to portal %d\n",
2158 md->md_me->me_portal);
2160 lnet_res_unlock(cpt);
2161 return -ENOENT; /* -ve! */
2164 CDEBUG(D_NET, "%s: ACK from %s into md %#llx\n",
2165 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
2166 hdr->msg.ack.dst_wmd.wh_object_cookie);
2168 lnet_msg_attach_md(msg, md, 0, 0);
2170 lnet_res_unlock(cpt);
2172 lnet_build_msg_event(msg, LNET_EVENT_ACK);
2174 lnet_ni_recv(ni, msg->msg_private, msg, 0, 0, 0, msg->msg_len);
2179 * \retval LNET_CREDIT_OK If \a msg is forwarded
2180 * \retval LNET_CREDIT_WAIT If \a msg is blocked because w/o buffer
2181 * \retval -ve error code
2184 lnet_parse_forward_locked(lnet_ni_t *ni, lnet_msg_t *msg)
2188 if (!the_lnet.ln_routing)
2191 if (msg->msg_rxpeer->lpni_rtrcredits <= 0 ||
2192 lnet_msg2bufpool(msg)->rbp_credits <= 0) {
2193 if (ni->ni_net->net_lnd->lnd_eager_recv == NULL) {
2194 msg->msg_rx_ready_delay = 1;
2196 lnet_net_unlock(msg->msg_rx_cpt);
2197 rc = lnet_ni_eager_recv(ni, msg);
2198 lnet_net_lock(msg->msg_rx_cpt);
2203 rc = lnet_post_routed_recv_locked(msg, 0);
2208 lnet_parse_local(lnet_ni_t *ni, lnet_msg_t *msg)
2212 switch (msg->msg_type) {
2214 rc = lnet_parse_ack(ni, msg);
2217 rc = lnet_parse_put(ni, msg);
2220 rc = lnet_parse_get(ni, msg, msg->msg_rdma_get);
2222 case LNET_MSG_REPLY:
2223 rc = lnet_parse_reply(ni, msg);
2225 default: /* prevent an unused label if !kernel */
2230 LASSERT(rc == 0 || rc == -ENOENT);
2235 lnet_msgtyp2str (int type)
2244 case LNET_MSG_REPLY:
2246 case LNET_MSG_HELLO:
2249 return ("<UNKNOWN>");
2254 lnet_print_hdr(lnet_hdr_t * hdr)
2256 lnet_process_id_t src = {0};
2257 lnet_process_id_t dst = {0};
2258 char *type_str = lnet_msgtyp2str(hdr->type);
2260 src.nid = hdr->src_nid;
2261 src.pid = hdr->src_pid;
2263 dst.nid = hdr->dest_nid;
2264 dst.pid = hdr->dest_pid;
2266 CWARN("P3 Header at %p of type %s\n", hdr, type_str);
2267 CWARN(" From %s\n", libcfs_id2str(src));
2268 CWARN(" To %s\n", libcfs_id2str(dst));
2270 switch (hdr->type) {
2275 CWARN(" Ptl index %d, ack md %#llx.%#llx, "
2276 "match bits %llu\n",
2277 hdr->msg.put.ptl_index,
2278 hdr->msg.put.ack_wmd.wh_interface_cookie,
2279 hdr->msg.put.ack_wmd.wh_object_cookie,
2280 hdr->msg.put.match_bits);
2281 CWARN(" Length %d, offset %d, hdr data %#llx\n",
2282 hdr->payload_length, hdr->msg.put.offset,
2283 hdr->msg.put.hdr_data);
2287 CWARN(" Ptl index %d, return md %#llx.%#llx, "
2288 "match bits %llu\n", hdr->msg.get.ptl_index,
2289 hdr->msg.get.return_wmd.wh_interface_cookie,
2290 hdr->msg.get.return_wmd.wh_object_cookie,
2291 hdr->msg.get.match_bits);
2292 CWARN(" Length %d, src offset %d\n",
2293 hdr->msg.get.sink_length,
2294 hdr->msg.get.src_offset);
2298 CWARN(" dst md %#llx.%#llx, "
2299 "manipulated length %d\n",
2300 hdr->msg.ack.dst_wmd.wh_interface_cookie,
2301 hdr->msg.ack.dst_wmd.wh_object_cookie,
2302 hdr->msg.ack.mlength);
2305 case LNET_MSG_REPLY:
2306 CWARN(" dst md %#llx.%#llx, "
2308 hdr->msg.reply.dst_wmd.wh_interface_cookie,
2309 hdr->msg.reply.dst_wmd.wh_object_cookie,
2310 hdr->payload_length);
2316 lnet_parse(lnet_ni_t *ni, lnet_hdr_t *hdr, lnet_nid_t from_nid,
2317 void *private, int rdma_req)
2322 struct lnet_msg *msg;
2323 lnet_pid_t dest_pid;
2324 lnet_nid_t dest_nid;
2326 struct lnet_peer_ni *lpni;
2327 __u32 payload_length;
2330 LASSERT (!in_interrupt ());
2332 type = le32_to_cpu(hdr->type);
2333 src_nid = le64_to_cpu(hdr->src_nid);
2334 dest_nid = le64_to_cpu(hdr->dest_nid);
2335 dest_pid = le32_to_cpu(hdr->dest_pid);
2336 payload_length = le32_to_cpu(hdr->payload_length);
2338 for_me = (ni->ni_nid == dest_nid);
2339 cpt = lnet_cpt_of_nid(from_nid, ni);
2344 if (payload_length > 0) {
2345 CERROR("%s, src %s: bad %s payload %d (0 expected)\n",
2346 libcfs_nid2str(from_nid),
2347 libcfs_nid2str(src_nid),
2348 lnet_msgtyp2str(type), payload_length);
2354 case LNET_MSG_REPLY:
2355 if (payload_length >
2356 (__u32)(for_me ? LNET_MAX_PAYLOAD : LNET_MTU)) {
2357 CERROR("%s, src %s: bad %s payload %d "
2358 "(%d max expected)\n",
2359 libcfs_nid2str(from_nid),
2360 libcfs_nid2str(src_nid),
2361 lnet_msgtyp2str(type),
2363 for_me ? LNET_MAX_PAYLOAD : LNET_MTU);
2369 CERROR("%s, src %s: Bad message type 0x%x\n",
2370 libcfs_nid2str(from_nid),
2371 libcfs_nid2str(src_nid), type);
2375 if (the_lnet.ln_routing &&
2376 ni->ni_last_alive != ktime_get_real_seconds()) {
2377 /* NB: so far here is the only place to set NI status to "up */
2379 ni->ni_last_alive = ktime_get_real_seconds();
2380 if (ni->ni_status != NULL &&
2381 ni->ni_status->ns_status == LNET_NI_STATUS_DOWN)
2382 ni->ni_status->ns_status = LNET_NI_STATUS_UP;
2386 /* Regard a bad destination NID as a protocol error. Senders should
2387 * know what they're doing; if they don't they're misconfigured, buggy
2388 * or malicious so we chop them off at the knees :) */
2391 if (LNET_NIDNET(dest_nid) == LNET_NIDNET(ni->ni_nid)) {
2392 /* should have gone direct */
2393 CERROR("%s, src %s: Bad dest nid %s "
2394 "(should have been sent direct)\n",
2395 libcfs_nid2str(from_nid),
2396 libcfs_nid2str(src_nid),
2397 libcfs_nid2str(dest_nid));
2401 if (lnet_islocalnid(dest_nid)) {
2402 /* dest is another local NI; sender should have used
2403 * this node's NID on its own network */
2404 CERROR("%s, src %s: Bad dest nid %s "
2405 "(it's my nid but on a different network)\n",
2406 libcfs_nid2str(from_nid),
2407 libcfs_nid2str(src_nid),
2408 libcfs_nid2str(dest_nid));
2412 if (rdma_req && type == LNET_MSG_GET) {
2413 CERROR("%s, src %s: Bad optimized GET for %s "
2414 "(final destination must be me)\n",
2415 libcfs_nid2str(from_nid),
2416 libcfs_nid2str(src_nid),
2417 libcfs_nid2str(dest_nid));
2421 if (!the_lnet.ln_routing) {
2422 CERROR("%s, src %s: Dropping message for %s "
2423 "(routing not enabled)\n",
2424 libcfs_nid2str(from_nid),
2425 libcfs_nid2str(src_nid),
2426 libcfs_nid2str(dest_nid));
2431 /* Message looks OK; we're not going to return an error, so we MUST
2432 * call back lnd_recv() come what may... */
2434 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
2435 fail_peer(src_nid, 0)) { /* shall we now? */
2436 CERROR("%s, src %s: Dropping %s to simulate failure\n",
2437 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
2438 lnet_msgtyp2str(type));
2442 if (!list_empty(&the_lnet.ln_drop_rules) &&
2443 lnet_drop_rule_match(hdr)) {
2444 CDEBUG(D_NET, "%s, src %s, dst %s: Dropping %s to simulate"
2445 "silent message loss\n",
2446 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
2447 libcfs_nid2str(dest_nid), lnet_msgtyp2str(type));
2452 msg = lnet_msg_alloc();
2454 CERROR("%s, src %s: Dropping %s (out of memory)\n",
2455 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
2456 lnet_msgtyp2str(type));
2460 /* msg zeroed in lnet_msg_alloc; i.e. flags all clear,
2461 * pointers NULL etc */
2463 msg->msg_type = type;
2464 msg->msg_private = private;
2465 msg->msg_receiving = 1;
2466 msg->msg_rdma_get = rdma_req;
2467 msg->msg_len = msg->msg_wanted = payload_length;
2468 msg->msg_offset = 0;
2469 msg->msg_hdr = *hdr;
2470 /* for building message event */
2471 msg->msg_from = from_nid;
2473 msg->msg_target.pid = dest_pid;
2474 msg->msg_target.nid = dest_nid;
2475 msg->msg_routing = 1;
2478 /* convert common msg->hdr fields to host byteorder */
2479 msg->msg_hdr.type = type;
2480 msg->msg_hdr.src_nid = src_nid;
2481 msg->msg_hdr.src_pid = le32_to_cpu(msg->msg_hdr.src_pid);
2482 msg->msg_hdr.dest_nid = dest_nid;
2483 msg->msg_hdr.dest_pid = dest_pid;
2484 msg->msg_hdr.payload_length = payload_length;
2486 /* Multi-Rail: Primary NID of source. */
2487 msg->msg_initiator = lnet_peer_primary_nid(src_nid);
2490 lpni = lnet_nid2peerni_locked(from_nid, cpt);
2492 lnet_net_unlock(cpt);
2493 CERROR("%s, src %s: Dropping %s "
2494 "(error %ld looking up sender)\n",
2495 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
2496 lnet_msgtyp2str(type), PTR_ERR(lpni));
2498 if (rc == -ESHUTDOWN)
2499 /* We are shutting down. Don't do anything more */
2503 msg->msg_rxpeer = lpni;
2505 lnet_ni_addref_locked(ni, cpt);
2507 if (lnet_isrouter(msg->msg_rxpeer)) {
2508 lnet_peer_set_alive(msg->msg_rxpeer);
2509 if (avoid_asym_router_failure &&
2510 LNET_NIDNET(src_nid) != LNET_NIDNET(from_nid)) {
2511 /* received a remote message from router, update
2512 * remote NI status on this router.
2513 * NB: multi-hop routed message will be ignored.
2515 lnet_router_ni_update_locked(msg->msg_rxpeer,
2516 LNET_NIDNET(src_nid));
2520 lnet_msg_commit(msg, cpt);
2522 /* message delay simulation */
2523 if (unlikely(!list_empty(&the_lnet.ln_delay_rules) &&
2524 lnet_delay_rule_match_locked(hdr, msg))) {
2525 lnet_net_unlock(cpt);
2530 rc = lnet_parse_forward_locked(ni, msg);
2531 lnet_net_unlock(cpt);
2536 if (rc == LNET_CREDIT_OK) {
2537 lnet_ni_recv(ni, msg->msg_private, msg, 0,
2538 0, payload_length, payload_length);
2543 lnet_net_unlock(cpt);
2545 rc = lnet_parse_local(ni, msg);
2551 LASSERT(msg->msg_md == NULL);
2552 lnet_finalize(ni, msg, rc);
2555 lnet_drop_message(ni, cpt, private, payload_length);
2558 EXPORT_SYMBOL(lnet_parse);
2561 lnet_drop_delayed_msg_list(struct list_head *head, char *reason)
2563 while (!list_empty(head)) {
2564 lnet_process_id_t id = {0};
2567 msg = list_entry(head->next, lnet_msg_t, msg_list);
2568 list_del(&msg->msg_list);
2570 id.nid = msg->msg_hdr.src_nid;
2571 id.pid = msg->msg_hdr.src_pid;
2573 LASSERT(msg->msg_md == NULL);
2574 LASSERT(msg->msg_rx_delayed);
2575 LASSERT(msg->msg_rxpeer != NULL);
2576 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
2578 CWARN("Dropping delayed PUT from %s portal %d match %llu"
2579 " offset %d length %d: %s\n",
2581 msg->msg_hdr.msg.put.ptl_index,
2582 msg->msg_hdr.msg.put.match_bits,
2583 msg->msg_hdr.msg.put.offset,
2584 msg->msg_hdr.payload_length, reason);
2586 /* NB I can't drop msg's ref on msg_rxpeer until after I've
2587 * called lnet_drop_message(), so I just hang onto msg as well
2588 * until that's done */
2590 lnet_drop_message(msg->msg_rxni, msg->msg_rx_cpt,
2591 msg->msg_private, msg->msg_len);
2593 * NB: message will not generate event because w/o attached MD,
2594 * but we still should give error code so lnet_msg_decommit()
2595 * can skip counters operations and other checks.
2597 lnet_finalize(msg->msg_rxni, msg, -ENOENT);
2602 lnet_recv_delayed_msg_list(struct list_head *head)
2604 while (!list_empty(head)) {
2606 lnet_process_id_t id;
2608 msg = list_entry(head->next, lnet_msg_t, msg_list);
2609 list_del(&msg->msg_list);
2611 /* md won't disappear under me, since each msg
2612 * holds a ref on it */
2614 id.nid = msg->msg_hdr.src_nid;
2615 id.pid = msg->msg_hdr.src_pid;
2617 LASSERT(msg->msg_rx_delayed);
2618 LASSERT(msg->msg_md != NULL);
2619 LASSERT(msg->msg_rxpeer != NULL);
2620 LASSERT(msg->msg_rxni != NULL);
2621 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
2623 CDEBUG(D_NET, "Resuming delayed PUT from %s portal %d "
2624 "match %llu offset %d length %d.\n",
2625 libcfs_id2str(id), msg->msg_hdr.msg.put.ptl_index,
2626 msg->msg_hdr.msg.put.match_bits,
2627 msg->msg_hdr.msg.put.offset,
2628 msg->msg_hdr.payload_length);
2630 lnet_recv_put(msg->msg_rxni, msg);
2635 * Initiate an asynchronous PUT operation.
2637 * There are several events associated with a PUT: completion of the send on
2638 * the initiator node (LNET_EVENT_SEND), and when the send completes
2639 * successfully, the receipt of an acknowledgment (LNET_EVENT_ACK) indicating
2640 * that the operation was accepted by the target. The event LNET_EVENT_PUT is
2641 * used at the target node to indicate the completion of incoming data
2644 * The local events will be logged in the EQ associated with the MD pointed to
2645 * by \a mdh handle. Using a MD without an associated EQ results in these
2646 * events being discarded. In this case, the caller must have another
2647 * mechanism (e.g., a higher level protocol) for determining when it is safe
2648 * to modify the memory region associated with the MD.
2650 * Note that LNet does not guarantee the order of LNET_EVENT_SEND and
2651 * LNET_EVENT_ACK, though intuitively ACK should happen after SEND.
2653 * \param self Indicates the NID of a local interface through which to send
2654 * the PUT request. Use LNET_NID_ANY to let LNet choose one by itself.
2655 * \param mdh A handle for the MD that describes the memory to be sent. The MD
2656 * must be "free floating" (See LNetMDBind()).
2657 * \param ack Controls whether an acknowledgment is requested.
2658 * Acknowledgments are only sent when they are requested by the initiating
2659 * process and the target MD enables them.
2660 * \param target A process identifier for the target process.
2661 * \param portal The index in the \a target's portal table.
2662 * \param match_bits The match bits to use for MD selection at the target
2664 * \param offset The offset into the target MD (only used when the target
2665 * MD has the LNET_MD_MANAGE_REMOTE option set).
2666 * \param hdr_data 64 bits of user data that can be included in the message
2667 * header. This data is written to an event queue entry at the target if an
2668 * EQ is present on the matching MD.
2670 * \retval 0 Success, and only in this case events will be generated
2671 * and logged to EQ (if it exists).
2672 * \retval -EIO Simulated failure.
2673 * \retval -ENOMEM Memory allocation failure.
2674 * \retval -ENOENT Invalid MD object.
2676 * \see lnet_event_t::hdr_data and lnet_event_kind_t.
2679 LNetPut(lnet_nid_t self, lnet_handle_md_t mdh, lnet_ack_req_t ack,
2680 lnet_process_id_t target, unsigned int portal,
2681 __u64 match_bits, unsigned int offset,
2684 struct lnet_msg *msg;
2685 struct lnet_libmd *md;
2689 LASSERT(the_lnet.ln_refcount > 0);
2691 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
2692 fail_peer(target.nid, 1)) { /* shall we now? */
2693 CERROR("Dropping PUT to %s: simulated failure\n",
2694 libcfs_id2str(target));
2698 msg = lnet_msg_alloc();
2700 CERROR("Dropping PUT to %s: ENOMEM on lnet_msg_t\n",
2701 libcfs_id2str(target));
2704 msg->msg_vmflush = !!memory_pressure_get();
2706 cpt = lnet_cpt_of_cookie(mdh.cookie);
2709 md = lnet_handle2md(&mdh);
2710 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
2711 CERROR("Dropping PUT (%llu:%d:%s): MD (%d) invalid\n",
2712 match_bits, portal, libcfs_id2str(target),
2713 md == NULL ? -1 : md->md_threshold);
2714 if (md != NULL && md->md_me != NULL)
2715 CERROR("Source MD also attached to portal %d\n",
2716 md->md_me->me_portal);
2717 lnet_res_unlock(cpt);
2723 CDEBUG(D_NET, "LNetPut -> %s\n", libcfs_id2str(target));
2725 lnet_msg_attach_md(msg, md, 0, 0);
2727 lnet_prep_send(msg, LNET_MSG_PUT, target, 0, md->md_length);
2729 msg->msg_hdr.msg.put.match_bits = cpu_to_le64(match_bits);
2730 msg->msg_hdr.msg.put.ptl_index = cpu_to_le32(portal);
2731 msg->msg_hdr.msg.put.offset = cpu_to_le32(offset);
2732 msg->msg_hdr.msg.put.hdr_data = hdr_data;
2734 /* NB handles only looked up by creator (no flips) */
2735 if (ack == LNET_ACK_REQ) {
2736 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
2737 the_lnet.ln_interface_cookie;
2738 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
2739 md->md_lh.lh_cookie;
2741 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
2742 LNET_WIRE_HANDLE_COOKIE_NONE;
2743 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
2744 LNET_WIRE_HANDLE_COOKIE_NONE;
2747 lnet_res_unlock(cpt);
2749 lnet_build_msg_event(msg, LNET_EVENT_SEND);
2751 rc = lnet_send(self, msg, LNET_NID_ANY);
2753 CNETERR("Error sending PUT to %s: %d\n",
2754 libcfs_id2str(target), rc);
2755 lnet_finalize(NULL, msg, rc);
2758 /* completion will be signalled by an event */
2761 EXPORT_SYMBOL(LNetPut);
2764 lnet_create_reply_msg (lnet_ni_t *ni, lnet_msg_t *getmsg)
2766 /* The LND can DMA direct to the GET md (i.e. no REPLY msg). This
2767 * returns a msg for the LND to pass to lnet_finalize() when the sink
2768 * data has been received.
2770 * CAVEAT EMPTOR: 'getmsg' is the original GET, which is freed when
2771 * lnet_finalize() is called on it, so the LND must call this first */
2773 struct lnet_msg *msg = lnet_msg_alloc();
2774 struct lnet_libmd *getmd = getmsg->msg_md;
2775 lnet_process_id_t peer_id = getmsg->msg_target;
2778 LASSERT(!getmsg->msg_target_is_router);
2779 LASSERT(!getmsg->msg_routing);
2782 CERROR("%s: Dropping REPLY from %s: can't allocate msg\n",
2783 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id));
2787 cpt = lnet_cpt_of_cookie(getmd->md_lh.lh_cookie);
2790 LASSERT(getmd->md_refcount > 0);
2792 if (getmd->md_threshold == 0) {
2793 CERROR("%s: Dropping REPLY from %s for inactive MD %p\n",
2794 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id),
2796 lnet_res_unlock(cpt);
2800 LASSERT(getmd->md_offset == 0);
2802 CDEBUG(D_NET, "%s: Reply from %s md %p\n",
2803 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id), getmd);
2805 /* setup information for lnet_build_msg_event */
2806 msg->msg_initiator = lnet_peer_primary_nid(peer_id.nid);
2807 /* Cheaper: msg->msg_initiator = getmsg->msg_txpeer->lp_nid; */
2808 msg->msg_from = peer_id.nid;
2809 msg->msg_type = LNET_MSG_GET; /* flag this msg as an "optimized" GET */
2810 msg->msg_hdr.src_nid = peer_id.nid;
2811 msg->msg_hdr.payload_length = getmd->md_length;
2812 msg->msg_receiving = 1; /* required by lnet_msg_attach_md */
2814 lnet_msg_attach_md(msg, getmd, getmd->md_offset, getmd->md_length);
2815 lnet_res_unlock(cpt);
2817 cpt = lnet_cpt_of_nid(peer_id.nid, ni);
2820 lnet_msg_commit(msg, cpt);
2821 lnet_net_unlock(cpt);
2823 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
2828 cpt = lnet_cpt_of_nid(peer_id.nid, ni);
2831 the_lnet.ln_counters[cpt]->drop_count++;
2832 the_lnet.ln_counters[cpt]->drop_length += getmd->md_length;
2833 lnet_net_unlock(cpt);
2840 EXPORT_SYMBOL(lnet_create_reply_msg);
2843 lnet_set_reply_msg_len(lnet_ni_t *ni, lnet_msg_t *reply, unsigned int len)
2845 /* Set the REPLY length, now the RDMA that elides the REPLY message has
2846 * completed and I know it. */
2847 LASSERT(reply != NULL);
2848 LASSERT(reply->msg_type == LNET_MSG_GET);
2849 LASSERT(reply->msg_ev.type == LNET_EVENT_REPLY);
2851 /* NB I trusted my peer to RDMA. If she tells me she's written beyond
2852 * the end of my buffer, I might as well be dead. */
2853 LASSERT(len <= reply->msg_ev.mlength);
2855 reply->msg_ev.mlength = len;
2857 EXPORT_SYMBOL(lnet_set_reply_msg_len);
2860 * Initiate an asynchronous GET operation.
2862 * On the initiator node, an LNET_EVENT_SEND is logged when the GET request
2863 * is sent, and an LNET_EVENT_REPLY is logged when the data returned from
2864 * the target node in the REPLY has been written to local MD.
2866 * On the target node, an LNET_EVENT_GET is logged when the GET request
2867 * arrives and is accepted into a MD.
2869 * \param self,target,portal,match_bits,offset See the discussion in LNetPut().
2870 * \param mdh A handle for the MD that describes the memory into which the
2871 * requested data will be received. The MD must be "free floating" (See LNetMDBind()).
2873 * \retval 0 Success, and only in this case events will be generated
2874 * and logged to EQ (if it exists) of the MD.
2875 * \retval -EIO Simulated failure.
2876 * \retval -ENOMEM Memory allocation failure.
2877 * \retval -ENOENT Invalid MD object.
2880 LNetGet(lnet_nid_t self, lnet_handle_md_t mdh,
2881 lnet_process_id_t target, unsigned int portal,
2882 __u64 match_bits, unsigned int offset)
2884 struct lnet_msg *msg;
2885 struct lnet_libmd *md;
2889 LASSERT(the_lnet.ln_refcount > 0);
2891 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
2892 fail_peer(target.nid, 1)) /* shall we now? */
2894 CERROR("Dropping GET to %s: simulated failure\n",
2895 libcfs_id2str(target));
2899 msg = lnet_msg_alloc();
2901 CERROR("Dropping GET to %s: ENOMEM on lnet_msg_t\n",
2902 libcfs_id2str(target));
2906 cpt = lnet_cpt_of_cookie(mdh.cookie);
2909 md = lnet_handle2md(&mdh);
2910 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
2911 CERROR("Dropping GET (%llu:%d:%s): MD (%d) invalid\n",
2912 match_bits, portal, libcfs_id2str(target),
2913 md == NULL ? -1 : md->md_threshold);
2914 if (md != NULL && md->md_me != NULL)
2915 CERROR("REPLY MD also attached to portal %d\n",
2916 md->md_me->me_portal);
2918 lnet_res_unlock(cpt);
2924 CDEBUG(D_NET, "LNetGet -> %s\n", libcfs_id2str(target));
2926 lnet_msg_attach_md(msg, md, 0, 0);
2928 lnet_prep_send(msg, LNET_MSG_GET, target, 0, 0);
2930 msg->msg_hdr.msg.get.match_bits = cpu_to_le64(match_bits);
2931 msg->msg_hdr.msg.get.ptl_index = cpu_to_le32(portal);
2932 msg->msg_hdr.msg.get.src_offset = cpu_to_le32(offset);
2933 msg->msg_hdr.msg.get.sink_length = cpu_to_le32(md->md_length);
2935 /* NB handles only looked up by creator (no flips) */
2936 msg->msg_hdr.msg.get.return_wmd.wh_interface_cookie =
2937 the_lnet.ln_interface_cookie;
2938 msg->msg_hdr.msg.get.return_wmd.wh_object_cookie =
2939 md->md_lh.lh_cookie;
2941 lnet_res_unlock(cpt);
2943 lnet_build_msg_event(msg, LNET_EVENT_SEND);
2945 rc = lnet_send(self, msg, LNET_NID_ANY);
2947 CNETERR("Error sending GET to %s: %d\n",
2948 libcfs_id2str(target), rc);
2949 lnet_finalize(NULL, msg, rc);
2952 /* completion will be signalled by an event */
2955 EXPORT_SYMBOL(LNetGet);
2958 * Calculate distance to node at \a dstnid.
2960 * \param dstnid Target NID.
2961 * \param srcnidp If not NULL, NID of the local interface to reach \a dstnid
2963 * \param orderp If not NULL, order of the route to reach \a dstnid is saved
2966 * \retval 0 If \a dstnid belongs to a local interface, and reserved option
2967 * local_nid_dist_zero is set, which is the default.
2968 * \retval positives Distance to target NID, i.e. number of hops plus one.
2969 * \retval -EHOSTUNREACH If \a dstnid is not reachable.
2972 LNetDist(lnet_nid_t dstnid, lnet_nid_t *srcnidp, __u32 *orderp)
2974 struct list_head *e;
2975 struct lnet_ni *ni = NULL;
2976 lnet_remotenet_t *rnet;
2977 __u32 dstnet = LNET_NIDNET(dstnid);
2981 struct list_head *rn_list;
2983 /* if !local_nid_dist_zero, I don't return a distance of 0 ever
2984 * (when lustre sees a distance of 0, it substitutes 0@lo), so I
2985 * keep order 0 free for 0@lo and order 1 free for a local NID
2988 LASSERT(the_lnet.ln_refcount > 0);
2990 cpt = lnet_net_lock_current();
2992 while ((ni = lnet_get_next_ni_locked(NULL, ni))) {
2993 if (ni->ni_nid == dstnid) {
2994 if (srcnidp != NULL)
2996 if (orderp != NULL) {
2997 if (LNET_NETTYP(LNET_NIDNET(dstnid)) == LOLND)
3002 lnet_net_unlock(cpt);
3004 return local_nid_dist_zero ? 0 : 1;
3007 if (LNET_NIDNET(ni->ni_nid) == dstnet) {
3008 /* Check if ni was originally created in
3009 * current net namespace.
3010 * If not, assign order above 0xffff0000,
3011 * to make this ni not a priority. */
3012 if (!net_eq(ni->ni_net_ns, current->nsproxy->net_ns))
3013 order += 0xffff0000;
3015 if (srcnidp != NULL)
3016 *srcnidp = ni->ni_nid;
3019 lnet_net_unlock(cpt);
3026 rn_list = lnet_net2rnethash(dstnet);
3027 list_for_each(e, rn_list) {
3028 rnet = list_entry(e, lnet_remotenet_t, lrn_list);
3030 if (rnet->lrn_net == dstnet) {
3031 lnet_route_t *route;
3032 lnet_route_t *shortest = NULL;
3033 __u32 shortest_hops = LNET_UNDEFINED_HOPS;
3036 LASSERT(!list_empty(&rnet->lrn_routes));
3038 list_for_each_entry(route, &rnet->lrn_routes,
3040 route_hops = route->lr_hops;
3041 if (route_hops == LNET_UNDEFINED_HOPS)
3043 if (shortest == NULL ||
3044 route_hops < shortest_hops) {
3046 shortest_hops = route_hops;
3050 LASSERT(shortest != NULL);
3051 hops = shortest_hops;
3052 if (srcnidp != NULL) {
3053 ni = lnet_get_next_ni_locked(
3054 shortest->lr_gateway->lpni_net,
3056 *srcnidp = ni->ni_nid;
3060 lnet_net_unlock(cpt);
3066 lnet_net_unlock(cpt);
3067 return -EHOSTUNREACH;
3069 EXPORT_SYMBOL(LNetDist);