Whamcloud - gitweb
LU-17705 ptlrpc: replace synchronize_rcu() with rcu_barrier()
[fs/lustre-release.git] / lnet / utils / wirecheck.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 /*
4  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
5  * Use is subject to license terms.
6  *
7  * Copyright (c) 2014, Intel Corporation.
8  */
9
10 /*
11  * This file is part of Lustre, http://www.lustre.org/
12  */
13
14 #ifndef _GNU_SOURCE
15 #define _GNU_SOURCE
16 #endif
17
18 #include <stddef.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <sys/types.h>
24 #include <sys/wait.h>
25 #include <linux/lnet/lnet-types.h>
26
27 #ifndef HAVE_STRNLEN
28 #define strnlen(s, i) strlen(s)
29 #endif
30
31 #define BLANK_LINE()                            \
32 do {                                            \
33         printf ("\n");                          \
34 } while (0)
35
36 #define COMMENT(c)                              \
37 do {                                            \
38         printf ("        /* "c" */\n");         \
39 } while (0)
40
41 #define STRINGIFY(a) #a
42
43 #define CHECK_BUILD_TEST(a)                                     \
44         printf("        BUILD_BUG_ON("#a");\n")
45
46 #define CHECK_DEFINE(a)                                                 \
47 do {                                                                    \
48         printf ("        BUILD_BUG_ON("#a" != "STRINGIFY(a)");\n");     \
49 } while (0)
50
51 #define CHECK_VALUE(a)                                     \
52 do {                                                       \
53         printf ("        BUILD_BUG_ON("#a" != %d);\n", a); \
54 } while (0)
55
56 #define CHECK_MEMBER_OFFSET(s,m)                \
57 do {                                            \
58         CHECK_VALUE((int)offsetof(s, m));       \
59 } while (0)
60
61 #define CHECK_MEMBER_SIZEOF(s,m)                \
62 do {                                            \
63         CHECK_VALUE((int)sizeof(((s *)0)->m));  \
64 } while (0)
65
66 #define CHECK_MEMBER_IS_FLEXIBLE(s, m)                  \
67 do {                                                                    \
68         CHECK_MEMBER_OFFSET(s, m);                                      \
69         CHECK_BUILD_TEST(offsetof(struct s, m) != sizeof(struct s));    \
70 } while (0)
71
72 #define CHECK_MEMBER(s,m)                       \
73 do {                                            \
74         CHECK_MEMBER_OFFSET(s, m);              \
75         CHECK_MEMBER_SIZEOF(s, m);              \
76 } while (0)
77
78 #define CHECK_STRUCT(s)                         \
79 do {                                            \
80         BLANK_LINE ();                          \
81         COMMENT ("Checks for struct "#s);       \
82         CHECK_VALUE((int)sizeof(s));            \
83 } while (0)
84
85 static void
86 check_lnet_handle_wire(void)
87 {
88         CHECK_STRUCT(struct lnet_handle_wire);
89         CHECK_MEMBER(struct lnet_handle_wire, wh_interface_cookie);
90         CHECK_MEMBER(struct lnet_handle_wire, wh_object_cookie);
91 }
92
93 static void
94 check_lnet_magicversion (void)
95 {
96         CHECK_STRUCT(struct lnet_magicversion);
97         CHECK_MEMBER(struct lnet_magicversion, magic);
98         CHECK_MEMBER(struct lnet_magicversion, version_major);
99         CHECK_MEMBER(struct lnet_magicversion, version_minor);
100 }
101
102 static void
103 check_lnet_hdr_nid4(void)
104 {
105         CHECK_STRUCT(struct _lnet_hdr_nid4);
106         CHECK_MEMBER(struct _lnet_hdr_nid4, dest_nid);
107         CHECK_MEMBER(struct _lnet_hdr_nid4, src_nid);
108         CHECK_MEMBER(struct _lnet_hdr_nid4, dest_pid);
109         CHECK_MEMBER(struct _lnet_hdr_nid4, src_pid);
110         CHECK_MEMBER(struct _lnet_hdr_nid4, type);
111         CHECK_MEMBER(struct _lnet_hdr_nid4, payload_length);
112         CHECK_MEMBER(struct _lnet_hdr_nid4, msg);
113
114         BLANK_LINE ();
115         COMMENT ("Ack");
116         CHECK_MEMBER(struct _lnet_hdr_nid4, msg.ack.dst_wmd);
117         CHECK_MEMBER(struct _lnet_hdr_nid4, msg.ack.match_bits);
118         CHECK_MEMBER(struct _lnet_hdr_nid4, msg.ack.mlength);
119
120         BLANK_LINE ();
121         COMMENT ("Put");
122         CHECK_MEMBER(struct _lnet_hdr_nid4, msg.put.ack_wmd);
123         CHECK_MEMBER(struct _lnet_hdr_nid4, msg.put.match_bits);
124         CHECK_MEMBER(struct _lnet_hdr_nid4, msg.put.hdr_data);
125         CHECK_MEMBER(struct _lnet_hdr_nid4, msg.put.ptl_index);
126         CHECK_MEMBER(struct _lnet_hdr_nid4, msg.put.offset);
127
128         BLANK_LINE ();
129         COMMENT ("Get");
130         CHECK_MEMBER(struct _lnet_hdr_nid4, msg.get.return_wmd);
131         CHECK_MEMBER(struct _lnet_hdr_nid4, msg.get.match_bits);
132         CHECK_MEMBER(struct _lnet_hdr_nid4, msg.get.ptl_index);
133         CHECK_MEMBER(struct _lnet_hdr_nid4, msg.get.src_offset);
134         CHECK_MEMBER(struct _lnet_hdr_nid4, msg.get.sink_length);
135
136         BLANK_LINE ();
137         COMMENT ("Reply");
138         CHECK_MEMBER(struct _lnet_hdr_nid4, msg.reply.dst_wmd);
139
140         BLANK_LINE ();
141         COMMENT ("Hello");
142         CHECK_MEMBER(struct _lnet_hdr_nid4, msg.hello.incarnation);
143         CHECK_MEMBER(struct _lnet_hdr_nid4, msg.hello.type);
144 }
145
146 static void
147 check_lnet_ni_status(void)
148 {
149         BLANK_LINE();
150         COMMENT("Checks for struct lnet_ni_status and related constants");
151
152         CHECK_DEFINE(LNET_NI_STATUS_INVALID);
153         CHECK_DEFINE(LNET_NI_STATUS_UP);
154         CHECK_DEFINE(LNET_NI_STATUS_DOWN);
155
156         CHECK_STRUCT(struct lnet_ni_status);
157         CHECK_MEMBER(struct lnet_ni_status, ns_nid);
158         CHECK_MEMBER(struct lnet_ni_status, ns_status);
159         CHECK_MEMBER(struct lnet_ni_status, ns_msg_size);
160
161         CHECK_STRUCT(struct lnet_ni_large_status);
162         CHECK_MEMBER(struct lnet_ni_large_status, ns_status);
163         CHECK_MEMBER(struct lnet_ni_large_status, ns_nid);
164 }
165
166 static void
167 check_lnet_ping_info(void)
168 {
169         BLANK_LINE();
170         COMMENT("Checks for struct lnet_ping_info and related constants");
171
172         CHECK_DEFINE(LNET_PROTO_PING_MAGIC);
173         CHECK_VALUE(LNET_PING_FEAT_INVAL);
174         CHECK_VALUE(LNET_PING_FEAT_BASE);
175         CHECK_VALUE(LNET_PING_FEAT_NI_STATUS);
176         CHECK_VALUE(LNET_PING_FEAT_RTE_DISABLED);
177         CHECK_VALUE(LNET_PING_FEAT_MULTI_RAIL);
178         CHECK_VALUE(LNET_PING_FEAT_DISCOVERY);
179         CHECK_VALUE(LNET_PING_FEAT_LARGE_ADDR);
180         CHECK_VALUE(LNET_PING_FEAT_PRIMARY_LARGE);
181         CHECK_VALUE(LNET_PING_FEAT_BITS);
182
183         CHECK_STRUCT(struct lnet_ping_info);
184         CHECK_MEMBER(struct lnet_ping_info, pi_magic);
185         CHECK_MEMBER(struct lnet_ping_info, pi_features);
186         CHECK_MEMBER(struct lnet_ping_info, pi_pid);
187         CHECK_MEMBER(struct lnet_ping_info, pi_nnis);
188         CHECK_MEMBER_IS_FLEXIBLE(struct lnet_ping_info, pi_ni);
189 }
190
191 static void
192 system_string(char *cmdline, char *str, int len)
193 {
194         int   fds[2];
195         int   rc;
196         pid_t pid;
197
198         rc = pipe (fds);
199         if (rc != 0)
200                 abort ();
201
202         pid = fork ();
203         if (pid == 0) {
204                 /* child */
205                 int   fd = fileno(stdout);
206
207                 rc = dup2(fds[1], fd);
208                 if (rc != fd)
209                         abort();
210
211                 exit(system(cmdline));
212                 /* notreached */
213         } else if ((int)pid < 0) {
214                 abort();
215         } else {
216                 FILE *f = fdopen (fds[0], "r");
217
218                 if (f == NULL)
219                         abort();
220
221                 close(fds[1]);
222
223                 if (fgets(str, len, f) == NULL)
224                         abort();
225
226                 if (waitpid(pid, &rc, 0) != pid)
227                         abort();
228
229                 if (!WIFEXITED(rc) ||
230                     WEXITSTATUS(rc) != 0)
231                         abort();
232
233                 if (strnlen(str, len) == len)
234                         str[len - 1] = 0;
235
236                 if (str[strlen(str) - 1] == '\n')
237                         str[strlen(str) - 1] = 0;
238
239                 fclose(f);
240         }
241 }
242
243 int
244 main (int argc, char **argv)
245 {
246         char unameinfo[256];
247         char gccinfo[256];
248
249         system_string("uname -a", unameinfo, sizeof(unameinfo));
250         system_string("gcc -v 2>&1 | tail -1", gccinfo, sizeof(gccinfo));
251
252         printf ("void lnet_assert_wire_constants (void)\n"
253                 "{\n"
254                 "        /* Wire protocol assertions generated by 'wirecheck'\n"
255                 "         * running on %s\n"
256                 "         * with %s */\n"
257                 "\n", unameinfo, gccinfo);
258
259         BLANK_LINE ();
260
261         COMMENT ("Constants...");
262
263         CHECK_DEFINE (LNET_PROTO_RA_MAGIC);
264
265         CHECK_DEFINE (LNET_PROTO_TCP_MAGIC);
266         CHECK_DEFINE (LNET_PROTO_TCP_VERSION_MAJOR);
267         CHECK_DEFINE (LNET_PROTO_TCP_VERSION_MINOR);
268
269         CHECK_VALUE (LNET_MSG_ACK);
270         CHECK_VALUE (LNET_MSG_PUT);
271         CHECK_VALUE (LNET_MSG_GET);
272         CHECK_VALUE (LNET_MSG_REPLY);
273         CHECK_VALUE (LNET_MSG_HELLO);
274
275         check_lnet_handle_wire();
276         check_lnet_magicversion();
277         check_lnet_hdr_nid4();
278         check_lnet_ni_status();
279         check_lnet_ping_info();
280
281         printf ("}\n\n");
282
283         return (0);
284 }