Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / lnet / klnds / viblnd / wirecheck.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #include <stdio.h>
38 #include <string.h>
39 #include <sys/types.h>
40 #include <sys/wait.h>
41
42 #include <lnet/api-support.h>
43
44 /* This ghastly hack to allows me to include lib-types.h It doesn't affect any
45  * assertions generated here (but fails-safe if it ever does) */
46 typedef struct {
47         int     counter;
48 } atomic_t;
49
50 #include <lnet/lib-types.h>
51
52 #define IBNAL_USE_FMR 1
53 #include "viblnd_wire.h"
54
55 #ifndef HAVE_STRNLEN
56 #define strnlen(s, i) strlen(s)
57 #endif
58
59 #define BLANK_LINE()                            \
60 do {                                            \
61         printf ("\n");                          \
62 } while (0)
63
64 #define COMMENT(c)                              \
65 do {                                            \
66         printf ("        /* "c" */\n");         \
67 } while (0)
68
69 #undef STRINGIFY
70 #define STRINGIFY(a) #a
71
72 #define CHECK_DEFINE(a)                                         \
73 do {                                                            \
74         printf ("        CLASSERT ("#a" == "STRINGIFY(a)");\n"); \
75 } while (0)
76
77 #define CHECK_VALUE(a)                                  \
78 do {                                                    \
79         printf ("        CLASSERT ("#a" == %d);\n", a);  \
80 } while (0)
81
82 #define CHECK_MEMBER_OFFSET(s,m)                \
83 do {                                            \
84         CHECK_VALUE((int)offsetof(s, m));       \
85 } while (0)
86
87 #define CHECK_MEMBER_SIZEOF(s,m)                \
88 do {                                            \
89         CHECK_VALUE((int)sizeof(((s *)0)->m));  \
90 } while (0)
91
92 #define CHECK_MEMBER(s,m)                       \
93 do {                                            \
94         CHECK_MEMBER_OFFSET(s, m);              \
95         CHECK_MEMBER_SIZEOF(s, m);              \
96 } while (0)
97
98 #define CHECK_STRUCT(s)                         \
99 do {                                            \
100         BLANK_LINE ();                          \
101         COMMENT ("Checks for struct "#s);       \
102         CHECK_VALUE((int)sizeof(s));            \
103 } while (0)
104
105 void
106 system_string (char *cmdline, char *str, int len)
107 {
108         int   fds[2];
109         int   rc;
110         pid_t pid;
111
112         rc = pipe (fds);
113         if (rc != 0)
114                 abort ();
115
116         pid = fork ();
117         if (pid == 0) {
118                 /* child */
119                 int   fd = fileno(stdout);
120
121                 rc = dup2(fds[1], fd);
122                 if (rc != fd)
123                         abort();
124
125                 exit(system(cmdline));
126                 /* notreached */
127         } else if ((int)pid < 0) {
128                 abort();
129         } else {
130                 FILE *f = fdopen (fds[0], "r");
131
132                 if (f == NULL)
133                         abort();
134
135                 close(fds[1]);
136
137                 if (fgets(str, len, f) == NULL)
138                         abort();
139
140                 if (waitpid(pid, &rc, 0) != pid)
141                         abort();
142
143                 if (!WIFEXITED(rc) ||
144                     WEXITSTATUS(rc) != 0)
145                         abort();
146
147                 if (strnlen(str, len) == len)
148                         str[len - 1] = 0;
149
150                 if (str[strlen(str) - 1] == '\n')
151                         str[strlen(str) - 1] = 0;
152
153                 fclose(f);
154         }
155 }
156
157 int
158 main (int argc, char **argv)
159 {
160         char unameinfo[80];
161         char gccinfo[80];
162
163         system_string("uname -a", unameinfo, sizeof(unameinfo));
164         system_string("gcc -v 2>&1 | tail -1", gccinfo, sizeof(gccinfo));
165
166         printf ("void vibnal_assert_wire_constants (void)\n"
167                 "{\n"
168                 "        /* Wire protocol assertions generated by 'wirecheck'\n"
169                 "         * running on %s\n"
170                 "         * with %s */\n"
171                 "\n", unameinfo, gccinfo);
172
173         BLANK_LINE ();
174         
175         COMMENT ("Constants...");
176         CHECK_DEFINE (IBNAL_MSG_MAGIC);
177         CHECK_DEFINE (IBNAL_MSG_VERSION);
178
179         CHECK_DEFINE (IBNAL_MSG_CONNREQ);
180         CHECK_DEFINE (IBNAL_MSG_CONNACK);
181         CHECK_DEFINE (IBNAL_MSG_NOOP);
182         CHECK_DEFINE (IBNAL_MSG_IMMEDIATE);
183         CHECK_DEFINE (IBNAL_MSG_PUT_REQ);
184         CHECK_DEFINE (IBNAL_MSG_PUT_NAK);
185         CHECK_DEFINE (IBNAL_MSG_PUT_ACK);
186         CHECK_DEFINE (IBNAL_MSG_PUT_DONE);
187         CHECK_DEFINE (IBNAL_MSG_GET_REQ);
188         CHECK_DEFINE (IBNAL_MSG_GET_DONE);
189
190         CHECK_DEFINE (IBNAL_REJECT_CONN_RACE);
191         CHECK_DEFINE (IBNAL_REJECT_NO_RESOURCES);
192         CHECK_DEFINE (IBNAL_REJECT_FATAL);
193
194         CHECK_STRUCT (kib_connparams_t);
195         CHECK_MEMBER (kib_connparams_t, ibcp_queue_depth);
196         CHECK_MEMBER (kib_connparams_t, ibcp_max_msg_size);
197         CHECK_MEMBER (kib_connparams_t, ibcp_max_frags);
198
199         CHECK_STRUCT (kib_immediate_msg_t);
200         CHECK_MEMBER (kib_immediate_msg_t, ibim_hdr);
201         CHECK_MEMBER (kib_immediate_msg_t, ibim_payload[13]);
202
203         CHECK_DEFINE (IBNAL_USE_FMR);
204 #if IBNAL_USE_FMR
205         CHECK_STRUCT (kib_rdma_desc_t);
206         CHECK_MEMBER (kib_rdma_desc_t, rd_addr);
207         CHECK_MEMBER (kib_rdma_desc_t, rd_nob);
208         CHECK_MEMBER (kib_rdma_desc_t, rd_key);
209 #else
210         CHECK_STRUCT (kib_rdma_frag_t);
211         CHECK_MEMBER (kib_rdma_frag_t, rf_nob);
212         CHECK_MEMBER (kib_rdma_frag_t, rf_addr_lo);
213         CHECK_MEMBER (kib_rdma_frag_t, rf_addr_hi);
214
215         CHECK_STRUCT (kib_rdma_desc_t);
216         CHECK_MEMBER (kib_rdma_desc_t, rd_key);
217         CHECK_MEMBER (kib_rdma_desc_t, rd_nfrag);
218         CHECK_MEMBER (kib_rdma_desc_t, rd_frags[13]);
219 #endif
220         CHECK_STRUCT (kib_putreq_msg_t);
221         CHECK_MEMBER (kib_putreq_msg_t, ibprm_hdr);
222         CHECK_MEMBER (kib_putreq_msg_t, ibprm_cookie);
223
224         CHECK_STRUCT (kib_putack_msg_t);
225         CHECK_MEMBER (kib_putack_msg_t, ibpam_src_cookie);
226         CHECK_MEMBER (kib_putack_msg_t, ibpam_dst_cookie);
227         CHECK_MEMBER (kib_putack_msg_t, ibpam_rd);
228
229         CHECK_STRUCT (kib_get_msg_t);
230         CHECK_MEMBER (kib_get_msg_t, ibgm_hdr);
231         CHECK_MEMBER (kib_get_msg_t, ibgm_cookie);
232         CHECK_MEMBER (kib_get_msg_t, ibgm_rd);
233
234         CHECK_STRUCT (kib_completion_msg_t);
235         CHECK_MEMBER (kib_completion_msg_t, ibcm_cookie);
236         CHECK_MEMBER (kib_completion_msg_t, ibcm_status);
237
238         CHECK_STRUCT (kib_msg_t);
239         CHECK_MEMBER (kib_msg_t, ibm_magic);
240         CHECK_MEMBER (kib_msg_t, ibm_version);
241         CHECK_MEMBER (kib_msg_t, ibm_type);
242         CHECK_MEMBER (kib_msg_t, ibm_credits);
243         CHECK_MEMBER (kib_msg_t, ibm_nob);
244         CHECK_MEMBER (kib_msg_t, ibm_cksum);
245         CHECK_MEMBER (kib_msg_t, ibm_srcnid);
246         CHECK_MEMBER (kib_msg_t, ibm_srcstamp);
247         CHECK_MEMBER (kib_msg_t, ibm_dstnid);
248         CHECK_MEMBER (kib_msg_t, ibm_dststamp);
249         CHECK_MEMBER (kib_msg_t, ibm_seq);
250         CHECK_MEMBER (kib_msg_t, ibm_u.connparams);
251         CHECK_MEMBER (kib_msg_t, ibm_u.immediate);
252         CHECK_MEMBER (kib_msg_t, ibm_u.putreq);
253         CHECK_MEMBER (kib_msg_t, ibm_u.putack);
254         CHECK_MEMBER (kib_msg_t, ibm_u.get);
255         CHECK_MEMBER (kib_msg_t, ibm_u.completion);
256
257         printf ("}\n\n");
258
259         return (0);
260 }