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