Merge remote-tracking branch 'staging/staging-next'
[deliverable/linux.git] / drivers / staging / lustre / lnet / lnet / lib-move.c
CommitLineData
d7e09d03
PT
1/*
2 * GPL HEADER START
3 *
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
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.
9 *
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).
15 *
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
6a5b99a4 18 * http://www.gnu.org/licenses/gpl-2.0.html
d7e09d03 19 *
d7e09d03
PT
20 * GPL HEADER END
21 */
22/*
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
25 *
1dc563a6 26 * Copyright (c) 2011, 2015, Intel Corporation.
d7e09d03
PT
27 */
28/*
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
31 *
32 * lnet/lnet/lib-move.c
33 *
34 * Data movement routines
35 */
36
37#define DEBUG_SUBSYSTEM S_LNET
38
9fdaf8c0 39#include "../../include/linux/lnet/lib-lnet.h"
d7e09d03
PT
40
41static int local_nid_dist_zero = 1;
8cc7b4b9
PT
42module_param(local_nid_dist_zero, int, 0444);
43MODULE_PARM_DESC(local_nid_dist_zero, "Reserved");
d7e09d03
PT
44
45int
af66a6e2 46lnet_fail_nid(lnet_nid_t nid, unsigned int threshold)
d7e09d03 47{
7e7ab095 48 lnet_test_peer_t *tp;
24f69590 49 lnet_test_peer_t *temp;
7e7ab095
MS
50 struct list_head *el;
51 struct list_head *next;
52 struct list_head cull;
d7e09d03 53
d7e09d03 54 /* NB: use lnet_net_lock(0) to serialize operations on test peers */
5fd88337 55 if (threshold) {
d7e09d03
PT
56 /* Adding a new entry */
57 LIBCFS_ALLOC(tp, sizeof(*tp));
06ace26e 58 if (!tp)
d7e09d03
PT
59 return -ENOMEM;
60
61 tp->tp_nid = nid;
62 tp->tp_threshold = threshold;
63
64 lnet_net_lock(0);
65 list_add_tail(&tp->tp_list, &the_lnet.ln_test_peers);
66 lnet_net_unlock(0);
67 return 0;
68 }
69
70 /* removing entries */
71 INIT_LIST_HEAD(&cull);
72
73 lnet_net_lock(0);
74
af66a6e2
LN
75 list_for_each_safe(el, next, &the_lnet.ln_test_peers) {
76 tp = list_entry(el, lnet_test_peer_t, tp_list);
d7e09d03 77
5fd88337 78 if (!tp->tp_threshold || /* needs culling anyway */
d7e09d03 79 nid == LNET_NID_ANY || /* removing all entries */
9b79ca85 80 tp->tp_nid == nid) { /* matched this one */
af66a6e2
LN
81 list_del(&tp->tp_list);
82 list_add(&tp->tp_list, &cull);
d7e09d03
PT
83 }
84 }
85
86 lnet_net_unlock(0);
87
24f69590 88 list_for_each_entry_safe(tp, temp, &cull, tp_list) {
af66a6e2
LN
89 list_del(&tp->tp_list);
90 LIBCFS_FREE(tp, sizeof(*tp));
d7e09d03
PT
91 }
92 return 0;
93}
94
95static int
af66a6e2 96fail_peer(lnet_nid_t nid, int outgoing)
d7e09d03
PT
97{
98 lnet_test_peer_t *tp;
24f69590 99 lnet_test_peer_t *temp;
7e7ab095
MS
100 struct list_head *el;
101 struct list_head *next;
102 struct list_head cull;
103 int fail = 0;
d7e09d03 104
af66a6e2 105 INIT_LIST_HEAD(&cull);
d7e09d03
PT
106
107 /* NB: use lnet_net_lock(0) to serialize operations on test peers */
108 lnet_net_lock(0);
109
af66a6e2
LN
110 list_for_each_safe(el, next, &the_lnet.ln_test_peers) {
111 tp = list_entry(el, lnet_test_peer_t, tp_list);
d7e09d03 112
5fd88337 113 if (!tp->tp_threshold) {
d7e09d03
PT
114 /* zombie entry */
115 if (outgoing) {
4420cfd3
JS
116 /*
117 * only cull zombies on outgoing tests,
d7e09d03 118 * since we may be at interrupt priority on
4420cfd3
JS
119 * incoming messages.
120 */
af66a6e2
LN
121 list_del(&tp->tp_list);
122 list_add(&tp->tp_list, &cull);
d7e09d03
PT
123 }
124 continue;
125 }
126
127 if (tp->tp_nid == LNET_NID_ANY || /* fail every peer */
128 nid == tp->tp_nid) { /* fail this peer */
129 fail = 1;
130
131 if (tp->tp_threshold != LNET_MD_THRESH_INF) {
132 tp->tp_threshold--;
133 if (outgoing &&
5fd88337 134 !tp->tp_threshold) {
d7e09d03 135 /* see above */
af66a6e2
LN
136 list_del(&tp->tp_list);
137 list_add(&tp->tp_list, &cull);
d7e09d03
PT
138 }
139 }
140 break;
141 }
142 }
143
144 lnet_net_unlock(0);
145
24f69590 146 list_for_each_entry_safe(tp, temp, &cull, tp_list) {
af66a6e2 147 list_del(&tp->tp_list);
d7e09d03 148
af66a6e2 149 LIBCFS_FREE(tp, sizeof(*tp));
d7e09d03
PT
150 }
151
2b5f2e44 152 return fail;
d7e09d03
PT
153}
154
155unsigned int
f351bad2 156lnet_iov_nob(unsigned int niov, struct kvec *iov)
d7e09d03
PT
157{
158 unsigned int nob = 0;
159
a739735c 160 LASSERT(!niov || iov);
d7e09d03
PT
161 while (niov-- > 0)
162 nob += (iov++)->iov_len;
163
2b5f2e44 164 return nob;
d7e09d03
PT
165}
166EXPORT_SYMBOL(lnet_iov_nob);
167
168void
4cae780e
AV
169lnet_copy_iov2iter(struct iov_iter *to,
170 unsigned int nsiov, const struct kvec *siov,
171 unsigned int soffset, unsigned int nob)
d7e09d03
PT
172{
173 /* NB diov, siov are READ-ONLY */
4cae780e
AV
174 const char *s;
175 size_t left;
d7e09d03 176
5fd88337 177 if (!nob)
d7e09d03
PT
178 return;
179
d7e09d03 180 /* skip complete frags before 'soffset' */
af66a6e2 181 LASSERT(nsiov > 0);
d7e09d03
PT
182 while (soffset >= siov->iov_len) {
183 soffset -= siov->iov_len;
184 siov++;
185 nsiov--;
af66a6e2 186 LASSERT(nsiov > 0);
d7e09d03
PT
187 }
188
4cae780e
AV
189 s = (char *)siov->iov_base + soffset;
190 left = siov->iov_len - soffset;
d7e09d03 191 do {
4cae780e 192 size_t n, copy = left;
af66a6e2 193 LASSERT(nsiov > 0);
d7e09d03 194
4cae780e
AV
195 if (copy > nob)
196 copy = nob;
197 n = copy_to_iter(s, copy, to);
198 if (n != copy)
199 return;
200 nob -= n;
201
202 siov++;
203 s = (char *)siov->iov_base;
204 left = siov->iov_len;
205 nsiov--;
206 } while (nob > 0);
207}
208EXPORT_SYMBOL(lnet_copy_iov2iter);
209
210void
211lnet_copy_kiov2iter(struct iov_iter *to,
212 unsigned int nsiov, const lnet_kiov_t *siov,
213 unsigned int soffset, unsigned int nob)
214{
215 if (!nob)
216 return;
217
218 LASSERT(!in_interrupt());
219
220 LASSERT(nsiov > 0);
221 while (soffset >= siov->bv_len) {
222 soffset -= siov->bv_len;
223 siov++;
224 nsiov--;
225 LASSERT(nsiov > 0);
226 }
227
228 do {
229 size_t copy = siov->bv_len - soffset, n;
230
231 LASSERT(nsiov > 0);
232
233 if (copy > nob)
234 copy = nob;
235 n = copy_page_to_iter(siov->bv_page,
236 siov->bv_offset + soffset,
237 copy, to);
238 if (n != copy)
239 return;
240 nob -= n;
241 siov++;
242 nsiov--;
243 soffset = 0;
d7e09d03
PT
244 } while (nob > 0);
245}
4cae780e 246EXPORT_SYMBOL(lnet_copy_kiov2iter);
d7e09d03
PT
247
248int
f351bad2 249lnet_extract_iov(int dst_niov, struct kvec *dst,
03766dca 250 int src_niov, const struct kvec *src,
c314c319 251 unsigned int offset, unsigned int len)
d7e09d03 252{
4420cfd3
JS
253 /*
254 * Initialise 'dst' to the subset of 'src' starting at 'offset',
d7e09d03 255 * for exactly 'len' bytes, and return the number of entries.
4420cfd3
JS
256 * NB not destructive to 'src'
257 */
7e7ab095
MS
258 unsigned int frag_len;
259 unsigned int niov;
d7e09d03 260
5fd88337 261 if (!len) /* no data => */
2b5f2e44 262 return 0; /* no frags */
d7e09d03 263
af66a6e2 264 LASSERT(src_niov > 0);
d7e09d03
PT
265 while (offset >= src->iov_len) { /* skip initial frags */
266 offset -= src->iov_len;
267 src_niov--;
268 src++;
af66a6e2 269 LASSERT(src_niov > 0);
d7e09d03
PT
270 }
271
272 niov = 1;
273 for (;;) {
af66a6e2
LN
274 LASSERT(src_niov > 0);
275 LASSERT((int)niov <= dst_niov);
d7e09d03
PT
276
277 frag_len = src->iov_len - offset;
278 dst->iov_base = ((char *)src->iov_base) + offset;
279
280 if (len <= frag_len) {
281 dst->iov_len = len;
2b5f2e44 282 return niov;
d7e09d03
PT
283 }
284
285 dst->iov_len = frag_len;
286
287 len -= frag_len;
288 dst++;
289 src++;
290 niov++;
291 src_niov--;
292 offset = 0;
293 }
294}
295EXPORT_SYMBOL(lnet_extract_iov);
296
d7e09d03 297unsigned int
af66a6e2 298lnet_kiov_nob(unsigned int niov, lnet_kiov_t *kiov)
d7e09d03 299{
7e7ab095 300 unsigned int nob = 0;
d7e09d03 301
a739735c 302 LASSERT(!niov || kiov);
d7e09d03 303 while (niov-- > 0)
65ffc679 304 nob += (kiov++)->bv_len;
d7e09d03 305
2b5f2e44 306 return nob;
d7e09d03
PT
307}
308EXPORT_SYMBOL(lnet_kiov_nob);
309
d7e09d03 310int
af66a6e2 311lnet_extract_kiov(int dst_niov, lnet_kiov_t *dst,
03766dca 312 int src_niov, const lnet_kiov_t *src,
c314c319 313 unsigned int offset, unsigned int len)
d7e09d03 314{
4420cfd3
JS
315 /*
316 * Initialise 'dst' to the subset of 'src' starting at 'offset',
d7e09d03 317 * for exactly 'len' bytes, and return the number of entries.
4420cfd3
JS
318 * NB not destructive to 'src'
319 */
7e7ab095
MS
320 unsigned int frag_len;
321 unsigned int niov;
d7e09d03 322
5fd88337 323 if (!len) /* no data => */
2b5f2e44 324 return 0; /* no frags */
d7e09d03 325
af66a6e2 326 LASSERT(src_niov > 0);
65ffc679
AV
327 while (offset >= src->bv_len) { /* skip initial frags */
328 offset -= src->bv_len;
d7e09d03
PT
329 src_niov--;
330 src++;
af66a6e2 331 LASSERT(src_niov > 0);
d7e09d03
PT
332 }
333
334 niov = 1;
335 for (;;) {
af66a6e2
LN
336 LASSERT(src_niov > 0);
337 LASSERT((int)niov <= dst_niov);
d7e09d03 338
65ffc679
AV
339 frag_len = src->bv_len - offset;
340 dst->bv_page = src->bv_page;
341 dst->bv_offset = src->bv_offset + offset;
d7e09d03
PT
342
343 if (len <= frag_len) {
65ffc679
AV
344 dst->bv_len = len;
345 LASSERT(dst->bv_offset + dst->bv_len
09cbfeaf 346 <= PAGE_SIZE);
2b5f2e44 347 return niov;
d7e09d03
PT
348 }
349
65ffc679
AV
350 dst->bv_len = frag_len;
351 LASSERT(dst->bv_offset + dst->bv_len <= PAGE_SIZE);
d7e09d03
PT
352
353 len -= frag_len;
354 dst++;
355 src++;
356 niov++;
357 src_niov--;
358 offset = 0;
359 }
360}
361EXPORT_SYMBOL(lnet_extract_kiov);
362
b7acfc95 363void
d7e09d03
PT
364lnet_ni_recv(lnet_ni_t *ni, void *private, lnet_msg_t *msg, int delayed,
365 unsigned int offset, unsigned int mlen, unsigned int rlen)
366{
7e7ab095 367 unsigned int niov = 0;
f351bad2 368 struct kvec *iov = NULL;
7e7ab095 369 lnet_kiov_t *kiov = NULL;
c1b7b8eb 370 struct iov_iter to;
7e7ab095 371 int rc;
d7e09d03 372
af66a6e2 373 LASSERT(!in_interrupt());
5fd88337 374 LASSERT(!mlen || msg);
d7e09d03 375
06ace26e 376 if (msg) {
d7e09d03
PT
377 LASSERT(msg->msg_receiving);
378 LASSERT(!msg->msg_sending);
379 LASSERT(rlen == msg->msg_len);
380 LASSERT(mlen <= msg->msg_len);
381 LASSERT(msg->msg_offset == offset);
382 LASSERT(msg->msg_wanted == mlen);
383
384 msg->msg_receiving = 0;
385
5fd88337 386 if (mlen) {
d7e09d03
PT
387 niov = msg->msg_niov;
388 iov = msg->msg_iov;
389 kiov = msg->msg_kiov;
390
af66a6e2 391 LASSERT(niov > 0);
06ace26e 392 LASSERT(!iov != !kiov);
d7e09d03
PT
393 }
394 }
395
c1b7b8eb
AV
396 if (iov) {
397 iov_iter_kvec(&to, ITER_KVEC | READ, iov, niov, mlen + offset);
398 iov_iter_advance(&to, offset);
399 } else {
400 iov_iter_bvec(&to, ITER_BVEC | READ, kiov, niov, mlen + offset);
401 iov_iter_advance(&to, offset);
402 }
403 rc = ni->ni_lnd->lnd_recv(ni, private, msg, delayed, &to, rlen);
d7e09d03
PT
404 if (rc < 0)
405 lnet_finalize(ni, msg, rc);
406}
407
f526b20a 408static void
d7e09d03
PT
409lnet_setpayloadbuffer(lnet_msg_t *msg)
410{
411 lnet_libmd_t *md = msg->msg_md;
412
af66a6e2
LN
413 LASSERT(msg->msg_len > 0);
414 LASSERT(!msg->msg_routing);
06ace26e 415 LASSERT(md);
5fd88337 416 LASSERT(!msg->msg_niov);
06ace26e
JS
417 LASSERT(!msg->msg_iov);
418 LASSERT(!msg->msg_kiov);
d7e09d03
PT
419
420 msg->msg_niov = md->md_niov;
5fd88337 421 if (md->md_options & LNET_MD_KIOV)
d7e09d03
PT
422 msg->msg_kiov = md->md_iov.kiov;
423 else
424 msg->msg_iov = md->md_iov.iov;
425}
426
427void
428lnet_prep_send(lnet_msg_t *msg, int type, lnet_process_id_t target,
429 unsigned int offset, unsigned int len)
430{
431 msg->msg_type = type;
432 msg->msg_target = target;
433 msg->msg_len = len;
434 msg->msg_offset = offset;
435
5fd88337 436 if (len)
d7e09d03
PT
437 lnet_setpayloadbuffer(msg);
438
af66a6e2 439 memset(&msg->msg_hdr, 0, sizeof(msg->msg_hdr));
d7e09d03
PT
440 msg->msg_hdr.type = cpu_to_le32(type);
441 msg->msg_hdr.dest_nid = cpu_to_le64(target.nid);
442 msg->msg_hdr.dest_pid = cpu_to_le32(target.pid);
443 /* src_nid will be set later */
444 msg->msg_hdr.src_pid = cpu_to_le32(the_lnet.ln_pid);
445 msg->msg_hdr.payload_length = cpu_to_le32(len);
446}
447
f526b20a 448static void
d7e09d03
PT
449lnet_ni_send(lnet_ni_t *ni, lnet_msg_t *msg)
450{
7e7ab095
MS
451 void *priv = msg->msg_private;
452 int rc;
d7e09d03 453
af66a6e2
LN
454 LASSERT(!in_interrupt());
455 LASSERT(LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND ||
c314c319 456 (msg->msg_txcredit && msg->msg_peertxcredit));
d7e09d03 457
0eee6778 458 rc = ni->ni_lnd->lnd_send(ni, priv, msg);
d7e09d03
PT
459 if (rc < 0)
460 lnet_finalize(ni, msg, rc);
461}
462
f526b20a 463static int
d7e09d03
PT
464lnet_ni_eager_recv(lnet_ni_t *ni, lnet_msg_t *msg)
465{
7e7ab095 466 int rc;
d7e09d03
PT
467
468 LASSERT(!msg->msg_sending);
469 LASSERT(msg->msg_receiving);
470 LASSERT(!msg->msg_rx_ready_delay);
06ace26e 471 LASSERT(ni->ni_lnd->lnd_eager_recv);
d7e09d03
PT
472
473 msg->msg_rx_ready_delay = 1;
0eee6778
JS
474 rc = ni->ni_lnd->lnd_eager_recv(ni, msg->msg_private, msg,
475 &msg->msg_private);
5fd88337 476 if (rc) {
2d00bd17 477 CERROR("recv from %s / send to %s aborted: eager_recv failed %d\n",
d7e09d03
PT
478 libcfs_nid2str(msg->msg_rxpeer->lp_nid),
479 libcfs_id2str(msg->msg_target), rc);
480 LASSERT(rc < 0); /* required by my callers */
481 }
482
483 return rc;
484}
485
486/* NB: caller shall hold a ref on 'lp' as I'd drop lnet_net_lock */
f526b20a 487static void
d7e09d03
PT
488lnet_ni_query_locked(lnet_ni_t *ni, lnet_peer_t *lp)
489{
a649ad1d 490 unsigned long last_alive = 0;
d7e09d03
PT
491
492 LASSERT(lnet_peer_aliveness_enabled(lp));
06ace26e 493 LASSERT(ni->ni_lnd->lnd_query);
d7e09d03
PT
494
495 lnet_net_unlock(lp->lp_cpt);
0eee6778 496 ni->ni_lnd->lnd_query(ni, lp->lp_nid, &last_alive);
d7e09d03
PT
497 lnet_net_lock(lp->lp_cpt);
498
499 lp->lp_last_query = cfs_time_current();
500
5fd88337 501 if (last_alive) /* NI has updated timestamp */
d7e09d03
PT
502 lp->lp_last_alive = last_alive;
503}
504
505/* NB: always called with lnet_net_lock held */
506static inline int
a649ad1d 507lnet_peer_is_alive(lnet_peer_t *lp, unsigned long now)
d7e09d03 508{
7e7ab095 509 int alive;
a649ad1d 510 unsigned long deadline;
d7e09d03 511
af66a6e2 512 LASSERT(lnet_peer_aliveness_enabled(lp));
d7e09d03
PT
513
514 /* Trust lnet_notify() if it has more recent aliveness news, but
515 * ignore the initial assumed death (see lnet_peers_start_down()).
516 */
517 if (!lp->lp_alive && lp->lp_alive_count > 0 &&
518 cfs_time_aftereq(lp->lp_timestamp, lp->lp_last_alive))
519 return 0;
520
521 deadline = cfs_time_add(lp->lp_last_alive,
522 cfs_time_seconds(lp->lp_ni->ni_peertimeout));
523 alive = cfs_time_after(deadline, now);
524
525 /* Update obsolete lp_alive except for routers assumed to be dead
526 * initially, because router checker would update aliveness in this
527 * case, and moreover lp_last_alive at peer creation is assumed.
528 */
529 if (alive && !lp->lp_alive &&
5fd88337 530 !(lnet_isrouter(lp) && !lp->lp_alive_count))
d7e09d03
PT
531 lnet_notify_locked(lp, 0, 1, lp->lp_last_alive);
532
533 return alive;
534}
535
4420cfd3
JS
536/*
537 * NB: returns 1 when alive, 0 when dead, negative when error;
538 * may drop the lnet_net_lock
539 */
f526b20a 540static int
af66a6e2 541lnet_peer_alive_locked(lnet_peer_t *lp)
d7e09d03 542{
a649ad1d 543 unsigned long now = cfs_time_current();
d7e09d03
PT
544
545 if (!lnet_peer_aliveness_enabled(lp))
546 return -ENODEV;
547
548 if (lnet_peer_is_alive(lp, now))
549 return 1;
550
4420cfd3
JS
551 /*
552 * Peer appears dead, but we should avoid frequent NI queries (at
553 * most once per lnet_queryinterval seconds).
554 */
5fd88337 555 if (lp->lp_last_query) {
d7e09d03
PT
556 static const int lnet_queryinterval = 1;
557
a649ad1d 558 unsigned long next_query =
d7e09d03
PT
559 cfs_time_add(lp->lp_last_query,
560 cfs_time_seconds(lnet_queryinterval));
561
699503bc 562 if (time_before(now, next_query)) {
d7e09d03 563 if (lp->lp_alive)
2d00bd17 564 CWARN("Unexpected aliveness of peer %s: %d < %d (%d/%d)\n",
d7e09d03
PT
565 libcfs_nid2str(lp->lp_nid),
566 (int)now, (int)next_query,
567 lnet_queryinterval,
568 lp->lp_ni->ni_peertimeout);
569 return 0;
570 }
571 }
572
573 /* query NI for latest aliveness news */
574 lnet_ni_query_locked(lp->lp_ni, lp);
575
576 if (lnet_peer_is_alive(lp, now))
577 return 1;
578
579 lnet_notify_locked(lp, 0, 0, lp->lp_last_alive);
580 return 0;
581}
582
dee2857e
IH
583/**
584 * \param msg The message to be sent.
585 * \param do_send True if lnet_ni_send() should be called in this function.
586 * lnet_send() is going to lnet_net_unlock immediately after this, so
587 * it sets do_send FALSE and I don't do the unlock/send/lock bit.
588 *
ec5fb5be
LZ
589 * \retval LNET_CREDIT_OK If \a msg sent or OK to send.
590 * \retval LNET_CREDIT_WAIT If \a msg blocked for credit.
591 * \retval -EHOSTUNREACH If the next hop of the message appears dead.
592 * \retval -ECANCELED If the MD of the message has been unlinked.
dee2857e
IH
593 */
594static int
d7e09d03
PT
595lnet_post_send_locked(lnet_msg_t *msg, int do_send)
596{
7e7ab095
MS
597 lnet_peer_t *lp = msg->msg_txpeer;
598 lnet_ni_t *ni = lp->lp_ni;
599 int cpt = msg->msg_tx_cpt;
600 struct lnet_tx_queue *tq = ni->ni_tx_queues[cpt];
d7e09d03
PT
601
602 /* non-lnet_send() callers have checked before */
603 LASSERT(!do_send || msg->msg_tx_delayed);
604 LASSERT(!msg->msg_receiving);
605 LASSERT(msg->msg_tx_committed);
606
d7e09d03 607 /* NB 'lp' is always the next hop */
5fd88337
JS
608 if (!(msg->msg_target.pid & LNET_PID_USERFLAG) &&
609 !lnet_peer_alive_locked(lp)) {
d7e09d03
PT
610 the_lnet.ln_counters[cpt]->drop_count++;
611 the_lnet.ln_counters[cpt]->drop_length += msg->msg_len;
612 lnet_net_unlock(cpt);
613
614 CNETERR("Dropping message for %s: peer not alive\n",
615 libcfs_id2str(msg->msg_target));
616 if (do_send)
617 lnet_finalize(ni, msg, -EHOSTUNREACH);
618
619 lnet_net_lock(cpt);
ec5fb5be 620 return -EHOSTUNREACH;
d7e09d03
PT
621 }
622
06ace26e 623 if (msg->msg_md &&
5fd88337 624 (msg->msg_md->md_flags & LNET_MD_FLAG_ABORTED)) {
dee2857e
IH
625 lnet_net_unlock(cpt);
626
2d00bd17 627 CNETERR("Aborting message for %s: LNetM[DE]Unlink() already called on the MD/ME.\n",
dee2857e
IH
628 libcfs_id2str(msg->msg_target));
629 if (do_send)
630 lnet_finalize(ni, msg, -ECANCELED);
631
632 lnet_net_lock(cpt);
ec5fb5be 633 return -ECANCELED;
dee2857e
IH
634 }
635
d7e09d03 636 if (!msg->msg_peertxcredit) {
af66a6e2 637 LASSERT((lp->lp_txcredits < 0) ==
c314c319 638 !list_empty(&lp->lp_txq));
d7e09d03
PT
639
640 msg->msg_peertxcredit = 1;
641 lp->lp_txqnob += msg->msg_len + sizeof(lnet_hdr_t);
642 lp->lp_txcredits--;
643
644 if (lp->lp_txcredits < lp->lp_mintxcredits)
645 lp->lp_mintxcredits = lp->lp_txcredits;
646
647 if (lp->lp_txcredits < 0) {
648 msg->msg_tx_delayed = 1;
649 list_add_tail(&msg->msg_list, &lp->lp_txq);
ec5fb5be 650 return LNET_CREDIT_WAIT;
d7e09d03
PT
651 }
652 }
653
654 if (!msg->msg_txcredit) {
655 LASSERT((tq->tq_credits < 0) ==
656 !list_empty(&tq->tq_delayed));
657
658 msg->msg_txcredit = 1;
659 tq->tq_credits--;
660
661 if (tq->tq_credits < tq->tq_credits_min)
662 tq->tq_credits_min = tq->tq_credits;
663
664 if (tq->tq_credits < 0) {
665 msg->msg_tx_delayed = 1;
666 list_add_tail(&msg->msg_list, &tq->tq_delayed);
ec5fb5be 667 return LNET_CREDIT_WAIT;
d7e09d03
PT
668 }
669 }
670
671 if (do_send) {
672 lnet_net_unlock(cpt);
673 lnet_ni_send(ni, msg);
674 lnet_net_lock(cpt);
675 }
ec5fb5be 676 return LNET_CREDIT_OK;
d7e09d03
PT
677}
678
f526b20a 679static lnet_rtrbufpool_t *
d7e09d03
PT
680lnet_msg2bufpool(lnet_msg_t *msg)
681{
7e7ab095
MS
682 lnet_rtrbufpool_t *rbp;
683 int cpt;
d7e09d03
PT
684
685 LASSERT(msg->msg_rx_committed);
686
687 cpt = msg->msg_rx_cpt;
688 rbp = &the_lnet.ln_rtrpools[cpt][0];
689
690 LASSERT(msg->msg_len <= LNET_MTU);
09cbfeaf 691 while (msg->msg_len > (unsigned int)rbp->rbp_npages * PAGE_SIZE) {
d7e09d03
PT
692 rbp++;
693 LASSERT(rbp < &the_lnet.ln_rtrpools[cpt][LNET_NRBPOOLS]);
694 }
695
696 return rbp;
697}
698
f526b20a 699static int
af66a6e2 700lnet_post_routed_recv_locked(lnet_msg_t *msg, int do_recv)
d7e09d03 701{
4420cfd3
JS
702 /*
703 * lnet_parse is going to lnet_net_unlock immediately after this, so it
ec5fb5be
LZ
704 * sets do_recv FALSE and I don't do the unlock/send/lock bit.
705 * I return LNET_CREDIT_WAIT if msg blocked and LNET_CREDIT_OK if
706 * received or OK to receive
4420cfd3 707 */
7e7ab095
MS
708 lnet_peer_t *lp = msg->msg_rxpeer;
709 lnet_rtrbufpool_t *rbp;
710 lnet_rtrbuf_t *rb;
d7e09d03 711
06ace26e
JS
712 LASSERT(!msg->msg_iov);
713 LASSERT(!msg->msg_kiov);
5fd88337 714 LASSERT(!msg->msg_niov);
af66a6e2
LN
715 LASSERT(msg->msg_routing);
716 LASSERT(msg->msg_receiving);
717 LASSERT(!msg->msg_sending);
d7e09d03
PT
718
719 /* non-lnet_parse callers only receive delayed messages */
720 LASSERT(!do_recv || msg->msg_rx_delayed);
721
722 if (!msg->msg_peerrtrcredit) {
af66a6e2 723 LASSERT((lp->lp_rtrcredits < 0) ==
c314c319 724 !list_empty(&lp->lp_rtrq));
d7e09d03
PT
725
726 msg->msg_peerrtrcredit = 1;
727 lp->lp_rtrcredits--;
728 if (lp->lp_rtrcredits < lp->lp_minrtrcredits)
729 lp->lp_minrtrcredits = lp->lp_rtrcredits;
730
731 if (lp->lp_rtrcredits < 0) {
732 /* must have checked eager_recv before here */
733 LASSERT(msg->msg_rx_ready_delay);
734 msg->msg_rx_delayed = 1;
735 list_add_tail(&msg->msg_list, &lp->lp_rtrq);
ec5fb5be 736 return LNET_CREDIT_WAIT;
d7e09d03
PT
737 }
738 }
739
740 rbp = lnet_msg2bufpool(msg);
741
742 if (!msg->msg_rtrcredit) {
d7e09d03
PT
743 msg->msg_rtrcredit = 1;
744 rbp->rbp_credits--;
745 if (rbp->rbp_credits < rbp->rbp_mincredits)
746 rbp->rbp_mincredits = rbp->rbp_credits;
747
748 if (rbp->rbp_credits < 0) {
749 /* must have checked eager_recv before here */
750 LASSERT(msg->msg_rx_ready_delay);
751 msg->msg_rx_delayed = 1;
752 list_add_tail(&msg->msg_list, &rbp->rbp_msgs);
ec5fb5be 753 return LNET_CREDIT_WAIT;
d7e09d03
PT
754 }
755 }
756
af66a6e2 757 LASSERT(!list_empty(&rbp->rbp_bufs));
d7e09d03
PT
758 rb = list_entry(rbp->rbp_bufs.next, lnet_rtrbuf_t, rb_list);
759 list_del(&rb->rb_list);
760
761 msg->msg_niov = rbp->rbp_npages;
762 msg->msg_kiov = &rb->rb_kiov[0];
763
764 if (do_recv) {
765 int cpt = msg->msg_rx_cpt;
766
767 lnet_net_unlock(cpt);
768 lnet_ni_recv(lp->lp_ni, msg->msg_private, msg, 1,
769 0, msg->msg_len, msg->msg_len);
770 lnet_net_lock(cpt);
771 }
ec5fb5be 772 return LNET_CREDIT_OK;
d7e09d03
PT
773}
774
775void
776lnet_return_tx_credits_locked(lnet_msg_t *msg)
777{
7e7ab095
MS
778 lnet_peer_t *txpeer = msg->msg_txpeer;
779 lnet_msg_t *msg2;
d7e09d03
PT
780
781 if (msg->msg_txcredit) {
7e7ab095 782 struct lnet_ni *ni = txpeer->lp_ni;
d7e09d03
PT
783 struct lnet_tx_queue *tq = ni->ni_tx_queues[msg->msg_tx_cpt];
784
785 /* give back NI txcredits */
786 msg->msg_txcredit = 0;
787
788 LASSERT((tq->tq_credits < 0) ==
789 !list_empty(&tq->tq_delayed));
790
791 tq->tq_credits++;
792 if (tq->tq_credits <= 0) {
793 msg2 = list_entry(tq->tq_delayed.next,
c314c319 794 lnet_msg_t, msg_list);
d7e09d03
PT
795 list_del(&msg2->msg_list);
796
797 LASSERT(msg2->msg_txpeer->lp_ni == ni);
798 LASSERT(msg2->msg_tx_delayed);
799
bb94df96 800 (void)lnet_post_send_locked(msg2, 1);
d7e09d03
PT
801 }
802 }
803
804 if (msg->msg_peertxcredit) {
805 /* give back peer txcredits */
806 msg->msg_peertxcredit = 0;
807
808 LASSERT((txpeer->lp_txcredits < 0) ==
809 !list_empty(&txpeer->lp_txq));
810
811 txpeer->lp_txqnob -= msg->msg_len + sizeof(lnet_hdr_t);
af66a6e2 812 LASSERT(txpeer->lp_txqnob >= 0);
d7e09d03
PT
813
814 txpeer->lp_txcredits++;
815 if (txpeer->lp_txcredits <= 0) {
816 msg2 = list_entry(txpeer->lp_txq.next,
c314c319 817 lnet_msg_t, msg_list);
d7e09d03
PT
818 list_del(&msg2->msg_list);
819
820 LASSERT(msg2->msg_txpeer == txpeer);
821 LASSERT(msg2->msg_tx_delayed);
822
bb94df96 823 (void)lnet_post_send_locked(msg2, 1);
d7e09d03
PT
824 }
825 }
826
06ace26e 827 if (txpeer) {
d7e09d03
PT
828 msg->msg_txpeer = NULL;
829 lnet_peer_decref_locked(txpeer);
830 }
831}
832
86ef6250
AS
833void
834lnet_schedule_blocked_locked(lnet_rtrbufpool_t *rbp)
835{
836 lnet_msg_t *msg;
837
838 if (list_empty(&rbp->rbp_msgs))
839 return;
840 msg = list_entry(rbp->rbp_msgs.next,
841 lnet_msg_t, msg_list);
842 list_del(&msg->msg_list);
843
844 (void)lnet_post_routed_recv_locked(msg, 1);
845}
846
847void
848lnet_drop_routed_msgs_locked(struct list_head *list, int cpt)
849{
850 struct list_head drop;
851 lnet_msg_t *msg;
852 lnet_msg_t *tmp;
853
854 INIT_LIST_HEAD(&drop);
855
856 list_splice_init(list, &drop);
857
858 lnet_net_unlock(cpt);
859
860 list_for_each_entry_safe(msg, tmp, &drop, msg_list) {
861 lnet_ni_recv(msg->msg_rxpeer->lp_ni, msg->msg_private, NULL,
862 0, 0, 0, msg->msg_hdr.payload_length);
863 list_del_init(&msg->msg_list);
864 lnet_finalize(NULL, msg, -ECANCELED);
865 }
866
867 lnet_net_lock(cpt);
868}
869
d7e09d03
PT
870void
871lnet_return_rx_credits_locked(lnet_msg_t *msg)
872{
7e7ab095
MS
873 lnet_peer_t *rxpeer = msg->msg_rxpeer;
874 lnet_msg_t *msg2;
d7e09d03
PT
875
876 if (msg->msg_rtrcredit) {
877 /* give back global router credits */
7e7ab095 878 lnet_rtrbuf_t *rb;
d7e09d03
PT
879 lnet_rtrbufpool_t *rbp;
880
4420cfd3
JS
881 /*
882 * NB If a msg ever blocks for a buffer in rbp_msgs, it stays
d7e09d03 883 * there until it gets one allocated, or aborts the wait
4420cfd3
JS
884 * itself
885 */
06ace26e 886 LASSERT(msg->msg_kiov);
d7e09d03
PT
887
888 rb = list_entry(msg->msg_kiov, lnet_rtrbuf_t, rb_kiov[0]);
889 rbp = rb->rb_pool;
d7e09d03
PT
890
891 msg->msg_kiov = NULL;
892 msg->msg_rtrcredit = 0;
893
86ef6250
AS
894 LASSERT(rbp == lnet_msg2bufpool(msg));
895
d7e09d03
PT
896 LASSERT((rbp->rbp_credits > 0) ==
897 !list_empty(&rbp->rbp_bufs));
898
86ef6250
AS
899 /*
900 * If routing is now turned off, we just drop this buffer and
901 * don't bother trying to return credits.
902 */
903 if (!the_lnet.ln_routing) {
904 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
905 goto routing_off;
906 }
d7e09d03 907
86ef6250
AS
908 /*
909 * It is possible that a user has lowered the desired number of
910 * buffers in this pool. Make sure we never put back
911 * more buffers than the stated number.
912 */
95fc2938 913 if (unlikely(rbp->rbp_credits >= rbp->rbp_req_nbuffers)) {
86ef6250
AS
914 /* Discard this buffer so we don't have too many. */
915 lnet_destroy_rtrbuf(rb, rbp->rbp_npages);
95fc2938 916 rbp->rbp_nbuffers--;
86ef6250
AS
917 } else {
918 list_add(&rb->rb_list, &rbp->rbp_bufs);
919 rbp->rbp_credits++;
920 if (rbp->rbp_credits <= 0)
921 lnet_schedule_blocked_locked(rbp);
d7e09d03
PT
922 }
923 }
924
86ef6250 925routing_off:
d7e09d03
PT
926 if (msg->msg_peerrtrcredit) {
927 /* give back peer router credits */
928 msg->msg_peerrtrcredit = 0;
929
930 LASSERT((rxpeer->lp_rtrcredits < 0) ==
931 !list_empty(&rxpeer->lp_rtrq));
932
933 rxpeer->lp_rtrcredits++;
86ef6250
AS
934 /*
935 * drop all messages which are queued to be routed on that
936 * peer.
937 */
938 if (!the_lnet.ln_routing) {
939 lnet_drop_routed_msgs_locked(&rxpeer->lp_rtrq,
940 msg->msg_rx_cpt);
941 } else if (rxpeer->lp_rtrcredits <= 0) {
d7e09d03 942 msg2 = list_entry(rxpeer->lp_rtrq.next,
c314c319 943 lnet_msg_t, msg_list);
d7e09d03
PT
944 list_del(&msg2->msg_list);
945
bb94df96 946 (void)lnet_post_routed_recv_locked(msg2, 1);
d7e09d03
PT
947 }
948 }
06ace26e 949 if (rxpeer) {
d7e09d03
PT
950 msg->msg_rxpeer = NULL;
951 lnet_peer_decref_locked(rxpeer);
952 }
953}
954
955static int
956lnet_compare_routes(lnet_route_t *r1, lnet_route_t *r2)
957{
958 lnet_peer_t *p1 = r1->lr_gateway;
959 lnet_peer_t *p2 = r2->lr_gateway;
b9bbb61c
AS
960 int r1_hops = (r1->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r1->lr_hops;
961 int r2_hops = (r2->lr_hops == LNET_UNDEFINED_HOPS) ? 1 : r2->lr_hops;
d7e09d03 962
e75fb87f
DO
963 if (r1->lr_priority < r2->lr_priority)
964 return 1;
965
966 if (r1->lr_priority > r2->lr_priority)
58cb2ad3 967 return -ERANGE;
e75fb87f 968
b9bbb61c 969 if (r1_hops < r2_hops)
d7e09d03
PT
970 return 1;
971
b9bbb61c 972 if (r1_hops > r2_hops)
58cb2ad3 973 return -ERANGE;
d7e09d03
PT
974
975 if (p1->lp_txqnob < p2->lp_txqnob)
976 return 1;
977
978 if (p1->lp_txqnob > p2->lp_txqnob)
58cb2ad3 979 return -ERANGE;
d7e09d03
PT
980
981 if (p1->lp_txcredits > p2->lp_txcredits)
982 return 1;
983
984 if (p1->lp_txcredits < p2->lp_txcredits)
58cb2ad3 985 return -ERANGE;
d7e09d03
PT
986
987 if (r1->lr_seq - r2->lr_seq <= 0)
988 return 1;
989
58cb2ad3 990 return -ERANGE;
d7e09d03
PT
991}
992
993static lnet_peer_t *
994lnet_find_route_locked(lnet_ni_t *ni, lnet_nid_t target, lnet_nid_t rtr_nid)
995{
7e7ab095 996 lnet_remotenet_t *rnet;
4f0bedec
CH
997 lnet_route_t *route;
998 lnet_route_t *best_route;
999 lnet_route_t *last_route;
7e7ab095
MS
1000 struct lnet_peer *lp_best;
1001 struct lnet_peer *lp;
1002 int rc;
d7e09d03 1003
4420cfd3
JS
1004 /*
1005 * If @rtr_nid is not LNET_NID_ANY, return the gateway with
1006 * rtr_nid nid, otherwise find the best gateway I can use
1007 */
d7e09d03 1008 rnet = lnet_find_net_locked(LNET_NIDNET(target));
06ace26e 1009 if (!rnet)
d7e09d03
PT
1010 return NULL;
1011
1012 lp_best = NULL;
4f0bedec
CH
1013 best_route = NULL;
1014 last_route = NULL;
1015 list_for_each_entry(route, &rnet->lrn_routes, lr_list) {
1016 lp = route->lr_gateway;
d7e09d03 1017
4ee23a84 1018 if (!lnet_is_route_alive(route))
d7e09d03
PT
1019 continue;
1020
06ace26e 1021 if (ni && lp->lp_ni != ni)
d7e09d03
PT
1022 continue;
1023
1024 if (lp->lp_nid == rtr_nid) /* it's pre-determined router */
1025 return lp;
1026
06ace26e 1027 if (!lp_best) {
4f0bedec
CH
1028 best_route = route;
1029 last_route = route;
d7e09d03
PT
1030 lp_best = lp;
1031 continue;
1032 }
1033
1034 /* no protection on below fields, but it's harmless */
4f0bedec
CH
1035 if (last_route->lr_seq - route->lr_seq < 0)
1036 last_route = route;
d7e09d03 1037
4f0bedec 1038 rc = lnet_compare_routes(route, best_route);
d7e09d03
PT
1039 if (rc < 0)
1040 continue;
1041
4f0bedec 1042 best_route = route;
d7e09d03
PT
1043 lp_best = lp;
1044 }
1045
4420cfd3
JS
1046 /*
1047 * set sequence number on the best router to the latest sequence + 1
d7e09d03 1048 * so we can round-robin all routers, it's race and inaccurate but
4420cfd3
JS
1049 * harmless and functional
1050 */
4f0bedec
CH
1051 if (best_route)
1052 best_route->lr_seq = last_route->lr_seq + 1;
d7e09d03
PT
1053 return lp_best;
1054}
1055
1056int
1057lnet_send(lnet_nid_t src_nid, lnet_msg_t *msg, lnet_nid_t rtr_nid)
1058{
7e7ab095
MS
1059 lnet_nid_t dst_nid = msg->msg_target.nid;
1060 struct lnet_ni *src_ni;
1061 struct lnet_ni *local_ni;
1062 struct lnet_peer *lp;
1063 int cpt;
1064 int cpt2;
1065 int rc;
d7e09d03 1066
4420cfd3
JS
1067 /*
1068 * NB: rtr_nid is set to LNET_NID_ANY for all current use-cases,
d7e09d03 1069 * but we might want to use pre-determined router for ACK/REPLY
4420cfd3
JS
1070 * in the future
1071 */
06ace26e
JS
1072 /* NB: ni == interface pre-determined (ACK/REPLY) */
1073 LASSERT(!msg->msg_txpeer);
af66a6e2
LN
1074 LASSERT(!msg->msg_sending);
1075 LASSERT(!msg->msg_target_is_router);
1076 LASSERT(!msg->msg_receiving);
d7e09d03
PT
1077
1078 msg->msg_sending = 1;
1079
1080 LASSERT(!msg->msg_tx_committed);
1081 cpt = lnet_cpt_of_nid(rtr_nid == LNET_NID_ANY ? dst_nid : rtr_nid);
1082 again:
1083 lnet_net_lock(cpt);
1084
1085 if (the_lnet.ln_shutdown) {
1086 lnet_net_unlock(cpt);
1087 return -ESHUTDOWN;
1088 }
1089
1090 if (src_nid == LNET_NID_ANY) {
1091 src_ni = NULL;
1092 } else {
1093 src_ni = lnet_nid2ni_locked(src_nid, cpt);
06ace26e 1094 if (!src_ni) {
d7e09d03 1095 lnet_net_unlock(cpt);
2d00bd17
JP
1096 LCONSOLE_WARN("Can't send to %s: src %s is not a local nid\n",
1097 libcfs_nid2str(dst_nid),
d7e09d03
PT
1098 libcfs_nid2str(src_nid));
1099 return -EINVAL;
1100 }
af66a6e2 1101 LASSERT(!msg->msg_routing);
d7e09d03
PT
1102 }
1103
1104 /* Is this for someone on a local network? */
1105 local_ni = lnet_net2ni_locked(LNET_NIDNET(dst_nid), cpt);
1106
06ace26e
JS
1107 if (local_ni) {
1108 if (!src_ni) {
d7e09d03
PT
1109 src_ni = local_ni;
1110 src_nid = src_ni->ni_nid;
1111 } else if (src_ni == local_ni) {
1112 lnet_ni_decref_locked(local_ni, cpt);
1113 } else {
1114 lnet_ni_decref_locked(local_ni, cpt);
1115 lnet_ni_decref_locked(src_ni, cpt);
1116 lnet_net_unlock(cpt);
1117 LCONSOLE_WARN("No route to %s via from %s\n",
1118 libcfs_nid2str(dst_nid),
1119 libcfs_nid2str(src_nid));
1120 return -EINVAL;
1121 }
1122
1123 LASSERT(src_nid != LNET_NID_ANY);
1124 lnet_msg_commit(msg, cpt);
1125
1126 if (!msg->msg_routing)
1127 msg->msg_hdr.src_nid = cpu_to_le64(src_nid);
1128
1129 if (src_ni == the_lnet.ln_loni) {
1130 /* No send credit hassles with LOLND */
1131 lnet_net_unlock(cpt);
1132 lnet_ni_send(src_ni, msg);
1133
1134 lnet_net_lock(cpt);
1135 lnet_ni_decref_locked(src_ni, cpt);
1136 lnet_net_unlock(cpt);
1137 return 0;
1138 }
1139
1140 rc = lnet_nid2peer_locked(&lp, dst_nid, cpt);
1141 /* lp has ref on src_ni; lose mine */
1142 lnet_ni_decref_locked(src_ni, cpt);
5fd88337 1143 if (rc) {
d7e09d03
PT
1144 lnet_net_unlock(cpt);
1145 LCONSOLE_WARN("Error %d finding peer %s\n", rc,
1146 libcfs_nid2str(dst_nid));
1147 /* ENOMEM or shutting down */
1148 return rc;
1149 }
af66a6e2 1150 LASSERT(lp->lp_ni == src_ni);
d7e09d03
PT
1151 } else {
1152 /* sending to a remote network */
1153 lp = lnet_find_route_locked(src_ni, dst_nid, rtr_nid);
06ace26e
JS
1154 if (!lp) {
1155 if (src_ni)
d7e09d03
PT
1156 lnet_ni_decref_locked(src_ni, cpt);
1157 lnet_net_unlock(cpt);
1158
2d00bd17 1159 LCONSOLE_WARN("No route to %s via %s (all routers down)\n",
d7e09d03
PT
1160 libcfs_id2str(msg->msg_target),
1161 libcfs_nid2str(src_nid));
1162 return -EHOSTUNREACH;
1163 }
1164
4420cfd3
JS
1165 /*
1166 * rtr_nid is LNET_NID_ANY or NID of pre-determined router,
d7e09d03
PT
1167 * it's possible that rtr_nid isn't LNET_NID_ANY and lp isn't
1168 * pre-determined router, this can happen if router table
4420cfd3
JS
1169 * was changed when we release the lock
1170 */
d7e09d03
PT
1171 if (rtr_nid != lp->lp_nid) {
1172 cpt2 = lnet_cpt_of_nid_locked(lp->lp_nid);
1173 if (cpt2 != cpt) {
06ace26e 1174 if (src_ni)
d7e09d03
PT
1175 lnet_ni_decref_locked(src_ni, cpt);
1176 lnet_net_unlock(cpt);
1177
1178 rtr_nid = lp->lp_nid;
1179 cpt = cpt2;
1180 goto again;
1181 }
1182 }
1183
1184 CDEBUG(D_NET, "Best route to %s via %s for %s %d\n",
1185 libcfs_nid2str(dst_nid), libcfs_nid2str(lp->lp_nid),
1186 lnet_msgtyp2str(msg->msg_type), msg->msg_len);
1187
06ace26e 1188 if (!src_ni) {
d7e09d03
PT
1189 src_ni = lp->lp_ni;
1190 src_nid = src_ni->ni_nid;
1191 } else {
af66a6e2 1192 LASSERT(src_ni == lp->lp_ni);
d7e09d03
PT
1193 lnet_ni_decref_locked(src_ni, cpt);
1194 }
1195
1196 lnet_peer_addref_locked(lp);
1197
1198 LASSERT(src_nid != LNET_NID_ANY);
1199 lnet_msg_commit(msg, cpt);
1200
1201 if (!msg->msg_routing) {
1202 /* I'm the source and now I know which NI to send on */
1203 msg->msg_hdr.src_nid = cpu_to_le64(src_nid);
1204 }
1205
1206 msg->msg_target_is_router = 1;
1207 msg->msg_target.nid = lp->lp_nid;
fe7cb65d 1208 msg->msg_target.pid = LNET_PID_LUSTRE;
d7e09d03
PT
1209 }
1210
1211 /* 'lp' is our best choice of peer */
1212
af66a6e2
LN
1213 LASSERT(!msg->msg_peertxcredit);
1214 LASSERT(!msg->msg_txcredit);
06ace26e 1215 LASSERT(!msg->msg_txpeer);
d7e09d03
PT
1216
1217 msg->msg_txpeer = lp; /* msg takes my ref on lp */
1218
1219 rc = lnet_post_send_locked(msg, 0);
1220 lnet_net_unlock(cpt);
1221
ec5fb5be
LZ
1222 if (rc < 0)
1223 return rc;
d7e09d03 1224
ec5fb5be 1225 if (rc == LNET_CREDIT_OK)
d7e09d03
PT
1226 lnet_ni_send(src_ni, msg);
1227
ec5fb5be 1228 return 0; /* rc == LNET_CREDIT_OK or LNET_CREDIT_WAIT */
d7e09d03
PT
1229}
1230
b7acfc95 1231void
d7e09d03
PT
1232lnet_drop_message(lnet_ni_t *ni, int cpt, void *private, unsigned int nob)
1233{
1234 lnet_net_lock(cpt);
1235 the_lnet.ln_counters[cpt]->drop_count++;
1236 the_lnet.ln_counters[cpt]->drop_length += nob;
1237 lnet_net_unlock(cpt);
1238
1239 lnet_ni_recv(ni, private, NULL, 0, 0, 0, nob);
1240}
1241
1242static void
1243lnet_recv_put(lnet_ni_t *ni, lnet_msg_t *msg)
1244{
7e7ab095 1245 lnet_hdr_t *hdr = &msg->msg_hdr;
d7e09d03 1246
5fd88337 1247 if (msg->msg_wanted)
d7e09d03
PT
1248 lnet_setpayloadbuffer(msg);
1249
1250 lnet_build_msg_event(msg, LNET_EVENT_PUT);
1251
4420cfd3
JS
1252 /*
1253 * Must I ACK? If so I'll grab the ack_wmd out of the header and put
1254 * it back into the ACK during lnet_finalize()
1255 */
5fd88337
JS
1256 msg->msg_ack = !lnet_is_wire_handle_none(&hdr->msg.put.ack_wmd) &&
1257 !(msg->msg_md->md_options & LNET_MD_ACK_DISABLE);
d7e09d03
PT
1258
1259 lnet_ni_recv(ni, msg->msg_private, msg, msg->msg_rx_delayed,
1260 msg->msg_offset, msg->msg_wanted, hdr->payload_length);
1261}
1262
1263static int
1264lnet_parse_put(lnet_ni_t *ni, lnet_msg_t *msg)
1265{
7e7ab095
MS
1266 lnet_hdr_t *hdr = &msg->msg_hdr;
1267 struct lnet_match_info info;
5b16d52b 1268 bool ready_delay;
7e7ab095 1269 int rc;
d7e09d03
PT
1270
1271 /* Convert put fields to host byte order */
1272 hdr->msg.put.match_bits = le64_to_cpu(hdr->msg.put.match_bits);
1273 hdr->msg.put.ptl_index = le32_to_cpu(hdr->msg.put.ptl_index);
1274 hdr->msg.put.offset = le32_to_cpu(hdr->msg.put.offset);
1275
1276 info.mi_id.nid = hdr->src_nid;
1277 info.mi_id.pid = hdr->src_pid;
1278 info.mi_opc = LNET_MD_OP_PUT;
1279 info.mi_portal = hdr->msg.put.ptl_index;
1280 info.mi_rlength = hdr->payload_length;
1281 info.mi_roffset = hdr->msg.put.offset;
1282 info.mi_mbits = hdr->msg.put.match_bits;
1283
06ace26e 1284 msg->msg_rx_ready_delay = !ni->ni_lnd->lnd_eager_recv;
5b16d52b 1285 ready_delay = msg->msg_rx_ready_delay;
d7e09d03
PT
1286
1287 again:
1288 rc = lnet_ptl_match_md(&info, msg);
1289 switch (rc) {
1290 default:
1291 LBUG();
1292
1293 case LNET_MATCHMD_OK:
1294 lnet_recv_put(ni, msg);
1295 return 0;
1296
1297 case LNET_MATCHMD_NONE:
5b16d52b
LZ
1298 /**
1299 * no eager_recv or has already called it, should
1300 * have been attached on delayed list
1301 */
1302 if (ready_delay)
d7e09d03
PT
1303 return 0;
1304
1305 rc = lnet_ni_eager_recv(ni, msg);
5b16d52b
LZ
1306 if (!rc) {
1307 ready_delay = true;
d7e09d03 1308 goto again;
5b16d52b 1309 }
d7e09d03
PT
1310 /* fall through */
1311
1312 case LNET_MATCHMD_DROP:
b0f5aad5 1313 CNETERR("Dropping PUT from %s portal %d match %llu offset %d length %d: %d\n",
d7e09d03
PT
1314 libcfs_id2str(info.mi_id), info.mi_portal,
1315 info.mi_mbits, info.mi_roffset, info.mi_rlength, rc);
1316
58cb2ad3 1317 return -ENOENT; /* -ve: OK but no match */
d7e09d03
PT
1318 }
1319}
1320
1321static int
1322lnet_parse_get(lnet_ni_t *ni, lnet_msg_t *msg, int rdma_get)
1323{
7e7ab095
MS
1324 struct lnet_match_info info;
1325 lnet_hdr_t *hdr = &msg->msg_hdr;
1326 lnet_handle_wire_t reply_wmd;
1327 int rc;
d7e09d03
PT
1328
1329 /* Convert get fields to host byte order */
7e7ab095
MS
1330 hdr->msg.get.match_bits = le64_to_cpu(hdr->msg.get.match_bits);
1331 hdr->msg.get.ptl_index = le32_to_cpu(hdr->msg.get.ptl_index);
1332 hdr->msg.get.sink_length = le32_to_cpu(hdr->msg.get.sink_length);
1333 hdr->msg.get.src_offset = le32_to_cpu(hdr->msg.get.src_offset);
1334
1335 info.mi_id.nid = hdr->src_nid;
1336 info.mi_id.pid = hdr->src_pid;
1337 info.mi_opc = LNET_MD_OP_GET;
1338 info.mi_portal = hdr->msg.get.ptl_index;
1339 info.mi_rlength = hdr->msg.get.sink_length;
1340 info.mi_roffset = hdr->msg.get.src_offset;
1341 info.mi_mbits = hdr->msg.get.match_bits;
d7e09d03
PT
1342
1343 rc = lnet_ptl_match_md(&info, msg);
1344 if (rc == LNET_MATCHMD_DROP) {
b0f5aad5 1345 CNETERR("Dropping GET from %s portal %d match %llu offset %d length %d\n",
d7e09d03
PT
1346 libcfs_id2str(info.mi_id), info.mi_portal,
1347 info.mi_mbits, info.mi_roffset, info.mi_rlength);
58cb2ad3 1348 return -ENOENT; /* -ve: OK but no match */
d7e09d03
PT
1349 }
1350
1351 LASSERT(rc == LNET_MATCHMD_OK);
1352
1353 lnet_build_msg_event(msg, LNET_EVENT_GET);
1354
1355 reply_wmd = hdr->msg.get.return_wmd;
1356
1357 lnet_prep_send(msg, LNET_MSG_REPLY, info.mi_id,
1358 msg->msg_offset, msg->msg_wanted);
1359
1360 msg->msg_hdr.msg.reply.dst_wmd = reply_wmd;
1361
1362 if (rdma_get) {
1363 /* The LND completes the REPLY from her recv procedure */
1364 lnet_ni_recv(ni, msg->msg_private, msg, 0,
1365 msg->msg_offset, msg->msg_len, msg->msg_len);
1366 return 0;
1367 }
1368
1369 lnet_ni_recv(ni, msg->msg_private, NULL, 0, 0, 0, 0);
1370 msg->msg_receiving = 0;
1371
1372 rc = lnet_send(ni->ni_nid, msg, LNET_NID_ANY);
1373 if (rc < 0) {
1374 /* didn't get as far as lnet_ni_send() */
1375 CERROR("%s: Unable to send REPLY for GET from %s: %d\n",
1376 libcfs_nid2str(ni->ni_nid),
1377 libcfs_id2str(info.mi_id), rc);
1378
1379 lnet_finalize(ni, msg, rc);
1380 }
1381
1382 return 0;
1383}
1384
1385static int
1386lnet_parse_reply(lnet_ni_t *ni, lnet_msg_t *msg)
1387{
7e7ab095
MS
1388 void *private = msg->msg_private;
1389 lnet_hdr_t *hdr = &msg->msg_hdr;
d7e09d03 1390 lnet_process_id_t src = {0};
7e7ab095
MS
1391 lnet_libmd_t *md;
1392 int rlength;
1393 int mlength;
1394 int cpt;
d7e09d03
PT
1395
1396 cpt = lnet_cpt_of_cookie(hdr->msg.reply.dst_wmd.wh_object_cookie);
1397 lnet_res_lock(cpt);
1398
1399 src.nid = hdr->src_nid;
1400 src.pid = hdr->src_pid;
1401
1402 /* NB handles only looked up by creator (no flips) */
1403 md = lnet_wire_handle2md(&hdr->msg.reply.dst_wmd);
5fd88337 1404 if (!md || !md->md_threshold || md->md_me) {
55f5a824 1405 CNETERR("%s: Dropping REPLY from %s for %s MD %#llx.%#llx\n",
d7e09d03 1406 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
06ace26e 1407 !md ? "invalid" : "inactive",
d7e09d03
PT
1408 hdr->msg.reply.dst_wmd.wh_interface_cookie,
1409 hdr->msg.reply.dst_wmd.wh_object_cookie);
06ace26e 1410 if (md && md->md_me)
d7e09d03
PT
1411 CERROR("REPLY MD also attached to portal %d\n",
1412 md->md_me->me_portal);
1413
1414 lnet_res_unlock(cpt);
58cb2ad3 1415 return -ENOENT; /* -ve: OK but no match */
d7e09d03
PT
1416 }
1417
5fd88337 1418 LASSERT(!md->md_offset);
d7e09d03
PT
1419
1420 rlength = hdr->payload_length;
005b23d6 1421 mlength = min_t(uint, rlength, md->md_length);
d7e09d03
PT
1422
1423 if (mlength < rlength &&
5fd88337 1424 !(md->md_options & LNET_MD_TRUNCATE)) {
55f5a824 1425 CNETERR("%s: Dropping REPLY from %s length %d for MD %#llx would overflow (%d)\n",
d7e09d03
PT
1426 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1427 rlength, hdr->msg.reply.dst_wmd.wh_object_cookie,
1428 mlength);
1429 lnet_res_unlock(cpt);
58cb2ad3 1430 return -ENOENT; /* -ve: OK but no match */
d7e09d03
PT
1431 }
1432
55f5a824 1433 CDEBUG(D_NET, "%s: Reply from %s of length %d/%d into md %#llx\n",
d7e09d03
PT
1434 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1435 mlength, rlength, hdr->msg.reply.dst_wmd.wh_object_cookie);
1436
1437 lnet_msg_attach_md(msg, md, 0, mlength);
1438
5fd88337 1439 if (mlength)
d7e09d03
PT
1440 lnet_setpayloadbuffer(msg);
1441
1442 lnet_res_unlock(cpt);
1443
1444 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
1445
1446 lnet_ni_recv(ni, private, msg, 0, 0, mlength, rlength);
1447 return 0;
1448}
1449
1450static int
1451lnet_parse_ack(lnet_ni_t *ni, lnet_msg_t *msg)
1452{
7e7ab095 1453 lnet_hdr_t *hdr = &msg->msg_hdr;
d7e09d03 1454 lnet_process_id_t src = {0};
7e7ab095
MS
1455 lnet_libmd_t *md;
1456 int cpt;
d7e09d03
PT
1457
1458 src.nid = hdr->src_nid;
1459 src.pid = hdr->src_pid;
1460
1461 /* Convert ack fields to host byte order */
1462 hdr->msg.ack.match_bits = le64_to_cpu(hdr->msg.ack.match_bits);
1463 hdr->msg.ack.mlength = le32_to_cpu(hdr->msg.ack.mlength);
1464
1465 cpt = lnet_cpt_of_cookie(hdr->msg.ack.dst_wmd.wh_object_cookie);
1466 lnet_res_lock(cpt);
1467
1468 /* NB handles only looked up by creator (no flips) */
1469 md = lnet_wire_handle2md(&hdr->msg.ack.dst_wmd);
5fd88337 1470 if (!md || !md->md_threshold || md->md_me) {
d7e09d03
PT
1471 /* Don't moan; this is expected */
1472 CDEBUG(D_NET,
55f5a824 1473 "%s: Dropping ACK from %s to %s MD %#llx.%#llx\n",
d7e09d03 1474 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
06ace26e 1475 !md ? "invalid" : "inactive",
d7e09d03
PT
1476 hdr->msg.ack.dst_wmd.wh_interface_cookie,
1477 hdr->msg.ack.dst_wmd.wh_object_cookie);
06ace26e 1478 if (md && md->md_me)
d7e09d03
PT
1479 CERROR("Source MD also attached to portal %d\n",
1480 md->md_me->me_portal);
1481
1482 lnet_res_unlock(cpt);
58cb2ad3 1483 return -ENOENT; /* -ve! */
d7e09d03
PT
1484 }
1485
55f5a824 1486 CDEBUG(D_NET, "%s: ACK from %s into md %#llx\n",
d7e09d03
PT
1487 libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1488 hdr->msg.ack.dst_wmd.wh_object_cookie);
1489
1490 lnet_msg_attach_md(msg, md, 0, 0);
1491
1492 lnet_res_unlock(cpt);
1493
1494 lnet_build_msg_event(msg, LNET_EVENT_ACK);
1495
1496 lnet_ni_recv(ni, msg->msg_private, msg, 0, 0, 0, msg->msg_len);
1497 return 0;
1498}
1499
ec5fb5be
LZ
1500/**
1501 * \retval LNET_CREDIT_OK If \a msg is forwarded
1502 * \retval LNET_CREDIT_WAIT If \a msg is blocked because w/o buffer
1503 * \retval -ve error code
1504 */
b7acfc95 1505int
d7e09d03
PT
1506lnet_parse_forward_locked(lnet_ni_t *ni, lnet_msg_t *msg)
1507{
7e7ab095 1508 int rc = 0;
d7e09d03 1509
86ef6250
AS
1510 if (!the_lnet.ln_routing)
1511 return -ECANCELED;
1512
d7e09d03
PT
1513 if (msg->msg_rxpeer->lp_rtrcredits <= 0 ||
1514 lnet_msg2bufpool(msg)->rbp_credits <= 0) {
06ace26e 1515 if (!ni->ni_lnd->lnd_eager_recv) {
d7e09d03
PT
1516 msg->msg_rx_ready_delay = 1;
1517 } else {
1518 lnet_net_unlock(msg->msg_rx_cpt);
1519 rc = lnet_ni_eager_recv(ni, msg);
1520 lnet_net_lock(msg->msg_rx_cpt);
1521 }
1522 }
1523
5fd88337 1524 if (!rc)
d7e09d03
PT
1525 rc = lnet_post_routed_recv_locked(msg, 0);
1526 return rc;
1527}
1528
b7acfc95
LZ
1529int
1530lnet_parse_local(lnet_ni_t *ni, lnet_msg_t *msg)
1531{
1532 int rc;
1533
1534 switch (msg->msg_type) {
1535 case LNET_MSG_ACK:
1536 rc = lnet_parse_ack(ni, msg);
1537 break;
1538 case LNET_MSG_PUT:
1539 rc = lnet_parse_put(ni, msg);
1540 break;
1541 case LNET_MSG_GET:
1542 rc = lnet_parse_get(ni, msg, msg->msg_rdma_get);
1543 break;
1544 case LNET_MSG_REPLY:
1545 rc = lnet_parse_reply(ni, msg);
1546 break;
1547 default: /* prevent an unused label if !kernel */
1548 LASSERT(0);
1549 return -EPROTO;
1550 }
1551
1552 LASSERT(!rc || rc == -ENOENT);
1553 return rc;
1554}
1555
d7e09d03 1556char *
af66a6e2 1557lnet_msgtyp2str(int type)
d7e09d03
PT
1558{
1559 switch (type) {
1560 case LNET_MSG_ACK:
2b5f2e44 1561 return "ACK";
d7e09d03 1562 case LNET_MSG_PUT:
2b5f2e44 1563 return "PUT";
d7e09d03 1564 case LNET_MSG_GET:
2b5f2e44 1565 return "GET";
d7e09d03 1566 case LNET_MSG_REPLY:
2b5f2e44 1567 return "REPLY";
d7e09d03 1568 case LNET_MSG_HELLO:
2b5f2e44 1569 return "HELLO";
d7e09d03 1570 default:
2b5f2e44 1571 return "<UNKNOWN>";
d7e09d03
PT
1572 }
1573}
d7e09d03
PT
1574
1575void
51f01fab 1576lnet_print_hdr(lnet_hdr_t *hdr)
d7e09d03
PT
1577{
1578 lnet_process_id_t src = {0};
1579 lnet_process_id_t dst = {0};
af66a6e2 1580 char *type_str = lnet_msgtyp2str(hdr->type);
d7e09d03
PT
1581
1582 src.nid = hdr->src_nid;
1583 src.pid = hdr->src_pid;
1584
1585 dst.nid = hdr->dest_nid;
1586 dst.pid = hdr->dest_pid;
1587
1588 CWARN("P3 Header at %p of type %s\n", hdr, type_str);
1589 CWARN(" From %s\n", libcfs_id2str(src));
1590 CWARN(" To %s\n", libcfs_id2str(dst));
1591
1592 switch (hdr->type) {
1593 default:
1594 break;
1595
1596 case LNET_MSG_PUT:
2d00bd17 1597 CWARN(" Ptl index %d, ack md %#llx.%#llx, match bits %llu\n",
d7e09d03
PT
1598 hdr->msg.put.ptl_index,
1599 hdr->msg.put.ack_wmd.wh_interface_cookie,
1600 hdr->msg.put.ack_wmd.wh_object_cookie,
1601 hdr->msg.put.match_bits);
55f5a824 1602 CWARN(" Length %d, offset %d, hdr data %#llx\n",
d7e09d03
PT
1603 hdr->payload_length, hdr->msg.put.offset,
1604 hdr->msg.put.hdr_data);
1605 break;
1606
1607 case LNET_MSG_GET:
2d00bd17
JP
1608 CWARN(" Ptl index %d, return md %#llx.%#llx, match bits %llu\n",
1609 hdr->msg.get.ptl_index,
d7e09d03
PT
1610 hdr->msg.get.return_wmd.wh_interface_cookie,
1611 hdr->msg.get.return_wmd.wh_object_cookie,
1612 hdr->msg.get.match_bits);
1613 CWARN(" Length %d, src offset %d\n",
1614 hdr->msg.get.sink_length,
1615 hdr->msg.get.src_offset);
1616 break;
1617
1618 case LNET_MSG_ACK:
2d00bd17 1619 CWARN(" dst md %#llx.%#llx, manipulated length %d\n",
d7e09d03
PT
1620 hdr->msg.ack.dst_wmd.wh_interface_cookie,
1621 hdr->msg.ack.dst_wmd.wh_object_cookie,
1622 hdr->msg.ack.mlength);
1623 break;
1624
1625 case LNET_MSG_REPLY:
2d00bd17 1626 CWARN(" dst md %#llx.%#llx, length %d\n",
d7e09d03
PT
1627 hdr->msg.reply.dst_wmd.wh_interface_cookie,
1628 hdr->msg.reply.dst_wmd.wh_object_cookie,
1629 hdr->payload_length);
1630 }
d7e09d03
PT
1631}
1632
1633int
1634lnet_parse(lnet_ni_t *ni, lnet_hdr_t *hdr, lnet_nid_t from_nid,
1635 void *private, int rdma_req)
1636{
7e7ab095
MS
1637 int rc = 0;
1638 int cpt;
1639 int for_me;
1640 struct lnet_msg *msg;
1641 lnet_pid_t dest_pid;
1642 lnet_nid_t dest_nid;
1643 lnet_nid_t src_nid;
1644 __u32 payload_length;
1645 __u32 type;
d7e09d03 1646
af66a6e2 1647 LASSERT(!in_interrupt());
d7e09d03
PT
1648
1649 type = le32_to_cpu(hdr->type);
1650 src_nid = le64_to_cpu(hdr->src_nid);
1651 dest_nid = le64_to_cpu(hdr->dest_nid);
1652 dest_pid = le32_to_cpu(hdr->dest_pid);
1653 payload_length = le32_to_cpu(hdr->payload_length);
1654
1655 for_me = (ni->ni_nid == dest_nid);
1656 cpt = lnet_cpt_of_nid(from_nid);
1657
1658 switch (type) {
1659 case LNET_MSG_ACK:
1660 case LNET_MSG_GET:
1661 if (payload_length > 0) {
1662 CERROR("%s, src %s: bad %s payload %d (0 expected)\n",
1663 libcfs_nid2str(from_nid),
1664 libcfs_nid2str(src_nid),
1665 lnet_msgtyp2str(type), payload_length);
1666 return -EPROTO;
1667 }
1668 break;
1669
1670 case LNET_MSG_PUT:
1671 case LNET_MSG_REPLY:
ae4003f0
LN
1672 if (payload_length >
1673 (__u32)(for_me ? LNET_MAX_PAYLOAD : LNET_MTU)) {
2d00bd17 1674 CERROR("%s, src %s: bad %s payload %d (%d max expected)\n",
d7e09d03
PT
1675 libcfs_nid2str(from_nid),
1676 libcfs_nid2str(src_nid),
1677 lnet_msgtyp2str(type),
1678 payload_length,
1679 for_me ? LNET_MAX_PAYLOAD : LNET_MTU);
1680 return -EPROTO;
1681 }
1682 break;
1683
1684 default:
1685 CERROR("%s, src %s: Bad message type 0x%x\n",
1686 libcfs_nid2str(from_nid),
1687 libcfs_nid2str(src_nid), type);
1688 return -EPROTO;
1689 }
1690
1691 if (the_lnet.ln_routing &&
ec0067d1 1692 ni->ni_last_alive != ktime_get_real_seconds()) {
d7e09d03 1693 /* NB: so far here is the only place to set NI status to "up */
86ef6250 1694 lnet_ni_lock(ni);
ec0067d1 1695 ni->ni_last_alive = ktime_get_real_seconds();
06ace26e 1696 if (ni->ni_status &&
d7e09d03
PT
1697 ni->ni_status->ns_status == LNET_NI_STATUS_DOWN)
1698 ni->ni_status->ns_status = LNET_NI_STATUS_UP;
1699 lnet_ni_unlock(ni);
1700 }
1701
4420cfd3
JS
1702 /*
1703 * Regard a bad destination NID as a protocol error. Senders should
d7e09d03 1704 * know what they're doing; if they don't they're misconfigured, buggy
4420cfd3
JS
1705 * or malicious so we chop them off at the knees :)
1706 */
d7e09d03
PT
1707 if (!for_me) {
1708 if (LNET_NIDNET(dest_nid) == LNET_NIDNET(ni->ni_nid)) {
1709 /* should have gone direct */
2d00bd17
JP
1710 CERROR("%s, src %s: Bad dest nid %s (should have been sent direct)\n",
1711 libcfs_nid2str(from_nid),
1712 libcfs_nid2str(src_nid),
1713 libcfs_nid2str(dest_nid));
d7e09d03
PT
1714 return -EPROTO;
1715 }
1716
1717 if (lnet_islocalnid(dest_nid)) {
4420cfd3
JS
1718 /*
1719 * dest is another local NI; sender should have used
1720 * this node's NID on its own network
1721 */
2d00bd17
JP
1722 CERROR("%s, src %s: Bad dest nid %s (it's my nid but on a different network)\n",
1723 libcfs_nid2str(from_nid),
1724 libcfs_nid2str(src_nid),
1725 libcfs_nid2str(dest_nid));
d7e09d03
PT
1726 return -EPROTO;
1727 }
1728
1729 if (rdma_req && type == LNET_MSG_GET) {
2d00bd17
JP
1730 CERROR("%s, src %s: Bad optimized GET for %s (final destination must be me)\n",
1731 libcfs_nid2str(from_nid),
1732 libcfs_nid2str(src_nid),
1733 libcfs_nid2str(dest_nid));
d7e09d03
PT
1734 return -EPROTO;
1735 }
1736
1737 if (!the_lnet.ln_routing) {
2d00bd17
JP
1738 CERROR("%s, src %s: Dropping message for %s (routing not enabled)\n",
1739 libcfs_nid2str(from_nid),
1740 libcfs_nid2str(src_nid),
1741 libcfs_nid2str(dest_nid));
d7e09d03
PT
1742 goto drop;
1743 }
1744 }
1745
4420cfd3
JS
1746 /*
1747 * Message looks OK; we're not going to return an error, so we MUST
1748 * call back lnd_recv() come what may...
1749 */
af66a6e2 1750 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
9b79ca85 1751 fail_peer(src_nid, 0)) { /* shall we now? */
d7e09d03
PT
1752 CERROR("%s, src %s: Dropping %s to simulate failure\n",
1753 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
1754 lnet_msgtyp2str(type));
1755 goto drop;
1756 }
1757
0fbbced2
LZ
1758 if (!list_empty(&the_lnet.ln_drop_rules) &&
1759 lnet_drop_rule_match(hdr)) {
1760 CDEBUG(D_NET, "%s, src %s, dst %s: Dropping %s to simulate silent message loss\n",
1761 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
1762 libcfs_nid2str(dest_nid), lnet_msgtyp2str(type));
1763 goto drop;
1764 }
1765
d7e09d03 1766 msg = lnet_msg_alloc();
06ace26e 1767 if (!msg) {
d7e09d03
PT
1768 CERROR("%s, src %s: Dropping %s (out of memory)\n",
1769 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
1770 lnet_msgtyp2str(type));
1771 goto drop;
1772 }
1773
ae4003f0
LN
1774 /* msg zeroed in lnet_msg_alloc;
1775 * i.e. flags all clear, pointers NULL etc
1776 */
d7e09d03
PT
1777 msg->msg_type = type;
1778 msg->msg_private = private;
1779 msg->msg_receiving = 1;
b7acfc95 1780 msg->msg_rdma_get = rdma_req;
d3d3d37a
JS
1781 msg->msg_wanted = payload_length;
1782 msg->msg_len = payload_length;
d7e09d03
PT
1783 msg->msg_offset = 0;
1784 msg->msg_hdr = *hdr;
1785 /* for building message event */
1786 msg->msg_from = from_nid;
1787 if (!for_me) {
1788 msg->msg_target.pid = dest_pid;
1789 msg->msg_target.nid = dest_nid;
1790 msg->msg_routing = 1;
1791
1792 } else {
1793 /* convert common msg->hdr fields to host byteorder */
1794 msg->msg_hdr.type = type;
1795 msg->msg_hdr.src_nid = src_nid;
1796 msg->msg_hdr.src_pid = le32_to_cpu(msg->msg_hdr.src_pid);
1797 msg->msg_hdr.dest_nid = dest_nid;
1798 msg->msg_hdr.dest_pid = dest_pid;
1799 msg->msg_hdr.payload_length = payload_length;
1800 }
1801
1802 lnet_net_lock(cpt);
1803 rc = lnet_nid2peer_locked(&msg->msg_rxpeer, from_nid, cpt);
5fd88337 1804 if (rc) {
d7e09d03 1805 lnet_net_unlock(cpt);
2d00bd17 1806 CERROR("%s, src %s: Dropping %s (error %d looking up sender)\n",
d7e09d03
PT
1807 libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
1808 lnet_msgtyp2str(type), rc);
1809 lnet_msg_free(msg);
e426f0d2
DO
1810 if (rc == -ESHUTDOWN)
1811 /* We are shutting down. Don't do anything more */
1812 return 0;
d7e09d03
PT
1813 goto drop;
1814 }
1815
af3fa7c7
LZ
1816 if (lnet_isrouter(msg->msg_rxpeer)) {
1817 lnet_peer_set_alive(msg->msg_rxpeer);
1818 if (avoid_asym_router_failure &&
1819 LNET_NIDNET(src_nid) != LNET_NIDNET(from_nid)) {
1820 /* received a remote message from router, update
1821 * remote NI status on this router.
1822 * NB: multi-hop routed message will be ignored.
1823 */
1824 lnet_router_ni_update_locked(msg->msg_rxpeer,
1825 LNET_NIDNET(src_nid));
1826 }
1827 }
1828
d7e09d03
PT
1829 lnet_msg_commit(msg, cpt);
1830
b7acfc95
LZ
1831 /* message delay simulation */
1832 if (unlikely(!list_empty(&the_lnet.ln_delay_rules) &&
1833 lnet_delay_rule_match_locked(hdr, msg))) {
1834 lnet_net_unlock(cpt);
1835 return 0;
1836 }
1837
d7e09d03
PT
1838 if (!for_me) {
1839 rc = lnet_parse_forward_locked(ni, msg);
1840 lnet_net_unlock(cpt);
1841
1842 if (rc < 0)
1843 goto free_drop;
ec5fb5be
LZ
1844
1845 if (rc == LNET_CREDIT_OK) {
d7e09d03
PT
1846 lnet_ni_recv(ni, msg->msg_private, msg, 0,
1847 0, payload_length, payload_length);
1848 }
1849 return 0;
1850 }
1851
1852 lnet_net_unlock(cpt);
1853
b7acfc95
LZ
1854 rc = lnet_parse_local(ni, msg);
1855 if (rc)
1856 goto free_drop;
1857 return 0;
d7e09d03
PT
1858
1859 free_drop:
06ace26e 1860 LASSERT(!msg->msg_md);
d7e09d03
PT
1861 lnet_finalize(ni, msg, rc);
1862
1863 drop:
1864 lnet_drop_message(ni, cpt, private, payload_length);
1865 return 0;
1866}
1867EXPORT_SYMBOL(lnet_parse);
1868
1869void
1870lnet_drop_delayed_msg_list(struct list_head *head, char *reason)
1871{
1872 while (!list_empty(head)) {
7e7ab095
MS
1873 lnet_process_id_t id = {0};
1874 lnet_msg_t *msg;
d7e09d03
PT
1875
1876 msg = list_entry(head->next, lnet_msg_t, msg_list);
1877 list_del(&msg->msg_list);
1878
1879 id.nid = msg->msg_hdr.src_nid;
1880 id.pid = msg->msg_hdr.src_pid;
1881
06ace26e 1882 LASSERT(!msg->msg_md);
d7e09d03 1883 LASSERT(msg->msg_rx_delayed);
06ace26e 1884 LASSERT(msg->msg_rxpeer);
d7e09d03
PT
1885 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
1886
b0f5aad5 1887 CWARN("Dropping delayed PUT from %s portal %d match %llu offset %d length %d: %s\n",
d7e09d03
PT
1888 libcfs_id2str(id),
1889 msg->msg_hdr.msg.put.ptl_index,
1890 msg->msg_hdr.msg.put.match_bits,
1891 msg->msg_hdr.msg.put.offset,
1892 msg->msg_hdr.payload_length, reason);
1893
4420cfd3
JS
1894 /*
1895 * NB I can't drop msg's ref on msg_rxpeer until after I've
d7e09d03 1896 * called lnet_drop_message(), so I just hang onto msg as well
4420cfd3
JS
1897 * until that's done
1898 */
d7e09d03
PT
1899 lnet_drop_message(msg->msg_rxpeer->lp_ni,
1900 msg->msg_rxpeer->lp_cpt,
1901 msg->msg_private, msg->msg_len);
1902 /*
1903 * NB: message will not generate event because w/o attached MD,
1904 * but we still should give error code so lnet_msg_decommit()
1905 * can skip counters operations and other checks.
1906 */
1907 lnet_finalize(msg->msg_rxpeer->lp_ni, msg, -ENOENT);
1908 }
1909}
1910
1911void
1912lnet_recv_delayed_msg_list(struct list_head *head)
1913{
1914 while (!list_empty(head)) {
7e7ab095
MS
1915 lnet_msg_t *msg;
1916 lnet_process_id_t id;
d7e09d03
PT
1917
1918 msg = list_entry(head->next, lnet_msg_t, msg_list);
1919 list_del(&msg->msg_list);
1920
4420cfd3
JS
1921 /*
1922 * md won't disappear under me, since each msg
1923 * holds a ref on it
1924 */
d7e09d03
PT
1925 id.nid = msg->msg_hdr.src_nid;
1926 id.pid = msg->msg_hdr.src_pid;
1927
1928 LASSERT(msg->msg_rx_delayed);
06ace26e
JS
1929 LASSERT(msg->msg_md);
1930 LASSERT(msg->msg_rxpeer);
d7e09d03
PT
1931 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
1932
2d00bd17
JP
1933 CDEBUG(D_NET, "Resuming delayed PUT from %s portal %d match %llu offset %d length %d.\n",
1934 libcfs_id2str(id), msg->msg_hdr.msg.put.ptl_index,
1935 msg->msg_hdr.msg.put.match_bits,
1936 msg->msg_hdr.msg.put.offset,
1937 msg->msg_hdr.payload_length);
d7e09d03
PT
1938
1939 lnet_recv_put(msg->msg_rxpeer->lp_ni, msg);
1940 }
1941}
1942
1943/**
1944 * Initiate an asynchronous PUT operation.
1945 *
1946 * There are several events associated with a PUT: completion of the send on
1947 * the initiator node (LNET_EVENT_SEND), and when the send completes
1948 * successfully, the receipt of an acknowledgment (LNET_EVENT_ACK) indicating
1949 * that the operation was accepted by the target. The event LNET_EVENT_PUT is
1950 * used at the target node to indicate the completion of incoming data
1951 * delivery.
1952 *
1953 * The local events will be logged in the EQ associated with the MD pointed to
1954 * by \a mdh handle. Using a MD without an associated EQ results in these
1955 * events being discarded. In this case, the caller must have another
1956 * mechanism (e.g., a higher level protocol) for determining when it is safe
1957 * to modify the memory region associated with the MD.
1958 *
1959 * Note that LNet does not guarantee the order of LNET_EVENT_SEND and
1960 * LNET_EVENT_ACK, though intuitively ACK should happen after SEND.
1961 *
1962 * \param self Indicates the NID of a local interface through which to send
1963 * the PUT request. Use LNET_NID_ANY to let LNet choose one by itself.
1964 * \param mdh A handle for the MD that describes the memory to be sent. The MD
1965 * must be "free floating" (See LNetMDBind()).
1966 * \param ack Controls whether an acknowledgment is requested.
1967 * Acknowledgments are only sent when they are requested by the initiating
1968 * process and the target MD enables them.
1969 * \param target A process identifier for the target process.
1970 * \param portal The index in the \a target's portal table.
1971 * \param match_bits The match bits to use for MD selection at the target
1972 * process.
1973 * \param offset The offset into the target MD (only used when the target
1974 * MD has the LNET_MD_MANAGE_REMOTE option set).
1975 * \param hdr_data 64 bits of user data that can be included in the message
1976 * header. This data is written to an event queue entry at the target if an
1977 * EQ is present on the matching MD.
1978 *
1979 * \retval 0 Success, and only in this case events will be generated
1980 * and logged to EQ (if it exists).
1981 * \retval -EIO Simulated failure.
1982 * \retval -ENOMEM Memory allocation failure.
1983 * \retval -ENOENT Invalid MD object.
1984 *
1985 * \see lnet_event_t::hdr_data and lnet_event_kind_t.
1986 */
1987int
1988LNetPut(lnet_nid_t self, lnet_handle_md_t mdh, lnet_ack_req_t ack,
1989 lnet_process_id_t target, unsigned int portal,
1990 __u64 match_bits, unsigned int offset,
1991 __u64 hdr_data)
1992{
7e7ab095
MS
1993 struct lnet_msg *msg;
1994 struct lnet_libmd *md;
1995 int cpt;
1996 int rc;
d7e09d03 1997
af66a6e2 1998 LASSERT(the_lnet.ln_refcount > 0);
d7e09d03 1999
af66a6e2 2000 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
9b79ca85 2001 fail_peer(target.nid, 1)) { /* shall we now? */
d7e09d03
PT
2002 CERROR("Dropping PUT to %s: simulated failure\n",
2003 libcfs_id2str(target));
2004 return -EIO;
2005 }
2006
2007 msg = lnet_msg_alloc();
06ace26e 2008 if (!msg) {
d7e09d03
PT
2009 CERROR("Dropping PUT to %s: ENOMEM on lnet_msg_t\n",
2010 libcfs_id2str(target));
2011 return -ENOMEM;
2012 }
2013 msg->msg_vmflush = !!memory_pressure_get();
2014
2015 cpt = lnet_cpt_of_cookie(mdh.cookie);
2016 lnet_res_lock(cpt);
2017
2018 md = lnet_handle2md(&mdh);
5fd88337 2019 if (!md || !md->md_threshold || md->md_me) {
b0f5aad5 2020 CERROR("Dropping PUT (%llu:%d:%s): MD (%d) invalid\n",
d7e09d03 2021 match_bits, portal, libcfs_id2str(target),
06ace26e
JS
2022 !md ? -1 : md->md_threshold);
2023 if (md && md->md_me)
d7e09d03
PT
2024 CERROR("Source MD also attached to portal %d\n",
2025 md->md_me->me_portal);
2026 lnet_res_unlock(cpt);
2027
2028 lnet_msg_free(msg);
2029 return -ENOENT;
2030 }
2031
2032 CDEBUG(D_NET, "LNetPut -> %s\n", libcfs_id2str(target));
2033
2034 lnet_msg_attach_md(msg, md, 0, 0);
2035
2036 lnet_prep_send(msg, LNET_MSG_PUT, target, 0, md->md_length);
2037
2038 msg->msg_hdr.msg.put.match_bits = cpu_to_le64(match_bits);
2039 msg->msg_hdr.msg.put.ptl_index = cpu_to_le32(portal);
2040 msg->msg_hdr.msg.put.offset = cpu_to_le32(offset);
2041 msg->msg_hdr.msg.put.hdr_data = hdr_data;
2042
2043 /* NB handles only looked up by creator (no flips) */
2044 if (ack == LNET_ACK_REQ) {
2045 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
2046 the_lnet.ln_interface_cookie;
2047 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
2048 md->md_lh.lh_cookie;
2049 } else {
2050 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie =
2051 LNET_WIRE_HANDLE_COOKIE_NONE;
2052 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie =
2053 LNET_WIRE_HANDLE_COOKIE_NONE;
2054 }
2055
2056 lnet_res_unlock(cpt);
2057
2058 lnet_build_msg_event(msg, LNET_EVENT_SEND);
2059
2060 rc = lnet_send(self, msg, LNET_NID_ANY);
5fd88337 2061 if (rc) {
af66a6e2 2062 CNETERR("Error sending PUT to %s: %d\n",
c314c319 2063 libcfs_id2str(target), rc);
af66a6e2 2064 lnet_finalize(NULL, msg, rc);
d7e09d03
PT
2065 }
2066
2067 /* completion will be signalled by an event */
2068 return 0;
2069}
2070EXPORT_SYMBOL(LNetPut);
2071
2072lnet_msg_t *
af66a6e2 2073lnet_create_reply_msg(lnet_ni_t *ni, lnet_msg_t *getmsg)
d7e09d03 2074{
4420cfd3
JS
2075 /*
2076 * The LND can DMA direct to the GET md (i.e. no REPLY msg). This
d7e09d03
PT
2077 * returns a msg for the LND to pass to lnet_finalize() when the sink
2078 * data has been received.
2079 *
2080 * CAVEAT EMPTOR: 'getmsg' is the original GET, which is freed when
4420cfd3
JS
2081 * lnet_finalize() is called on it, so the LND must call this first
2082 */
7e7ab095
MS
2083 struct lnet_msg *msg = lnet_msg_alloc();
2084 struct lnet_libmd *getmd = getmsg->msg_md;
2085 lnet_process_id_t peer_id = getmsg->msg_target;
2086 int cpt;
d7e09d03
PT
2087
2088 LASSERT(!getmsg->msg_target_is_router);
2089 LASSERT(!getmsg->msg_routing);
2090
06ace26e 2091 if (!msg) {
af66a6e2 2092 CERROR("%s: Dropping REPLY from %s: can't allocate msg\n",
c314c319 2093 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id));
d7e09d03
PT
2094 goto drop;
2095 }
2096
600e9b49
LZ
2097 cpt = lnet_cpt_of_cookie(getmd->md_lh.lh_cookie);
2098 lnet_res_lock(cpt);
2099
2100 LASSERT(getmd->md_refcount > 0);
2101
5fd88337 2102 if (!getmd->md_threshold) {
af66a6e2 2103 CERROR("%s: Dropping REPLY from %s for inactive MD %p\n",
c314c319
JS
2104 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id),
2105 getmd);
d7e09d03
PT
2106 lnet_res_unlock(cpt);
2107 goto drop;
2108 }
2109
5fd88337 2110 LASSERT(!getmd->md_offset);
d7e09d03
PT
2111
2112 CDEBUG(D_NET, "%s: Reply from %s md %p\n",
2113 libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id), getmd);
2114
2115 /* setup information for lnet_build_msg_event */
2116 msg->msg_from = peer_id.nid;
2117 msg->msg_type = LNET_MSG_GET; /* flag this msg as an "optimized" GET */
2118 msg->msg_hdr.src_nid = peer_id.nid;
2119 msg->msg_hdr.payload_length = getmd->md_length;
2120 msg->msg_receiving = 1; /* required by lnet_msg_attach_md */
2121
2122 lnet_msg_attach_md(msg, getmd, getmd->md_offset, getmd->md_length);
2123 lnet_res_unlock(cpt);
2124
2125 cpt = lnet_cpt_of_nid(peer_id.nid);
2126
2127 lnet_net_lock(cpt);
2128 lnet_msg_commit(msg, cpt);
2129 lnet_net_unlock(cpt);
2130
2131 lnet_build_msg_event(msg, LNET_EVENT_REPLY);
2132
2133 return msg;
2134
2135 drop:
2136 cpt = lnet_cpt_of_nid(peer_id.nid);
2137
2138 lnet_net_lock(cpt);
2139 the_lnet.ln_counters[cpt]->drop_count++;
2140 the_lnet.ln_counters[cpt]->drop_length += getmd->md_length;
2141 lnet_net_unlock(cpt);
2142
06ace26e 2143 if (msg)
d7e09d03
PT
2144 lnet_msg_free(msg);
2145
2146 return NULL;
2147}
2148EXPORT_SYMBOL(lnet_create_reply_msg);
2149
2150void
2151lnet_set_reply_msg_len(lnet_ni_t *ni, lnet_msg_t *reply, unsigned int len)
2152{
4420cfd3
JS
2153 /*
2154 * Set the REPLY length, now the RDMA that elides the REPLY message has
2155 * completed and I know it.
2156 */
06ace26e 2157 LASSERT(reply);
af66a6e2
LN
2158 LASSERT(reply->msg_type == LNET_MSG_GET);
2159 LASSERT(reply->msg_ev.type == LNET_EVENT_REPLY);
d7e09d03 2160
4420cfd3
JS
2161 /*
2162 * NB I trusted my peer to RDMA. If she tells me she's written beyond
2163 * the end of my buffer, I might as well be dead.
2164 */
af66a6e2 2165 LASSERT(len <= reply->msg_ev.mlength);
d7e09d03
PT
2166
2167 reply->msg_ev.mlength = len;
2168}
2169EXPORT_SYMBOL(lnet_set_reply_msg_len);
2170
2171/**
2172 * Initiate an asynchronous GET operation.
2173 *
2174 * On the initiator node, an LNET_EVENT_SEND is logged when the GET request
2175 * is sent, and an LNET_EVENT_REPLY is logged when the data returned from
2176 * the target node in the REPLY has been written to local MD.
2177 *
2178 * On the target node, an LNET_EVENT_GET is logged when the GET request
2179 * arrives and is accepted into a MD.
2180 *
2181 * \param self,target,portal,match_bits,offset See the discussion in LNetPut().
2182 * \param mdh A handle for the MD that describes the memory into which the
ae4003f0
LN
2183 * requested data will be received. The MD must be "free floating"
2184 * (See LNetMDBind()).
d7e09d03
PT
2185 *
2186 * \retval 0 Success, and only in this case events will be generated
2187 * and logged to EQ (if it exists) of the MD.
2188 * \retval -EIO Simulated failure.
2189 * \retval -ENOMEM Memory allocation failure.
2190 * \retval -ENOENT Invalid MD object.
2191 */
2192int
2193LNetGet(lnet_nid_t self, lnet_handle_md_t mdh,
2194 lnet_process_id_t target, unsigned int portal,
2195 __u64 match_bits, unsigned int offset)
2196{
7e7ab095
MS
2197 struct lnet_msg *msg;
2198 struct lnet_libmd *md;
2199 int cpt;
2200 int rc;
d7e09d03 2201
af66a6e2 2202 LASSERT(the_lnet.ln_refcount > 0);
d7e09d03 2203
af66a6e2 2204 if (!list_empty(&the_lnet.ln_test_peers) && /* normally we don't */
9b79ca85 2205 fail_peer(target.nid, 1)) { /* shall we now? */
d7e09d03
PT
2206 CERROR("Dropping GET to %s: simulated failure\n",
2207 libcfs_id2str(target));
2208 return -EIO;
2209 }
2210
2211 msg = lnet_msg_alloc();
06ace26e 2212 if (!msg) {
d7e09d03
PT
2213 CERROR("Dropping GET to %s: ENOMEM on lnet_msg_t\n",
2214 libcfs_id2str(target));
2215 return -ENOMEM;
2216 }
2217
2218 cpt = lnet_cpt_of_cookie(mdh.cookie);
2219 lnet_res_lock(cpt);
2220
2221 md = lnet_handle2md(&mdh);
5fd88337 2222 if (!md || !md->md_threshold || md->md_me) {
b0f5aad5 2223 CERROR("Dropping GET (%llu:%d:%s): MD (%d) invalid\n",
d7e09d03 2224 match_bits, portal, libcfs_id2str(target),
06ace26e
JS
2225 !md ? -1 : md->md_threshold);
2226 if (md && md->md_me)
d7e09d03
PT
2227 CERROR("REPLY MD also attached to portal %d\n",
2228 md->md_me->me_portal);
2229
2230 lnet_res_unlock(cpt);
2231
2232 lnet_msg_free(msg);
d7e09d03
PT
2233 return -ENOENT;
2234 }
2235
2236 CDEBUG(D_NET, "LNetGet -> %s\n", libcfs_id2str(target));
2237
2238 lnet_msg_attach_md(msg, md, 0, 0);
2239
2240 lnet_prep_send(msg, LNET_MSG_GET, target, 0, 0);
2241
2242 msg->msg_hdr.msg.get.match_bits = cpu_to_le64(match_bits);
2243 msg->msg_hdr.msg.get.ptl_index = cpu_to_le32(portal);
2244 msg->msg_hdr.msg.get.src_offset = cpu_to_le32(offset);
2245 msg->msg_hdr.msg.get.sink_length = cpu_to_le32(md->md_length);
2246
2247 /* NB handles only looked up by creator (no flips) */
2248 msg->msg_hdr.msg.get.return_wmd.wh_interface_cookie =
2249 the_lnet.ln_interface_cookie;
2250 msg->msg_hdr.msg.get.return_wmd.wh_object_cookie =
2251 md->md_lh.lh_cookie;
2252
2253 lnet_res_unlock(cpt);
2254
2255 lnet_build_msg_event(msg, LNET_EVENT_SEND);
2256
2257 rc = lnet_send(self, msg, LNET_NID_ANY);
2258 if (rc < 0) {
af66a6e2 2259 CNETERR("Error sending GET to %s: %d\n",
c314c319 2260 libcfs_id2str(target), rc);
af66a6e2 2261 lnet_finalize(NULL, msg, rc);
d7e09d03
PT
2262 }
2263
2264 /* completion will be signalled by an event */
2265 return 0;
2266}
2267EXPORT_SYMBOL(LNetGet);
2268
2269/**
2270 * Calculate distance to node at \a dstnid.
2271 *
2272 * \param dstnid Target NID.
2273 * \param srcnidp If not NULL, NID of the local interface to reach \a dstnid
2274 * is saved here.
2275 * \param orderp If not NULL, order of the route to reach \a dstnid is saved
2276 * here.
2277 *
2278 * \retval 0 If \a dstnid belongs to a local interface, and reserved option
2279 * local_nid_dist_zero is set, which is the default.
2280 * \retval positives Distance to target NID, i.e. number of hops plus one.
2281 * \retval -EHOSTUNREACH If \a dstnid is not reachable.
2282 */
2283int
2284LNetDist(lnet_nid_t dstnid, lnet_nid_t *srcnidp, __u32 *orderp)
2285{
7e7ab095
MS
2286 struct list_head *e;
2287 struct lnet_ni *ni;
2288 lnet_remotenet_t *rnet;
2289 __u32 dstnet = LNET_NIDNET(dstnid);
2290 int hops;
2291 int cpt;
2292 __u32 order = 2;
2293 struct list_head *rn_list;
d7e09d03 2294
4420cfd3
JS
2295 /*
2296 * if !local_nid_dist_zero, I don't return a distance of 0 ever
d7e09d03
PT
2297 * (when lustre sees a distance of 0, it substitutes 0@lo), so I
2298 * keep order 0 free for 0@lo and order 1 free for a local NID
4420cfd3
JS
2299 * match
2300 */
af66a6e2 2301 LASSERT(the_lnet.ln_refcount > 0);
d7e09d03
PT
2302
2303 cpt = lnet_net_lock_current();
2304
af66a6e2 2305 list_for_each(e, &the_lnet.ln_nis) {
d7e09d03
PT
2306 ni = list_entry(e, lnet_ni_t, ni_list);
2307
2308 if (ni->ni_nid == dstnid) {
06ace26e 2309 if (srcnidp)
d7e09d03 2310 *srcnidp = dstnid;
06ace26e 2311 if (orderp) {
d7e09d03
PT
2312 if (LNET_NETTYP(LNET_NIDNET(dstnid)) == LOLND)
2313 *orderp = 0;
2314 else
2315 *orderp = 1;
2316 }
2317 lnet_net_unlock(cpt);
2318
2319 return local_nid_dist_zero ? 0 : 1;
2320 }
2321
2322 if (LNET_NIDNET(ni->ni_nid) == dstnet) {
06ace26e 2323 if (srcnidp)
d7e09d03 2324 *srcnidp = ni->ni_nid;
06ace26e 2325 if (orderp)
d7e09d03
PT
2326 *orderp = order;
2327 lnet_net_unlock(cpt);
2328 return 1;
2329 }
2330
2331 order++;
2332 }
2333
2334 rn_list = lnet_net2rnethash(dstnet);
2335 list_for_each(e, rn_list) {
2336 rnet = list_entry(e, lnet_remotenet_t, lrn_list);
2337
2338 if (rnet->lrn_net == dstnet) {
2339 lnet_route_t *route;
2340 lnet_route_t *shortest = NULL;
b9bbb61c
AS
2341 __u32 shortest_hops = LNET_UNDEFINED_HOPS;
2342 __u32 route_hops;
d7e09d03 2343
af66a6e2 2344 LASSERT(!list_empty(&rnet->lrn_routes));
d7e09d03
PT
2345
2346 list_for_each_entry(route, &rnet->lrn_routes,
c314c319 2347 lr_list) {
b9bbb61c
AS
2348 route_hops = route->lr_hops;
2349 if (route_hops == LNET_UNDEFINED_HOPS)
2350 route_hops = 1;
06ace26e 2351 if (!shortest ||
b9bbb61c 2352 route_hops < shortest_hops) {
d7e09d03 2353 shortest = route;
b9bbb61c
AS
2354 shortest_hops = route_hops;
2355 }
d7e09d03
PT
2356 }
2357
06ace26e 2358 LASSERT(shortest);
b9bbb61c 2359 hops = shortest_hops;
06ace26e 2360 if (srcnidp)
d7e09d03 2361 *srcnidp = shortest->lr_gateway->lp_ni->ni_nid;
06ace26e 2362 if (orderp)
d7e09d03
PT
2363 *orderp = order;
2364 lnet_net_unlock(cpt);
2365 return hops + 1;
2366 }
2367 order++;
2368 }
2369
2370 lnet_net_unlock(cpt);
2371 return -EHOSTUNREACH;
2372}
2373EXPORT_SYMBOL(LNetDist);
This page took 0.6111 seconds and 5 git commands to generate.