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 "../../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)
51 lnet_test_peer_t *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, lnet_test_peer_t, 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 lnet_test_peer_t *tp;
101 lnet_test_peer_t *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, lnet_test_peer_t, 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;
199 n = copy_to_iter(s, copy, to);
205 s = (char *)siov->iov_base;
206 left = siov->iov_len;
210 EXPORT_SYMBOL(lnet_copy_iov2iter);
213 lnet_copy_kiov2iter(struct iov_iter *to,
214 unsigned int nsiov, const lnet_kiov_t *siov,
215 unsigned int soffset, unsigned int nob)
220 LASSERT(!in_interrupt());
223 while (soffset >= siov->bv_len) {
224 soffset -= siov->bv_len;
231 size_t copy = siov->bv_len - soffset, n;
237 n = copy_page_to_iter(siov->bv_page,
238 siov->bv_offset + soffset,
248 EXPORT_SYMBOL(lnet_copy_kiov2iter);
251 lnet_extract_iov(int dst_niov, struct kvec *dst,
252 int src_niov, const struct kvec *src,
253 unsigned int offset, unsigned int len)
256 * Initialise 'dst' to the subset of 'src' starting at 'offset',
257 * for exactly 'len' bytes, and return the number of entries.
258 * NB not destructive to 'src'
260 unsigned int frag_len;
263 if (!len) /* no data => */
264 return 0; /* no frags */
266 LASSERT(src_niov > 0);
267 while (offset >= src->iov_len) { /* skip initial frags */
268 offset -= src->iov_len;
271 LASSERT(src_niov > 0);
276 LASSERT(src_niov > 0);
277 LASSERT((int)niov <= dst_niov);
279 frag_len = src->iov_len - offset;
280 dst->iov_base = ((char *)src->iov_base) + offset;
282 if (len <= frag_len) {
287 dst->iov_len = frag_len;
297 EXPORT_SYMBOL(lnet_extract_iov);
300 lnet_kiov_nob(unsigned int niov, lnet_kiov_t *kiov)
302 unsigned int nob = 0;
304 LASSERT(!niov || kiov);
306 nob += (kiov++)->bv_len;
310 EXPORT_SYMBOL(lnet_kiov_nob);
313 lnet_extract_kiov(int dst_niov, lnet_kiov_t *dst,
314 int src_niov, const lnet_kiov_t *src,
315 unsigned int offset, unsigned int len)
318 * Initialise 'dst' to the subset of 'src' starting at 'offset',
319 * for exactly 'len' bytes, and return the number of entries.
320 * NB not destructive to 'src'
322 unsigned int frag_len;
325 if (!len) /* no data => */
326 return 0; /* no frags */
328 LASSERT(src_niov > 0);
329 while (offset >= src->bv_len) { /* skip initial frags */
330 offset -= src->bv_len;
333 LASSERT(src_niov > 0);
338 LASSERT(src_niov > 0);
339 LASSERT((int)niov <= dst_niov);
341 frag_len = src->bv_len - offset;
342 dst->bv_page = src->bv_page;
343 dst->bv_offset = src->bv_offset + offset;
345 if (len <= frag_len) {
347 LASSERT(dst->bv_offset + dst->bv_len
352 dst->bv_len = frag_len;
353 LASSERT(dst->bv_offset + dst->bv_len <= PAGE_SIZE);
363 EXPORT_SYMBOL(lnet_extract_kiov);
366 lnet_ni_recv(lnet_ni_t *ni, void *private, lnet_msg_t *msg, int delayed,
367 unsigned int offset, unsigned int mlen, unsigned int rlen)
369 unsigned int niov = 0;
370 struct kvec *iov = NULL;
371 lnet_kiov_t *kiov = NULL;
375 LASSERT(!in_interrupt());
376 LASSERT(!mlen || msg);
379 LASSERT(msg->msg_receiving);
380 LASSERT(!msg->msg_sending);
381 LASSERT(rlen == msg->msg_len);
382 LASSERT(mlen <= msg->msg_len);
383 LASSERT(msg->msg_offset == offset);
384 LASSERT(msg->msg_wanted == mlen);
386 msg->msg_receiving = 0;
389 niov = msg->msg_niov;
391 kiov = msg->msg_kiov;
394 LASSERT(!iov != !kiov);
399 iov_iter_kvec(&to, ITER_KVEC | READ, iov, niov, mlen + offset);
400 iov_iter_advance(&to, offset);
402 iov_iter_bvec(&to, ITER_BVEC | READ, kiov, niov, mlen + offset);
403 iov_iter_advance(&to, offset);
405 rc = ni->ni_lnd->lnd_recv(ni, private, msg, delayed, &to, rlen);
407 lnet_finalize(ni, msg, rc);
411 lnet_setpayloadbuffer(lnet_msg_t *msg)
413 lnet_libmd_t *md = msg->msg_md;
415 LASSERT(msg->msg_len > 0);
416 LASSERT(!msg->msg_routing);
418 LASSERT(!msg->msg_niov);
419 LASSERT(!msg->msg_iov);
420 LASSERT(!msg->msg_kiov);
422 msg->msg_niov = md->md_niov;
423 if (md->md_options & LNET_MD_KIOV)
424 msg->msg_kiov = md->md_iov.kiov;
426 msg->msg_iov = md->md_iov.iov;
430 lnet_prep_send(lnet_msg_t *msg, int type, lnet_process_id_t target,
431 unsigned int offset, unsigned int len)
433 msg->msg_type = type;
434 msg->msg_target = target;
436 msg->msg_offset = offset;
439 lnet_setpayloadbuffer(msg);
441 memset(&msg->msg_hdr, 0, sizeof(msg->msg_hdr));
442 msg->msg_hdr.type = cpu_to_le32(type);
443 msg->msg_hdr.dest_nid = cpu_to_le64(target.nid);
444 msg->msg_hdr.dest_pid = cpu_to_le32(target.pid);
445 /* src_nid will be set later */
446 msg->msg_hdr.src_pid = cpu_to_le32(the_lnet.ln_pid);
447 msg->msg_hdr.payload_length = cpu_to_le32(len);
451 lnet_ni_send(lnet_ni_t *ni, lnet_msg_t *msg)
453 void *priv = msg->msg_private;
456 LASSERT(!in_interrupt());
457 LASSERT(LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND ||
458 (msg->msg_txcredit && msg->msg_peertxcredit));
460 rc = ni->ni_lnd->lnd_send(ni, priv, msg);
462 lnet_finalize(ni, msg, rc);
466 lnet_ni_eager_recv(lnet_ni_t *ni, lnet_msg_t *msg)
470 LASSERT(!msg->msg_sending);
471 LASSERT(msg->msg_receiving);
472 LASSERT(!msg->msg_rx_ready_delay);
473 LASSERT(ni->ni_lnd->lnd_eager_recv);
475 msg->msg_rx_ready_delay = 1;
476 rc = ni->ni_lnd->lnd_eager_recv(ni, msg->msg_private, msg,
479 CERROR("recv from %s / send to %s aborted: eager_recv failed %d\n",
480 libcfs_nid2str(msg->msg_rxpeer->lp_nid),
481 libcfs_id2str(msg->msg_target), rc);
482 LASSERT(rc < 0); /* required by my callers */
488 /* NB: caller shall hold a ref on 'lp' as I'd drop lnet_net_lock */
490 lnet_ni_query_locked(lnet_ni_t *ni, lnet_peer_t *lp)
492 unsigned long last_alive = 0;
494 LASSERT(lnet_peer_aliveness_enabled(lp));
495 LASSERT(ni->ni_lnd->lnd_query);
497 lnet_net_unlock(lp->lp_cpt);
498 ni->ni_lnd->lnd_query(ni, lp->lp_nid, &last_alive);
499 lnet_net_lock(lp->lp_cpt);
501 lp->lp_last_query = cfs_time_current();
503 if (last_alive) /* NI has updated timestamp */
504 lp->lp_last_alive = last_alive;
507 /* NB: always called with lnet_net_lock held */
509 lnet_peer_is_alive(lnet_peer_t *lp, unsigned long now)
512 unsigned long deadline;
514 LASSERT(lnet_peer_aliveness_enabled(lp));
516 /* Trust lnet_notify() if it has more recent aliveness news, but
517 * ignore the initial assumed death (see lnet_peers_start_down()).
519 if (!lp->lp_alive && lp->lp_alive_count > 0 &&
520 cfs_time_aftereq(lp->lp_timestamp, lp->lp_last_alive))
523 deadline = cfs_time_add(lp->lp_last_alive,
524 cfs_time_seconds(lp->lp_ni->ni_peertimeout));
525 alive = cfs_time_after(deadline, now);
527 /* Update obsolete lp_alive except for routers assumed to be dead
528 * initially, because router checker would update aliveness in this
529 * case, and moreover lp_last_alive at peer creation is assumed.
531 if (alive && !lp->lp_alive &&
532 !(lnet_isrouter(lp) && !lp->lp_alive_count))
533 lnet_notify_locked(lp, 0, 1, lp->lp_last_alive);
539 * NB: returns 1 when alive, 0 when dead, negative when error;
540 * may drop the lnet_net_lock
543 lnet_peer_alive_locked(lnet_peer_t *lp)
545 unsigned long now = cfs_time_current();
547 if (!lnet_peer_aliveness_enabled(lp))
550 if (lnet_peer_is_alive(lp, now))
554 * Peer appears dead, but we should avoid frequent NI queries (at
555 * most once per lnet_queryinterval seconds).
557 if (lp->lp_last_query) {
558 static const int lnet_queryinterval = 1;
560 unsigned long next_query =
561 cfs_time_add(lp->lp_last_query,
562 cfs_time_seconds(lnet_queryinterval));
564 if (time_before(now, next_query)) {
566 CWARN("Unexpected aliveness of peer %s: %d < %d (%d/%d)\n",
567 libcfs_nid2str(lp->lp_nid),
568 (int)now, (int)next_query,
570 lp->lp_ni->ni_peertimeout);
575 /* query NI for latest aliveness news */
576 lnet_ni_query_locked(lp->lp_ni, lp);
578 if (lnet_peer_is_alive(lp, now))
581 lnet_notify_locked(lp, 0, 0, lp->lp_last_alive);
586 * \param msg The message to be sent.
587 * \param do_send True if lnet_ni_send() should be called in this function.
588 * lnet_send() is going to lnet_net_unlock immediately after this, so
589 * it sets do_send FALSE and I don't do the unlock/send/lock bit.
591 * \retval LNET_CREDIT_OK If \a msg sent or OK to send.
592 * \retval LNET_CREDIT_WAIT If \a msg blocked for credit.
593 * \retval -EHOSTUNREACH If the next hop of the message appears dead.
594 * \retval -ECANCELED If the MD of the message has been unlinked.
597 lnet_post_send_locked(lnet_msg_t *msg, int do_send)
599 lnet_peer_t *lp = msg->msg_txpeer;
600 lnet_ni_t *ni = lp->lp_ni;
601 int cpt = msg->msg_tx_cpt;
602 struct lnet_tx_queue *tq = ni->ni_tx_queues[cpt];
604 /* non-lnet_send() callers have checked before */
605 LASSERT(!do_send || msg->msg_tx_delayed);
606 LASSERT(!msg->msg_receiving);
607 LASSERT(msg->msg_tx_committed);
609 /* NB 'lp' is always the next hop */
610 if (!(msg->msg_target.pid & LNET_PID_USERFLAG) &&
611 !lnet_peer_alive_locked(lp)) {
612 the_lnet.ln_counters[cpt]->drop_count++;
613 the_lnet.ln_counters[cpt]->drop_length += msg->msg_len;
614 lnet_net_unlock(cpt);
616 CNETERR("Dropping message for %s: peer not alive\n",
617 libcfs_id2str(msg->msg_target));
619 lnet_finalize(ni, msg, -EHOSTUNREACH);
622 return -EHOSTUNREACH;
626 (msg->msg_md->md_flags & LNET_MD_FLAG_ABORTED)) {
627 lnet_net_unlock(cpt);
629 CNETERR("Aborting message for %s: LNetM[DE]Unlink() already called on the MD/ME.\n",
630 libcfs_id2str(msg->msg_target));
632 lnet_finalize(ni, msg, -ECANCELED);
638 if (!msg->msg_peertxcredit) {
639 LASSERT((lp->lp_txcredits < 0) ==
640 !list_empty(&lp->lp_txq));
642 msg->msg_peertxcredit = 1;
643 lp->lp_txqnob += msg->msg_len + sizeof(lnet_hdr_t);
646 if (lp->lp_txcredits < lp->lp_mintxcredits)
647 lp->lp_mintxcredits = lp->lp_txcredits;
649 if (lp->lp_txcredits < 0) {
650 msg->msg_tx_delayed = 1;
651 list_add_tail(&msg->msg_list, &lp->lp_txq);
652 return LNET_CREDIT_WAIT;
656 if (!msg->msg_txcredit) {
657 LASSERT((tq->tq_credits < 0) ==
658 !list_empty(&tq->tq_delayed));
660 msg->msg_txcredit = 1;
663 if (tq->tq_credits < tq->tq_credits_min)
664 tq->tq_credits_min = tq->tq_credits;
666 if (tq->tq_credits < 0) {
667 msg->msg_tx_delayed = 1;
668 list_add_tail(&msg->msg_list, &tq->tq_delayed);
669 return LNET_CREDIT_WAIT;
674 lnet_net_unlock(cpt);
675 lnet_ni_send(ni, msg);
678 return LNET_CREDIT_OK;
681 static lnet_rtrbufpool_t *
682 lnet_msg2bufpool(lnet_msg_t *msg)
684 lnet_rtrbufpool_t *rbp;
687 LASSERT(msg->msg_rx_committed);
689 cpt = msg->msg_rx_cpt;
690 rbp = &the_lnet.ln_rtrpools[cpt][0];
692 LASSERT(msg->msg_len <= LNET_MTU);
693 while (msg->msg_len > (unsigned int)rbp->rbp_npages * PAGE_SIZE) {
695 LASSERT(rbp < &the_lnet.ln_rtrpools[cpt][LNET_NRBPOOLS]);
702 lnet_post_routed_recv_locked(lnet_msg_t *msg, int do_recv)
705 * lnet_parse is going to lnet_net_unlock immediately after this, so it
706 * sets do_recv FALSE and I don't do the unlock/send/lock bit.
707 * I return LNET_CREDIT_WAIT if msg blocked and LNET_CREDIT_OK if
708 * received or OK to receive
710 lnet_peer_t *lp = msg->msg_rxpeer;
711 lnet_rtrbufpool_t *rbp;
714 LASSERT(!msg->msg_iov);
715 LASSERT(!msg->msg_kiov);
716 LASSERT(!msg->msg_niov);
717 LASSERT(msg->msg_routing);
718 LASSERT(msg->msg_receiving);
719 LASSERT(!msg->msg_sending);
721 /* non-lnet_parse callers only receive delayed messages */
722 LASSERT(!do_recv || msg->msg_rx_delayed);
724 if (!msg->msg_peerrtrcredit) {
725 LASSERT((lp->lp_rtrcredits < 0) ==
726 !list_empty(&lp->lp_rtrq));
728 msg->msg_peerrtrcredit = 1;
730 if (lp->lp_rtrcredits < lp->lp_minrtrcredits)
731 lp->lp_minrtrcredits = lp->lp_rtrcredits;
733 if (lp->lp_rtrcredits < 0) {
734 /* must have checked eager_recv before here */
735 LASSERT(msg->msg_rx_ready_delay);
736 msg->msg_rx_delayed = 1;
737 list_add_tail(&msg->msg_list, &lp->lp_rtrq);
738 return LNET_CREDIT_WAIT;
742 rbp = lnet_msg2bufpool(msg);
744 if (!msg->msg_rtrcredit) {
745 msg->msg_rtrcredit = 1;
747 if (rbp->rbp_credits < rbp->rbp_mincredits)
748 rbp->rbp_mincredits = rbp->rbp_credits;
750 if (rbp->rbp_credits < 0) {
751 /* must have checked eager_recv before here */
752 LASSERT(msg->msg_rx_ready_delay);
753 msg->msg_rx_delayed = 1;
754 list_add_tail(&msg->msg_list, &rbp->rbp_msgs);
755 return LNET_CREDIT_WAIT;
759 LASSERT(!list_empty(&rbp->rbp_bufs));
760 rb = list_entry(rbp->rbp_bufs.next, lnet_rtrbuf_t, rb_list);
761 list_del(&rb->rb_list);
763 msg->msg_niov = rbp->rbp_npages;
764 msg->msg_kiov = &rb->rb_kiov[0];
767 int cpt = msg->msg_rx_cpt;
769 lnet_net_unlock(cpt);
770 lnet_ni_recv(lp->lp_ni, msg->msg_private, msg, 1,
771 0, msg->msg_len, msg->msg_len);
774 return LNET_CREDIT_OK;
778 lnet_return_tx_credits_locked(lnet_msg_t *msg)
780 lnet_peer_t *txpeer = msg->msg_txpeer;
783 if (msg->msg_txcredit) {
784 struct lnet_ni *ni = txpeer->lp_ni;
785 struct lnet_tx_queue *tq = ni->ni_tx_queues[msg->msg_tx_cpt];
787 /* give back NI txcredits */
788 msg->msg_txcredit = 0;
790 LASSERT((tq->tq_credits < 0) ==
791 !list_empty(&tq->tq_delayed));
794 if (tq->tq_credits <= 0) {
795 msg2 = list_entry(tq->tq_delayed.next,
796 lnet_msg_t, msg_list);
797 list_del(&msg2->msg_list);
799 LASSERT(msg2->msg_txpeer->lp_ni == ni);
800 LASSERT(msg2->msg_tx_delayed);
802 (void)lnet_post_send_locked(msg2, 1);
806 if (msg->msg_peertxcredit) {
807 /* give back peer txcredits */
808 msg->msg_peertxcredit = 0;
810 LASSERT((txpeer->lp_txcredits < 0) ==
811 !list_empty(&txpeer->lp_txq));
813 txpeer->lp_txqnob -= msg->msg_len + sizeof(lnet_hdr_t);
814 LASSERT(txpeer->lp_txqnob >= 0);
816 txpeer->lp_txcredits++;
817 if (txpeer->lp_txcredits <= 0) {
818 msg2 = list_entry(txpeer->lp_txq.next,
819 lnet_msg_t, msg_list);
820 list_del(&msg2->msg_list);
822 LASSERT(msg2->msg_txpeer == txpeer);
823 LASSERT(msg2->msg_tx_delayed);
825 (void)lnet_post_send_locked(msg2, 1);
830 msg->msg_txpeer = NULL;
831 lnet_peer_decref_locked(txpeer);
836 lnet_schedule_blocked_locked(lnet_rtrbufpool_t *rbp)
840 if (list_empty(&rbp->rbp_msgs))
842 msg = list_entry(rbp->rbp_msgs.next,
843 lnet_msg_t, msg_list);
844 list_del(&msg->msg_list);
846 (void)lnet_post_routed_recv_locked(msg, 1);
850 lnet_drop_routed_msgs_locked(struct list_head *list, int cpt)
852 struct list_head drop;
856 INIT_LIST_HEAD(&drop);
858 list_splice_init(list, &drop);
860 lnet_net_unlock(cpt);
862 list_for_each_entry_safe(msg, tmp, &drop, msg_list) {
863 lnet_ni_recv(msg->msg_rxpeer->lp_ni, msg->msg_private, NULL,
864 0, 0, 0, msg->msg_hdr.payload_length);
865 list_del_init(&msg->msg_list);
866 lnet_finalize(NULL, msg, -ECANCELED);
873 lnet_return_rx_credits_locked(lnet_msg_t *msg)
875 lnet_peer_t *rxpeer = msg->msg_rxpeer;
878 if (msg->msg_rtrcredit) {
879 /* give back global router credits */
881 lnet_rtrbufpool_t *rbp;
884 * NB If a msg ever blocks for a buffer in rbp_msgs, it stays
885 * there until it gets one allocated, or aborts the wait
888 LASSERT(msg->msg_kiov);
890 rb = list_entry(msg->msg_kiov, lnet_rtrbuf_t, rb_kiov[0]);
893 msg->msg_kiov = NULL;
894 msg->msg_rtrcredit = 0;
896 LASSERT(rbp == lnet_msg2bufpool(msg));
898 LASSERT((rbp->rbp_credits > 0) ==
899 !list_empty(&rbp->rbp_bufs));
902 * If routing is now turned off, we just drop this buffer and
903 * don't bother trying to return credits.
905 if (!the_lnet.ln_routing) {
906 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
911 * It is possible that a user has lowered the desired number of
912 * buffers in this pool. Make sure we never put back
913 * more buffers than the stated number.
915 if (unlikely(rbp->rbp_credits >= rbp->rbp_req_nbuffers)) {
916 /* Discard this buffer so we don't have too many. */
917 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
920 list_add(&rb->rb_list, &rbp->rbp_bufs);
922 if (rbp->rbp_credits <= 0)
923 lnet_schedule_blocked_locked(rbp);
928 if (msg->msg_peerrtrcredit) {
929 /* give back peer router credits */
930 msg->msg_peerrtrcredit = 0;
932 LASSERT((rxpeer->lp_rtrcredits < 0) ==
933 !list_empty(&rxpeer->lp_rtrq));
935 rxpeer->lp_rtrcredits++;
937 * drop all messages which are queued to be routed on that
940 if (!the_lnet.ln_routing) {
941 lnet_drop_routed_msgs_locked(&rxpeer->lp_rtrq,
943 } else if (rxpeer->lp_rtrcredits <= 0) {
944 msg2 = list_entry(rxpeer->lp_rtrq.next,
945 lnet_msg_t, msg_list);
946 list_del(&msg2->msg_list);
948 (void)lnet_post_routed_recv_locked(msg2, 1);
952 msg->msg_rxpeer = NULL;
953 lnet_peer_decref_locked(rxpeer);
958 lnet_compare_routes(lnet_route_t *r1, lnet_route_t *r2)
960 lnet_peer_t *p1 = r1->lr_gateway;
961 lnet_peer_t *p2 = r2->lr_gateway;
962 int r1_hops = (r1->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r1->lr_hops;
963 int r2_hops = (r2->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r2->lr_hops;
965 if (r1->lr_priority < r2->lr_priority)
968 if (r1->lr_priority > r2->lr_priority)
971 if (r1_hops < r2_hops)
974 if (r1_hops > r2_hops)
977 if (p1->lp_txqnob < p2->lp_txqnob)
980 if (p1->lp_txqnob > p2->lp_txqnob)
983 if (p1->lp_txcredits > p2->lp_txcredits)
986 if (p1->lp_txcredits < p2->lp_txcredits)
989 if (r1->lr_seq - r2->lr_seq <= 0)
996 lnet_find_route_locked(lnet_ni_t *ni, lnet_nid_t target, lnet_nid_t rtr_nid)
998 lnet_remotenet_t *rnet;
1000 lnet_route_t *best_route;
1001 lnet_route_t *last_route;
1002 struct lnet_peer *lp_best;
1003 struct lnet_peer *lp;
1007 * If @rtr_nid is not LNET_NID_ANY, return the gateway with
1008 * rtr_nid nid, otherwise find the best gateway I can use
1010 rnet = lnet_find_net_locked(LNET_NIDNET(target));
1017 list_for_each_entry(route, &rnet->lrn_routes, lr_list) {
1018 lp = route->lr_gateway;
1020 if (!lnet_is_route_alive(route))
1023 if (ni && lp->lp_ni != ni)
1026 if (lp->lp_nid == rtr_nid) /* it's pre-determined router */
1036 /* no protection on below fields, but it's harmless */
1037 if (last_route->lr_seq - route->lr_seq < 0)
1040 rc = lnet_compare_routes(route, best_route);
1049 * set sequence number on the best router to the latest sequence + 1
1050 * so we can round-robin all routers, it's race and inaccurate but
1051 * harmless and functional
1054 best_route->lr_seq = last_route->lr_seq + 1;
1059 lnet_send(lnet_nid_t src_nid, lnet_msg_t *msg, lnet_nid_t rtr_nid)
1061 lnet_nid_t dst_nid = msg->msg_target.nid;
1062 struct lnet_ni *src_ni;
1063 struct lnet_ni *local_ni;
1064 struct lnet_peer *lp;
1070 * NB: rtr_nid is set to LNET_NID_ANY for all current use-cases,
1071 * but we might want to use pre-determined router for ACK/REPLY
1074 /* NB: ni == interface pre-determined (ACK/REPLY) */
1075 LASSERT(!msg->msg_txpeer);
1076 LASSERT(!msg->msg_sending);
1077 LASSERT(!msg->msg_target_is_router);
1078 LASSERT(!msg->msg_receiving);
1080 msg->msg_sending = 1;
1082 LASSERT(!msg->msg_tx_committed);
1083 cpt = lnet_cpt_of_nid(rtr_nid == LNET_NID_ANY ? dst_nid : rtr_nid);
1087 if (the_lnet.ln_shutdown) {
1088 lnet_net_unlock(cpt);
1092 if (src_nid == LNET_NID_ANY) {
1095 src_ni = lnet_nid2ni_locked(src_nid, cpt);
1097 lnet_net_unlock(cpt);
1098 LCONSOLE_WARN("Can't send to %s: src %s is not a local nid\n",
1099 libcfs_nid2str(dst_nid),
1100 libcfs_nid2str(src_nid));
1103 LASSERT(!msg->msg_routing);
1106 /* Is this for someone on a local network? */
1107 local_ni = lnet_net2ni_locked(LNET_NIDNET(dst_nid), cpt);
1112 src_nid = src_ni->ni_nid;
1113 } else if (src_ni == local_ni) {
1114 lnet_ni_decref_locked(local_ni, cpt);
1116 lnet_ni_decref_locked(local_ni, cpt);
1117 lnet_ni_decref_locked(src_ni, cpt);
1118 lnet_net_unlock(cpt);
1119 LCONSOLE_WARN("No route to %s via from %s\n",
1120 libcfs_nid2str(dst_nid),
1121 libcfs_nid2str(src_nid));
1125 LASSERT(src_nid != LNET_NID_ANY);
1126 lnet_msg_commit(msg, cpt);
1128 if (!msg->msg_routing)
1129 msg->msg_hdr.src_nid = cpu_to_le64(src_nid);
1131 if (src_ni == the_lnet.ln_loni) {
1132 /* No send credit hassles with LOLND */
1133 lnet_net_unlock(cpt);
1134 lnet_ni_send(src_ni, msg);
1137 lnet_ni_decref_locked(src_ni, cpt);
1138 lnet_net_unlock(cpt);
1142 rc = lnet_nid2peer_locked(&lp, dst_nid, cpt);
1143 /* lp has ref on src_ni; lose mine */
1144 lnet_ni_decref_locked(src_ni, cpt);
1146 lnet_net_unlock(cpt);
1147 LCONSOLE_WARN("Error %d finding peer %s\n", rc,
1148 libcfs_nid2str(dst_nid));
1149 /* ENOMEM or shutting down */
1152 LASSERT(lp->lp_ni == src_ni);
1154 /* sending to a remote network */
1155 lp = lnet_find_route_locked(src_ni, dst_nid, rtr_nid);
1158 lnet_ni_decref_locked(src_ni, cpt);
1159 lnet_net_unlock(cpt);
1161 LCONSOLE_WARN("No route to %s via %s (all routers down)\n",
1162 libcfs_id2str(msg->msg_target),
1163 libcfs_nid2str(src_nid));
1164 return -EHOSTUNREACH;
1168 * rtr_nid is LNET_NID_ANY or NID of pre-determined router,
1169 * it's possible that rtr_nid isn't LNET_NID_ANY and lp isn't
1170 * pre-determined router, this can happen if router table
1171 * was changed when we release the lock
1173 if (rtr_nid != lp->lp_nid) {
1174 cpt2 = lnet_cpt_of_nid_locked(lp->lp_nid);
1177 lnet_ni_decref_locked(src_ni, cpt);
1178 lnet_net_unlock(cpt);
1180 rtr_nid = lp->lp_nid;
1186 CDEBUG(D_NET, "Best route to %s via %s for %s %d\n",
1187 libcfs_nid2str(dst_nid), libcfs_nid2str(lp->lp_nid),
1188 lnet_msgtyp2str(msg->msg_type), msg->msg_len);
1192 src_nid = src_ni->ni_nid;
1194 LASSERT(src_ni == lp->lp_ni);
1195 lnet_ni_decref_locked(src_ni, cpt);
1198 lnet_peer_addref_locked(lp);
1200 LASSERT(src_nid != LNET_NID_ANY);
1201 lnet_msg_commit(msg, cpt);
1203 if (!msg->msg_routing) {
1204 /* I'm the source and now I know which NI to send on */
1205 msg->msg_hdr.src_nid = cpu_to_le64(src_nid);
1208 msg->msg_target_is_router = 1;
1209 msg->msg_target.nid = lp->lp_nid;
1210 msg->msg_target.pid = LNET_PID_LUSTRE;
1213 /* 'lp' is our best choice of peer */
1215 LASSERT(!msg->msg_peertxcredit);
1216 LASSERT(!msg->msg_txcredit);
1217 LASSERT(!msg->msg_txpeer);
1219 msg->msg_txpeer = lp; /* msg takes my ref on lp */
1221 rc = lnet_post_send_locked(msg, 0);
1222 lnet_net_unlock(cpt);
1227 if (rc == LNET_CREDIT_OK)
1228 lnet_ni_send(src_ni, msg);
1230 return 0; /* rc == LNET_CREDIT_OK or LNET_CREDIT_WAIT */
1234 lnet_drop_message(lnet_ni_t *ni, int cpt, void *private, unsigned int nob)
1237 the_lnet.ln_counters[cpt]->drop_count++;
1238 the_lnet.ln_counters[cpt]->drop_length += nob;
1239 lnet_net_unlock(cpt);
1241 lnet_ni_recv(ni, private, NULL, 0, 0, 0, nob);
1245 lnet_recv_put(lnet_ni_t *ni, lnet_msg_t *msg)
1247 lnet_hdr_t *hdr = &msg->msg_hdr;
1249 if (msg->msg_wanted)
1250 lnet_setpayloadbuffer(msg);
1252 lnet_build_msg_event(msg, LNET_EVENT_PUT);
1255 * Must I ACK? If so I'll grab the ack_wmd out of the header and put
1256 * it back into the ACK during lnet_finalize()
1258 msg->msg_ack = !lnet_is_wire_handle_none(&hdr->msg.put.ack_wmd) &&
1259 !(msg->msg_md->md_options & LNET_MD_ACK_DISABLE);
1261 lnet_ni_recv(ni, msg->msg_private, msg, msg->msg_rx_delayed,
1262 msg->msg_offset, msg->msg_wanted, hdr->payload_length);
1266 lnet_parse_put(lnet_ni_t *ni, lnet_msg_t *msg)
1268 lnet_hdr_t *hdr = &msg->msg_hdr;
1269 struct lnet_match_info info;
1273 /* Convert put fields to host byte order */
1274 hdr->msg.put.match_bits = le64_to_cpu(hdr->msg.put.match_bits);
1275 hdr->msg.put.ptl_index = le32_to_cpu(hdr->msg.put.ptl_index);
1276 hdr->msg.put.offset = le32_to_cpu(hdr->msg.put.offset);
1278 info.mi_id.nid = hdr->src_nid;
1279 info.mi_id.pid = hdr->src_pid;
1280 info.mi_opc = LNET_MD_OP_PUT;
1281 info.mi_portal = hdr->msg.put.ptl_index;
1282 info.mi_rlength = hdr->payload_length;
1283 info.mi_roffset = hdr->msg.put.offset;
1284 info.mi_mbits = hdr->msg.put.match_bits;
1286 msg->msg_rx_ready_delay = !ni->ni_lnd->lnd_eager_recv;
1287 ready_delay = msg->msg_rx_ready_delay;
1290 rc = lnet_ptl_match_md(&info, msg);
1295 case LNET_MATCHMD_OK:
1296 lnet_recv_put(ni, msg);
1299 case LNET_MATCHMD_NONE:
1301 * no eager_recv or has already called it, should
1302 * have been attached on delayed list
1307 rc = lnet_ni_eager_recv(ni, msg);
1314 case LNET_MATCHMD_DROP:
1315 CNETERR("Dropping PUT from %s portal %d match %llu offset %d length %d: %d\n",
1316 libcfs_id2str(info.mi_id), info.mi_portal,
1317 info.mi_mbits, info.mi_roffset, info.mi_rlength, rc);
1319 return -ENOENT; /* -ve: OK but no match */
1324 lnet_parse_get(lnet_ni_t *ni, lnet_msg_t *msg, int rdma_get)
1326 struct lnet_match_info info;
1327 lnet_hdr_t *hdr = &msg->msg_hdr;
1328 lnet_handle_wire_t reply_wmd;
1331 /* Convert get fields to host byte order */
1332 hdr->msg.get.match_bits = le64_to_cpu(hdr->msg.get.match_bits);
1333 hdr->msg.get.ptl_index = le32_to_cpu(hdr->msg.get.ptl_index);
1334 hdr->msg.get.sink_length = le32_to_cpu(hdr->msg.get.sink_length);
1335 hdr->msg.get.src_offset = le32_to_cpu(hdr->msg.get.src_offset);
1337 info.mi_id.nid = hdr->src_nid;
1338 info.mi_id.pid = hdr->src_pid;
1339 info.mi_opc = LNET_MD_OP_GET;
1340 info.mi_portal = hdr->msg.get.ptl_index;
1341 info.mi_rlength = hdr->msg.get.sink_length;
1342 info.mi_roffset = hdr->msg.get.src_offset;
1343 info.mi_mbits = hdr->msg.get.match_bits;
1345 rc = lnet_ptl_match_md(&info, msg);
1346 if (rc == LNET_MATCHMD_DROP) {
1347 CNETERR("Dropping GET from %s portal %d match %llu offset %d length %d\n",
1348 libcfs_id2str(info.mi_id), info.mi_portal,
1349 info.mi_mbits, info.mi_roffset, info.mi_rlength);
1350 return -ENOENT; /* -ve: OK but no match */
1353 LASSERT(rc == LNET_MATCHMD_OK);
1355 lnet_build_msg_event(msg, LNET_EVENT_GET);
1357 reply_wmd = hdr->msg.get.return_wmd;
1359 lnet_prep_send(msg, LNET_MSG_REPLY, info.mi_id,
1360 msg->msg_offset, msg->msg_wanted);
1362 msg->msg_hdr.msg.reply.dst_wmd = reply_wmd;
1365 /* The LND completes the REPLY from her recv procedure */
1366 lnet_ni_recv(ni, msg->msg_private, msg, 0,
1367 msg->msg_offset, msg->msg_len, msg->msg_len);
1371 lnet_ni_recv(ni, msg->msg_private, NULL, 0, 0, 0, 0);
1372 msg->msg_receiving = 0;
1374 rc = lnet_send(ni->ni_nid, msg, LNET_NID_ANY);
1376 /* didn't get as far as lnet_ni_send() */
1377 CERROR("%s: Unable to send REPLY for GET from %s: %d\n",
1378 libcfs_nid2str(ni->ni_nid),
1379 libcfs_id2str(info.mi_id), rc);
1381 lnet_finalize(ni, msg, rc);
1388 lnet_parse_reply(lnet_ni_t *ni, lnet_msg_t *msg)
1390 void *private = msg->msg_private;
1391 lnet_hdr_t *hdr = &msg->msg_hdr;
1392 lnet_process_id_t src = {0};
1398 cpt = lnet_cpt_of_cookie(hdr->msg.reply.dst_wmd.wh_object_cookie);
1401 src.nid = hdr->src_nid;
1402 src.pid = hdr->src_pid;
1404 /* NB handles only looked up by creator (no flips) */
1405 md = lnet_wire_handle2md(&hdr->msg.reply.dst_wmd);
1406 if (!md || !md->md_threshold || md->md_me) {
1407 CNETERR("%s: Dropping REPLY from %s for %s MD %#llx.%#llx\n",
1408 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1409 !md ? "invalid" : "inactive",
1410 hdr->msg.reply.dst_wmd.wh_interface_cookie,
1411 hdr->msg.reply.dst_wmd.wh_object_cookie);
1412 if (md && md->md_me)
1413 CERROR("REPLY MD also attached to portal %d\n",
1414 md->md_me->me_portal);
1416 lnet_res_unlock(cpt);
1417 return -ENOENT; /* -ve: OK but no match */
1420 LASSERT(!md->md_offset);
1422 rlength = hdr->payload_length;
1423 mlength = min_t(uint, rlength, md->md_length);
1425 if (mlength < rlength &&
1426 !(md->md_options & LNET_MD_TRUNCATE)) {
1427 CNETERR("%s: Dropping REPLY from %s length %d for MD %#llx would overflow (%d)\n",
1428 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1429 rlength, hdr->msg.reply.dst_wmd.wh_object_cookie,
1431 lnet_res_unlock(cpt);
1432 return -ENOENT; /* -ve: OK but no match */
1435 CDEBUG(D_NET, "%s: Reply from %s of length %d/%d into md %#llx\n",
1436 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1437 mlength, rlength, hdr->msg.reply.dst_wmd.wh_object_cookie);
1439 lnet_msg_attach_md(msg, md, 0, mlength);
1442 lnet_setpayloadbuffer(msg);
1444 lnet_res_unlock(cpt);
1446 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
1448 lnet_ni_recv(ni, private, msg, 0, 0, mlength, rlength);
1453 lnet_parse_ack(lnet_ni_t *ni, lnet_msg_t *msg)
1455 lnet_hdr_t *hdr = &msg->msg_hdr;
1456 lnet_process_id_t src = {0};
1460 src.nid = hdr->src_nid;
1461 src.pid = hdr->src_pid;
1463 /* Convert ack fields to host byte order */
1464 hdr->msg.ack.match_bits = le64_to_cpu(hdr->msg.ack.match_bits);
1465 hdr->msg.ack.mlength = le32_to_cpu(hdr->msg.ack.mlength);
1467 cpt = lnet_cpt_of_cookie(hdr->msg.ack.dst_wmd.wh_object_cookie);
1470 /* NB handles only looked up by creator (no flips) */
1471 md = lnet_wire_handle2md(&hdr->msg.ack.dst_wmd);
1472 if (!md || !md->md_threshold || md->md_me) {
1473 /* Don't moan; this is expected */
1475 "%s: Dropping ACK from %s to %s MD %#llx.%#llx\n",
1476 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1477 !md ? "invalid" : "inactive",
1478 hdr->msg.ack.dst_wmd.wh_interface_cookie,
1479 hdr->msg.ack.dst_wmd.wh_object_cookie);
1480 if (md && md->md_me)
1481 CERROR("Source MD also attached to portal %d\n",
1482 md->md_me->me_portal);
1484 lnet_res_unlock(cpt);
1485 return -ENOENT; /* -ve! */
1488 CDEBUG(D_NET, "%s: ACK from %s into md %#llx\n",
1489 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1490 hdr->msg.ack.dst_wmd.wh_object_cookie);
1492 lnet_msg_attach_md(msg, md, 0, 0);
1494 lnet_res_unlock(cpt);
1496 lnet_build_msg_event(msg, LNET_EVENT_ACK);
1498 lnet_ni_recv(ni, msg->msg_private, msg, 0, 0, 0, msg->msg_len);
1503 * \retval LNET_CREDIT_OK If \a msg is forwarded
1504 * \retval LNET_CREDIT_WAIT If \a msg is blocked because w/o buffer
1505 * \retval -ve error code
1508 lnet_parse_forward_locked(lnet_ni_t *ni, lnet_msg_t *msg)
1512 if (!the_lnet.ln_routing)
1515 if (msg->msg_rxpeer->lp_rtrcredits <= 0 ||
1516 lnet_msg2bufpool(msg)->rbp_credits <= 0) {
1517 if (!ni->ni_lnd->lnd_eager_recv) {
1518 msg->msg_rx_ready_delay = 1;
1520 lnet_net_unlock(msg->msg_rx_cpt);
1521 rc = lnet_ni_eager_recv(ni, msg);
1522 lnet_net_lock(msg->msg_rx_cpt);
1527 rc = lnet_post_routed_recv_locked(msg, 0);
1532 lnet_parse_local(lnet_ni_t *ni, lnet_msg_t *msg)
1536 switch (msg->msg_type) {
1538 rc = lnet_parse_ack(ni, msg);
1541 rc = lnet_parse_put(ni, msg);
1544 rc = lnet_parse_get(ni, msg, msg->msg_rdma_get);
1546 case LNET_MSG_REPLY:
1547 rc = lnet_parse_reply(ni, msg);
1549 default: /* prevent an unused label if !kernel */
1554 LASSERT(!rc || rc == -ENOENT);
1559 lnet_msgtyp2str(int type)
1568 case LNET_MSG_REPLY:
1570 case LNET_MSG_HELLO:
1578 lnet_print_hdr(lnet_hdr_t *hdr)
1580 lnet_process_id_t src = {0};
1581 lnet_process_id_t dst = {0};
1582 char *type_str = lnet_msgtyp2str(hdr->type);
1584 src.nid = hdr->src_nid;
1585 src.pid = hdr->src_pid;
1587 dst.nid = hdr->dest_nid;
1588 dst.pid = hdr->dest_pid;
1590 CWARN("P3 Header at %p of type %s\n", hdr, type_str);
1591 CWARN(" From %s\n", libcfs_id2str(src));
1592 CWARN(" To %s\n", libcfs_id2str(dst));
1594 switch (hdr->type) {
1599 CWARN(" Ptl index %d, ack md %#llx.%#llx, match bits %llu\n",
1600 hdr->msg.put.ptl_index,
1601 hdr->msg.put.ack_wmd.wh_interface_cookie,
1602 hdr->msg.put.ack_wmd.wh_object_cookie,
1603 hdr->msg.put.match_bits);
1604 CWARN(" Length %d, offset %d, hdr data %#llx\n",
1605 hdr->payload_length, hdr->msg.put.offset,
1606 hdr->msg.put.hdr_data);
1610 CWARN(" Ptl index %d, return md %#llx.%#llx, match bits %llu\n",
1611 hdr->msg.get.ptl_index,
1612 hdr->msg.get.return_wmd.wh_interface_cookie,
1613 hdr->msg.get.return_wmd.wh_object_cookie,
1614 hdr->msg.get.match_bits);
1615 CWARN(" Length %d, src offset %d\n",
1616 hdr->msg.get.sink_length,
1617 hdr->msg.get.src_offset);
1621 CWARN(" dst md %#llx.%#llx, manipulated length %d\n",
1622 hdr->msg.ack.dst_wmd.wh_interface_cookie,
1623 hdr->msg.ack.dst_wmd.wh_object_cookie,
1624 hdr->msg.ack.mlength);
1627 case LNET_MSG_REPLY:
1628 CWARN(" dst md %#llx.%#llx, length %d\n",
1629 hdr->msg.reply.dst_wmd.wh_interface_cookie,
1630 hdr->msg.reply.dst_wmd.wh_object_cookie,
1631 hdr->payload_length);
1636 lnet_parse(lnet_ni_t *ni, lnet_hdr_t *hdr, lnet_nid_t from_nid,
1637 void *private, int rdma_req)
1642 struct lnet_msg *msg;
1643 lnet_pid_t dest_pid;
1644 lnet_nid_t dest_nid;
1646 __u32 payload_length;
1649 LASSERT(!in_interrupt());
1651 type = le32_to_cpu(hdr->type);
1652 src_nid = le64_to_cpu(hdr->src_nid);
1653 dest_nid = le64_to_cpu(hdr->dest_nid);
1654 dest_pid = le32_to_cpu(hdr->dest_pid);
1655 payload_length = le32_to_cpu(hdr->payload_length);
1657 for_me = (ni->ni_nid == dest_nid);
1658 cpt = lnet_cpt_of_nid(from_nid);
1663 if (payload_length > 0) {
1664 CERROR("%s, src %s: bad %s payload %d (0 expected)\n",
1665 libcfs_nid2str(from_nid),
1666 libcfs_nid2str(src_nid),
1667 lnet_msgtyp2str(type), payload_length);
1673 case LNET_MSG_REPLY:
1674 if (payload_length >
1675 (__u32)(for_me ? LNET_MAX_PAYLOAD : LNET_MTU)) {
1676 CERROR("%s, src %s: bad %s payload %d (%d max expected)\n",
1677 libcfs_nid2str(from_nid),
1678 libcfs_nid2str(src_nid),
1679 lnet_msgtyp2str(type),
1681 for_me ? LNET_MAX_PAYLOAD : LNET_MTU);
1687 CERROR("%s, src %s: Bad message type 0x%x\n",
1688 libcfs_nid2str(from_nid),
1689 libcfs_nid2str(src_nid), type);
1693 if (the_lnet.ln_routing &&
1694 ni->ni_last_alive != ktime_get_real_seconds()) {
1695 /* NB: so far here is the only place to set NI status to "up */
1697 ni->ni_last_alive = ktime_get_real_seconds();
1698 if (ni->ni_status &&
1699 ni->ni_status->ns_status == LNET_NI_STATUS_DOWN)
1700 ni->ni_status->ns_status = LNET_NI_STATUS_UP;
1705 * Regard a bad destination NID as a protocol error. Senders should
1706 * know what they're doing; if they don't they're misconfigured, buggy
1707 * or malicious so we chop them off at the knees :)
1710 if (LNET_NIDNET(dest_nid) == LNET_NIDNET(ni->ni_nid)) {
1711 /* should have gone direct */
1712 CERROR("%s, src %s: Bad dest nid %s (should have been sent direct)\n",
1713 libcfs_nid2str(from_nid),
1714 libcfs_nid2str(src_nid),
1715 libcfs_nid2str(dest_nid));
1719 if (lnet_islocalnid(dest_nid)) {
1721 * dest is another local NI; sender should have used
1722 * this node's NID on its own network
1724 CERROR("%s, src %s: Bad dest nid %s (it's my nid but on a different network)\n",
1725 libcfs_nid2str(from_nid),
1726 libcfs_nid2str(src_nid),
1727 libcfs_nid2str(dest_nid));
1731 if (rdma_req && type == LNET_MSG_GET) {
1732 CERROR("%s, src %s: Bad optimized GET for %s (final destination must be me)\n",
1733 libcfs_nid2str(from_nid),
1734 libcfs_nid2str(src_nid),
1735 libcfs_nid2str(dest_nid));
1739 if (!the_lnet.ln_routing) {
1740 CERROR("%s, src %s: Dropping message for %s (routing not enabled)\n",
1741 libcfs_nid2str(from_nid),
1742 libcfs_nid2str(src_nid),
1743 libcfs_nid2str(dest_nid));
1749 * Message looks OK; we're not going to return an error, so we MUST
1750 * call back lnd_recv() come what may...
1752 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
1753 fail_peer(src_nid, 0)) { /* shall we now? */
1754 CERROR("%s, src %s: Dropping %s to simulate failure\n",
1755 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
1756 lnet_msgtyp2str(type));
1760 if (!list_empty(&the_lnet.ln_drop_rules) &&
1761 lnet_drop_rule_match(hdr)) {
1762 CDEBUG(D_NET, "%s, src %s, dst %s: Dropping %s to simulate silent message loss\n",
1763 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
1764 libcfs_nid2str(dest_nid), lnet_msgtyp2str(type));
1768 msg = lnet_msg_alloc();
1770 CERROR("%s, src %s: Dropping %s (out of memory)\n",
1771 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
1772 lnet_msgtyp2str(type));
1776 /* msg zeroed in lnet_msg_alloc;
1777 * i.e. flags all clear, pointers NULL etc
1779 msg->msg_type = type;
1780 msg->msg_private = private;
1781 msg->msg_receiving = 1;
1782 msg->msg_rdma_get = rdma_req;
1783 msg->msg_wanted = payload_length;
1784 msg->msg_len = payload_length;
1785 msg->msg_offset = 0;
1786 msg->msg_hdr = *hdr;
1787 /* for building message event */
1788 msg->msg_from = from_nid;
1790 msg->msg_target.pid = dest_pid;
1791 msg->msg_target.nid = dest_nid;
1792 msg->msg_routing = 1;
1795 /* convert common msg->hdr fields to host byteorder */
1796 msg->msg_hdr.type = type;
1797 msg->msg_hdr.src_nid = src_nid;
1798 msg->msg_hdr.src_pid = le32_to_cpu(msg->msg_hdr.src_pid);
1799 msg->msg_hdr.dest_nid = dest_nid;
1800 msg->msg_hdr.dest_pid = dest_pid;
1801 msg->msg_hdr.payload_length = payload_length;
1805 rc = lnet_nid2peer_locked(&msg->msg_rxpeer, from_nid, cpt);
1807 lnet_net_unlock(cpt);
1808 CERROR("%s, src %s: Dropping %s (error %d looking up sender)\n",
1809 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
1810 lnet_msgtyp2str(type), rc);
1812 if (rc == -ESHUTDOWN)
1813 /* We are shutting down. Don't do anything more */
1818 if (lnet_isrouter(msg->msg_rxpeer)) {
1819 lnet_peer_set_alive(msg->msg_rxpeer);
1820 if (avoid_asym_router_failure &&
1821 LNET_NIDNET(src_nid) != LNET_NIDNET(from_nid)) {
1822 /* received a remote message from router, update
1823 * remote NI status on this router.
1824 * NB: multi-hop routed message will be ignored.
1826 lnet_router_ni_update_locked(msg->msg_rxpeer,
1827 LNET_NIDNET(src_nid));
1831 lnet_msg_commit(msg, cpt);
1833 /* message delay simulation */
1834 if (unlikely(!list_empty(&the_lnet.ln_delay_rules) &&
1835 lnet_delay_rule_match_locked(hdr, msg))) {
1836 lnet_net_unlock(cpt);
1841 rc = lnet_parse_forward_locked(ni, msg);
1842 lnet_net_unlock(cpt);
1847 if (rc == LNET_CREDIT_OK) {
1848 lnet_ni_recv(ni, msg->msg_private, msg, 0,
1849 0, payload_length, payload_length);
1854 lnet_net_unlock(cpt);
1856 rc = lnet_parse_local(ni, msg);
1862 LASSERT(!msg->msg_md);
1863 lnet_finalize(ni, msg, rc);
1866 lnet_drop_message(ni, cpt, private, payload_length);
1869 EXPORT_SYMBOL(lnet_parse);
1872 lnet_drop_delayed_msg_list(struct list_head *head, char *reason)
1874 while (!list_empty(head)) {
1875 lnet_process_id_t id = {0};
1878 msg = list_entry(head->next, lnet_msg_t, msg_list);
1879 list_del(&msg->msg_list);
1881 id.nid = msg->msg_hdr.src_nid;
1882 id.pid = msg->msg_hdr.src_pid;
1884 LASSERT(!msg->msg_md);
1885 LASSERT(msg->msg_rx_delayed);
1886 LASSERT(msg->msg_rxpeer);
1887 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
1889 CWARN("Dropping delayed PUT from %s portal %d match %llu offset %d length %d: %s\n",
1891 msg->msg_hdr.msg.put.ptl_index,
1892 msg->msg_hdr.msg.put.match_bits,
1893 msg->msg_hdr.msg.put.offset,
1894 msg->msg_hdr.payload_length, reason);
1897 * NB I can't drop msg's ref on msg_rxpeer until after I've
1898 * called lnet_drop_message(), so I just hang onto msg as well
1901 lnet_drop_message(msg->msg_rxpeer->lp_ni,
1902 msg->msg_rxpeer->lp_cpt,
1903 msg->msg_private, msg->msg_len);
1905 * NB: message will not generate event because w/o attached MD,
1906 * but we still should give error code so lnet_msg_decommit()
1907 * can skip counters operations and other checks.
1909 lnet_finalize(msg->msg_rxpeer->lp_ni, msg, -ENOENT);
1914 lnet_recv_delayed_msg_list(struct list_head *head)
1916 while (!list_empty(head)) {
1918 lnet_process_id_t id;
1920 msg = list_entry(head->next, lnet_msg_t, msg_list);
1921 list_del(&msg->msg_list);
1924 * md won't disappear under me, since each msg
1927 id.nid = msg->msg_hdr.src_nid;
1928 id.pid = msg->msg_hdr.src_pid;
1930 LASSERT(msg->msg_rx_delayed);
1931 LASSERT(msg->msg_md);
1932 LASSERT(msg->msg_rxpeer);
1933 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
1935 CDEBUG(D_NET, "Resuming delayed PUT from %s portal %d match %llu offset %d length %d.\n",
1936 libcfs_id2str(id), msg->msg_hdr.msg.put.ptl_index,
1937 msg->msg_hdr.msg.put.match_bits,
1938 msg->msg_hdr.msg.put.offset,
1939 msg->msg_hdr.payload_length);
1941 lnet_recv_put(msg->msg_rxpeer->lp_ni, msg);
1946 * Initiate an asynchronous PUT operation.
1948 * There are several events associated with a PUT: completion of the send on
1949 * the initiator node (LNET_EVENT_SEND), and when the send completes
1950 * successfully, the receipt of an acknowledgment (LNET_EVENT_ACK) indicating
1951 * that the operation was accepted by the target. The event LNET_EVENT_PUT is
1952 * used at the target node to indicate the completion of incoming data
1955 * The local events will be logged in the EQ associated with the MD pointed to
1956 * by \a mdh handle. Using a MD without an associated EQ results in these
1957 * events being discarded. In this case, the caller must have another
1958 * mechanism (e.g., a higher level protocol) for determining when it is safe
1959 * to modify the memory region associated with the MD.
1961 * Note that LNet does not guarantee the order of LNET_EVENT_SEND and
1962 * LNET_EVENT_ACK, though intuitively ACK should happen after SEND.
1964 * \param self Indicates the NID of a local interface through which to send
1965 * the PUT request. Use LNET_NID_ANY to let LNet choose one by itself.
1966 * \param mdh A handle for the MD that describes the memory to be sent. The MD
1967 * must be "free floating" (See LNetMDBind()).
1968 * \param ack Controls whether an acknowledgment is requested.
1969 * Acknowledgments are only sent when they are requested by the initiating
1970 * process and the target MD enables them.
1971 * \param target A process identifier for the target process.
1972 * \param portal The index in the \a target's portal table.
1973 * \param match_bits The match bits to use for MD selection at the target
1975 * \param offset The offset into the target MD (only used when the target
1976 * MD has the LNET_MD_MANAGE_REMOTE option set).
1977 * \param hdr_data 64 bits of user data that can be included in the message
1978 * header. This data is written to an event queue entry at the target if an
1979 * EQ is present on the matching MD.
1981 * \retval 0 Success, and only in this case events will be generated
1982 * and logged to EQ (if it exists).
1983 * \retval -EIO Simulated failure.
1984 * \retval -ENOMEM Memory allocation failure.
1985 * \retval -ENOENT Invalid MD object.
1987 * \see lnet_event_t::hdr_data and lnet_event_kind_t.
1990 LNetPut(lnet_nid_t self, lnet_handle_md_t mdh, lnet_ack_req_t ack,
1991 lnet_process_id_t target, unsigned int portal,
1992 __u64 match_bits, unsigned int offset,
1995 struct lnet_msg *msg;
1996 struct lnet_libmd *md;
2000 LASSERT(the_lnet.ln_refcount > 0);
2002 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
2003 fail_peer(target.nid, 1)) { /* shall we now? */
2004 CERROR("Dropping PUT to %s: simulated failure\n",
2005 libcfs_id2str(target));
2009 msg = lnet_msg_alloc();
2011 CERROR("Dropping PUT to %s: ENOMEM on lnet_msg_t\n",
2012 libcfs_id2str(target));
2015 msg->msg_vmflush = !!memory_pressure_get();
2017 cpt = lnet_cpt_of_cookie(mdh.cookie);
2020 md = lnet_handle2md(&mdh);
2021 if (!md || !md->md_threshold || md->md_me) {
2022 CERROR("Dropping PUT (%llu:%d:%s): MD (%d) invalid\n",
2023 match_bits, portal, libcfs_id2str(target),
2024 !md ? -1 : md->md_threshold);
2025 if (md && md->md_me)
2026 CERROR("Source MD also attached to portal %d\n",
2027 md->md_me->me_portal);
2028 lnet_res_unlock(cpt);
2034 CDEBUG(D_NET, "LNetPut -> %s\n", libcfs_id2str(target));
2036 lnet_msg_attach_md(msg, md, 0, 0);
2038 lnet_prep_send(msg, LNET_MSG_PUT, target, 0, md->md_length);
2040 msg->msg_hdr.msg.put.match_bits = cpu_to_le64(match_bits);
2041 msg->msg_hdr.msg.put.ptl_index = cpu_to_le32(portal);
2042 msg->msg_hdr.msg.put.offset = cpu_to_le32(offset);
2043 msg->msg_hdr.msg.put.hdr_data = hdr_data;
2045 /* NB handles only looked up by creator (no flips) */
2046 if (ack == LNET_ACK_REQ) {
2047 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
2048 the_lnet.ln_interface_cookie;
2049 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
2050 md->md_lh.lh_cookie;
2052 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
2053 LNET_WIRE_HANDLE_COOKIE_NONE;
2054 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
2055 LNET_WIRE_HANDLE_COOKIE_NONE;
2058 lnet_res_unlock(cpt);
2060 lnet_build_msg_event(msg, LNET_EVENT_SEND);
2062 rc = lnet_send(self, msg, LNET_NID_ANY);
2064 CNETERR("Error sending PUT to %s: %d\n",
2065 libcfs_id2str(target), rc);
2066 lnet_finalize(NULL, msg, rc);
2069 /* completion will be signalled by an event */
2072 EXPORT_SYMBOL(LNetPut);
2075 lnet_create_reply_msg(lnet_ni_t *ni, lnet_msg_t *getmsg)
2078 * The LND can DMA direct to the GET md (i.e. no REPLY msg). This
2079 * returns a msg for the LND to pass to lnet_finalize() when the sink
2080 * data has been received.
2082 * CAVEAT EMPTOR: 'getmsg' is the original GET, which is freed when
2083 * lnet_finalize() is called on it, so the LND must call this first
2085 struct lnet_msg *msg = lnet_msg_alloc();
2086 struct lnet_libmd *getmd = getmsg->msg_md;
2087 lnet_process_id_t peer_id = getmsg->msg_target;
2090 LASSERT(!getmsg->msg_target_is_router);
2091 LASSERT(!getmsg->msg_routing);
2094 CERROR("%s: Dropping REPLY from %s: can't allocate msg\n",
2095 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id));
2099 cpt = lnet_cpt_of_cookie(getmd->md_lh.lh_cookie);
2102 LASSERT(getmd->md_refcount > 0);
2104 if (!getmd->md_threshold) {
2105 CERROR("%s: Dropping REPLY from %s for inactive MD %p\n",
2106 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id),
2108 lnet_res_unlock(cpt);
2112 LASSERT(!getmd->md_offset);
2114 CDEBUG(D_NET, "%s: Reply from %s md %p\n",
2115 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id), getmd);
2117 /* setup information for lnet_build_msg_event */
2118 msg->msg_from = peer_id.nid;
2119 msg->msg_type = LNET_MSG_GET; /* flag this msg as an "optimized" GET */
2120 msg->msg_hdr.src_nid = peer_id.nid;
2121 msg->msg_hdr.payload_length = getmd->md_length;
2122 msg->msg_receiving = 1; /* required by lnet_msg_attach_md */
2124 lnet_msg_attach_md(msg, getmd, getmd->md_offset, getmd->md_length);
2125 lnet_res_unlock(cpt);
2127 cpt = lnet_cpt_of_nid(peer_id.nid);
2130 lnet_msg_commit(msg, cpt);
2131 lnet_net_unlock(cpt);
2133 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
2138 cpt = lnet_cpt_of_nid(peer_id.nid);
2141 the_lnet.ln_counters[cpt]->drop_count++;
2142 the_lnet.ln_counters[cpt]->drop_length += getmd->md_length;
2143 lnet_net_unlock(cpt);
2150 EXPORT_SYMBOL(lnet_create_reply_msg);
2153 lnet_set_reply_msg_len(lnet_ni_t *ni, lnet_msg_t *reply, unsigned int len)
2156 * Set the REPLY length, now the RDMA that elides the REPLY message has
2157 * completed and I know it.
2160 LASSERT(reply->msg_type == LNET_MSG_GET);
2161 LASSERT(reply->msg_ev.type == LNET_EVENT_REPLY);
2164 * NB I trusted my peer to RDMA. If she tells me she's written beyond
2165 * the end of my buffer, I might as well be dead.
2167 LASSERT(len <= reply->msg_ev.mlength);
2169 reply->msg_ev.mlength = len;
2171 EXPORT_SYMBOL(lnet_set_reply_msg_len);
2174 * Initiate an asynchronous GET operation.
2176 * On the initiator node, an LNET_EVENT_SEND is logged when the GET request
2177 * is sent, and an LNET_EVENT_REPLY is logged when the data returned from
2178 * the target node in the REPLY has been written to local MD.
2180 * On the target node, an LNET_EVENT_GET is logged when the GET request
2181 * arrives and is accepted into a MD.
2183 * \param self,target,portal,match_bits,offset See the discussion in LNetPut().
2184 * \param mdh A handle for the MD that describes the memory into which the
2185 * requested data will be received. The MD must be "free floating"
2186 * (See LNetMDBind()).
2188 * \retval 0 Success, and only in this case events will be generated
2189 * and logged to EQ (if it exists) of the MD.
2190 * \retval -EIO Simulated failure.
2191 * \retval -ENOMEM Memory allocation failure.
2192 * \retval -ENOENT Invalid MD object.
2195 LNetGet(lnet_nid_t self, lnet_handle_md_t mdh,
2196 lnet_process_id_t target, unsigned int portal,
2197 __u64 match_bits, unsigned int offset)
2199 struct lnet_msg *msg;
2200 struct lnet_libmd *md;
2204 LASSERT(the_lnet.ln_refcount > 0);
2206 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
2207 fail_peer(target.nid, 1)) { /* shall we now? */
2208 CERROR("Dropping GET to %s: simulated failure\n",
2209 libcfs_id2str(target));
2213 msg = lnet_msg_alloc();
2215 CERROR("Dropping GET to %s: ENOMEM on lnet_msg_t\n",
2216 libcfs_id2str(target));
2220 cpt = lnet_cpt_of_cookie(mdh.cookie);
2223 md = lnet_handle2md(&mdh);
2224 if (!md || !md->md_threshold || md->md_me) {
2225 CERROR("Dropping GET (%llu:%d:%s): MD (%d) invalid\n",
2226 match_bits, portal, libcfs_id2str(target),
2227 !md ? -1 : md->md_threshold);
2228 if (md && md->md_me)
2229 CERROR("REPLY MD also attached to portal %d\n",
2230 md->md_me->me_portal);
2232 lnet_res_unlock(cpt);
2238 CDEBUG(D_NET, "LNetGet -> %s\n", libcfs_id2str(target));
2240 lnet_msg_attach_md(msg, md, 0, 0);
2242 lnet_prep_send(msg, LNET_MSG_GET, target, 0, 0);
2244 msg->msg_hdr.msg.get.match_bits = cpu_to_le64(match_bits);
2245 msg->msg_hdr.msg.get.ptl_index = cpu_to_le32(portal);
2246 msg->msg_hdr.msg.get.src_offset = cpu_to_le32(offset);
2247 msg->msg_hdr.msg.get.sink_length = cpu_to_le32(md->md_length);
2249 /* NB handles only looked up by creator (no flips) */
2250 msg->msg_hdr.msg.get.return_wmd.wh_interface_cookie =
2251 the_lnet.ln_interface_cookie;
2252 msg->msg_hdr.msg.get.return_wmd.wh_object_cookie =
2253 md->md_lh.lh_cookie;
2255 lnet_res_unlock(cpt);
2257 lnet_build_msg_event(msg, LNET_EVENT_SEND);
2259 rc = lnet_send(self, msg, LNET_NID_ANY);
2261 CNETERR("Error sending GET to %s: %d\n",
2262 libcfs_id2str(target), rc);
2263 lnet_finalize(NULL, msg, rc);
2266 /* completion will be signalled by an event */
2269 EXPORT_SYMBOL(LNetGet);
2272 * Calculate distance to node at \a dstnid.
2274 * \param dstnid Target NID.
2275 * \param srcnidp If not NULL, NID of the local interface to reach \a dstnid
2277 * \param orderp If not NULL, order of the route to reach \a dstnid is saved
2280 * \retval 0 If \a dstnid belongs to a local interface, and reserved option
2281 * local_nid_dist_zero is set, which is the default.
2282 * \retval positives Distance to target NID, i.e. number of hops plus one.
2283 * \retval -EHOSTUNREACH If \a dstnid is not reachable.
2286 LNetDist(lnet_nid_t dstnid, lnet_nid_t *srcnidp, __u32 *orderp)
2288 struct list_head *e;
2290 lnet_remotenet_t *rnet;
2291 __u32 dstnet = LNET_NIDNET(dstnid);
2295 struct list_head *rn_list;
2298 * if !local_nid_dist_zero, I don't return a distance of 0 ever
2299 * (when lustre sees a distance of 0, it substitutes 0@lo), so I
2300 * keep order 0 free for 0@lo and order 1 free for a local NID
2303 LASSERT(the_lnet.ln_refcount > 0);
2305 cpt = lnet_net_lock_current();
2307 list_for_each(e, &the_lnet.ln_nis) {
2308 ni = list_entry(e, lnet_ni_t, ni_list);
2310 if (ni->ni_nid == dstnid) {
2314 if (LNET_NETTYP(LNET_NIDNET(dstnid)) == LOLND)
2319 lnet_net_unlock(cpt);
2321 return local_nid_dist_zero ? 0 : 1;
2324 if (LNET_NIDNET(ni->ni_nid) == dstnet) {
2326 * Check if ni was originally created in
2327 * current net namespace.
2328 * If not, assign order above 0xffff0000,
2329 * to make this ni not a priority.
2331 if (!net_eq(ni->ni_net_ns, current->nsproxy->net_ns))
2332 order += 0xffff0000;
2335 *srcnidp = ni->ni_nid;
2338 lnet_net_unlock(cpt);
2345 rn_list = lnet_net2rnethash(dstnet);
2346 list_for_each(e, rn_list) {
2347 rnet = list_entry(e, lnet_remotenet_t, lrn_list);
2349 if (rnet->lrn_net == dstnet) {
2350 lnet_route_t *route;
2351 lnet_route_t *shortest = NULL;
2352 __u32 shortest_hops = LNET_UNDEFINED_HOPS;
2355 LASSERT(!list_empty(&rnet->lrn_routes));
2357 list_for_each_entry(route, &rnet->lrn_routes,
2359 route_hops = route->lr_hops;
2360 if (route_hops == LNET_UNDEFINED_HOPS)
2363 route_hops < shortest_hops) {
2365 shortest_hops = route_hops;
2370 hops = shortest_hops;
2372 *srcnidp = shortest->lr_gateway->lp_ni->ni_nid;
2375 lnet_net_unlock(cpt);
2381 lnet_net_unlock(cpt);
2382 return -EHOSTUNREACH;
2384 EXPORT_SYMBOL(LNetDist);