Whamcloud - gitweb
LU-5396 lnet/klnds: add sparse annotation __user wherever needed
[fs/lustre-release.git] / lnet / utils / wirecheck.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  */
34
35 #ifndef _GNU_SOURCE
36 #define _GNU_SOURCE
37 #endif
38
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <sys/types.h>
44 #include <sys/wait.h>
45 #include <lnet/lib-types.h>
46
47 #include <string.h>
48
49 #ifndef HAVE_STRNLEN
50 #define strnlen(s, i) strlen(s)
51 #endif
52
53 #define BLANK_LINE()                            \
54 do {                                            \
55         printf ("\n");                          \
56 } while (0)
57
58 #define COMMENT(c)                              \
59 do {                                            \
60         printf ("        /* "c" */\n");         \
61 } while (0)
62
63 #define STRINGIFY(a) #a
64
65 #define CHECK_DEFINE(a)                                                 \
66 do {                                                                    \
67         printf ("        CLASSERT ("#a" == "STRINGIFY(a)");\n");        \
68 } while (0)
69
70 #define CHECK_VALUE(a)                                  \
71 do {                                                    \
72         printf ("        CLASSERT ("#a" == %d);\n", a); \
73 } while (0)
74
75 #define CHECK_MEMBER_OFFSET(s,m)                \
76 do {                                            \
77         CHECK_VALUE((int)offsetof(s, m));       \
78 } while (0)
79
80 #define CHECK_MEMBER_SIZEOF(s,m)                \
81 do {                                            \
82         CHECK_VALUE((int)sizeof(((s *)0)->m));  \
83 } while (0)
84
85 #define CHECK_MEMBER(s,m)                       \
86 do {                                            \
87         CHECK_MEMBER_OFFSET(s, m);              \
88         CHECK_MEMBER_SIZEOF(s, m);              \
89 } while (0)
90
91 #define CHECK_STRUCT(s)                         \
92 do {                                            \
93         BLANK_LINE ();                          \
94         COMMENT ("Checks for struct "#s);       \
95         CHECK_VALUE((int)sizeof(s));            \
96 } while (0)
97
98 void
99 check_lnet_handle_wire (void)
100 {
101         CHECK_STRUCT (lnet_handle_wire_t);
102         CHECK_MEMBER (lnet_handle_wire_t, wh_interface_cookie);
103         CHECK_MEMBER (lnet_handle_wire_t, wh_object_cookie);
104 }
105
106 void
107 check_lnet_magicversion (void)
108 {
109         CHECK_STRUCT (lnet_magicversion_t);
110         CHECK_MEMBER (lnet_magicversion_t, magic);
111         CHECK_MEMBER (lnet_magicversion_t, version_major);
112         CHECK_MEMBER (lnet_magicversion_t, version_minor);
113 }
114
115 void
116 check_lnet_hdr (void)
117 {
118         CHECK_STRUCT (lnet_hdr_t);
119         CHECK_MEMBER (lnet_hdr_t, dest_nid);
120         CHECK_MEMBER (lnet_hdr_t, src_nid);
121         CHECK_MEMBER (lnet_hdr_t, dest_pid);
122         CHECK_MEMBER (lnet_hdr_t, src_pid);
123         CHECK_MEMBER (lnet_hdr_t, type);
124         CHECK_MEMBER (lnet_hdr_t, payload_length);
125         CHECK_MEMBER (lnet_hdr_t, msg);
126
127         BLANK_LINE ();
128         COMMENT ("Ack");
129         CHECK_MEMBER (lnet_hdr_t, msg.ack.dst_wmd);
130         CHECK_MEMBER (lnet_hdr_t, msg.ack.match_bits);
131         CHECK_MEMBER (lnet_hdr_t, msg.ack.mlength);
132
133         BLANK_LINE ();
134         COMMENT ("Put");
135         CHECK_MEMBER (lnet_hdr_t, msg.put.ack_wmd);
136         CHECK_MEMBER (lnet_hdr_t, msg.put.match_bits);
137         CHECK_MEMBER (lnet_hdr_t, msg.put.hdr_data);
138         CHECK_MEMBER (lnet_hdr_t, msg.put.ptl_index);
139         CHECK_MEMBER (lnet_hdr_t, msg.put.offset);
140
141         BLANK_LINE ();
142         COMMENT ("Get");
143         CHECK_MEMBER (lnet_hdr_t, msg.get.return_wmd);
144         CHECK_MEMBER (lnet_hdr_t, msg.get.match_bits);
145         CHECK_MEMBER (lnet_hdr_t, msg.get.ptl_index);
146         CHECK_MEMBER (lnet_hdr_t, msg.get.src_offset);
147         CHECK_MEMBER (lnet_hdr_t, msg.get.sink_length);
148
149         BLANK_LINE ();
150         COMMENT ("Reply");
151         CHECK_MEMBER (lnet_hdr_t, msg.reply.dst_wmd);
152
153         BLANK_LINE ();
154         COMMENT ("Hello");
155         CHECK_MEMBER (lnet_hdr_t, msg.hello.incarnation);
156         CHECK_MEMBER (lnet_hdr_t, msg.hello.type);
157 }
158
159 void
160 system_string (char *cmdline, char *str, int len)
161 {
162         int   fds[2];
163         int   rc;
164         pid_t pid;
165
166         rc = pipe (fds);
167         if (rc != 0)
168                 abort ();
169
170         pid = fork ();
171         if (pid == 0) {
172                 /* child */
173                 int   fd = fileno(stdout);
174
175                 rc = dup2(fds[1], fd);
176                 if (rc != fd)
177                         abort();
178
179                 exit(system(cmdline));
180                 /* notreached */
181         } else if ((int)pid < 0) {
182                 abort();
183         } else {
184                 FILE *f = fdopen (fds[0], "r");
185
186                 if (f == NULL)
187                         abort();
188
189                 close(fds[1]);
190
191                 if (fgets(str, len, f) == NULL)
192                         abort();
193
194                 if (waitpid(pid, &rc, 0) != pid)
195                         abort();
196
197                 if (!WIFEXITED(rc) ||
198                     WEXITSTATUS(rc) != 0)
199                         abort();
200
201                 if (strnlen(str, len) == len)
202                         str[len - 1] = 0;
203
204                 if (str[strlen(str) - 1] == '\n')
205                         str[strlen(str) - 1] = 0;
206
207                 fclose(f);
208         }
209 }
210
211 int
212 main (int argc, char **argv)
213 {
214         char unameinfo[256];
215         char gccinfo[256];
216
217         system_string("uname -a", unameinfo, sizeof(unameinfo));
218         system_string("gcc -v 2>&1 | tail -1", gccinfo, sizeof(gccinfo));
219
220         printf ("void lnet_assert_wire_constants (void)\n"
221                 "{\n"
222                 "        /* Wire protocol assertions generated by 'wirecheck'\n"
223                 "         * running on %s\n"
224                 "         * with %s */\n"
225                 "\n", unameinfo, gccinfo);
226
227         BLANK_LINE ();
228
229         COMMENT ("Constants...");
230
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 }