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_lnd->lnd_recv)(ni, private, msg, delayed,
593 niov, iov, kiov, offset, mlen, rlen);
595 lnet_finalize(ni, msg, rc);
599 lnet_setpayloadbuffer(lnet_msg_t *msg)
601 lnet_libmd_t *md = msg->msg_md;
603 LASSERT(msg->msg_len > 0);
604 LASSERT(!msg->msg_routing);
606 LASSERT(msg->msg_niov == 0);
607 LASSERT(msg->msg_iov == NULL);
608 LASSERT(msg->msg_kiov == NULL);
610 msg->msg_niov = md->md_niov;
611 if ((md->md_options & LNET_MD_KIOV) != 0)
612 msg->msg_kiov = md->md_iov.kiov;
614 msg->msg_iov = md->md_iov.iov;
618 lnet_prep_send(lnet_msg_t *msg, int type, lnet_process_id_t target,
619 unsigned int offset, unsigned int len)
621 msg->msg_type = type;
622 msg->msg_target = target;
624 msg->msg_offset = offset;
627 lnet_setpayloadbuffer(msg);
629 memset(&msg->msg_hdr, 0, sizeof(msg->msg_hdr));
630 msg->msg_hdr.type = cpu_to_le32(type);
631 msg->msg_hdr.dest_nid = cpu_to_le64(target.nid);
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_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_lnd->lnd_eager_recv != NULL);
663 msg->msg_rx_ready_delay = 1;
664 rc = (ni->ni_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->lp_nid),
670 libcfs_id2str(msg->msg_target), rc);
671 LASSERT(rc < 0); /* required by my callers */
677 /* NB: caller shall hold a ref on 'lp' as I'd drop lnet_net_lock */
679 lnet_ni_query_locked(lnet_ni_t *ni, lnet_peer_t *lp)
681 cfs_time_t last_alive = 0;
683 LASSERT(lnet_peer_aliveness_enabled(lp));
684 LASSERT(ni->ni_lnd->lnd_query != NULL);
686 lnet_net_unlock(lp->lp_cpt);
687 (ni->ni_lnd->lnd_query)(ni, lp->lp_nid, &last_alive);
688 lnet_net_lock(lp->lp_cpt);
690 lp->lp_last_query = cfs_time_current();
692 if (last_alive != 0) /* NI has updated timestamp */
693 lp->lp_last_alive = last_alive;
696 /* NB: always called with lnet_net_lock held */
698 lnet_peer_is_alive (lnet_peer_t *lp, cfs_time_t now)
703 LASSERT(lnet_peer_aliveness_enabled(lp));
705 /* Trust lnet_notify() if it has more recent aliveness news, but
706 * ignore the initial assumed death (see lnet_peers_start_down()).
708 if (!lp->lp_alive && lp->lp_alive_count > 0 &&
709 cfs_time_aftereq(lp->lp_timestamp, lp->lp_last_alive))
712 deadline = cfs_time_add(lp->lp_last_alive,
713 cfs_time_seconds(lp->lp_ni->ni_peertimeout));
714 alive = cfs_time_after(deadline, now);
716 /* Update obsolete lp_alive except for routers assumed to be dead
717 * initially, because router checker would update aliveness in this
718 * case, and moreover lp_last_alive at peer creation is assumed.
720 if (alive && !lp->lp_alive &&
721 !(lnet_isrouter(lp) && lp->lp_alive_count == 0))
722 lnet_notify_locked(lp, 0, 1, lp->lp_last_alive);
728 /* NB: returns 1 when alive, 0 when dead, negative when error;
729 * may drop the lnet_net_lock */
731 lnet_peer_alive_locked (lnet_peer_t *lp)
733 cfs_time_t now = cfs_time_current();
735 if (!lnet_peer_aliveness_enabled(lp))
738 if (lnet_peer_is_alive(lp, now))
741 /* Peer appears dead, but we should avoid frequent NI queries (at
742 * most once per lnet_queryinterval seconds). */
743 if (lp->lp_last_query != 0) {
744 static const int lnet_queryinterval = 1;
746 cfs_time_t next_query =
747 cfs_time_add(lp->lp_last_query,
748 cfs_time_seconds(lnet_queryinterval));
750 if (cfs_time_before(now, next_query)) {
752 CWARN("Unexpected aliveness of peer %s: "
754 libcfs_nid2str(lp->lp_nid),
755 (int)now, (int)next_query,
757 lp->lp_ni->ni_peertimeout);
762 /* query NI for latest aliveness news */
763 lnet_ni_query_locked(lp->lp_ni, lp);
765 if (lnet_peer_is_alive(lp, now))
768 lnet_notify_locked(lp, 0, 0, lp->lp_last_alive);
773 * \param msg The message to be sent.
774 * \param do_send True if lnet_ni_send() should be called in this function.
775 * lnet_send() is going to lnet_net_unlock immediately after this, so
776 * it sets do_send FALSE and I don't do the unlock/send/lock bit.
778 * \retval LNET_CREDIT_OK If \a msg sent or OK to send.
779 * \retval LNET_CREDIT_WAIT If \a msg blocked for credit.
780 * \retval -EHOSTUNREACH If the next hop of the message appears dead.
781 * \retval -ECANCELED If the MD of the message has been unlinked.
784 lnet_post_send_locked(lnet_msg_t *msg, int do_send)
786 lnet_peer_t *lp = msg->msg_txpeer;
787 lnet_ni_t *ni = lp->lp_ni;
788 int cpt = msg->msg_tx_cpt;
789 struct lnet_tx_queue *tq = ni->ni_tx_queues[cpt];
791 /* non-lnet_send() callers have checked before */
792 LASSERT(!do_send || msg->msg_tx_delayed);
793 LASSERT(!msg->msg_receiving);
794 LASSERT(msg->msg_tx_committed);
796 /* NB 'lp' is always the next hop */
797 if ((msg->msg_target.pid & LNET_PID_USERFLAG) == 0 &&
798 lnet_peer_alive_locked(lp) == 0) {
799 the_lnet.ln_counters[cpt]->drop_count++;
800 the_lnet.ln_counters[cpt]->drop_length += msg->msg_len;
801 lnet_net_unlock(cpt);
803 CNETERR("Dropping message for %s: peer not alive\n",
804 libcfs_id2str(msg->msg_target));
806 lnet_finalize(ni, msg, -EHOSTUNREACH);
809 return -EHOSTUNREACH;
812 if (msg->msg_md != NULL &&
813 (msg->msg_md->md_flags & LNET_MD_FLAG_ABORTED) != 0) {
814 lnet_net_unlock(cpt);
816 CNETERR("Aborting message for %s: LNetM[DE]Unlink() already "
817 "called on the MD/ME.\n",
818 libcfs_id2str(msg->msg_target));
820 lnet_finalize(ni, msg, -ECANCELED);
826 if (!msg->msg_peertxcredit) {
827 LASSERT((lp->lp_txcredits < 0) ==
828 !list_empty(&lp->lp_txq));
830 msg->msg_peertxcredit = 1;
831 lp->lp_txqnob += msg->msg_len + sizeof(lnet_hdr_t);
834 if (lp->lp_txcredits < lp->lp_mintxcredits)
835 lp->lp_mintxcredits = lp->lp_txcredits;
837 if (lp->lp_txcredits < 0) {
838 msg->msg_tx_delayed = 1;
839 list_add_tail(&msg->msg_list, &lp->lp_txq);
840 return LNET_CREDIT_WAIT;
844 if (!msg->msg_txcredit) {
845 LASSERT((tq->tq_credits < 0) ==
846 !list_empty(&tq->tq_delayed));
848 msg->msg_txcredit = 1;
851 if (tq->tq_credits < tq->tq_credits_min)
852 tq->tq_credits_min = tq->tq_credits;
854 if (tq->tq_credits < 0) {
855 msg->msg_tx_delayed = 1;
856 list_add_tail(&msg->msg_list, &tq->tq_delayed);
857 return LNET_CREDIT_WAIT;
862 lnet_net_unlock(cpt);
863 lnet_ni_send(ni, msg);
866 return LNET_CREDIT_OK;
870 static lnet_rtrbufpool_t *
871 lnet_msg2bufpool(lnet_msg_t *msg)
873 lnet_rtrbufpool_t *rbp;
876 LASSERT(msg->msg_rx_committed);
878 cpt = msg->msg_rx_cpt;
879 rbp = &the_lnet.ln_rtrpools[cpt][0];
881 LASSERT(msg->msg_len <= LNET_MTU);
882 while (msg->msg_len > (unsigned int)rbp->rbp_npages * PAGE_SIZE) {
884 LASSERT(rbp < &the_lnet.ln_rtrpools[cpt][LNET_NRBPOOLS]);
891 lnet_post_routed_recv_locked (lnet_msg_t *msg, int do_recv)
893 /* lnet_parse is going to lnet_net_unlock immediately after this, so it
894 * sets do_recv FALSE and I don't do the unlock/send/lock bit.
895 * I return LNET_CREDIT_WAIT if msg blocked and LNET_CREDIT_OK if
896 * received or OK to receive */
897 lnet_peer_t *lp = msg->msg_rxpeer;
898 lnet_rtrbufpool_t *rbp;
901 LASSERT(msg->msg_iov == NULL);
902 LASSERT(msg->msg_kiov == NULL);
903 LASSERT(msg->msg_niov == 0);
904 LASSERT(msg->msg_routing);
905 LASSERT(msg->msg_receiving);
906 LASSERT(!msg->msg_sending);
908 /* non-lnet_parse callers only receive delayed messages */
909 LASSERT(!do_recv || msg->msg_rx_delayed);
911 if (!msg->msg_peerrtrcredit) {
912 LASSERT((lp->lp_rtrcredits < 0) ==
913 !list_empty(&lp->lp_rtrq));
915 msg->msg_peerrtrcredit = 1;
917 if (lp->lp_rtrcredits < lp->lp_minrtrcredits)
918 lp->lp_minrtrcredits = lp->lp_rtrcredits;
920 if (lp->lp_rtrcredits < 0) {
921 /* must have checked eager_recv before here */
922 LASSERT(msg->msg_rx_ready_delay);
923 msg->msg_rx_delayed = 1;
924 list_add_tail(&msg->msg_list, &lp->lp_rtrq);
925 return LNET_CREDIT_WAIT;
929 rbp = lnet_msg2bufpool(msg);
931 if (!msg->msg_rtrcredit) {
932 msg->msg_rtrcredit = 1;
934 if (rbp->rbp_credits < rbp->rbp_mincredits)
935 rbp->rbp_mincredits = rbp->rbp_credits;
937 if (rbp->rbp_credits < 0) {
938 /* must have checked eager_recv before here */
939 LASSERT(msg->msg_rx_ready_delay);
940 msg->msg_rx_delayed = 1;
941 list_add_tail(&msg->msg_list, &rbp->rbp_msgs);
942 return LNET_CREDIT_WAIT;
946 LASSERT(!list_empty(&rbp->rbp_bufs));
947 rb = list_entry(rbp->rbp_bufs.next, lnet_rtrbuf_t, rb_list);
948 list_del(&rb->rb_list);
950 msg->msg_niov = rbp->rbp_npages;
951 msg->msg_kiov = &rb->rb_kiov[0];
954 int cpt = msg->msg_rx_cpt;
956 lnet_net_unlock(cpt);
957 lnet_ni_recv(lp->lp_ni, msg->msg_private, msg, 1,
958 0, msg->msg_len, msg->msg_len);
961 return LNET_CREDIT_OK;
965 lnet_return_tx_credits_locked(lnet_msg_t *msg)
967 lnet_peer_t *txpeer = msg->msg_txpeer;
970 if (msg->msg_txcredit) {
971 struct lnet_ni *ni = txpeer->lp_ni;
972 struct lnet_tx_queue *tq = ni->ni_tx_queues[msg->msg_tx_cpt];
974 /* give back NI txcredits */
975 msg->msg_txcredit = 0;
977 LASSERT((tq->tq_credits < 0) ==
978 !list_empty(&tq->tq_delayed));
981 if (tq->tq_credits <= 0) {
982 msg2 = list_entry(tq->tq_delayed.next,
983 lnet_msg_t, msg_list);
984 list_del(&msg2->msg_list);
986 LASSERT(msg2->msg_txpeer->lp_ni == ni);
987 LASSERT(msg2->msg_tx_delayed);
989 (void) lnet_post_send_locked(msg2, 1);
993 if (msg->msg_peertxcredit) {
994 /* give back peer txcredits */
995 msg->msg_peertxcredit = 0;
997 LASSERT((txpeer->lp_txcredits < 0) ==
998 !list_empty(&txpeer->lp_txq));
1000 txpeer->lp_txqnob -= msg->msg_len + sizeof(lnet_hdr_t);
1001 LASSERT(txpeer->lp_txqnob >= 0);
1003 txpeer->lp_txcredits++;
1004 if (txpeer->lp_txcredits <= 0) {
1005 msg2 = list_entry(txpeer->lp_txq.next,
1006 lnet_msg_t, msg_list);
1007 list_del(&msg2->msg_list);
1009 LASSERT(msg2->msg_txpeer == txpeer);
1010 LASSERT(msg2->msg_tx_delayed);
1012 (void) lnet_post_send_locked(msg2, 1);
1016 if (txpeer != NULL) {
1017 msg->msg_txpeer = NULL;
1018 lnet_peer_decref_locked(txpeer);
1023 lnet_schedule_blocked_locked(lnet_rtrbufpool_t *rbp)
1027 if (list_empty(&rbp->rbp_msgs))
1029 msg = list_entry(rbp->rbp_msgs.next,
1030 lnet_msg_t, msg_list);
1031 list_del(&msg->msg_list);
1033 (void)lnet_post_routed_recv_locked(msg, 1);
1037 lnet_drop_routed_msgs_locked(struct list_head *list, int cpt)
1041 struct list_head drop;
1043 INIT_LIST_HEAD(&drop);
1045 list_splice_init(list, &drop);
1047 lnet_net_unlock(cpt);
1049 list_for_each_entry_safe(msg, tmp, &drop, msg_list) {
1050 lnet_ni_recv(msg->msg_rxpeer->lp_ni, msg->msg_private, NULL,
1051 0, 0, 0, msg->msg_hdr.payload_length);
1052 list_del_init(&msg->msg_list);
1053 lnet_finalize(NULL, msg, -ECANCELED);
1060 lnet_return_rx_credits_locked(lnet_msg_t *msg)
1062 lnet_peer_t *rxpeer = msg->msg_rxpeer;
1065 if (msg->msg_rtrcredit) {
1066 /* give back global router credits */
1068 lnet_rtrbufpool_t *rbp;
1070 /* NB If a msg ever blocks for a buffer in rbp_msgs, it stays
1071 * there until it gets one allocated, or aborts the wait
1073 LASSERT(msg->msg_kiov != NULL);
1075 rb = list_entry(msg->msg_kiov, lnet_rtrbuf_t, rb_kiov[0]);
1078 msg->msg_kiov = NULL;
1079 msg->msg_rtrcredit = 0;
1081 LASSERT(rbp == lnet_msg2bufpool(msg));
1083 LASSERT((rbp->rbp_credits > 0) ==
1084 !list_empty(&rbp->rbp_bufs));
1086 /* If routing is now turned off, we just drop this buffer and
1087 * don't bother trying to return credits. */
1088 if (!the_lnet.ln_routing) {
1089 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1093 /* It is possible that a user has lowered the desired number of
1094 * buffers in this pool. Make sure we never put back
1095 * more buffers than the stated number. */
1096 if (unlikely(rbp->rbp_credits >= rbp->rbp_req_nbuffers)) {
1097 /* Discard this buffer so we don't have too
1099 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1100 rbp->rbp_nbuffers--;
1102 list_add(&rb->rb_list, &rbp->rbp_bufs);
1104 if (rbp->rbp_credits <= 0)
1105 lnet_schedule_blocked_locked(rbp);
1110 if (msg->msg_peerrtrcredit) {
1111 /* give back peer router credits */
1112 msg->msg_peerrtrcredit = 0;
1114 LASSERT((rxpeer->lp_rtrcredits < 0) ==
1115 !list_empty(&rxpeer->lp_rtrq));
1117 rxpeer->lp_rtrcredits++;
1119 /* drop all messages which are queued to be routed on that
1121 if (!the_lnet.ln_routing) {
1122 lnet_drop_routed_msgs_locked(&rxpeer->lp_rtrq,
1124 } else if (rxpeer->lp_rtrcredits <= 0) {
1125 msg2 = list_entry(rxpeer->lp_rtrq.next,
1126 lnet_msg_t, msg_list);
1127 list_del(&msg2->msg_list);
1129 (void) lnet_post_routed_recv_locked(msg2, 1);
1132 if (rxpeer != NULL) {
1133 msg->msg_rxpeer = NULL;
1134 lnet_peer_decref_locked(rxpeer);
1139 lnet_compare_routes(lnet_route_t *r1, lnet_route_t *r2)
1141 lnet_peer_t *p1 = r1->lr_gateway;
1142 lnet_peer_t *p2 = r2->lr_gateway;
1143 int r1_hops = (r1->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r1->lr_hops;
1144 int r2_hops = (r2->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r2->lr_hops;
1146 if (r1->lr_priority < r2->lr_priority)
1149 if (r1->lr_priority > r2->lr_priority)
1152 if (r1_hops < r2_hops)
1155 if (r1_hops > r2_hops)
1158 if (p1->lp_txqnob < p2->lp_txqnob)
1161 if (p1->lp_txqnob > p2->lp_txqnob)
1164 if (p1->lp_txcredits > p2->lp_txcredits)
1167 if (p1->lp_txcredits < p2->lp_txcredits)
1170 if (r1->lr_seq - r2->lr_seq <= 0)
1176 static lnet_peer_t *
1177 lnet_find_route_locked(lnet_ni_t *ni, lnet_nid_t target, lnet_nid_t rtr_nid)
1179 lnet_remotenet_t *rnet;
1180 lnet_route_t *route;
1181 lnet_route_t *best_route;
1182 lnet_route_t *last_route;
1183 struct lnet_peer *lp_best;
1184 struct lnet_peer *lp;
1187 /* If @rtr_nid is not LNET_NID_ANY, return the gateway with
1188 * rtr_nid nid, otherwise find the best gateway I can use */
1190 rnet = lnet_find_net_locked(LNET_NIDNET(target));
1195 best_route = last_route = NULL;
1196 list_for_each_entry(route, &rnet->lrn_routes, lr_list) {
1197 lp = route->lr_gateway;
1199 if (!lnet_is_route_alive(route))
1202 if (ni != NULL && lp->lp_ni != ni)
1205 if (lp->lp_nid == rtr_nid) /* it's pre-determined router */
1208 if (lp_best == NULL) {
1209 best_route = last_route = route;
1214 /* no protection on below fields, but it's harmless */
1215 if (last_route->lr_seq - route->lr_seq < 0)
1218 rc = lnet_compare_routes(route, best_route);
1226 /* set sequence number on the best router to the latest sequence + 1
1227 * so we can round-robin all routers, it's race and inaccurate but
1228 * harmless and functional */
1229 if (best_route != NULL)
1230 best_route->lr_seq = last_route->lr_seq + 1;
1235 lnet_send(lnet_nid_t src_nid, lnet_msg_t *msg, lnet_nid_t rtr_nid)
1237 lnet_nid_t dst_nid = msg->msg_target.nid;
1238 struct lnet_ni *src_ni;
1239 struct lnet_ni *local_ni;
1240 struct lnet_peer *lp;
1245 /* NB: rtr_nid is set to LNET_NID_ANY for all current use-cases,
1246 * but we might want to use pre-determined router for ACK/REPLY
1248 /* NB: ni != NULL == interface pre-determined (ACK/REPLY) */
1249 LASSERT(msg->msg_txpeer == NULL);
1250 LASSERT(!msg->msg_sending);
1251 LASSERT(!msg->msg_target_is_router);
1252 LASSERT(!msg->msg_receiving);
1254 msg->msg_sending = 1;
1256 LASSERT(!msg->msg_tx_committed);
1257 cpt = lnet_cpt_of_nid(rtr_nid == LNET_NID_ANY ? dst_nid : rtr_nid);
1261 if (the_lnet.ln_shutdown) {
1262 lnet_net_unlock(cpt);
1266 if (src_nid == LNET_NID_ANY) {
1269 src_ni = lnet_nid2ni_locked(src_nid, cpt);
1270 if (src_ni == NULL) {
1271 lnet_net_unlock(cpt);
1272 LCONSOLE_WARN("Can't send to %s: src %s is not a "
1273 "local nid\n", libcfs_nid2str(dst_nid),
1274 libcfs_nid2str(src_nid));
1277 LASSERT(!msg->msg_routing);
1280 /* Is this for someone on a local network? */
1281 local_ni = lnet_net2ni_locked(LNET_NIDNET(dst_nid), cpt);
1283 if (local_ni != NULL) {
1284 if (src_ni == NULL) {
1286 src_nid = src_ni->ni_nid;
1287 } else if (src_ni == local_ni) {
1288 lnet_ni_decref_locked(local_ni, cpt);
1290 lnet_ni_decref_locked(local_ni, cpt);
1291 lnet_ni_decref_locked(src_ni, cpt);
1292 lnet_net_unlock(cpt);
1293 LCONSOLE_WARN("No route to %s via from %s\n",
1294 libcfs_nid2str(dst_nid),
1295 libcfs_nid2str(src_nid));
1299 LASSERT(src_nid != LNET_NID_ANY);
1300 lnet_msg_commit(msg, cpt);
1302 if (!msg->msg_routing)
1303 msg->msg_hdr.src_nid = cpu_to_le64(src_nid);
1305 if (src_ni == the_lnet.ln_loni) {
1306 /* No send credit hassles with LOLND */
1307 lnet_net_unlock(cpt);
1308 lnet_ni_send(src_ni, msg);
1311 lnet_ni_decref_locked(src_ni, cpt);
1312 lnet_net_unlock(cpt);
1316 rc = lnet_nid2peer_locked(&lp, dst_nid, cpt);
1317 /* lp has ref on src_ni; lose mine */
1318 lnet_ni_decref_locked(src_ni, cpt);
1320 lnet_net_unlock(cpt);
1321 LCONSOLE_WARN("Error %d finding peer %s\n", rc,
1322 libcfs_nid2str(dst_nid));
1323 /* ENOMEM or shutting down */
1326 LASSERT(lp->lp_ni == src_ni);
1328 /* sending to a remote network */
1329 lp = lnet_find_route_locked(src_ni, dst_nid, rtr_nid);
1332 lnet_ni_decref_locked(src_ni, cpt);
1333 lnet_net_unlock(cpt);
1335 LCONSOLE_WARN("No route to %s via %s "
1336 "(all routers down)\n",
1337 libcfs_id2str(msg->msg_target),
1338 libcfs_nid2str(src_nid));
1339 return -EHOSTUNREACH;
1342 /* rtr_nid is LNET_NID_ANY or NID of pre-determined router,
1343 * it's possible that rtr_nid isn't LNET_NID_ANY and lp isn't
1344 * pre-determined router, this can happen if router table
1345 * was changed when we release the lock */
1346 if (rtr_nid != lp->lp_nid) {
1347 cpt2 = lnet_cpt_of_nid_locked(lp->lp_nid);
1350 lnet_ni_decref_locked(src_ni, cpt);
1351 lnet_net_unlock(cpt);
1353 rtr_nid = lp->lp_nid;
1359 CDEBUG(D_NET, "Best route to %s via %s for %s %d\n",
1360 libcfs_nid2str(dst_nid), libcfs_nid2str(lp->lp_nid),
1361 lnet_msgtyp2str(msg->msg_type), msg->msg_len);
1363 if (src_ni == NULL) {
1365 src_nid = src_ni->ni_nid;
1367 LASSERT(src_ni == lp->lp_ni);
1368 lnet_ni_decref_locked(src_ni, cpt);
1371 lnet_peer_addref_locked(lp);
1373 LASSERT(src_nid != LNET_NID_ANY);
1374 lnet_msg_commit(msg, cpt);
1376 if (!msg->msg_routing) {
1377 /* I'm the source and now I know which NI to send on */
1378 msg->msg_hdr.src_nid = cpu_to_le64(src_nid);
1381 msg->msg_target_is_router = 1;
1382 msg->msg_target.nid = lp->lp_nid;
1383 msg->msg_target.pid = LNET_PID_LUSTRE;
1386 /* 'lp' is our best choice of peer */
1388 LASSERT(!msg->msg_peertxcredit);
1389 LASSERT(!msg->msg_txcredit);
1390 LASSERT(msg->msg_txpeer == NULL);
1392 msg->msg_txpeer = lp; /* msg takes my ref on lp */
1394 rc = lnet_post_send_locked(msg, 0);
1395 lnet_net_unlock(cpt);
1400 if (rc == LNET_CREDIT_OK)
1401 lnet_ni_send(src_ni, msg);
1403 return 0; /* rc == LNET_CREDIT_OK or LNET_CREDIT_WAIT */
1407 lnet_drop_message(lnet_ni_t *ni, int cpt, void *private, unsigned int nob)
1410 the_lnet.ln_counters[cpt]->drop_count++;
1411 the_lnet.ln_counters[cpt]->drop_length += nob;
1412 lnet_net_unlock(cpt);
1414 lnet_ni_recv(ni, private, NULL, 0, 0, 0, nob);
1418 lnet_recv_put(lnet_ni_t *ni, lnet_msg_t *msg)
1420 lnet_hdr_t *hdr = &msg->msg_hdr;
1422 if (msg->msg_wanted != 0)
1423 lnet_setpayloadbuffer(msg);
1425 lnet_build_msg_event(msg, LNET_EVENT_PUT);
1427 /* Must I ACK? If so I'll grab the ack_wmd out of the header and put
1428 * it back into the ACK during lnet_finalize() */
1429 msg->msg_ack = (!lnet_is_wire_handle_none(&hdr->msg.put.ack_wmd) &&
1430 (msg->msg_md->md_options & LNET_MD_ACK_DISABLE) == 0);
1432 lnet_ni_recv(ni, msg->msg_private, msg, msg->msg_rx_delayed,
1433 msg->msg_offset, msg->msg_wanted, hdr->payload_length);
1437 lnet_parse_put(lnet_ni_t *ni, lnet_msg_t *msg)
1439 lnet_hdr_t *hdr = &msg->msg_hdr;
1440 struct lnet_match_info info;
1444 /* Convert put fields to host byte order */
1445 hdr->msg.put.match_bits = le64_to_cpu(hdr->msg.put.match_bits);
1446 hdr->msg.put.ptl_index = le32_to_cpu(hdr->msg.put.ptl_index);
1447 hdr->msg.put.offset = le32_to_cpu(hdr->msg.put.offset);
1449 info.mi_id.nid = hdr->src_nid;
1450 info.mi_id.pid = hdr->src_pid;
1451 info.mi_opc = LNET_MD_OP_PUT;
1452 info.mi_portal = hdr->msg.put.ptl_index;
1453 info.mi_rlength = hdr->payload_length;
1454 info.mi_roffset = hdr->msg.put.offset;
1455 info.mi_mbits = hdr->msg.put.match_bits;
1457 msg->msg_rx_ready_delay = ni->ni_lnd->lnd_eager_recv == NULL;
1458 ready_delay = msg->msg_rx_ready_delay;
1461 rc = lnet_ptl_match_md(&info, msg);
1466 case LNET_MATCHMD_OK:
1467 lnet_recv_put(ni, msg);
1470 case LNET_MATCHMD_NONE:
1472 /* no eager_recv or has already called it, should
1473 * have been attached on delayed list */
1476 rc = lnet_ni_eager_recv(ni, msg);
1483 case LNET_MATCHMD_DROP:
1484 CNETERR("Dropping PUT from %s portal %d match %llu"
1485 " offset %d length %d: %d\n",
1486 libcfs_id2str(info.mi_id), info.mi_portal,
1487 info.mi_mbits, info.mi_roffset, info.mi_rlength, rc);
1489 return -ENOENT; /* -ve: OK but no match */
1494 lnet_parse_get(lnet_ni_t *ni, lnet_msg_t *msg, int rdma_get)
1496 struct lnet_match_info info;
1497 lnet_hdr_t *hdr = &msg->msg_hdr;
1498 lnet_handle_wire_t reply_wmd;
1501 /* Convert get fields to host byte order */
1502 hdr->msg.get.match_bits = le64_to_cpu(hdr->msg.get.match_bits);
1503 hdr->msg.get.ptl_index = le32_to_cpu(hdr->msg.get.ptl_index);
1504 hdr->msg.get.sink_length = le32_to_cpu(hdr->msg.get.sink_length);
1505 hdr->msg.get.src_offset = le32_to_cpu(hdr->msg.get.src_offset);
1507 info.mi_id.nid = hdr->src_nid;
1508 info.mi_id.pid = hdr->src_pid;
1509 info.mi_opc = LNET_MD_OP_GET;
1510 info.mi_portal = hdr->msg.get.ptl_index;
1511 info.mi_rlength = hdr->msg.get.sink_length;
1512 info.mi_roffset = hdr->msg.get.src_offset;
1513 info.mi_mbits = hdr->msg.get.match_bits;
1515 rc = lnet_ptl_match_md(&info, msg);
1516 if (rc == LNET_MATCHMD_DROP) {
1517 CNETERR("Dropping GET from %s portal %d match %llu"
1518 " offset %d length %d\n",
1519 libcfs_id2str(info.mi_id), info.mi_portal,
1520 info.mi_mbits, info.mi_roffset, info.mi_rlength);
1521 return -ENOENT; /* -ve: OK but no match */
1524 LASSERT(rc == LNET_MATCHMD_OK);
1526 lnet_build_msg_event(msg, LNET_EVENT_GET);
1528 reply_wmd = hdr->msg.get.return_wmd;
1530 lnet_prep_send(msg, LNET_MSG_REPLY, info.mi_id,
1531 msg->msg_offset, msg->msg_wanted);
1533 msg->msg_hdr.msg.reply.dst_wmd = reply_wmd;
1536 /* The LND completes the REPLY from her recv procedure */
1537 lnet_ni_recv(ni, msg->msg_private, msg, 0,
1538 msg->msg_offset, msg->msg_len, msg->msg_len);
1542 lnet_ni_recv(ni, msg->msg_private, NULL, 0, 0, 0, 0);
1543 msg->msg_receiving = 0;
1545 rc = lnet_send(ni->ni_nid, msg, LNET_NID_ANY);
1547 /* didn't get as far as lnet_ni_send() */
1548 CERROR("%s: Unable to send REPLY for GET from %s: %d\n",
1549 libcfs_nid2str(ni->ni_nid),
1550 libcfs_id2str(info.mi_id), rc);
1552 lnet_finalize(ni, msg, rc);
1559 lnet_parse_reply(lnet_ni_t *ni, lnet_msg_t *msg)
1561 void *private = msg->msg_private;
1562 lnet_hdr_t *hdr = &msg->msg_hdr;
1563 lnet_process_id_t src = {0};
1569 cpt = lnet_cpt_of_cookie(hdr->msg.reply.dst_wmd.wh_object_cookie);
1572 src.nid = hdr->src_nid;
1573 src.pid = hdr->src_pid;
1575 /* NB handles only looked up by creator (no flips) */
1576 md = lnet_wire_handle2md(&hdr->msg.reply.dst_wmd);
1577 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
1578 CNETERR("%s: Dropping REPLY from %s for %s "
1580 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1581 (md == NULL) ? "invalid" : "inactive",
1582 hdr->msg.reply.dst_wmd.wh_interface_cookie,
1583 hdr->msg.reply.dst_wmd.wh_object_cookie);
1584 if (md != NULL && md->md_me != NULL)
1585 CERROR("REPLY MD also attached to portal %d\n",
1586 md->md_me->me_portal);
1588 lnet_res_unlock(cpt);
1589 return -ENOENT; /* -ve: OK but no match */
1592 LASSERT(md->md_offset == 0);
1594 rlength = hdr->payload_length;
1595 mlength = MIN(rlength, (int)md->md_length);
1597 if (mlength < rlength &&
1598 (md->md_options & LNET_MD_TRUNCATE) == 0) {
1599 CNETERR("%s: Dropping REPLY from %s length %d "
1600 "for MD %#llx would overflow (%d)\n",
1601 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1602 rlength, hdr->msg.reply.dst_wmd.wh_object_cookie,
1604 lnet_res_unlock(cpt);
1605 return -ENOENT; /* -ve: OK but no match */
1608 CDEBUG(D_NET, "%s: Reply from %s of length %d/%d into md %#llx\n",
1609 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1610 mlength, rlength, hdr->msg.reply.dst_wmd.wh_object_cookie);
1612 lnet_msg_attach_md(msg, md, 0, mlength);
1615 lnet_setpayloadbuffer(msg);
1617 lnet_res_unlock(cpt);
1619 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
1621 lnet_ni_recv(ni, private, msg, 0, 0, mlength, rlength);
1626 lnet_parse_ack(lnet_ni_t *ni, lnet_msg_t *msg)
1628 lnet_hdr_t *hdr = &msg->msg_hdr;
1629 lnet_process_id_t src = {0};
1633 src.nid = hdr->src_nid;
1634 src.pid = hdr->src_pid;
1636 /* Convert ack fields to host byte order */
1637 hdr->msg.ack.match_bits = le64_to_cpu(hdr->msg.ack.match_bits);
1638 hdr->msg.ack.mlength = le32_to_cpu(hdr->msg.ack.mlength);
1640 cpt = lnet_cpt_of_cookie(hdr->msg.ack.dst_wmd.wh_object_cookie);
1643 /* NB handles only looked up by creator (no flips) */
1644 md = lnet_wire_handle2md(&hdr->msg.ack.dst_wmd);
1645 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
1646 /* Don't moan; this is expected */
1648 "%s: Dropping ACK from %s to %s MD %#llx.%#llx\n",
1649 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1650 (md == NULL) ? "invalid" : "inactive",
1651 hdr->msg.ack.dst_wmd.wh_interface_cookie,
1652 hdr->msg.ack.dst_wmd.wh_object_cookie);
1653 if (md != NULL && md->md_me != NULL)
1654 CERROR("Source MD also attached to portal %d\n",
1655 md->md_me->me_portal);
1657 lnet_res_unlock(cpt);
1658 return -ENOENT; /* -ve! */
1661 CDEBUG(D_NET, "%s: ACK from %s into md %#llx\n",
1662 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1663 hdr->msg.ack.dst_wmd.wh_object_cookie);
1665 lnet_msg_attach_md(msg, md, 0, 0);
1667 lnet_res_unlock(cpt);
1669 lnet_build_msg_event(msg, LNET_EVENT_ACK);
1671 lnet_ni_recv(ni, msg->msg_private, msg, 0, 0, 0, msg->msg_len);
1676 * \retval LNET_CREDIT_OK If \a msg is forwarded
1677 * \retval LNET_CREDIT_WAIT If \a msg is blocked because w/o buffer
1678 * \retval -ve error code
1681 lnet_parse_forward_locked(lnet_ni_t *ni, lnet_msg_t *msg)
1685 if (!the_lnet.ln_routing)
1688 if (msg->msg_rxpeer->lp_rtrcredits <= 0 ||
1689 lnet_msg2bufpool(msg)->rbp_credits <= 0) {
1690 if (ni->ni_lnd->lnd_eager_recv == NULL) {
1691 msg->msg_rx_ready_delay = 1;
1693 lnet_net_unlock(msg->msg_rx_cpt);
1694 rc = lnet_ni_eager_recv(ni, msg);
1695 lnet_net_lock(msg->msg_rx_cpt);
1700 rc = lnet_post_routed_recv_locked(msg, 0);
1705 lnet_parse_local(lnet_ni_t *ni, lnet_msg_t *msg)
1709 switch (msg->msg_type) {
1711 rc = lnet_parse_ack(ni, msg);
1714 rc = lnet_parse_put(ni, msg);
1717 rc = lnet_parse_get(ni, msg, msg->msg_rdma_get);
1719 case LNET_MSG_REPLY:
1720 rc = lnet_parse_reply(ni, msg);
1722 default: /* prevent an unused label if !kernel */
1727 LASSERT(rc == 0 || rc == -ENOENT);
1732 lnet_msgtyp2str (int type)
1741 case LNET_MSG_REPLY:
1743 case LNET_MSG_HELLO:
1746 return ("<UNKNOWN>");
1751 lnet_print_hdr(lnet_hdr_t * hdr)
1753 lnet_process_id_t src = {0};
1754 lnet_process_id_t dst = {0};
1755 char *type_str = lnet_msgtyp2str(hdr->type);
1757 src.nid = hdr->src_nid;
1758 src.pid = hdr->src_pid;
1760 dst.nid = hdr->dest_nid;
1761 dst.pid = hdr->dest_pid;
1763 CWARN("P3 Header at %p of type %s\n", hdr, type_str);
1764 CWARN(" From %s\n", libcfs_id2str(src));
1765 CWARN(" To %s\n", libcfs_id2str(dst));
1767 switch (hdr->type) {
1772 CWARN(" Ptl index %d, ack md %#llx.%#llx, "
1773 "match bits %llu\n",
1774 hdr->msg.put.ptl_index,
1775 hdr->msg.put.ack_wmd.wh_interface_cookie,
1776 hdr->msg.put.ack_wmd.wh_object_cookie,
1777 hdr->msg.put.match_bits);
1778 CWARN(" Length %d, offset %d, hdr data %#llx\n",
1779 hdr->payload_length, hdr->msg.put.offset,
1780 hdr->msg.put.hdr_data);
1784 CWARN(" Ptl index %d, return md %#llx.%#llx, "
1785 "match bits %llu\n", hdr->msg.get.ptl_index,
1786 hdr->msg.get.return_wmd.wh_interface_cookie,
1787 hdr->msg.get.return_wmd.wh_object_cookie,
1788 hdr->msg.get.match_bits);
1789 CWARN(" Length %d, src offset %d\n",
1790 hdr->msg.get.sink_length,
1791 hdr->msg.get.src_offset);
1795 CWARN(" dst md %#llx.%#llx, "
1796 "manipulated length %d\n",
1797 hdr->msg.ack.dst_wmd.wh_interface_cookie,
1798 hdr->msg.ack.dst_wmd.wh_object_cookie,
1799 hdr->msg.ack.mlength);
1802 case LNET_MSG_REPLY:
1803 CWARN(" dst md %#llx.%#llx, "
1805 hdr->msg.reply.dst_wmd.wh_interface_cookie,
1806 hdr->msg.reply.dst_wmd.wh_object_cookie,
1807 hdr->payload_length);
1813 lnet_parse(lnet_ni_t *ni, lnet_hdr_t *hdr, lnet_nid_t from_nid,
1814 void *private, int rdma_req)
1819 struct lnet_msg *msg;
1820 lnet_pid_t dest_pid;
1821 lnet_nid_t dest_nid;
1823 __u32 payload_length;
1826 LASSERT (!in_interrupt ());
1828 type = le32_to_cpu(hdr->type);
1829 src_nid = le64_to_cpu(hdr->src_nid);
1830 dest_nid = le64_to_cpu(hdr->dest_nid);
1831 dest_pid = le32_to_cpu(hdr->dest_pid);
1832 payload_length = le32_to_cpu(hdr->payload_length);
1834 for_me = (ni->ni_nid == dest_nid);
1835 cpt = lnet_cpt_of_nid(from_nid);
1840 if (payload_length > 0) {
1841 CERROR("%s, src %s: bad %s payload %d (0 expected)\n",
1842 libcfs_nid2str(from_nid),
1843 libcfs_nid2str(src_nid),
1844 lnet_msgtyp2str(type), payload_length);
1850 case LNET_MSG_REPLY:
1851 if (payload_length >
1852 (__u32)(for_me ? LNET_MAX_PAYLOAD : LNET_MTU)) {
1853 CERROR("%s, src %s: bad %s payload %d "
1854 "(%d max expected)\n",
1855 libcfs_nid2str(from_nid),
1856 libcfs_nid2str(src_nid),
1857 lnet_msgtyp2str(type),
1859 for_me ? LNET_MAX_PAYLOAD : LNET_MTU);
1865 CERROR("%s, src %s: Bad message type 0x%x\n",
1866 libcfs_nid2str(from_nid),
1867 libcfs_nid2str(src_nid), type);
1871 if (the_lnet.ln_routing &&
1872 ni->ni_last_alive != ktime_get_real_seconds()) {
1873 /* NB: so far here is the only place to set NI status to "up */
1875 ni->ni_last_alive = ktime_get_real_seconds();
1876 if (ni->ni_status != NULL &&
1877 ni->ni_status->ns_status == LNET_NI_STATUS_DOWN)
1878 ni->ni_status->ns_status = LNET_NI_STATUS_UP;
1882 /* Regard a bad destination NID as a protocol error. Senders should
1883 * know what they're doing; if they don't they're misconfigured, buggy
1884 * or malicious so we chop them off at the knees :) */
1887 if (LNET_NIDNET(dest_nid) == LNET_NIDNET(ni->ni_nid)) {
1888 /* should have gone direct */
1889 CERROR("%s, src %s: Bad dest nid %s "
1890 "(should have been sent direct)\n",
1891 libcfs_nid2str(from_nid),
1892 libcfs_nid2str(src_nid),
1893 libcfs_nid2str(dest_nid));
1897 if (lnet_islocalnid(dest_nid)) {
1898 /* dest is another local NI; sender should have used
1899 * this node's NID on its own network */
1900 CERROR("%s, src %s: Bad dest nid %s "
1901 "(it's my nid but on a different network)\n",
1902 libcfs_nid2str(from_nid),
1903 libcfs_nid2str(src_nid),
1904 libcfs_nid2str(dest_nid));
1908 if (rdma_req && type == LNET_MSG_GET) {
1909 CERROR("%s, src %s: Bad optimized GET for %s "
1910 "(final destination must be me)\n",
1911 libcfs_nid2str(from_nid),
1912 libcfs_nid2str(src_nid),
1913 libcfs_nid2str(dest_nid));
1917 if (!the_lnet.ln_routing) {
1918 CERROR("%s, src %s: Dropping message for %s "
1919 "(routing not enabled)\n",
1920 libcfs_nid2str(from_nid),
1921 libcfs_nid2str(src_nid),
1922 libcfs_nid2str(dest_nid));
1927 /* Message looks OK; we're not going to return an error, so we MUST
1928 * call back lnd_recv() come what may... */
1930 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
1931 fail_peer(src_nid, 0)) { /* shall we now? */
1932 CERROR("%s, src %s: Dropping %s to simulate failure\n",
1933 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
1934 lnet_msgtyp2str(type));
1938 if (!list_empty(&the_lnet.ln_drop_rules) &&
1939 lnet_drop_rule_match(hdr)) {
1940 CDEBUG(D_NET, "%s, src %s, dst %s: Dropping %s to simulate"
1941 "silent message loss\n",
1942 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
1943 libcfs_nid2str(dest_nid), lnet_msgtyp2str(type));
1948 msg = lnet_msg_alloc();
1950 CERROR("%s, src %s: Dropping %s (out of memory)\n",
1951 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
1952 lnet_msgtyp2str(type));
1956 /* msg zeroed in lnet_msg_alloc; i.e. flags all clear,
1957 * pointers NULL etc */
1959 msg->msg_type = type;
1960 msg->msg_private = private;
1961 msg->msg_receiving = 1;
1962 msg->msg_rdma_get = rdma_req;
1963 msg->msg_len = msg->msg_wanted = payload_length;
1964 msg->msg_offset = 0;
1965 msg->msg_hdr = *hdr;
1966 /* for building message event */
1967 msg->msg_from = from_nid;
1969 msg->msg_target.pid = dest_pid;
1970 msg->msg_target.nid = dest_nid;
1971 msg->msg_routing = 1;
1974 /* convert common msg->hdr fields to host byteorder */
1975 msg->msg_hdr.type = type;
1976 msg->msg_hdr.src_nid = src_nid;
1977 msg->msg_hdr.src_pid = le32_to_cpu(msg->msg_hdr.src_pid);
1978 msg->msg_hdr.dest_nid = dest_nid;
1979 msg->msg_hdr.dest_pid = dest_pid;
1980 msg->msg_hdr.payload_length = payload_length;
1984 rc = lnet_nid2peer_locked(&msg->msg_rxpeer, from_nid, cpt);
1986 lnet_net_unlock(cpt);
1987 CERROR("%s, src %s: Dropping %s "
1988 "(error %d looking up sender)\n",
1989 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
1990 lnet_msgtyp2str(type), rc);
1992 if (rc == -ESHUTDOWN)
1993 /* We are shutting down. Don't do anything more */
1998 if (lnet_isrouter(msg->msg_rxpeer)) {
1999 lnet_peer_set_alive(msg->msg_rxpeer);
2000 if (avoid_asym_router_failure &&
2001 LNET_NIDNET(src_nid) != LNET_NIDNET(from_nid)) {
2002 /* received a remote message from router, update
2003 * remote NI status on this router.
2004 * NB: multi-hop routed message will be ignored.
2006 lnet_router_ni_update_locked(msg->msg_rxpeer,
2007 LNET_NIDNET(src_nid));
2011 lnet_msg_commit(msg, cpt);
2013 /* message delay simulation */
2014 if (unlikely(!list_empty(&the_lnet.ln_delay_rules) &&
2015 lnet_delay_rule_match_locked(hdr, msg))) {
2016 lnet_net_unlock(cpt);
2021 rc = lnet_parse_forward_locked(ni, msg);
2022 lnet_net_unlock(cpt);
2027 if (rc == LNET_CREDIT_OK) {
2028 lnet_ni_recv(ni, msg->msg_private, msg, 0,
2029 0, payload_length, payload_length);
2034 lnet_net_unlock(cpt);
2036 rc = lnet_parse_local(ni, msg);
2042 LASSERT(msg->msg_md == NULL);
2043 lnet_finalize(ni, msg, rc);
2046 lnet_drop_message(ni, cpt, private, payload_length);
2049 EXPORT_SYMBOL(lnet_parse);
2052 lnet_drop_delayed_msg_list(struct list_head *head, char *reason)
2054 while (!list_empty(head)) {
2055 lnet_process_id_t id = {0};
2058 msg = list_entry(head->next, lnet_msg_t, msg_list);
2059 list_del(&msg->msg_list);
2061 id.nid = msg->msg_hdr.src_nid;
2062 id.pid = msg->msg_hdr.src_pid;
2064 LASSERT(msg->msg_md == NULL);
2065 LASSERT(msg->msg_rx_delayed);
2066 LASSERT(msg->msg_rxpeer != NULL);
2067 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
2069 CWARN("Dropping delayed PUT from %s portal %d match %llu"
2070 " offset %d length %d: %s\n",
2072 msg->msg_hdr.msg.put.ptl_index,
2073 msg->msg_hdr.msg.put.match_bits,
2074 msg->msg_hdr.msg.put.offset,
2075 msg->msg_hdr.payload_length, reason);
2077 /* NB I can't drop msg's ref on msg_rxpeer until after I've
2078 * called lnet_drop_message(), so I just hang onto msg as well
2079 * until that's done */
2081 lnet_drop_message(msg->msg_rxpeer->lp_ni,
2082 msg->msg_rxpeer->lp_cpt,
2083 msg->msg_private, msg->msg_len);
2085 * NB: message will not generate event because w/o attached MD,
2086 * but we still should give error code so lnet_msg_decommit()
2087 * can skip counters operations and other checks.
2089 lnet_finalize(msg->msg_rxpeer->lp_ni, msg, -ENOENT);
2094 lnet_recv_delayed_msg_list(struct list_head *head)
2096 while (!list_empty(head)) {
2098 lnet_process_id_t id;
2100 msg = list_entry(head->next, lnet_msg_t, msg_list);
2101 list_del(&msg->msg_list);
2103 /* md won't disappear under me, since each msg
2104 * holds a ref on it */
2106 id.nid = msg->msg_hdr.src_nid;
2107 id.pid = msg->msg_hdr.src_pid;
2109 LASSERT(msg->msg_rx_delayed);
2110 LASSERT(msg->msg_md != NULL);
2111 LASSERT(msg->msg_rxpeer != NULL);
2112 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
2114 CDEBUG(D_NET, "Resuming delayed PUT from %s portal %d "
2115 "match %llu offset %d length %d.\n",
2116 libcfs_id2str(id), msg->msg_hdr.msg.put.ptl_index,
2117 msg->msg_hdr.msg.put.match_bits,
2118 msg->msg_hdr.msg.put.offset,
2119 msg->msg_hdr.payload_length);
2121 lnet_recv_put(msg->msg_rxpeer->lp_ni, msg);
2126 * Initiate an asynchronous PUT operation.
2128 * There are several events associated with a PUT: completion of the send on
2129 * the initiator node (LNET_EVENT_SEND), and when the send completes
2130 * successfully, the receipt of an acknowledgment (LNET_EVENT_ACK) indicating
2131 * that the operation was accepted by the target. The event LNET_EVENT_PUT is
2132 * used at the target node to indicate the completion of incoming data
2135 * The local events will be logged in the EQ associated with the MD pointed to
2136 * by \a mdh handle. Using a MD without an associated EQ results in these
2137 * events being discarded. In this case, the caller must have another
2138 * mechanism (e.g., a higher level protocol) for determining when it is safe
2139 * to modify the memory region associated with the MD.
2141 * Note that LNet does not guarantee the order of LNET_EVENT_SEND and
2142 * LNET_EVENT_ACK, though intuitively ACK should happen after SEND.
2144 * \param self Indicates the NID of a local interface through which to send
2145 * the PUT request. Use LNET_NID_ANY to let LNet choose one by itself.
2146 * \param mdh A handle for the MD that describes the memory to be sent. The MD
2147 * must be "free floating" (See LNetMDBind()).
2148 * \param ack Controls whether an acknowledgment is requested.
2149 * Acknowledgments are only sent when they are requested by the initiating
2150 * process and the target MD enables them.
2151 * \param target A process identifier for the target process.
2152 * \param portal The index in the \a target's portal table.
2153 * \param match_bits The match bits to use for MD selection at the target
2155 * \param offset The offset into the target MD (only used when the target
2156 * MD has the LNET_MD_MANAGE_REMOTE option set).
2157 * \param hdr_data 64 bits of user data that can be included in the message
2158 * header. This data is written to an event queue entry at the target if an
2159 * EQ is present on the matching MD.
2161 * \retval 0 Success, and only in this case events will be generated
2162 * and logged to EQ (if it exists).
2163 * \retval -EIO Simulated failure.
2164 * \retval -ENOMEM Memory allocation failure.
2165 * \retval -ENOENT Invalid MD object.
2167 * \see lnet_event_t::hdr_data and lnet_event_kind_t.
2170 LNetPut(lnet_nid_t self, lnet_handle_md_t mdh, lnet_ack_req_t ack,
2171 lnet_process_id_t target, unsigned int portal,
2172 __u64 match_bits, unsigned int offset,
2175 struct lnet_msg *msg;
2176 struct lnet_libmd *md;
2180 LASSERT(the_lnet.ln_refcount > 0);
2182 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
2183 fail_peer(target.nid, 1)) { /* shall we now? */
2184 CERROR("Dropping PUT to %s: simulated failure\n",
2185 libcfs_id2str(target));
2189 msg = lnet_msg_alloc();
2191 CERROR("Dropping PUT to %s: ENOMEM on lnet_msg_t\n",
2192 libcfs_id2str(target));
2195 msg->msg_vmflush = !!memory_pressure_get();
2197 cpt = lnet_cpt_of_cookie(mdh.cookie);
2200 md = lnet_handle2md(&mdh);
2201 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
2202 CERROR("Dropping PUT (%llu:%d:%s): MD (%d) invalid\n",
2203 match_bits, portal, libcfs_id2str(target),
2204 md == NULL ? -1 : md->md_threshold);
2205 if (md != NULL && md->md_me != NULL)
2206 CERROR("Source MD also attached to portal %d\n",
2207 md->md_me->me_portal);
2208 lnet_res_unlock(cpt);
2214 CDEBUG(D_NET, "LNetPut -> %s\n", libcfs_id2str(target));
2216 lnet_msg_attach_md(msg, md, 0, 0);
2218 lnet_prep_send(msg, LNET_MSG_PUT, target, 0, md->md_length);
2220 msg->msg_hdr.msg.put.match_bits = cpu_to_le64(match_bits);
2221 msg->msg_hdr.msg.put.ptl_index = cpu_to_le32(portal);
2222 msg->msg_hdr.msg.put.offset = cpu_to_le32(offset);
2223 msg->msg_hdr.msg.put.hdr_data = hdr_data;
2225 /* NB handles only looked up by creator (no flips) */
2226 if (ack == LNET_ACK_REQ) {
2227 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
2228 the_lnet.ln_interface_cookie;
2229 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
2230 md->md_lh.lh_cookie;
2232 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
2233 LNET_WIRE_HANDLE_COOKIE_NONE;
2234 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
2235 LNET_WIRE_HANDLE_COOKIE_NONE;
2238 lnet_res_unlock(cpt);
2240 lnet_build_msg_event(msg, LNET_EVENT_SEND);
2242 rc = lnet_send(self, msg, LNET_NID_ANY);
2244 CNETERR("Error sending PUT to %s: %d\n",
2245 libcfs_id2str(target), rc);
2246 lnet_finalize(NULL, msg, rc);
2249 /* completion will be signalled by an event */
2252 EXPORT_SYMBOL(LNetPut);
2255 lnet_create_reply_msg (lnet_ni_t *ni, lnet_msg_t *getmsg)
2257 /* The LND can DMA direct to the GET md (i.e. no REPLY msg). This
2258 * returns a msg for the LND to pass to lnet_finalize() when the sink
2259 * data has been received.
2261 * CAVEAT EMPTOR: 'getmsg' is the original GET, which is freed when
2262 * lnet_finalize() is called on it, so the LND must call this first */
2264 struct lnet_msg *msg = lnet_msg_alloc();
2265 struct lnet_libmd *getmd = getmsg->msg_md;
2266 lnet_process_id_t peer_id = getmsg->msg_target;
2269 LASSERT(!getmsg->msg_target_is_router);
2270 LASSERT(!getmsg->msg_routing);
2273 CERROR("%s: Dropping REPLY from %s: can't allocate msg\n",
2274 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id));
2278 cpt = lnet_cpt_of_cookie(getmd->md_lh.lh_cookie);
2281 LASSERT(getmd->md_refcount > 0);
2283 if (getmd->md_threshold == 0) {
2284 CERROR("%s: Dropping REPLY from %s for inactive MD %p\n",
2285 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id),
2287 lnet_res_unlock(cpt);
2291 LASSERT(getmd->md_offset == 0);
2293 CDEBUG(D_NET, "%s: Reply from %s md %p\n",
2294 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id), getmd);
2296 /* setup information for lnet_build_msg_event */
2297 msg->msg_from = peer_id.nid;
2298 msg->msg_type = LNET_MSG_GET; /* flag this msg as an "optimized" GET */
2299 msg->msg_hdr.src_nid = peer_id.nid;
2300 msg->msg_hdr.payload_length = getmd->md_length;
2301 msg->msg_receiving = 1; /* required by lnet_msg_attach_md */
2303 lnet_msg_attach_md(msg, getmd, getmd->md_offset, getmd->md_length);
2304 lnet_res_unlock(cpt);
2306 cpt = lnet_cpt_of_nid(peer_id.nid);
2309 lnet_msg_commit(msg, cpt);
2310 lnet_net_unlock(cpt);
2312 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
2317 cpt = lnet_cpt_of_nid(peer_id.nid);
2320 the_lnet.ln_counters[cpt]->drop_count++;
2321 the_lnet.ln_counters[cpt]->drop_length += getmd->md_length;
2322 lnet_net_unlock(cpt);
2329 EXPORT_SYMBOL(lnet_create_reply_msg);
2332 lnet_set_reply_msg_len(lnet_ni_t *ni, lnet_msg_t *reply, unsigned int len)
2334 /* Set the REPLY length, now the RDMA that elides the REPLY message has
2335 * completed and I know it. */
2336 LASSERT(reply != NULL);
2337 LASSERT(reply->msg_type == LNET_MSG_GET);
2338 LASSERT(reply->msg_ev.type == LNET_EVENT_REPLY);
2340 /* NB I trusted my peer to RDMA. If she tells me she's written beyond
2341 * the end of my buffer, I might as well be dead. */
2342 LASSERT(len <= reply->msg_ev.mlength);
2344 reply->msg_ev.mlength = len;
2346 EXPORT_SYMBOL(lnet_set_reply_msg_len);
2349 * Initiate an asynchronous GET operation.
2351 * On the initiator node, an LNET_EVENT_SEND is logged when the GET request
2352 * is sent, and an LNET_EVENT_REPLY is logged when the data returned from
2353 * the target node in the REPLY has been written to local MD.
2355 * On the target node, an LNET_EVENT_GET is logged when the GET request
2356 * arrives and is accepted into a MD.
2358 * \param self,target,portal,match_bits,offset See the discussion in LNetPut().
2359 * \param mdh A handle for the MD that describes the memory into which the
2360 * requested data will be received. The MD must be "free floating" (See LNetMDBind()).
2362 * \retval 0 Success, and only in this case events will be generated
2363 * and logged to EQ (if it exists) of the MD.
2364 * \retval -EIO Simulated failure.
2365 * \retval -ENOMEM Memory allocation failure.
2366 * \retval -ENOENT Invalid MD object.
2369 LNetGet(lnet_nid_t self, lnet_handle_md_t mdh,
2370 lnet_process_id_t target, unsigned int portal,
2371 __u64 match_bits, unsigned int offset)
2373 struct lnet_msg *msg;
2374 struct lnet_libmd *md;
2378 LASSERT(the_lnet.ln_refcount > 0);
2380 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
2381 fail_peer(target.nid, 1)) /* shall we now? */
2383 CERROR("Dropping GET to %s: simulated failure\n",
2384 libcfs_id2str(target));
2388 msg = lnet_msg_alloc();
2390 CERROR("Dropping GET to %s: ENOMEM on lnet_msg_t\n",
2391 libcfs_id2str(target));
2395 cpt = lnet_cpt_of_cookie(mdh.cookie);
2398 md = lnet_handle2md(&mdh);
2399 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
2400 CERROR("Dropping GET (%llu:%d:%s): MD (%d) invalid\n",
2401 match_bits, portal, libcfs_id2str(target),
2402 md == NULL ? -1 : md->md_threshold);
2403 if (md != NULL && md->md_me != NULL)
2404 CERROR("REPLY MD also attached to portal %d\n",
2405 md->md_me->me_portal);
2407 lnet_res_unlock(cpt);
2413 CDEBUG(D_NET, "LNetGet -> %s\n", libcfs_id2str(target));
2415 lnet_msg_attach_md(msg, md, 0, 0);
2417 lnet_prep_send(msg, LNET_MSG_GET, target, 0, 0);
2419 msg->msg_hdr.msg.get.match_bits = cpu_to_le64(match_bits);
2420 msg->msg_hdr.msg.get.ptl_index = cpu_to_le32(portal);
2421 msg->msg_hdr.msg.get.src_offset = cpu_to_le32(offset);
2422 msg->msg_hdr.msg.get.sink_length = cpu_to_le32(md->md_length);
2424 /* NB handles only looked up by creator (no flips) */
2425 msg->msg_hdr.msg.get.return_wmd.wh_interface_cookie =
2426 the_lnet.ln_interface_cookie;
2427 msg->msg_hdr.msg.get.return_wmd.wh_object_cookie =
2428 md->md_lh.lh_cookie;
2430 lnet_res_unlock(cpt);
2432 lnet_build_msg_event(msg, LNET_EVENT_SEND);
2434 rc = lnet_send(self, msg, LNET_NID_ANY);
2436 CNETERR("Error sending GET to %s: %d\n",
2437 libcfs_id2str(target), rc);
2438 lnet_finalize(NULL, msg, rc);
2441 /* completion will be signalled by an event */
2444 EXPORT_SYMBOL(LNetGet);
2447 * Calculate distance to node at \a dstnid.
2449 * \param dstnid Target NID.
2450 * \param srcnidp If not NULL, NID of the local interface to reach \a dstnid
2452 * \param orderp If not NULL, order of the route to reach \a dstnid is saved
2455 * \retval 0 If \a dstnid belongs to a local interface, and reserved option
2456 * local_nid_dist_zero is set, which is the default.
2457 * \retval positives Distance to target NID, i.e. number of hops plus one.
2458 * \retval -EHOSTUNREACH If \a dstnid is not reachable.
2461 LNetDist(lnet_nid_t dstnid, lnet_nid_t *srcnidp, __u32 *orderp)
2463 struct list_head *e;
2465 lnet_remotenet_t *rnet;
2466 __u32 dstnet = LNET_NIDNET(dstnid);
2470 struct list_head *rn_list;
2472 /* if !local_nid_dist_zero, I don't return a distance of 0 ever
2473 * (when lustre sees a distance of 0, it substitutes 0@lo), so I
2474 * keep order 0 free for 0@lo and order 1 free for a local NID
2477 LASSERT(the_lnet.ln_refcount > 0);
2479 cpt = lnet_net_lock_current();
2481 list_for_each(e, &the_lnet.ln_nis) {
2482 ni = list_entry(e, lnet_ni_t, ni_list);
2484 if (ni->ni_nid == dstnid) {
2485 if (srcnidp != NULL)
2487 if (orderp != NULL) {
2488 if (LNET_NETTYP(LNET_NIDNET(dstnid)) == LOLND)
2493 lnet_net_unlock(cpt);
2495 return local_nid_dist_zero ? 0 : 1;
2498 if (LNET_NIDNET(ni->ni_nid) == dstnet) {
2499 /* Check if ni was originally created in
2500 * current net namespace.
2501 * If not, assign order above 0xffff0000,
2502 * to make this ni not a priority. */
2503 if (!net_eq(ni->ni_net_ns, current->nsproxy->net_ns))
2504 order += 0xffff0000;
2506 if (srcnidp != NULL)
2507 *srcnidp = ni->ni_nid;
2510 lnet_net_unlock(cpt);
2517 rn_list = lnet_net2rnethash(dstnet);
2518 list_for_each(e, rn_list) {
2519 rnet = list_entry(e, lnet_remotenet_t, lrn_list);
2521 if (rnet->lrn_net == dstnet) {
2522 lnet_route_t *route;
2523 lnet_route_t *shortest = NULL;
2524 __u32 shortest_hops = LNET_UNDEFINED_HOPS;
2527 LASSERT(!list_empty(&rnet->lrn_routes));
2529 list_for_each_entry(route, &rnet->lrn_routes,
2531 route_hops = route->lr_hops;
2532 if (route_hops == LNET_UNDEFINED_HOPS)
2534 if (shortest == NULL ||
2535 route_hops < shortest_hops) {
2537 shortest_hops = route_hops;
2541 LASSERT(shortest != NULL);
2542 hops = shortest_hops;
2543 if (srcnidp != NULL)
2544 *srcnidp = shortest->lr_gateway->lp_ni->ni_nid;
2547 lnet_net_unlock(cpt);
2553 lnet_net_unlock(cpt);
2554 return -EHOSTUNREACH;
2556 EXPORT_SYMBOL(LNetDist);