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