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) 2011, 2015, 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 <linux/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)
50 struct lnet_test_peer *tp;
51 struct lnet_test_peer *temp;
53 struct list_head *next;
54 struct list_head cull;
56 /* NB: use lnet_net_lock(0) to serialize operations on test peers */
58 /* Adding a new entry */
59 LIBCFS_ALLOC(tp, sizeof(*tp));
64 tp->tp_threshold = threshold;
67 list_add_tail(&tp->tp_list, &the_lnet.ln_test_peers);
72 /* removing entries */
73 INIT_LIST_HEAD(&cull);
77 list_for_each_safe(el, next, &the_lnet.ln_test_peers) {
78 tp = list_entry(el, struct lnet_test_peer, tp_list);
80 if (!tp->tp_threshold || /* needs culling anyway */
81 nid == LNET_NID_ANY || /* removing all entries */
82 tp->tp_nid == nid) { /* matched this one */
83 list_del(&tp->tp_list);
84 list_add(&tp->tp_list, &cull);
90 list_for_each_entry_safe(tp, temp, &cull, tp_list) {
91 list_del(&tp->tp_list);
92 LIBCFS_FREE(tp, sizeof(*tp));
98 fail_peer(lnet_nid_t nid, int outgoing)
100 struct lnet_test_peer *tp;
101 struct lnet_test_peer *temp;
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, struct lnet_test_peer, tp_list);
115 if (!tp->tp_threshold) {
119 * only cull zombies on outgoing tests,
120 * since we may be at interrupt priority on
123 list_del(&tp->tp_list);
124 list_add(&tp->tp_list, &cull);
129 if (tp->tp_nid == LNET_NID_ANY || /* fail every peer */
130 nid == tp->tp_nid) { /* fail this peer */
133 if (tp->tp_threshold != LNET_MD_THRESH_INF) {
138 list_del(&tp->tp_list);
139 list_add(&tp->tp_list, &cull);
148 list_for_each_entry_safe(tp, temp, &cull, tp_list) {
149 list_del(&tp->tp_list);
151 LIBCFS_FREE(tp, sizeof(*tp));
158 lnet_iov_nob(unsigned int niov, struct kvec *iov)
160 unsigned int nob = 0;
162 LASSERT(!niov || iov);
164 nob += (iov++)->iov_len;
168 EXPORT_SYMBOL(lnet_iov_nob);
171 lnet_copy_iov2iter(struct iov_iter *to,
172 unsigned int nsiov, const struct kvec *siov,
173 unsigned int soffset, unsigned int nob)
175 /* NB diov, siov are READ-ONLY */
182 /* skip complete frags before 'soffset' */
184 while (soffset >= siov->iov_len) {
185 soffset -= siov->iov_len;
191 s = (char *)siov->iov_base + soffset;
192 left = siov->iov_len - soffset;
194 size_t n, copy = left;
200 n = copy_to_iter(s, copy, to);
206 s = (char *)siov->iov_base;
207 left = siov->iov_len;
211 EXPORT_SYMBOL(lnet_copy_iov2iter);
214 lnet_copy_kiov2iter(struct iov_iter *to,
215 unsigned int nsiov, const struct bio_vec *siov,
216 unsigned int soffset, unsigned int nob)
221 LASSERT(!in_interrupt());
224 while (soffset >= siov->bv_len) {
225 soffset -= siov->bv_len;
232 size_t copy = siov->bv_len - soffset, n;
238 n = copy_page_to_iter(siov->bv_page,
239 siov->bv_offset + soffset,
249 EXPORT_SYMBOL(lnet_copy_kiov2iter);
252 lnet_extract_iov(int dst_niov, struct kvec *dst,
253 int src_niov, const struct kvec *src,
254 unsigned int offset, unsigned int len)
257 * Initialise 'dst' to the subset of 'src' starting at 'offset',
258 * for exactly 'len' bytes, and return the number of entries.
259 * NB not destructive to 'src'
261 unsigned int frag_len;
264 if (!len) /* no data => */
265 return 0; /* no frags */
267 LASSERT(src_niov > 0);
268 while (offset >= src->iov_len) { /* skip initial frags */
269 offset -= src->iov_len;
272 LASSERT(src_niov > 0);
277 LASSERT(src_niov > 0);
278 LASSERT((int)niov <= dst_niov);
280 frag_len = src->iov_len - offset;
281 dst->iov_base = ((char *)src->iov_base) + offset;
283 if (len <= frag_len) {
288 dst->iov_len = frag_len;
298 EXPORT_SYMBOL(lnet_extract_iov);
301 lnet_kiov_nob(unsigned int niov, struct bio_vec *kiov)
303 unsigned int nob = 0;
305 LASSERT(!niov || kiov);
307 nob += (kiov++)->bv_len;
311 EXPORT_SYMBOL(lnet_kiov_nob);
314 lnet_extract_kiov(int dst_niov, struct bio_vec *dst,
315 int src_niov, const struct bio_vec *src,
316 unsigned int offset, unsigned int len)
319 * Initialise 'dst' to the subset of 'src' starting at 'offset',
320 * for exactly 'len' bytes, and return the number of entries.
321 * NB not destructive to 'src'
323 unsigned int frag_len;
326 if (!len) /* no data => */
327 return 0; /* no frags */
329 LASSERT(src_niov > 0);
330 while (offset >= src->bv_len) { /* skip initial frags */
331 offset -= src->bv_len;
334 LASSERT(src_niov > 0);
339 LASSERT(src_niov > 0);
340 LASSERT((int)niov <= dst_niov);
342 frag_len = src->bv_len - offset;
343 dst->bv_page = src->bv_page;
344 dst->bv_offset = src->bv_offset + offset;
346 if (len <= frag_len) {
348 LASSERT(dst->bv_offset + dst->bv_len
353 dst->bv_len = frag_len;
354 LASSERT(dst->bv_offset + dst->bv_len <= PAGE_SIZE);
364 EXPORT_SYMBOL(lnet_extract_kiov);
367 lnet_ni_recv(struct lnet_ni *ni, void *private, struct lnet_msg *msg,
368 int delayed, unsigned int offset, unsigned int mlen,
371 unsigned int niov = 0;
372 struct kvec *iov = NULL;
373 struct bio_vec *kiov = NULL;
377 LASSERT(!in_interrupt());
378 LASSERT(!mlen || msg);
381 LASSERT(msg->msg_receiving);
382 LASSERT(!msg->msg_sending);
383 LASSERT(rlen == msg->msg_len);
384 LASSERT(mlen <= msg->msg_len);
385 LASSERT(msg->msg_offset == offset);
386 LASSERT(msg->msg_wanted == mlen);
388 msg->msg_receiving = 0;
391 niov = msg->msg_niov;
393 kiov = msg->msg_kiov;
396 LASSERT(!iov != !kiov);
401 iov_iter_kvec(&to, ITER_KVEC | READ, iov, niov, mlen + offset);
402 iov_iter_advance(&to, offset);
404 iov_iter_bvec(&to, ITER_BVEC | READ, kiov, niov, mlen + offset);
405 iov_iter_advance(&to, offset);
407 rc = ni->ni_lnd->lnd_recv(ni, private, msg, delayed, &to, rlen);
409 lnet_finalize(ni, msg, rc);
413 lnet_setpayloadbuffer(struct lnet_msg *msg)
415 struct lnet_libmd *md = msg->msg_md;
417 LASSERT(msg->msg_len > 0);
418 LASSERT(!msg->msg_routing);
420 LASSERT(!msg->msg_niov);
421 LASSERT(!msg->msg_iov);
422 LASSERT(!msg->msg_kiov);
424 msg->msg_niov = md->md_niov;
425 if (md->md_options & LNET_MD_KIOV)
426 msg->msg_kiov = md->md_iov.kiov;
428 msg->msg_iov = md->md_iov.iov;
432 lnet_prep_send(struct lnet_msg *msg, int type, struct lnet_process_id target,
433 unsigned int offset, unsigned int len)
435 msg->msg_type = type;
436 msg->msg_target = target;
438 msg->msg_offset = offset;
441 lnet_setpayloadbuffer(msg);
443 memset(&msg->msg_hdr, 0, sizeof(msg->msg_hdr));
444 msg->msg_hdr.type = cpu_to_le32(type);
445 msg->msg_hdr.dest_nid = cpu_to_le64(target.nid);
446 msg->msg_hdr.dest_pid = cpu_to_le32(target.pid);
447 /* src_nid will be set later */
448 msg->msg_hdr.src_pid = cpu_to_le32(the_lnet.ln_pid);
449 msg->msg_hdr.payload_length = cpu_to_le32(len);
453 lnet_ni_send(struct lnet_ni *ni, struct lnet_msg *msg)
455 void *priv = msg->msg_private;
458 LASSERT(!in_interrupt());
459 LASSERT(LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND ||
460 (msg->msg_txcredit && msg->msg_peertxcredit));
462 rc = ni->ni_lnd->lnd_send(ni, priv, msg);
464 lnet_finalize(ni, msg, rc);
468 lnet_ni_eager_recv(struct lnet_ni *ni, struct lnet_msg *msg)
472 LASSERT(!msg->msg_sending);
473 LASSERT(msg->msg_receiving);
474 LASSERT(!msg->msg_rx_ready_delay);
475 LASSERT(ni->ni_lnd->lnd_eager_recv);
477 msg->msg_rx_ready_delay = 1;
478 rc = ni->ni_lnd->lnd_eager_recv(ni, msg->msg_private, msg,
481 CERROR("recv from %s / send to %s aborted: eager_recv failed %d\n",
482 libcfs_nid2str(msg->msg_rxpeer->lp_nid),
483 libcfs_id2str(msg->msg_target), rc);
484 LASSERT(rc < 0); /* required by my callers */
490 /* NB: caller shall hold a ref on 'lp' as I'd drop lnet_net_lock */
492 lnet_ni_query_locked(struct lnet_ni *ni, struct lnet_peer *lp)
494 unsigned long last_alive = 0;
496 LASSERT(lnet_peer_aliveness_enabled(lp));
497 LASSERT(ni->ni_lnd->lnd_query);
499 lnet_net_unlock(lp->lp_cpt);
500 ni->ni_lnd->lnd_query(ni, lp->lp_nid, &last_alive);
501 lnet_net_lock(lp->lp_cpt);
503 lp->lp_last_query = cfs_time_current();
505 if (last_alive) /* NI has updated timestamp */
506 lp->lp_last_alive = last_alive;
509 /* NB: always called with lnet_net_lock held */
511 lnet_peer_is_alive(struct lnet_peer *lp, unsigned long now)
514 unsigned long deadline;
516 LASSERT(lnet_peer_aliveness_enabled(lp));
518 /* Trust lnet_notify() if it has more recent aliveness news, but
519 * ignore the initial assumed death (see lnet_peers_start_down()).
521 if (!lp->lp_alive && lp->lp_alive_count > 0 &&
522 cfs_time_aftereq(lp->lp_timestamp, lp->lp_last_alive))
525 deadline = cfs_time_add(lp->lp_last_alive,
526 cfs_time_seconds(lp->lp_ni->ni_peertimeout));
527 alive = cfs_time_after(deadline, now);
529 /* Update obsolete lp_alive except for routers assumed to be dead
530 * initially, because router checker would update aliveness in this
531 * case, and moreover lp_last_alive at peer creation is assumed.
533 if (alive && !lp->lp_alive &&
534 !(lnet_isrouter(lp) && !lp->lp_alive_count))
535 lnet_notify_locked(lp, 0, 1, lp->lp_last_alive);
541 * NB: returns 1 when alive, 0 when dead, negative when error;
542 * may drop the lnet_net_lock
545 lnet_peer_alive_locked(struct lnet_peer *lp)
547 unsigned long now = cfs_time_current();
549 if (!lnet_peer_aliveness_enabled(lp))
552 if (lnet_peer_is_alive(lp, now))
556 * Peer appears dead, but we should avoid frequent NI queries (at
557 * most once per lnet_queryinterval seconds).
559 if (lp->lp_last_query) {
560 static const int lnet_queryinterval = 1;
562 unsigned long next_query =
563 cfs_time_add(lp->lp_last_query,
564 cfs_time_seconds(lnet_queryinterval));
566 if (time_before(now, next_query)) {
568 CWARN("Unexpected aliveness of peer %s: %d < %d (%d/%d)\n",
569 libcfs_nid2str(lp->lp_nid),
570 (int)now, (int)next_query,
572 lp->lp_ni->ni_peertimeout);
577 /* query NI for latest aliveness news */
578 lnet_ni_query_locked(lp->lp_ni, lp);
580 if (lnet_peer_is_alive(lp, now))
583 lnet_notify_locked(lp, 0, 0, lp->lp_last_alive);
588 * \param msg The message to be sent.
589 * \param do_send True if lnet_ni_send() should be called in this function.
590 * lnet_send() is going to lnet_net_unlock immediately after this, so
591 * it sets do_send FALSE and I don't do the unlock/send/lock bit.
593 * \retval LNET_CREDIT_OK If \a msg sent or OK to send.
594 * \retval LNET_CREDIT_WAIT If \a msg blocked for credit.
595 * \retval -EHOSTUNREACH If the next hop of the message appears dead.
596 * \retval -ECANCELED If the MD of the message has been unlinked.
599 lnet_post_send_locked(struct lnet_msg *msg, int do_send)
601 struct lnet_peer *lp = msg->msg_txpeer;
602 struct lnet_ni *ni = lp->lp_ni;
603 int cpt = msg->msg_tx_cpt;
604 struct lnet_tx_queue *tq = ni->ni_tx_queues[cpt];
606 /* non-lnet_send() callers have checked before */
607 LASSERT(!do_send || msg->msg_tx_delayed);
608 LASSERT(!msg->msg_receiving);
609 LASSERT(msg->msg_tx_committed);
611 /* NB 'lp' is always the next hop */
612 if (!(msg->msg_target.pid & LNET_PID_USERFLAG) &&
613 !lnet_peer_alive_locked(lp)) {
614 the_lnet.ln_counters[cpt]->drop_count++;
615 the_lnet.ln_counters[cpt]->drop_length += msg->msg_len;
616 lnet_net_unlock(cpt);
618 CNETERR("Dropping message for %s: peer not alive\n",
619 libcfs_id2str(msg->msg_target));
621 lnet_finalize(ni, msg, -EHOSTUNREACH);
624 return -EHOSTUNREACH;
628 (msg->msg_md->md_flags & LNET_MD_FLAG_ABORTED)) {
629 lnet_net_unlock(cpt);
631 CNETERR("Aborting message for %s: LNetM[DE]Unlink() already called on the MD/ME.\n",
632 libcfs_id2str(msg->msg_target));
634 lnet_finalize(ni, msg, -ECANCELED);
640 if (!msg->msg_peertxcredit) {
641 LASSERT((lp->lp_txcredits < 0) ==
642 !list_empty(&lp->lp_txq));
644 msg->msg_peertxcredit = 1;
645 lp->lp_txqnob += msg->msg_len + sizeof(struct lnet_hdr);
648 if (lp->lp_txcredits < lp->lp_mintxcredits)
649 lp->lp_mintxcredits = lp->lp_txcredits;
651 if (lp->lp_txcredits < 0) {
652 msg->msg_tx_delayed = 1;
653 list_add_tail(&msg->msg_list, &lp->lp_txq);
654 return LNET_CREDIT_WAIT;
658 if (!msg->msg_txcredit) {
659 LASSERT((tq->tq_credits < 0) ==
660 !list_empty(&tq->tq_delayed));
662 msg->msg_txcredit = 1;
665 if (tq->tq_credits < tq->tq_credits_min)
666 tq->tq_credits_min = tq->tq_credits;
668 if (tq->tq_credits < 0) {
669 msg->msg_tx_delayed = 1;
670 list_add_tail(&msg->msg_list, &tq->tq_delayed);
671 return LNET_CREDIT_WAIT;
676 lnet_net_unlock(cpt);
677 lnet_ni_send(ni, msg);
680 return LNET_CREDIT_OK;
683 static struct lnet_rtrbufpool *
684 lnet_msg2bufpool(struct lnet_msg *msg)
686 struct lnet_rtrbufpool *rbp;
689 LASSERT(msg->msg_rx_committed);
691 cpt = msg->msg_rx_cpt;
692 rbp = &the_lnet.ln_rtrpools[cpt][0];
694 LASSERT(msg->msg_len <= LNET_MTU);
695 while (msg->msg_len > (unsigned int)rbp->rbp_npages * PAGE_SIZE) {
697 LASSERT(rbp < &the_lnet.ln_rtrpools[cpt][LNET_NRBPOOLS]);
704 lnet_post_routed_recv_locked(struct lnet_msg *msg, int do_recv)
707 * lnet_parse is going to lnet_net_unlock immediately after this, so it
708 * sets do_recv FALSE and I don't do the unlock/send/lock bit.
709 * I return LNET_CREDIT_WAIT if msg blocked and LNET_CREDIT_OK if
710 * received or OK to receive
712 struct lnet_peer *lp = msg->msg_rxpeer;
713 struct lnet_rtrbufpool *rbp;
714 struct lnet_rtrbuf *rb;
716 LASSERT(!msg->msg_iov);
717 LASSERT(!msg->msg_kiov);
718 LASSERT(!msg->msg_niov);
719 LASSERT(msg->msg_routing);
720 LASSERT(msg->msg_receiving);
721 LASSERT(!msg->msg_sending);
723 /* non-lnet_parse callers only receive delayed messages */
724 LASSERT(!do_recv || msg->msg_rx_delayed);
726 if (!msg->msg_peerrtrcredit) {
727 LASSERT((lp->lp_rtrcredits < 0) ==
728 !list_empty(&lp->lp_rtrq));
730 msg->msg_peerrtrcredit = 1;
732 if (lp->lp_rtrcredits < lp->lp_minrtrcredits)
733 lp->lp_minrtrcredits = lp->lp_rtrcredits;
735 if (lp->lp_rtrcredits < 0) {
736 /* must have checked eager_recv before here */
737 LASSERT(msg->msg_rx_ready_delay);
738 msg->msg_rx_delayed = 1;
739 list_add_tail(&msg->msg_list, &lp->lp_rtrq);
740 return LNET_CREDIT_WAIT;
744 rbp = lnet_msg2bufpool(msg);
746 if (!msg->msg_rtrcredit) {
747 msg->msg_rtrcredit = 1;
749 if (rbp->rbp_credits < rbp->rbp_mincredits)
750 rbp->rbp_mincredits = rbp->rbp_credits;
752 if (rbp->rbp_credits < 0) {
753 /* must have checked eager_recv before here */
754 LASSERT(msg->msg_rx_ready_delay);
755 msg->msg_rx_delayed = 1;
756 list_add_tail(&msg->msg_list, &rbp->rbp_msgs);
757 return LNET_CREDIT_WAIT;
761 LASSERT(!list_empty(&rbp->rbp_bufs));
762 rb = list_entry(rbp->rbp_bufs.next, struct lnet_rtrbuf, rb_list);
763 list_del(&rb->rb_list);
765 msg->msg_niov = rbp->rbp_npages;
766 msg->msg_kiov = &rb->rb_kiov[0];
769 int cpt = msg->msg_rx_cpt;
771 lnet_net_unlock(cpt);
772 lnet_ni_recv(lp->lp_ni, msg->msg_private, msg, 1,
773 0, msg->msg_len, msg->msg_len);
776 return LNET_CREDIT_OK;
780 lnet_return_tx_credits_locked(struct lnet_msg *msg)
782 struct lnet_peer *txpeer = msg->msg_txpeer;
783 struct lnet_msg *msg2;
785 if (msg->msg_txcredit) {
786 struct lnet_ni *ni = txpeer->lp_ni;
787 struct lnet_tx_queue *tq = ni->ni_tx_queues[msg->msg_tx_cpt];
789 /* give back NI txcredits */
790 msg->msg_txcredit = 0;
792 LASSERT((tq->tq_credits < 0) ==
793 !list_empty(&tq->tq_delayed));
796 if (tq->tq_credits <= 0) {
797 msg2 = list_entry(tq->tq_delayed.next,
798 struct lnet_msg, msg_list);
799 list_del(&msg2->msg_list);
801 LASSERT(msg2->msg_txpeer->lp_ni == ni);
802 LASSERT(msg2->msg_tx_delayed);
804 (void)lnet_post_send_locked(msg2, 1);
808 if (msg->msg_peertxcredit) {
809 /* give back peer txcredits */
810 msg->msg_peertxcredit = 0;
812 LASSERT((txpeer->lp_txcredits < 0) ==
813 !list_empty(&txpeer->lp_txq));
815 txpeer->lp_txqnob -= msg->msg_len + sizeof(struct lnet_hdr);
816 LASSERT(txpeer->lp_txqnob >= 0);
818 txpeer->lp_txcredits++;
819 if (txpeer->lp_txcredits <= 0) {
820 msg2 = list_entry(txpeer->lp_txq.next,
821 struct lnet_msg, msg_list);
822 list_del(&msg2->msg_list);
824 LASSERT(msg2->msg_txpeer == txpeer);
825 LASSERT(msg2->msg_tx_delayed);
827 (void)lnet_post_send_locked(msg2, 1);
832 msg->msg_txpeer = NULL;
833 lnet_peer_decref_locked(txpeer);
838 lnet_schedule_blocked_locked(struct lnet_rtrbufpool *rbp)
840 struct lnet_msg *msg;
842 if (list_empty(&rbp->rbp_msgs))
844 msg = list_entry(rbp->rbp_msgs.next,
845 struct lnet_msg, msg_list);
846 list_del(&msg->msg_list);
848 (void)lnet_post_routed_recv_locked(msg, 1);
852 lnet_drop_routed_msgs_locked(struct list_head *list, int cpt)
854 struct list_head drop;
855 struct lnet_msg *msg;
856 struct lnet_msg *tmp;
858 INIT_LIST_HEAD(&drop);
860 list_splice_init(list, &drop);
862 lnet_net_unlock(cpt);
864 list_for_each_entry_safe(msg, tmp, &drop, msg_list) {
865 lnet_ni_recv(msg->msg_rxpeer->lp_ni, msg->msg_private, NULL,
866 0, 0, 0, msg->msg_hdr.payload_length);
867 list_del_init(&msg->msg_list);
868 lnet_finalize(NULL, msg, -ECANCELED);
875 lnet_return_rx_credits_locked(struct lnet_msg *msg)
877 struct lnet_peer *rxpeer = msg->msg_rxpeer;
878 struct lnet_msg *msg2;
880 if (msg->msg_rtrcredit) {
881 /* give back global router credits */
882 struct lnet_rtrbuf *rb;
883 struct lnet_rtrbufpool *rbp;
886 * NB If a msg ever blocks for a buffer in rbp_msgs, it stays
887 * there until it gets one allocated, or aborts the wait
890 LASSERT(msg->msg_kiov);
892 rb = list_entry(msg->msg_kiov, struct lnet_rtrbuf, rb_kiov[0]);
895 msg->msg_kiov = NULL;
896 msg->msg_rtrcredit = 0;
898 LASSERT(rbp == lnet_msg2bufpool(msg));
900 LASSERT((rbp->rbp_credits > 0) ==
901 !list_empty(&rbp->rbp_bufs));
904 * If routing is now turned off, we just drop this buffer and
905 * don't bother trying to return credits.
907 if (!the_lnet.ln_routing) {
908 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
913 * It is possible that a user has lowered the desired number of
914 * buffers in this pool. Make sure we never put back
915 * more buffers than the stated number.
917 if (unlikely(rbp->rbp_credits >= rbp->rbp_req_nbuffers)) {
918 /* Discard this buffer so we don't have too many. */
919 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
922 list_add(&rb->rb_list, &rbp->rbp_bufs);
924 if (rbp->rbp_credits <= 0)
925 lnet_schedule_blocked_locked(rbp);
930 if (msg->msg_peerrtrcredit) {
931 /* give back peer router credits */
932 msg->msg_peerrtrcredit = 0;
934 LASSERT((rxpeer->lp_rtrcredits < 0) ==
935 !list_empty(&rxpeer->lp_rtrq));
937 rxpeer->lp_rtrcredits++;
939 * drop all messages which are queued to be routed on that
942 if (!the_lnet.ln_routing) {
943 lnet_drop_routed_msgs_locked(&rxpeer->lp_rtrq,
945 } else if (rxpeer->lp_rtrcredits <= 0) {
946 msg2 = list_entry(rxpeer->lp_rtrq.next,
947 struct lnet_msg, msg_list);
948 list_del(&msg2->msg_list);
950 (void)lnet_post_routed_recv_locked(msg2, 1);
954 msg->msg_rxpeer = NULL;
955 lnet_peer_decref_locked(rxpeer);
960 lnet_compare_routes(struct lnet_route *r1, struct lnet_route *r2)
962 struct lnet_peer *p1 = r1->lr_gateway;
963 struct lnet_peer *p2 = r2->lr_gateway;
964 int r1_hops = (r1->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r1->lr_hops;
965 int r2_hops = (r2->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r2->lr_hops;
967 if (r1->lr_priority < r2->lr_priority)
970 if (r1->lr_priority > r2->lr_priority)
973 if (r1_hops < r2_hops)
976 if (r1_hops > r2_hops)
979 if (p1->lp_txqnob < p2->lp_txqnob)
982 if (p1->lp_txqnob > p2->lp_txqnob)
985 if (p1->lp_txcredits > p2->lp_txcredits)
988 if (p1->lp_txcredits < p2->lp_txcredits)
991 if (r1->lr_seq - r2->lr_seq <= 0)
997 static struct lnet_peer *
998 lnet_find_route_locked(struct lnet_ni *ni, lnet_nid_t target,
1001 struct lnet_remotenet *rnet;
1002 struct lnet_route *route;
1003 struct lnet_route *best_route;
1004 struct lnet_route *last_route;
1005 struct lnet_peer *lp_best;
1006 struct lnet_peer *lp;
1010 * If @rtr_nid is not LNET_NID_ANY, return the gateway with
1011 * rtr_nid nid, otherwise find the best gateway I can use
1013 rnet = lnet_find_net_locked(LNET_NIDNET(target));
1020 list_for_each_entry(route, &rnet->lrn_routes, lr_list) {
1021 lp = route->lr_gateway;
1023 if (!lnet_is_route_alive(route))
1026 if (ni && lp->lp_ni != ni)
1029 if (lp->lp_nid == rtr_nid) /* it's pre-determined router */
1039 /* no protection on below fields, but it's harmless */
1040 if (last_route->lr_seq - route->lr_seq < 0)
1043 rc = lnet_compare_routes(route, best_route);
1052 * set sequence number on the best router to the latest sequence + 1
1053 * so we can round-robin all routers, it's race and inaccurate but
1054 * harmless and functional
1057 best_route->lr_seq = last_route->lr_seq + 1;
1062 lnet_send(lnet_nid_t src_nid, struct lnet_msg *msg, lnet_nid_t rtr_nid)
1064 lnet_nid_t dst_nid = msg->msg_target.nid;
1065 struct lnet_ni *src_ni;
1066 struct lnet_ni *local_ni;
1067 struct lnet_peer *lp;
1073 * NB: rtr_nid is set to LNET_NID_ANY for all current use-cases,
1074 * but we might want to use pre-determined router for ACK/REPLY
1077 /* NB: ni == interface pre-determined (ACK/REPLY) */
1078 LASSERT(!msg->msg_txpeer);
1079 LASSERT(!msg->msg_sending);
1080 LASSERT(!msg->msg_target_is_router);
1081 LASSERT(!msg->msg_receiving);
1083 msg->msg_sending = 1;
1085 LASSERT(!msg->msg_tx_committed);
1086 cpt = lnet_cpt_of_nid(rtr_nid == LNET_NID_ANY ? dst_nid : rtr_nid);
1090 if (the_lnet.ln_shutdown) {
1091 lnet_net_unlock(cpt);
1095 if (src_nid == LNET_NID_ANY) {
1098 src_ni = lnet_nid2ni_locked(src_nid, cpt);
1100 lnet_net_unlock(cpt);
1101 LCONSOLE_WARN("Can't send to %s: src %s is not a local nid\n",
1102 libcfs_nid2str(dst_nid),
1103 libcfs_nid2str(src_nid));
1106 LASSERT(!msg->msg_routing);
1109 /* Is this for someone on a local network? */
1110 local_ni = lnet_net2ni_locked(LNET_NIDNET(dst_nid), cpt);
1115 src_nid = src_ni->ni_nid;
1116 } else if (src_ni == local_ni) {
1117 lnet_ni_decref_locked(local_ni, cpt);
1119 lnet_ni_decref_locked(local_ni, cpt);
1120 lnet_ni_decref_locked(src_ni, cpt);
1121 lnet_net_unlock(cpt);
1122 LCONSOLE_WARN("No route to %s via from %s\n",
1123 libcfs_nid2str(dst_nid),
1124 libcfs_nid2str(src_nid));
1128 LASSERT(src_nid != LNET_NID_ANY);
1129 lnet_msg_commit(msg, cpt);
1131 if (!msg->msg_routing)
1132 msg->msg_hdr.src_nid = cpu_to_le64(src_nid);
1134 if (src_ni == the_lnet.ln_loni) {
1135 /* No send credit hassles with LOLND */
1136 lnet_net_unlock(cpt);
1137 lnet_ni_send(src_ni, msg);
1140 lnet_ni_decref_locked(src_ni, cpt);
1141 lnet_net_unlock(cpt);
1145 rc = lnet_nid2peer_locked(&lp, dst_nid, cpt);
1146 /* lp has ref on src_ni; lose mine */
1147 lnet_ni_decref_locked(src_ni, cpt);
1149 lnet_net_unlock(cpt);
1150 LCONSOLE_WARN("Error %d finding peer %s\n", rc,
1151 libcfs_nid2str(dst_nid));
1152 /* ENOMEM or shutting down */
1155 LASSERT(lp->lp_ni == src_ni);
1157 /* sending to a remote network */
1158 lp = lnet_find_route_locked(src_ni, dst_nid, rtr_nid);
1161 lnet_ni_decref_locked(src_ni, cpt);
1162 lnet_net_unlock(cpt);
1164 LCONSOLE_WARN("No route to %s via %s (all routers down)\n",
1165 libcfs_id2str(msg->msg_target),
1166 libcfs_nid2str(src_nid));
1167 return -EHOSTUNREACH;
1171 * rtr_nid is LNET_NID_ANY or NID of pre-determined router,
1172 * it's possible that rtr_nid isn't LNET_NID_ANY and lp isn't
1173 * pre-determined router, this can happen if router table
1174 * was changed when we release the lock
1176 if (rtr_nid != lp->lp_nid) {
1177 cpt2 = lnet_cpt_of_nid_locked(lp->lp_nid);
1180 lnet_ni_decref_locked(src_ni, cpt);
1181 lnet_net_unlock(cpt);
1183 rtr_nid = lp->lp_nid;
1189 CDEBUG(D_NET, "Best route to %s via %s for %s %d\n",
1190 libcfs_nid2str(dst_nid), libcfs_nid2str(lp->lp_nid),
1191 lnet_msgtyp2str(msg->msg_type), msg->msg_len);
1195 src_nid = src_ni->ni_nid;
1197 LASSERT(src_ni == lp->lp_ni);
1198 lnet_ni_decref_locked(src_ni, cpt);
1201 lnet_peer_addref_locked(lp);
1203 LASSERT(src_nid != LNET_NID_ANY);
1204 lnet_msg_commit(msg, cpt);
1206 if (!msg->msg_routing) {
1207 /* I'm the source and now I know which NI to send on */
1208 msg->msg_hdr.src_nid = cpu_to_le64(src_nid);
1211 msg->msg_target_is_router = 1;
1212 msg->msg_target.nid = lp->lp_nid;
1213 msg->msg_target.pid = LNET_PID_LUSTRE;
1216 /* 'lp' is our best choice of peer */
1218 LASSERT(!msg->msg_peertxcredit);
1219 LASSERT(!msg->msg_txcredit);
1220 LASSERT(!msg->msg_txpeer);
1222 msg->msg_txpeer = lp; /* msg takes my ref on lp */
1224 rc = lnet_post_send_locked(msg, 0);
1225 lnet_net_unlock(cpt);
1230 if (rc == LNET_CREDIT_OK)
1231 lnet_ni_send(src_ni, msg);
1233 return 0; /* rc == LNET_CREDIT_OK or LNET_CREDIT_WAIT */
1237 lnet_drop_message(struct lnet_ni *ni, int cpt, void *private, unsigned int nob)
1240 the_lnet.ln_counters[cpt]->drop_count++;
1241 the_lnet.ln_counters[cpt]->drop_length += nob;
1242 lnet_net_unlock(cpt);
1244 lnet_ni_recv(ni, private, NULL, 0, 0, 0, nob);
1248 lnet_recv_put(struct lnet_ni *ni, struct lnet_msg *msg)
1250 struct lnet_hdr *hdr = &msg->msg_hdr;
1252 if (msg->msg_wanted)
1253 lnet_setpayloadbuffer(msg);
1255 lnet_build_msg_event(msg, LNET_EVENT_PUT);
1258 * Must I ACK? If so I'll grab the ack_wmd out of the header and put
1259 * it back into the ACK during lnet_finalize()
1261 msg->msg_ack = !lnet_is_wire_handle_none(&hdr->msg.put.ack_wmd) &&
1262 !(msg->msg_md->md_options & LNET_MD_ACK_DISABLE);
1264 lnet_ni_recv(ni, msg->msg_private, msg, msg->msg_rx_delayed,
1265 msg->msg_offset, msg->msg_wanted, hdr->payload_length);
1269 lnet_parse_put(struct lnet_ni *ni, struct lnet_msg *msg)
1271 struct lnet_hdr *hdr = &msg->msg_hdr;
1272 struct lnet_match_info info;
1276 /* Convert put fields to host byte order */
1277 le64_to_cpus(&hdr->msg.put.match_bits);
1278 le32_to_cpus(&hdr->msg.put.ptl_index);
1279 le32_to_cpus(&hdr->msg.put.offset);
1281 info.mi_id.nid = hdr->src_nid;
1282 info.mi_id.pid = hdr->src_pid;
1283 info.mi_opc = LNET_MD_OP_PUT;
1284 info.mi_portal = hdr->msg.put.ptl_index;
1285 info.mi_rlength = hdr->payload_length;
1286 info.mi_roffset = hdr->msg.put.offset;
1287 info.mi_mbits = hdr->msg.put.match_bits;
1289 msg->msg_rx_ready_delay = !ni->ni_lnd->lnd_eager_recv;
1290 ready_delay = msg->msg_rx_ready_delay;
1293 rc = lnet_ptl_match_md(&info, msg);
1298 case LNET_MATCHMD_OK:
1299 lnet_recv_put(ni, msg);
1302 case LNET_MATCHMD_NONE:
1304 * no eager_recv or has already called it, should
1305 * have been attached on delayed list
1310 rc = lnet_ni_eager_recv(ni, msg);
1317 case LNET_MATCHMD_DROP:
1318 CNETERR("Dropping PUT from %s portal %d match %llu offset %d length %d: %d\n",
1319 libcfs_id2str(info.mi_id), info.mi_portal,
1320 info.mi_mbits, info.mi_roffset, info.mi_rlength, rc);
1322 return -ENOENT; /* -ve: OK but no match */
1327 lnet_parse_get(struct lnet_ni *ni, struct lnet_msg *msg, int rdma_get)
1329 struct lnet_match_info info;
1330 struct lnet_hdr *hdr = &msg->msg_hdr;
1331 struct lnet_handle_wire reply_wmd;
1334 /* Convert get fields to host byte order */
1335 le64_to_cpus(&hdr->msg.get.match_bits);
1336 le32_to_cpus(&hdr->msg.get.ptl_index);
1337 le32_to_cpus(&hdr->msg.get.sink_length);
1338 le32_to_cpus(&hdr->msg.get.src_offset);
1340 info.mi_id.nid = hdr->src_nid;
1341 info.mi_id.pid = hdr->src_pid;
1342 info.mi_opc = LNET_MD_OP_GET;
1343 info.mi_portal = hdr->msg.get.ptl_index;
1344 info.mi_rlength = hdr->msg.get.sink_length;
1345 info.mi_roffset = hdr->msg.get.src_offset;
1346 info.mi_mbits = hdr->msg.get.match_bits;
1348 rc = lnet_ptl_match_md(&info, msg);
1349 if (rc == LNET_MATCHMD_DROP) {
1350 CNETERR("Dropping GET from %s portal %d match %llu offset %d length %d\n",
1351 libcfs_id2str(info.mi_id), info.mi_portal,
1352 info.mi_mbits, info.mi_roffset, info.mi_rlength);
1353 return -ENOENT; /* -ve: OK but no match */
1356 LASSERT(rc == LNET_MATCHMD_OK);
1358 lnet_build_msg_event(msg, LNET_EVENT_GET);
1360 reply_wmd = hdr->msg.get.return_wmd;
1362 lnet_prep_send(msg, LNET_MSG_REPLY, info.mi_id,
1363 msg->msg_offset, msg->msg_wanted);
1365 msg->msg_hdr.msg.reply.dst_wmd = reply_wmd;
1368 /* The LND completes the REPLY from her recv procedure */
1369 lnet_ni_recv(ni, msg->msg_private, msg, 0,
1370 msg->msg_offset, msg->msg_len, msg->msg_len);
1374 lnet_ni_recv(ni, msg->msg_private, NULL, 0, 0, 0, 0);
1375 msg->msg_receiving = 0;
1377 rc = lnet_send(ni->ni_nid, msg, LNET_NID_ANY);
1379 /* didn't get as far as lnet_ni_send() */
1380 CERROR("%s: Unable to send REPLY for GET from %s: %d\n",
1381 libcfs_nid2str(ni->ni_nid),
1382 libcfs_id2str(info.mi_id), rc);
1384 lnet_finalize(ni, msg, rc);
1391 lnet_parse_reply(struct lnet_ni *ni, struct lnet_msg *msg)
1393 void *private = msg->msg_private;
1394 struct lnet_hdr *hdr = &msg->msg_hdr;
1395 struct lnet_process_id src = {0};
1396 struct lnet_libmd *md;
1401 cpt = lnet_cpt_of_cookie(hdr->msg.reply.dst_wmd.wh_object_cookie);
1404 src.nid = hdr->src_nid;
1405 src.pid = hdr->src_pid;
1407 /* NB handles only looked up by creator (no flips) */
1408 md = lnet_wire_handle2md(&hdr->msg.reply.dst_wmd);
1409 if (!md || !md->md_threshold || md->md_me) {
1410 CNETERR("%s: Dropping REPLY from %s for %s MD %#llx.%#llx\n",
1411 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1412 !md ? "invalid" : "inactive",
1413 hdr->msg.reply.dst_wmd.wh_interface_cookie,
1414 hdr->msg.reply.dst_wmd.wh_object_cookie);
1415 if (md && md->md_me)
1416 CERROR("REPLY MD also attached to portal %d\n",
1417 md->md_me->me_portal);
1419 lnet_res_unlock(cpt);
1420 return -ENOENT; /* -ve: OK but no match */
1423 LASSERT(!md->md_offset);
1425 rlength = hdr->payload_length;
1426 mlength = min_t(uint, rlength, md->md_length);
1428 if (mlength < rlength &&
1429 !(md->md_options & LNET_MD_TRUNCATE)) {
1430 CNETERR("%s: Dropping REPLY from %s length %d for MD %#llx would overflow (%d)\n",
1431 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1432 rlength, hdr->msg.reply.dst_wmd.wh_object_cookie,
1434 lnet_res_unlock(cpt);
1435 return -ENOENT; /* -ve: OK but no match */
1438 CDEBUG(D_NET, "%s: Reply from %s of length %d/%d into md %#llx\n",
1439 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1440 mlength, rlength, hdr->msg.reply.dst_wmd.wh_object_cookie);
1442 lnet_msg_attach_md(msg, md, 0, mlength);
1445 lnet_setpayloadbuffer(msg);
1447 lnet_res_unlock(cpt);
1449 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
1451 lnet_ni_recv(ni, private, msg, 0, 0, mlength, rlength);
1456 lnet_parse_ack(struct lnet_ni *ni, struct lnet_msg *msg)
1458 struct lnet_hdr *hdr = &msg->msg_hdr;
1459 struct lnet_process_id src = {0};
1460 struct lnet_libmd *md;
1463 src.nid = hdr->src_nid;
1464 src.pid = hdr->src_pid;
1466 /* Convert ack fields to host byte order */
1467 le64_to_cpus(&hdr->msg.ack.match_bits);
1468 le32_to_cpus(&hdr->msg.ack.mlength);
1470 cpt = lnet_cpt_of_cookie(hdr->msg.ack.dst_wmd.wh_object_cookie);
1473 /* NB handles only looked up by creator (no flips) */
1474 md = lnet_wire_handle2md(&hdr->msg.ack.dst_wmd);
1475 if (!md || !md->md_threshold || md->md_me) {
1476 /* Don't moan; this is expected */
1478 "%s: Dropping ACK from %s to %s MD %#llx.%#llx\n",
1479 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1480 !md ? "invalid" : "inactive",
1481 hdr->msg.ack.dst_wmd.wh_interface_cookie,
1482 hdr->msg.ack.dst_wmd.wh_object_cookie);
1483 if (md && md->md_me)
1484 CERROR("Source MD also attached to portal %d\n",
1485 md->md_me->me_portal);
1487 lnet_res_unlock(cpt);
1488 return -ENOENT; /* -ve! */
1491 CDEBUG(D_NET, "%s: ACK from %s into md %#llx\n",
1492 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1493 hdr->msg.ack.dst_wmd.wh_object_cookie);
1495 lnet_msg_attach_md(msg, md, 0, 0);
1497 lnet_res_unlock(cpt);
1499 lnet_build_msg_event(msg, LNET_EVENT_ACK);
1501 lnet_ni_recv(ni, msg->msg_private, msg, 0, 0, 0, msg->msg_len);
1506 * \retval LNET_CREDIT_OK If \a msg is forwarded
1507 * \retval LNET_CREDIT_WAIT If \a msg is blocked because w/o buffer
1508 * \retval -ve error code
1511 lnet_parse_forward_locked(struct lnet_ni *ni, struct lnet_msg *msg)
1515 if (!the_lnet.ln_routing)
1518 if (msg->msg_rxpeer->lp_rtrcredits <= 0 ||
1519 lnet_msg2bufpool(msg)->rbp_credits <= 0) {
1520 if (!ni->ni_lnd->lnd_eager_recv) {
1521 msg->msg_rx_ready_delay = 1;
1523 lnet_net_unlock(msg->msg_rx_cpt);
1524 rc = lnet_ni_eager_recv(ni, msg);
1525 lnet_net_lock(msg->msg_rx_cpt);
1530 rc = lnet_post_routed_recv_locked(msg, 0);
1535 lnet_parse_local(struct lnet_ni *ni, struct lnet_msg *msg)
1539 switch (msg->msg_type) {
1541 rc = lnet_parse_ack(ni, msg);
1544 rc = lnet_parse_put(ni, msg);
1547 rc = lnet_parse_get(ni, msg, msg->msg_rdma_get);
1549 case LNET_MSG_REPLY:
1550 rc = lnet_parse_reply(ni, msg);
1552 default: /* prevent an unused label if !kernel */
1557 LASSERT(!rc || rc == -ENOENT);
1562 lnet_msgtyp2str(int type)
1571 case LNET_MSG_REPLY:
1573 case LNET_MSG_HELLO:
1581 lnet_print_hdr(struct lnet_hdr *hdr)
1583 struct lnet_process_id src = {0};
1584 struct lnet_process_id dst = {0};
1585 char *type_str = lnet_msgtyp2str(hdr->type);
1587 src.nid = hdr->src_nid;
1588 src.pid = hdr->src_pid;
1590 dst.nid = hdr->dest_nid;
1591 dst.pid = hdr->dest_pid;
1593 CWARN("P3 Header at %p of type %s\n", hdr, type_str);
1594 CWARN(" From %s\n", libcfs_id2str(src));
1595 CWARN(" To %s\n", libcfs_id2str(dst));
1597 switch (hdr->type) {
1602 CWARN(" Ptl index %d, ack md %#llx.%#llx, match bits %llu\n",
1603 hdr->msg.put.ptl_index,
1604 hdr->msg.put.ack_wmd.wh_interface_cookie,
1605 hdr->msg.put.ack_wmd.wh_object_cookie,
1606 hdr->msg.put.match_bits);
1607 CWARN(" Length %d, offset %d, hdr data %#llx\n",
1608 hdr->payload_length, hdr->msg.put.offset,
1609 hdr->msg.put.hdr_data);
1613 CWARN(" Ptl index %d, return md %#llx.%#llx, match bits %llu\n",
1614 hdr->msg.get.ptl_index,
1615 hdr->msg.get.return_wmd.wh_interface_cookie,
1616 hdr->msg.get.return_wmd.wh_object_cookie,
1617 hdr->msg.get.match_bits);
1618 CWARN(" Length %d, src offset %d\n",
1619 hdr->msg.get.sink_length,
1620 hdr->msg.get.src_offset);
1624 CWARN(" dst md %#llx.%#llx, manipulated length %d\n",
1625 hdr->msg.ack.dst_wmd.wh_interface_cookie,
1626 hdr->msg.ack.dst_wmd.wh_object_cookie,
1627 hdr->msg.ack.mlength);
1630 case LNET_MSG_REPLY:
1631 CWARN(" dst md %#llx.%#llx, length %d\n",
1632 hdr->msg.reply.dst_wmd.wh_interface_cookie,
1633 hdr->msg.reply.dst_wmd.wh_object_cookie,
1634 hdr->payload_length);
1639 lnet_parse(struct lnet_ni *ni, struct lnet_hdr *hdr, lnet_nid_t from_nid,
1640 void *private, int rdma_req)
1645 struct lnet_msg *msg;
1646 lnet_pid_t dest_pid;
1647 lnet_nid_t dest_nid;
1649 __u32 payload_length;
1652 LASSERT(!in_interrupt());
1654 type = le32_to_cpu(hdr->type);
1655 src_nid = le64_to_cpu(hdr->src_nid);
1656 dest_nid = le64_to_cpu(hdr->dest_nid);
1657 dest_pid = le32_to_cpu(hdr->dest_pid);
1658 payload_length = le32_to_cpu(hdr->payload_length);
1660 for_me = (ni->ni_nid == dest_nid);
1661 cpt = lnet_cpt_of_nid(from_nid);
1666 if (payload_length > 0) {
1667 CERROR("%s, src %s: bad %s payload %d (0 expected)\n",
1668 libcfs_nid2str(from_nid),
1669 libcfs_nid2str(src_nid),
1670 lnet_msgtyp2str(type), payload_length);
1676 case LNET_MSG_REPLY:
1677 if (payload_length >
1678 (__u32)(for_me ? LNET_MAX_PAYLOAD : LNET_MTU)) {
1679 CERROR("%s, src %s: bad %s payload %d (%d max expected)\n",
1680 libcfs_nid2str(from_nid),
1681 libcfs_nid2str(src_nid),
1682 lnet_msgtyp2str(type),
1684 for_me ? LNET_MAX_PAYLOAD : LNET_MTU);
1690 CERROR("%s, src %s: Bad message type 0x%x\n",
1691 libcfs_nid2str(from_nid),
1692 libcfs_nid2str(src_nid), type);
1696 if (the_lnet.ln_routing &&
1697 ni->ni_last_alive != ktime_get_real_seconds()) {
1698 /* NB: so far here is the only place to set NI status to "up */
1700 ni->ni_last_alive = ktime_get_real_seconds();
1701 if (ni->ni_status &&
1702 ni->ni_status->ns_status == LNET_NI_STATUS_DOWN)
1703 ni->ni_status->ns_status = LNET_NI_STATUS_UP;
1708 * Regard a bad destination NID as a protocol error. Senders should
1709 * know what they're doing; if they don't they're misconfigured, buggy
1710 * or malicious so we chop them off at the knees :)
1713 if (LNET_NIDNET(dest_nid) == LNET_NIDNET(ni->ni_nid)) {
1714 /* should have gone direct */
1715 CERROR("%s, src %s: Bad dest nid %s (should have been sent direct)\n",
1716 libcfs_nid2str(from_nid),
1717 libcfs_nid2str(src_nid),
1718 libcfs_nid2str(dest_nid));
1722 if (lnet_islocalnid(dest_nid)) {
1724 * dest is another local NI; sender should have used
1725 * this node's NID on its own network
1727 CERROR("%s, src %s: Bad dest nid %s (it's my nid but on a different network)\n",
1728 libcfs_nid2str(from_nid),
1729 libcfs_nid2str(src_nid),
1730 libcfs_nid2str(dest_nid));
1734 if (rdma_req && type == LNET_MSG_GET) {
1735 CERROR("%s, src %s: Bad optimized GET for %s (final destination must be me)\n",
1736 libcfs_nid2str(from_nid),
1737 libcfs_nid2str(src_nid),
1738 libcfs_nid2str(dest_nid));
1742 if (!the_lnet.ln_routing) {
1743 CERROR("%s, src %s: Dropping message for %s (routing not enabled)\n",
1744 libcfs_nid2str(from_nid),
1745 libcfs_nid2str(src_nid),
1746 libcfs_nid2str(dest_nid));
1752 * Message looks OK; we're not going to return an error, so we MUST
1753 * call back lnd_recv() come what may...
1755 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
1756 fail_peer(src_nid, 0)) { /* shall we now? */
1757 CERROR("%s, src %s: Dropping %s to simulate failure\n",
1758 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
1759 lnet_msgtyp2str(type));
1763 if (!list_empty(&the_lnet.ln_drop_rules) &&
1764 lnet_drop_rule_match(hdr)) {
1765 CDEBUG(D_NET, "%s, src %s, dst %s: Dropping %s to simulate silent message loss\n",
1766 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
1767 libcfs_nid2str(dest_nid), lnet_msgtyp2str(type));
1771 msg = lnet_msg_alloc();
1773 CERROR("%s, src %s: Dropping %s (out of memory)\n",
1774 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
1775 lnet_msgtyp2str(type));
1779 /* msg zeroed in lnet_msg_alloc;
1780 * i.e. flags all clear, pointers NULL etc
1782 msg->msg_type = type;
1783 msg->msg_private = private;
1784 msg->msg_receiving = 1;
1785 msg->msg_rdma_get = rdma_req;
1786 msg->msg_wanted = payload_length;
1787 msg->msg_len = payload_length;
1788 msg->msg_offset = 0;
1789 msg->msg_hdr = *hdr;
1790 /* for building message event */
1791 msg->msg_from = from_nid;
1793 msg->msg_target.pid = dest_pid;
1794 msg->msg_target.nid = dest_nid;
1795 msg->msg_routing = 1;
1798 /* convert common msg->hdr fields to host byteorder */
1799 msg->msg_hdr.type = type;
1800 msg->msg_hdr.src_nid = src_nid;
1801 le32_to_cpus(&msg->msg_hdr.src_pid);
1802 msg->msg_hdr.dest_nid = dest_nid;
1803 msg->msg_hdr.dest_pid = dest_pid;
1804 msg->msg_hdr.payload_length = payload_length;
1808 rc = lnet_nid2peer_locked(&msg->msg_rxpeer, from_nid, cpt);
1810 lnet_net_unlock(cpt);
1811 CERROR("%s, src %s: Dropping %s (error %d looking up sender)\n",
1812 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
1813 lnet_msgtyp2str(type), rc);
1815 if (rc == -ESHUTDOWN)
1816 /* We are shutting down. Don't do anything more */
1821 if (lnet_isrouter(msg->msg_rxpeer)) {
1822 lnet_peer_set_alive(msg->msg_rxpeer);
1823 if (avoid_asym_router_failure &&
1824 LNET_NIDNET(src_nid) != LNET_NIDNET(from_nid)) {
1825 /* received a remote message from router, update
1826 * remote NI status on this router.
1827 * NB: multi-hop routed message will be ignored.
1829 lnet_router_ni_update_locked(msg->msg_rxpeer,
1830 LNET_NIDNET(src_nid));
1834 lnet_msg_commit(msg, cpt);
1836 /* message delay simulation */
1837 if (unlikely(!list_empty(&the_lnet.ln_delay_rules) &&
1838 lnet_delay_rule_match_locked(hdr, msg))) {
1839 lnet_net_unlock(cpt);
1844 rc = lnet_parse_forward_locked(ni, msg);
1845 lnet_net_unlock(cpt);
1850 if (rc == LNET_CREDIT_OK) {
1851 lnet_ni_recv(ni, msg->msg_private, msg, 0,
1852 0, payload_length, payload_length);
1857 lnet_net_unlock(cpt);
1859 rc = lnet_parse_local(ni, msg);
1865 LASSERT(!msg->msg_md);
1866 lnet_finalize(ni, msg, rc);
1869 lnet_drop_message(ni, cpt, private, payload_length);
1872 EXPORT_SYMBOL(lnet_parse);
1875 lnet_drop_delayed_msg_list(struct list_head *head, char *reason)
1877 while (!list_empty(head)) {
1878 struct lnet_process_id id = {0};
1879 struct lnet_msg *msg;
1881 msg = list_entry(head->next, struct lnet_msg, msg_list);
1882 list_del(&msg->msg_list);
1884 id.nid = msg->msg_hdr.src_nid;
1885 id.pid = msg->msg_hdr.src_pid;
1887 LASSERT(!msg->msg_md);
1888 LASSERT(msg->msg_rx_delayed);
1889 LASSERT(msg->msg_rxpeer);
1890 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
1892 CWARN("Dropping delayed PUT from %s portal %d match %llu offset %d length %d: %s\n",
1894 msg->msg_hdr.msg.put.ptl_index,
1895 msg->msg_hdr.msg.put.match_bits,
1896 msg->msg_hdr.msg.put.offset,
1897 msg->msg_hdr.payload_length, reason);
1900 * NB I can't drop msg's ref on msg_rxpeer until after I've
1901 * called lnet_drop_message(), so I just hang onto msg as well
1904 lnet_drop_message(msg->msg_rxpeer->lp_ni,
1905 msg->msg_rxpeer->lp_cpt,
1906 msg->msg_private, msg->msg_len);
1908 * NB: message will not generate event because w/o attached MD,
1909 * but we still should give error code so lnet_msg_decommit()
1910 * can skip counters operations and other checks.
1912 lnet_finalize(msg->msg_rxpeer->lp_ni, msg, -ENOENT);
1917 lnet_recv_delayed_msg_list(struct list_head *head)
1919 while (!list_empty(head)) {
1920 struct lnet_msg *msg;
1921 struct lnet_process_id id;
1923 msg = list_entry(head->next, struct lnet_msg, msg_list);
1924 list_del(&msg->msg_list);
1927 * md won't disappear under me, since each msg
1930 id.nid = msg->msg_hdr.src_nid;
1931 id.pid = msg->msg_hdr.src_pid;
1933 LASSERT(msg->msg_rx_delayed);
1934 LASSERT(msg->msg_md);
1935 LASSERT(msg->msg_rxpeer);
1936 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
1938 CDEBUG(D_NET, "Resuming delayed PUT from %s portal %d match %llu offset %d length %d.\n",
1939 libcfs_id2str(id), msg->msg_hdr.msg.put.ptl_index,
1940 msg->msg_hdr.msg.put.match_bits,
1941 msg->msg_hdr.msg.put.offset,
1942 msg->msg_hdr.payload_length);
1944 lnet_recv_put(msg->msg_rxpeer->lp_ni, msg);
1949 * Initiate an asynchronous PUT operation.
1951 * There are several events associated with a PUT: completion of the send on
1952 * the initiator node (LNET_EVENT_SEND), and when the send completes
1953 * successfully, the receipt of an acknowledgment (LNET_EVENT_ACK) indicating
1954 * that the operation was accepted by the target. The event LNET_EVENT_PUT is
1955 * used at the target node to indicate the completion of incoming data
1958 * The local events will be logged in the EQ associated with the MD pointed to
1959 * by \a mdh handle. Using a MD without an associated EQ results in these
1960 * events being discarded. In this case, the caller must have another
1961 * mechanism (e.g., a higher level protocol) for determining when it is safe
1962 * to modify the memory region associated with the MD.
1964 * Note that LNet does not guarantee the order of LNET_EVENT_SEND and
1965 * LNET_EVENT_ACK, though intuitively ACK should happen after SEND.
1967 * \param self Indicates the NID of a local interface through which to send
1968 * the PUT request. Use LNET_NID_ANY to let LNet choose one by itself.
1969 * \param mdh A handle for the MD that describes the memory to be sent. The MD
1970 * must be "free floating" (See LNetMDBind()).
1971 * \param ack Controls whether an acknowledgment is requested.
1972 * Acknowledgments are only sent when they are requested by the initiating
1973 * process and the target MD enables them.
1974 * \param target A process identifier for the target process.
1975 * \param portal The index in the \a target's portal table.
1976 * \param match_bits The match bits to use for MD selection at the target
1978 * \param offset The offset into the target MD (only used when the target
1979 * MD has the LNET_MD_MANAGE_REMOTE option set).
1980 * \param hdr_data 64 bits of user data that can be included in the message
1981 * header. This data is written to an event queue entry at the target if an
1982 * EQ is present on the matching MD.
1984 * \retval 0 Success, and only in this case events will be generated
1985 * and logged to EQ (if it exists).
1986 * \retval -EIO Simulated failure.
1987 * \retval -ENOMEM Memory allocation failure.
1988 * \retval -ENOENT Invalid MD object.
1990 * \see lnet_event::hdr_data and lnet_event_kind.
1993 LNetPut(lnet_nid_t self, struct lnet_handle_md mdh, enum lnet_ack_req ack,
1994 struct lnet_process_id target, unsigned int portal,
1995 __u64 match_bits, unsigned int offset,
1998 struct lnet_msg *msg;
1999 struct lnet_libmd *md;
2003 LASSERT(the_lnet.ln_refcount > 0);
2005 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
2006 fail_peer(target.nid, 1)) { /* shall we now? */
2007 CERROR("Dropping PUT to %s: simulated failure\n",
2008 libcfs_id2str(target));
2012 msg = lnet_msg_alloc();
2014 CERROR("Dropping PUT to %s: ENOMEM on struct lnet_msg\n",
2015 libcfs_id2str(target));
2018 msg->msg_vmflush = !!memory_pressure_get();
2020 cpt = lnet_cpt_of_cookie(mdh.cookie);
2023 md = lnet_handle2md(&mdh);
2024 if (!md || !md->md_threshold || md->md_me) {
2025 CERROR("Dropping PUT (%llu:%d:%s): MD (%d) invalid\n",
2026 match_bits, portal, libcfs_id2str(target),
2027 !md ? -1 : md->md_threshold);
2028 if (md && md->md_me)
2029 CERROR("Source MD also attached to portal %d\n",
2030 md->md_me->me_portal);
2031 lnet_res_unlock(cpt);
2037 CDEBUG(D_NET, "%s -> %s\n", __func__, libcfs_id2str(target));
2039 lnet_msg_attach_md(msg, md, 0, 0);
2041 lnet_prep_send(msg, LNET_MSG_PUT, target, 0, md->md_length);
2043 msg->msg_hdr.msg.put.match_bits = cpu_to_le64(match_bits);
2044 msg->msg_hdr.msg.put.ptl_index = cpu_to_le32(portal);
2045 msg->msg_hdr.msg.put.offset = cpu_to_le32(offset);
2046 msg->msg_hdr.msg.put.hdr_data = hdr_data;
2048 /* NB handles only looked up by creator (no flips) */
2049 if (ack == LNET_ACK_REQ) {
2050 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
2051 the_lnet.ln_interface_cookie;
2052 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
2053 md->md_lh.lh_cookie;
2055 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
2056 LNET_WIRE_HANDLE_COOKIE_NONE;
2057 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
2058 LNET_WIRE_HANDLE_COOKIE_NONE;
2061 lnet_res_unlock(cpt);
2063 lnet_build_msg_event(msg, LNET_EVENT_SEND);
2065 rc = lnet_send(self, msg, LNET_NID_ANY);
2067 CNETERR("Error sending PUT to %s: %d\n",
2068 libcfs_id2str(target), rc);
2069 lnet_finalize(NULL, msg, rc);
2072 /* completion will be signalled by an event */
2075 EXPORT_SYMBOL(LNetPut);
2078 lnet_create_reply_msg(struct lnet_ni *ni, struct lnet_msg *getmsg)
2081 * The LND can DMA direct to the GET md (i.e. no REPLY msg). This
2082 * returns a msg for the LND to pass to lnet_finalize() when the sink
2083 * data has been received.
2085 * CAVEAT EMPTOR: 'getmsg' is the original GET, which is freed when
2086 * lnet_finalize() is called on it, so the LND must call this first
2088 struct lnet_msg *msg = lnet_msg_alloc();
2089 struct lnet_libmd *getmd = getmsg->msg_md;
2090 struct lnet_process_id peer_id = getmsg->msg_target;
2093 LASSERT(!getmsg->msg_target_is_router);
2094 LASSERT(!getmsg->msg_routing);
2097 CERROR("%s: Dropping REPLY from %s: can't allocate msg\n",
2098 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id));
2102 cpt = lnet_cpt_of_cookie(getmd->md_lh.lh_cookie);
2105 LASSERT(getmd->md_refcount > 0);
2107 if (!getmd->md_threshold) {
2108 CERROR("%s: Dropping REPLY from %s for inactive MD %p\n",
2109 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id),
2111 lnet_res_unlock(cpt);
2115 LASSERT(!getmd->md_offset);
2117 CDEBUG(D_NET, "%s: Reply from %s md %p\n",
2118 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id), getmd);
2120 /* setup information for lnet_build_msg_event */
2121 msg->msg_from = peer_id.nid;
2122 msg->msg_type = LNET_MSG_GET; /* flag this msg as an "optimized" GET */
2123 msg->msg_hdr.src_nid = peer_id.nid;
2124 msg->msg_hdr.payload_length = getmd->md_length;
2125 msg->msg_receiving = 1; /* required by lnet_msg_attach_md */
2127 lnet_msg_attach_md(msg, getmd, getmd->md_offset, getmd->md_length);
2128 lnet_res_unlock(cpt);
2130 cpt = lnet_cpt_of_nid(peer_id.nid);
2133 lnet_msg_commit(msg, cpt);
2134 lnet_net_unlock(cpt);
2136 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
2141 cpt = lnet_cpt_of_nid(peer_id.nid);
2144 the_lnet.ln_counters[cpt]->drop_count++;
2145 the_lnet.ln_counters[cpt]->drop_length += getmd->md_length;
2146 lnet_net_unlock(cpt);
2153 EXPORT_SYMBOL(lnet_create_reply_msg);
2156 lnet_set_reply_msg_len(struct lnet_ni *ni, struct lnet_msg *reply,
2160 * Set the REPLY length, now the RDMA that elides the REPLY message has
2161 * completed and I know it.
2164 LASSERT(reply->msg_type == LNET_MSG_GET);
2165 LASSERT(reply->msg_ev.type == LNET_EVENT_REPLY);
2168 * NB I trusted my peer to RDMA. If she tells me she's written beyond
2169 * the end of my buffer, I might as well be dead.
2171 LASSERT(len <= reply->msg_ev.mlength);
2173 reply->msg_ev.mlength = len;
2175 EXPORT_SYMBOL(lnet_set_reply_msg_len);
2178 * Initiate an asynchronous GET operation.
2180 * On the initiator node, an LNET_EVENT_SEND is logged when the GET request
2181 * is sent, and an LNET_EVENT_REPLY is logged when the data returned from
2182 * the target node in the REPLY has been written to local MD.
2184 * On the target node, an LNET_EVENT_GET is logged when the GET request
2185 * arrives and is accepted into a MD.
2187 * \param self,target,portal,match_bits,offset See the discussion in LNetPut().
2188 * \param mdh A handle for the MD that describes the memory into which the
2189 * requested data will be received. The MD must be "free floating"
2190 * (See LNetMDBind()).
2192 * \retval 0 Success, and only in this case events will be generated
2193 * and logged to EQ (if it exists) of the MD.
2194 * \retval -EIO Simulated failure.
2195 * \retval -ENOMEM Memory allocation failure.
2196 * \retval -ENOENT Invalid MD object.
2199 LNetGet(lnet_nid_t self, struct lnet_handle_md mdh,
2200 struct lnet_process_id target, unsigned int portal,
2201 __u64 match_bits, unsigned int offset)
2203 struct lnet_msg *msg;
2204 struct lnet_libmd *md;
2208 LASSERT(the_lnet.ln_refcount > 0);
2210 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
2211 fail_peer(target.nid, 1)) { /* shall we now? */
2212 CERROR("Dropping GET to %s: simulated failure\n",
2213 libcfs_id2str(target));
2217 msg = lnet_msg_alloc();
2219 CERROR("Dropping GET to %s: ENOMEM on struct lnet_msg\n",
2220 libcfs_id2str(target));
2224 cpt = lnet_cpt_of_cookie(mdh.cookie);
2227 md = lnet_handle2md(&mdh);
2228 if (!md || !md->md_threshold || md->md_me) {
2229 CERROR("Dropping GET (%llu:%d:%s): MD (%d) invalid\n",
2230 match_bits, portal, libcfs_id2str(target),
2231 !md ? -1 : md->md_threshold);
2232 if (md && md->md_me)
2233 CERROR("REPLY MD also attached to portal %d\n",
2234 md->md_me->me_portal);
2236 lnet_res_unlock(cpt);
2242 CDEBUG(D_NET, "%s -> %s\n", __func__, libcfs_id2str(target));
2244 lnet_msg_attach_md(msg, md, 0, 0);
2246 lnet_prep_send(msg, LNET_MSG_GET, target, 0, 0);
2248 msg->msg_hdr.msg.get.match_bits = cpu_to_le64(match_bits);
2249 msg->msg_hdr.msg.get.ptl_index = cpu_to_le32(portal);
2250 msg->msg_hdr.msg.get.src_offset = cpu_to_le32(offset);
2251 msg->msg_hdr.msg.get.sink_length = cpu_to_le32(md->md_length);
2253 /* NB handles only looked up by creator (no flips) */
2254 msg->msg_hdr.msg.get.return_wmd.wh_interface_cookie =
2255 the_lnet.ln_interface_cookie;
2256 msg->msg_hdr.msg.get.return_wmd.wh_object_cookie =
2257 md->md_lh.lh_cookie;
2259 lnet_res_unlock(cpt);
2261 lnet_build_msg_event(msg, LNET_EVENT_SEND);
2263 rc = lnet_send(self, msg, LNET_NID_ANY);
2265 CNETERR("Error sending GET to %s: %d\n",
2266 libcfs_id2str(target), rc);
2267 lnet_finalize(NULL, msg, rc);
2270 /* completion will be signalled by an event */
2273 EXPORT_SYMBOL(LNetGet);
2276 * Calculate distance to node at \a dstnid.
2278 * \param dstnid Target NID.
2279 * \param srcnidp If not NULL, NID of the local interface to reach \a dstnid
2281 * \param orderp If not NULL, order of the route to reach \a dstnid is saved
2284 * \retval 0 If \a dstnid belongs to a local interface, and reserved option
2285 * local_nid_dist_zero is set, which is the default.
2286 * \retval positives Distance to target NID, i.e. number of hops plus one.
2287 * \retval -EHOSTUNREACH If \a dstnid is not reachable.
2290 LNetDist(lnet_nid_t dstnid, lnet_nid_t *srcnidp, __u32 *orderp)
2292 struct list_head *e;
2294 struct lnet_remotenet *rnet;
2295 __u32 dstnet = LNET_NIDNET(dstnid);
2299 struct list_head *rn_list;
2302 * if !local_nid_dist_zero, I don't return a distance of 0 ever
2303 * (when lustre sees a distance of 0, it substitutes 0@lo), so I
2304 * keep order 0 free for 0@lo and order 1 free for a local NID
2307 LASSERT(the_lnet.ln_refcount > 0);
2309 cpt = lnet_net_lock_current();
2311 list_for_each(e, &the_lnet.ln_nis) {
2312 ni = list_entry(e, struct lnet_ni, ni_list);
2314 if (ni->ni_nid == dstnid) {
2318 if (LNET_NETTYP(LNET_NIDNET(dstnid)) == LOLND)
2323 lnet_net_unlock(cpt);
2325 return local_nid_dist_zero ? 0 : 1;
2328 if (LNET_NIDNET(ni->ni_nid) == dstnet) {
2330 * Check if ni was originally created in
2331 * current net namespace.
2332 * If not, assign order above 0xffff0000,
2333 * to make this ni not a priority.
2335 if (!net_eq(ni->ni_net_ns, current->nsproxy->net_ns))
2336 order += 0xffff0000;
2339 *srcnidp = ni->ni_nid;
2342 lnet_net_unlock(cpt);
2349 rn_list = lnet_net2rnethash(dstnet);
2350 list_for_each(e, rn_list) {
2351 rnet = list_entry(e, struct lnet_remotenet, lrn_list);
2353 if (rnet->lrn_net == dstnet) {
2354 struct lnet_route *route;
2355 struct lnet_route *shortest = NULL;
2356 __u32 shortest_hops = LNET_UNDEFINED_HOPS;
2359 LASSERT(!list_empty(&rnet->lrn_routes));
2361 list_for_each_entry(route, &rnet->lrn_routes,
2363 route_hops = route->lr_hops;
2364 if (route_hops == LNET_UNDEFINED_HOPS)
2367 route_hops < shortest_hops) {
2369 shortest_hops = route_hops;
2374 hops = shortest_hops;
2376 *srcnidp = shortest->lr_gateway->lp_ni->ni_nid;
2379 lnet_net_unlock(cpt);
2385 lnet_net_unlock(cpt);
2386 return -EHOSTUNREACH;
2388 EXPORT_SYMBOL(LNetDist);