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, 2013, 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 /** lnet message has credit and can be submitted to lnd for send/receive */
46 #define LNET_CREDIT_OK 0
47 /** lnet message is waiting for credit */
48 #define LNET_CREDIT_WAIT 1
50 static int local_nid_dist_zero = 1;
51 CFS_MODULE_PARM(local_nid_dist_zero, "i", int, 0444,
55 lnet_fail_nid(lnet_nid_t nid, unsigned int threshold)
59 struct list_head *next;
60 struct list_head cull;
62 LASSERT(the_lnet.ln_init);
64 /* NB: use lnet_net_lock(0) to serialize operations on test peers */
66 /* Adding a new entry */
67 LIBCFS_ALLOC(tp, sizeof(*tp));
72 tp->tp_threshold = threshold;
75 list_add_tail(&tp->tp_list, &the_lnet.ln_test_peers);
80 /* removing entries */
81 INIT_LIST_HEAD(&cull);
85 list_for_each_safe(el, next, &the_lnet.ln_test_peers) {
86 tp = list_entry(el, lnet_test_peer_t, tp_list);
88 if (tp->tp_threshold == 0 || /* needs culling anyway */
89 nid == LNET_NID_ANY || /* removing all entries */
90 tp->tp_nid == nid) { /* matched this one */
91 list_del(&tp->tp_list);
92 list_add(&tp->tp_list, &cull);
98 while (!list_empty(&cull)) {
99 tp = list_entry(cull.next, lnet_test_peer_t, tp_list);
101 list_del(&tp->tp_list);
102 LIBCFS_FREE(tp, sizeof(*tp));
108 fail_peer (lnet_nid_t nid, int outgoing)
110 lnet_test_peer_t *tp;
111 struct list_head *el;
112 struct list_head *next;
113 struct list_head cull;
116 INIT_LIST_HEAD(&cull);
118 /* NB: use lnet_net_lock(0) to serialize operations on test peers */
121 list_for_each_safe(el, next, &the_lnet.ln_test_peers) {
122 tp = list_entry(el, lnet_test_peer_t, tp_list);
124 if (tp->tp_threshold == 0) {
127 /* only cull zombies on outgoing tests,
128 * since we may be at interrupt priority on
129 * incoming messages. */
130 list_del(&tp->tp_list);
131 list_add(&tp->tp_list, &cull);
136 if (tp->tp_nid == LNET_NID_ANY || /* fail every peer */
137 nid == tp->tp_nid) { /* fail this peer */
140 if (tp->tp_threshold != LNET_MD_THRESH_INF) {
143 tp->tp_threshold == 0) {
145 list_del(&tp->tp_list);
146 list_add(&tp->tp_list, &cull);
155 while (!list_empty(&cull)) {
156 tp = list_entry(cull.next, lnet_test_peer_t, tp_list);
157 list_del(&tp->tp_list);
159 LIBCFS_FREE(tp, sizeof(*tp));
166 lnet_iov_nob (unsigned int niov, struct iovec *iov)
168 unsigned int nob = 0;
170 LASSERT(niov == 0 || iov != NULL);
172 nob += (iov++)->iov_len;
176 EXPORT_SYMBOL(lnet_iov_nob);
179 lnet_copy_iov2iov (unsigned int ndiov, struct iovec *diov, unsigned int doffset,
180 unsigned int nsiov, struct iovec *siov, unsigned int soffset,
183 /* NB diov, siov are READ-ONLY */
184 unsigned int this_nob;
189 /* skip complete frags before 'doffset' */
191 while (doffset >= diov->iov_len) {
192 doffset -= diov->iov_len;
198 /* skip complete frags before 'soffset' */
200 while (soffset >= siov->iov_len) {
201 soffset -= siov->iov_len;
210 this_nob = MIN(diov->iov_len - doffset,
211 siov->iov_len - soffset);
212 this_nob = MIN(this_nob, nob);
214 memcpy ((char *)diov->iov_base + doffset,
215 (char *)siov->iov_base + soffset, this_nob);
218 if (diov->iov_len > doffset + this_nob) {
226 if (siov->iov_len > soffset + this_nob) {
235 EXPORT_SYMBOL(lnet_copy_iov2iov);
238 lnet_extract_iov (int dst_niov, struct iovec *dst,
239 int src_niov, struct iovec *src,
240 unsigned int offset, unsigned int len)
242 /* Initialise 'dst' to the subset of 'src' starting at 'offset',
243 * for exactly 'len' bytes, and return the number of entries.
244 * NB not destructive to 'src' */
245 unsigned int frag_len;
248 if (len == 0) /* no data => */
249 return (0); /* no frags */
251 LASSERT (src_niov > 0);
252 while (offset >= src->iov_len) { /* skip initial frags */
253 offset -= src->iov_len;
256 LASSERT (src_niov > 0);
261 LASSERT (src_niov > 0);
262 LASSERT ((int)niov <= dst_niov);
264 frag_len = src->iov_len - offset;
265 dst->iov_base = ((char *)src->iov_base) + offset;
267 if (len <= frag_len) {
272 dst->iov_len = frag_len;
282 EXPORT_SYMBOL(lnet_extract_iov);
286 lnet_kiov_nob (unsigned int niov, lnet_kiov_t *kiov)
293 lnet_copy_kiov2kiov (unsigned int ndkiov, lnet_kiov_t *dkiov, unsigned int doffset,
294 unsigned int nskiov, lnet_kiov_t *skiov, unsigned int soffset,
301 lnet_copy_kiov2iov (unsigned int niov, struct iovec *iov, unsigned int iovoffset,
302 unsigned int nkiov, lnet_kiov_t *kiov, unsigned int kiovoffset,
309 lnet_copy_iov2kiov (unsigned int nkiov, lnet_kiov_t *kiov, unsigned int kiovoffset,
310 unsigned int niov, struct iovec *iov, unsigned int iovoffset,
317 lnet_extract_kiov (int dst_niov, lnet_kiov_t *dst,
318 int src_niov, lnet_kiov_t *src,
319 unsigned int offset, unsigned int len)
324 #else /* __KERNEL__ */
327 lnet_kiov_nob (unsigned int niov, lnet_kiov_t *kiov)
329 unsigned int nob = 0;
331 LASSERT(niov == 0 || kiov != NULL);
333 nob += (kiov++)->kiov_len;
337 EXPORT_SYMBOL(lnet_kiov_nob);
340 lnet_copy_kiov2kiov (unsigned int ndiov, lnet_kiov_t *diov, unsigned int doffset,
341 unsigned int nsiov, lnet_kiov_t *siov, unsigned int soffset,
344 /* NB diov, siov are READ-ONLY */
345 unsigned int this_nob;
352 LASSERT (!in_interrupt ());
355 while (doffset >= diov->kiov_len) {
356 doffset -= diov->kiov_len;
363 while (soffset >= siov->kiov_len) {
364 soffset -= siov->kiov_len;
373 this_nob = MIN(diov->kiov_len - doffset,
374 siov->kiov_len - soffset);
375 this_nob = MIN(this_nob, nob);
378 daddr = ((char *)kmap(diov->kiov_page)) +
379 diov->kiov_offset + doffset;
381 saddr = ((char *)kmap(siov->kiov_page)) +
382 siov->kiov_offset + soffset;
384 /* Vanishing risk of kmap deadlock when mapping 2 pages.
385 * However in practice at least one of the kiovs will be mapped
386 * kernel pages and the map/unmap will be NOOPs */
388 memcpy (daddr, saddr, this_nob);
391 if (diov->kiov_len > doffset + this_nob) {
395 kunmap(diov->kiov_page);
402 if (siov->kiov_len > soffset + this_nob) {
406 kunmap(siov->kiov_page);
415 kunmap(diov->kiov_page);
417 kunmap(siov->kiov_page);
419 EXPORT_SYMBOL(lnet_copy_kiov2kiov);
422 lnet_copy_kiov2iov (unsigned int niov, struct iovec *iov, unsigned int iovoffset,
423 unsigned int nkiov, lnet_kiov_t *kiov, unsigned int kiovoffset,
426 /* NB iov, kiov are READ-ONLY */
427 unsigned int this_nob;
433 LASSERT (!in_interrupt ());
436 while (iovoffset >= iov->iov_len) {
437 iovoffset -= iov->iov_len;
444 while (kiovoffset >= kiov->kiov_len) {
445 kiovoffset -= kiov->kiov_len;
454 this_nob = MIN(iov->iov_len - iovoffset,
455 kiov->kiov_len - kiovoffset);
456 this_nob = MIN(this_nob, nob);
459 addr = ((char *)kmap(kiov->kiov_page)) +
460 kiov->kiov_offset + kiovoffset;
462 memcpy ((char *)iov->iov_base + iovoffset, addr, this_nob);
465 if (iov->iov_len > iovoffset + this_nob) {
466 iovoffset += this_nob;
473 if (kiov->kiov_len > kiovoffset + this_nob) {
475 kiovoffset += this_nob;
477 kunmap(kiov->kiov_page);
487 kunmap(kiov->kiov_page);
489 EXPORT_SYMBOL(lnet_copy_kiov2iov);
492 lnet_copy_iov2kiov (unsigned int nkiov, lnet_kiov_t *kiov, unsigned int kiovoffset,
493 unsigned int niov, struct iovec *iov, unsigned int iovoffset,
496 /* NB kiov, iov are READ-ONLY */
497 unsigned int this_nob;
503 LASSERT (!in_interrupt ());
506 while (kiovoffset >= kiov->kiov_len) {
507 kiovoffset -= kiov->kiov_len;
514 while (iovoffset >= iov->iov_len) {
515 iovoffset -= iov->iov_len;
524 this_nob = MIN(kiov->kiov_len - kiovoffset,
525 iov->iov_len - iovoffset);
526 this_nob = MIN(this_nob, nob);
529 addr = ((char *)kmap(kiov->kiov_page)) +
530 kiov->kiov_offset + kiovoffset;
532 memcpy (addr, (char *)iov->iov_base + iovoffset, this_nob);
535 if (kiov->kiov_len > kiovoffset + this_nob) {
537 kiovoffset += this_nob;
539 kunmap(kiov->kiov_page);
546 if (iov->iov_len > iovoffset + this_nob) {
547 iovoffset += this_nob;
556 kunmap(kiov->kiov_page);
558 EXPORT_SYMBOL(lnet_copy_iov2kiov);
561 lnet_extract_kiov (int dst_niov, lnet_kiov_t *dst,
562 int src_niov, lnet_kiov_t *src,
563 unsigned int offset, unsigned int len)
565 /* Initialise 'dst' to the subset of 'src' starting at 'offset',
566 * for exactly 'len' bytes, and return the number of entries.
567 * NB not destructive to 'src' */
568 unsigned int frag_len;
571 if (len == 0) /* no data => */
572 return (0); /* no frags */
574 LASSERT (src_niov > 0);
575 while (offset >= src->kiov_len) { /* skip initial frags */
576 offset -= src->kiov_len;
579 LASSERT (src_niov > 0);
584 LASSERT (src_niov > 0);
585 LASSERT ((int)niov <= dst_niov);
587 frag_len = src->kiov_len - offset;
588 dst->kiov_page = src->kiov_page;
589 dst->kiov_offset = src->kiov_offset + offset;
591 if (len <= frag_len) {
593 LASSERT (dst->kiov_offset + dst->kiov_len <= PAGE_CACHE_SIZE);
597 dst->kiov_len = frag_len;
598 LASSERT (dst->kiov_offset + dst->kiov_len <= PAGE_CACHE_SIZE);
608 EXPORT_SYMBOL(lnet_extract_kiov);
612 lnet_ni_recv(lnet_ni_t *ni, void *private, lnet_msg_t *msg, int delayed,
613 unsigned int offset, unsigned int mlen, unsigned int rlen)
615 unsigned int niov = 0;
616 struct iovec *iov = NULL;
617 lnet_kiov_t *kiov = NULL;
620 LASSERT (!in_interrupt ());
621 LASSERT (mlen == 0 || msg != NULL);
624 LASSERT(msg->msg_receiving);
625 LASSERT(!msg->msg_sending);
626 LASSERT(rlen == msg->msg_len);
627 LASSERT(mlen <= msg->msg_len);
628 LASSERT(msg->msg_offset == offset);
629 LASSERT(msg->msg_wanted == mlen);
631 msg->msg_receiving = 0;
634 niov = msg->msg_niov;
636 kiov = msg->msg_kiov;
639 LASSERT ((iov == NULL) != (kiov == NULL));
643 rc = (ni->ni_lnd->lnd_recv)(ni, private, msg, delayed,
644 niov, iov, kiov, offset, mlen, rlen);
646 lnet_finalize(ni, msg, rc);
650 lnet_setpayloadbuffer(lnet_msg_t *msg)
652 lnet_libmd_t *md = msg->msg_md;
654 LASSERT (msg->msg_len > 0);
655 LASSERT (!msg->msg_routing);
656 LASSERT (md != NULL);
657 LASSERT (msg->msg_niov == 0);
658 LASSERT (msg->msg_iov == NULL);
659 LASSERT (msg->msg_kiov == NULL);
661 msg->msg_niov = md->md_niov;
662 if ((md->md_options & LNET_MD_KIOV) != 0)
663 msg->msg_kiov = md->md_iov.kiov;
665 msg->msg_iov = md->md_iov.iov;
669 lnet_prep_send(lnet_msg_t *msg, int type, lnet_process_id_t target,
670 unsigned int offset, unsigned int len)
672 msg->msg_type = type;
673 msg->msg_target = target;
675 msg->msg_offset = offset;
678 lnet_setpayloadbuffer(msg);
680 memset (&msg->msg_hdr, 0, sizeof (msg->msg_hdr));
681 msg->msg_hdr.type = cpu_to_le32(type);
682 msg->msg_hdr.dest_nid = cpu_to_le64(target.nid);
683 msg->msg_hdr.dest_pid = cpu_to_le32(target.pid);
684 /* src_nid will be set later */
685 msg->msg_hdr.src_pid = cpu_to_le32(the_lnet.ln_pid);
686 msg->msg_hdr.payload_length = cpu_to_le32(len);
690 lnet_ni_send(lnet_ni_t *ni, lnet_msg_t *msg)
692 void *priv = msg->msg_private;
695 LASSERT (!in_interrupt ());
696 LASSERT (LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND ||
697 (msg->msg_txcredit && msg->msg_peertxcredit));
699 rc = (ni->ni_lnd->lnd_send)(ni, priv, msg);
701 lnet_finalize(ni, msg, rc);
705 lnet_ni_eager_recv(lnet_ni_t *ni, lnet_msg_t *msg)
709 LASSERT(!msg->msg_sending);
710 LASSERT(msg->msg_receiving);
711 LASSERT(!msg->msg_rx_ready_delay);
712 LASSERT(ni->ni_lnd->lnd_eager_recv != NULL);
714 msg->msg_rx_ready_delay = 1;
715 rc = (ni->ni_lnd->lnd_eager_recv)(ni, msg->msg_private, msg,
718 CERROR("recv from %s / send to %s aborted: "
719 "eager_recv failed %d\n",
720 libcfs_nid2str(msg->msg_rxpeer->lp_nid),
721 libcfs_id2str(msg->msg_target), rc);
722 LASSERT(rc < 0); /* required by my callers */
728 /* NB: caller shall hold a ref on 'lp' as I'd drop lnet_net_lock */
730 lnet_ni_query_locked(lnet_ni_t *ni, lnet_peer_t *lp)
732 cfs_time_t last_alive = 0;
734 LASSERT(lnet_peer_aliveness_enabled(lp));
735 LASSERT(ni->ni_lnd->lnd_query != NULL);
737 lnet_net_unlock(lp->lp_cpt);
738 (ni->ni_lnd->lnd_query)(ni, lp->lp_nid, &last_alive);
739 lnet_net_lock(lp->lp_cpt);
741 lp->lp_last_query = cfs_time_current();
743 if (last_alive != 0) /* NI has updated timestamp */
744 lp->lp_last_alive = last_alive;
747 /* NB: always called with lnet_net_lock held */
749 lnet_peer_is_alive (lnet_peer_t *lp, cfs_time_t now)
754 LASSERT (lnet_peer_aliveness_enabled(lp));
756 /* Trust lnet_notify() if it has more recent aliveness news, but
757 * ignore the initial assumed death (see lnet_peers_start_down()).
759 if (!lp->lp_alive && lp->lp_alive_count > 0 &&
760 cfs_time_aftereq(lp->lp_timestamp, lp->lp_last_alive))
763 deadline = cfs_time_add(lp->lp_last_alive,
764 cfs_time_seconds(lp->lp_ni->ni_peertimeout));
765 alive = cfs_time_after(deadline, now);
767 /* Update obsolete lp_alive except for routers assumed to be dead
768 * initially, because router checker would update aliveness in this
769 * case, and moreover lp_last_alive at peer creation is assumed.
771 if (alive && !lp->lp_alive &&
772 !(lnet_isrouter(lp) && lp->lp_alive_count == 0))
773 lnet_notify_locked(lp, 0, 1, lp->lp_last_alive);
779 /* NB: returns 1 when alive, 0 when dead, negative when error;
780 * may drop the lnet_net_lock */
782 lnet_peer_alive_locked (lnet_peer_t *lp)
784 cfs_time_t now = cfs_time_current();
786 if (!lnet_peer_aliveness_enabled(lp))
789 if (lnet_peer_is_alive(lp, now))
792 /* Peer appears dead, but we should avoid frequent NI queries (at
793 * most once per lnet_queryinterval seconds). */
794 if (lp->lp_last_query != 0) {
795 static const int lnet_queryinterval = 1;
797 cfs_time_t next_query =
798 cfs_time_add(lp->lp_last_query,
799 cfs_time_seconds(lnet_queryinterval));
801 if (cfs_time_before(now, next_query)) {
803 CWARN("Unexpected aliveness of peer %s: "
805 libcfs_nid2str(lp->lp_nid),
806 (int)now, (int)next_query,
808 lp->lp_ni->ni_peertimeout);
813 /* query NI for latest aliveness news */
814 lnet_ni_query_locked(lp->lp_ni, lp);
816 if (lnet_peer_is_alive(lp, now))
819 lnet_notify_locked(lp, 0, 0, lp->lp_last_alive);
824 * \param msg The message to be sent.
825 * \param do_send True if lnet_ni_send() should be called in this function.
826 * lnet_send() is going to lnet_net_unlock immediately after this, so
827 * it sets do_send FALSE and I don't do the unlock/send/lock bit.
829 * \retval LNET_CREDIT_OK If \a msg sent or OK to send.
830 * \retval LNET_CREDIT_WAIT If \a msg blocked for credit.
831 * \retval -EHOSTUNREACH If the next hop of the message appears dead.
832 * \retval -ECANCELED If the MD of the message has been unlinked.
835 lnet_post_send_locked(lnet_msg_t *msg, int do_send)
837 lnet_peer_t *lp = msg->msg_txpeer;
838 lnet_ni_t *ni = lp->lp_ni;
839 int cpt = msg->msg_tx_cpt;
840 struct lnet_tx_queue *tq = ni->ni_tx_queues[cpt];
842 /* non-lnet_send() callers have checked before */
843 LASSERT(!do_send || msg->msg_tx_delayed);
844 LASSERT(!msg->msg_receiving);
845 LASSERT(msg->msg_tx_committed);
847 /* NB 'lp' is always the next hop */
848 if ((msg->msg_target.pid & LNET_PID_USERFLAG) == 0 &&
849 lnet_peer_alive_locked(lp) == 0) {
850 the_lnet.ln_counters[cpt]->drop_count++;
851 the_lnet.ln_counters[cpt]->drop_length += msg->msg_len;
852 lnet_net_unlock(cpt);
854 CNETERR("Dropping message for %s: peer not alive\n",
855 libcfs_id2str(msg->msg_target));
857 lnet_finalize(ni, msg, -EHOSTUNREACH);
860 return -EHOSTUNREACH;
863 if (msg->msg_md != NULL &&
864 (msg->msg_md->md_flags & LNET_MD_FLAG_ABORTED) != 0) {
865 lnet_net_unlock(cpt);
867 CNETERR("Aborting message for %s: LNetM[DE]Unlink() already "
868 "called on the MD/ME.\n",
869 libcfs_id2str(msg->msg_target));
871 lnet_finalize(ni, msg, -ECANCELED);
877 if (!msg->msg_peertxcredit) {
878 LASSERT((lp->lp_txcredits < 0) ==
879 !list_empty(&lp->lp_txq));
881 msg->msg_peertxcredit = 1;
882 lp->lp_txqnob += msg->msg_len + sizeof(lnet_hdr_t);
885 if (lp->lp_txcredits < lp->lp_mintxcredits)
886 lp->lp_mintxcredits = lp->lp_txcredits;
888 if (lp->lp_txcredits < 0) {
889 msg->msg_tx_delayed = 1;
890 list_add_tail(&msg->msg_list, &lp->lp_txq);
891 return LNET_CREDIT_WAIT;
895 if (!msg->msg_txcredit) {
896 LASSERT((tq->tq_credits < 0) ==
897 !list_empty(&tq->tq_delayed));
899 msg->msg_txcredit = 1;
902 if (tq->tq_credits < tq->tq_credits_min)
903 tq->tq_credits_min = tq->tq_credits;
905 if (tq->tq_credits < 0) {
906 msg->msg_tx_delayed = 1;
907 list_add_tail(&msg->msg_list, &tq->tq_delayed);
908 return LNET_CREDIT_WAIT;
913 lnet_net_unlock(cpt);
914 lnet_ni_send(ni, msg);
917 return LNET_CREDIT_OK;
923 lnet_msg2bufpool(lnet_msg_t *msg)
925 lnet_rtrbufpool_t *rbp;
928 LASSERT(msg->msg_rx_committed);
930 cpt = msg->msg_rx_cpt;
931 rbp = &the_lnet.ln_rtrpools[cpt][0];
933 LASSERT(msg->msg_len <= LNET_MTU);
934 while (msg->msg_len > (unsigned int)rbp->rbp_npages * PAGE_CACHE_SIZE) {
936 LASSERT(rbp < &the_lnet.ln_rtrpools[cpt][LNET_NRBPOOLS]);
943 lnet_post_routed_recv_locked (lnet_msg_t *msg, int do_recv)
945 /* lnet_parse is going to lnet_net_unlock immediately after this, so it
946 * sets do_recv FALSE and I don't do the unlock/send/lock bit.
947 * I return LNET_CREDIT_WAIT if msg blocked and LNET_CREDIT_OK if
948 * received or OK to receive */
949 lnet_peer_t *lp = msg->msg_rxpeer;
950 lnet_rtrbufpool_t *rbp;
953 LASSERT (msg->msg_iov == NULL);
954 LASSERT (msg->msg_kiov == NULL);
955 LASSERT (msg->msg_niov == 0);
956 LASSERT (msg->msg_routing);
957 LASSERT (msg->msg_receiving);
958 LASSERT (!msg->msg_sending);
960 /* non-lnet_parse callers only receive delayed messages */
961 LASSERT(!do_recv || msg->msg_rx_delayed);
963 if (!msg->msg_peerrtrcredit) {
964 LASSERT((lp->lp_rtrcredits < 0) ==
965 !list_empty(&lp->lp_rtrq));
967 msg->msg_peerrtrcredit = 1;
969 if (lp->lp_rtrcredits < lp->lp_minrtrcredits)
970 lp->lp_minrtrcredits = lp->lp_rtrcredits;
972 if (lp->lp_rtrcredits < 0) {
973 /* must have checked eager_recv before here */
974 LASSERT(msg->msg_rx_ready_delay);
975 msg->msg_rx_delayed = 1;
976 list_add_tail(&msg->msg_list, &lp->lp_rtrq);
977 return LNET_CREDIT_WAIT;
981 rbp = lnet_msg2bufpool(msg);
983 if (!msg->msg_rtrcredit) {
984 msg->msg_rtrcredit = 1;
986 if (rbp->rbp_credits < rbp->rbp_mincredits)
987 rbp->rbp_mincredits = rbp->rbp_credits;
989 if (rbp->rbp_credits < 0) {
990 /* must have checked eager_recv before here */
991 LASSERT(msg->msg_rx_ready_delay);
992 msg->msg_rx_delayed = 1;
993 list_add_tail(&msg->msg_list, &rbp->rbp_msgs);
994 return LNET_CREDIT_WAIT;
998 LASSERT(!list_empty(&rbp->rbp_bufs));
999 rb = list_entry(rbp->rbp_bufs.next, lnet_rtrbuf_t, rb_list);
1000 list_del(&rb->rb_list);
1002 msg->msg_niov = rbp->rbp_npages;
1003 msg->msg_kiov = &rb->rb_kiov[0];
1006 int cpt = msg->msg_rx_cpt;
1008 lnet_net_unlock(cpt);
1009 lnet_ni_recv(lp->lp_ni, msg->msg_private, msg, 1,
1010 0, msg->msg_len, msg->msg_len);
1013 return LNET_CREDIT_OK;
1018 lnet_return_tx_credits_locked(lnet_msg_t *msg)
1020 lnet_peer_t *txpeer = msg->msg_txpeer;
1023 if (msg->msg_txcredit) {
1024 struct lnet_ni *ni = txpeer->lp_ni;
1025 struct lnet_tx_queue *tq = ni->ni_tx_queues[msg->msg_tx_cpt];
1027 /* give back NI txcredits */
1028 msg->msg_txcredit = 0;
1030 LASSERT((tq->tq_credits < 0) ==
1031 !list_empty(&tq->tq_delayed));
1034 if (tq->tq_credits <= 0) {
1035 msg2 = list_entry(tq->tq_delayed.next,
1036 lnet_msg_t, msg_list);
1037 list_del(&msg2->msg_list);
1039 LASSERT(msg2->msg_txpeer->lp_ni == ni);
1040 LASSERT(msg2->msg_tx_delayed);
1042 (void) lnet_post_send_locked(msg2, 1);
1046 if (msg->msg_peertxcredit) {
1047 /* give back peer txcredits */
1048 msg->msg_peertxcredit = 0;
1050 LASSERT((txpeer->lp_txcredits < 0) ==
1051 !list_empty(&txpeer->lp_txq));
1053 txpeer->lp_txqnob -= msg->msg_len + sizeof(lnet_hdr_t);
1054 LASSERT (txpeer->lp_txqnob >= 0);
1056 txpeer->lp_txcredits++;
1057 if (txpeer->lp_txcredits <= 0) {
1058 msg2 = list_entry(txpeer->lp_txq.next,
1059 lnet_msg_t, msg_list);
1060 list_del(&msg2->msg_list);
1062 LASSERT(msg2->msg_txpeer == txpeer);
1063 LASSERT(msg2->msg_tx_delayed);
1065 (void) lnet_post_send_locked(msg2, 1);
1069 if (txpeer != NULL) {
1070 msg->msg_txpeer = NULL;
1071 lnet_peer_decref_locked(txpeer);
1077 lnet_schedule_blocked_locked(lnet_rtrbufpool_t *rbp)
1081 if (list_empty(&rbp->rbp_msgs))
1083 msg = list_entry(rbp->rbp_msgs.next,
1084 lnet_msg_t, msg_list);
1085 list_del(&msg->msg_list);
1087 (void)lnet_post_routed_recv_locked(msg, 1);
1092 lnet_drop_routed_msgs_locked(struct list_head *list, int cpt)
1096 struct list_head drop;
1098 INIT_LIST_HEAD(&drop);
1100 list_splice_init(list, &drop);
1102 lnet_net_unlock(cpt);
1104 list_for_each_entry_safe(msg, tmp, &drop, msg_list) {
1105 lnet_ni_recv(msg->msg_rxpeer->lp_ni, msg->msg_private, NULL,
1106 0, 0, 0, msg->msg_hdr.payload_length);
1107 list_del_init(&msg->msg_list);
1108 lnet_finalize(NULL, msg, -ECANCELED);
1115 lnet_return_rx_credits_locked(lnet_msg_t *msg)
1117 lnet_peer_t *rxpeer = msg->msg_rxpeer;
1121 if (msg->msg_rtrcredit) {
1122 /* give back global router credits */
1124 lnet_rtrbufpool_t *rbp;
1126 /* NB If a msg ever blocks for a buffer in rbp_msgs, it stays
1127 * there until it gets one allocated, or aborts the wait
1129 LASSERT(msg->msg_kiov != NULL);
1131 rb = list_entry(msg->msg_kiov, lnet_rtrbuf_t, rb_kiov[0]);
1134 msg->msg_kiov = NULL;
1135 msg->msg_rtrcredit = 0;
1137 LASSERT(rbp == lnet_msg2bufpool(msg));
1139 LASSERT((rbp->rbp_credits > 0) ==
1140 !list_empty(&rbp->rbp_bufs));
1142 /* If routing is now turned off, we just drop this buffer and
1143 * don't bother trying to return credits. */
1144 if (!the_lnet.ln_routing) {
1145 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1149 /* It is possible that a user has lowered the desired number of
1150 * buffers in this pool. Make sure we never put back
1151 * more buffers than the stated number. */
1152 if (rbp->rbp_credits >= rbp->rbp_nbuffers) {
1153 /* Discard this buffer so we don't have too many. */
1154 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1156 list_add(&rb->rb_list, &rbp->rbp_bufs);
1158 if (rbp->rbp_credits <= 0)
1159 lnet_schedule_blocked_locked(rbp);
1164 if (msg->msg_peerrtrcredit) {
1165 /* give back peer router credits */
1166 msg->msg_peerrtrcredit = 0;
1168 LASSERT((rxpeer->lp_rtrcredits < 0) ==
1169 !list_empty(&rxpeer->lp_rtrq));
1171 rxpeer->lp_rtrcredits++;
1173 /* drop all messages which are queued to be routed on that
1175 if (!the_lnet.ln_routing) {
1176 lnet_drop_routed_msgs_locked(&rxpeer->lp_rtrq,
1178 } else if (rxpeer->lp_rtrcredits <= 0) {
1179 msg2 = list_entry(rxpeer->lp_rtrq.next,
1180 lnet_msg_t, msg_list);
1181 list_del(&msg2->msg_list);
1183 (void) lnet_post_routed_recv_locked(msg2, 1);
1187 LASSERT(!msg->msg_rtrcredit);
1188 LASSERT(!msg->msg_peerrtrcredit);
1190 if (rxpeer != NULL) {
1191 msg->msg_rxpeer = NULL;
1192 lnet_peer_decref_locked(rxpeer);
1197 lnet_compare_routes(lnet_route_t *r1, lnet_route_t *r2)
1199 lnet_peer_t *p1 = r1->lr_gateway;
1200 lnet_peer_t *p2 = r2->lr_gateway;
1202 if (r1->lr_priority < r2->lr_priority)
1205 if (r1->lr_priority > r2->lr_priority)
1208 if (r1->lr_hops < r2->lr_hops)
1211 if (r1->lr_hops > r2->lr_hops)
1214 if (p1->lp_txqnob < p2->lp_txqnob)
1217 if (p1->lp_txqnob > p2->lp_txqnob)
1220 if (p1->lp_txcredits > p2->lp_txcredits)
1223 if (p1->lp_txcredits < p2->lp_txcredits)
1226 if (r1->lr_seq - r2->lr_seq <= 0)
1232 static lnet_peer_t *
1233 lnet_find_route_locked(lnet_ni_t *ni, lnet_nid_t target, lnet_nid_t rtr_nid)
1235 lnet_remotenet_t *rnet;
1236 lnet_route_t *route;
1237 lnet_route_t *best_route;
1238 lnet_route_t *last_route;
1239 struct lnet_peer *lp_best;
1240 struct lnet_peer *lp;
1243 /* If @rtr_nid is not LNET_NID_ANY, return the gateway with
1244 * rtr_nid nid, otherwise find the best gateway I can use */
1246 rnet = lnet_find_net_locked(LNET_NIDNET(target));
1251 best_route = last_route = NULL;
1252 list_for_each_entry(route, &rnet->lrn_routes, lr_list) {
1253 lp = route->lr_gateway;
1255 if (!lnet_is_route_alive(route))
1258 if (ni != NULL && lp->lp_ni != ni)
1261 if (lp->lp_nid == rtr_nid) /* it's pre-determined router */
1264 if (lp_best == NULL) {
1265 best_route = last_route = route;
1270 /* no protection on below fields, but it's harmless */
1271 if (last_route->lr_seq - route->lr_seq < 0)
1274 rc = lnet_compare_routes(route, best_route);
1282 /* set sequence number on the best router to the latest sequence + 1
1283 * so we can round-robin all routers, it's race and inaccurate but
1284 * harmless and functional */
1285 if (best_route != NULL)
1286 best_route->lr_seq = last_route->lr_seq + 1;
1291 lnet_send(lnet_nid_t src_nid, lnet_msg_t *msg, lnet_nid_t rtr_nid)
1293 lnet_nid_t dst_nid = msg->msg_target.nid;
1294 struct lnet_ni *src_ni;
1295 struct lnet_ni *local_ni;
1296 struct lnet_peer *lp;
1301 /* NB: rtr_nid is set to LNET_NID_ANY for all current use-cases,
1302 * but we might want to use pre-determined router for ACK/REPLY
1304 /* NB: ni != NULL == interface pre-determined (ACK/REPLY) */
1305 LASSERT (msg->msg_txpeer == NULL);
1306 LASSERT (!msg->msg_sending);
1307 LASSERT (!msg->msg_target_is_router);
1308 LASSERT (!msg->msg_receiving);
1310 msg->msg_sending = 1;
1312 LASSERT(!msg->msg_tx_committed);
1313 cpt = lnet_cpt_of_nid(rtr_nid == LNET_NID_ANY ? dst_nid : rtr_nid);
1317 if (the_lnet.ln_shutdown) {
1318 lnet_net_unlock(cpt);
1322 if (src_nid == LNET_NID_ANY) {
1325 src_ni = lnet_nid2ni_locked(src_nid, cpt);
1326 if (src_ni == NULL) {
1327 lnet_net_unlock(cpt);
1328 LCONSOLE_WARN("Can't send to %s: src %s is not a "
1329 "local nid\n", libcfs_nid2str(dst_nid),
1330 libcfs_nid2str(src_nid));
1333 LASSERT (!msg->msg_routing);
1336 /* Is this for someone on a local network? */
1337 local_ni = lnet_net2ni_locked(LNET_NIDNET(dst_nid), cpt);
1339 if (local_ni != NULL) {
1340 if (src_ni == NULL) {
1342 src_nid = src_ni->ni_nid;
1343 } else if (src_ni == local_ni) {
1344 lnet_ni_decref_locked(local_ni, cpt);
1346 lnet_ni_decref_locked(local_ni, cpt);
1347 lnet_ni_decref_locked(src_ni, cpt);
1348 lnet_net_unlock(cpt);
1349 LCONSOLE_WARN("No route to %s via from %s\n",
1350 libcfs_nid2str(dst_nid),
1351 libcfs_nid2str(src_nid));
1355 LASSERT(src_nid != LNET_NID_ANY);
1356 lnet_msg_commit(msg, cpt);
1358 if (!msg->msg_routing)
1359 msg->msg_hdr.src_nid = cpu_to_le64(src_nid);
1361 if (src_ni == the_lnet.ln_loni) {
1362 /* No send credit hassles with LOLND */
1363 lnet_net_unlock(cpt);
1364 lnet_ni_send(src_ni, msg);
1367 lnet_ni_decref_locked(src_ni, cpt);
1368 lnet_net_unlock(cpt);
1372 rc = lnet_nid2peer_locked(&lp, dst_nid, cpt);
1373 /* lp has ref on src_ni; lose mine */
1374 lnet_ni_decref_locked(src_ni, cpt);
1376 lnet_net_unlock(cpt);
1377 LCONSOLE_WARN("Error %d finding peer %s\n", rc,
1378 libcfs_nid2str(dst_nid));
1379 /* ENOMEM or shutting down */
1382 LASSERT (lp->lp_ni == src_ni);
1385 lnet_net_unlock(cpt);
1388 * - once application finishes computation, check here to update
1389 * router states before it waits for pending IO in LNetEQPoll
1390 * - recursion breaker: router checker sends no message
1391 * to remote networks */
1392 if (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING)
1393 lnet_router_checker();
1397 /* sending to a remote network */
1398 lp = lnet_find_route_locked(src_ni, dst_nid, rtr_nid);
1401 lnet_ni_decref_locked(src_ni, cpt);
1402 lnet_net_unlock(cpt);
1404 LCONSOLE_WARN("No route to %s via %s "
1405 "(all routers down)\n",
1406 libcfs_id2str(msg->msg_target),
1407 libcfs_nid2str(src_nid));
1408 return -EHOSTUNREACH;
1411 /* rtr_nid is LNET_NID_ANY or NID of pre-determined router,
1412 * it's possible that rtr_nid isn't LNET_NID_ANY and lp isn't
1413 * pre-determined router, this can happen if router table
1414 * was changed when we release the lock */
1415 if (rtr_nid != lp->lp_nid) {
1416 cpt2 = lnet_cpt_of_nid_locked(lp->lp_nid);
1419 lnet_ni_decref_locked(src_ni, cpt);
1420 lnet_net_unlock(cpt);
1422 rtr_nid = lp->lp_nid;
1428 CDEBUG(D_NET, "Best route to %s via %s for %s %d\n",
1429 libcfs_nid2str(dst_nid), libcfs_nid2str(lp->lp_nid),
1430 lnet_msgtyp2str(msg->msg_type), msg->msg_len);
1432 if (src_ni == NULL) {
1434 src_nid = src_ni->ni_nid;
1436 LASSERT (src_ni == lp->lp_ni);
1437 lnet_ni_decref_locked(src_ni, cpt);
1440 lnet_peer_addref_locked(lp);
1442 LASSERT(src_nid != LNET_NID_ANY);
1443 lnet_msg_commit(msg, cpt);
1445 if (!msg->msg_routing) {
1446 /* I'm the source and now I know which NI to send on */
1447 msg->msg_hdr.src_nid = cpu_to_le64(src_nid);
1450 msg->msg_target_is_router = 1;
1451 msg->msg_target.nid = lp->lp_nid;
1452 msg->msg_target.pid = LUSTRE_SRV_LNET_PID;
1455 /* 'lp' is our best choice of peer */
1457 LASSERT (!msg->msg_peertxcredit);
1458 LASSERT (!msg->msg_txcredit);
1459 LASSERT (msg->msg_txpeer == NULL);
1461 msg->msg_txpeer = lp; /* msg takes my ref on lp */
1463 rc = lnet_post_send_locked(msg, 0);
1464 lnet_net_unlock(cpt);
1469 if (rc == LNET_CREDIT_OK)
1470 lnet_ni_send(src_ni, msg);
1472 return 0; /* rc == LNET_CREDIT_OK or LNET_CREDIT_WAIT */
1476 lnet_drop_message(lnet_ni_t *ni, int cpt, void *private, unsigned int nob)
1479 the_lnet.ln_counters[cpt]->drop_count++;
1480 the_lnet.ln_counters[cpt]->drop_length += nob;
1481 lnet_net_unlock(cpt);
1483 lnet_ni_recv(ni, private, NULL, 0, 0, 0, nob);
1487 lnet_recv_put(lnet_ni_t *ni, lnet_msg_t *msg)
1489 lnet_hdr_t *hdr = &msg->msg_hdr;
1491 if (msg->msg_wanted != 0)
1492 lnet_setpayloadbuffer(msg);
1494 lnet_build_msg_event(msg, LNET_EVENT_PUT);
1496 /* Must I ACK? If so I'll grab the ack_wmd out of the header and put
1497 * it back into the ACK during lnet_finalize() */
1498 msg->msg_ack = (!lnet_is_wire_handle_none(&hdr->msg.put.ack_wmd) &&
1499 (msg->msg_md->md_options & LNET_MD_ACK_DISABLE) == 0);
1501 lnet_ni_recv(ni, msg->msg_private, msg, msg->msg_rx_delayed,
1502 msg->msg_offset, msg->msg_wanted, hdr->payload_length);
1506 lnet_parse_put(lnet_ni_t *ni, lnet_msg_t *msg)
1508 lnet_hdr_t *hdr = &msg->msg_hdr;
1509 struct lnet_match_info info;
1512 /* Convert put fields to host byte order */
1513 hdr->msg.put.match_bits = le64_to_cpu(hdr->msg.put.match_bits);
1514 hdr->msg.put.ptl_index = le32_to_cpu(hdr->msg.put.ptl_index);
1515 hdr->msg.put.offset = le32_to_cpu(hdr->msg.put.offset);
1517 info.mi_id.nid = hdr->src_nid;
1518 info.mi_id.pid = hdr->src_pid;
1519 info.mi_opc = LNET_MD_OP_PUT;
1520 info.mi_portal = hdr->msg.put.ptl_index;
1521 info.mi_rlength = hdr->payload_length;
1522 info.mi_roffset = hdr->msg.put.offset;
1523 info.mi_mbits = hdr->msg.put.match_bits;
1525 msg->msg_rx_ready_delay = ni->ni_lnd->lnd_eager_recv == NULL;
1528 rc = lnet_ptl_match_md(&info, msg);
1533 case LNET_MATCHMD_OK:
1534 lnet_recv_put(ni, msg);
1537 case LNET_MATCHMD_NONE:
1538 if (msg->msg_rx_delayed) /* attached on delayed list */
1541 rc = lnet_ni_eager_recv(ni, msg);
1546 case LNET_MATCHMD_DROP:
1547 CNETERR("Dropping PUT from %s portal %d match "LPU64
1548 " offset %d length %d: %d\n",
1549 libcfs_id2str(info.mi_id), info.mi_portal,
1550 info.mi_mbits, info.mi_roffset, info.mi_rlength, rc);
1552 return ENOENT; /* +ve: OK but no match */
1557 lnet_parse_get(lnet_ni_t *ni, lnet_msg_t *msg, int rdma_get)
1559 struct lnet_match_info info;
1560 lnet_hdr_t *hdr = &msg->msg_hdr;
1561 lnet_handle_wire_t reply_wmd;
1564 /* Convert get fields to host byte order */
1565 hdr->msg.get.match_bits = le64_to_cpu(hdr->msg.get.match_bits);
1566 hdr->msg.get.ptl_index = le32_to_cpu(hdr->msg.get.ptl_index);
1567 hdr->msg.get.sink_length = le32_to_cpu(hdr->msg.get.sink_length);
1568 hdr->msg.get.src_offset = le32_to_cpu(hdr->msg.get.src_offset);
1570 info.mi_id.nid = hdr->src_nid;
1571 info.mi_id.pid = hdr->src_pid;
1572 info.mi_opc = LNET_MD_OP_GET;
1573 info.mi_portal = hdr->msg.get.ptl_index;
1574 info.mi_rlength = hdr->msg.get.sink_length;
1575 info.mi_roffset = hdr->msg.get.src_offset;
1576 info.mi_mbits = hdr->msg.get.match_bits;
1578 rc = lnet_ptl_match_md(&info, msg);
1579 if (rc == LNET_MATCHMD_DROP) {
1580 CNETERR("Dropping GET from %s portal %d match "LPU64
1581 " offset %d length %d\n",
1582 libcfs_id2str(info.mi_id), info.mi_portal,
1583 info.mi_mbits, info.mi_roffset, info.mi_rlength);
1584 return ENOENT; /* +ve: OK but no match */
1587 LASSERT(rc == LNET_MATCHMD_OK);
1589 lnet_build_msg_event(msg, LNET_EVENT_GET);
1591 reply_wmd = hdr->msg.get.return_wmd;
1593 lnet_prep_send(msg, LNET_MSG_REPLY, info.mi_id,
1594 msg->msg_offset, msg->msg_wanted);
1596 msg->msg_hdr.msg.reply.dst_wmd = reply_wmd;
1599 /* The LND completes the REPLY from her recv procedure */
1600 lnet_ni_recv(ni, msg->msg_private, msg, 0,
1601 msg->msg_offset, msg->msg_len, msg->msg_len);
1605 lnet_ni_recv(ni, msg->msg_private, NULL, 0, 0, 0, 0);
1606 msg->msg_receiving = 0;
1608 rc = lnet_send(ni->ni_nid, msg, LNET_NID_ANY);
1610 /* didn't get as far as lnet_ni_send() */
1611 CERROR("%s: Unable to send REPLY for GET from %s: %d\n",
1612 libcfs_nid2str(ni->ni_nid),
1613 libcfs_id2str(info.mi_id), rc);
1615 lnet_finalize(ni, msg, rc);
1622 lnet_parse_reply(lnet_ni_t *ni, lnet_msg_t *msg)
1624 void *private = msg->msg_private;
1625 lnet_hdr_t *hdr = &msg->msg_hdr;
1626 lnet_process_id_t src = {0};
1632 cpt = lnet_cpt_of_cookie(hdr->msg.reply.dst_wmd.wh_object_cookie);
1635 src.nid = hdr->src_nid;
1636 src.pid = hdr->src_pid;
1638 /* NB handles only looked up by creator (no flips) */
1639 md = lnet_wire_handle2md(&hdr->msg.reply.dst_wmd);
1640 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
1641 CNETERR("%s: Dropping REPLY from %s for %s "
1642 "MD "LPX64"."LPX64"\n",
1643 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1644 (md == NULL) ? "invalid" : "inactive",
1645 hdr->msg.reply.dst_wmd.wh_interface_cookie,
1646 hdr->msg.reply.dst_wmd.wh_object_cookie);
1647 if (md != NULL && md->md_me != NULL)
1648 CERROR("REPLY MD also attached to portal %d\n",
1649 md->md_me->me_portal);
1651 lnet_res_unlock(cpt);
1652 return ENOENT; /* +ve: OK but no match */
1655 LASSERT (md->md_offset == 0);
1657 rlength = hdr->payload_length;
1658 mlength = MIN(rlength, (int)md->md_length);
1660 if (mlength < rlength &&
1661 (md->md_options & LNET_MD_TRUNCATE) == 0) {
1662 CNETERR("%s: Dropping REPLY from %s length %d "
1663 "for MD "LPX64" would overflow (%d)\n",
1664 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1665 rlength, hdr->msg.reply.dst_wmd.wh_object_cookie,
1667 lnet_res_unlock(cpt);
1668 return ENOENT; /* +ve: OK but no match */
1671 CDEBUG(D_NET, "%s: Reply from %s of length %d/%d into md "LPX64"\n",
1672 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1673 mlength, rlength, hdr->msg.reply.dst_wmd.wh_object_cookie);
1675 lnet_msg_attach_md(msg, md, 0, mlength);
1678 lnet_setpayloadbuffer(msg);
1680 lnet_res_unlock(cpt);
1682 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
1684 lnet_ni_recv(ni, private, msg, 0, 0, mlength, rlength);
1689 lnet_parse_ack(lnet_ni_t *ni, lnet_msg_t *msg)
1691 lnet_hdr_t *hdr = &msg->msg_hdr;
1692 lnet_process_id_t src = {0};
1696 src.nid = hdr->src_nid;
1697 src.pid = hdr->src_pid;
1699 /* Convert ack fields to host byte order */
1700 hdr->msg.ack.match_bits = le64_to_cpu(hdr->msg.ack.match_bits);
1701 hdr->msg.ack.mlength = le32_to_cpu(hdr->msg.ack.mlength);
1703 cpt = lnet_cpt_of_cookie(hdr->msg.ack.dst_wmd.wh_object_cookie);
1706 /* NB handles only looked up by creator (no flips) */
1707 md = lnet_wire_handle2md(&hdr->msg.ack.dst_wmd);
1708 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
1709 /* Don't moan; this is expected */
1711 "%s: Dropping ACK from %s to %s MD "LPX64"."LPX64"\n",
1712 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1713 (md == NULL) ? "invalid" : "inactive",
1714 hdr->msg.ack.dst_wmd.wh_interface_cookie,
1715 hdr->msg.ack.dst_wmd.wh_object_cookie);
1716 if (md != NULL && md->md_me != NULL)
1717 CERROR("Source MD also attached to portal %d\n",
1718 md->md_me->me_portal);
1720 lnet_res_unlock(cpt);
1721 return ENOENT; /* +ve! */
1724 CDEBUG(D_NET, "%s: ACK from %s into md "LPX64"\n",
1725 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1726 hdr->msg.ack.dst_wmd.wh_object_cookie);
1728 lnet_msg_attach_md(msg, md, 0, 0);
1730 lnet_res_unlock(cpt);
1732 lnet_build_msg_event(msg, LNET_EVENT_ACK);
1734 lnet_ni_recv(ni, msg->msg_private, msg, 0, 0, 0, msg->msg_len);
1739 * \retval LNET_CREDIT_OK If \a msg is forwarded
1740 * \retval LNET_CREDIT_WAIT If \a msg is blocked because w/o buffer
1741 * \retval -ve error code
1744 lnet_parse_forward_locked(lnet_ni_t *ni, lnet_msg_t *msg)
1749 if (!the_lnet.ln_routing)
1752 if (msg->msg_rxpeer->lp_rtrcredits <= 0 ||
1753 lnet_msg2bufpool(msg)->rbp_credits <= 0) {
1754 if (ni->ni_lnd->lnd_eager_recv == NULL) {
1755 msg->msg_rx_ready_delay = 1;
1757 lnet_net_unlock(msg->msg_rx_cpt);
1758 rc = lnet_ni_eager_recv(ni, msg);
1759 lnet_net_lock(msg->msg_rx_cpt);
1764 rc = lnet_post_routed_recv_locked(msg, 0);
1772 lnet_msgtyp2str (int type)
1781 case LNET_MSG_REPLY:
1783 case LNET_MSG_HELLO:
1786 return ("<UNKNOWN>");
1789 EXPORT_SYMBOL(lnet_msgtyp2str);
1792 lnet_print_hdr(lnet_hdr_t * hdr)
1794 lnet_process_id_t src = {0};
1795 lnet_process_id_t dst = {0};
1796 char *type_str = lnet_msgtyp2str (hdr->type);
1798 src.nid = hdr->src_nid;
1799 src.pid = hdr->src_pid;
1801 dst.nid = hdr->dest_nid;
1802 dst.pid = hdr->dest_pid;
1804 CWARN("P3 Header at %p of type %s\n", hdr, type_str);
1805 CWARN(" From %s\n", libcfs_id2str(src));
1806 CWARN(" To %s\n", libcfs_id2str(dst));
1808 switch (hdr->type) {
1813 CWARN(" Ptl index %d, ack md "LPX64"."LPX64", "
1814 "match bits "LPU64"\n",
1815 hdr->msg.put.ptl_index,
1816 hdr->msg.put.ack_wmd.wh_interface_cookie,
1817 hdr->msg.put.ack_wmd.wh_object_cookie,
1818 hdr->msg.put.match_bits);
1819 CWARN(" Length %d, offset %d, hdr data "LPX64"\n",
1820 hdr->payload_length, hdr->msg.put.offset,
1821 hdr->msg.put.hdr_data);
1825 CWARN(" Ptl index %d, return md "LPX64"."LPX64", "
1826 "match bits "LPU64"\n", hdr->msg.get.ptl_index,
1827 hdr->msg.get.return_wmd.wh_interface_cookie,
1828 hdr->msg.get.return_wmd.wh_object_cookie,
1829 hdr->msg.get.match_bits);
1830 CWARN(" Length %d, src offset %d\n",
1831 hdr->msg.get.sink_length,
1832 hdr->msg.get.src_offset);
1836 CWARN(" dst md "LPX64"."LPX64", "
1837 "manipulated length %d\n",
1838 hdr->msg.ack.dst_wmd.wh_interface_cookie,
1839 hdr->msg.ack.dst_wmd.wh_object_cookie,
1840 hdr->msg.ack.mlength);
1843 case LNET_MSG_REPLY:
1844 CWARN(" dst md "LPX64"."LPX64", "
1846 hdr->msg.reply.dst_wmd.wh_interface_cookie,
1847 hdr->msg.reply.dst_wmd.wh_object_cookie,
1848 hdr->payload_length);
1854 lnet_parse(lnet_ni_t *ni, lnet_hdr_t *hdr, lnet_nid_t from_nid,
1855 void *private, int rdma_req)
1860 struct lnet_msg *msg;
1861 lnet_pid_t dest_pid;
1862 lnet_nid_t dest_nid;
1864 __u32 payload_length;
1867 LASSERT (!in_interrupt ());
1869 type = le32_to_cpu(hdr->type);
1870 src_nid = le64_to_cpu(hdr->src_nid);
1871 dest_nid = le64_to_cpu(hdr->dest_nid);
1872 dest_pid = le32_to_cpu(hdr->dest_pid);
1873 payload_length = le32_to_cpu(hdr->payload_length);
1875 for_me = (ni->ni_nid == dest_nid);
1876 cpt = lnet_cpt_of_nid(from_nid);
1881 if (payload_length > 0) {
1882 CERROR("%s, src %s: bad %s payload %d (0 expected)\n",
1883 libcfs_nid2str(from_nid),
1884 libcfs_nid2str(src_nid),
1885 lnet_msgtyp2str(type), payload_length);
1891 case LNET_MSG_REPLY:
1892 if (payload_length >
1893 (__u32)(for_me ? LNET_MAX_PAYLOAD : LNET_MTU)) {
1894 CERROR("%s, src %s: bad %s payload %d "
1895 "(%d max expected)\n",
1896 libcfs_nid2str(from_nid),
1897 libcfs_nid2str(src_nid),
1898 lnet_msgtyp2str(type),
1900 for_me ? LNET_MAX_PAYLOAD : LNET_MTU);
1906 CERROR("%s, src %s: Bad message type 0x%x\n",
1907 libcfs_nid2str(from_nid),
1908 libcfs_nid2str(src_nid), type);
1912 if (the_lnet.ln_routing &&
1913 ni->ni_last_alive != cfs_time_current_sec()) {
1914 /* NB: so far here is the only place to set NI status to "up */
1916 ni->ni_last_alive = cfs_time_current_sec();
1917 if (ni->ni_status != NULL &&
1918 ni->ni_status->ns_status == LNET_NI_STATUS_DOWN)
1919 ni->ni_status->ns_status = LNET_NI_STATUS_UP;
1923 /* Regard a bad destination NID as a protocol error. Senders should
1924 * know what they're doing; if they don't they're misconfigured, buggy
1925 * or malicious so we chop them off at the knees :) */
1928 if (LNET_NIDNET(dest_nid) == LNET_NIDNET(ni->ni_nid)) {
1929 /* should have gone direct */
1930 CERROR("%s, src %s: Bad dest nid %s "
1931 "(should have been sent direct)\n",
1932 libcfs_nid2str(from_nid),
1933 libcfs_nid2str(src_nid),
1934 libcfs_nid2str(dest_nid));
1938 if (lnet_islocalnid(dest_nid)) {
1939 /* dest is another local NI; sender should have used
1940 * this node's NID on its own network */
1941 CERROR("%s, src %s: Bad dest nid %s "
1942 "(it's my nid but on a different network)\n",
1943 libcfs_nid2str(from_nid),
1944 libcfs_nid2str(src_nid),
1945 libcfs_nid2str(dest_nid));
1949 if (rdma_req && type == LNET_MSG_GET) {
1950 CERROR("%s, src %s: Bad optimized GET for %s "
1951 "(final destination must be me)\n",
1952 libcfs_nid2str(from_nid),
1953 libcfs_nid2str(src_nid),
1954 libcfs_nid2str(dest_nid));
1958 if (!the_lnet.ln_routing) {
1959 CERROR("%s, src %s: Dropping message for %s "
1960 "(routing not enabled)\n",
1961 libcfs_nid2str(from_nid),
1962 libcfs_nid2str(src_nid),
1963 libcfs_nid2str(dest_nid));
1968 /* Message looks OK; we're not going to return an error, so we MUST
1969 * call back lnd_recv() come what may... */
1971 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
1972 fail_peer(src_nid, 0)) { /* shall we now? */
1973 CERROR("%s, src %s: Dropping %s to simulate failure\n",
1974 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
1975 lnet_msgtyp2str(type));
1979 msg = lnet_msg_alloc();
1981 CERROR("%s, src %s: Dropping %s (out of memory)\n",
1982 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
1983 lnet_msgtyp2str(type));
1987 /* msg zeroed in lnet_msg_alloc; i.e. flags all clear,
1988 * pointers NULL etc */
1990 msg->msg_type = type;
1991 msg->msg_private = private;
1992 msg->msg_receiving = 1;
1993 msg->msg_len = msg->msg_wanted = payload_length;
1994 msg->msg_offset = 0;
1995 msg->msg_hdr = *hdr;
1996 /* for building message event */
1997 msg->msg_from = from_nid;
1999 msg->msg_target.pid = dest_pid;
2000 msg->msg_target.nid = dest_nid;
2001 msg->msg_routing = 1;
2004 /* convert common msg->hdr fields to host byteorder */
2005 msg->msg_hdr.type = type;
2006 msg->msg_hdr.src_nid = src_nid;
2007 msg->msg_hdr.src_pid = le32_to_cpu(msg->msg_hdr.src_pid);
2008 msg->msg_hdr.dest_nid = dest_nid;
2009 msg->msg_hdr.dest_pid = dest_pid;
2010 msg->msg_hdr.payload_length = payload_length;
2014 rc = lnet_nid2peer_locked(&msg->msg_rxpeer, from_nid, cpt);
2016 lnet_net_unlock(cpt);
2017 CERROR("%s, src %s: Dropping %s "
2018 "(error %d looking up sender)\n",
2019 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
2020 lnet_msgtyp2str(type), rc);
2025 lnet_msg_commit(msg, cpt);
2028 rc = lnet_parse_forward_locked(ni, msg);
2029 lnet_net_unlock(cpt);
2034 if (rc == LNET_CREDIT_OK) {
2035 lnet_ni_recv(ni, msg->msg_private, msg, 0,
2036 0, payload_length, payload_length);
2041 lnet_net_unlock(cpt);
2045 rc = lnet_parse_ack(ni, msg);
2048 rc = lnet_parse_put(ni, msg);
2051 rc = lnet_parse_get(ni, msg, rdma_req);
2053 case LNET_MSG_REPLY:
2054 rc = lnet_parse_reply(ni, msg);
2059 goto free_drop; /* prevent an unused label if !kernel */
2065 LASSERT(rc == ENOENT);
2068 LASSERT(msg->msg_md == NULL);
2069 lnet_finalize(ni, msg, rc);
2072 lnet_drop_message(ni, cpt, private, payload_length);
2075 EXPORT_SYMBOL(lnet_parse);
2078 lnet_drop_delayed_msg_list(struct list_head *head, char *reason)
2080 while (!list_empty(head)) {
2081 lnet_process_id_t id = {0};
2084 msg = list_entry(head->next, lnet_msg_t, msg_list);
2085 list_del(&msg->msg_list);
2087 id.nid = msg->msg_hdr.src_nid;
2088 id.pid = msg->msg_hdr.src_pid;
2090 LASSERT(msg->msg_md == NULL);
2091 LASSERT(msg->msg_rx_delayed);
2092 LASSERT(msg->msg_rxpeer != NULL);
2093 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
2095 CWARN("Dropping delayed PUT from %s portal %d match "LPU64
2096 " offset %d length %d: %s\n",
2098 msg->msg_hdr.msg.put.ptl_index,
2099 msg->msg_hdr.msg.put.match_bits,
2100 msg->msg_hdr.msg.put.offset,
2101 msg->msg_hdr.payload_length, reason);
2103 /* NB I can't drop msg's ref on msg_rxpeer until after I've
2104 * called lnet_drop_message(), so I just hang onto msg as well
2105 * until that's done */
2107 lnet_drop_message(msg->msg_rxpeer->lp_ni,
2108 msg->msg_rxpeer->lp_cpt,
2109 msg->msg_private, msg->msg_len);
2111 * NB: message will not generate event because w/o attached MD,
2112 * but we still should give error code so lnet_msg_decommit()
2113 * can skip counters operations and other checks.
2115 lnet_finalize(msg->msg_rxpeer->lp_ni, msg, -ENOENT);
2120 lnet_recv_delayed_msg_list(struct list_head *head)
2122 while (!list_empty(head)) {
2124 lnet_process_id_t id;
2126 msg = list_entry(head->next, lnet_msg_t, msg_list);
2127 list_del(&msg->msg_list);
2129 /* md won't disappear under me, since each msg
2130 * holds a ref on it */
2132 id.nid = msg->msg_hdr.src_nid;
2133 id.pid = msg->msg_hdr.src_pid;
2135 LASSERT(msg->msg_rx_delayed);
2136 LASSERT(msg->msg_md != NULL);
2137 LASSERT(msg->msg_rxpeer != NULL);
2138 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
2140 CDEBUG(D_NET, "Resuming delayed PUT from %s portal %d "
2141 "match "LPU64" offset %d length %d.\n",
2142 libcfs_id2str(id), msg->msg_hdr.msg.put.ptl_index,
2143 msg->msg_hdr.msg.put.match_bits,
2144 msg->msg_hdr.msg.put.offset,
2145 msg->msg_hdr.payload_length);
2147 lnet_recv_put(msg->msg_rxpeer->lp_ni, msg);
2152 * Initiate an asynchronous PUT operation.
2154 * There are several events associated with a PUT: completion of the send on
2155 * the initiator node (LNET_EVENT_SEND), and when the send completes
2156 * successfully, the receipt of an acknowledgment (LNET_EVENT_ACK) indicating
2157 * that the operation was accepted by the target. The event LNET_EVENT_PUT is
2158 * used at the target node to indicate the completion of incoming data
2161 * The local events will be logged in the EQ associated with the MD pointed to
2162 * by \a mdh handle. Using a MD without an associated EQ results in these
2163 * events being discarded. In this case, the caller must have another
2164 * mechanism (e.g., a higher level protocol) for determining when it is safe
2165 * to modify the memory region associated with the MD.
2167 * Note that LNet does not guarantee the order of LNET_EVENT_SEND and
2168 * LNET_EVENT_ACK, though intuitively ACK should happen after SEND.
2170 * \param self Indicates the NID of a local interface through which to send
2171 * the PUT request. Use LNET_NID_ANY to let LNet choose one by itself.
2172 * \param mdh A handle for the MD that describes the memory to be sent. The MD
2173 * must be "free floating" (See LNetMDBind()).
2174 * \param ack Controls whether an acknowledgment is requested.
2175 * Acknowledgments are only sent when they are requested by the initiating
2176 * process and the target MD enables them.
2177 * \param target A process identifier for the target process.
2178 * \param portal The index in the \a target's portal table.
2179 * \param match_bits The match bits to use for MD selection at the target
2181 * \param offset The offset into the target MD (only used when the target
2182 * MD has the LNET_MD_MANAGE_REMOTE option set).
2183 * \param hdr_data 64 bits of user data that can be included in the message
2184 * header. This data is written to an event queue entry at the target if an
2185 * EQ is present on the matching MD.
2187 * \retval 0 Success, and only in this case events will be generated
2188 * and logged to EQ (if it exists).
2189 * \retval -EIO Simulated failure.
2190 * \retval -ENOMEM Memory allocation failure.
2191 * \retval -ENOENT Invalid MD object.
2193 * \see lnet_event_t::hdr_data and lnet_event_kind_t.
2196 LNetPut(lnet_nid_t self, lnet_handle_md_t mdh, lnet_ack_req_t ack,
2197 lnet_process_id_t target, unsigned int portal,
2198 __u64 match_bits, unsigned int offset,
2201 struct lnet_msg *msg;
2202 struct lnet_libmd *md;
2206 LASSERT(the_lnet.ln_init);
2207 LASSERT(the_lnet.ln_refcount > 0);
2209 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
2210 fail_peer(target.nid, 1)) { /* shall we now? */
2211 CERROR("Dropping PUT to %s: simulated failure\n",
2212 libcfs_id2str(target));
2216 msg = lnet_msg_alloc();
2218 CERROR("Dropping PUT to %s: ENOMEM on lnet_msg_t\n",
2219 libcfs_id2str(target));
2222 msg->msg_vmflush = !!memory_pressure_get();
2224 cpt = lnet_cpt_of_cookie(mdh.cookie);
2227 md = lnet_handle2md(&mdh);
2228 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
2229 CERROR("Dropping PUT ("LPU64":%d:%s): MD (%d) invalid\n",
2230 match_bits, portal, libcfs_id2str(target),
2231 md == NULL ? -1 : md->md_threshold);
2232 if (md != NULL && md->md_me != NULL)
2233 CERROR("Source MD also attached to portal %d\n",
2234 md->md_me->me_portal);
2235 lnet_res_unlock(cpt);
2241 CDEBUG(D_NET, "LNetPut -> %s\n", libcfs_id2str(target));
2243 lnet_msg_attach_md(msg, md, 0, 0);
2245 lnet_prep_send(msg, LNET_MSG_PUT, target, 0, md->md_length);
2247 msg->msg_hdr.msg.put.match_bits = cpu_to_le64(match_bits);
2248 msg->msg_hdr.msg.put.ptl_index = cpu_to_le32(portal);
2249 msg->msg_hdr.msg.put.offset = cpu_to_le32(offset);
2250 msg->msg_hdr.msg.put.hdr_data = hdr_data;
2252 /* NB handles only looked up by creator (no flips) */
2253 if (ack == LNET_ACK_REQ) {
2254 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
2255 the_lnet.ln_interface_cookie;
2256 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
2257 md->md_lh.lh_cookie;
2259 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
2260 LNET_WIRE_HANDLE_COOKIE_NONE;
2261 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
2262 LNET_WIRE_HANDLE_COOKIE_NONE;
2265 lnet_res_unlock(cpt);
2267 lnet_build_msg_event(msg, LNET_EVENT_SEND);
2269 rc = lnet_send(self, msg, LNET_NID_ANY);
2271 CNETERR( "Error sending PUT to %s: %d\n",
2272 libcfs_id2str(target), rc);
2273 lnet_finalize (NULL, msg, rc);
2276 /* completion will be signalled by an event */
2279 EXPORT_SYMBOL(LNetPut);
2282 lnet_create_reply_msg (lnet_ni_t *ni, lnet_msg_t *getmsg)
2284 /* The LND can DMA direct to the GET md (i.e. no REPLY msg). This
2285 * returns a msg for the LND to pass to lnet_finalize() when the sink
2286 * data has been received.
2288 * CAVEAT EMPTOR: 'getmsg' is the original GET, which is freed when
2289 * lnet_finalize() is called on it, so the LND must call this first */
2291 struct lnet_msg *msg = lnet_msg_alloc();
2292 struct lnet_libmd *getmd = getmsg->msg_md;
2293 lnet_process_id_t peer_id = getmsg->msg_target;
2296 LASSERT(!getmsg->msg_target_is_router);
2297 LASSERT(!getmsg->msg_routing);
2300 CERROR("%s: Dropping REPLY from %s: can't allocate msg\n",
2301 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id));
2305 cpt = lnet_cpt_of_cookie(getmd->md_lh.lh_cookie);
2308 LASSERT(getmd->md_refcount > 0);
2310 if (getmd->md_threshold == 0) {
2311 CERROR ("%s: Dropping REPLY from %s for inactive MD %p\n",
2312 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id),
2314 lnet_res_unlock(cpt);
2318 LASSERT(getmd->md_offset == 0);
2320 CDEBUG(D_NET, "%s: Reply from %s md %p\n",
2321 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id), getmd);
2323 /* setup information for lnet_build_msg_event */
2324 msg->msg_from = peer_id.nid;
2325 msg->msg_type = LNET_MSG_GET; /* flag this msg as an "optimized" GET */
2326 msg->msg_hdr.src_nid = peer_id.nid;
2327 msg->msg_hdr.payload_length = getmd->md_length;
2328 msg->msg_receiving = 1; /* required by lnet_msg_attach_md */
2330 lnet_msg_attach_md(msg, getmd, getmd->md_offset, getmd->md_length);
2331 lnet_res_unlock(cpt);
2333 cpt = lnet_cpt_of_nid(peer_id.nid);
2336 lnet_msg_commit(msg, cpt);
2337 lnet_net_unlock(cpt);
2339 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
2344 cpt = lnet_cpt_of_nid(peer_id.nid);
2347 the_lnet.ln_counters[cpt]->drop_count++;
2348 the_lnet.ln_counters[cpt]->drop_length += getmd->md_length;
2349 lnet_net_unlock(cpt);
2356 EXPORT_SYMBOL(lnet_create_reply_msg);
2359 lnet_set_reply_msg_len(lnet_ni_t *ni, lnet_msg_t *reply, unsigned int len)
2361 /* Set the REPLY length, now the RDMA that elides the REPLY message has
2362 * completed and I know it. */
2363 LASSERT (reply != NULL);
2364 LASSERT (reply->msg_type == LNET_MSG_GET);
2365 LASSERT (reply->msg_ev.type == LNET_EVENT_REPLY);
2367 /* NB I trusted my peer to RDMA. If she tells me she's written beyond
2368 * the end of my buffer, I might as well be dead. */
2369 LASSERT (len <= reply->msg_ev.mlength);
2371 reply->msg_ev.mlength = len;
2373 EXPORT_SYMBOL(lnet_set_reply_msg_len);
2376 * Initiate an asynchronous GET operation.
2378 * On the initiator node, an LNET_EVENT_SEND is logged when the GET request
2379 * is sent, and an LNET_EVENT_REPLY is logged when the data returned from
2380 * the target node in the REPLY has been written to local MD.
2382 * On the target node, an LNET_EVENT_GET is logged when the GET request
2383 * arrives and is accepted into a MD.
2385 * \param self,target,portal,match_bits,offset See the discussion in LNetPut().
2386 * \param mdh A handle for the MD that describes the memory into which the
2387 * requested data will be received. The MD must be "free floating" (See LNetMDBind()).
2389 * \retval 0 Success, and only in this case events will be generated
2390 * and logged to EQ (if it exists) of the MD.
2391 * \retval -EIO Simulated failure.
2392 * \retval -ENOMEM Memory allocation failure.
2393 * \retval -ENOENT Invalid MD object.
2396 LNetGet(lnet_nid_t self, lnet_handle_md_t mdh,
2397 lnet_process_id_t target, unsigned int portal,
2398 __u64 match_bits, unsigned int offset)
2400 struct lnet_msg *msg;
2401 struct lnet_libmd *md;
2405 LASSERT (the_lnet.ln_init);
2406 LASSERT (the_lnet.ln_refcount > 0);
2408 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
2409 fail_peer(target.nid, 1)) /* shall we now? */
2411 CERROR("Dropping GET to %s: simulated failure\n",
2412 libcfs_id2str(target));
2416 msg = lnet_msg_alloc();
2418 CERROR("Dropping GET to %s: ENOMEM on lnet_msg_t\n",
2419 libcfs_id2str(target));
2423 cpt = lnet_cpt_of_cookie(mdh.cookie);
2426 md = lnet_handle2md(&mdh);
2427 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
2428 CERROR("Dropping GET ("LPU64":%d:%s): MD (%d) invalid\n",
2429 match_bits, portal, libcfs_id2str(target),
2430 md == NULL ? -1 : md->md_threshold);
2431 if (md != NULL && md->md_me != NULL)
2432 CERROR("REPLY MD also attached to portal %d\n",
2433 md->md_me->me_portal);
2435 lnet_res_unlock(cpt);
2441 CDEBUG(D_NET, "LNetGet -> %s\n", libcfs_id2str(target));
2443 lnet_msg_attach_md(msg, md, 0, 0);
2445 lnet_prep_send(msg, LNET_MSG_GET, target, 0, 0);
2447 msg->msg_hdr.msg.get.match_bits = cpu_to_le64(match_bits);
2448 msg->msg_hdr.msg.get.ptl_index = cpu_to_le32(portal);
2449 msg->msg_hdr.msg.get.src_offset = cpu_to_le32(offset);
2450 msg->msg_hdr.msg.get.sink_length = cpu_to_le32(md->md_length);
2452 /* NB handles only looked up by creator (no flips) */
2453 msg->msg_hdr.msg.get.return_wmd.wh_interface_cookie =
2454 the_lnet.ln_interface_cookie;
2455 msg->msg_hdr.msg.get.return_wmd.wh_object_cookie =
2456 md->md_lh.lh_cookie;
2458 lnet_res_unlock(cpt);
2460 lnet_build_msg_event(msg, LNET_EVENT_SEND);
2462 rc = lnet_send(self, msg, LNET_NID_ANY);
2464 CNETERR("Error sending GET to %s: %d\n",
2465 libcfs_id2str(target), rc);
2466 lnet_finalize(NULL, msg, rc);
2469 /* completion will be signalled by an event */
2472 EXPORT_SYMBOL(LNetGet);
2475 * Calculate distance to node at \a dstnid.
2477 * \param dstnid Target NID.
2478 * \param srcnidp If not NULL, NID of the local interface to reach \a dstnid
2480 * \param orderp If not NULL, order of the route to reach \a dstnid is saved
2483 * \retval 0 If \a dstnid belongs to a local interface, and reserved option
2484 * local_nid_dist_zero is set, which is the default.
2485 * \retval positives Distance to target NID, i.e. number of hops plus one.
2486 * \retval -EHOSTUNREACH If \a dstnid is not reachable.
2489 LNetDist(lnet_nid_t dstnid, lnet_nid_t *srcnidp, __u32 *orderp)
2491 struct list_head *e;
2493 lnet_remotenet_t *rnet;
2494 __u32 dstnet = LNET_NIDNET(dstnid);
2498 struct list_head *rn_list;
2500 /* if !local_nid_dist_zero, I don't return a distance of 0 ever
2501 * (when lustre sees a distance of 0, it substitutes 0@lo), so I
2502 * keep order 0 free for 0@lo and order 1 free for a local NID
2505 LASSERT (the_lnet.ln_init);
2506 LASSERT (the_lnet.ln_refcount > 0);
2508 cpt = lnet_net_lock_current();
2510 list_for_each(e, &the_lnet.ln_nis) {
2511 ni = list_entry(e, lnet_ni_t, ni_list);
2513 if (ni->ni_nid == dstnid) {
2514 if (srcnidp != NULL)
2516 if (orderp != NULL) {
2517 if (LNET_NETTYP(LNET_NIDNET(dstnid)) == LOLND)
2522 lnet_net_unlock(cpt);
2524 return local_nid_dist_zero ? 0 : 1;
2527 if (LNET_NIDNET(ni->ni_nid) == dstnet) {
2528 if (srcnidp != NULL)
2529 *srcnidp = ni->ni_nid;
2532 lnet_net_unlock(cpt);
2539 rn_list = lnet_net2rnethash(dstnet);
2540 list_for_each(e, rn_list) {
2541 rnet = list_entry(e, lnet_remotenet_t, lrn_list);
2543 if (rnet->lrn_net == dstnet) {
2544 lnet_route_t *route;
2545 lnet_route_t *shortest = NULL;
2547 LASSERT(!list_empty(&rnet->lrn_routes));
2549 list_for_each_entry(route, &rnet->lrn_routes,
2551 if (shortest == NULL ||
2552 route->lr_hops < shortest->lr_hops)
2556 LASSERT (shortest != NULL);
2557 hops = shortest->lr_hops;
2558 if (srcnidp != NULL)
2559 *srcnidp = shortest->lr_gateway->lp_ni->ni_nid;
2562 lnet_net_unlock(cpt);
2568 lnet_net_unlock(cpt);
2569 return -EHOSTUNREACH;
2571 EXPORT_SYMBOL(LNetDist);
2574 * Set the number of asynchronous messages expected from a target process.
2576 * This function is only meaningful for userspace callers. It's a no-op when
2577 * called from kernel.
2579 * Asynchronous messages are those that can come from a target when the
2580 * userspace process is not waiting for IO to complete; e.g., AST callbacks
2581 * from Lustre servers. Specifying the expected number of such messages
2582 * allows them to be eagerly received when user process is not running in
2583 * LNet; otherwise network errors may occur.
2585 * \param id Process ID of the target process.
2586 * \param nasync Number of asynchronous messages expected from the target.
2588 * \return 0 on success, and an error code otherwise.
2591 LNetSetAsync(lnet_process_id_t id, int nasync)
2597 lnet_remotenet_t *rnet;
2598 struct list_head *tmp;
2599 lnet_route_t *route;
2607 /* Target on a local network? */
2608 ni = lnet_net2ni(LNET_NIDNET(id.nid));
2610 if (ni->ni_lnd->lnd_setasync != NULL)
2611 rc = (ni->ni_lnd->lnd_setasync)(ni, id, nasync);
2616 /* Target on a remote network: apply to routers */
2618 LIBCFS_ALLOC(nids, maxnids * sizeof(*nids));
2623 /* Snapshot all the router NIDs */
2624 cpt = lnet_net_lock_current();
2625 rnet = lnet_find_net_locked(LNET_NIDNET(id.nid));
2627 list_for_each(tmp, &rnet->lrn_routes) {
2628 if (nnids == maxnids) {
2629 lnet_net_unlock(cpt);
2630 LIBCFS_FREE(nids, maxnids * sizeof(*nids));
2635 route = list_entry(tmp, lnet_route_t, lr_list);
2636 nids[nnids++] = route->lr_gateway->lp_nid;
2639 lnet_net_unlock(cpt);
2641 /* set async on all the routers */
2642 while (nnids-- > 0) {
2643 id.pid = LUSTRE_SRV_LNET_PID;
2644 id.nid = nids[nnids];
2646 ni = lnet_net2ni(LNET_NIDNET(id.nid));
2650 if (ni->ni_lnd->lnd_setasync != NULL) {
2651 rc2 = (ni->ni_lnd->lnd_setasync)(ni, id, nasync);
2658 LIBCFS_FREE(nids, maxnids * sizeof(*nids));
2662 EXPORT_SYMBOL(LNetSetAsync);