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