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