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.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
34 * lnet/lnet/lib-move.c
36 * Data movement routines
39 #define DEBUG_SUBSYSTEM S_LNET
41 #include <lnet/lib-lnet.h>
43 static int local_nid_dist_zero = 1;
44 CFS_MODULE_PARM(local_nid_dist_zero, "i", int, 0444,
48 lnet_fail_nid (lnet_nid_t nid, unsigned int threshold)
55 LASSERT (the_lnet.ln_init);
58 /* Adding a new entry */
59 LIBCFS_ALLOC(tp, sizeof(*tp));
64 tp->tp_threshold = threshold;
67 cfs_list_add_tail (&tp->tp_list, &the_lnet.ln_test_peers);
72 /* removing entries */
73 CFS_INIT_LIST_HEAD (&cull);
77 cfs_list_for_each_safe (el, next, &the_lnet.ln_test_peers) {
78 tp = cfs_list_entry (el, lnet_test_peer_t, tp_list);
80 if (tp->tp_threshold == 0 || /* needs culling anyway */
81 nid == LNET_NID_ANY || /* removing all entries */
82 tp->tp_nid == nid) /* matched this one */
84 cfs_list_del (&tp->tp_list);
85 cfs_list_add (&tp->tp_list, &cull);
91 while (!cfs_list_empty (&cull)) {
92 tp = cfs_list_entry (cull.next, lnet_test_peer_t, tp_list);
94 cfs_list_del (&tp->tp_list);
95 LIBCFS_FREE(tp, sizeof (*tp));
101 fail_peer (lnet_nid_t nid, int outgoing)
103 lnet_test_peer_t *tp;
109 CFS_INIT_LIST_HEAD (&cull);
113 cfs_list_for_each_safe (el, next, &the_lnet.ln_test_peers) {
114 tp = cfs_list_entry (el, lnet_test_peer_t, tp_list);
116 if (tp->tp_threshold == 0) {
119 /* only cull zombies on outgoing tests,
120 * since we may be at interrupt priority on
121 * incoming messages. */
122 cfs_list_del (&tp->tp_list);
123 cfs_list_add (&tp->tp_list, &cull);
128 if (tp->tp_nid == LNET_NID_ANY || /* fail every peer */
129 nid == tp->tp_nid) { /* fail this peer */
132 if (tp->tp_threshold != LNET_MD_THRESH_INF) {
135 tp->tp_threshold == 0) {
137 cfs_list_del (&tp->tp_list);
138 cfs_list_add (&tp->tp_list, &cull);
147 while (!cfs_list_empty (&cull)) {
148 tp = cfs_list_entry (cull.next, lnet_test_peer_t, tp_list);
149 cfs_list_del (&tp->tp_list);
151 LIBCFS_FREE(tp, sizeof (*tp));
158 lnet_iov_nob (unsigned int niov, struct iovec *iov)
160 unsigned int nob = 0;
163 nob += (iov++)->iov_len;
169 lnet_copy_iov2iov (unsigned int ndiov, struct iovec *diov, unsigned int doffset,
170 unsigned int nsiov, struct iovec *siov, unsigned int soffset,
173 /* NB diov, siov are READ-ONLY */
174 unsigned int this_nob;
179 /* skip complete frags before 'doffset' */
181 while (doffset >= diov->iov_len) {
182 doffset -= diov->iov_len;
188 /* skip complete frags before 'soffset' */
190 while (soffset >= siov->iov_len) {
191 soffset -= siov->iov_len;
200 this_nob = MIN(diov->iov_len - doffset,
201 siov->iov_len - soffset);
202 this_nob = MIN(this_nob, nob);
204 memcpy ((char *)diov->iov_base + doffset,
205 (char *)siov->iov_base + soffset, this_nob);
208 if (diov->iov_len > doffset + this_nob) {
216 if (siov->iov_len > soffset + this_nob) {
227 lnet_extract_iov (int dst_niov, struct iovec *dst,
228 int src_niov, struct iovec *src,
229 unsigned int offset, unsigned int len)
231 /* Initialise 'dst' to the subset of 'src' starting at 'offset',
232 * for exactly 'len' bytes, and return the number of entries.
233 * NB not destructive to 'src' */
234 unsigned int frag_len;
237 if (len == 0) /* no data => */
238 return (0); /* no frags */
240 LASSERT (src_niov > 0);
241 while (offset >= src->iov_len) { /* skip initial frags */
242 offset -= src->iov_len;
245 LASSERT (src_niov > 0);
250 LASSERT (src_niov > 0);
251 LASSERT ((int)niov <= dst_niov);
253 frag_len = src->iov_len - offset;
254 dst->iov_base = ((char *)src->iov_base) + offset;
256 if (len <= frag_len) {
261 dst->iov_len = frag_len;
274 lnet_kiov_nob (unsigned int niov, lnet_kiov_t *kiov)
281 lnet_copy_kiov2kiov (unsigned int ndkiov, lnet_kiov_t *dkiov, unsigned int doffset,
282 unsigned int nskiov, lnet_kiov_t *skiov, unsigned int soffset,
289 lnet_copy_kiov2iov (unsigned int niov, struct iovec *iov, unsigned int iovoffset,
290 unsigned int nkiov, lnet_kiov_t *kiov, unsigned int kiovoffset,
297 lnet_copy_iov2kiov (unsigned int nkiov, lnet_kiov_t *kiov, unsigned int kiovoffset,
298 unsigned int niov, struct iovec *iov, unsigned int iovoffset,
305 lnet_extract_kiov (int dst_niov, lnet_kiov_t *dst,
306 int src_niov, lnet_kiov_t *src,
307 unsigned int offset, unsigned int len)
312 #else /* __KERNEL__ */
315 lnet_kiov_nob (unsigned int niov, lnet_kiov_t *kiov)
317 unsigned int nob = 0;
320 nob += (kiov++)->kiov_len;
326 lnet_copy_kiov2kiov (unsigned int ndiov, lnet_kiov_t *diov, unsigned int doffset,
327 unsigned int nsiov, lnet_kiov_t *siov, unsigned int soffset,
330 /* NB diov, siov are READ-ONLY */
331 unsigned int this_nob;
338 LASSERT (!cfs_in_interrupt ());
341 while (doffset >= diov->kiov_len) {
342 doffset -= diov->kiov_len;
349 while (soffset >= siov->kiov_len) {
350 soffset -= siov->kiov_len;
359 this_nob = MIN(diov->kiov_len - doffset,
360 siov->kiov_len - soffset);
361 this_nob = MIN(this_nob, nob);
364 daddr = ((char *)cfs_kmap(diov->kiov_page)) +
365 diov->kiov_offset + doffset;
367 saddr = ((char *)cfs_kmap(siov->kiov_page)) +
368 siov->kiov_offset + soffset;
370 /* Vanishing risk of kmap deadlock when mapping 2 pages.
371 * However in practice at least one of the kiovs will be mapped
372 * kernel pages and the map/unmap will be NOOPs */
374 memcpy (daddr, saddr, this_nob);
377 if (diov->kiov_len > doffset + this_nob) {
381 cfs_kunmap(diov->kiov_page);
388 if (siov->kiov_len > soffset + this_nob) {
392 cfs_kunmap(siov->kiov_page);
401 cfs_kunmap(diov->kiov_page);
403 cfs_kunmap(siov->kiov_page);
407 lnet_copy_kiov2iov (unsigned int niov, struct iovec *iov, unsigned int iovoffset,
408 unsigned int nkiov, lnet_kiov_t *kiov, unsigned int kiovoffset,
411 /* NB iov, kiov are READ-ONLY */
412 unsigned int this_nob;
418 LASSERT (!cfs_in_interrupt ());
421 while (iovoffset >= iov->iov_len) {
422 iovoffset -= iov->iov_len;
429 while (kiovoffset >= kiov->kiov_len) {
430 kiovoffset -= kiov->kiov_len;
439 this_nob = MIN(iov->iov_len - iovoffset,
440 kiov->kiov_len - kiovoffset);
441 this_nob = MIN(this_nob, nob);
444 addr = ((char *)cfs_kmap(kiov->kiov_page)) +
445 kiov->kiov_offset + kiovoffset;
447 memcpy ((char *)iov->iov_base + iovoffset, addr, this_nob);
450 if (iov->iov_len > iovoffset + this_nob) {
451 iovoffset += this_nob;
458 if (kiov->kiov_len > kiovoffset + this_nob) {
460 kiovoffset += this_nob;
462 cfs_kunmap(kiov->kiov_page);
472 cfs_kunmap(kiov->kiov_page);
476 lnet_copy_iov2kiov (unsigned int nkiov, lnet_kiov_t *kiov, unsigned int kiovoffset,
477 unsigned int niov, struct iovec *iov, unsigned int iovoffset,
480 /* NB kiov, iov are READ-ONLY */
481 unsigned int this_nob;
487 LASSERT (!cfs_in_interrupt ());
490 while (kiovoffset >= kiov->kiov_len) {
491 kiovoffset -= kiov->kiov_len;
498 while (iovoffset >= iov->iov_len) {
499 iovoffset -= iov->iov_len;
508 this_nob = MIN(kiov->kiov_len - kiovoffset,
509 iov->iov_len - iovoffset);
510 this_nob = MIN(this_nob, nob);
513 addr = ((char *)cfs_kmap(kiov->kiov_page)) +
514 kiov->kiov_offset + kiovoffset;
516 memcpy (addr, (char *)iov->iov_base + iovoffset, this_nob);
519 if (kiov->kiov_len > kiovoffset + this_nob) {
521 kiovoffset += this_nob;
523 cfs_kunmap(kiov->kiov_page);
530 if (iov->iov_len > iovoffset + this_nob) {
531 iovoffset += this_nob;
540 cfs_kunmap(kiov->kiov_page);
544 lnet_extract_kiov (int dst_niov, lnet_kiov_t *dst,
545 int src_niov, lnet_kiov_t *src,
546 unsigned int offset, unsigned int len)
548 /* Initialise 'dst' to the subset of 'src' starting at 'offset',
549 * for exactly 'len' bytes, and return the number of entries.
550 * NB not destructive to 'src' */
551 unsigned int frag_len;
554 if (len == 0) /* no data => */
555 return (0); /* no frags */
557 LASSERT (src_niov > 0);
558 while (offset >= src->kiov_len) { /* skip initial frags */
559 offset -= src->kiov_len;
562 LASSERT (src_niov > 0);
567 LASSERT (src_niov > 0);
568 LASSERT ((int)niov <= dst_niov);
570 frag_len = src->kiov_len - offset;
571 dst->kiov_page = src->kiov_page;
572 dst->kiov_offset = src->kiov_offset + offset;
574 if (len <= frag_len) {
576 LASSERT (dst->kiov_offset + dst->kiov_len <= CFS_PAGE_SIZE);
580 dst->kiov_len = frag_len;
581 LASSERT (dst->kiov_offset + dst->kiov_len <= CFS_PAGE_SIZE);
594 lnet_ni_recv(lnet_ni_t *ni, void *private, lnet_msg_t *msg, int delayed,
595 unsigned int offset, unsigned int mlen, unsigned int rlen)
597 unsigned int niov = 0;
598 struct iovec *iov = NULL;
599 lnet_kiov_t *kiov = NULL;
602 LASSERT (!cfs_in_interrupt ());
603 LASSERT (mlen == 0 || msg != NULL);
606 LASSERT(msg->msg_receiving);
607 LASSERT(!msg->msg_sending);
608 LASSERT(rlen == msg->msg_len);
609 LASSERT(mlen <= msg->msg_len);
610 LASSERT(msg->msg_offset == offset);
611 LASSERT(msg->msg_wanted == mlen);
613 msg->msg_receiving = 0;
616 niov = msg->msg_niov;
618 kiov = msg->msg_kiov;
621 LASSERT ((iov == NULL) != (kiov == NULL));
625 rc = (ni->ni_lnd->lnd_recv)(ni, private, msg, delayed,
626 niov, iov, kiov, offset, mlen, rlen);
628 lnet_finalize(ni, msg, rc);
632 lnet_compare_routes(lnet_route_t *r1, lnet_route_t *r2)
634 lnet_peer_t *p1 = r1->lr_gateway;
635 lnet_peer_t *p2 = r2->lr_gateway;
637 if (r1->lr_hops < r2->lr_hops)
640 if (r1->lr_hops > r2->lr_hops)
643 if (p1->lp_txqnob < p2->lp_txqnob)
646 if (p1->lp_txqnob > p2->lp_txqnob)
649 if (p1->lp_txcredits > p2->lp_txcredits)
652 if (p1->lp_txcredits < p2->lp_txcredits)
660 lnet_setpayloadbuffer(lnet_msg_t *msg)
662 lnet_libmd_t *md = msg->msg_md;
664 LASSERT (msg->msg_len > 0);
665 LASSERT (!msg->msg_routing);
666 LASSERT (md != NULL);
667 LASSERT (msg->msg_niov == 0);
668 LASSERT (msg->msg_iov == NULL);
669 LASSERT (msg->msg_kiov == NULL);
671 msg->msg_niov = md->md_niov;
672 if ((md->md_options & LNET_MD_KIOV) != 0)
673 msg->msg_kiov = md->md_iov.kiov;
675 msg->msg_iov = md->md_iov.iov;
679 lnet_prep_send(lnet_msg_t *msg, int type, lnet_process_id_t target,
680 unsigned int offset, unsigned int len)
682 msg->msg_type = type;
683 msg->msg_target = target;
685 msg->msg_offset = offset;
688 lnet_setpayloadbuffer(msg);
690 memset (&msg->msg_hdr, 0, sizeof (msg->msg_hdr));
691 msg->msg_hdr.type = cpu_to_le32(type);
692 msg->msg_hdr.dest_nid = cpu_to_le64(target.nid);
693 msg->msg_hdr.dest_pid = cpu_to_le32(target.pid);
694 /* src_nid will be set later */
695 msg->msg_hdr.src_pid = cpu_to_le32(the_lnet.ln_pid);
696 msg->msg_hdr.payload_length = cpu_to_le32(len);
700 lnet_ni_send(lnet_ni_t *ni, lnet_msg_t *msg)
702 void *priv = msg->msg_private;
705 LASSERT (!cfs_in_interrupt ());
706 LASSERT (LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND ||
707 (msg->msg_txcredit && msg->msg_peertxcredit));
709 rc = (ni->ni_lnd->lnd_send)(ni, priv, msg);
711 lnet_finalize(ni, msg, rc);
715 lnet_eager_recv_locked(lnet_msg_t *msg)
721 LASSERT (!msg->msg_delayed);
722 msg->msg_delayed = 1;
724 LASSERT (msg->msg_receiving);
725 LASSERT (!msg->msg_sending);
727 peer = msg->msg_rxpeer;
730 if (ni->ni_lnd->lnd_eager_recv != NULL) {
733 rc = (ni->ni_lnd->lnd_eager_recv)(ni, msg->msg_private, msg,
736 CERROR("recv from %s / send to %s aborted: "
737 "eager_recv failed %d\n",
738 libcfs_nid2str(peer->lp_nid),
739 libcfs_id2str(msg->msg_target), rc);
740 LASSERT (rc < 0); /* required by my callers */
749 /* NB: caller shall hold a ref on 'lp' as I'd drop LNET_LOCK */
751 lnet_ni_peer_alive(lnet_peer_t *lp)
753 cfs_time_t last_alive = 0;
754 lnet_ni_t *ni = lp->lp_ni;
756 LASSERT (lnet_peer_aliveness_enabled(lp));
757 LASSERT (ni->ni_lnd->lnd_query != NULL);
758 LASSERT (the_lnet.ln_routing == 1);
761 (ni->ni_lnd->lnd_query)(ni, lp->lp_nid, &last_alive);
764 lp->lp_last_query = cfs_time_current();
766 if (last_alive != 0) /* NI has updated timestamp */
767 lp->lp_last_alive = last_alive;
770 /* NB: always called with LNET_LOCK held */
772 lnet_peer_is_alive (lnet_peer_t *lp, cfs_time_t now)
777 LASSERT (lnet_peer_aliveness_enabled(lp));
778 LASSERT (the_lnet.ln_routing == 1);
780 /* Trust lnet_notify() if it has more recent aliveness news, but
781 * ignore the initial assumed death (see lnet_peers_start_down()).
783 if (!lp->lp_alive && lp->lp_alive_count > 0 &&
784 cfs_time_aftereq(lp->lp_timestamp, lp->lp_last_alive))
787 deadline = cfs_time_add(lp->lp_last_alive,
788 cfs_time_seconds(lp->lp_ni->ni_peertimeout));
789 alive = cfs_time_after(deadline, now);
791 /* Update obsolete lp_alive except for routers assumed to be dead
792 * initially, because router checker would update aliveness in this
793 * case, and moreover lp_last_alive at peer creation is assumed.
795 if (alive && !lp->lp_alive &&
796 !(lnet_isrouter(lp) && lp->lp_alive_count == 0))
797 lnet_notify_locked(lp, 0, 1, lp->lp_last_alive);
803 /* NB: returns 1 when alive, 0 when dead, negative when error;
804 * may drop the LNET_LOCK */
806 lnet_peer_alive_locked (lnet_peer_t *lp)
808 cfs_time_t now = cfs_time_current();
810 /* LU-630: only router checks peer health. */
811 if (the_lnet.ln_routing == 0)
814 if (!lnet_peer_aliveness_enabled(lp))
817 if (lnet_peer_is_alive(lp, now))
820 /* Peer appears dead, but we should avoid frequent NI queries (at
821 * most once per lnet_queryinterval seconds). */
822 if (lp->lp_last_query != 0) {
823 static const int lnet_queryinterval = 1;
825 cfs_time_t next_query =
826 cfs_time_add(lp->lp_last_query,
827 cfs_time_seconds(lnet_queryinterval));
829 if (cfs_time_before(now, next_query)) {
831 CWARN("Unexpected aliveness of peer %s: "
833 libcfs_nid2str(lp->lp_nid),
834 (int)now, (int)next_query,
836 lp->lp_ni->ni_peertimeout);
841 /* query NI for latest aliveness news */
842 lnet_ni_peer_alive(lp);
844 if (lnet_peer_is_alive(lp, now))
847 lnet_notify_locked(lp, 0, 0, lp->lp_last_alive);
852 lnet_post_send_locked (lnet_msg_t *msg, int do_send)
854 /* lnet_send is going to LNET_UNLOCK immediately after this, so it sets
855 * do_send FALSE and I don't do the unlock/send/lock bit. I return
856 * EAGAIN if msg blocked, EHOSTUNREACH if msg_txpeer appears dead, and
857 * 0 if sent or OK to send */
858 lnet_peer_t *lp = msg->msg_txpeer;
859 lnet_ni_t *ni = lp->lp_ni;
861 /* non-lnet_send() callers have checked before */
862 LASSERT (!do_send || msg->msg_delayed);
863 LASSERT (!msg->msg_receiving);
865 /* NB 'lp' is always the next hop */
866 if ((msg->msg_target.pid & LNET_PID_USERFLAG) == 0 &&
867 lnet_peer_alive_locked(lp) == 0) {
868 the_lnet.ln_counters.drop_count++;
869 the_lnet.ln_counters.drop_length += msg->msg_len;
872 CNETERR("Dropping message for %s: peer not alive\n",
873 libcfs_id2str(msg->msg_target));
875 lnet_finalize(ni, msg, -EHOSTUNREACH);
881 if (!msg->msg_peertxcredit) {
882 LASSERT ((lp->lp_txcredits < 0) ==
883 !cfs_list_empty(&lp->lp_txq));
885 msg->msg_peertxcredit = 1;
886 lp->lp_txqnob += msg->msg_len + sizeof(lnet_hdr_t);
889 if (lp->lp_txcredits < lp->lp_mintxcredits)
890 lp->lp_mintxcredits = lp->lp_txcredits;
892 if (lp->lp_txcredits < 0) {
893 msg->msg_delayed = 1;
894 cfs_list_add_tail(&msg->msg_list, &lp->lp_txq);
899 if (!msg->msg_txcredit) {
900 LASSERT ((ni->ni_txcredits < 0) ==
901 !cfs_list_empty(&ni->ni_txq));
903 msg->msg_txcredit = 1;
906 if (ni->ni_txcredits < ni->ni_mintxcredits)
907 ni->ni_mintxcredits = ni->ni_txcredits;
909 if (ni->ni_txcredits < 0) {
910 msg->msg_delayed = 1;
911 cfs_list_add_tail(&msg->msg_list, &ni->ni_txq);
918 lnet_ni_send(ni, msg);
927 lnet_msg2bufpool(lnet_msg_t *msg)
929 lnet_rtrbufpool_t *rbp = &the_lnet.ln_rtrpools[0];
931 LASSERT (msg->msg_len <= LNET_MTU);
932 while (msg->msg_len > (unsigned int)rbp->rbp_npages * CFS_PAGE_SIZE) {
934 LASSERT (rbp < &the_lnet.ln_rtrpools[LNET_NRBPOOLS]);
941 lnet_post_routed_recv_locked (lnet_msg_t *msg, int do_recv)
943 /* lnet_parse is going to LNET_UNLOCK immediately after this, so it
944 * sets do_recv FALSE and I don't do the unlock/send/lock bit. I
945 * return EAGAIN if msg blocked and 0 if received or OK to receive */
946 lnet_peer_t *lp = msg->msg_rxpeer;
947 lnet_rtrbufpool_t *rbp;
950 LASSERT (msg->msg_iov == NULL);
951 LASSERT (msg->msg_kiov == NULL);
952 LASSERT (msg->msg_niov == 0);
953 LASSERT (msg->msg_routing);
954 LASSERT (msg->msg_receiving);
955 LASSERT (!msg->msg_sending);
957 /* non-lnet_parse callers only send delayed messages */
958 LASSERT (!do_recv || msg->msg_delayed);
960 if (!msg->msg_peerrtrcredit) {
961 LASSERT ((lp->lp_rtrcredits < 0) ==
962 !cfs_list_empty(&lp->lp_rtrq));
964 msg->msg_peerrtrcredit = 1;
966 if (lp->lp_rtrcredits < lp->lp_minrtrcredits)
967 lp->lp_minrtrcredits = lp->lp_rtrcredits;
969 if (lp->lp_rtrcredits < 0) {
970 /* must have checked eager_recv before here */
971 LASSERT (msg->msg_delayed);
972 cfs_list_add_tail(&msg->msg_list, &lp->lp_rtrq);
977 rbp = lnet_msg2bufpool(msg);
979 if (!msg->msg_rtrcredit) {
980 LASSERT ((rbp->rbp_credits < 0) ==
981 !cfs_list_empty(&rbp->rbp_msgs));
983 msg->msg_rtrcredit = 1;
985 if (rbp->rbp_credits < rbp->rbp_mincredits)
986 rbp->rbp_mincredits = rbp->rbp_credits;
988 if (rbp->rbp_credits < 0) {
989 /* must have checked eager_recv before here */
990 LASSERT (msg->msg_delayed);
991 cfs_list_add_tail(&msg->msg_list, &rbp->rbp_msgs);
996 LASSERT (!cfs_list_empty(&rbp->rbp_bufs));
997 rb = cfs_list_entry(rbp->rbp_bufs.next, lnet_rtrbuf_t, rb_list);
998 cfs_list_del(&rb->rb_list);
1000 msg->msg_niov = rbp->rbp_npages;
1001 msg->msg_kiov = &rb->rb_kiov[0];
1005 lnet_ni_recv(lp->lp_ni, msg->msg_private, msg, 1,
1006 0, msg->msg_len, msg->msg_len);
1014 lnet_return_tx_credits_locked(lnet_msg_t *msg)
1016 lnet_peer_t *txpeer = msg->msg_txpeer;
1020 if (msg->msg_txcredit) {
1021 /* give back NI txcredits */
1022 msg->msg_txcredit = 0;
1025 LASSERT((ni->ni_txcredits < 0) == !cfs_list_empty(&ni->ni_txq));
1028 if (ni->ni_txcredits <= 0) {
1029 msg2 = cfs_list_entry(ni->ni_txq.next, lnet_msg_t,
1031 cfs_list_del(&msg2->msg_list);
1033 LASSERT(msg2->msg_txpeer->lp_ni == ni);
1034 LASSERT(msg2->msg_delayed);
1036 (void) lnet_post_send_locked(msg2, 1);
1040 if (msg->msg_peertxcredit) {
1041 /* give back peer txcredits */
1042 msg->msg_peertxcredit = 0;
1044 LASSERT((txpeer->lp_txcredits < 0) ==
1045 !cfs_list_empty(&txpeer->lp_txq));
1047 txpeer->lp_txqnob -= msg->msg_len + sizeof(lnet_hdr_t);
1048 LASSERT (txpeer->lp_txqnob >= 0);
1050 txpeer->lp_txcredits++;
1051 if (txpeer->lp_txcredits <= 0) {
1052 msg2 = cfs_list_entry(txpeer->lp_txq.next,
1053 lnet_msg_t, msg_list);
1054 cfs_list_del(&msg2->msg_list);
1056 LASSERT (msg2->msg_txpeer == txpeer);
1057 LASSERT (msg2->msg_delayed);
1059 (void) lnet_post_send_locked(msg2, 1);
1063 if (txpeer != NULL) {
1064 msg->msg_txpeer = NULL;
1065 lnet_peer_decref_locked(txpeer);
1070 lnet_return_rx_credits_locked(lnet_msg_t *msg)
1072 lnet_peer_t *rxpeer = msg->msg_rxpeer;
1076 if (msg->msg_rtrcredit) {
1077 /* give back global router credits */
1079 lnet_rtrbufpool_t *rbp;
1081 /* NB If a msg ever blocks for a buffer in rbp_msgs, it stays
1082 * there until it gets one allocated, or aborts the wait
1084 LASSERT (msg->msg_kiov != NULL);
1086 rb = cfs_list_entry(msg->msg_kiov, lnet_rtrbuf_t, rb_kiov[0]);
1088 LASSERT (rbp == lnet_msg2bufpool(msg));
1090 msg->msg_kiov = NULL;
1091 msg->msg_rtrcredit = 0;
1093 LASSERT((rbp->rbp_credits < 0) ==
1094 !cfs_list_empty(&rbp->rbp_msgs));
1095 LASSERT((rbp->rbp_credits > 0) ==
1096 !cfs_list_empty(&rbp->rbp_bufs));
1098 cfs_list_add(&rb->rb_list, &rbp->rbp_bufs);
1100 if (rbp->rbp_credits <= 0) {
1101 msg2 = cfs_list_entry(rbp->rbp_msgs.next,
1102 lnet_msg_t, msg_list);
1103 cfs_list_del(&msg2->msg_list);
1105 (void) lnet_post_routed_recv_locked(msg2, 1);
1109 if (msg->msg_peerrtrcredit) {
1110 /* give back peer router credits */
1111 msg->msg_peerrtrcredit = 0;
1113 LASSERT((rxpeer->lp_rtrcredits < 0) ==
1114 !cfs_list_empty(&rxpeer->lp_rtrq));
1116 rxpeer->lp_rtrcredits++;
1117 if (rxpeer->lp_rtrcredits <= 0) {
1118 msg2 = cfs_list_entry(rxpeer->lp_rtrq.next,
1119 lnet_msg_t, msg_list);
1120 cfs_list_del(&msg2->msg_list);
1122 (void) lnet_post_routed_recv_locked(msg2, 1);
1126 LASSERT (!msg->msg_rtrcredit);
1127 LASSERT (!msg->msg_peerrtrcredit);
1129 if (rxpeer != NULL) {
1130 msg->msg_rxpeer = NULL;
1131 lnet_peer_decref_locked(rxpeer);
1135 static lnet_peer_t *
1136 lnet_find_route_locked(lnet_ni_t *ni, lnet_nid_t target)
1138 lnet_remotenet_t *rnet;
1140 lnet_route_t *rtr_best;
1141 lnet_route_t *rtr_last;
1142 struct lnet_peer *lp_best;
1143 struct lnet_peer *lp;
1146 rnet = lnet_find_net_locked(LNET_NIDNET(target));
1151 rtr_best = rtr_last = NULL;
1152 cfs_list_for_each_entry(rtr, &rnet->lrn_routes, lr_list) {
1153 lp = rtr->lr_gateway;
1155 if (!lp->lp_alive || /* gateway is down */
1156 (lp->lp_ping_version == LNET_PROTO_PING_VERSION &&
1157 rtr->lr_downis != 0)) /* NI to target is down */
1160 if (ni != NULL && lp->lp_ni != ni)
1163 if (lp_best == NULL) {
1164 rtr_best = rtr_last = rtr;
1169 rc = lnet_compare_routes(rtr, rtr_best);
1177 if (rtr_best != NULL) {
1178 /* Place selected route at the end of the route list to ensure
1179 * fairness; everything else being equal... */
1180 cfs_list_del(&rtr_best->lr_list);
1181 cfs_list_add_tail(&rtr_best->lr_list, &rnet->lrn_routes);
1188 lnet_send(lnet_nid_t src_nid, lnet_msg_t *msg)
1190 lnet_nid_t dst_nid = msg->msg_target.nid;
1192 lnet_ni_t *local_ni;
1196 LASSERT (msg->msg_txpeer == NULL);
1197 LASSERT (!msg->msg_sending);
1198 LASSERT (!msg->msg_target_is_router);
1199 LASSERT (!msg->msg_receiving);
1201 msg->msg_sending = 1;
1203 /* NB! ni != NULL == interface pre-determined (ACK/REPLY) */
1207 if (the_lnet.ln_shutdown) {
1212 if (src_nid == LNET_NID_ANY) {
1215 src_ni = lnet_nid2ni_locked(src_nid);
1216 if (src_ni == NULL) {
1218 LCONSOLE_WARN("Can't send to %s: src %s is not a "
1219 "local nid\n", libcfs_nid2str(dst_nid),
1220 libcfs_nid2str(src_nid));
1223 LASSERT (!msg->msg_routing);
1226 lnet_msg_commit(msg, 1);
1227 /* Is this for someone on a local network? */
1228 local_ni = lnet_net2ni_locked(LNET_NIDNET(dst_nid));
1230 if (local_ni != NULL) {
1231 if (src_ni == NULL) {
1233 src_nid = src_ni->ni_nid;
1234 } else if (src_ni == local_ni) {
1235 lnet_ni_decref_locked(local_ni);
1237 lnet_ni_decref_locked(local_ni);
1238 lnet_ni_decref_locked(src_ni);
1240 LCONSOLE_WARN("No route to %s via from %s\n",
1241 libcfs_nid2str(dst_nid),
1242 libcfs_nid2str(src_nid));
1246 LASSERT (src_nid != LNET_NID_ANY);
1248 if (!msg->msg_routing)
1249 msg->msg_hdr.src_nid = cpu_to_le64(src_nid);
1251 if (src_ni == the_lnet.ln_loni) {
1252 /* No send credit hassles with LOLND */
1254 lnet_ni_send(src_ni, msg);
1255 lnet_ni_decref(src_ni);
1259 rc = lnet_nid2peer_locked(&lp, dst_nid);
1260 lnet_ni_decref_locked(src_ni); /* lp has ref on src_ni; lose mine */
1263 LCONSOLE_WARN("Error %d finding peer %s\n", rc,
1264 libcfs_nid2str(dst_nid));
1265 /* ENOMEM or shutting down */
1268 LASSERT (lp->lp_ni == src_ni);
1274 * - once application finishes computation, check here to update
1275 * router states before it waits for pending IO in LNetEQPoll
1276 * - recursion breaker: router checker sends no message
1277 * to remote networks */
1278 if (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING)
1279 lnet_router_checker();
1283 /* sending to a remote network */
1284 lp = lnet_find_route_locked(src_ni, dst_nid);
1287 lnet_ni_decref_locked(src_ni);
1290 LCONSOLE_WARN("No route to %s via %s "
1291 "(all routers down)\n",
1292 libcfs_id2str(msg->msg_target),
1293 libcfs_nid2str(src_nid));
1294 return -EHOSTUNREACH;
1297 CDEBUG(D_NET, "Best route to %s via %s for %s %d\n",
1298 libcfs_nid2str(dst_nid), libcfs_nid2str(lp->lp_nid),
1299 lnet_msgtyp2str(msg->msg_type), msg->msg_len);
1301 if (src_ni == NULL) {
1303 src_nid = src_ni->ni_nid;
1305 LASSERT (src_ni == lp->lp_ni);
1306 lnet_ni_decref_locked(src_ni);
1309 lnet_peer_addref_locked(lp);
1311 LASSERT (src_nid != LNET_NID_ANY);
1313 if (!msg->msg_routing) {
1314 /* I'm the source and now I know which NI to send on */
1315 msg->msg_hdr.src_nid = cpu_to_le64(src_nid);
1318 msg->msg_target_is_router = 1;
1319 msg->msg_target.nid = lp->lp_nid;
1320 msg->msg_target.pid = LUSTRE_SRV_LNET_PID;
1323 /* 'lp' is our best choice of peer */
1325 LASSERT (!msg->msg_peertxcredit);
1326 LASSERT (!msg->msg_txcredit);
1327 LASSERT (msg->msg_txpeer == NULL);
1329 msg->msg_txpeer = lp; /* msg takes my ref on lp */
1331 rc = lnet_post_send_locked(msg, 0);
1334 if (rc == EHOSTUNREACH)
1335 return -EHOSTUNREACH;
1338 lnet_ni_send(src_ni, msg);
1344 lnet_drop_message (lnet_ni_t *ni, void *private, unsigned int nob)
1347 the_lnet.ln_counters.drop_count++;
1348 the_lnet.ln_counters.drop_length += nob;
1351 lnet_ni_recv(ni, private, NULL, 0, 0, 0, nob);
1355 lnet_recv_put(lnet_ni_t *ni, lnet_msg_t *msg)
1357 lnet_hdr_t *hdr = &msg->msg_hdr;
1359 if (msg->msg_wanted != 0)
1360 lnet_setpayloadbuffer(msg);
1362 lnet_build_msg_event(msg, LNET_EVENT_PUT);
1364 /* Must I ACK? If so I'll grab the ack_wmd out of the header and put
1365 * it back into the ACK during lnet_finalize() */
1366 msg->msg_ack = (!lnet_is_wire_handle_none(&hdr->msg.put.ack_wmd) &&
1367 (msg->msg_md->md_options & LNET_MD_ACK_DISABLE) == 0);
1369 lnet_ni_recv(ni, msg->msg_private, msg, msg->msg_delayed,
1370 msg->msg_offset, msg->msg_wanted, hdr->payload_length);
1374 lnet_parse_put(lnet_ni_t *ni, lnet_msg_t *msg)
1379 lnet_hdr_t *hdr = &msg->msg_hdr;
1380 unsigned int rlength = hdr->payload_length;
1381 lnet_process_id_t src= {0};
1384 src.nid = hdr->src_nid;
1385 src.pid = hdr->src_pid;
1387 /* Convert put fields to host byte order */
1388 hdr->msg.put.match_bits = le64_to_cpu(hdr->msg.put.match_bits);
1389 hdr->msg.put.ptl_index = le32_to_cpu(hdr->msg.put.ptl_index);
1390 hdr->msg.put.offset = le32_to_cpu(hdr->msg.put.offset);
1392 index = hdr->msg.put.ptl_index;
1397 rc = lnet_match_md(index, LNET_MD_OP_PUT, src,
1398 rlength, hdr->msg.put.offset,
1399 hdr->msg.put.match_bits, msg);
1404 case LNET_MATCHMD_OK:
1406 lnet_recv_put(ni, msg);
1409 case LNET_MATCHMD_NONE:
1410 ptl = the_lnet.ln_portals[index];
1411 version = ptl->ptl_ml_version;
1414 if (!msg->msg_delayed)
1415 rc = lnet_eager_recv_locked(msg);
1418 !the_lnet.ln_shutdown &&
1419 lnet_ptl_is_lazy(ptl)) {
1420 if (version != ptl->ptl_ml_version)
1423 cfs_list_add_tail(&msg->msg_list, &ptl->ptl_msgq);
1424 ptl->ptl_msgq_version++;
1427 CDEBUG(D_NET, "Delaying PUT from %s portal %d match "
1428 LPU64" offset %d length %d: no match \n",
1429 libcfs_id2str(src), index,
1430 hdr->msg.put.match_bits,
1431 hdr->msg.put.offset, rlength);
1436 case LNET_MATCHMD_DROP:
1437 CNETERR("Dropping PUT from %s portal %d match "LPU64
1438 " offset %d length %d: %d\n",
1439 libcfs_id2str(src), index,
1440 hdr->msg.put.match_bits,
1441 hdr->msg.put.offset, rlength, rc);
1444 return ENOENT; /* +ve: OK but no match */
1449 lnet_parse_get(lnet_ni_t *ni, lnet_msg_t *msg, int rdma_get)
1451 lnet_hdr_t *hdr = &msg->msg_hdr;
1452 lnet_process_id_t src = {0};
1453 lnet_handle_wire_t reply_wmd;
1456 src.nid = hdr->src_nid;
1457 src.pid = hdr->src_pid;
1459 /* Convert get fields to host byte order */
1460 hdr->msg.get.match_bits = le64_to_cpu(hdr->msg.get.match_bits);
1461 hdr->msg.get.ptl_index = le32_to_cpu(hdr->msg.get.ptl_index);
1462 hdr->msg.get.sink_length = le32_to_cpu(hdr->msg.get.sink_length);
1463 hdr->msg.get.src_offset = le32_to_cpu(hdr->msg.get.src_offset);
1467 rc = lnet_match_md(hdr->msg.get.ptl_index, LNET_MD_OP_GET, src,
1468 hdr->msg.get.sink_length, hdr->msg.get.src_offset,
1469 hdr->msg.get.match_bits, msg);
1470 if (rc == LNET_MATCHMD_DROP) {
1471 CNETERR("Dropping GET from %s portal %d match "LPU64
1472 " offset %d length %d\n",
1474 hdr->msg.get.ptl_index,
1475 hdr->msg.get.match_bits,
1476 hdr->msg.get.src_offset,
1477 hdr->msg.get.sink_length);
1479 return ENOENT; /* +ve: OK but no match */
1482 LASSERT (rc == LNET_MATCHMD_OK);
1486 lnet_build_msg_event(msg, LNET_EVENT_GET);
1488 reply_wmd = hdr->msg.get.return_wmd;
1490 lnet_prep_send(msg, LNET_MSG_REPLY, src,
1491 msg->msg_offset, msg->msg_wanted);
1493 msg->msg_hdr.msg.reply.dst_wmd = reply_wmd;
1496 /* The LND completes the REPLY from her recv procedure */
1497 lnet_ni_recv(ni, msg->msg_private, msg, 0,
1498 msg->msg_offset, msg->msg_len, msg->msg_len);
1502 lnet_ni_recv(ni, msg->msg_private, NULL, 0, 0, 0, 0);
1503 msg->msg_receiving = 0;
1505 rc = lnet_send(ni->ni_nid, msg);
1507 /* didn't get as far as lnet_ni_send() */
1508 CERROR("%s: Unable to send REPLY for GET from %s: %d\n",
1509 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src), rc);
1511 lnet_finalize(ni, msg, rc);
1518 lnet_parse_reply(lnet_ni_t *ni, lnet_msg_t *msg)
1520 void *private = msg->msg_private;
1521 lnet_hdr_t *hdr = &msg->msg_hdr;
1522 lnet_process_id_t src = {0};
1529 src.nid = hdr->src_nid;
1530 src.pid = hdr->src_pid;
1532 /* NB handles only looked up by creator (no flips) */
1533 md = lnet_wire_handle2md(&hdr->msg.reply.dst_wmd);
1534 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
1535 CNETERR("%s: Dropping REPLY from %s for %s "
1536 "MD "LPX64"."LPX64"\n",
1537 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1538 (md == NULL) ? "invalid" : "inactive",
1539 hdr->msg.reply.dst_wmd.wh_interface_cookie,
1540 hdr->msg.reply.dst_wmd.wh_object_cookie);
1541 if (md != NULL && md->md_me != NULL)
1542 CERROR("REPLY MD also attached to portal %d\n",
1543 md->md_me->me_portal);
1546 return ENOENT; /* +ve: OK but no match */
1549 LASSERT (md->md_offset == 0);
1551 rlength = hdr->payload_length;
1552 mlength = MIN(rlength, (int)md->md_length);
1554 if (mlength < rlength &&
1555 (md->md_options & LNET_MD_TRUNCATE) == 0) {
1556 CNETERR("%s: Dropping REPLY from %s length %d "
1557 "for MD "LPX64" would overflow (%d)\n",
1558 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1559 rlength, hdr->msg.reply.dst_wmd.wh_object_cookie,
1562 return ENOENT; /* +ve: OK but no match */
1565 CDEBUG(D_NET, "%s: Reply from %s of length %d/%d into md "LPX64"\n",
1566 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1567 mlength, rlength, hdr->msg.reply.dst_wmd.wh_object_cookie);
1569 lnet_msg_attach_md(msg, md, 0, mlength);
1572 lnet_setpayloadbuffer(msg);
1576 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
1578 lnet_ni_recv(ni, private, msg, 0, 0, mlength, rlength);
1583 lnet_parse_ack(lnet_ni_t *ni, lnet_msg_t *msg)
1585 lnet_hdr_t *hdr = &msg->msg_hdr;
1586 lnet_process_id_t src = {0};
1589 src.nid = hdr->src_nid;
1590 src.pid = hdr->src_pid;
1592 /* Convert ack fields to host byte order */
1593 hdr->msg.ack.match_bits = le64_to_cpu(hdr->msg.ack.match_bits);
1594 hdr->msg.ack.mlength = le32_to_cpu(hdr->msg.ack.mlength);
1598 /* NB handles only looked up by creator (no flips) */
1599 md = lnet_wire_handle2md(&hdr->msg.ack.dst_wmd);
1600 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
1601 /* Don't moan; this is expected */
1603 "%s: Dropping ACK from %s to %s MD "LPX64"."LPX64"\n",
1604 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1605 (md == NULL) ? "invalid" : "inactive",
1606 hdr->msg.ack.dst_wmd.wh_interface_cookie,
1607 hdr->msg.ack.dst_wmd.wh_object_cookie);
1608 if (md != NULL && md->md_me != NULL)
1609 CERROR("Source MD also attached to portal %d\n",
1610 md->md_me->me_portal);
1613 return ENOENT; /* +ve! */
1616 CDEBUG(D_NET, "%s: ACK from %s into md "LPX64"\n",
1617 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1618 hdr->msg.ack.dst_wmd.wh_object_cookie);
1620 lnet_msg_attach_md(msg, md, 0, 0);
1624 lnet_build_msg_event(msg, LNET_EVENT_ACK);
1626 lnet_ni_recv(ni, msg->msg_private, msg, 0, 0, 0, msg->msg_len);
1631 lnet_msgtyp2str (int type)
1640 case LNET_MSG_REPLY:
1642 case LNET_MSG_HELLO:
1645 return ("<UNKNOWN>");
1650 lnet_print_hdr(lnet_hdr_t * hdr)
1652 lnet_process_id_t src = {0};
1653 lnet_process_id_t dst = {0};
1654 char *type_str = lnet_msgtyp2str (hdr->type);
1656 src.nid = hdr->src_nid;
1657 src.pid = hdr->src_pid;
1659 dst.nid = hdr->dest_nid;
1660 dst.pid = hdr->dest_pid;
1662 CWARN("P3 Header at %p of type %s\n", hdr, type_str);
1663 CWARN(" From %s\n", libcfs_id2str(src));
1664 CWARN(" To %s\n", libcfs_id2str(dst));
1666 switch (hdr->type) {
1671 CWARN(" Ptl index %d, ack md "LPX64"."LPX64", "
1672 "match bits "LPU64"\n",
1673 hdr->msg.put.ptl_index,
1674 hdr->msg.put.ack_wmd.wh_interface_cookie,
1675 hdr->msg.put.ack_wmd.wh_object_cookie,
1676 hdr->msg.put.match_bits);
1677 CWARN(" Length %d, offset %d, hdr data "LPX64"\n",
1678 hdr->payload_length, hdr->msg.put.offset,
1679 hdr->msg.put.hdr_data);
1683 CWARN(" Ptl index %d, return md "LPX64"."LPX64", "
1684 "match bits "LPU64"\n", hdr->msg.get.ptl_index,
1685 hdr->msg.get.return_wmd.wh_interface_cookie,
1686 hdr->msg.get.return_wmd.wh_object_cookie,
1687 hdr->msg.get.match_bits);
1688 CWARN(" Length %d, src offset %d\n",
1689 hdr->msg.get.sink_length,
1690 hdr->msg.get.src_offset);
1694 CWARN(" dst md "LPX64"."LPX64", "
1695 "manipulated length %d\n",
1696 hdr->msg.ack.dst_wmd.wh_interface_cookie,
1697 hdr->msg.ack.dst_wmd.wh_object_cookie,
1698 hdr->msg.ack.mlength);
1701 case LNET_MSG_REPLY:
1702 CWARN(" dst md "LPX64"."LPX64", "
1704 hdr->msg.reply.dst_wmd.wh_interface_cookie,
1705 hdr->msg.reply.dst_wmd.wh_object_cookie,
1706 hdr->payload_length);
1712 lnet_parse(lnet_ni_t *ni, lnet_hdr_t *hdr, lnet_nid_t from_nid,
1713 void *private, int rdma_req)
1718 lnet_pid_t dest_pid;
1719 lnet_nid_t dest_nid;
1721 __u32 payload_length;
1724 LASSERT (!cfs_in_interrupt ());
1726 type = le32_to_cpu(hdr->type);
1727 src_nid = le64_to_cpu(hdr->src_nid);
1728 dest_nid = le64_to_cpu(hdr->dest_nid);
1729 dest_pid = le32_to_cpu(hdr->dest_pid);
1730 payload_length = le32_to_cpu(hdr->payload_length);
1732 for_me = (ni->ni_nid == dest_nid);
1737 if (payload_length > 0) {
1738 CERROR("%s, src %s: bad %s payload %d (0 expected)\n",
1739 libcfs_nid2str(from_nid),
1740 libcfs_nid2str(src_nid),
1741 lnet_msgtyp2str(type), payload_length);
1747 case LNET_MSG_REPLY:
1748 if (payload_length > (__u32)(for_me ? LNET_MAX_PAYLOAD : LNET_MTU)) {
1749 CERROR("%s, src %s: bad %s payload %d "
1750 "(%d max expected)\n",
1751 libcfs_nid2str(from_nid),
1752 libcfs_nid2str(src_nid),
1753 lnet_msgtyp2str(type),
1755 for_me ? LNET_MAX_PAYLOAD : LNET_MTU);
1761 CERROR("%s, src %s: Bad message type 0x%x\n",
1762 libcfs_nid2str(from_nid),
1763 libcfs_nid2str(src_nid), type);
1767 if (the_lnet.ln_routing &&
1768 ni->ni_last_alive != cfs_time_current_sec()) {
1771 /* NB: so far here is the only place to set NI status to "up */
1772 ni->ni_last_alive = cfs_time_current_sec();
1773 if (ni->ni_status != NULL &&
1774 ni->ni_status->ns_status == LNET_NI_STATUS_DOWN)
1775 ni->ni_status->ns_status = LNET_NI_STATUS_UP;
1779 /* Regard a bad destination NID as a protocol error. Senders should
1780 * know what they're doing; if they don't they're misconfigured, buggy
1781 * or malicious so we chop them off at the knees :) */
1784 if (LNET_NIDNET(dest_nid) == LNET_NIDNET(ni->ni_nid)) {
1785 /* should have gone direct */
1786 CERROR ("%s, src %s: Bad dest nid %s "
1787 "(should have been sent direct)\n",
1788 libcfs_nid2str(from_nid),
1789 libcfs_nid2str(src_nid),
1790 libcfs_nid2str(dest_nid));
1794 if (lnet_islocalnid(dest_nid)) {
1795 /* dest is another local NI; sender should have used
1796 * this node's NID on its own network */
1797 CERROR ("%s, src %s: Bad dest nid %s "
1798 "(it's my nid but on a different network)\n",
1799 libcfs_nid2str(from_nid),
1800 libcfs_nid2str(src_nid),
1801 libcfs_nid2str(dest_nid));
1805 if (rdma_req && type == LNET_MSG_GET) {
1806 CERROR ("%s, src %s: Bad optimized GET for %s "
1807 "(final destination must be me)\n",
1808 libcfs_nid2str(from_nid),
1809 libcfs_nid2str(src_nid),
1810 libcfs_nid2str(dest_nid));
1814 if (!the_lnet.ln_routing) {
1815 CERROR ("%s, src %s: Dropping message for %s "
1816 "(routing not enabled)\n",
1817 libcfs_nid2str(from_nid),
1818 libcfs_nid2str(src_nid),
1819 libcfs_nid2str(dest_nid));
1824 /* Message looks OK; we're not going to return an error, so we MUST
1825 * call back lnd_recv() come what may... */
1827 if (!cfs_list_empty (&the_lnet.ln_test_peers) && /* normally we don't */
1828 fail_peer (src_nid, 0)) /* shall we now? */
1830 CERROR("%s, src %s: Dropping %s to simulate failure\n",
1831 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
1832 lnet_msgtyp2str(type));
1836 msg = lnet_msg_alloc();
1838 CERROR("%s, src %s: Dropping %s (out of memory)\n",
1839 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
1840 lnet_msgtyp2str(type));
1844 /* msg zeroed in lnet_msg_alloc; i.e. flags all clear, pointers NULL etc */
1846 msg->msg_type = type;
1847 msg->msg_private = private;
1848 msg->msg_receiving = 1;
1849 msg->msg_len = msg->msg_wanted = payload_length;
1850 msg->msg_offset = 0;
1851 msg->msg_hdr = *hdr;
1852 /* for building message event */
1853 msg->msg_from = from_nid;
1856 rc = lnet_nid2peer_locked(&msg->msg_rxpeer, from_nid);
1859 CERROR("%s, src %s: Dropping %s "
1860 "(error %d looking up sender)\n",
1861 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
1862 lnet_msgtyp2str(type), rc);
1867 lnet_msg_commit(msg, 0);
1874 msg->msg_target.pid = dest_pid;
1875 msg->msg_target.nid = dest_nid;
1876 msg->msg_routing = 1;
1877 msg->msg_offset = 0;
1880 if (msg->msg_rxpeer->lp_rtrcredits <= 0 ||
1881 lnet_msg2bufpool(msg)->rbp_credits <= 0) {
1882 rc = lnet_eager_recv_locked(msg);
1888 rc = lnet_post_routed_recv_locked(msg, 0);
1892 lnet_ni_recv(ni, msg->msg_private, msg, 0,
1893 0, payload_length, payload_length);
1897 /* convert common msg->hdr fields to host byteorder */
1898 msg->msg_hdr.type = type;
1899 msg->msg_hdr.src_nid = src_nid;
1900 msg->msg_hdr.src_pid = le32_to_cpu(msg->msg_hdr.src_pid);
1901 msg->msg_hdr.dest_nid = dest_nid;
1902 msg->msg_hdr.dest_pid = dest_pid;
1903 msg->msg_hdr.payload_length = payload_length;
1907 rc = lnet_parse_ack(ni, msg);
1910 rc = lnet_parse_put(ni, msg);
1913 rc = lnet_parse_get(ni, msg, rdma_req);
1915 case LNET_MSG_REPLY:
1916 rc = lnet_parse_reply(ni, msg);
1921 goto free_drop; /* prevent an unused label if !kernel */
1927 LASSERT (rc == ENOENT);
1930 LASSERT(msg->msg_md == NULL);
1931 lnet_finalize(ni, msg, rc);
1934 lnet_drop_message(ni, private, payload_length);
1939 lnet_drop_delayed_msg_list(cfs_list_t *head, char *reason)
1941 while (!cfs_list_empty(head)) {
1942 lnet_process_id_t id = {0};
1945 msg = cfs_list_entry(head->next, lnet_msg_t, msg_list);
1946 cfs_list_del(&msg->msg_list);
1948 id.nid = msg->msg_hdr.src_nid;
1949 id.pid = msg->msg_hdr.src_pid;
1951 LASSERT(msg->msg_md == NULL);
1952 LASSERT(msg->msg_delayed);
1953 LASSERT(msg->msg_rxpeer != NULL);
1954 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
1956 CWARN("Dropping delayed PUT from %s portal %d match "LPU64
1957 " offset %d length %d: %s\n",
1959 msg->msg_hdr.msg.put.ptl_index,
1960 msg->msg_hdr.msg.put.match_bits,
1961 msg->msg_hdr.msg.put.offset,
1962 msg->msg_hdr.payload_length, reason);
1964 /* NB I can't drop msg's ref on msg_rxpeer until after I've
1965 * called lnet_drop_message(), so I just hang onto msg as well
1966 * until that's done */
1968 lnet_drop_message(msg->msg_rxpeer->lp_ni,
1969 msg->msg_private, msg->msg_len);
1972 lnet_peer_decref_locked(msg->msg_rxpeer);
1980 lnet_recv_delayed_msg_list(cfs_list_t *head)
1982 while (!cfs_list_empty(head)) {
1984 lnet_process_id_t id;
1986 msg = cfs_list_entry(head->next, lnet_msg_t, msg_list);
1987 cfs_list_del(&msg->msg_list);
1989 /* md won't disappear under me, since each msg
1990 * holds a ref on it */
1992 id.nid = msg->msg_hdr.src_nid;
1993 id.pid = msg->msg_hdr.src_pid;
1995 LASSERT(msg->msg_delayed);
1996 LASSERT(msg->msg_md != NULL);
1997 LASSERT(msg->msg_rxpeer != NULL);
1998 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
2000 CDEBUG(D_NET, "Resuming delayed PUT from %s portal %d "
2001 "match "LPU64" offset %d length %d.\n",
2002 libcfs_id2str(id), msg->msg_hdr.msg.put.ptl_index,
2003 msg->msg_hdr.msg.put.match_bits,
2004 msg->msg_hdr.msg.put.offset,
2005 msg->msg_hdr.payload_length);
2007 lnet_recv_put(msg->msg_rxpeer->lp_ni, msg);
2012 * Initiate an asynchronous PUT operation.
2014 * There are several events associated with a PUT: completion of the send on
2015 * the initiator node (LNET_EVENT_SEND), and when the send completes
2016 * successfully, the receipt of an acknowledgment (LNET_EVENT_ACK) indicating
2017 * that the operation was accepted by the target. The event LNET_EVENT_PUT is
2018 * used at the target node to indicate the completion of incoming data
2021 * The local events will be logged in the EQ associated with the MD pointed to
2022 * by \a mdh handle. Using a MD without an associated EQ results in these
2023 * events being discarded. In this case, the caller must have another
2024 * mechanism (e.g., a higher level protocol) for determining when it is safe
2025 * to modify the memory region associated with the MD.
2027 * Note that LNet does not guarantee the order of LNET_EVENT_SEND and
2028 * LNET_EVENT_ACK, though intuitively ACK should happen after SEND.
2030 * \param self Indicates the NID of a local interface through which to send
2031 * the PUT request. Use LNET_NID_ANY to let LNet choose one by itself.
2032 * \param mdh A handle for the MD that describes the memory to be sent. The MD
2033 * must be "free floating" (See LNetMDBind()).
2034 * \param ack Controls whether an acknowledgment is requested.
2035 * Acknowledgments are only sent when they are requested by the initiating
2036 * process and the target MD enables them.
2037 * \param target A process identifier for the target process.
2038 * \param portal The index in the \a target's portal table.
2039 * \param match_bits The match bits to use for MD selection at the target
2041 * \param offset The offset into the target MD (only used when the target
2042 * MD has the LNET_MD_MANAGE_REMOTE option set).
2043 * \param hdr_data 64 bits of user data that can be included in the message
2044 * header. This data is written to an event queue entry at the target if an
2045 * EQ is present on the matching MD.
2047 * \retval 0 Success, and only in this case events will be generated
2048 * and logged to EQ (if it exists).
2049 * \retval -EIO Simulated failure.
2050 * \retval -ENOMEM Memory allocation failure.
2051 * \retval -ENOENT Invalid MD object.
2053 * \see lnet_event_t::hdr_data and lnet_event_kind_t.
2056 LNetPut(lnet_nid_t self, lnet_handle_md_t mdh, lnet_ack_req_t ack,
2057 lnet_process_id_t target, unsigned int portal,
2058 __u64 match_bits, unsigned int offset,
2065 LASSERT (the_lnet.ln_init);
2066 LASSERT (the_lnet.ln_refcount > 0);
2068 if (!cfs_list_empty (&the_lnet.ln_test_peers) && /* normally we don't */
2069 fail_peer (target.nid, 1)) /* shall we now? */
2071 CERROR("Dropping PUT to %s: simulated failure\n",
2072 libcfs_id2str(target));
2076 msg = lnet_msg_alloc();
2078 CERROR("Dropping PUT to %s: ENOMEM on lnet_msg_t\n",
2079 libcfs_id2str(target));
2082 msg->msg_vmflush = !!cfs_memory_pressure_get();
2086 md = lnet_handle2md(&mdh);
2087 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
2088 lnet_msg_free_locked(msg);
2090 CERROR("Dropping PUT ("LPU64":%d:%s): MD (%d) invalid\n",
2091 match_bits, portal, libcfs_id2str(target),
2092 md == NULL ? -1 : md->md_threshold);
2093 if (md != NULL && md->md_me != NULL)
2094 CERROR("Source MD also attached to portal %d\n",
2095 md->md_me->me_portal);
2101 CDEBUG(D_NET, "LNetPut -> %s\n", libcfs_id2str(target));
2103 lnet_msg_attach_md(msg, md, 0, 0);
2105 lnet_prep_send(msg, LNET_MSG_PUT, target, 0, md->md_length);
2107 msg->msg_hdr.msg.put.match_bits = cpu_to_le64(match_bits);
2108 msg->msg_hdr.msg.put.ptl_index = cpu_to_le32(portal);
2109 msg->msg_hdr.msg.put.offset = cpu_to_le32(offset);
2110 msg->msg_hdr.msg.put.hdr_data = hdr_data;
2112 /* NB handles only looked up by creator (no flips) */
2113 if (ack == LNET_ACK_REQ) {
2114 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
2115 the_lnet.ln_interface_cookie;
2116 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
2117 md->md_lh.lh_cookie;
2119 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
2120 LNET_WIRE_HANDLE_COOKIE_NONE;
2121 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
2122 LNET_WIRE_HANDLE_COOKIE_NONE;
2127 lnet_build_msg_event(msg, LNET_EVENT_SEND);
2129 rc = lnet_send(self, msg);
2131 CNETERR( "Error sending PUT to %s: %d\n",
2132 libcfs_id2str(target), rc);
2133 lnet_finalize (NULL, msg, rc);
2136 /* completion will be signalled by an event */
2141 lnet_create_reply_msg (lnet_ni_t *ni, lnet_msg_t *getmsg)
2143 /* The LND can DMA direct to the GET md (i.e. no REPLY msg). This
2144 * returns a msg for the LND to pass to lnet_finalize() when the sink
2145 * data has been received.
2147 * CAVEAT EMPTOR: 'getmsg' is the original GET, which is freed when
2148 * lnet_finalize() is called on it, so the LND must call this first */
2150 lnet_msg_t *msg = lnet_msg_alloc();
2151 lnet_libmd_t *getmd = getmsg->msg_md;
2152 lnet_process_id_t peer_id = getmsg->msg_target;
2154 LASSERT (!getmsg->msg_target_is_router);
2155 LASSERT (!getmsg->msg_routing);
2159 LASSERT (getmd->md_refcount > 0);
2162 CERROR ("%s: Dropping REPLY from %s: can't allocate msg\n",
2163 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id));
2167 if (getmd->md_threshold == 0) {
2168 CERROR ("%s: Dropping REPLY from %s for inactive MD %p\n",
2169 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id),
2174 LASSERT (getmd->md_offset == 0);
2176 CDEBUG(D_NET, "%s: Reply from %s md %p\n",
2177 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id), getmd);
2179 /* setup information for lnet_build_msg_event */
2180 msg->msg_from = peer_id.nid;
2181 msg->msg_type = LNET_MSG_GET; /* flag this msg as an "optimized" GET */
2182 msg->msg_hdr.src_nid = peer_id.nid;
2183 msg->msg_hdr.payload_length = getmd->md_length;
2184 msg->msg_receiving = 1; /* required by lnet_msg_attach_md */
2186 lnet_msg_attach_md(msg, getmd, getmd->md_offset, getmd->md_length);
2187 lnet_msg_commit(msg, 0);
2191 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
2196 lnet_msg_free_locked(msg);
2198 the_lnet.ln_counters.drop_count++;
2199 the_lnet.ln_counters.drop_length += getmd->md_length;
2207 lnet_set_reply_msg_len(lnet_ni_t *ni, lnet_msg_t *reply, unsigned int len)
2209 /* Set the REPLY length, now the RDMA that elides the REPLY message has
2210 * completed and I know it. */
2211 LASSERT (reply != NULL);
2212 LASSERT (reply->msg_type == LNET_MSG_GET);
2213 LASSERT (reply->msg_ev.type == LNET_EVENT_REPLY);
2215 /* NB I trusted my peer to RDMA. If she tells me she's written beyond
2216 * the end of my buffer, I might as well be dead. */
2217 LASSERT (len <= reply->msg_ev.mlength);
2219 reply->msg_ev.mlength = len;
2223 * Initiate an asynchronous GET operation.
2225 * On the initiator node, an LNET_EVENT_SEND is logged when the GET request
2226 * is sent, and an LNET_EVENT_REPLY is logged when the data returned from
2227 * the target node in the REPLY has been written to local MD.
2229 * On the target node, an LNET_EVENT_GET is logged when the GET request
2230 * arrives and is accepted into a MD.
2232 * \param self,target,portal,match_bits,offset See the discussion in LNetPut().
2233 * \param mdh A handle for the MD that describes the memory into which the
2234 * requested data will be received. The MD must be "free floating" (See LNetMDBind()).
2236 * \retval 0 Success, and only in this case events will be generated
2237 * and logged to EQ (if it exists) of the MD.
2238 * \retval -EIO Simulated failure.
2239 * \retval -ENOMEM Memory allocation failure.
2240 * \retval -ENOENT Invalid MD object.
2243 LNetGet(lnet_nid_t self, lnet_handle_md_t mdh,
2244 lnet_process_id_t target, unsigned int portal,
2245 __u64 match_bits, unsigned int offset)
2251 LASSERT (the_lnet.ln_init);
2252 LASSERT (the_lnet.ln_refcount > 0);
2254 if (!cfs_list_empty (&the_lnet.ln_test_peers) && /* normally we don't */
2255 fail_peer (target.nid, 1)) /* shall we now? */
2257 CERROR("Dropping GET to %s: simulated failure\n",
2258 libcfs_id2str(target));
2262 msg = lnet_msg_alloc();
2264 CERROR("Dropping GET to %s: ENOMEM on lnet_msg_t\n",
2265 libcfs_id2str(target));
2271 md = lnet_handle2md(&mdh);
2272 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
2273 lnet_msg_free_locked(msg);
2275 CERROR("Dropping GET ("LPU64":%d:%s): MD (%d) invalid\n",
2276 match_bits, portal, libcfs_id2str(target),
2277 md == NULL ? -1 : md->md_threshold);
2278 if (md != NULL && md->md_me != NULL)
2279 CERROR("REPLY MD also attached to portal %d\n",
2280 md->md_me->me_portal);
2286 CDEBUG(D_NET, "LNetGet -> %s\n", libcfs_id2str(target));
2288 lnet_msg_attach_md(msg, md, 0, 0);
2290 lnet_prep_send(msg, LNET_MSG_GET, target, 0, 0);
2292 msg->msg_hdr.msg.get.match_bits = cpu_to_le64(match_bits);
2293 msg->msg_hdr.msg.get.ptl_index = cpu_to_le32(portal);
2294 msg->msg_hdr.msg.get.src_offset = cpu_to_le32(offset);
2295 msg->msg_hdr.msg.get.sink_length = cpu_to_le32(md->md_length);
2297 /* NB handles only looked up by creator (no flips) */
2298 msg->msg_hdr.msg.get.return_wmd.wh_interface_cookie =
2299 the_lnet.ln_interface_cookie;
2300 msg->msg_hdr.msg.get.return_wmd.wh_object_cookie =
2301 md->md_lh.lh_cookie;
2305 lnet_build_msg_event(msg, LNET_EVENT_SEND);
2307 rc = lnet_send(self, msg);
2309 CNETERR( "Error sending GET to %s: %d\n",
2310 libcfs_id2str(target), rc);
2311 lnet_finalize (NULL, msg, rc);
2314 /* completion will be signalled by an event */
2319 * Calculate distance to node at \a dstnid.
2321 * \param dstnid Target NID.
2322 * \param srcnidp If not NULL, NID of the local interface to reach \a dstnid
2324 * \param orderp If not NULL, order of the route to reach \a dstnid is saved
2327 * \retval 0 If \a dstnid belongs to a local interface, and reserved option
2328 * local_nid_dist_zero is set, which is the default.
2329 * \retval positives Distance to target NID, i.e. number of hops plus one.
2330 * \retval -EHOSTUNREACH If \a dstnid is not reachable.
2333 LNetDist (lnet_nid_t dstnid, lnet_nid_t *srcnidp, __u32 *orderp)
2337 lnet_remotenet_t *rnet;
2338 __u32 dstnet = LNET_NIDNET(dstnid);
2342 /* if !local_nid_dist_zero, I don't return a distance of 0 ever
2343 * (when lustre sees a distance of 0, it substitutes 0@lo), so I
2344 * keep order 0 free for 0@lo and order 1 free for a local NID
2347 LASSERT (the_lnet.ln_init);
2348 LASSERT (the_lnet.ln_refcount > 0);
2352 cfs_list_for_each (e, &the_lnet.ln_nis) {
2353 ni = cfs_list_entry(e, lnet_ni_t, ni_list);
2355 if (ni->ni_nid == dstnid) {
2356 if (srcnidp != NULL)
2358 if (orderp != NULL) {
2359 if (LNET_NETTYP(LNET_NIDNET(dstnid)) == LOLND)
2366 return local_nid_dist_zero ? 0 : 1;
2369 if (LNET_NIDNET(ni->ni_nid) == dstnet) {
2370 if (srcnidp != NULL)
2371 *srcnidp = ni->ni_nid;
2381 cfs_list_for_each (e, &the_lnet.ln_remote_nets) {
2382 rnet = cfs_list_entry(e, lnet_remotenet_t, lrn_list);
2384 if (rnet->lrn_net == dstnet) {
2385 lnet_route_t *route;
2386 lnet_route_t *shortest = NULL;
2388 LASSERT (!cfs_list_empty(&rnet->lrn_routes));
2390 cfs_list_for_each_entry(route, &rnet->lrn_routes,
2392 if (shortest == NULL ||
2393 route->lr_hops < shortest->lr_hops)
2397 LASSERT (shortest != NULL);
2398 hops = shortest->lr_hops;
2399 if (srcnidp != NULL)
2400 *srcnidp = shortest->lr_gateway->lp_ni->ni_nid;
2410 return -EHOSTUNREACH;
2414 * Set the number of asynchronous messages expected from a target process.
2416 * This function is only meaningful for userspace callers. It's a no-op when
2417 * called from kernel.
2419 * Asynchronous messages are those that can come from a target when the
2420 * userspace process is not waiting for IO to complete; e.g., AST callbacks
2421 * from Lustre servers. Specifying the expected number of such messages
2422 * allows them to be eagerly received when user process is not running in
2423 * LNet; otherwise network errors may occur.
2425 * \param id Process ID of the target process.
2426 * \param nasync Number of asynchronous messages expected from the target.
2428 * \return 0 on success, and an error code otherwise.
2431 LNetSetAsync(lnet_process_id_t id, int nasync)
2437 lnet_remotenet_t *rnet;
2439 lnet_route_t *route;
2446 /* Target on a local network? */
2447 ni = lnet_net2ni(LNET_NIDNET(id.nid));
2449 if (ni->ni_lnd->lnd_setasync != NULL)
2450 rc = (ni->ni_lnd->lnd_setasync)(ni, id, nasync);
2455 /* Target on a remote network: apply to routers */
2457 LIBCFS_ALLOC(nids, maxnids * sizeof(*nids));
2462 /* Snapshot all the router NIDs */
2464 rnet = lnet_find_net_locked(LNET_NIDNET(id.nid));
2466 cfs_list_for_each(tmp, &rnet->lrn_routes) {
2467 if (nnids == maxnids) {
2469 LIBCFS_FREE(nids, maxnids * sizeof(*nids));
2474 route = cfs_list_entry(tmp, lnet_route_t, lr_list);
2475 nids[nnids++] = route->lr_gateway->lp_nid;
2480 /* set async on all the routers */
2481 while (nnids-- > 0) {
2482 id.pid = LUSTRE_SRV_LNET_PID;
2483 id.nid = nids[nnids];
2485 ni = lnet_net2ni(LNET_NIDNET(id.nid));
2489 if (ni->ni_lnd->lnd_setasync != NULL) {
2490 rc2 = (ni->ni_lnd->lnd_setasync)(ni, id, nasync);
2497 LIBCFS_FREE(nids, maxnids * sizeof(*nids));