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