1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lnet/lnet/lib-move.c
38 * Data movement routines
41 #define DEBUG_SUBSYSTEM S_LNET
43 #include <lnet/lib-lnet.h>
45 static int local_nid_dist_zero = 1;
46 CFS_MODULE_PARM(local_nid_dist_zero, "i", int, 0444,
50 static void lnet_commit_md (lnet_libmd_t *md, lnet_msg_t *msg);
52 #define LNET_MATCHMD_NONE 0 /* Didn't match */
53 #define LNET_MATCHMD_OK 1 /* Matched OK */
54 #define LNET_MATCHMD_DROP 2 /* Must be discarded */
57 lnet_try_match_md (int index, int op_mask, lnet_process_id_t src,
58 unsigned int rlength, unsigned int roffset,
59 __u64 match_bits, lnet_libmd_t *md, lnet_msg_t *msg,
60 unsigned int *mlength_out, unsigned int *offset_out)
62 /* ALWAYS called holding the LNET_LOCK, and can't LNET_UNLOCK;
63 * lnet_match_blocked_msg() relies on this to avoid races */
66 lnet_me_t *me = md->md_me;
68 /* mismatched MD op */
69 if ((md->md_options & op_mask) == 0)
70 return LNET_MATCHMD_NONE;
73 if (lnet_md_exhausted(md))
74 return LNET_MATCHMD_NONE;
76 /* mismatched ME nid/pid? */
77 if (me->me_match_id.nid != LNET_NID_ANY &&
78 me->me_match_id.nid != src.nid)
79 return LNET_MATCHMD_NONE;
81 if (me->me_match_id.pid != LNET_PID_ANY &&
82 me->me_match_id.pid != src.pid)
83 return LNET_MATCHMD_NONE;
85 /* mismatched ME matchbits? */
86 if (((me->me_match_bits ^ match_bits) & ~me->me_ignore_bits) != 0)
87 return LNET_MATCHMD_NONE;
89 /* Hurrah! This _is_ a match; check it out... */
91 if ((md->md_options & LNET_MD_MANAGE_REMOTE) == 0)
92 offset = md->md_offset;
96 if ((md->md_options & LNET_MD_MAX_SIZE) != 0) {
97 mlength = md->md_max_size;
98 LASSERT (md->md_offset + mlength <= md->md_length);
100 mlength = md->md_length - offset;
103 if (rlength <= mlength) { /* fits in allowed space */
105 } else if ((md->md_options & LNET_MD_TRUNCATE) == 0) {
106 /* this packet _really_ is too big */
107 CERROR("Matching packet from %s, match "LPU64
108 " length %d too big: %d left, %d allowed\n",
109 libcfs_id2str(src), match_bits, rlength,
110 md->md_length - offset, mlength);
112 return LNET_MATCHMD_DROP;
115 /* Commit to this ME/MD */
116 CDEBUG(D_NET, "Incoming %s index %x from %s of "
117 "length %d/%d into md "LPX64" [%d] + %d\n",
118 (op_mask == LNET_MD_OP_PUT) ? "put" : "get",
119 index, libcfs_id2str(src), mlength, rlength,
120 md->md_lh.lh_cookie, md->md_niov, offset);
122 lnet_commit_md(md, msg);
123 md->md_offset = offset + mlength;
125 /* NB Caller will set ev.type and ev.hdr_data */
126 msg->msg_ev.initiator = src;
127 msg->msg_ev.pt_index = index;
128 msg->msg_ev.match_bits = match_bits;
129 msg->msg_ev.rlength = rlength;
130 msg->msg_ev.mlength = mlength;
131 msg->msg_ev.offset = offset;
133 lnet_md_deconstruct(md, &msg->msg_ev.md);
134 lnet_md2handle(&msg->msg_ev.md_handle, md);
136 *offset_out = offset;
137 *mlength_out = mlength;
139 /* Auto-unlink NOW, so the ME gets unlinked if required.
140 * We bumped md->md_refcount above so the MD just gets flagged
141 * for unlink when it is finalized. */
142 if ((md->md_flags & LNET_MD_FLAG_AUTO_UNLINK) != 0 &&
143 lnet_md_exhausted(md)) {
147 return LNET_MATCHMD_OK;
151 lnet_match_md(int index, int op_mask, lnet_process_id_t src,
152 unsigned int rlength, unsigned int roffset,
153 __u64 match_bits, lnet_msg_t *msg,
154 unsigned int *mlength_out, unsigned int *offset_out,
155 lnet_libmd_t **md_out)
157 lnet_portal_t *ptl = &the_lnet.ln_portals[index];
164 CDEBUG (D_NET, "Request from %s of length %d into portal %d "
165 "MB="LPX64"\n", libcfs_id2str(src), rlength, index, match_bits);
167 if (index < 0 || index >= the_lnet.ln_nportals) {
168 CERROR("Invalid portal %d not in [0-%d]\n",
169 index, the_lnet.ln_nportals);
170 return LNET_MATCHMD_DROP;
173 head = lnet_portal_me_head(index, src, match_bits);
174 if (head == NULL) /* nobody posted anything on this portal */
177 cfs_list_for_each_entry_safe_typed (me, tmp, head,
178 lnet_me_t, me_list) {
181 /* ME attached but MD not attached yet */
185 LASSERT (me == md->md_me);
187 rc = lnet_try_match_md(index, op_mask, src, rlength,
188 roffset, match_bits, md, msg,
189 mlength_out, offset_out);
194 case LNET_MATCHMD_NONE:
197 case LNET_MATCHMD_OK:
199 return LNET_MATCHMD_OK;
201 case LNET_MATCHMD_DROP:
202 return LNET_MATCHMD_DROP;
208 if (op_mask == LNET_MD_OP_GET ||
209 !lnet_portal_is_lazy(ptl))
210 return LNET_MATCHMD_DROP;
212 return LNET_MATCHMD_NONE;
216 lnet_fail_nid (lnet_nid_t nid, unsigned int threshold)
218 lnet_test_peer_t *tp;
223 LASSERT (the_lnet.ln_init);
225 if (threshold != 0) {
226 /* Adding a new entry */
227 LIBCFS_ALLOC(tp, sizeof(*tp));
232 tp->tp_threshold = threshold;
235 cfs_list_add_tail (&tp->tp_list, &the_lnet.ln_test_peers);
240 /* removing entries */
241 CFS_INIT_LIST_HEAD (&cull);
245 cfs_list_for_each_safe (el, next, &the_lnet.ln_test_peers) {
246 tp = cfs_list_entry (el, lnet_test_peer_t, tp_list);
248 if (tp->tp_threshold == 0 || /* needs culling anyway */
249 nid == LNET_NID_ANY || /* removing all entries */
250 tp->tp_nid == nid) /* matched this one */
252 cfs_list_del (&tp->tp_list);
253 cfs_list_add (&tp->tp_list, &cull);
259 while (!cfs_list_empty (&cull)) {
260 tp = cfs_list_entry (cull.next, lnet_test_peer_t, tp_list);
262 cfs_list_del (&tp->tp_list);
263 LIBCFS_FREE(tp, sizeof (*tp));
269 fail_peer (lnet_nid_t nid, int outgoing)
271 lnet_test_peer_t *tp;
277 CFS_INIT_LIST_HEAD (&cull);
281 cfs_list_for_each_safe (el, next, &the_lnet.ln_test_peers) {
282 tp = cfs_list_entry (el, lnet_test_peer_t, tp_list);
284 if (tp->tp_threshold == 0) {
287 /* only cull zombies on outgoing tests,
288 * since we may be at interrupt priority on
289 * incoming messages. */
290 cfs_list_del (&tp->tp_list);
291 cfs_list_add (&tp->tp_list, &cull);
296 if (tp->tp_nid == LNET_NID_ANY || /* fail every peer */
297 nid == tp->tp_nid) { /* fail this peer */
300 if (tp->tp_threshold != LNET_MD_THRESH_INF) {
303 tp->tp_threshold == 0) {
305 cfs_list_del (&tp->tp_list);
306 cfs_list_add (&tp->tp_list, &cull);
315 while (!cfs_list_empty (&cull)) {
316 tp = cfs_list_entry (cull.next, lnet_test_peer_t, tp_list);
317 cfs_list_del (&tp->tp_list);
319 LIBCFS_FREE(tp, sizeof (*tp));
326 lnet_iov_nob (unsigned int niov, struct iovec *iov)
328 unsigned int nob = 0;
331 nob += (iov++)->iov_len;
337 lnet_copy_iov2iov (unsigned int ndiov, struct iovec *diov, unsigned int doffset,
338 unsigned int nsiov, struct iovec *siov, unsigned int soffset,
341 /* NB diov, siov are READ-ONLY */
342 unsigned int this_nob;
347 /* skip complete frags before 'doffset' */
349 while (doffset >= diov->iov_len) {
350 doffset -= diov->iov_len;
356 /* skip complete frags before 'soffset' */
358 while (soffset >= siov->iov_len) {
359 soffset -= siov->iov_len;
368 this_nob = MIN(diov->iov_len - doffset,
369 siov->iov_len - soffset);
370 this_nob = MIN(this_nob, nob);
372 memcpy ((char *)diov->iov_base + doffset,
373 (char *)siov->iov_base + soffset, this_nob);
376 if (diov->iov_len > doffset + this_nob) {
384 if (siov->iov_len > soffset + this_nob) {
395 lnet_extract_iov (int dst_niov, struct iovec *dst,
396 int src_niov, struct iovec *src,
397 unsigned int offset, unsigned int len)
399 /* Initialise 'dst' to the subset of 'src' starting at 'offset',
400 * for exactly 'len' bytes, and return the number of entries.
401 * NB not destructive to 'src' */
402 unsigned int frag_len;
405 if (len == 0) /* no data => */
406 return (0); /* no frags */
408 LASSERT (src_niov > 0);
409 while (offset >= src->iov_len) { /* skip initial frags */
410 offset -= src->iov_len;
413 LASSERT (src_niov > 0);
418 LASSERT (src_niov > 0);
419 LASSERT ((int)niov <= dst_niov);
421 frag_len = src->iov_len - offset;
422 dst->iov_base = ((char *)src->iov_base) + offset;
424 if (len <= frag_len) {
429 dst->iov_len = frag_len;
442 lnet_kiov_nob (unsigned int niov, lnet_kiov_t *kiov)
449 lnet_copy_kiov2kiov (unsigned int ndkiov, lnet_kiov_t *dkiov, unsigned int doffset,
450 unsigned int nskiov, lnet_kiov_t *skiov, unsigned int soffset,
457 lnet_copy_kiov2iov (unsigned int niov, struct iovec *iov, unsigned int iovoffset,
458 unsigned int nkiov, lnet_kiov_t *kiov, unsigned int kiovoffset,
465 lnet_copy_iov2kiov (unsigned int nkiov, lnet_kiov_t *kiov, unsigned int kiovoffset,
466 unsigned int niov, struct iovec *iov, unsigned int iovoffset,
473 lnet_extract_kiov (int dst_niov, lnet_kiov_t *dst,
474 int src_niov, lnet_kiov_t *src,
475 unsigned int offset, unsigned int len)
480 #else /* __KERNEL__ */
483 lnet_kiov_nob (unsigned int niov, lnet_kiov_t *kiov)
485 unsigned int nob = 0;
488 nob += (kiov++)->kiov_len;
494 lnet_copy_kiov2kiov (unsigned int ndiov, lnet_kiov_t *diov, unsigned int doffset,
495 unsigned int nsiov, lnet_kiov_t *siov, unsigned int soffset,
498 /* NB diov, siov are READ-ONLY */
499 unsigned int this_nob;
506 LASSERT (!cfs_in_interrupt ());
509 while (doffset >= diov->kiov_len) {
510 doffset -= diov->kiov_len;
517 while (soffset >= siov->kiov_len) {
518 soffset -= siov->kiov_len;
527 this_nob = MIN(diov->kiov_len - doffset,
528 siov->kiov_len - soffset);
529 this_nob = MIN(this_nob, nob);
532 daddr = ((char *)cfs_kmap(diov->kiov_page)) +
533 diov->kiov_offset + doffset;
535 saddr = ((char *)cfs_kmap(siov->kiov_page)) +
536 siov->kiov_offset + soffset;
538 /* Vanishing risk of kmap deadlock when mapping 2 pages.
539 * However in practice at least one of the kiovs will be mapped
540 * kernel pages and the map/unmap will be NOOPs */
542 memcpy (daddr, saddr, this_nob);
545 if (diov->kiov_len > doffset + this_nob) {
549 cfs_kunmap(diov->kiov_page);
556 if (siov->kiov_len > soffset + this_nob) {
560 cfs_kunmap(siov->kiov_page);
569 cfs_kunmap(diov->kiov_page);
571 cfs_kunmap(siov->kiov_page);
575 lnet_copy_kiov2iov (unsigned int niov, struct iovec *iov, unsigned int iovoffset,
576 unsigned int nkiov, lnet_kiov_t *kiov, unsigned int kiovoffset,
579 /* NB iov, kiov are READ-ONLY */
580 unsigned int this_nob;
586 LASSERT (!cfs_in_interrupt ());
589 while (iovoffset >= iov->iov_len) {
590 iovoffset -= iov->iov_len;
597 while (kiovoffset >= kiov->kiov_len) {
598 kiovoffset -= kiov->kiov_len;
607 this_nob = MIN(iov->iov_len - iovoffset,
608 kiov->kiov_len - kiovoffset);
609 this_nob = MIN(this_nob, nob);
612 addr = ((char *)cfs_kmap(kiov->kiov_page)) +
613 kiov->kiov_offset + kiovoffset;
615 memcpy ((char *)iov->iov_base + iovoffset, addr, this_nob);
618 if (iov->iov_len > iovoffset + this_nob) {
619 iovoffset += this_nob;
626 if (kiov->kiov_len > kiovoffset + this_nob) {
628 kiovoffset += this_nob;
630 cfs_kunmap(kiov->kiov_page);
640 cfs_kunmap(kiov->kiov_page);
644 lnet_copy_iov2kiov (unsigned int nkiov, lnet_kiov_t *kiov, unsigned int kiovoffset,
645 unsigned int niov, struct iovec *iov, unsigned int iovoffset,
648 /* NB kiov, iov are READ-ONLY */
649 unsigned int this_nob;
655 LASSERT (!cfs_in_interrupt ());
658 while (kiovoffset >= kiov->kiov_len) {
659 kiovoffset -= kiov->kiov_len;
666 while (iovoffset >= iov->iov_len) {
667 iovoffset -= iov->iov_len;
676 this_nob = MIN(kiov->kiov_len - kiovoffset,
677 iov->iov_len - iovoffset);
678 this_nob = MIN(this_nob, nob);
681 addr = ((char *)cfs_kmap(kiov->kiov_page)) +
682 kiov->kiov_offset + kiovoffset;
684 memcpy (addr, (char *)iov->iov_base + iovoffset, this_nob);
687 if (kiov->kiov_len > kiovoffset + this_nob) {
689 kiovoffset += this_nob;
691 cfs_kunmap(kiov->kiov_page);
698 if (iov->iov_len > iovoffset + this_nob) {
699 iovoffset += this_nob;
708 cfs_kunmap(kiov->kiov_page);
712 lnet_extract_kiov (int dst_niov, lnet_kiov_t *dst,
713 int src_niov, lnet_kiov_t *src,
714 unsigned int offset, unsigned int len)
716 /* Initialise 'dst' to the subset of 'src' starting at 'offset',
717 * for exactly 'len' bytes, and return the number of entries.
718 * NB not destructive to 'src' */
719 unsigned int frag_len;
722 if (len == 0) /* no data => */
723 return (0); /* no frags */
725 LASSERT (src_niov > 0);
726 while (offset >= src->kiov_len) { /* skip initial frags */
727 offset -= src->kiov_len;
730 LASSERT (src_niov > 0);
735 LASSERT (src_niov > 0);
736 LASSERT ((int)niov <= dst_niov);
738 frag_len = src->kiov_len - offset;
739 dst->kiov_page = src->kiov_page;
740 dst->kiov_offset = src->kiov_offset + offset;
742 if (len <= frag_len) {
744 LASSERT (dst->kiov_offset + dst->kiov_len <= CFS_PAGE_SIZE);
748 dst->kiov_len = frag_len;
749 LASSERT (dst->kiov_offset + dst->kiov_len <= CFS_PAGE_SIZE);
762 lnet_ni_recv(lnet_ni_t *ni, void *private, lnet_msg_t *msg, int delayed,
763 unsigned int offset, unsigned int mlen, unsigned int rlen)
765 unsigned int niov = 0;
766 struct iovec *iov = NULL;
767 lnet_kiov_t *kiov = NULL;
770 LASSERT (!cfs_in_interrupt ());
771 LASSERT (mlen == 0 || msg != NULL);
774 LASSERT(msg->msg_receiving);
775 LASSERT(!msg->msg_sending);
776 LASSERT(rlen == msg->msg_len);
777 LASSERT(mlen <= msg->msg_len);
779 msg->msg_wanted = mlen;
780 msg->msg_offset = offset;
781 msg->msg_receiving = 0;
784 niov = msg->msg_niov;
786 kiov = msg->msg_kiov;
789 LASSERT ((iov == NULL) != (kiov == NULL));
793 rc = (ni->ni_lnd->lnd_recv)(ni, private, msg, delayed,
794 niov, iov, kiov, offset, mlen, rlen);
796 lnet_finalize(ni, msg, rc);
800 lnet_compare_routes(lnet_route_t *r1, lnet_route_t *r2)
802 lnet_peer_t *p1 = r1->lr_gateway;
803 lnet_peer_t *p2 = r2->lr_gateway;
805 if (r1->lr_hops < r2->lr_hops)
808 if (r1->lr_hops > r2->lr_hops)
811 if (p1->lp_txqnob < p2->lp_txqnob)
814 if (p1->lp_txqnob > p2->lp_txqnob)
817 if (p1->lp_txcredits > p2->lp_txcredits)
820 if (p1->lp_txcredits < p2->lp_txcredits)
828 lnet_setpayloadbuffer(lnet_msg_t *msg)
830 lnet_libmd_t *md = msg->msg_md;
832 LASSERT (msg->msg_len > 0);
833 LASSERT (!msg->msg_routing);
834 LASSERT (md != NULL);
835 LASSERT (msg->msg_niov == 0);
836 LASSERT (msg->msg_iov == NULL);
837 LASSERT (msg->msg_kiov == NULL);
839 msg->msg_niov = md->md_niov;
840 if ((md->md_options & LNET_MD_KIOV) != 0)
841 msg->msg_kiov = md->md_iov.kiov;
843 msg->msg_iov = md->md_iov.iov;
847 lnet_prep_send(lnet_msg_t *msg, int type, lnet_process_id_t target,
848 unsigned int offset, unsigned int len)
850 msg->msg_type = type;
851 msg->msg_target = target;
853 msg->msg_offset = offset;
856 lnet_setpayloadbuffer(msg);
858 memset (&msg->msg_hdr, 0, sizeof (msg->msg_hdr));
859 msg->msg_hdr.type = cpu_to_le32(type);
860 msg->msg_hdr.dest_nid = cpu_to_le64(target.nid);
861 msg->msg_hdr.dest_pid = cpu_to_le32(target.pid);
862 /* src_nid will be set later */
863 msg->msg_hdr.src_pid = cpu_to_le32(the_lnet.ln_pid);
864 msg->msg_hdr.payload_length = cpu_to_le32(len);
868 lnet_ni_send(lnet_ni_t *ni, lnet_msg_t *msg)
870 void *priv = msg->msg_private;
873 LASSERT (!cfs_in_interrupt ());
874 LASSERT (LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND ||
875 (msg->msg_txcredit && msg->msg_peertxcredit));
877 rc = (ni->ni_lnd->lnd_send)(ni, priv, msg);
879 lnet_finalize(ni, msg, rc);
883 lnet_eager_recv_locked(lnet_msg_t *msg)
889 LASSERT (!msg->msg_delayed);
890 msg->msg_delayed = 1;
892 LASSERT (msg->msg_receiving);
893 LASSERT (!msg->msg_sending);
895 peer = msg->msg_rxpeer;
898 if (ni->ni_lnd->lnd_eager_recv != NULL) {
901 rc = (ni->ni_lnd->lnd_eager_recv)(ni, msg->msg_private, msg,
904 CERROR("recv from %s / send to %s aborted: "
905 "eager_recv failed %d\n",
906 libcfs_nid2str(peer->lp_nid),
907 libcfs_id2str(msg->msg_target), rc);
908 LASSERT (rc < 0); /* required by my callers */
917 /* NB: caller shall hold a ref on 'lp' as I'd drop LNET_LOCK */
919 lnet_ni_peer_alive(lnet_peer_t *lp)
921 cfs_time_t last_alive = 0;
922 lnet_ni_t *ni = lp->lp_ni;
924 LASSERT (lnet_peer_aliveness_enabled(lp));
925 LASSERT (ni->ni_lnd->lnd_query != NULL);
926 LASSERT (the_lnet.ln_routing == 1);
929 (ni->ni_lnd->lnd_query)(ni, lp->lp_nid, &last_alive);
932 lp->lp_last_query = cfs_time_current();
934 if (last_alive != 0) /* NI has updated timestamp */
935 lp->lp_last_alive = last_alive;
938 /* NB: always called with LNET_LOCK held */
940 lnet_peer_is_alive (lnet_peer_t *lp, cfs_time_t now)
945 LASSERT (lnet_peer_aliveness_enabled(lp));
946 LASSERT (the_lnet.ln_routing == 1);
948 /* Trust lnet_notify() if it has more recent aliveness news, but
949 * ignore the initial assumed death (see lnet_peers_start_down()).
951 if (!lp->lp_alive && lp->lp_alive_count > 0 &&
952 cfs_time_aftereq(lp->lp_timestamp, lp->lp_last_alive))
955 deadline = cfs_time_add(lp->lp_last_alive,
956 cfs_time_seconds(lp->lp_ni->ni_peertimeout));
957 alive = cfs_time_after(deadline, now);
959 /* Update obsolete lp_alive except for routers assumed to be dead
960 * initially, because router checker would update aliveness in this
961 * case, and moreover lp_last_alive at peer creation is assumed.
963 if (alive && !lp->lp_alive &&
964 !(lnet_isrouter(lp) && lp->lp_alive_count == 0))
965 lnet_notify_locked(lp, 0, 1, lp->lp_last_alive);
971 /* NB: returns 1 when alive, 0 when dead, negative when error;
972 * may drop the LNET_LOCK */
974 lnet_peer_alive_locked (lnet_peer_t *lp)
976 cfs_time_t now = cfs_time_current();
978 /* LU-630: only router checks peer health. */
979 if (the_lnet.ln_routing == 0)
982 if (!lnet_peer_aliveness_enabled(lp))
985 if (lnet_peer_is_alive(lp, now))
988 /* Peer appears dead, but we should avoid frequent NI queries (at
989 * most once per lnet_queryinterval seconds). */
990 if (lp->lp_last_query != 0) {
991 static const int lnet_queryinterval = 1;
993 cfs_time_t next_query =
994 cfs_time_add(lp->lp_last_query,
995 cfs_time_seconds(lnet_queryinterval));
997 if (cfs_time_before(now, next_query)) {
999 CWARN("Unexpected aliveness of peer %s: "
1000 "%d < %d (%d/%d)\n",
1001 libcfs_nid2str(lp->lp_nid),
1002 (int)now, (int)next_query,
1004 lp->lp_ni->ni_peertimeout);
1009 /* query NI for latest aliveness news */
1010 lnet_ni_peer_alive(lp);
1012 if (lnet_peer_is_alive(lp, now))
1015 lnet_notify_locked(lp, 0, 0, lp->lp_last_alive);
1020 lnet_post_send_locked (lnet_msg_t *msg, int do_send)
1022 /* lnet_send is going to LNET_UNLOCK immediately after this, so it sets
1023 * do_send FALSE and I don't do the unlock/send/lock bit. I return
1024 * EAGAIN if msg blocked, EHOSTUNREACH if msg_txpeer appears dead, and
1025 * 0 if sent or OK to send */
1026 lnet_peer_t *lp = msg->msg_txpeer;
1027 lnet_ni_t *ni = lp->lp_ni;
1029 /* non-lnet_send() callers have checked before */
1030 LASSERT (!do_send || msg->msg_delayed);
1031 LASSERT (!msg->msg_receiving);
1033 /* NB 'lp' is always the next hop */
1034 if ((msg->msg_target.pid & LNET_PID_USERFLAG) == 0 &&
1035 lnet_peer_alive_locked(lp) == 0) {
1036 the_lnet.ln_counters.drop_count++;
1037 the_lnet.ln_counters.drop_length += msg->msg_len;
1040 CNETERR("Dropping message for %s: peer not alive\n",
1041 libcfs_id2str(msg->msg_target));
1043 lnet_finalize(ni, msg, -EHOSTUNREACH);
1046 return EHOSTUNREACH;
1049 if (!msg->msg_peertxcredit) {
1050 LASSERT ((lp->lp_txcredits < 0) ==
1051 !cfs_list_empty(&lp->lp_txq));
1053 msg->msg_peertxcredit = 1;
1054 lp->lp_txqnob += msg->msg_len + sizeof(lnet_hdr_t);
1057 if (lp->lp_txcredits < lp->lp_mintxcredits)
1058 lp->lp_mintxcredits = lp->lp_txcredits;
1060 if (lp->lp_txcredits < 0) {
1061 msg->msg_delayed = 1;
1062 cfs_list_add_tail(&msg->msg_list, &lp->lp_txq);
1067 if (!msg->msg_txcredit) {
1068 LASSERT ((ni->ni_txcredits < 0) ==
1069 !cfs_list_empty(&ni->ni_txq));
1071 msg->msg_txcredit = 1;
1074 if (ni->ni_txcredits < ni->ni_mintxcredits)
1075 ni->ni_mintxcredits = ni->ni_txcredits;
1077 if (ni->ni_txcredits < 0) {
1078 msg->msg_delayed = 1;
1079 cfs_list_add_tail(&msg->msg_list, &ni->ni_txq);
1086 lnet_ni_send(ni, msg);
1094 lnet_commit_routedmsg (lnet_msg_t *msg)
1096 /* ALWAYS called holding the LNET_LOCK */
1097 LASSERT (msg->msg_routing);
1099 the_lnet.ln_counters.msgs_alloc++;
1100 if (the_lnet.ln_counters.msgs_alloc >
1101 the_lnet.ln_counters.msgs_max)
1102 the_lnet.ln_counters.msgs_max =
1103 the_lnet.ln_counters.msgs_alloc;
1105 the_lnet.ln_counters.route_count++;
1106 the_lnet.ln_counters.route_length += msg->msg_len;
1108 LASSERT (!msg->msg_onactivelist);
1109 msg->msg_onactivelist = 1;
1110 cfs_list_add (&msg->msg_activelist, &the_lnet.ln_active_msgs);
1114 lnet_msg2bufpool(lnet_msg_t *msg)
1116 lnet_rtrbufpool_t *rbp = &the_lnet.ln_rtrpools[0];
1118 LASSERT (msg->msg_len <= LNET_MTU);
1119 while (msg->msg_len > (unsigned int)rbp->rbp_npages * CFS_PAGE_SIZE) {
1121 LASSERT (rbp < &the_lnet.ln_rtrpools[LNET_NRBPOOLS]);
1128 lnet_post_routed_recv_locked (lnet_msg_t *msg, int do_recv)
1130 /* lnet_parse is going to LNET_UNLOCK immediately after this, so it
1131 * sets do_recv FALSE and I don't do the unlock/send/lock bit. I
1132 * return EAGAIN if msg blocked and 0 if received or OK to receive */
1133 lnet_peer_t *lp = msg->msg_rxpeer;
1134 lnet_rtrbufpool_t *rbp;
1137 LASSERT (msg->msg_iov == NULL);
1138 LASSERT (msg->msg_kiov == NULL);
1139 LASSERT (msg->msg_niov == 0);
1140 LASSERT (msg->msg_routing);
1141 LASSERT (msg->msg_receiving);
1142 LASSERT (!msg->msg_sending);
1144 /* non-lnet_parse callers only send delayed messages */
1145 LASSERT (!do_recv || msg->msg_delayed);
1147 if (!msg->msg_peerrtrcredit) {
1148 LASSERT ((lp->lp_rtrcredits < 0) ==
1149 !cfs_list_empty(&lp->lp_rtrq));
1151 msg->msg_peerrtrcredit = 1;
1152 lp->lp_rtrcredits--;
1153 if (lp->lp_rtrcredits < lp->lp_minrtrcredits)
1154 lp->lp_minrtrcredits = lp->lp_rtrcredits;
1156 if (lp->lp_rtrcredits < 0) {
1157 /* must have checked eager_recv before here */
1158 LASSERT (msg->msg_delayed);
1159 cfs_list_add_tail(&msg->msg_list, &lp->lp_rtrq);
1164 rbp = lnet_msg2bufpool(msg);
1166 if (!msg->msg_rtrcredit) {
1167 LASSERT ((rbp->rbp_credits < 0) ==
1168 !cfs_list_empty(&rbp->rbp_msgs));
1170 msg->msg_rtrcredit = 1;
1172 if (rbp->rbp_credits < rbp->rbp_mincredits)
1173 rbp->rbp_mincredits = rbp->rbp_credits;
1175 if (rbp->rbp_credits < 0) {
1176 /* must have checked eager_recv before here */
1177 LASSERT (msg->msg_delayed);
1178 cfs_list_add_tail(&msg->msg_list, &rbp->rbp_msgs);
1183 LASSERT (!cfs_list_empty(&rbp->rbp_bufs));
1184 rb = cfs_list_entry(rbp->rbp_bufs.next, lnet_rtrbuf_t, rb_list);
1185 cfs_list_del(&rb->rb_list);
1187 msg->msg_niov = rbp->rbp_npages;
1188 msg->msg_kiov = &rb->rb_kiov[0];
1192 lnet_ni_recv(lp->lp_ni, msg->msg_private, msg, 1,
1193 0, msg->msg_len, msg->msg_len);
1201 lnet_return_credits_locked (lnet_msg_t *msg)
1203 lnet_peer_t *txpeer = msg->msg_txpeer;
1204 lnet_peer_t *rxpeer = msg->msg_rxpeer;
1208 if (msg->msg_txcredit) {
1209 /* give back NI txcredits */
1210 msg->msg_txcredit = 0;
1213 LASSERT((ni->ni_txcredits < 0) == !cfs_list_empty(&ni->ni_txq));
1216 if (ni->ni_txcredits <= 0) {
1217 msg2 = cfs_list_entry(ni->ni_txq.next, lnet_msg_t,
1219 cfs_list_del(&msg2->msg_list);
1221 LASSERT(msg2->msg_txpeer->lp_ni == ni);
1222 LASSERT(msg2->msg_delayed);
1224 (void) lnet_post_send_locked(msg2, 1);
1228 if (msg->msg_peertxcredit) {
1229 /* give back peer txcredits */
1230 msg->msg_peertxcredit = 0;
1232 LASSERT((txpeer->lp_txcredits < 0) ==
1233 !cfs_list_empty(&txpeer->lp_txq));
1235 txpeer->lp_txqnob -= msg->msg_len + sizeof(lnet_hdr_t);
1236 LASSERT (txpeer->lp_txqnob >= 0);
1238 txpeer->lp_txcredits++;
1239 if (txpeer->lp_txcredits <= 0) {
1240 msg2 = cfs_list_entry(txpeer->lp_txq.next,
1241 lnet_msg_t, msg_list);
1242 cfs_list_del(&msg2->msg_list);
1244 LASSERT (msg2->msg_txpeer == txpeer);
1245 LASSERT (msg2->msg_delayed);
1247 (void) lnet_post_send_locked(msg2, 1);
1251 if (txpeer != NULL) {
1252 msg->msg_txpeer = NULL;
1253 lnet_peer_decref_locked(txpeer);
1257 if (msg->msg_rtrcredit) {
1258 /* give back global router credits */
1260 lnet_rtrbufpool_t *rbp;
1262 /* NB If a msg ever blocks for a buffer in rbp_msgs, it stays
1263 * there until it gets one allocated, or aborts the wait
1265 LASSERT (msg->msg_kiov != NULL);
1267 rb = cfs_list_entry(msg->msg_kiov, lnet_rtrbuf_t, rb_kiov[0]);
1269 LASSERT (rbp == lnet_msg2bufpool(msg));
1271 msg->msg_kiov = NULL;
1272 msg->msg_rtrcredit = 0;
1274 LASSERT((rbp->rbp_credits < 0) ==
1275 !cfs_list_empty(&rbp->rbp_msgs));
1276 LASSERT((rbp->rbp_credits > 0) ==
1277 !cfs_list_empty(&rbp->rbp_bufs));
1279 cfs_list_add(&rb->rb_list, &rbp->rbp_bufs);
1281 if (rbp->rbp_credits <= 0) {
1282 msg2 = cfs_list_entry(rbp->rbp_msgs.next,
1283 lnet_msg_t, msg_list);
1284 cfs_list_del(&msg2->msg_list);
1286 (void) lnet_post_routed_recv_locked(msg2, 1);
1290 if (msg->msg_peerrtrcredit) {
1291 /* give back peer router credits */
1292 msg->msg_peerrtrcredit = 0;
1294 LASSERT((rxpeer->lp_rtrcredits < 0) ==
1295 !cfs_list_empty(&rxpeer->lp_rtrq));
1297 rxpeer->lp_rtrcredits++;
1298 if (rxpeer->lp_rtrcredits <= 0) {
1299 msg2 = cfs_list_entry(rxpeer->lp_rtrq.next,
1300 lnet_msg_t, msg_list);
1301 cfs_list_del(&msg2->msg_list);
1303 (void) lnet_post_routed_recv_locked(msg2, 1);
1307 LASSERT (!msg->msg_rtrcredit);
1308 LASSERT (!msg->msg_peerrtrcredit);
1310 if (rxpeer != NULL) {
1311 msg->msg_rxpeer = NULL;
1312 lnet_peer_decref_locked(rxpeer);
1317 lnet_send(lnet_nid_t src_nid, lnet_msg_t *msg)
1319 lnet_nid_t dst_nid = msg->msg_target.nid;
1321 lnet_ni_t *local_ni;
1322 lnet_remotenet_t *rnet;
1323 lnet_route_t *route;
1324 lnet_route_t *best_route;
1330 LASSERT (msg->msg_txpeer == NULL);
1331 LASSERT (!msg->msg_sending);
1332 LASSERT (!msg->msg_target_is_router);
1333 LASSERT (!msg->msg_receiving);
1335 msg->msg_sending = 1;
1337 /* NB! ni != NULL == interface pre-determined (ACK/REPLY) */
1341 if (the_lnet.ln_shutdown) {
1346 if (src_nid == LNET_NID_ANY) {
1349 src_ni = lnet_nid2ni_locked(src_nid);
1350 if (src_ni == NULL) {
1352 LCONSOLE_WARN("Can't send to %s: src %s is not a "
1353 "local nid\n", libcfs_nid2str(dst_nid),
1354 libcfs_nid2str(src_nid));
1357 LASSERT (!msg->msg_routing);
1360 /* Is this for someone on a local network? */
1361 local_ni = lnet_net2ni_locked(LNET_NIDNET(dst_nid));
1363 if (local_ni != NULL) {
1364 if (src_ni == NULL) {
1366 src_nid = src_ni->ni_nid;
1367 } else if (src_ni == local_ni) {
1368 lnet_ni_decref_locked(local_ni);
1370 lnet_ni_decref_locked(local_ni);
1371 lnet_ni_decref_locked(src_ni);
1373 LCONSOLE_WARN("No route to %s via from %s\n",
1374 libcfs_nid2str(dst_nid),
1375 libcfs_nid2str(src_nid));
1379 LASSERT (src_nid != LNET_NID_ANY);
1381 if (!msg->msg_routing)
1382 msg->msg_hdr.src_nid = cpu_to_le64(src_nid);
1384 if (src_ni == the_lnet.ln_loni) {
1385 /* No send credit hassles with LOLND */
1387 lnet_ni_send(src_ni, msg);
1388 lnet_ni_decref(src_ni);
1392 rc = lnet_nid2peer_locked(&lp, dst_nid);
1393 lnet_ni_decref_locked(src_ni); /* lp has ref on src_ni; lose mine */
1396 LCONSOLE_WARN("Error %d finding peer %s\n", rc,
1397 libcfs_nid2str(dst_nid));
1398 /* ENOMEM or shutting down */
1401 LASSERT (lp->lp_ni == src_ni);
1407 * - once application finishes computation, check here to update
1408 * router states before it waits for pending IO in LNetEQPoll
1409 * - recursion breaker: router checker sends no message
1410 * to remote networks */
1411 if (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING)
1412 lnet_router_checker();
1416 /* sending to a remote network */
1417 rnet = lnet_find_net_locked(LNET_NIDNET(dst_nid));
1420 lnet_ni_decref_locked(src_ni);
1422 LCONSOLE_WARN("No route to %s\n",
1423 libcfs_id2str(msg->msg_target));
1424 return -EHOSTUNREACH;
1427 /* Find the best gateway I can use */
1430 cfs_list_for_each(tmp, &rnet->lrn_routes) {
1431 route = cfs_list_entry(tmp, lnet_route_t, lr_list);
1432 lp2 = route->lr_gateway;
1434 if (lp2->lp_alive &&
1435 lnet_router_down_ni(lp2, rnet->lrn_net) <= 0 &&
1436 (src_ni == NULL || lp2->lp_ni == src_ni) &&
1438 lnet_compare_routes(route, best_route) > 0)) {
1446 lnet_ni_decref_locked(src_ni);
1449 LCONSOLE_WARN("No route to %s via %s "
1450 "(all routers down)\n",
1451 libcfs_id2str(msg->msg_target),
1452 libcfs_nid2str(src_nid));
1453 return -EHOSTUNREACH;
1456 /* Place selected route at the end of the route list to ensure
1457 * fairness; everything else being equal... */
1458 cfs_list_del(&best_route->lr_list);
1459 cfs_list_add_tail(&best_route->lr_list, &rnet->lrn_routes);
1460 CDEBUG(D_NET, "Best route to %s via %s for %s %d\n",
1461 libcfs_nid2str(dst_nid), libcfs_nid2str(lp->lp_nid),
1462 lnet_msgtyp2str(msg->msg_type), msg->msg_len);
1464 if (src_ni == NULL) {
1466 src_nid = src_ni->ni_nid;
1468 LASSERT (src_ni == lp->lp_ni);
1469 lnet_ni_decref_locked(src_ni);
1472 lnet_peer_addref_locked(lp);
1474 LASSERT (src_nid != LNET_NID_ANY);
1476 if (!msg->msg_routing) {
1477 /* I'm the source and now I know which NI to send on */
1478 msg->msg_hdr.src_nid = cpu_to_le64(src_nid);
1481 msg->msg_target_is_router = 1;
1482 msg->msg_target.nid = lp->lp_nid;
1483 msg->msg_target.pid = LUSTRE_SRV_LNET_PID;
1486 /* 'lp' is our best choice of peer */
1488 LASSERT (!msg->msg_peertxcredit);
1489 LASSERT (!msg->msg_txcredit);
1490 LASSERT (msg->msg_txpeer == NULL);
1492 msg->msg_txpeer = lp; /* msg takes my ref on lp */
1494 rc = lnet_post_send_locked(msg, 0);
1497 if (rc == EHOSTUNREACH)
1498 return -EHOSTUNREACH;
1501 lnet_ni_send(src_ni, msg);
1507 lnet_commit_md (lnet_libmd_t *md, lnet_msg_t *msg)
1509 /* ALWAYS called holding the LNET_LOCK */
1510 /* Here, we commit the MD to a network OP by marking it busy and
1511 * decrementing its threshold. Come what may, the network "owns"
1512 * the MD until a call to lnet_finalize() signals completion. */
1513 LASSERT (!msg->msg_routing);
1518 if (md->md_threshold != LNET_MD_THRESH_INF) {
1519 LASSERT (md->md_threshold > 0);
1523 the_lnet.ln_counters.msgs_alloc++;
1524 if (the_lnet.ln_counters.msgs_alloc >
1525 the_lnet.ln_counters.msgs_max)
1526 the_lnet.ln_counters.msgs_max =
1527 the_lnet.ln_counters.msgs_alloc;
1529 LASSERT (!msg->msg_onactivelist);
1530 msg->msg_onactivelist = 1;
1531 cfs_list_add (&msg->msg_activelist, &the_lnet.ln_active_msgs);
1535 lnet_drop_message (lnet_ni_t *ni, void *private, unsigned int nob)
1538 the_lnet.ln_counters.drop_count++;
1539 the_lnet.ln_counters.drop_length += nob;
1542 lnet_ni_recv(ni, private, NULL, 0, 0, 0, nob);
1546 lnet_drop_delayed_put(lnet_msg_t *msg, char *reason)
1548 lnet_process_id_t id = {0};
1550 id.nid = msg->msg_hdr.src_nid;
1551 id.pid = msg->msg_hdr.src_pid;
1553 LASSERT (msg->msg_md == NULL);
1554 LASSERT (msg->msg_delayed);
1555 LASSERT (msg->msg_rxpeer != NULL);
1556 LASSERT (msg->msg_hdr.type == LNET_MSG_PUT);
1558 CWARN("Dropping delayed PUT from %s portal %d match "LPU64
1559 " offset %d length %d: %s\n",
1561 msg->msg_hdr.msg.put.ptl_index,
1562 msg->msg_hdr.msg.put.match_bits,
1563 msg->msg_hdr.msg.put.offset,
1564 msg->msg_hdr.payload_length,
1567 /* NB I can't drop msg's ref on msg_rxpeer until after I've
1568 * called lnet_drop_message(), so I just hang onto msg as well
1569 * until that's done */
1571 lnet_drop_message(msg->msg_rxpeer->lp_ni,
1572 msg->msg_private, msg->msg_len);
1576 lnet_peer_decref_locked(msg->msg_rxpeer);
1577 msg->msg_rxpeer = NULL;
1585 * Turn on the lazy portal attribute. Use with caution!
1587 * This portal attribute only affects incoming PUT requests to the portal,
1588 * and is off by default. By default, if there's no matching MD for an
1589 * incoming PUT request, it is simply dropped. With the lazy attribute on,
1590 * such requests are queued indefinitely until either a matching MD is
1591 * posted to the portal or the lazy attribute is turned off.
1593 * It would prevent dropped requests, however it should be regarded as the
1594 * last line of defense - i.e. users must keep a close watch on active
1595 * buffers on a lazy portal and once it becomes too low post more buffers as
1596 * soon as possible. This is because delayed requests usually have detrimental
1597 * effects on underlying network connections. A few delayed requests often
1598 * suffice to bring an underlying connection to a complete halt, due to flow
1599 * control mechanisms.
1601 * There's also a DOS attack risk. If users don't post match-all MDs on a
1602 * lazy portal, a malicious peer can easily stop a service by sending some
1603 * PUT requests with match bits that won't match any MD. A routed server is
1604 * especially vulnerable since the connections to its neighbor routers are
1605 * shared among all clients.
1607 * \param portal Index of the portal to enable the lazy attribute on.
1609 * \retval 0 On success.
1610 * \retval -EINVAL If \a portal is not a valid index.
1613 LNetSetLazyPortal(int portal)
1615 lnet_portal_t *ptl = &the_lnet.ln_portals[portal];
1617 if (portal < 0 || portal >= the_lnet.ln_nportals)
1620 CDEBUG(D_NET, "Setting portal %d lazy\n", portal);
1623 lnet_portal_setopt(ptl, LNET_PTL_LAZY);
1630 * Turn off the lazy portal attribute. Delayed requests on the portal,
1631 * if any, will be all dropped when this function returns.
1633 * \param portal Index of the portal to disable the lazy attribute on.
1635 * \retval 0 On success.
1636 * \retval -EINVAL If \a portal is not a valid index.
1639 LNetClearLazyPortal(int portal)
1642 lnet_portal_t *ptl = &the_lnet.ln_portals[portal];
1645 if (portal < 0 || portal >= the_lnet.ln_nportals)
1650 if (!lnet_portal_is_lazy(ptl)) {
1655 if (the_lnet.ln_shutdown)
1656 CWARN ("Active lazy portal %d on exit\n", portal);
1658 CDEBUG (D_NET, "clearing portal %d lazy\n", portal);
1660 /* grab all the blocked messages atomically */
1661 cfs_list_add(&zombies, &ptl->ptl_msgq);
1662 cfs_list_del_init(&ptl->ptl_msgq);
1664 ptl->ptl_msgq_version++;
1665 lnet_portal_unsetopt(ptl, LNET_PTL_LAZY);
1669 while (!cfs_list_empty(&zombies)) {
1670 msg = cfs_list_entry(zombies.next, lnet_msg_t, msg_list);
1671 cfs_list_del(&msg->msg_list);
1673 lnet_drop_delayed_put(msg, "Clearing lazy portal attr");
1680 lnet_recv_put(lnet_libmd_t *md, lnet_msg_t *msg, int delayed,
1681 unsigned int offset, unsigned int mlength)
1683 lnet_hdr_t *hdr = &msg->msg_hdr;
1687 the_lnet.ln_counters.recv_count++;
1688 the_lnet.ln_counters.recv_length += mlength;
1693 lnet_setpayloadbuffer(msg);
1695 msg->msg_ev.type = LNET_EVENT_PUT;
1696 msg->msg_ev.target.pid = hdr->dest_pid;
1697 msg->msg_ev.target.nid = hdr->dest_nid;
1698 msg->msg_ev.hdr_data = hdr->msg.put.hdr_data;
1700 /* Must I ACK? If so I'll grab the ack_wmd out of the header and put
1701 * it back into the ACK during lnet_finalize() */
1702 msg->msg_ack = (!lnet_is_wire_handle_none(&hdr->msg.put.ack_wmd) &&
1703 (md->md_options & LNET_MD_ACK_DISABLE) == 0);
1705 lnet_ni_recv(msg->msg_rxpeer->lp_ni,
1707 msg, delayed, offset, mlength,
1708 hdr->payload_length);
1711 /* called with LNET_LOCK held */
1713 lnet_match_blocked_msg(lnet_libmd_t *md)
1715 CFS_LIST_HEAD (drops);
1716 CFS_LIST_HEAD (matches);
1721 lnet_me_t *me = md->md_me;
1723 LASSERT (me->me_portal < (unsigned int)the_lnet.ln_nportals);
1725 ptl = &the_lnet.ln_portals[me->me_portal];
1726 if (!lnet_portal_is_lazy(ptl)) {
1727 LASSERT (cfs_list_empty(&ptl->ptl_msgq));
1731 LASSERT (md->md_refcount == 0); /* a brand new MD */
1733 cfs_list_for_each_safe (entry, tmp, &ptl->ptl_msgq) {
1736 unsigned int mlength;
1737 unsigned int offset;
1739 lnet_process_id_t src;
1741 msg = cfs_list_entry(entry, lnet_msg_t, msg_list);
1743 LASSERT (msg->msg_delayed);
1745 hdr = &msg->msg_hdr;
1746 index = hdr->msg.put.ptl_index;
1748 src.nid = hdr->src_nid;
1749 src.pid = hdr->src_pid;
1751 rc = lnet_try_match_md(index, LNET_MD_OP_PUT, src,
1752 hdr->payload_length,
1753 hdr->msg.put.offset,
1754 hdr->msg.put.match_bits,
1755 md, msg, &mlength, &offset);
1757 if (rc == LNET_MATCHMD_NONE)
1760 /* Hurrah! This _is_ a match */
1761 cfs_list_del(&msg->msg_list);
1762 ptl->ptl_msgq_version++;
1764 if (rc == LNET_MATCHMD_OK) {
1765 cfs_list_add_tail(&msg->msg_list, &matches);
1767 CDEBUG(D_NET, "Resuming delayed PUT from %s portal %d "
1768 "match "LPU64" offset %d length %d.\n",
1770 hdr->msg.put.ptl_index,
1771 hdr->msg.put.match_bits,
1772 hdr->msg.put.offset,
1773 hdr->payload_length);
1775 LASSERT (rc == LNET_MATCHMD_DROP);
1777 cfs_list_add_tail(&msg->msg_list, &drops);
1780 if (lnet_md_exhausted(md))
1786 cfs_list_for_each_safe (entry, tmp, &drops) {
1787 msg = cfs_list_entry(entry, lnet_msg_t, msg_list);
1789 cfs_list_del(&msg->msg_list);
1791 lnet_drop_delayed_put(msg, "Bad match");
1794 cfs_list_for_each_safe (entry, tmp, &matches) {
1795 msg = cfs_list_entry(entry, lnet_msg_t, msg_list);
1797 cfs_list_del(&msg->msg_list);
1799 /* md won't disappear under me, since each msg
1800 * holds a ref on it */
1801 lnet_recv_put(md, msg, 1,
1803 msg->msg_ev.mlength);
1810 lnet_parse_put(lnet_ni_t *ni, lnet_msg_t *msg)
1815 lnet_hdr_t *hdr = &msg->msg_hdr;
1816 unsigned int rlength = hdr->payload_length;
1817 unsigned int mlength = 0;
1818 unsigned int offset = 0;
1819 lnet_process_id_t src= {0};
1823 src.nid = hdr->src_nid;
1824 src.pid = hdr->src_pid;
1826 /* Convert put fields to host byte order */
1827 hdr->msg.put.match_bits = le64_to_cpu(hdr->msg.put.match_bits);
1828 hdr->msg.put.ptl_index = le32_to_cpu(hdr->msg.put.ptl_index);
1829 hdr->msg.put.offset = le32_to_cpu(hdr->msg.put.offset);
1831 index = hdr->msg.put.ptl_index;
1836 rc = lnet_match_md(index, LNET_MD_OP_PUT, src,
1837 rlength, hdr->msg.put.offset,
1838 hdr->msg.put.match_bits, msg,
1839 &mlength, &offset, &md);
1844 case LNET_MATCHMD_OK:
1846 lnet_recv_put(md, msg, msg->msg_delayed, offset, mlength);
1849 case LNET_MATCHMD_NONE:
1850 ptl = &the_lnet.ln_portals[index];
1851 version = ptl->ptl_ml_version;
1854 if (!msg->msg_delayed)
1855 rc = lnet_eager_recv_locked(msg);
1858 !the_lnet.ln_shutdown &&
1859 lnet_portal_is_lazy(ptl)) {
1860 if (version != ptl->ptl_ml_version)
1863 cfs_list_add_tail(&msg->msg_list, &ptl->ptl_msgq);
1864 ptl->ptl_msgq_version++;
1867 CDEBUG(D_NET, "Delaying PUT from %s portal %d match "
1868 LPU64" offset %d length %d: no match \n",
1869 libcfs_id2str(src), index,
1870 hdr->msg.put.match_bits,
1871 hdr->msg.put.offset, rlength);
1876 case LNET_MATCHMD_DROP:
1877 CNETERR("Dropping PUT from %s portal %d match "LPU64
1878 " offset %d length %d: %d\n",
1879 libcfs_id2str(src), index,
1880 hdr->msg.put.match_bits,
1881 hdr->msg.put.offset, rlength, rc);
1884 return ENOENT; /* +ve: OK but no match */
1889 lnet_parse_get(lnet_ni_t *ni, lnet_msg_t *msg, int rdma_get)
1891 lnet_hdr_t *hdr = &msg->msg_hdr;
1892 unsigned int mlength = 0;
1893 unsigned int offset = 0;
1894 lnet_process_id_t src = {0};
1895 lnet_handle_wire_t reply_wmd;
1899 src.nid = hdr->src_nid;
1900 src.pid = hdr->src_pid;
1902 /* Convert get fields to host byte order */
1903 hdr->msg.get.match_bits = le64_to_cpu(hdr->msg.get.match_bits);
1904 hdr->msg.get.ptl_index = le32_to_cpu(hdr->msg.get.ptl_index);
1905 hdr->msg.get.sink_length = le32_to_cpu(hdr->msg.get.sink_length);
1906 hdr->msg.get.src_offset = le32_to_cpu(hdr->msg.get.src_offset);
1910 rc = lnet_match_md(hdr->msg.get.ptl_index, LNET_MD_OP_GET, src,
1911 hdr->msg.get.sink_length, hdr->msg.get.src_offset,
1912 hdr->msg.get.match_bits, msg,
1913 &mlength, &offset, &md);
1914 if (rc == LNET_MATCHMD_DROP) {
1915 CNETERR("Dropping GET from %s portal %d match "LPU64
1916 " offset %d length %d\n",
1918 hdr->msg.get.ptl_index,
1919 hdr->msg.get.match_bits,
1920 hdr->msg.get.src_offset,
1921 hdr->msg.get.sink_length);
1923 return ENOENT; /* +ve: OK but no match */
1926 LASSERT (rc == LNET_MATCHMD_OK);
1928 the_lnet.ln_counters.send_count++;
1929 the_lnet.ln_counters.send_length += mlength;
1933 msg->msg_ev.type = LNET_EVENT_GET;
1934 msg->msg_ev.target.pid = hdr->dest_pid;
1935 msg->msg_ev.target.nid = hdr->dest_nid;
1936 msg->msg_ev.hdr_data = 0;
1938 reply_wmd = hdr->msg.get.return_wmd;
1940 lnet_prep_send(msg, LNET_MSG_REPLY, src, offset, mlength);
1942 msg->msg_hdr.msg.reply.dst_wmd = reply_wmd;
1945 /* The LND completes the REPLY from her recv procedure */
1946 lnet_ni_recv(ni, msg->msg_private, msg, 0,
1947 msg->msg_offset, msg->msg_len, msg->msg_len);
1951 lnet_ni_recv(ni, msg->msg_private, NULL, 0, 0, 0, 0);
1952 msg->msg_receiving = 0;
1954 rc = lnet_send(ni->ni_nid, msg);
1956 /* didn't get as far as lnet_ni_send() */
1957 CERROR("%s: Unable to send REPLY for GET from %s: %d\n",
1958 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src), rc);
1960 lnet_finalize(ni, msg, rc);
1967 lnet_parse_reply(lnet_ni_t *ni, lnet_msg_t *msg)
1969 void *private = msg->msg_private;
1970 lnet_hdr_t *hdr = &msg->msg_hdr;
1971 lnet_process_id_t src = {0};
1978 src.nid = hdr->src_nid;
1979 src.pid = hdr->src_pid;
1981 /* NB handles only looked up by creator (no flips) */
1982 md = lnet_wire_handle2md(&hdr->msg.reply.dst_wmd);
1983 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
1984 CNETERR("%s: Dropping REPLY from %s for %s "
1985 "MD "LPX64"."LPX64"\n",
1986 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1987 (md == NULL) ? "invalid" : "inactive",
1988 hdr->msg.reply.dst_wmd.wh_interface_cookie,
1989 hdr->msg.reply.dst_wmd.wh_object_cookie);
1990 if (md != NULL && md->md_me != NULL)
1991 CERROR("REPLY MD also attached to portal %d\n",
1992 md->md_me->me_portal);
1995 return ENOENT; /* +ve: OK but no match */
1998 LASSERT (md->md_offset == 0);
2000 rlength = hdr->payload_length;
2001 mlength = MIN(rlength, (int)md->md_length);
2003 if (mlength < rlength &&
2004 (md->md_options & LNET_MD_TRUNCATE) == 0) {
2005 CNETERR("%s: Dropping REPLY from %s length %d "
2006 "for MD "LPX64" would overflow (%d)\n",
2007 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
2008 rlength, hdr->msg.reply.dst_wmd.wh_object_cookie,
2011 return ENOENT; /* +ve: OK but no match */
2014 CDEBUG(D_NET, "%s: Reply from %s of length %d/%d into md "LPX64"\n",
2015 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
2016 mlength, rlength, hdr->msg.reply.dst_wmd.wh_object_cookie);
2018 lnet_commit_md(md, msg);
2021 lnet_setpayloadbuffer(msg);
2023 msg->msg_ev.type = LNET_EVENT_REPLY;
2024 msg->msg_ev.target.pid = hdr->dest_pid;
2025 msg->msg_ev.target.nid = hdr->dest_nid;
2026 msg->msg_ev.initiator = src;
2027 msg->msg_ev.rlength = rlength;
2028 msg->msg_ev.mlength = mlength;
2029 msg->msg_ev.offset = 0;
2031 lnet_md_deconstruct(md, &msg->msg_ev.md);
2032 lnet_md2handle(&msg->msg_ev.md_handle, md);
2034 the_lnet.ln_counters.recv_count++;
2035 the_lnet.ln_counters.recv_length += mlength;
2039 lnet_ni_recv(ni, private, msg, 0, 0, mlength, rlength);
2044 lnet_parse_ack(lnet_ni_t *ni, lnet_msg_t *msg)
2046 lnet_hdr_t *hdr = &msg->msg_hdr;
2047 lnet_process_id_t src = {0};
2050 src.nid = hdr->src_nid;
2051 src.pid = hdr->src_pid;
2053 /* Convert ack fields to host byte order */
2054 hdr->msg.ack.match_bits = le64_to_cpu(hdr->msg.ack.match_bits);
2055 hdr->msg.ack.mlength = le32_to_cpu(hdr->msg.ack.mlength);
2059 /* NB handles only looked up by creator (no flips) */
2060 md = lnet_wire_handle2md(&hdr->msg.ack.dst_wmd);
2061 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
2062 /* Don't moan; this is expected */
2064 "%s: Dropping ACK from %s to %s MD "LPX64"."LPX64"\n",
2065 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
2066 (md == NULL) ? "invalid" : "inactive",
2067 hdr->msg.ack.dst_wmd.wh_interface_cookie,
2068 hdr->msg.ack.dst_wmd.wh_object_cookie);
2069 if (md != NULL && md->md_me != NULL)
2070 CERROR("Source MD also attached to portal %d\n",
2071 md->md_me->me_portal);
2074 return ENOENT; /* +ve! */
2077 CDEBUG(D_NET, "%s: ACK from %s into md "LPX64"\n",
2078 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
2079 hdr->msg.ack.dst_wmd.wh_object_cookie);
2081 lnet_commit_md(md, msg);
2083 msg->msg_ev.type = LNET_EVENT_ACK;
2084 msg->msg_ev.target.pid = hdr->dest_pid;
2085 msg->msg_ev.target.nid = hdr->dest_nid;
2086 msg->msg_ev.initiator = src;
2087 msg->msg_ev.mlength = hdr->msg.ack.mlength;
2088 msg->msg_ev.match_bits = hdr->msg.ack.match_bits;
2090 lnet_md_deconstruct(md, &msg->msg_ev.md);
2091 lnet_md2handle(&msg->msg_ev.md_handle, md);
2093 the_lnet.ln_counters.recv_count++;
2097 lnet_ni_recv(ni, msg->msg_private, msg, 0, 0, 0, msg->msg_len);
2102 lnet_msgtyp2str (int type)
2111 case LNET_MSG_REPLY:
2113 case LNET_MSG_HELLO:
2116 return ("<UNKNOWN>");
2121 lnet_print_hdr(lnet_hdr_t * hdr)
2123 lnet_process_id_t src = {0};
2124 lnet_process_id_t dst = {0};
2125 char *type_str = lnet_msgtyp2str (hdr->type);
2127 src.nid = hdr->src_nid;
2128 src.pid = hdr->src_pid;
2130 dst.nid = hdr->dest_nid;
2131 dst.pid = hdr->dest_pid;
2133 CWARN("P3 Header at %p of type %s\n", hdr, type_str);
2134 CWARN(" From %s\n", libcfs_id2str(src));
2135 CWARN(" To %s\n", libcfs_id2str(dst));
2137 switch (hdr->type) {
2142 CWARN(" Ptl index %d, ack md "LPX64"."LPX64", "
2143 "match bits "LPU64"\n",
2144 hdr->msg.put.ptl_index,
2145 hdr->msg.put.ack_wmd.wh_interface_cookie,
2146 hdr->msg.put.ack_wmd.wh_object_cookie,
2147 hdr->msg.put.match_bits);
2148 CWARN(" Length %d, offset %d, hdr data "LPX64"\n",
2149 hdr->payload_length, hdr->msg.put.offset,
2150 hdr->msg.put.hdr_data);
2154 CWARN(" Ptl index %d, return md "LPX64"."LPX64", "
2155 "match bits "LPU64"\n", hdr->msg.get.ptl_index,
2156 hdr->msg.get.return_wmd.wh_interface_cookie,
2157 hdr->msg.get.return_wmd.wh_object_cookie,
2158 hdr->msg.get.match_bits);
2159 CWARN(" Length %d, src offset %d\n",
2160 hdr->msg.get.sink_length,
2161 hdr->msg.get.src_offset);
2165 CWARN(" dst md "LPX64"."LPX64", "
2166 "manipulated length %d\n",
2167 hdr->msg.ack.dst_wmd.wh_interface_cookie,
2168 hdr->msg.ack.dst_wmd.wh_object_cookie,
2169 hdr->msg.ack.mlength);
2172 case LNET_MSG_REPLY:
2173 CWARN(" dst md "LPX64"."LPX64", "
2175 hdr->msg.reply.dst_wmd.wh_interface_cookie,
2176 hdr->msg.reply.dst_wmd.wh_object_cookie,
2177 hdr->payload_length);
2183 lnet_parse(lnet_ni_t *ni, lnet_hdr_t *hdr, lnet_nid_t from_nid,
2184 void *private, int rdma_req)
2189 lnet_pid_t dest_pid;
2190 lnet_nid_t dest_nid;
2192 __u32 payload_length;
2195 LASSERT (!cfs_in_interrupt ());
2197 type = le32_to_cpu(hdr->type);
2198 src_nid = le64_to_cpu(hdr->src_nid);
2199 dest_nid = le64_to_cpu(hdr->dest_nid);
2200 dest_pid = le32_to_cpu(hdr->dest_pid);
2201 payload_length = le32_to_cpu(hdr->payload_length);
2203 for_me = (ni->ni_nid == dest_nid);
2208 if (payload_length > 0) {
2209 CERROR("%s, src %s: bad %s payload %d (0 expected)\n",
2210 libcfs_nid2str(from_nid),
2211 libcfs_nid2str(src_nid),
2212 lnet_msgtyp2str(type), payload_length);
2218 case LNET_MSG_REPLY:
2219 if (payload_length > (__u32)(for_me ? LNET_MAX_PAYLOAD : LNET_MTU)) {
2220 CERROR("%s, src %s: bad %s payload %d "
2221 "(%d max expected)\n",
2222 libcfs_nid2str(from_nid),
2223 libcfs_nid2str(src_nid),
2224 lnet_msgtyp2str(type),
2226 for_me ? LNET_MAX_PAYLOAD : LNET_MTU);
2232 CERROR("%s, src %s: Bad message type 0x%x\n",
2233 libcfs_nid2str(from_nid),
2234 libcfs_nid2str(src_nid), type);
2238 if (the_lnet.ln_routing) {
2239 cfs_time_t now = cfs_time_current();
2243 ni->ni_last_alive = now;
2244 if (ni->ni_status != NULL &&
2245 ni->ni_status->ns_status == LNET_NI_STATUS_DOWN)
2246 ni->ni_status->ns_status = LNET_NI_STATUS_UP;
2251 /* Regard a bad destination NID as a protocol error. Senders should
2252 * know what they're doing; if they don't they're misconfigured, buggy
2253 * or malicious so we chop them off at the knees :) */
2256 if (LNET_NIDNET(dest_nid) == LNET_NIDNET(ni->ni_nid)) {
2257 /* should have gone direct */
2258 CERROR ("%s, src %s: Bad dest nid %s "
2259 "(should have been sent direct)\n",
2260 libcfs_nid2str(from_nid),
2261 libcfs_nid2str(src_nid),
2262 libcfs_nid2str(dest_nid));
2266 if (lnet_islocalnid(dest_nid)) {
2267 /* dest is another local NI; sender should have used
2268 * this node's NID on its own network */
2269 CERROR ("%s, src %s: Bad dest nid %s "
2270 "(it's my nid but on a different network)\n",
2271 libcfs_nid2str(from_nid),
2272 libcfs_nid2str(src_nid),
2273 libcfs_nid2str(dest_nid));
2277 if (rdma_req && type == LNET_MSG_GET) {
2278 CERROR ("%s, src %s: Bad optimized GET for %s "
2279 "(final destination must be me)\n",
2280 libcfs_nid2str(from_nid),
2281 libcfs_nid2str(src_nid),
2282 libcfs_nid2str(dest_nid));
2286 if (!the_lnet.ln_routing) {
2287 CERROR ("%s, src %s: Dropping message for %s "
2288 "(routing not enabled)\n",
2289 libcfs_nid2str(from_nid),
2290 libcfs_nid2str(src_nid),
2291 libcfs_nid2str(dest_nid));
2296 /* Message looks OK; we're not going to return an error, so we MUST
2297 * call back lnd_recv() come what may... */
2299 if (!cfs_list_empty (&the_lnet.ln_test_peers) && /* normally we don't */
2300 fail_peer (src_nid, 0)) /* shall we now? */
2302 CERROR("%s, src %s: Dropping %s to simulate failure\n",
2303 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
2304 lnet_msgtyp2str(type));
2308 msg = lnet_msg_alloc();
2310 CERROR("%s, src %s: Dropping %s (out of memory)\n",
2311 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
2312 lnet_msgtyp2str(type));
2316 /* msg zeroed in lnet_msg_alloc; i.e. flags all clear, pointers NULL etc */
2318 msg->msg_type = type;
2319 msg->msg_private = private;
2320 msg->msg_receiving = 1;
2321 msg->msg_len = msg->msg_wanted = payload_length;
2322 msg->msg_offset = 0;
2323 msg->msg_hdr = *hdr;
2326 rc = lnet_nid2peer_locked(&msg->msg_rxpeer, from_nid);
2329 CERROR("%s, src %s: Dropping %s "
2330 "(error %d looking up sender)\n",
2331 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
2332 lnet_msgtyp2str(type), rc);
2341 msg->msg_target.pid = dest_pid;
2342 msg->msg_target.nid = dest_nid;
2343 msg->msg_routing = 1;
2344 msg->msg_offset = 0;
2347 if (msg->msg_rxpeer->lp_rtrcredits <= 0 ||
2348 lnet_msg2bufpool(msg)->rbp_credits <= 0) {
2349 rc = lnet_eager_recv_locked(msg);
2355 lnet_commit_routedmsg(msg);
2356 rc = lnet_post_routed_recv_locked(msg, 0);
2360 lnet_ni_recv(ni, msg->msg_private, msg, 0,
2361 0, payload_length, payload_length);
2365 /* convert common msg->hdr fields to host byteorder */
2366 msg->msg_hdr.type = type;
2367 msg->msg_hdr.src_nid = src_nid;
2368 msg->msg_hdr.src_pid = le32_to_cpu(msg->msg_hdr.src_pid);
2369 msg->msg_hdr.dest_nid = dest_nid;
2370 msg->msg_hdr.dest_pid = dest_pid;
2371 msg->msg_hdr.payload_length = payload_length;
2373 msg->msg_ev.sender = from_nid;
2377 rc = lnet_parse_ack(ni, msg);
2380 rc = lnet_parse_put(ni, msg);
2383 rc = lnet_parse_get(ni, msg, rdma_req);
2385 case LNET_MSG_REPLY:
2386 rc = lnet_parse_reply(ni, msg);
2390 goto free_drop; /* prevent an unused label if !kernel */
2396 LASSERT (rc == ENOENT);
2399 LASSERT (msg->msg_md == NULL);
2401 if (msg->msg_rxpeer != NULL) {
2402 lnet_peer_decref_locked(msg->msg_rxpeer);
2403 msg->msg_rxpeer = NULL;
2405 lnet_msg_free(msg); /* expects LNET_LOCK held */
2409 lnet_drop_message(ni, private, payload_length);
2414 * Initiate an asynchronous PUT operation.
2416 * There are several events associated with a PUT: completion of the send on
2417 * the initiator node (LNET_EVENT_SEND), and when the send completes
2418 * successfully, the receipt of an acknowledgment (LNET_EVENT_ACK) indicating
2419 * that the operation was accepted by the target. The event LNET_EVENT_PUT is
2420 * used at the target node to indicate the completion of incoming data
2423 * The local events will be logged in the EQ associated with the MD pointed to
2424 * by \a mdh handle. Using a MD without an associated EQ results in these
2425 * events being discarded. In this case, the caller must have another
2426 * mechanism (e.g., a higher level protocol) for determining when it is safe
2427 * to modify the memory region associated with the MD.
2429 * Note that LNet does not guarantee the order of LNET_EVENT_SEND and
2430 * LNET_EVENT_ACK, though intuitively ACK should happen after SEND.
2432 * \param self Indicates the NID of a local interface through which to send
2433 * the PUT request. Use LNET_NID_ANY to let LNet choose one by itself.
2434 * \param mdh A handle for the MD that describes the memory to be sent. The MD
2435 * must be "free floating" (See LNetMDBind()).
2436 * \param ack Controls whether an acknowledgment is requested.
2437 * Acknowledgments are only sent when they are requested by the initiating
2438 * process and the target MD enables them.
2439 * \param target A process identifier for the target process.
2440 * \param portal The index in the \a target's portal table.
2441 * \param match_bits The match bits to use for MD selection at the target
2443 * \param offset The offset into the target MD (only used when the target
2444 * MD has the LNET_MD_MANAGE_REMOTE option set).
2445 * \param hdr_data 64 bits of user data that can be included in the message
2446 * header. This data is written to an event queue entry at the target if an
2447 * EQ is present on the matching MD.
2449 * \retval 0 Success, and only in this case events will be generated
2450 * and logged to EQ (if it exists).
2451 * \retval -EIO Simulated failure.
2452 * \retval -ENOMEM Memory allocation failure.
2453 * \retval -ENOENT Invalid MD object.
2455 * \see lnet_event_t::hdr_data and lnet_event_kind_t.
2458 LNetPut(lnet_nid_t self, lnet_handle_md_t mdh, lnet_ack_req_t ack,
2459 lnet_process_id_t target, unsigned int portal,
2460 __u64 match_bits, unsigned int offset,
2467 LASSERT (the_lnet.ln_init);
2468 LASSERT (the_lnet.ln_refcount > 0);
2470 if (!cfs_list_empty (&the_lnet.ln_test_peers) && /* normally we don't */
2471 fail_peer (target.nid, 1)) /* shall we now? */
2473 CERROR("Dropping PUT to %s: simulated failure\n",
2474 libcfs_id2str(target));
2478 msg = lnet_msg_alloc();
2480 CERROR("Dropping PUT to %s: ENOMEM on lnet_msg_t\n",
2481 libcfs_id2str(target));
2484 msg->msg_vmflush = !!cfs_memory_pressure_get();
2488 md = lnet_handle2md(&mdh);
2489 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
2492 CERROR("Dropping PUT ("LPU64":%d:%s): MD (%d) invalid\n",
2493 match_bits, portal, libcfs_id2str(target),
2494 md == NULL ? -1 : md->md_threshold);
2495 if (md != NULL && md->md_me != NULL)
2496 CERROR("Source MD also attached to portal %d\n",
2497 md->md_me->me_portal);
2503 CDEBUG(D_NET, "LNetPut -> %s\n", libcfs_id2str(target));
2505 lnet_commit_md(md, msg);
2507 lnet_prep_send(msg, LNET_MSG_PUT, target, 0, md->md_length);
2509 msg->msg_hdr.msg.put.match_bits = cpu_to_le64(match_bits);
2510 msg->msg_hdr.msg.put.ptl_index = cpu_to_le32(portal);
2511 msg->msg_hdr.msg.put.offset = cpu_to_le32(offset);
2512 msg->msg_hdr.msg.put.hdr_data = hdr_data;
2514 /* NB handles only looked up by creator (no flips) */
2515 if (ack == LNET_ACK_REQ) {
2516 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
2517 the_lnet.ln_interface_cookie;
2518 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
2519 md->md_lh.lh_cookie;
2521 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
2522 LNET_WIRE_HANDLE_COOKIE_NONE;
2523 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
2524 LNET_WIRE_HANDLE_COOKIE_NONE;
2527 msg->msg_ev.type = LNET_EVENT_SEND;
2528 msg->msg_ev.initiator.nid = LNET_NID_ANY;
2529 msg->msg_ev.initiator.pid = the_lnet.ln_pid;
2530 msg->msg_ev.target = target;
2531 msg->msg_ev.sender = LNET_NID_ANY;
2532 msg->msg_ev.pt_index = portal;
2533 msg->msg_ev.match_bits = match_bits;
2534 msg->msg_ev.rlength = md->md_length;
2535 msg->msg_ev.mlength = md->md_length;
2536 msg->msg_ev.offset = offset;
2537 msg->msg_ev.hdr_data = hdr_data;
2539 lnet_md_deconstruct(md, &msg->msg_ev.md);
2540 lnet_md2handle(&msg->msg_ev.md_handle, md);
2542 the_lnet.ln_counters.send_count++;
2543 the_lnet.ln_counters.send_length += md->md_length;
2547 rc = lnet_send(self, msg);
2549 CNETERR( "Error sending PUT to %s: %d\n",
2550 libcfs_id2str(target), rc);
2551 lnet_finalize (NULL, msg, rc);
2554 /* completion will be signalled by an event */
2559 lnet_create_reply_msg (lnet_ni_t *ni, lnet_msg_t *getmsg)
2561 /* The LND can DMA direct to the GET md (i.e. no REPLY msg). This
2562 * returns a msg for the LND to pass to lnet_finalize() when the sink
2563 * data has been received.
2565 * CAVEAT EMPTOR: 'getmsg' is the original GET, which is freed when
2566 * lnet_finalize() is called on it, so the LND must call this first */
2568 lnet_msg_t *msg = lnet_msg_alloc();
2569 lnet_libmd_t *getmd = getmsg->msg_md;
2570 lnet_process_id_t peer_id = getmsg->msg_target;
2572 LASSERT (!getmsg->msg_target_is_router);
2573 LASSERT (!getmsg->msg_routing);
2577 LASSERT (getmd->md_refcount > 0);
2580 CERROR ("%s: Dropping REPLY from %s: can't allocate msg\n",
2581 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id));
2585 if (getmd->md_threshold == 0) {
2586 CERROR ("%s: Dropping REPLY from %s for inactive MD %p\n",
2587 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id),
2592 LASSERT (getmd->md_offset == 0);
2594 CDEBUG(D_NET, "%s: Reply from %s md %p\n",
2595 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id), getmd);
2597 lnet_commit_md (getmd, msg);
2599 msg->msg_type = LNET_MSG_GET; /* flag this msg as an "optimized" GET */
2601 msg->msg_ev.type = LNET_EVENT_REPLY;
2602 msg->msg_ev.initiator = peer_id;
2603 msg->msg_ev.sender = peer_id.nid; /* optimized GETs can't be routed */
2604 msg->msg_ev.rlength = msg->msg_ev.mlength = getmd->md_length;
2605 msg->msg_ev.offset = 0;
2607 lnet_md_deconstruct(getmd, &msg->msg_ev.md);
2608 lnet_md2handle(&msg->msg_ev.md_handle, getmd);
2610 the_lnet.ln_counters.recv_count++;
2611 the_lnet.ln_counters.recv_length += getmd->md_length;
2620 the_lnet.ln_counters.drop_count++;
2621 the_lnet.ln_counters.drop_length += getmd->md_length;
2629 lnet_set_reply_msg_len(lnet_ni_t *ni, lnet_msg_t *reply, unsigned int len)
2631 /* Set the REPLY length, now the RDMA that elides the REPLY message has
2632 * completed and I know it. */
2633 LASSERT (reply != NULL);
2634 LASSERT (reply->msg_type == LNET_MSG_GET);
2635 LASSERT (reply->msg_ev.type == LNET_EVENT_REPLY);
2637 /* NB I trusted my peer to RDMA. If she tells me she's written beyond
2638 * the end of my buffer, I might as well be dead. */
2639 LASSERT (len <= reply->msg_ev.mlength);
2641 reply->msg_ev.mlength = len;
2645 * Initiate an asynchronous GET operation.
2647 * On the initiator node, an LNET_EVENT_SEND is logged when the GET request
2648 * is sent, and an LNET_EVENT_REPLY is logged when the data returned from
2649 * the target node in the REPLY has been written to local MD.
2651 * On the target node, an LNET_EVENT_GET is logged when the GET request
2652 * arrives and is accepted into a MD.
2654 * \param self,target,portal,match_bits,offset See the discussion in LNetPut().
2655 * \param mdh A handle for the MD that describes the memory into which the
2656 * requested data will be received. The MD must be "free floating" (See LNetMDBind()).
2658 * \retval 0 Success, and only in this case events will be generated
2659 * and logged to EQ (if it exists) of the MD.
2660 * \retval -EIO Simulated failure.
2661 * \retval -ENOMEM Memory allocation failure.
2662 * \retval -ENOENT Invalid MD object.
2665 LNetGet(lnet_nid_t self, lnet_handle_md_t mdh,
2666 lnet_process_id_t target, unsigned int portal,
2667 __u64 match_bits, unsigned int offset)
2673 LASSERT (the_lnet.ln_init);
2674 LASSERT (the_lnet.ln_refcount > 0);
2676 if (!cfs_list_empty (&the_lnet.ln_test_peers) && /* normally we don't */
2677 fail_peer (target.nid, 1)) /* shall we now? */
2679 CERROR("Dropping GET to %s: simulated failure\n",
2680 libcfs_id2str(target));
2684 msg = lnet_msg_alloc();
2686 CERROR("Dropping GET to %s: ENOMEM on lnet_msg_t\n",
2687 libcfs_id2str(target));
2693 md = lnet_handle2md(&mdh);
2694 if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
2697 CERROR("Dropping GET ("LPU64":%d:%s): MD (%d) invalid\n",
2698 match_bits, portal, libcfs_id2str(target),
2699 md == NULL ? -1 : md->md_threshold);
2700 if (md != NULL && md->md_me != NULL)
2701 CERROR("REPLY MD also attached to portal %d\n",
2702 md->md_me->me_portal);
2708 CDEBUG(D_NET, "LNetGet -> %s\n", libcfs_id2str(target));
2710 lnet_commit_md(md, msg);
2712 lnet_prep_send(msg, LNET_MSG_GET, target, 0, 0);
2714 msg->msg_hdr.msg.get.match_bits = cpu_to_le64(match_bits);
2715 msg->msg_hdr.msg.get.ptl_index = cpu_to_le32(portal);
2716 msg->msg_hdr.msg.get.src_offset = cpu_to_le32(offset);
2717 msg->msg_hdr.msg.get.sink_length = cpu_to_le32(md->md_length);
2719 /* NB handles only looked up by creator (no flips) */
2720 msg->msg_hdr.msg.get.return_wmd.wh_interface_cookie =
2721 the_lnet.ln_interface_cookie;
2722 msg->msg_hdr.msg.get.return_wmd.wh_object_cookie =
2723 md->md_lh.lh_cookie;
2725 msg->msg_ev.type = LNET_EVENT_SEND;
2726 msg->msg_ev.initiator.nid = LNET_NID_ANY;
2727 msg->msg_ev.initiator.pid = the_lnet.ln_pid;
2728 msg->msg_ev.target = target;
2729 msg->msg_ev.sender = LNET_NID_ANY;
2730 msg->msg_ev.pt_index = portal;
2731 msg->msg_ev.match_bits = match_bits;
2732 msg->msg_ev.rlength = md->md_length;
2733 msg->msg_ev.mlength = md->md_length;
2734 msg->msg_ev.offset = offset;
2735 msg->msg_ev.hdr_data = 0;
2737 lnet_md_deconstruct(md, &msg->msg_ev.md);
2738 lnet_md2handle(&msg->msg_ev.md_handle, md);
2740 the_lnet.ln_counters.send_count++;
2744 rc = lnet_send(self, msg);
2746 CNETERR( "Error sending GET to %s: %d\n",
2747 libcfs_id2str(target), rc);
2748 lnet_finalize (NULL, msg, rc);
2751 /* completion will be signalled by an event */
2756 * Calculate distance to node at \a dstnid.
2758 * \param dstnid Target NID.
2759 * \param srcnidp If not NULL, NID of the local interface to reach \a dstnid
2761 * \param orderp If not NULL, order of the route to reach \a dstnid is saved
2764 * \retval 0 If \a dstnid belongs to a local interface, and reserved option
2765 * local_nid_dist_zero is set, which is the default.
2766 * \retval positives Distance to target NID, i.e. number of hops plus one.
2767 * \retval -EHOSTUNREACH If \a dstnid is not reachable.
2770 LNetDist (lnet_nid_t dstnid, lnet_nid_t *srcnidp, __u32 *orderp)
2774 lnet_remotenet_t *rnet;
2775 __u32 dstnet = LNET_NIDNET(dstnid);
2779 /* if !local_nid_dist_zero, I don't return a distance of 0 ever
2780 * (when lustre sees a distance of 0, it substitutes 0@lo), so I
2781 * keep order 0 free for 0@lo and order 1 free for a local NID
2784 LASSERT (the_lnet.ln_init);
2785 LASSERT (the_lnet.ln_refcount > 0);
2789 cfs_list_for_each (e, &the_lnet.ln_nis) {
2790 ni = cfs_list_entry(e, lnet_ni_t, ni_list);
2792 if (ni->ni_nid == dstnid) {
2793 if (srcnidp != NULL)
2795 if (orderp != NULL) {
2796 if (LNET_NETTYP(LNET_NIDNET(dstnid)) == LOLND)
2803 return local_nid_dist_zero ? 0 : 1;
2806 if (LNET_NIDNET(ni->ni_nid) == dstnet) {
2807 if (srcnidp != NULL)
2808 *srcnidp = ni->ni_nid;
2818 cfs_list_for_each (e, &the_lnet.ln_remote_nets) {
2819 rnet = cfs_list_entry(e, lnet_remotenet_t, lrn_list);
2821 if (rnet->lrn_net == dstnet) {
2822 lnet_route_t *route;
2823 lnet_route_t *shortest = NULL;
2825 LASSERT (!cfs_list_empty(&rnet->lrn_routes));
2827 cfs_list_for_each_entry(route, &rnet->lrn_routes,
2829 if (shortest == NULL ||
2830 route->lr_hops < shortest->lr_hops)
2834 LASSERT (shortest != NULL);
2835 hops = shortest->lr_hops;
2836 if (srcnidp != NULL)
2837 *srcnidp = shortest->lr_gateway->lp_ni->ni_nid;
2847 return -EHOSTUNREACH;
2851 * Set the number of asynchronous messages expected from a target process.
2853 * This function is only meaningful for userspace callers. It's a no-op when
2854 * called from kernel.
2856 * Asynchronous messages are those that can come from a target when the
2857 * userspace process is not waiting for IO to complete; e.g., AST callbacks
2858 * from Lustre servers. Specifying the expected number of such messages
2859 * allows them to be eagerly received when user process is not running in
2860 * LNet; otherwise network errors may occur.
2862 * \param id Process ID of the target process.
2863 * \param nasync Number of asynchronous messages expected from the target.
2865 * \return 0 on success, and an error code otherwise.
2868 LNetSetAsync(lnet_process_id_t id, int nasync)
2874 lnet_remotenet_t *rnet;
2876 lnet_route_t *route;
2883 /* Target on a local network? */
2884 ni = lnet_net2ni(LNET_NIDNET(id.nid));
2886 if (ni->ni_lnd->lnd_setasync != NULL)
2887 rc = (ni->ni_lnd->lnd_setasync)(ni, id, nasync);
2892 /* Target on a remote network: apply to routers */
2894 LIBCFS_ALLOC(nids, maxnids * sizeof(*nids));
2899 /* Snapshot all the router NIDs */
2901 rnet = lnet_find_net_locked(LNET_NIDNET(id.nid));
2903 cfs_list_for_each(tmp, &rnet->lrn_routes) {
2904 if (nnids == maxnids) {
2906 LIBCFS_FREE(nids, maxnids * sizeof(*nids));
2911 route = cfs_list_entry(tmp, lnet_route_t, lr_list);
2912 nids[nnids++] = route->lr_gateway->lp_nid;
2917 /* set async on all the routers */
2918 while (nnids-- > 0) {
2919 id.pid = LUSTRE_SRV_LNET_PID;
2920 id.nid = nids[nnids];
2922 ni = lnet_net2ni(LNET_NIDNET(id.nid));
2926 if (ni->ni_lnd->lnd_setasync != NULL) {
2927 rc2 = (ni->ni_lnd->lnd_setasync)(ni, id, nasync);
2934 LIBCFS_FREE(nids, maxnids * sizeof(*nids));