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