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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, 2014, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lnet/lnet/lib-move.c
38 * Data movement routines
41 #define DEBUG_SUBSYSTEM S_LNET
43 #include <lnet/lib-lnet.h>
45 static int local_nid_dist_zero = 1;
46 CFS_MODULE_PARM(local_nid_dist_zero, "i", int, 0444,
50 lnet_fail_nid(lnet_nid_t nid, unsigned int threshold)
54 struct list_head *next;
55 struct list_head cull;
57 /* NB: use lnet_net_lock(0) to serialize operations on test peers */
59 /* Adding a new entry */
60 LIBCFS_ALLOC(tp, sizeof(*tp));
65 tp->tp_threshold = threshold;
68 list_add_tail(&tp->tp_list, &the_lnet.ln_test_peers);
73 /* removing entries */
74 INIT_LIST_HEAD(&cull);
78 list_for_each_safe(el, next, &the_lnet.ln_test_peers) {
79 tp = list_entry(el, lnet_test_peer_t, tp_list);
81 if (tp->tp_threshold == 0 || /* needs culling anyway */
82 nid == LNET_NID_ANY || /* removing all entries */
83 tp->tp_nid == nid) { /* matched this one */
84 list_del(&tp->tp_list);
85 list_add(&tp->tp_list, &cull);
91 while (!list_empty(&cull)) {
92 tp = list_entry(cull.next, lnet_test_peer_t, tp_list);
94 list_del(&tp->tp_list);
95 LIBCFS_FREE(tp, sizeof(*tp));
101 fail_peer (lnet_nid_t nid, int outgoing)
103 lnet_test_peer_t *tp;
104 struct list_head *el;
105 struct list_head *next;
106 struct list_head cull;
109 INIT_LIST_HEAD(&cull);
111 /* NB: use lnet_net_lock(0) to serialize operations on test peers */
114 list_for_each_safe(el, next, &the_lnet.ln_test_peers) {
115 tp = list_entry(el, lnet_test_peer_t, tp_list);
117 if (tp->tp_threshold == 0) {
120 /* only cull zombies on outgoing tests,
121 * since we may be at interrupt priority on
122 * incoming messages. */
123 list_del(&tp->tp_list);
124 list_add(&tp->tp_list, &cull);
129 if (tp->tp_nid == LNET_NID_ANY || /* fail every peer */
130 nid == tp->tp_nid) { /* fail this peer */
133 if (tp->tp_threshold != LNET_MD_THRESH_INF) {
136 tp->tp_threshold == 0) {
138 list_del(&tp->tp_list);
139 list_add(&tp->tp_list, &cull);
148 while (!list_empty(&cull)) {
149 tp = list_entry(cull.next, lnet_test_peer_t, tp_list);
150 list_del(&tp->tp_list);
152 LIBCFS_FREE(tp, sizeof(*tp));
159 lnet_iov_nob (unsigned int niov, struct iovec *iov)
161 unsigned int nob = 0;
163 LASSERT(niov == 0 || iov != NULL);
165 nob += (iov++)->iov_len;
169 EXPORT_SYMBOL(lnet_iov_nob);
172 lnet_copy_iov2iov (unsigned int ndiov, struct iovec *diov, unsigned int doffset,
173 unsigned int nsiov, struct iovec *siov, unsigned int soffset,
176 /* NB diov, siov are READ-ONLY */
177 unsigned int this_nob;
182 /* skip complete frags before 'doffset' */
184 while (doffset >= diov->iov_len) {
185 doffset -= diov->iov_len;
191 /* skip complete frags before 'soffset' */
193 while (soffset >= siov->iov_len) {
194 soffset -= siov->iov_len;
203 this_nob = MIN(diov->iov_len - doffset,
204 siov->iov_len - soffset);
205 this_nob = MIN(this_nob, nob);
207 memcpy ((char *)diov->iov_base + doffset,
208 (char *)siov->iov_base + soffset, this_nob);
211 if (diov->iov_len > doffset + this_nob) {
219 if (siov->iov_len > soffset + this_nob) {
228 EXPORT_SYMBOL(lnet_copy_iov2iov);
231 lnet_extract_iov (int dst_niov, struct iovec *dst,
232 int src_niov, struct iovec *src,
233 unsigned int offset, unsigned int len)
235 /* Initialise 'dst' to the subset of 'src' starting at 'offset',
236 * for exactly 'len' bytes, and return the number of entries.
237 * NB not destructive to 'src' */
238 unsigned int frag_len;
241 if (len == 0) /* no data => */
242 return (0); /* no frags */
244 LASSERT (src_niov > 0);
245 while (offset >= src->iov_len) { /* skip initial frags */
246 offset -= src->iov_len;
249 LASSERT (src_niov > 0);
254 LASSERT (src_niov > 0);
255 LASSERT ((int)niov <= dst_niov);
257 frag_len = src->iov_len - offset;
258 dst->iov_base = ((char *)src->iov_base) + offset;
260 if (len <= frag_len) {
265 dst->iov_len = frag_len;
275 EXPORT_SYMBOL(lnet_extract_iov);
279 lnet_kiov_nob (unsigned int niov, lnet_kiov_t *kiov)
281 unsigned int nob = 0;
283 LASSERT(niov == 0 || kiov != NULL);
285 nob += (kiov++)->kiov_len;
289 EXPORT_SYMBOL(lnet_kiov_nob);
292 lnet_copy_kiov2kiov (unsigned int ndiov, lnet_kiov_t *diov, unsigned int doffset,
293 unsigned int nsiov, lnet_kiov_t *siov, unsigned int soffset,
296 /* NB diov, siov are READ-ONLY */
297 unsigned int this_nob;
304 LASSERT (!in_interrupt ());
307 while (doffset >= diov->kiov_len) {
308 doffset -= diov->kiov_len;
315 while (soffset >= siov->kiov_len) {
316 soffset -= siov->kiov_len;
325 this_nob = MIN(diov->kiov_len - doffset,
326 siov->kiov_len - soffset);
327 this_nob = MIN(this_nob, nob);
330 daddr = ((char *)kmap(diov->kiov_page)) +
331 diov->kiov_offset + doffset;
333 saddr = ((char *)kmap(siov->kiov_page)) +
334 siov->kiov_offset + soffset;
336 /* Vanishing risk of kmap deadlock when mapping 2 pages.
337 * However in practice at least one of the kiovs will be mapped
338 * kernel pages and the map/unmap will be NOOPs */
340 memcpy (daddr, saddr, this_nob);
343 if (diov->kiov_len > doffset + this_nob) {
347 kunmap(diov->kiov_page);
354 if (siov->kiov_len > soffset + this_nob) {
358 kunmap(siov->kiov_page);
367 kunmap(diov->kiov_page);
369 kunmap(siov->kiov_page);
371 EXPORT_SYMBOL(lnet_copy_kiov2kiov);
374 lnet_copy_kiov2iov (unsigned int niov, struct iovec *iov, unsigned int iovoffset,
375 unsigned int nkiov, lnet_kiov_t *kiov, unsigned int kiovoffset,
378 /* NB iov, kiov are READ-ONLY */
379 unsigned int this_nob;
385 LASSERT (!in_interrupt ());
388 while (iovoffset >= iov->iov_len) {
389 iovoffset -= iov->iov_len;
396 while (kiovoffset >= kiov->kiov_len) {
397 kiovoffset -= kiov->kiov_len;
406 this_nob = MIN(iov->iov_len - iovoffset,
407 kiov->kiov_len - kiovoffset);
408 this_nob = MIN(this_nob, nob);
411 addr = ((char *)kmap(kiov->kiov_page)) +
412 kiov->kiov_offset + kiovoffset;
414 memcpy ((char *)iov->iov_base + iovoffset, addr, this_nob);
417 if (iov->iov_len > iovoffset + this_nob) {
418 iovoffset += this_nob;
425 if (kiov->kiov_len > kiovoffset + this_nob) {
427 kiovoffset += this_nob;
429 kunmap(kiov->kiov_page);
439 kunmap(kiov->kiov_page);
441 EXPORT_SYMBOL(lnet_copy_kiov2iov);
444 lnet_copy_iov2kiov (unsigned int nkiov, lnet_kiov_t *kiov, unsigned int kiovoffset,
445 unsigned int niov, struct iovec *iov, unsigned int iovoffset,
448 /* NB kiov, iov are READ-ONLY */
449 unsigned int this_nob;
455 LASSERT (!in_interrupt ());
458 while (kiovoffset >= kiov->kiov_len) {
459 kiovoffset -= kiov->kiov_len;
466 while (iovoffset >= iov->iov_len) {
467 iovoffset -= iov->iov_len;
476 this_nob = MIN(kiov->kiov_len - kiovoffset,
477 iov->iov_len - iovoffset);
478 this_nob = MIN(this_nob, nob);
481 addr = ((char *)kmap(kiov->kiov_page)) +
482 kiov->kiov_offset + kiovoffset;
484 memcpy (addr, (char *)iov->iov_base + iovoffset, this_nob);
487 if (kiov->kiov_len > kiovoffset + this_nob) {
489 kiovoffset += this_nob;
491 kunmap(kiov->kiov_page);
498 if (iov->iov_len > iovoffset + this_nob) {
499 iovoffset += this_nob;
508 kunmap(kiov->kiov_page);
510 EXPORT_SYMBOL(lnet_copy_iov2kiov);
513 lnet_extract_kiov (int dst_niov, lnet_kiov_t *dst,
514 int src_niov, lnet_kiov_t *src,
515 unsigned int offset, unsigned int len)
517 /* Initialise 'dst' to the subset of 'src' starting at 'offset',
518 * for exactly 'len' bytes, and return the number of entries.
519 * NB not destructive to 'src' */
520 unsigned int frag_len;
523 if (len == 0) /* no data => */
524 return (0); /* no frags */
526 LASSERT (src_niov > 0);
527 while (offset >= src->kiov_len) { /* skip initial frags */
528 offset -= src->kiov_len;
531 LASSERT (src_niov > 0);
536 LASSERT (src_niov > 0);
537 LASSERT ((int)niov <= dst_niov);
539 frag_len = src->kiov_len - offset;
540 dst->kiov_page = src->kiov_page;
541 dst->kiov_offset = src->kiov_offset + offset;
543 if (len <= frag_len) {
545 LASSERT (dst->kiov_offset + dst->kiov_len <= PAGE_CACHE_SIZE);
549 dst->kiov_len = frag_len;
550 LASSERT (dst->kiov_offset + dst->kiov_len <= PAGE_CACHE_SIZE);
560 EXPORT_SYMBOL(lnet_extract_kiov);
563 lnet_ni_recv(lnet_ni_t *ni, void *private, lnet_msg_t *msg, int delayed,
564 unsigned int offset, unsigned int mlen, unsigned int rlen)
566 unsigned int niov = 0;
567 struct iovec *iov = NULL;
568 lnet_kiov_t *kiov = NULL;
571 LASSERT (!in_interrupt ());
572 LASSERT (mlen == 0 || msg != NULL);
575 LASSERT(msg->msg_receiving);
576 LASSERT(!msg->msg_sending);
577 LASSERT(rlen == msg->msg_len);
578 LASSERT(mlen <= msg->msg_len);
579 LASSERT(msg->msg_offset == offset);
580 LASSERT(msg->msg_wanted == mlen);
582 msg->msg_receiving = 0;
585 niov = msg->msg_niov;
587 kiov = msg->msg_kiov;
590 LASSERT ((iov == NULL) != (kiov == NULL));
594 rc = (ni->ni_lnd->lnd_recv)(ni, private, msg, delayed,
595 niov, iov, kiov, offset, mlen, rlen);
597 lnet_finalize(ni, msg, rc);
601 lnet_setpayloadbuffer(lnet_msg_t *msg)
603 lnet_libmd_t *md = msg->msg_md;
605 LASSERT (msg->msg_len > 0);
606 LASSERT (!msg->msg_routing);
607 LASSERT (md != NULL);
608 LASSERT (msg->msg_niov == 0);
609 LASSERT (msg->msg_iov == NULL);
610 LASSERT (msg->msg_kiov == NULL);
612 msg->msg_niov = md->md_niov;
613 if ((md->md_options & LNET_MD_KIOV) != 0)
614 msg->msg_kiov = md->md_iov.kiov;
616 msg->msg_iov = md->md_iov.iov;
620 lnet_prep_send(lnet_msg_t *msg, int type, lnet_process_id_t target,
621 unsigned int offset, unsigned int len)
623 msg->msg_type = type;
624 msg->msg_target = target;
626 msg->msg_offset = offset;
629 lnet_setpayloadbuffer(msg);
631 memset (&msg->msg_hdr, 0, sizeof (msg->msg_hdr));
632 msg->msg_hdr.type = cpu_to_le32(type);
633 msg->msg_hdr.dest_nid = cpu_to_le64(target.nid);
634 msg->msg_hdr.dest_pid = cpu_to_le32(target.pid);
635 /* src_nid will be set later */
636 msg->msg_hdr.src_pid = cpu_to_le32(the_lnet.ln_pid);
637 msg->msg_hdr.payload_length = cpu_to_le32(len);
641 lnet_ni_send(lnet_ni_t *ni, lnet_msg_t *msg)
643 void *priv = msg->msg_private;
646 LASSERT (!in_interrupt ());
647 LASSERT (LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND ||
648 (msg->msg_txcredit && msg->msg_peertxcredit));
650 rc = (ni->ni_lnd->lnd_send)(ni, priv, msg);
652 lnet_finalize(ni, msg, rc);
656 lnet_ni_eager_recv(lnet_ni_t *ni, lnet_msg_t *msg)
660 LASSERT(!msg->msg_sending);
661 LASSERT(msg->msg_receiving);
662 LASSERT(!msg->msg_rx_ready_delay);
663 LASSERT(ni->ni_lnd->lnd_eager_recv != NULL);
665 msg->msg_rx_ready_delay = 1;
666 rc = (ni->ni_lnd->lnd_eager_recv)(ni, msg->msg_private, msg,
669 CERROR("recv from %s / send to %s aborted: "
670 "eager_recv failed %d\n",
671 libcfs_nid2str(msg->msg_rxpeer->lp_nid),
672 libcfs_id2str(msg->msg_target), rc);
673 LASSERT(rc < 0); /* required by my callers */
679 /* NB: caller shall hold a ref on 'lp' as I'd drop lnet_net_lock */
681 lnet_ni_query_locked(lnet_ni_t *ni, lnet_peer_t *lp)
683 cfs_time_t last_alive = 0;
685 LASSERT(lnet_peer_aliveness_enabled(lp));
686 LASSERT(ni->ni_lnd->lnd_query != NULL);
688 lnet_net_unlock(lp->lp_cpt);
689 (ni->ni_lnd->lnd_query)(ni, lp->lp_nid, &last_alive);
690 lnet_net_lock(lp->lp_cpt);
692 lp->lp_last_query = cfs_time_current();
694 if (last_alive != 0) /* NI has updated timestamp */
695 lp->lp_last_alive = last_alive;
698 /* NB: always called with lnet_net_lock held */
700 lnet_peer_is_alive (lnet_peer_t *lp, cfs_time_t now)
705 LASSERT (lnet_peer_aliveness_enabled(lp));
707 /* Trust lnet_notify() if it has more recent aliveness news, but
708 * ignore the initial assumed death (see lnet_peers_start_down()).
710 if (!lp->lp_alive && lp->lp_alive_count > 0 &&
711 cfs_time_aftereq(lp->lp_timestamp, lp->lp_last_alive))
714 deadline = cfs_time_add(lp->lp_last_alive,
715 cfs_time_seconds(lp->lp_ni->ni_peertimeout));
716 alive = cfs_time_after(deadline, now);
718 /* Update obsolete lp_alive except for routers assumed to be dead
719 * initially, because router checker would update aliveness in this
720 * case, and moreover lp_last_alive at peer creation is assumed.
722 if (alive && !lp->lp_alive &&
723 !(lnet_isrouter(lp) && lp->lp_alive_count == 0))
724 lnet_notify_locked(lp, 0, 1, lp->lp_last_alive);
730 /* NB: returns 1 when alive, 0 when dead, negative when error;
731 * may drop the lnet_net_lock */
733 lnet_peer_alive_locked (lnet_peer_t *lp)
735 cfs_time_t now = cfs_time_current();
737 if (!lnet_peer_aliveness_enabled(lp))
740 if (lnet_peer_is_alive(lp, now))
743 /* Peer appears dead, but we should avoid frequent NI queries (at
744 * most once per lnet_queryinterval seconds). */
745 if (lp->lp_last_query != 0) {
746 static const int lnet_queryinterval = 1;
748 cfs_time_t next_query =
749 cfs_time_add(lp->lp_last_query,
750 cfs_time_seconds(lnet_queryinterval));
752 if (cfs_time_before(now, next_query)) {
754 CWARN("Unexpected aliveness of peer %s: "
756 libcfs_nid2str(lp->lp_nid),
757 (int)now, (int)next_query,
759 lp->lp_ni->ni_peertimeout);
764 /* query NI for latest aliveness news */
765 lnet_ni_query_locked(lp->lp_ni, lp);
767 if (lnet_peer_is_alive(lp, now))
770 lnet_notify_locked(lp, 0, 0, lp->lp_last_alive);
775 * \param msg The message to be sent.
776 * \param do_send True if lnet_ni_send() should be called in this function.
777 * lnet_send() is going to lnet_net_unlock immediately after this, so
778 * it sets do_send FALSE and I don't do the unlock/send/lock bit.
780 * \retval LNET_CREDIT_OK If \a msg sent or OK to send.
781 * \retval LNET_CREDIT_WAIT If \a msg blocked for credit.
782 * \retval -EHOSTUNREACH If the next hop of the message appears dead.
783 * \retval -ECANCELED If the MD of the message has been unlinked.
786 lnet_post_send_locked(lnet_msg_t *msg, int do_send)
788 lnet_peer_t *lp = msg->msg_txpeer;
789 lnet_ni_t *ni = lp->lp_ni;
790 int cpt = msg->msg_tx_cpt;
791 struct lnet_tx_queue *tq = ni->ni_tx_queues[cpt];
793 /* non-lnet_send() callers have checked before */
794 LASSERT(!do_send || msg->msg_tx_delayed);
795 LASSERT(!msg->msg_receiving);
796 LASSERT(msg->msg_tx_committed);
798 /* NB 'lp' is always the next hop */
799 if ((msg->msg_target.pid & LNET_PID_USERFLAG) == 0 &&
800 lnet_peer_alive_locked(lp) == 0) {
801 the_lnet.ln_counters[cpt]->drop_count++;
802 the_lnet.ln_counters[cpt]->drop_length += msg->msg_len;
803 lnet_net_unlock(cpt);
805 CNETERR("Dropping message for %s: peer not alive\n",
806 libcfs_id2str(msg->msg_target));
808 lnet_finalize(ni, msg, -EHOSTUNREACH);
811 return -EHOSTUNREACH;
814 if (msg->msg_md != NULL &&
815 (msg->msg_md->md_flags & LNET_MD_FLAG_ABORTED) != 0) {
816 lnet_net_unlock(cpt);
818 CNETERR("Aborting message for %s: LNetM[DE]Unlink() already "
819 "called on the MD/ME.\n",
820 libcfs_id2str(msg->msg_target));
822 lnet_finalize(ni, msg, -ECANCELED);
828 if (!msg->msg_peertxcredit) {
829 LASSERT((lp->lp_txcredits < 0) ==
830 !list_empty(&lp->lp_txq));
832 msg->msg_peertxcredit = 1;
833 lp->lp_txqnob += msg->msg_len + sizeof(lnet_hdr_t);
836 if (lp->lp_txcredits < lp->lp_mintxcredits)
837 lp->lp_mintxcredits = lp->lp_txcredits;
839 if (lp->lp_txcredits < 0) {
840 msg->msg_tx_delayed = 1;
841 list_add_tail(&msg->msg_list, &lp->lp_txq);
842 return LNET_CREDIT_WAIT;
846 if (!msg->msg_txcredit) {
847 LASSERT((tq->tq_credits < 0) ==
848 !list_empty(&tq->tq_delayed));
850 msg->msg_txcredit = 1;
853 if (tq->tq_credits < tq->tq_credits_min)
854 tq->tq_credits_min = tq->tq_credits;
856 if (tq->tq_credits < 0) {
857 msg->msg_tx_delayed = 1;
858 list_add_tail(&msg->msg_list, &tq->tq_delayed);
859 return LNET_CREDIT_WAIT;
864 lnet_net_unlock(cpt);
865 lnet_ni_send(ni, msg);
868 return LNET_CREDIT_OK;
872 static lnet_rtrbufpool_t *
873 lnet_msg2bufpool(lnet_msg_t *msg)
875 lnet_rtrbufpool_t *rbp;
878 LASSERT(msg->msg_rx_committed);
880 cpt = msg->msg_rx_cpt;
881 rbp = &the_lnet.ln_rtrpools[cpt][0];
883 LASSERT(msg->msg_len <= LNET_MTU);
884 while (msg->msg_len > (unsigned int)rbp->rbp_npages * PAGE_CACHE_SIZE) {
886 LASSERT(rbp < &the_lnet.ln_rtrpools[cpt][LNET_NRBPOOLS]);
893 lnet_post_routed_recv_locked (lnet_msg_t *msg, int do_recv)
895 /* lnet_parse is going to lnet_net_unlock immediately after this, so it
896 * sets do_recv FALSE and I don't do the unlock/send/lock bit.
897 * I return LNET_CREDIT_WAIT if msg blocked and LNET_CREDIT_OK if
898 * received or OK to receive */
899 lnet_peer_t *lp = msg->msg_rxpeer;
900 lnet_rtrbufpool_t *rbp;
903 LASSERT (msg->msg_iov == NULL);
904 LASSERT (msg->msg_kiov == NULL);
905 LASSERT (msg->msg_niov == 0);
906 LASSERT (msg->msg_routing);
907 LASSERT (msg->msg_receiving);
908 LASSERT (!msg->msg_sending);
910 /* non-lnet_parse callers only receive delayed messages */
911 LASSERT(!do_recv || msg->msg_rx_delayed);
913 if (!msg->msg_peerrtrcredit) {
914 LASSERT((lp->lp_rtrcredits < 0) ==
915 !list_empty(&lp->lp_rtrq));
917 msg->msg_peerrtrcredit = 1;
919 if (lp->lp_rtrcredits < lp->lp_minrtrcredits)
920 lp->lp_minrtrcredits = lp->lp_rtrcredits;
922 if (lp->lp_rtrcredits < 0) {
923 /* must have checked eager_recv before here */
924 LASSERT(msg->msg_rx_ready_delay);
925 msg->msg_rx_delayed = 1;
926 list_add_tail(&msg->msg_list, &lp->lp_rtrq);
927 return LNET_CREDIT_WAIT;
931 rbp = lnet_msg2bufpool(msg);
933 if (!msg->msg_rtrcredit) {
934 msg->msg_rtrcredit = 1;
936 if (rbp->rbp_credits < rbp->rbp_mincredits)
937 rbp->rbp_mincredits = rbp->rbp_credits;
939 if (rbp->rbp_credits < 0) {
940 /* must have checked eager_recv before here */
941 LASSERT(msg->msg_rx_ready_delay);
942 msg->msg_rx_delayed = 1;
943 list_add_tail(&msg->msg_list, &rbp->rbp_msgs);
944 return LNET_CREDIT_WAIT;
948 LASSERT(!list_empty(&rbp->rbp_bufs));
949 rb = list_entry(rbp->rbp_bufs.next, lnet_rtrbuf_t, rb_list);
950 list_del(&rb->rb_list);
952 msg->msg_niov = rbp->rbp_npages;
953 msg->msg_kiov = &rb->rb_kiov[0];
956 int cpt = msg->msg_rx_cpt;
958 lnet_net_unlock(cpt);
959 lnet_ni_recv(lp->lp_ni, msg->msg_private, msg, 1,
960 0, msg->msg_len, msg->msg_len);
963 return LNET_CREDIT_OK;
967 lnet_return_tx_credits_locked(lnet_msg_t *msg)
969 lnet_peer_t *txpeer = msg->msg_txpeer;
972 if (msg->msg_txcredit) {
973 struct lnet_ni *ni = txpeer->lp_ni;
974 struct lnet_tx_queue *tq = ni->ni_tx_queues[msg->msg_tx_cpt];
976 /* give back NI txcredits */
977 msg->msg_txcredit = 0;
979 LASSERT((tq->tq_credits < 0) ==
980 !list_empty(&tq->tq_delayed));
983 if (tq->tq_credits <= 0) {
984 msg2 = list_entry(tq->tq_delayed.next,
985 lnet_msg_t, msg_list);
986 list_del(&msg2->msg_list);
988 LASSERT(msg2->msg_txpeer->lp_ni == ni);
989 LASSERT(msg2->msg_tx_delayed);
991 (void) lnet_post_send_locked(msg2, 1);
995 if (msg->msg_peertxcredit) {
996 /* give back peer txcredits */
997 msg->msg_peertxcredit = 0;
999 LASSERT((txpeer->lp_txcredits < 0) ==
1000 !list_empty(&txpeer->lp_txq));
1002 txpeer->lp_txqnob -= msg->msg_len + sizeof(lnet_hdr_t);
1003 LASSERT (txpeer->lp_txqnob >= 0);
1005 txpeer->lp_txcredits++;
1006 if (txpeer->lp_txcredits <= 0) {
1007 msg2 = list_entry(txpeer->lp_txq.next,
1008 lnet_msg_t, msg_list);
1009 list_del(&msg2->msg_list);
1011 LASSERT(msg2->msg_txpeer == txpeer);
1012 LASSERT(msg2->msg_tx_delayed);
1014 (void) lnet_post_send_locked(msg2, 1);
1018 if (txpeer != NULL) {
1019 msg->msg_txpeer = NULL;
1020 lnet_peer_decref_locked(txpeer);
1025 lnet_schedule_blocked_locked(lnet_rtrbufpool_t *rbp)
1029 if (list_empty(&rbp->rbp_msgs))
1031 msg = list_entry(rbp->rbp_msgs.next,
1032 lnet_msg_t, msg_list);
1033 list_del(&msg->msg_list);
1035 (void)lnet_post_routed_recv_locked(msg, 1);
1039 lnet_drop_routed_msgs_locked(struct list_head *list, int cpt)
1043 struct list_head drop;
1045 INIT_LIST_HEAD(&drop);
1047 list_splice_init(list, &drop);
1049 lnet_net_unlock(cpt);
1051 list_for_each_entry_safe(msg, tmp, &drop, msg_list) {
1052 lnet_ni_recv(msg->msg_rxpeer->lp_ni, msg->msg_private, NULL,
1053 0, 0, 0, msg->msg_hdr.payload_length);
1054 list_del_init(&msg->msg_list);
1055 lnet_finalize(NULL, msg, -ECANCELED);
1062 lnet_return_rx_credits_locked(lnet_msg_t *msg)
1064 lnet_peer_t *rxpeer = msg->msg_rxpeer;
1067 if (msg->msg_rtrcredit) {
1068 /* give back global router credits */
1070 lnet_rtrbufpool_t *rbp;
1072 /* NB If a msg ever blocks for a buffer in rbp_msgs, it stays
1073 * there until it gets one allocated, or aborts the wait
1075 LASSERT(msg->msg_kiov != NULL);
1077 rb = list_entry(msg->msg_kiov, lnet_rtrbuf_t, rb_kiov[0]);
1080 msg->msg_kiov = NULL;
1081 msg->msg_rtrcredit = 0;
1083 LASSERT(rbp == lnet_msg2bufpool(msg));
1085 LASSERT((rbp->rbp_credits > 0) ==
1086 !list_empty(&rbp->rbp_bufs));
1088 /* If routing is now turned off, we just drop this buffer and
1089 * don't bother trying to return credits. */
1090 if (!the_lnet.ln_routing) {
1091 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1095 /* It is possible that a user has lowered the desired number of
1096 * buffers in this pool. Make sure we never put back
1097 * more buffers than the stated number. */
1098 if (unlikely(rbp->rbp_credits >= rbp->rbp_req_nbuffers)) {
1099 /* Discard this buffer so we don't have too
1101 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1102 rbp->rbp_nbuffers--;
1104 list_add(&rb->rb_list, &rbp->rbp_bufs);
1106 if (rbp->rbp_credits <= 0)
1107 lnet_schedule_blocked_locked(rbp);
1112 if (msg->msg_peerrtrcredit) {
1113 /* give back peer router credits */
1114 msg->msg_peerrtrcredit = 0;
1116 LASSERT((rxpeer->lp_rtrcredits < 0) ==
1117 !list_empty(&rxpeer->lp_rtrq));
1119 rxpeer->lp_rtrcredits++;
1121 /* drop all messages which are queued to be routed on that
1123 if (!the_lnet.ln_routing) {
1124 lnet_drop_routed_msgs_locked(&rxpeer->lp_rtrq,
1126 } else if (rxpeer->lp_rtrcredits <= 0) {
1127 msg2 = list_entry(rxpeer->lp_rtrq.next,
1128 lnet_msg_t, msg_list);
1129 list_del(&msg2->msg_list);
1131 (void) lnet_post_routed_recv_locked(msg2, 1);
1134 if (rxpeer != NULL) {
1135 msg->msg_rxpeer = NULL;
1136 lnet_peer_decref_locked(rxpeer);
1141 lnet_compare_routes(lnet_route_t *r1, lnet_route_t *r2)
1143 lnet_peer_t *p1 = r1->lr_gateway;
1144 lnet_peer_t *p2 = r2->lr_gateway;
1146 if (r1->lr_priority < r2->lr_priority)
1149 if (r1->lr_priority > r2->lr_priority)
1152 if (r1->lr_hops < r2->lr_hops)
1155 if (r1->lr_hops > r2->lr_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;
1443 /* Convert put fields to host byte order */
1444 hdr->msg.put.match_bits = le64_to_cpu(hdr->msg.put.match_bits);
1445 hdr->msg.put.ptl_index = le32_to_cpu(hdr->msg.put.ptl_index);
1446 hdr->msg.put.offset = le32_to_cpu(hdr->msg.put.offset);
1448 info.mi_id.nid = hdr->src_nid;
1449 info.mi_id.pid = hdr->src_pid;
1450 info.mi_opc = LNET_MD_OP_PUT;
1451 info.mi_portal = hdr->msg.put.ptl_index;
1452 info.mi_rlength = hdr->payload_length;
1453 info.mi_roffset = hdr->msg.put.offset;
1454 info.mi_mbits = hdr->msg.put.match_bits;
1456 msg->msg_rx_ready_delay = ni->ni_lnd->lnd_eager_recv == NULL;
1459 rc = lnet_ptl_match_md(&info, msg);
1464 case LNET_MATCHMD_OK:
1465 lnet_recv_put(ni, msg);
1468 case LNET_MATCHMD_NONE:
1469 if (msg->msg_rx_delayed) /* attached on delayed list */
1472 rc = lnet_ni_eager_recv(ni, msg);
1477 case LNET_MATCHMD_DROP:
1478 CNETERR("Dropping PUT from %s portal %d match "LPU64
1479 " offset %d length %d: %d\n",
1480 libcfs_id2str(info.mi_id), info.mi_portal,
1481 info.mi_mbits, info.mi_roffset, info.mi_rlength, rc);
1483 return ENOENT; /* +ve: OK but no match */
1488 lnet_parse_get(lnet_ni_t *ni, lnet_msg_t *msg, int rdma_get)
1490 struct lnet_match_info info;
1491 lnet_hdr_t *hdr = &msg->msg_hdr;
1492 lnet_handle_wire_t reply_wmd;
1495 /* Convert get fields to host byte order */
1496 hdr->msg.get.match_bits = le64_to_cpu(hdr->msg.get.match_bits);
1497 hdr->msg.get.ptl_index = le32_to_cpu(hdr->msg.get.ptl_index);
1498 hdr->msg.get.sink_length = le32_to_cpu(hdr->msg.get.sink_length);
1499 hdr->msg.get.src_offset = le32_to_cpu(hdr->msg.get.src_offset);
1501 info.mi_id.nid = hdr->src_nid;
1502 info.mi_id.pid = hdr->src_pid;
1503 info.mi_opc = LNET_MD_OP_GET;
1504 info.mi_portal = hdr->msg.get.ptl_index;
1505 info.mi_rlength = hdr->msg.get.sink_length;
1506 info.mi_roffset = hdr->msg.get.src_offset;
1507 info.mi_mbits = hdr->msg.get.match_bits;
1509 rc = lnet_ptl_match_md(&info, msg);
1510 if (rc == LNET_MATCHMD_DROP) {
1511 CNETERR("Dropping GET from %s portal %d match "LPU64
1512 " offset %d length %d\n",
1513 libcfs_id2str(info.mi_id), info.mi_portal,
1514 info.mi_mbits, info.mi_roffset, info.mi_rlength);
1515 return ENOENT; /* +ve: OK but no match */
1518 LASSERT(rc == LNET_MATCHMD_OK);
1520 lnet_build_msg_event(msg, LNET_EVENT_GET);
1522 reply_wmd = hdr->msg.get.return_wmd;
1524 lnet_prep_send(msg, LNET_MSG_REPLY, info.mi_id,
1525 msg->msg_offset, msg->msg_wanted);
1527 msg->msg_hdr.msg.reply.dst_wmd = reply_wmd;
1530 /* The LND completes the REPLY from her recv procedure */
1531 lnet_ni_recv(ni, msg->msg_private, msg, 0,
1532 msg->msg_offset, msg->msg_len, msg->msg_len);
1536 lnet_ni_recv(ni, msg->msg_private, NULL, 0, 0, 0, 0);
1537 msg->msg_receiving = 0;
1539 rc = lnet_send(ni->ni_nid, msg, LNET_NID_ANY);
1541 /* didn't get as far as lnet_ni_send() */
1542 CERROR("%s: Unable to send REPLY for GET from %s: %d\n",
1543 libcfs_nid2str(ni->ni_nid),
1544 libcfs_id2str(info.mi_id), rc);
1546 lnet_finalize(ni, msg, rc);
1553 lnet_parse_reply(lnet_ni_t *ni, lnet_msg_t *msg)
1555 void *private = msg->msg_private;
1556 lnet_hdr_t *hdr = &msg->msg_hdr;
1557 lnet_process_id_t src = {0};
1563 cpt = lnet_cpt_of_cookie(hdr->msg.reply.dst_wmd.wh_object_cookie);
1566 src.nid = hdr->src_nid;
1567 src.pid = hdr->src_pid;
1569 /* NB handles only looked up by creator (no flips) */
1570 md = lnet_wire_handle2md(&hdr->msg.reply.dst_wmd);
1571 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
1572 CNETERR("%s: Dropping REPLY from %s for %s "
1573 "MD "LPX64"."LPX64"\n",
1574 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1575 (md == NULL) ? "invalid" : "inactive",
1576 hdr->msg.reply.dst_wmd.wh_interface_cookie,
1577 hdr->msg.reply.dst_wmd.wh_object_cookie);
1578 if (md != NULL && md->md_me != NULL)
1579 CERROR("REPLY MD also attached to portal %d\n",
1580 md->md_me->me_portal);
1582 lnet_res_unlock(cpt);
1583 return ENOENT; /* +ve: OK but no match */
1586 LASSERT (md->md_offset == 0);
1588 rlength = hdr->payload_length;
1589 mlength = MIN(rlength, (int)md->md_length);
1591 if (mlength < rlength &&
1592 (md->md_options & LNET_MD_TRUNCATE) == 0) {
1593 CNETERR("%s: Dropping REPLY from %s length %d "
1594 "for MD "LPX64" would overflow (%d)\n",
1595 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1596 rlength, hdr->msg.reply.dst_wmd.wh_object_cookie,
1598 lnet_res_unlock(cpt);
1599 return ENOENT; /* +ve: OK but no match */
1602 CDEBUG(D_NET, "%s: Reply from %s of length %d/%d into md "LPX64"\n",
1603 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1604 mlength, rlength, hdr->msg.reply.dst_wmd.wh_object_cookie);
1606 lnet_msg_attach_md(msg, md, 0, mlength);
1609 lnet_setpayloadbuffer(msg);
1611 lnet_res_unlock(cpt);
1613 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
1615 lnet_ni_recv(ni, private, msg, 0, 0, mlength, rlength);
1620 lnet_parse_ack(lnet_ni_t *ni, lnet_msg_t *msg)
1622 lnet_hdr_t *hdr = &msg->msg_hdr;
1623 lnet_process_id_t src = {0};
1627 src.nid = hdr->src_nid;
1628 src.pid = hdr->src_pid;
1630 /* Convert ack fields to host byte order */
1631 hdr->msg.ack.match_bits = le64_to_cpu(hdr->msg.ack.match_bits);
1632 hdr->msg.ack.mlength = le32_to_cpu(hdr->msg.ack.mlength);
1634 cpt = lnet_cpt_of_cookie(hdr->msg.ack.dst_wmd.wh_object_cookie);
1637 /* NB handles only looked up by creator (no flips) */
1638 md = lnet_wire_handle2md(&hdr->msg.ack.dst_wmd);
1639 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
1640 /* Don't moan; this is expected */
1642 "%s: Dropping ACK from %s to %s MD "LPX64"."LPX64"\n",
1643 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1644 (md == NULL) ? "invalid" : "inactive",
1645 hdr->msg.ack.dst_wmd.wh_interface_cookie,
1646 hdr->msg.ack.dst_wmd.wh_object_cookie);
1647 if (md != NULL && md->md_me != NULL)
1648 CERROR("Source MD also attached to portal %d\n",
1649 md->md_me->me_portal);
1651 lnet_res_unlock(cpt);
1652 return ENOENT; /* +ve! */
1655 CDEBUG(D_NET, "%s: ACK from %s into md "LPX64"\n",
1656 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1657 hdr->msg.ack.dst_wmd.wh_object_cookie);
1659 lnet_msg_attach_md(msg, md, 0, 0);
1661 lnet_res_unlock(cpt);
1663 lnet_build_msg_event(msg, LNET_EVENT_ACK);
1665 lnet_ni_recv(ni, msg->msg_private, msg, 0, 0, 0, msg->msg_len);
1670 * \retval LNET_CREDIT_OK If \a msg is forwarded
1671 * \retval LNET_CREDIT_WAIT If \a msg is blocked because w/o buffer
1672 * \retval -ve error code
1675 lnet_parse_forward_locked(lnet_ni_t *ni, lnet_msg_t *msg)
1679 if (!the_lnet.ln_routing)
1682 if (msg->msg_rxpeer->lp_rtrcredits <= 0 ||
1683 lnet_msg2bufpool(msg)->rbp_credits <= 0) {
1684 if (ni->ni_lnd->lnd_eager_recv == NULL) {
1685 msg->msg_rx_ready_delay = 1;
1687 lnet_net_unlock(msg->msg_rx_cpt);
1688 rc = lnet_ni_eager_recv(ni, msg);
1689 lnet_net_lock(msg->msg_rx_cpt);
1694 rc = lnet_post_routed_recv_locked(msg, 0);
1699 lnet_parse_local(lnet_ni_t *ni, lnet_msg_t *msg)
1703 switch (msg->msg_type) {
1705 rc = lnet_parse_ack(ni, msg);
1708 rc = lnet_parse_put(ni, msg);
1711 rc = lnet_parse_get(ni, msg, msg->msg_rdma_get);
1713 case LNET_MSG_REPLY:
1714 rc = lnet_parse_reply(ni, msg);
1716 default: /* prevent an unused label if !kernel */
1721 LASSERT(rc == 0 || rc == ENOENT);
1726 lnet_msgtyp2str (int type)
1735 case LNET_MSG_REPLY:
1737 case LNET_MSG_HELLO:
1740 return ("<UNKNOWN>");
1745 lnet_print_hdr(lnet_hdr_t * hdr)
1747 lnet_process_id_t src = {0};
1748 lnet_process_id_t dst = {0};
1749 char *type_str = lnet_msgtyp2str (hdr->type);
1751 src.nid = hdr->src_nid;
1752 src.pid = hdr->src_pid;
1754 dst.nid = hdr->dest_nid;
1755 dst.pid = hdr->dest_pid;
1757 CWARN("P3 Header at %p of type %s\n", hdr, type_str);
1758 CWARN(" From %s\n", libcfs_id2str(src));
1759 CWARN(" To %s\n", libcfs_id2str(dst));
1761 switch (hdr->type) {
1766 CWARN(" Ptl index %d, ack md "LPX64"."LPX64", "
1767 "match bits "LPU64"\n",
1768 hdr->msg.put.ptl_index,
1769 hdr->msg.put.ack_wmd.wh_interface_cookie,
1770 hdr->msg.put.ack_wmd.wh_object_cookie,
1771 hdr->msg.put.match_bits);
1772 CWARN(" Length %d, offset %d, hdr data "LPX64"\n",
1773 hdr->payload_length, hdr->msg.put.offset,
1774 hdr->msg.put.hdr_data);
1778 CWARN(" Ptl index %d, return md "LPX64"."LPX64", "
1779 "match bits "LPU64"\n", hdr->msg.get.ptl_index,
1780 hdr->msg.get.return_wmd.wh_interface_cookie,
1781 hdr->msg.get.return_wmd.wh_object_cookie,
1782 hdr->msg.get.match_bits);
1783 CWARN(" Length %d, src offset %d\n",
1784 hdr->msg.get.sink_length,
1785 hdr->msg.get.src_offset);
1789 CWARN(" dst md "LPX64"."LPX64", "
1790 "manipulated length %d\n",
1791 hdr->msg.ack.dst_wmd.wh_interface_cookie,
1792 hdr->msg.ack.dst_wmd.wh_object_cookie,
1793 hdr->msg.ack.mlength);
1796 case LNET_MSG_REPLY:
1797 CWARN(" dst md "LPX64"."LPX64", "
1799 hdr->msg.reply.dst_wmd.wh_interface_cookie,
1800 hdr->msg.reply.dst_wmd.wh_object_cookie,
1801 hdr->payload_length);
1807 lnet_parse(lnet_ni_t *ni, lnet_hdr_t *hdr, lnet_nid_t from_nid,
1808 void *private, int rdma_req)
1813 struct lnet_msg *msg;
1814 lnet_pid_t dest_pid;
1815 lnet_nid_t dest_nid;
1817 __u32 payload_length;
1820 LASSERT (!in_interrupt ());
1822 type = le32_to_cpu(hdr->type);
1823 src_nid = le64_to_cpu(hdr->src_nid);
1824 dest_nid = le64_to_cpu(hdr->dest_nid);
1825 dest_pid = le32_to_cpu(hdr->dest_pid);
1826 payload_length = le32_to_cpu(hdr->payload_length);
1828 for_me = (ni->ni_nid == dest_nid);
1829 cpt = lnet_cpt_of_nid(from_nid);
1834 if (payload_length > 0) {
1835 CERROR("%s, src %s: bad %s payload %d (0 expected)\n",
1836 libcfs_nid2str(from_nid),
1837 libcfs_nid2str(src_nid),
1838 lnet_msgtyp2str(type), payload_length);
1844 case LNET_MSG_REPLY:
1845 if (payload_length >
1846 (__u32)(for_me ? LNET_MAX_PAYLOAD : LNET_MTU)) {
1847 CERROR("%s, src %s: bad %s payload %d "
1848 "(%d max expected)\n",
1849 libcfs_nid2str(from_nid),
1850 libcfs_nid2str(src_nid),
1851 lnet_msgtyp2str(type),
1853 for_me ? LNET_MAX_PAYLOAD : LNET_MTU);
1859 CERROR("%s, src %s: Bad message type 0x%x\n",
1860 libcfs_nid2str(from_nid),
1861 libcfs_nid2str(src_nid), type);
1865 if (the_lnet.ln_routing &&
1866 ni->ni_last_alive != cfs_time_current_sec()) {
1867 /* NB: so far here is the only place to set NI status to "up */
1869 ni->ni_last_alive = cfs_time_current_sec();
1870 if (ni->ni_status != NULL &&
1871 ni->ni_status->ns_status == LNET_NI_STATUS_DOWN)
1872 ni->ni_status->ns_status = LNET_NI_STATUS_UP;
1876 /* Regard a bad destination NID as a protocol error. Senders should
1877 * know what they're doing; if they don't they're misconfigured, buggy
1878 * or malicious so we chop them off at the knees :) */
1881 if (LNET_NIDNET(dest_nid) == LNET_NIDNET(ni->ni_nid)) {
1882 /* should have gone direct */
1883 CERROR("%s, src %s: Bad dest nid %s "
1884 "(should have been sent direct)\n",
1885 libcfs_nid2str(from_nid),
1886 libcfs_nid2str(src_nid),
1887 libcfs_nid2str(dest_nid));
1891 if (lnet_islocalnid(dest_nid)) {
1892 /* dest is another local NI; sender should have used
1893 * this node's NID on its own network */
1894 CERROR("%s, src %s: Bad dest nid %s "
1895 "(it's my nid but on a different network)\n",
1896 libcfs_nid2str(from_nid),
1897 libcfs_nid2str(src_nid),
1898 libcfs_nid2str(dest_nid));
1902 if (rdma_req && type == LNET_MSG_GET) {
1903 CERROR("%s, src %s: Bad optimized GET for %s "
1904 "(final destination must be me)\n",
1905 libcfs_nid2str(from_nid),
1906 libcfs_nid2str(src_nid),
1907 libcfs_nid2str(dest_nid));
1911 if (!the_lnet.ln_routing) {
1912 CERROR("%s, src %s: Dropping message for %s "
1913 "(routing not enabled)\n",
1914 libcfs_nid2str(from_nid),
1915 libcfs_nid2str(src_nid),
1916 libcfs_nid2str(dest_nid));
1921 /* Message looks OK; we're not going to return an error, so we MUST
1922 * call back lnd_recv() come what may... */
1924 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
1925 fail_peer(src_nid, 0)) { /* shall we now? */
1926 CERROR("%s, src %s: Dropping %s to simulate failure\n",
1927 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
1928 lnet_msgtyp2str(type));
1932 if (!list_empty(&the_lnet.ln_drop_rules) &&
1933 lnet_drop_rule_match(hdr)) {
1934 CDEBUG(D_NET, "%s, src %s, dst %s: Dropping %s to simulate"
1935 "silent message loss\n",
1936 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
1937 libcfs_nid2str(dest_nid), lnet_msgtyp2str(type));
1942 msg = lnet_msg_alloc();
1944 CERROR("%s, src %s: Dropping %s (out of memory)\n",
1945 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
1946 lnet_msgtyp2str(type));
1950 /* msg zeroed in lnet_msg_alloc; i.e. flags all clear,
1951 * pointers NULL etc */
1953 msg->msg_type = type;
1954 msg->msg_private = private;
1955 msg->msg_receiving = 1;
1956 msg->msg_rdma_get = rdma_req;
1957 msg->msg_len = msg->msg_wanted = payload_length;
1958 msg->msg_offset = 0;
1959 msg->msg_hdr = *hdr;
1960 /* for building message event */
1961 msg->msg_from = from_nid;
1963 msg->msg_target.pid = dest_pid;
1964 msg->msg_target.nid = dest_nid;
1965 msg->msg_routing = 1;
1968 /* convert common msg->hdr fields to host byteorder */
1969 msg->msg_hdr.type = type;
1970 msg->msg_hdr.src_nid = src_nid;
1971 msg->msg_hdr.src_pid = le32_to_cpu(msg->msg_hdr.src_pid);
1972 msg->msg_hdr.dest_nid = dest_nid;
1973 msg->msg_hdr.dest_pid = dest_pid;
1974 msg->msg_hdr.payload_length = payload_length;
1978 rc = lnet_nid2peer_locked(&msg->msg_rxpeer, from_nid, cpt);
1980 lnet_net_unlock(cpt);
1981 CERROR("%s, src %s: Dropping %s "
1982 "(error %d looking up sender)\n",
1983 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
1984 lnet_msgtyp2str(type), rc);
1989 if (lnet_isrouter(msg->msg_rxpeer)) {
1990 lnet_peer_set_alive(msg->msg_rxpeer);
1991 if (avoid_asym_router_failure &&
1992 LNET_NIDNET(src_nid) != LNET_NIDNET(from_nid)) {
1993 /* received a remote message from router, update
1994 * remote NI status on this router.
1995 * NB: multi-hop routed message will be ignored.
1997 lnet_router_ni_update_locked(msg->msg_rxpeer,
1998 LNET_NIDNET(src_nid));
2002 lnet_msg_commit(msg, cpt);
2004 /* message delay simulation */
2005 if (unlikely(!list_empty(&the_lnet.ln_delay_rules) &&
2006 lnet_delay_rule_match_locked(hdr, msg))) {
2007 lnet_net_unlock(cpt);
2012 rc = lnet_parse_forward_locked(ni, msg);
2013 lnet_net_unlock(cpt);
2018 if (rc == LNET_CREDIT_OK) {
2019 lnet_ni_recv(ni, msg->msg_private, msg, 0,
2020 0, payload_length, payload_length);
2025 lnet_net_unlock(cpt);
2027 rc = lnet_parse_local(ni, msg);
2033 LASSERT(msg->msg_md == NULL);
2034 lnet_finalize(ni, msg, rc);
2037 lnet_drop_message(ni, cpt, private, payload_length);
2040 EXPORT_SYMBOL(lnet_parse);
2043 lnet_drop_delayed_msg_list(struct list_head *head, char *reason)
2045 while (!list_empty(head)) {
2046 lnet_process_id_t id = {0};
2049 msg = list_entry(head->next, lnet_msg_t, msg_list);
2050 list_del(&msg->msg_list);
2052 id.nid = msg->msg_hdr.src_nid;
2053 id.pid = msg->msg_hdr.src_pid;
2055 LASSERT(msg->msg_md == NULL);
2056 LASSERT(msg->msg_rx_delayed);
2057 LASSERT(msg->msg_rxpeer != NULL);
2058 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
2060 CWARN("Dropping delayed PUT from %s portal %d match "LPU64
2061 " offset %d length %d: %s\n",
2063 msg->msg_hdr.msg.put.ptl_index,
2064 msg->msg_hdr.msg.put.match_bits,
2065 msg->msg_hdr.msg.put.offset,
2066 msg->msg_hdr.payload_length, reason);
2068 /* NB I can't drop msg's ref on msg_rxpeer until after I've
2069 * called lnet_drop_message(), so I just hang onto msg as well
2070 * until that's done */
2072 lnet_drop_message(msg->msg_rxpeer->lp_ni,
2073 msg->msg_rxpeer->lp_cpt,
2074 msg->msg_private, msg->msg_len);
2076 * NB: message will not generate event because w/o attached MD,
2077 * but we still should give error code so lnet_msg_decommit()
2078 * can skip counters operations and other checks.
2080 lnet_finalize(msg->msg_rxpeer->lp_ni, msg, -ENOENT);
2085 lnet_recv_delayed_msg_list(struct list_head *head)
2087 while (!list_empty(head)) {
2089 lnet_process_id_t id;
2091 msg = list_entry(head->next, lnet_msg_t, msg_list);
2092 list_del(&msg->msg_list);
2094 /* md won't disappear under me, since each msg
2095 * holds a ref on it */
2097 id.nid = msg->msg_hdr.src_nid;
2098 id.pid = msg->msg_hdr.src_pid;
2100 LASSERT(msg->msg_rx_delayed);
2101 LASSERT(msg->msg_md != NULL);
2102 LASSERT(msg->msg_rxpeer != NULL);
2103 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
2105 CDEBUG(D_NET, "Resuming delayed PUT from %s portal %d "
2106 "match "LPU64" offset %d length %d.\n",
2107 libcfs_id2str(id), msg->msg_hdr.msg.put.ptl_index,
2108 msg->msg_hdr.msg.put.match_bits,
2109 msg->msg_hdr.msg.put.offset,
2110 msg->msg_hdr.payload_length);
2112 lnet_recv_put(msg->msg_rxpeer->lp_ni, msg);
2117 * Initiate an asynchronous PUT operation.
2119 * There are several events associated with a PUT: completion of the send on
2120 * the initiator node (LNET_EVENT_SEND), and when the send completes
2121 * successfully, the receipt of an acknowledgment (LNET_EVENT_ACK) indicating
2122 * that the operation was accepted by the target. The event LNET_EVENT_PUT is
2123 * used at the target node to indicate the completion of incoming data
2126 * The local events will be logged in the EQ associated with the MD pointed to
2127 * by \a mdh handle. Using a MD without an associated EQ results in these
2128 * events being discarded. In this case, the caller must have another
2129 * mechanism (e.g., a higher level protocol) for determining when it is safe
2130 * to modify the memory region associated with the MD.
2132 * Note that LNet does not guarantee the order of LNET_EVENT_SEND and
2133 * LNET_EVENT_ACK, though intuitively ACK should happen after SEND.
2135 * \param self Indicates the NID of a local interface through which to send
2136 * the PUT request. Use LNET_NID_ANY to let LNet choose one by itself.
2137 * \param mdh A handle for the MD that describes the memory to be sent. The MD
2138 * must be "free floating" (See LNetMDBind()).
2139 * \param ack Controls whether an acknowledgment is requested.
2140 * Acknowledgments are only sent when they are requested by the initiating
2141 * process and the target MD enables them.
2142 * \param target A process identifier for the target process.
2143 * \param portal The index in the \a target's portal table.
2144 * \param match_bits The match bits to use for MD selection at the target
2146 * \param offset The offset into the target MD (only used when the target
2147 * MD has the LNET_MD_MANAGE_REMOTE option set).
2148 * \param hdr_data 64 bits of user data that can be included in the message
2149 * header. This data is written to an event queue entry at the target if an
2150 * EQ is present on the matching MD.
2152 * \retval 0 Success, and only in this case events will be generated
2153 * and logged to EQ (if it exists).
2154 * \retval -EIO Simulated failure.
2155 * \retval -ENOMEM Memory allocation failure.
2156 * \retval -ENOENT Invalid MD object.
2158 * \see lnet_event_t::hdr_data and lnet_event_kind_t.
2161 LNetPut(lnet_nid_t self, lnet_handle_md_t mdh, lnet_ack_req_t ack,
2162 lnet_process_id_t target, unsigned int portal,
2163 __u64 match_bits, unsigned int offset,
2166 struct lnet_msg *msg;
2167 struct lnet_libmd *md;
2171 LASSERT(the_lnet.ln_refcount > 0);
2173 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
2174 fail_peer(target.nid, 1)) { /* shall we now? */
2175 CERROR("Dropping PUT to %s: simulated failure\n",
2176 libcfs_id2str(target));
2180 msg = lnet_msg_alloc();
2182 CERROR("Dropping PUT to %s: ENOMEM on lnet_msg_t\n",
2183 libcfs_id2str(target));
2186 msg->msg_vmflush = !!memory_pressure_get();
2188 cpt = lnet_cpt_of_cookie(mdh.cookie);
2191 md = lnet_handle2md(&mdh);
2192 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
2193 CERROR("Dropping PUT ("LPU64":%d:%s): MD (%d) invalid\n",
2194 match_bits, portal, libcfs_id2str(target),
2195 md == NULL ? -1 : md->md_threshold);
2196 if (md != NULL && md->md_me != NULL)
2197 CERROR("Source MD also attached to portal %d\n",
2198 md->md_me->me_portal);
2199 lnet_res_unlock(cpt);
2205 CDEBUG(D_NET, "LNetPut -> %s\n", libcfs_id2str(target));
2207 lnet_msg_attach_md(msg, md, 0, 0);
2209 lnet_prep_send(msg, LNET_MSG_PUT, target, 0, md->md_length);
2211 msg->msg_hdr.msg.put.match_bits = cpu_to_le64(match_bits);
2212 msg->msg_hdr.msg.put.ptl_index = cpu_to_le32(portal);
2213 msg->msg_hdr.msg.put.offset = cpu_to_le32(offset);
2214 msg->msg_hdr.msg.put.hdr_data = hdr_data;
2216 /* NB handles only looked up by creator (no flips) */
2217 if (ack == LNET_ACK_REQ) {
2218 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
2219 the_lnet.ln_interface_cookie;
2220 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
2221 md->md_lh.lh_cookie;
2223 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
2224 LNET_WIRE_HANDLE_COOKIE_NONE;
2225 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
2226 LNET_WIRE_HANDLE_COOKIE_NONE;
2229 lnet_res_unlock(cpt);
2231 lnet_build_msg_event(msg, LNET_EVENT_SEND);
2233 rc = lnet_send(self, msg, LNET_NID_ANY);
2235 CNETERR( "Error sending PUT to %s: %d\n",
2236 libcfs_id2str(target), rc);
2237 lnet_finalize (NULL, msg, rc);
2240 /* completion will be signalled by an event */
2243 EXPORT_SYMBOL(LNetPut);
2246 lnet_create_reply_msg (lnet_ni_t *ni, lnet_msg_t *getmsg)
2248 /* The LND can DMA direct to the GET md (i.e. no REPLY msg). This
2249 * returns a msg for the LND to pass to lnet_finalize() when the sink
2250 * data has been received.
2252 * CAVEAT EMPTOR: 'getmsg' is the original GET, which is freed when
2253 * lnet_finalize() is called on it, so the LND must call this first */
2255 struct lnet_msg *msg = lnet_msg_alloc();
2256 struct lnet_libmd *getmd = getmsg->msg_md;
2257 lnet_process_id_t peer_id = getmsg->msg_target;
2260 LASSERT(!getmsg->msg_target_is_router);
2261 LASSERT(!getmsg->msg_routing);
2264 CERROR("%s: Dropping REPLY from %s: can't allocate msg\n",
2265 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id));
2269 cpt = lnet_cpt_of_cookie(getmd->md_lh.lh_cookie);
2272 LASSERT(getmd->md_refcount > 0);
2274 if (getmd->md_threshold == 0) {
2275 CERROR ("%s: Dropping REPLY from %s for inactive MD %p\n",
2276 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id),
2278 lnet_res_unlock(cpt);
2282 LASSERT(getmd->md_offset == 0);
2284 CDEBUG(D_NET, "%s: Reply from %s md %p\n",
2285 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id), getmd);
2287 /* setup information for lnet_build_msg_event */
2288 msg->msg_from = peer_id.nid;
2289 msg->msg_type = LNET_MSG_GET; /* flag this msg as an "optimized" GET */
2290 msg->msg_hdr.src_nid = peer_id.nid;
2291 msg->msg_hdr.payload_length = getmd->md_length;
2292 msg->msg_receiving = 1; /* required by lnet_msg_attach_md */
2294 lnet_msg_attach_md(msg, getmd, getmd->md_offset, getmd->md_length);
2295 lnet_res_unlock(cpt);
2297 cpt = lnet_cpt_of_nid(peer_id.nid);
2300 lnet_msg_commit(msg, cpt);
2301 lnet_net_unlock(cpt);
2303 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
2308 cpt = lnet_cpt_of_nid(peer_id.nid);
2311 the_lnet.ln_counters[cpt]->drop_count++;
2312 the_lnet.ln_counters[cpt]->drop_length += getmd->md_length;
2313 lnet_net_unlock(cpt);
2320 EXPORT_SYMBOL(lnet_create_reply_msg);
2323 lnet_set_reply_msg_len(lnet_ni_t *ni, lnet_msg_t *reply, unsigned int len)
2325 /* Set the REPLY length, now the RDMA that elides the REPLY message has
2326 * completed and I know it. */
2327 LASSERT (reply != NULL);
2328 LASSERT (reply->msg_type == LNET_MSG_GET);
2329 LASSERT (reply->msg_ev.type == LNET_EVENT_REPLY);
2331 /* NB I trusted my peer to RDMA. If she tells me she's written beyond
2332 * the end of my buffer, I might as well be dead. */
2333 LASSERT (len <= reply->msg_ev.mlength);
2335 reply->msg_ev.mlength = len;
2337 EXPORT_SYMBOL(lnet_set_reply_msg_len);
2340 * Initiate an asynchronous GET operation.
2342 * On the initiator node, an LNET_EVENT_SEND is logged when the GET request
2343 * is sent, and an LNET_EVENT_REPLY is logged when the data returned from
2344 * the target node in the REPLY has been written to local MD.
2346 * On the target node, an LNET_EVENT_GET is logged when the GET request
2347 * arrives and is accepted into a MD.
2349 * \param self,target,portal,match_bits,offset See the discussion in LNetPut().
2350 * \param mdh A handle for the MD that describes the memory into which the
2351 * requested data will be received. The MD must be "free floating" (See LNetMDBind()).
2353 * \retval 0 Success, and only in this case events will be generated
2354 * and logged to EQ (if it exists) of the MD.
2355 * \retval -EIO Simulated failure.
2356 * \retval -ENOMEM Memory allocation failure.
2357 * \retval -ENOENT Invalid MD object.
2360 LNetGet(lnet_nid_t self, lnet_handle_md_t mdh,
2361 lnet_process_id_t target, unsigned int portal,
2362 __u64 match_bits, unsigned int offset)
2364 struct lnet_msg *msg;
2365 struct lnet_libmd *md;
2369 LASSERT (the_lnet.ln_refcount > 0);
2371 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
2372 fail_peer(target.nid, 1)) /* shall we now? */
2374 CERROR("Dropping GET to %s: simulated failure\n",
2375 libcfs_id2str(target));
2379 msg = lnet_msg_alloc();
2381 CERROR("Dropping GET to %s: ENOMEM on lnet_msg_t\n",
2382 libcfs_id2str(target));
2386 cpt = lnet_cpt_of_cookie(mdh.cookie);
2389 md = lnet_handle2md(&mdh);
2390 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
2391 CERROR("Dropping GET ("LPU64":%d:%s): MD (%d) invalid\n",
2392 match_bits, portal, libcfs_id2str(target),
2393 md == NULL ? -1 : md->md_threshold);
2394 if (md != NULL && md->md_me != NULL)
2395 CERROR("REPLY MD also attached to portal %d\n",
2396 md->md_me->me_portal);
2398 lnet_res_unlock(cpt);
2404 CDEBUG(D_NET, "LNetGet -> %s\n", libcfs_id2str(target));
2406 lnet_msg_attach_md(msg, md, 0, 0);
2408 lnet_prep_send(msg, LNET_MSG_GET, target, 0, 0);
2410 msg->msg_hdr.msg.get.match_bits = cpu_to_le64(match_bits);
2411 msg->msg_hdr.msg.get.ptl_index = cpu_to_le32(portal);
2412 msg->msg_hdr.msg.get.src_offset = cpu_to_le32(offset);
2413 msg->msg_hdr.msg.get.sink_length = cpu_to_le32(md->md_length);
2415 /* NB handles only looked up by creator (no flips) */
2416 msg->msg_hdr.msg.get.return_wmd.wh_interface_cookie =
2417 the_lnet.ln_interface_cookie;
2418 msg->msg_hdr.msg.get.return_wmd.wh_object_cookie =
2419 md->md_lh.lh_cookie;
2421 lnet_res_unlock(cpt);
2423 lnet_build_msg_event(msg, LNET_EVENT_SEND);
2425 rc = lnet_send(self, msg, LNET_NID_ANY);
2427 CNETERR("Error sending GET to %s: %d\n",
2428 libcfs_id2str(target), rc);
2429 lnet_finalize(NULL, msg, rc);
2432 /* completion will be signalled by an event */
2435 EXPORT_SYMBOL(LNetGet);
2438 * Calculate distance to node at \a dstnid.
2440 * \param dstnid Target NID.
2441 * \param srcnidp If not NULL, NID of the local interface to reach \a dstnid
2443 * \param orderp If not NULL, order of the route to reach \a dstnid is saved
2446 * \retval 0 If \a dstnid belongs to a local interface, and reserved option
2447 * local_nid_dist_zero is set, which is the default.
2448 * \retval positives Distance to target NID, i.e. number of hops plus one.
2449 * \retval -EHOSTUNREACH If \a dstnid is not reachable.
2452 LNetDist(lnet_nid_t dstnid, lnet_nid_t *srcnidp, __u32 *orderp)
2454 struct list_head *e;
2456 lnet_remotenet_t *rnet;
2457 __u32 dstnet = LNET_NIDNET(dstnid);
2461 struct list_head *rn_list;
2463 /* if !local_nid_dist_zero, I don't return a distance of 0 ever
2464 * (when lustre sees a distance of 0, it substitutes 0@lo), so I
2465 * keep order 0 free for 0@lo and order 1 free for a local NID
2468 LASSERT (the_lnet.ln_refcount > 0);
2470 cpt = lnet_net_lock_current();
2472 list_for_each(e, &the_lnet.ln_nis) {
2473 ni = list_entry(e, lnet_ni_t, ni_list);
2475 if (ni->ni_nid == dstnid) {
2476 if (srcnidp != NULL)
2478 if (orderp != NULL) {
2479 if (LNET_NETTYP(LNET_NIDNET(dstnid)) == LOLND)
2484 lnet_net_unlock(cpt);
2486 return local_nid_dist_zero ? 0 : 1;
2489 if (LNET_NIDNET(ni->ni_nid) == dstnet) {
2490 if (srcnidp != NULL)
2491 *srcnidp = ni->ni_nid;
2494 lnet_net_unlock(cpt);
2501 rn_list = lnet_net2rnethash(dstnet);
2502 list_for_each(e, rn_list) {
2503 rnet = list_entry(e, lnet_remotenet_t, lrn_list);
2505 if (rnet->lrn_net == dstnet) {
2506 lnet_route_t *route;
2507 lnet_route_t *shortest = NULL;
2509 LASSERT(!list_empty(&rnet->lrn_routes));
2511 list_for_each_entry(route, &rnet->lrn_routes,
2513 if (shortest == NULL ||
2514 route->lr_hops < shortest->lr_hops)
2518 LASSERT (shortest != NULL);
2519 hops = shortest->lr_hops;
2520 if (srcnidp != NULL)
2521 *srcnidp = shortest->lr_gateway->lp_ni->ni_nid;
2524 lnet_net_unlock(cpt);
2530 lnet_net_unlock(cpt);
2531 return -EHOSTUNREACH;
2533 EXPORT_SYMBOL(LNetDist);