Whamcloud - gitweb
b=3031
[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 ptl_err_t
439 lib_lo_rxkiov(lib_nal_t *nal, void *private, lib_msg_t *libmsg,
440               unsigned int niov, ptl_kiov_t *kiov,
441               size_t offset, size_t mlen, size_t rlen)
442 {
443         LASSERT (0);
444 }
445
446 ptl_err_t
447 lib_lo_txkiov (lib_nal_t *nal, void *private, lib_msg_t *libmsg,
448                ptl_hdr_t *hdr, int type, ptl_nid_t nid, ptl_pid_t pid,
449                unsigned int payload_niov, ptl_kiov_t *payload_kiov,
450                size_t payload_offset, size_t payload_nob)
451 {
452         LASSERT (0);
453 }
454
455 #else /* __KERNEL__ */
456
457 ptl_size_t
458 lib_kiov_nob (int niov, ptl_kiov_t *kiov)
459 {
460         ptl_size_t  nob = 0;
461
462         while (niov-- > 0)
463                 nob += (kiov++)->kiov_len;
464
465         return (nob);
466 }
467
468 void
469 lib_copy_kiov2buf (char *dest, int niov, ptl_kiov_t *kiov,
470                    ptl_size_t offset, ptl_size_t len)
471 {
472         ptl_size_t  nob;
473         char       *addr;
474
475         if (len == 0)
476                 return;
477
478         LASSERT (!in_interrupt ());
479
480         LASSERT (niov > 0);
481         while (offset > kiov->kiov_len) {
482                 offset -= kiov->kiov_len;
483                 kiov++;
484                 niov--;
485                 LASSERT (niov > 0);
486         }
487
488         do {
489                 LASSERT (niov > 0);
490                 nob = MIN (kiov->kiov_len - offset, len);
491
492                 addr = ((char *)cfs_kmap(kiov->kiov_page)) + kiov->kiov_offset +
493                         offset;
494                 memcpy (dest, addr, nob);
495                 cfs_kunmap (kiov->kiov_page);
496
497                 len -= nob;
498                 dest += nob;
499                 niov--;
500                 kiov++;
501                 offset = 0;
502         } while (len > 0);
503 }
504
505 void
506 lib_copy_buf2kiov (int niov, ptl_kiov_t *kiov, ptl_size_t offset,
507                    char *src, ptl_size_t len)
508 {
509         ptl_size_t  nob;
510         char       *addr;
511
512         if (len == 0)
513                 return;
514
515         LASSERT (!in_interrupt ());
516
517         LASSERT (niov > 0);
518         while (offset >= kiov->kiov_len) {
519                 offset -= kiov->kiov_len;
520                 kiov++;
521                 niov--;
522                 LASSERT (niov > 0);
523         }
524
525         do {
526                 LASSERT (niov > 0);
527                 nob = MIN (kiov->kiov_len - offset, len);
528
529                 addr = ((char *)cfs_kmap(kiov->kiov_page)) + kiov->kiov_offset +
530                         offset;
531                 memcpy (addr, src, nob);
532                 cfs_kunmap (kiov->kiov_page);
533
534                 len -= nob;
535                 src += nob;
536                 niov--;
537                 kiov++;
538                 offset = 0;
539         } while (len > 0);
540 }
541
542 int
543 lib_extract_kiov (int dst_niov, ptl_kiov_t *dst,
544                   int src_niov, ptl_kiov_t *src,
545                   ptl_size_t offset, ptl_size_t len)
546 {
547         /* Initialise 'dst' to the subset of 'src' starting at 'offset',
548          * for exactly 'len' bytes, and return the number of entries.
549          * NB not destructive to 'src' */
550         ptl_size_t      frag_len;
551         int             niov;
552
553         if (len == 0)                           /* no data => */
554                 return (0);                     /* no frags */
555
556         LASSERT (src_niov > 0);
557         while (offset >= src->kiov_len) {      /* skip initial frags */
558                 offset -= src->kiov_len;
559                 src_niov--;
560                 src++;
561                 LASSERT (src_niov > 0);
562         }
563
564         niov = 1;
565         for (;;) {
566                 LASSERT (src_niov > 0);
567                 LASSERT (niov <= dst_niov);
568
569                 frag_len = src->kiov_len - offset;
570                 dst->kiov_page = src->kiov_page;
571                 dst->kiov_offset = src->kiov_offset + offset;
572
573                 if (len <= frag_len) {
574                         dst->kiov_len = len;
575                         LASSERT (dst->kiov_offset + dst->kiov_len <= PAGE_SIZE);
576                         return (niov);
577                 }
578
579                 dst->kiov_len = frag_len;
580                 LASSERT (dst->kiov_offset + dst->kiov_len <= PAGE_SIZE);
581
582                 len -= frag_len;
583                 dst++;
584                 src++;
585                 niov++;
586                 src_niov--;
587                 offset = 0;
588         }
589 }
590
591 #ifndef __KERNEL__
592 #if !defined(kmap)
593 #define kmap(page) ((page)->addr)
594 #endif
595 #if !defined(kunmap)
596 #define kunmap(page) do {} while(0)
597 #endif
598 #if !defined(page_address)
599 #define page_address(page) ((page)->page_address)
600 #endif
601 #endif
602
603 ptl_err_t
604 lib_lo_rxkiov(lib_nal_t    *nal,
605               void         *private,
606               lib_msg_t    *libmsg,
607               unsigned int  niov,
608               ptl_kiov_t   *kiov,
609               size_t        offset,
610               size_t        mlen,
611               size_t        rlen)
612 {
613         void          *srcaddr = NULL;
614         void          *dstaddr = NULL;
615         unsigned long  srcfrag = 0;
616         unsigned long  dstfrag = 0;
617         unsigned long  fraglen;
618         lo_desc_t     *lod = (lo_desc_t *)private;
619
620         /* I only handle unmapped->unmapped matches */
621         LASSERT(lod->lod_type == LOD_KIOV);
622
623         if (mlen == 0)
624                 return PTL_OK;
625
626         while (offset >= kiov->kiov_len) {
627                 offset -= kiov->kiov_len;
628                 kiov++;
629                 niov--;
630                 LASSERT(niov > 0);
631         }
632
633         while (lod->lod_offset >= lod->lod_iov.kiov->kiov_len) {
634                 lod->lod_offset -= lod->lod_iov.kiov->kiov_len;
635                 lod->lod_iov.kiov++;
636                 lod->lod_niov--;
637                 LASSERT(lod->lod_niov > 0);
638         }
639
640         do {
641                 /* CAVEAT EMPTOR:
642                  * I kmap 2 pages at once == slight risk of deadlock */
643                 LASSERT(niov > 0);
644                 if (dstaddr == NULL) {
645                         dstaddr = (void *)
646                                 ((unsigned long)cfs_kmap(kiov->kiov_page) +
647                                  kiov->kiov_offset + offset);
648                         dstfrag = kiov->kiov_len -  offset;
649                 }
650
651                 LASSERT(lod->lod_niov > 0);
652                 if (srcaddr == NULL) {
653                         srcaddr = (void *)
654                          ((unsigned long)cfs_kmap(lod->lod_iov.kiov->kiov_page)+
655                           lod->lod_iov.kiov->kiov_offset + lod->lod_offset);
656                         srcfrag = lod->lod_iov.kiov->kiov_len - lod->lod_offset;
657                 }
658
659                 fraglen = MIN(srcfrag, dstfrag);
660                 if (fraglen > mlen)
661                         fraglen = mlen;
662
663                 memcpy(dstaddr, srcaddr, fraglen);
664
665                 if (fraglen < dstfrag) {
666                         dstfrag -= fraglen;
667                         dstaddr = (void *)((unsigned long)dstaddr + fraglen);
668                 } else {
669                         cfs_kunmap(kiov->kiov_page);
670                         dstaddr = NULL;
671                         offset = 0;
672                         kiov++;
673                         niov--;
674                 }
675
676                 if (fraglen < srcfrag) {
677                         srcfrag -= fraglen;
678                         srcaddr = (void *)((unsigned long)srcaddr + fraglen);
679                 } else {
680                         cfs_kunmap(lod->lod_iov.kiov->kiov_page);
681                         srcaddr = NULL;
682                         lod->lod_offset = 0;
683                         lod->lod_iov.kiov++;
684                         lod->lod_niov--;
685                 }
686
687                 mlen -= fraglen;
688         } while (mlen > 0);
689
690         if (dstaddr != NULL)
691                 cfs_kunmap(kiov->kiov_page);
692
693         if (srcaddr != NULL)
694                 cfs_kunmap(lod->lod_iov.kiov->kiov_page);
695
696         lib_finalize(nal, private, libmsg, PTL_OK);
697         return PTL_OK;
698 }
699
700 ptl_err_t
701 lib_lo_txkiov (lib_nal_t    *nal,
702                void         *private,
703                lib_msg_t    *libmsg,
704                ptl_hdr_t    *hdr,
705                int           type,
706                ptl_nid_t     nid,
707                ptl_pid_t     pid,
708                unsigned int  payload_niov,
709                ptl_kiov_t   *payload_kiov,
710                size_t        payload_offset,
711                size_t        payload_nob)
712 {
713         lo_desc_t lod = {
714                 .lod_type     = LOD_KIOV,
715                 .lod_niov     = payload_niov,
716                 .lod_offset   = payload_offset,
717                 .lod_nob      = payload_nob,
718                 .lod_iov      = { .kiov = payload_kiov } };
719         ptl_err_t   rc;
720
721         rc = do_lib_parse(nal, hdr, &lod, 1);
722         if (rc == PTL_OK)
723                 lib_finalize(nal, private, libmsg, PTL_OK);
724
725         return rc;
726 }
727 #endif
728
729 ptl_err_t
730 lib_lo_rxiov(lib_nal_t    *nal,
731              void         *private,
732              lib_msg_t    *libmsg,
733              unsigned int  niov,
734              struct iovec *iov,
735              size_t        offset,
736              size_t        mlen,
737              size_t        rlen)
738 {
739         lo_desc_t *lod = (lo_desc_t *)private;
740
741         /* I only handle mapped->mapped matches */
742         LASSERT(lod->lod_type == LOD_IOV);
743         LASSERT(mlen > 0);
744
745         while (offset >= iov->iov_len) {
746                 offset -= iov->iov_len;
747                 iov++;
748                 niov--;
749                 LASSERT(niov > 0);
750         }
751
752         while (lod->lod_offset >= lod->lod_iov.iov->iov_len) {
753                 lod->lod_offset -= lod->lod_iov.iov->iov_len;
754                 lod->lod_iov.iov++;
755                 lod->lod_niov--;
756                 LASSERT(lod->lod_niov > 0);
757         }
758
759         do {
760                 int fraglen = MIN(iov->iov_len - offset,
761                                   lod->lod_iov.iov->iov_len - lod->lod_offset);
762
763                 LASSERT(niov > 0);
764                 LASSERT(lod->lod_niov > 0);
765
766                 if (fraglen > mlen)
767                         fraglen = mlen;
768
769                 memcpy((void *)((unsigned long)iov->iov_base + offset),
770                        (void *)((unsigned long)lod->lod_iov.iov->iov_base +
771                                 lod->lod_offset),
772                        fraglen);
773
774                 if (offset + fraglen < iov->iov_len) {
775                         offset += fraglen;
776                 } else {
777                         offset = 0;
778                         iov++;
779                         niov--;
780                 }
781
782                 if (lod->lod_offset + fraglen < lod->lod_iov.iov->iov_len ) {
783                         lod->lod_offset += fraglen;
784                 } else {
785                         lod->lod_offset = 0;
786                         lod->lod_iov.iov++;
787                         lod->lod_niov--;
788                 }
789
790                 mlen -= fraglen;
791         } while (mlen > 0);
792
793         lib_finalize(nal, private, libmsg, PTL_OK);
794         return PTL_OK;
795 }
796
797 ptl_err_t
798 lib_lo_txiov (lib_nal_t    *nal,
799               void         *private,
800               lib_msg_t    *libmsg,
801               ptl_hdr_t    *hdr,
802               int           type,
803               ptl_nid_t     nid,
804               ptl_pid_t     pid,
805               unsigned int  payload_niov,
806               struct iovec *payload_iov,
807               size_t        payload_offset,
808               size_t        payload_nob)
809 {
810         lo_desc_t lod = {
811                 .lod_type    = LOD_IOV,
812                 .lod_niov    = payload_niov,
813                 .lod_offset  = payload_offset,
814                 .lod_nob     = payload_nob,
815                 .lod_iov     = { .iov = payload_iov } };
816         ptl_err_t rc;
817
818         rc = do_lib_parse(nal, hdr, &lod, 1);
819         if (rc == PTL_OK)
820                 lib_finalize(nal, private, libmsg, PTL_OK);
821
822         return rc;
823 }
824
825 ptl_err_t
826 lib_lo_recv (lib_nal_t *nal, void *private, lib_msg_t *msg, lib_md_t *md,
827              ptl_size_t offset, ptl_size_t mlen, ptl_size_t rlen)
828 {
829         if (mlen == 0) {
830                 lib_finalize(nal, private, msg, PTL_OK);
831                 return PTL_OK;
832         }
833
834         if ((md->options & PTL_MD_KIOV) == 0)
835                 return lib_lo_rxiov(nal, private, msg,
836                                     md->md_niov, md->md_iov.iov,
837                                     offset, mlen, rlen);
838
839         return lib_lo_rxkiov(nal, private, msg,
840                              md->md_niov, md->md_iov.kiov,
841                              offset, mlen, rlen);
842 }
843
844 ptl_err_t
845 lib_recv (lib_nal_t *nal, void *private, lib_msg_t *msg, lib_md_t *md,
846           ptl_size_t offset, ptl_size_t mlen, ptl_size_t rlen)
847 {
848         if (mlen == 0)
849                 return (nal->libnal_recv(nal, private, msg,
850                                          0, NULL,
851                                          offset, mlen, rlen));
852
853         if ((md->options & PTL_MD_KIOV) == 0)
854                 return (nal->libnal_recv(nal, private, msg,
855                                          md->md_niov, md->md_iov.iov,
856                                          offset, mlen, rlen));
857
858         return (nal->libnal_recv_pages(nal, private, msg,
859                                        md->md_niov, md->md_iov.kiov,
860                                        offset, mlen, rlen));
861 }
862
863 ptl_err_t
864 lib_send (lib_nal_t *nal, void *private, lib_msg_t *msg,
865           ptl_hdr_t *hdr, int type, ptl_nid_t nid, ptl_pid_t pid,
866           lib_md_t *md, ptl_size_t offset, ptl_size_t len)
867 {
868         int loopback = (nal->libnal_ni.ni_loopback &&
869                         (nid == nal->libnal_ni.ni_pid.nid));
870
871         if (len == 0) {
872                 if (loopback)
873                         return lib_lo_txiov(nal, private, msg,
874                                             hdr, type, nid, pid,
875                                             0, NULL,
876                                             offset, len);
877                 else
878                         return nal->libnal_send(nal, private, msg,
879                                                 hdr, type, nid, pid,
880                                                 0, NULL,
881                                                 offset, len);
882         }
883
884         if ((md->options & PTL_MD_KIOV) == 0) {
885                 if (loopback)
886                         return lib_lo_txiov(nal, private, msg,
887                                             hdr, type, nid, pid,
888                                             md->md_niov, md->md_iov.iov,
889                                             offset, len);
890                 else
891                         return nal->libnal_send(nal, private, msg,
892                                                 hdr, type, nid, pid,
893                                                 md->md_niov, md->md_iov.iov,
894                                                 offset, len);
895         }
896
897         if (loopback)
898                 return lib_lo_txkiov(nal, private, msg,
899                                      hdr, type, nid, pid,
900                                      md->md_niov, md->md_iov.kiov,
901                                      offset, len);
902         else
903                 return nal->libnal_send_pages(nal, private, msg,
904                                               hdr, type, nid, pid,
905                                               md->md_niov, md->md_iov.kiov,
906                                               offset, len);
907 }
908
909 static void
910 lib_commit_md (lib_nal_t *nal, lib_md_t *md, lib_msg_t *msg)
911 {
912         /* ALWAYS called holding the LIB_LOCK */
913         lib_counters_t *counters = &nal->libnal_ni.ni_counters;
914
915         /* Here, we commit the MD to a network OP by marking it busy and
916          * decrementing its threshold.  Come what may, the network "owns"
917          * the MD until a call to lib_finalize() signals completion. */
918         msg->md = md;
919
920         md->pending++;
921         if (md->threshold != PTL_MD_THRESH_INF) {
922                 LASSERT (md->threshold > 0);
923                 md->threshold--;
924         }
925
926         counters->msgs_alloc++;
927         if (counters->msgs_alloc > counters->msgs_max)
928                 counters->msgs_max = counters->msgs_alloc;
929
930         list_add (&msg->msg_list, &nal->libnal_ni.ni_active_msgs);
931 }
932
933 static void
934 lib_drop_message (lib_nal_t *nal, void *private, ptl_hdr_t *hdr, int loopback)
935 {
936         unsigned long flags;
937
938         /* CAVEAT EMPTOR: this only drops messages that we've not committed
939          * to receive (init_msg() not called) and therefore can't cause an
940          * event. */
941
942         LIB_LOCK(nal, flags);
943         nal->libnal_ni.ni_counters.drop_count++;
944         nal->libnal_ni.ni_counters.drop_length += hdr->payload_length;
945         LIB_UNLOCK(nal, flags);
946
947         /* NULL msg => if NAL calls lib_finalize it will be a noop */
948         if (!loopback)
949                 (void) lib_recv(nal, private, NULL, NULL, 0, 0,
950                                 hdr->payload_length);
951 }
952
953 /*
954  * Incoming messages have a ptl_msg_t object associated with them
955  * by the library.  This object encapsulates the state of the
956  * message and allows the NAL to do non-blocking receives or sends
957  * of long messages.
958  *
959  */
960 static ptl_err_t
961 parse_put(lib_nal_t *nal, ptl_hdr_t *hdr, void *private,
962           lib_msg_t *msg, int loopback)
963 {
964         lib_ni_t        *ni = &nal->libnal_ni;
965         ptl_size_t       mlength = 0;
966         ptl_size_t       offset = 0;
967         ptl_err_t        rc;
968         lib_md_t        *md;
969         unsigned long    flags;
970
971         /* Convert put fields to host byte order */
972         hdr->msg.put.match_bits = le64_to_cpu(hdr->msg.put.match_bits);
973         hdr->msg.put.ptl_index = le32_to_cpu(hdr->msg.put.ptl_index);
974         hdr->msg.put.offset = le32_to_cpu(hdr->msg.put.offset);
975
976         LIB_LOCK(nal, flags);
977
978         md = lib_match_md(nal, hdr->msg.put.ptl_index, PTL_MD_OP_PUT,
979                           hdr->src_nid, hdr->src_pid,
980                           hdr->payload_length, hdr->msg.put.offset,
981                           hdr->msg.put.match_bits, msg,
982                           &mlength, &offset);
983         if (md == NULL) {
984                 LIB_UNLOCK(nal, flags);
985                 return (PTL_FAIL);
986         }
987
988         msg->ev.type = PTL_EVENT_PUT_END;
989         msg->ev.hdr_data = hdr->msg.put.hdr_data;
990
991         if (!ptl_is_wire_handle_none(&hdr->msg.put.ack_wmd) &&
992             !(md->options & PTL_MD_ACK_DISABLE)) {
993                 msg->ack_wmd = hdr->msg.put.ack_wmd;
994         }
995
996         ni->ni_counters.recv_count++;
997         ni->ni_counters.recv_length += mlength;
998
999         LIB_UNLOCK(nal, flags);
1000
1001         if (loopback)
1002                 rc = lib_lo_recv(nal, private, msg, md, offset, mlength,
1003                                  hdr->payload_length);
1004         else
1005                 rc = lib_recv(nal, private, msg, md, offset, mlength,
1006                               hdr->payload_length);
1007
1008         if (rc != PTL_OK)
1009                 CERROR(LPU64": error on receiving PUT from "LPU64": %d\n",
1010                        ni->ni_pid.nid, hdr->src_nid, rc);
1011
1012         return (rc);
1013 }
1014
1015 static ptl_err_t
1016 parse_get(lib_nal_t *nal, ptl_hdr_t *hdr, void *private,
1017           lib_msg_t *msg, int loopback)
1018 {
1019         lib_ni_t        *ni = &nal->libnal_ni;
1020         ptl_size_t       mlength = 0;
1021         ptl_size_t       offset = 0;
1022         lib_md_t        *md;
1023         ptl_hdr_t        reply;
1024         unsigned long    flags;
1025         int              rc;
1026
1027         /* Convert get fields to host byte order */
1028         hdr->msg.get.match_bits = le64_to_cpu(hdr->msg.get.match_bits);
1029         hdr->msg.get.ptl_index = le32_to_cpu(hdr->msg.get.ptl_index);
1030         hdr->msg.get.sink_length = le32_to_cpu(hdr->msg.get.sink_length);
1031         hdr->msg.get.src_offset = le32_to_cpu(hdr->msg.get.src_offset);
1032
1033         LIB_LOCK(nal, flags);
1034
1035         md = lib_match_md(nal, hdr->msg.get.ptl_index, PTL_MD_OP_GET,
1036                           hdr->src_nid, hdr->src_pid,
1037                           hdr->msg.get.sink_length, hdr->msg.get.src_offset,
1038                           hdr->msg.get.match_bits, msg,
1039                           &mlength, &offset);
1040         if (md == NULL) {
1041                 LIB_UNLOCK(nal, flags);
1042                 return (PTL_FAIL);
1043         }
1044
1045         msg->ev.type = PTL_EVENT_GET_END;
1046         msg->ev.hdr_data = 0;
1047
1048         ni->ni_counters.send_count++;
1049         ni->ni_counters.send_length += mlength;
1050
1051         LIB_UNLOCK(nal, flags);
1052
1053         memset (&reply, 0, sizeof (reply));
1054         reply.type     = cpu_to_le32(PTL_MSG_REPLY);
1055         reply.dest_nid = cpu_to_le64(hdr->src_nid);
1056         reply.dest_pid = cpu_to_le32(hdr->src_pid);
1057         reply.src_nid  = cpu_to_le64(ni->ni_pid.nid);
1058         reply.src_pid  = cpu_to_le32(ni->ni_pid.pid);
1059         reply.payload_length = cpu_to_le32(mlength);
1060
1061         reply.msg.reply.dst_wmd = hdr->msg.get.return_wmd;
1062
1063         /* NB call lib_send() _BEFORE_ lib_recv() completes the incoming
1064          * message.  Some NALs _require_ this to implement optimized GET */
1065
1066         rc = lib_send (nal, private, msg, &reply, PTL_MSG_REPLY,
1067                        hdr->src_nid, hdr->src_pid, md, offset, mlength);
1068         if (rc != PTL_OK)
1069                 CERROR(LPU64": Unable to send REPLY for GET from "LPU64": %d\n",
1070                        ni->ni_pid.nid, hdr->src_nid, rc);
1071
1072         /* Discard any junk after the hdr */
1073         if (!loopback)
1074                 (void) lib_recv(nal, private, NULL, NULL, 0, 0,
1075                                 hdr->payload_length);
1076
1077         return (rc);
1078 }
1079
1080 static ptl_err_t
1081 parse_reply(lib_nal_t *nal, ptl_hdr_t *hdr, void *private,
1082             lib_msg_t *msg, int loopback)
1083 {
1084         lib_ni_t        *ni = &nal->libnal_ni;
1085         lib_md_t        *md;
1086         int              rlength;
1087         int              length;
1088         unsigned long    flags;
1089         ptl_err_t        rc;
1090
1091         LIB_LOCK(nal, flags);
1092
1093         /* NB handles only looked up by creator (no flips) */
1094         md = ptl_wire_handle2md(&hdr->msg.reply.dst_wmd, nal);
1095         if (md == NULL || md->threshold == 0) {
1096                 CERROR (LPU64": Dropping REPLY from "LPU64" for %s MD "LPX64"."LPX64"\n",
1097                         ni->ni_pid.nid, hdr->src_nid,
1098                         md == NULL ? "invalid" : "inactive",
1099                         hdr->msg.reply.dst_wmd.wh_interface_cookie,
1100                         hdr->msg.reply.dst_wmd.wh_object_cookie);
1101
1102                 LIB_UNLOCK(nal, flags);
1103                 return (PTL_FAIL);
1104         }
1105
1106         LASSERT (md->offset == 0);
1107
1108         length = rlength = hdr->payload_length;
1109
1110         if (length > md->length) {
1111                 if ((md->options & PTL_MD_TRUNCATE) == 0) {
1112                         CERROR (LPU64": Dropping REPLY from "LPU64
1113                                 " length %d for MD "LPX64" would overflow (%d)\n",
1114                                 ni->ni_pid.nid, hdr->src_nid, length,
1115                                 hdr->msg.reply.dst_wmd.wh_object_cookie,
1116                                 md->length);
1117                         LIB_UNLOCK(nal, flags);
1118                         return (PTL_FAIL);
1119                 }
1120                 length = md->length;
1121         }
1122
1123         CDEBUG(D_NET, "Reply from "LPU64" of length %d/%d into md "LPX64"\n",
1124                hdr->src_nid, length, rlength,
1125                hdr->msg.reply.dst_wmd.wh_object_cookie);
1126
1127         lib_commit_md(nal, md, msg);
1128
1129         msg->ev.type = PTL_EVENT_REPLY_END;
1130         msg->ev.initiator.nid = hdr->src_nid;
1131         msg->ev.initiator.pid = hdr->src_pid;
1132         msg->ev.rlength = rlength;
1133         msg->ev.mlength = length;
1134         msg->ev.offset = 0;
1135
1136         lib_md_deconstruct(nal, md, &msg->ev.md);
1137         ptl_md2handle(&msg->ev.md_handle, nal, md);
1138
1139         ni->ni_counters.recv_count++;
1140         ni->ni_counters.recv_length += length;
1141
1142         LIB_UNLOCK(nal, flags);
1143
1144         if (loopback)
1145                 rc = lib_lo_recv(nal, private, msg, md, 0, length, rlength);
1146         else
1147                 rc = lib_recv(nal, private, msg, md, 0, length, rlength);
1148
1149         if (rc != PTL_OK)
1150                 CERROR(LPU64": error on receiving REPLY from "LPU64": %d\n",
1151                        ni->ni_pid.nid, hdr->src_nid, rc);
1152
1153         return (rc);
1154 }
1155
1156 static ptl_err_t
1157 parse_ack(lib_nal_t *nal, ptl_hdr_t *hdr, void *private, 
1158           lib_msg_t *msg, int loopback)
1159 {
1160         lib_ni_t      *ni = &nal->libnal_ni;
1161         lib_md_t      *md;
1162         unsigned long  flags;
1163
1164         /* Convert ack fields to host byte order */
1165         hdr->msg.ack.match_bits = le64_to_cpu(hdr->msg.ack.match_bits);
1166         hdr->msg.ack.mlength = le32_to_cpu(hdr->msg.ack.mlength);
1167
1168         LIB_LOCK(nal, flags);
1169
1170         /* NB handles only looked up by creator (no flips) */
1171         md = ptl_wire_handle2md(&hdr->msg.ack.dst_wmd, nal);
1172         if (md == NULL || md->threshold == 0) {
1173                 CDEBUG(D_INFO, LPU64": Dropping ACK from "LPU64" to %s MD "
1174                        LPX64"."LPX64"\n", ni->ni_pid.nid, hdr->src_nid,
1175                        (md == NULL) ? "invalid" : "inactive",
1176                        hdr->msg.ack.dst_wmd.wh_interface_cookie,
1177                        hdr->msg.ack.dst_wmd.wh_object_cookie);
1178
1179                 LIB_UNLOCK(nal, flags);
1180                 return (PTL_FAIL);
1181         }
1182
1183         CDEBUG(D_NET, LPU64": ACK from "LPU64" into md "LPX64"\n",
1184                ni->ni_pid.nid, hdr->src_nid,
1185                hdr->msg.ack.dst_wmd.wh_object_cookie);
1186
1187         lib_commit_md(nal, md, msg);
1188
1189         msg->ev.type = PTL_EVENT_ACK;
1190         msg->ev.initiator.nid = hdr->src_nid;
1191         msg->ev.initiator.pid = hdr->src_pid;
1192         msg->ev.mlength = hdr->msg.ack.mlength;
1193         msg->ev.match_bits = hdr->msg.ack.match_bits;
1194
1195         lib_md_deconstruct(nal, md, &msg->ev.md);
1196         ptl_md2handle(&msg->ev.md_handle, nal, md);
1197
1198         ni->ni_counters.recv_count++;
1199
1200         LIB_UNLOCK(nal, flags);
1201
1202         /* We have received and matched up the ack OK, create the
1203          * completion event now... */
1204         lib_finalize(nal, private, msg, PTL_OK);
1205
1206         /* ...and now discard any junk after the hdr */
1207         if (!loopback)
1208                 (void) lib_recv(nal, private, NULL, NULL, 0, 0,
1209                                 hdr->payload_length);
1210
1211        return (PTL_OK);
1212 }
1213
1214 static char *
1215 hdr_type_string (ptl_hdr_t *hdr)
1216 {
1217         switch (hdr->type) {
1218         case PTL_MSG_ACK:
1219                 return ("ACK");
1220         case PTL_MSG_PUT:
1221                 return ("PUT");
1222         case PTL_MSG_GET:
1223                 return ("GET");
1224         case PTL_MSG_REPLY:
1225                 return ("REPLY");
1226         case PTL_MSG_HELLO:
1227                 return ("HELLO");
1228         default:
1229                 return ("<UNKNOWN>");
1230         }
1231 }
1232
1233 void print_hdr(lib_nal_t *nal, ptl_hdr_t * hdr)
1234 {
1235         char *type_str = hdr_type_string (hdr);
1236
1237         CWARN("P3 Header at %p of type %s\n", hdr, type_str);
1238         CWARN("    From nid/pid "LPX64"/%u", hdr->src_nid, hdr->src_pid);
1239         CWARN("    To nid/pid "LPX64"/%u\n", hdr->dest_nid, hdr->dest_pid);
1240
1241         switch (hdr->type) {
1242         default:
1243                 break;
1244
1245         case PTL_MSG_PUT:
1246                 CWARN("    Ptl index %d, ack md "LPX64"."LPX64", "
1247                       "match bits "LPX64"\n",
1248                       hdr->msg.put.ptl_index,
1249                       hdr->msg.put.ack_wmd.wh_interface_cookie,
1250                       hdr->msg.put.ack_wmd.wh_object_cookie,
1251                       hdr->msg.put.match_bits);
1252                 CWARN("    Length %d, offset %d, hdr data "LPX64"\n",
1253                       hdr->payload_length, hdr->msg.put.offset,
1254                       hdr->msg.put.hdr_data);
1255                 break;
1256
1257         case PTL_MSG_GET:
1258                 CWARN("    Ptl index %d, return md "LPX64"."LPX64", "
1259                       "match bits "LPX64"\n", hdr->msg.get.ptl_index,
1260                       hdr->msg.get.return_wmd.wh_interface_cookie,
1261                       hdr->msg.get.return_wmd.wh_object_cookie,
1262                       hdr->msg.get.match_bits);
1263                 CWARN("    Length %d, src offset %d\n",
1264                       hdr->msg.get.sink_length,
1265                       hdr->msg.get.src_offset);
1266                 break;
1267
1268         case PTL_MSG_ACK:
1269                 CWARN("    dst md "LPX64"."LPX64", "
1270                       "manipulated length %d\n",
1271                       hdr->msg.ack.dst_wmd.wh_interface_cookie,
1272                       hdr->msg.ack.dst_wmd.wh_object_cookie,
1273                       hdr->msg.ack.mlength);
1274                 break;
1275
1276         case PTL_MSG_REPLY:
1277                 CWARN("    dst md "LPX64"."LPX64", "
1278                       "length %d\n",
1279                       hdr->msg.reply.dst_wmd.wh_interface_cookie,
1280                       hdr->msg.reply.dst_wmd.wh_object_cookie,
1281                       hdr->payload_length);
1282         }
1283
1284 }                               /* end of print_hdr() */
1285
1286
1287 ptl_err_t
1288 lib_parse(lib_nal_t *nal, ptl_hdr_t *hdr, void *private)
1289 {
1290         return do_lib_parse(nal, hdr, private, 0);
1291 }
1292
1293 ptl_err_t
1294 do_lib_parse(lib_nal_t *nal, ptl_hdr_t *hdr, void *private, int loopback)
1295 {
1296         unsigned long  flags;
1297         ptl_err_t      rc;
1298         lib_msg_t     *msg;
1299
1300         /* NB we return PTL_OK if we manage to parse the header and believe
1301          * it looks OK.  Anything that goes wrong with receiving the
1302          * message after that point is the responsibility of the NAL */
1303
1304         /* convert common fields to host byte order */
1305         hdr->type = le32_to_cpu(hdr->type);
1306         hdr->src_nid = le64_to_cpu(hdr->src_nid);
1307         hdr->src_pid = le32_to_cpu(hdr->src_pid);
1308         hdr->dest_pid = le32_to_cpu(hdr->dest_pid);
1309         hdr->payload_length = le32_to_cpu(hdr->payload_length);
1310
1311         switch (hdr->type) {
1312         case PTL_MSG_HELLO: {
1313                 /* dest_nid is really ptl_magicversion_t */
1314                 ptl_magicversion_t *mv = (ptl_magicversion_t *)&hdr->dest_nid;
1315
1316                 mv->magic = le32_to_cpu(mv->magic);
1317                 mv->version_major = le16_to_cpu(mv->version_major);
1318                 mv->version_minor = le16_to_cpu(mv->version_minor);
1319
1320                 if (mv->magic == PORTALS_PROTO_MAGIC &&
1321                     mv->version_major == PORTALS_PROTO_VERSION_MAJOR &&
1322                     mv->version_minor == PORTALS_PROTO_VERSION_MINOR) {
1323                         CWARN (LPU64": Dropping unexpected HELLO message: "
1324                                "magic %d, version %d.%d from "LPD64"\n",
1325                                nal->libnal_ni.ni_pid.nid, mv->magic,
1326                                mv->version_major, mv->version_minor,
1327                                hdr->src_nid);
1328
1329                         /* it's good but we don't want it */
1330                         lib_drop_message(nal, private, hdr, loopback);
1331                         return PTL_OK;
1332                 }
1333
1334                 /* we got garbage */
1335                 CERROR (LPU64": Bad HELLO message: "
1336                         "magic %d, version %d.%d from "LPD64"\n",
1337                         nal->libnal_ni.ni_pid.nid, mv->magic,
1338                         mv->version_major, mv->version_minor,
1339                         hdr->src_nid);
1340                 return PTL_FAIL;
1341         }
1342
1343         case PTL_MSG_ACK:
1344         case PTL_MSG_PUT:
1345         case PTL_MSG_GET:
1346         case PTL_MSG_REPLY:
1347                 hdr->dest_nid = le64_to_cpu(hdr->dest_nid);
1348                 if (hdr->dest_nid != nal->libnal_ni.ni_pid.nid) {
1349                         CERROR(LPU64": BAD dest NID in %s message from"
1350                                LPU64" to "LPU64" (not me)\n",
1351                                nal->libnal_ni.ni_pid.nid, hdr_type_string (hdr),
1352                                hdr->src_nid, hdr->dest_nid);
1353                         return PTL_FAIL;
1354                 }
1355                 break;
1356
1357         default:
1358                 CERROR(LPU64": Bad message type 0x%x from "LPU64"\n",
1359                        nal->libnal_ni.ni_pid.nid, hdr->type, hdr->src_nid);
1360                 return PTL_FAIL;
1361         }
1362
1363         /* We've decided we're not receiving garbage since we can parse the
1364          * header.  We will return PTL_OK come what may... */
1365
1366         if (!list_empty (&nal->libnal_ni.ni_test_peers) && /* normally we don't */
1367             fail_peer (nal, hdr->src_nid, 0))      /* shall we now? */
1368         {
1369                 CERROR(LPU64": Dropping incoming %s from "LPU64
1370                        ": simulated failure\n",
1371                        nal->libnal_ni.ni_pid.nid, hdr_type_string (hdr),
1372                        hdr->src_nid);
1373                 lib_drop_message(nal, private, hdr, loopback);
1374                 return PTL_OK;
1375         }
1376
1377         msg = lib_msg_alloc(nal);
1378         if (msg == NULL) {
1379                 CERROR(LPU64": Dropping incoming %s from "LPU64
1380                        ": can't allocate a lib_msg_t\n",
1381                        nal->libnal_ni.ni_pid.nid, hdr_type_string (hdr),
1382                        hdr->src_nid);
1383                 lib_drop_message(nal, private, hdr, loopback);
1384                 return PTL_OK;
1385         }
1386
1387         switch (hdr->type) {
1388         case PTL_MSG_ACK:
1389                 rc = parse_ack(nal, hdr, private, msg, loopback);
1390                 break;
1391         case PTL_MSG_PUT:
1392                 rc = parse_put(nal, hdr, private, msg, loopback);
1393                 break;
1394         case PTL_MSG_GET:
1395                 rc = parse_get(nal, hdr, private, msg, loopback);
1396                 break;
1397         case PTL_MSG_REPLY:
1398                 rc = parse_reply(nal, hdr, private, msg, loopback);
1399                 break;
1400         default:
1401                 LASSERT(0);
1402                 rc = PTL_FAIL;                  /* no compiler warning please */
1403                 break;
1404         }
1405
1406         if (rc != PTL_OK) {
1407                 if (msg->md != NULL) {
1408                         /* committed... */
1409                         lib_finalize(nal, private, msg, rc);
1410                 } else {
1411                         LIB_LOCK(nal, flags);
1412                         lib_msg_free(nal, msg); /* expects LIB_LOCK held */
1413                         LIB_UNLOCK(nal, flags);
1414
1415                         lib_drop_message(nal, private, hdr, loopback);
1416                 }
1417         }
1418
1419         return PTL_OK;
1420         /* That's "OK I can parse it", not "OK I like it" :) */
1421 }
1422
1423 int
1424 lib_api_put(nal_t *apinal, ptl_handle_md_t *mdh,
1425             ptl_ack_req_t ack, ptl_process_id_t *id,
1426             ptl_pt_index_t portal, ptl_ac_index_t ac,
1427             ptl_match_bits_t match_bits,
1428             ptl_size_t offset, ptl_hdr_data_t hdr_data)
1429 {
1430         lib_nal_t        *nal = apinal->nal_data;
1431         lib_ni_t         *ni = &nal->libnal_ni;
1432         lib_msg_t        *msg;
1433         ptl_hdr_t         hdr;
1434         lib_md_t         *md;
1435         unsigned long     flags;
1436         int               rc;
1437
1438         if (!list_empty (&ni->ni_test_peers) && /* normally we don't */
1439             fail_peer (nal, id->nid, 1))           /* shall we now? */
1440         {
1441                 CERROR("Dropping PUT to "LPU64": simulated failure\n",
1442                        id->nid);
1443                 return PTL_PROCESS_INVALID;
1444         }
1445
1446         msg = lib_msg_alloc(nal);
1447         if (msg == NULL) {
1448                 CERROR(LPU64": Dropping PUT to "LPU64": ENOMEM on lib_msg_t\n",
1449                        ni->ni_pid.nid, id->nid);
1450                 return PTL_NO_SPACE;
1451         }
1452
1453         LIB_LOCK(nal, flags);
1454
1455         md = ptl_handle2md(mdh, nal);
1456         if (md == NULL || md->threshold == 0) {
1457                 lib_msg_free(nal, msg);
1458                 LIB_UNLOCK(nal, flags);
1459
1460                 return PTL_MD_INVALID;
1461         }
1462
1463         CDEBUG(D_NET, "PtlPut -> "LPX64"\n", id->nid);
1464
1465         memset (&hdr, 0, sizeof (hdr));
1466         hdr.type     = cpu_to_le32(PTL_MSG_PUT);
1467         hdr.dest_nid = cpu_to_le64(id->nid);
1468         hdr.dest_pid = cpu_to_le32(id->pid);
1469         hdr.src_nid  = cpu_to_le64(ni->ni_pid.nid);
1470         hdr.src_pid  = cpu_to_le32(ni->ni_pid.pid);
1471         hdr.payload_length = cpu_to_le32(md->length);
1472
1473         /* NB handles only looked up by creator (no flips) */
1474         if (ack == PTL_ACK_REQ) {
1475                 hdr.msg.put.ack_wmd.wh_interface_cookie = ni->ni_interface_cookie;
1476                 hdr.msg.put.ack_wmd.wh_object_cookie = md->md_lh.lh_cookie;
1477         } else {
1478                 hdr.msg.put.ack_wmd = PTL_WIRE_HANDLE_NONE;
1479         }
1480
1481         hdr.msg.put.match_bits = cpu_to_le64(match_bits);
1482         hdr.msg.put.ptl_index = cpu_to_le32(portal);
1483         hdr.msg.put.offset = cpu_to_le32(offset);
1484         hdr.msg.put.hdr_data = hdr_data;
1485
1486         lib_commit_md(nal, md, msg);
1487
1488         msg->ev.type = PTL_EVENT_SEND_END;
1489         msg->ev.initiator.nid = ni->ni_pid.nid;
1490         msg->ev.initiator.pid = ni->ni_pid.pid;
1491         msg->ev.pt_index = portal;
1492         msg->ev.match_bits = match_bits;
1493         msg->ev.rlength = md->length;
1494         msg->ev.mlength = md->length;
1495         msg->ev.offset = offset;
1496         msg->ev.hdr_data = hdr_data;
1497
1498         lib_md_deconstruct(nal, md, &msg->ev.md);
1499         ptl_md2handle(&msg->ev.md_handle, nal, md);
1500
1501         ni->ni_counters.send_count++;
1502         ni->ni_counters.send_length += md->length;
1503
1504         LIB_UNLOCK(nal, flags);
1505
1506         rc = lib_send (nal, NULL, msg, &hdr, PTL_MSG_PUT,
1507                        id->nid, id->pid, md, 0, md->length);
1508         if (rc != PTL_OK) {
1509                 CERROR("Error sending PUT to "LPX64": %d\n",
1510                        id->nid, rc);
1511                 lib_finalize (nal, NULL, msg, rc);
1512         }
1513
1514         /* completion will be signalled by an event */
1515         return PTL_OK;
1516 }
1517
1518 lib_msg_t *
1519 lib_create_reply_msg (lib_nal_t *nal, ptl_nid_t peer_nid, lib_msg_t *getmsg)
1520 {
1521         /* The NAL can DMA direct to the GET md (i.e. no REPLY msg).  This
1522          * returns a msg for the NAL to pass to lib_finalize() when the sink
1523          * data has been received.
1524          *
1525          * CAVEAT EMPTOR: 'getmsg' is the original GET, which is freed when
1526          * lib_finalize() is called on it, so the NAL must call this first */
1527
1528         lib_ni_t        *ni = &nal->libnal_ni;
1529         lib_msg_t       *msg = lib_msg_alloc(nal);
1530         lib_md_t        *getmd = getmsg->md;
1531         unsigned long    flags;
1532
1533         LIB_LOCK(nal, flags);
1534
1535         LASSERT (getmd->pending > 0);
1536
1537         if (msg == NULL) {
1538                 CERROR ("Dropping REPLY from "LPU64": can't allocate msg\n",
1539                         peer_nid);
1540                 goto drop;
1541         }
1542
1543         if (getmd->threshold == 0) {
1544                 CERROR ("Dropping REPLY from "LPU64" for inactive MD %p\n",
1545                         peer_nid, getmd);
1546                 goto drop_msg;
1547         }
1548
1549         LASSERT (getmd->offset == 0);
1550
1551         CDEBUG(D_NET, "Reply from "LPU64" md %p\n", peer_nid, getmd);
1552
1553         lib_commit_md (nal, getmd, msg);
1554
1555         msg->ev.type = PTL_EVENT_REPLY_END;
1556         msg->ev.initiator.nid = peer_nid;
1557         msg->ev.initiator.pid = 0;      /* XXX FIXME!!! */
1558         msg->ev.rlength = msg->ev.mlength = getmd->length;
1559         msg->ev.offset = 0;
1560
1561         lib_md_deconstruct(nal, getmd, &msg->ev.md);
1562         ptl_md2handle(&msg->ev.md_handle, nal, getmd);
1563
1564         ni->ni_counters.recv_count++;
1565         ni->ni_counters.recv_length += getmd->length;
1566
1567         LIB_UNLOCK(nal, flags);
1568
1569         return msg;
1570
1571  drop_msg:
1572         lib_msg_free(nal, msg);
1573  drop:
1574         nal->libnal_ni.ni_counters.drop_count++;
1575         nal->libnal_ni.ni_counters.drop_length += getmd->length;
1576
1577         LIB_UNLOCK (nal, flags);
1578
1579         return NULL;
1580 }
1581
1582 int
1583 lib_api_get(nal_t *apinal, ptl_handle_md_t *mdh, ptl_process_id_t *id,
1584             ptl_pt_index_t portal, ptl_ac_index_t ac,
1585             ptl_match_bits_t match_bits, ptl_size_t offset)
1586 {
1587         lib_nal_t        *nal = apinal->nal_data;
1588         lib_ni_t         *ni = &nal->libnal_ni;
1589         lib_msg_t        *msg;
1590         ptl_hdr_t         hdr;
1591         lib_md_t         *md;
1592         unsigned long     flags;
1593         int               rc;
1594
1595         if (!list_empty (&ni->ni_test_peers) && /* normally we don't */
1596             fail_peer (nal, id->nid, 1))           /* shall we now? */
1597         {
1598                 CERROR("Dropping PUT to "LPX64": simulated failure\n",
1599                        id->nid);
1600                 return PTL_PROCESS_INVALID;
1601         }
1602
1603         msg = lib_msg_alloc(nal);
1604         if (msg == NULL) {
1605                 CERROR("Dropping GET to "LPU64": ENOMEM on lib_msg_t\n",
1606                        id->nid);
1607                 return PTL_NO_SPACE;
1608         }
1609
1610         LIB_LOCK(nal, flags);
1611
1612         md = ptl_handle2md(mdh, nal);
1613         if (md == NULL || !md->threshold) {
1614                 lib_msg_free(nal, msg);
1615                 LIB_UNLOCK(nal, flags);
1616
1617                 return PTL_MD_INVALID;
1618         }
1619
1620         CDEBUG(D_NET, "PtlGet -> %Lu: %lu\n", (unsigned long long)id->nid,
1621                (unsigned long)id->pid);
1622
1623         memset (&hdr, 0, sizeof (hdr));
1624         hdr.type     = cpu_to_le32(PTL_MSG_GET);
1625         hdr.dest_nid = cpu_to_le64(id->nid);
1626         hdr.dest_pid = cpu_to_le32(id->pid);
1627         hdr.src_nid  = cpu_to_le64(ni->ni_pid.nid);
1628         hdr.src_pid  = cpu_to_le32(ni->ni_pid.pid);
1629         hdr.payload_length = 0;
1630
1631         /* NB handles only looked up by creator (no flips) */
1632         hdr.msg.get.return_wmd.wh_interface_cookie = ni->ni_interface_cookie;
1633         hdr.msg.get.return_wmd.wh_object_cookie = md->md_lh.lh_cookie;
1634
1635         hdr.msg.get.match_bits = cpu_to_le64(match_bits);
1636         hdr.msg.get.ptl_index = cpu_to_le32(portal);
1637         hdr.msg.get.src_offset = cpu_to_le32(offset);
1638         hdr.msg.get.sink_length = cpu_to_le32(md->length);
1639
1640         lib_commit_md(nal, md, msg);
1641
1642         msg->ev.type = PTL_EVENT_SEND_END;
1643         msg->ev.initiator = ni->ni_pid;
1644         msg->ev.pt_index = portal;
1645         msg->ev.match_bits = match_bits;
1646         msg->ev.rlength = md->length;
1647         msg->ev.mlength = md->length;
1648         msg->ev.offset = offset;
1649         msg->ev.hdr_data = 0;
1650
1651         lib_md_deconstruct(nal, md, &msg->ev.md);
1652         ptl_md2handle(&msg->ev.md_handle, nal, md);
1653
1654         ni->ni_counters.send_count++;
1655
1656         LIB_UNLOCK(nal, flags);
1657
1658         rc = lib_send (nal, NULL, msg, &hdr, PTL_MSG_GET,
1659                        id->nid, id->pid, NULL, 0, 0);
1660         if (rc != PTL_OK) {
1661                 CERROR(LPU64": error sending GET to "LPU64": %d\n",
1662                        ni->ni_pid.nid, id->nid, rc);
1663                 lib_finalize (nal, NULL, msg, rc);
1664         }
1665
1666         /* completion will be signalled by an event */
1667         return PTL_OK;
1668 }
1669
1670 void lib_assert_wire_constants (void)
1671 {
1672         /* Wire protocol assertions generated by 'wirecheck'
1673          * running on Linux mdevi 2.4.21-p4smp-55chaos #1 SMP Tue Jun 8 14:38:44 PDT 2004 i686 i686 i
1674          * with gcc version 3.2.3 20030502 (Red Hat Linux 3.2.3-34) */
1675
1676
1677         /* Constants... */
1678         LASSERT (PORTALS_PROTO_MAGIC == 0xeebc0ded);
1679         LASSERT (PORTALS_PROTO_VERSION_MAJOR == 1);
1680         LASSERT (PORTALS_PROTO_VERSION_MINOR == 0);
1681         LASSERT (PTL_MSG_ACK == 0);
1682         LASSERT (PTL_MSG_PUT == 1);
1683         LASSERT (PTL_MSG_GET == 2);
1684         LASSERT (PTL_MSG_REPLY == 3);
1685         LASSERT (PTL_MSG_HELLO == 4);
1686
1687         /* Checks for struct ptl_handle_wire_t */
1688         LASSERT ((int)sizeof(ptl_handle_wire_t) == 16);
1689         LASSERT ((int)offsetof(ptl_handle_wire_t, wh_interface_cookie) == 0);
1690         LASSERT ((int)sizeof(((ptl_handle_wire_t *)0)->wh_interface_cookie) == 8);
1691         LASSERT ((int)offsetof(ptl_handle_wire_t, wh_object_cookie) == 8);
1692         LASSERT ((int)sizeof(((ptl_handle_wire_t *)0)->wh_object_cookie) == 8);
1693
1694         /* Checks for struct ptl_magicversion_t */
1695         LASSERT ((int)sizeof(ptl_magicversion_t) == 8);
1696         LASSERT ((int)offsetof(ptl_magicversion_t, magic) == 0);
1697         LASSERT ((int)sizeof(((ptl_magicversion_t *)0)->magic) == 4);
1698         LASSERT ((int)offsetof(ptl_magicversion_t, version_major) == 4);
1699         LASSERT ((int)sizeof(((ptl_magicversion_t *)0)->version_major) == 2);
1700         LASSERT ((int)offsetof(ptl_magicversion_t, version_minor) == 6);
1701         LASSERT ((int)sizeof(((ptl_magicversion_t *)0)->version_minor) == 2);
1702
1703         /* Checks for struct ptl_hdr_t */
1704         LASSERT ((int)sizeof(ptl_hdr_t) == 72);
1705         LASSERT ((int)offsetof(ptl_hdr_t, dest_nid) == 0);
1706         LASSERT ((int)sizeof(((ptl_hdr_t *)0)->dest_nid) == 8);
1707         LASSERT ((int)offsetof(ptl_hdr_t, src_nid) == 8);
1708         LASSERT ((int)sizeof(((ptl_hdr_t *)0)->src_nid) == 8);
1709         LASSERT ((int)offsetof(ptl_hdr_t, dest_pid) == 16);
1710         LASSERT ((int)sizeof(((ptl_hdr_t *)0)->dest_pid) == 4);
1711         LASSERT ((int)offsetof(ptl_hdr_t, src_pid) == 20);
1712         LASSERT ((int)sizeof(((ptl_hdr_t *)0)->src_pid) == 4);
1713         LASSERT ((int)offsetof(ptl_hdr_t, type) == 24);
1714         LASSERT ((int)sizeof(((ptl_hdr_t *)0)->type) == 4);
1715         LASSERT ((int)offsetof(ptl_hdr_t, payload_length) == 28);
1716         LASSERT ((int)sizeof(((ptl_hdr_t *)0)->payload_length) == 4);
1717         LASSERT ((int)offsetof(ptl_hdr_t, msg) == 32);
1718         LASSERT ((int)sizeof(((ptl_hdr_t *)0)->msg) == 40);
1719
1720         /* Ack */
1721         LASSERT ((int)offsetof(ptl_hdr_t, msg.ack.dst_wmd) == 32);
1722         LASSERT ((int)sizeof(((ptl_hdr_t *)0)->msg.ack.dst_wmd) == 16);
1723         LASSERT ((int)offsetof(ptl_hdr_t, msg.ack.match_bits) == 48);
1724         LASSERT ((int)sizeof(((ptl_hdr_t *)0)->msg.ack.match_bits) == 8);
1725         LASSERT ((int)offsetof(ptl_hdr_t, msg.ack.mlength) == 56);
1726         LASSERT ((int)sizeof(((ptl_hdr_t *)0)->msg.ack.mlength) == 4);
1727
1728         /* Put */
1729         LASSERT ((int)offsetof(ptl_hdr_t, msg.put.ack_wmd) == 32);
1730         LASSERT ((int)sizeof(((ptl_hdr_t *)0)->msg.put.ack_wmd) == 16);
1731         LASSERT ((int)offsetof(ptl_hdr_t, msg.put.match_bits) == 48);
1732         LASSERT ((int)sizeof(((ptl_hdr_t *)0)->msg.put.match_bits) == 8);
1733         LASSERT ((int)offsetof(ptl_hdr_t, msg.put.hdr_data) == 56);
1734         LASSERT ((int)sizeof(((ptl_hdr_t *)0)->msg.put.hdr_data) == 8);
1735         LASSERT ((int)offsetof(ptl_hdr_t, msg.put.ptl_index) == 64);
1736         LASSERT ((int)sizeof(((ptl_hdr_t *)0)->msg.put.ptl_index) == 4);
1737         LASSERT ((int)offsetof(ptl_hdr_t, msg.put.offset) == 68);
1738         LASSERT ((int)sizeof(((ptl_hdr_t *)0)->msg.put.offset) == 4);
1739
1740         /* Get */
1741         LASSERT ((int)offsetof(ptl_hdr_t, msg.get.return_wmd) == 32);
1742         LASSERT ((int)sizeof(((ptl_hdr_t *)0)->msg.get.return_wmd) == 16);
1743         LASSERT ((int)offsetof(ptl_hdr_t, msg.get.match_bits) == 48);
1744         LASSERT ((int)sizeof(((ptl_hdr_t *)0)->msg.get.match_bits) == 8);
1745         LASSERT ((int)offsetof(ptl_hdr_t, msg.get.ptl_index) == 56);
1746         LASSERT ((int)sizeof(((ptl_hdr_t *)0)->msg.get.ptl_index) == 4);
1747         LASSERT ((int)offsetof(ptl_hdr_t, msg.get.src_offset) == 60);
1748         LASSERT ((int)sizeof(((ptl_hdr_t *)0)->msg.get.src_offset) == 4);
1749         LASSERT ((int)offsetof(ptl_hdr_t, msg.get.sink_length) == 64);
1750         LASSERT ((int)sizeof(((ptl_hdr_t *)0)->msg.get.sink_length) == 4);
1751
1752         /* Reply */
1753         LASSERT ((int)offsetof(ptl_hdr_t, msg.reply.dst_wmd) == 32);
1754         LASSERT ((int)sizeof(((ptl_hdr_t *)0)->msg.reply.dst_wmd) == 16);
1755
1756         /* Hello */
1757         LASSERT ((int)offsetof(ptl_hdr_t, msg.hello.incarnation) == 32);
1758         LASSERT ((int)sizeof(((ptl_hdr_t *)0)->msg.hello.incarnation) == 8);
1759         LASSERT ((int)offsetof(ptl_hdr_t, msg.hello.type) == 40);
1760         LASSERT ((int)sizeof(((ptl_hdr_t *)0)->msg.hello.type) == 4);
1761 }