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