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