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