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