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, 2015, 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>
44 #include <linux/nsproxy.h>
45 #include <net/net_namespace.h>
47 static int local_nid_dist_zero = 1;
48 module_param(local_nid_dist_zero, int, 0444);
49 MODULE_PARM_DESC(local_nid_dist_zero, "Reserved");
52 lnet_fail_nid(lnet_nid_t nid, unsigned int threshold)
56 struct list_head *next;
57 struct list_head cull;
59 /* NB: use lnet_net_lock(0) to serialize operations on test peers */
61 /* Adding a new entry */
62 LIBCFS_ALLOC(tp, sizeof(*tp));
67 tp->tp_threshold = threshold;
70 list_add_tail(&tp->tp_list, &the_lnet.ln_test_peers);
75 /* removing entries */
76 INIT_LIST_HEAD(&cull);
80 list_for_each_safe(el, next, &the_lnet.ln_test_peers) {
81 tp = list_entry(el, lnet_test_peer_t, tp_list);
83 if (tp->tp_threshold == 0 || /* needs culling anyway */
84 nid == LNET_NID_ANY || /* removing all entries */
85 tp->tp_nid == nid) { /* matched this one */
86 list_del(&tp->tp_list);
87 list_add(&tp->tp_list, &cull);
93 while (!list_empty(&cull)) {
94 tp = list_entry(cull.next, lnet_test_peer_t, tp_list);
96 list_del(&tp->tp_list);
97 LIBCFS_FREE(tp, sizeof(*tp));
103 fail_peer (lnet_nid_t nid, int outgoing)
105 lnet_test_peer_t *tp;
106 struct list_head *el;
107 struct list_head *next;
108 struct list_head cull;
111 INIT_LIST_HEAD(&cull);
113 /* NB: use lnet_net_lock(0) to serialize operations on test peers */
116 list_for_each_safe(el, next, &the_lnet.ln_test_peers) {
117 tp = list_entry(el, lnet_test_peer_t, tp_list);
119 if (tp->tp_threshold == 0) {
122 /* only cull zombies on outgoing tests,
123 * since we may be at interrupt priority on
124 * incoming messages. */
125 list_del(&tp->tp_list);
126 list_add(&tp->tp_list, &cull);
131 if (tp->tp_nid == LNET_NID_ANY || /* fail every peer */
132 nid == tp->tp_nid) { /* fail this peer */
135 if (tp->tp_threshold != LNET_MD_THRESH_INF) {
138 tp->tp_threshold == 0) {
140 list_del(&tp->tp_list);
141 list_add(&tp->tp_list, &cull);
150 while (!list_empty(&cull)) {
151 tp = list_entry(cull.next, lnet_test_peer_t, tp_list);
152 list_del(&tp->tp_list);
154 LIBCFS_FREE(tp, sizeof(*tp));
161 lnet_iov_nob(unsigned int niov, struct kvec *iov)
163 unsigned int nob = 0;
165 LASSERT(niov == 0 || iov != NULL);
167 nob += (iov++)->iov_len;
171 EXPORT_SYMBOL(lnet_iov_nob);
174 lnet_copy_iov2iov(unsigned int ndiov, struct kvec *diov, unsigned int doffset,
175 unsigned int nsiov, struct kvec *siov, unsigned int soffset,
178 /* NB diov, siov are READ-ONLY */
179 unsigned int this_nob;
184 /* skip complete frags before 'doffset' */
186 while (doffset >= diov->iov_len) {
187 doffset -= diov->iov_len;
193 /* skip complete frags before 'soffset' */
195 while (soffset >= siov->iov_len) {
196 soffset -= siov->iov_len;
205 this_nob = MIN(diov->iov_len - doffset,
206 siov->iov_len - soffset);
207 this_nob = MIN(this_nob, nob);
209 memcpy ((char *)diov->iov_base + doffset,
210 (char *)siov->iov_base + soffset, this_nob);
213 if (diov->iov_len > doffset + this_nob) {
221 if (siov->iov_len > soffset + this_nob) {
230 EXPORT_SYMBOL(lnet_copy_iov2iov);
233 lnet_extract_iov(int dst_niov, struct kvec *dst,
234 int src_niov, struct kvec *src,
235 unsigned int offset, unsigned int len)
237 /* Initialise 'dst' to the subset of 'src' starting at 'offset',
238 * for exactly 'len' bytes, and return the number of entries.
239 * NB not destructive to 'src' */
240 unsigned int frag_len;
243 if (len == 0) /* no data => */
244 return (0); /* no frags */
246 LASSERT (src_niov > 0);
247 while (offset >= src->iov_len) { /* skip initial frags */
248 offset -= src->iov_len;
251 LASSERT (src_niov > 0);
256 LASSERT (src_niov > 0);
257 LASSERT ((int)niov <= dst_niov);
259 frag_len = src->iov_len - offset;
260 dst->iov_base = ((char *)src->iov_base) + offset;
262 if (len <= frag_len) {
267 dst->iov_len = frag_len;
277 EXPORT_SYMBOL(lnet_extract_iov);
281 lnet_kiov_nob(unsigned int niov, lnet_kiov_t *kiov)
283 unsigned int nob = 0;
285 LASSERT(niov == 0 || kiov != NULL);
287 nob += (kiov++)->kiov_len;
291 EXPORT_SYMBOL(lnet_kiov_nob);
294 lnet_copy_kiov2kiov(unsigned int ndiov, lnet_kiov_t *diov, unsigned int doffset,
295 unsigned int nsiov, lnet_kiov_t *siov, unsigned int soffset,
298 /* NB diov, siov are READ-ONLY */
299 unsigned int this_nob;
306 LASSERT (!in_interrupt ());
309 while (doffset >= diov->kiov_len) {
310 doffset -= diov->kiov_len;
317 while (soffset >= siov->kiov_len) {
318 soffset -= siov->kiov_len;
327 this_nob = MIN(diov->kiov_len - doffset,
328 siov->kiov_len - soffset);
329 this_nob = MIN(this_nob, nob);
332 daddr = ((char *)kmap(diov->kiov_page)) +
333 diov->kiov_offset + doffset;
335 saddr = ((char *)kmap(siov->kiov_page)) +
336 siov->kiov_offset + soffset;
338 /* Vanishing risk of kmap deadlock when mapping 2 pages.
339 * However in practice at least one of the kiovs will be mapped
340 * kernel pages and the map/unmap will be NOOPs */
342 memcpy (daddr, saddr, this_nob);
345 if (diov->kiov_len > doffset + this_nob) {
349 kunmap(diov->kiov_page);
356 if (siov->kiov_len > soffset + this_nob) {
360 kunmap(siov->kiov_page);
369 kunmap(diov->kiov_page);
371 kunmap(siov->kiov_page);
373 EXPORT_SYMBOL(lnet_copy_kiov2kiov);
376 lnet_copy_kiov2iov (unsigned int niov, struct kvec *iov, unsigned int iovoffset,
377 unsigned int nkiov, lnet_kiov_t *kiov, unsigned int kiovoffset,
380 /* NB iov, kiov are READ-ONLY */
381 unsigned int this_nob;
387 LASSERT (!in_interrupt ());
390 while (iovoffset >= iov->iov_len) {
391 iovoffset -= iov->iov_len;
398 while (kiovoffset >= kiov->kiov_len) {
399 kiovoffset -= kiov->kiov_len;
408 this_nob = MIN(iov->iov_len - iovoffset,
409 kiov->kiov_len - kiovoffset);
410 this_nob = MIN(this_nob, nob);
413 addr = ((char *)kmap(kiov->kiov_page)) +
414 kiov->kiov_offset + kiovoffset;
416 memcpy ((char *)iov->iov_base + iovoffset, addr, this_nob);
419 if (iov->iov_len > iovoffset + this_nob) {
420 iovoffset += this_nob;
427 if (kiov->kiov_len > kiovoffset + this_nob) {
429 kiovoffset += this_nob;
431 kunmap(kiov->kiov_page);
441 kunmap(kiov->kiov_page);
443 EXPORT_SYMBOL(lnet_copy_kiov2iov);
446 lnet_copy_iov2kiov(unsigned int nkiov, lnet_kiov_t *kiov, unsigned int kiovoffset,
447 unsigned int niov, struct kvec *iov, unsigned int iovoffset,
450 /* NB kiov, iov are READ-ONLY */
451 unsigned int this_nob;
457 LASSERT (!in_interrupt ());
460 while (kiovoffset >= kiov->kiov_len) {
461 kiovoffset -= kiov->kiov_len;
468 while (iovoffset >= iov->iov_len) {
469 iovoffset -= iov->iov_len;
478 this_nob = MIN(kiov->kiov_len - kiovoffset,
479 iov->iov_len - iovoffset);
480 this_nob = MIN(this_nob, nob);
483 addr = ((char *)kmap(kiov->kiov_page)) +
484 kiov->kiov_offset + kiovoffset;
486 memcpy (addr, (char *)iov->iov_base + iovoffset, this_nob);
489 if (kiov->kiov_len > kiovoffset + this_nob) {
491 kiovoffset += this_nob;
493 kunmap(kiov->kiov_page);
500 if (iov->iov_len > iovoffset + this_nob) {
501 iovoffset += this_nob;
510 kunmap(kiov->kiov_page);
512 EXPORT_SYMBOL(lnet_copy_iov2kiov);
515 lnet_extract_kiov(int dst_niov, lnet_kiov_t *dst,
516 int src_niov, lnet_kiov_t *src,
517 unsigned int offset, unsigned int len)
519 /* Initialise 'dst' to the subset of 'src' starting at 'offset',
520 * for exactly 'len' bytes, and return the number of entries.
521 * NB not destructive to 'src' */
522 unsigned int frag_len;
525 if (len == 0) /* no data => */
526 return (0); /* no frags */
528 LASSERT (src_niov > 0);
529 while (offset >= src->kiov_len) { /* skip initial frags */
530 offset -= src->kiov_len;
533 LASSERT (src_niov > 0);
538 LASSERT (src_niov > 0);
539 LASSERT ((int)niov <= dst_niov);
541 frag_len = src->kiov_len - offset;
542 dst->kiov_page = src->kiov_page;
543 dst->kiov_offset = src->kiov_offset + offset;
545 if (len <= frag_len) {
547 LASSERT (dst->kiov_offset + dst->kiov_len <= PAGE_CACHE_SIZE);
551 dst->kiov_len = frag_len;
552 LASSERT (dst->kiov_offset + dst->kiov_len <= PAGE_CACHE_SIZE);
562 EXPORT_SYMBOL(lnet_extract_kiov);
565 lnet_ni_recv(lnet_ni_t *ni, void *private, lnet_msg_t *msg, int delayed,
566 unsigned int offset, unsigned int mlen, unsigned int rlen)
568 unsigned int niov = 0;
569 struct kvec *iov = NULL;
570 lnet_kiov_t *kiov = NULL;
573 LASSERT (!in_interrupt ());
574 LASSERT (mlen == 0 || msg != NULL);
577 LASSERT(msg->msg_receiving);
578 LASSERT(!msg->msg_sending);
579 LASSERT(rlen == msg->msg_len);
580 LASSERT(mlen <= msg->msg_len);
581 LASSERT(msg->msg_offset == offset);
582 LASSERT(msg->msg_wanted == mlen);
584 msg->msg_receiving = 0;
587 niov = msg->msg_niov;
589 kiov = msg->msg_kiov;
592 LASSERT ((iov == NULL) != (kiov == NULL));
596 rc = (ni->ni_lnd->lnd_recv)(ni, private, msg, delayed,
597 niov, iov, kiov, offset, mlen, rlen);
599 lnet_finalize(ni, msg, rc);
603 lnet_setpayloadbuffer(lnet_msg_t *msg)
605 lnet_libmd_t *md = msg->msg_md;
607 LASSERT (msg->msg_len > 0);
608 LASSERT (!msg->msg_routing);
609 LASSERT (md != NULL);
610 LASSERT (msg->msg_niov == 0);
611 LASSERT (msg->msg_iov == NULL);
612 LASSERT (msg->msg_kiov == NULL);
614 msg->msg_niov = md->md_niov;
615 if ((md->md_options & LNET_MD_KIOV) != 0)
616 msg->msg_kiov = md->md_iov.kiov;
618 msg->msg_iov = md->md_iov.iov;
622 lnet_prep_send(lnet_msg_t *msg, int type, lnet_process_id_t target,
623 unsigned int offset, unsigned int len)
625 msg->msg_type = type;
626 msg->msg_target = target;
628 msg->msg_offset = offset;
631 lnet_setpayloadbuffer(msg);
633 memset (&msg->msg_hdr, 0, sizeof (msg->msg_hdr));
634 msg->msg_hdr.type = cpu_to_le32(type);
635 msg->msg_hdr.dest_nid = cpu_to_le64(target.nid);
636 msg->msg_hdr.dest_pid = cpu_to_le32(target.pid);
637 /* src_nid will be set later */
638 msg->msg_hdr.src_pid = cpu_to_le32(the_lnet.ln_pid);
639 msg->msg_hdr.payload_length = cpu_to_le32(len);
643 lnet_ni_send(lnet_ni_t *ni, lnet_msg_t *msg)
645 void *priv = msg->msg_private;
648 LASSERT (!in_interrupt ());
649 LASSERT (LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND ||
650 (msg->msg_txcredit && msg->msg_peertxcredit));
652 rc = (ni->ni_lnd->lnd_send)(ni, priv, msg);
654 lnet_finalize(ni, msg, rc);
658 lnet_ni_eager_recv(lnet_ni_t *ni, lnet_msg_t *msg)
662 LASSERT(!msg->msg_sending);
663 LASSERT(msg->msg_receiving);
664 LASSERT(!msg->msg_rx_ready_delay);
665 LASSERT(ni->ni_lnd->lnd_eager_recv != NULL);
667 msg->msg_rx_ready_delay = 1;
668 rc = (ni->ni_lnd->lnd_eager_recv)(ni, msg->msg_private, msg,
671 CERROR("recv from %s / send to %s aborted: "
672 "eager_recv failed %d\n",
673 libcfs_nid2str(msg->msg_rxpeer->lp_nid),
674 libcfs_id2str(msg->msg_target), rc);
675 LASSERT(rc < 0); /* required by my callers */
681 /* NB: caller shall hold a ref on 'lp' as I'd drop lnet_net_lock */
683 lnet_ni_query_locked(lnet_ni_t *ni, lnet_peer_t *lp)
685 cfs_time_t last_alive = 0;
687 LASSERT(lnet_peer_aliveness_enabled(lp));
688 LASSERT(ni->ni_lnd->lnd_query != NULL);
690 lnet_net_unlock(lp->lp_cpt);
691 (ni->ni_lnd->lnd_query)(ni, lp->lp_nid, &last_alive);
692 lnet_net_lock(lp->lp_cpt);
694 lp->lp_last_query = cfs_time_current();
696 if (last_alive != 0) /* NI has updated timestamp */
697 lp->lp_last_alive = last_alive;
700 /* NB: always called with lnet_net_lock held */
702 lnet_peer_is_alive (lnet_peer_t *lp, cfs_time_t now)
707 LASSERT (lnet_peer_aliveness_enabled(lp));
709 /* Trust lnet_notify() if it has more recent aliveness news, but
710 * ignore the initial assumed death (see lnet_peers_start_down()).
712 if (!lp->lp_alive && lp->lp_alive_count > 0 &&
713 cfs_time_aftereq(lp->lp_timestamp, lp->lp_last_alive))
716 deadline = cfs_time_add(lp->lp_last_alive,
717 cfs_time_seconds(lp->lp_ni->ni_peertimeout));
718 alive = cfs_time_after(deadline, now);
720 /* Update obsolete lp_alive except for routers assumed to be dead
721 * initially, because router checker would update aliveness in this
722 * case, and moreover lp_last_alive at peer creation is assumed.
724 if (alive && !lp->lp_alive &&
725 !(lnet_isrouter(lp) && lp->lp_alive_count == 0))
726 lnet_notify_locked(lp, 0, 1, lp->lp_last_alive);
732 /* NB: returns 1 when alive, 0 when dead, negative when error;
733 * may drop the lnet_net_lock */
735 lnet_peer_alive_locked (lnet_peer_t *lp)
737 cfs_time_t now = cfs_time_current();
739 if (!lnet_peer_aliveness_enabled(lp))
742 if (lnet_peer_is_alive(lp, now))
745 /* Peer appears dead, but we should avoid frequent NI queries (at
746 * most once per lnet_queryinterval seconds). */
747 if (lp->lp_last_query != 0) {
748 static const int lnet_queryinterval = 1;
750 cfs_time_t next_query =
751 cfs_time_add(lp->lp_last_query,
752 cfs_time_seconds(lnet_queryinterval));
754 if (cfs_time_before(now, next_query)) {
756 CWARN("Unexpected aliveness of peer %s: "
758 libcfs_nid2str(lp->lp_nid),
759 (int)now, (int)next_query,
761 lp->lp_ni->ni_peertimeout);
766 /* query NI for latest aliveness news */
767 lnet_ni_query_locked(lp->lp_ni, lp);
769 if (lnet_peer_is_alive(lp, now))
772 lnet_notify_locked(lp, 0, 0, lp->lp_last_alive);
777 * \param msg The message to be sent.
778 * \param do_send True if lnet_ni_send() should be called in this function.
779 * lnet_send() is going to lnet_net_unlock immediately after this, so
780 * it sets do_send FALSE and I don't do the unlock/send/lock bit.
782 * \retval LNET_CREDIT_OK If \a msg sent or OK to send.
783 * \retval LNET_CREDIT_WAIT If \a msg blocked for credit.
784 * \retval -EHOSTUNREACH If the next hop of the message appears dead.
785 * \retval -ECANCELED If the MD of the message has been unlinked.
788 lnet_post_send_locked(lnet_msg_t *msg, int do_send)
790 lnet_peer_t *lp = msg->msg_txpeer;
791 lnet_ni_t *ni = lp->lp_ni;
792 int cpt = msg->msg_tx_cpt;
793 struct lnet_tx_queue *tq = ni->ni_tx_queues[cpt];
795 /* non-lnet_send() callers have checked before */
796 LASSERT(!do_send || msg->msg_tx_delayed);
797 LASSERT(!msg->msg_receiving);
798 LASSERT(msg->msg_tx_committed);
800 /* NB 'lp' is always the next hop */
801 if ((msg->msg_target.pid & LNET_PID_USERFLAG) == 0 &&
802 lnet_peer_alive_locked(lp) == 0) {
803 the_lnet.ln_counters[cpt]->drop_count++;
804 the_lnet.ln_counters[cpt]->drop_length += msg->msg_len;
805 lnet_net_unlock(cpt);
807 CNETERR("Dropping message for %s: peer not alive\n",
808 libcfs_id2str(msg->msg_target));
810 lnet_finalize(ni, msg, -EHOSTUNREACH);
813 return -EHOSTUNREACH;
816 if (msg->msg_md != NULL &&
817 (msg->msg_md->md_flags & LNET_MD_FLAG_ABORTED) != 0) {
818 lnet_net_unlock(cpt);
820 CNETERR("Aborting message for %s: LNetM[DE]Unlink() already "
821 "called on the MD/ME.\n",
822 libcfs_id2str(msg->msg_target));
824 lnet_finalize(ni, msg, -ECANCELED);
830 if (!msg->msg_peertxcredit) {
831 LASSERT((lp->lp_txcredits < 0) ==
832 !list_empty(&lp->lp_txq));
834 msg->msg_peertxcredit = 1;
835 lp->lp_txqnob += msg->msg_len + sizeof(lnet_hdr_t);
838 if (lp->lp_txcredits < lp->lp_mintxcredits)
839 lp->lp_mintxcredits = lp->lp_txcredits;
841 if (lp->lp_txcredits < 0) {
842 msg->msg_tx_delayed = 1;
843 list_add_tail(&msg->msg_list, &lp->lp_txq);
844 return LNET_CREDIT_WAIT;
848 if (!msg->msg_txcredit) {
849 LASSERT((tq->tq_credits < 0) ==
850 !list_empty(&tq->tq_delayed));
852 msg->msg_txcredit = 1;
855 if (tq->tq_credits < tq->tq_credits_min)
856 tq->tq_credits_min = tq->tq_credits;
858 if (tq->tq_credits < 0) {
859 msg->msg_tx_delayed = 1;
860 list_add_tail(&msg->msg_list, &tq->tq_delayed);
861 return LNET_CREDIT_WAIT;
866 lnet_net_unlock(cpt);
867 lnet_ni_send(ni, msg);
870 return LNET_CREDIT_OK;
874 static lnet_rtrbufpool_t *
875 lnet_msg2bufpool(lnet_msg_t *msg)
877 lnet_rtrbufpool_t *rbp;
880 LASSERT(msg->msg_rx_committed);
882 cpt = msg->msg_rx_cpt;
883 rbp = &the_lnet.ln_rtrpools[cpt][0];
885 LASSERT(msg->msg_len <= LNET_MTU);
886 while (msg->msg_len > (unsigned int)rbp->rbp_npages * PAGE_CACHE_SIZE) {
888 LASSERT(rbp < &the_lnet.ln_rtrpools[cpt][LNET_NRBPOOLS]);
895 lnet_post_routed_recv_locked (lnet_msg_t *msg, int do_recv)
897 /* lnet_parse is going to lnet_net_unlock immediately after this, so it
898 * sets do_recv FALSE and I don't do the unlock/send/lock bit.
899 * I return LNET_CREDIT_WAIT if msg blocked and LNET_CREDIT_OK if
900 * received or OK to receive */
901 lnet_peer_t *lp = msg->msg_rxpeer;
902 lnet_rtrbufpool_t *rbp;
905 LASSERT (msg->msg_iov == NULL);
906 LASSERT (msg->msg_kiov == NULL);
907 LASSERT (msg->msg_niov == 0);
908 LASSERT (msg->msg_routing);
909 LASSERT (msg->msg_receiving);
910 LASSERT (!msg->msg_sending);
912 /* non-lnet_parse callers only receive delayed messages */
913 LASSERT(!do_recv || msg->msg_rx_delayed);
915 if (!msg->msg_peerrtrcredit) {
916 LASSERT((lp->lp_rtrcredits < 0) ==
917 !list_empty(&lp->lp_rtrq));
919 msg->msg_peerrtrcredit = 1;
921 if (lp->lp_rtrcredits < lp->lp_minrtrcredits)
922 lp->lp_minrtrcredits = lp->lp_rtrcredits;
924 if (lp->lp_rtrcredits < 0) {
925 /* must have checked eager_recv before here */
926 LASSERT(msg->msg_rx_ready_delay);
927 msg->msg_rx_delayed = 1;
928 list_add_tail(&msg->msg_list, &lp->lp_rtrq);
929 return LNET_CREDIT_WAIT;
933 rbp = lnet_msg2bufpool(msg);
935 if (!msg->msg_rtrcredit) {
936 msg->msg_rtrcredit = 1;
938 if (rbp->rbp_credits < rbp->rbp_mincredits)
939 rbp->rbp_mincredits = rbp->rbp_credits;
941 if (rbp->rbp_credits < 0) {
942 /* must have checked eager_recv before here */
943 LASSERT(msg->msg_rx_ready_delay);
944 msg->msg_rx_delayed = 1;
945 list_add_tail(&msg->msg_list, &rbp->rbp_msgs);
946 return LNET_CREDIT_WAIT;
950 LASSERT(!list_empty(&rbp->rbp_bufs));
951 rb = list_entry(rbp->rbp_bufs.next, lnet_rtrbuf_t, rb_list);
952 list_del(&rb->rb_list);
954 msg->msg_niov = rbp->rbp_npages;
955 msg->msg_kiov = &rb->rb_kiov[0];
958 int cpt = msg->msg_rx_cpt;
960 lnet_net_unlock(cpt);
961 lnet_ni_recv(lp->lp_ni, msg->msg_private, msg, 1,
962 0, msg->msg_len, msg->msg_len);
965 return LNET_CREDIT_OK;
969 lnet_return_tx_credits_locked(lnet_msg_t *msg)
971 lnet_peer_t *txpeer = msg->msg_txpeer;
974 if (msg->msg_txcredit) {
975 struct lnet_ni *ni = txpeer->lp_ni;
976 struct lnet_tx_queue *tq = ni->ni_tx_queues[msg->msg_tx_cpt];
978 /* give back NI txcredits */
979 msg->msg_txcredit = 0;
981 LASSERT((tq->tq_credits < 0) ==
982 !list_empty(&tq->tq_delayed));
985 if (tq->tq_credits <= 0) {
986 msg2 = list_entry(tq->tq_delayed.next,
987 lnet_msg_t, msg_list);
988 list_del(&msg2->msg_list);
990 LASSERT(msg2->msg_txpeer->lp_ni == ni);
991 LASSERT(msg2->msg_tx_delayed);
993 (void) lnet_post_send_locked(msg2, 1);
997 if (msg->msg_peertxcredit) {
998 /* give back peer txcredits */
999 msg->msg_peertxcredit = 0;
1001 LASSERT((txpeer->lp_txcredits < 0) ==
1002 !list_empty(&txpeer->lp_txq));
1004 txpeer->lp_txqnob -= msg->msg_len + sizeof(lnet_hdr_t);
1005 LASSERT (txpeer->lp_txqnob >= 0);
1007 txpeer->lp_txcredits++;
1008 if (txpeer->lp_txcredits <= 0) {
1009 msg2 = list_entry(txpeer->lp_txq.next,
1010 lnet_msg_t, msg_list);
1011 list_del(&msg2->msg_list);
1013 LASSERT(msg2->msg_txpeer == txpeer);
1014 LASSERT(msg2->msg_tx_delayed);
1016 (void) lnet_post_send_locked(msg2, 1);
1020 if (txpeer != NULL) {
1021 msg->msg_txpeer = NULL;
1022 lnet_peer_decref_locked(txpeer);
1027 lnet_schedule_blocked_locked(lnet_rtrbufpool_t *rbp)
1031 if (list_empty(&rbp->rbp_msgs))
1033 msg = list_entry(rbp->rbp_msgs.next,
1034 lnet_msg_t, msg_list);
1035 list_del(&msg->msg_list);
1037 (void)lnet_post_routed_recv_locked(msg, 1);
1041 lnet_drop_routed_msgs_locked(struct list_head *list, int cpt)
1045 struct list_head drop;
1047 INIT_LIST_HEAD(&drop);
1049 list_splice_init(list, &drop);
1051 lnet_net_unlock(cpt);
1053 list_for_each_entry_safe(msg, tmp, &drop, msg_list) {
1054 lnet_ni_recv(msg->msg_rxpeer->lp_ni, msg->msg_private, NULL,
1055 0, 0, 0, msg->msg_hdr.payload_length);
1056 list_del_init(&msg->msg_list);
1057 lnet_finalize(NULL, msg, -ECANCELED);
1064 lnet_return_rx_credits_locked(lnet_msg_t *msg)
1066 lnet_peer_t *rxpeer = msg->msg_rxpeer;
1069 if (msg->msg_rtrcredit) {
1070 /* give back global router credits */
1072 lnet_rtrbufpool_t *rbp;
1074 /* NB If a msg ever blocks for a buffer in rbp_msgs, it stays
1075 * there until it gets one allocated, or aborts the wait
1077 LASSERT(msg->msg_kiov != NULL);
1079 rb = list_entry(msg->msg_kiov, lnet_rtrbuf_t, rb_kiov[0]);
1082 msg->msg_kiov = NULL;
1083 msg->msg_rtrcredit = 0;
1085 LASSERT(rbp == lnet_msg2bufpool(msg));
1087 LASSERT((rbp->rbp_credits > 0) ==
1088 !list_empty(&rbp->rbp_bufs));
1090 /* If routing is now turned off, we just drop this buffer and
1091 * don't bother trying to return credits. */
1092 if (!the_lnet.ln_routing) {
1093 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1097 /* It is possible that a user has lowered the desired number of
1098 * buffers in this pool. Make sure we never put back
1099 * more buffers than the stated number. */
1100 if (unlikely(rbp->rbp_credits >= rbp->rbp_req_nbuffers)) {
1101 /* Discard this buffer so we don't have too
1103 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1104 rbp->rbp_nbuffers--;
1106 list_add(&rb->rb_list, &rbp->rbp_bufs);
1108 if (rbp->rbp_credits <= 0)
1109 lnet_schedule_blocked_locked(rbp);
1114 if (msg->msg_peerrtrcredit) {
1115 /* give back peer router credits */
1116 msg->msg_peerrtrcredit = 0;
1118 LASSERT((rxpeer->lp_rtrcredits < 0) ==
1119 !list_empty(&rxpeer->lp_rtrq));
1121 rxpeer->lp_rtrcredits++;
1123 /* drop all messages which are queued to be routed on that
1125 if (!the_lnet.ln_routing) {
1126 lnet_drop_routed_msgs_locked(&rxpeer->lp_rtrq,
1128 } else if (rxpeer->lp_rtrcredits <= 0) {
1129 msg2 = list_entry(rxpeer->lp_rtrq.next,
1130 lnet_msg_t, msg_list);
1131 list_del(&msg2->msg_list);
1133 (void) lnet_post_routed_recv_locked(msg2, 1);
1136 if (rxpeer != NULL) {
1137 msg->msg_rxpeer = NULL;
1138 lnet_peer_decref_locked(rxpeer);
1143 lnet_compare_routes(lnet_route_t *r1, lnet_route_t *r2)
1145 lnet_peer_t *p1 = r1->lr_gateway;
1146 lnet_peer_t *p2 = r2->lr_gateway;
1147 int r1_hops = (r1->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r1->lr_hops;
1148 int r2_hops = (r2->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r2->lr_hops;
1150 if (r1->lr_priority < r2->lr_priority)
1153 if (r1->lr_priority > r2->lr_priority)
1156 if (r1_hops < r2_hops)
1159 if (r1_hops > r2_hops)
1162 if (p1->lp_txqnob < p2->lp_txqnob)
1165 if (p1->lp_txqnob > p2->lp_txqnob)
1168 if (p1->lp_txcredits > p2->lp_txcredits)
1171 if (p1->lp_txcredits < p2->lp_txcredits)
1174 if (r1->lr_seq - r2->lr_seq <= 0)
1180 static lnet_peer_t *
1181 lnet_find_route_locked(lnet_ni_t *ni, lnet_nid_t target, lnet_nid_t rtr_nid)
1183 lnet_remotenet_t *rnet;
1184 lnet_route_t *route;
1185 lnet_route_t *best_route;
1186 lnet_route_t *last_route;
1187 struct lnet_peer *lp_best;
1188 struct lnet_peer *lp;
1191 /* If @rtr_nid is not LNET_NID_ANY, return the gateway with
1192 * rtr_nid nid, otherwise find the best gateway I can use */
1194 rnet = lnet_find_net_locked(LNET_NIDNET(target));
1199 best_route = last_route = NULL;
1200 list_for_each_entry(route, &rnet->lrn_routes, lr_list) {
1201 lp = route->lr_gateway;
1203 if (!lnet_is_route_alive(route))
1206 if (ni != NULL && lp->lp_ni != ni)
1209 if (lp->lp_nid == rtr_nid) /* it's pre-determined router */
1212 if (lp_best == NULL) {
1213 best_route = last_route = route;
1218 /* no protection on below fields, but it's harmless */
1219 if (last_route->lr_seq - route->lr_seq < 0)
1222 rc = lnet_compare_routes(route, best_route);
1230 /* set sequence number on the best router to the latest sequence + 1
1231 * so we can round-robin all routers, it's race and inaccurate but
1232 * harmless and functional */
1233 if (best_route != NULL)
1234 best_route->lr_seq = last_route->lr_seq + 1;
1239 lnet_send(lnet_nid_t src_nid, lnet_msg_t *msg, lnet_nid_t rtr_nid)
1241 lnet_nid_t dst_nid = msg->msg_target.nid;
1242 struct lnet_ni *src_ni;
1243 struct lnet_ni *local_ni;
1244 struct lnet_peer *lp;
1249 /* NB: rtr_nid is set to LNET_NID_ANY for all current use-cases,
1250 * but we might want to use pre-determined router for ACK/REPLY
1252 /* NB: ni != NULL == interface pre-determined (ACK/REPLY) */
1253 LASSERT (msg->msg_txpeer == NULL);
1254 LASSERT (!msg->msg_sending);
1255 LASSERT (!msg->msg_target_is_router);
1256 LASSERT (!msg->msg_receiving);
1258 msg->msg_sending = 1;
1260 LASSERT(!msg->msg_tx_committed);
1261 cpt = lnet_cpt_of_nid(rtr_nid == LNET_NID_ANY ? dst_nid : rtr_nid);
1265 if (the_lnet.ln_shutdown) {
1266 lnet_net_unlock(cpt);
1270 if (src_nid == LNET_NID_ANY) {
1273 src_ni = lnet_nid2ni_locked(src_nid, cpt);
1274 if (src_ni == NULL) {
1275 lnet_net_unlock(cpt);
1276 LCONSOLE_WARN("Can't send to %s: src %s is not a "
1277 "local nid\n", libcfs_nid2str(dst_nid),
1278 libcfs_nid2str(src_nid));
1281 LASSERT (!msg->msg_routing);
1284 /* Is this for someone on a local network? */
1285 local_ni = lnet_net2ni_locked(LNET_NIDNET(dst_nid), cpt);
1287 if (local_ni != NULL) {
1288 if (src_ni == NULL) {
1290 src_nid = src_ni->ni_nid;
1291 } else if (src_ni == local_ni) {
1292 lnet_ni_decref_locked(local_ni, cpt);
1294 lnet_ni_decref_locked(local_ni, cpt);
1295 lnet_ni_decref_locked(src_ni, cpt);
1296 lnet_net_unlock(cpt);
1297 LCONSOLE_WARN("No route to %s via from %s\n",
1298 libcfs_nid2str(dst_nid),
1299 libcfs_nid2str(src_nid));
1303 LASSERT(src_nid != LNET_NID_ANY);
1304 lnet_msg_commit(msg, cpt);
1306 if (!msg->msg_routing)
1307 msg->msg_hdr.src_nid = cpu_to_le64(src_nid);
1309 if (src_ni == the_lnet.ln_loni) {
1310 /* No send credit hassles with LOLND */
1311 lnet_net_unlock(cpt);
1312 lnet_ni_send(src_ni, msg);
1315 lnet_ni_decref_locked(src_ni, cpt);
1316 lnet_net_unlock(cpt);
1320 rc = lnet_nid2peer_locked(&lp, dst_nid, cpt);
1321 /* lp has ref on src_ni; lose mine */
1322 lnet_ni_decref_locked(src_ni, cpt);
1324 lnet_net_unlock(cpt);
1325 LCONSOLE_WARN("Error %d finding peer %s\n", rc,
1326 libcfs_nid2str(dst_nid));
1327 /* ENOMEM or shutting down */
1330 LASSERT (lp->lp_ni == src_ni);
1332 /* sending to a remote network */
1333 lp = lnet_find_route_locked(src_ni, dst_nid, rtr_nid);
1336 lnet_ni_decref_locked(src_ni, cpt);
1337 lnet_net_unlock(cpt);
1339 LCONSOLE_WARN("No route to %s via %s "
1340 "(all routers down)\n",
1341 libcfs_id2str(msg->msg_target),
1342 libcfs_nid2str(src_nid));
1343 return -EHOSTUNREACH;
1346 /* rtr_nid is LNET_NID_ANY or NID of pre-determined router,
1347 * it's possible that rtr_nid isn't LNET_NID_ANY and lp isn't
1348 * pre-determined router, this can happen if router table
1349 * was changed when we release the lock */
1350 if (rtr_nid != lp->lp_nid) {
1351 cpt2 = lnet_cpt_of_nid_locked(lp->lp_nid);
1354 lnet_ni_decref_locked(src_ni, cpt);
1355 lnet_net_unlock(cpt);
1357 rtr_nid = lp->lp_nid;
1363 CDEBUG(D_NET, "Best route to %s via %s for %s %d\n",
1364 libcfs_nid2str(dst_nid), libcfs_nid2str(lp->lp_nid),
1365 lnet_msgtyp2str(msg->msg_type), msg->msg_len);
1367 if (src_ni == NULL) {
1369 src_nid = src_ni->ni_nid;
1371 LASSERT (src_ni == lp->lp_ni);
1372 lnet_ni_decref_locked(src_ni, cpt);
1375 lnet_peer_addref_locked(lp);
1377 LASSERT(src_nid != LNET_NID_ANY);
1378 lnet_msg_commit(msg, cpt);
1380 if (!msg->msg_routing) {
1381 /* I'm the source and now I know which NI to send on */
1382 msg->msg_hdr.src_nid = cpu_to_le64(src_nid);
1385 msg->msg_target_is_router = 1;
1386 msg->msg_target.nid = lp->lp_nid;
1387 msg->msg_target.pid = LNET_PID_LUSTRE;
1390 /* 'lp' is our best choice of peer */
1392 LASSERT (!msg->msg_peertxcredit);
1393 LASSERT (!msg->msg_txcredit);
1394 LASSERT (msg->msg_txpeer == NULL);
1396 msg->msg_txpeer = lp; /* msg takes my ref on lp */
1398 rc = lnet_post_send_locked(msg, 0);
1399 lnet_net_unlock(cpt);
1404 if (rc == LNET_CREDIT_OK)
1405 lnet_ni_send(src_ni, msg);
1407 return 0; /* rc == LNET_CREDIT_OK or LNET_CREDIT_WAIT */
1411 lnet_drop_message(lnet_ni_t *ni, int cpt, void *private, unsigned int nob)
1414 the_lnet.ln_counters[cpt]->drop_count++;
1415 the_lnet.ln_counters[cpt]->drop_length += nob;
1416 lnet_net_unlock(cpt);
1418 lnet_ni_recv(ni, private, NULL, 0, 0, 0, nob);
1422 lnet_recv_put(lnet_ni_t *ni, lnet_msg_t *msg)
1424 lnet_hdr_t *hdr = &msg->msg_hdr;
1426 if (msg->msg_wanted != 0)
1427 lnet_setpayloadbuffer(msg);
1429 lnet_build_msg_event(msg, LNET_EVENT_PUT);
1431 /* Must I ACK? If so I'll grab the ack_wmd out of the header and put
1432 * it back into the ACK during lnet_finalize() */
1433 msg->msg_ack = (!lnet_is_wire_handle_none(&hdr->msg.put.ack_wmd) &&
1434 (msg->msg_md->md_options & LNET_MD_ACK_DISABLE) == 0);
1436 lnet_ni_recv(ni, msg->msg_private, msg, msg->msg_rx_delayed,
1437 msg->msg_offset, msg->msg_wanted, hdr->payload_length);
1441 lnet_parse_put(lnet_ni_t *ni, lnet_msg_t *msg)
1443 lnet_hdr_t *hdr = &msg->msg_hdr;
1444 struct lnet_match_info info;
1448 /* Convert put fields to host byte order */
1449 hdr->msg.put.match_bits = le64_to_cpu(hdr->msg.put.match_bits);
1450 hdr->msg.put.ptl_index = le32_to_cpu(hdr->msg.put.ptl_index);
1451 hdr->msg.put.offset = le32_to_cpu(hdr->msg.put.offset);
1453 info.mi_id.nid = hdr->src_nid;
1454 info.mi_id.pid = hdr->src_pid;
1455 info.mi_opc = LNET_MD_OP_PUT;
1456 info.mi_portal = hdr->msg.put.ptl_index;
1457 info.mi_rlength = hdr->payload_length;
1458 info.mi_roffset = hdr->msg.put.offset;
1459 info.mi_mbits = hdr->msg.put.match_bits;
1461 msg->msg_rx_ready_delay = ni->ni_lnd->lnd_eager_recv == NULL;
1462 ready_delay = msg->msg_rx_ready_delay;
1465 rc = lnet_ptl_match_md(&info, msg);
1470 case LNET_MATCHMD_OK:
1471 lnet_recv_put(ni, msg);
1474 case LNET_MATCHMD_NONE:
1476 /* no eager_recv or has already called it, should
1477 * have been attached on delayed list */
1480 rc = lnet_ni_eager_recv(ni, msg);
1487 case LNET_MATCHMD_DROP:
1488 CNETERR("Dropping PUT from %s portal %d match %llu"
1489 " offset %d length %d: %d\n",
1490 libcfs_id2str(info.mi_id), info.mi_portal,
1491 info.mi_mbits, info.mi_roffset, info.mi_rlength, rc);
1493 return -ENOENT; /* -ve: OK but no match */
1498 lnet_parse_get(lnet_ni_t *ni, lnet_msg_t *msg, int rdma_get)
1500 struct lnet_match_info info;
1501 lnet_hdr_t *hdr = &msg->msg_hdr;
1502 lnet_handle_wire_t reply_wmd;
1505 /* Convert get fields to host byte order */
1506 hdr->msg.get.match_bits = le64_to_cpu(hdr->msg.get.match_bits);
1507 hdr->msg.get.ptl_index = le32_to_cpu(hdr->msg.get.ptl_index);
1508 hdr->msg.get.sink_length = le32_to_cpu(hdr->msg.get.sink_length);
1509 hdr->msg.get.src_offset = le32_to_cpu(hdr->msg.get.src_offset);
1511 info.mi_id.nid = hdr->src_nid;
1512 info.mi_id.pid = hdr->src_pid;
1513 info.mi_opc = LNET_MD_OP_GET;
1514 info.mi_portal = hdr->msg.get.ptl_index;
1515 info.mi_rlength = hdr->msg.get.sink_length;
1516 info.mi_roffset = hdr->msg.get.src_offset;
1517 info.mi_mbits = hdr->msg.get.match_bits;
1519 rc = lnet_ptl_match_md(&info, msg);
1520 if (rc == LNET_MATCHMD_DROP) {
1521 CNETERR("Dropping GET from %s portal %d match %llu"
1522 " offset %d length %d\n",
1523 libcfs_id2str(info.mi_id), info.mi_portal,
1524 info.mi_mbits, info.mi_roffset, info.mi_rlength);
1525 return -ENOENT; /* -ve: OK but no match */
1528 LASSERT(rc == LNET_MATCHMD_OK);
1530 lnet_build_msg_event(msg, LNET_EVENT_GET);
1532 reply_wmd = hdr->msg.get.return_wmd;
1534 lnet_prep_send(msg, LNET_MSG_REPLY, info.mi_id,
1535 msg->msg_offset, msg->msg_wanted);
1537 msg->msg_hdr.msg.reply.dst_wmd = reply_wmd;
1540 /* The LND completes the REPLY from her recv procedure */
1541 lnet_ni_recv(ni, msg->msg_private, msg, 0,
1542 msg->msg_offset, msg->msg_len, msg->msg_len);
1546 lnet_ni_recv(ni, msg->msg_private, NULL, 0, 0, 0, 0);
1547 msg->msg_receiving = 0;
1549 rc = lnet_send(ni->ni_nid, msg, LNET_NID_ANY);
1551 /* didn't get as far as lnet_ni_send() */
1552 CERROR("%s: Unable to send REPLY for GET from %s: %d\n",
1553 libcfs_nid2str(ni->ni_nid),
1554 libcfs_id2str(info.mi_id), rc);
1556 lnet_finalize(ni, msg, rc);
1563 lnet_parse_reply(lnet_ni_t *ni, lnet_msg_t *msg)
1565 void *private = msg->msg_private;
1566 lnet_hdr_t *hdr = &msg->msg_hdr;
1567 lnet_process_id_t src = {0};
1573 cpt = lnet_cpt_of_cookie(hdr->msg.reply.dst_wmd.wh_object_cookie);
1576 src.nid = hdr->src_nid;
1577 src.pid = hdr->src_pid;
1579 /* NB handles only looked up by creator (no flips) */
1580 md = lnet_wire_handle2md(&hdr->msg.reply.dst_wmd);
1581 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
1582 CNETERR("%s: Dropping REPLY from %s for %s "
1584 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1585 (md == NULL) ? "invalid" : "inactive",
1586 hdr->msg.reply.dst_wmd.wh_interface_cookie,
1587 hdr->msg.reply.dst_wmd.wh_object_cookie);
1588 if (md != NULL && md->md_me != NULL)
1589 CERROR("REPLY MD also attached to portal %d\n",
1590 md->md_me->me_portal);
1592 lnet_res_unlock(cpt);
1593 return -ENOENT; /* -ve: OK but no match */
1596 LASSERT (md->md_offset == 0);
1598 rlength = hdr->payload_length;
1599 mlength = MIN(rlength, (int)md->md_length);
1601 if (mlength < rlength &&
1602 (md->md_options & LNET_MD_TRUNCATE) == 0) {
1603 CNETERR("%s: Dropping REPLY from %s length %d "
1604 "for MD %#llx would overflow (%d)\n",
1605 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1606 rlength, hdr->msg.reply.dst_wmd.wh_object_cookie,
1608 lnet_res_unlock(cpt);
1609 return -ENOENT; /* -ve: OK but no match */
1612 CDEBUG(D_NET, "%s: Reply from %s of length %d/%d into md %#llx\n",
1613 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1614 mlength, rlength, hdr->msg.reply.dst_wmd.wh_object_cookie);
1616 lnet_msg_attach_md(msg, md, 0, mlength);
1619 lnet_setpayloadbuffer(msg);
1621 lnet_res_unlock(cpt);
1623 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
1625 lnet_ni_recv(ni, private, msg, 0, 0, mlength, rlength);
1630 lnet_parse_ack(lnet_ni_t *ni, lnet_msg_t *msg)
1632 lnet_hdr_t *hdr = &msg->msg_hdr;
1633 lnet_process_id_t src = {0};
1637 src.nid = hdr->src_nid;
1638 src.pid = hdr->src_pid;
1640 /* Convert ack fields to host byte order */
1641 hdr->msg.ack.match_bits = le64_to_cpu(hdr->msg.ack.match_bits);
1642 hdr->msg.ack.mlength = le32_to_cpu(hdr->msg.ack.mlength);
1644 cpt = lnet_cpt_of_cookie(hdr->msg.ack.dst_wmd.wh_object_cookie);
1647 /* NB handles only looked up by creator (no flips) */
1648 md = lnet_wire_handle2md(&hdr->msg.ack.dst_wmd);
1649 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
1650 /* Don't moan; this is expected */
1652 "%s: Dropping ACK from %s to %s MD %#llx.%#llx\n",
1653 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1654 (md == NULL) ? "invalid" : "inactive",
1655 hdr->msg.ack.dst_wmd.wh_interface_cookie,
1656 hdr->msg.ack.dst_wmd.wh_object_cookie);
1657 if (md != NULL && md->md_me != NULL)
1658 CERROR("Source MD also attached to portal %d\n",
1659 md->md_me->me_portal);
1661 lnet_res_unlock(cpt);
1662 return -ENOENT; /* -ve! */
1665 CDEBUG(D_NET, "%s: ACK from %s into md %#llx\n",
1666 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1667 hdr->msg.ack.dst_wmd.wh_object_cookie);
1669 lnet_msg_attach_md(msg, md, 0, 0);
1671 lnet_res_unlock(cpt);
1673 lnet_build_msg_event(msg, LNET_EVENT_ACK);
1675 lnet_ni_recv(ni, msg->msg_private, msg, 0, 0, 0, msg->msg_len);
1680 * \retval LNET_CREDIT_OK If \a msg is forwarded
1681 * \retval LNET_CREDIT_WAIT If \a msg is blocked because w/o buffer
1682 * \retval -ve error code
1685 lnet_parse_forward_locked(lnet_ni_t *ni, lnet_msg_t *msg)
1689 if (!the_lnet.ln_routing)
1692 if (msg->msg_rxpeer->lp_rtrcredits <= 0 ||
1693 lnet_msg2bufpool(msg)->rbp_credits <= 0) {
1694 if (ni->ni_lnd->lnd_eager_recv == NULL) {
1695 msg->msg_rx_ready_delay = 1;
1697 lnet_net_unlock(msg->msg_rx_cpt);
1698 rc = lnet_ni_eager_recv(ni, msg);
1699 lnet_net_lock(msg->msg_rx_cpt);
1704 rc = lnet_post_routed_recv_locked(msg, 0);
1709 lnet_parse_local(lnet_ni_t *ni, lnet_msg_t *msg)
1713 switch (msg->msg_type) {
1715 rc = lnet_parse_ack(ni, msg);
1718 rc = lnet_parse_put(ni, msg);
1721 rc = lnet_parse_get(ni, msg, msg->msg_rdma_get);
1723 case LNET_MSG_REPLY:
1724 rc = lnet_parse_reply(ni, msg);
1726 default: /* prevent an unused label if !kernel */
1731 LASSERT(rc == 0 || rc == -ENOENT);
1736 lnet_msgtyp2str (int type)
1745 case LNET_MSG_REPLY:
1747 case LNET_MSG_HELLO:
1750 return ("<UNKNOWN>");
1755 lnet_print_hdr(lnet_hdr_t * hdr)
1757 lnet_process_id_t src = {0};
1758 lnet_process_id_t dst = {0};
1759 char *type_str = lnet_msgtyp2str (hdr->type);
1761 src.nid = hdr->src_nid;
1762 src.pid = hdr->src_pid;
1764 dst.nid = hdr->dest_nid;
1765 dst.pid = hdr->dest_pid;
1767 CWARN("P3 Header at %p of type %s\n", hdr, type_str);
1768 CWARN(" From %s\n", libcfs_id2str(src));
1769 CWARN(" To %s\n", libcfs_id2str(dst));
1771 switch (hdr->type) {
1776 CWARN(" Ptl index %d, ack md %#llx.%#llx, "
1777 "match bits %llu\n",
1778 hdr->msg.put.ptl_index,
1779 hdr->msg.put.ack_wmd.wh_interface_cookie,
1780 hdr->msg.put.ack_wmd.wh_object_cookie,
1781 hdr->msg.put.match_bits);
1782 CWARN(" Length %d, offset %d, hdr data %#llx\n",
1783 hdr->payload_length, hdr->msg.put.offset,
1784 hdr->msg.put.hdr_data);
1788 CWARN(" Ptl index %d, return md %#llx.%#llx, "
1789 "match bits %llu\n", hdr->msg.get.ptl_index,
1790 hdr->msg.get.return_wmd.wh_interface_cookie,
1791 hdr->msg.get.return_wmd.wh_object_cookie,
1792 hdr->msg.get.match_bits);
1793 CWARN(" Length %d, src offset %d\n",
1794 hdr->msg.get.sink_length,
1795 hdr->msg.get.src_offset);
1799 CWARN(" dst md %#llx.%#llx, "
1800 "manipulated length %d\n",
1801 hdr->msg.ack.dst_wmd.wh_interface_cookie,
1802 hdr->msg.ack.dst_wmd.wh_object_cookie,
1803 hdr->msg.ack.mlength);
1806 case LNET_MSG_REPLY:
1807 CWARN(" dst md %#llx.%#llx, "
1809 hdr->msg.reply.dst_wmd.wh_interface_cookie,
1810 hdr->msg.reply.dst_wmd.wh_object_cookie,
1811 hdr->payload_length);
1817 lnet_parse(lnet_ni_t *ni, lnet_hdr_t *hdr, lnet_nid_t from_nid,
1818 void *private, int rdma_req)
1823 struct lnet_msg *msg;
1824 lnet_pid_t dest_pid;
1825 lnet_nid_t dest_nid;
1827 __u32 payload_length;
1830 LASSERT (!in_interrupt ());
1832 type = le32_to_cpu(hdr->type);
1833 src_nid = le64_to_cpu(hdr->src_nid);
1834 dest_nid = le64_to_cpu(hdr->dest_nid);
1835 dest_pid = le32_to_cpu(hdr->dest_pid);
1836 payload_length = le32_to_cpu(hdr->payload_length);
1838 for_me = (ni->ni_nid == dest_nid);
1839 cpt = lnet_cpt_of_nid(from_nid);
1844 if (payload_length > 0) {
1845 CERROR("%s, src %s: bad %s payload %d (0 expected)\n",
1846 libcfs_nid2str(from_nid),
1847 libcfs_nid2str(src_nid),
1848 lnet_msgtyp2str(type), payload_length);
1854 case LNET_MSG_REPLY:
1855 if (payload_length >
1856 (__u32)(for_me ? LNET_MAX_PAYLOAD : LNET_MTU)) {
1857 CERROR("%s, src %s: bad %s payload %d "
1858 "(%d max expected)\n",
1859 libcfs_nid2str(from_nid),
1860 libcfs_nid2str(src_nid),
1861 lnet_msgtyp2str(type),
1863 for_me ? LNET_MAX_PAYLOAD : LNET_MTU);
1869 CERROR("%s, src %s: Bad message type 0x%x\n",
1870 libcfs_nid2str(from_nid),
1871 libcfs_nid2str(src_nid), type);
1875 if (the_lnet.ln_routing &&
1876 ni->ni_last_alive != cfs_time_current_sec()) {
1877 /* NB: so far here is the only place to set NI status to "up */
1879 ni->ni_last_alive = cfs_time_current_sec();
1880 if (ni->ni_status != NULL &&
1881 ni->ni_status->ns_status == LNET_NI_STATUS_DOWN)
1882 ni->ni_status->ns_status = LNET_NI_STATUS_UP;
1886 /* Regard a bad destination NID as a protocol error. Senders should
1887 * know what they're doing; if they don't they're misconfigured, buggy
1888 * or malicious so we chop them off at the knees :) */
1891 if (LNET_NIDNET(dest_nid) == LNET_NIDNET(ni->ni_nid)) {
1892 /* should have gone direct */
1893 CERROR("%s, src %s: Bad dest nid %s "
1894 "(should have been sent direct)\n",
1895 libcfs_nid2str(from_nid),
1896 libcfs_nid2str(src_nid),
1897 libcfs_nid2str(dest_nid));
1901 if (lnet_islocalnid(dest_nid)) {
1902 /* dest is another local NI; sender should have used
1903 * this node's NID on its own network */
1904 CERROR("%s, src %s: Bad dest nid %s "
1905 "(it's my nid but on a different network)\n",
1906 libcfs_nid2str(from_nid),
1907 libcfs_nid2str(src_nid),
1908 libcfs_nid2str(dest_nid));
1912 if (rdma_req && type == LNET_MSG_GET) {
1913 CERROR("%s, src %s: Bad optimized GET for %s "
1914 "(final destination must be me)\n",
1915 libcfs_nid2str(from_nid),
1916 libcfs_nid2str(src_nid),
1917 libcfs_nid2str(dest_nid));
1921 if (!the_lnet.ln_routing) {
1922 CERROR("%s, src %s: Dropping message for %s "
1923 "(routing not enabled)\n",
1924 libcfs_nid2str(from_nid),
1925 libcfs_nid2str(src_nid),
1926 libcfs_nid2str(dest_nid));
1931 /* Message looks OK; we're not going to return an error, so we MUST
1932 * call back lnd_recv() come what may... */
1934 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
1935 fail_peer(src_nid, 0)) { /* shall we now? */
1936 CERROR("%s, src %s: Dropping %s to simulate failure\n",
1937 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
1938 lnet_msgtyp2str(type));
1942 if (!list_empty(&the_lnet.ln_drop_rules) &&
1943 lnet_drop_rule_match(hdr)) {
1944 CDEBUG(D_NET, "%s, src %s, dst %s: Dropping %s to simulate"
1945 "silent message loss\n",
1946 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
1947 libcfs_nid2str(dest_nid), lnet_msgtyp2str(type));
1952 msg = lnet_msg_alloc();
1954 CERROR("%s, src %s: Dropping %s (out of memory)\n",
1955 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
1956 lnet_msgtyp2str(type));
1960 /* msg zeroed in lnet_msg_alloc; i.e. flags all clear,
1961 * pointers NULL etc */
1963 msg->msg_type = type;
1964 msg->msg_private = private;
1965 msg->msg_receiving = 1;
1966 msg->msg_rdma_get = rdma_req;
1967 msg->msg_len = msg->msg_wanted = payload_length;
1968 msg->msg_offset = 0;
1969 msg->msg_hdr = *hdr;
1970 /* for building message event */
1971 msg->msg_from = from_nid;
1973 msg->msg_target.pid = dest_pid;
1974 msg->msg_target.nid = dest_nid;
1975 msg->msg_routing = 1;
1978 /* convert common msg->hdr fields to host byteorder */
1979 msg->msg_hdr.type = type;
1980 msg->msg_hdr.src_nid = src_nid;
1981 msg->msg_hdr.src_pid = le32_to_cpu(msg->msg_hdr.src_pid);
1982 msg->msg_hdr.dest_nid = dest_nid;
1983 msg->msg_hdr.dest_pid = dest_pid;
1984 msg->msg_hdr.payload_length = payload_length;
1988 rc = lnet_nid2peer_locked(&msg->msg_rxpeer, from_nid, cpt);
1990 lnet_net_unlock(cpt);
1991 CERROR("%s, src %s: Dropping %s "
1992 "(error %d looking up sender)\n",
1993 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
1994 lnet_msgtyp2str(type), rc);
1996 if (rc == -ESHUTDOWN)
1997 /* We are shutting down. Don't do anything more */
2002 if (lnet_isrouter(msg->msg_rxpeer)) {
2003 lnet_peer_set_alive(msg->msg_rxpeer);
2004 if (avoid_asym_router_failure &&
2005 LNET_NIDNET(src_nid) != LNET_NIDNET(from_nid)) {
2006 /* received a remote message from router, update
2007 * remote NI status on this router.
2008 * NB: multi-hop routed message will be ignored.
2010 lnet_router_ni_update_locked(msg->msg_rxpeer,
2011 LNET_NIDNET(src_nid));
2015 lnet_msg_commit(msg, cpt);
2017 /* message delay simulation */
2018 if (unlikely(!list_empty(&the_lnet.ln_delay_rules) &&
2019 lnet_delay_rule_match_locked(hdr, msg))) {
2020 lnet_net_unlock(cpt);
2025 rc = lnet_parse_forward_locked(ni, msg);
2026 lnet_net_unlock(cpt);
2031 if (rc == LNET_CREDIT_OK) {
2032 lnet_ni_recv(ni, msg->msg_private, msg, 0,
2033 0, payload_length, payload_length);
2038 lnet_net_unlock(cpt);
2040 rc = lnet_parse_local(ni, msg);
2046 LASSERT(msg->msg_md == NULL);
2047 lnet_finalize(ni, msg, rc);
2050 lnet_drop_message(ni, cpt, private, payload_length);
2053 EXPORT_SYMBOL(lnet_parse);
2056 lnet_drop_delayed_msg_list(struct list_head *head, char *reason)
2058 while (!list_empty(head)) {
2059 lnet_process_id_t id = {0};
2062 msg = list_entry(head->next, lnet_msg_t, msg_list);
2063 list_del(&msg->msg_list);
2065 id.nid = msg->msg_hdr.src_nid;
2066 id.pid = msg->msg_hdr.src_pid;
2068 LASSERT(msg->msg_md == NULL);
2069 LASSERT(msg->msg_rx_delayed);
2070 LASSERT(msg->msg_rxpeer != NULL);
2071 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
2073 CWARN("Dropping delayed PUT from %s portal %d match %llu"
2074 " offset %d length %d: %s\n",
2076 msg->msg_hdr.msg.put.ptl_index,
2077 msg->msg_hdr.msg.put.match_bits,
2078 msg->msg_hdr.msg.put.offset,
2079 msg->msg_hdr.payload_length, reason);
2081 /* NB I can't drop msg's ref on msg_rxpeer until after I've
2082 * called lnet_drop_message(), so I just hang onto msg as well
2083 * until that's done */
2085 lnet_drop_message(msg->msg_rxpeer->lp_ni,
2086 msg->msg_rxpeer->lp_cpt,
2087 msg->msg_private, msg->msg_len);
2089 * NB: message will not generate event because w/o attached MD,
2090 * but we still should give error code so lnet_msg_decommit()
2091 * can skip counters operations and other checks.
2093 lnet_finalize(msg->msg_rxpeer->lp_ni, msg, -ENOENT);
2098 lnet_recv_delayed_msg_list(struct list_head *head)
2100 while (!list_empty(head)) {
2102 lnet_process_id_t id;
2104 msg = list_entry(head->next, lnet_msg_t, msg_list);
2105 list_del(&msg->msg_list);
2107 /* md won't disappear under me, since each msg
2108 * holds a ref on it */
2110 id.nid = msg->msg_hdr.src_nid;
2111 id.pid = msg->msg_hdr.src_pid;
2113 LASSERT(msg->msg_rx_delayed);
2114 LASSERT(msg->msg_md != NULL);
2115 LASSERT(msg->msg_rxpeer != NULL);
2116 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
2118 CDEBUG(D_NET, "Resuming delayed PUT from %s portal %d "
2119 "match %llu offset %d length %d.\n",
2120 libcfs_id2str(id), msg->msg_hdr.msg.put.ptl_index,
2121 msg->msg_hdr.msg.put.match_bits,
2122 msg->msg_hdr.msg.put.offset,
2123 msg->msg_hdr.payload_length);
2125 lnet_recv_put(msg->msg_rxpeer->lp_ni, msg);
2130 * Initiate an asynchronous PUT operation.
2132 * There are several events associated with a PUT: completion of the send on
2133 * the initiator node (LNET_EVENT_SEND), and when the send completes
2134 * successfully, the receipt of an acknowledgment (LNET_EVENT_ACK) indicating
2135 * that the operation was accepted by the target. The event LNET_EVENT_PUT is
2136 * used at the target node to indicate the completion of incoming data
2139 * The local events will be logged in the EQ associated with the MD pointed to
2140 * by \a mdh handle. Using a MD without an associated EQ results in these
2141 * events being discarded. In this case, the caller must have another
2142 * mechanism (e.g., a higher level protocol) for determining when it is safe
2143 * to modify the memory region associated with the MD.
2145 * Note that LNet does not guarantee the order of LNET_EVENT_SEND and
2146 * LNET_EVENT_ACK, though intuitively ACK should happen after SEND.
2148 * \param self Indicates the NID of a local interface through which to send
2149 * the PUT request. Use LNET_NID_ANY to let LNet choose one by itself.
2150 * \param mdh A handle for the MD that describes the memory to be sent. The MD
2151 * must be "free floating" (See LNetMDBind()).
2152 * \param ack Controls whether an acknowledgment is requested.
2153 * Acknowledgments are only sent when they are requested by the initiating
2154 * process and the target MD enables them.
2155 * \param target A process identifier for the target process.
2156 * \param portal The index in the \a target's portal table.
2157 * \param match_bits The match bits to use for MD selection at the target
2159 * \param offset The offset into the target MD (only used when the target
2160 * MD has the LNET_MD_MANAGE_REMOTE option set).
2161 * \param hdr_data 64 bits of user data that can be included in the message
2162 * header. This data is written to an event queue entry at the target if an
2163 * EQ is present on the matching MD.
2165 * \retval 0 Success, and only in this case events will be generated
2166 * and logged to EQ (if it exists).
2167 * \retval -EIO Simulated failure.
2168 * \retval -ENOMEM Memory allocation failure.
2169 * \retval -ENOENT Invalid MD object.
2171 * \see lnet_event_t::hdr_data and lnet_event_kind_t.
2174 LNetPut(lnet_nid_t self, lnet_handle_md_t mdh, lnet_ack_req_t ack,
2175 lnet_process_id_t target, unsigned int portal,
2176 __u64 match_bits, unsigned int offset,
2179 struct lnet_msg *msg;
2180 struct lnet_libmd *md;
2184 LASSERT(the_lnet.ln_refcount > 0);
2186 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
2187 fail_peer(target.nid, 1)) { /* shall we now? */
2188 CERROR("Dropping PUT to %s: simulated failure\n",
2189 libcfs_id2str(target));
2193 msg = lnet_msg_alloc();
2195 CERROR("Dropping PUT to %s: ENOMEM on lnet_msg_t\n",
2196 libcfs_id2str(target));
2199 msg->msg_vmflush = !!memory_pressure_get();
2201 cpt = lnet_cpt_of_cookie(mdh.cookie);
2204 md = lnet_handle2md(&mdh);
2205 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
2206 CERROR("Dropping PUT (%llu:%d:%s): MD (%d) invalid\n",
2207 match_bits, portal, libcfs_id2str(target),
2208 md == NULL ? -1 : md->md_threshold);
2209 if (md != NULL && md->md_me != NULL)
2210 CERROR("Source MD also attached to portal %d\n",
2211 md->md_me->me_portal);
2212 lnet_res_unlock(cpt);
2218 CDEBUG(D_NET, "LNetPut -> %s\n", libcfs_id2str(target));
2220 lnet_msg_attach_md(msg, md, 0, 0);
2222 lnet_prep_send(msg, LNET_MSG_PUT, target, 0, md->md_length);
2224 msg->msg_hdr.msg.put.match_bits = cpu_to_le64(match_bits);
2225 msg->msg_hdr.msg.put.ptl_index = cpu_to_le32(portal);
2226 msg->msg_hdr.msg.put.offset = cpu_to_le32(offset);
2227 msg->msg_hdr.msg.put.hdr_data = hdr_data;
2229 /* NB handles only looked up by creator (no flips) */
2230 if (ack == LNET_ACK_REQ) {
2231 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
2232 the_lnet.ln_interface_cookie;
2233 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
2234 md->md_lh.lh_cookie;
2236 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
2237 LNET_WIRE_HANDLE_COOKIE_NONE;
2238 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
2239 LNET_WIRE_HANDLE_COOKIE_NONE;
2242 lnet_res_unlock(cpt);
2244 lnet_build_msg_event(msg, LNET_EVENT_SEND);
2246 rc = lnet_send(self, msg, LNET_NID_ANY);
2248 CNETERR( "Error sending PUT to %s: %d\n",
2249 libcfs_id2str(target), rc);
2250 lnet_finalize (NULL, msg, rc);
2253 /* completion will be signalled by an event */
2256 EXPORT_SYMBOL(LNetPut);
2259 lnet_create_reply_msg (lnet_ni_t *ni, lnet_msg_t *getmsg)
2261 /* The LND can DMA direct to the GET md (i.e. no REPLY msg). This
2262 * returns a msg for the LND to pass to lnet_finalize() when the sink
2263 * data has been received.
2265 * CAVEAT EMPTOR: 'getmsg' is the original GET, which is freed when
2266 * lnet_finalize() is called on it, so the LND must call this first */
2268 struct lnet_msg *msg = lnet_msg_alloc();
2269 struct lnet_libmd *getmd = getmsg->msg_md;
2270 lnet_process_id_t peer_id = getmsg->msg_target;
2273 LASSERT(!getmsg->msg_target_is_router);
2274 LASSERT(!getmsg->msg_routing);
2277 CERROR("%s: Dropping REPLY from %s: can't allocate msg\n",
2278 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id));
2282 cpt = lnet_cpt_of_cookie(getmd->md_lh.lh_cookie);
2285 LASSERT(getmd->md_refcount > 0);
2287 if (getmd->md_threshold == 0) {
2288 CERROR ("%s: Dropping REPLY from %s for inactive MD %p\n",
2289 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id),
2291 lnet_res_unlock(cpt);
2295 LASSERT(getmd->md_offset == 0);
2297 CDEBUG(D_NET, "%s: Reply from %s md %p\n",
2298 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id), getmd);
2300 /* setup information for lnet_build_msg_event */
2301 msg->msg_from = peer_id.nid;
2302 msg->msg_type = LNET_MSG_GET; /* flag this msg as an "optimized" GET */
2303 msg->msg_hdr.src_nid = peer_id.nid;
2304 msg->msg_hdr.payload_length = getmd->md_length;
2305 msg->msg_receiving = 1; /* required by lnet_msg_attach_md */
2307 lnet_msg_attach_md(msg, getmd, getmd->md_offset, getmd->md_length);
2308 lnet_res_unlock(cpt);
2310 cpt = lnet_cpt_of_nid(peer_id.nid);
2313 lnet_msg_commit(msg, cpt);
2314 lnet_net_unlock(cpt);
2316 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
2321 cpt = lnet_cpt_of_nid(peer_id.nid);
2324 the_lnet.ln_counters[cpt]->drop_count++;
2325 the_lnet.ln_counters[cpt]->drop_length += getmd->md_length;
2326 lnet_net_unlock(cpt);
2333 EXPORT_SYMBOL(lnet_create_reply_msg);
2336 lnet_set_reply_msg_len(lnet_ni_t *ni, lnet_msg_t *reply, unsigned int len)
2338 /* Set the REPLY length, now the RDMA that elides the REPLY message has
2339 * completed and I know it. */
2340 LASSERT (reply != NULL);
2341 LASSERT (reply->msg_type == LNET_MSG_GET);
2342 LASSERT (reply->msg_ev.type == LNET_EVENT_REPLY);
2344 /* NB I trusted my peer to RDMA. If she tells me she's written beyond
2345 * the end of my buffer, I might as well be dead. */
2346 LASSERT (len <= reply->msg_ev.mlength);
2348 reply->msg_ev.mlength = len;
2350 EXPORT_SYMBOL(lnet_set_reply_msg_len);
2353 * Initiate an asynchronous GET operation.
2355 * On the initiator node, an LNET_EVENT_SEND is logged when the GET request
2356 * is sent, and an LNET_EVENT_REPLY is logged when the data returned from
2357 * the target node in the REPLY has been written to local MD.
2359 * On the target node, an LNET_EVENT_GET is logged when the GET request
2360 * arrives and is accepted into a MD.
2362 * \param self,target,portal,match_bits,offset See the discussion in LNetPut().
2363 * \param mdh A handle for the MD that describes the memory into which the
2364 * requested data will be received. The MD must be "free floating" (See LNetMDBind()).
2366 * \retval 0 Success, and only in this case events will be generated
2367 * and logged to EQ (if it exists) of the MD.
2368 * \retval -EIO Simulated failure.
2369 * \retval -ENOMEM Memory allocation failure.
2370 * \retval -ENOENT Invalid MD object.
2373 LNetGet(lnet_nid_t self, lnet_handle_md_t mdh,
2374 lnet_process_id_t target, unsigned int portal,
2375 __u64 match_bits, unsigned int offset)
2377 struct lnet_msg *msg;
2378 struct lnet_libmd *md;
2382 LASSERT (the_lnet.ln_refcount > 0);
2384 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
2385 fail_peer(target.nid, 1)) /* shall we now? */
2387 CERROR("Dropping GET to %s: simulated failure\n",
2388 libcfs_id2str(target));
2392 msg = lnet_msg_alloc();
2394 CERROR("Dropping GET to %s: ENOMEM on lnet_msg_t\n",
2395 libcfs_id2str(target));
2399 cpt = lnet_cpt_of_cookie(mdh.cookie);
2402 md = lnet_handle2md(&mdh);
2403 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
2404 CERROR("Dropping GET (%llu:%d:%s): MD (%d) invalid\n",
2405 match_bits, portal, libcfs_id2str(target),
2406 md == NULL ? -1 : md->md_threshold);
2407 if (md != NULL && md->md_me != NULL)
2408 CERROR("REPLY MD also attached to portal %d\n",
2409 md->md_me->me_portal);
2411 lnet_res_unlock(cpt);
2417 CDEBUG(D_NET, "LNetGet -> %s\n", libcfs_id2str(target));
2419 lnet_msg_attach_md(msg, md, 0, 0);
2421 lnet_prep_send(msg, LNET_MSG_GET, target, 0, 0);
2423 msg->msg_hdr.msg.get.match_bits = cpu_to_le64(match_bits);
2424 msg->msg_hdr.msg.get.ptl_index = cpu_to_le32(portal);
2425 msg->msg_hdr.msg.get.src_offset = cpu_to_le32(offset);
2426 msg->msg_hdr.msg.get.sink_length = cpu_to_le32(md->md_length);
2428 /* NB handles only looked up by creator (no flips) */
2429 msg->msg_hdr.msg.get.return_wmd.wh_interface_cookie =
2430 the_lnet.ln_interface_cookie;
2431 msg->msg_hdr.msg.get.return_wmd.wh_object_cookie =
2432 md->md_lh.lh_cookie;
2434 lnet_res_unlock(cpt);
2436 lnet_build_msg_event(msg, LNET_EVENT_SEND);
2438 rc = lnet_send(self, msg, LNET_NID_ANY);
2440 CNETERR("Error sending GET to %s: %d\n",
2441 libcfs_id2str(target), rc);
2442 lnet_finalize(NULL, msg, rc);
2445 /* completion will be signalled by an event */
2448 EXPORT_SYMBOL(LNetGet);
2451 * Calculate distance to node at \a dstnid.
2453 * \param dstnid Target NID.
2454 * \param srcnidp If not NULL, NID of the local interface to reach \a dstnid
2456 * \param orderp If not NULL, order of the route to reach \a dstnid is saved
2459 * \retval 0 If \a dstnid belongs to a local interface, and reserved option
2460 * local_nid_dist_zero is set, which is the default.
2461 * \retval positives Distance to target NID, i.e. number of hops plus one.
2462 * \retval -EHOSTUNREACH If \a dstnid is not reachable.
2465 LNetDist(lnet_nid_t dstnid, lnet_nid_t *srcnidp, __u32 *orderp)
2467 struct list_head *e;
2469 lnet_remotenet_t *rnet;
2470 __u32 dstnet = LNET_NIDNET(dstnid);
2474 struct list_head *rn_list;
2476 /* if !local_nid_dist_zero, I don't return a distance of 0 ever
2477 * (when lustre sees a distance of 0, it substitutes 0@lo), so I
2478 * keep order 0 free for 0@lo and order 1 free for a local NID
2481 LASSERT (the_lnet.ln_refcount > 0);
2483 cpt = lnet_net_lock_current();
2485 list_for_each(e, &the_lnet.ln_nis) {
2486 ni = list_entry(e, lnet_ni_t, ni_list);
2488 if (ni->ni_nid == dstnid) {
2489 if (srcnidp != NULL)
2491 if (orderp != NULL) {
2492 if (LNET_NETTYP(LNET_NIDNET(dstnid)) == LOLND)
2497 lnet_net_unlock(cpt);
2499 return local_nid_dist_zero ? 0 : 1;
2502 if (LNET_NIDNET(ni->ni_nid) == dstnet) {
2503 /* Check if ni was originally created in
2504 * current net namespace.
2505 * If not, assign order above 0xffff0000,
2506 * to make this ni not a priority. */
2507 if (!net_eq(ni->ni_net_ns, current->nsproxy->net_ns))
2508 order += 0xffff0000;
2510 if (srcnidp != NULL)
2511 *srcnidp = ni->ni_nid;
2514 lnet_net_unlock(cpt);
2521 rn_list = lnet_net2rnethash(dstnet);
2522 list_for_each(e, rn_list) {
2523 rnet = list_entry(e, lnet_remotenet_t, lrn_list);
2525 if (rnet->lrn_net == dstnet) {
2526 lnet_route_t *route;
2527 lnet_route_t *shortest = NULL;
2528 __u32 shortest_hops = LNET_UNDEFINED_HOPS;
2531 LASSERT(!list_empty(&rnet->lrn_routes));
2533 list_for_each_entry(route, &rnet->lrn_routes,
2535 route_hops = route->lr_hops;
2536 if (route_hops == LNET_UNDEFINED_HOPS)
2538 if (shortest == NULL ||
2539 route_hops < shortest_hops) {
2541 shortest_hops = route_hops;
2545 LASSERT(shortest != NULL);
2546 hops = shortest_hops;
2547 if (srcnidp != NULL)
2548 *srcnidp = shortest->lr_gateway->lp_ni->ni_nid;
2551 lnet_net_unlock(cpt);
2557 lnet_net_unlock(cpt);
2558 return -EHOSTUNREACH;
2560 EXPORT_SYMBOL(LNetDist);