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