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