1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
5 * Data movement routines
7 * Copyright (c) 2001-2003 Cluster File Systems, Inc.
8 * Copyright (c) 2001-2002 Sandia National Laboratories
10 * This file is part of Lustre, http://www.sf.net/projects/lustre/
12 * Lustre is free software; you can redistribute it and/or
13 * modify it under the terms of version 2 of the GNU General Public
14 * License as published by the Free Software Foundation.
16 * Lustre is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with Lustre; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 # define DEBUG_SUBSYSTEM S_PORTALS
30 # include <linux/kp30.h>
32 #include <portals/p30.h>
33 #include <portals/lib-p30.h>
36 static void lib_commit_md (lib_nal_t *nal, lib_md_t *md, lib_msg_t *msg);
39 lib_match_md(lib_nal_t *nal, int index, int op_mask,
40 ptl_nid_t src_nid, ptl_pid_t src_pid,
41 ptl_size_t rlength, ptl_size_t roffset,
42 ptl_match_bits_t match_bits, lib_msg_t *msg,
43 ptl_size_t *mlength_out, ptl_size_t *offset_out)
45 lib_ni_t *ni = &nal->libnal_ni;
46 struct list_head *match_list = &ni->ni_portals.tbl[index];
47 struct list_head *tmp;
54 CDEBUG (D_NET, "Request from "LPU64".%d of length %d into portal %d "
55 "MB="LPX64"\n", src_nid, src_pid, rlength, index, match_bits);
57 if (index < 0 || index >= ni->ni_portals.size) {
58 CERROR("Invalid portal %d not in [0-%d]\n",
59 index, ni->ni_portals.size);
63 list_for_each (tmp, match_list) {
64 me = list_entry(tmp, lib_me_t, me_list);
67 /* ME attached but MD not attached yet */
71 LASSERT (me == md->me);
73 /* mismatched MD op */
74 if ((md->options & op_mask) == 0)
78 if (lib_md_exhausted(md))
81 /* mismatched ME nid/pid? */
82 if (me->match_id.nid != PTL_NID_ANY &&
83 me->match_id.nid != src_nid)
86 CDEBUG(D_NET,"match_id.pid [%x], src_pid [%x]\n", me->match_id.pid, src_pid);
88 if (me->match_id.pid != PTL_PID_ANY &&
89 me->match_id.pid != src_pid)
92 /* mismatched ME matchbits? */
93 if (((me->match_bits ^ match_bits) & ~me->ignore_bits) != 0)
96 /* Hurrah! This _is_ a match; check it out... */
98 if ((md->options & PTL_MD_MANAGE_REMOTE) == 0)
103 if ((md->options & PTL_MD_MAX_SIZE) != 0) {
104 mlength = md->max_size;
105 LASSERT (md->offset + mlength <= md->length);
107 mlength = md->length - offset;
110 if (rlength <= mlength) { /* fits in allowed space */
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,
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);
127 lib_commit_md(nal, md, msg);
128 md->offset = offset + mlength;
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;
139 lib_md_deconstruct(nal, md, &msg->ev.md);
140 ptl_md2handle(&msg->ev.md_handle, nal, md);
142 *offset_out = offset;
143 *mlength_out = mlength;
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);
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);
163 int lib_api_fail_nid (nal_t *apinal, ptl_nid_t nid, unsigned int threshold)
165 lib_nal_t *nal = apinal->nal_data;
168 struct list_head *el;
169 struct list_head *next;
170 struct list_head cull;
172 if (threshold != 0) {
173 /* Adding a new entry */
174 PORTAL_ALLOC(tp, sizeof(*tp));
179 tp->tp_threshold = threshold;
181 LIB_LOCK(nal, flags);
182 list_add_tail (&tp->tp_list, &nal->libnal_ni.ni_test_peers);
183 LIB_UNLOCK(nal, flags);
187 /* removing entries */
188 INIT_LIST_HEAD (&cull);
190 LIB_LOCK(nal, flags);
192 list_for_each_safe (el, next, &nal->libnal_ni.ni_test_peers) {
193 tp = list_entry (el, lib_test_peer_t, tp_list);
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 */
199 list_del (&tp->tp_list);
200 list_add (&tp->tp_list, &cull);
204 LIB_UNLOCK(nal, flags);
206 while (!list_empty (&cull)) {
207 tp = list_entry (cull.next, lib_test_peer_t, tp_list);
209 list_del (&tp->tp_list);
210 PORTAL_FREE(tp, sizeof (*tp));
216 fail_peer (lib_nal_t *nal, ptl_nid_t nid, int outgoing)
219 struct list_head *el;
220 struct list_head *next;
222 struct list_head cull;
225 INIT_LIST_HEAD (&cull);
227 LIB_LOCK (nal, flags);
229 list_for_each_safe (el, next, &nal->libnal_ni.ni_test_peers) {
230 tp = list_entry (el, lib_test_peer_t, tp_list);
232 if (tp->tp_threshold == 0) {
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);
244 if (tp->tp_nid == PTL_NID_ANY || /* fail every peer */
245 nid == tp->tp_nid) { /* fail this peer */
248 if (tp->tp_threshold != PTL_MD_THRESH_INF) {
251 tp->tp_threshold == 0) {
253 list_del (&tp->tp_list);
254 list_add (&tp->tp_list, &cull);
261 LIB_UNLOCK (nal, flags);
263 while (!list_empty (&cull)) {
264 tp = list_entry (cull.next, lib_test_peer_t, tp_list);
265 list_del (&tp->tp_list);
267 PORTAL_FREE(tp, sizeof (*tp));
274 lib_iov_nob (int niov, struct iovec *iov)
279 nob += (iov++)->iov_len;
285 lib_copy_iov2buf (char *dest, int niov, struct iovec *iov,
286 ptl_size_t offset, ptl_size_t len)
293 /* skip complete frags before 'offset' */
295 while (offset >= iov->iov_len) {
296 offset -= iov->iov_len;
304 nob = MIN (iov->iov_len - offset, len);
305 memcpy (dest, iov->iov_base + offset, nob);
316 lib_copy_buf2iov (int niov, struct iovec *iov, ptl_size_t offset,
317 char *src, ptl_size_t len)
324 /* skip complete frags before 'offset' */
326 while (offset >= iov->iov_len) {
327 offset -= iov->iov_len;
335 nob = MIN (iov->iov_len - offset, len);
336 memcpy (iov->iov_base + offset, src, nob);
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)
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' */
357 if (len == 0) /* no data => */
358 return (0); /* no frags */
360 LASSERT (src_niov > 0);
361 while (offset >= src->iov_len) { /* skip initial frags */
362 offset -= src->iov_len;
365 LASSERT (src_niov > 0);
370 LASSERT (src_niov > 0);
371 LASSERT (niov <= dst_niov);
373 frag_len = src->iov_len - offset;
374 dst->iov_base = ((char *)src->iov_base) + offset;
376 if (len <= frag_len) {
381 dst->iov_len = frag_len;
394 lib_kiov_nob (int niov, ptl_kiov_t *kiov)
401 lib_copy_kiov2buf (char *dest, int niov, ptl_kiov_t *kiov,
402 ptl_size_t offset, ptl_size_t len)
408 lib_copy_buf2kiov (int niov, ptl_kiov_t *kiov, ptl_size_t offset,
409 char *src, ptl_size_t len)
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)
425 lib_kiov_nob (int niov, ptl_kiov_t *kiov)
430 nob += (kiov++)->kiov_len;
436 lib_copy_kiov2buf (char *dest, int niov, ptl_kiov_t *kiov,
437 ptl_size_t offset, ptl_size_t len)
445 LASSERT (!in_interrupt ());
448 while (offset > kiov->kiov_len) {
449 offset -= kiov->kiov_len;
457 nob = MIN (kiov->kiov_len - offset, len);
459 addr = ((char *)kmap (kiov->kiov_page)) + kiov->kiov_offset + offset;
460 memcpy (dest, addr, nob);
461 kunmap (kiov->kiov_page);
472 lib_copy_buf2kiov (int niov, ptl_kiov_t *kiov, ptl_size_t offset,
473 char *src, ptl_size_t len)
481 LASSERT (!in_interrupt ());
484 while (offset >= kiov->kiov_len) {
485 offset -= kiov->kiov_len;
493 nob = MIN (kiov->kiov_len - offset, len);
495 addr = ((char *)kmap (kiov->kiov_page)) + kiov->kiov_offset + offset;
496 memcpy (addr, src, nob);
497 kunmap (kiov->kiov_page);
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)
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' */
518 if (len == 0) /* no data => */
519 return (0); /* no frags */
521 LASSERT (src_niov > 0);
522 while (offset >= src->kiov_len) { /* skip initial frags */
523 offset -= src->kiov_len;
526 LASSERT (src_niov > 0);
531 LASSERT (src_niov > 0);
532 LASSERT (niov <= dst_niov);
534 frag_len = src->kiov_len - offset;
535 dst->kiov_page = src->kiov_page;
536 dst->kiov_offset = src->kiov_offset + offset;
538 if (len <= frag_len) {
540 LASSERT (dst->kiov_offset + dst->kiov_len <= PAGE_SIZE);
544 dst->kiov_len = frag_len;
545 LASSERT (dst->kiov_offset + dst->kiov_len <= PAGE_SIZE);
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)
562 return (nal->libnal_recv(nal, private, msg,
564 offset, mlen, rlen));
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));
571 return (nal->libnal_recv_pages(nal, private, msg,
572 md->md_niov, md->md_iov.kiov,
573 offset, mlen, rlen));
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)
582 return (nal->libnal_send(nal, private, msg,
587 if ((md->options & PTL_MD_KIOV) == 0)
588 return (nal->libnal_send(nal, private, msg,
590 md->md_niov, md->md_iov.iov,
593 return (nal->libnal_send_pages(nal, private, msg,
595 md->md_niov, md->md_iov.kiov,
600 lib_commit_md (lib_nal_t *nal, lib_md_t *md, lib_msg_t *msg)
602 /* ALWAYS called holding the LIB_LOCK */
603 lib_counters_t *counters = &nal->libnal_ni.ni_counters;
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. */
611 if (md->threshold != PTL_MD_THRESH_INF) {
612 LASSERT (md->threshold > 0);
616 counters->msgs_alloc++;
617 if (counters->msgs_alloc > counters->msgs_max)
618 counters->msgs_max = counters->msgs_alloc;
620 list_add (&msg->msg_list, &nal->libnal_ni.ni_active_msgs);
624 lib_drop_message (lib_nal_t *nal, void *private, ptl_hdr_t *hdr)
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
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);
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);
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
649 parse_put(lib_nal_t *nal, ptl_hdr_t *hdr, void *private, lib_msg_t *msg)
651 lib_ni_t *ni = &nal->libnal_ni;
652 ptl_size_t mlength = 0;
653 ptl_size_t offset = 0;
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);
663 LIB_LOCK(nal, flags);
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,
671 LIB_UNLOCK(nal, flags);
675 msg->ev.type = PTL_EVENT_PUT_END;
676 msg->ev.hdr_data = hdr->msg.put.hdr_data;
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;
683 ni->ni_counters.recv_count++;
684 ni->ni_counters.recv_length += mlength;
686 LIB_UNLOCK(nal, flags);
688 rc = lib_recv(nal, private, msg, md, offset, mlength,
689 hdr->payload_length);
691 CERROR(LPU64": error on receiving PUT from "LPU64": %d\n",
692 ni->ni_pid.nid, hdr->src_nid, rc);
698 parse_get(lib_nal_t *nal, ptl_hdr_t *hdr, void *private, lib_msg_t *msg)
700 lib_ni_t *ni = &nal->libnal_ni;
701 ptl_size_t mlength = 0;
702 ptl_size_t offset = 0;
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);
714 LIB_LOCK(nal, flags);
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,
722 LIB_UNLOCK(nal, flags);
726 msg->ev.type = PTL_EVENT_GET_END;
727 msg->ev.hdr_data = 0;
729 ni->ni_counters.send_count++;
730 ni->ni_counters.send_length += mlength;
732 LIB_UNLOCK(nal, flags);
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);
742 reply.msg.reply.dst_wmd = hdr->msg.get.return_wmd;
744 /* NB call lib_send() _BEFORE_ lib_recv() completes the incoming
745 * message. Some NALs _require_ this to implement optimized GET */
747 rc = lib_send (nal, private, msg, &reply, PTL_MSG_REPLY,
748 hdr->src_nid, hdr->src_pid, md, offset, mlength);
750 CERROR(LPU64": Unable to send REPLY for GET from "LPU64": %d\n",
751 ni->ni_pid.nid, hdr->src_nid, rc);
753 /* Discard any junk after the hdr */
754 (void) lib_recv(nal, private, NULL, NULL, 0, 0, hdr->payload_length);
760 parse_reply(lib_nal_t *nal, ptl_hdr_t *hdr, void *private, lib_msg_t *msg)
762 lib_ni_t *ni = &nal->libnal_ni;
769 LIB_LOCK(nal, flags);
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);
780 LIB_UNLOCK(nal, flags);
784 LASSERT (md->offset == 0);
786 length = rlength = hdr->payload_length;
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,
795 LIB_UNLOCK(nal, flags);
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);
805 lib_commit_md(nal, md, msg);
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;
814 lib_md_deconstruct(nal, md, &msg->ev.md);
815 ptl_md2handle(&msg->ev.md_handle, nal, md);
817 ni->ni_counters.recv_count++;
818 ni->ni_counters.recv_length += length;
820 LIB_UNLOCK(nal, flags);
822 rc = lib_recv(nal, private, msg, md, 0, length, rlength);
824 CERROR(LPU64": error on receiving REPLY from "LPU64": %d\n",
825 ni->ni_pid.nid, hdr->src_nid, rc);
831 parse_ack(lib_nal_t *nal, ptl_hdr_t *hdr, void *private, lib_msg_t *msg)
833 lib_ni_t *ni = &nal->libnal_ni;
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);
841 LIB_LOCK(nal, flags);
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);
852 LIB_UNLOCK(nal, flags);
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);
860 lib_commit_md(nal, md, msg);
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;
868 lib_md_deconstruct(nal, md, &msg->ev.md);
869 ptl_md2handle(&msg->ev.md_handle, nal, md);
871 ni->ni_counters.recv_count++;
873 LIB_UNLOCK(nal, flags);
875 /* We have received and matched up the ack OK, create the
876 * completion event now... */
877 lib_finalize(nal, private, msg, PTL_OK);
879 /* ...and now discard any junk after the hdr */
880 (void) lib_recv(nal, private, NULL, NULL, 0, 0, hdr->payload_length);
886 hdr_type_string (ptl_hdr_t *hdr)
900 return ("<UNKNOWN>");
904 void print_hdr(lib_nal_t *nal, ptl_hdr_t * hdr)
906 char *type_str = hdr_type_string (hdr);
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);
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);
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);
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);
948 CWARN(" dst md "LPX64"."LPX64", "
950 hdr->msg.reply.dst_wmd.wh_interface_cookie,
951 hdr->msg.reply.dst_wmd.wh_object_cookie,
952 hdr->payload_length);
955 } /* end of print_hdr() */
959 lib_parse(lib_nal_t *nal, ptl_hdr_t *hdr, void *private)
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 */
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);
977 case PTL_MSG_HELLO: {
978 /* dest_nid is really ptl_magicversion_t */
979 ptl_magicversion_t *mv = (ptl_magicversion_t *)&hdr->dest_nid;
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);
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,
994 /* it's good but we don't want it */
995 lib_drop_message(nal, private, hdr);
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,
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);
1023 CERROR(LPU64": Bad message type 0x%x from "LPU64"\n",
1024 nal->libnal_ni.ni_pid.nid, hdr->type, hdr->src_nid);
1028 /* We've decided we're not receiving garbage since we can parse the
1029 * header. We will return PTL_OK come what may... */
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? */
1034 CERROR(LPU64": Dropping incoming %s from "LPU64
1035 ": simulated failure\n",
1036 nal->libnal_ni.ni_pid.nid, hdr_type_string (hdr),
1038 lib_drop_message(nal, private, hdr);
1042 msg = lib_msg_alloc(nal);
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),
1048 lib_drop_message(nal, private, hdr);
1052 switch (hdr->type) {
1054 rc = parse_ack(nal, hdr, private, msg);
1057 rc = parse_put(nal, hdr, private, msg);
1060 rc = parse_get(nal, hdr, private, msg);
1063 rc = parse_reply(nal, hdr, private, msg);
1067 rc = PTL_FAIL; /* no compiler warning please */
1072 if (msg->md != NULL) {
1074 lib_finalize(nal, private, msg, rc);
1076 LIB_LOCK(nal, flags);
1077 lib_msg_free(nal, msg); /* expects LIB_LOCK held */
1078 LIB_UNLOCK(nal, flags);
1080 lib_drop_message(nal, private, hdr);
1085 /* That's "OK I can parse it", not "OK I like it" :) */
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)
1095 lib_nal_t *nal = apinal->nal_data;
1096 lib_ni_t *ni = &nal->libnal_ni;
1100 unsigned long flags;
1103 if (!list_empty (&ni->ni_test_peers) && /* normally we don't */
1104 fail_peer (nal, id->nid, 1)) /* shall we now? */
1106 CERROR("Dropping PUT to "LPU64": simulated failure\n",
1108 return PTL_PROCESS_INVALID;
1111 msg = lib_msg_alloc(nal);
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;
1118 LIB_LOCK(nal, flags);
1120 md = ptl_handle2md(mdh, nal);
1121 if (md == NULL || md->threshold == 0) {
1122 lib_msg_free(nal, msg);
1123 LIB_UNLOCK(nal, flags);
1125 return PTL_MD_INVALID;
1128 CDEBUG(D_NET, "PtlPut -> "LPX64"\n", id->nid);
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);
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;
1143 hdr.msg.put.ack_wmd = PTL_WIRE_HANDLE_NONE;
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;
1151 lib_commit_md(nal, md, msg);
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;
1163 lib_md_deconstruct(nal, md, &msg->ev.md);
1164 ptl_md2handle(&msg->ev.md_handle, nal, md);
1166 ni->ni_counters.send_count++;
1167 ni->ni_counters.send_length += md->length;
1169 LIB_UNLOCK(nal, flags);
1171 rc = lib_send (nal, NULL, msg, &hdr, PTL_MSG_PUT,
1172 id->nid, id->pid, md, 0, md->length);
1174 CERROR("Error sending PUT to "LPX64": %d\n",
1176 lib_finalize (nal, NULL, msg, rc);
1179 /* completion will be signalled by an event */
1184 lib_create_reply_msg (lib_nal_t *nal, ptl_nid_t peer_nid, lib_msg_t *getmsg)
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.
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 */
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;
1198 LIB_LOCK(nal, flags);
1200 LASSERT (getmd->pending > 0);
1203 CERROR ("Dropping REPLY from "LPU64": can't allocate msg\n",
1208 if (getmd->threshold == 0) {
1209 CERROR ("Dropping REPLY from "LPU64" for inactive MD %p\n",
1214 LASSERT (getmd->offset == 0);
1216 CDEBUG(D_NET, "Reply from "LPU64" md %p\n", peer_nid, getmd);
1218 lib_commit_md (nal, getmd, msg);
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;
1226 lib_md_deconstruct(nal, getmd, &msg->ev.md);
1227 ptl_md2handle(&msg->ev.md_handle, nal, getmd);
1229 ni->ni_counters.recv_count++;
1230 ni->ni_counters.recv_length += getmd->length;
1232 LIB_UNLOCK(nal, flags);
1237 lib_msg_free(nal, msg);
1239 nal->libnal_ni.ni_counters.drop_count++;
1240 nal->libnal_ni.ni_counters.drop_length += getmd->length;
1242 LIB_UNLOCK (nal, flags);
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)
1252 lib_nal_t *nal = apinal->nal_data;
1253 lib_ni_t *ni = &nal->libnal_ni;
1257 unsigned long flags;
1260 if (!list_empty (&ni->ni_test_peers) && /* normally we don't */
1261 fail_peer (nal, id->nid, 1)) /* shall we now? */
1263 CERROR("Dropping PUT to "LPX64": simulated failure\n",
1265 return PTL_PROCESS_INVALID;
1268 msg = lib_msg_alloc(nal);
1270 CERROR("Dropping GET to "LPU64": ENOMEM on lib_msg_t\n",
1272 return PTL_NO_SPACE;
1275 LIB_LOCK(nal, flags);
1277 md = ptl_handle2md(mdh, nal);
1278 if (md == NULL || !md->threshold) {
1279 lib_msg_free(nal, msg);
1280 LIB_UNLOCK(nal, flags);
1282 return PTL_MD_INVALID;
1285 CDEBUG(D_NET, "PtlGet -> %Lu: %lu\n", (unsigned long long)id->nid,
1286 (unsigned long)id->pid);
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;
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;
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);
1305 lib_commit_md(nal, md, msg);
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;
1316 lib_md_deconstruct(nal, md, &msg->ev.md);
1317 ptl_md2handle(&msg->ev.md_handle, nal, md);
1319 ni->ni_counters.send_count++;
1321 LIB_UNLOCK(nal, flags);
1323 rc = lib_send (nal, NULL, msg, &hdr, PTL_MSG_GET,
1324 id->nid, id->pid, NULL, 0, 0);
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);
1331 /* completion will be signalled by an event */
1335 void lib_assert_wire_constants (void)
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) */
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);
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);
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);
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);
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);
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);
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);
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);
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);