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 static int local_nid_dist_zero = 1;
46 CFS_MODULE_PARM(local_nid_dist_zero, "i", int, 0444,
50 lnet_fail_nid (lnet_nid_t nid, unsigned int threshold)
57 LASSERT (the_lnet.ln_init);
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 cfs_list_add_tail(&tp->tp_list, &the_lnet.ln_test_peers);
75 /* removing entries */
76 CFS_INIT_LIST_HEAD(&cull);
80 cfs_list_for_each_safe (el, next, &the_lnet.ln_test_peers) {
81 tp = cfs_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 */
87 cfs_list_del (&tp->tp_list);
88 cfs_list_add (&tp->tp_list, &cull);
94 while (!cfs_list_empty (&cull)) {
95 tp = cfs_list_entry (cull.next, lnet_test_peer_t, tp_list);
97 cfs_list_del (&tp->tp_list);
98 LIBCFS_FREE(tp, sizeof (*tp));
104 fail_peer (lnet_nid_t nid, int outgoing)
106 lnet_test_peer_t *tp;
112 CFS_INIT_LIST_HEAD (&cull);
114 /* NB: use lnet_net_lock(0) to serialize operations on test peers */
117 cfs_list_for_each_safe (el, next, &the_lnet.ln_test_peers) {
118 tp = cfs_list_entry (el, lnet_test_peer_t, tp_list);
120 if (tp->tp_threshold == 0) {
123 /* only cull zombies on outgoing tests,
124 * since we may be at interrupt priority on
125 * incoming messages. */
126 cfs_list_del (&tp->tp_list);
127 cfs_list_add (&tp->tp_list, &cull);
132 if (tp->tp_nid == LNET_NID_ANY || /* fail every peer */
133 nid == tp->tp_nid) { /* fail this peer */
136 if (tp->tp_threshold != LNET_MD_THRESH_INF) {
139 tp->tp_threshold == 0) {
141 cfs_list_del (&tp->tp_list);
142 cfs_list_add (&tp->tp_list, &cull);
151 while (!cfs_list_empty (&cull)) {
152 tp = cfs_list_entry (cull.next, lnet_test_peer_t, tp_list);
153 cfs_list_del (&tp->tp_list);
155 LIBCFS_FREE(tp, sizeof (*tp));
162 lnet_iov_nob (unsigned int niov, struct iovec *iov)
164 unsigned int nob = 0;
167 nob += (iov++)->iov_len;
171 EXPORT_SYMBOL(lnet_iov_nob);
174 lnet_copy_iov2iov (unsigned int ndiov, struct iovec *diov, unsigned int doffset,
175 unsigned int nsiov, struct iovec *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 iovec *dst,
234 int src_niov, struct iovec *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)
288 lnet_copy_kiov2kiov (unsigned int ndkiov, lnet_kiov_t *dkiov, unsigned int doffset,
289 unsigned int nskiov, lnet_kiov_t *skiov, unsigned int soffset,
296 lnet_copy_kiov2iov (unsigned int niov, struct iovec *iov, unsigned int iovoffset,
297 unsigned int nkiov, lnet_kiov_t *kiov, unsigned int kiovoffset,
304 lnet_copy_iov2kiov (unsigned int nkiov, lnet_kiov_t *kiov, unsigned int kiovoffset,
305 unsigned int niov, struct iovec *iov, unsigned int iovoffset,
312 lnet_extract_kiov (int dst_niov, lnet_kiov_t *dst,
313 int src_niov, lnet_kiov_t *src,
314 unsigned int offset, unsigned int len)
319 #else /* __KERNEL__ */
322 lnet_kiov_nob (unsigned int niov, lnet_kiov_t *kiov)
324 unsigned int nob = 0;
327 nob += (kiov++)->kiov_len;
331 EXPORT_SYMBOL(lnet_kiov_nob);
334 lnet_copy_kiov2kiov (unsigned int ndiov, lnet_kiov_t *diov, unsigned int doffset,
335 unsigned int nsiov, lnet_kiov_t *siov, unsigned int soffset,
338 /* NB diov, siov are READ-ONLY */
339 unsigned int this_nob;
346 LASSERT (!cfs_in_interrupt ());
349 while (doffset >= diov->kiov_len) {
350 doffset -= diov->kiov_len;
357 while (soffset >= siov->kiov_len) {
358 soffset -= siov->kiov_len;
367 this_nob = MIN(diov->kiov_len - doffset,
368 siov->kiov_len - soffset);
369 this_nob = MIN(this_nob, nob);
372 daddr = ((char *)kmap(diov->kiov_page)) +
373 diov->kiov_offset + doffset;
375 saddr = ((char *)kmap(siov->kiov_page)) +
376 siov->kiov_offset + soffset;
378 /* Vanishing risk of kmap deadlock when mapping 2 pages.
379 * However in practice at least one of the kiovs will be mapped
380 * kernel pages and the map/unmap will be NOOPs */
382 memcpy (daddr, saddr, this_nob);
385 if (diov->kiov_len > doffset + this_nob) {
389 kunmap(diov->kiov_page);
396 if (siov->kiov_len > soffset + this_nob) {
400 kunmap(siov->kiov_page);
409 kunmap(diov->kiov_page);
411 kunmap(siov->kiov_page);
413 EXPORT_SYMBOL(lnet_copy_kiov2kiov);
416 lnet_copy_kiov2iov (unsigned int niov, struct iovec *iov, unsigned int iovoffset,
417 unsigned int nkiov, lnet_kiov_t *kiov, unsigned int kiovoffset,
420 /* NB iov, kiov are READ-ONLY */
421 unsigned int this_nob;
427 LASSERT (!cfs_in_interrupt ());
430 while (iovoffset >= iov->iov_len) {
431 iovoffset -= iov->iov_len;
438 while (kiovoffset >= kiov->kiov_len) {
439 kiovoffset -= kiov->kiov_len;
448 this_nob = MIN(iov->iov_len - iovoffset,
449 kiov->kiov_len - kiovoffset);
450 this_nob = MIN(this_nob, nob);
453 addr = ((char *)kmap(kiov->kiov_page)) +
454 kiov->kiov_offset + kiovoffset;
456 memcpy ((char *)iov->iov_base + iovoffset, addr, this_nob);
459 if (iov->iov_len > iovoffset + this_nob) {
460 iovoffset += this_nob;
467 if (kiov->kiov_len > kiovoffset + this_nob) {
469 kiovoffset += this_nob;
471 kunmap(kiov->kiov_page);
481 kunmap(kiov->kiov_page);
483 EXPORT_SYMBOL(lnet_copy_kiov2iov);
486 lnet_copy_iov2kiov (unsigned int nkiov, lnet_kiov_t *kiov, unsigned int kiovoffset,
487 unsigned int niov, struct iovec *iov, unsigned int iovoffset,
490 /* NB kiov, iov are READ-ONLY */
491 unsigned int this_nob;
497 LASSERT (!cfs_in_interrupt ());
500 while (kiovoffset >= kiov->kiov_len) {
501 kiovoffset -= kiov->kiov_len;
508 while (iovoffset >= iov->iov_len) {
509 iovoffset -= iov->iov_len;
518 this_nob = MIN(kiov->kiov_len - kiovoffset,
519 iov->iov_len - iovoffset);
520 this_nob = MIN(this_nob, nob);
523 addr = ((char *)kmap(kiov->kiov_page)) +
524 kiov->kiov_offset + kiovoffset;
526 memcpy (addr, (char *)iov->iov_base + iovoffset, this_nob);
529 if (kiov->kiov_len > kiovoffset + this_nob) {
531 kiovoffset += this_nob;
533 kunmap(kiov->kiov_page);
540 if (iov->iov_len > iovoffset + this_nob) {
541 iovoffset += this_nob;
550 kunmap(kiov->kiov_page);
552 EXPORT_SYMBOL(lnet_copy_iov2kiov);
555 lnet_extract_kiov (int dst_niov, lnet_kiov_t *dst,
556 int src_niov, lnet_kiov_t *src,
557 unsigned int offset, unsigned int len)
559 /* Initialise 'dst' to the subset of 'src' starting at 'offset',
560 * for exactly 'len' bytes, and return the number of entries.
561 * NB not destructive to 'src' */
562 unsigned int frag_len;
565 if (len == 0) /* no data => */
566 return (0); /* no frags */
568 LASSERT (src_niov > 0);
569 while (offset >= src->kiov_len) { /* skip initial frags */
570 offset -= src->kiov_len;
573 LASSERT (src_niov > 0);
578 LASSERT (src_niov > 0);
579 LASSERT ((int)niov <= dst_niov);
581 frag_len = src->kiov_len - offset;
582 dst->kiov_page = src->kiov_page;
583 dst->kiov_offset = src->kiov_offset + offset;
585 if (len <= frag_len) {
587 LASSERT (dst->kiov_offset + dst->kiov_len <= PAGE_CACHE_SIZE);
591 dst->kiov_len = frag_len;
592 LASSERT (dst->kiov_offset + dst->kiov_len <= PAGE_CACHE_SIZE);
602 EXPORT_SYMBOL(lnet_extract_kiov);
606 lnet_ni_recv(lnet_ni_t *ni, void *private, lnet_msg_t *msg, int delayed,
607 unsigned int offset, unsigned int mlen, unsigned int rlen)
609 unsigned int niov = 0;
610 struct iovec *iov = NULL;
611 lnet_kiov_t *kiov = NULL;
614 LASSERT (!cfs_in_interrupt ());
615 LASSERT (mlen == 0 || msg != NULL);
618 LASSERT(msg->msg_receiving);
619 LASSERT(!msg->msg_sending);
620 LASSERT(rlen == msg->msg_len);
621 LASSERT(mlen <= msg->msg_len);
622 LASSERT(msg->msg_offset == offset);
623 LASSERT(msg->msg_wanted == mlen);
625 msg->msg_receiving = 0;
628 niov = msg->msg_niov;
630 kiov = msg->msg_kiov;
633 LASSERT ((iov == NULL) != (kiov == NULL));
637 rc = (ni->ni_lnd->lnd_recv)(ni, private, msg, delayed,
638 niov, iov, kiov, offset, mlen, rlen);
640 lnet_finalize(ni, msg, rc);
644 lnet_setpayloadbuffer(lnet_msg_t *msg)
646 lnet_libmd_t *md = msg->msg_md;
648 LASSERT (msg->msg_len > 0);
649 LASSERT (!msg->msg_routing);
650 LASSERT (md != NULL);
651 LASSERT (msg->msg_niov == 0);
652 LASSERT (msg->msg_iov == NULL);
653 LASSERT (msg->msg_kiov == NULL);
655 msg->msg_niov = md->md_niov;
656 if ((md->md_options & LNET_MD_KIOV) != 0)
657 msg->msg_kiov = md->md_iov.kiov;
659 msg->msg_iov = md->md_iov.iov;
663 lnet_prep_send(lnet_msg_t *msg, int type, lnet_process_id_t target,
664 unsigned int offset, unsigned int len)
666 msg->msg_type = type;
667 msg->msg_target = target;
669 msg->msg_offset = offset;
672 lnet_setpayloadbuffer(msg);
674 memset (&msg->msg_hdr, 0, sizeof (msg->msg_hdr));
675 msg->msg_hdr.type = cpu_to_le32(type);
676 msg->msg_hdr.dest_nid = cpu_to_le64(target.nid);
677 msg->msg_hdr.dest_pid = cpu_to_le32(target.pid);
678 /* src_nid will be set later */
679 msg->msg_hdr.src_pid = cpu_to_le32(the_lnet.ln_pid);
680 msg->msg_hdr.payload_length = cpu_to_le32(len);
684 lnet_ni_send(lnet_ni_t *ni, lnet_msg_t *msg)
686 void *priv = msg->msg_private;
689 LASSERT (!cfs_in_interrupt ());
690 LASSERT (LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND ||
691 (msg->msg_txcredit && msg->msg_peertxcredit));
693 rc = (ni->ni_lnd->lnd_send)(ni, priv, msg);
695 lnet_finalize(ni, msg, rc);
699 lnet_ni_eager_recv(lnet_ni_t *ni, lnet_msg_t *msg)
703 LASSERT(!msg->msg_sending);
704 LASSERT(msg->msg_receiving);
705 LASSERT(!msg->msg_rx_ready_delay);
706 LASSERT(ni->ni_lnd->lnd_eager_recv != NULL);
708 msg->msg_rx_ready_delay = 1;
709 rc = (ni->ni_lnd->lnd_eager_recv)(ni, msg->msg_private, msg,
712 CERROR("recv from %s / send to %s aborted: "
713 "eager_recv failed %d\n",
714 libcfs_nid2str(msg->msg_rxpeer->lp_nid),
715 libcfs_id2str(msg->msg_target), rc);
716 LASSERT(rc < 0); /* required by my callers */
722 /* NB: caller shall hold a ref on 'lp' as I'd drop lnet_net_lock */
724 lnet_ni_query_locked(lnet_ni_t *ni, lnet_peer_t *lp)
726 cfs_time_t last_alive = 0;
728 LASSERT(lnet_peer_aliveness_enabled(lp));
729 LASSERT(ni->ni_lnd->lnd_query != NULL);
731 lnet_net_unlock(lp->lp_cpt);
732 (ni->ni_lnd->lnd_query)(ni, lp->lp_nid, &last_alive);
733 lnet_net_lock(lp->lp_cpt);
735 lp->lp_last_query = cfs_time_current();
737 if (last_alive != 0) /* NI has updated timestamp */
738 lp->lp_last_alive = last_alive;
741 /* NB: always called with lnet_net_lock held */
743 lnet_peer_is_alive (lnet_peer_t *lp, cfs_time_t now)
748 LASSERT (lnet_peer_aliveness_enabled(lp));
750 /* Trust lnet_notify() if it has more recent aliveness news, but
751 * ignore the initial assumed death (see lnet_peers_start_down()).
753 if (!lp->lp_alive && lp->lp_alive_count > 0 &&
754 cfs_time_aftereq(lp->lp_timestamp, lp->lp_last_alive))
757 deadline = cfs_time_add(lp->lp_last_alive,
758 cfs_time_seconds(lp->lp_ni->ni_peertimeout));
759 alive = cfs_time_after(deadline, now);
761 /* Update obsolete lp_alive except for routers assumed to be dead
762 * initially, because router checker would update aliveness in this
763 * case, and moreover lp_last_alive at peer creation is assumed.
765 if (alive && !lp->lp_alive &&
766 !(lnet_isrouter(lp) && lp->lp_alive_count == 0))
767 lnet_notify_locked(lp, 0, 1, lp->lp_last_alive);
773 /* NB: returns 1 when alive, 0 when dead, negative when error;
774 * may drop the lnet_net_lock */
776 lnet_peer_alive_locked (lnet_peer_t *lp)
778 cfs_time_t now = cfs_time_current();
780 if (!lnet_peer_aliveness_enabled(lp))
783 if (lnet_peer_is_alive(lp, now))
786 /* Peer appears dead, but we should avoid frequent NI queries (at
787 * most once per lnet_queryinterval seconds). */
788 if (lp->lp_last_query != 0) {
789 static const int lnet_queryinterval = 1;
791 cfs_time_t next_query =
792 cfs_time_add(lp->lp_last_query,
793 cfs_time_seconds(lnet_queryinterval));
795 if (cfs_time_before(now, next_query)) {
797 CWARN("Unexpected aliveness of peer %s: "
799 libcfs_nid2str(lp->lp_nid),
800 (int)now, (int)next_query,
802 lp->lp_ni->ni_peertimeout);
807 /* query NI for latest aliveness news */
808 lnet_ni_query_locked(lp->lp_ni, lp);
810 if (lnet_peer_is_alive(lp, now))
813 lnet_notify_locked(lp, 0, 0, lp->lp_last_alive);
818 lnet_post_send_locked(lnet_msg_t *msg, int do_send)
820 /* lnet_send is going to lnet_net_unlock immediately after this,
821 * so it sets do_send FALSE and I don't do the unlock/send/lock bit.
822 * I return EAGAIN if msg blocked, EHOSTUNREACH if msg_txpeer
823 * appears dead, and 0 if sent or OK to send */
824 struct lnet_peer *lp = msg->msg_txpeer;
825 struct lnet_ni *ni = lp->lp_ni;
826 struct lnet_tx_queue *tq;
829 /* non-lnet_send() callers have checked before */
830 LASSERT(!do_send || msg->msg_tx_delayed);
831 LASSERT(!msg->msg_receiving);
832 LASSERT(msg->msg_tx_committed);
834 cpt = msg->msg_tx_cpt;
835 tq = ni->ni_tx_queues[cpt];
837 /* NB 'lp' is always the next hop */
838 if ((msg->msg_target.pid & LNET_PID_USERFLAG) == 0 &&
839 lnet_peer_alive_locked(lp) == 0) {
840 the_lnet.ln_counters[cpt]->drop_count++;
841 the_lnet.ln_counters[cpt]->drop_length += msg->msg_len;
842 lnet_net_unlock(cpt);
844 CNETERR("Dropping message for %s: peer not alive\n",
845 libcfs_id2str(msg->msg_target));
847 lnet_finalize(ni, msg, -EHOSTUNREACH);
853 if (!msg->msg_peertxcredit) {
854 LASSERT ((lp->lp_txcredits < 0) ==
855 !cfs_list_empty(&lp->lp_txq));
857 msg->msg_peertxcredit = 1;
858 lp->lp_txqnob += msg->msg_len + sizeof(lnet_hdr_t);
861 if (lp->lp_txcredits < lp->lp_mintxcredits)
862 lp->lp_mintxcredits = lp->lp_txcredits;
864 if (lp->lp_txcredits < 0) {
865 msg->msg_tx_delayed = 1;
866 cfs_list_add_tail(&msg->msg_list, &lp->lp_txq);
871 if (!msg->msg_txcredit) {
872 LASSERT((tq->tq_credits < 0) ==
873 !cfs_list_empty(&tq->tq_delayed));
875 msg->msg_txcredit = 1;
878 if (tq->tq_credits < tq->tq_credits_min)
879 tq->tq_credits_min = tq->tq_credits;
881 if (tq->tq_credits < 0) {
882 msg->msg_tx_delayed = 1;
883 cfs_list_add_tail(&msg->msg_list, &tq->tq_delayed);
889 lnet_net_unlock(cpt);
890 lnet_ni_send(ni, msg);
899 lnet_msg2bufpool(lnet_msg_t *msg)
901 lnet_rtrbufpool_t *rbp;
904 LASSERT(msg->msg_rx_committed);
906 cpt = msg->msg_rx_cpt;
907 rbp = &the_lnet.ln_rtrpools[cpt][0];
909 LASSERT(msg->msg_len <= LNET_MTU);
910 while (msg->msg_len > (unsigned int)rbp->rbp_npages * PAGE_CACHE_SIZE) {
912 LASSERT(rbp < &the_lnet.ln_rtrpools[cpt][LNET_NRBPOOLS]);
919 lnet_post_routed_recv_locked (lnet_msg_t *msg, int do_recv)
921 /* lnet_parse is going to lnet_net_unlock immediately after this, so it
922 * sets do_recv FALSE and I don't do the unlock/send/lock bit. I
923 * return EAGAIN if msg blocked and 0 if received or OK to receive */
924 lnet_peer_t *lp = msg->msg_rxpeer;
925 lnet_rtrbufpool_t *rbp;
928 LASSERT (msg->msg_iov == NULL);
929 LASSERT (msg->msg_kiov == NULL);
930 LASSERT (msg->msg_niov == 0);
931 LASSERT (msg->msg_routing);
932 LASSERT (msg->msg_receiving);
933 LASSERT (!msg->msg_sending);
935 /* non-lnet_parse callers only receive delayed messages */
936 LASSERT(!do_recv || msg->msg_rx_delayed);
938 if (!msg->msg_peerrtrcredit) {
939 LASSERT ((lp->lp_rtrcredits < 0) ==
940 !cfs_list_empty(&lp->lp_rtrq));
942 msg->msg_peerrtrcredit = 1;
944 if (lp->lp_rtrcredits < lp->lp_minrtrcredits)
945 lp->lp_minrtrcredits = lp->lp_rtrcredits;
947 if (lp->lp_rtrcredits < 0) {
948 /* must have checked eager_recv before here */
949 LASSERT(msg->msg_rx_ready_delay);
950 msg->msg_rx_delayed = 1;
951 cfs_list_add_tail(&msg->msg_list, &lp->lp_rtrq);
956 rbp = lnet_msg2bufpool(msg);
958 if (!msg->msg_rtrcredit) {
959 LASSERT ((rbp->rbp_credits < 0) ==
960 !cfs_list_empty(&rbp->rbp_msgs));
962 msg->msg_rtrcredit = 1;
964 if (rbp->rbp_credits < rbp->rbp_mincredits)
965 rbp->rbp_mincredits = rbp->rbp_credits;
967 if (rbp->rbp_credits < 0) {
968 /* must have checked eager_recv before here */
969 LASSERT(msg->msg_rx_ready_delay);
970 msg->msg_rx_delayed = 1;
971 cfs_list_add_tail(&msg->msg_list, &rbp->rbp_msgs);
976 LASSERT (!cfs_list_empty(&rbp->rbp_bufs));
977 rb = cfs_list_entry(rbp->rbp_bufs.next, lnet_rtrbuf_t, rb_list);
978 cfs_list_del(&rb->rb_list);
980 msg->msg_niov = rbp->rbp_npages;
981 msg->msg_kiov = &rb->rb_kiov[0];
984 int cpt = msg->msg_rx_cpt;
986 lnet_net_unlock(cpt);
987 lnet_ni_recv(lp->lp_ni, msg->msg_private, msg, 1,
988 0, msg->msg_len, msg->msg_len);
996 lnet_return_tx_credits_locked(lnet_msg_t *msg)
998 lnet_peer_t *txpeer = msg->msg_txpeer;
1001 if (msg->msg_txcredit) {
1002 struct lnet_ni *ni = txpeer->lp_ni;
1003 struct lnet_tx_queue *tq = ni->ni_tx_queues[msg->msg_tx_cpt];
1005 /* give back NI txcredits */
1006 msg->msg_txcredit = 0;
1008 LASSERT((tq->tq_credits < 0) ==
1009 !cfs_list_empty(&tq->tq_delayed));
1012 if (tq->tq_credits <= 0) {
1013 msg2 = cfs_list_entry(tq->tq_delayed.next,
1014 lnet_msg_t, msg_list);
1015 cfs_list_del(&msg2->msg_list);
1017 LASSERT(msg2->msg_txpeer->lp_ni == ni);
1018 LASSERT(msg2->msg_tx_delayed);
1020 (void) lnet_post_send_locked(msg2, 1);
1024 if (msg->msg_peertxcredit) {
1025 /* give back peer txcredits */
1026 msg->msg_peertxcredit = 0;
1028 LASSERT((txpeer->lp_txcredits < 0) ==
1029 !cfs_list_empty(&txpeer->lp_txq));
1031 txpeer->lp_txqnob -= msg->msg_len + sizeof(lnet_hdr_t);
1032 LASSERT (txpeer->lp_txqnob >= 0);
1034 txpeer->lp_txcredits++;
1035 if (txpeer->lp_txcredits <= 0) {
1036 msg2 = cfs_list_entry(txpeer->lp_txq.next,
1037 lnet_msg_t, msg_list);
1038 cfs_list_del(&msg2->msg_list);
1040 LASSERT(msg2->msg_txpeer == txpeer);
1041 LASSERT(msg2->msg_tx_delayed);
1043 (void) lnet_post_send_locked(msg2, 1);
1047 if (txpeer != NULL) {
1048 msg->msg_txpeer = NULL;
1049 lnet_peer_decref_locked(txpeer);
1054 lnet_return_rx_credits_locked(lnet_msg_t *msg)
1056 lnet_peer_t *rxpeer = msg->msg_rxpeer;
1060 if (msg->msg_rtrcredit) {
1061 /* give back global router credits */
1063 lnet_rtrbufpool_t *rbp;
1065 /* NB If a msg ever blocks for a buffer in rbp_msgs, it stays
1066 * there until it gets one allocated, or aborts the wait
1068 LASSERT (msg->msg_kiov != NULL);
1070 rb = cfs_list_entry(msg->msg_kiov, lnet_rtrbuf_t, rb_kiov[0]);
1072 LASSERT (rbp == lnet_msg2bufpool(msg));
1074 msg->msg_kiov = NULL;
1075 msg->msg_rtrcredit = 0;
1077 LASSERT((rbp->rbp_credits < 0) ==
1078 !cfs_list_empty(&rbp->rbp_msgs));
1079 LASSERT((rbp->rbp_credits > 0) ==
1080 !cfs_list_empty(&rbp->rbp_bufs));
1082 cfs_list_add(&rb->rb_list, &rbp->rbp_bufs);
1084 if (rbp->rbp_credits <= 0) {
1085 msg2 = cfs_list_entry(rbp->rbp_msgs.next,
1086 lnet_msg_t, msg_list);
1087 cfs_list_del(&msg2->msg_list);
1089 (void) lnet_post_routed_recv_locked(msg2, 1);
1093 if (msg->msg_peerrtrcredit) {
1094 /* give back peer router credits */
1095 msg->msg_peerrtrcredit = 0;
1097 LASSERT((rxpeer->lp_rtrcredits < 0) ==
1098 !cfs_list_empty(&rxpeer->lp_rtrq));
1100 rxpeer->lp_rtrcredits++;
1101 if (rxpeer->lp_rtrcredits <= 0) {
1102 msg2 = cfs_list_entry(rxpeer->lp_rtrq.next,
1103 lnet_msg_t, msg_list);
1104 cfs_list_del(&msg2->msg_list);
1106 (void) lnet_post_routed_recv_locked(msg2, 1);
1110 LASSERT (!msg->msg_rtrcredit);
1111 LASSERT (!msg->msg_peerrtrcredit);
1113 if (rxpeer != NULL) {
1114 msg->msg_rxpeer = NULL;
1115 lnet_peer_decref_locked(rxpeer);
1120 lnet_compare_routes(lnet_route_t *r1, lnet_route_t *r2)
1122 lnet_peer_t *p1 = r1->lr_gateway;
1123 lnet_peer_t *p2 = r2->lr_gateway;
1125 if (r1->lr_priority < r2->lr_priority)
1128 if (r1->lr_priority > r2->lr_priority)
1131 if (r1->lr_hops < r2->lr_hops)
1134 if (r1->lr_hops > r2->lr_hops)
1137 if (p1->lp_txqnob < p2->lp_txqnob)
1140 if (p1->lp_txqnob > p2->lp_txqnob)
1143 if (p1->lp_txcredits > p2->lp_txcredits)
1146 if (p1->lp_txcredits < p2->lp_txcredits)
1149 if (r1->lr_seq - r2->lr_seq <= 0)
1155 static lnet_peer_t *
1156 lnet_find_route_locked(lnet_ni_t *ni, lnet_nid_t target, lnet_nid_t rtr_nid)
1158 lnet_remotenet_t *rnet;
1160 lnet_route_t *rtr_best;
1161 lnet_route_t *rtr_last;
1162 struct lnet_peer *lp_best;
1163 struct lnet_peer *lp;
1166 /* If @rtr_nid is not LNET_NID_ANY, return the gateway with
1167 * rtr_nid nid, otherwise find the best gateway I can use */
1169 rnet = lnet_find_net_locked(LNET_NIDNET(target));
1174 rtr_best = rtr_last = NULL;
1175 cfs_list_for_each_entry(rtr, &rnet->lrn_routes, lr_list) {
1176 lp = rtr->lr_gateway;
1178 if (!lp->lp_alive || /* gateway is down */
1179 ((lp->lp_ping_feats & LNET_PING_FEAT_NI_STATUS) != 0 &&
1180 rtr->lr_downis != 0)) /* NI to target is down */
1183 if (ni != NULL && lp->lp_ni != ni)
1186 if (lp->lp_nid == rtr_nid) /* it's pre-determined router */
1189 if (lp_best == NULL) {
1190 rtr_best = rtr_last = rtr;
1195 /* no protection on below fields, but it's harmless */
1196 if (rtr_last->lr_seq - rtr->lr_seq < 0)
1199 rc = lnet_compare_routes(rtr, rtr_best);
1207 /* set sequence number on the best router to the latest sequence + 1
1208 * so we can round-robin all routers, it's race and inaccurate but
1209 * harmless and functional */
1210 if (rtr_best != NULL)
1211 rtr_best->lr_seq = rtr_last->lr_seq + 1;
1216 lnet_send(lnet_nid_t src_nid, lnet_msg_t *msg, lnet_nid_t rtr_nid)
1218 lnet_nid_t dst_nid = msg->msg_target.nid;
1219 struct lnet_ni *src_ni;
1220 struct lnet_ni *local_ni;
1221 struct lnet_peer *lp;
1226 /* NB: rtr_nid is set to LNET_NID_ANY for all current use-cases,
1227 * but we might want to use pre-determined router for ACK/REPLY
1229 /* NB: ni != NULL == interface pre-determined (ACK/REPLY) */
1230 LASSERT (msg->msg_txpeer == NULL);
1231 LASSERT (!msg->msg_sending);
1232 LASSERT (!msg->msg_target_is_router);
1233 LASSERT (!msg->msg_receiving);
1235 msg->msg_sending = 1;
1237 LASSERT(!msg->msg_tx_committed);
1238 cpt = lnet_cpt_of_nid(rtr_nid == LNET_NID_ANY ? dst_nid : rtr_nid);
1242 if (the_lnet.ln_shutdown) {
1243 lnet_net_unlock(cpt);
1247 if (src_nid == LNET_NID_ANY) {
1250 src_ni = lnet_nid2ni_locked(src_nid, cpt);
1251 if (src_ni == NULL) {
1252 lnet_net_unlock(cpt);
1253 LCONSOLE_WARN("Can't send to %s: src %s is not a "
1254 "local nid\n", libcfs_nid2str(dst_nid),
1255 libcfs_nid2str(src_nid));
1258 LASSERT (!msg->msg_routing);
1261 /* Is this for someone on a local network? */
1262 local_ni = lnet_net2ni_locked(LNET_NIDNET(dst_nid), cpt);
1264 if (local_ni != NULL) {
1265 if (src_ni == NULL) {
1267 src_nid = src_ni->ni_nid;
1268 } else if (src_ni == local_ni) {
1269 lnet_ni_decref_locked(local_ni, cpt);
1271 lnet_ni_decref_locked(local_ni, cpt);
1272 lnet_ni_decref_locked(src_ni, cpt);
1273 lnet_net_unlock(cpt);
1274 LCONSOLE_WARN("No route to %s via from %s\n",
1275 libcfs_nid2str(dst_nid),
1276 libcfs_nid2str(src_nid));
1280 LASSERT(src_nid != LNET_NID_ANY);
1281 lnet_msg_commit(msg, cpt);
1283 if (!msg->msg_routing)
1284 msg->msg_hdr.src_nid = cpu_to_le64(src_nid);
1286 if (src_ni == the_lnet.ln_loni) {
1287 /* No send credit hassles with LOLND */
1288 lnet_net_unlock(cpt);
1289 lnet_ni_send(src_ni, msg);
1292 lnet_ni_decref_locked(src_ni, cpt);
1293 lnet_net_unlock(cpt);
1297 rc = lnet_nid2peer_locked(&lp, dst_nid, cpt);
1298 /* lp has ref on src_ni; lose mine */
1299 lnet_ni_decref_locked(src_ni, cpt);
1301 lnet_net_unlock(cpt);
1302 LCONSOLE_WARN("Error %d finding peer %s\n", rc,
1303 libcfs_nid2str(dst_nid));
1304 /* ENOMEM or shutting down */
1307 LASSERT (lp->lp_ni == src_ni);
1310 lnet_net_unlock(cpt);
1313 * - once application finishes computation, check here to update
1314 * router states before it waits for pending IO in LNetEQPoll
1315 * - recursion breaker: router checker sends no message
1316 * to remote networks */
1317 if (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING)
1318 lnet_router_checker();
1322 /* sending to a remote network */
1323 lp = lnet_find_route_locked(src_ni, dst_nid, rtr_nid);
1326 lnet_ni_decref_locked(src_ni, cpt);
1327 lnet_net_unlock(cpt);
1329 LCONSOLE_WARN("No route to %s via %s "
1330 "(all routers down)\n",
1331 libcfs_id2str(msg->msg_target),
1332 libcfs_nid2str(src_nid));
1333 return -EHOSTUNREACH;
1336 /* rtr_nid is LNET_NID_ANY or NID of pre-determined router,
1337 * it's possible that rtr_nid isn't LNET_NID_ANY and lp isn't
1338 * pre-determined router, this can happen if router table
1339 * was changed when we release the lock */
1340 if (rtr_nid != lp->lp_nid) {
1341 cpt2 = lnet_cpt_of_nid_locked(lp->lp_nid);
1344 lnet_ni_decref_locked(src_ni, cpt);
1345 lnet_net_unlock(cpt);
1347 rtr_nid = lp->lp_nid;
1353 CDEBUG(D_NET, "Best route to %s via %s for %s %d\n",
1354 libcfs_nid2str(dst_nid), libcfs_nid2str(lp->lp_nid),
1355 lnet_msgtyp2str(msg->msg_type), msg->msg_len);
1357 if (src_ni == NULL) {
1359 src_nid = src_ni->ni_nid;
1361 LASSERT (src_ni == lp->lp_ni);
1362 lnet_ni_decref_locked(src_ni, cpt);
1365 lnet_peer_addref_locked(lp);
1367 LASSERT(src_nid != LNET_NID_ANY);
1368 lnet_msg_commit(msg, cpt);
1370 if (!msg->msg_routing) {
1371 /* I'm the source and now I know which NI to send on */
1372 msg->msg_hdr.src_nid = cpu_to_le64(src_nid);
1375 msg->msg_target_is_router = 1;
1376 msg->msg_target.nid = lp->lp_nid;
1377 msg->msg_target.pid = LUSTRE_SRV_LNET_PID;
1380 /* 'lp' is our best choice of peer */
1382 LASSERT (!msg->msg_peertxcredit);
1383 LASSERT (!msg->msg_txcredit);
1384 LASSERT (msg->msg_txpeer == NULL);
1386 msg->msg_txpeer = lp; /* msg takes my ref on lp */
1388 rc = lnet_post_send_locked(msg, 0);
1389 lnet_net_unlock(cpt);
1391 if (rc == EHOSTUNREACH)
1392 return -EHOSTUNREACH;
1395 lnet_ni_send(src_ni, msg);
1401 lnet_drop_message(lnet_ni_t *ni, int cpt, void *private, unsigned int nob)
1404 the_lnet.ln_counters[cpt]->drop_count++;
1405 the_lnet.ln_counters[cpt]->drop_length += nob;
1406 lnet_net_unlock(cpt);
1408 lnet_ni_recv(ni, private, NULL, 0, 0, 0, nob);
1412 lnet_recv_put(lnet_ni_t *ni, lnet_msg_t *msg)
1414 lnet_hdr_t *hdr = &msg->msg_hdr;
1416 if (msg->msg_wanted != 0)
1417 lnet_setpayloadbuffer(msg);
1419 lnet_build_msg_event(msg, LNET_EVENT_PUT);
1421 /* Must I ACK? If so I'll grab the ack_wmd out of the header and put
1422 * it back into the ACK during lnet_finalize() */
1423 msg->msg_ack = (!lnet_is_wire_handle_none(&hdr->msg.put.ack_wmd) &&
1424 (msg->msg_md->md_options & LNET_MD_ACK_DISABLE) == 0);
1426 lnet_ni_recv(ni, msg->msg_private, msg, msg->msg_rx_delayed,
1427 msg->msg_offset, msg->msg_wanted, hdr->payload_length);
1431 lnet_parse_put(lnet_ni_t *ni, lnet_msg_t *msg)
1433 lnet_hdr_t *hdr = &msg->msg_hdr;
1434 struct lnet_match_info info;
1437 /* Convert put fields to host byte order */
1438 hdr->msg.put.match_bits = le64_to_cpu(hdr->msg.put.match_bits);
1439 hdr->msg.put.ptl_index = le32_to_cpu(hdr->msg.put.ptl_index);
1440 hdr->msg.put.offset = le32_to_cpu(hdr->msg.put.offset);
1442 info.mi_id.nid = hdr->src_nid;
1443 info.mi_id.pid = hdr->src_pid;
1444 info.mi_opc = LNET_MD_OP_PUT;
1445 info.mi_portal = hdr->msg.put.ptl_index;
1446 info.mi_rlength = hdr->payload_length;
1447 info.mi_roffset = hdr->msg.put.offset;
1448 info.mi_mbits = hdr->msg.put.match_bits;
1450 msg->msg_rx_ready_delay = ni->ni_lnd->lnd_eager_recv == NULL;
1453 rc = lnet_ptl_match_md(&info, msg);
1458 case LNET_MATCHMD_OK:
1459 lnet_recv_put(ni, msg);
1462 case LNET_MATCHMD_NONE:
1463 if (msg->msg_rx_delayed) /* attached on delayed list */
1466 rc = lnet_ni_eager_recv(ni, msg);
1471 case LNET_MATCHMD_DROP:
1472 CNETERR("Dropping PUT from %s portal %d match "LPU64
1473 " offset %d length %d: %d\n",
1474 libcfs_id2str(info.mi_id), info.mi_portal,
1475 info.mi_mbits, info.mi_roffset, info.mi_rlength, rc);
1477 return ENOENT; /* +ve: OK but no match */
1482 lnet_parse_get(lnet_ni_t *ni, lnet_msg_t *msg, int rdma_get)
1484 struct lnet_match_info info;
1485 lnet_hdr_t *hdr = &msg->msg_hdr;
1486 lnet_handle_wire_t reply_wmd;
1489 /* Convert get fields to host byte order */
1490 hdr->msg.get.match_bits = le64_to_cpu(hdr->msg.get.match_bits);
1491 hdr->msg.get.ptl_index = le32_to_cpu(hdr->msg.get.ptl_index);
1492 hdr->msg.get.sink_length = le32_to_cpu(hdr->msg.get.sink_length);
1493 hdr->msg.get.src_offset = le32_to_cpu(hdr->msg.get.src_offset);
1495 info.mi_id.nid = hdr->src_nid;
1496 info.mi_id.pid = hdr->src_pid;
1497 info.mi_opc = LNET_MD_OP_GET;
1498 info.mi_portal = hdr->msg.get.ptl_index;
1499 info.mi_rlength = hdr->msg.get.sink_length;
1500 info.mi_roffset = hdr->msg.get.src_offset;
1501 info.mi_mbits = hdr->msg.get.match_bits;
1503 rc = lnet_ptl_match_md(&info, msg);
1504 if (rc == LNET_MATCHMD_DROP) {
1505 CNETERR("Dropping GET from %s portal %d match "LPU64
1506 " offset %d length %d\n",
1507 libcfs_id2str(info.mi_id), info.mi_portal,
1508 info.mi_mbits, info.mi_roffset, info.mi_rlength);
1509 return ENOENT; /* +ve: OK but no match */
1512 LASSERT(rc == LNET_MATCHMD_OK);
1514 lnet_build_msg_event(msg, LNET_EVENT_GET);
1516 reply_wmd = hdr->msg.get.return_wmd;
1518 lnet_prep_send(msg, LNET_MSG_REPLY, info.mi_id,
1519 msg->msg_offset, msg->msg_wanted);
1521 msg->msg_hdr.msg.reply.dst_wmd = reply_wmd;
1524 /* The LND completes the REPLY from her recv procedure */
1525 lnet_ni_recv(ni, msg->msg_private, msg, 0,
1526 msg->msg_offset, msg->msg_len, msg->msg_len);
1530 lnet_ni_recv(ni, msg->msg_private, NULL, 0, 0, 0, 0);
1531 msg->msg_receiving = 0;
1533 rc = lnet_send(ni->ni_nid, msg, LNET_NID_ANY);
1535 /* didn't get as far as lnet_ni_send() */
1536 CERROR("%s: Unable to send REPLY for GET from %s: %d\n",
1537 libcfs_nid2str(ni->ni_nid),
1538 libcfs_id2str(info.mi_id), rc);
1540 lnet_finalize(ni, msg, rc);
1547 lnet_parse_reply(lnet_ni_t *ni, lnet_msg_t *msg)
1549 void *private = msg->msg_private;
1550 lnet_hdr_t *hdr = &msg->msg_hdr;
1551 lnet_process_id_t src = {0};
1557 cpt = lnet_cpt_of_cookie(hdr->msg.reply.dst_wmd.wh_object_cookie);
1560 src.nid = hdr->src_nid;
1561 src.pid = hdr->src_pid;
1563 /* NB handles only looked up by creator (no flips) */
1564 md = lnet_wire_handle2md(&hdr->msg.reply.dst_wmd);
1565 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
1566 CNETERR("%s: Dropping REPLY from %s for %s "
1567 "MD "LPX64"."LPX64"\n",
1568 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1569 (md == NULL) ? "invalid" : "inactive",
1570 hdr->msg.reply.dst_wmd.wh_interface_cookie,
1571 hdr->msg.reply.dst_wmd.wh_object_cookie);
1572 if (md != NULL && md->md_me != NULL)
1573 CERROR("REPLY MD also attached to portal %d\n",
1574 md->md_me->me_portal);
1576 lnet_res_unlock(cpt);
1577 return ENOENT; /* +ve: OK but no match */
1580 LASSERT (md->md_offset == 0);
1582 rlength = hdr->payload_length;
1583 mlength = MIN(rlength, (int)md->md_length);
1585 if (mlength < rlength &&
1586 (md->md_options & LNET_MD_TRUNCATE) == 0) {
1587 CNETERR("%s: Dropping REPLY from %s length %d "
1588 "for MD "LPX64" would overflow (%d)\n",
1589 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1590 rlength, hdr->msg.reply.dst_wmd.wh_object_cookie,
1592 lnet_res_unlock(cpt);
1593 return ENOENT; /* +ve: OK but no match */
1596 CDEBUG(D_NET, "%s: Reply from %s of length %d/%d into md "LPX64"\n",
1597 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1598 mlength, rlength, hdr->msg.reply.dst_wmd.wh_object_cookie);
1600 lnet_msg_attach_md(msg, md, 0, mlength);
1603 lnet_setpayloadbuffer(msg);
1605 lnet_res_unlock(cpt);
1607 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
1609 lnet_ni_recv(ni, private, msg, 0, 0, mlength, rlength);
1614 lnet_parse_ack(lnet_ni_t *ni, lnet_msg_t *msg)
1616 lnet_hdr_t *hdr = &msg->msg_hdr;
1617 lnet_process_id_t src = {0};
1621 src.nid = hdr->src_nid;
1622 src.pid = hdr->src_pid;
1624 /* Convert ack fields to host byte order */
1625 hdr->msg.ack.match_bits = le64_to_cpu(hdr->msg.ack.match_bits);
1626 hdr->msg.ack.mlength = le32_to_cpu(hdr->msg.ack.mlength);
1628 cpt = lnet_cpt_of_cookie(hdr->msg.ack.dst_wmd.wh_object_cookie);
1631 /* NB handles only looked up by creator (no flips) */
1632 md = lnet_wire_handle2md(&hdr->msg.ack.dst_wmd);
1633 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
1634 /* Don't moan; this is expected */
1636 "%s: Dropping ACK from %s to %s MD "LPX64"."LPX64"\n",
1637 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1638 (md == NULL) ? "invalid" : "inactive",
1639 hdr->msg.ack.dst_wmd.wh_interface_cookie,
1640 hdr->msg.ack.dst_wmd.wh_object_cookie);
1641 if (md != NULL && md->md_me != NULL)
1642 CERROR("Source MD also attached to portal %d\n",
1643 md->md_me->me_portal);
1645 lnet_res_unlock(cpt);
1646 return ENOENT; /* +ve! */
1649 CDEBUG(D_NET, "%s: ACK from %s into md "LPX64"\n",
1650 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1651 hdr->msg.ack.dst_wmd.wh_object_cookie);
1653 lnet_msg_attach_md(msg, md, 0, 0);
1655 lnet_res_unlock(cpt);
1657 lnet_build_msg_event(msg, LNET_EVENT_ACK);
1659 lnet_ni_recv(ni, msg->msg_private, msg, 0, 0, 0, msg->msg_len);
1664 lnet_parse_forward_locked(lnet_ni_t *ni, lnet_msg_t *msg)
1669 if (msg->msg_rxpeer->lp_rtrcredits <= 0 ||
1670 lnet_msg2bufpool(msg)->rbp_credits <= 0) {
1671 if (ni->ni_lnd->lnd_eager_recv == NULL) {
1672 msg->msg_rx_ready_delay = 1;
1674 lnet_net_unlock(msg->msg_rx_cpt);
1675 rc = lnet_ni_eager_recv(ni, msg);
1676 lnet_net_lock(msg->msg_rx_cpt);
1681 rc = lnet_post_routed_recv_locked(msg, 0);
1689 lnet_msgtyp2str (int type)
1698 case LNET_MSG_REPLY:
1700 case LNET_MSG_HELLO:
1703 return ("<UNKNOWN>");
1706 EXPORT_SYMBOL(lnet_msgtyp2str);
1709 lnet_print_hdr(lnet_hdr_t * hdr)
1711 lnet_process_id_t src = {0};
1712 lnet_process_id_t dst = {0};
1713 char *type_str = lnet_msgtyp2str (hdr->type);
1715 src.nid = hdr->src_nid;
1716 src.pid = hdr->src_pid;
1718 dst.nid = hdr->dest_nid;
1719 dst.pid = hdr->dest_pid;
1721 CWARN("P3 Header at %p of type %s\n", hdr, type_str);
1722 CWARN(" From %s\n", libcfs_id2str(src));
1723 CWARN(" To %s\n", libcfs_id2str(dst));
1725 switch (hdr->type) {
1730 CWARN(" Ptl index %d, ack md "LPX64"."LPX64", "
1731 "match bits "LPU64"\n",
1732 hdr->msg.put.ptl_index,
1733 hdr->msg.put.ack_wmd.wh_interface_cookie,
1734 hdr->msg.put.ack_wmd.wh_object_cookie,
1735 hdr->msg.put.match_bits);
1736 CWARN(" Length %d, offset %d, hdr data "LPX64"\n",
1737 hdr->payload_length, hdr->msg.put.offset,
1738 hdr->msg.put.hdr_data);
1742 CWARN(" Ptl index %d, return md "LPX64"."LPX64", "
1743 "match bits "LPU64"\n", hdr->msg.get.ptl_index,
1744 hdr->msg.get.return_wmd.wh_interface_cookie,
1745 hdr->msg.get.return_wmd.wh_object_cookie,
1746 hdr->msg.get.match_bits);
1747 CWARN(" Length %d, src offset %d\n",
1748 hdr->msg.get.sink_length,
1749 hdr->msg.get.src_offset);
1753 CWARN(" dst md "LPX64"."LPX64", "
1754 "manipulated length %d\n",
1755 hdr->msg.ack.dst_wmd.wh_interface_cookie,
1756 hdr->msg.ack.dst_wmd.wh_object_cookie,
1757 hdr->msg.ack.mlength);
1760 case LNET_MSG_REPLY:
1761 CWARN(" dst md "LPX64"."LPX64", "
1763 hdr->msg.reply.dst_wmd.wh_interface_cookie,
1764 hdr->msg.reply.dst_wmd.wh_object_cookie,
1765 hdr->payload_length);
1771 lnet_parse(lnet_ni_t *ni, lnet_hdr_t *hdr, lnet_nid_t from_nid,
1772 void *private, int rdma_req)
1777 struct lnet_msg *msg;
1778 lnet_pid_t dest_pid;
1779 lnet_nid_t dest_nid;
1781 __u32 payload_length;
1784 LASSERT (!cfs_in_interrupt ());
1786 type = le32_to_cpu(hdr->type);
1787 src_nid = le64_to_cpu(hdr->src_nid);
1788 dest_nid = le64_to_cpu(hdr->dest_nid);
1789 dest_pid = le32_to_cpu(hdr->dest_pid);
1790 payload_length = le32_to_cpu(hdr->payload_length);
1792 for_me = (ni->ni_nid == dest_nid);
1793 cpt = lnet_cpt_of_nid(from_nid);
1798 if (payload_length > 0) {
1799 CERROR("%s, src %s: bad %s payload %d (0 expected)\n",
1800 libcfs_nid2str(from_nid),
1801 libcfs_nid2str(src_nid),
1802 lnet_msgtyp2str(type), payload_length);
1808 case LNET_MSG_REPLY:
1809 if (payload_length > (__u32)(for_me ? LNET_MAX_PAYLOAD : LNET_MTU)) {
1810 CERROR("%s, src %s: bad %s payload %d "
1811 "(%d max expected)\n",
1812 libcfs_nid2str(from_nid),
1813 libcfs_nid2str(src_nid),
1814 lnet_msgtyp2str(type),
1816 for_me ? LNET_MAX_PAYLOAD : LNET_MTU);
1822 CERROR("%s, src %s: Bad message type 0x%x\n",
1823 libcfs_nid2str(from_nid),
1824 libcfs_nid2str(src_nid), type);
1828 if (the_lnet.ln_routing &&
1829 ni->ni_last_alive != cfs_time_current_sec()) {
1832 /* NB: so far here is the only place to set NI status to "up */
1833 ni->ni_last_alive = cfs_time_current_sec();
1834 if (ni->ni_status != NULL &&
1835 ni->ni_status->ns_status == LNET_NI_STATUS_DOWN)
1836 ni->ni_status->ns_status = LNET_NI_STATUS_UP;
1840 /* Regard a bad destination NID as a protocol error. Senders should
1841 * know what they're doing; if they don't they're misconfigured, buggy
1842 * or malicious so we chop them off at the knees :) */
1845 if (LNET_NIDNET(dest_nid) == LNET_NIDNET(ni->ni_nid)) {
1846 /* should have gone direct */
1847 CERROR ("%s, src %s: Bad dest nid %s "
1848 "(should have been sent direct)\n",
1849 libcfs_nid2str(from_nid),
1850 libcfs_nid2str(src_nid),
1851 libcfs_nid2str(dest_nid));
1855 if (lnet_islocalnid(dest_nid)) {
1856 /* dest is another local NI; sender should have used
1857 * this node's NID on its own network */
1858 CERROR ("%s, src %s: Bad dest nid %s "
1859 "(it's my nid but on a different network)\n",
1860 libcfs_nid2str(from_nid),
1861 libcfs_nid2str(src_nid),
1862 libcfs_nid2str(dest_nid));
1866 if (rdma_req && type == LNET_MSG_GET) {
1867 CERROR ("%s, src %s: Bad optimized GET for %s "
1868 "(final destination must be me)\n",
1869 libcfs_nid2str(from_nid),
1870 libcfs_nid2str(src_nid),
1871 libcfs_nid2str(dest_nid));
1875 if (!the_lnet.ln_routing) {
1876 CERROR ("%s, src %s: Dropping message for %s "
1877 "(routing not enabled)\n",
1878 libcfs_nid2str(from_nid),
1879 libcfs_nid2str(src_nid),
1880 libcfs_nid2str(dest_nid));
1885 /* Message looks OK; we're not going to return an error, so we MUST
1886 * call back lnd_recv() come what may... */
1888 if (!cfs_list_empty (&the_lnet.ln_test_peers) && /* normally we don't */
1889 fail_peer (src_nid, 0)) /* shall we now? */
1891 CERROR("%s, src %s: Dropping %s to simulate failure\n",
1892 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
1893 lnet_msgtyp2str(type));
1897 msg = lnet_msg_alloc();
1899 CERROR("%s, src %s: Dropping %s (out of memory)\n",
1900 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
1901 lnet_msgtyp2str(type));
1905 /* msg zeroed in lnet_msg_alloc; i.e. flags all clear, pointers NULL etc */
1907 msg->msg_type = type;
1908 msg->msg_private = private;
1909 msg->msg_receiving = 1;
1910 msg->msg_len = msg->msg_wanted = payload_length;
1911 msg->msg_offset = 0;
1912 msg->msg_hdr = *hdr;
1913 /* for building message event */
1914 msg->msg_from = from_nid;
1916 msg->msg_target.pid = dest_pid;
1917 msg->msg_target.nid = dest_nid;
1918 msg->msg_routing = 1;
1921 /* convert common msg->hdr fields to host byteorder */
1922 msg->msg_hdr.type = type;
1923 msg->msg_hdr.src_nid = src_nid;
1924 msg->msg_hdr.src_pid = le32_to_cpu(msg->msg_hdr.src_pid);
1925 msg->msg_hdr.dest_nid = dest_nid;
1926 msg->msg_hdr.dest_pid = dest_pid;
1927 msg->msg_hdr.payload_length = payload_length;
1931 rc = lnet_nid2peer_locked(&msg->msg_rxpeer, from_nid, cpt);
1933 lnet_net_unlock(cpt);
1934 CERROR("%s, src %s: Dropping %s "
1935 "(error %d looking up sender)\n",
1936 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
1937 lnet_msgtyp2str(type), rc);
1942 lnet_msg_commit(msg, cpt);
1945 rc = lnet_parse_forward_locked(ni, msg);
1946 lnet_net_unlock(cpt);
1951 lnet_ni_recv(ni, msg->msg_private, msg, 0,
1952 0, payload_length, payload_length);
1957 lnet_net_unlock(cpt);
1961 rc = lnet_parse_ack(ni, msg);
1964 rc = lnet_parse_put(ni, msg);
1967 rc = lnet_parse_get(ni, msg, rdma_req);
1969 case LNET_MSG_REPLY:
1970 rc = lnet_parse_reply(ni, msg);
1975 goto free_drop; /* prevent an unused label if !kernel */
1981 LASSERT (rc == ENOENT);
1984 LASSERT(msg->msg_md == NULL);
1985 lnet_finalize(ni, msg, rc);
1988 lnet_drop_message(ni, cpt, private, payload_length);
1991 EXPORT_SYMBOL(lnet_parse);
1994 lnet_drop_delayed_msg_list(cfs_list_t *head, char *reason)
1996 while (!cfs_list_empty(head)) {
1997 lnet_process_id_t id = {0};
2000 msg = cfs_list_entry(head->next, lnet_msg_t, msg_list);
2001 cfs_list_del(&msg->msg_list);
2003 id.nid = msg->msg_hdr.src_nid;
2004 id.pid = msg->msg_hdr.src_pid;
2006 LASSERT(msg->msg_md == NULL);
2007 LASSERT(msg->msg_rx_delayed);
2008 LASSERT(msg->msg_rxpeer != NULL);
2009 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
2011 CWARN("Dropping delayed PUT from %s portal %d match "LPU64
2012 " offset %d length %d: %s\n",
2014 msg->msg_hdr.msg.put.ptl_index,
2015 msg->msg_hdr.msg.put.match_bits,
2016 msg->msg_hdr.msg.put.offset,
2017 msg->msg_hdr.payload_length, reason);
2019 /* NB I can't drop msg's ref on msg_rxpeer until after I've
2020 * called lnet_drop_message(), so I just hang onto msg as well
2021 * until that's done */
2023 lnet_drop_message(msg->msg_rxpeer->lp_ni,
2024 msg->msg_rxpeer->lp_cpt,
2025 msg->msg_private, msg->msg_len);
2027 * NB: message will not generate event because w/o attached MD,
2028 * but we still should give error code so lnet_msg_decommit()
2029 * can skip counters operations and other checks.
2031 lnet_finalize(msg->msg_rxpeer->lp_ni, msg, -ENOENT);
2036 lnet_recv_delayed_msg_list(cfs_list_t *head)
2038 while (!cfs_list_empty(head)) {
2040 lnet_process_id_t id;
2042 msg = cfs_list_entry(head->next, lnet_msg_t, msg_list);
2043 cfs_list_del(&msg->msg_list);
2045 /* md won't disappear under me, since each msg
2046 * holds a ref on it */
2048 id.nid = msg->msg_hdr.src_nid;
2049 id.pid = msg->msg_hdr.src_pid;
2051 LASSERT(msg->msg_rx_delayed);
2052 LASSERT(msg->msg_md != NULL);
2053 LASSERT(msg->msg_rxpeer != NULL);
2054 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
2056 CDEBUG(D_NET, "Resuming delayed PUT from %s portal %d "
2057 "match "LPU64" offset %d length %d.\n",
2058 libcfs_id2str(id), msg->msg_hdr.msg.put.ptl_index,
2059 msg->msg_hdr.msg.put.match_bits,
2060 msg->msg_hdr.msg.put.offset,
2061 msg->msg_hdr.payload_length);
2063 lnet_recv_put(msg->msg_rxpeer->lp_ni, msg);
2068 * Initiate an asynchronous PUT operation.
2070 * There are several events associated with a PUT: completion of the send on
2071 * the initiator node (LNET_EVENT_SEND), and when the send completes
2072 * successfully, the receipt of an acknowledgment (LNET_EVENT_ACK) indicating
2073 * that the operation was accepted by the target. The event LNET_EVENT_PUT is
2074 * used at the target node to indicate the completion of incoming data
2077 * The local events will be logged in the EQ associated with the MD pointed to
2078 * by \a mdh handle. Using a MD without an associated EQ results in these
2079 * events being discarded. In this case, the caller must have another
2080 * mechanism (e.g., a higher level protocol) for determining when it is safe
2081 * to modify the memory region associated with the MD.
2083 * Note that LNet does not guarantee the order of LNET_EVENT_SEND and
2084 * LNET_EVENT_ACK, though intuitively ACK should happen after SEND.
2086 * \param self Indicates the NID of a local interface through which to send
2087 * the PUT request. Use LNET_NID_ANY to let LNet choose one by itself.
2088 * \param mdh A handle for the MD that describes the memory to be sent. The MD
2089 * must be "free floating" (See LNetMDBind()).
2090 * \param ack Controls whether an acknowledgment is requested.
2091 * Acknowledgments are only sent when they are requested by the initiating
2092 * process and the target MD enables them.
2093 * \param target A process identifier for the target process.
2094 * \param portal The index in the \a target's portal table.
2095 * \param match_bits The match bits to use for MD selection at the target
2097 * \param offset The offset into the target MD (only used when the target
2098 * MD has the LNET_MD_MANAGE_REMOTE option set).
2099 * \param hdr_data 64 bits of user data that can be included in the message
2100 * header. This data is written to an event queue entry at the target if an
2101 * EQ is present on the matching MD.
2103 * \retval 0 Success, and only in this case events will be generated
2104 * and logged to EQ (if it exists).
2105 * \retval -EIO Simulated failure.
2106 * \retval -ENOMEM Memory allocation failure.
2107 * \retval -ENOENT Invalid MD object.
2109 * \see lnet_event_t::hdr_data and lnet_event_kind_t.
2112 LNetPut(lnet_nid_t self, lnet_handle_md_t mdh, lnet_ack_req_t ack,
2113 lnet_process_id_t target, unsigned int portal,
2114 __u64 match_bits, unsigned int offset,
2117 struct lnet_msg *msg;
2118 struct lnet_libmd *md;
2122 LASSERT (the_lnet.ln_init);
2123 LASSERT (the_lnet.ln_refcount > 0);
2125 if (!cfs_list_empty (&the_lnet.ln_test_peers) && /* normally we don't */
2126 fail_peer (target.nid, 1)) /* shall we now? */
2128 CERROR("Dropping PUT to %s: simulated failure\n",
2129 libcfs_id2str(target));
2133 msg = lnet_msg_alloc();
2135 CERROR("Dropping PUT to %s: ENOMEM on lnet_msg_t\n",
2136 libcfs_id2str(target));
2139 msg->msg_vmflush = !!memory_pressure_get();
2141 cpt = lnet_cpt_of_cookie(mdh.cookie);
2144 md = lnet_handle2md(&mdh);
2145 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
2146 CERROR("Dropping PUT ("LPU64":%d:%s): MD (%d) invalid\n",
2147 match_bits, portal, libcfs_id2str(target),
2148 md == NULL ? -1 : md->md_threshold);
2149 if (md != NULL && md->md_me != NULL)
2150 CERROR("Source MD also attached to portal %d\n",
2151 md->md_me->me_portal);
2152 lnet_res_unlock(cpt);
2158 CDEBUG(D_NET, "LNetPut -> %s\n", libcfs_id2str(target));
2160 lnet_msg_attach_md(msg, md, 0, 0);
2162 lnet_prep_send(msg, LNET_MSG_PUT, target, 0, md->md_length);
2164 msg->msg_hdr.msg.put.match_bits = cpu_to_le64(match_bits);
2165 msg->msg_hdr.msg.put.ptl_index = cpu_to_le32(portal);
2166 msg->msg_hdr.msg.put.offset = cpu_to_le32(offset);
2167 msg->msg_hdr.msg.put.hdr_data = hdr_data;
2169 /* NB handles only looked up by creator (no flips) */
2170 if (ack == LNET_ACK_REQ) {
2171 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
2172 the_lnet.ln_interface_cookie;
2173 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
2174 md->md_lh.lh_cookie;
2176 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
2177 LNET_WIRE_HANDLE_COOKIE_NONE;
2178 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
2179 LNET_WIRE_HANDLE_COOKIE_NONE;
2182 lnet_res_unlock(cpt);
2184 lnet_build_msg_event(msg, LNET_EVENT_SEND);
2186 rc = lnet_send(self, msg, LNET_NID_ANY);
2188 CNETERR( "Error sending PUT to %s: %d\n",
2189 libcfs_id2str(target), rc);
2190 lnet_finalize (NULL, msg, rc);
2193 /* completion will be signalled by an event */
2196 EXPORT_SYMBOL(LNetPut);
2199 lnet_create_reply_msg (lnet_ni_t *ni, lnet_msg_t *getmsg)
2201 /* The LND can DMA direct to the GET md (i.e. no REPLY msg). This
2202 * returns a msg for the LND to pass to lnet_finalize() when the sink
2203 * data has been received.
2205 * CAVEAT EMPTOR: 'getmsg' is the original GET, which is freed when
2206 * lnet_finalize() is called on it, so the LND must call this first */
2208 struct lnet_msg *msg = lnet_msg_alloc();
2209 struct lnet_libmd *getmd = getmsg->msg_md;
2210 lnet_process_id_t peer_id = getmsg->msg_target;
2213 LASSERT(!getmsg->msg_target_is_router);
2214 LASSERT(!getmsg->msg_routing);
2216 cpt = lnet_cpt_of_cookie(getmd->md_lh.lh_cookie);
2219 LASSERT (getmd->md_refcount > 0);
2222 CERROR ("%s: Dropping REPLY from %s: can't allocate msg\n",
2223 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id));
2227 if (getmd->md_threshold == 0) {
2228 CERROR ("%s: Dropping REPLY from %s for inactive MD %p\n",
2229 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id),
2231 lnet_res_unlock(cpt);
2235 LASSERT(getmd->md_offset == 0);
2237 CDEBUG(D_NET, "%s: Reply from %s md %p\n",
2238 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id), getmd);
2240 /* setup information for lnet_build_msg_event */
2241 msg->msg_from = peer_id.nid;
2242 msg->msg_type = LNET_MSG_GET; /* flag this msg as an "optimized" GET */
2243 msg->msg_hdr.src_nid = peer_id.nid;
2244 msg->msg_hdr.payload_length = getmd->md_length;
2245 msg->msg_receiving = 1; /* required by lnet_msg_attach_md */
2247 lnet_msg_attach_md(msg, getmd, getmd->md_offset, getmd->md_length);
2248 lnet_res_unlock(cpt);
2250 cpt = lnet_cpt_of_nid(peer_id.nid);
2253 lnet_msg_commit(msg, cpt);
2254 lnet_net_unlock(cpt);
2256 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
2261 cpt = lnet_cpt_of_nid(peer_id.nid);
2264 the_lnet.ln_counters[cpt]->drop_count++;
2265 the_lnet.ln_counters[cpt]->drop_length += getmd->md_length;
2266 lnet_net_unlock(cpt);
2273 EXPORT_SYMBOL(lnet_create_reply_msg);
2276 lnet_set_reply_msg_len(lnet_ni_t *ni, lnet_msg_t *reply, unsigned int len)
2278 /* Set the REPLY length, now the RDMA that elides the REPLY message has
2279 * completed and I know it. */
2280 LASSERT (reply != NULL);
2281 LASSERT (reply->msg_type == LNET_MSG_GET);
2282 LASSERT (reply->msg_ev.type == LNET_EVENT_REPLY);
2284 /* NB I trusted my peer to RDMA. If she tells me she's written beyond
2285 * the end of my buffer, I might as well be dead. */
2286 LASSERT (len <= reply->msg_ev.mlength);
2288 reply->msg_ev.mlength = len;
2290 EXPORT_SYMBOL(lnet_set_reply_msg_len);
2293 * Initiate an asynchronous GET operation.
2295 * On the initiator node, an LNET_EVENT_SEND is logged when the GET request
2296 * is sent, and an LNET_EVENT_REPLY is logged when the data returned from
2297 * the target node in the REPLY has been written to local MD.
2299 * On the target node, an LNET_EVENT_GET is logged when the GET request
2300 * arrives and is accepted into a MD.
2302 * \param self,target,portal,match_bits,offset See the discussion in LNetPut().
2303 * \param mdh A handle for the MD that describes the memory into which the
2304 * requested data will be received. The MD must be "free floating" (See LNetMDBind()).
2306 * \retval 0 Success, and only in this case events will be generated
2307 * and logged to EQ (if it exists) of the MD.
2308 * \retval -EIO Simulated failure.
2309 * \retval -ENOMEM Memory allocation failure.
2310 * \retval -ENOENT Invalid MD object.
2313 LNetGet(lnet_nid_t self, lnet_handle_md_t mdh,
2314 lnet_process_id_t target, unsigned int portal,
2315 __u64 match_bits, unsigned int offset)
2317 struct lnet_msg *msg;
2318 struct lnet_libmd *md;
2322 LASSERT (the_lnet.ln_init);
2323 LASSERT (the_lnet.ln_refcount > 0);
2325 if (!cfs_list_empty (&the_lnet.ln_test_peers) && /* normally we don't */
2326 fail_peer (target.nid, 1)) /* shall we now? */
2328 CERROR("Dropping GET to %s: simulated failure\n",
2329 libcfs_id2str(target));
2333 msg = lnet_msg_alloc();
2335 CERROR("Dropping GET to %s: ENOMEM on lnet_msg_t\n",
2336 libcfs_id2str(target));
2340 cpt = lnet_cpt_of_cookie(mdh.cookie);
2343 md = lnet_handle2md(&mdh);
2344 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
2345 CERROR("Dropping GET ("LPU64":%d:%s): MD (%d) invalid\n",
2346 match_bits, portal, libcfs_id2str(target),
2347 md == NULL ? -1 : md->md_threshold);
2348 if (md != NULL && md->md_me != NULL)
2349 CERROR("REPLY MD also attached to portal %d\n",
2350 md->md_me->me_portal);
2352 lnet_res_unlock(cpt);
2359 CDEBUG(D_NET, "LNetGet -> %s\n", libcfs_id2str(target));
2361 lnet_msg_attach_md(msg, md, 0, 0);
2363 lnet_prep_send(msg, LNET_MSG_GET, target, 0, 0);
2365 msg->msg_hdr.msg.get.match_bits = cpu_to_le64(match_bits);
2366 msg->msg_hdr.msg.get.ptl_index = cpu_to_le32(portal);
2367 msg->msg_hdr.msg.get.src_offset = cpu_to_le32(offset);
2368 msg->msg_hdr.msg.get.sink_length = cpu_to_le32(md->md_length);
2370 /* NB handles only looked up by creator (no flips) */
2371 msg->msg_hdr.msg.get.return_wmd.wh_interface_cookie =
2372 the_lnet.ln_interface_cookie;
2373 msg->msg_hdr.msg.get.return_wmd.wh_object_cookie =
2374 md->md_lh.lh_cookie;
2376 lnet_res_unlock(cpt);
2378 lnet_build_msg_event(msg, LNET_EVENT_SEND);
2380 rc = lnet_send(self, msg, LNET_NID_ANY);
2382 CNETERR( "Error sending GET to %s: %d\n",
2383 libcfs_id2str(target), rc);
2384 lnet_finalize (NULL, msg, rc);
2387 /* completion will be signalled by an event */
2390 EXPORT_SYMBOL(LNetGet);
2393 * Calculate distance to node at \a dstnid.
2395 * \param dstnid Target NID.
2396 * \param srcnidp If not NULL, NID of the local interface to reach \a dstnid
2398 * \param orderp If not NULL, order of the route to reach \a dstnid is saved
2401 * \retval 0 If \a dstnid belongs to a local interface, and reserved option
2402 * local_nid_dist_zero is set, which is the default.
2403 * \retval positives Distance to target NID, i.e. number of hops plus one.
2404 * \retval -EHOSTUNREACH If \a dstnid is not reachable.
2407 LNetDist(lnet_nid_t dstnid, lnet_nid_t *srcnidp, __u32 *orderp)
2411 lnet_remotenet_t *rnet;
2412 __u32 dstnet = LNET_NIDNET(dstnid);
2416 cfs_list_t *rn_list;
2418 /* if !local_nid_dist_zero, I don't return a distance of 0 ever
2419 * (when lustre sees a distance of 0, it substitutes 0@lo), so I
2420 * keep order 0 free for 0@lo and order 1 free for a local NID
2423 LASSERT (the_lnet.ln_init);
2424 LASSERT (the_lnet.ln_refcount > 0);
2426 cpt = lnet_net_lock_current();
2428 cfs_list_for_each (e, &the_lnet.ln_nis) {
2429 ni = cfs_list_entry(e, lnet_ni_t, ni_list);
2431 if (ni->ni_nid == dstnid) {
2432 if (srcnidp != NULL)
2434 if (orderp != NULL) {
2435 if (LNET_NETTYP(LNET_NIDNET(dstnid)) == LOLND)
2440 lnet_net_unlock(cpt);
2442 return local_nid_dist_zero ? 0 : 1;
2445 if (LNET_NIDNET(ni->ni_nid) == dstnet) {
2446 if (srcnidp != NULL)
2447 *srcnidp = ni->ni_nid;
2450 lnet_net_unlock(cpt);
2457 rn_list = lnet_net2rnethash(dstnet);
2458 cfs_list_for_each(e, rn_list) {
2459 rnet = cfs_list_entry(e, lnet_remotenet_t, lrn_list);
2461 if (rnet->lrn_net == dstnet) {
2462 lnet_route_t *route;
2463 lnet_route_t *shortest = NULL;
2465 LASSERT (!cfs_list_empty(&rnet->lrn_routes));
2467 cfs_list_for_each_entry(route, &rnet->lrn_routes,
2469 if (shortest == NULL ||
2470 route->lr_hops < shortest->lr_hops)
2474 LASSERT (shortest != NULL);
2475 hops = shortest->lr_hops;
2476 if (srcnidp != NULL)
2477 *srcnidp = shortest->lr_gateway->lp_ni->ni_nid;
2480 lnet_net_unlock(cpt);
2486 lnet_net_unlock(cpt);
2487 return -EHOSTUNREACH;
2489 EXPORT_SYMBOL(LNetDist);
2492 * Set the number of asynchronous messages expected from a target process.
2494 * This function is only meaningful for userspace callers. It's a no-op when
2495 * called from kernel.
2497 * Asynchronous messages are those that can come from a target when the
2498 * userspace process is not waiting for IO to complete; e.g., AST callbacks
2499 * from Lustre servers. Specifying the expected number of such messages
2500 * allows them to be eagerly received when user process is not running in
2501 * LNet; otherwise network errors may occur.
2503 * \param id Process ID of the target process.
2504 * \param nasync Number of asynchronous messages expected from the target.
2506 * \return 0 on success, and an error code otherwise.
2509 LNetSetAsync(lnet_process_id_t id, int nasync)
2515 lnet_remotenet_t *rnet;
2517 lnet_route_t *route;
2525 /* Target on a local network? */
2526 ni = lnet_net2ni(LNET_NIDNET(id.nid));
2528 if (ni->ni_lnd->lnd_setasync != NULL)
2529 rc = (ni->ni_lnd->lnd_setasync)(ni, id, nasync);
2534 /* Target on a remote network: apply to routers */
2536 LIBCFS_ALLOC(nids, maxnids * sizeof(*nids));
2541 /* Snapshot all the router NIDs */
2542 cpt = lnet_net_lock_current();
2543 rnet = lnet_find_net_locked(LNET_NIDNET(id.nid));
2545 cfs_list_for_each(tmp, &rnet->lrn_routes) {
2546 if (nnids == maxnids) {
2547 lnet_net_unlock(cpt);
2548 LIBCFS_FREE(nids, maxnids * sizeof(*nids));
2553 route = cfs_list_entry(tmp, lnet_route_t, lr_list);
2554 nids[nnids++] = route->lr_gateway->lp_nid;
2557 lnet_net_unlock(cpt);
2559 /* set async on all the routers */
2560 while (nnids-- > 0) {
2561 id.pid = LUSTRE_SRV_LNET_PID;
2562 id.nid = nids[nnids];
2564 ni = lnet_net2ni(LNET_NIDNET(id.nid));
2568 if (ni->ni_lnd->lnd_setasync != NULL) {
2569 rc2 = (ni->ni_lnd->lnd_setasync)(ni, id, nasync);
2576 LIBCFS_FREE(nids, maxnids * sizeof(*nids));
2580 EXPORT_SYMBOL(LNetSetAsync);