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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lnet/lnet/lib-move.c
34 * Data movement routines
37 #define DEBUG_SUBSYSTEM S_LNET
39 #include <lnet/lib-lnet.h>
40 #include <linux/nsproxy.h>
41 #include <net/net_namespace.h>
43 static int local_nid_dist_zero = 1;
44 module_param(local_nid_dist_zero, int, 0444);
45 MODULE_PARM_DESC(local_nid_dist_zero, "Reserved");
48 lnet_fail_nid(lnet_nid_t nid, unsigned int threshold)
52 struct list_head *next;
53 struct list_head cull;
55 /* NB: use lnet_net_lock(0) to serialize operations on test peers */
57 /* Adding a new entry */
58 LIBCFS_ALLOC(tp, sizeof(*tp));
63 tp->tp_threshold = threshold;
66 list_add_tail(&tp->tp_list, &the_lnet.ln_test_peers);
71 /* removing entries */
72 INIT_LIST_HEAD(&cull);
76 list_for_each_safe(el, next, &the_lnet.ln_test_peers) {
77 tp = list_entry(el, lnet_test_peer_t, tp_list);
79 if (tp->tp_threshold == 0 || /* needs culling anyway */
80 nid == LNET_NID_ANY || /* removing all entries */
81 tp->tp_nid == nid) { /* matched this one */
82 list_del(&tp->tp_list);
83 list_add(&tp->tp_list, &cull);
89 while (!list_empty(&cull)) {
90 tp = list_entry(cull.next, lnet_test_peer_t, tp_list);
92 list_del(&tp->tp_list);
93 LIBCFS_FREE(tp, sizeof(*tp));
99 fail_peer (lnet_nid_t nid, int outgoing)
101 lnet_test_peer_t *tp;
102 struct list_head *el;
103 struct list_head *next;
104 struct list_head cull;
107 INIT_LIST_HEAD(&cull);
109 /* NB: use lnet_net_lock(0) to serialize operations on test peers */
112 list_for_each_safe(el, next, &the_lnet.ln_test_peers) {
113 tp = list_entry(el, lnet_test_peer_t, tp_list);
115 if (tp->tp_threshold == 0) {
118 /* only cull zombies on outgoing tests,
119 * since we may be at interrupt priority on
120 * incoming messages. */
121 list_del(&tp->tp_list);
122 list_add(&tp->tp_list, &cull);
127 if (tp->tp_nid == LNET_NID_ANY || /* fail every peer */
128 nid == tp->tp_nid) { /* fail this peer */
131 if (tp->tp_threshold != LNET_MD_THRESH_INF) {
134 tp->tp_threshold == 0) {
136 list_del(&tp->tp_list);
137 list_add(&tp->tp_list, &cull);
146 while (!list_empty(&cull)) {
147 tp = list_entry(cull.next, lnet_test_peer_t, tp_list);
148 list_del(&tp->tp_list);
150 LIBCFS_FREE(tp, sizeof(*tp));
157 lnet_iov_nob(unsigned int niov, struct kvec *iov)
159 unsigned int nob = 0;
161 LASSERT(niov == 0 || iov != NULL);
163 nob += (iov++)->iov_len;
167 EXPORT_SYMBOL(lnet_iov_nob);
170 lnet_copy_iov2iov(unsigned int ndiov, struct kvec *diov, unsigned int doffset,
171 unsigned int nsiov, struct kvec *siov, unsigned int soffset,
174 /* NB diov, siov are READ-ONLY */
175 unsigned int this_nob;
180 /* skip complete frags before 'doffset' */
182 while (doffset >= diov->iov_len) {
183 doffset -= diov->iov_len;
189 /* skip complete frags before 'soffset' */
191 while (soffset >= siov->iov_len) {
192 soffset -= siov->iov_len;
201 this_nob = MIN(diov->iov_len - doffset,
202 siov->iov_len - soffset);
203 this_nob = MIN(this_nob, nob);
205 memcpy((char *)diov->iov_base + doffset,
206 (char *)siov->iov_base + soffset, this_nob);
209 if (diov->iov_len > doffset + this_nob) {
217 if (siov->iov_len > soffset + this_nob) {
226 EXPORT_SYMBOL(lnet_copy_iov2iov);
229 lnet_extract_iov(int dst_niov, struct kvec *dst,
230 int src_niov, struct kvec *src,
231 unsigned int offset, unsigned int len)
233 /* Initialise 'dst' to the subset of 'src' starting at 'offset',
234 * for exactly 'len' bytes, and return the number of entries.
235 * NB not destructive to 'src' */
236 unsigned int frag_len;
239 if (len == 0) /* no data => */
240 return (0); /* no frags */
242 LASSERT(src_niov > 0);
243 while (offset >= src->iov_len) { /* skip initial frags */
244 offset -= src->iov_len;
247 LASSERT(src_niov > 0);
252 LASSERT(src_niov > 0);
253 LASSERT((int)niov <= dst_niov);
255 frag_len = src->iov_len - offset;
256 dst->iov_base = ((char *)src->iov_base) + offset;
258 if (len <= frag_len) {
263 dst->iov_len = frag_len;
273 EXPORT_SYMBOL(lnet_extract_iov);
277 lnet_kiov_nob(unsigned int niov, lnet_kiov_t *kiov)
279 unsigned int nob = 0;
281 LASSERT(niov == 0 || kiov != NULL);
283 nob += (kiov++)->kiov_len;
287 EXPORT_SYMBOL(lnet_kiov_nob);
290 lnet_copy_kiov2kiov(unsigned int ndiov, lnet_kiov_t *diov, unsigned int doffset,
291 unsigned int nsiov, lnet_kiov_t *siov, unsigned int soffset,
294 /* NB diov, siov are READ-ONLY */
295 unsigned int this_nob;
302 LASSERT (!in_interrupt ());
305 while (doffset >= diov->kiov_len) {
306 doffset -= diov->kiov_len;
313 while (soffset >= siov->kiov_len) {
314 soffset -= siov->kiov_len;
323 this_nob = MIN(diov->kiov_len - doffset,
324 siov->kiov_len - soffset);
325 this_nob = MIN(this_nob, nob);
328 daddr = ((char *)kmap(diov->kiov_page)) +
329 diov->kiov_offset + doffset;
331 saddr = ((char *)kmap(siov->kiov_page)) +
332 siov->kiov_offset + soffset;
334 /* Vanishing risk of kmap deadlock when mapping 2 pages.
335 * However in practice at least one of the kiovs will be mapped
336 * kernel pages and the map/unmap will be NOOPs */
338 memcpy (daddr, saddr, this_nob);
341 if (diov->kiov_len > doffset + this_nob) {
345 kunmap(diov->kiov_page);
352 if (siov->kiov_len > soffset + this_nob) {
356 kunmap(siov->kiov_page);
365 kunmap(diov->kiov_page);
367 kunmap(siov->kiov_page);
369 EXPORT_SYMBOL(lnet_copy_kiov2kiov);
372 lnet_copy_kiov2iov (unsigned int niov, struct kvec *iov, unsigned int iovoffset,
373 unsigned int nkiov, lnet_kiov_t *kiov, unsigned int kiovoffset,
376 /* NB iov, kiov are READ-ONLY */
377 unsigned int this_nob;
383 LASSERT (!in_interrupt ());
386 while (iovoffset >= iov->iov_len) {
387 iovoffset -= iov->iov_len;
394 while (kiovoffset >= kiov->kiov_len) {
395 kiovoffset -= kiov->kiov_len;
404 this_nob = MIN(iov->iov_len - iovoffset,
405 kiov->kiov_len - kiovoffset);
406 this_nob = MIN(this_nob, nob);
409 addr = ((char *)kmap(kiov->kiov_page)) +
410 kiov->kiov_offset + kiovoffset;
412 memcpy((char *)iov->iov_base + iovoffset, addr, this_nob);
415 if (iov->iov_len > iovoffset + this_nob) {
416 iovoffset += this_nob;
423 if (kiov->kiov_len > kiovoffset + this_nob) {
425 kiovoffset += this_nob;
427 kunmap(kiov->kiov_page);
437 kunmap(kiov->kiov_page);
439 EXPORT_SYMBOL(lnet_copy_kiov2iov);
442 lnet_copy_iov2kiov(unsigned int nkiov, lnet_kiov_t *kiov, unsigned int kiovoffset,
443 unsigned int niov, struct kvec *iov, unsigned int iovoffset,
446 /* NB kiov, iov are READ-ONLY */
447 unsigned int this_nob;
453 LASSERT (!in_interrupt ());
456 while (kiovoffset >= kiov->kiov_len) {
457 kiovoffset -= kiov->kiov_len;
464 while (iovoffset >= iov->iov_len) {
465 iovoffset -= iov->iov_len;
474 this_nob = MIN(kiov->kiov_len - kiovoffset,
475 iov->iov_len - iovoffset);
476 this_nob = MIN(this_nob, nob);
479 addr = ((char *)kmap(kiov->kiov_page)) +
480 kiov->kiov_offset + kiovoffset;
482 memcpy (addr, (char *)iov->iov_base + iovoffset, this_nob);
485 if (kiov->kiov_len > kiovoffset + this_nob) {
487 kiovoffset += this_nob;
489 kunmap(kiov->kiov_page);
496 if (iov->iov_len > iovoffset + this_nob) {
497 iovoffset += this_nob;
506 kunmap(kiov->kiov_page);
508 EXPORT_SYMBOL(lnet_copy_iov2kiov);
511 lnet_extract_kiov(int dst_niov, lnet_kiov_t *dst,
512 int src_niov, lnet_kiov_t *src,
513 unsigned int offset, unsigned int len)
515 /* Initialise 'dst' to the subset of 'src' starting at 'offset',
516 * for exactly 'len' bytes, and return the number of entries.
517 * NB not destructive to 'src' */
518 unsigned int frag_len;
521 if (len == 0) /* no data => */
522 return (0); /* no frags */
524 LASSERT(src_niov > 0);
525 while (offset >= src->kiov_len) { /* skip initial frags */
526 offset -= src->kiov_len;
529 LASSERT(src_niov > 0);
534 LASSERT(src_niov > 0);
535 LASSERT((int)niov <= dst_niov);
537 frag_len = src->kiov_len - offset;
538 dst->kiov_page = src->kiov_page;
539 dst->kiov_offset = src->kiov_offset + offset;
541 if (len <= frag_len) {
543 LASSERT(dst->kiov_offset + dst->kiov_len <= PAGE_SIZE);
547 dst->kiov_len = frag_len;
548 LASSERT(dst->kiov_offset + dst->kiov_len <= PAGE_SIZE);
558 EXPORT_SYMBOL(lnet_extract_kiov);
561 lnet_ni_recv(lnet_ni_t *ni, void *private, lnet_msg_t *msg, int delayed,
562 unsigned int offset, unsigned int mlen, unsigned int rlen)
564 unsigned int niov = 0;
565 struct kvec *iov = NULL;
566 lnet_kiov_t *kiov = NULL;
569 LASSERT (!in_interrupt ());
570 LASSERT (mlen == 0 || msg != NULL);
573 LASSERT(msg->msg_receiving);
574 LASSERT(!msg->msg_sending);
575 LASSERT(rlen == msg->msg_len);
576 LASSERT(mlen <= msg->msg_len);
577 LASSERT(msg->msg_offset == offset);
578 LASSERT(msg->msg_wanted == mlen);
580 msg->msg_receiving = 0;
583 niov = msg->msg_niov;
585 kiov = msg->msg_kiov;
588 LASSERT ((iov == NULL) != (kiov == NULL));
592 rc = (ni->ni_net->net_lnd->lnd_recv)(ni, private, msg, delayed,
593 niov, iov, kiov, offset, mlen,
596 lnet_finalize(ni, msg, rc);
600 lnet_setpayloadbuffer(lnet_msg_t *msg)
602 lnet_libmd_t *md = msg->msg_md;
604 LASSERT(msg->msg_len > 0);
605 LASSERT(!msg->msg_routing);
607 LASSERT(msg->msg_niov == 0);
608 LASSERT(msg->msg_iov == NULL);
609 LASSERT(msg->msg_kiov == NULL);
611 msg->msg_niov = md->md_niov;
612 if ((md->md_options & LNET_MD_KIOV) != 0)
613 msg->msg_kiov = md->md_iov.kiov;
615 msg->msg_iov = md->md_iov.iov;
619 lnet_prep_send(lnet_msg_t *msg, int type, lnet_process_id_t target,
620 unsigned int offset, unsigned int len)
622 msg->msg_type = type;
623 msg->msg_target = target;
625 msg->msg_offset = offset;
628 lnet_setpayloadbuffer(msg);
630 memset (&msg->msg_hdr, 0, sizeof (msg->msg_hdr));
631 msg->msg_hdr.type = cpu_to_le32(type);
632 msg->msg_hdr.dest_pid = cpu_to_le32(target.pid);
633 /* src_nid will be set later */
634 msg->msg_hdr.src_pid = cpu_to_le32(the_lnet.ln_pid);
635 msg->msg_hdr.payload_length = cpu_to_le32(len);
639 lnet_ni_send(lnet_ni_t *ni, lnet_msg_t *msg)
641 void *priv = msg->msg_private;
644 LASSERT (!in_interrupt ());
645 LASSERT (LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND ||
646 (msg->msg_txcredit && msg->msg_peertxcredit));
648 rc = (ni->ni_net->net_lnd->lnd_send)(ni, priv, msg);
650 lnet_finalize(ni, msg, rc);
654 lnet_ni_eager_recv(lnet_ni_t *ni, lnet_msg_t *msg)
658 LASSERT(!msg->msg_sending);
659 LASSERT(msg->msg_receiving);
660 LASSERT(!msg->msg_rx_ready_delay);
661 LASSERT(ni->ni_net->net_lnd->lnd_eager_recv != NULL);
663 msg->msg_rx_ready_delay = 1;
664 rc = (ni->ni_net->net_lnd->lnd_eager_recv)(ni, msg->msg_private, msg,
667 CERROR("recv from %s / send to %s aborted: "
668 "eager_recv failed %d\n",
669 libcfs_nid2str(msg->msg_rxpeer->lpni_nid),
670 libcfs_id2str(msg->msg_target), rc);
671 LASSERT(rc < 0); /* required by my callers */
677 /* NB: caller shall hold a ref on 'lp' as I'd drop lnet_net_lock */
679 lnet_ni_query_locked(lnet_ni_t *ni, struct lnet_peer_ni *lp)
681 cfs_time_t last_alive = 0;
683 LASSERT(lnet_peer_aliveness_enabled(lp));
684 LASSERT(ni->ni_net->net_lnd->lnd_query != NULL);
686 lnet_net_unlock(lp->lpni_cpt);
687 (ni->ni_net->net_lnd->lnd_query)(ni, lp->lpni_nid, &last_alive);
688 lnet_net_lock(lp->lpni_cpt);
690 lp->lpni_last_query = cfs_time_current();
692 if (last_alive != 0) /* NI has updated timestamp */
693 lp->lpni_last_alive = last_alive;
696 /* NB: always called with lnet_net_lock held */
698 lnet_peer_is_alive (struct lnet_peer_ni *lp, cfs_time_t now)
703 LASSERT (lnet_peer_aliveness_enabled(lp));
706 * Trust lnet_notify() if it has more recent aliveness news, but
707 * ignore the initial assumed death (see lnet_peers_start_down()).
709 if (!lp->lpni_alive && lp->lpni_alive_count > 0 &&
710 cfs_time_aftereq(lp->lpni_timestamp, lp->lpni_last_alive))
714 cfs_time_add(lp->lpni_last_alive,
715 cfs_time_seconds(lp->lpni_net->net_tunables.
717 alive = cfs_time_after(deadline, now);
720 * Update obsolete lp_alive except for routers assumed to be dead
721 * initially, because router checker would update aliveness in this
722 * case, and moreover lpni_last_alive at peer creation is assumed.
724 if (alive && !lp->lpni_alive &&
725 !(lnet_isrouter(lp) && lp->lpni_alive_count == 0))
726 lnet_notify_locked(lp, 0, 1, lp->lpni_last_alive);
732 /* NB: returns 1 when alive, 0 when dead, negative when error;
733 * may drop the lnet_net_lock */
735 lnet_peer_alive_locked (struct lnet_ni *ni, struct lnet_peer_ni *lp)
737 cfs_time_t now = cfs_time_current();
739 if (!lnet_peer_aliveness_enabled(lp))
742 if (lnet_peer_is_alive(lp, now))
746 * Peer appears dead, but we should avoid frequent NI queries (at
747 * most once per lnet_queryinterval seconds).
749 if (lp->lpni_last_query != 0) {
750 static const int lnet_queryinterval = 1;
752 cfs_time_t next_query =
753 cfs_time_add(lp->lpni_last_query,
754 cfs_time_seconds(lnet_queryinterval));
756 if (cfs_time_before(now, next_query)) {
758 CWARN("Unexpected aliveness of peer %s: "
760 libcfs_nid2str(lp->lpni_nid),
761 (int)now, (int)next_query,
763 lp->lpni_net->net_tunables.lct_peer_timeout);
768 /* query NI for latest aliveness news */
769 lnet_ni_query_locked(ni, lp);
771 if (lnet_peer_is_alive(lp, now))
774 lnet_notify_locked(lp, 0, 0, lp->lpni_last_alive);
779 * \param msg The message to be sent.
780 * \param do_send True if lnet_ni_send() should be called in this function.
781 * lnet_send() is going to lnet_net_unlock immediately after this, so
782 * it sets do_send FALSE and I don't do the unlock/send/lock bit.
784 * \retval LNET_CREDIT_OK If \a msg sent or OK to send.
785 * \retval LNET_CREDIT_WAIT If \a msg blocked for credit.
786 * \retval -EHOSTUNREACH If the next hop of the message appears dead.
787 * \retval -ECANCELED If the MD of the message has been unlinked.
790 lnet_post_send_locked(lnet_msg_t *msg, int do_send)
792 struct lnet_peer_ni *lp = msg->msg_txpeer;
793 struct lnet_ni *ni = msg->msg_txni;
794 int cpt = msg->msg_tx_cpt;
795 struct lnet_tx_queue *tq = ni->ni_tx_queues[cpt];
797 /* non-lnet_send() callers have checked before */
798 LASSERT(!do_send || msg->msg_tx_delayed);
799 LASSERT(!msg->msg_receiving);
800 LASSERT(msg->msg_tx_committed);
802 /* NB 'lp' is always the next hop */
803 if ((msg->msg_target.pid & LNET_PID_USERFLAG) == 0 &&
804 lnet_peer_alive_locked(ni, lp) == 0) {
805 the_lnet.ln_counters[cpt]->drop_count++;
806 the_lnet.ln_counters[cpt]->drop_length += msg->msg_len;
807 lnet_net_unlock(cpt);
809 CNETERR("Dropping message for %s: peer not alive\n",
810 libcfs_id2str(msg->msg_target));
812 lnet_finalize(ni, msg, -EHOSTUNREACH);
815 return -EHOSTUNREACH;
818 if (msg->msg_md != NULL &&
819 (msg->msg_md->md_flags & LNET_MD_FLAG_ABORTED) != 0) {
820 lnet_net_unlock(cpt);
822 CNETERR("Aborting message for %s: LNetM[DE]Unlink() already "
823 "called on the MD/ME.\n",
824 libcfs_id2str(msg->msg_target));
826 lnet_finalize(ni, msg, -ECANCELED);
832 if (!msg->msg_peertxcredit) {
833 LASSERT((lp->lpni_txcredits < 0) ==
834 !list_empty(&lp->lpni_txq));
836 msg->msg_peertxcredit = 1;
837 lp->lpni_txqnob += msg->msg_len + sizeof(lnet_hdr_t);
838 lp->lpni_txcredits--;
840 if (lp->lpni_txcredits < lp->lpni_mintxcredits)
841 lp->lpni_mintxcredits = lp->lpni_txcredits;
843 if (lp->lpni_txcredits < 0) {
844 msg->msg_tx_delayed = 1;
845 list_add_tail(&msg->msg_list, &lp->lpni_txq);
846 return LNET_CREDIT_WAIT;
850 if (!msg->msg_txcredit) {
851 LASSERT((tq->tq_credits < 0) ==
852 !list_empty(&tq->tq_delayed));
854 msg->msg_txcredit = 1;
857 if (tq->tq_credits < tq->tq_credits_min)
858 tq->tq_credits_min = tq->tq_credits;
860 if (tq->tq_credits < 0) {
861 msg->msg_tx_delayed = 1;
862 list_add_tail(&msg->msg_list, &tq->tq_delayed);
863 return LNET_CREDIT_WAIT;
868 lnet_net_unlock(cpt);
869 lnet_ni_send(ni, msg);
872 return LNET_CREDIT_OK;
876 static lnet_rtrbufpool_t *
877 lnet_msg2bufpool(lnet_msg_t *msg)
879 lnet_rtrbufpool_t *rbp;
882 LASSERT(msg->msg_rx_committed);
884 cpt = msg->msg_rx_cpt;
885 rbp = &the_lnet.ln_rtrpools[cpt][0];
887 LASSERT(msg->msg_len <= LNET_MTU);
888 while (msg->msg_len > (unsigned int)rbp->rbp_npages * PAGE_SIZE) {
890 LASSERT(rbp < &the_lnet.ln_rtrpools[cpt][LNET_NRBPOOLS]);
897 lnet_post_routed_recv_locked (lnet_msg_t *msg, int do_recv)
899 /* lnet_parse is going to lnet_net_unlock immediately after this, so it
900 * sets do_recv FALSE and I don't do the unlock/send/lock bit.
901 * I return LNET_CREDIT_WAIT if msg blocked and LNET_CREDIT_OK if
902 * received or OK to receive */
903 struct lnet_peer_ni *lp = msg->msg_rxpeer;
904 lnet_rtrbufpool_t *rbp;
907 LASSERT (msg->msg_iov == NULL);
908 LASSERT (msg->msg_kiov == NULL);
909 LASSERT (msg->msg_niov == 0);
910 LASSERT (msg->msg_routing);
911 LASSERT (msg->msg_receiving);
912 LASSERT (!msg->msg_sending);
914 /* non-lnet_parse callers only receive delayed messages */
915 LASSERT(!do_recv || msg->msg_rx_delayed);
917 if (!msg->msg_peerrtrcredit) {
918 LASSERT((lp->lpni_rtrcredits < 0) ==
919 !list_empty(&lp->lpni_rtrq));
921 msg->msg_peerrtrcredit = 1;
922 lp->lpni_rtrcredits--;
923 if (lp->lpni_rtrcredits < lp->lpni_minrtrcredits)
924 lp->lpni_minrtrcredits = lp->lpni_rtrcredits;
926 if (lp->lpni_rtrcredits < 0) {
927 /* must have checked eager_recv before here */
928 LASSERT(msg->msg_rx_ready_delay);
929 msg->msg_rx_delayed = 1;
930 list_add_tail(&msg->msg_list, &lp->lpni_rtrq);
931 return LNET_CREDIT_WAIT;
935 rbp = lnet_msg2bufpool(msg);
937 if (!msg->msg_rtrcredit) {
938 msg->msg_rtrcredit = 1;
940 if (rbp->rbp_credits < rbp->rbp_mincredits)
941 rbp->rbp_mincredits = rbp->rbp_credits;
943 if (rbp->rbp_credits < 0) {
944 /* must have checked eager_recv before here */
945 LASSERT(msg->msg_rx_ready_delay);
946 msg->msg_rx_delayed = 1;
947 list_add_tail(&msg->msg_list, &rbp->rbp_msgs);
948 return LNET_CREDIT_WAIT;
952 LASSERT(!list_empty(&rbp->rbp_bufs));
953 rb = list_entry(rbp->rbp_bufs.next, lnet_rtrbuf_t, rb_list);
954 list_del(&rb->rb_list);
956 msg->msg_niov = rbp->rbp_npages;
957 msg->msg_kiov = &rb->rb_kiov[0];
960 int cpt = msg->msg_rx_cpt;
962 lnet_net_unlock(cpt);
963 lnet_ni_recv(msg->msg_rxni, msg->msg_private, msg, 1,
964 0, msg->msg_len, msg->msg_len);
967 return LNET_CREDIT_OK;
971 lnet_return_tx_credits_locked(lnet_msg_t *msg)
973 struct lnet_peer_ni *txpeer = msg->msg_txpeer;
974 struct lnet_ni *txni = msg->msg_txni;
977 if (msg->msg_txcredit) {
978 struct lnet_ni *ni = msg->msg_txni;
979 struct lnet_tx_queue *tq = ni->ni_tx_queues[msg->msg_tx_cpt];
981 /* give back NI txcredits */
982 msg->msg_txcredit = 0;
984 LASSERT((tq->tq_credits < 0) ==
985 !list_empty(&tq->tq_delayed));
988 if (tq->tq_credits <= 0) {
989 msg2 = list_entry(tq->tq_delayed.next,
990 lnet_msg_t, msg_list);
991 list_del(&msg2->msg_list);
993 LASSERT(msg2->msg_txni == ni);
994 LASSERT(msg2->msg_tx_delayed);
996 (void) lnet_post_send_locked(msg2, 1);
1000 if (msg->msg_peertxcredit) {
1001 /* give back peer txcredits */
1002 msg->msg_peertxcredit = 0;
1004 LASSERT((txpeer->lpni_txcredits < 0) ==
1005 !list_empty(&txpeer->lpni_txq));
1007 txpeer->lpni_txqnob -= msg->msg_len + sizeof(lnet_hdr_t);
1008 LASSERT (txpeer->lpni_txqnob >= 0);
1010 txpeer->lpni_txcredits++;
1011 if (txpeer->lpni_txcredits <= 0) {
1012 msg2 = list_entry(txpeer->lpni_txq.next,
1013 lnet_msg_t, msg_list);
1014 list_del(&msg2->msg_list);
1016 LASSERT(msg2->msg_txpeer == txpeer);
1017 LASSERT(msg2->msg_tx_delayed);
1019 (void) lnet_post_send_locked(msg2, 1);
1024 msg->msg_txni = NULL;
1025 lnet_ni_decref_locked(txni, msg->msg_tx_cpt);
1028 if (txpeer != NULL) {
1031 * Once the patch for the health comes in we need to set
1032 * the health of the peer ni to bad when we fail to send
1034 * int status = msg->msg_ev.status;
1036 * lnet_set_peer_ni_health_locked(txpeer, false)
1038 msg->msg_txpeer = NULL;
1039 lnet_peer_ni_decref_locked(txpeer);
1044 lnet_schedule_blocked_locked(lnet_rtrbufpool_t *rbp)
1048 if (list_empty(&rbp->rbp_msgs))
1050 msg = list_entry(rbp->rbp_msgs.next,
1051 lnet_msg_t, msg_list);
1052 list_del(&msg->msg_list);
1054 (void)lnet_post_routed_recv_locked(msg, 1);
1058 lnet_drop_routed_msgs_locked(struct list_head *list, int cpt)
1062 struct list_head drop;
1064 INIT_LIST_HEAD(&drop);
1066 list_splice_init(list, &drop);
1068 lnet_net_unlock(cpt);
1070 list_for_each_entry_safe(msg, tmp, &drop, msg_list) {
1071 lnet_ni_recv(msg->msg_rxni, msg->msg_private, NULL,
1072 0, 0, 0, msg->msg_hdr.payload_length);
1073 list_del_init(&msg->msg_list);
1074 lnet_finalize(NULL, msg, -ECANCELED);
1081 lnet_return_rx_credits_locked(lnet_msg_t *msg)
1083 struct lnet_peer_ni *rxpeer = msg->msg_rxpeer;
1084 struct lnet_ni *rxni = msg->msg_rxni;
1087 if (msg->msg_rtrcredit) {
1088 /* give back global router credits */
1090 lnet_rtrbufpool_t *rbp;
1092 /* NB If a msg ever blocks for a buffer in rbp_msgs, it stays
1093 * there until it gets one allocated, or aborts the wait
1095 LASSERT(msg->msg_kiov != NULL);
1097 rb = list_entry(msg->msg_kiov, lnet_rtrbuf_t, rb_kiov[0]);
1100 msg->msg_kiov = NULL;
1101 msg->msg_rtrcredit = 0;
1103 LASSERT(rbp == lnet_msg2bufpool(msg));
1105 LASSERT((rbp->rbp_credits > 0) ==
1106 !list_empty(&rbp->rbp_bufs));
1108 /* If routing is now turned off, we just drop this buffer and
1109 * don't bother trying to return credits. */
1110 if (!the_lnet.ln_routing) {
1111 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1115 /* It is possible that a user has lowered the desired number of
1116 * buffers in this pool. Make sure we never put back
1117 * more buffers than the stated number. */
1118 if (unlikely(rbp->rbp_credits >= rbp->rbp_req_nbuffers)) {
1119 /* Discard this buffer so we don't have too
1121 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1122 rbp->rbp_nbuffers--;
1124 list_add(&rb->rb_list, &rbp->rbp_bufs);
1126 if (rbp->rbp_credits <= 0)
1127 lnet_schedule_blocked_locked(rbp);
1132 if (msg->msg_peerrtrcredit) {
1133 /* give back peer router credits */
1134 msg->msg_peerrtrcredit = 0;
1136 LASSERT((rxpeer->lpni_rtrcredits < 0) ==
1137 !list_empty(&rxpeer->lpni_rtrq));
1139 rxpeer->lpni_rtrcredits++;
1141 /* drop all messages which are queued to be routed on that
1143 if (!the_lnet.ln_routing) {
1144 lnet_drop_routed_msgs_locked(&rxpeer->lpni_rtrq,
1146 } else if (rxpeer->lpni_rtrcredits <= 0) {
1147 msg2 = list_entry(rxpeer->lpni_rtrq.next,
1148 lnet_msg_t, msg_list);
1149 list_del(&msg2->msg_list);
1151 (void) lnet_post_routed_recv_locked(msg2, 1);
1155 msg->msg_rxni = NULL;
1156 lnet_ni_decref_locked(rxni, msg->msg_rx_cpt);
1158 if (rxpeer != NULL) {
1159 msg->msg_rxpeer = NULL;
1160 lnet_peer_ni_decref_locked(rxpeer);
1165 lnet_compare_peers(struct lnet_peer_ni *p1, struct lnet_peer_ni *p2)
1167 if (p1->lpni_txqnob < p2->lpni_txqnob)
1170 if (p1->lpni_txqnob > p2->lpni_txqnob)
1173 if (p1->lpni_txcredits > p2->lpni_txcredits)
1176 if (p1->lpni_txcredits < p2->lpni_txcredits)
1183 lnet_compare_routes(lnet_route_t *r1, lnet_route_t *r2)
1185 struct lnet_peer_ni *p1 = r1->lr_gateway;
1186 struct lnet_peer_ni *p2 = r2->lr_gateway;
1187 int r1_hops = (r1->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r1->lr_hops;
1188 int r2_hops = (r2->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r2->lr_hops;
1191 if (r1->lr_priority < r2->lr_priority)
1194 if (r1->lr_priority > r2->lr_priority)
1197 if (r1_hops < r2_hops)
1200 if (r1_hops > r2_hops)
1203 rc = lnet_compare_peers(p1, p2);
1207 if (r1->lr_seq - r2->lr_seq <= 0)
1213 static struct lnet_peer_ni *
1214 lnet_find_route_locked(struct lnet_net *net, lnet_nid_t target,
1217 lnet_remotenet_t *rnet;
1218 lnet_route_t *route;
1219 lnet_route_t *best_route;
1220 lnet_route_t *last_route;
1221 struct lnet_peer_ni *lpni_best;
1222 struct lnet_peer_ni *lp;
1225 /* If @rtr_nid is not LNET_NID_ANY, return the gateway with
1226 * rtr_nid nid, otherwise find the best gateway I can use */
1228 rnet = lnet_find_rnet_locked(LNET_NIDNET(target));
1233 best_route = last_route = NULL;
1234 list_for_each_entry(route, &rnet->lrn_routes, lr_list) {
1235 lp = route->lr_gateway;
1237 if (!lnet_is_route_alive(route))
1240 if (net != NULL && lp->lpni_net != net)
1243 if (lp->lpni_nid == rtr_nid) /* it's pre-determined router */
1246 if (lpni_best == NULL) {
1247 best_route = last_route = route;
1252 /* no protection on below fields, but it's harmless */
1253 if (last_route->lr_seq - route->lr_seq < 0)
1256 rc = lnet_compare_routes(route, best_route);
1264 /* set sequence number on the best router to the latest sequence + 1
1265 * so we can round-robin all routers, it's race and inaccurate but
1266 * harmless and functional */
1267 if (best_route != NULL)
1268 best_route->lr_seq = last_route->lr_seq + 1;
1273 lnet_select_pathway(lnet_nid_t src_nid, lnet_nid_t dst_nid,
1274 struct lnet_msg *msg, lnet_nid_t rtr_nid, bool *lo_sent)
1276 struct lnet_ni *best_ni = NULL;
1277 struct lnet_peer_ni *best_lpni = NULL;
1278 struct lnet_peer_ni *net_gw = NULL;
1279 struct lnet_peer_ni *best_gw = NULL;
1280 struct lnet_peer_ni *lpni;
1281 struct lnet_peer *peer = NULL;
1282 struct lnet_peer_net *peer_net;
1283 struct lnet_net *local_net;
1284 struct lnet_ni *ni = NULL;
1286 bool routing = false;
1287 bool ni_is_pref = false;
1288 bool preferred = false;
1289 int best_credits = 0;
1291 int best_lpni_credits = INT_MIN;
1295 * get an initial CPT to use for locking. The idea here is not to
1296 * serialize the calls to select_pathway, so that as many
1297 * operations can run concurrently as possible. To do that we use
1298 * the CPT where this call is being executed. Later on when we
1299 * determine the CPT to use in lnet_message_commit, we switch the
1300 * lock and check if there was any configuration changes, if none,
1301 * then we proceed, if there is, then we'll need to update the cpt
1302 * and redo the operation.
1304 cpt = lnet_net_lock_current();
1311 if (the_lnet.ln_shutdown) {
1312 lnet_net_unlock(cpt);
1317 * initialize the variables which could be reused if we go to
1321 seq = lnet_get_dlc_seq_locked();
1323 rc = lnet_find_or_create_peer_locked(dst_nid, cpt, &peer);
1325 lnet_net_unlock(cpt);
1329 /* If peer is not healthy then can not send anything to it */
1330 if (!lnet_is_peer_healthy_locked(peer)) {
1331 lnet_net_unlock(cpt);
1332 return -EHOSTUNREACH;
1336 * STEP 1: first jab at determineing best_ni
1337 * if src_nid is explicitly specified, then best_ni is already
1338 * pre-determiend for us. Otherwise we need to select the best
1339 * one to use later on
1341 if (src_nid != LNET_NID_ANY) {
1342 best_ni = lnet_nid2ni_locked(src_nid, cpt);
1344 lnet_net_unlock(cpt);
1345 LCONSOLE_WARN("Can't send to %s: src %s is not a "
1346 "local nid\n", libcfs_nid2str(dst_nid),
1347 libcfs_nid2str(src_nid));
1351 if (best_ni->ni_net->net_id != LNET_NIDNET(dst_nid)) {
1352 lnet_net_unlock(cpt);
1353 LCONSOLE_WARN("No route to %s via from %s\n",
1354 libcfs_nid2str(dst_nid),
1355 libcfs_nid2str(src_nid));
1360 if (best_ni == the_lnet.ln_loni) {
1361 /* No send credit hassles with LOLND */
1362 msg->msg_hdr.dest_nid = cpu_to_le64(best_ni->ni_nid);
1363 if (!msg->msg_routing)
1364 msg->msg_hdr.src_nid = cpu_to_le64(best_ni->ni_nid);
1365 msg->msg_target.nid = best_ni->ni_nid;
1366 lnet_msg_commit(msg, cpt);
1368 lnet_ni_addref_locked(best_ni, cpt);
1369 lnet_net_unlock(cpt);
1370 msg->msg_txni = best_ni;
1371 lnet_ni_send(best_ni, msg);
1381 * Decide whether we need to route to peer_ni.
1382 * Get the local net that I need to be on to be able to directly
1383 * send to that peer.
1385 * a. Find the peer which the dst_nid belongs to.
1386 * b. Iterate through each of the peer_nets/nis to decide
1387 * the best peer/local_ni pair to use
1389 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_on_peer_list) {
1390 if (!lnet_is_peer_net_healthy_locked(peer_net))
1393 local_net = lnet_get_net_locked(peer_net->lpn_net_id);
1396 * go through each peer_ni on that peer_net and
1397 * determine the best possible gw to go through
1399 list_for_each_entry(lpni, &peer_net->lpn_peer_nis,
1400 lpni_on_peer_net_list) {
1401 net_gw = lnet_find_route_locked(NULL,
1406 * if no route is found for that network then
1407 * move onto the next peer_ni in the peer
1416 rc = lnet_compare_peers(net_gw,
1428 local_net = lnet_get_net_locked
1429 (LNET_NIDNET(best_gw->lpni_nid));
1436 /* no routable net found go on to a different net */
1441 * Second jab at determining best_ni
1442 * if we get here then the peer we're trying to send
1443 * to is on a directly connected network, and we'll
1444 * need to pick the local_ni on that network to send
1447 while ((ni = lnet_get_next_ni_locked(local_net, ni))) {
1448 if (!lnet_is_ni_healthy_locked(ni))
1450 /* TODO: compare NUMA distance */
1451 if (ni->ni_tx_queues[cpt]->tq_credits <=
1454 * all we want is to read tq_credits
1455 * value as an approximation of how
1456 * busy the NI is. No need to grab a lock
1459 } else if (best_ni) {
1460 if ((best_ni)->ni_seq - ni->ni_seq <= 0)
1462 (best_ni)->ni_seq = ni->ni_seq + 1;
1466 best_credits = ni->ni_tx_queues[cpt]->tq_credits;
1471 lnet_net_unlock(cpt);
1472 LCONSOLE_WARN("No local ni found to send from to %s\n",
1473 libcfs_nid2str(dst_nid));
1483 if (msg->msg_type == LNET_MSG_REPLY ||
1484 msg->msg_type == LNET_MSG_ACK) {
1486 * for replies we want to respond on the same peer_ni we
1487 * received the message on if possible. If not, then pick
1488 * a peer_ni to send to
1490 best_lpni = lnet_find_peer_ni_locked(dst_nid);
1492 lnet_peer_ni_decref_locked(best_lpni);
1495 CDEBUG(D_NET, "unable to send msg_type %d to "
1496 "originating %s\n", msg->msg_type,
1497 libcfs_nid2str(dst_nid));
1501 peer_net = lnet_peer_get_net_locked(peer,
1502 best_ni->ni_net->net_id);
1504 * peer_net is not available or the src_nid is explicitly defined
1505 * and the peer_net for that src_nid is unhealthy. find a route to
1506 * the destination nid.
1509 (src_nid != LNET_NID_ANY &&
1510 !lnet_is_peer_net_healthy_locked(peer_net))) {
1511 best_gw = lnet_find_route_locked(best_ni->ni_net,
1515 * if no route is found for that network then
1516 * move onto the next peer_ni in the peer
1519 lnet_net_unlock(cpt);
1520 LCONSOLE_WARN("No route to peer from %s\n",
1521 libcfs_nid2str(best_ni->ni_nid));
1522 return -EHOSTUNREACH;
1525 CDEBUG(D_NET, "Best route to %s via %s for %s %d\n",
1526 libcfs_nid2str(lpni->lpni_nid),
1527 libcfs_nid2str(best_gw->lpni_nid),
1528 lnet_msgtyp2str(msg->msg_type), msg->msg_len);
1530 best_lpni = lnet_find_peer_ni_locked(dst_nid);
1531 LASSERT(best_lpni != NULL);
1532 lnet_peer_ni_decref_locked(best_lpni);
1537 } else if (!lnet_is_peer_net_healthy_locked(peer_net)) {
1539 * this peer_net is unhealthy but we still have an opportunity
1540 * to find another peer_net that we can use
1542 __u32 net_id = peer_net->lpn_net_id;
1543 lnet_net_unlock(cpt);
1545 LCONSOLE_WARN("peer net %s unhealthy\n",
1546 libcfs_net2str(net_id));
1551 while ((lpni = lnet_get_next_peer_ni_locked(peer, peer_net, lpni))) {
1553 * if this peer ni is not healty just skip it, no point in
1554 * examining it further
1556 if (!lnet_is_peer_ni_healthy_locked(lpni))
1558 ni_is_pref = lnet_peer_is_ni_pref_locked(lpni, best_ni);
1560 if (!preferred && ni_is_pref) {
1562 } else if (preferred && !ni_is_pref) {
1564 } if (lpni->lpni_txcredits <= best_lpni_credits)
1566 else if (best_lpni) {
1567 if (best_lpni->lpni_seq - lpni->lpni_seq <= 0)
1569 best_lpni->lpni_seq = lpni->lpni_seq + 1;
1573 best_lpni_credits = lpni->lpni_txcredits;
1576 /* if we still can't find a peer ni then we can't reach it */
1578 __u32 net_id = peer_net->lpn_net_id;
1579 lnet_net_unlock(cpt);
1580 LCONSOLE_WARN("no peer_ni found on peer net %s\n",
1581 libcfs_net2str(net_id));
1587 * determine the cpt to use and if it has changed then
1588 * lock the new cpt and check if the config has changed.
1589 * If it has changed then repeat the algorithm since the
1590 * ni or peer list could have changed and the algorithm
1591 * would endup picking a different ni/peer_ni pair.
1593 cpt2 = best_lpni->lpni_cpt;
1595 lnet_net_unlock(cpt);
1598 seq2 = lnet_get_dlc_seq_locked();
1600 lnet_net_unlock(cpt);
1606 * store the best_lpni in the message right away to avoid having
1607 * to do the same operation under different conditions
1609 msg->msg_txpeer = (routing) ? best_gw : best_lpni;
1610 msg->msg_txni = best_ni;
1612 * grab a reference for the best_ni since now it's in use in this
1613 * send. the reference will need to be dropped when the message is
1614 * finished in lnet_finalize()
1616 lnet_ni_addref_locked(msg->msg_txni, cpt);
1617 lnet_peer_ni_addref_locked(msg->msg_txpeer);
1620 * set the destination nid in the message here because it's
1621 * possible that we'd be sending to a different nid than the one
1624 msg->msg_hdr.dest_nid = cpu_to_le64(msg->msg_txpeer->lpni_nid);
1627 * Always set the target.nid to the best peer picked. Either the
1628 * nid will be one of the preconfigured NIDs, or the same NID as
1629 * what was originaly set in the target or it will be the NID of
1630 * a router if this message should be routed
1632 msg->msg_target.nid = msg->msg_txpeer->lpni_nid;
1635 * lnet_msg_commit assigns the correct cpt to the message, which
1636 * is used to decrement the correct refcount on the ni when it's
1637 * time to return the credits
1639 lnet_msg_commit(msg, cpt);
1642 * If we are routing the message then we don't need to overwrite
1643 * the src_nid since it would've been set at the origin. Otherwise
1644 * we are the originator so we need to set it.
1646 if (!msg->msg_routing)
1647 msg->msg_hdr.src_nid = cpu_to_le64(msg->msg_txni->ni_nid);
1650 msg->msg_target_is_router = 1;
1651 msg->msg_target.pid = LNET_PID_LUSTRE;
1654 rc = lnet_post_send_locked(msg, 0);
1656 lnet_net_unlock(cpt);
1662 lnet_send(lnet_nid_t src_nid, lnet_msg_t *msg, lnet_nid_t rtr_nid)
1664 lnet_nid_t dst_nid = msg->msg_target.nid;
1666 bool lo_sent = false;
1669 * NB: rtr_nid is set to LNET_NID_ANY for all current use-cases,
1670 * but we might want to use pre-determined router for ACK/REPLY
1673 /* NB: ni != NULL == interface pre-determined (ACK/REPLY) */
1674 LASSERT (msg->msg_txpeer == NULL);
1675 LASSERT (!msg->msg_sending);
1676 LASSERT (!msg->msg_target_is_router);
1677 LASSERT (!msg->msg_receiving);
1679 msg->msg_sending = 1;
1681 LASSERT(!msg->msg_tx_committed);
1683 rc = lnet_select_pathway(src_nid, dst_nid, msg, rtr_nid, &lo_sent);
1684 if (rc < 0 || lo_sent)
1687 if (rc == LNET_CREDIT_OK)
1688 lnet_ni_send(msg->msg_txni, msg);
1690 /* rc == LNET_CREDIT_OK or LNET_CREDIT_WAIT */
1695 lnet_drop_message(lnet_ni_t *ni, int cpt, void *private, unsigned int nob)
1698 the_lnet.ln_counters[cpt]->drop_count++;
1699 the_lnet.ln_counters[cpt]->drop_length += nob;
1700 lnet_net_unlock(cpt);
1702 lnet_ni_recv(ni, private, NULL, 0, 0, 0, nob);
1706 lnet_recv_put(lnet_ni_t *ni, lnet_msg_t *msg)
1708 lnet_hdr_t *hdr = &msg->msg_hdr;
1710 if (msg->msg_wanted != 0)
1711 lnet_setpayloadbuffer(msg);
1713 lnet_build_msg_event(msg, LNET_EVENT_PUT);
1715 /* Must I ACK? If so I'll grab the ack_wmd out of the header and put
1716 * it back into the ACK during lnet_finalize() */
1717 msg->msg_ack = (!lnet_is_wire_handle_none(&hdr->msg.put.ack_wmd) &&
1718 (msg->msg_md->md_options & LNET_MD_ACK_DISABLE) == 0);
1720 lnet_ni_recv(ni, msg->msg_private, msg, msg->msg_rx_delayed,
1721 msg->msg_offset, msg->msg_wanted, hdr->payload_length);
1725 lnet_parse_put(lnet_ni_t *ni, lnet_msg_t *msg)
1727 lnet_hdr_t *hdr = &msg->msg_hdr;
1728 struct lnet_match_info info;
1732 /* Convert put fields to host byte order */
1733 hdr->msg.put.match_bits = le64_to_cpu(hdr->msg.put.match_bits);
1734 hdr->msg.put.ptl_index = le32_to_cpu(hdr->msg.put.ptl_index);
1735 hdr->msg.put.offset = le32_to_cpu(hdr->msg.put.offset);
1737 info.mi_id.nid = hdr->src_nid;
1738 info.mi_id.pid = hdr->src_pid;
1739 info.mi_opc = LNET_MD_OP_PUT;
1740 info.mi_portal = hdr->msg.put.ptl_index;
1741 info.mi_rlength = hdr->payload_length;
1742 info.mi_roffset = hdr->msg.put.offset;
1743 info.mi_mbits = hdr->msg.put.match_bits;
1744 info.mi_cpt = msg->msg_rxpeer->lpni_cpt;
1746 msg->msg_rx_ready_delay = ni->ni_net->net_lnd->lnd_eager_recv == NULL;
1747 ready_delay = msg->msg_rx_ready_delay;
1750 rc = lnet_ptl_match_md(&info, msg);
1755 case LNET_MATCHMD_OK:
1756 lnet_recv_put(ni, msg);
1759 case LNET_MATCHMD_NONE:
1761 /* no eager_recv or has already called it, should
1762 * have been attached on delayed list */
1765 rc = lnet_ni_eager_recv(ni, msg);
1772 case LNET_MATCHMD_DROP:
1773 CNETERR("Dropping PUT from %s portal %d match %llu"
1774 " offset %d length %d: %d\n",
1775 libcfs_id2str(info.mi_id), info.mi_portal,
1776 info.mi_mbits, info.mi_roffset, info.mi_rlength, rc);
1778 return -ENOENT; /* -ve: OK but no match */
1783 lnet_parse_get(lnet_ni_t *ni, lnet_msg_t *msg, int rdma_get)
1785 struct lnet_match_info info;
1786 lnet_hdr_t *hdr = &msg->msg_hdr;
1787 struct lnet_handle_wire reply_wmd;
1790 /* Convert get fields to host byte order */
1791 hdr->msg.get.match_bits = le64_to_cpu(hdr->msg.get.match_bits);
1792 hdr->msg.get.ptl_index = le32_to_cpu(hdr->msg.get.ptl_index);
1793 hdr->msg.get.sink_length = le32_to_cpu(hdr->msg.get.sink_length);
1794 hdr->msg.get.src_offset = le32_to_cpu(hdr->msg.get.src_offset);
1796 info.mi_id.nid = hdr->src_nid;
1797 info.mi_id.pid = hdr->src_pid;
1798 info.mi_opc = LNET_MD_OP_GET;
1799 info.mi_portal = hdr->msg.get.ptl_index;
1800 info.mi_rlength = hdr->msg.get.sink_length;
1801 info.mi_roffset = hdr->msg.get.src_offset;
1802 info.mi_mbits = hdr->msg.get.match_bits;
1804 rc = lnet_ptl_match_md(&info, msg);
1805 if (rc == LNET_MATCHMD_DROP) {
1806 CNETERR("Dropping GET from %s portal %d match %llu"
1807 " offset %d length %d\n",
1808 libcfs_id2str(info.mi_id), info.mi_portal,
1809 info.mi_mbits, info.mi_roffset, info.mi_rlength);
1810 return -ENOENT; /* -ve: OK but no match */
1813 LASSERT(rc == LNET_MATCHMD_OK);
1815 lnet_build_msg_event(msg, LNET_EVENT_GET);
1817 reply_wmd = hdr->msg.get.return_wmd;
1819 lnet_prep_send(msg, LNET_MSG_REPLY, info.mi_id,
1820 msg->msg_offset, msg->msg_wanted);
1822 msg->msg_hdr.msg.reply.dst_wmd = reply_wmd;
1825 /* The LND completes the REPLY from her recv procedure */
1826 lnet_ni_recv(ni, msg->msg_private, msg, 0,
1827 msg->msg_offset, msg->msg_len, msg->msg_len);
1831 lnet_ni_recv(ni, msg->msg_private, NULL, 0, 0, 0, 0);
1832 msg->msg_receiving = 0;
1834 rc = lnet_send(ni->ni_nid, msg, LNET_NID_ANY);
1836 /* didn't get as far as lnet_ni_send() */
1837 CERROR("%s: Unable to send REPLY for GET from %s: %d\n",
1838 libcfs_nid2str(ni->ni_nid),
1839 libcfs_id2str(info.mi_id), rc);
1841 lnet_finalize(ni, msg, rc);
1848 lnet_parse_reply(lnet_ni_t *ni, lnet_msg_t *msg)
1850 void *private = msg->msg_private;
1851 lnet_hdr_t *hdr = &msg->msg_hdr;
1852 lnet_process_id_t src = {0};
1858 cpt = lnet_cpt_of_cookie(hdr->msg.reply.dst_wmd.wh_object_cookie);
1861 src.nid = hdr->src_nid;
1862 src.pid = hdr->src_pid;
1864 /* NB handles only looked up by creator (no flips) */
1865 md = lnet_wire_handle2md(&hdr->msg.reply.dst_wmd);
1866 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
1867 CNETERR("%s: Dropping REPLY from %s for %s "
1869 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1870 (md == NULL) ? "invalid" : "inactive",
1871 hdr->msg.reply.dst_wmd.wh_interface_cookie,
1872 hdr->msg.reply.dst_wmd.wh_object_cookie);
1873 if (md != NULL && md->md_me != NULL)
1874 CERROR("REPLY MD also attached to portal %d\n",
1875 md->md_me->me_portal);
1877 lnet_res_unlock(cpt);
1878 return -ENOENT; /* -ve: OK but no match */
1881 LASSERT(md->md_offset == 0);
1883 rlength = hdr->payload_length;
1884 mlength = MIN(rlength, (int)md->md_length);
1886 if (mlength < rlength &&
1887 (md->md_options & LNET_MD_TRUNCATE) == 0) {
1888 CNETERR("%s: Dropping REPLY from %s length %d "
1889 "for MD %#llx would overflow (%d)\n",
1890 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1891 rlength, hdr->msg.reply.dst_wmd.wh_object_cookie,
1893 lnet_res_unlock(cpt);
1894 return -ENOENT; /* -ve: OK but no match */
1897 CDEBUG(D_NET, "%s: Reply from %s of length %d/%d into md %#llx\n",
1898 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1899 mlength, rlength, hdr->msg.reply.dst_wmd.wh_object_cookie);
1901 lnet_msg_attach_md(msg, md, 0, mlength);
1904 lnet_setpayloadbuffer(msg);
1906 lnet_res_unlock(cpt);
1908 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
1910 lnet_ni_recv(ni, private, msg, 0, 0, mlength, rlength);
1915 lnet_parse_ack(lnet_ni_t *ni, lnet_msg_t *msg)
1917 lnet_hdr_t *hdr = &msg->msg_hdr;
1918 lnet_process_id_t src = {0};
1922 src.nid = hdr->src_nid;
1923 src.pid = hdr->src_pid;
1925 /* Convert ack fields to host byte order */
1926 hdr->msg.ack.match_bits = le64_to_cpu(hdr->msg.ack.match_bits);
1927 hdr->msg.ack.mlength = le32_to_cpu(hdr->msg.ack.mlength);
1929 cpt = lnet_cpt_of_cookie(hdr->msg.ack.dst_wmd.wh_object_cookie);
1932 /* NB handles only looked up by creator (no flips) */
1933 md = lnet_wire_handle2md(&hdr->msg.ack.dst_wmd);
1934 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
1935 /* Don't moan; this is expected */
1937 "%s: Dropping ACK from %s to %s MD %#llx.%#llx\n",
1938 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1939 (md == NULL) ? "invalid" : "inactive",
1940 hdr->msg.ack.dst_wmd.wh_interface_cookie,
1941 hdr->msg.ack.dst_wmd.wh_object_cookie);
1942 if (md != NULL && md->md_me != NULL)
1943 CERROR("Source MD also attached to portal %d\n",
1944 md->md_me->me_portal);
1946 lnet_res_unlock(cpt);
1947 return -ENOENT; /* -ve! */
1950 CDEBUG(D_NET, "%s: ACK from %s into md %#llx\n",
1951 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1952 hdr->msg.ack.dst_wmd.wh_object_cookie);
1954 lnet_msg_attach_md(msg, md, 0, 0);
1956 lnet_res_unlock(cpt);
1958 lnet_build_msg_event(msg, LNET_EVENT_ACK);
1960 lnet_ni_recv(ni, msg->msg_private, msg, 0, 0, 0, msg->msg_len);
1965 * \retval LNET_CREDIT_OK If \a msg is forwarded
1966 * \retval LNET_CREDIT_WAIT If \a msg is blocked because w/o buffer
1967 * \retval -ve error code
1970 lnet_parse_forward_locked(lnet_ni_t *ni, lnet_msg_t *msg)
1974 if (!the_lnet.ln_routing)
1977 if (msg->msg_rxpeer->lpni_rtrcredits <= 0 ||
1978 lnet_msg2bufpool(msg)->rbp_credits <= 0) {
1979 if (ni->ni_net->net_lnd->lnd_eager_recv == NULL) {
1980 msg->msg_rx_ready_delay = 1;
1982 lnet_net_unlock(msg->msg_rx_cpt);
1983 rc = lnet_ni_eager_recv(ni, msg);
1984 lnet_net_lock(msg->msg_rx_cpt);
1989 rc = lnet_post_routed_recv_locked(msg, 0);
1994 lnet_parse_local(lnet_ni_t *ni, lnet_msg_t *msg)
1998 switch (msg->msg_type) {
2000 rc = lnet_parse_ack(ni, msg);
2003 rc = lnet_parse_put(ni, msg);
2006 rc = lnet_parse_get(ni, msg, msg->msg_rdma_get);
2008 case LNET_MSG_REPLY:
2009 rc = lnet_parse_reply(ni, msg);
2011 default: /* prevent an unused label if !kernel */
2016 LASSERT(rc == 0 || rc == -ENOENT);
2021 lnet_msgtyp2str (int type)
2030 case LNET_MSG_REPLY:
2032 case LNET_MSG_HELLO:
2035 return ("<UNKNOWN>");
2040 lnet_print_hdr(lnet_hdr_t * hdr)
2042 lnet_process_id_t src = {0};
2043 lnet_process_id_t dst = {0};
2044 char *type_str = lnet_msgtyp2str(hdr->type);
2046 src.nid = hdr->src_nid;
2047 src.pid = hdr->src_pid;
2049 dst.nid = hdr->dest_nid;
2050 dst.pid = hdr->dest_pid;
2052 CWARN("P3 Header at %p of type %s\n", hdr, type_str);
2053 CWARN(" From %s\n", libcfs_id2str(src));
2054 CWARN(" To %s\n", libcfs_id2str(dst));
2056 switch (hdr->type) {
2061 CWARN(" Ptl index %d, ack md %#llx.%#llx, "
2062 "match bits %llu\n",
2063 hdr->msg.put.ptl_index,
2064 hdr->msg.put.ack_wmd.wh_interface_cookie,
2065 hdr->msg.put.ack_wmd.wh_object_cookie,
2066 hdr->msg.put.match_bits);
2067 CWARN(" Length %d, offset %d, hdr data %#llx\n",
2068 hdr->payload_length, hdr->msg.put.offset,
2069 hdr->msg.put.hdr_data);
2073 CWARN(" Ptl index %d, return md %#llx.%#llx, "
2074 "match bits %llu\n", hdr->msg.get.ptl_index,
2075 hdr->msg.get.return_wmd.wh_interface_cookie,
2076 hdr->msg.get.return_wmd.wh_object_cookie,
2077 hdr->msg.get.match_bits);
2078 CWARN(" Length %d, src offset %d\n",
2079 hdr->msg.get.sink_length,
2080 hdr->msg.get.src_offset);
2084 CWARN(" dst md %#llx.%#llx, "
2085 "manipulated length %d\n",
2086 hdr->msg.ack.dst_wmd.wh_interface_cookie,
2087 hdr->msg.ack.dst_wmd.wh_object_cookie,
2088 hdr->msg.ack.mlength);
2091 case LNET_MSG_REPLY:
2092 CWARN(" dst md %#llx.%#llx, "
2094 hdr->msg.reply.dst_wmd.wh_interface_cookie,
2095 hdr->msg.reply.dst_wmd.wh_object_cookie,
2096 hdr->payload_length);
2102 lnet_parse(lnet_ni_t *ni, lnet_hdr_t *hdr, lnet_nid_t from_nid,
2103 void *private, int rdma_req)
2108 struct lnet_msg *msg;
2109 lnet_pid_t dest_pid;
2110 lnet_nid_t dest_nid;
2112 __u32 payload_length;
2115 LASSERT (!in_interrupt ());
2117 type = le32_to_cpu(hdr->type);
2118 src_nid = le64_to_cpu(hdr->src_nid);
2119 dest_nid = le64_to_cpu(hdr->dest_nid);
2120 dest_pid = le32_to_cpu(hdr->dest_pid);
2121 payload_length = le32_to_cpu(hdr->payload_length);
2123 for_me = (ni->ni_nid == dest_nid);
2124 cpt = lnet_cpt_of_nid(from_nid, ni);
2129 if (payload_length > 0) {
2130 CERROR("%s, src %s: bad %s payload %d (0 expected)\n",
2131 libcfs_nid2str(from_nid),
2132 libcfs_nid2str(src_nid),
2133 lnet_msgtyp2str(type), payload_length);
2139 case LNET_MSG_REPLY:
2140 if (payload_length >
2141 (__u32)(for_me ? LNET_MAX_PAYLOAD : LNET_MTU)) {
2142 CERROR("%s, src %s: bad %s payload %d "
2143 "(%d max expected)\n",
2144 libcfs_nid2str(from_nid),
2145 libcfs_nid2str(src_nid),
2146 lnet_msgtyp2str(type),
2148 for_me ? LNET_MAX_PAYLOAD : LNET_MTU);
2154 CERROR("%s, src %s: Bad message type 0x%x\n",
2155 libcfs_nid2str(from_nid),
2156 libcfs_nid2str(src_nid), type);
2160 if (the_lnet.ln_routing &&
2161 ni->ni_last_alive != ktime_get_real_seconds()) {
2162 /* NB: so far here is the only place to set NI status to "up */
2164 ni->ni_last_alive = ktime_get_real_seconds();
2165 if (ni->ni_status != NULL &&
2166 ni->ni_status->ns_status == LNET_NI_STATUS_DOWN)
2167 ni->ni_status->ns_status = LNET_NI_STATUS_UP;
2171 /* Regard a bad destination NID as a protocol error. Senders should
2172 * know what they're doing; if they don't they're misconfigured, buggy
2173 * or malicious so we chop them off at the knees :) */
2176 if (LNET_NIDNET(dest_nid) == LNET_NIDNET(ni->ni_nid)) {
2177 /* should have gone direct */
2178 CERROR("%s, src %s: Bad dest nid %s "
2179 "(should have been sent direct)\n",
2180 libcfs_nid2str(from_nid),
2181 libcfs_nid2str(src_nid),
2182 libcfs_nid2str(dest_nid));
2186 if (lnet_islocalnid(dest_nid)) {
2187 /* dest is another local NI; sender should have used
2188 * this node's NID on its own network */
2189 CERROR("%s, src %s: Bad dest nid %s "
2190 "(it's my nid but on a different network)\n",
2191 libcfs_nid2str(from_nid),
2192 libcfs_nid2str(src_nid),
2193 libcfs_nid2str(dest_nid));
2197 if (rdma_req && type == LNET_MSG_GET) {
2198 CERROR("%s, src %s: Bad optimized GET for %s "
2199 "(final destination must be me)\n",
2200 libcfs_nid2str(from_nid),
2201 libcfs_nid2str(src_nid),
2202 libcfs_nid2str(dest_nid));
2206 if (!the_lnet.ln_routing) {
2207 CERROR("%s, src %s: Dropping message for %s "
2208 "(routing not enabled)\n",
2209 libcfs_nid2str(from_nid),
2210 libcfs_nid2str(src_nid),
2211 libcfs_nid2str(dest_nid));
2216 /* Message looks OK; we're not going to return an error, so we MUST
2217 * call back lnd_recv() come what may... */
2219 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
2220 fail_peer(src_nid, 0)) { /* shall we now? */
2221 CERROR("%s, src %s: Dropping %s to simulate failure\n",
2222 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
2223 lnet_msgtyp2str(type));
2227 if (!list_empty(&the_lnet.ln_drop_rules) &&
2228 lnet_drop_rule_match(hdr)) {
2229 CDEBUG(D_NET, "%s, src %s, dst %s: Dropping %s to simulate"
2230 "silent message loss\n",
2231 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
2232 libcfs_nid2str(dest_nid), lnet_msgtyp2str(type));
2237 msg = lnet_msg_alloc();
2239 CERROR("%s, src %s: Dropping %s (out of memory)\n",
2240 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
2241 lnet_msgtyp2str(type));
2245 /* msg zeroed in lnet_msg_alloc; i.e. flags all clear,
2246 * pointers NULL etc */
2248 msg->msg_type = type;
2249 msg->msg_private = private;
2250 msg->msg_receiving = 1;
2251 msg->msg_rdma_get = rdma_req;
2252 msg->msg_len = msg->msg_wanted = payload_length;
2253 msg->msg_offset = 0;
2254 msg->msg_hdr = *hdr;
2255 /* for building message event */
2256 msg->msg_from = from_nid;
2258 msg->msg_target.pid = dest_pid;
2259 msg->msg_target.nid = dest_nid;
2260 msg->msg_routing = 1;
2263 /* convert common msg->hdr fields to host byteorder */
2264 msg->msg_hdr.type = type;
2265 msg->msg_hdr.src_nid = src_nid;
2266 msg->msg_hdr.src_pid = le32_to_cpu(msg->msg_hdr.src_pid);
2267 msg->msg_hdr.dest_nid = dest_nid;
2268 msg->msg_hdr.dest_pid = dest_pid;
2269 msg->msg_hdr.payload_length = payload_length;
2273 rc = lnet_nid2peerni_locked(&msg->msg_rxpeer, from_nid, cpt);
2275 lnet_net_unlock(cpt);
2276 CERROR("%s, src %s: Dropping %s "
2277 "(error %d looking up sender)\n",
2278 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
2279 lnet_msgtyp2str(type), rc);
2281 if (rc == -ESHUTDOWN)
2282 /* We are shutting down. Don't do anything more */
2287 lnet_ni_addref_locked(ni, cpt);
2289 if (lnet_isrouter(msg->msg_rxpeer)) {
2290 lnet_peer_set_alive(msg->msg_rxpeer);
2291 if (avoid_asym_router_failure &&
2292 LNET_NIDNET(src_nid) != LNET_NIDNET(from_nid)) {
2293 /* received a remote message from router, update
2294 * remote NI status on this router.
2295 * NB: multi-hop routed message will be ignored.
2297 lnet_router_ni_update_locked(msg->msg_rxpeer,
2298 LNET_NIDNET(src_nid));
2302 lnet_msg_commit(msg, cpt);
2304 /* message delay simulation */
2305 if (unlikely(!list_empty(&the_lnet.ln_delay_rules) &&
2306 lnet_delay_rule_match_locked(hdr, msg))) {
2307 lnet_net_unlock(cpt);
2312 rc = lnet_parse_forward_locked(ni, msg);
2313 lnet_net_unlock(cpt);
2318 if (rc == LNET_CREDIT_OK) {
2319 lnet_ni_recv(ni, msg->msg_private, msg, 0,
2320 0, payload_length, payload_length);
2325 lnet_net_unlock(cpt);
2327 rc = lnet_parse_local(ni, msg);
2333 LASSERT(msg->msg_md == NULL);
2334 lnet_finalize(ni, msg, rc);
2337 lnet_drop_message(ni, cpt, private, payload_length);
2340 EXPORT_SYMBOL(lnet_parse);
2343 lnet_drop_delayed_msg_list(struct list_head *head, char *reason)
2345 while (!list_empty(head)) {
2346 lnet_process_id_t id = {0};
2349 msg = list_entry(head->next, lnet_msg_t, msg_list);
2350 list_del(&msg->msg_list);
2352 id.nid = msg->msg_hdr.src_nid;
2353 id.pid = msg->msg_hdr.src_pid;
2355 LASSERT(msg->msg_md == NULL);
2356 LASSERT(msg->msg_rx_delayed);
2357 LASSERT(msg->msg_rxpeer != NULL);
2358 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
2360 CWARN("Dropping delayed PUT from %s portal %d match %llu"
2361 " offset %d length %d: %s\n",
2363 msg->msg_hdr.msg.put.ptl_index,
2364 msg->msg_hdr.msg.put.match_bits,
2365 msg->msg_hdr.msg.put.offset,
2366 msg->msg_hdr.payload_length, reason);
2368 /* NB I can't drop msg's ref on msg_rxpeer until after I've
2369 * called lnet_drop_message(), so I just hang onto msg as well
2370 * until that's done */
2372 lnet_drop_message(msg->msg_rxni,
2373 msg->msg_rxpeer->lpni_cpt,
2374 msg->msg_private, msg->msg_len);
2376 * NB: message will not generate event because w/o attached MD,
2377 * but we still should give error code so lnet_msg_decommit()
2378 * can skip counters operations and other checks.
2380 lnet_finalize(msg->msg_rxni, msg, -ENOENT);
2385 lnet_recv_delayed_msg_list(struct list_head *head)
2387 while (!list_empty(head)) {
2389 lnet_process_id_t id;
2391 msg = list_entry(head->next, lnet_msg_t, msg_list);
2392 list_del(&msg->msg_list);
2394 /* md won't disappear under me, since each msg
2395 * holds a ref on it */
2397 id.nid = msg->msg_hdr.src_nid;
2398 id.pid = msg->msg_hdr.src_pid;
2400 LASSERT(msg->msg_rx_delayed);
2401 LASSERT(msg->msg_md != NULL);
2402 LASSERT(msg->msg_rxpeer != NULL);
2403 LASSERT(msg->msg_rxni != NULL);
2404 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
2406 CDEBUG(D_NET, "Resuming delayed PUT from %s portal %d "
2407 "match %llu offset %d length %d.\n",
2408 libcfs_id2str(id), msg->msg_hdr.msg.put.ptl_index,
2409 msg->msg_hdr.msg.put.match_bits,
2410 msg->msg_hdr.msg.put.offset,
2411 msg->msg_hdr.payload_length);
2413 lnet_recv_put(msg->msg_rxni, msg);
2418 * Initiate an asynchronous PUT operation.
2420 * There are several events associated with a PUT: completion of the send on
2421 * the initiator node (LNET_EVENT_SEND), and when the send completes
2422 * successfully, the receipt of an acknowledgment (LNET_EVENT_ACK) indicating
2423 * that the operation was accepted by the target. The event LNET_EVENT_PUT is
2424 * used at the target node to indicate the completion of incoming data
2427 * The local events will be logged in the EQ associated with the MD pointed to
2428 * by \a mdh handle. Using a MD without an associated EQ results in these
2429 * events being discarded. In this case, the caller must have another
2430 * mechanism (e.g., a higher level protocol) for determining when it is safe
2431 * to modify the memory region associated with the MD.
2433 * Note that LNet does not guarantee the order of LNET_EVENT_SEND and
2434 * LNET_EVENT_ACK, though intuitively ACK should happen after SEND.
2436 * \param self Indicates the NID of a local interface through which to send
2437 * the PUT request. Use LNET_NID_ANY to let LNet choose one by itself.
2438 * \param mdh A handle for the MD that describes the memory to be sent. The MD
2439 * must be "free floating" (See LNetMDBind()).
2440 * \param ack Controls whether an acknowledgment is requested.
2441 * Acknowledgments are only sent when they are requested by the initiating
2442 * process and the target MD enables them.
2443 * \param target A process identifier for the target process.
2444 * \param portal The index in the \a target's portal table.
2445 * \param match_bits The match bits to use for MD selection at the target
2447 * \param offset The offset into the target MD (only used when the target
2448 * MD has the LNET_MD_MANAGE_REMOTE option set).
2449 * \param hdr_data 64 bits of user data that can be included in the message
2450 * header. This data is written to an event queue entry at the target if an
2451 * EQ is present on the matching MD.
2453 * \retval 0 Success, and only in this case events will be generated
2454 * and logged to EQ (if it exists).
2455 * \retval -EIO Simulated failure.
2456 * \retval -ENOMEM Memory allocation failure.
2457 * \retval -ENOENT Invalid MD object.
2459 * \see lnet_event_t::hdr_data and lnet_event_kind_t.
2462 LNetPut(lnet_nid_t self, lnet_handle_md_t mdh, lnet_ack_req_t ack,
2463 lnet_process_id_t target, unsigned int portal,
2464 __u64 match_bits, unsigned int offset,
2467 struct lnet_msg *msg;
2468 struct lnet_libmd *md;
2472 LASSERT(the_lnet.ln_refcount > 0);
2474 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
2475 fail_peer(target.nid, 1)) { /* shall we now? */
2476 CERROR("Dropping PUT to %s: simulated failure\n",
2477 libcfs_id2str(target));
2481 msg = lnet_msg_alloc();
2483 CERROR("Dropping PUT to %s: ENOMEM on lnet_msg_t\n",
2484 libcfs_id2str(target));
2487 msg->msg_vmflush = !!memory_pressure_get();
2489 cpt = lnet_cpt_of_cookie(mdh.cookie);
2492 md = lnet_handle2md(&mdh);
2493 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
2494 CERROR("Dropping PUT (%llu:%d:%s): MD (%d) invalid\n",
2495 match_bits, portal, libcfs_id2str(target),
2496 md == NULL ? -1 : md->md_threshold);
2497 if (md != NULL && md->md_me != NULL)
2498 CERROR("Source MD also attached to portal %d\n",
2499 md->md_me->me_portal);
2500 lnet_res_unlock(cpt);
2506 CDEBUG(D_NET, "LNetPut -> %s\n", libcfs_id2str(target));
2508 lnet_msg_attach_md(msg, md, 0, 0);
2510 lnet_prep_send(msg, LNET_MSG_PUT, target, 0, md->md_length);
2512 msg->msg_hdr.msg.put.match_bits = cpu_to_le64(match_bits);
2513 msg->msg_hdr.msg.put.ptl_index = cpu_to_le32(portal);
2514 msg->msg_hdr.msg.put.offset = cpu_to_le32(offset);
2515 msg->msg_hdr.msg.put.hdr_data = hdr_data;
2517 /* NB handles only looked up by creator (no flips) */
2518 if (ack == LNET_ACK_REQ) {
2519 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
2520 the_lnet.ln_interface_cookie;
2521 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
2522 md->md_lh.lh_cookie;
2524 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
2525 LNET_WIRE_HANDLE_COOKIE_NONE;
2526 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
2527 LNET_WIRE_HANDLE_COOKIE_NONE;
2530 lnet_res_unlock(cpt);
2532 lnet_build_msg_event(msg, LNET_EVENT_SEND);
2534 rc = lnet_send(self, msg, LNET_NID_ANY);
2536 CNETERR("Error sending PUT to %s: %d\n",
2537 libcfs_id2str(target), rc);
2538 lnet_finalize(NULL, msg, rc);
2541 /* completion will be signalled by an event */
2544 EXPORT_SYMBOL(LNetPut);
2547 lnet_create_reply_msg (lnet_ni_t *ni, lnet_msg_t *getmsg)
2549 /* The LND can DMA direct to the GET md (i.e. no REPLY msg). This
2550 * returns a msg for the LND to pass to lnet_finalize() when the sink
2551 * data has been received.
2553 * CAVEAT EMPTOR: 'getmsg' is the original GET, which is freed when
2554 * lnet_finalize() is called on it, so the LND must call this first */
2556 struct lnet_msg *msg = lnet_msg_alloc();
2557 struct lnet_libmd *getmd = getmsg->msg_md;
2558 lnet_process_id_t peer_id = getmsg->msg_target;
2561 LASSERT(!getmsg->msg_target_is_router);
2562 LASSERT(!getmsg->msg_routing);
2565 CERROR("%s: Dropping REPLY from %s: can't allocate msg\n",
2566 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id));
2570 cpt = lnet_cpt_of_cookie(getmd->md_lh.lh_cookie);
2573 LASSERT(getmd->md_refcount > 0);
2575 if (getmd->md_threshold == 0) {
2576 CERROR("%s: Dropping REPLY from %s for inactive MD %p\n",
2577 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id),
2579 lnet_res_unlock(cpt);
2583 LASSERT(getmd->md_offset == 0);
2585 CDEBUG(D_NET, "%s: Reply from %s md %p\n",
2586 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id), getmd);
2588 /* setup information for lnet_build_msg_event */
2589 msg->msg_from = peer_id.nid;
2590 msg->msg_type = LNET_MSG_GET; /* flag this msg as an "optimized" GET */
2591 msg->msg_hdr.src_nid = peer_id.nid;
2592 msg->msg_hdr.payload_length = getmd->md_length;
2593 msg->msg_receiving = 1; /* required by lnet_msg_attach_md */
2595 lnet_msg_attach_md(msg, getmd, getmd->md_offset, getmd->md_length);
2596 lnet_res_unlock(cpt);
2598 cpt = lnet_cpt_of_nid(peer_id.nid, ni);
2601 lnet_msg_commit(msg, cpt);
2602 lnet_net_unlock(cpt);
2604 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
2609 cpt = lnet_cpt_of_nid(peer_id.nid, ni);
2612 the_lnet.ln_counters[cpt]->drop_count++;
2613 the_lnet.ln_counters[cpt]->drop_length += getmd->md_length;
2614 lnet_net_unlock(cpt);
2621 EXPORT_SYMBOL(lnet_create_reply_msg);
2624 lnet_set_reply_msg_len(lnet_ni_t *ni, lnet_msg_t *reply, unsigned int len)
2626 /* Set the REPLY length, now the RDMA that elides the REPLY message has
2627 * completed and I know it. */
2628 LASSERT(reply != NULL);
2629 LASSERT(reply->msg_type == LNET_MSG_GET);
2630 LASSERT(reply->msg_ev.type == LNET_EVENT_REPLY);
2632 /* NB I trusted my peer to RDMA. If she tells me she's written beyond
2633 * the end of my buffer, I might as well be dead. */
2634 LASSERT(len <= reply->msg_ev.mlength);
2636 reply->msg_ev.mlength = len;
2638 EXPORT_SYMBOL(lnet_set_reply_msg_len);
2641 * Initiate an asynchronous GET operation.
2643 * On the initiator node, an LNET_EVENT_SEND is logged when the GET request
2644 * is sent, and an LNET_EVENT_REPLY is logged when the data returned from
2645 * the target node in the REPLY has been written to local MD.
2647 * On the target node, an LNET_EVENT_GET is logged when the GET request
2648 * arrives and is accepted into a MD.
2650 * \param self,target,portal,match_bits,offset See the discussion in LNetPut().
2651 * \param mdh A handle for the MD that describes the memory into which the
2652 * requested data will be received. The MD must be "free floating" (See LNetMDBind()).
2654 * \retval 0 Success, and only in this case events will be generated
2655 * and logged to EQ (if it exists) of the MD.
2656 * \retval -EIO Simulated failure.
2657 * \retval -ENOMEM Memory allocation failure.
2658 * \retval -ENOENT Invalid MD object.
2661 LNetGet(lnet_nid_t self, lnet_handle_md_t mdh,
2662 lnet_process_id_t target, unsigned int portal,
2663 __u64 match_bits, unsigned int offset)
2665 struct lnet_msg *msg;
2666 struct lnet_libmd *md;
2670 LASSERT(the_lnet.ln_refcount > 0);
2672 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
2673 fail_peer(target.nid, 1)) /* shall we now? */
2675 CERROR("Dropping GET to %s: simulated failure\n",
2676 libcfs_id2str(target));
2680 msg = lnet_msg_alloc();
2682 CERROR("Dropping GET to %s: ENOMEM on lnet_msg_t\n",
2683 libcfs_id2str(target));
2687 cpt = lnet_cpt_of_cookie(mdh.cookie);
2690 md = lnet_handle2md(&mdh);
2691 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
2692 CERROR("Dropping GET (%llu:%d:%s): MD (%d) invalid\n",
2693 match_bits, portal, libcfs_id2str(target),
2694 md == NULL ? -1 : md->md_threshold);
2695 if (md != NULL && md->md_me != NULL)
2696 CERROR("REPLY MD also attached to portal %d\n",
2697 md->md_me->me_portal);
2699 lnet_res_unlock(cpt);
2705 CDEBUG(D_NET, "LNetGet -> %s\n", libcfs_id2str(target));
2707 lnet_msg_attach_md(msg, md, 0, 0);
2709 lnet_prep_send(msg, LNET_MSG_GET, target, 0, 0);
2711 msg->msg_hdr.msg.get.match_bits = cpu_to_le64(match_bits);
2712 msg->msg_hdr.msg.get.ptl_index = cpu_to_le32(portal);
2713 msg->msg_hdr.msg.get.src_offset = cpu_to_le32(offset);
2714 msg->msg_hdr.msg.get.sink_length = cpu_to_le32(md->md_length);
2716 /* NB handles only looked up by creator (no flips) */
2717 msg->msg_hdr.msg.get.return_wmd.wh_interface_cookie =
2718 the_lnet.ln_interface_cookie;
2719 msg->msg_hdr.msg.get.return_wmd.wh_object_cookie =
2720 md->md_lh.lh_cookie;
2722 lnet_res_unlock(cpt);
2724 lnet_build_msg_event(msg, LNET_EVENT_SEND);
2726 rc = lnet_send(self, msg, LNET_NID_ANY);
2728 CNETERR("Error sending GET to %s: %d\n",
2729 libcfs_id2str(target), rc);
2730 lnet_finalize(NULL, msg, rc);
2733 /* completion will be signalled by an event */
2736 EXPORT_SYMBOL(LNetGet);
2739 * Calculate distance to node at \a dstnid.
2741 * \param dstnid Target NID.
2742 * \param srcnidp If not NULL, NID of the local interface to reach \a dstnid
2744 * \param orderp If not NULL, order of the route to reach \a dstnid is saved
2747 * \retval 0 If \a dstnid belongs to a local interface, and reserved option
2748 * local_nid_dist_zero is set, which is the default.
2749 * \retval positives Distance to target NID, i.e. number of hops plus one.
2750 * \retval -EHOSTUNREACH If \a dstnid is not reachable.
2753 LNetDist(lnet_nid_t dstnid, lnet_nid_t *srcnidp, __u32 *orderp)
2755 struct list_head *e;
2756 struct lnet_ni *ni = NULL;
2757 lnet_remotenet_t *rnet;
2758 __u32 dstnet = LNET_NIDNET(dstnid);
2762 struct list_head *rn_list;
2764 /* if !local_nid_dist_zero, I don't return a distance of 0 ever
2765 * (when lustre sees a distance of 0, it substitutes 0@lo), so I
2766 * keep order 0 free for 0@lo and order 1 free for a local NID
2769 LASSERT(the_lnet.ln_refcount > 0);
2771 cpt = lnet_net_lock_current();
2773 while ((ni = lnet_get_next_ni_locked(NULL, ni))) {
2774 if (ni->ni_nid == dstnid) {
2775 if (srcnidp != NULL)
2777 if (orderp != NULL) {
2778 if (LNET_NETTYP(LNET_NIDNET(dstnid)) == LOLND)
2783 lnet_net_unlock(cpt);
2785 return local_nid_dist_zero ? 0 : 1;
2788 if (LNET_NIDNET(ni->ni_nid) == dstnet) {
2789 /* Check if ni was originally created in
2790 * current net namespace.
2791 * If not, assign order above 0xffff0000,
2792 * to make this ni not a priority. */
2793 if (!net_eq(ni->ni_net_ns, current->nsproxy->net_ns))
2794 order += 0xffff0000;
2796 if (srcnidp != NULL)
2797 *srcnidp = ni->ni_nid;
2800 lnet_net_unlock(cpt);
2807 rn_list = lnet_net2rnethash(dstnet);
2808 list_for_each(e, rn_list) {
2809 rnet = list_entry(e, lnet_remotenet_t, lrn_list);
2811 if (rnet->lrn_net == dstnet) {
2812 lnet_route_t *route;
2813 lnet_route_t *shortest = NULL;
2814 __u32 shortest_hops = LNET_UNDEFINED_HOPS;
2817 LASSERT(!list_empty(&rnet->lrn_routes));
2819 list_for_each_entry(route, &rnet->lrn_routes,
2821 route_hops = route->lr_hops;
2822 if (route_hops == LNET_UNDEFINED_HOPS)
2824 if (shortest == NULL ||
2825 route_hops < shortest_hops) {
2827 shortest_hops = route_hops;
2831 LASSERT(shortest != NULL);
2832 hops = shortest_hops;
2833 if (srcnidp != NULL) {
2834 ni = lnet_get_next_ni_locked(
2835 shortest->lr_gateway->lpni_net,
2837 *srcnidp = ni->ni_nid;
2841 lnet_net_unlock(cpt);
2847 lnet_net_unlock(cpt);
2848 return -EHOSTUNREACH;
2850 EXPORT_SYMBOL(LNetDist);