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