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