4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, 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/pagemap.h>
41 #include <lnet/lib-lnet.h>
42 #include <linux/nsproxy.h>
43 #include <net/net_namespace.h>
45 static int local_nid_dist_zero = 1;
46 module_param(local_nid_dist_zero, int, 0444);
47 MODULE_PARM_DESC(local_nid_dist_zero, "Reserved");
49 static inline struct lnet_comm_count *
50 get_stats_counts(struct lnet_element_stats *stats,
51 enum lnet_stats_type stats_type)
54 case LNET_STATS_TYPE_SEND:
55 return &stats->el_send_stats;
56 case LNET_STATS_TYPE_RECV:
57 return &stats->el_recv_stats;
58 case LNET_STATS_TYPE_DROP:
59 return &stats->el_drop_stats;
61 CERROR("Unknown stats type\n");
67 void lnet_incr_stats(struct lnet_element_stats *stats,
68 enum lnet_msg_type msg_type,
69 enum lnet_stats_type stats_type)
71 struct lnet_comm_count *counts = get_stats_counts(stats, stats_type);
77 atomic_inc(&counts->co_ack_count);
80 atomic_inc(&counts->co_put_count);
83 atomic_inc(&counts->co_get_count);
86 atomic_inc(&counts->co_reply_count);
89 atomic_inc(&counts->co_hello_count);
92 CERROR("There is a BUG in the code. Unknown message type\n");
97 __u32 lnet_sum_stats(struct lnet_element_stats *stats,
98 enum lnet_stats_type stats_type)
100 struct lnet_comm_count *counts = get_stats_counts(stats, stats_type);
104 return (atomic_read(&counts->co_ack_count) +
105 atomic_read(&counts->co_put_count) +
106 atomic_read(&counts->co_get_count) +
107 atomic_read(&counts->co_reply_count) +
108 atomic_read(&counts->co_hello_count));
111 static inline void assign_stats(struct lnet_ioctl_comm_count *msg_stats,
112 struct lnet_comm_count *counts)
114 msg_stats->ico_get_count = atomic_read(&counts->co_get_count);
115 msg_stats->ico_put_count = atomic_read(&counts->co_put_count);
116 msg_stats->ico_reply_count = atomic_read(&counts->co_reply_count);
117 msg_stats->ico_ack_count = atomic_read(&counts->co_ack_count);
118 msg_stats->ico_hello_count = atomic_read(&counts->co_hello_count);
121 void lnet_usr_translate_stats(struct lnet_ioctl_element_msg_stats *msg_stats,
122 struct lnet_element_stats *stats)
124 struct lnet_comm_count *counts;
129 counts = get_stats_counts(stats, LNET_STATS_TYPE_SEND);
132 assign_stats(&msg_stats->im_send_stats, counts);
134 counts = get_stats_counts(stats, LNET_STATS_TYPE_RECV);
137 assign_stats(&msg_stats->im_recv_stats, counts);
139 counts = get_stats_counts(stats, LNET_STATS_TYPE_DROP);
142 assign_stats(&msg_stats->im_drop_stats, counts);
146 lnet_fail_nid(lnet_nid_t nid, unsigned int threshold)
148 struct lnet_test_peer *tp;
149 struct list_head *el;
150 struct list_head *next;
151 struct list_head cull;
153 /* NB: use lnet_net_lock(0) to serialize operations on test peers */
154 if (threshold != 0) {
155 /* Adding a new entry */
156 LIBCFS_ALLOC(tp, sizeof(*tp));
161 tp->tp_threshold = threshold;
164 list_add_tail(&tp->tp_list, &the_lnet.ln_test_peers);
169 /* removing entries */
170 INIT_LIST_HEAD(&cull);
174 list_for_each_safe(el, next, &the_lnet.ln_test_peers) {
175 tp = list_entry(el, struct lnet_test_peer, tp_list);
177 if (tp->tp_threshold == 0 || /* needs culling anyway */
178 nid == LNET_NID_ANY || /* removing all entries */
179 tp->tp_nid == nid) { /* matched this one */
180 list_del(&tp->tp_list);
181 list_add(&tp->tp_list, &cull);
187 while (!list_empty(&cull)) {
188 tp = list_entry(cull.next, struct lnet_test_peer, tp_list);
190 list_del(&tp->tp_list);
191 LIBCFS_FREE(tp, sizeof(*tp));
197 fail_peer (lnet_nid_t nid, int outgoing)
199 struct lnet_test_peer *tp;
200 struct list_head *el;
201 struct list_head *next;
202 struct list_head cull;
205 INIT_LIST_HEAD(&cull);
207 /* NB: use lnet_net_lock(0) to serialize operations on test peers */
210 list_for_each_safe(el, next, &the_lnet.ln_test_peers) {
211 tp = list_entry(el, struct lnet_test_peer, tp_list);
213 if (tp->tp_threshold == 0) {
216 /* only cull zombies on outgoing tests,
217 * since we may be at interrupt priority on
218 * incoming messages. */
219 list_del(&tp->tp_list);
220 list_add(&tp->tp_list, &cull);
225 if (tp->tp_nid == LNET_NID_ANY || /* fail every peer */
226 nid == tp->tp_nid) { /* fail this peer */
229 if (tp->tp_threshold != LNET_MD_THRESH_INF) {
232 tp->tp_threshold == 0) {
234 list_del(&tp->tp_list);
235 list_add(&tp->tp_list, &cull);
244 while (!list_empty(&cull)) {
245 tp = list_entry(cull.next, struct lnet_test_peer, tp_list);
246 list_del(&tp->tp_list);
248 LIBCFS_FREE(tp, sizeof(*tp));
255 lnet_iov_nob(unsigned int niov, struct kvec *iov)
257 unsigned int nob = 0;
259 LASSERT(niov == 0 || iov != NULL);
261 nob += (iov++)->iov_len;
265 EXPORT_SYMBOL(lnet_iov_nob);
268 lnet_copy_iov2iov(unsigned int ndiov, struct kvec *diov, unsigned int doffset,
269 unsigned int nsiov, struct kvec *siov, unsigned int soffset,
272 /* NB diov, siov are READ-ONLY */
273 unsigned int this_nob;
278 /* skip complete frags before 'doffset' */
280 while (doffset >= diov->iov_len) {
281 doffset -= diov->iov_len;
287 /* skip complete frags before 'soffset' */
289 while (soffset >= siov->iov_len) {
290 soffset -= siov->iov_len;
299 this_nob = MIN(diov->iov_len - doffset,
300 siov->iov_len - soffset);
301 this_nob = MIN(this_nob, nob);
303 memcpy((char *)diov->iov_base + doffset,
304 (char *)siov->iov_base + soffset, this_nob);
307 if (diov->iov_len > doffset + this_nob) {
315 if (siov->iov_len > soffset + this_nob) {
324 EXPORT_SYMBOL(lnet_copy_iov2iov);
327 lnet_extract_iov(int dst_niov, struct kvec *dst,
328 int src_niov, struct kvec *src,
329 unsigned int offset, unsigned int len)
331 /* Initialise 'dst' to the subset of 'src' starting at 'offset',
332 * for exactly 'len' bytes, and return the number of entries.
333 * NB not destructive to 'src' */
334 unsigned int frag_len;
337 if (len == 0) /* no data => */
338 return (0); /* no frags */
340 LASSERT(src_niov > 0);
341 while (offset >= src->iov_len) { /* skip initial frags */
342 offset -= src->iov_len;
345 LASSERT(src_niov > 0);
350 LASSERT(src_niov > 0);
351 LASSERT((int)niov <= dst_niov);
353 frag_len = src->iov_len - offset;
354 dst->iov_base = ((char *)src->iov_base) + offset;
356 if (len <= frag_len) {
361 dst->iov_len = frag_len;
371 EXPORT_SYMBOL(lnet_extract_iov);
375 lnet_kiov_nob(unsigned int niov, lnet_kiov_t *kiov)
377 unsigned int nob = 0;
379 LASSERT(niov == 0 || kiov != NULL);
381 nob += (kiov++)->kiov_len;
385 EXPORT_SYMBOL(lnet_kiov_nob);
388 lnet_copy_kiov2kiov(unsigned int ndiov, lnet_kiov_t *diov, unsigned int doffset,
389 unsigned int nsiov, lnet_kiov_t *siov, unsigned int soffset,
392 /* NB diov, siov are READ-ONLY */
393 unsigned int this_nob;
400 LASSERT (!in_interrupt ());
403 while (doffset >= diov->kiov_len) {
404 doffset -= diov->kiov_len;
411 while (soffset >= siov->kiov_len) {
412 soffset -= siov->kiov_len;
421 this_nob = MIN(diov->kiov_len - doffset,
422 siov->kiov_len - soffset);
423 this_nob = MIN(this_nob, nob);
426 daddr = ((char *)kmap(diov->kiov_page)) +
427 diov->kiov_offset + doffset;
429 saddr = ((char *)kmap(siov->kiov_page)) +
430 siov->kiov_offset + soffset;
432 /* Vanishing risk of kmap deadlock when mapping 2 pages.
433 * However in practice at least one of the kiovs will be mapped
434 * kernel pages and the map/unmap will be NOOPs */
436 memcpy (daddr, saddr, this_nob);
439 if (diov->kiov_len > doffset + this_nob) {
443 kunmap(diov->kiov_page);
450 if (siov->kiov_len > soffset + this_nob) {
454 kunmap(siov->kiov_page);
463 kunmap(diov->kiov_page);
465 kunmap(siov->kiov_page);
467 EXPORT_SYMBOL(lnet_copy_kiov2kiov);
470 lnet_copy_kiov2iov (unsigned int niov, struct kvec *iov, unsigned int iovoffset,
471 unsigned int nkiov, lnet_kiov_t *kiov, unsigned int kiovoffset,
474 /* NB iov, kiov are READ-ONLY */
475 unsigned int this_nob;
481 LASSERT (!in_interrupt ());
484 while (iovoffset >= iov->iov_len) {
485 iovoffset -= iov->iov_len;
492 while (kiovoffset >= kiov->kiov_len) {
493 kiovoffset -= kiov->kiov_len;
502 this_nob = MIN(iov->iov_len - iovoffset,
503 kiov->kiov_len - kiovoffset);
504 this_nob = MIN(this_nob, nob);
507 addr = ((char *)kmap(kiov->kiov_page)) +
508 kiov->kiov_offset + kiovoffset;
510 memcpy((char *)iov->iov_base + iovoffset, addr, this_nob);
513 if (iov->iov_len > iovoffset + this_nob) {
514 iovoffset += this_nob;
521 if (kiov->kiov_len > kiovoffset + this_nob) {
523 kiovoffset += this_nob;
525 kunmap(kiov->kiov_page);
535 kunmap(kiov->kiov_page);
537 EXPORT_SYMBOL(lnet_copy_kiov2iov);
540 lnet_copy_iov2kiov(unsigned int nkiov, lnet_kiov_t *kiov, unsigned int kiovoffset,
541 unsigned int niov, struct kvec *iov, unsigned int iovoffset,
544 /* NB kiov, iov are READ-ONLY */
545 unsigned int this_nob;
551 LASSERT (!in_interrupt ());
554 while (kiovoffset >= kiov->kiov_len) {
555 kiovoffset -= kiov->kiov_len;
562 while (iovoffset >= iov->iov_len) {
563 iovoffset -= iov->iov_len;
572 this_nob = MIN(kiov->kiov_len - kiovoffset,
573 iov->iov_len - iovoffset);
574 this_nob = MIN(this_nob, nob);
577 addr = ((char *)kmap(kiov->kiov_page)) +
578 kiov->kiov_offset + kiovoffset;
580 memcpy (addr, (char *)iov->iov_base + iovoffset, this_nob);
583 if (kiov->kiov_len > kiovoffset + this_nob) {
585 kiovoffset += this_nob;
587 kunmap(kiov->kiov_page);
594 if (iov->iov_len > iovoffset + this_nob) {
595 iovoffset += this_nob;
604 kunmap(kiov->kiov_page);
606 EXPORT_SYMBOL(lnet_copy_iov2kiov);
609 lnet_extract_kiov(int dst_niov, lnet_kiov_t *dst,
610 int src_niov, lnet_kiov_t *src,
611 unsigned int offset, unsigned int len)
613 /* Initialise 'dst' to the subset of 'src' starting at 'offset',
614 * for exactly 'len' bytes, and return the number of entries.
615 * NB not destructive to 'src' */
616 unsigned int frag_len;
619 if (len == 0) /* no data => */
620 return (0); /* no frags */
622 LASSERT(src_niov > 0);
623 while (offset >= src->kiov_len) { /* skip initial frags */
624 offset -= src->kiov_len;
627 LASSERT(src_niov > 0);
632 LASSERT(src_niov > 0);
633 LASSERT((int)niov <= dst_niov);
635 frag_len = src->kiov_len - offset;
636 dst->kiov_page = src->kiov_page;
637 dst->kiov_offset = src->kiov_offset + offset;
639 if (len <= frag_len) {
641 LASSERT(dst->kiov_offset + dst->kiov_len <= PAGE_SIZE);
645 dst->kiov_len = frag_len;
646 LASSERT(dst->kiov_offset + dst->kiov_len <= PAGE_SIZE);
656 EXPORT_SYMBOL(lnet_extract_kiov);
659 lnet_ni_recv(struct lnet_ni *ni, void *private, struct lnet_msg *msg,
660 int delayed, unsigned int offset, unsigned int mlen,
663 unsigned int niov = 0;
664 struct kvec *iov = NULL;
665 lnet_kiov_t *kiov = NULL;
668 LASSERT (!in_interrupt ());
669 LASSERT (mlen == 0 || msg != NULL);
672 LASSERT(msg->msg_receiving);
673 LASSERT(!msg->msg_sending);
674 LASSERT(rlen == msg->msg_len);
675 LASSERT(mlen <= msg->msg_len);
676 LASSERT(msg->msg_offset == offset);
677 LASSERT(msg->msg_wanted == mlen);
679 msg->msg_receiving = 0;
682 niov = msg->msg_niov;
684 kiov = msg->msg_kiov;
687 LASSERT ((iov == NULL) != (kiov == NULL));
691 rc = (ni->ni_net->net_lnd->lnd_recv)(ni, private, msg, delayed,
692 niov, iov, kiov, offset, mlen,
695 lnet_finalize(msg, rc);
699 lnet_setpayloadbuffer(struct lnet_msg *msg)
701 struct lnet_libmd *md = msg->msg_md;
703 LASSERT(msg->msg_len > 0);
704 LASSERT(!msg->msg_routing);
706 LASSERT(msg->msg_niov == 0);
707 LASSERT(msg->msg_iov == NULL);
708 LASSERT(msg->msg_kiov == NULL);
710 msg->msg_niov = md->md_niov;
711 if ((md->md_options & LNET_MD_KIOV) != 0)
712 msg->msg_kiov = md->md_iov.kiov;
714 msg->msg_iov = md->md_iov.iov;
718 lnet_prep_send(struct lnet_msg *msg, int type, struct lnet_process_id target,
719 unsigned int offset, unsigned int len)
721 msg->msg_type = type;
722 msg->msg_target = target;
724 msg->msg_offset = offset;
727 lnet_setpayloadbuffer(msg);
729 memset (&msg->msg_hdr, 0, sizeof (msg->msg_hdr));
730 msg->msg_hdr.type = cpu_to_le32(type);
731 /* dest_nid will be overwritten by lnet_select_pathway() */
732 msg->msg_hdr.dest_nid = cpu_to_le64(target.nid);
733 msg->msg_hdr.dest_pid = cpu_to_le32(target.pid);
734 /* src_nid will be set later */
735 msg->msg_hdr.src_pid = cpu_to_le32(the_lnet.ln_pid);
736 msg->msg_hdr.payload_length = cpu_to_le32(len);
740 lnet_ni_send(struct lnet_ni *ni, struct lnet_msg *msg)
742 void *priv = msg->msg_private;
745 LASSERT (!in_interrupt ());
746 LASSERT (LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND ||
747 (msg->msg_txcredit && msg->msg_peertxcredit));
749 rc = (ni->ni_net->net_lnd->lnd_send)(ni, priv, msg);
751 lnet_finalize(msg, rc);
755 lnet_ni_eager_recv(struct lnet_ni *ni, struct lnet_msg *msg)
759 LASSERT(!msg->msg_sending);
760 LASSERT(msg->msg_receiving);
761 LASSERT(!msg->msg_rx_ready_delay);
762 LASSERT(ni->ni_net->net_lnd->lnd_eager_recv != NULL);
764 msg->msg_rx_ready_delay = 1;
765 rc = (ni->ni_net->net_lnd->lnd_eager_recv)(ni, msg->msg_private, msg,
768 CERROR("recv from %s / send to %s aborted: "
769 "eager_recv failed %d\n",
770 libcfs_nid2str(msg->msg_rxpeer->lpni_nid),
771 libcfs_id2str(msg->msg_target), rc);
772 LASSERT(rc < 0); /* required by my callers */
779 * This function can be called from two paths:
780 * 1. when sending a message
781 * 2. when decommiting a message (lnet_msg_decommit_tx())
782 * In both these cases the peer_ni should have it's reference count
783 * acquired by the caller and therefore it is safe to drop the spin
784 * lock before calling lnd_query()
787 lnet_ni_query_locked(struct lnet_ni *ni, struct lnet_peer_ni *lp)
789 cfs_time_t last_alive = 0;
790 int cpt = lnet_cpt_of_nid_locked(lp->lpni_nid, ni);
792 LASSERT(lnet_peer_aliveness_enabled(lp));
793 LASSERT(ni->ni_net->net_lnd->lnd_query != NULL);
795 lnet_net_unlock(cpt);
796 (ni->ni_net->net_lnd->lnd_query)(ni, lp->lpni_nid, &last_alive);
799 lp->lpni_last_query = cfs_time_current();
801 if (last_alive != 0) /* NI has updated timestamp */
802 lp->lpni_last_alive = last_alive;
805 /* NB: always called with lnet_net_lock held */
807 lnet_peer_is_alive (struct lnet_peer_ni *lp, cfs_time_t now)
812 LASSERT (lnet_peer_aliveness_enabled(lp));
815 * Trust lnet_notify() if it has more recent aliveness news, but
816 * ignore the initial assumed death (see lnet_peers_start_down()).
818 spin_lock(&lp->lpni_lock);
819 if (!lp->lpni_alive && lp->lpni_alive_count > 0 &&
820 cfs_time_aftereq(lp->lpni_timestamp, lp->lpni_last_alive)) {
821 spin_unlock(&lp->lpni_lock);
826 cfs_time_add(lp->lpni_last_alive,
827 cfs_time_seconds(lp->lpni_net->net_tunables.
829 alive = cfs_time_after(deadline, now);
832 * Update obsolete lp_alive except for routers assumed to be dead
833 * initially, because router checker would update aliveness in this
834 * case, and moreover lpni_last_alive at peer creation is assumed.
836 if (alive && !lp->lpni_alive &&
837 !(lnet_isrouter(lp) && lp->lpni_alive_count == 0)) {
838 spin_unlock(&lp->lpni_lock);
839 lnet_notify_locked(lp, 0, 1, lp->lpni_last_alive);
841 spin_unlock(&lp->lpni_lock);
848 /* NB: returns 1 when alive, 0 when dead, negative when error;
849 * may drop the lnet_net_lock */
851 lnet_peer_alive_locked (struct lnet_ni *ni, struct lnet_peer_ni *lp)
853 cfs_time_t now = cfs_time_current();
855 if (!lnet_peer_aliveness_enabled(lp))
858 if (lnet_peer_is_alive(lp, now))
862 * Peer appears dead, but we should avoid frequent NI queries (at
863 * most once per lnet_queryinterval seconds).
865 if (lp->lpni_last_query != 0) {
866 static const int lnet_queryinterval = 1;
868 cfs_time_t next_query =
869 cfs_time_add(lp->lpni_last_query,
870 cfs_time_seconds(lnet_queryinterval));
872 if (cfs_time_before(now, next_query)) {
874 CWARN("Unexpected aliveness of peer %s: "
876 libcfs_nid2str(lp->lpni_nid),
877 (int)now, (int)next_query,
879 lp->lpni_net->net_tunables.lct_peer_timeout);
884 /* query NI for latest aliveness news */
885 lnet_ni_query_locked(ni, lp);
887 if (lnet_peer_is_alive(lp, now))
890 lnet_notify_locked(lp, 0, 0, lp->lpni_last_alive);
895 * \param msg The message to be sent.
896 * \param do_send True if lnet_ni_send() should be called in this function.
897 * lnet_send() is going to lnet_net_unlock immediately after this, so
898 * it sets do_send FALSE and I don't do the unlock/send/lock bit.
900 * \retval LNET_CREDIT_OK If \a msg sent or OK to send.
901 * \retval LNET_CREDIT_WAIT If \a msg blocked for credit.
902 * \retval -EHOSTUNREACH If the next hop of the message appears dead.
903 * \retval -ECANCELED If the MD of the message has been unlinked.
906 lnet_post_send_locked(struct lnet_msg *msg, int do_send)
908 struct lnet_peer_ni *lp = msg->msg_txpeer;
909 struct lnet_ni *ni = msg->msg_txni;
910 int cpt = msg->msg_tx_cpt;
911 struct lnet_tx_queue *tq = ni->ni_tx_queues[cpt];
913 /* non-lnet_send() callers have checked before */
914 LASSERT(!do_send || msg->msg_tx_delayed);
915 LASSERT(!msg->msg_receiving);
916 LASSERT(msg->msg_tx_committed);
918 /* NB 'lp' is always the next hop */
919 if ((msg->msg_target.pid & LNET_PID_USERFLAG) == 0 &&
920 lnet_peer_alive_locked(ni, lp) == 0) {
921 the_lnet.ln_counters[cpt]->drop_count++;
922 the_lnet.ln_counters[cpt]->drop_length += msg->msg_len;
923 lnet_net_unlock(cpt);
925 lnet_incr_stats(&msg->msg_txpeer->lpni_stats,
927 LNET_STATS_TYPE_DROP);
929 lnet_incr_stats(&msg->msg_txni->ni_stats,
931 LNET_STATS_TYPE_DROP);
933 CNETERR("Dropping message for %s: peer not alive\n",
934 libcfs_id2str(msg->msg_target));
936 lnet_finalize(msg, -EHOSTUNREACH);
939 return -EHOSTUNREACH;
942 if (msg->msg_md != NULL &&
943 (msg->msg_md->md_flags & LNET_MD_FLAG_ABORTED) != 0) {
944 lnet_net_unlock(cpt);
946 CNETERR("Aborting message for %s: LNetM[DE]Unlink() already "
947 "called on the MD/ME.\n",
948 libcfs_id2str(msg->msg_target));
950 lnet_finalize(msg, -ECANCELED);
956 if (!msg->msg_peertxcredit) {
957 spin_lock(&lp->lpni_lock);
958 LASSERT((lp->lpni_txcredits < 0) ==
959 !list_empty(&lp->lpni_txq));
961 msg->msg_peertxcredit = 1;
962 lp->lpni_txqnob += msg->msg_len + sizeof(struct lnet_hdr);
963 lp->lpni_txcredits--;
965 if (lp->lpni_txcredits < lp->lpni_mintxcredits)
966 lp->lpni_mintxcredits = lp->lpni_txcredits;
968 if (lp->lpni_txcredits < 0) {
969 msg->msg_tx_delayed = 1;
970 list_add_tail(&msg->msg_list, &lp->lpni_txq);
971 spin_unlock(&lp->lpni_lock);
972 return LNET_CREDIT_WAIT;
974 spin_unlock(&lp->lpni_lock);
977 if (!msg->msg_txcredit) {
978 LASSERT((tq->tq_credits < 0) ==
979 !list_empty(&tq->tq_delayed));
981 msg->msg_txcredit = 1;
983 atomic_dec(&ni->ni_tx_credits);
985 if (tq->tq_credits < tq->tq_credits_min)
986 tq->tq_credits_min = tq->tq_credits;
988 if (tq->tq_credits < 0) {
989 msg->msg_tx_delayed = 1;
990 list_add_tail(&msg->msg_list, &tq->tq_delayed);
991 return LNET_CREDIT_WAIT;
996 lnet_net_unlock(cpt);
997 lnet_ni_send(ni, msg);
1000 return LNET_CREDIT_OK;
1004 static struct lnet_rtrbufpool *
1005 lnet_msg2bufpool(struct lnet_msg *msg)
1007 struct lnet_rtrbufpool *rbp;
1010 LASSERT(msg->msg_rx_committed);
1012 cpt = msg->msg_rx_cpt;
1013 rbp = &the_lnet.ln_rtrpools[cpt][0];
1015 LASSERT(msg->msg_len <= LNET_MTU);
1016 while (msg->msg_len > (unsigned int)rbp->rbp_npages * PAGE_SIZE) {
1018 LASSERT(rbp < &the_lnet.ln_rtrpools[cpt][LNET_NRBPOOLS]);
1025 lnet_post_routed_recv_locked(struct lnet_msg *msg, int do_recv)
1027 /* lnet_parse is going to lnet_net_unlock immediately after this, so it
1028 * sets do_recv FALSE and I don't do the unlock/send/lock bit.
1029 * I return LNET_CREDIT_WAIT if msg blocked and LNET_CREDIT_OK if
1030 * received or OK to receive */
1031 struct lnet_peer_ni *lp = msg->msg_rxpeer;
1032 struct lnet_rtrbufpool *rbp;
1033 struct lnet_rtrbuf *rb;
1035 LASSERT (msg->msg_iov == NULL);
1036 LASSERT (msg->msg_kiov == NULL);
1037 LASSERT (msg->msg_niov == 0);
1038 LASSERT (msg->msg_routing);
1039 LASSERT (msg->msg_receiving);
1040 LASSERT (!msg->msg_sending);
1042 /* non-lnet_parse callers only receive delayed messages */
1043 LASSERT(!do_recv || msg->msg_rx_delayed);
1045 if (!msg->msg_peerrtrcredit) {
1046 spin_lock(&lp->lpni_lock);
1047 LASSERT((lp->lpni_rtrcredits < 0) ==
1048 !list_empty(&lp->lpni_rtrq));
1050 msg->msg_peerrtrcredit = 1;
1051 lp->lpni_rtrcredits--;
1052 if (lp->lpni_rtrcredits < lp->lpni_minrtrcredits)
1053 lp->lpni_minrtrcredits = lp->lpni_rtrcredits;
1055 if (lp->lpni_rtrcredits < 0) {
1056 /* must have checked eager_recv before here */
1057 LASSERT(msg->msg_rx_ready_delay);
1058 msg->msg_rx_delayed = 1;
1059 list_add_tail(&msg->msg_list, &lp->lpni_rtrq);
1060 spin_unlock(&lp->lpni_lock);
1061 return LNET_CREDIT_WAIT;
1063 spin_unlock(&lp->lpni_lock);
1066 rbp = lnet_msg2bufpool(msg);
1068 if (!msg->msg_rtrcredit) {
1069 msg->msg_rtrcredit = 1;
1071 if (rbp->rbp_credits < rbp->rbp_mincredits)
1072 rbp->rbp_mincredits = rbp->rbp_credits;
1074 if (rbp->rbp_credits < 0) {
1075 /* must have checked eager_recv before here */
1076 LASSERT(msg->msg_rx_ready_delay);
1077 msg->msg_rx_delayed = 1;
1078 list_add_tail(&msg->msg_list, &rbp->rbp_msgs);
1079 return LNET_CREDIT_WAIT;
1083 LASSERT(!list_empty(&rbp->rbp_bufs));
1084 rb = list_entry(rbp->rbp_bufs.next, struct lnet_rtrbuf, rb_list);
1085 list_del(&rb->rb_list);
1087 msg->msg_niov = rbp->rbp_npages;
1088 msg->msg_kiov = &rb->rb_kiov[0];
1091 int cpt = msg->msg_rx_cpt;
1093 lnet_net_unlock(cpt);
1094 lnet_ni_recv(msg->msg_rxni, msg->msg_private, msg, 1,
1095 0, msg->msg_len, msg->msg_len);
1098 return LNET_CREDIT_OK;
1102 lnet_return_tx_credits_locked(struct lnet_msg *msg)
1104 struct lnet_peer_ni *txpeer = msg->msg_txpeer;
1105 struct lnet_ni *txni = msg->msg_txni;
1106 struct lnet_msg *msg2;
1108 if (msg->msg_txcredit) {
1109 struct lnet_ni *ni = msg->msg_txni;
1110 struct lnet_tx_queue *tq = ni->ni_tx_queues[msg->msg_tx_cpt];
1112 /* give back NI txcredits */
1113 msg->msg_txcredit = 0;
1115 LASSERT((tq->tq_credits < 0) ==
1116 !list_empty(&tq->tq_delayed));
1119 atomic_inc(&ni->ni_tx_credits);
1120 if (tq->tq_credits <= 0) {
1121 msg2 = list_entry(tq->tq_delayed.next,
1122 struct lnet_msg, msg_list);
1123 list_del(&msg2->msg_list);
1125 LASSERT(msg2->msg_txni == ni);
1126 LASSERT(msg2->msg_tx_delayed);
1127 LASSERT(msg2->msg_tx_cpt == msg->msg_tx_cpt);
1129 (void) lnet_post_send_locked(msg2, 1);
1133 if (msg->msg_peertxcredit) {
1134 /* give back peer txcredits */
1135 msg->msg_peertxcredit = 0;
1137 spin_lock(&txpeer->lpni_lock);
1138 LASSERT((txpeer->lpni_txcredits < 0) ==
1139 !list_empty(&txpeer->lpni_txq));
1141 txpeer->lpni_txqnob -= msg->msg_len + sizeof(struct lnet_hdr);
1142 LASSERT(txpeer->lpni_txqnob >= 0);
1144 txpeer->lpni_txcredits++;
1145 if (txpeer->lpni_txcredits <= 0) {
1148 msg2 = list_entry(txpeer->lpni_txq.next,
1149 struct lnet_msg, msg_list);
1150 list_del(&msg2->msg_list);
1151 spin_unlock(&txpeer->lpni_lock);
1153 LASSERT(msg2->msg_txpeer == txpeer);
1154 LASSERT(msg2->msg_tx_delayed);
1156 msg2_cpt = msg2->msg_tx_cpt;
1159 * The msg_cpt can be different from the msg2_cpt
1160 * so we need to make sure we lock the correct cpt
1162 * Once we call lnet_post_send_locked() it is no
1163 * longer safe to access msg2, since it could've
1164 * been freed by lnet_finalize(), but we still
1165 * need to relock the correct cpt, so we cache the
1166 * msg2_cpt for the purpose of the check that
1167 * follows the call to lnet_pose_send_locked().
1169 if (msg2_cpt != msg->msg_tx_cpt) {
1170 lnet_net_unlock(msg->msg_tx_cpt);
1171 lnet_net_lock(msg2_cpt);
1173 (void) lnet_post_send_locked(msg2, 1);
1174 if (msg2_cpt != msg->msg_tx_cpt) {
1175 lnet_net_unlock(msg2_cpt);
1176 lnet_net_lock(msg->msg_tx_cpt);
1179 spin_unlock(&txpeer->lpni_lock);
1184 msg->msg_txni = NULL;
1185 lnet_ni_decref_locked(txni, msg->msg_tx_cpt);
1188 if (txpeer != NULL) {
1191 * Once the patch for the health comes in we need to set
1192 * the health of the peer ni to bad when we fail to send
1194 * int status = msg->msg_ev.status;
1196 * lnet_set_peer_ni_health_locked(txpeer, false)
1198 msg->msg_txpeer = NULL;
1199 lnet_peer_ni_decref_locked(txpeer);
1204 lnet_schedule_blocked_locked(struct lnet_rtrbufpool *rbp)
1206 struct lnet_msg *msg;
1208 if (list_empty(&rbp->rbp_msgs))
1210 msg = list_entry(rbp->rbp_msgs.next,
1211 struct lnet_msg, msg_list);
1212 list_del(&msg->msg_list);
1214 (void)lnet_post_routed_recv_locked(msg, 1);
1218 lnet_drop_routed_msgs_locked(struct list_head *list, int cpt)
1220 struct lnet_msg *msg;
1221 struct lnet_msg *tmp;
1223 lnet_net_unlock(cpt);
1225 list_for_each_entry_safe(msg, tmp, list, msg_list) {
1226 lnet_ni_recv(msg->msg_rxni, msg->msg_private, NULL,
1227 0, 0, 0, msg->msg_hdr.payload_length);
1228 list_del_init(&msg->msg_list);
1229 lnet_finalize(msg, -ECANCELED);
1236 lnet_return_rx_credits_locked(struct lnet_msg *msg)
1238 struct lnet_peer_ni *rxpeer = msg->msg_rxpeer;
1239 struct lnet_ni *rxni = msg->msg_rxni;
1240 struct lnet_msg *msg2;
1242 if (msg->msg_rtrcredit) {
1243 /* give back global router credits */
1244 struct lnet_rtrbuf *rb;
1245 struct lnet_rtrbufpool *rbp;
1247 /* NB If a msg ever blocks for a buffer in rbp_msgs, it stays
1248 * there until it gets one allocated, or aborts the wait
1250 LASSERT(msg->msg_kiov != NULL);
1252 rb = list_entry(msg->msg_kiov, struct lnet_rtrbuf, rb_kiov[0]);
1255 msg->msg_kiov = NULL;
1256 msg->msg_rtrcredit = 0;
1258 LASSERT(rbp == lnet_msg2bufpool(msg));
1260 LASSERT((rbp->rbp_credits > 0) ==
1261 !list_empty(&rbp->rbp_bufs));
1263 /* If routing is now turned off, we just drop this buffer and
1264 * don't bother trying to return credits. */
1265 if (!the_lnet.ln_routing) {
1266 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1270 /* It is possible that a user has lowered the desired number of
1271 * buffers in this pool. Make sure we never put back
1272 * more buffers than the stated number. */
1273 if (unlikely(rbp->rbp_credits >= rbp->rbp_req_nbuffers)) {
1274 /* Discard this buffer so we don't have too
1276 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1277 rbp->rbp_nbuffers--;
1279 list_add(&rb->rb_list, &rbp->rbp_bufs);
1281 if (rbp->rbp_credits <= 0)
1282 lnet_schedule_blocked_locked(rbp);
1287 if (msg->msg_peerrtrcredit) {
1288 /* give back peer router credits */
1289 msg->msg_peerrtrcredit = 0;
1291 spin_lock(&rxpeer->lpni_lock);
1292 LASSERT((rxpeer->lpni_rtrcredits < 0) ==
1293 !list_empty(&rxpeer->lpni_rtrq));
1295 rxpeer->lpni_rtrcredits++;
1297 /* drop all messages which are queued to be routed on that
1299 if (!the_lnet.ln_routing) {
1300 struct list_head drop;
1301 INIT_LIST_HEAD(&drop);
1302 list_splice_init(&rxpeer->lpni_rtrq, &drop);
1303 spin_unlock(&rxpeer->lpni_lock);
1304 lnet_drop_routed_msgs_locked(&drop, msg->msg_rx_cpt);
1305 } else if (rxpeer->lpni_rtrcredits <= 0) {
1306 msg2 = list_entry(rxpeer->lpni_rtrq.next,
1307 struct lnet_msg, msg_list);
1308 list_del(&msg2->msg_list);
1309 spin_unlock(&rxpeer->lpni_lock);
1310 (void) lnet_post_routed_recv_locked(msg2, 1);
1312 spin_unlock(&rxpeer->lpni_lock);
1316 msg->msg_rxni = NULL;
1317 lnet_ni_decref_locked(rxni, msg->msg_rx_cpt);
1319 if (rxpeer != NULL) {
1320 msg->msg_rxpeer = NULL;
1321 lnet_peer_ni_decref_locked(rxpeer);
1326 lnet_compare_peers(struct lnet_peer_ni *p1, struct lnet_peer_ni *p2)
1328 if (p1->lpni_txqnob < p2->lpni_txqnob)
1331 if (p1->lpni_txqnob > p2->lpni_txqnob)
1334 if (p1->lpni_txcredits > p2->lpni_txcredits)
1337 if (p1->lpni_txcredits < p2->lpni_txcredits)
1344 lnet_compare_routes(struct lnet_route *r1, struct lnet_route *r2)
1346 struct lnet_peer_ni *p1 = r1->lr_gateway;
1347 struct lnet_peer_ni *p2 = r2->lr_gateway;
1348 int r1_hops = (r1->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r1->lr_hops;
1349 int r2_hops = (r2->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r2->lr_hops;
1352 if (r1->lr_priority < r2->lr_priority)
1355 if (r1->lr_priority > r2->lr_priority)
1358 if (r1_hops < r2_hops)
1361 if (r1_hops > r2_hops)
1364 rc = lnet_compare_peers(p1, p2);
1368 if (r1->lr_seq - r2->lr_seq <= 0)
1374 static struct lnet_peer_ni *
1375 lnet_find_route_locked(struct lnet_net *net, lnet_nid_t target,
1378 struct lnet_remotenet *rnet;
1379 struct lnet_route *route;
1380 struct lnet_route *best_route;
1381 struct lnet_route *last_route;
1382 struct lnet_peer_ni *lpni_best;
1383 struct lnet_peer_ni *lp;
1386 /* If @rtr_nid is not LNET_NID_ANY, return the gateway with
1387 * rtr_nid nid, otherwise find the best gateway I can use */
1389 rnet = lnet_find_rnet_locked(LNET_NIDNET(target));
1394 best_route = last_route = NULL;
1395 list_for_each_entry(route, &rnet->lrn_routes, lr_list) {
1396 lp = route->lr_gateway;
1398 if (!lnet_is_route_alive(route))
1401 if (net != NULL && lp->lpni_net != net)
1404 if (lp->lpni_nid == rtr_nid) /* it's pre-determined router */
1407 if (lpni_best == NULL) {
1408 best_route = last_route = route;
1413 /* no protection on below fields, but it's harmless */
1414 if (last_route->lr_seq - route->lr_seq < 0)
1417 rc = lnet_compare_routes(route, best_route);
1425 /* set sequence number on the best router to the latest sequence + 1
1426 * so we can round-robin all routers, it's race and inaccurate but
1427 * harmless and functional */
1428 if (best_route != NULL)
1429 best_route->lr_seq = last_route->lr_seq + 1;
1433 static struct lnet_ni *
1434 lnet_get_best_ni(struct lnet_net *local_net, struct lnet_ni *cur_ni,
1437 struct lnet_ni *ni = NULL, *best_ni = cur_ni;
1438 unsigned int shortest_distance;
1441 if (best_ni == NULL) {
1442 shortest_distance = UINT_MAX;
1443 best_credits = INT_MIN;
1445 shortest_distance = cfs_cpt_distance(lnet_cpt_table(), md_cpt,
1446 best_ni->ni_dev_cpt);
1447 best_credits = atomic_read(&best_ni->ni_tx_credits);
1450 while ((ni = lnet_get_next_ni_locked(local_net, ni))) {
1451 unsigned int distance;
1454 if (!lnet_is_ni_healthy_locked(ni))
1457 ni_credits = atomic_read(&ni->ni_tx_credits);
1460 * calculate the distance from the CPT on which
1461 * the message memory is allocated to the CPT of
1462 * the NI's physical device
1464 distance = cfs_cpt_distance(lnet_cpt_table(),
1469 * All distances smaller than the NUMA range
1470 * are treated equally.
1472 if (distance < lnet_numa_range)
1473 distance = lnet_numa_range;
1476 * Select on shorter distance, then available
1477 * credits, then round-robin.
1479 if (distance > shortest_distance) {
1481 } else if (distance < shortest_distance) {
1482 shortest_distance = distance;
1483 } else if (ni_credits < best_credits) {
1485 } else if (ni_credits == best_credits) {
1486 if (best_ni && (best_ni)->ni_seq <= ni->ni_seq)
1490 best_credits = ni_credits;
1497 * Traffic to the LNET_RESERVED_PORTAL may not trigger peer discovery,
1498 * because such traffic is required to perform discovery. We therefore
1499 * exclude all GET and PUT on that portal. We also exclude all ACK and
1500 * REPLY traffic, but that is because the portal is not tracked in the
1501 * message structure for these message types. We could restrict this
1502 * further by also checking for LNET_PROTO_PING_MATCHBITS.
1505 lnet_msg_discovery(struct lnet_msg *msg)
1507 if (msg->msg_type == LNET_MSG_PUT) {
1508 if (msg->msg_hdr.msg.put.ptl_index != LNET_RESERVED_PORTAL)
1510 } else if (msg->msg_type == LNET_MSG_GET) {
1511 if (msg->msg_hdr.msg.get.ptl_index != LNET_RESERVED_PORTAL)
1518 lnet_select_pathway(lnet_nid_t src_nid, lnet_nid_t dst_nid,
1519 struct lnet_msg *msg, lnet_nid_t rtr_nid)
1521 struct lnet_ni *best_ni;
1522 struct lnet_peer_ni *best_lpni;
1523 struct lnet_peer_ni *best_gw;
1524 struct lnet_peer_ni *lpni;
1525 struct lnet_peer_ni *final_dst;
1526 struct lnet_peer *peer;
1527 struct lnet_peer_net *peer_net;
1528 struct lnet_net *local_net;
1535 int best_lpni_credits;
1539 * get an initial CPT to use for locking. The idea here is not to
1540 * serialize the calls to select_pathway, so that as many
1541 * operations can run concurrently as possible. To do that we use
1542 * the CPT where this call is being executed. Later on when we
1543 * determine the CPT to use in lnet_message_commit, we switch the
1544 * lock and check if there was any configuration change. If none,
1545 * then we proceed, if there is, then we restart the operation.
1547 cpt = lnet_net_lock_current();
1549 md_cpt = lnet_cpt_of_md(msg->msg_md, msg->msg_offset);
1550 if (md_cpt == CFS_CPT_ANY)
1561 local_found = false;
1564 * lnet_nid2peerni_locked() is the path that will find an
1565 * existing peer_ni, or create one and mark it as having been
1566 * created due to network traffic.
1568 lpni = lnet_nid2peerni_locked(dst_nid, LNET_NID_ANY, cpt);
1570 lnet_net_unlock(cpt);
1571 return PTR_ERR(lpni);
1575 * If we're being asked to send to the loopback interface, there
1576 * is no need to go through any selection. We can just shortcut
1577 * the entire process and send over lolnd
1579 if (LNET_NETTYP(LNET_NIDNET(dst_nid)) == LOLND) {
1580 lnet_peer_ni_decref_locked(lpni);
1581 best_ni = the_lnet.ln_loni;
1586 * Now that we have a peer_ni, check if we want to discover
1587 * the peer. Traffic to the LNET_RESERVED_PORTAL should not
1588 * trigger discovery.
1590 peer = lpni->lpni_peer_net->lpn_peer;
1591 if (lnet_msg_discovery(msg) && !lnet_peer_is_uptodate(peer)) {
1592 rc = lnet_discover_peer_locked(lpni, cpt, false);
1594 lnet_peer_ni_decref_locked(lpni);
1595 lnet_net_unlock(cpt);
1598 /* The peer may have changed. */
1599 peer = lpni->lpni_peer_net->lpn_peer;
1600 /* queue message and return */
1601 msg->msg_src_nid_param = src_nid;
1602 msg->msg_rtr_nid_param = rtr_nid;
1603 msg->msg_sending = 0;
1604 list_add_tail(&msg->msg_list, &peer->lp_dc_pendq);
1605 lnet_peer_ni_decref_locked(lpni);
1606 lnet_net_unlock(cpt);
1608 CDEBUG(D_NET, "%s pending discovery\n",
1609 libcfs_nid2str(peer->lp_primary_nid));
1611 return LNET_DC_WAIT;
1613 lnet_peer_ni_decref_locked(lpni);
1615 /* If peer is not healthy then can not send anything to it */
1616 if (!lnet_is_peer_healthy_locked(peer)) {
1617 lnet_net_unlock(cpt);
1618 return -EHOSTUNREACH;
1622 * STEP 1: first jab at determining best_ni
1623 * if src_nid is explicitly specified, then best_ni is already
1624 * pre-determiend for us. Otherwise we need to select the best
1625 * one to use later on
1627 if (src_nid != LNET_NID_ANY) {
1628 best_ni = lnet_nid2ni_locked(src_nid, cpt);
1630 lnet_net_unlock(cpt);
1631 LCONSOLE_WARN("Can't send to %s: src %s is not a "
1632 "local nid\n", libcfs_nid2str(dst_nid),
1633 libcfs_nid2str(src_nid));
1638 if (msg->msg_type == LNET_MSG_REPLY ||
1639 msg->msg_type == LNET_MSG_ACK ||
1640 !lnet_peer_is_multi_rail(peer) ||
1643 * for replies we want to respond on the same peer_ni we
1644 * received the message on if possible. If not, then pick
1645 * a peer_ni to send to
1647 * if the peer is non-multi-rail then you want to send to
1648 * the dst_nid provided as well.
1650 * If the best_ni has already been determined, IE the
1651 * src_nid has been specified, then use the
1652 * destination_nid provided as well, since we're
1653 * continuing a series of related messages for the same
1656 * It is expected to find the lpni using dst_nid, since we
1657 * created it earlier.
1659 best_lpni = lnet_find_peer_ni_locked(dst_nid);
1661 lnet_peer_ni_decref_locked(best_lpni);
1663 if (best_lpni && !lnet_get_net_locked(LNET_NIDNET(dst_nid))) {
1665 * this lpni is not on a local network so we need
1666 * to route this reply.
1668 best_gw = lnet_find_route_locked(NULL,
1669 best_lpni->lpni_nid,
1673 * RULE: Each node considers only the next-hop
1675 * We're going to route the message, so change the peer to
1678 LASSERT(best_gw->lpni_peer_net);
1679 LASSERT(best_gw->lpni_peer_net->lpn_peer);
1680 peer = best_gw->lpni_peer_net->lpn_peer;
1683 * if the router is not multi-rail then use the best_gw
1684 * found to send the message to
1686 if (!lnet_peer_is_multi_rail(peer))
1687 best_lpni = best_gw;
1695 } else if (!best_lpni) {
1696 lnet_net_unlock(cpt);
1697 CERROR("unable to send msg_type %d to "
1698 "originating %s. Destination NID not in DB\n",
1699 msg->msg_type, libcfs_nid2str(dst_nid));
1705 * We must use a consistent source address when sending to a
1706 * non-MR peer. However, a non-MR peer can have multiple NIDs
1707 * on multiple networks, and we may even need to talk to this
1708 * peer on multiple networks -- certain types of
1709 * load-balancing configuration do this.
1711 * So we need to pick the NI the peer prefers for this
1712 * particular network.
1714 if (!lnet_peer_is_multi_rail(peer)) {
1716 lnet_net_unlock(cpt);
1717 CERROR("no route to %s\n",
1718 libcfs_nid2str(dst_nid));
1719 return -EHOSTUNREACH;
1722 /* best ni is already set if src_nid was provided */
1724 /* Get the target peer_ni */
1725 peer_net = lnet_peer_get_net_locked(peer,
1726 LNET_NIDNET(best_lpni->lpni_nid));
1727 LASSERT(peer_net != NULL);
1728 list_for_each_entry(lpni, &peer_net->lpn_peer_nis,
1730 if (lpni->lpni_pref_nnids == 0)
1732 LASSERT(lpni->lpni_pref_nnids == 1);
1733 best_ni = lnet_nid2ni_locked(
1734 lpni->lpni_pref.nid, cpt);
1738 /* if best_ni is still not set just pick one */
1740 best_ni = lnet_net2ni_locked(
1741 best_lpni->lpni_net->net_id, cpt);
1742 /* If there is no best_ni we don't have a route */
1744 lnet_net_unlock(cpt);
1745 CERROR("no path to %s from net %s\n",
1746 libcfs_nid2str(best_lpni->lpni_nid),
1747 libcfs_net2str(best_lpni->lpni_net->net_id));
1748 return -EHOSTUNREACH;
1750 lpni = list_entry(peer_net->lpn_peer_nis.next,
1751 struct lnet_peer_ni,
1754 /* Set preferred NI if necessary. */
1755 if (lpni->lpni_pref_nnids == 0)
1756 lnet_peer_ni_set_non_mr_pref_nid(lpni, best_ni->ni_nid);
1760 * if we already found a best_ni because src_nid is specified and
1761 * best_lpni because we are replying to a message then just send
1764 if (best_ni && best_lpni)
1768 * If we already found a best_ni because src_nid is specified then
1769 * pick the peer then send the message
1775 * pick the best_ni by going through all the possible networks of
1776 * that peer and see which local NI is best suited to talk to that
1779 * Locally connected networks will always be preferred over
1780 * a routed network. If there are only routed paths to the peer,
1781 * then the best route is chosen. If all routes are equal then
1782 * they are used in round robin.
1784 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_peer_nets) {
1785 if (!lnet_is_peer_net_healthy_locked(peer_net))
1788 local_net = lnet_get_net_locked(peer_net->lpn_net_id);
1789 if (!local_net && !routing && !local_found) {
1790 struct lnet_peer_ni *net_gw;
1792 lpni = list_entry(peer_net->lpn_peer_nis.next,
1793 struct lnet_peer_ni,
1796 net_gw = lnet_find_route_locked(NULL,
1804 * lnet_find_route_locked() call
1805 * will return the best_Gw on the
1806 * lpni->lpni_nid network.
1807 * However, best_gw and net_gw can
1808 * be on different networks.
1809 * Therefore need to compare them
1810 * to pick the better of either.
1812 if (lnet_compare_peers(best_gw, net_gw) > 0)
1814 if (best_gw->lpni_gw_seq <= net_gw->lpni_gw_seq)
1829 * a gw on this network is found, but there could be
1830 * other better gateways on other networks. So don't pick
1831 * the best_ni until we determine the best_gw.
1836 /* if no local_net found continue */
1841 * Iterate through the NIs in this local Net and select
1842 * the NI to send from. The selection is determined by
1843 * these 3 criterion in the following priority:
1845 * 2. NI available credits
1848 best_ni = lnet_get_best_ni(local_net, best_ni, md_cpt);
1851 if (!best_ni && !best_gw) {
1852 lnet_net_unlock(cpt);
1853 LCONSOLE_WARN("No local ni found to send from to %s\n",
1854 libcfs_nid2str(dst_nid));
1859 best_ni = lnet_get_best_ni(best_gw->lpni_net, best_ni, md_cpt);
1860 LASSERT(best_gw && best_ni);
1863 * We're going to route the message, so change the peer to
1866 LASSERT(best_gw->lpni_peer_net);
1867 LASSERT(best_gw->lpni_peer_net->lpn_peer);
1868 best_gw->lpni_gw_seq++;
1869 peer = best_gw->lpni_peer_net->lpn_peer;
1873 * Now that we selected the NI to use increment its sequence
1874 * number so the Round Robin algorithm will detect that it has
1875 * been used and pick the next NI.
1881 * At this point the best_ni is on a local network on which
1882 * the peer has a peer_ni as well
1884 peer_net = lnet_peer_get_net_locked(peer,
1885 best_ni->ni_net->net_id);
1887 * peer_net is not available or the src_nid is explicitly defined
1888 * and the peer_net for that src_nid is unhealthy. find a route to
1889 * the destination nid.
1892 (src_nid != LNET_NID_ANY &&
1893 !lnet_is_peer_net_healthy_locked(peer_net))) {
1894 best_gw = lnet_find_route_locked(best_ni->ni_net,
1898 * if no route is found for that network then
1899 * move onto the next peer_ni in the peer
1902 lnet_net_unlock(cpt);
1903 LCONSOLE_WARN("No route to peer from %s\n",
1904 libcfs_nid2str(best_ni->ni_nid));
1905 return -EHOSTUNREACH;
1908 CDEBUG(D_NET, "Best route to %s via %s for %s %d\n",
1909 libcfs_nid2str(dst_nid),
1910 libcfs_nid2str(best_gw->lpni_nid),
1911 lnet_msgtyp2str(msg->msg_type), msg->msg_len);
1915 * RULE: Each node considers only the next-hop
1917 * We're going to route the message, so change the peer to
1920 LASSERT(best_gw->lpni_peer_net);
1921 LASSERT(best_gw->lpni_peer_net->lpn_peer);
1922 peer = best_gw->lpni_peer_net->lpn_peer;
1923 } else if (!lnet_is_peer_net_healthy_locked(peer_net)) {
1925 * this peer_net is unhealthy but we still have an opportunity
1926 * to find another peer_net that we can use
1928 __u32 net_id = peer_net->lpn_net_id;
1929 LCONSOLE_WARN("peer net %s unhealthy\n",
1930 libcfs_net2str(net_id));
1935 * Look at the peer NIs for the destination peer that connect
1936 * to the chosen net. If a peer_ni is preferred when using the
1937 * best_ni to communicate, we use that one. If there is no
1938 * preferred peer_ni, or there are multiple preferred peer_ni,
1939 * the available transmit credits are used. If the transmit
1940 * credits are equal, we round-robin over the peer_ni.
1943 best_lpni_credits = INT_MIN;
1946 while ((lpni = lnet_get_next_peer_ni_locked(peer, peer_net, lpni))) {
1948 * if this peer ni is not healthy just skip it, no point in
1949 * examining it further
1951 if (!lnet_is_peer_ni_healthy_locked(lpni))
1953 ni_is_pref = lnet_peer_is_pref_nid_locked(lpni,
1956 /* if this is a preferred peer use it */
1957 if (!preferred && ni_is_pref) {
1959 } else if (preferred && !ni_is_pref) {
1961 * this is not the preferred peer so let's ignore
1965 } else if (lpni->lpni_txcredits < best_lpni_credits) {
1967 * We already have a peer that has more credits
1968 * available than this one. No need to consider
1969 * this peer further.
1972 } else if (lpni->lpni_txcredits == best_lpni_credits) {
1974 * The best peer found so far and the current peer
1975 * have the same number of available credits let's
1976 * make sure to select between them using Round
1980 if (best_lpni->lpni_seq <= lpni->lpni_seq)
1986 best_lpni_credits = lpni->lpni_txcredits;
1989 /* if we still can't find a peer ni then we can't reach it */
1991 __u32 net_id = (peer_net) ? peer_net->lpn_net_id :
1992 LNET_NIDNET(dst_nid);
1993 lnet_net_unlock(cpt);
1994 LCONSOLE_WARN("no peer_ni found on peer net %s\n",
1995 libcfs_net2str(net_id));
1996 return -EHOSTUNREACH;
2001 /* Shortcut for loopback. */
2002 if (best_ni == the_lnet.ln_loni) {
2003 /* No send credit hassles with LOLND */
2004 lnet_ni_addref_locked(best_ni, cpt);
2005 msg->msg_hdr.dest_nid = cpu_to_le64(best_ni->ni_nid);
2006 if (!msg->msg_routing)
2007 msg->msg_hdr.src_nid = cpu_to_le64(best_ni->ni_nid);
2008 msg->msg_target.nid = best_ni->ni_nid;
2009 lnet_msg_commit(msg, cpt);
2010 msg->msg_txni = best_ni;
2011 lnet_net_unlock(cpt);
2013 return LNET_CREDIT_OK;
2016 routing = routing || routing2;
2019 * Increment sequence number of the peer selected so that we
2020 * pick the next one in Round Robin.
2022 best_lpni->lpni_seq++;
2025 * grab a reference on the peer_ni so it sticks around even if
2026 * we need to drop and relock the lnet_net_lock below.
2028 lnet_peer_ni_addref_locked(best_lpni);
2031 * Use lnet_cpt_of_nid() to determine the CPT used to commit the
2032 * message. This ensures that we get a CPT that is correct for
2033 * the NI when the NI has been restricted to a subset of all CPTs.
2034 * If the selected CPT differs from the one currently locked, we
2035 * must unlock and relock the lnet_net_lock(), and then check whether
2036 * the configuration has changed. We don't have a hold on the best_ni
2037 * yet, and it may have vanished.
2039 cpt2 = lnet_cpt_of_nid_locked(best_lpni->lpni_nid, best_ni);
2041 __u32 seq = lnet_get_dlc_seq_locked();
2042 lnet_net_unlock(cpt);
2045 if (seq != lnet_get_dlc_seq_locked()) {
2046 lnet_peer_ni_decref_locked(best_lpni);
2052 * store the best_lpni in the message right away to avoid having
2053 * to do the same operation under different conditions
2055 msg->msg_txpeer = best_lpni;
2056 msg->msg_txni = best_ni;
2059 * grab a reference for the best_ni since now it's in use in this
2060 * send. the reference will need to be dropped when the message is
2061 * finished in lnet_finalize()
2063 lnet_ni_addref_locked(msg->msg_txni, cpt);
2066 * Always set the target.nid to the best peer picked. Either the
2067 * nid will be one of the preconfigured NIDs, or the same NID as
2068 * what was originally set in the target or it will be the NID of
2069 * a router if this message should be routed
2071 msg->msg_target.nid = msg->msg_txpeer->lpni_nid;
2074 * lnet_msg_commit assigns the correct cpt to the message, which
2075 * is used to decrement the correct refcount on the ni when it's
2076 * time to return the credits
2078 lnet_msg_commit(msg, cpt);
2081 * If we are routing the message then we don't need to overwrite
2082 * the src_nid since it would've been set at the origin. Otherwise
2083 * we are the originator so we need to set it.
2085 if (!msg->msg_routing)
2086 msg->msg_hdr.src_nid = cpu_to_le64(msg->msg_txni->ni_nid);
2089 msg->msg_target_is_router = 1;
2090 msg->msg_target.pid = LNET_PID_LUSTRE;
2092 * since we're routing we want to ensure that the
2093 * msg_hdr.dest_nid is set to the final destination. When
2094 * the router receives this message it knows how to route
2097 msg->msg_hdr.dest_nid =
2098 cpu_to_le64(final_dst ? final_dst->lpni_nid : dst_nid);
2101 * if we're not routing set the dest_nid to the best peer
2102 * ni that we picked earlier in the algorithm.
2104 msg->msg_hdr.dest_nid = cpu_to_le64(msg->msg_txpeer->lpni_nid);
2107 rc = lnet_post_send_locked(msg, 0);
2110 CDEBUG(D_NET, "TRACE: %s(%s:%s) -> %s(%s:%s) : %s\n",
2111 libcfs_nid2str(msg->msg_hdr.src_nid),
2112 libcfs_nid2str(msg->msg_txni->ni_nid),
2113 libcfs_nid2str(src_nid),
2114 libcfs_nid2str(msg->msg_hdr.dest_nid),
2115 libcfs_nid2str(dst_nid),
2116 libcfs_nid2str(msg->msg_txpeer->lpni_nid),
2117 lnet_msgtyp2str(msg->msg_type));
2119 lnet_net_unlock(cpt);
2125 lnet_send(lnet_nid_t src_nid, struct lnet_msg *msg, lnet_nid_t rtr_nid)
2127 lnet_nid_t dst_nid = msg->msg_target.nid;
2131 * NB: rtr_nid is set to LNET_NID_ANY for all current use-cases,
2132 * but we might want to use pre-determined router for ACK/REPLY
2135 /* NB: ni != NULL == interface pre-determined (ACK/REPLY) */
2136 LASSERT (msg->msg_txpeer == NULL);
2137 LASSERT (!msg->msg_sending);
2138 LASSERT (!msg->msg_target_is_router);
2139 LASSERT (!msg->msg_receiving);
2141 msg->msg_sending = 1;
2143 LASSERT(!msg->msg_tx_committed);
2145 rc = lnet_select_pathway(src_nid, dst_nid, msg, rtr_nid);
2149 if (rc == LNET_CREDIT_OK)
2150 lnet_ni_send(msg->msg_txni, msg);
2152 /* rc == LNET_CREDIT_OK or LNET_CREDIT_WAIT or LNET_DC_WAIT */
2157 lnet_drop_message(struct lnet_ni *ni, int cpt, void *private, unsigned int nob,
2161 lnet_incr_stats(&ni->ni_stats, msg_type, LNET_STATS_TYPE_DROP);
2162 the_lnet.ln_counters[cpt]->drop_count++;
2163 the_lnet.ln_counters[cpt]->drop_length += nob;
2164 lnet_net_unlock(cpt);
2166 lnet_ni_recv(ni, private, NULL, 0, 0, 0, nob);
2170 lnet_recv_put(struct lnet_ni *ni, struct lnet_msg *msg)
2172 struct lnet_hdr *hdr = &msg->msg_hdr;
2174 if (msg->msg_wanted != 0)
2175 lnet_setpayloadbuffer(msg);
2177 lnet_build_msg_event(msg, LNET_EVENT_PUT);
2179 /* Must I ACK? If so I'll grab the ack_wmd out of the header and put
2180 * it back into the ACK during lnet_finalize() */
2181 msg->msg_ack = (!lnet_is_wire_handle_none(&hdr->msg.put.ack_wmd) &&
2182 (msg->msg_md->md_options & LNET_MD_ACK_DISABLE) == 0);
2184 lnet_ni_recv(ni, msg->msg_private, msg, msg->msg_rx_delayed,
2185 msg->msg_offset, msg->msg_wanted, hdr->payload_length);
2189 lnet_parse_put(struct lnet_ni *ni, struct lnet_msg *msg)
2191 struct lnet_hdr *hdr = &msg->msg_hdr;
2192 struct lnet_match_info info;
2196 /* Convert put fields to host byte order */
2197 hdr->msg.put.match_bits = le64_to_cpu(hdr->msg.put.match_bits);
2198 hdr->msg.put.ptl_index = le32_to_cpu(hdr->msg.put.ptl_index);
2199 hdr->msg.put.offset = le32_to_cpu(hdr->msg.put.offset);
2201 /* Primary peer NID. */
2202 info.mi_id.nid = msg->msg_initiator;
2203 info.mi_id.pid = hdr->src_pid;
2204 info.mi_opc = LNET_MD_OP_PUT;
2205 info.mi_portal = hdr->msg.put.ptl_index;
2206 info.mi_rlength = hdr->payload_length;
2207 info.mi_roffset = hdr->msg.put.offset;
2208 info.mi_mbits = hdr->msg.put.match_bits;
2209 info.mi_cpt = lnet_cpt_of_nid(msg->msg_rxpeer->lpni_nid, ni);
2211 msg->msg_rx_ready_delay = ni->ni_net->net_lnd->lnd_eager_recv == NULL;
2212 ready_delay = msg->msg_rx_ready_delay;
2215 rc = lnet_ptl_match_md(&info, msg);
2220 case LNET_MATCHMD_OK:
2221 lnet_recv_put(ni, msg);
2224 case LNET_MATCHMD_NONE:
2226 /* no eager_recv or has already called it, should
2227 * have been attached on delayed list */
2230 rc = lnet_ni_eager_recv(ni, msg);
2237 case LNET_MATCHMD_DROP:
2238 CNETERR("Dropping PUT from %s portal %d match %llu"
2239 " offset %d length %d: %d\n",
2240 libcfs_id2str(info.mi_id), info.mi_portal,
2241 info.mi_mbits, info.mi_roffset, info.mi_rlength, rc);
2243 return -ENOENT; /* -ve: OK but no match */
2248 lnet_parse_get(struct lnet_ni *ni, struct lnet_msg *msg, int rdma_get)
2250 struct lnet_match_info info;
2251 struct lnet_hdr *hdr = &msg->msg_hdr;
2252 struct lnet_process_id source_id;
2253 struct lnet_handle_wire reply_wmd;
2256 /* Convert get fields to host byte order */
2257 hdr->msg.get.match_bits = le64_to_cpu(hdr->msg.get.match_bits);
2258 hdr->msg.get.ptl_index = le32_to_cpu(hdr->msg.get.ptl_index);
2259 hdr->msg.get.sink_length = le32_to_cpu(hdr->msg.get.sink_length);
2260 hdr->msg.get.src_offset = le32_to_cpu(hdr->msg.get.src_offset);
2262 source_id.nid = hdr->src_nid;
2263 source_id.pid = hdr->src_pid;
2264 /* Primary peer NID */
2265 info.mi_id.nid = msg->msg_initiator;
2266 info.mi_id.pid = hdr->src_pid;
2267 info.mi_opc = LNET_MD_OP_GET;
2268 info.mi_portal = hdr->msg.get.ptl_index;
2269 info.mi_rlength = hdr->msg.get.sink_length;
2270 info.mi_roffset = hdr->msg.get.src_offset;
2271 info.mi_mbits = hdr->msg.get.match_bits;
2272 info.mi_cpt = lnet_cpt_of_nid(msg->msg_rxpeer->lpni_nid, ni);
2274 rc = lnet_ptl_match_md(&info, msg);
2275 if (rc == LNET_MATCHMD_DROP) {
2276 CNETERR("Dropping GET from %s portal %d match %llu"
2277 " offset %d length %d\n",
2278 libcfs_id2str(info.mi_id), info.mi_portal,
2279 info.mi_mbits, info.mi_roffset, info.mi_rlength);
2280 return -ENOENT; /* -ve: OK but no match */
2283 LASSERT(rc == LNET_MATCHMD_OK);
2285 lnet_build_msg_event(msg, LNET_EVENT_GET);
2287 reply_wmd = hdr->msg.get.return_wmd;
2289 lnet_prep_send(msg, LNET_MSG_REPLY, source_id,
2290 msg->msg_offset, msg->msg_wanted);
2292 msg->msg_hdr.msg.reply.dst_wmd = reply_wmd;
2295 /* The LND completes the REPLY from her recv procedure */
2296 lnet_ni_recv(ni, msg->msg_private, msg, 0,
2297 msg->msg_offset, msg->msg_len, msg->msg_len);
2301 lnet_ni_recv(ni, msg->msg_private, NULL, 0, 0, 0, 0);
2302 msg->msg_receiving = 0;
2304 rc = lnet_send(ni->ni_nid, msg, LNET_NID_ANY);
2306 /* didn't get as far as lnet_ni_send() */
2307 CERROR("%s: Unable to send REPLY for GET from %s: %d\n",
2308 libcfs_nid2str(ni->ni_nid),
2309 libcfs_id2str(info.mi_id), rc);
2311 lnet_finalize(msg, rc);
2318 lnet_parse_reply(struct lnet_ni *ni, struct lnet_msg *msg)
2320 void *private = msg->msg_private;
2321 struct lnet_hdr *hdr = &msg->msg_hdr;
2322 struct lnet_process_id src = {0};
2323 struct lnet_libmd *md;
2328 cpt = lnet_cpt_of_cookie(hdr->msg.reply.dst_wmd.wh_object_cookie);
2331 src.nid = hdr->src_nid;
2332 src.pid = hdr->src_pid;
2334 /* NB handles only looked up by creator (no flips) */
2335 md = lnet_wire_handle2md(&hdr->msg.reply.dst_wmd);
2336 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
2337 CNETERR("%s: Dropping REPLY from %s for %s "
2339 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
2340 (md == NULL) ? "invalid" : "inactive",
2341 hdr->msg.reply.dst_wmd.wh_interface_cookie,
2342 hdr->msg.reply.dst_wmd.wh_object_cookie);
2343 if (md != NULL && md->md_me != NULL)
2344 CERROR("REPLY MD also attached to portal %d\n",
2345 md->md_me->me_portal);
2347 lnet_res_unlock(cpt);
2348 return -ENOENT; /* -ve: OK but no match */
2351 LASSERT(md->md_offset == 0);
2353 rlength = hdr->payload_length;
2354 mlength = MIN(rlength, (int)md->md_length);
2356 if (mlength < rlength &&
2357 (md->md_options & LNET_MD_TRUNCATE) == 0) {
2358 CNETERR("%s: Dropping REPLY from %s length %d "
2359 "for MD %#llx would overflow (%d)\n",
2360 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
2361 rlength, hdr->msg.reply.dst_wmd.wh_object_cookie,
2363 lnet_res_unlock(cpt);
2364 return -ENOENT; /* -ve: OK but no match */
2367 CDEBUG(D_NET, "%s: Reply from %s of length %d/%d into md %#llx\n",
2368 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
2369 mlength, rlength, hdr->msg.reply.dst_wmd.wh_object_cookie);
2371 lnet_msg_attach_md(msg, md, 0, mlength);
2374 lnet_setpayloadbuffer(msg);
2376 lnet_res_unlock(cpt);
2378 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
2380 lnet_ni_recv(ni, private, msg, 0, 0, mlength, rlength);
2385 lnet_parse_ack(struct lnet_ni *ni, struct lnet_msg *msg)
2387 struct lnet_hdr *hdr = &msg->msg_hdr;
2388 struct lnet_process_id src = {0};
2389 struct lnet_libmd *md;
2392 src.nid = hdr->src_nid;
2393 src.pid = hdr->src_pid;
2395 /* Convert ack fields to host byte order */
2396 hdr->msg.ack.match_bits = le64_to_cpu(hdr->msg.ack.match_bits);
2397 hdr->msg.ack.mlength = le32_to_cpu(hdr->msg.ack.mlength);
2399 cpt = lnet_cpt_of_cookie(hdr->msg.ack.dst_wmd.wh_object_cookie);
2402 /* NB handles only looked up by creator (no flips) */
2403 md = lnet_wire_handle2md(&hdr->msg.ack.dst_wmd);
2404 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
2405 /* Don't moan; this is expected */
2407 "%s: Dropping ACK from %s to %s MD %#llx.%#llx\n",
2408 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
2409 (md == NULL) ? "invalid" : "inactive",
2410 hdr->msg.ack.dst_wmd.wh_interface_cookie,
2411 hdr->msg.ack.dst_wmd.wh_object_cookie);
2412 if (md != NULL && md->md_me != NULL)
2413 CERROR("Source MD also attached to portal %d\n",
2414 md->md_me->me_portal);
2416 lnet_res_unlock(cpt);
2417 return -ENOENT; /* -ve! */
2420 CDEBUG(D_NET, "%s: ACK from %s into md %#llx\n",
2421 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
2422 hdr->msg.ack.dst_wmd.wh_object_cookie);
2424 lnet_msg_attach_md(msg, md, 0, 0);
2426 lnet_res_unlock(cpt);
2428 lnet_build_msg_event(msg, LNET_EVENT_ACK);
2430 lnet_ni_recv(ni, msg->msg_private, msg, 0, 0, 0, msg->msg_len);
2435 * \retval LNET_CREDIT_OK If \a msg is forwarded
2436 * \retval LNET_CREDIT_WAIT If \a msg is blocked because w/o buffer
2437 * \retval -ve error code
2440 lnet_parse_forward_locked(struct lnet_ni *ni, struct lnet_msg *msg)
2444 if (!the_lnet.ln_routing)
2447 if (msg->msg_rxpeer->lpni_rtrcredits <= 0 ||
2448 lnet_msg2bufpool(msg)->rbp_credits <= 0) {
2449 if (ni->ni_net->net_lnd->lnd_eager_recv == NULL) {
2450 msg->msg_rx_ready_delay = 1;
2452 lnet_net_unlock(msg->msg_rx_cpt);
2453 rc = lnet_ni_eager_recv(ni, msg);
2454 lnet_net_lock(msg->msg_rx_cpt);
2459 rc = lnet_post_routed_recv_locked(msg, 0);
2464 lnet_parse_local(struct lnet_ni *ni, struct lnet_msg *msg)
2468 switch (msg->msg_type) {
2470 rc = lnet_parse_ack(ni, msg);
2473 rc = lnet_parse_put(ni, msg);
2476 rc = lnet_parse_get(ni, msg, msg->msg_rdma_get);
2478 case LNET_MSG_REPLY:
2479 rc = lnet_parse_reply(ni, msg);
2481 default: /* prevent an unused label if !kernel */
2486 LASSERT(rc == 0 || rc == -ENOENT);
2491 lnet_msgtyp2str (int type)
2500 case LNET_MSG_REPLY:
2502 case LNET_MSG_HELLO:
2505 return ("<UNKNOWN>");
2510 lnet_print_hdr(struct lnet_hdr *hdr)
2512 struct lnet_process_id src = {
2513 .nid = hdr->src_nid,
2514 .pid = hdr->src_pid,
2516 struct lnet_process_id dst = {
2517 .nid = hdr->dest_nid,
2518 .pid = hdr->dest_pid,
2520 char *type_str = lnet_msgtyp2str(hdr->type);
2522 CWARN("P3 Header at %p of type %s\n", hdr, type_str);
2523 CWARN(" From %s\n", libcfs_id2str(src));
2524 CWARN(" To %s\n", libcfs_id2str(dst));
2526 switch (hdr->type) {
2531 CWARN(" Ptl index %d, ack md %#llx.%#llx, "
2532 "match bits %llu\n",
2533 hdr->msg.put.ptl_index,
2534 hdr->msg.put.ack_wmd.wh_interface_cookie,
2535 hdr->msg.put.ack_wmd.wh_object_cookie,
2536 hdr->msg.put.match_bits);
2537 CWARN(" Length %d, offset %d, hdr data %#llx\n",
2538 hdr->payload_length, hdr->msg.put.offset,
2539 hdr->msg.put.hdr_data);
2543 CWARN(" Ptl index %d, return md %#llx.%#llx, "
2544 "match bits %llu\n", hdr->msg.get.ptl_index,
2545 hdr->msg.get.return_wmd.wh_interface_cookie,
2546 hdr->msg.get.return_wmd.wh_object_cookie,
2547 hdr->msg.get.match_bits);
2548 CWARN(" Length %d, src offset %d\n",
2549 hdr->msg.get.sink_length,
2550 hdr->msg.get.src_offset);
2554 CWARN(" dst md %#llx.%#llx, "
2555 "manipulated length %d\n",
2556 hdr->msg.ack.dst_wmd.wh_interface_cookie,
2557 hdr->msg.ack.dst_wmd.wh_object_cookie,
2558 hdr->msg.ack.mlength);
2561 case LNET_MSG_REPLY:
2562 CWARN(" dst md %#llx.%#llx, "
2564 hdr->msg.reply.dst_wmd.wh_interface_cookie,
2565 hdr->msg.reply.dst_wmd.wh_object_cookie,
2566 hdr->payload_length);
2572 lnet_parse(struct lnet_ni *ni, struct lnet_hdr *hdr, lnet_nid_t from_nid,
2573 void *private, int rdma_req)
2578 struct lnet_msg *msg;
2579 lnet_pid_t dest_pid;
2580 lnet_nid_t dest_nid;
2582 struct lnet_peer_ni *lpni;
2583 __u32 payload_length;
2586 LASSERT (!in_interrupt ());
2588 type = le32_to_cpu(hdr->type);
2589 src_nid = le64_to_cpu(hdr->src_nid);
2590 dest_nid = le64_to_cpu(hdr->dest_nid);
2591 dest_pid = le32_to_cpu(hdr->dest_pid);
2592 payload_length = le32_to_cpu(hdr->payload_length);
2594 for_me = (ni->ni_nid == dest_nid);
2595 cpt = lnet_cpt_of_nid(from_nid, ni);
2597 CDEBUG(D_NET, "TRACE: %s(%s) <- %s : %s - %s\n",
2598 libcfs_nid2str(dest_nid),
2599 libcfs_nid2str(ni->ni_nid),
2600 libcfs_nid2str(src_nid),
2601 lnet_msgtyp2str(type),
2602 (for_me) ? "for me" : "routed");
2607 if (payload_length > 0) {
2608 CERROR("%s, src %s: bad %s payload %d (0 expected)\n",
2609 libcfs_nid2str(from_nid),
2610 libcfs_nid2str(src_nid),
2611 lnet_msgtyp2str(type), payload_length);
2617 case LNET_MSG_REPLY:
2618 if (payload_length >
2619 (__u32)(for_me ? LNET_MAX_PAYLOAD : LNET_MTU)) {
2620 CERROR("%s, src %s: bad %s payload %d "
2621 "(%d max expected)\n",
2622 libcfs_nid2str(from_nid),
2623 libcfs_nid2str(src_nid),
2624 lnet_msgtyp2str(type),
2626 for_me ? LNET_MAX_PAYLOAD : LNET_MTU);
2632 CERROR("%s, src %s: Bad message type 0x%x\n",
2633 libcfs_nid2str(from_nid),
2634 libcfs_nid2str(src_nid), type);
2638 if (the_lnet.ln_routing &&
2639 ni->ni_last_alive != ktime_get_real_seconds()) {
2640 /* NB: so far here is the only place to set NI status to "up */
2642 ni->ni_last_alive = ktime_get_real_seconds();
2643 if (ni->ni_status != NULL &&
2644 ni->ni_status->ns_status == LNET_NI_STATUS_DOWN)
2645 ni->ni_status->ns_status = LNET_NI_STATUS_UP;
2649 /* Regard a bad destination NID as a protocol error. Senders should
2650 * know what they're doing; if they don't they're misconfigured, buggy
2651 * or malicious so we chop them off at the knees :) */
2654 if (LNET_NIDNET(dest_nid) == LNET_NIDNET(ni->ni_nid)) {
2655 /* should have gone direct */
2656 CERROR("%s, src %s: Bad dest nid %s "
2657 "(should have been sent direct)\n",
2658 libcfs_nid2str(from_nid),
2659 libcfs_nid2str(src_nid),
2660 libcfs_nid2str(dest_nid));
2664 if (lnet_islocalnid(dest_nid)) {
2665 /* dest is another local NI; sender should have used
2666 * this node's NID on its own network */
2667 CERROR("%s, src %s: Bad dest nid %s "
2668 "(it's my nid but on a different network)\n",
2669 libcfs_nid2str(from_nid),
2670 libcfs_nid2str(src_nid),
2671 libcfs_nid2str(dest_nid));
2675 if (rdma_req && type == LNET_MSG_GET) {
2676 CERROR("%s, src %s: Bad optimized GET for %s "
2677 "(final destination must be me)\n",
2678 libcfs_nid2str(from_nid),
2679 libcfs_nid2str(src_nid),
2680 libcfs_nid2str(dest_nid));
2684 if (!the_lnet.ln_routing) {
2685 CERROR("%s, src %s: Dropping message for %s "
2686 "(routing not enabled)\n",
2687 libcfs_nid2str(from_nid),
2688 libcfs_nid2str(src_nid),
2689 libcfs_nid2str(dest_nid));
2694 /* Message looks OK; we're not going to return an error, so we MUST
2695 * call back lnd_recv() come what may... */
2697 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
2698 fail_peer(src_nid, 0)) { /* shall we now? */
2699 CERROR("%s, src %s: Dropping %s to simulate failure\n",
2700 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
2701 lnet_msgtyp2str(type));
2705 if (!list_empty(&the_lnet.ln_drop_rules) &&
2706 lnet_drop_rule_match(hdr)) {
2707 CDEBUG(D_NET, "%s, src %s, dst %s: Dropping %s to simulate"
2708 "silent message loss\n",
2709 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
2710 libcfs_nid2str(dest_nid), lnet_msgtyp2str(type));
2715 msg = lnet_msg_alloc();
2717 CERROR("%s, src %s: Dropping %s (out of memory)\n",
2718 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
2719 lnet_msgtyp2str(type));
2723 /* msg zeroed in lnet_msg_alloc; i.e. flags all clear,
2724 * pointers NULL etc */
2726 msg->msg_type = type;
2727 msg->msg_private = private;
2728 msg->msg_receiving = 1;
2729 msg->msg_rdma_get = rdma_req;
2730 msg->msg_len = msg->msg_wanted = payload_length;
2731 msg->msg_offset = 0;
2732 msg->msg_hdr = *hdr;
2733 /* for building message event */
2734 msg->msg_from = from_nid;
2736 msg->msg_target.pid = dest_pid;
2737 msg->msg_target.nid = dest_nid;
2738 msg->msg_routing = 1;
2741 /* convert common msg->hdr fields to host byteorder */
2742 msg->msg_hdr.type = type;
2743 msg->msg_hdr.src_nid = src_nid;
2744 msg->msg_hdr.src_pid = le32_to_cpu(msg->msg_hdr.src_pid);
2745 msg->msg_hdr.dest_nid = dest_nid;
2746 msg->msg_hdr.dest_pid = dest_pid;
2747 msg->msg_hdr.payload_length = payload_length;
2751 lpni = lnet_nid2peerni_locked(from_nid, ni->ni_nid, cpt);
2753 lnet_net_unlock(cpt);
2754 CERROR("%s, src %s: Dropping %s "
2755 "(error %ld looking up sender)\n",
2756 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
2757 lnet_msgtyp2str(type), PTR_ERR(lpni));
2759 if (rc == -ESHUTDOWN)
2760 /* We are shutting down. Don't do anything more */
2764 msg->msg_rxpeer = lpni;
2766 lnet_ni_addref_locked(ni, cpt);
2767 /* Multi-Rail: Primary NID of source. */
2768 msg->msg_initiator = lnet_peer_primary_nid_locked(src_nid);
2770 if (lnet_isrouter(msg->msg_rxpeer)) {
2771 lnet_peer_set_alive(msg->msg_rxpeer);
2772 if (avoid_asym_router_failure &&
2773 LNET_NIDNET(src_nid) != LNET_NIDNET(from_nid)) {
2774 /* received a remote message from router, update
2775 * remote NI status on this router.
2776 * NB: multi-hop routed message will be ignored.
2778 lnet_router_ni_update_locked(msg->msg_rxpeer,
2779 LNET_NIDNET(src_nid));
2783 lnet_msg_commit(msg, cpt);
2785 /* message delay simulation */
2786 if (unlikely(!list_empty(&the_lnet.ln_delay_rules) &&
2787 lnet_delay_rule_match_locked(hdr, msg))) {
2788 lnet_net_unlock(cpt);
2793 rc = lnet_parse_forward_locked(ni, msg);
2794 lnet_net_unlock(cpt);
2799 if (rc == LNET_CREDIT_OK) {
2800 lnet_ni_recv(ni, msg->msg_private, msg, 0,
2801 0, payload_length, payload_length);
2806 lnet_net_unlock(cpt);
2808 rc = lnet_parse_local(ni, msg);
2814 LASSERT(msg->msg_md == NULL);
2815 lnet_finalize(msg, rc);
2818 lnet_drop_message(ni, cpt, private, payload_length, type);
2821 EXPORT_SYMBOL(lnet_parse);
2824 lnet_drop_delayed_msg_list(struct list_head *head, char *reason)
2826 while (!list_empty(head)) {
2827 struct lnet_process_id id = {0};
2828 struct lnet_msg *msg;
2830 msg = list_entry(head->next, struct lnet_msg, msg_list);
2831 list_del(&msg->msg_list);
2833 id.nid = msg->msg_hdr.src_nid;
2834 id.pid = msg->msg_hdr.src_pid;
2836 LASSERT(msg->msg_md == NULL);
2837 LASSERT(msg->msg_rx_delayed);
2838 LASSERT(msg->msg_rxpeer != NULL);
2839 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
2841 CWARN("Dropping delayed PUT from %s portal %d match %llu"
2842 " offset %d length %d: %s\n",
2844 msg->msg_hdr.msg.put.ptl_index,
2845 msg->msg_hdr.msg.put.match_bits,
2846 msg->msg_hdr.msg.put.offset,
2847 msg->msg_hdr.payload_length, reason);
2849 /* NB I can't drop msg's ref on msg_rxpeer until after I've
2850 * called lnet_drop_message(), so I just hang onto msg as well
2851 * until that's done */
2853 lnet_drop_message(msg->msg_rxni, msg->msg_rx_cpt,
2854 msg->msg_private, msg->msg_len,
2857 * NB: message will not generate event because w/o attached MD,
2858 * but we still should give error code so lnet_msg_decommit()
2859 * can skip counters operations and other checks.
2861 lnet_finalize(msg, -ENOENT);
2866 lnet_recv_delayed_msg_list(struct list_head *head)
2868 while (!list_empty(head)) {
2869 struct lnet_msg *msg;
2870 struct lnet_process_id id;
2872 msg = list_entry(head->next, struct lnet_msg, msg_list);
2873 list_del(&msg->msg_list);
2875 /* md won't disappear under me, since each msg
2876 * holds a ref on it */
2878 id.nid = msg->msg_hdr.src_nid;
2879 id.pid = msg->msg_hdr.src_pid;
2881 LASSERT(msg->msg_rx_delayed);
2882 LASSERT(msg->msg_md != NULL);
2883 LASSERT(msg->msg_rxpeer != NULL);
2884 LASSERT(msg->msg_rxni != NULL);
2885 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
2887 CDEBUG(D_NET, "Resuming delayed PUT from %s portal %d "
2888 "match %llu offset %d length %d.\n",
2889 libcfs_id2str(id), msg->msg_hdr.msg.put.ptl_index,
2890 msg->msg_hdr.msg.put.match_bits,
2891 msg->msg_hdr.msg.put.offset,
2892 msg->msg_hdr.payload_length);
2894 lnet_recv_put(msg->msg_rxni, msg);
2899 * Initiate an asynchronous PUT operation.
2901 * There are several events associated with a PUT: completion of the send on
2902 * the initiator node (LNET_EVENT_SEND), and when the send completes
2903 * successfully, the receipt of an acknowledgment (LNET_EVENT_ACK) indicating
2904 * that the operation was accepted by the target. The event LNET_EVENT_PUT is
2905 * used at the target node to indicate the completion of incoming data
2908 * The local events will be logged in the EQ associated with the MD pointed to
2909 * by \a mdh handle. Using a MD without an associated EQ results in these
2910 * events being discarded. In this case, the caller must have another
2911 * mechanism (e.g., a higher level protocol) for determining when it is safe
2912 * to modify the memory region associated with the MD.
2914 * Note that LNet does not guarantee the order of LNET_EVENT_SEND and
2915 * LNET_EVENT_ACK, though intuitively ACK should happen after SEND.
2917 * \param self Indicates the NID of a local interface through which to send
2918 * the PUT request. Use LNET_NID_ANY to let LNet choose one by itself.
2919 * \param mdh A handle for the MD that describes the memory to be sent. The MD
2920 * must be "free floating" (See LNetMDBind()).
2921 * \param ack Controls whether an acknowledgment is requested.
2922 * Acknowledgments are only sent when they are requested by the initiating
2923 * process and the target MD enables them.
2924 * \param target A process identifier for the target process.
2925 * \param portal The index in the \a target's portal table.
2926 * \param match_bits The match bits to use for MD selection at the target
2928 * \param offset The offset into the target MD (only used when the target
2929 * MD has the LNET_MD_MANAGE_REMOTE option set).
2930 * \param hdr_data 64 bits of user data that can be included in the message
2931 * header. This data is written to an event queue entry at the target if an
2932 * EQ is present on the matching MD.
2934 * \retval 0 Success, and only in this case events will be generated
2935 * and logged to EQ (if it exists).
2936 * \retval -EIO Simulated failure.
2937 * \retval -ENOMEM Memory allocation failure.
2938 * \retval -ENOENT Invalid MD object.
2940 * \see struct lnet_event::hdr_data and lnet_event_kind_t.
2943 LNetPut(lnet_nid_t self, struct lnet_handle_md mdh, enum lnet_ack_req ack,
2944 struct lnet_process_id target, unsigned int portal,
2945 __u64 match_bits, unsigned int offset,
2948 struct lnet_msg *msg;
2949 struct lnet_libmd *md;
2953 LASSERT(the_lnet.ln_refcount > 0);
2955 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
2956 fail_peer(target.nid, 1)) { /* shall we now? */
2957 CERROR("Dropping PUT to %s: simulated failure\n",
2958 libcfs_id2str(target));
2962 msg = lnet_msg_alloc();
2964 CERROR("Dropping PUT to %s: ENOMEM on struct lnet_msg\n",
2965 libcfs_id2str(target));
2968 msg->msg_vmflush = !!memory_pressure_get();
2970 cpt = lnet_cpt_of_cookie(mdh.cookie);
2973 md = lnet_handle2md(&mdh);
2974 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
2975 CERROR("Dropping PUT (%llu:%d:%s): MD (%d) invalid\n",
2976 match_bits, portal, libcfs_id2str(target),
2977 md == NULL ? -1 : md->md_threshold);
2978 if (md != NULL && md->md_me != NULL)
2979 CERROR("Source MD also attached to portal %d\n",
2980 md->md_me->me_portal);
2981 lnet_res_unlock(cpt);
2987 CDEBUG(D_NET, "LNetPut -> %s\n", libcfs_id2str(target));
2989 lnet_msg_attach_md(msg, md, 0, 0);
2991 lnet_prep_send(msg, LNET_MSG_PUT, target, 0, md->md_length);
2993 msg->msg_hdr.msg.put.match_bits = cpu_to_le64(match_bits);
2994 msg->msg_hdr.msg.put.ptl_index = cpu_to_le32(portal);
2995 msg->msg_hdr.msg.put.offset = cpu_to_le32(offset);
2996 msg->msg_hdr.msg.put.hdr_data = hdr_data;
2998 /* NB handles only looked up by creator (no flips) */
2999 if (ack == LNET_ACK_REQ) {
3000 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
3001 the_lnet.ln_interface_cookie;
3002 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
3003 md->md_lh.lh_cookie;
3005 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
3006 LNET_WIRE_HANDLE_COOKIE_NONE;
3007 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
3008 LNET_WIRE_HANDLE_COOKIE_NONE;
3011 lnet_res_unlock(cpt);
3013 lnet_build_msg_event(msg, LNET_EVENT_SEND);
3015 rc = lnet_send(self, msg, LNET_NID_ANY);
3017 CNETERR("Error sending PUT to %s: %d\n",
3018 libcfs_id2str(target), rc);
3019 lnet_finalize(msg, rc);
3022 /* completion will be signalled by an event */
3025 EXPORT_SYMBOL(LNetPut);
3028 * The LND can DMA direct to the GET md (i.e. no REPLY msg). This
3029 * returns a msg for the LND to pass to lnet_finalize() when the sink
3030 * data has been received.
3032 * CAVEAT EMPTOR: 'getmsg' is the original GET, which is freed when
3033 * lnet_finalize() is called on it, so the LND must call this first
3036 lnet_create_reply_msg(struct lnet_ni *ni, struct lnet_msg *getmsg)
3038 struct lnet_msg *msg = lnet_msg_alloc();
3039 struct lnet_libmd *getmd = getmsg->msg_md;
3040 struct lnet_process_id peer_id = getmsg->msg_target;
3043 LASSERT(!getmsg->msg_target_is_router);
3044 LASSERT(!getmsg->msg_routing);
3047 CERROR("%s: Dropping REPLY from %s: can't allocate msg\n",
3048 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id));
3052 cpt = lnet_cpt_of_cookie(getmd->md_lh.lh_cookie);
3055 LASSERT(getmd->md_refcount > 0);
3057 if (getmd->md_threshold == 0) {
3058 CERROR("%s: Dropping REPLY from %s for inactive MD %p\n",
3059 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id),
3061 lnet_res_unlock(cpt);
3065 LASSERT(getmd->md_offset == 0);
3067 CDEBUG(D_NET, "%s: Reply from %s md %p\n",
3068 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id), getmd);
3070 /* setup information for lnet_build_msg_event */
3071 msg->msg_initiator = getmsg->msg_txpeer->lpni_peer_net->lpn_peer->lp_primary_nid;
3072 msg->msg_from = peer_id.nid;
3073 msg->msg_type = LNET_MSG_GET; /* flag this msg as an "optimized" GET */
3074 msg->msg_hdr.src_nid = peer_id.nid;
3075 msg->msg_hdr.payload_length = getmd->md_length;
3076 msg->msg_receiving = 1; /* required by lnet_msg_attach_md */
3078 lnet_msg_attach_md(msg, getmd, getmd->md_offset, getmd->md_length);
3079 lnet_res_unlock(cpt);
3081 cpt = lnet_cpt_of_nid(peer_id.nid, ni);
3084 lnet_msg_commit(msg, cpt);
3085 lnet_net_unlock(cpt);
3087 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
3092 cpt = lnet_cpt_of_nid(peer_id.nid, ni);
3095 lnet_incr_stats(&ni->ni_stats, LNET_MSG_GET, LNET_STATS_TYPE_DROP);
3096 the_lnet.ln_counters[cpt]->drop_count++;
3097 the_lnet.ln_counters[cpt]->drop_length += getmd->md_length;
3098 lnet_net_unlock(cpt);
3105 EXPORT_SYMBOL(lnet_create_reply_msg);
3108 lnet_set_reply_msg_len(struct lnet_ni *ni, struct lnet_msg *reply,
3111 /* Set the REPLY length, now the RDMA that elides the REPLY message has
3112 * completed and I know it. */
3113 LASSERT(reply != NULL);
3114 LASSERT(reply->msg_type == LNET_MSG_GET);
3115 LASSERT(reply->msg_ev.type == LNET_EVENT_REPLY);
3117 /* NB I trusted my peer to RDMA. If she tells me she's written beyond
3118 * the end of my buffer, I might as well be dead. */
3119 LASSERT(len <= reply->msg_ev.mlength);
3121 reply->msg_ev.mlength = len;
3123 EXPORT_SYMBOL(lnet_set_reply_msg_len);
3126 * Initiate an asynchronous GET operation.
3128 * On the initiator node, an LNET_EVENT_SEND is logged when the GET request
3129 * is sent, and an LNET_EVENT_REPLY is logged when the data returned from
3130 * the target node in the REPLY has been written to local MD.
3132 * On the target node, an LNET_EVENT_GET is logged when the GET request
3133 * arrives and is accepted into a MD.
3135 * \param self,target,portal,match_bits,offset See the discussion in LNetPut().
3136 * \param mdh A handle for the MD that describes the memory into which the
3137 * requested data will be received. The MD must be "free floating" (See LNetMDBind()).
3139 * \retval 0 Success, and only in this case events will be generated
3140 * and logged to EQ (if it exists) of the MD.
3141 * \retval -EIO Simulated failure.
3142 * \retval -ENOMEM Memory allocation failure.
3143 * \retval -ENOENT Invalid MD object.
3146 LNetGet(lnet_nid_t self, struct lnet_handle_md mdh,
3147 struct lnet_process_id target, unsigned int portal,
3148 __u64 match_bits, unsigned int offset)
3150 struct lnet_msg *msg;
3151 struct lnet_libmd *md;
3155 LASSERT(the_lnet.ln_refcount > 0);
3157 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
3158 fail_peer(target.nid, 1)) /* shall we now? */
3160 CERROR("Dropping GET to %s: simulated failure\n",
3161 libcfs_id2str(target));
3165 msg = lnet_msg_alloc();
3167 CERROR("Dropping GET to %s: ENOMEM on struct lnet_msg\n",
3168 libcfs_id2str(target));
3172 cpt = lnet_cpt_of_cookie(mdh.cookie);
3175 md = lnet_handle2md(&mdh);
3176 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
3177 CERROR("Dropping GET (%llu:%d:%s): MD (%d) invalid\n",
3178 match_bits, portal, libcfs_id2str(target),
3179 md == NULL ? -1 : md->md_threshold);
3180 if (md != NULL && md->md_me != NULL)
3181 CERROR("REPLY MD also attached to portal %d\n",
3182 md->md_me->me_portal);
3184 lnet_res_unlock(cpt);
3190 CDEBUG(D_NET, "LNetGet -> %s\n", libcfs_id2str(target));
3192 lnet_msg_attach_md(msg, md, 0, 0);
3194 lnet_prep_send(msg, LNET_MSG_GET, target, 0, 0);
3196 msg->msg_hdr.msg.get.match_bits = cpu_to_le64(match_bits);
3197 msg->msg_hdr.msg.get.ptl_index = cpu_to_le32(portal);
3198 msg->msg_hdr.msg.get.src_offset = cpu_to_le32(offset);
3199 msg->msg_hdr.msg.get.sink_length = cpu_to_le32(md->md_length);
3201 /* NB handles only looked up by creator (no flips) */
3202 msg->msg_hdr.msg.get.return_wmd.wh_interface_cookie =
3203 the_lnet.ln_interface_cookie;
3204 msg->msg_hdr.msg.get.return_wmd.wh_object_cookie =
3205 md->md_lh.lh_cookie;
3207 lnet_res_unlock(cpt);
3209 lnet_build_msg_event(msg, LNET_EVENT_SEND);
3211 rc = lnet_send(self, msg, LNET_NID_ANY);
3213 CNETERR("Error sending GET to %s: %d\n",
3214 libcfs_id2str(target), rc);
3215 lnet_finalize(msg, rc);
3218 /* completion will be signalled by an event */
3221 EXPORT_SYMBOL(LNetGet);
3224 * Calculate distance to node at \a dstnid.
3226 * \param dstnid Target NID.
3227 * \param srcnidp If not NULL, NID of the local interface to reach \a dstnid
3229 * \param orderp If not NULL, order of the route to reach \a dstnid is saved
3232 * \retval 0 If \a dstnid belongs to a local interface, and reserved option
3233 * local_nid_dist_zero is set, which is the default.
3234 * \retval positives Distance to target NID, i.e. number of hops plus one.
3235 * \retval -EHOSTUNREACH If \a dstnid is not reachable.
3238 LNetDist(lnet_nid_t dstnid, lnet_nid_t *srcnidp, __u32 *orderp)
3240 struct list_head *e;
3241 struct lnet_ni *ni = NULL;
3242 struct lnet_remotenet *rnet;
3243 __u32 dstnet = LNET_NIDNET(dstnid);
3247 struct list_head *rn_list;
3249 /* if !local_nid_dist_zero, I don't return a distance of 0 ever
3250 * (when lustre sees a distance of 0, it substitutes 0@lo), so I
3251 * keep order 0 free for 0@lo and order 1 free for a local NID
3254 LASSERT(the_lnet.ln_refcount > 0);
3256 cpt = lnet_net_lock_current();
3258 while ((ni = lnet_get_next_ni_locked(NULL, ni))) {
3259 if (ni->ni_nid == dstnid) {
3260 if (srcnidp != NULL)
3262 if (orderp != NULL) {
3263 if (LNET_NETTYP(LNET_NIDNET(dstnid)) == LOLND)
3268 lnet_net_unlock(cpt);
3270 return local_nid_dist_zero ? 0 : 1;
3273 if (LNET_NIDNET(ni->ni_nid) == dstnet) {
3274 /* Check if ni was originally created in
3275 * current net namespace.
3276 * If not, assign order above 0xffff0000,
3277 * to make this ni not a priority. */
3278 if (!net_eq(ni->ni_net_ns, current->nsproxy->net_ns))
3279 order += 0xffff0000;
3281 if (srcnidp != NULL)
3282 *srcnidp = ni->ni_nid;
3285 lnet_net_unlock(cpt);
3292 rn_list = lnet_net2rnethash(dstnet);
3293 list_for_each(e, rn_list) {
3294 rnet = list_entry(e, struct lnet_remotenet, lrn_list);
3296 if (rnet->lrn_net == dstnet) {
3297 struct lnet_route *route;
3298 struct lnet_route *shortest = NULL;
3299 __u32 shortest_hops = LNET_UNDEFINED_HOPS;
3302 LASSERT(!list_empty(&rnet->lrn_routes));
3304 list_for_each_entry(route, &rnet->lrn_routes,
3306 route_hops = route->lr_hops;
3307 if (route_hops == LNET_UNDEFINED_HOPS)
3309 if (shortest == NULL ||
3310 route_hops < shortest_hops) {
3312 shortest_hops = route_hops;
3316 LASSERT(shortest != NULL);
3317 hops = shortest_hops;
3318 if (srcnidp != NULL) {
3319 ni = lnet_get_next_ni_locked(
3320 shortest->lr_gateway->lpni_net,
3322 *srcnidp = ni->ni_nid;
3326 lnet_net_unlock(cpt);
3332 lnet_net_unlock(cpt);
3333 return -EHOSTUNREACH;
3335 EXPORT_SYMBOL(LNetDist);