Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / lnet / klnds / ptllnd / 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  * lnet/klnds/ptllnd/wirecheck.c
37  *
38  * Author: PJ Kirner <pjkirner@clusterfs.com>
39  */
40 #include <stdio.h>
41 #include <string.h>
42 #include <sys/types.h>
43 #include <sys/wait.h>
44
45 #include <config.h>
46
47 #include <lnet/api-support.h>
48
49 /* This ghastly hack to allows me to include lib-types.h It doesn't affect any
50  * assertions generated here (but fails-safe if it ever does) */
51 typedef struct {
52         int     counter;
53 } atomic_t;
54
55 #include <lnet/lib-types.h>
56 #include <lnet/ptllnd_wire.h>
57
58 #ifndef HAVE_STRNLEN
59 #define strnlen(s, i) strlen(s)
60 #endif
61
62 #define BLANK_LINE()                            \
63 do {                                            \
64         printf ("\n");                          \
65 } while (0)
66
67 #define COMMENT(c)                              \
68 do {                                            \
69         printf ("        /* "c" */\n");         \
70 } while (0)
71
72 #undef STRINGIFY
73 #define STRINGIFY(a) #a
74
75 #define CHECK_DEFINE(a)                                         \
76 do {                                                            \
77         printf ("        CLASSERT ("#a" == "STRINGIFY(a)");\n"); \
78 } while (0)
79
80 #define CHECK_VALUE(a)                                  \
81 do {                                                    \
82         printf ("        CLASSERT ("#a" == %d);\n", a);  \
83 } while (0)
84
85 #define CHECK_MEMBER_OFFSET(s,m)                \
86 do {                                            \
87         CHECK_VALUE((int)offsetof(s, m));       \
88 } while (0)
89
90 #define CHECK_MEMBER_SIZEOF(s,m)                \
91 do {                                            \
92         CHECK_VALUE((int)sizeof(((s *)0)->m));  \
93 } while (0)
94
95 #define CHECK_MEMBER(s,m)                       \
96 do {                                            \
97         CHECK_MEMBER_OFFSET(s, m);              \
98         CHECK_MEMBER_SIZEOF(s, m);              \
99 } while (0)
100
101 #define CHECK_STRUCT(s)                         \
102 do {                                            \
103         BLANK_LINE ();                          \
104         COMMENT ("Checks for struct "#s);       \
105         CHECK_VALUE((int)sizeof(s));            \
106 } while (0)
107
108 void
109 system_string (char *cmdline, char *str, int len)
110 {
111         int   fds[2];
112         int   rc;
113         pid_t pid;
114
115         rc = pipe (fds);
116         if (rc != 0)
117                 abort ();
118
119         pid = fork ();
120         if (pid == 0) {
121                 /* child */
122                 int   fd = fileno(stdout);
123
124                 rc = dup2(fds[1], fd);
125                 if (rc != fd)
126                         abort();
127
128                 exit(system(cmdline));
129                 /* notreached */
130         } else if ((int)pid < 0) {
131                 abort();
132         } else {
133                 FILE *f = fdopen (fds[0], "r");
134
135                 if (f == NULL)
136                         abort();
137
138                 close(fds[1]);
139
140                 if (fgets(str, len, f) == NULL)
141                         abort();
142
143                 if (waitpid(pid, &rc, 0) != pid)
144                         abort();
145
146                 if (!WIFEXITED(rc) ||
147                     WEXITSTATUS(rc) != 0)
148                         abort();
149
150                 if (strnlen(str, len) == len)
151                         str[len - 1] = 0;
152
153                 if (str[strlen(str) - 1] == '\n')
154                         str[strlen(str) - 1] = 0;
155
156                 fclose(f);
157         }
158 }
159
160 int
161 main (int argc, char **argv)
162 {
163         char unameinfo[80];
164         char gccinfo[80];
165
166         system_string("uname -a", unameinfo, sizeof(unameinfo));
167         system_string("gcc -v 2>&1 | tail -1", gccinfo, sizeof(gccinfo));
168
169         printf ("void kptllnd_assert_wire_constants (void)\n"
170                 "{\n"
171                 "        /* Wire protocol assertions generated by 'wirecheck'\n"
172                 "         * running on %s\n"
173                 "         * with %s */\n"
174                 "\n", unameinfo, gccinfo);
175
176         BLANK_LINE ();
177
178         COMMENT ("Constants...");
179         CHECK_DEFINE (PTL_RESERVED_MATCHBITS);
180         CHECK_DEFINE (LNET_MSG_MATCHBITS);
181         
182         CHECK_DEFINE (PTLLND_MSG_MAGIC);
183         CHECK_DEFINE (PTLLND_MSG_VERSION);
184
185         CHECK_DEFINE (PTLLND_RDMA_OK);
186         CHECK_DEFINE (PTLLND_RDMA_FAIL);
187
188         CHECK_DEFINE (PTLLND_MSG_TYPE_INVALID);
189         CHECK_DEFINE (PTLLND_MSG_TYPE_PUT);
190         CHECK_DEFINE (PTLLND_MSG_TYPE_GET);
191         CHECK_DEFINE (PTLLND_MSG_TYPE_IMMEDIATE);
192         CHECK_DEFINE (PTLLND_MSG_TYPE_NOOP);
193         CHECK_DEFINE (PTLLND_MSG_TYPE_HELLO);
194         CHECK_DEFINE (PTLLND_MSG_TYPE_NAK);
195
196         CHECK_STRUCT (kptl_msg_t);
197         CHECK_MEMBER (kptl_msg_t, ptlm_magic);
198         CHECK_MEMBER (kptl_msg_t, ptlm_version);
199         CHECK_MEMBER (kptl_msg_t, ptlm_type);
200         CHECK_MEMBER (kptl_msg_t, ptlm_credits);
201         CHECK_MEMBER (kptl_msg_t, ptlm_nob);
202         CHECK_MEMBER (kptl_msg_t, ptlm_cksum);
203         CHECK_MEMBER (kptl_msg_t, ptlm_srcnid);
204         CHECK_MEMBER (kptl_msg_t, ptlm_srcstamp);
205         CHECK_MEMBER (kptl_msg_t, ptlm_dstnid);
206         CHECK_MEMBER (kptl_msg_t, ptlm_dststamp);
207         CHECK_MEMBER (kptl_msg_t, ptlm_srcpid);
208         CHECK_MEMBER (kptl_msg_t, ptlm_dstpid);
209         CHECK_MEMBER (kptl_msg_t, ptlm_u.immediate);
210         CHECK_MEMBER (kptl_msg_t, ptlm_u.rdma);
211         CHECK_MEMBER (kptl_msg_t, ptlm_u.hello);
212
213         CHECK_STRUCT (kptl_immediate_msg_t);
214         CHECK_MEMBER (kptl_immediate_msg_t, kptlim_hdr);
215         CHECK_MEMBER (kptl_immediate_msg_t, kptlim_payload[13]);
216
217         CHECK_STRUCT (kptl_rdma_msg_t);
218         CHECK_MEMBER (kptl_rdma_msg_t, kptlrm_hdr);
219         CHECK_MEMBER (kptl_rdma_msg_t, kptlrm_matchbits);
220
221         CHECK_STRUCT (kptl_hello_msg_t);
222         CHECK_MEMBER (kptl_hello_msg_t, kptlhm_matchbits);
223         CHECK_MEMBER (kptl_hello_msg_t, kptlhm_max_msg_size);
224
225         printf ("}\n\n");
226
227         return (0);
228 }