Whamcloud - gitweb
- removed used function lnet_nid_alive_locked.
[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 /* NB: caller shall hold a ref on 'lp' as I'd drop LNET_LOCK */
903 void
904 lnet_ni_peer_alive(lnet_peer_t *lp)
905 {
906         time_t      last_alive = 0;
907         lnet_ni_t  *ni = lp->lp_ni;
908
909         LASSERT (ni != NULL);
910         LASSERT (ni->ni_peertimeout > 0);
911         LASSERT (ni->ni_lnd->lnd_query != NULL);
912
913         LNET_UNLOCK();
914         (ni->ni_lnd->lnd_query)(ni, lp->lp_nid, &last_alive);
915         LNET_LOCK();
916
917         if (last_alive != 0) /* NI has updated timestamp */
918                 lp->lp_last_alive = last_alive;
919         return;
920 }
921
922 /* NB: always called with LNET_LOCK held */
923 static inline int
924 lnet_peer_is_alive (lnet_peer_t *lp, time_t now)
925 {
926         lnet_ni_t  *ni = lp->lp_ni;
927         time_t      deadline;
928         int         alive;
929
930         LASSERT (ni != NULL);
931         LASSERT (ni->ni_peertimeout > 0);
932
933         if (!lp->lp_alive && lp->lp_alive_count > 0 &&
934             cfs_time_aftereq(lp->lp_timestamp, lp->lp_last_alive))
935                         return 0;
936
937         deadline = cfs_time_add(lp->lp_last_alive, ni->ni_peertimeout);
938         alive = cfs_time_after(deadline, now);
939         if (alive && !lp->lp_alive) /* update obsolete lp_alive */
940                 lnet_notify_locked(lp, 0, 1, lp->lp_last_alive);
941
942         return alive;
943 }
944
945 /* NB: returns 1 when alive, 0 when dead, negative when error;
946  *     may drop the LNET_LOCK */
947 int
948 lnet_peer_alive_locked (lnet_peer_t *lp)
949 {
950         lnet_ni_t  *ni = lp->lp_ni;
951         time_t      now = cfs_time_current_sec();
952
953         LASSERT (ni != NULL);
954
955         if (ni->ni_peertimeout <= 0)  /* disabled */
956                 return -ENODEV;
957
958         if (lnet_peer_is_alive(lp, now))
959                 return 1;
960
961         /* peer appears dead, query LND for latest aliveness news */
962         lnet_ni_peer_alive(lp);
963
964         if (lnet_peer_is_alive(lp, now))
965                 return 1;
966
967         lnet_notify_locked(lp, 0, 0, lp->lp_last_alive);
968         return 0;
969 }
970
971 int
972 lnet_post_send_locked (lnet_msg_t *msg, int do_send)
973 {
974         /* lnet_send is going to LNET_UNLOCK immediately after this, so it sets
975          * do_send FALSE and I don't do the unlock/send/lock bit.  I return
976          * EAGAIN if msg blocked, EHOSTUNREACH if msg_txpeer appears dead, and
977          * 0 if sent or OK to send */
978         lnet_peer_t *lp = msg->msg_txpeer;
979         lnet_ni_t   *ni = lp->lp_ni;
980
981         /* non-lnet_send() callers have checked before */
982         LASSERT (!do_send || msg->msg_delayed);
983         LASSERT (!msg->msg_receiving);
984
985         /* NB 'lp' is always the next hop */
986         if ((msg->msg_target.pid & LNET_PID_USERFLAG) == 0 &&
987             lnet_peer_alive_locked(lp) == 0) {
988                 LNET_UNLOCK();
989
990                 CDEBUG(D_NETERROR, "Dropping message for %s: peer not alive\n",
991                        libcfs_id2str(msg->msg_target));
992                 if (do_send)
993                         lnet_finalize(ni, msg, -EHOSTUNREACH);
994
995                 LNET_LOCK();
996                 return EHOSTUNREACH;
997         }
998
999         if (!msg->msg_peertxcredit) {
1000                 LASSERT ((lp->lp_txcredits < 0) == !list_empty(&lp->lp_txq));
1001
1002                 msg->msg_peertxcredit = 1;
1003                 lp->lp_txqnob += msg->msg_len + sizeof(lnet_hdr_t);
1004                 lp->lp_txcredits--;
1005
1006                 if (lp->lp_txcredits < lp->lp_mintxcredits)
1007                         lp->lp_mintxcredits = lp->lp_txcredits;
1008
1009                 if (lp->lp_txcredits < 0) {
1010                         msg->msg_delayed = 1;
1011                         list_add_tail (&msg->msg_list, &lp->lp_txq);
1012                         return EAGAIN;
1013                 }
1014         }
1015
1016         if (!msg->msg_txcredit) {
1017                 LASSERT ((ni->ni_txcredits < 0) == !list_empty(&ni->ni_txq));
1018
1019                 msg->msg_txcredit = 1;
1020                 ni->ni_txcredits--;
1021
1022                 if (ni->ni_txcredits < ni->ni_mintxcredits)
1023                         ni->ni_mintxcredits = ni->ni_txcredits;
1024
1025                 if (ni->ni_txcredits < 0) {
1026                         msg->msg_delayed = 1;
1027                         list_add_tail (&msg->msg_list, &ni->ni_txq);
1028                         return EAGAIN;
1029                 }
1030         }
1031
1032         if (do_send) {
1033                 LNET_UNLOCK();
1034                 lnet_ni_send(ni, msg);
1035                 LNET_LOCK();
1036         }
1037         return 0;
1038 }
1039
1040 #ifdef __KERNEL__
1041 static void
1042 lnet_commit_routedmsg (lnet_msg_t *msg)
1043 {
1044         /* ALWAYS called holding the LNET_LOCK */
1045         LASSERT (msg->msg_routing);
1046
1047         the_lnet.ln_counters.msgs_alloc++;
1048         if (the_lnet.ln_counters.msgs_alloc >
1049             the_lnet.ln_counters.msgs_max)
1050                 the_lnet.ln_counters.msgs_max =
1051                         the_lnet.ln_counters.msgs_alloc;
1052
1053         the_lnet.ln_counters.route_count++;
1054         the_lnet.ln_counters.route_length += msg->msg_len;
1055
1056         LASSERT (!msg->msg_onactivelist);
1057         msg->msg_onactivelist = 1;
1058         list_add (&msg->msg_activelist, &the_lnet.ln_active_msgs);
1059 }
1060
1061 lnet_rtrbufpool_t *
1062 lnet_msg2bufpool(lnet_msg_t *msg)
1063 {
1064         lnet_rtrbufpool_t *rbp = &the_lnet.ln_rtrpools[0];
1065
1066         LASSERT (msg->msg_len <= LNET_MTU);
1067         while (msg->msg_len > (unsigned int)rbp->rbp_npages * CFS_PAGE_SIZE) {
1068                 rbp++;
1069                 LASSERT (rbp < &the_lnet.ln_rtrpools[LNET_NRBPOOLS]);
1070         }
1071
1072         return rbp;
1073 }
1074
1075 int
1076 lnet_post_routed_recv_locked (lnet_msg_t *msg, int do_recv)
1077 {
1078         /* lnet_parse is going to LNET_UNLOCK immediately after this, so it
1079          * sets do_recv FALSE and I don't do the unlock/send/lock bit.  I
1080          * return EAGAIN if msg blocked and 0 if received or OK to receive */
1081         lnet_peer_t         *lp = msg->msg_rxpeer;
1082         lnet_rtrbufpool_t   *rbp;
1083         lnet_rtrbuf_t       *rb;
1084
1085         LASSERT (msg->msg_iov == NULL);
1086         LASSERT (msg->msg_kiov == NULL);
1087         LASSERT (msg->msg_niov == 0);
1088         LASSERT (msg->msg_routing);
1089         LASSERT (msg->msg_receiving);
1090         LASSERT (!msg->msg_sending);
1091
1092         /* non-lnet_parse callers only send delayed messages */
1093         LASSERT (!do_recv || msg->msg_delayed);
1094
1095         if (!msg->msg_peerrtrcredit) {
1096                 LASSERT ((lp->lp_rtrcredits < 0) == !list_empty(&lp->lp_rtrq));
1097
1098                 msg->msg_peerrtrcredit = 1;
1099                 lp->lp_rtrcredits--;
1100                 if (lp->lp_rtrcredits < lp->lp_minrtrcredits)
1101                         lp->lp_minrtrcredits = lp->lp_rtrcredits;
1102
1103                 if (lp->lp_rtrcredits < 0) {
1104                         /* must have checked eager_recv before here */
1105                         LASSERT (msg->msg_delayed);
1106                         list_add_tail(&msg->msg_list, &lp->lp_rtrq);
1107                         return EAGAIN;
1108                 }
1109         }
1110
1111         rbp = lnet_msg2bufpool(msg);
1112
1113         if (!msg->msg_rtrcredit) {
1114                 LASSERT ((rbp->rbp_credits < 0) == !list_empty(&rbp->rbp_msgs));
1115
1116                 msg->msg_rtrcredit = 1;
1117                 rbp->rbp_credits--;
1118                 if (rbp->rbp_credits < rbp->rbp_mincredits)
1119                         rbp->rbp_mincredits = rbp->rbp_credits;
1120
1121                 if (rbp->rbp_credits < 0) {
1122                         /* must have checked eager_recv before here */
1123                         LASSERT (msg->msg_delayed);
1124                         list_add_tail(&msg->msg_list, &rbp->rbp_msgs);
1125                         return EAGAIN;
1126                 }
1127         }
1128
1129         LASSERT (!list_empty(&rbp->rbp_bufs));
1130         rb = list_entry(rbp->rbp_bufs.next, lnet_rtrbuf_t, rb_list);
1131         list_del(&rb->rb_list);
1132
1133         msg->msg_niov = rbp->rbp_npages;
1134         msg->msg_kiov = &rb->rb_kiov[0];
1135
1136         if (do_recv) {
1137                 LNET_UNLOCK();
1138                 lnet_ni_recv(lp->lp_ni, msg->msg_private, msg, 1,
1139                              0, msg->msg_len, msg->msg_len);
1140                 LNET_LOCK();
1141         }
1142         return 0;
1143 }
1144 #endif
1145
1146 void
1147 lnet_return_credits_locked (lnet_msg_t *msg)
1148 {
1149         lnet_peer_t       *txpeer = msg->msg_txpeer;
1150         lnet_peer_t       *rxpeer = msg->msg_rxpeer;
1151         lnet_msg_t        *msg2;
1152         lnet_ni_t         *ni;
1153
1154         if (msg->msg_txcredit) {
1155                 /* give back NI txcredits */
1156                 msg->msg_txcredit = 0;
1157                 ni = txpeer->lp_ni;
1158
1159                 LASSERT((ni->ni_txcredits < 0) == !list_empty(&ni->ni_txq));
1160
1161                 ni->ni_txcredits++;
1162                 if (ni->ni_txcredits <= 0) {
1163                         msg2 = list_entry(ni->ni_txq.next, lnet_msg_t, msg_list);
1164                         list_del(&msg2->msg_list);
1165
1166                         LASSERT(msg2->msg_txpeer->lp_ni == ni);
1167                         LASSERT(msg2->msg_delayed);
1168
1169                         (void) lnet_post_send_locked(msg2, 1);
1170                 }
1171         }
1172
1173         if (msg->msg_peertxcredit) {
1174                 /* give back peer txcredits */
1175                 msg->msg_peertxcredit = 0;
1176
1177                 LASSERT((txpeer->lp_txcredits < 0) == !list_empty(&txpeer->lp_txq));
1178
1179                 txpeer->lp_txqnob -= msg->msg_len + sizeof(lnet_hdr_t);
1180                 LASSERT (txpeer->lp_txqnob >= 0);
1181
1182                 txpeer->lp_txcredits++;
1183                 if (txpeer->lp_txcredits <= 0) {
1184                         msg2 = list_entry(txpeer->lp_txq.next,
1185                                           lnet_msg_t, msg_list);
1186                         list_del(&msg2->msg_list);
1187
1188                         LASSERT (msg2->msg_txpeer == txpeer);
1189                         LASSERT (msg2->msg_delayed);
1190
1191                         (void) lnet_post_send_locked(msg2, 1);
1192                 }
1193         }
1194
1195         if (txpeer != NULL) {
1196                 msg->msg_txpeer = NULL;
1197                 lnet_peer_decref_locked(txpeer);
1198         }
1199
1200 #ifdef __KERNEL__
1201         if (msg->msg_rtrcredit) {
1202                 /* give back global router credits */
1203                 lnet_rtrbuf_t     *rb;
1204                 lnet_rtrbufpool_t *rbp;
1205
1206                 /* NB If a msg ever blocks for a buffer in rbp_msgs, it stays
1207                  * there until it gets one allocated, or aborts the wait
1208                  * itself */
1209                 LASSERT (msg->msg_kiov != NULL);
1210
1211                 rb = list_entry(msg->msg_kiov, lnet_rtrbuf_t, rb_kiov[0]);
1212                 rbp = rb->rb_pool;
1213                 LASSERT (rbp == lnet_msg2bufpool(msg));
1214
1215                 msg->msg_kiov = NULL;
1216                 msg->msg_rtrcredit = 0;
1217
1218                 LASSERT((rbp->rbp_credits < 0) == !list_empty(&rbp->rbp_msgs));
1219                 LASSERT((rbp->rbp_credits > 0) == !list_empty(&rbp->rbp_bufs));
1220
1221                 list_add(&rb->rb_list, &rbp->rbp_bufs);
1222                 rbp->rbp_credits++;
1223                 if (rbp->rbp_credits <= 0) {
1224                         msg2 = list_entry(rbp->rbp_msgs.next,
1225                                           lnet_msg_t, msg_list);
1226                         list_del(&msg2->msg_list);
1227
1228                         (void) lnet_post_routed_recv_locked(msg2, 1);
1229                 }
1230         }
1231
1232         if (msg->msg_peerrtrcredit) {
1233                 /* give back peer router credits */
1234                 msg->msg_peerrtrcredit = 0;
1235
1236                 LASSERT((rxpeer->lp_rtrcredits < 0) == !list_empty(&rxpeer->lp_rtrq));
1237
1238                 rxpeer->lp_rtrcredits++;
1239                 if (rxpeer->lp_rtrcredits <= 0) {
1240                         msg2 = list_entry(rxpeer->lp_rtrq.next,
1241                                           lnet_msg_t, msg_list);
1242                         list_del(&msg2->msg_list);
1243
1244                         (void) lnet_post_routed_recv_locked(msg2, 1);
1245                 }
1246         }
1247 #else
1248         LASSERT (!msg->msg_rtrcredit);
1249         LASSERT (!msg->msg_peerrtrcredit);
1250 #endif
1251         if (rxpeer != NULL) {
1252                 msg->msg_rxpeer = NULL;
1253                 lnet_peer_decref_locked(rxpeer);
1254         }
1255 }
1256
1257 int
1258 lnet_send(lnet_nid_t src_nid, lnet_msg_t *msg)
1259 {
1260         lnet_nid_t        dst_nid = msg->msg_target.nid;
1261         lnet_ni_t        *src_ni;
1262         lnet_ni_t        *local_ni;
1263         lnet_remotenet_t *rnet;
1264         lnet_route_t     *route;
1265         lnet_route_t     *best_route;
1266         struct list_head *tmp;
1267         lnet_peer_t      *lp;
1268         lnet_peer_t      *lp2;
1269         int               rc;
1270
1271         LASSERT (msg->msg_txpeer == NULL);
1272         LASSERT (!msg->msg_sending);
1273         LASSERT (!msg->msg_target_is_router);
1274         LASSERT (!msg->msg_receiving);
1275
1276         msg->msg_sending = 1;
1277
1278         /* NB! ni != NULL == interface pre-determined (ACK/REPLY) */
1279
1280         LNET_LOCK();
1281
1282         if (the_lnet.ln_shutdown) {
1283                 LNET_UNLOCK();
1284                 return -ESHUTDOWN;
1285         }
1286
1287         if (src_nid == LNET_NID_ANY) {
1288                 src_ni = NULL;
1289         } else {
1290                 src_ni = lnet_nid2ni_locked(src_nid);
1291                 if (src_ni == NULL) {
1292                         LNET_UNLOCK();
1293                         CERROR("Can't send to %s: src %s is not a local nid\n",
1294                                libcfs_nid2str(dst_nid), libcfs_nid2str(src_nid));
1295                         return -EINVAL;
1296                 }
1297                 LASSERT (!msg->msg_routing);
1298         }
1299
1300         /* Is this for someone on a local network? */
1301         local_ni = lnet_net2ni_locked(LNET_NIDNET(dst_nid));
1302
1303         if (local_ni != NULL) {
1304                 if (src_ni == NULL) {
1305                         src_ni = local_ni;
1306                         src_nid = src_ni->ni_nid;
1307                 } else if (src_ni == local_ni) {
1308                         lnet_ni_decref_locked(local_ni);
1309                 } else {
1310                         lnet_ni_decref_locked(local_ni);
1311                         lnet_ni_decref_locked(src_ni);
1312                         LNET_UNLOCK();
1313                         CERROR("no route to %s via from %s\n",
1314                                libcfs_nid2str(dst_nid), libcfs_nid2str(src_nid));
1315                         return -EINVAL;
1316                 }
1317
1318                 LASSERT (src_nid != LNET_NID_ANY);
1319
1320                 if (!msg->msg_routing)
1321                         msg->msg_hdr.src_nid = cpu_to_le64(src_nid);
1322
1323                 if (src_ni == the_lnet.ln_loni) {
1324                         /* No send credit hassles with LOLND */
1325                         LNET_UNLOCK();
1326                         lnet_ni_send(src_ni, msg);
1327                         lnet_ni_decref(src_ni);
1328                         return 0;
1329                 }
1330
1331                 rc = lnet_nid2peer_locked(&lp, dst_nid);
1332                 lnet_ni_decref_locked(src_ni);  /* lp has ref on src_ni; lose mine */
1333                 if (rc != 0) {
1334                         LNET_UNLOCK();
1335                         CERROR("Error %d finding peer %s\n", rc,
1336                                libcfs_nid2str(dst_nid));
1337                         /* ENOMEM or shutting down */
1338                         return rc;
1339                 }
1340                 LASSERT (lp->lp_ni == src_ni);
1341         } else {
1342                 /* sending to a remote network */
1343                 rnet = lnet_find_net_locked(LNET_NIDNET(dst_nid));
1344                 if (rnet == NULL) {
1345                         if (src_ni != NULL)
1346                                 lnet_ni_decref_locked(src_ni);
1347                         LNET_UNLOCK();
1348                         CERROR("No route to %s\n", libcfs_id2str(msg->msg_target));
1349                         return -EHOSTUNREACH;
1350                 }
1351
1352                 /* Find the best gateway I can use */
1353                 lp = NULL;
1354                 best_route = NULL;
1355                 list_for_each(tmp, &rnet->lrn_routes) {
1356                         route = list_entry(tmp, lnet_route_t, lr_list);
1357                         lp2 = route->lr_gateway;
1358
1359                         if (lp2->lp_alive &&
1360                             (src_ni == NULL || lp2->lp_ni == src_ni) &&
1361                             (lp == NULL || lnet_compare_routers(lp2, lp) > 0)) {
1362                                 best_route = route;
1363                                 lp = lp2;
1364                         }
1365                 }
1366
1367                 if (lp == NULL) {
1368                         if (src_ni != NULL)
1369                                 lnet_ni_decref_locked(src_ni);
1370                         LNET_UNLOCK();
1371                         CERROR("No route to %s (all routers down)\n",
1372                                libcfs_id2str(msg->msg_target));
1373                         return -EHOSTUNREACH;
1374                 }
1375
1376                 /* Place selected route at the end of the route list to ensure
1377                  * fairness; everything else being equal... */
1378                 list_del(&best_route->lr_list);
1379                 list_add_tail(&best_route->lr_list, &rnet->lrn_routes);
1380
1381                 if (src_ni == NULL) {
1382                         src_ni = lp->lp_ni;
1383                         src_nid = src_ni->ni_nid;
1384                 } else {
1385                         LASSERT (src_ni == lp->lp_ni);
1386                         lnet_ni_decref_locked(src_ni);
1387                 }
1388
1389                 lnet_peer_addref_locked(lp);
1390
1391                 LASSERT (src_nid != LNET_NID_ANY);
1392
1393                 if (!msg->msg_routing) {
1394                         /* I'm the source and now I know which NI to send on */
1395                         msg->msg_hdr.src_nid = cpu_to_le64(src_nid);
1396                 }
1397
1398                 msg->msg_target_is_router = 1;
1399                 msg->msg_target.nid = lp->lp_nid;
1400                 msg->msg_target.pid = LUSTRE_SRV_LNET_PID;
1401         }
1402
1403         /* 'lp' is our best choice of peer */
1404
1405         LASSERT (!msg->msg_peertxcredit);
1406         LASSERT (!msg->msg_txcredit);
1407         LASSERT (msg->msg_txpeer == NULL);
1408
1409         msg->msg_txpeer = lp;                   /* msg takes my ref on lp */
1410
1411         rc = lnet_post_send_locked(msg, 0);
1412         LNET_UNLOCK();
1413
1414         if (rc == EHOSTUNREACH)
1415                 return -EHOSTUNREACH;
1416
1417         if (rc == 0)
1418                 lnet_ni_send(src_ni, msg);
1419
1420         return 0;
1421 }
1422
1423 static void
1424 lnet_commit_md (lnet_libmd_t *md, lnet_msg_t *msg)
1425 {
1426         /* ALWAYS called holding the LNET_LOCK */
1427         /* Here, we commit the MD to a network OP by marking it busy and
1428          * decrementing its threshold.  Come what may, the network "owns"
1429          * the MD until a call to lnet_finalize() signals completion. */
1430         LASSERT (!msg->msg_routing);
1431
1432         msg->msg_md = md;
1433
1434         md->md_refcount++;
1435         if (md->md_threshold != LNET_MD_THRESH_INF) {
1436                 LASSERT (md->md_threshold > 0);
1437                 md->md_threshold--;
1438         }
1439
1440         the_lnet.ln_counters.msgs_alloc++;
1441         if (the_lnet.ln_counters.msgs_alloc > 
1442             the_lnet.ln_counters.msgs_max)
1443                 the_lnet.ln_counters.msgs_max = 
1444                         the_lnet.ln_counters.msgs_alloc;
1445
1446         LASSERT (!msg->msg_onactivelist);
1447         msg->msg_onactivelist = 1;
1448         list_add (&msg->msg_activelist, &the_lnet.ln_active_msgs);
1449 }
1450
1451 static void
1452 lnet_drop_message (lnet_ni_t *ni, void *private, unsigned int nob)
1453 {
1454         LNET_LOCK();
1455         the_lnet.ln_counters.drop_count++;
1456         the_lnet.ln_counters.drop_length += nob;
1457         LNET_UNLOCK();
1458
1459         lnet_ni_recv(ni, private, NULL, 0, 0, 0, nob);
1460 }
1461
1462 static void
1463 lnet_drop_delayed_put(lnet_msg_t *msg, char *reason)
1464 {
1465         lnet_process_id_t id = {0};
1466
1467         id.nid = msg->msg_hdr.src_nid;
1468         id.pid = msg->msg_hdr.src_pid;
1469
1470         LASSERT (msg->msg_md == NULL);
1471         LASSERT (msg->msg_delayed);
1472         LASSERT (msg->msg_rxpeer != NULL);
1473         LASSERT (msg->msg_hdr.type == LNET_MSG_PUT);
1474
1475         CWARN("Dropping delayed PUT from %s portal %d match "LPU64
1476               " offset %d length %d: %s\n", 
1477               libcfs_id2str(id),
1478               msg->msg_hdr.msg.put.ptl_index,
1479               msg->msg_hdr.msg.put.match_bits,
1480               msg->msg_hdr.msg.put.offset,
1481               msg->msg_hdr.payload_length,
1482               reason);
1483
1484         /* NB I can't drop msg's ref on msg_rxpeer until after I've
1485          * called lnet_drop_message(), so I just hang onto msg as well
1486          * until that's done */
1487
1488         lnet_drop_message(msg->msg_rxpeer->lp_ni,
1489                           msg->msg_private, msg->msg_len);
1490
1491         LNET_LOCK();
1492
1493         lnet_peer_decref_locked(msg->msg_rxpeer);
1494         msg->msg_rxpeer = NULL;
1495
1496         lnet_msg_free(msg);
1497
1498         LNET_UNLOCK();
1499 }
1500
1501 int
1502 LNetSetLazyPortal(int portal)
1503 {
1504         lnet_portal_t *ptl = &the_lnet.ln_portals[portal];
1505
1506         if (portal < 0 || portal >= the_lnet.ln_nportals)
1507                 return -EINVAL;
1508
1509         CDEBUG(D_NET, "Setting portal %d lazy\n", portal);
1510
1511         LNET_LOCK();
1512
1513         ptl->ptl_options |= LNET_PTL_LAZY;
1514
1515         LNET_UNLOCK();
1516
1517         return 0;
1518 }
1519
1520 int
1521 LNetClearLazyPortal(int portal)
1522 {
1523         struct list_head  zombies;
1524         lnet_portal_t    *ptl = &the_lnet.ln_portals[portal];
1525         lnet_msg_t       *msg;
1526
1527         if (portal < 0 || portal >= the_lnet.ln_nportals)
1528                 return -EINVAL;
1529
1530         LNET_LOCK();
1531
1532         if ((ptl->ptl_options & LNET_PTL_LAZY) == 0) {
1533                 LNET_UNLOCK();
1534                 return 0;
1535         }
1536
1537         if (the_lnet.ln_shutdown)
1538                 CWARN ("Active lazy portal %d on exit\n", portal);
1539         else
1540                 CDEBUG (D_NET, "clearing portal %d lazy\n", portal);
1541
1542         /* grab all the blocked messages atomically */
1543         list_add(&zombies, &ptl->ptl_msgq);
1544         list_del_init(&ptl->ptl_msgq);
1545
1546         ptl->ptl_msgq_version++;
1547         ptl->ptl_options &= ~LNET_PTL_LAZY;
1548
1549         LNET_UNLOCK();
1550
1551         while (!list_empty(&zombies)) {
1552                 msg = list_entry(zombies.next, lnet_msg_t, msg_list);
1553                 list_del(&msg->msg_list);
1554
1555                 lnet_drop_delayed_put(msg, "Clearing lazy portal attr");
1556         }
1557
1558         return 0;
1559 }
1560
1561 static void
1562 lnet_recv_put(lnet_libmd_t *md, lnet_msg_t *msg, int delayed,
1563               unsigned int offset, unsigned int mlength)
1564 {
1565         lnet_hdr_t       *hdr = &msg->msg_hdr;
1566
1567         LNET_LOCK();
1568
1569         the_lnet.ln_counters.recv_count++;
1570         the_lnet.ln_counters.recv_length += mlength;
1571
1572         LNET_UNLOCK();
1573
1574         if (mlength != 0)
1575                 lnet_setpayloadbuffer(msg);
1576
1577         msg->msg_ev.type       = LNET_EVENT_PUT;
1578         msg->msg_ev.target.pid = hdr->dest_pid;
1579         msg->msg_ev.target.nid = hdr->dest_nid;
1580         msg->msg_ev.hdr_data   = hdr->msg.put.hdr_data;
1581
1582         /* Must I ACK?  If so I'll grab the ack_wmd out of the header and put
1583          * it back into the ACK during lnet_finalize() */
1584         msg->msg_ack = (!lnet_is_wire_handle_none(&hdr->msg.put.ack_wmd) &&
1585                         (md->md_options & LNET_MD_ACK_DISABLE) == 0);
1586
1587         lnet_ni_recv(msg->msg_rxpeer->lp_ni,
1588                      msg->msg_private,
1589                      msg, delayed, offset, mlength,
1590                      hdr->payload_length);
1591 }
1592
1593 /* called with LNET_LOCK held */
1594 void
1595 lnet_match_blocked_msg(lnet_libmd_t *md)
1596 {
1597         CFS_LIST_HEAD    (drops);
1598         CFS_LIST_HEAD    (matches);
1599         struct list_head *tmp;
1600         struct list_head *entry;
1601         lnet_msg_t       *msg;
1602         lnet_me_t        *me  = md->md_me;
1603         lnet_portal_t    *ptl = &the_lnet.ln_portals[me->me_portal];
1604
1605         LASSERT (me->me_portal < (unsigned int)the_lnet.ln_nportals);
1606
1607         if ((ptl->ptl_options & LNET_PTL_LAZY) == 0) {
1608                 LASSERT (list_empty(&ptl->ptl_msgq));
1609                 return;
1610         }
1611
1612         LASSERT (md->md_refcount == 0); /* a brand new MD */
1613
1614         list_for_each_safe (entry, tmp, &ptl->ptl_msgq) {
1615                 int               rc;
1616                 int               index;
1617                 unsigned int      mlength;
1618                 unsigned int      offset;
1619                 lnet_hdr_t       *hdr;
1620                 lnet_process_id_t src;
1621
1622                 msg = list_entry(entry, lnet_msg_t, msg_list);
1623
1624                 LASSERT (msg->msg_delayed);
1625
1626                 hdr   = &msg->msg_hdr;
1627                 index = hdr->msg.put.ptl_index;
1628
1629                 src.nid = hdr->src_nid;
1630                 src.pid = hdr->src_pid;
1631
1632                 rc = lnet_try_match_md(index, LNET_MD_OP_PUT, src,
1633                                        hdr->payload_length,
1634                                        hdr->msg.put.offset,
1635                                        hdr->msg.put.match_bits,
1636                                        md, msg, &mlength, &offset);
1637
1638                 if (rc == LNET_MATCHMD_NONE)
1639                         continue;
1640
1641                 /* Hurrah! This _is_ a match */
1642                 list_del(&msg->msg_list);
1643                 ptl->ptl_msgq_version++;
1644
1645                 if (rc == LNET_MATCHMD_OK) {
1646                         list_add_tail(&msg->msg_list, &matches);
1647
1648                         CDEBUG(D_NET, "Resuming delayed PUT from %s portal %d "
1649                                "match "LPU64" offset %d length %d.\n",
1650                                libcfs_id2str(src),
1651                                hdr->msg.put.ptl_index,
1652                                hdr->msg.put.match_bits,
1653                                hdr->msg.put.offset,
1654                                hdr->payload_length);
1655                 } else {
1656                         LASSERT (rc == LNET_MATCHMD_DROP);
1657
1658                         list_add_tail(&msg->msg_list, &drops);
1659                 }
1660
1661                 if (lnet_md_exhausted(md))
1662                         break;
1663         }
1664
1665         LNET_UNLOCK();
1666
1667         list_for_each_safe (entry, tmp, &drops) {
1668                 msg = list_entry(entry, lnet_msg_t, msg_list);
1669
1670                 list_del(&msg->msg_list);
1671
1672                 lnet_drop_delayed_put(msg, "Bad match");
1673         }
1674
1675         list_for_each_safe (entry, tmp, &matches) {
1676                 msg = list_entry(entry, lnet_msg_t, msg_list);
1677
1678                 list_del(&msg->msg_list);
1679
1680                 /* md won't disappear under me, since each msg
1681                  * holds a ref on it */
1682                 lnet_recv_put(md, msg, 1,
1683                               msg->msg_ev.offset,
1684                               msg->msg_ev.mlength);
1685         }
1686
1687         LNET_LOCK();
1688 }
1689
1690 static int
1691 lnet_parse_put(lnet_ni_t *ni, lnet_msg_t *msg)
1692 {
1693         int               rc;
1694         int               index;
1695         __u64             version;
1696         lnet_hdr_t       *hdr = &msg->msg_hdr;
1697         unsigned int      rlength = hdr->payload_length;
1698         unsigned int      mlength = 0;
1699         unsigned int      offset = 0;
1700         lnet_process_id_t src= {0};
1701         lnet_libmd_t     *md;
1702         lnet_portal_t    *ptl;
1703
1704         src.nid = hdr->src_nid;
1705         src.pid = hdr->src_pid;
1706
1707         /* Convert put fields to host byte order */
1708         hdr->msg.put.match_bits = le64_to_cpu(hdr->msg.put.match_bits);
1709         hdr->msg.put.ptl_index = le32_to_cpu(hdr->msg.put.ptl_index);
1710         hdr->msg.put.offset = le32_to_cpu(hdr->msg.put.offset);
1711
1712         index = hdr->msg.put.ptl_index;
1713         ptl = &the_lnet.ln_portals[index];
1714
1715         LNET_LOCK();
1716
1717  again:
1718         rc = lnet_match_md(index, LNET_MD_OP_PUT, src,
1719                            rlength, hdr->msg.put.offset,
1720                            hdr->msg.put.match_bits, msg,
1721                            &mlength, &offset, &md);
1722         switch (rc) {
1723         default:
1724                 LBUG();
1725
1726         case LNET_MATCHMD_OK:
1727                 LNET_UNLOCK();
1728                 lnet_recv_put(md, msg, msg->msg_delayed, offset, mlength);
1729                 return 0;
1730
1731         case LNET_MATCHMD_NONE:
1732                 version = ptl->ptl_ml_version;
1733
1734                 rc = 0;
1735                 if (!msg->msg_delayed)
1736                         rc = lnet_eager_recv_locked(msg);
1737
1738                 if (rc == 0 &&
1739                     !the_lnet.ln_shutdown &&
1740                     ((ptl->ptl_options & LNET_PTL_LAZY) != 0)) {
1741                         if (version != ptl->ptl_ml_version)
1742                                 goto again;
1743
1744                         list_add_tail(&msg->msg_list, &ptl->ptl_msgq);
1745                         ptl->ptl_msgq_version++;
1746                         LNET_UNLOCK();
1747
1748                         CDEBUG(D_NET, "Delaying PUT from %s portal %d match "
1749                                LPU64" offset %d length %d: no match \n",
1750                                libcfs_id2str(src), index,
1751                                hdr->msg.put.match_bits,
1752                                hdr->msg.put.offset, rlength);
1753                         return 0;
1754                 }
1755                 /* fall through */
1756
1757         case LNET_MATCHMD_DROP:
1758                 CDEBUG(D_NETERROR,
1759                        "Dropping PUT from %s portal %d match "LPU64
1760                        " offset %d length %d: %d\n",
1761                        libcfs_id2str(src), index,
1762                        hdr->msg.put.match_bits,
1763                        hdr->msg.put.offset, rlength, rc);
1764                 LNET_UNLOCK();
1765
1766                 return ENOENT;          /* +ve: OK but no match */
1767         }
1768 }
1769
1770 static int
1771 lnet_parse_get(lnet_ni_t *ni, lnet_msg_t *msg, int rdma_get)
1772 {
1773         lnet_hdr_t        *hdr = &msg->msg_hdr;
1774         unsigned int       mlength = 0;
1775         unsigned int       offset = 0;
1776         lnet_process_id_t  src = {0};
1777         lnet_handle_wire_t reply_wmd;
1778         lnet_libmd_t      *md;
1779         int                rc;
1780
1781         src.nid = hdr->src_nid;
1782         src.pid = hdr->src_pid;
1783
1784         /* Convert get fields to host byte order */
1785         hdr->msg.get.match_bits = le64_to_cpu(hdr->msg.get.match_bits);
1786         hdr->msg.get.ptl_index = le32_to_cpu(hdr->msg.get.ptl_index);
1787         hdr->msg.get.sink_length = le32_to_cpu(hdr->msg.get.sink_length);
1788         hdr->msg.get.src_offset = le32_to_cpu(hdr->msg.get.src_offset);
1789
1790         LNET_LOCK();
1791
1792         rc = lnet_match_md(hdr->msg.get.ptl_index, LNET_MD_OP_GET, src,
1793                            hdr->msg.get.sink_length, hdr->msg.get.src_offset,
1794                            hdr->msg.get.match_bits, msg,
1795                            &mlength, &offset, &md);
1796         if (rc == LNET_MATCHMD_DROP) {
1797                 CDEBUG(D_NETERROR,
1798                        "Dropping GET from %s portal %d match "LPU64
1799                        " offset %d length %d\n",
1800                        libcfs_id2str(src),
1801                        hdr->msg.get.ptl_index,
1802                        hdr->msg.get.match_bits,
1803                        hdr->msg.get.src_offset,
1804                        hdr->msg.get.sink_length);
1805                 LNET_UNLOCK();
1806                 return ENOENT;                  /* +ve: OK but no match */
1807         }
1808
1809         LASSERT (rc == LNET_MATCHMD_OK);
1810
1811         the_lnet.ln_counters.send_count++;
1812         the_lnet.ln_counters.send_length += mlength;
1813
1814         LNET_UNLOCK();
1815
1816         msg->msg_ev.type = LNET_EVENT_GET;
1817         msg->msg_ev.target.pid = hdr->dest_pid;
1818         msg->msg_ev.target.nid = hdr->dest_nid;
1819         msg->msg_ev.hdr_data = 0;
1820
1821         reply_wmd = hdr->msg.get.return_wmd;
1822
1823         lnet_prep_send(msg, LNET_MSG_REPLY, src, offset, mlength);
1824
1825         msg->msg_hdr.msg.reply.dst_wmd = reply_wmd;
1826
1827         if (rdma_get) {
1828                 /* The LND completes the REPLY from her recv procedure */
1829                 lnet_ni_recv(ni, msg->msg_private, msg, 0,
1830                              msg->msg_offset, msg->msg_len, msg->msg_len);
1831                 return 0;
1832         }
1833
1834         lnet_ni_recv(ni, msg->msg_private, NULL, 0, 0, 0, 0);
1835         msg->msg_receiving = 0;
1836
1837         rc = lnet_send(ni->ni_nid, msg);
1838         if (rc < 0) {
1839                 /* didn't get as far as lnet_ni_send() */
1840                 CERROR("%s: Unable to send REPLY for GET from %s: %d\n",
1841                        libcfs_nid2str(ni->ni_nid), libcfs_id2str(src), rc);
1842
1843                 lnet_finalize(ni, msg, rc);
1844         }
1845
1846         return 0;
1847 }
1848
1849 static int
1850 lnet_parse_reply(lnet_ni_t *ni, lnet_msg_t *msg)
1851 {
1852         void             *private = msg->msg_private;
1853         lnet_hdr_t       *hdr = &msg->msg_hdr;
1854         lnet_process_id_t src = {0};
1855         lnet_libmd_t     *md;
1856         int               rlength;
1857         int               mlength;
1858
1859         LNET_LOCK();
1860
1861         src.nid = hdr->src_nid;
1862         src.pid = hdr->src_pid;
1863
1864         /* NB handles only looked up by creator (no flips) */
1865         md = lnet_wire_handle2md(&hdr->msg.reply.dst_wmd);
1866         if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
1867                 CDEBUG(D_NETERROR, "%s: Dropping REPLY from %s for %s "
1868                        "MD "LPX64"."LPX64"\n", 
1869                        libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1870                        (md == NULL) ? "invalid" : "inactive",
1871                        hdr->msg.reply.dst_wmd.wh_interface_cookie,
1872                        hdr->msg.reply.dst_wmd.wh_object_cookie);
1873                 if (md != NULL && md->md_me != NULL)
1874                         CERROR("REPLY MD also attached to portal %d\n",
1875                                md->md_me->me_portal);
1876
1877                 LNET_UNLOCK();
1878                 return ENOENT;                  /* +ve: OK but no match */
1879         }
1880
1881         LASSERT (md->md_offset == 0);
1882
1883         rlength = hdr->payload_length;
1884         mlength = MIN(rlength, (int)md->md_length);
1885
1886         if (mlength < rlength &&
1887             (md->md_options & LNET_MD_TRUNCATE) == 0) {
1888                 CDEBUG(D_NETERROR, "%s: Dropping REPLY from %s length %d "
1889                        "for MD "LPX64" would overflow (%d)\n",
1890                        libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1891                         rlength, hdr->msg.reply.dst_wmd.wh_object_cookie,
1892                         mlength);
1893                 LNET_UNLOCK();
1894                 return ENOENT;          /* +ve: OK but no match */
1895         }
1896
1897         CDEBUG(D_NET, "%s: Reply from %s of length %d/%d into md "LPX64"\n",
1898                libcfs_nid2str(ni->ni_nid), libcfs_id2str(src), 
1899                mlength, rlength, hdr->msg.reply.dst_wmd.wh_object_cookie);
1900
1901         lnet_commit_md(md, msg);
1902
1903         if (mlength != 0)
1904                 lnet_setpayloadbuffer(msg);
1905
1906         msg->msg_ev.type = LNET_EVENT_REPLY;
1907         msg->msg_ev.target.pid = hdr->dest_pid;
1908         msg->msg_ev.target.nid = hdr->dest_nid;
1909         msg->msg_ev.initiator = src;
1910         msg->msg_ev.rlength = rlength;
1911         msg->msg_ev.mlength = mlength;
1912         msg->msg_ev.offset = 0;
1913
1914         lnet_md_deconstruct(md, &msg->msg_ev.md);
1915         lnet_md2handle(&msg->msg_ev.md_handle, md);
1916
1917         the_lnet.ln_counters.recv_count++;
1918         the_lnet.ln_counters.recv_length += mlength;
1919
1920         LNET_UNLOCK();
1921
1922         lnet_ni_recv(ni, private, msg, 0, 0, mlength, rlength);
1923         return 0;
1924 }
1925
1926 static int
1927 lnet_parse_ack(lnet_ni_t *ni, lnet_msg_t *msg)
1928 {
1929         lnet_hdr_t       *hdr = &msg->msg_hdr;
1930         lnet_process_id_t src = {0};
1931         lnet_libmd_t     *md;
1932
1933         src.nid = hdr->src_nid;
1934         src.pid = hdr->src_pid;
1935
1936         /* Convert ack fields to host byte order */
1937         hdr->msg.ack.match_bits = le64_to_cpu(hdr->msg.ack.match_bits);
1938         hdr->msg.ack.mlength = le32_to_cpu(hdr->msg.ack.mlength);
1939
1940         LNET_LOCK();
1941
1942         /* NB handles only looked up by creator (no flips) */
1943         md = lnet_wire_handle2md(&hdr->msg.ack.dst_wmd);
1944         if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
1945                 /* Don't moan; this is expected */
1946                 CDEBUG(D_NET,
1947                        "%s: Dropping ACK from %s to %s MD "LPX64"."LPX64"\n",
1948                        libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1949                        (md == NULL) ? "invalid" : "inactive",
1950                        hdr->msg.ack.dst_wmd.wh_interface_cookie,
1951                        hdr->msg.ack.dst_wmd.wh_object_cookie);
1952                 if (md != NULL && md->md_me != NULL)
1953                         CERROR("Source MD also attached to portal %d\n",
1954                                md->md_me->me_portal);
1955
1956                 LNET_UNLOCK();
1957                 return ENOENT;                  /* +ve! */
1958         }
1959
1960         CDEBUG(D_NET, "%s: ACK from %s into md "LPX64"\n",
1961                libcfs_nid2str(ni->ni_nid), libcfs_id2str(src), 
1962                hdr->msg.ack.dst_wmd.wh_object_cookie);
1963
1964         lnet_commit_md(md, msg);
1965
1966         msg->msg_ev.type = LNET_EVENT_ACK;
1967         msg->msg_ev.target.pid = hdr->dest_pid;
1968         msg->msg_ev.target.nid = hdr->dest_nid;
1969         msg->msg_ev.initiator = src;
1970         msg->msg_ev.mlength = hdr->msg.ack.mlength;
1971         msg->msg_ev.match_bits = hdr->msg.ack.match_bits;
1972
1973         lnet_md_deconstruct(md, &msg->msg_ev.md);
1974         lnet_md2handle(&msg->msg_ev.md_handle, md);
1975
1976         the_lnet.ln_counters.recv_count++;
1977
1978         LNET_UNLOCK();
1979
1980         lnet_ni_recv(ni, msg->msg_private, msg, 0, 0, 0, msg->msg_len);
1981         return 0;
1982 }
1983
1984 char *
1985 lnet_msgtyp2str (int type)
1986 {
1987         switch (type) {
1988         case LNET_MSG_ACK:
1989                 return ("ACK");
1990         case LNET_MSG_PUT:
1991                 return ("PUT");
1992         case LNET_MSG_GET:
1993                 return ("GET");
1994         case LNET_MSG_REPLY:
1995                 return ("REPLY");
1996         case LNET_MSG_HELLO:
1997                 return ("HELLO");
1998         default:
1999                 return ("<UNKNOWN>");
2000         }
2001 }
2002
2003 void
2004 lnet_print_hdr(lnet_hdr_t * hdr)
2005 {
2006         lnet_process_id_t src = {0};
2007         lnet_process_id_t dst = {0};
2008         char *type_str = lnet_msgtyp2str (hdr->type);
2009
2010         src.nid = hdr->src_nid;
2011         src.pid = hdr->src_pid;
2012
2013         dst.nid = hdr->dest_nid;
2014         dst.pid = hdr->dest_pid;
2015
2016         CWARN("P3 Header at %p of type %s\n", hdr, type_str);
2017         CWARN("    From %s\n", libcfs_id2str(src));
2018         CWARN("    To   %s\n", libcfs_id2str(dst));
2019
2020         switch (hdr->type) {
2021         default:
2022                 break;
2023
2024         case LNET_MSG_PUT:
2025                 CWARN("    Ptl index %d, ack md "LPX64"."LPX64", "
2026                       "match bits "LPU64"\n",
2027                       hdr->msg.put.ptl_index,
2028                       hdr->msg.put.ack_wmd.wh_interface_cookie,
2029                       hdr->msg.put.ack_wmd.wh_object_cookie,
2030                       hdr->msg.put.match_bits);
2031                 CWARN("    Length %d, offset %d, hdr data "LPX64"\n",
2032                       hdr->payload_length, hdr->msg.put.offset,
2033                       hdr->msg.put.hdr_data);
2034                 break;
2035
2036         case LNET_MSG_GET:
2037                 CWARN("    Ptl index %d, return md "LPX64"."LPX64", "
2038                       "match bits "LPU64"\n", hdr->msg.get.ptl_index,
2039                       hdr->msg.get.return_wmd.wh_interface_cookie,
2040                       hdr->msg.get.return_wmd.wh_object_cookie,
2041                       hdr->msg.get.match_bits);
2042                 CWARN("    Length %d, src offset %d\n",
2043                       hdr->msg.get.sink_length,
2044                       hdr->msg.get.src_offset);
2045                 break;
2046
2047         case LNET_MSG_ACK:
2048                 CWARN("    dst md "LPX64"."LPX64", "
2049                       "manipulated length %d\n",
2050                       hdr->msg.ack.dst_wmd.wh_interface_cookie,
2051                       hdr->msg.ack.dst_wmd.wh_object_cookie,
2052                       hdr->msg.ack.mlength);
2053                 break;
2054
2055         case LNET_MSG_REPLY:
2056                 CWARN("    dst md "LPX64"."LPX64", "
2057                       "length %d\n",
2058                       hdr->msg.reply.dst_wmd.wh_interface_cookie,
2059                       hdr->msg.reply.dst_wmd.wh_object_cookie,
2060                       hdr->payload_length);
2061         }
2062
2063 }
2064
2065
2066 int
2067 lnet_parse(lnet_ni_t *ni, lnet_hdr_t *hdr, lnet_nid_t from_nid, 
2068            void *private, int rdma_req)
2069 {
2070         int            rc = 0;
2071         int            for_me;
2072         lnet_msg_t    *msg;
2073         lnet_pid_t     dest_pid;
2074         lnet_nid_t     dest_nid;
2075         lnet_nid_t     src_nid;
2076         __u32          payload_length;
2077         __u32          type;
2078
2079         LASSERT (!in_interrupt ());
2080
2081         type = le32_to_cpu(hdr->type);
2082         src_nid = le64_to_cpu(hdr->src_nid);
2083         dest_nid = le64_to_cpu(hdr->dest_nid);
2084         dest_pid = le32_to_cpu(hdr->dest_pid);
2085         payload_length = le32_to_cpu(hdr->payload_length);
2086
2087         for_me = (ni->ni_nid == dest_nid);
2088
2089         switch (type) {
2090         case LNET_MSG_ACK:
2091         case LNET_MSG_GET:
2092                 if (payload_length > 0) {
2093                         CERROR("%s, src %s: bad %s payload %d (0 expected)\n",
2094                                libcfs_nid2str(from_nid),
2095                                libcfs_nid2str(src_nid),
2096                                lnet_msgtyp2str(type), payload_length);
2097                         return -EPROTO;
2098                 }
2099                 break;
2100
2101         case LNET_MSG_PUT:
2102         case LNET_MSG_REPLY:
2103                 if (payload_length > (__u32)(for_me ? LNET_MAX_PAYLOAD : LNET_MTU)) {
2104                         CERROR("%s, src %s: bad %s payload %d "
2105                                "(%d max expected)\n",
2106                                libcfs_nid2str(from_nid),
2107                                libcfs_nid2str(src_nid),
2108                                lnet_msgtyp2str(type),
2109                                payload_length,
2110                                for_me ? LNET_MAX_PAYLOAD : LNET_MTU);
2111                         return -EPROTO;
2112                 }
2113                 break;
2114
2115         default:
2116                 CERROR("%s, src %s: Bad message type 0x%x\n",
2117                        libcfs_nid2str(from_nid),
2118                        libcfs_nid2str(src_nid), type);
2119                 return -EPROTO;
2120         }
2121
2122         /* Regard a bad destination NID as a protocol error.  Senders should
2123          * know what they're doing; if they don't they're misconfigured, buggy
2124          * or malicious so we chop them off at the knees :) */
2125
2126         if (!for_me) {
2127                 if (LNET_NIDNET(dest_nid) == LNET_NIDNET(ni->ni_nid)) {
2128                         /* should have gone direct */
2129                         CERROR ("%s, src %s: Bad dest nid %s "
2130                                 "(should have been sent direct)\n",
2131                                 libcfs_nid2str(from_nid),
2132                                 libcfs_nid2str(src_nid),
2133                                 libcfs_nid2str(dest_nid));
2134                         return -EPROTO;
2135                 }
2136
2137                 if (lnet_islocalnid(dest_nid)) {
2138                         /* dest is another local NI; sender should have used
2139                          * this node's NID on its own network */
2140                         CERROR ("%s, src %s: Bad dest nid %s "
2141                                 "(it's my nid but on a different network)\n",
2142                                 libcfs_nid2str(from_nid),
2143                                 libcfs_nid2str(src_nid),
2144                                 libcfs_nid2str(dest_nid));
2145                         return -EPROTO;
2146                 }
2147
2148                 if (rdma_req && type == LNET_MSG_GET) {
2149                         CERROR ("%s, src %s: Bad optimized GET for %s "
2150                                 "(final destination must be me)\n",
2151                                 libcfs_nid2str(from_nid),
2152                                 libcfs_nid2str(src_nid),
2153                                 libcfs_nid2str(dest_nid));
2154                         return -EPROTO;
2155                 }
2156
2157                 if (!the_lnet.ln_routing) {
2158                         CERROR ("%s, src %s: Dropping message for %s "
2159                                 "(routing not enabled)\n",
2160                                 libcfs_nid2str(from_nid),
2161                                 libcfs_nid2str(src_nid),
2162                                 libcfs_nid2str(dest_nid));
2163                         goto drop;
2164                 }
2165         }
2166
2167         /* Message looks OK; we're not going to return an error, so we MUST
2168          * call back lnd_recv() come what may... */
2169
2170         if (!list_empty (&the_lnet.ln_test_peers) && /* normally we don't */
2171             fail_peer (src_nid, 0))             /* shall we now? */
2172         {
2173                 CERROR("%s, src %s: Dropping %s to simulate failure\n",
2174                        libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
2175                        lnet_msgtyp2str(type));
2176                 goto drop;
2177         }
2178
2179         msg = lnet_msg_alloc();
2180         if (msg == NULL) {
2181                 CERROR("%s, src %s: Dropping %s (out of memory)\n",
2182                        libcfs_nid2str(from_nid), libcfs_nid2str(src_nid), 
2183                        lnet_msgtyp2str(type));
2184                 goto drop;
2185         }
2186
2187         /* msg zeroed in lnet_msg_alloc; i.e. flags all clear, pointers NULL etc */
2188
2189         msg->msg_type = type;
2190         msg->msg_private = private;
2191         msg->msg_receiving = 1;
2192         msg->msg_len = msg->msg_wanted = payload_length;
2193         msg->msg_offset = 0;
2194         msg->msg_hdr = *hdr;
2195
2196         LNET_LOCK();
2197         rc = lnet_nid2peer_locked(&msg->msg_rxpeer, from_nid);
2198         if (rc != 0) {
2199                 LNET_UNLOCK();
2200                 CERROR("%s, src %s: Dropping %s "
2201                        "(error %d looking up sender)\n",
2202                        libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
2203                        lnet_msgtyp2str(type), rc);
2204                 goto free_drop;
2205         }
2206         LNET_UNLOCK();
2207
2208 #ifndef __KERNEL__
2209         LASSERT (for_me);
2210 #else
2211         if (!for_me) {
2212                 msg->msg_target.pid = dest_pid;
2213                 msg->msg_target.nid = dest_nid;
2214                 msg->msg_routing = 1;
2215                 msg->msg_offset = 0;
2216
2217                 LNET_LOCK();
2218                 if (msg->msg_rxpeer->lp_rtrcredits <= 0 ||
2219                     lnet_msg2bufpool(msg)->rbp_credits <= 0) {
2220                         rc = lnet_eager_recv_locked(msg);
2221                         if (rc != 0) {
2222                                 LNET_UNLOCK();
2223                                 goto free_drop;
2224                         }
2225                 }
2226                 lnet_commit_routedmsg(msg);
2227                 rc = lnet_post_routed_recv_locked(msg, 0);
2228                 LNET_UNLOCK();
2229
2230                 if (rc == 0)
2231                         lnet_ni_recv(ni, msg->msg_private, msg, 0,
2232                                      0, payload_length, payload_length);
2233                 return 0;
2234         }
2235 #endif
2236         /* convert common msg->hdr fields to host byteorder */
2237         msg->msg_hdr.type = type;
2238         msg->msg_hdr.src_nid = src_nid;
2239         msg->msg_hdr.src_pid = le32_to_cpu(msg->msg_hdr.src_pid);
2240         msg->msg_hdr.dest_nid = dest_nid;
2241         msg->msg_hdr.dest_pid = dest_pid;
2242         msg->msg_hdr.payload_length = payload_length;
2243
2244         msg->msg_ev.sender = from_nid;
2245
2246         switch (type) {
2247         case LNET_MSG_ACK:
2248                 rc = lnet_parse_ack(ni, msg);
2249                 break;
2250         case LNET_MSG_PUT:
2251                 rc = lnet_parse_put(ni, msg);
2252                 break;
2253         case LNET_MSG_GET:
2254                 rc = lnet_parse_get(ni, msg, rdma_req);
2255                 break;
2256         case LNET_MSG_REPLY:
2257                 rc = lnet_parse_reply(ni, msg);
2258                 break;
2259         default:
2260                 LASSERT(0);
2261                 goto free_drop;  /* prevent an unused label if !kernel */
2262         }
2263
2264         if (rc == 0)
2265                 return 0;
2266
2267         LASSERT (rc == ENOENT);
2268
2269  free_drop:
2270         LASSERT (msg->msg_md == NULL);
2271         LNET_LOCK();
2272         if (msg->msg_rxpeer != NULL) {
2273                 lnet_peer_decref_locked(msg->msg_rxpeer);
2274                 msg->msg_rxpeer = NULL;
2275         }
2276         lnet_msg_free(msg);                     /* expects LNET_LOCK held */
2277         LNET_UNLOCK();
2278
2279  drop:
2280         lnet_drop_message(ni, private, payload_length);
2281         return 0;
2282 }
2283
2284 int
2285 LNetPut(lnet_nid_t self, lnet_handle_md_t mdh, lnet_ack_req_t ack,
2286         lnet_process_id_t target, unsigned int portal,
2287         __u64 match_bits, unsigned int offset,
2288         __u64 hdr_data)
2289 {
2290         lnet_msg_t       *msg;
2291         lnet_libmd_t     *md;
2292         int               rc;
2293
2294         LASSERT (the_lnet.ln_init);
2295         LASSERT (the_lnet.ln_refcount > 0);
2296
2297         if (!list_empty (&the_lnet.ln_test_peers) && /* normally we don't */
2298             fail_peer (target.nid, 1))          /* shall we now? */
2299         {
2300                 CERROR("Dropping PUT to %s: simulated failure\n",
2301                        libcfs_id2str(target));
2302                 return -EIO;
2303         }
2304
2305         msg = lnet_msg_alloc();
2306         if (msg == NULL) {
2307                 CERROR("Dropping PUT to %s: ENOMEM on lnet_msg_t\n",
2308                        libcfs_id2str(target));
2309                 return -ENOMEM;
2310         }
2311
2312         LNET_LOCK();
2313
2314         md = lnet_handle2md(&mdh);
2315         if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
2316                 lnet_msg_free(msg);
2317
2318                 CERROR("Dropping PUT ("LPU64":%d:%s): MD (%d) invalid\n",
2319                        match_bits, portal, libcfs_id2str(target),
2320                        md == NULL ? -1 : md->md_threshold);
2321                 if (md != NULL && md->md_me != NULL)
2322                         CERROR("Source MD also attached to portal %d\n",
2323                                md->md_me->me_portal);
2324
2325                 LNET_UNLOCK();
2326                 return -ENOENT;
2327         }
2328
2329         CDEBUG(D_NET, "LNetPut -> %s\n", libcfs_id2str(target));
2330
2331         lnet_commit_md(md, msg);
2332
2333         lnet_prep_send(msg, LNET_MSG_PUT, target, 0, md->md_length);
2334
2335         msg->msg_hdr.msg.put.match_bits = cpu_to_le64(match_bits);
2336         msg->msg_hdr.msg.put.ptl_index = cpu_to_le32(portal);
2337         msg->msg_hdr.msg.put.offset = cpu_to_le32(offset);
2338         msg->msg_hdr.msg.put.hdr_data = hdr_data;
2339
2340         /* NB handles only looked up by creator (no flips) */
2341         if (ack == LNET_ACK_REQ) {
2342                 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie = 
2343                         the_lnet.ln_interface_cookie;
2344                 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie = 
2345                         md->md_lh.lh_cookie;
2346         } else {
2347                 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie = 
2348                         LNET_WIRE_HANDLE_COOKIE_NONE;
2349                 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie = 
2350                         LNET_WIRE_HANDLE_COOKIE_NONE;
2351         }
2352
2353         msg->msg_ev.type = LNET_EVENT_SEND;
2354         msg->msg_ev.initiator.nid = LNET_NID_ANY;
2355         msg->msg_ev.initiator.pid = the_lnet.ln_pid;
2356         msg->msg_ev.target = target;
2357         msg->msg_ev.sender = LNET_NID_ANY;
2358         msg->msg_ev.pt_index = portal;
2359         msg->msg_ev.match_bits = match_bits;
2360         msg->msg_ev.rlength = md->md_length;
2361         msg->msg_ev.mlength = md->md_length;
2362         msg->msg_ev.offset = offset;
2363         msg->msg_ev.hdr_data = hdr_data;
2364
2365         lnet_md_deconstruct(md, &msg->msg_ev.md);
2366         lnet_md2handle(&msg->msg_ev.md_handle, md);
2367
2368         the_lnet.ln_counters.send_count++;
2369         the_lnet.ln_counters.send_length += md->md_length;
2370
2371         LNET_UNLOCK();
2372
2373         rc = lnet_send(self, msg);
2374         if (rc != 0) {
2375                 CERROR("Error sending PUT to %s: %d\n",
2376                        libcfs_id2str(target), rc);
2377                 lnet_finalize (NULL, msg, rc);
2378         }
2379
2380         /* completion will be signalled by an event */
2381         return 0;
2382 }
2383
2384 lnet_msg_t *
2385 lnet_create_reply_msg (lnet_ni_t *ni, lnet_msg_t *getmsg)
2386 {
2387         /* The LND can DMA direct to the GET md (i.e. no REPLY msg).  This
2388          * returns a msg for the LND to pass to lnet_finalize() when the sink
2389          * data has been received.
2390          *
2391          * CAVEAT EMPTOR: 'getmsg' is the original GET, which is freed when
2392          * lnet_finalize() is called on it, so the LND must call this first */
2393
2394         lnet_msg_t        *msg = lnet_msg_alloc();
2395         lnet_libmd_t      *getmd = getmsg->msg_md;
2396         lnet_process_id_t  peer_id = getmsg->msg_target;
2397
2398         LASSERT (!getmsg->msg_target_is_router);
2399         LASSERT (!getmsg->msg_routing);
2400
2401         LNET_LOCK();
2402
2403         LASSERT (getmd->md_refcount > 0);
2404
2405         if (msg == NULL) {
2406                 CERROR ("%s: Dropping REPLY from %s: can't allocate msg\n",
2407                         libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id));
2408                 goto drop;
2409         }
2410
2411         if (getmd->md_threshold == 0) {
2412                 CERROR ("%s: Dropping REPLY from %s for inactive MD %p\n",
2413                         libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id), 
2414                         getmd);
2415                 goto drop_msg;
2416         }
2417
2418         LASSERT (getmd->md_offset == 0);
2419
2420         CDEBUG(D_NET, "%s: Reply from %s md %p\n", 
2421                libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id), getmd);
2422
2423         lnet_commit_md (getmd, msg);
2424
2425         msg->msg_type = LNET_MSG_GET; /* flag this msg as an "optimized" GET */
2426
2427         msg->msg_ev.type = LNET_EVENT_REPLY;
2428         msg->msg_ev.initiator = peer_id;
2429         msg->msg_ev.sender = peer_id.nid;  /* optimized GETs can't be routed */
2430         msg->msg_ev.rlength = msg->msg_ev.mlength = getmd->md_length;
2431         msg->msg_ev.offset = 0;
2432
2433         lnet_md_deconstruct(getmd, &msg->msg_ev.md);
2434         lnet_md2handle(&msg->msg_ev.md_handle, getmd);
2435
2436         the_lnet.ln_counters.recv_count++;
2437         the_lnet.ln_counters.recv_length += getmd->md_length;
2438
2439         LNET_UNLOCK();
2440
2441         return msg;
2442
2443  drop_msg:
2444         lnet_msg_free(msg);
2445  drop:
2446         the_lnet.ln_counters.drop_count++;
2447         the_lnet.ln_counters.drop_length += getmd->md_length;
2448
2449         LNET_UNLOCK ();
2450
2451         return NULL;
2452 }
2453
2454 void
2455 lnet_set_reply_msg_len(lnet_ni_t *ni, lnet_msg_t *reply, unsigned int len)
2456 {
2457         /* Set the REPLY length, now the RDMA that elides the REPLY message has
2458          * completed and I know it. */
2459         LASSERT (reply != NULL);
2460         LASSERT (reply->msg_type == LNET_MSG_GET);
2461         LASSERT (reply->msg_ev.type == LNET_EVENT_REPLY);
2462
2463         /* NB I trusted my peer to RDMA.  If she tells me she's written beyond
2464          * the end of my buffer, I might as well be dead. */
2465         LASSERT (len <= reply->msg_ev.mlength);
2466
2467         reply->msg_ev.mlength = len;
2468 }
2469
2470 int
2471 LNetGet(lnet_nid_t self, lnet_handle_md_t mdh, 
2472         lnet_process_id_t target, unsigned int portal, 
2473         __u64 match_bits, unsigned int offset)
2474 {
2475         lnet_msg_t       *msg;
2476         lnet_libmd_t     *md;
2477         int               rc;
2478
2479         LASSERT (the_lnet.ln_init);
2480         LASSERT (the_lnet.ln_refcount > 0);
2481
2482         if (!list_empty (&the_lnet.ln_test_peers) && /* normally we don't */
2483             fail_peer (target.nid, 1))          /* shall we now? */
2484         {
2485                 CERROR("Dropping GET to %s: simulated failure\n",
2486                        libcfs_id2str(target));
2487                 return -EIO;
2488         }
2489
2490         msg = lnet_msg_alloc();
2491         if (msg == NULL) {
2492                 CERROR("Dropping GET to %s: ENOMEM on lnet_msg_t\n",
2493                        libcfs_id2str(target));
2494                 return -ENOMEM;
2495         }
2496
2497         LNET_LOCK();
2498
2499         md = lnet_handle2md(&mdh);
2500         if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
2501                 lnet_msg_free(msg);
2502
2503                 CERROR("Dropping GET ("LPU64":%d:%s): MD (%d) invalid\n",
2504                        match_bits, portal, libcfs_id2str(target),
2505                        md == NULL ? -1 : md->md_threshold);
2506                 if (md != NULL && md->md_me != NULL)
2507                         CERROR("REPLY MD also attached to portal %d\n",
2508                                md->md_me->me_portal);
2509
2510                 LNET_UNLOCK();
2511                 return -ENOENT;
2512         }
2513
2514         CDEBUG(D_NET, "LNetGet -> %s\n", libcfs_id2str(target));
2515
2516         lnet_commit_md(md, msg);
2517
2518         lnet_prep_send(msg, LNET_MSG_GET, target, 0, 0);
2519
2520         msg->msg_hdr.msg.get.match_bits = cpu_to_le64(match_bits);
2521         msg->msg_hdr.msg.get.ptl_index = cpu_to_le32(portal);
2522         msg->msg_hdr.msg.get.src_offset = cpu_to_le32(offset);
2523         msg->msg_hdr.msg.get.sink_length = cpu_to_le32(md->md_length);
2524
2525         /* NB handles only looked up by creator (no flips) */
2526         msg->msg_hdr.msg.get.return_wmd.wh_interface_cookie = 
2527                 the_lnet.ln_interface_cookie;
2528         msg->msg_hdr.msg.get.return_wmd.wh_object_cookie = 
2529                 md->md_lh.lh_cookie;
2530
2531         msg->msg_ev.type = LNET_EVENT_SEND;
2532         msg->msg_ev.initiator.nid = LNET_NID_ANY;
2533         msg->msg_ev.initiator.pid = the_lnet.ln_pid;
2534         msg->msg_ev.target = target;
2535         msg->msg_ev.sender = LNET_NID_ANY;
2536         msg->msg_ev.pt_index = portal;
2537         msg->msg_ev.match_bits = match_bits;
2538         msg->msg_ev.rlength = md->md_length;
2539         msg->msg_ev.mlength = md->md_length;
2540         msg->msg_ev.offset = offset;
2541         msg->msg_ev.hdr_data = 0;
2542
2543         lnet_md_deconstruct(md, &msg->msg_ev.md);
2544         lnet_md2handle(&msg->msg_ev.md_handle, md);
2545
2546         the_lnet.ln_counters.send_count++;
2547
2548         LNET_UNLOCK();
2549
2550         rc = lnet_send(self, msg);
2551         if (rc < 0) {
2552                 CERROR("error sending GET to %s: %d\n",
2553                        libcfs_id2str(target), rc);
2554                 lnet_finalize (NULL, msg, rc);
2555         }
2556
2557         /* completion will be signalled by an event */
2558         return 0;
2559 }
2560
2561 int
2562 LNetDist (lnet_nid_t dstnid, lnet_nid_t *srcnidp, __u32 *orderp)
2563 {
2564         struct list_head *e;
2565         lnet_ni_t        *ni;
2566         lnet_route_t     *route;
2567         lnet_remotenet_t *rnet;
2568         __u32             dstnet = LNET_NIDNET(dstnid);
2569         int               hops;
2570         __u32             order = 2;
2571
2572         /* if !local_nid_dist_zero, I don't return a distance of 0 ever
2573          * (when lustre sees a distance of 0, it substitutes 0@lo), so I
2574          * keep order 0 free for 0@lo and order 1 free for a local NID
2575          * match */
2576
2577         LASSERT (the_lnet.ln_init);
2578         LASSERT (the_lnet.ln_refcount > 0);
2579
2580         LNET_LOCK();
2581
2582         list_for_each (e, &the_lnet.ln_nis) {
2583                 ni = list_entry(e, lnet_ni_t, ni_list);
2584
2585                 if (ni->ni_nid == dstnid) {
2586                         if (srcnidp != NULL)
2587                                 *srcnidp = dstnid;
2588                         if (orderp != NULL) {
2589                                 if (LNET_NETTYP(LNET_NIDNET(dstnid)) == LOLND)
2590                                         *orderp = 0;
2591                                 else
2592                                         *orderp = 1;
2593                         }
2594                         LNET_UNLOCK();
2595
2596                         return local_nid_dist_zero ? 0 : 1;
2597                 }
2598
2599                 if (LNET_NIDNET(ni->ni_nid) == dstnet) {
2600                         if (srcnidp != NULL)
2601                                 *srcnidp = ni->ni_nid;
2602                         if (orderp != NULL)
2603                                 *orderp = order;
2604                         LNET_UNLOCK();
2605                         return 1;
2606                 }
2607
2608                 order++;
2609         }
2610
2611         list_for_each (e, &the_lnet.ln_remote_nets) {
2612                 rnet = list_entry(e, lnet_remotenet_t, lrn_list);
2613
2614                 if (rnet->lrn_net == dstnet) {
2615                         LASSERT (!list_empty(&rnet->lrn_routes));
2616                         route = list_entry(rnet->lrn_routes.next,
2617                                            lnet_route_t, lr_list);
2618                         hops = rnet->lrn_hops;
2619                         if (srcnidp != NULL)
2620                                 *srcnidp = route->lr_gateway->lp_ni->ni_nid;
2621                         if (orderp != NULL)
2622                                 *orderp = order;
2623                         LNET_UNLOCK();
2624                         return hops + 1;
2625                 }
2626                 order++;
2627         }
2628
2629         LNET_UNLOCK();
2630         return -EHOSTUNREACH;
2631 }
2632
2633 int
2634 LNetSetAsync(lnet_process_id_t id, int nasync)
2635 {
2636 #ifdef __KERNEL__
2637         return 0;
2638 #else
2639         lnet_ni_t        *ni;
2640         lnet_remotenet_t *rnet;
2641         struct list_head *tmp;
2642         lnet_route_t     *route;
2643         lnet_nid_t       *nids;
2644         int               nnids;
2645         int               maxnids = 256;
2646         int               rc = 0;
2647         int               rc2;
2648
2649         /* Target on a local network? */ 
2650
2651         ni = lnet_net2ni(LNET_NIDNET(id.nid));
2652         if (ni != NULL) {
2653                 if (ni->ni_lnd->lnd_setasync != NULL) 
2654                         rc = (ni->ni_lnd->lnd_setasync)(ni, id, nasync);
2655                 lnet_ni_decref(ni);
2656                 return rc;
2657         }
2658
2659         /* Target on a remote network: apply to routers */
2660  again:
2661         LIBCFS_ALLOC(nids, maxnids * sizeof(*nids));
2662         if (nids == NULL)
2663                 return -ENOMEM;
2664         nnids = 0;
2665
2666         /* Snapshot all the router NIDs */
2667         LNET_LOCK();
2668         rnet = lnet_find_net_locked(LNET_NIDNET(id.nid));
2669         if (rnet != NULL) {
2670                 list_for_each(tmp, &rnet->lrn_routes) {
2671                         if (nnids == maxnids) {
2672                                 LNET_UNLOCK();
2673                                 LIBCFS_FREE(nids, maxnids * sizeof(*nids));
2674                                 maxnids *= 2;
2675                                 goto again;
2676                         }
2677
2678                         route = list_entry(tmp, lnet_route_t, lr_list);
2679                         nids[nnids++] = route->lr_gateway->lp_nid;
2680                 }
2681         }
2682         LNET_UNLOCK();
2683
2684         /* set async on all the routers */
2685         while (nnids-- > 0) {
2686                 id.pid = LUSTRE_SRV_LNET_PID;
2687                 id.nid = nids[nnids];
2688
2689                 ni = lnet_net2ni(LNET_NIDNET(id.nid));
2690                 if (ni == NULL)
2691                         continue;
2692
2693                 if (ni->ni_lnd->lnd_setasync != NULL) {
2694                         rc2 = (ni->ni_lnd->lnd_setasync)(ni, id, nasync);
2695                         if (rc2 != 0)
2696                                 rc = rc2;
2697                 }
2698                 lnet_ni_decref(ni);
2699         }
2700
2701         LIBCFS_FREE(nids, maxnids * sizeof(*nids));
2702         return rc;
2703 #endif
2704 }