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