Whamcloud - gitweb
* Added loopback optimisation to lib-move.c
[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_PORTALS
26
27 #ifndef __KERNEL__
28 # include <stdio.h>
29 #else
30 # include <libcfs/kp30.h>
31 #endif
32 #include <portals/p30.h>
33 #include <portals/lib-p30.h>
34
35 /* forward ref */
36 static void lib_commit_md (lib_nal_t *nal, lib_md_t *md, lib_msg_t *msg);
37 static ptl_err_t do_lib_parse(lib_nal_t *nal, ptl_hdr_t *hdr, 
38                               void *private, int loopback);
39
40 static lib_md_t *
41 lib_match_md(lib_nal_t *nal, int index, int op_mask,
42              ptl_nid_t src_nid, ptl_pid_t src_pid,
43              ptl_size_t rlength, ptl_size_t roffset,
44              ptl_match_bits_t match_bits, lib_msg_t *msg,
45              ptl_size_t *mlength_out, ptl_size_t *offset_out)
46 {
47         lib_ni_t         *ni = &nal->libnal_ni;
48         struct list_head *match_list = &ni->ni_portals.tbl[index];
49         struct list_head *tmp;
50         lib_me_t         *me;
51         lib_md_t         *md;
52         ptl_size_t        mlength;
53         ptl_size_t        offset;
54         ENTRY;
55
56         CDEBUG (D_NET, "Request from "LPU64".%d of length %d into portal %d "
57                 "MB="LPX64"\n", src_nid, src_pid, rlength, index, match_bits);
58
59         if (index < 0 || index >= ni->ni_portals.size) {
60                 CERROR("Invalid portal %d not in [0-%d]\n",
61                        index, ni->ni_portals.size);
62                 goto failed;
63         }
64
65         list_for_each (tmp, match_list) {
66                 me = list_entry(tmp, lib_me_t, me_list);
67                 md = me->md;
68
69                  /* ME attached but MD not attached yet */
70                 if (md == NULL)
71                         continue;
72
73                 LASSERT (me == md->me);
74
75                 /* mismatched MD op */
76                 if ((md->options & op_mask) == 0)
77                         continue;
78
79                 /* MD exhausted */
80                 if (lib_md_exhausted(md))
81                         continue;
82
83                 /* mismatched ME nid/pid? */
84                 if (me->match_id.nid != PTL_NID_ANY &&
85                     me->match_id.nid != src_nid)
86                         continue;
87
88                 CDEBUG(D_NET, "match_id.pid [%x], src_pid [%x]\n",
89                        me->match_id.pid, src_pid);
90
91                 if (me->match_id.pid != PTL_PID_ANY &&
92                     me->match_id.pid != src_pid)
93                         continue;
94
95                 /* mismatched ME matchbits? */
96                 if (((me->match_bits ^ match_bits) & ~me->ignore_bits) != 0)
97                         continue;
98
99                 /* Hurrah! This _is_ a match; check it out... */
100
101                 if ((md->options & PTL_MD_MANAGE_REMOTE) == 0)
102                         offset = md->offset;
103                 else
104                         offset = roffset;
105
106                 if ((md->options & PTL_MD_MAX_SIZE) != 0) {
107                         mlength = md->max_size;
108                         LASSERT (md->offset + mlength <= md->length);
109                 } else {
110                         mlength = md->length - offset;
111                 }
112
113                 if (rlength <= mlength) {        /* fits in allowed space */
114                         mlength = rlength;
115                 } else if ((md->options & PTL_MD_TRUNCATE) == 0) {
116                         /* this packet _really_ is too big */
117                         CERROR("Matching packet %d too big: %d left, "
118                                "%d allowed\n", rlength, md->length - offset,
119                                mlength);
120                         goto failed;
121                 }
122
123                 /* Commit to this ME/MD */
124                 CDEBUG(D_NET, "Incoming %s index %x from "LPU64"/%u of "
125                        "length %d/%d into md "LPX64" [%d] + %d\n",
126                        (op_mask == PTL_MD_OP_PUT) ? "put" : "get",
127                        index, src_nid, src_pid, mlength, rlength,
128                        md->md_lh.lh_cookie, md->md_niov, offset);
129
130                 lib_commit_md(nal, md, msg);
131                 md->offset = offset + mlength;
132
133                 /* NB Caller sets ev.type and ev.hdr_data */
134                 msg->ev.initiator.nid = src_nid;
135                 msg->ev.initiator.pid = src_pid;
136                 msg->ev.pt_index = index;
137                 msg->ev.match_bits = match_bits;
138                 msg->ev.rlength = rlength;
139                 msg->ev.mlength = mlength;
140                 msg->ev.offset = offset;
141
142                 lib_md_deconstruct(nal, md, &msg->ev.md);
143                 ptl_md2handle(&msg->ev.md_handle, nal, md);
144
145                 *offset_out = offset;
146                 *mlength_out = mlength;
147
148                 /* Auto-unlink NOW, so the ME gets unlinked if required.
149                  * We bumped md->pending above so the MD just gets flagged
150                  * for unlink when it is finalized. */
151                 if ((md->md_flags & PTL_MD_FLAG_AUTO_UNLINK) != 0 &&
152                     lib_md_exhausted(md))
153                         lib_md_unlink(nal, md);
154
155                 RETURN (md);
156         }
157
158  failed:
159         CERROR (LPU64": Dropping %s from "LPU64".%d portal %d match "LPX64
160                 " offset %d length %d: no match\n",
161                 ni->ni_pid.nid, (op_mask == PTL_MD_OP_GET) ? "GET" : "PUT",
162                 src_nid, src_pid, index, match_bits, roffset, rlength);
163         RETURN(NULL);
164 }
165
166 int lib_api_fail_nid (nal_t *apinal, ptl_nid_t nid, unsigned int threshold)
167 {
168         lib_nal_t         *nal = apinal->nal_data;
169         lib_test_peer_t   *tp;
170         unsigned long      flags;
171         struct list_head  *el;
172         struct list_head  *next;
173         struct list_head   cull;
174
175         if (threshold != 0) {
176                 /* Adding a new entry */
177                 PORTAL_ALLOC(tp, sizeof(*tp));
178                 if (tp == NULL)
179                         return PTL_NO_SPACE;
180
181                 tp->tp_nid = nid;
182                 tp->tp_threshold = threshold;
183
184                 LIB_LOCK(nal, flags);
185                 list_add_tail (&tp->tp_list, &nal->libnal_ni.ni_test_peers);
186                 LIB_UNLOCK(nal, flags);
187                 return PTL_OK;
188         }
189
190         /* removing entries */
191         CFS_INIT_LIST_HEAD (&cull);
192
193         LIB_LOCK(nal, flags);
194
195         list_for_each_safe (el, next, &nal->libnal_ni.ni_test_peers) {
196                 tp = list_entry (el, lib_test_peer_t, tp_list);
197
198                 if (tp->tp_threshold == 0 ||    /* needs culling anyway */
199                     nid == PTL_NID_ANY ||       /* removing all entries */
200                     tp->tp_nid == nid)          /* matched this one */
201                 {
202                         list_del (&tp->tp_list);
203                         list_add (&tp->tp_list, &cull);
204                 }
205         }
206
207         LIB_UNLOCK(nal, flags);
208
209         while (!list_empty (&cull)) {
210                 tp = list_entry (cull.next, lib_test_peer_t, tp_list);
211
212                 list_del (&tp->tp_list);
213                 PORTAL_FREE(tp, sizeof (*tp));
214         }
215         return PTL_OK;
216 }
217
218 int 
219 lib_api_loopback (nal_t *apinal, int set, int *enabled)
220 {
221         lib_nal_t *nal = apinal->nal_data;
222
223         if (set)
224                 nal->libnal_ni.ni_loopback = *enabled;
225         else
226                 *enabled = nal->libnal_ni.ni_loopback;
227         
228         return PTL_OK;
229 }
230
231 static int
232 fail_peer (lib_nal_t *nal, ptl_nid_t nid, int outgoing)
233 {
234         lib_test_peer_t  *tp;
235         struct list_head *el;
236         struct list_head *next;
237         unsigned long     flags;
238         struct list_head  cull;
239         int               fail = 0;
240
241         CFS_INIT_LIST_HEAD (&cull);
242
243         LIB_LOCK (nal, flags);
244
245         list_for_each_safe (el, next, &nal->libnal_ni.ni_test_peers) {
246                 tp = list_entry (el, lib_test_peer_t, tp_list);
247
248                 if (tp->tp_threshold == 0) {
249                         /* zombie entry */
250                         if (outgoing) {
251                                 /* only cull zombies on outgoing tests,
252                                  * since we may be at interrupt priority on
253                                  * incoming messages. */
254                                 list_del (&tp->tp_list);
255                                 list_add (&tp->tp_list, &cull);
256                         }
257                         continue;
258                 }
259
260                 if (tp->tp_nid == PTL_NID_ANY || /* fail every peer */
261                     nid == tp->tp_nid) {        /* fail this peer */
262                         fail = 1;
263
264                         if (tp->tp_threshold != PTL_MD_THRESH_INF) {
265                                 tp->tp_threshold--;
266                                 if (outgoing &&
267                                     tp->tp_threshold == 0) {
268                                         /* see above */
269                                         list_del (&tp->tp_list);
270                                         list_add (&tp->tp_list, &cull);
271                                 }
272                         }
273                         break;
274                 }
275         }
276
277         LIB_UNLOCK (nal, flags);
278
279         while (!list_empty (&cull)) {
280                 tp = list_entry (cull.next, lib_test_peer_t, tp_list);
281                 list_del (&tp->tp_list);
282
283                 PORTAL_FREE(tp, sizeof (*tp));
284         }
285
286         return (fail);
287 }
288
289 ptl_size_t
290 lib_iov_nob (int niov, struct iovec *iov)
291 {
292         ptl_size_t nob = 0;
293
294         while (niov-- > 0)
295                 nob += (iov++)->iov_len;
296
297         return (nob);
298 }
299
300 void
301 lib_copy_iov2buf (char *dest, int niov, struct iovec *iov,
302                   ptl_size_t offset, ptl_size_t len)
303 {
304         ptl_size_t nob;
305
306         if (len == 0)
307                 return;
308
309         /* skip complete frags before 'offset' */
310         LASSERT (niov > 0);
311         while (offset >= iov->iov_len) {
312                 offset -= iov->iov_len;
313                 iov++;
314                 niov--;
315                 LASSERT (niov > 0);
316         }
317
318         do {
319                 LASSERT (niov > 0);
320                 nob = MIN (iov->iov_len - offset, len);
321                 memcpy (dest, iov->iov_base + offset, nob);
322
323                 len -= nob;
324                 dest += nob;
325                 niov--;
326                 iov++;
327                 offset = 0;
328         } while (len > 0);
329 }
330
331 void
332 lib_copy_buf2iov (int niov, struct iovec *iov, ptl_size_t offset,
333                   char *src, ptl_size_t len)
334 {
335         ptl_size_t nob;
336
337         if (len == 0)
338                 return;
339
340         /* skip complete frags before 'offset' */
341         LASSERT (niov > 0);
342         while (offset >= iov->iov_len) {
343                 offset -= iov->iov_len;
344                 iov++;
345                 niov--;
346                 LASSERT (niov > 0);
347         }
348
349         do {
350                 LASSERT (niov > 0);
351                 nob = MIN (iov->iov_len - offset, len);
352                 memcpy (iov->iov_base + offset, src, nob);
353
354                 len -= nob;
355                 src += nob;
356                 niov--;
357                 iov++;
358                 offset = 0;
359         } while (len > 0);
360 }
361
362 int
363 lib_extract_iov (int dst_niov, struct iovec *dst,
364                  int src_niov, struct iovec *src,
365                  ptl_size_t offset, ptl_size_t len)
366 {
367         /* Initialise 'dst' to the subset of 'src' starting at 'offset',
368          * for exactly 'len' bytes, and return the number of entries.
369          * NB not destructive to 'src' */
370         ptl_size_t      frag_len;
371         int             niov;
372
373         if (len == 0)                           /* no data => */
374                 return (0);                     /* no frags */
375
376         LASSERT (src_niov > 0);
377         while (offset >= src->iov_len) {      /* skip initial frags */
378                 offset -= src->iov_len;
379                 src_niov--;
380                 src++;
381                 LASSERT (src_niov > 0);
382         }
383
384         niov = 1;
385         for (;;) {
386                 LASSERT (src_niov > 0);
387                 LASSERT (niov <= dst_niov);
388
389                 frag_len = src->iov_len - offset;
390                 dst->iov_base = ((char *)src->iov_base) + offset;
391
392                 if (len <= frag_len) {
393                         dst->iov_len = len;
394                         return (niov);
395                 }
396
397                 dst->iov_len = frag_len;
398
399                 len -= frag_len;
400                 dst++;
401                 src++;
402                 niov++;
403                 src_niov--;
404                 offset = 0;
405         }
406 }
407
408 #ifndef __KERNEL__
409 ptl_size_t
410 lib_kiov_nob (int niov, ptl_kiov_t *kiov)
411 {
412         LASSERT (0);
413         return (0);
414 }
415
416 void
417 lib_copy_kiov2buf (char *dest, int niov, ptl_kiov_t *kiov,
418                    ptl_size_t offset, ptl_size_t len)
419 {
420         LASSERT (0);
421 }
422
423 void
424 lib_copy_buf2kiov (int niov, ptl_kiov_t *kiov, ptl_size_t offset,
425                    char *src, ptl_size_t len)
426 {
427         LASSERT (0);
428 }
429
430 int
431 lib_extract_kiov (int dst_niov, ptl_kiov_t *dst,
432                   int src_niov, ptl_kiov_t *src,
433                   ptl_size_t offset, ptl_size_t len)
434 {
435         LASSERT (0);
436 }
437
438 #else
439
440 ptl_size_t
441 lib_kiov_nob (int niov, ptl_kiov_t *kiov)
442 {
443         ptl_size_t  nob = 0;
444
445         while (niov-- > 0)
446                 nob += (kiov++)->kiov_len;
447
448         return (nob);
449 }
450
451 void
452 lib_copy_kiov2buf (char *dest, int niov, ptl_kiov_t *kiov,
453                    ptl_size_t offset, ptl_size_t len)
454 {
455         ptl_size_t  nob;
456         char       *addr;
457
458         if (len == 0)
459                 return;
460
461         LASSERT (!in_interrupt ());
462
463         LASSERT (niov > 0);
464         while (offset > kiov->kiov_len) {
465                 offset -= kiov->kiov_len;
466                 kiov++;
467                 niov--;
468                 LASSERT (niov > 0);
469         }
470
471         do{
472                 LASSERT (niov > 0);
473                 nob = MIN (kiov->kiov_len - offset, len);
474
475                 addr = ((char *)cfs_kmap (kiov->kiov_page)) + kiov->kiov_offset + offset;
476                 memcpy (dest, addr, nob);
477                 cfs_kunmap (kiov->kiov_page);
478
479                 len -= nob;
480                 dest += nob;
481                 niov--;
482                 kiov++;
483                 offset = 0;
484         } while (len > 0);
485 }
486
487 void
488 lib_copy_buf2kiov (int niov, ptl_kiov_t *kiov, ptl_size_t offset,
489                    char *src, ptl_size_t len)
490 {
491         ptl_size_t  nob;
492         char       *addr;
493
494         if (len == 0)
495                 return;
496
497         LASSERT (!in_interrupt ());
498
499         LASSERT (niov > 0);
500         while (offset >= kiov->kiov_len) {
501                 offset -= kiov->kiov_len;
502                 kiov++;
503                 niov--;
504                 LASSERT (niov > 0);
505         }
506
507         do {
508                 LASSERT (niov > 0);
509                 nob = MIN (kiov->kiov_len - offset, len);
510
511                 addr = ((char *)cfs_kmap (kiov->kiov_page)) + kiov->kiov_offset + offset;
512                 memcpy (addr, src, nob);
513                 cfs_kunmap (kiov->kiov_page);
514
515                 len -= nob;
516                 src += nob;
517                 niov--;
518                 kiov++;
519                 offset = 0;
520         } while (len > 0);
521 }
522
523 int
524 lib_extract_kiov (int dst_niov, ptl_kiov_t *dst,
525                   int src_niov, ptl_kiov_t *src,
526                   ptl_size_t offset, ptl_size_t len)
527 {
528         /* Initialise 'dst' to the subset of 'src' starting at 'offset',
529          * for exactly 'len' bytes, and return the number of entries.
530          * NB not destructive to 'src' */
531         ptl_size_t      frag_len;
532         int             niov;
533
534         if (len == 0)                           /* no data => */
535                 return (0);                     /* no frags */
536
537         LASSERT (src_niov > 0);
538         while (offset >= src->kiov_len) {      /* skip initial frags */
539                 offset -= src->kiov_len;
540                 src_niov--;
541                 src++;
542                 LASSERT (src_niov > 0);
543         }
544
545         niov = 1;
546         for (;;) {
547                 LASSERT (src_niov > 0);
548                 LASSERT (niov <= dst_niov);
549
550                 frag_len = src->kiov_len - offset;
551                 dst->kiov_page = src->kiov_page;
552                 dst->kiov_offset = src->kiov_offset + offset;
553
554                 if (len <= frag_len) {
555                         dst->kiov_len = len;
556                         LASSERT (dst->kiov_offset + dst->kiov_len <= PAGE_SIZE);
557                         return (niov);
558                 }
559
560                 dst->kiov_len = frag_len;
561                 LASSERT (dst->kiov_offset + dst->kiov_len <= PAGE_SIZE);
562
563                 len -= frag_len;
564                 dst++;
565                 src++;
566                 niov++;
567                 src_niov--;
568                 offset = 0;
569         }
570 }
571 #endif
572
573 ptl_err_t
574 lib_lo_rxiov(lib_nal_t    *nal,
575              void         *private,
576              lib_msg_t    *libmsg,
577              unsigned int  niov,
578              struct iovec *iov,
579              size_t        offset,
580              size_t        mlen,
581              size_t        rlen)
582 {
583         lo_desc_t *lod = (lo_desc_t *)private;
584
585         /* I only handle mapped->mapped matches */
586         LASSERT(lod->lod_type == LOD_IOV);
587         LASSERT(mlen > 0);
588
589         while (offset >= iov->iov_len) {
590                 offset -= iov->iov_len;
591                 iov++;
592                 niov--;
593                 LASSERT(niov > 0);
594         }
595         
596         while (lod->lod_offset >= lod->lod_iov.iov->iov_len) {
597                 lod->lod_offset -= lod->lod_iov.iov->iov_len;
598                 lod->lod_iov.iov++;
599                 lod->lod_niov--;
600                 LASSERT(lod->lod_niov > 0);
601         }
602         
603         do {
604                 int fraglen = MIN(iov->iov_len - offset,
605                                   lod->lod_iov.iov->iov_len - lod->lod_offset);
606
607                 LASSERT(niov > 0);
608                 LASSERT(lod->lod_niov > 0);
609
610                 if (fraglen > mlen)
611                         fraglen = mlen;
612                 
613                 memcpy((void *)((unsigned long)iov->iov_base + offset),
614                        (void *)((unsigned long)lod->lod_iov.iov->iov_base +
615                                 lod->lod_offset),
616                        fraglen);
617
618                 if (offset + fraglen < iov->iov_len) {
619                         offset += fraglen;
620                 } else {
621                         offset = 0;
622                         iov++;
623                         niov--;
624                 }
625
626                 if (lod->lod_offset + fraglen < lod->lod_iov.iov->iov_len ) {
627                         lod->lod_offset += fraglen;
628                 } else {
629                         lod->lod_offset = 0;
630                         lod->lod_iov.iov++;
631                         lod->lod_niov--;
632                 }
633
634                 mlen -= fraglen;
635         } while (mlen > 0);
636         
637         lib_finalize(nal, private, libmsg, PTL_OK);
638         return PTL_OK;
639 }
640
641 ptl_err_t
642 lib_lo_rxkiov(lib_nal_t    *nal,
643               void         *private,
644               lib_msg_t    *libmsg,
645               unsigned int  niov,
646               ptl_kiov_t   *kiov,
647               size_t        offset,
648               size_t        mlen,
649               size_t        rlen)
650 {
651         void          *srcaddr = NULL;
652         void          *dstaddr = NULL;
653         unsigned long  srcfrag = 0;
654         unsigned long  dstfrag = 0;
655         unsigned long  fraglen;
656         lo_desc_t     *lod = (lo_desc_t *)private;
657
658         /* I only handle unmapped->unmapped matches */
659         LASSERT(lod->lod_type == LOD_KIOV);
660
661         if (mlen == 0)
662                 return PTL_OK;
663
664         while (offset >= kiov->kiov_len) {
665                 offset -= kiov->kiov_len;
666                 kiov++;
667                 niov--;
668                 LASSERT(niov > 0);
669         }
670
671         while (lod->lod_offset >= lod->lod_iov.kiov->kiov_len) {
672                 lod->lod_offset -= lod->lod_iov.kiov->kiov_len;
673                 lod->lod_iov.kiov++;
674                 lod->lod_niov--;
675                 LASSERT(lod->lod_niov > 0);
676         }
677
678         do {
679                 /* CAVEAT EMPTOR: I kmap 2 pages at once == slight risk of deadlock */
680                 LASSERT(niov > 0);
681                 if (dstaddr == NULL) {
682                         dstaddr = (void *)((unsigned long)kmap(kiov->kiov_page) +
683                                            kiov->kiov_offset + offset);
684                         dstfrag = kiov->kiov_len -  offset;
685                 }
686
687                 LASSERT(lod->lod_niov > 0);
688                 if (srcaddr == NULL) {
689                         srcaddr = (void *)((unsigned long)kmap(lod->lod_iov.kiov->kiov_page) +
690                                            lod->lod_iov.kiov->kiov_offset + lod->lod_offset);
691                         srcfrag = lod->lod_iov.kiov->kiov_len - lod->lod_offset;
692                 }
693                 
694                 fraglen = MIN(srcfrag, dstfrag);
695                 if (fraglen > mlen)
696                         fraglen = mlen;
697                 
698                 memcpy(dstaddr, srcaddr, fraglen);
699                 
700                 if (fraglen < dstfrag) {
701                         dstfrag -= fraglen;
702                         dstaddr = (void *)((unsigned long)dstaddr + fraglen);
703                 } else {
704                         kunmap(kiov->kiov_page);
705                         dstaddr = NULL;
706                         offset = 0;
707                         kiov++;
708                         niov--;
709                 }
710
711                 if (fraglen < srcfrag) {
712                         srcfrag -= fraglen;
713                         srcaddr = (void *)((unsigned long)srcaddr + fraglen);
714                 } else {
715                         kunmap(lod->lod_iov.kiov->kiov_page);
716                         srcaddr = NULL;
717                         lod->lod_offset = 0;
718                         lod->lod_iov.kiov++;
719                         lod->lod_niov--;
720                 }
721
722                 mlen -= fraglen;
723         } while (mlen > 0);
724
725         if (dstaddr != NULL)
726                 kunmap(kiov->kiov_page);
727
728         if (srcaddr != NULL)
729                 kunmap(lod->lod_iov.kiov->kiov_page);
730
731         lib_finalize(nal, private, libmsg, PTL_OK);
732         return PTL_OK;
733 }
734
735 ptl_err_t
736 lib_lo_txiov (lib_nal_t    *nal,
737               void         *private,
738               lib_msg_t    *libmsg,
739               ptl_hdr_t    *hdr,
740               int           type,
741               ptl_nid_t     nid,
742               ptl_pid_t     pid,
743               unsigned int  payload_niov,
744               struct iovec *payload_iov,
745               size_t        payload_offset,
746               size_t        payload_nob)
747 {
748         lo_desc_t lod = {
749                 .lod_type    = LOD_IOV,
750                 .lod_niov    = payload_niov,
751                 .lod_offset  = payload_offset,
752                 .lod_nob     = payload_nob,
753                 .lod_iov     = { .iov = payload_iov } };
754         ptl_err_t rc;
755
756         rc = do_lib_parse(nal, hdr, &lod, 1);
757         if (rc == PTL_OK)
758                 lib_finalize(nal, private, libmsg, PTL_OK);
759         
760         return rc;
761 }
762
763 ptl_err_t
764 lib_lo_txkiov (lib_nal_t    *nal,
765                void         *private,
766                lib_msg_t    *libmsg,
767                ptl_hdr_t    *hdr,
768                int           type,
769                ptl_nid_t     nid,
770                ptl_pid_t     pid,
771                unsigned int  payload_niov,
772                ptl_kiov_t   *payload_kiov,
773                size_t        payload_offset,
774                size_t        payload_nob)
775 {
776         lo_desc_t lod = {
777                 .lod_type     = LOD_KIOV,
778                 .lod_niov     = payload_niov,
779                 .lod_offset   = payload_offset,
780                 .lod_nob      = payload_nob,
781                 .lod_iov      = { .kiov = payload_kiov } };
782         ptl_err_t   rc;
783
784         rc = do_lib_parse(nal, hdr, &lod, 1);
785         if (rc == PTL_OK)
786                 lib_finalize(nal, private, libmsg, PTL_OK);
787         
788         return rc;
789 }
790
791 ptl_err_t
792 lib_lo_recv (lib_nal_t *nal, void *private, lib_msg_t *msg, lib_md_t *md,
793              ptl_size_t offset, ptl_size_t mlen, ptl_size_t rlen)
794 {
795         if (mlen == 0) {
796                 lib_finalize(nal, private, msg, PTL_OK);
797                 return PTL_OK;
798         }
799         
800         if ((md->options & PTL_MD_KIOV) == 0)
801                 return lib_lo_rxiov(nal, private, msg,
802                                     md->md_niov, md->md_iov.iov,
803                                     offset, mlen, rlen);
804         
805         return lib_lo_rxkiov(nal, private, msg,
806                              md->md_niov, md->md_iov.kiov,
807                              offset, mlen, rlen);
808 }
809
810 ptl_err_t
811 lib_recv (lib_nal_t *nal, void *private, lib_msg_t *msg, lib_md_t *md,
812           ptl_size_t offset, ptl_size_t mlen, ptl_size_t rlen)
813 {
814         if (mlen == 0)
815                 return (nal->libnal_recv(nal, private, msg,
816                                          0, NULL,
817                                          offset, mlen, rlen));
818
819         if ((md->options & PTL_MD_KIOV) == 0)
820                 return (nal->libnal_recv(nal, private, msg,
821                                          md->md_niov, md->md_iov.iov,
822                                          offset, mlen, rlen));
823
824         return (nal->libnal_recv_pages(nal, private, msg,
825                                        md->md_niov, md->md_iov.kiov,
826                                        offset, mlen, rlen));
827 }
828
829 ptl_err_t
830 lib_send (lib_nal_t *nal, void *private, lib_msg_t *msg,
831           ptl_hdr_t *hdr, int type, ptl_nid_t nid, ptl_pid_t pid,
832           lib_md_t *md, ptl_size_t offset, ptl_size_t len)
833 {
834         int loopback = (nal->libnal_ni.ni_loopback &&
835                         (nid == nal->libnal_ni.ni_pid.nid));
836
837         if (len == 0) {
838                 if (loopback)
839                         return lib_lo_txiov(nal, private, msg,
840                                             hdr, type, nid, pid,
841                                             0, NULL,
842                                             offset, len);
843                 else
844                         return nal->libnal_send(nal, private, msg,
845                                                 hdr, type, nid, pid,
846                                                 0, NULL,
847                                                 offset, len);
848         }
849         
850         if ((md->options & PTL_MD_KIOV) == 0) {
851                 if (loopback)
852                         return lib_lo_txiov(nal, private, msg,
853                                             hdr, type, nid, pid,
854                                             md->md_niov, md->md_iov.iov,
855                                             offset, len);
856                 else
857                         return nal->libnal_send(nal, private, msg,
858                                                 hdr, type, nid, pid,
859                                                 md->md_niov, md->md_iov.iov,
860                                                 offset, len);
861         }
862         
863         if (loopback)
864                 return lib_lo_txkiov(nal, private, msg,
865                                      hdr, type, nid, pid,
866                                      md->md_niov, md->md_iov.kiov,
867                                      offset, len);
868         else
869                 return nal->libnal_send_pages(nal, private, msg,
870                                               hdr, type, nid, pid,
871                                               md->md_niov, md->md_iov.kiov,
872                                               offset, len);
873 }
874
875 static void
876 lib_commit_md (lib_nal_t *nal, lib_md_t *md, lib_msg_t *msg)
877 {
878         /* ALWAYS called holding the LIB_LOCK */
879         lib_counters_t *counters = &nal->libnal_ni.ni_counters;
880
881         /* Here, we commit the MD to a network OP by marking it busy and
882          * decrementing its threshold.  Come what may, the network "owns"
883          * the MD until a call to lib_finalize() signals completion. */
884         msg->md = md;
885
886         md->pending++;
887         if (md->threshold != PTL_MD_THRESH_INF) {
888                 LASSERT (md->threshold > 0);
889                 md->threshold--;
890         }
891
892         counters->msgs_alloc++;
893         if (counters->msgs_alloc > counters->msgs_max)
894                 counters->msgs_max = counters->msgs_alloc;
895
896         list_add (&msg->msg_list, &nal->libnal_ni.ni_active_msgs);
897 }
898
899 static void
900 lib_drop_message (lib_nal_t *nal, void *private, ptl_hdr_t *hdr, int loopback)
901 {
902         unsigned long flags;
903
904         /* CAVEAT EMPTOR: this only drops messages that we've not committed
905          * to receive (init_msg() not called) and therefore can't cause an
906          * event. */
907
908         LIB_LOCK(nal, flags);
909         nal->libnal_ni.ni_counters.drop_count++;
910         nal->libnal_ni.ni_counters.drop_length += hdr->payload_length;
911         LIB_UNLOCK(nal, flags);
912
913         /* NULL msg => if NAL calls lib_finalize it will be a noop */
914         if (!loopback)
915                 (void) lib_recv(nal, private, NULL, NULL, 0, 0,
916                                 hdr->payload_length);
917 }
918
919 /*
920  * Incoming messages have a ptl_msg_t object associated with them
921  * by the library.  This object encapsulates the state of the
922  * message and allows the NAL to do non-blocking receives or sends
923  * of long messages.
924  *
925  */
926 static ptl_err_t
927 parse_put(lib_nal_t *nal, ptl_hdr_t *hdr, void *private, 
928           lib_msg_t *msg, int loopback)
929 {
930         lib_ni_t        *ni = &nal->libnal_ni;
931         ptl_size_t       mlength = 0;
932         ptl_size_t       offset = 0;
933         ptl_err_t        rc;
934         lib_md_t        *md;
935         unsigned long    flags;
936
937         /* Convert put fields to host byte order */
938         hdr->msg.put.match_bits = le64_to_cpu(hdr->msg.put.match_bits);
939         hdr->msg.put.ptl_index = le32_to_cpu(hdr->msg.put.ptl_index);
940         hdr->msg.put.offset = le32_to_cpu(hdr->msg.put.offset);
941
942         LIB_LOCK(nal, flags);
943
944         md = lib_match_md(nal, hdr->msg.put.ptl_index, PTL_MD_OP_PUT,
945                           hdr->src_nid, hdr->src_pid,
946                           hdr->payload_length, hdr->msg.put.offset,
947                           hdr->msg.put.match_bits, msg,
948                           &mlength, &offset);
949         if (md == NULL) {
950                 LIB_UNLOCK(nal, flags);
951                 return (PTL_FAIL);
952         }
953
954         msg->ev.type = PTL_EVENT_PUT_END;
955         msg->ev.hdr_data = hdr->msg.put.hdr_data;
956
957         if (!ptl_is_wire_handle_none(&hdr->msg.put.ack_wmd) &&
958             !(md->options & PTL_MD_ACK_DISABLE)) {
959                 msg->ack_wmd = hdr->msg.put.ack_wmd;
960         }
961
962         ni->ni_counters.recv_count++;
963         ni->ni_counters.recv_length += mlength;
964
965         LIB_UNLOCK(nal, flags);
966
967         if (loopback)
968                 rc = lib_lo_recv(nal, private, msg, md, offset, mlength,
969                                  hdr->payload_length);
970         else
971                 rc = lib_recv(nal, private, msg, md, offset, mlength,
972                               hdr->payload_length);
973
974         if (rc != PTL_OK)
975                 CERROR(LPU64": error on receiving PUT from "LPU64": %d\n",
976                        ni->ni_pid.nid, hdr->src_nid, rc);
977
978         return (rc);
979 }
980
981 static ptl_err_t
982 parse_get(lib_nal_t *nal, ptl_hdr_t *hdr, void *private,
983           lib_msg_t *msg, int loopback)
984 {
985         lib_ni_t        *ni = &nal->libnal_ni;
986         ptl_size_t       mlength = 0;
987         ptl_size_t       offset = 0;
988         lib_md_t        *md;
989         ptl_hdr_t        reply;
990         unsigned long    flags;
991         int              rc;
992
993         /* Convert get fields to host byte order */
994         hdr->msg.get.match_bits = le64_to_cpu(hdr->msg.get.match_bits);
995         hdr->msg.get.ptl_index = le32_to_cpu(hdr->msg.get.ptl_index);
996         hdr->msg.get.sink_length = le32_to_cpu(hdr->msg.get.sink_length);
997         hdr->msg.get.src_offset = le32_to_cpu(hdr->msg.get.src_offset);
998
999         LIB_LOCK(nal, flags);
1000
1001         md = lib_match_md(nal, hdr->msg.get.ptl_index, PTL_MD_OP_GET,
1002                           hdr->src_nid, hdr->src_pid,
1003                           hdr->msg.get.sink_length, hdr->msg.get.src_offset,
1004                           hdr->msg.get.match_bits, msg,
1005                           &mlength, &offset);
1006         if (md == NULL) {
1007                 LIB_UNLOCK(nal, flags);
1008                 return (PTL_FAIL);
1009         }
1010
1011         msg->ev.type = PTL_EVENT_GET_END;
1012         msg->ev.hdr_data = 0;
1013
1014         ni->ni_counters.send_count++;
1015         ni->ni_counters.send_length += mlength;
1016
1017         LIB_UNLOCK(nal, flags);
1018
1019         memset (&reply, 0, sizeof (reply));
1020         reply.type     = cpu_to_le32(PTL_MSG_REPLY);
1021         reply.dest_nid = cpu_to_le64(hdr->src_nid);
1022         reply.dest_pid = cpu_to_le32(hdr->src_pid);
1023         reply.src_nid  = cpu_to_le64(ni->ni_pid.nid);
1024         reply.src_pid  = cpu_to_le32(ni->ni_pid.pid);
1025         reply.payload_length = cpu_to_le32(mlength);
1026
1027         reply.msg.reply.dst_wmd = hdr->msg.get.return_wmd;
1028
1029         /* NB call lib_send() _BEFORE_ lib_recv() completes the incoming
1030          * message.  Some NALs _require_ this to implement optimized GET */
1031
1032         rc = lib_send (nal, private, msg, &reply, PTL_MSG_REPLY,
1033                        hdr->src_nid, hdr->src_pid, md, offset, mlength);
1034         if (rc != PTL_OK)
1035                 CERROR(LPU64": Unable to send REPLY for GET from "LPU64": %d\n",
1036                        ni->ni_pid.nid, hdr->src_nid, rc);
1037
1038         /* Discard any junk after the hdr */
1039         if (!loopback)
1040                 (void) lib_recv(nal, private, NULL, NULL, 0, 0,
1041                                 hdr->payload_length);
1042
1043         return (rc);
1044 }
1045
1046 static ptl_err_t
1047 parse_reply(lib_nal_t *nal, ptl_hdr_t *hdr, void *private,
1048             lib_msg_t *msg, int loopback)
1049 {
1050         lib_ni_t        *ni = &nal->libnal_ni;
1051         lib_md_t        *md;
1052         int              rlength;
1053         int              length;
1054         unsigned long    flags;
1055         ptl_err_t        rc;
1056
1057         LIB_LOCK(nal, flags);
1058
1059         /* NB handles only looked up by creator (no flips) */
1060         md = ptl_wire_handle2md(&hdr->msg.reply.dst_wmd, nal);
1061         if (md == NULL || md->threshold == 0) {
1062                 CERROR (LPU64": Dropping REPLY from "LPU64" for %s MD "LPX64"."LPX64"\n",
1063                         ni->ni_pid.nid, hdr->src_nid,
1064                         md == NULL ? "invalid" : "inactive",
1065                         hdr->msg.reply.dst_wmd.wh_interface_cookie,
1066                         hdr->msg.reply.dst_wmd.wh_object_cookie);
1067
1068                 LIB_UNLOCK(nal, flags);
1069                 return (PTL_FAIL);
1070         }
1071
1072         LASSERT (md->offset == 0);
1073
1074         length = rlength = hdr->payload_length;
1075
1076         if (length > md->length) {
1077                 if ((md->options & PTL_MD_TRUNCATE) == 0) {
1078                         CERROR (LPU64": Dropping REPLY from "LPU64
1079                                 " length %d for MD "LPX64" would overflow (%d)\n",
1080                                 ni->ni_pid.nid, hdr->src_nid, length,
1081                                 hdr->msg.reply.dst_wmd.wh_object_cookie,
1082                                 md->length);
1083                         LIB_UNLOCK(nal, flags);
1084                         return (PTL_FAIL);
1085                 }
1086                 length = md->length;
1087         }
1088
1089         CDEBUG(D_NET, "Reply from "LPU64" of length %d/%d into md "LPX64"\n",
1090                hdr->src_nid, length, rlength,
1091                hdr->msg.reply.dst_wmd.wh_object_cookie);
1092
1093         lib_commit_md(nal, md, msg);
1094
1095         msg->ev.type = PTL_EVENT_REPLY_END;
1096         msg->ev.initiator.nid = hdr->src_nid;
1097         msg->ev.initiator.pid = hdr->src_pid;
1098         msg->ev.rlength = rlength;
1099         msg->ev.mlength = length;
1100         msg->ev.offset = 0;
1101
1102         lib_md_deconstruct(nal, md, &msg->ev.md);
1103         ptl_md2handle(&msg->ev.md_handle, nal, md);
1104
1105         ni->ni_counters.recv_count++;
1106         ni->ni_counters.recv_length += length;
1107
1108         LIB_UNLOCK(nal, flags);
1109
1110         if (loopback)
1111                 rc = lib_lo_recv(nal, private, msg, md, 0, length, rlength);
1112         else
1113                 rc = lib_recv(nal, private, msg, md, 0, length, rlength);
1114
1115         if (rc != PTL_OK)
1116                 CERROR(LPU64": error on receiving REPLY from "LPU64": %d\n",
1117                        ni->ni_pid.nid, hdr->src_nid, rc);
1118
1119         return (rc);
1120 }
1121
1122 static ptl_err_t
1123 parse_ack(lib_nal_t *nal, ptl_hdr_t *hdr, void *private, 
1124           lib_msg_t *msg, int loopback)
1125 {
1126         lib_ni_t      *ni = &nal->libnal_ni;
1127         lib_md_t      *md;
1128         unsigned long  flags;
1129
1130         /* Convert ack fields to host byte order */
1131         hdr->msg.ack.match_bits = le64_to_cpu(hdr->msg.ack.match_bits);
1132         hdr->msg.ack.mlength = le32_to_cpu(hdr->msg.ack.mlength);
1133
1134         LIB_LOCK(nal, flags);
1135
1136         /* NB handles only looked up by creator (no flips) */
1137         md = ptl_wire_handle2md(&hdr->msg.ack.dst_wmd, nal);
1138         if (md == NULL || md->threshold == 0) {
1139                 CDEBUG(D_INFO, LPU64": Dropping ACK from "LPU64" to %s MD "
1140                        LPX64"."LPX64"\n", ni->ni_pid.nid, hdr->src_nid,
1141                        (md == NULL) ? "invalid" : "inactive",
1142                        hdr->msg.ack.dst_wmd.wh_interface_cookie,
1143                        hdr->msg.ack.dst_wmd.wh_object_cookie);
1144
1145                 LIB_UNLOCK(nal, flags);
1146                 return (PTL_FAIL);
1147         }
1148
1149         CDEBUG(D_NET, LPU64": ACK from "LPU64" into md "LPX64"\n",
1150                ni->ni_pid.nid, hdr->src_nid,
1151                hdr->msg.ack.dst_wmd.wh_object_cookie);
1152
1153         lib_commit_md(nal, md, msg);
1154
1155         msg->ev.type = PTL_EVENT_ACK;
1156         msg->ev.initiator.nid = hdr->src_nid;
1157         msg->ev.initiator.pid = hdr->src_pid;
1158         msg->ev.mlength = hdr->msg.ack.mlength;
1159         msg->ev.match_bits = hdr->msg.ack.match_bits;
1160
1161         lib_md_deconstruct(nal, md, &msg->ev.md);
1162         ptl_md2handle(&msg->ev.md_handle, nal, md);
1163
1164         ni->ni_counters.recv_count++;
1165
1166         LIB_UNLOCK(nal, flags);
1167
1168         /* We have received and matched up the ack OK, create the
1169          * completion event now... */
1170         lib_finalize(nal, private, msg, PTL_OK);
1171
1172         /* ...and now discard any junk after the hdr */
1173         if (!loopback)
1174                 (void) lib_recv(nal, private, NULL, NULL, 0, 0,
1175                                 hdr->payload_length);
1176
1177        return (PTL_OK);
1178 }
1179
1180 static char *
1181 hdr_type_string (ptl_hdr_t *hdr)
1182 {
1183         switch (hdr->type) {
1184         case PTL_MSG_ACK:
1185                 return ("ACK");
1186         case PTL_MSG_PUT:
1187                 return ("PUT");
1188         case PTL_MSG_GET:
1189                 return ("GET");
1190         case PTL_MSG_REPLY:
1191                 return ("REPLY");
1192         case PTL_MSG_HELLO:
1193                 return ("HELLO");
1194         default:
1195                 return ("<UNKNOWN>");
1196         }
1197 }
1198
1199 void print_hdr(lib_nal_t *nal, ptl_hdr_t * hdr)
1200 {
1201         char *type_str = hdr_type_string (hdr);
1202
1203         CWARN("P3 Header at %p of type %s\n", hdr, type_str);
1204         CWARN("    From nid/pid "LPX64"/%u", hdr->src_nid, hdr->src_pid);
1205         CWARN("    To nid/pid "LPX64"/%u\n", hdr->dest_nid, hdr->dest_pid);
1206
1207         switch (hdr->type) {
1208         default:
1209                 break;
1210
1211         case PTL_MSG_PUT:
1212                 CWARN("    Ptl index %d, ack md "LPX64"."LPX64", "
1213                       "match bits "LPX64"\n",
1214                       hdr->msg.put.ptl_index,
1215                       hdr->msg.put.ack_wmd.wh_interface_cookie,
1216                       hdr->msg.put.ack_wmd.wh_object_cookie,
1217                       hdr->msg.put.match_bits);
1218                 CWARN("    Length %d, offset %d, hdr data "LPX64"\n",
1219                       hdr->payload_length, hdr->msg.put.offset,
1220                       hdr->msg.put.hdr_data);
1221                 break;
1222
1223         case PTL_MSG_GET:
1224                 CWARN("    Ptl index %d, return md "LPX64"."LPX64", "
1225                       "match bits "LPX64"\n", hdr->msg.get.ptl_index,
1226                       hdr->msg.get.return_wmd.wh_interface_cookie,
1227                       hdr->msg.get.return_wmd.wh_object_cookie,
1228                       hdr->msg.get.match_bits);
1229                 CWARN("    Length %d, src offset %d\n",
1230                       hdr->msg.get.sink_length,
1231                       hdr->msg.get.src_offset);
1232                 break;
1233
1234         case PTL_MSG_ACK:
1235                 CWARN("    dst md "LPX64"."LPX64", "
1236                       "manipulated length %d\n",
1237                       hdr->msg.ack.dst_wmd.wh_interface_cookie,
1238                       hdr->msg.ack.dst_wmd.wh_object_cookie,
1239                       hdr->msg.ack.mlength);
1240                 break;
1241
1242         case PTL_MSG_REPLY:
1243                 CWARN("    dst md "LPX64"."LPX64", "
1244                       "length %d\n",
1245                       hdr->msg.reply.dst_wmd.wh_interface_cookie,
1246                       hdr->msg.reply.dst_wmd.wh_object_cookie,
1247                       hdr->payload_length);
1248         }
1249
1250 }                               /* end of print_hdr() */
1251
1252
1253 ptl_err_t
1254 lib_parse(lib_nal_t *nal, ptl_hdr_t *hdr, void *private)
1255 {
1256         return do_lib_parse(nal, hdr, private, 0);
1257 }
1258
1259 ptl_err_t
1260 do_lib_parse(lib_nal_t *nal, ptl_hdr_t *hdr, void *private, int loopback)
1261 {
1262         unsigned long  flags;
1263         ptl_err_t      rc;
1264         lib_msg_t     *msg;
1265
1266         /* NB we return PTL_OK if we manage to parse the header and believe
1267          * it looks OK.  Anything that goes wrong with receiving the
1268          * message after that point is the responsibility of the NAL */
1269
1270         /* convert common fields to host byte order */
1271         hdr->type = le32_to_cpu(hdr->type);
1272         hdr->src_nid = le64_to_cpu(hdr->src_nid);
1273         hdr->src_pid = le32_to_cpu(hdr->src_pid);
1274         hdr->dest_pid = le32_to_cpu(hdr->dest_pid);
1275         hdr->payload_length = le32_to_cpu(hdr->payload_length);
1276
1277         switch (hdr->type) {
1278         case PTL_MSG_HELLO: {
1279                 /* dest_nid is really ptl_magicversion_t */
1280                 ptl_magicversion_t *mv = (ptl_magicversion_t *)&hdr->dest_nid;
1281
1282                 mv->magic = le32_to_cpu(mv->magic);
1283                 mv->version_major = le16_to_cpu(mv->version_major);
1284                 mv->version_minor = le16_to_cpu(mv->version_minor);
1285
1286                 if (mv->magic == PORTALS_PROTO_MAGIC &&
1287                     mv->version_major == PORTALS_PROTO_VERSION_MAJOR &&
1288                     mv->version_minor == PORTALS_PROTO_VERSION_MINOR) {
1289                         CWARN (LPU64": Dropping unexpected HELLO message: "
1290                                "magic %d, version %d.%d from "LPD64"\n",
1291                                nal->libnal_ni.ni_pid.nid, mv->magic,
1292                                mv->version_major, mv->version_minor,
1293                                hdr->src_nid);
1294
1295                         /* it's good but we don't want it */
1296                         lib_drop_message(nal, private, hdr, loopback);
1297                         return PTL_OK;
1298                 }
1299
1300                 /* we got garbage */
1301                 CERROR (LPU64": Bad HELLO message: "
1302                         "magic %d, version %d.%d from "LPD64"\n",
1303                         nal->libnal_ni.ni_pid.nid, mv->magic,
1304                         mv->version_major, mv->version_minor,
1305                         hdr->src_nid);
1306                 return PTL_FAIL;
1307         }
1308
1309         case PTL_MSG_ACK:
1310         case PTL_MSG_PUT:
1311         case PTL_MSG_GET:
1312         case PTL_MSG_REPLY:
1313                 hdr->dest_nid = le64_to_cpu(hdr->dest_nid);
1314                 if (hdr->dest_nid != nal->libnal_ni.ni_pid.nid) {
1315                         CERROR(LPU64": BAD dest NID in %s message from"
1316                                LPU64" to "LPU64" (not me)\n",
1317                                nal->libnal_ni.ni_pid.nid, hdr_type_string (hdr),
1318                                hdr->src_nid, hdr->dest_nid);
1319                         return PTL_FAIL;
1320                 }
1321                 break;
1322
1323         default:
1324                 CERROR(LPU64": Bad message type 0x%x from "LPU64"\n",
1325                        nal->libnal_ni.ni_pid.nid, hdr->type, hdr->src_nid);
1326                 return PTL_FAIL;
1327         }
1328
1329         /* We've decided we're not receiving garbage since we can parse the
1330          * header.  We will return PTL_OK come what may... */
1331
1332         if (!list_empty (&nal->libnal_ni.ni_test_peers) && /* normally we don't */
1333             fail_peer (nal, hdr->src_nid, 0))      /* shall we now? */
1334         {
1335                 CERROR(LPU64": Dropping incoming %s from "LPU64
1336                        ": simulated failure\n",
1337                        nal->libnal_ni.ni_pid.nid, hdr_type_string (hdr),
1338                        hdr->src_nid);
1339                 lib_drop_message(nal, private, hdr, loopback);
1340                 return PTL_OK;
1341         }
1342
1343         msg = lib_msg_alloc(nal);
1344         if (msg == NULL) {
1345                 CERROR(LPU64": Dropping incoming %s from "LPU64
1346                        ": can't allocate a lib_msg_t\n",
1347                        nal->libnal_ni.ni_pid.nid, hdr_type_string (hdr),
1348                        hdr->src_nid);
1349                 lib_drop_message(nal, private, hdr, loopback);
1350                 return PTL_OK;
1351         }
1352
1353         switch (hdr->type) {
1354         case PTL_MSG_ACK:
1355                 rc = parse_ack(nal, hdr, private, msg, loopback);
1356                 break;
1357         case PTL_MSG_PUT:
1358                 rc = parse_put(nal, hdr, private, msg, loopback);
1359                 break;
1360         case PTL_MSG_GET:
1361                 rc = parse_get(nal, hdr, private, msg, loopback);
1362                 break;
1363         case PTL_MSG_REPLY:
1364                 rc = parse_reply(nal, hdr, private, msg, loopback);
1365                 break;
1366         default:
1367                 LASSERT(0);
1368                 rc = PTL_FAIL;                  /* no compiler warning please */
1369                 break;
1370         }
1371
1372         if (rc != PTL_OK) {
1373                 if (msg->md != NULL) {
1374                         /* committed... */
1375                         lib_finalize(nal, private, msg, rc);
1376                 } else {
1377                         LIB_LOCK(nal, flags);
1378                         lib_msg_free(nal, msg); /* expects LIB_LOCK held */
1379                         LIB_UNLOCK(nal, flags);
1380
1381                         lib_drop_message(nal, private, hdr, loopback);
1382                 }
1383         }
1384
1385         return PTL_OK;
1386         /* That's "OK I can parse it", not "OK I like it" :) */
1387 }
1388
1389 int
1390 lib_api_put(nal_t *apinal, ptl_handle_md_t *mdh,
1391             ptl_ack_req_t ack, ptl_process_id_t *id,
1392             ptl_pt_index_t portal, ptl_ac_index_t ac,
1393             ptl_match_bits_t match_bits,
1394             ptl_size_t offset, ptl_hdr_data_t hdr_data)
1395 {
1396         lib_nal_t        *nal = apinal->nal_data;
1397         lib_ni_t         *ni = &nal->libnal_ni;
1398         lib_msg_t        *msg;
1399         ptl_hdr_t         hdr;
1400         lib_md_t         *md;
1401         unsigned long     flags;
1402         int               rc;
1403
1404         if (!list_empty (&ni->ni_test_peers) && /* normally we don't */
1405             fail_peer (nal, id->nid, 1))           /* shall we now? */
1406         {
1407                 CERROR("Dropping PUT to "LPU64": simulated failure\n",
1408                        id->nid);
1409                 return PTL_PROCESS_INVALID;
1410         }
1411
1412         msg = lib_msg_alloc(nal);
1413         if (msg == NULL) {
1414                 CERROR(LPU64": Dropping PUT to "LPU64": ENOMEM on lib_msg_t\n",
1415                        ni->ni_pid.nid, id->nid);
1416                 return PTL_NO_SPACE;
1417         }
1418
1419         LIB_LOCK(nal, flags);
1420
1421         md = ptl_handle2md(mdh, nal);
1422         if (md == NULL || md->threshold == 0) {
1423                 lib_msg_free(nal, msg);
1424                 LIB_UNLOCK(nal, flags);
1425
1426                 return PTL_MD_INVALID;
1427         }
1428
1429         CDEBUG(D_NET, "PtlPut -> "LPX64"\n", id->nid);
1430
1431         memset (&hdr, 0, sizeof (hdr));
1432         hdr.type     = cpu_to_le32(PTL_MSG_PUT);
1433         hdr.dest_nid = cpu_to_le64(id->nid);
1434         hdr.dest_pid = cpu_to_le32(id->pid);
1435         hdr.src_nid  = cpu_to_le64(ni->ni_pid.nid);
1436         hdr.src_pid  = cpu_to_le32(ni->ni_pid.pid);
1437         hdr.payload_length = cpu_to_le32(md->length);
1438
1439         /* NB handles only looked up by creator (no flips) */
1440         if (ack == PTL_ACK_REQ) {
1441                 hdr.msg.put.ack_wmd.wh_interface_cookie = ni->ni_interface_cookie;
1442                 hdr.msg.put.ack_wmd.wh_object_cookie = md->md_lh.lh_cookie;
1443         } else {
1444                 hdr.msg.put.ack_wmd = PTL_WIRE_HANDLE_NONE;
1445         }
1446
1447         hdr.msg.put.match_bits = cpu_to_le64(match_bits);
1448         hdr.msg.put.ptl_index = cpu_to_le32(portal);
1449         hdr.msg.put.offset = cpu_to_le32(offset);
1450         hdr.msg.put.hdr_data = hdr_data;
1451
1452         lib_commit_md(nal, md, msg);
1453
1454         msg->ev.type = PTL_EVENT_SEND_END;
1455         msg->ev.initiator.nid = ni->ni_pid.nid;
1456         msg->ev.initiator.pid = ni->ni_pid.pid;
1457         msg->ev.pt_index = portal;
1458         msg->ev.match_bits = match_bits;
1459         msg->ev.rlength = md->length;
1460         msg->ev.mlength = md->length;
1461         msg->ev.offset = offset;
1462         msg->ev.hdr_data = hdr_data;
1463
1464         lib_md_deconstruct(nal, md, &msg->ev.md);
1465         ptl_md2handle(&msg->ev.md_handle, nal, md);
1466
1467         ni->ni_counters.send_count++;
1468         ni->ni_counters.send_length += md->length;
1469
1470         LIB_UNLOCK(nal, flags);
1471
1472         rc = lib_send (nal, NULL, msg, &hdr, PTL_MSG_PUT,
1473                        id->nid, id->pid, md, 0, md->length);
1474         if (rc != PTL_OK) {
1475                 CERROR("Error sending PUT to "LPX64": %d\n",
1476                        id->nid, rc);
1477                 lib_finalize (nal, NULL, msg, rc);
1478         }
1479
1480         /* completion will be signalled by an event */
1481         return PTL_OK;
1482 }
1483
1484 lib_msg_t *
1485 lib_create_reply_msg (lib_nal_t *nal, ptl_nid_t peer_nid, lib_msg_t *getmsg)
1486 {
1487         /* The NAL can DMA direct to the GET md (i.e. no REPLY msg).  This
1488          * returns a msg for the NAL to pass to lib_finalize() when the sink
1489          * data has been received.
1490          *
1491          * CAVEAT EMPTOR: 'getmsg' is the original GET, which is freed when
1492          * lib_finalize() is called on it, so the NAL must call this first */
1493
1494         lib_ni_t        *ni = &nal->libnal_ni;
1495         lib_msg_t       *msg = lib_msg_alloc(nal);
1496         lib_md_t        *getmd = getmsg->md;
1497         unsigned long    flags;
1498
1499         LIB_LOCK(nal, flags);
1500
1501         LASSERT (getmd->pending > 0);
1502
1503         if (msg == NULL) {
1504                 CERROR ("Dropping REPLY from "LPU64": can't allocate msg\n",
1505                         peer_nid);
1506                 goto drop;
1507         }
1508
1509         if (getmd->threshold == 0) {
1510                 CERROR ("Dropping REPLY from "LPU64" for inactive MD %p\n",
1511                         peer_nid, getmd);
1512                 goto drop_msg;
1513         }
1514
1515         LASSERT (getmd->offset == 0);
1516
1517         CDEBUG(D_NET, "Reply from "LPU64" md %p\n", peer_nid, getmd);
1518
1519         lib_commit_md (nal, getmd, msg);
1520
1521         msg->ev.type = PTL_EVENT_REPLY_END;
1522         msg->ev.initiator.nid = peer_nid;
1523         msg->ev.initiator.pid = 0;      /* XXX FIXME!!! */
1524         msg->ev.rlength = msg->ev.mlength = getmd->length;
1525         msg->ev.offset = 0;
1526
1527         lib_md_deconstruct(nal, getmd, &msg->ev.md);
1528         ptl_md2handle(&msg->ev.md_handle, nal, getmd);
1529
1530         ni->ni_counters.recv_count++;
1531         ni->ni_counters.recv_length += getmd->length;
1532
1533         LIB_UNLOCK(nal, flags);
1534
1535         return msg;
1536
1537  drop_msg:
1538         lib_msg_free(nal, msg);
1539  drop:
1540         nal->libnal_ni.ni_counters.drop_count++;
1541         nal->libnal_ni.ni_counters.drop_length += getmd->length;
1542
1543         LIB_UNLOCK (nal, flags);
1544
1545         return NULL;
1546 }
1547
1548 int
1549 lib_api_get(nal_t *apinal, ptl_handle_md_t *mdh, ptl_process_id_t *id,
1550             ptl_pt_index_t portal, ptl_ac_index_t ac,
1551             ptl_match_bits_t match_bits, ptl_size_t offset)
1552 {
1553         lib_nal_t        *nal = apinal->nal_data;
1554         lib_ni_t         *ni = &nal->libnal_ni;
1555         lib_msg_t        *msg;
1556         ptl_hdr_t         hdr;
1557         lib_md_t         *md;
1558         unsigned long     flags;
1559         int               rc;
1560
1561         if (!list_empty (&ni->ni_test_peers) && /* normally we don't */
1562             fail_peer (nal, id->nid, 1))           /* shall we now? */
1563         {
1564                 CERROR("Dropping PUT to "LPX64": simulated failure\n",
1565                        id->nid);
1566                 return PTL_PROCESS_INVALID;
1567         }
1568
1569         msg = lib_msg_alloc(nal);
1570         if (msg == NULL) {
1571                 CERROR("Dropping GET to "LPU64": ENOMEM on lib_msg_t\n",
1572                        id->nid);
1573                 return PTL_NO_SPACE;
1574         }
1575
1576         LIB_LOCK(nal, flags);
1577
1578         md = ptl_handle2md(mdh, nal);
1579         if (md == NULL || !md->threshold) {
1580                 lib_msg_free(nal, msg);
1581                 LIB_UNLOCK(nal, flags);
1582
1583                 return PTL_MD_INVALID;
1584         }
1585
1586         CDEBUG(D_NET, "PtlGet -> %Lu: %lu\n", (unsigned long long)id->nid,
1587                (unsigned long)id->pid);
1588
1589         memset (&hdr, 0, sizeof (hdr));
1590         hdr.type     = cpu_to_le32(PTL_MSG_GET);
1591         hdr.dest_nid = cpu_to_le64(id->nid);
1592         hdr.dest_pid = cpu_to_le32(id->pid);
1593         hdr.src_nid  = cpu_to_le64(ni->ni_pid.nid);
1594         hdr.src_pid  = cpu_to_le32(ni->ni_pid.pid);
1595         hdr.payload_length = 0;
1596
1597         /* NB handles only looked up by creator (no flips) */
1598         hdr.msg.get.return_wmd.wh_interface_cookie = ni->ni_interface_cookie;
1599         hdr.msg.get.return_wmd.wh_object_cookie = md->md_lh.lh_cookie;
1600
1601         hdr.msg.get.match_bits = cpu_to_le64(match_bits);
1602         hdr.msg.get.ptl_index = cpu_to_le32(portal);
1603         hdr.msg.get.src_offset = cpu_to_le32(offset);
1604         hdr.msg.get.sink_length = cpu_to_le32(md->length);
1605
1606         lib_commit_md(nal, md, msg);
1607
1608         msg->ev.type = PTL_EVENT_SEND_END;
1609         msg->ev.initiator = ni->ni_pid;
1610         msg->ev.pt_index = portal;
1611         msg->ev.match_bits = match_bits;
1612         msg->ev.rlength = md->length;
1613         msg->ev.mlength = md->length;
1614         msg->ev.offset = offset;
1615         msg->ev.hdr_data = 0;
1616
1617         lib_md_deconstruct(nal, md, &msg->ev.md);
1618         ptl_md2handle(&msg->ev.md_handle, nal, md);
1619
1620         ni->ni_counters.send_count++;
1621
1622         LIB_UNLOCK(nal, flags);
1623
1624         rc = lib_send (nal, NULL, msg, &hdr, PTL_MSG_GET,
1625                        id->nid, id->pid, NULL, 0, 0);
1626         if (rc != PTL_OK) {
1627                 CERROR(LPU64": error sending GET to "LPU64": %d\n",
1628                        ni->ni_pid.nid, id->nid, rc);
1629                 lib_finalize (nal, NULL, msg, rc);
1630         }
1631
1632         /* completion will be signalled by an event */
1633         return PTL_OK;
1634 }
1635
1636 void lib_assert_wire_constants (void)
1637 {
1638         /* Wire protocol assertions generated by 'wirecheck'
1639          * running on Linux mdevi 2.4.21-p4smp-55chaos #1 SMP Tue Jun 8 14:38:44 PDT 2004 i686 i686 i
1640          * with gcc version 3.2.3 20030502 (Red Hat Linux 3.2.3-34) */
1641
1642
1643         /* Constants... */
1644         LASSERT (PORTALS_PROTO_MAGIC == 0xeebc0ded);
1645         LASSERT (PORTALS_PROTO_VERSION_MAJOR == 1);
1646         LASSERT (PORTALS_PROTO_VERSION_MINOR == 0);
1647         LASSERT (PTL_MSG_ACK == 0);
1648         LASSERT (PTL_MSG_PUT == 1);
1649         LASSERT (PTL_MSG_GET == 2);
1650         LASSERT (PTL_MSG_REPLY == 3);
1651         LASSERT (PTL_MSG_HELLO == 4);
1652
1653         /* Checks for struct ptl_handle_wire_t */
1654         LASSERT ((int)sizeof(ptl_handle_wire_t) == 16);
1655         LASSERT ((int)offsetof(ptl_handle_wire_t, wh_interface_cookie) == 0);
1656         LASSERT ((int)sizeof(((ptl_handle_wire_t *)0)->wh_interface_cookie) == 8);
1657         LASSERT ((int)offsetof(ptl_handle_wire_t, wh_object_cookie) == 8);
1658         LASSERT ((int)sizeof(((ptl_handle_wire_t *)0)->wh_object_cookie) == 8);
1659
1660         /* Checks for struct ptl_magicversion_t */
1661         LASSERT ((int)sizeof(ptl_magicversion_t) == 8);
1662         LASSERT ((int)offsetof(ptl_magicversion_t, magic) == 0);
1663         LASSERT ((int)sizeof(((ptl_magicversion_t *)0)->magic) == 4);
1664         LASSERT ((int)offsetof(ptl_magicversion_t, version_major) == 4);
1665         LASSERT ((int)sizeof(((ptl_magicversion_t *)0)->version_major) == 2);
1666         LASSERT ((int)offsetof(ptl_magicversion_t, version_minor) == 6);
1667         LASSERT ((int)sizeof(((ptl_magicversion_t *)0)->version_minor) == 2);
1668
1669         /* Checks for struct ptl_hdr_t */
1670         LASSERT ((int)sizeof(ptl_hdr_t) == 72);
1671         LASSERT ((int)offsetof(ptl_hdr_t, dest_nid) == 0);
1672         LASSERT ((int)sizeof(((ptl_hdr_t *)0)->dest_nid) == 8);
1673         LASSERT ((int)offsetof(ptl_hdr_t, src_nid) == 8);
1674         LASSERT ((int)sizeof(((ptl_hdr_t *)0)->src_nid) == 8);
1675         LASSERT ((int)offsetof(ptl_hdr_t, dest_pid) == 16);
1676         LASSERT ((int)sizeof(((ptl_hdr_t *)0)->dest_pid) == 4);
1677         LASSERT ((int)offsetof(ptl_hdr_t, src_pid) == 20);
1678         LASSERT ((int)sizeof(((ptl_hdr_t *)0)->src_pid) == 4);
1679         LASSERT ((int)offsetof(ptl_hdr_t, type) == 24);
1680         LASSERT ((int)sizeof(((ptl_hdr_t *)0)->type) == 4);
1681         LASSERT ((int)offsetof(ptl_hdr_t, payload_length) == 28);
1682         LASSERT ((int)sizeof(((ptl_hdr_t *)0)->payload_length) == 4);
1683         LASSERT ((int)offsetof(ptl_hdr_t, msg) == 32);
1684         LASSERT ((int)sizeof(((ptl_hdr_t *)0)->msg) == 40);
1685
1686         /* Ack */
1687         LASSERT ((int)offsetof(ptl_hdr_t, msg.ack.dst_wmd) == 32);
1688         LASSERT ((int)sizeof(((ptl_hdr_t *)0)->msg.ack.dst_wmd) == 16);
1689         LASSERT ((int)offsetof(ptl_hdr_t, msg.ack.match_bits) == 48);
1690         LASSERT ((int)sizeof(((ptl_hdr_t *)0)->msg.ack.match_bits) == 8);
1691         LASSERT ((int)offsetof(ptl_hdr_t, msg.ack.mlength) == 56);
1692         LASSERT ((int)sizeof(((ptl_hdr_t *)0)->msg.ack.mlength) == 4);
1693
1694         /* Put */
1695         LASSERT ((int)offsetof(ptl_hdr_t, msg.put.ack_wmd) == 32);
1696         LASSERT ((int)sizeof(((ptl_hdr_t *)0)->msg.put.ack_wmd) == 16);
1697         LASSERT ((int)offsetof(ptl_hdr_t, msg.put.match_bits) == 48);
1698         LASSERT ((int)sizeof(((ptl_hdr_t *)0)->msg.put.match_bits) == 8);
1699         LASSERT ((int)offsetof(ptl_hdr_t, msg.put.hdr_data) == 56);
1700         LASSERT ((int)sizeof(((ptl_hdr_t *)0)->msg.put.hdr_data) == 8);
1701         LASSERT ((int)offsetof(ptl_hdr_t, msg.put.ptl_index) == 64);
1702         LASSERT ((int)sizeof(((ptl_hdr_t *)0)->msg.put.ptl_index) == 4);
1703         LASSERT ((int)offsetof(ptl_hdr_t, msg.put.offset) == 68);
1704         LASSERT ((int)sizeof(((ptl_hdr_t *)0)->msg.put.offset) == 4);
1705
1706         /* Get */
1707         LASSERT ((int)offsetof(ptl_hdr_t, msg.get.return_wmd) == 32);
1708         LASSERT ((int)sizeof(((ptl_hdr_t *)0)->msg.get.return_wmd) == 16);
1709         LASSERT ((int)offsetof(ptl_hdr_t, msg.get.match_bits) == 48);
1710         LASSERT ((int)sizeof(((ptl_hdr_t *)0)->msg.get.match_bits) == 8);
1711         LASSERT ((int)offsetof(ptl_hdr_t, msg.get.ptl_index) == 56);
1712         LASSERT ((int)sizeof(((ptl_hdr_t *)0)->msg.get.ptl_index) == 4);
1713         LASSERT ((int)offsetof(ptl_hdr_t, msg.get.src_offset) == 60);
1714         LASSERT ((int)sizeof(((ptl_hdr_t *)0)->msg.get.src_offset) == 4);
1715         LASSERT ((int)offsetof(ptl_hdr_t, msg.get.sink_length) == 64);
1716         LASSERT ((int)sizeof(((ptl_hdr_t *)0)->msg.get.sink_length) == 4);
1717
1718         /* Reply */
1719         LASSERT ((int)offsetof(ptl_hdr_t, msg.reply.dst_wmd) == 32);
1720         LASSERT ((int)sizeof(((ptl_hdr_t *)0)->msg.reply.dst_wmd) == 16);
1721
1722         /* Hello */
1723         LASSERT ((int)offsetof(ptl_hdr_t, msg.hello.incarnation) == 32);
1724         LASSERT ((int)sizeof(((ptl_hdr_t *)0)->msg.hello.incarnation) == 8);
1725         LASSERT ((int)offsetof(ptl_hdr_t, msg.hello.type) == 40);
1726         LASSERT ((int)sizeof(((ptl_hdr_t *)0)->msg.hello.type) == 4);
1727 }