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