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