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 struct lnet_send_data {
50 struct lnet_ni *sd_best_ni;
51 struct lnet_peer_ni *sd_best_lpni;
52 struct lnet_peer_ni *sd_final_dst_lpni;
53 struct lnet_peer *sd_peer;
54 struct lnet_peer *sd_gw_peer;
55 struct lnet_peer_ni *sd_gw_lpni;
56 struct lnet_peer_net *sd_peer_net;
57 struct lnet_msg *sd_msg;
58 lnet_nid_t sd_dst_nid;
59 lnet_nid_t sd_src_nid;
60 lnet_nid_t sd_rtr_nid;
66 static inline struct lnet_comm_count *
67 get_stats_counts(struct lnet_element_stats *stats,
68 enum lnet_stats_type stats_type)
71 case LNET_STATS_TYPE_SEND:
72 return &stats->el_send_stats;
73 case LNET_STATS_TYPE_RECV:
74 return &stats->el_recv_stats;
75 case LNET_STATS_TYPE_DROP:
76 return &stats->el_drop_stats;
78 CERROR("Unknown stats type\n");
84 void lnet_incr_stats(struct lnet_element_stats *stats,
85 enum lnet_msg_type msg_type,
86 enum lnet_stats_type stats_type)
88 struct lnet_comm_count *counts = get_stats_counts(stats, stats_type);
94 atomic_inc(&counts->co_ack_count);
97 atomic_inc(&counts->co_put_count);
100 atomic_inc(&counts->co_get_count);
103 atomic_inc(&counts->co_reply_count);
106 atomic_inc(&counts->co_hello_count);
109 CERROR("There is a BUG in the code. Unknown message type\n");
114 __u32 lnet_sum_stats(struct lnet_element_stats *stats,
115 enum lnet_stats_type stats_type)
117 struct lnet_comm_count *counts = get_stats_counts(stats, stats_type);
121 return (atomic_read(&counts->co_ack_count) +
122 atomic_read(&counts->co_put_count) +
123 atomic_read(&counts->co_get_count) +
124 atomic_read(&counts->co_reply_count) +
125 atomic_read(&counts->co_hello_count));
128 static inline void assign_stats(struct lnet_ioctl_comm_count *msg_stats,
129 struct lnet_comm_count *counts)
131 msg_stats->ico_get_count = atomic_read(&counts->co_get_count);
132 msg_stats->ico_put_count = atomic_read(&counts->co_put_count);
133 msg_stats->ico_reply_count = atomic_read(&counts->co_reply_count);
134 msg_stats->ico_ack_count = atomic_read(&counts->co_ack_count);
135 msg_stats->ico_hello_count = atomic_read(&counts->co_hello_count);
138 void lnet_usr_translate_stats(struct lnet_ioctl_element_msg_stats *msg_stats,
139 struct lnet_element_stats *stats)
141 struct lnet_comm_count *counts;
146 counts = get_stats_counts(stats, LNET_STATS_TYPE_SEND);
149 assign_stats(&msg_stats->im_send_stats, counts);
151 counts = get_stats_counts(stats, LNET_STATS_TYPE_RECV);
154 assign_stats(&msg_stats->im_recv_stats, counts);
156 counts = get_stats_counts(stats, LNET_STATS_TYPE_DROP);
159 assign_stats(&msg_stats->im_drop_stats, counts);
163 lnet_fail_nid(lnet_nid_t nid, unsigned int threshold)
165 struct lnet_test_peer *tp;
166 struct list_head *el;
167 struct list_head *next;
168 struct list_head cull;
170 /* NB: use lnet_net_lock(0) to serialize operations on test peers */
171 if (threshold != 0) {
172 /* Adding a new entry */
173 LIBCFS_ALLOC(tp, sizeof(*tp));
178 tp->tp_threshold = threshold;
181 list_add_tail(&tp->tp_list, &the_lnet.ln_test_peers);
186 /* removing entries */
187 INIT_LIST_HEAD(&cull);
191 list_for_each_safe(el, next, &the_lnet.ln_test_peers) {
192 tp = list_entry(el, struct lnet_test_peer, tp_list);
194 if (tp->tp_threshold == 0 || /* needs culling anyway */
195 nid == LNET_NID_ANY || /* removing all entries */
196 tp->tp_nid == nid) { /* matched this one */
197 list_del(&tp->tp_list);
198 list_add(&tp->tp_list, &cull);
204 while (!list_empty(&cull)) {
205 tp = list_entry(cull.next, struct lnet_test_peer, tp_list);
207 list_del(&tp->tp_list);
208 LIBCFS_FREE(tp, sizeof(*tp));
214 fail_peer (lnet_nid_t nid, int outgoing)
216 struct lnet_test_peer *tp;
217 struct list_head *el;
218 struct list_head *next;
219 struct list_head cull;
222 INIT_LIST_HEAD(&cull);
224 /* NB: use lnet_net_lock(0) to serialize operations on test peers */
227 list_for_each_safe(el, next, &the_lnet.ln_test_peers) {
228 tp = list_entry(el, struct lnet_test_peer, tp_list);
230 if (tp->tp_threshold == 0) {
233 /* only cull zombies on outgoing tests,
234 * since we may be at interrupt priority on
235 * incoming messages. */
236 list_del(&tp->tp_list);
237 list_add(&tp->tp_list, &cull);
242 if (tp->tp_nid == LNET_NID_ANY || /* fail every peer */
243 nid == tp->tp_nid) { /* fail this peer */
246 if (tp->tp_threshold != LNET_MD_THRESH_INF) {
249 tp->tp_threshold == 0) {
251 list_del(&tp->tp_list);
252 list_add(&tp->tp_list, &cull);
261 while (!list_empty(&cull)) {
262 tp = list_entry(cull.next, struct lnet_test_peer, tp_list);
263 list_del(&tp->tp_list);
265 LIBCFS_FREE(tp, sizeof(*tp));
272 lnet_iov_nob(unsigned int niov, struct kvec *iov)
274 unsigned int nob = 0;
276 LASSERT(niov == 0 || iov != NULL);
278 nob += (iov++)->iov_len;
282 EXPORT_SYMBOL(lnet_iov_nob);
285 lnet_copy_iov2iov(unsigned int ndiov, struct kvec *diov, unsigned int doffset,
286 unsigned int nsiov, struct kvec *siov, unsigned int soffset,
289 /* NB diov, siov are READ-ONLY */
290 unsigned int this_nob;
295 /* skip complete frags before 'doffset' */
297 while (doffset >= diov->iov_len) {
298 doffset -= diov->iov_len;
304 /* skip complete frags before 'soffset' */
306 while (soffset >= siov->iov_len) {
307 soffset -= siov->iov_len;
316 this_nob = MIN(diov->iov_len - doffset,
317 siov->iov_len - soffset);
318 this_nob = MIN(this_nob, nob);
320 memcpy((char *)diov->iov_base + doffset,
321 (char *)siov->iov_base + soffset, this_nob);
324 if (diov->iov_len > doffset + this_nob) {
332 if (siov->iov_len > soffset + this_nob) {
341 EXPORT_SYMBOL(lnet_copy_iov2iov);
344 lnet_extract_iov(int dst_niov, struct kvec *dst,
345 int src_niov, struct kvec *src,
346 unsigned int offset, unsigned int len)
348 /* Initialise 'dst' to the subset of 'src' starting at 'offset',
349 * for exactly 'len' bytes, and return the number of entries.
350 * NB not destructive to 'src' */
351 unsigned int frag_len;
354 if (len == 0) /* no data => */
355 return (0); /* no frags */
357 LASSERT(src_niov > 0);
358 while (offset >= src->iov_len) { /* skip initial frags */
359 offset -= src->iov_len;
362 LASSERT(src_niov > 0);
367 LASSERT(src_niov > 0);
368 LASSERT((int)niov <= dst_niov);
370 frag_len = src->iov_len - offset;
371 dst->iov_base = ((char *)src->iov_base) + offset;
373 if (len <= frag_len) {
378 dst->iov_len = frag_len;
388 EXPORT_SYMBOL(lnet_extract_iov);
392 lnet_kiov_nob(unsigned int niov, lnet_kiov_t *kiov)
394 unsigned int nob = 0;
396 LASSERT(niov == 0 || kiov != NULL);
398 nob += (kiov++)->kiov_len;
402 EXPORT_SYMBOL(lnet_kiov_nob);
405 lnet_copy_kiov2kiov(unsigned int ndiov, lnet_kiov_t *diov, unsigned int doffset,
406 unsigned int nsiov, lnet_kiov_t *siov, unsigned int soffset,
409 /* NB diov, siov are READ-ONLY */
410 unsigned int this_nob;
417 LASSERT (!in_interrupt ());
420 while (doffset >= diov->kiov_len) {
421 doffset -= diov->kiov_len;
428 while (soffset >= siov->kiov_len) {
429 soffset -= siov->kiov_len;
438 this_nob = MIN(diov->kiov_len - doffset,
439 siov->kiov_len - soffset);
440 this_nob = MIN(this_nob, nob);
443 daddr = ((char *)kmap(diov->kiov_page)) +
444 diov->kiov_offset + doffset;
446 saddr = ((char *)kmap(siov->kiov_page)) +
447 siov->kiov_offset + soffset;
449 /* Vanishing risk of kmap deadlock when mapping 2 pages.
450 * However in practice at least one of the kiovs will be mapped
451 * kernel pages and the map/unmap will be NOOPs */
453 memcpy (daddr, saddr, this_nob);
456 if (diov->kiov_len > doffset + this_nob) {
460 kunmap(diov->kiov_page);
467 if (siov->kiov_len > soffset + this_nob) {
471 kunmap(siov->kiov_page);
480 kunmap(diov->kiov_page);
482 kunmap(siov->kiov_page);
484 EXPORT_SYMBOL(lnet_copy_kiov2kiov);
487 lnet_copy_kiov2iov (unsigned int niov, struct kvec *iov, unsigned int iovoffset,
488 unsigned int nkiov, lnet_kiov_t *kiov, unsigned int kiovoffset,
491 /* NB iov, kiov are READ-ONLY */
492 unsigned int this_nob;
498 LASSERT (!in_interrupt ());
501 while (iovoffset >= iov->iov_len) {
502 iovoffset -= iov->iov_len;
509 while (kiovoffset >= kiov->kiov_len) {
510 kiovoffset -= kiov->kiov_len;
519 this_nob = MIN(iov->iov_len - iovoffset,
520 kiov->kiov_len - kiovoffset);
521 this_nob = MIN(this_nob, nob);
524 addr = ((char *)kmap(kiov->kiov_page)) +
525 kiov->kiov_offset + kiovoffset;
527 memcpy((char *)iov->iov_base + iovoffset, addr, this_nob);
530 if (iov->iov_len > iovoffset + this_nob) {
531 iovoffset += this_nob;
538 if (kiov->kiov_len > kiovoffset + this_nob) {
540 kiovoffset += this_nob;
542 kunmap(kiov->kiov_page);
552 kunmap(kiov->kiov_page);
554 EXPORT_SYMBOL(lnet_copy_kiov2iov);
557 lnet_copy_iov2kiov(unsigned int nkiov, lnet_kiov_t *kiov, unsigned int kiovoffset,
558 unsigned int niov, struct kvec *iov, unsigned int iovoffset,
561 /* NB kiov, iov are READ-ONLY */
562 unsigned int this_nob;
568 LASSERT (!in_interrupt ());
571 while (kiovoffset >= kiov->kiov_len) {
572 kiovoffset -= kiov->kiov_len;
579 while (iovoffset >= iov->iov_len) {
580 iovoffset -= iov->iov_len;
589 this_nob = MIN(kiov->kiov_len - kiovoffset,
590 iov->iov_len - iovoffset);
591 this_nob = MIN(this_nob, nob);
594 addr = ((char *)kmap(kiov->kiov_page)) +
595 kiov->kiov_offset + kiovoffset;
597 memcpy (addr, (char *)iov->iov_base + iovoffset, this_nob);
600 if (kiov->kiov_len > kiovoffset + this_nob) {
602 kiovoffset += this_nob;
604 kunmap(kiov->kiov_page);
611 if (iov->iov_len > iovoffset + this_nob) {
612 iovoffset += this_nob;
621 kunmap(kiov->kiov_page);
623 EXPORT_SYMBOL(lnet_copy_iov2kiov);
626 lnet_extract_kiov(int dst_niov, lnet_kiov_t *dst,
627 int src_niov, lnet_kiov_t *src,
628 unsigned int offset, unsigned int len)
630 /* Initialise 'dst' to the subset of 'src' starting at 'offset',
631 * for exactly 'len' bytes, and return the number of entries.
632 * NB not destructive to 'src' */
633 unsigned int frag_len;
636 if (len == 0) /* no data => */
637 return (0); /* no frags */
639 LASSERT(src_niov > 0);
640 while (offset >= src->kiov_len) { /* skip initial frags */
641 offset -= src->kiov_len;
644 LASSERT(src_niov > 0);
649 LASSERT(src_niov > 0);
650 LASSERT((int)niov <= dst_niov);
652 frag_len = src->kiov_len - offset;
653 dst->kiov_page = src->kiov_page;
654 dst->kiov_offset = src->kiov_offset + offset;
656 if (len <= frag_len) {
658 LASSERT(dst->kiov_offset + dst->kiov_len <= PAGE_SIZE);
662 dst->kiov_len = frag_len;
663 LASSERT(dst->kiov_offset + dst->kiov_len <= PAGE_SIZE);
673 EXPORT_SYMBOL(lnet_extract_kiov);
676 lnet_ni_recv(struct lnet_ni *ni, void *private, struct lnet_msg *msg,
677 int delayed, unsigned int offset, unsigned int mlen,
680 unsigned int niov = 0;
681 struct kvec *iov = NULL;
682 lnet_kiov_t *kiov = NULL;
685 LASSERT (!in_interrupt ());
686 LASSERT (mlen == 0 || msg != NULL);
689 LASSERT(msg->msg_receiving);
690 LASSERT(!msg->msg_sending);
691 LASSERT(rlen == msg->msg_len);
692 LASSERT(mlen <= msg->msg_len);
693 LASSERT(msg->msg_offset == offset);
694 LASSERT(msg->msg_wanted == mlen);
696 msg->msg_receiving = 0;
699 niov = msg->msg_niov;
701 kiov = msg->msg_kiov;
704 LASSERT ((iov == NULL) != (kiov == NULL));
708 rc = (ni->ni_net->net_lnd->lnd_recv)(ni, private, msg, delayed,
709 niov, iov, kiov, offset, mlen,
712 lnet_finalize(msg, rc);
716 lnet_setpayloadbuffer(struct lnet_msg *msg)
718 struct lnet_libmd *md = msg->msg_md;
720 LASSERT(msg->msg_len > 0);
721 LASSERT(!msg->msg_routing);
723 LASSERT(msg->msg_niov == 0);
724 LASSERT(msg->msg_iov == NULL);
725 LASSERT(msg->msg_kiov == NULL);
727 msg->msg_niov = md->md_niov;
728 if ((md->md_options & LNET_MD_KIOV) != 0)
729 msg->msg_kiov = md->md_iov.kiov;
731 msg->msg_iov = md->md_iov.iov;
735 lnet_prep_send(struct lnet_msg *msg, int type, struct lnet_process_id target,
736 unsigned int offset, unsigned int len)
738 msg->msg_type = type;
739 msg->msg_target = target;
741 msg->msg_offset = offset;
744 lnet_setpayloadbuffer(msg);
746 memset (&msg->msg_hdr, 0, sizeof (msg->msg_hdr));
747 msg->msg_hdr.type = cpu_to_le32(type);
748 /* dest_nid will be overwritten by lnet_select_pathway() */
749 msg->msg_hdr.dest_nid = cpu_to_le64(target.nid);
750 msg->msg_hdr.dest_pid = cpu_to_le32(target.pid);
751 /* src_nid will be set later */
752 msg->msg_hdr.src_pid = cpu_to_le32(the_lnet.ln_pid);
753 msg->msg_hdr.payload_length = cpu_to_le32(len);
757 lnet_ni_send(struct lnet_ni *ni, struct lnet_msg *msg)
759 void *priv = msg->msg_private;
762 LASSERT (!in_interrupt ());
763 LASSERT (LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND ||
764 (msg->msg_txcredit && msg->msg_peertxcredit));
766 rc = (ni->ni_net->net_lnd->lnd_send)(ni, priv, msg);
768 lnet_finalize(msg, rc);
772 lnet_ni_eager_recv(struct lnet_ni *ni, struct lnet_msg *msg)
776 LASSERT(!msg->msg_sending);
777 LASSERT(msg->msg_receiving);
778 LASSERT(!msg->msg_rx_ready_delay);
779 LASSERT(ni->ni_net->net_lnd->lnd_eager_recv != NULL);
781 msg->msg_rx_ready_delay = 1;
782 rc = (ni->ni_net->net_lnd->lnd_eager_recv)(ni, msg->msg_private, msg,
785 CERROR("recv from %s / send to %s aborted: "
786 "eager_recv failed %d\n",
787 libcfs_nid2str(msg->msg_rxpeer->lpni_nid),
788 libcfs_id2str(msg->msg_target), rc);
789 LASSERT(rc < 0); /* required by my callers */
796 * This function can be called from two paths:
797 * 1. when sending a message
798 * 2. when decommiting a message (lnet_msg_decommit_tx())
799 * In both these cases the peer_ni should have it's reference count
800 * acquired by the caller and therefore it is safe to drop the spin
801 * lock before calling lnd_query()
804 lnet_ni_query_locked(struct lnet_ni *ni, struct lnet_peer_ni *lp)
806 time64_t last_alive = 0;
807 int cpt = lnet_cpt_of_nid_locked(lp->lpni_nid, ni);
809 LASSERT(lnet_peer_aliveness_enabled(lp));
810 LASSERT(ni->ni_net->net_lnd->lnd_query != NULL);
812 lnet_net_unlock(cpt);
813 (ni->ni_net->net_lnd->lnd_query)(ni, lp->lpni_nid, &last_alive);
816 lp->lpni_last_query = ktime_get_seconds();
818 if (last_alive != 0) /* NI has updated timestamp */
819 lp->lpni_last_alive = last_alive;
822 /* NB: always called with lnet_net_lock held */
824 lnet_peer_is_alive(struct lnet_peer_ni *lp, time64_t now)
829 LASSERT (lnet_peer_aliveness_enabled(lp));
832 * Trust lnet_notify() if it has more recent aliveness news, but
833 * ignore the initial assumed death (see lnet_peers_start_down()).
835 spin_lock(&lp->lpni_lock);
836 if (!lp->lpni_alive && lp->lpni_alive_count > 0 &&
837 lp->lpni_timestamp >= lp->lpni_last_alive) {
838 spin_unlock(&lp->lpni_lock);
842 deadline = lp->lpni_last_alive +
843 lp->lpni_net->net_tunables.lct_peer_timeout;
844 alive = deadline > now;
847 * Update obsolete lp_alive except for routers assumed to be dead
848 * initially, because router checker would update aliveness in this
849 * case, and moreover lpni_last_alive at peer creation is assumed.
851 if (alive && !lp->lpni_alive &&
852 !(lnet_isrouter(lp) && lp->lpni_alive_count == 0)) {
853 spin_unlock(&lp->lpni_lock);
854 lnet_notify_locked(lp, 0, 1, lp->lpni_last_alive);
856 spin_unlock(&lp->lpni_lock);
863 /* NB: returns 1 when alive, 0 when dead, negative when error;
864 * may drop the lnet_net_lock */
866 lnet_peer_alive_locked(struct lnet_ni *ni, struct lnet_peer_ni *lp)
868 time64_t now = ktime_get_seconds();
870 if (!lnet_peer_aliveness_enabled(lp))
873 if (lnet_peer_is_alive(lp, now))
877 * Peer appears dead, but we should avoid frequent NI queries (at
878 * most once per lnet_queryinterval seconds).
880 if (lp->lpni_last_query != 0) {
881 static const int lnet_queryinterval = 1;
884 next_query = lp->lpni_last_query + lnet_queryinterval;
886 if (now < next_query) {
888 CWARN("Unexpected aliveness of peer %s: "
889 "%lld < %lld (%d/%d)\n",
890 libcfs_nid2str(lp->lpni_nid),
893 lp->lpni_net->net_tunables.lct_peer_timeout);
898 /* query NI for latest aliveness news */
899 lnet_ni_query_locked(ni, lp);
901 if (lnet_peer_is_alive(lp, now))
904 lnet_notify_locked(lp, 0, 0, lp->lpni_last_alive);
909 * \param msg The message to be sent.
910 * \param do_send True if lnet_ni_send() should be called in this function.
911 * lnet_send() is going to lnet_net_unlock immediately after this, so
912 * it sets do_send FALSE and I don't do the unlock/send/lock bit.
914 * \retval LNET_CREDIT_OK If \a msg sent or OK to send.
915 * \retval LNET_CREDIT_WAIT If \a msg blocked for credit.
916 * \retval -EHOSTUNREACH If the next hop of the message appears dead.
917 * \retval -ECANCELED If the MD of the message has been unlinked.
920 lnet_post_send_locked(struct lnet_msg *msg, int do_send)
922 struct lnet_peer_ni *lp = msg->msg_txpeer;
923 struct lnet_ni *ni = msg->msg_txni;
924 int cpt = msg->msg_tx_cpt;
925 struct lnet_tx_queue *tq = ni->ni_tx_queues[cpt];
927 /* non-lnet_send() callers have checked before */
928 LASSERT(!do_send || msg->msg_tx_delayed);
929 LASSERT(!msg->msg_receiving);
930 LASSERT(msg->msg_tx_committed);
932 /* NB 'lp' is always the next hop */
933 if ((msg->msg_target.pid & LNET_PID_USERFLAG) == 0 &&
934 lnet_peer_alive_locked(ni, lp) == 0) {
935 the_lnet.ln_counters[cpt]->drop_count++;
936 the_lnet.ln_counters[cpt]->drop_length += msg->msg_len;
937 lnet_net_unlock(cpt);
939 lnet_incr_stats(&msg->msg_txpeer->lpni_stats,
941 LNET_STATS_TYPE_DROP);
943 lnet_incr_stats(&msg->msg_txni->ni_stats,
945 LNET_STATS_TYPE_DROP);
947 CNETERR("Dropping message for %s: peer not alive\n",
948 libcfs_id2str(msg->msg_target));
950 lnet_finalize(msg, -EHOSTUNREACH);
953 return -EHOSTUNREACH;
956 if (msg->msg_md != NULL &&
957 (msg->msg_md->md_flags & LNET_MD_FLAG_ABORTED) != 0) {
958 lnet_net_unlock(cpt);
960 CNETERR("Aborting message for %s: LNetM[DE]Unlink() already "
961 "called on the MD/ME.\n",
962 libcfs_id2str(msg->msg_target));
964 lnet_finalize(msg, -ECANCELED);
970 if (!msg->msg_peertxcredit) {
971 spin_lock(&lp->lpni_lock);
972 LASSERT((lp->lpni_txcredits < 0) ==
973 !list_empty(&lp->lpni_txq));
975 msg->msg_peertxcredit = 1;
976 lp->lpni_txqnob += msg->msg_len + sizeof(struct lnet_hdr);
977 lp->lpni_txcredits--;
979 if (lp->lpni_txcredits < lp->lpni_mintxcredits)
980 lp->lpni_mintxcredits = lp->lpni_txcredits;
982 if (lp->lpni_txcredits < 0) {
983 msg->msg_tx_delayed = 1;
984 list_add_tail(&msg->msg_list, &lp->lpni_txq);
985 spin_unlock(&lp->lpni_lock);
986 return LNET_CREDIT_WAIT;
988 spin_unlock(&lp->lpni_lock);
991 if (!msg->msg_txcredit) {
992 LASSERT((tq->tq_credits < 0) ==
993 !list_empty(&tq->tq_delayed));
995 msg->msg_txcredit = 1;
997 atomic_dec(&ni->ni_tx_credits);
999 if (tq->tq_credits < tq->tq_credits_min)
1000 tq->tq_credits_min = tq->tq_credits;
1002 if (tq->tq_credits < 0) {
1003 msg->msg_tx_delayed = 1;
1004 list_add_tail(&msg->msg_list, &tq->tq_delayed);
1005 return LNET_CREDIT_WAIT;
1010 lnet_net_unlock(cpt);
1011 lnet_ni_send(ni, msg);
1014 return LNET_CREDIT_OK;
1018 static struct lnet_rtrbufpool *
1019 lnet_msg2bufpool(struct lnet_msg *msg)
1021 struct lnet_rtrbufpool *rbp;
1024 LASSERT(msg->msg_rx_committed);
1026 cpt = msg->msg_rx_cpt;
1027 rbp = &the_lnet.ln_rtrpools[cpt][0];
1029 LASSERT(msg->msg_len <= LNET_MTU);
1030 while (msg->msg_len > (unsigned int)rbp->rbp_npages * PAGE_SIZE) {
1032 LASSERT(rbp < &the_lnet.ln_rtrpools[cpt][LNET_NRBPOOLS]);
1039 lnet_post_routed_recv_locked(struct lnet_msg *msg, int do_recv)
1041 /* lnet_parse is going to lnet_net_unlock immediately after this, so it
1042 * sets do_recv FALSE and I don't do the unlock/send/lock bit.
1043 * I return LNET_CREDIT_WAIT if msg blocked and LNET_CREDIT_OK if
1044 * received or OK to receive */
1045 struct lnet_peer_ni *lp = msg->msg_rxpeer;
1046 struct lnet_rtrbufpool *rbp;
1047 struct lnet_rtrbuf *rb;
1049 LASSERT (msg->msg_iov == NULL);
1050 LASSERT (msg->msg_kiov == NULL);
1051 LASSERT (msg->msg_niov == 0);
1052 LASSERT (msg->msg_routing);
1053 LASSERT (msg->msg_receiving);
1054 LASSERT (!msg->msg_sending);
1056 /* non-lnet_parse callers only receive delayed messages */
1057 LASSERT(!do_recv || msg->msg_rx_delayed);
1059 if (!msg->msg_peerrtrcredit) {
1060 spin_lock(&lp->lpni_lock);
1061 LASSERT((lp->lpni_rtrcredits < 0) ==
1062 !list_empty(&lp->lpni_rtrq));
1064 msg->msg_peerrtrcredit = 1;
1065 lp->lpni_rtrcredits--;
1066 if (lp->lpni_rtrcredits < lp->lpni_minrtrcredits)
1067 lp->lpni_minrtrcredits = lp->lpni_rtrcredits;
1069 if (lp->lpni_rtrcredits < 0) {
1070 /* must have checked eager_recv before here */
1071 LASSERT(msg->msg_rx_ready_delay);
1072 msg->msg_rx_delayed = 1;
1073 list_add_tail(&msg->msg_list, &lp->lpni_rtrq);
1074 spin_unlock(&lp->lpni_lock);
1075 return LNET_CREDIT_WAIT;
1077 spin_unlock(&lp->lpni_lock);
1080 rbp = lnet_msg2bufpool(msg);
1082 if (!msg->msg_rtrcredit) {
1083 msg->msg_rtrcredit = 1;
1085 if (rbp->rbp_credits < rbp->rbp_mincredits)
1086 rbp->rbp_mincredits = rbp->rbp_credits;
1088 if (rbp->rbp_credits < 0) {
1089 /* must have checked eager_recv before here */
1090 LASSERT(msg->msg_rx_ready_delay);
1091 msg->msg_rx_delayed = 1;
1092 list_add_tail(&msg->msg_list, &rbp->rbp_msgs);
1093 return LNET_CREDIT_WAIT;
1097 LASSERT(!list_empty(&rbp->rbp_bufs));
1098 rb = list_entry(rbp->rbp_bufs.next, struct lnet_rtrbuf, rb_list);
1099 list_del(&rb->rb_list);
1101 msg->msg_niov = rbp->rbp_npages;
1102 msg->msg_kiov = &rb->rb_kiov[0];
1105 int cpt = msg->msg_rx_cpt;
1107 lnet_net_unlock(cpt);
1108 lnet_ni_recv(msg->msg_rxni, msg->msg_private, msg, 1,
1109 0, msg->msg_len, msg->msg_len);
1112 return LNET_CREDIT_OK;
1116 lnet_return_tx_credits_locked(struct lnet_msg *msg)
1118 struct lnet_peer_ni *txpeer = msg->msg_txpeer;
1119 struct lnet_ni *txni = msg->msg_txni;
1120 struct lnet_msg *msg2;
1122 if (msg->msg_txcredit) {
1123 struct lnet_ni *ni = msg->msg_txni;
1124 struct lnet_tx_queue *tq = ni->ni_tx_queues[msg->msg_tx_cpt];
1126 /* give back NI txcredits */
1127 msg->msg_txcredit = 0;
1129 LASSERT((tq->tq_credits < 0) ==
1130 !list_empty(&tq->tq_delayed));
1133 atomic_inc(&ni->ni_tx_credits);
1134 if (tq->tq_credits <= 0) {
1135 msg2 = list_entry(tq->tq_delayed.next,
1136 struct lnet_msg, msg_list);
1137 list_del(&msg2->msg_list);
1139 LASSERT(msg2->msg_txni == ni);
1140 LASSERT(msg2->msg_tx_delayed);
1141 LASSERT(msg2->msg_tx_cpt == msg->msg_tx_cpt);
1143 (void) lnet_post_send_locked(msg2, 1);
1147 if (msg->msg_peertxcredit) {
1148 /* give back peer txcredits */
1149 msg->msg_peertxcredit = 0;
1151 spin_lock(&txpeer->lpni_lock);
1152 LASSERT((txpeer->lpni_txcredits < 0) ==
1153 !list_empty(&txpeer->lpni_txq));
1155 txpeer->lpni_txqnob -= msg->msg_len + sizeof(struct lnet_hdr);
1156 LASSERT(txpeer->lpni_txqnob >= 0);
1158 txpeer->lpni_txcredits++;
1159 if (txpeer->lpni_txcredits <= 0) {
1162 msg2 = list_entry(txpeer->lpni_txq.next,
1163 struct lnet_msg, msg_list);
1164 list_del(&msg2->msg_list);
1165 spin_unlock(&txpeer->lpni_lock);
1167 LASSERT(msg2->msg_txpeer == txpeer);
1168 LASSERT(msg2->msg_tx_delayed);
1170 msg2_cpt = msg2->msg_tx_cpt;
1173 * The msg_cpt can be different from the msg2_cpt
1174 * so we need to make sure we lock the correct cpt
1176 * Once we call lnet_post_send_locked() it is no
1177 * longer safe to access msg2, since it could've
1178 * been freed by lnet_finalize(), but we still
1179 * need to relock the correct cpt, so we cache the
1180 * msg2_cpt for the purpose of the check that
1181 * follows the call to lnet_pose_send_locked().
1183 if (msg2_cpt != msg->msg_tx_cpt) {
1184 lnet_net_unlock(msg->msg_tx_cpt);
1185 lnet_net_lock(msg2_cpt);
1187 (void) lnet_post_send_locked(msg2, 1);
1188 if (msg2_cpt != msg->msg_tx_cpt) {
1189 lnet_net_unlock(msg2_cpt);
1190 lnet_net_lock(msg->msg_tx_cpt);
1193 spin_unlock(&txpeer->lpni_lock);
1198 msg->msg_txni = NULL;
1199 lnet_ni_decref_locked(txni, msg->msg_tx_cpt);
1202 if (txpeer != NULL) {
1205 * Once the patch for the health comes in we need to set
1206 * the health of the peer ni to bad when we fail to send
1208 * int status = msg->msg_ev.status;
1210 * lnet_set_peer_ni_health_locked(txpeer, false)
1212 msg->msg_txpeer = NULL;
1213 lnet_peer_ni_decref_locked(txpeer);
1218 lnet_schedule_blocked_locked(struct lnet_rtrbufpool *rbp)
1220 struct lnet_msg *msg;
1222 if (list_empty(&rbp->rbp_msgs))
1224 msg = list_entry(rbp->rbp_msgs.next,
1225 struct lnet_msg, msg_list);
1226 list_del(&msg->msg_list);
1228 (void)lnet_post_routed_recv_locked(msg, 1);
1232 lnet_drop_routed_msgs_locked(struct list_head *list, int cpt)
1234 struct lnet_msg *msg;
1235 struct lnet_msg *tmp;
1237 lnet_net_unlock(cpt);
1239 list_for_each_entry_safe(msg, tmp, list, msg_list) {
1240 lnet_ni_recv(msg->msg_rxni, msg->msg_private, NULL,
1241 0, 0, 0, msg->msg_hdr.payload_length);
1242 list_del_init(&msg->msg_list);
1243 lnet_finalize(msg, -ECANCELED);
1250 lnet_return_rx_credits_locked(struct lnet_msg *msg)
1252 struct lnet_peer_ni *rxpeer = msg->msg_rxpeer;
1253 struct lnet_ni *rxni = msg->msg_rxni;
1254 struct lnet_msg *msg2;
1256 if (msg->msg_rtrcredit) {
1257 /* give back global router credits */
1258 struct lnet_rtrbuf *rb;
1259 struct lnet_rtrbufpool *rbp;
1261 /* NB If a msg ever blocks for a buffer in rbp_msgs, it stays
1262 * there until it gets one allocated, or aborts the wait
1264 LASSERT(msg->msg_kiov != NULL);
1266 rb = list_entry(msg->msg_kiov, struct lnet_rtrbuf, rb_kiov[0]);
1269 msg->msg_kiov = NULL;
1270 msg->msg_rtrcredit = 0;
1272 LASSERT(rbp == lnet_msg2bufpool(msg));
1274 LASSERT((rbp->rbp_credits > 0) ==
1275 !list_empty(&rbp->rbp_bufs));
1277 /* If routing is now turned off, we just drop this buffer and
1278 * don't bother trying to return credits. */
1279 if (!the_lnet.ln_routing) {
1280 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1284 /* It is possible that a user has lowered the desired number of
1285 * buffers in this pool. Make sure we never put back
1286 * more buffers than the stated number. */
1287 if (unlikely(rbp->rbp_credits >= rbp->rbp_req_nbuffers)) {
1288 /* Discard this buffer so we don't have too
1290 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
1291 rbp->rbp_nbuffers--;
1293 list_add(&rb->rb_list, &rbp->rbp_bufs);
1295 if (rbp->rbp_credits <= 0)
1296 lnet_schedule_blocked_locked(rbp);
1301 if (msg->msg_peerrtrcredit) {
1302 /* give back peer router credits */
1303 msg->msg_peerrtrcredit = 0;
1305 spin_lock(&rxpeer->lpni_lock);
1306 LASSERT((rxpeer->lpni_rtrcredits < 0) ==
1307 !list_empty(&rxpeer->lpni_rtrq));
1309 rxpeer->lpni_rtrcredits++;
1311 /* drop all messages which are queued to be routed on that
1313 if (!the_lnet.ln_routing) {
1314 struct list_head drop;
1315 INIT_LIST_HEAD(&drop);
1316 list_splice_init(&rxpeer->lpni_rtrq, &drop);
1317 spin_unlock(&rxpeer->lpni_lock);
1318 lnet_drop_routed_msgs_locked(&drop, msg->msg_rx_cpt);
1319 } else if (rxpeer->lpni_rtrcredits <= 0) {
1320 msg2 = list_entry(rxpeer->lpni_rtrq.next,
1321 struct lnet_msg, msg_list);
1322 list_del(&msg2->msg_list);
1323 spin_unlock(&rxpeer->lpni_lock);
1324 (void) lnet_post_routed_recv_locked(msg2, 1);
1326 spin_unlock(&rxpeer->lpni_lock);
1330 msg->msg_rxni = NULL;
1331 lnet_ni_decref_locked(rxni, msg->msg_rx_cpt);
1333 if (rxpeer != NULL) {
1334 msg->msg_rxpeer = NULL;
1335 lnet_peer_ni_decref_locked(rxpeer);
1340 lnet_compare_peers(struct lnet_peer_ni *p1, struct lnet_peer_ni *p2)
1342 if (p1->lpni_txqnob < p2->lpni_txqnob)
1345 if (p1->lpni_txqnob > p2->lpni_txqnob)
1348 if (p1->lpni_txcredits > p2->lpni_txcredits)
1351 if (p1->lpni_txcredits < p2->lpni_txcredits)
1358 lnet_compare_routes(struct lnet_route *r1, struct lnet_route *r2)
1360 struct lnet_peer_ni *p1 = r1->lr_gateway;
1361 struct lnet_peer_ni *p2 = r2->lr_gateway;
1362 int r1_hops = (r1->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r1->lr_hops;
1363 int r2_hops = (r2->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r2->lr_hops;
1366 if (r1->lr_priority < r2->lr_priority)
1369 if (r1->lr_priority > r2->lr_priority)
1372 if (r1_hops < r2_hops)
1375 if (r1_hops > r2_hops)
1378 rc = lnet_compare_peers(p1, p2);
1382 if (r1->lr_seq - r2->lr_seq <= 0)
1388 static struct lnet_peer_ni *
1389 lnet_find_route_locked(struct lnet_net *net, __u32 remote_net,
1392 struct lnet_remotenet *rnet;
1393 struct lnet_route *route;
1394 struct lnet_route *best_route;
1395 struct lnet_route *last_route;
1396 struct lnet_peer_ni *lpni_best;
1397 struct lnet_peer_ni *lp;
1400 /* If @rtr_nid is not LNET_NID_ANY, return the gateway with
1401 * rtr_nid nid, otherwise find the best gateway I can use */
1403 rnet = lnet_find_rnet_locked(remote_net);
1408 best_route = last_route = NULL;
1409 list_for_each_entry(route, &rnet->lrn_routes, lr_list) {
1410 lp = route->lr_gateway;
1412 if (!lnet_is_route_alive(route))
1415 if (net != NULL && lp->lpni_net != net)
1418 if (lp->lpni_nid == rtr_nid) /* it's pre-determined router */
1421 if (lpni_best == NULL) {
1422 best_route = last_route = route;
1427 /* no protection on below fields, but it's harmless */
1428 if (last_route->lr_seq - route->lr_seq < 0)
1431 rc = lnet_compare_routes(route, best_route);
1439 /* set sequence number on the best router to the latest sequence + 1
1440 * so we can round-robin all routers, it's race and inaccurate but
1441 * harmless and functional */
1442 if (best_route != NULL)
1443 best_route->lr_seq = last_route->lr_seq + 1;
1447 static struct lnet_ni *
1448 lnet_get_best_ni(struct lnet_net *local_net, struct lnet_ni *best_ni,
1449 struct lnet_peer *peer, struct lnet_peer_net *peer_net,
1452 struct lnet_ni *ni = NULL;
1453 unsigned int shortest_distance;
1457 * If there is no peer_ni that we can send to on this network,
1458 * then there is no point in looking for a new best_ni here.
1460 if (!lnet_get_next_peer_ni_locked(peer, peer_net, NULL))
1463 if (best_ni == NULL) {
1464 shortest_distance = UINT_MAX;
1465 best_credits = INT_MIN;
1467 shortest_distance = cfs_cpt_distance(lnet_cpt_table(), md_cpt,
1468 best_ni->ni_dev_cpt);
1469 best_credits = atomic_read(&best_ni->ni_tx_credits);
1472 while ((ni = lnet_get_next_ni_locked(local_net, ni))) {
1473 unsigned int distance;
1476 if (!lnet_is_ni_healthy_locked(ni))
1479 ni_credits = atomic_read(&ni->ni_tx_credits);
1482 * calculate the distance from the CPT on which
1483 * the message memory is allocated to the CPT of
1484 * the NI's physical device
1486 distance = cfs_cpt_distance(lnet_cpt_table(),
1490 CDEBUG(D_NET, "compare ni %s [c:%d, d:%d, s:%d] with best_ni %s [c:%d, d:%d, s:%d]\n",
1491 libcfs_nid2str(ni->ni_nid), ni_credits, distance,
1492 ni->ni_seq, (best_ni) ? libcfs_nid2str(best_ni->ni_nid)
1493 : "not seleced", best_credits, shortest_distance,
1494 (best_ni) ? best_ni->ni_seq : 0);
1497 * All distances smaller than the NUMA range
1498 * are treated equally.
1500 if (distance < lnet_numa_range)
1501 distance = lnet_numa_range;
1504 * Select on shorter distance, then available
1505 * credits, then round-robin.
1507 if (distance > shortest_distance) {
1509 } else if (distance < shortest_distance) {
1510 shortest_distance = distance;
1511 } else if (ni_credits < best_credits) {
1513 } else if (ni_credits == best_credits) {
1514 if (best_ni && (best_ni)->ni_seq <= ni->ni_seq)
1518 best_credits = ni_credits;
1521 CDEBUG(D_NET, "selected best_ni %s\n",
1522 (best_ni) ? libcfs_nid2str(best_ni->ni_nid) : "no selection");
1528 * Traffic to the LNET_RESERVED_PORTAL may not trigger peer discovery,
1529 * because such traffic is required to perform discovery. We therefore
1530 * exclude all GET and PUT on that portal. We also exclude all ACK and
1531 * REPLY traffic, but that is because the portal is not tracked in the
1532 * message structure for these message types. We could restrict this
1533 * further by also checking for LNET_PROTO_PING_MATCHBITS.
1536 lnet_msg_discovery(struct lnet_msg *msg)
1538 if (msg->msg_type == LNET_MSG_PUT) {
1539 if (msg->msg_hdr.msg.put.ptl_index != LNET_RESERVED_PORTAL)
1541 } else if (msg->msg_type == LNET_MSG_GET) {
1542 if (msg->msg_hdr.msg.get.ptl_index != LNET_RESERVED_PORTAL)
1548 #define SRC_SPEC 0x0001
1549 #define SRC_ANY 0x0002
1550 #define LOCAL_DST 0x0004
1551 #define REMOTE_DST 0x0008
1552 #define MR_DST 0x0010
1553 #define NMR_DST 0x0020
1554 #define SND_RESP 0x0040
1556 /* The following to defines are used for return codes */
1557 #define REPEAT_SEND 0x1000
1558 #define PASS_THROUGH 0x2000
1560 /* The different cases lnet_select pathway needs to handle */
1561 #define SRC_SPEC_LOCAL_MR_DST (SRC_SPEC | LOCAL_DST | MR_DST)
1562 #define SRC_SPEC_ROUTER_MR_DST (SRC_SPEC | REMOTE_DST | MR_DST)
1563 #define SRC_SPEC_LOCAL_NMR_DST (SRC_SPEC | LOCAL_DST | NMR_DST)
1564 #define SRC_SPEC_ROUTER_NMR_DST (SRC_SPEC | REMOTE_DST | NMR_DST)
1565 #define SRC_ANY_LOCAL_MR_DST (SRC_ANY | LOCAL_DST | MR_DST)
1566 #define SRC_ANY_ROUTER_MR_DST (SRC_ANY | REMOTE_DST | MR_DST)
1567 #define SRC_ANY_LOCAL_NMR_DST (SRC_ANY | LOCAL_DST | NMR_DST)
1568 #define SRC_ANY_ROUTER_NMR_DST (SRC_ANY | REMOTE_DST | NMR_DST)
1571 lnet_handle_send(struct lnet_send_data *sd)
1573 struct lnet_ni *best_ni = sd->sd_best_ni;
1574 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
1575 struct lnet_peer_ni *final_dst_lpni = sd->sd_final_dst_lpni;
1576 struct lnet_msg *msg = sd->sd_msg;
1578 __u32 send_case = sd->sd_send_case;
1580 __u32 routing = send_case & REMOTE_DST;
1583 * Increment sequence number of the selected peer so that we
1584 * pick the next one in Round Robin.
1586 best_lpni->lpni_seq++;
1589 * grab a reference on the peer_ni so it sticks around even if
1590 * we need to drop and relock the lnet_net_lock below.
1592 lnet_peer_ni_addref_locked(best_lpni);
1595 * Use lnet_cpt_of_nid() to determine the CPT used to commit the
1596 * message. This ensures that we get a CPT that is correct for
1597 * the NI when the NI has been restricted to a subset of all CPTs.
1598 * If the selected CPT differs from the one currently locked, we
1599 * must unlock and relock the lnet_net_lock(), and then check whether
1600 * the configuration has changed. We don't have a hold on the best_ni
1601 * yet, and it may have vanished.
1603 cpt2 = lnet_cpt_of_nid_locked(best_lpni->lpni_nid, best_ni);
1604 if (sd->sd_cpt != cpt2) {
1605 __u32 seq = lnet_get_dlc_seq_locked();
1606 lnet_net_unlock(sd->sd_cpt);
1608 lnet_net_lock(sd->sd_cpt);
1609 if (seq != lnet_get_dlc_seq_locked()) {
1610 lnet_peer_ni_decref_locked(best_lpni);
1616 * store the best_lpni in the message right away to avoid having
1617 * to do the same operation under different conditions
1619 msg->msg_txpeer = best_lpni;
1620 msg->msg_txni = best_ni;
1623 * grab a reference for the best_ni since now it's in use in this
1624 * send. The reference will be dropped in lnet_finalize()
1626 lnet_ni_addref_locked(msg->msg_txni, sd->sd_cpt);
1629 * Always set the target.nid to the best peer picked. Either the
1630 * NID will be one of the peer NIDs selected, or the same NID as
1631 * what was originally set in the target or it will be the NID of
1632 * a router if this message should be routed
1634 msg->msg_target.nid = msg->msg_txpeer->lpni_nid;
1637 * lnet_msg_commit assigns the correct cpt to the message, which
1638 * is used to decrement the correct refcount on the ni when it's
1639 * time to return the credits
1641 lnet_msg_commit(msg, sd->sd_cpt);
1644 * If we are routing the message then we keep the src_nid that was
1645 * set by the originator. If we are not routing then we are the
1646 * originator and set it here.
1648 if (!msg->msg_routing)
1649 msg->msg_hdr.src_nid = cpu_to_le64(msg->msg_txni->ni_nid);
1652 msg->msg_target_is_router = 1;
1653 msg->msg_target.pid = LNET_PID_LUSTRE;
1655 * since we're routing we want to ensure that the
1656 * msg_hdr.dest_nid is set to the final destination. When
1657 * the router receives this message it knows how to route
1660 * final_dst_lpni is set at the beginning of the
1661 * lnet_select_pathway() function and is never changed.
1662 * It's safe to use it here.
1664 msg->msg_hdr.dest_nid = cpu_to_le64(final_dst_lpni->lpni_nid);
1667 * if we're not routing set the dest_nid to the best peer
1668 * ni NID that we picked earlier in the algorithm.
1670 msg->msg_hdr.dest_nid = cpu_to_le64(msg->msg_txpeer->lpni_nid);
1673 rc = lnet_post_send_locked(msg, 0);
1676 CDEBUG(D_NET, "TRACE: %s(%s:%s) -> %s(%s:%s) : %s\n",
1677 libcfs_nid2str(msg->msg_hdr.src_nid),
1678 libcfs_nid2str(msg->msg_txni->ni_nid),
1679 libcfs_nid2str(sd->sd_src_nid),
1680 libcfs_nid2str(msg->msg_hdr.dest_nid),
1681 libcfs_nid2str(sd->sd_dst_nid),
1682 libcfs_nid2str(msg->msg_txpeer->lpni_nid),
1683 lnet_msgtyp2str(msg->msg_type));
1688 static struct lnet_peer_ni *
1689 lnet_select_peer_ni(struct lnet_send_data *sd, struct lnet_peer *peer,
1690 struct lnet_peer_net *peer_net)
1693 * Look at the peer NIs for the destination peer that connect
1694 * to the chosen net. If a peer_ni is preferred when using the
1695 * best_ni to communicate, we use that one. If there is no
1696 * preferred peer_ni, or there are multiple preferred peer_ni,
1697 * the available transmit credits are used. If the transmit
1698 * credits are equal, we round-robin over the peer_ni.
1700 struct lnet_peer_ni *lpni = NULL;
1701 struct lnet_peer_ni *best_lpni = NULL;
1702 struct lnet_ni *best_ni = sd->sd_best_ni;
1703 lnet_nid_t dst_nid = sd->sd_dst_nid;
1704 int best_lpni_credits = INT_MIN;
1705 bool preferred = false;
1708 while ((lpni = lnet_get_next_peer_ni_locked(peer, peer_net, lpni))) {
1710 * if the best_ni we've chosen aleady has this lpni
1711 * preferred, then let's use it
1713 ni_is_pref = lnet_peer_is_pref_nid_locked(lpni,
1716 CDEBUG(D_NET, "%s ni_is_pref = %d\n",
1717 libcfs_nid2str(best_ni->ni_nid), ni_is_pref);
1720 CDEBUG(D_NET, "%s c:[%d, %d], s:[%d, %d]\n",
1721 libcfs_nid2str(lpni->lpni_nid),
1722 lpni->lpni_txcredits, best_lpni_credits,
1723 lpni->lpni_seq, best_lpni->lpni_seq);
1725 /* if this is a preferred peer use it */
1726 if (!preferred && ni_is_pref) {
1728 } else if (preferred && !ni_is_pref) {
1730 * this is not the preferred peer so let's ignore
1734 } else if (lpni->lpni_txcredits < best_lpni_credits) {
1736 * We already have a peer that has more credits
1737 * available than this one. No need to consider
1738 * this peer further.
1741 } else if (lpni->lpni_txcredits == best_lpni_credits) {
1743 * The best peer found so far and the current peer
1744 * have the same number of available credits let's
1745 * make sure to select between them using Round
1749 if (best_lpni->lpni_seq <= lpni->lpni_seq)
1755 best_lpni_credits = lpni->lpni_txcredits;
1758 /* if we still can't find a peer ni then we can't reach it */
1760 __u32 net_id = (peer_net) ? peer_net->lpn_net_id :
1761 LNET_NIDNET(dst_nid);
1762 CDEBUG(D_NET, "no peer_ni found on peer net %s\n",
1763 libcfs_net2str(net_id));
1767 CDEBUG(D_NET, "sd_best_lpni = %s\n",
1768 libcfs_nid2str(best_lpni->lpni_nid));
1774 * Prerequisite: the best_ni should already be set in the sd
1776 static inline struct lnet_peer_ni *
1777 lnet_find_best_lpni_on_net(struct lnet_send_data *sd, struct lnet_peer *peer,
1780 struct lnet_peer_net *peer_net;
1783 * The gateway is Multi-Rail capable so now we must select the
1786 peer_net = lnet_peer_get_net_locked(peer, net_id);
1789 CERROR("gateway peer %s has no NI on net %s\n",
1790 libcfs_nid2str(peer->lp_primary_nid),
1791 libcfs_net2str(net_id));
1795 return lnet_select_peer_ni(sd, peer, peer_net);
1799 lnet_set_non_mr_pref_nid(struct lnet_send_data *sd)
1801 if (sd->sd_send_case & NMR_DST &&
1802 sd->sd_msg->msg_type != LNET_MSG_REPLY &&
1803 sd->sd_msg->msg_type != LNET_MSG_ACK &&
1804 sd->sd_best_lpni->lpni_pref_nnids == 0) {
1805 CDEBUG(D_NET, "Setting preferred local NID %s on NMR peer %s\n",
1806 libcfs_nid2str(sd->sd_best_ni->ni_nid),
1807 libcfs_nid2str(sd->sd_best_lpni->lpni_nid));
1808 lnet_peer_ni_set_non_mr_pref_nid(sd->sd_best_lpni,
1809 sd->sd_best_ni->ni_nid);
1818 * use the source and destination NIDs as the pathway
1821 lnet_handle_spec_local_nmr_dst(struct lnet_send_data *sd)
1823 /* the destination lpni is set before we get here. */
1826 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
1827 if (!sd->sd_best_ni) {
1828 CERROR("Can't send to %s: src %s is not a "
1829 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
1830 libcfs_nid2str(sd->sd_src_nid));
1835 * the preferred NID will only be set for NMR peers
1837 lnet_set_non_mr_pref_nid(sd);
1839 return lnet_handle_send(sd);
1847 * Run the selection algorithm on the peer NIs unless we're sending
1848 * a response, in this case just send to the destination
1851 lnet_handle_spec_local_mr_dst(struct lnet_send_data *sd)
1853 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
1854 if (!sd->sd_best_ni) {
1855 CERROR("Can't send to %s: src %s is not a "
1856 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
1857 libcfs_nid2str(sd->sd_src_nid));
1862 * only run the selection algorithm to pick the peer_ni if we're
1863 * sending a GET or a PUT. Responses are sent to the same
1864 * destination NID provided.
1866 if (!(sd->sd_send_case & SND_RESP)) {
1868 lnet_find_best_lpni_on_net(sd, sd->sd_peer,
1869 sd->sd_best_ni->ni_net->net_id);
1872 if (sd->sd_best_lpni)
1873 return lnet_handle_send(sd);
1875 CERROR("can't send to %s. no NI on %s\n",
1876 libcfs_nid2str(sd->sd_dst_nid),
1877 libcfs_net2str(sd->sd_best_ni->ni_net->net_id));
1879 return -EHOSTUNREACH;
1883 lnet_find_best_ni_on_spec_net(struct lnet_ni *cur_best_ni,
1884 struct lnet_peer *peer,
1885 struct lnet_peer_net *peer_net,
1889 struct lnet_net *local_net;
1890 struct lnet_ni *best_ni;
1892 local_net = lnet_get_net_locked(peer_net->lpn_net_id);
1897 * Iterate through the NIs in this local Net and select
1898 * the NI to send from. The selection is determined by
1899 * these 3 criterion in the following priority:
1901 * 2. NI available credits
1904 best_ni = lnet_get_best_ni(local_net, cur_best_ni,
1905 peer, peer_net, cpt);
1907 if (incr_seq && best_ni)
1914 lnet_handle_find_routed_path(struct lnet_send_data *sd,
1916 struct lnet_peer_ni **gw_lpni,
1917 struct lnet_peer **gw_peer)
1919 struct lnet_peer_ni *gw;
1920 lnet_nid_t src_nid = sd->sd_src_nid;
1922 gw = lnet_find_route_locked(NULL, LNET_NIDNET(dst_nid),
1925 CERROR("no route to %s from %s\n",
1926 libcfs_nid2str(dst_nid), libcfs_nid2str(src_nid));
1927 return -EHOSTUNREACH;
1930 /* get the peer of the gw_ni */
1931 LASSERT(gw->lpni_peer_net);
1932 LASSERT(gw->lpni_peer_net->lpn_peer);
1934 *gw_peer = gw->lpni_peer_net->lpn_peer;
1936 if (!sd->sd_best_ni)
1937 sd->sd_best_ni = lnet_find_best_ni_on_spec_net(NULL, *gw_peer,
1942 if (!sd->sd_best_ni) {
1943 CERROR("Internal Error. Expected local ni on %s "
1944 "but non found :%s\n",
1945 libcfs_net2str(gw->lpni_peer_net->lpn_net_id),
1946 libcfs_nid2str(sd->sd_src_nid));
1951 * if gw is MR let's find its best peer_ni
1953 if (lnet_peer_is_multi_rail(*gw_peer)) {
1954 gw = lnet_find_best_lpni_on_net(sd, *gw_peer,
1955 sd->sd_best_ni->ni_net->net_id);
1957 * We've already verified that the gw has an NI on that
1958 * desired net, but we're not finding it. Something is
1962 CERROR("Internal Error. Route expected to %s from %s\n",
1963 libcfs_nid2str(dst_nid),
1964 libcfs_nid2str(src_nid));
1979 * Remote destination
1980 * Non-MR destination
1984 * Remote destination
1987 * The handling of these two cases is similar. Even though the destination
1988 * can be MR or non-MR, we'll deal directly with the router.
1991 lnet_handle_spec_router_dst(struct lnet_send_data *sd)
1994 struct lnet_peer_ni *gw_lpni = NULL;
1995 struct lnet_peer *gw_peer = NULL;
1998 sd->sd_best_ni = lnet_nid2ni_locked(sd->sd_src_nid, sd->sd_cpt);
1999 if (!sd->sd_best_ni) {
2000 CERROR("Can't send to %s: src %s is not a "
2001 "local nid\n", libcfs_nid2str(sd->sd_dst_nid),
2002 libcfs_nid2str(sd->sd_src_nid));
2006 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2011 if (sd->sd_send_case & NMR_DST)
2013 * since the final destination is non-MR let's set its preferred
2014 * NID before we send
2016 lnet_set_non_mr_pref_nid(sd);
2019 * We're going to send to the gw found so let's set its
2022 sd->sd_peer = gw_peer;
2023 sd->sd_best_lpni = gw_lpni;
2025 return lnet_handle_send(sd);
2029 lnet_find_best_ni_on_local_net(struct lnet_peer *peer, int md_cpt)
2031 struct lnet_peer_net *peer_net = NULL;
2032 struct lnet_ni *best_ni = NULL;
2035 * The peer can have multiple interfaces, some of them can be on
2036 * the local network and others on a routed network. We should
2037 * prefer the local network. However if the local network is not
2038 * available then we need to try the routed network
2041 /* go through all the peer nets and find the best_ni */
2042 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_peer_nets) {
2044 * The peer's list of nets can contain non-local nets. We
2045 * want to only examine the local ones.
2047 if (!lnet_get_net_locked(peer_net->lpn_net_id))
2049 best_ni = lnet_find_best_ni_on_spec_net(best_ni, peer,
2050 peer_net, md_cpt, false);
2054 /* increment sequence number so we can round robin */
2060 static struct lnet_ni *
2061 lnet_find_existing_preferred_best_ni(struct lnet_send_data *sd)
2063 struct lnet_ni *best_ni = NULL;
2064 struct lnet_peer_net *peer_net;
2065 struct lnet_peer *peer = sd->sd_peer;
2066 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
2067 struct lnet_peer_ni *lpni;
2068 int cpt = sd->sd_cpt;
2071 * We must use a consistent source address when sending to a
2072 * non-MR peer. However, a non-MR peer can have multiple NIDs
2073 * on multiple networks, and we may even need to talk to this
2074 * peer on multiple networks -- certain types of
2075 * load-balancing configuration do this.
2077 * So we need to pick the NI the peer prefers for this
2078 * particular network.
2081 /* Get the target peer_ni */
2082 peer_net = lnet_peer_get_net_locked(peer,
2083 LNET_NIDNET(best_lpni->lpni_nid));
2084 LASSERT(peer_net != NULL);
2085 list_for_each_entry(lpni, &peer_net->lpn_peer_nis,
2087 if (lpni->lpni_pref_nnids == 0)
2089 LASSERT(lpni->lpni_pref_nnids == 1);
2090 best_ni = lnet_nid2ni_locked(
2091 lpni->lpni_pref.nid, cpt);
2098 /* Prerequisite: sd->sd_peer and sd->sd_best_lpni should be set */
2100 lnet_select_preferred_best_ni(struct lnet_send_data *sd)
2102 struct lnet_ni *best_ni = NULL;
2103 struct lnet_peer_ni *best_lpni = sd->sd_best_lpni;
2106 * We must use a consistent source address when sending to a
2107 * non-MR peer. However, a non-MR peer can have multiple NIDs
2108 * on multiple networks, and we may even need to talk to this
2109 * peer on multiple networks -- certain types of
2110 * load-balancing configuration do this.
2112 * So we need to pick the NI the peer prefers for this
2113 * particular network.
2116 best_ni = lnet_find_existing_preferred_best_ni(sd);
2118 /* if best_ni is still not set just pick one */
2121 lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2122 sd->sd_best_lpni->lpni_peer_net,
2123 sd->sd_md_cpt, true);
2124 /* If there is no best_ni we don't have a route */
2126 CERROR("no path to %s from net %s\n",
2127 libcfs_nid2str(best_lpni->lpni_nid),
2128 libcfs_net2str(best_lpni->lpni_net->net_id));
2129 return -EHOSTUNREACH;
2133 sd->sd_best_ni = best_ni;
2135 /* Set preferred NI if necessary. */
2136 lnet_set_non_mr_pref_nid(sd);
2143 * Source not specified
2147 * always use the same source NID for NMR peers
2148 * If we've talked to that peer before then we already have a preferred
2149 * source NI associated with it. Otherwise, we select a preferred local NI
2150 * and store it in the peer
2153 lnet_handle_any_local_nmr_dst(struct lnet_send_data *sd)
2157 /* sd->sd_best_lpni is already set to the final destination */
2160 * At this point we should've created the peer ni and peer. If we
2161 * can't find it, then something went wrong. Instead of assert
2162 * output a relevant message and fail the send
2164 if (!sd->sd_best_lpni) {
2165 CERROR("Internal fault. Unable to send msg %s to %s. "
2167 lnet_msgtyp2str(sd->sd_msg->msg_type),
2168 libcfs_nid2str(sd->sd_dst_nid));
2172 rc = lnet_select_preferred_best_ni(sd);
2174 rc = lnet_handle_send(sd);
2180 lnet_handle_any_mr_dsta(struct lnet_send_data *sd)
2183 * NOTE we've already handled the remote peer case. So we only
2184 * need to worry about the local case here.
2186 * if we're sending a response, ACK or reply, we need to send it
2187 * to the destination NID given to us. At this point we already
2188 * have the peer_ni we're suppose to send to, so just find the
2189 * best_ni on the peer net and use that. Since we're sending to an
2190 * MR peer then we can just run the selection algorithm on our
2191 * local NIs and pick the best one.
2193 if (sd->sd_send_case & SND_RESP) {
2195 lnet_find_best_ni_on_spec_net(NULL, sd->sd_peer,
2196 sd->sd_best_lpni->lpni_peer_net,
2197 sd->sd_md_cpt, true);
2199 if (!sd->sd_best_ni) {
2201 * We're not going to deal with not able to send
2202 * a response to the provided final destination
2204 CERROR("Can't send response to %s. "
2205 "No local NI available\n",
2206 libcfs_nid2str(sd->sd_dst_nid));
2207 return -EHOSTUNREACH;
2210 return lnet_handle_send(sd);
2214 * If we get here that means we're sending a fresh request, PUT or
2215 * GET, so we need to run our standard selection algorithm.
2216 * First find the best local interface that's on any of the peer's
2219 sd->sd_best_ni = lnet_find_best_ni_on_local_net(sd->sd_peer,
2221 if (sd->sd_best_ni) {
2223 lnet_find_best_lpni_on_net(sd, sd->sd_peer,
2224 sd->sd_best_ni->ni_net->net_id);
2227 * if we're successful in selecting a peer_ni on the local
2228 * network, then send to it. Otherwise fall through and
2229 * try and see if we can reach it over another routed
2232 if (sd->sd_best_lpni) {
2234 * in case we initially started with a routed
2235 * destination, let's reset to local
2237 sd->sd_send_case &= ~REMOTE_DST;
2238 sd->sd_send_case |= LOCAL_DST;
2239 return lnet_handle_send(sd);
2242 CERROR("Internal Error. Expected to have a best_lpni: "
2244 libcfs_nid2str(sd->sd_src_nid),
2245 libcfs_nid2str(sd->sd_dst_nid));
2251 * Peer doesn't have a local network. Let's see if there is
2252 * a remote network we can reach it on.
2254 return PASS_THROUGH;
2259 * Source NID not specified
2264 * Source NID not speified
2265 * Remote destination
2268 * In both of these cases if we're sending a response, ACK or REPLY, then
2269 * we need to send to the destination NID provided.
2271 * In the remote case let's deal with MR routers.
2276 lnet_handle_any_mr_dst(struct lnet_send_data *sd)
2279 struct lnet_peer *gw_peer = NULL;
2280 struct lnet_peer_ni *gw_lpni = NULL;
2283 * handle sending a response to a remote peer here so we don't
2284 * have to worry about it if we hit lnet_handle_any_mr_dsta()
2286 if (sd->sd_send_case & REMOTE_DST &&
2287 sd->sd_send_case & SND_RESP) {
2288 struct lnet_peer_ni *gw;
2289 struct lnet_peer *gw_peer;
2291 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw,
2294 CERROR("Can't send response to %s. "
2295 "No route available\n",
2296 libcfs_nid2str(sd->sd_dst_nid));
2297 return -EHOSTUNREACH;
2300 sd->sd_best_lpni = gw;
2301 sd->sd_peer = gw_peer;
2303 return lnet_handle_send(sd);
2307 * Even though the NID for the peer might not be on a local network,
2308 * since the peer is MR there could be other interfaces on the
2309 * local network. In that case we'd still like to prefer the local
2310 * network over the routed network. If we're unable to do that
2311 * then we select the best router among the different routed networks,
2312 * and if the router is MR then we can deal with it as such.
2314 rc = lnet_handle_any_mr_dsta(sd);
2315 if (rc != PASS_THROUGH)
2319 * TODO; One possible enhancement is to run the selection
2320 * algorithm on the peer. However for remote peers the credits are
2321 * not decremented, so we'll be basically going over the peer NIs
2322 * in round robin. An MR router will run the selection algorithm
2323 * on the next-hop interfaces.
2325 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2330 sd->sd_send_case &= ~LOCAL_DST;
2331 sd->sd_send_case |= REMOTE_DST;
2333 sd->sd_peer = gw_peer;
2334 sd->sd_best_lpni = gw_lpni;
2336 return lnet_handle_send(sd);
2340 * Source not specified
2341 * Remote destination
2344 * Must send to the specified peer NID using the same source NID that
2345 * we've used before. If it's the first time to talk to that peer then
2346 * find the source NI and assign it as preferred to that peer
2349 lnet_handle_any_router_nmr_dst(struct lnet_send_data *sd)
2352 struct lnet_peer_ni *gw_lpni = NULL;
2353 struct lnet_peer *gw_peer = NULL;
2356 * Let's set if we have a preferred NI to talk to this NMR peer
2358 sd->sd_best_ni = lnet_find_existing_preferred_best_ni(sd);
2361 * find the router and that'll find the best NI if we didn't find
2364 rc = lnet_handle_find_routed_path(sd, sd->sd_dst_nid, &gw_lpni,
2370 * set the best_ni we've chosen as the preferred one for
2373 lnet_set_non_mr_pref_nid(sd);
2375 /* we'll be sending to the gw */
2376 sd->sd_best_lpni = gw_lpni;
2377 sd->sd_peer = gw_peer;
2379 return lnet_handle_send(sd);
2383 lnet_handle_send_case_locked(struct lnet_send_data *sd)
2386 * turn off the SND_RESP bit.
2387 * It will be checked in the case handling
2389 __u32 send_case = sd->sd_send_case &= ~SND_RESP ;
2391 CDEBUG(D_NET, "Source %s%s to %s %s %s destination\n",
2392 (send_case & SRC_SPEC) ? "Specified: " : "ANY",
2393 (send_case & SRC_SPEC) ? libcfs_nid2str(sd->sd_src_nid) : "",
2394 (send_case & MR_DST) ? "MR: " : "NMR: ",
2395 libcfs_nid2str(sd->sd_dst_nid),
2396 (send_case & LOCAL_DST) ? "local" : "routed");
2398 switch (send_case) {
2400 * For all cases where the source is specified, we should always
2401 * use the destination NID, whether it's an MR destination or not,
2402 * since we're continuing a series of related messages for the
2405 case SRC_SPEC_LOCAL_NMR_DST:
2406 return lnet_handle_spec_local_nmr_dst(sd);
2407 case SRC_SPEC_LOCAL_MR_DST:
2408 return lnet_handle_spec_local_mr_dst(sd);
2409 case SRC_SPEC_ROUTER_NMR_DST:
2410 case SRC_SPEC_ROUTER_MR_DST:
2411 return lnet_handle_spec_router_dst(sd);
2412 case SRC_ANY_LOCAL_NMR_DST:
2413 return lnet_handle_any_local_nmr_dst(sd);
2414 case SRC_ANY_LOCAL_MR_DST:
2415 case SRC_ANY_ROUTER_MR_DST:
2416 return lnet_handle_any_mr_dst(sd);
2417 case SRC_ANY_ROUTER_NMR_DST:
2418 return lnet_handle_any_router_nmr_dst(sd);
2420 CERROR("Unknown send case\n");
2426 lnet_select_pathway(lnet_nid_t src_nid, lnet_nid_t dst_nid,
2427 struct lnet_msg *msg, lnet_nid_t rtr_nid)
2429 struct lnet_peer_ni *lpni;
2430 struct lnet_peer *peer;
2431 struct lnet_send_data send_data;
2434 __u32 send_case = 0;
2436 memset(&send_data, 0, sizeof(send_data));
2439 * get an initial CPT to use for locking. The idea here is not to
2440 * serialize the calls to select_pathway, so that as many
2441 * operations can run concurrently as possible. To do that we use
2442 * the CPT where this call is being executed. Later on when we
2443 * determine the CPT to use in lnet_message_commit, we switch the
2444 * lock and check if there was any configuration change. If none,
2445 * then we proceed, if there is, then we restart the operation.
2447 cpt = lnet_net_lock_current();
2449 md_cpt = lnet_cpt_of_md(msg->msg_md, msg->msg_offset);
2450 if (md_cpt == CFS_CPT_ANY)
2456 * If we're being asked to send to the loopback interface, there
2457 * is no need to go through any selection. We can just shortcut
2458 * the entire process and send over lolnd
2460 if (LNET_NETTYP(LNET_NIDNET(dst_nid)) == LOLND) {
2461 /* No send credit hassles with LOLND */
2462 lnet_ni_addref_locked(the_lnet.ln_loni, cpt);
2463 msg->msg_hdr.dest_nid = cpu_to_le64(the_lnet.ln_loni->ni_nid);
2464 if (!msg->msg_routing)
2465 msg->msg_hdr.src_nid =
2466 cpu_to_le64(the_lnet.ln_loni->ni_nid);
2467 msg->msg_target.nid = the_lnet.ln_loni->ni_nid;
2468 lnet_msg_commit(msg, cpt);
2469 msg->msg_txni = the_lnet.ln_loni;
2470 lnet_net_unlock(cpt);
2472 return LNET_CREDIT_OK;
2476 * find an existing peer_ni, or create one and mark it as having been
2477 * created due to network traffic. This call will create the
2478 * peer->peer_net->peer_ni tree.
2480 lpni = lnet_nid2peerni_locked(dst_nid, LNET_NID_ANY, cpt);
2482 lnet_net_unlock(cpt);
2483 return PTR_ERR(lpni);
2487 * Now that we have a peer_ni, check if we want to discover
2488 * the peer. Traffic to the LNET_RESERVED_PORTAL should not
2489 * trigger discovery.
2491 peer = lpni->lpni_peer_net->lpn_peer;
2492 if (lnet_msg_discovery(msg) && !lnet_peer_is_uptodate(peer)) {
2493 lnet_nid_t primary_nid;
2494 rc = lnet_discover_peer_locked(lpni, cpt, false);
2496 lnet_peer_ni_decref_locked(lpni);
2497 lnet_net_unlock(cpt);
2500 /* The peer may have changed. */
2501 peer = lpni->lpni_peer_net->lpn_peer;
2502 /* queue message and return */
2503 msg->msg_src_nid_param = src_nid;
2504 msg->msg_rtr_nid_param = rtr_nid;
2505 msg->msg_sending = 0;
2506 list_add_tail(&msg->msg_list, &peer->lp_dc_pendq);
2507 lnet_peer_ni_decref_locked(lpni);
2508 primary_nid = peer->lp_primary_nid;
2509 lnet_net_unlock(cpt);
2511 CDEBUG(D_NET, "%s pending discovery\n",
2512 libcfs_nid2str(primary_nid));
2514 return LNET_DC_WAIT;
2516 lnet_peer_ni_decref_locked(lpni);
2518 /* If peer is not healthy then can not send anything to it */
2519 if (!lnet_is_peer_healthy_locked(peer)) {
2520 lnet_net_unlock(cpt);
2521 return -EHOSTUNREACH;
2525 * Identify the different send cases
2527 if (src_nid == LNET_NID_ANY)
2528 send_case |= SRC_ANY;
2530 send_case |= SRC_SPEC;
2532 if (lnet_get_net_locked(LNET_NIDNET(dst_nid)))
2533 send_case |= LOCAL_DST;
2535 send_case |= REMOTE_DST;
2537 if (!lnet_peer_is_multi_rail(peer))
2538 send_case |= NMR_DST;
2540 send_case |= MR_DST;
2542 if (msg->msg_type == LNET_MSG_REPLY ||
2543 msg->msg_type == LNET_MSG_ACK)
2544 send_case |= SND_RESP;
2546 /* assign parameters to the send_data */
2547 send_data.sd_msg = msg;
2548 send_data.sd_rtr_nid = rtr_nid;
2549 send_data.sd_src_nid = src_nid;
2550 send_data.sd_dst_nid = dst_nid;
2551 send_data.sd_best_lpni = lpni;
2553 * keep a pointer to the final destination in case we're going to
2554 * route, so we'll need to access it later
2556 send_data.sd_final_dst_lpni = lpni;
2557 send_data.sd_peer = peer;
2558 send_data.sd_md_cpt = md_cpt;
2559 send_data.sd_cpt = cpt;
2560 send_data.sd_send_case = send_case;
2562 rc = lnet_handle_send_case_locked(&send_data);
2564 if (rc == REPEAT_SEND)
2567 lnet_net_unlock(send_data.sd_cpt);
2573 lnet_send(lnet_nid_t src_nid, struct lnet_msg *msg, lnet_nid_t rtr_nid)
2575 lnet_nid_t dst_nid = msg->msg_target.nid;
2579 * NB: rtr_nid is set to LNET_NID_ANY for all current use-cases,
2580 * but we might want to use pre-determined router for ACK/REPLY
2583 /* NB: ni != NULL == interface pre-determined (ACK/REPLY) */
2584 LASSERT (msg->msg_txpeer == NULL);
2585 LASSERT (!msg->msg_sending);
2586 LASSERT (!msg->msg_target_is_router);
2587 LASSERT (!msg->msg_receiving);
2589 msg->msg_sending = 1;
2591 LASSERT(!msg->msg_tx_committed);
2593 rc = lnet_select_pathway(src_nid, dst_nid, msg, rtr_nid);
2597 if (rc == LNET_CREDIT_OK)
2598 lnet_ni_send(msg->msg_txni, msg);
2600 /* rc == LNET_CREDIT_OK or LNET_CREDIT_WAIT or LNET_DC_WAIT */
2605 lnet_drop_message(struct lnet_ni *ni, int cpt, void *private, unsigned int nob,
2609 lnet_incr_stats(&ni->ni_stats, msg_type, LNET_STATS_TYPE_DROP);
2610 the_lnet.ln_counters[cpt]->drop_count++;
2611 the_lnet.ln_counters[cpt]->drop_length += nob;
2612 lnet_net_unlock(cpt);
2614 lnet_ni_recv(ni, private, NULL, 0, 0, 0, nob);
2618 lnet_recv_put(struct lnet_ni *ni, struct lnet_msg *msg)
2620 struct lnet_hdr *hdr = &msg->msg_hdr;
2622 if (msg->msg_wanted != 0)
2623 lnet_setpayloadbuffer(msg);
2625 lnet_build_msg_event(msg, LNET_EVENT_PUT);
2627 /* Must I ACK? If so I'll grab the ack_wmd out of the header and put
2628 * it back into the ACK during lnet_finalize() */
2629 msg->msg_ack = (!lnet_is_wire_handle_none(&hdr->msg.put.ack_wmd) &&
2630 (msg->msg_md->md_options & LNET_MD_ACK_DISABLE) == 0);
2632 lnet_ni_recv(ni, msg->msg_private, msg, msg->msg_rx_delayed,
2633 msg->msg_offset, msg->msg_wanted, hdr->payload_length);
2637 lnet_parse_put(struct lnet_ni *ni, struct lnet_msg *msg)
2639 struct lnet_hdr *hdr = &msg->msg_hdr;
2640 struct lnet_match_info info;
2644 /* Convert put fields to host byte order */
2645 hdr->msg.put.match_bits = le64_to_cpu(hdr->msg.put.match_bits);
2646 hdr->msg.put.ptl_index = le32_to_cpu(hdr->msg.put.ptl_index);
2647 hdr->msg.put.offset = le32_to_cpu(hdr->msg.put.offset);
2649 /* Primary peer NID. */
2650 info.mi_id.nid = msg->msg_initiator;
2651 info.mi_id.pid = hdr->src_pid;
2652 info.mi_opc = LNET_MD_OP_PUT;
2653 info.mi_portal = hdr->msg.put.ptl_index;
2654 info.mi_rlength = hdr->payload_length;
2655 info.mi_roffset = hdr->msg.put.offset;
2656 info.mi_mbits = hdr->msg.put.match_bits;
2657 info.mi_cpt = lnet_cpt_of_nid(msg->msg_rxpeer->lpni_nid, ni);
2659 msg->msg_rx_ready_delay = ni->ni_net->net_lnd->lnd_eager_recv == NULL;
2660 ready_delay = msg->msg_rx_ready_delay;
2663 rc = lnet_ptl_match_md(&info, msg);
2668 case LNET_MATCHMD_OK:
2669 lnet_recv_put(ni, msg);
2672 case LNET_MATCHMD_NONE:
2674 /* no eager_recv or has already called it, should
2675 * have been attached on delayed list */
2678 rc = lnet_ni_eager_recv(ni, msg);
2685 case LNET_MATCHMD_DROP:
2686 CNETERR("Dropping PUT from %s portal %d match %llu"
2687 " offset %d length %d: %d\n",
2688 libcfs_id2str(info.mi_id), info.mi_portal,
2689 info.mi_mbits, info.mi_roffset, info.mi_rlength, rc);
2691 return -ENOENT; /* -ve: OK but no match */
2696 lnet_parse_get(struct lnet_ni *ni, struct lnet_msg *msg, int rdma_get)
2698 struct lnet_match_info info;
2699 struct lnet_hdr *hdr = &msg->msg_hdr;
2700 struct lnet_process_id source_id;
2701 struct lnet_handle_wire reply_wmd;
2704 /* Convert get fields to host byte order */
2705 hdr->msg.get.match_bits = le64_to_cpu(hdr->msg.get.match_bits);
2706 hdr->msg.get.ptl_index = le32_to_cpu(hdr->msg.get.ptl_index);
2707 hdr->msg.get.sink_length = le32_to_cpu(hdr->msg.get.sink_length);
2708 hdr->msg.get.src_offset = le32_to_cpu(hdr->msg.get.src_offset);
2710 source_id.nid = hdr->src_nid;
2711 source_id.pid = hdr->src_pid;
2712 /* Primary peer NID */
2713 info.mi_id.nid = msg->msg_initiator;
2714 info.mi_id.pid = hdr->src_pid;
2715 info.mi_opc = LNET_MD_OP_GET;
2716 info.mi_portal = hdr->msg.get.ptl_index;
2717 info.mi_rlength = hdr->msg.get.sink_length;
2718 info.mi_roffset = hdr->msg.get.src_offset;
2719 info.mi_mbits = hdr->msg.get.match_bits;
2720 info.mi_cpt = lnet_cpt_of_nid(msg->msg_rxpeer->lpni_nid, ni);
2722 rc = lnet_ptl_match_md(&info, msg);
2723 if (rc == LNET_MATCHMD_DROP) {
2724 CNETERR("Dropping GET from %s portal %d match %llu"
2725 " offset %d length %d\n",
2726 libcfs_id2str(info.mi_id), info.mi_portal,
2727 info.mi_mbits, info.mi_roffset, info.mi_rlength);
2728 return -ENOENT; /* -ve: OK but no match */
2731 LASSERT(rc == LNET_MATCHMD_OK);
2733 lnet_build_msg_event(msg, LNET_EVENT_GET);
2735 reply_wmd = hdr->msg.get.return_wmd;
2737 lnet_prep_send(msg, LNET_MSG_REPLY, source_id,
2738 msg->msg_offset, msg->msg_wanted);
2740 msg->msg_hdr.msg.reply.dst_wmd = reply_wmd;
2743 /* The LND completes the REPLY from her recv procedure */
2744 lnet_ni_recv(ni, msg->msg_private, msg, 0,
2745 msg->msg_offset, msg->msg_len, msg->msg_len);
2749 lnet_ni_recv(ni, msg->msg_private, NULL, 0, 0, 0, 0);
2750 msg->msg_receiving = 0;
2752 rc = lnet_send(ni->ni_nid, msg, LNET_NID_ANY);
2754 /* didn't get as far as lnet_ni_send() */
2755 CERROR("%s: Unable to send REPLY for GET from %s: %d\n",
2756 libcfs_nid2str(ni->ni_nid),
2757 libcfs_id2str(info.mi_id), rc);
2759 lnet_finalize(msg, rc);
2766 lnet_parse_reply(struct lnet_ni *ni, struct lnet_msg *msg)
2768 void *private = msg->msg_private;
2769 struct lnet_hdr *hdr = &msg->msg_hdr;
2770 struct lnet_process_id src = {0};
2771 struct lnet_libmd *md;
2776 cpt = lnet_cpt_of_cookie(hdr->msg.reply.dst_wmd.wh_object_cookie);
2779 src.nid = hdr->src_nid;
2780 src.pid = hdr->src_pid;
2782 /* NB handles only looked up by creator (no flips) */
2783 md = lnet_wire_handle2md(&hdr->msg.reply.dst_wmd);
2784 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
2785 CNETERR("%s: Dropping REPLY from %s for %s "
2787 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
2788 (md == NULL) ? "invalid" : "inactive",
2789 hdr->msg.reply.dst_wmd.wh_interface_cookie,
2790 hdr->msg.reply.dst_wmd.wh_object_cookie);
2791 if (md != NULL && md->md_me != NULL)
2792 CERROR("REPLY MD also attached to portal %d\n",
2793 md->md_me->me_portal);
2795 lnet_res_unlock(cpt);
2796 return -ENOENT; /* -ve: OK but no match */
2799 LASSERT(md->md_offset == 0);
2801 rlength = hdr->payload_length;
2802 mlength = MIN(rlength, (int)md->md_length);
2804 if (mlength < rlength &&
2805 (md->md_options & LNET_MD_TRUNCATE) == 0) {
2806 CNETERR("%s: Dropping REPLY from %s length %d "
2807 "for MD %#llx would overflow (%d)\n",
2808 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
2809 rlength, hdr->msg.reply.dst_wmd.wh_object_cookie,
2811 lnet_res_unlock(cpt);
2812 return -ENOENT; /* -ve: OK but no match */
2815 CDEBUG(D_NET, "%s: Reply from %s of length %d/%d into md %#llx\n",
2816 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
2817 mlength, rlength, hdr->msg.reply.dst_wmd.wh_object_cookie);
2819 lnet_msg_attach_md(msg, md, 0, mlength);
2822 lnet_setpayloadbuffer(msg);
2824 lnet_res_unlock(cpt);
2826 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
2828 lnet_ni_recv(ni, private, msg, 0, 0, mlength, rlength);
2833 lnet_parse_ack(struct lnet_ni *ni, struct lnet_msg *msg)
2835 struct lnet_hdr *hdr = &msg->msg_hdr;
2836 struct lnet_process_id src = {0};
2837 struct lnet_libmd *md;
2840 src.nid = hdr->src_nid;
2841 src.pid = hdr->src_pid;
2843 /* Convert ack fields to host byte order */
2844 hdr->msg.ack.match_bits = le64_to_cpu(hdr->msg.ack.match_bits);
2845 hdr->msg.ack.mlength = le32_to_cpu(hdr->msg.ack.mlength);
2847 cpt = lnet_cpt_of_cookie(hdr->msg.ack.dst_wmd.wh_object_cookie);
2850 /* NB handles only looked up by creator (no flips) */
2851 md = lnet_wire_handle2md(&hdr->msg.ack.dst_wmd);
2852 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
2853 /* Don't moan; this is expected */
2855 "%s: Dropping ACK from %s to %s MD %#llx.%#llx\n",
2856 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
2857 (md == NULL) ? "invalid" : "inactive",
2858 hdr->msg.ack.dst_wmd.wh_interface_cookie,
2859 hdr->msg.ack.dst_wmd.wh_object_cookie);
2860 if (md != NULL && md->md_me != NULL)
2861 CERROR("Source MD also attached to portal %d\n",
2862 md->md_me->me_portal);
2864 lnet_res_unlock(cpt);
2865 return -ENOENT; /* -ve! */
2868 CDEBUG(D_NET, "%s: ACK from %s into md %#llx\n",
2869 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
2870 hdr->msg.ack.dst_wmd.wh_object_cookie);
2872 lnet_msg_attach_md(msg, md, 0, 0);
2874 lnet_res_unlock(cpt);
2876 lnet_build_msg_event(msg, LNET_EVENT_ACK);
2878 lnet_ni_recv(ni, msg->msg_private, msg, 0, 0, 0, msg->msg_len);
2883 * \retval LNET_CREDIT_OK If \a msg is forwarded
2884 * \retval LNET_CREDIT_WAIT If \a msg is blocked because w/o buffer
2885 * \retval -ve error code
2888 lnet_parse_forward_locked(struct lnet_ni *ni, struct lnet_msg *msg)
2892 if (!the_lnet.ln_routing)
2895 if (msg->msg_rxpeer->lpni_rtrcredits <= 0 ||
2896 lnet_msg2bufpool(msg)->rbp_credits <= 0) {
2897 if (ni->ni_net->net_lnd->lnd_eager_recv == NULL) {
2898 msg->msg_rx_ready_delay = 1;
2900 lnet_net_unlock(msg->msg_rx_cpt);
2901 rc = lnet_ni_eager_recv(ni, msg);
2902 lnet_net_lock(msg->msg_rx_cpt);
2907 rc = lnet_post_routed_recv_locked(msg, 0);
2912 lnet_parse_local(struct lnet_ni *ni, struct lnet_msg *msg)
2916 switch (msg->msg_type) {
2918 rc = lnet_parse_ack(ni, msg);
2921 rc = lnet_parse_put(ni, msg);
2924 rc = lnet_parse_get(ni, msg, msg->msg_rdma_get);
2926 case LNET_MSG_REPLY:
2927 rc = lnet_parse_reply(ni, msg);
2929 default: /* prevent an unused label if !kernel */
2934 LASSERT(rc == 0 || rc == -ENOENT);
2939 lnet_msgtyp2str (int type)
2948 case LNET_MSG_REPLY:
2950 case LNET_MSG_HELLO:
2953 return ("<UNKNOWN>");
2958 lnet_print_hdr(struct lnet_hdr *hdr)
2960 struct lnet_process_id src = {
2961 .nid = hdr->src_nid,
2962 .pid = hdr->src_pid,
2964 struct lnet_process_id dst = {
2965 .nid = hdr->dest_nid,
2966 .pid = hdr->dest_pid,
2968 char *type_str = lnet_msgtyp2str(hdr->type);
2970 CWARN("P3 Header at %p of type %s\n", hdr, type_str);
2971 CWARN(" From %s\n", libcfs_id2str(src));
2972 CWARN(" To %s\n", libcfs_id2str(dst));
2974 switch (hdr->type) {
2979 CWARN(" Ptl index %d, ack md %#llx.%#llx, "
2980 "match bits %llu\n",
2981 hdr->msg.put.ptl_index,
2982 hdr->msg.put.ack_wmd.wh_interface_cookie,
2983 hdr->msg.put.ack_wmd.wh_object_cookie,
2984 hdr->msg.put.match_bits);
2985 CWARN(" Length %d, offset %d, hdr data %#llx\n",
2986 hdr->payload_length, hdr->msg.put.offset,
2987 hdr->msg.put.hdr_data);
2991 CWARN(" Ptl index %d, return md %#llx.%#llx, "
2992 "match bits %llu\n", hdr->msg.get.ptl_index,
2993 hdr->msg.get.return_wmd.wh_interface_cookie,
2994 hdr->msg.get.return_wmd.wh_object_cookie,
2995 hdr->msg.get.match_bits);
2996 CWARN(" Length %d, src offset %d\n",
2997 hdr->msg.get.sink_length,
2998 hdr->msg.get.src_offset);
3002 CWARN(" dst md %#llx.%#llx, "
3003 "manipulated length %d\n",
3004 hdr->msg.ack.dst_wmd.wh_interface_cookie,
3005 hdr->msg.ack.dst_wmd.wh_object_cookie,
3006 hdr->msg.ack.mlength);
3009 case LNET_MSG_REPLY:
3010 CWARN(" dst md %#llx.%#llx, "
3012 hdr->msg.reply.dst_wmd.wh_interface_cookie,
3013 hdr->msg.reply.dst_wmd.wh_object_cookie,
3014 hdr->payload_length);
3020 lnet_parse(struct lnet_ni *ni, struct lnet_hdr *hdr, lnet_nid_t from_nid,
3021 void *private, int rdma_req)
3026 struct lnet_msg *msg;
3027 lnet_pid_t dest_pid;
3028 lnet_nid_t dest_nid;
3030 struct lnet_peer_ni *lpni;
3031 __u32 payload_length;
3034 LASSERT (!in_interrupt ());
3036 type = le32_to_cpu(hdr->type);
3037 src_nid = le64_to_cpu(hdr->src_nid);
3038 dest_nid = le64_to_cpu(hdr->dest_nid);
3039 dest_pid = le32_to_cpu(hdr->dest_pid);
3040 payload_length = le32_to_cpu(hdr->payload_length);
3042 for_me = (ni->ni_nid == dest_nid);
3043 cpt = lnet_cpt_of_nid(from_nid, ni);
3045 CDEBUG(D_NET, "TRACE: %s(%s) <- %s : %s - %s\n",
3046 libcfs_nid2str(dest_nid),
3047 libcfs_nid2str(ni->ni_nid),
3048 libcfs_nid2str(src_nid),
3049 lnet_msgtyp2str(type),
3050 (for_me) ? "for me" : "routed");
3055 if (payload_length > 0) {
3056 CERROR("%s, src %s: bad %s payload %d (0 expected)\n",
3057 libcfs_nid2str(from_nid),
3058 libcfs_nid2str(src_nid),
3059 lnet_msgtyp2str(type), payload_length);
3065 case LNET_MSG_REPLY:
3066 if (payload_length >
3067 (__u32)(for_me ? LNET_MAX_PAYLOAD : LNET_MTU)) {
3068 CERROR("%s, src %s: bad %s payload %d "
3069 "(%d max expected)\n",
3070 libcfs_nid2str(from_nid),
3071 libcfs_nid2str(src_nid),
3072 lnet_msgtyp2str(type),
3074 for_me ? LNET_MAX_PAYLOAD : LNET_MTU);
3080 CERROR("%s, src %s: Bad message type 0x%x\n",
3081 libcfs_nid2str(from_nid),
3082 libcfs_nid2str(src_nid), type);
3086 if (the_lnet.ln_routing &&
3087 ni->ni_last_alive != ktime_get_real_seconds()) {
3088 /* NB: so far here is the only place to set NI status to "up */
3090 ni->ni_last_alive = ktime_get_real_seconds();
3091 if (ni->ni_status != NULL &&
3092 ni->ni_status->ns_status == LNET_NI_STATUS_DOWN)
3093 ni->ni_status->ns_status = LNET_NI_STATUS_UP;
3097 /* Regard a bad destination NID as a protocol error. Senders should
3098 * know what they're doing; if they don't they're misconfigured, buggy
3099 * or malicious so we chop them off at the knees :) */
3102 if (LNET_NIDNET(dest_nid) == LNET_NIDNET(ni->ni_nid)) {
3103 /* should have gone direct */
3104 CERROR("%s, src %s: Bad dest nid %s "
3105 "(should have been sent direct)\n",
3106 libcfs_nid2str(from_nid),
3107 libcfs_nid2str(src_nid),
3108 libcfs_nid2str(dest_nid));
3112 if (lnet_islocalnid(dest_nid)) {
3113 /* dest is another local NI; sender should have used
3114 * this node's NID on its own network */
3115 CERROR("%s, src %s: Bad dest nid %s "
3116 "(it's my nid but on a different network)\n",
3117 libcfs_nid2str(from_nid),
3118 libcfs_nid2str(src_nid),
3119 libcfs_nid2str(dest_nid));
3123 if (rdma_req && type == LNET_MSG_GET) {
3124 CERROR("%s, src %s: Bad optimized GET for %s "
3125 "(final destination must be me)\n",
3126 libcfs_nid2str(from_nid),
3127 libcfs_nid2str(src_nid),
3128 libcfs_nid2str(dest_nid));
3132 if (!the_lnet.ln_routing) {
3133 CERROR("%s, src %s: Dropping message for %s "
3134 "(routing not enabled)\n",
3135 libcfs_nid2str(from_nid),
3136 libcfs_nid2str(src_nid),
3137 libcfs_nid2str(dest_nid));
3142 /* Message looks OK; we're not going to return an error, so we MUST
3143 * call back lnd_recv() come what may... */
3145 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
3146 fail_peer(src_nid, 0)) { /* shall we now? */
3147 CERROR("%s, src %s: Dropping %s to simulate failure\n",
3148 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
3149 lnet_msgtyp2str(type));
3153 if (!list_empty(&the_lnet.ln_drop_rules) &&
3154 lnet_drop_rule_match(hdr)) {
3155 CDEBUG(D_NET, "%s, src %s, dst %s: Dropping %s to simulate"
3156 "silent message loss\n",
3157 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
3158 libcfs_nid2str(dest_nid), lnet_msgtyp2str(type));
3163 msg = lnet_msg_alloc();
3165 CERROR("%s, src %s: Dropping %s (out of memory)\n",
3166 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
3167 lnet_msgtyp2str(type));
3171 /* msg zeroed in lnet_msg_alloc; i.e. flags all clear,
3172 * pointers NULL etc */
3174 msg->msg_type = type;
3175 msg->msg_private = private;
3176 msg->msg_receiving = 1;
3177 msg->msg_rdma_get = rdma_req;
3178 msg->msg_len = msg->msg_wanted = payload_length;
3179 msg->msg_offset = 0;
3180 msg->msg_hdr = *hdr;
3181 /* for building message event */
3182 msg->msg_from = from_nid;
3184 msg->msg_target.pid = dest_pid;
3185 msg->msg_target.nid = dest_nid;
3186 msg->msg_routing = 1;
3189 /* convert common msg->hdr fields to host byteorder */
3190 msg->msg_hdr.type = type;
3191 msg->msg_hdr.src_nid = src_nid;
3192 msg->msg_hdr.src_pid = le32_to_cpu(msg->msg_hdr.src_pid);
3193 msg->msg_hdr.dest_nid = dest_nid;
3194 msg->msg_hdr.dest_pid = dest_pid;
3195 msg->msg_hdr.payload_length = payload_length;
3199 lpni = lnet_nid2peerni_locked(from_nid, ni->ni_nid, cpt);
3201 lnet_net_unlock(cpt);
3202 CERROR("%s, src %s: Dropping %s "
3203 "(error %ld looking up sender)\n",
3204 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
3205 lnet_msgtyp2str(type), PTR_ERR(lpni));
3207 if (rc == -ESHUTDOWN)
3208 /* We are shutting down. Don't do anything more */
3212 msg->msg_rxpeer = lpni;
3214 lnet_ni_addref_locked(ni, cpt);
3215 /* Multi-Rail: Primary NID of source. */
3216 msg->msg_initiator = lnet_peer_primary_nid_locked(src_nid);
3218 if (lnet_isrouter(msg->msg_rxpeer)) {
3219 lnet_peer_set_alive(msg->msg_rxpeer);
3220 if (avoid_asym_router_failure &&
3221 LNET_NIDNET(src_nid) != LNET_NIDNET(from_nid)) {
3222 /* received a remote message from router, update
3223 * remote NI status on this router.
3224 * NB: multi-hop routed message will be ignored.
3226 lnet_router_ni_update_locked(msg->msg_rxpeer,
3227 LNET_NIDNET(src_nid));
3231 lnet_msg_commit(msg, cpt);
3233 /* message delay simulation */
3234 if (unlikely(!list_empty(&the_lnet.ln_delay_rules) &&
3235 lnet_delay_rule_match_locked(hdr, msg))) {
3236 lnet_net_unlock(cpt);
3241 rc = lnet_parse_forward_locked(ni, msg);
3242 lnet_net_unlock(cpt);
3247 if (rc == LNET_CREDIT_OK) {
3248 lnet_ni_recv(ni, msg->msg_private, msg, 0,
3249 0, payload_length, payload_length);
3254 lnet_net_unlock(cpt);
3256 rc = lnet_parse_local(ni, msg);
3262 LASSERT(msg->msg_md == NULL);
3263 lnet_finalize(msg, rc);
3266 lnet_drop_message(ni, cpt, private, payload_length, type);
3269 EXPORT_SYMBOL(lnet_parse);
3272 lnet_drop_delayed_msg_list(struct list_head *head, char *reason)
3274 while (!list_empty(head)) {
3275 struct lnet_process_id id = {0};
3276 struct lnet_msg *msg;
3278 msg = list_entry(head->next, struct lnet_msg, msg_list);
3279 list_del(&msg->msg_list);
3281 id.nid = msg->msg_hdr.src_nid;
3282 id.pid = msg->msg_hdr.src_pid;
3284 LASSERT(msg->msg_md == NULL);
3285 LASSERT(msg->msg_rx_delayed);
3286 LASSERT(msg->msg_rxpeer != NULL);
3287 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
3289 CWARN("Dropping delayed PUT from %s portal %d match %llu"
3290 " offset %d length %d: %s\n",
3292 msg->msg_hdr.msg.put.ptl_index,
3293 msg->msg_hdr.msg.put.match_bits,
3294 msg->msg_hdr.msg.put.offset,
3295 msg->msg_hdr.payload_length, reason);
3297 /* NB I can't drop msg's ref on msg_rxpeer until after I've
3298 * called lnet_drop_message(), so I just hang onto msg as well
3299 * until that's done */
3301 lnet_drop_message(msg->msg_rxni, msg->msg_rx_cpt,
3302 msg->msg_private, msg->msg_len,
3305 * NB: message will not generate event because w/o attached MD,
3306 * but we still should give error code so lnet_msg_decommit()
3307 * can skip counters operations and other checks.
3309 lnet_finalize(msg, -ENOENT);
3314 lnet_recv_delayed_msg_list(struct list_head *head)
3316 while (!list_empty(head)) {
3317 struct lnet_msg *msg;
3318 struct lnet_process_id id;
3320 msg = list_entry(head->next, struct lnet_msg, msg_list);
3321 list_del(&msg->msg_list);
3323 /* md won't disappear under me, since each msg
3324 * holds a ref on it */
3326 id.nid = msg->msg_hdr.src_nid;
3327 id.pid = msg->msg_hdr.src_pid;
3329 LASSERT(msg->msg_rx_delayed);
3330 LASSERT(msg->msg_md != NULL);
3331 LASSERT(msg->msg_rxpeer != NULL);
3332 LASSERT(msg->msg_rxni != NULL);
3333 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
3335 CDEBUG(D_NET, "Resuming delayed PUT from %s portal %d "
3336 "match %llu offset %d length %d.\n",
3337 libcfs_id2str(id), msg->msg_hdr.msg.put.ptl_index,
3338 msg->msg_hdr.msg.put.match_bits,
3339 msg->msg_hdr.msg.put.offset,
3340 msg->msg_hdr.payload_length);
3342 lnet_recv_put(msg->msg_rxni, msg);
3347 * Initiate an asynchronous PUT operation.
3349 * There are several events associated with a PUT: completion of the send on
3350 * the initiator node (LNET_EVENT_SEND), and when the send completes
3351 * successfully, the receipt of an acknowledgment (LNET_EVENT_ACK) indicating
3352 * that the operation was accepted by the target. The event LNET_EVENT_PUT is
3353 * used at the target node to indicate the completion of incoming data
3356 * The local events will be logged in the EQ associated with the MD pointed to
3357 * by \a mdh handle. Using a MD without an associated EQ results in these
3358 * events being discarded. In this case, the caller must have another
3359 * mechanism (e.g., a higher level protocol) for determining when it is safe
3360 * to modify the memory region associated with the MD.
3362 * Note that LNet does not guarantee the order of LNET_EVENT_SEND and
3363 * LNET_EVENT_ACK, though intuitively ACK should happen after SEND.
3365 * \param self Indicates the NID of a local interface through which to send
3366 * the PUT request. Use LNET_NID_ANY to let LNet choose one by itself.
3367 * \param mdh A handle for the MD that describes the memory to be sent. The MD
3368 * must be "free floating" (See LNetMDBind()).
3369 * \param ack Controls whether an acknowledgment is requested.
3370 * Acknowledgments are only sent when they are requested by the initiating
3371 * process and the target MD enables them.
3372 * \param target A process identifier for the target process.
3373 * \param portal The index in the \a target's portal table.
3374 * \param match_bits The match bits to use for MD selection at the target
3376 * \param offset The offset into the target MD (only used when the target
3377 * MD has the LNET_MD_MANAGE_REMOTE option set).
3378 * \param hdr_data 64 bits of user data that can be included in the message
3379 * header. This data is written to an event queue entry at the target if an
3380 * EQ is present on the matching MD.
3382 * \retval 0 Success, and only in this case events will be generated
3383 * and logged to EQ (if it exists).
3384 * \retval -EIO Simulated failure.
3385 * \retval -ENOMEM Memory allocation failure.
3386 * \retval -ENOENT Invalid MD object.
3388 * \see struct lnet_event::hdr_data and lnet_event_kind_t.
3391 LNetPut(lnet_nid_t self, struct lnet_handle_md mdh, enum lnet_ack_req ack,
3392 struct lnet_process_id target, unsigned int portal,
3393 __u64 match_bits, unsigned int offset,
3396 struct lnet_msg *msg;
3397 struct lnet_libmd *md;
3401 LASSERT(the_lnet.ln_refcount > 0);
3403 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
3404 fail_peer(target.nid, 1)) { /* shall we now? */
3405 CERROR("Dropping PUT to %s: simulated failure\n",
3406 libcfs_id2str(target));
3410 msg = lnet_msg_alloc();
3412 CERROR("Dropping PUT to %s: ENOMEM on struct lnet_msg\n",
3413 libcfs_id2str(target));
3416 msg->msg_vmflush = !!memory_pressure_get();
3418 cpt = lnet_cpt_of_cookie(mdh.cookie);
3421 md = lnet_handle2md(&mdh);
3422 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
3423 CERROR("Dropping PUT (%llu:%d:%s): MD (%d) invalid\n",
3424 match_bits, portal, libcfs_id2str(target),
3425 md == NULL ? -1 : md->md_threshold);
3426 if (md != NULL && md->md_me != NULL)
3427 CERROR("Source MD also attached to portal %d\n",
3428 md->md_me->me_portal);
3429 lnet_res_unlock(cpt);
3435 CDEBUG(D_NET, "LNetPut -> %s\n", libcfs_id2str(target));
3437 lnet_msg_attach_md(msg, md, 0, 0);
3439 lnet_prep_send(msg, LNET_MSG_PUT, target, 0, md->md_length);
3441 msg->msg_hdr.msg.put.match_bits = cpu_to_le64(match_bits);
3442 msg->msg_hdr.msg.put.ptl_index = cpu_to_le32(portal);
3443 msg->msg_hdr.msg.put.offset = cpu_to_le32(offset);
3444 msg->msg_hdr.msg.put.hdr_data = hdr_data;
3446 /* NB handles only looked up by creator (no flips) */
3447 if (ack == LNET_ACK_REQ) {
3448 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
3449 the_lnet.ln_interface_cookie;
3450 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
3451 md->md_lh.lh_cookie;
3453 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
3454 LNET_WIRE_HANDLE_COOKIE_NONE;
3455 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
3456 LNET_WIRE_HANDLE_COOKIE_NONE;
3459 lnet_res_unlock(cpt);
3461 lnet_build_msg_event(msg, LNET_EVENT_SEND);
3463 rc = lnet_send(self, msg, LNET_NID_ANY);
3465 CNETERR("Error sending PUT to %s: %d\n",
3466 libcfs_id2str(target), rc);
3467 lnet_finalize(msg, rc);
3470 /* completion will be signalled by an event */
3473 EXPORT_SYMBOL(LNetPut);
3476 * The LND can DMA direct to the GET md (i.e. no REPLY msg). This
3477 * returns a msg for the LND to pass to lnet_finalize() when the sink
3478 * data has been received.
3480 * CAVEAT EMPTOR: 'getmsg' is the original GET, which is freed when
3481 * lnet_finalize() is called on it, so the LND must call this first
3484 lnet_create_reply_msg(struct lnet_ni *ni, struct lnet_msg *getmsg)
3486 struct lnet_msg *msg = lnet_msg_alloc();
3487 struct lnet_libmd *getmd = getmsg->msg_md;
3488 struct lnet_process_id peer_id = getmsg->msg_target;
3491 LASSERT(!getmsg->msg_target_is_router);
3492 LASSERT(!getmsg->msg_routing);
3495 CERROR("%s: Dropping REPLY from %s: can't allocate msg\n",
3496 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id));
3500 cpt = lnet_cpt_of_cookie(getmd->md_lh.lh_cookie);
3503 LASSERT(getmd->md_refcount > 0);
3505 if (getmd->md_threshold == 0) {
3506 CERROR("%s: Dropping REPLY from %s for inactive MD %p\n",
3507 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id),
3509 lnet_res_unlock(cpt);
3513 LASSERT(getmd->md_offset == 0);
3515 CDEBUG(D_NET, "%s: Reply from %s md %p\n",
3516 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id), getmd);
3518 /* setup information for lnet_build_msg_event */
3519 msg->msg_initiator = getmsg->msg_txpeer->lpni_peer_net->lpn_peer->lp_primary_nid;
3520 msg->msg_from = peer_id.nid;
3521 msg->msg_type = LNET_MSG_GET; /* flag this msg as an "optimized" GET */
3522 msg->msg_hdr.src_nid = peer_id.nid;
3523 msg->msg_hdr.payload_length = getmd->md_length;
3524 msg->msg_receiving = 1; /* required by lnet_msg_attach_md */
3526 lnet_msg_attach_md(msg, getmd, getmd->md_offset, getmd->md_length);
3527 lnet_res_unlock(cpt);
3529 cpt = lnet_cpt_of_nid(peer_id.nid, ni);
3532 lnet_msg_commit(msg, cpt);
3533 lnet_net_unlock(cpt);
3535 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
3540 cpt = lnet_cpt_of_nid(peer_id.nid, ni);
3543 lnet_incr_stats(&ni->ni_stats, LNET_MSG_GET, LNET_STATS_TYPE_DROP);
3544 the_lnet.ln_counters[cpt]->drop_count++;
3545 the_lnet.ln_counters[cpt]->drop_length += getmd->md_length;
3546 lnet_net_unlock(cpt);
3553 EXPORT_SYMBOL(lnet_create_reply_msg);
3556 lnet_set_reply_msg_len(struct lnet_ni *ni, struct lnet_msg *reply,
3559 /* Set the REPLY length, now the RDMA that elides the REPLY message has
3560 * completed and I know it. */
3561 LASSERT(reply != NULL);
3562 LASSERT(reply->msg_type == LNET_MSG_GET);
3563 LASSERT(reply->msg_ev.type == LNET_EVENT_REPLY);
3565 /* NB I trusted my peer to RDMA. If she tells me she's written beyond
3566 * the end of my buffer, I might as well be dead. */
3567 LASSERT(len <= reply->msg_ev.mlength);
3569 reply->msg_ev.mlength = len;
3571 EXPORT_SYMBOL(lnet_set_reply_msg_len);
3574 * Initiate an asynchronous GET operation.
3576 * On the initiator node, an LNET_EVENT_SEND is logged when the GET request
3577 * is sent, and an LNET_EVENT_REPLY is logged when the data returned from
3578 * the target node in the REPLY has been written to local MD.
3580 * On the target node, an LNET_EVENT_GET is logged when the GET request
3581 * arrives and is accepted into a MD.
3583 * \param self,target,portal,match_bits,offset See the discussion in LNetPut().
3584 * \param mdh A handle for the MD that describes the memory into which the
3585 * requested data will be received. The MD must be "free floating" (See LNetMDBind()).
3587 * \retval 0 Success, and only in this case events will be generated
3588 * and logged to EQ (if it exists) of the MD.
3589 * \retval -EIO Simulated failure.
3590 * \retval -ENOMEM Memory allocation failure.
3591 * \retval -ENOENT Invalid MD object.
3594 LNetGet(lnet_nid_t self, struct lnet_handle_md mdh,
3595 struct lnet_process_id target, unsigned int portal,
3596 __u64 match_bits, unsigned int offset)
3598 struct lnet_msg *msg;
3599 struct lnet_libmd *md;
3603 LASSERT(the_lnet.ln_refcount > 0);
3605 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
3606 fail_peer(target.nid, 1)) /* shall we now? */
3608 CERROR("Dropping GET to %s: simulated failure\n",
3609 libcfs_id2str(target));
3613 msg = lnet_msg_alloc();
3615 CERROR("Dropping GET to %s: ENOMEM on struct lnet_msg\n",
3616 libcfs_id2str(target));
3620 cpt = lnet_cpt_of_cookie(mdh.cookie);
3623 md = lnet_handle2md(&mdh);
3624 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
3625 CERROR("Dropping GET (%llu:%d:%s): MD (%d) invalid\n",
3626 match_bits, portal, libcfs_id2str(target),
3627 md == NULL ? -1 : md->md_threshold);
3628 if (md != NULL && md->md_me != NULL)
3629 CERROR("REPLY MD also attached to portal %d\n",
3630 md->md_me->me_portal);
3632 lnet_res_unlock(cpt);
3638 CDEBUG(D_NET, "LNetGet -> %s\n", libcfs_id2str(target));
3640 lnet_msg_attach_md(msg, md, 0, 0);
3642 lnet_prep_send(msg, LNET_MSG_GET, target, 0, 0);
3644 msg->msg_hdr.msg.get.match_bits = cpu_to_le64(match_bits);
3645 msg->msg_hdr.msg.get.ptl_index = cpu_to_le32(portal);
3646 msg->msg_hdr.msg.get.src_offset = cpu_to_le32(offset);
3647 msg->msg_hdr.msg.get.sink_length = cpu_to_le32(md->md_length);
3649 /* NB handles only looked up by creator (no flips) */
3650 msg->msg_hdr.msg.get.return_wmd.wh_interface_cookie =
3651 the_lnet.ln_interface_cookie;
3652 msg->msg_hdr.msg.get.return_wmd.wh_object_cookie =
3653 md->md_lh.lh_cookie;
3655 lnet_res_unlock(cpt);
3657 lnet_build_msg_event(msg, LNET_EVENT_SEND);
3659 rc = lnet_send(self, msg, LNET_NID_ANY);
3661 CNETERR("Error sending GET to %s: %d\n",
3662 libcfs_id2str(target), rc);
3663 lnet_finalize(msg, rc);
3666 /* completion will be signalled by an event */
3669 EXPORT_SYMBOL(LNetGet);
3672 * Calculate distance to node at \a dstnid.
3674 * \param dstnid Target NID.
3675 * \param srcnidp If not NULL, NID of the local interface to reach \a dstnid
3677 * \param orderp If not NULL, order of the route to reach \a dstnid is saved
3680 * \retval 0 If \a dstnid belongs to a local interface, and reserved option
3681 * local_nid_dist_zero is set, which is the default.
3682 * \retval positives Distance to target NID, i.e. number of hops plus one.
3683 * \retval -EHOSTUNREACH If \a dstnid is not reachable.
3686 LNetDist(lnet_nid_t dstnid, lnet_nid_t *srcnidp, __u32 *orderp)
3688 struct list_head *e;
3689 struct lnet_ni *ni = NULL;
3690 struct lnet_remotenet *rnet;
3691 __u32 dstnet = LNET_NIDNET(dstnid);
3695 struct list_head *rn_list;
3697 /* if !local_nid_dist_zero, I don't return a distance of 0 ever
3698 * (when lustre sees a distance of 0, it substitutes 0@lo), so I
3699 * keep order 0 free for 0@lo and order 1 free for a local NID
3702 LASSERT(the_lnet.ln_refcount > 0);
3704 cpt = lnet_net_lock_current();
3706 while ((ni = lnet_get_next_ni_locked(NULL, ni))) {
3707 if (ni->ni_nid == dstnid) {
3708 if (srcnidp != NULL)
3710 if (orderp != NULL) {
3711 if (LNET_NETTYP(LNET_NIDNET(dstnid)) == LOLND)
3716 lnet_net_unlock(cpt);
3718 return local_nid_dist_zero ? 0 : 1;
3721 if (LNET_NIDNET(ni->ni_nid) == dstnet) {
3722 /* Check if ni was originally created in
3723 * current net namespace.
3724 * If not, assign order above 0xffff0000,
3725 * to make this ni not a priority. */
3726 if (!net_eq(ni->ni_net_ns, current->nsproxy->net_ns))
3727 order += 0xffff0000;
3729 if (srcnidp != NULL)
3730 *srcnidp = ni->ni_nid;
3733 lnet_net_unlock(cpt);
3740 rn_list = lnet_net2rnethash(dstnet);
3741 list_for_each(e, rn_list) {
3742 rnet = list_entry(e, struct lnet_remotenet, lrn_list);
3744 if (rnet->lrn_net == dstnet) {
3745 struct lnet_route *route;
3746 struct lnet_route *shortest = NULL;
3747 __u32 shortest_hops = LNET_UNDEFINED_HOPS;
3750 LASSERT(!list_empty(&rnet->lrn_routes));
3752 list_for_each_entry(route, &rnet->lrn_routes,
3754 route_hops = route->lr_hops;
3755 if (route_hops == LNET_UNDEFINED_HOPS)
3757 if (shortest == NULL ||
3758 route_hops < shortest_hops) {
3760 shortest_hops = route_hops;
3764 LASSERT(shortest != NULL);
3765 hops = shortest_hops;
3766 if (srcnidp != NULL) {
3767 ni = lnet_get_next_ni_locked(
3768 shortest->lr_gateway->lpni_net,
3770 *srcnidp = ni->ni_nid;
3774 lnet_net_unlock(cpt);
3780 lnet_net_unlock(cpt);
3781 return -EHOSTUNREACH;
3783 EXPORT_SYMBOL(LNetDist);