Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / lnet / utils / 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 #ifndef _GNU_SOURCE
38 #define _GNU_SOURCE
39 #endif
40
41 #include <stdio.h>
42 #include <sys/types.h>
43 #include <sys/wait.h>
44 #include <lnet/lib-lnet.h>
45
46 #include <string.h>
47
48 #ifndef HAVE_STRNLEN
49 #define strnlen(s, i) strlen(s)
50 #endif
51
52 #define BLANK_LINE()                            \
53 do {                                            \
54         printf ("\n");                          \
55 } while (0)
56
57 #define COMMENT(c)                              \
58 do {                                            \
59         printf ("        /* "c" */\n");         \
60 } while (0)
61
62 #define STRINGIFY(a) #a
63
64 #define CHECK_DEFINE(a)                                                 \
65 do {                                                                    \
66         printf ("        CLASSERT ("#a" == "STRINGIFY(a)");\n");        \
67 } while (0)
68
69 #define CHECK_VALUE(a)                                  \
70 do {                                                    \
71         printf ("        CLASSERT ("#a" == %d);\n", a); \
72 } while (0)
73
74 #define CHECK_MEMBER_OFFSET(s,m)                \
75 do {                                            \
76         CHECK_VALUE((int)offsetof(s, m));       \
77 } while (0)
78
79 #define CHECK_MEMBER_SIZEOF(s,m)                \
80 do {                                            \
81         CHECK_VALUE((int)sizeof(((s *)0)->m));  \
82 } while (0)
83
84 #define CHECK_MEMBER(s,m)                       \
85 do {                                            \
86         CHECK_MEMBER_OFFSET(s, m);              \
87         CHECK_MEMBER_SIZEOF(s, m);              \
88 } while (0)
89
90 #define CHECK_STRUCT(s)                         \
91 do {                                            \
92         BLANK_LINE ();                          \
93         COMMENT ("Checks for struct "#s);       \
94         CHECK_VALUE((int)sizeof(s));            \
95 } while (0)
96
97 void
98 check_lnet_handle_wire (void)
99 {
100         CHECK_STRUCT (lnet_handle_wire_t);
101         CHECK_MEMBER (lnet_handle_wire_t, wh_interface_cookie);
102         CHECK_MEMBER (lnet_handle_wire_t, wh_object_cookie);
103 }
104
105 void
106 check_lnet_magicversion (void)
107 {
108         CHECK_STRUCT (lnet_magicversion_t);
109         CHECK_MEMBER (lnet_magicversion_t, magic);
110         CHECK_MEMBER (lnet_magicversion_t, version_major);
111         CHECK_MEMBER (lnet_magicversion_t, version_minor);
112 }
113
114 void
115 check_lnet_hdr (void)
116 {
117         CHECK_STRUCT (lnet_hdr_t);
118         CHECK_MEMBER (lnet_hdr_t, dest_nid);
119         CHECK_MEMBER (lnet_hdr_t, src_nid);
120         CHECK_MEMBER (lnet_hdr_t, dest_pid);
121         CHECK_MEMBER (lnet_hdr_t, src_pid);
122         CHECK_MEMBER (lnet_hdr_t, type);
123         CHECK_MEMBER (lnet_hdr_t, payload_length);
124         CHECK_MEMBER (lnet_hdr_t, msg);
125
126         BLANK_LINE ();
127         COMMENT ("Ack");
128         CHECK_MEMBER (lnet_hdr_t, msg.ack.dst_wmd);
129         CHECK_MEMBER (lnet_hdr_t, msg.ack.match_bits);
130         CHECK_MEMBER (lnet_hdr_t, msg.ack.mlength);
131
132         BLANK_LINE ();
133         COMMENT ("Put");
134         CHECK_MEMBER (lnet_hdr_t, msg.put.ack_wmd);
135         CHECK_MEMBER (lnet_hdr_t, msg.put.match_bits);
136         CHECK_MEMBER (lnet_hdr_t, msg.put.hdr_data);
137         CHECK_MEMBER (lnet_hdr_t, msg.put.ptl_index);
138         CHECK_MEMBER (lnet_hdr_t, msg.put.offset);
139
140         BLANK_LINE ();
141         COMMENT ("Get");
142         CHECK_MEMBER (lnet_hdr_t, msg.get.return_wmd);
143         CHECK_MEMBER (lnet_hdr_t, msg.get.match_bits);
144         CHECK_MEMBER (lnet_hdr_t, msg.get.ptl_index);
145         CHECK_MEMBER (lnet_hdr_t, msg.get.src_offset);
146         CHECK_MEMBER (lnet_hdr_t, msg.get.sink_length);
147
148         BLANK_LINE ();
149         COMMENT ("Reply");
150         CHECK_MEMBER (lnet_hdr_t, msg.reply.dst_wmd);
151
152         BLANK_LINE ();
153         COMMENT ("Hello");
154         CHECK_MEMBER (lnet_hdr_t, msg.hello.incarnation);
155         CHECK_MEMBER (lnet_hdr_t, msg.hello.type);
156 }
157
158 void
159 system_string (char *cmdline, char *str, int len)
160 {
161         int   fds[2];
162         int   rc;
163         pid_t pid;
164
165         rc = pipe (fds);
166         if (rc != 0)
167                 abort ();
168
169         pid = fork ();
170         if (pid == 0) {
171                 /* child */
172                 int   fd = fileno(stdout);
173
174                 rc = dup2(fds[1], fd);
175                 if (rc != fd)
176                         abort();
177
178                 exit(system(cmdline));
179                 /* notreached */
180         } else if ((int)pid < 0) {
181                 abort();
182         } else {
183                 FILE *f = fdopen (fds[0], "r");
184
185                 if (f == NULL)
186                         abort();
187
188                 close(fds[1]);
189
190                 if (fgets(str, len, f) == NULL)
191                         abort();
192
193                 if (waitpid(pid, &rc, 0) != pid)
194                         abort();
195
196                 if (!WIFEXITED(rc) ||
197                     WEXITSTATUS(rc) != 0)
198                         abort();
199
200                 if (strnlen(str, len) == len)
201                         str[len - 1] = 0;
202
203                 if (str[strlen(str) - 1] == '\n')
204                         str[strlen(str) - 1] = 0;
205
206                 fclose(f);
207         }
208 }
209
210 int
211 main (int argc, char **argv)
212 {
213         char unameinfo[256];
214         char gccinfo[256];
215
216         system_string("uname -a", unameinfo, sizeof(unameinfo));
217         system_string("gcc -v 2>&1 | tail -1", gccinfo, sizeof(gccinfo));
218
219         printf ("void lnet_assert_wire_constants (void)\n"
220                 "{\n"
221                 "        /* Wire protocol assertions generated by 'wirecheck'\n"
222                 "         * running on %s\n"
223                 "         * with %s */\n"
224                 "\n", unameinfo, gccinfo);
225
226         BLANK_LINE ();
227
228         COMMENT ("Constants...");
229
230         CHECK_DEFINE (LNET_PROTO_OPENIB_MAGIC);
231         CHECK_DEFINE (LNET_PROTO_RA_MAGIC);
232
233         CHECK_DEFINE (LNET_PROTO_TCP_MAGIC);
234         CHECK_DEFINE (LNET_PROTO_TCP_VERSION_MAJOR);
235         CHECK_DEFINE (LNET_PROTO_TCP_VERSION_MINOR);
236
237         CHECK_VALUE (LNET_MSG_ACK);
238         CHECK_VALUE (LNET_MSG_PUT);
239         CHECK_VALUE (LNET_MSG_GET);
240         CHECK_VALUE (LNET_MSG_REPLY);
241         CHECK_VALUE (LNET_MSG_HELLO);
242
243         check_lnet_handle_wire ();
244         check_lnet_magicversion ();
245         check_lnet_hdr ();
246
247         printf ("}\n\n");
248
249         return (0);
250 }