Whamcloud - gitweb
LU-10391 lnet: allow ping packet to contain large nids
[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_nid4(void)
113 {
114         CHECK_STRUCT(struct _lnet_hdr_nid4);
115         CHECK_MEMBER(struct _lnet_hdr_nid4, dest_nid);
116         CHECK_MEMBER(struct _lnet_hdr_nid4, src_nid);
117         CHECK_MEMBER(struct _lnet_hdr_nid4, dest_pid);
118         CHECK_MEMBER(struct _lnet_hdr_nid4, src_pid);
119         CHECK_MEMBER(struct _lnet_hdr_nid4, type);
120         CHECK_MEMBER(struct _lnet_hdr_nid4, payload_length);
121         CHECK_MEMBER(struct _lnet_hdr_nid4, msg);
122
123         BLANK_LINE ();
124         COMMENT ("Ack");
125         CHECK_MEMBER(struct _lnet_hdr_nid4, msg.ack.dst_wmd);
126         CHECK_MEMBER(struct _lnet_hdr_nid4, msg.ack.match_bits);
127         CHECK_MEMBER(struct _lnet_hdr_nid4, msg.ack.mlength);
128
129         BLANK_LINE ();
130         COMMENT ("Put");
131         CHECK_MEMBER(struct _lnet_hdr_nid4, msg.put.ack_wmd);
132         CHECK_MEMBER(struct _lnet_hdr_nid4, msg.put.match_bits);
133         CHECK_MEMBER(struct _lnet_hdr_nid4, msg.put.hdr_data);
134         CHECK_MEMBER(struct _lnet_hdr_nid4, msg.put.ptl_index);
135         CHECK_MEMBER(struct _lnet_hdr_nid4, msg.put.offset);
136
137         BLANK_LINE ();
138         COMMENT ("Get");
139         CHECK_MEMBER(struct _lnet_hdr_nid4, msg.get.return_wmd);
140         CHECK_MEMBER(struct _lnet_hdr_nid4, msg.get.match_bits);
141         CHECK_MEMBER(struct _lnet_hdr_nid4, msg.get.ptl_index);
142         CHECK_MEMBER(struct _lnet_hdr_nid4, msg.get.src_offset);
143         CHECK_MEMBER(struct _lnet_hdr_nid4, msg.get.sink_length);
144
145         BLANK_LINE ();
146         COMMENT ("Reply");
147         CHECK_MEMBER(struct _lnet_hdr_nid4, msg.reply.dst_wmd);
148
149         BLANK_LINE ();
150         COMMENT ("Hello");
151         CHECK_MEMBER(struct _lnet_hdr_nid4, msg.hello.incarnation);
152         CHECK_MEMBER(struct _lnet_hdr_nid4, 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_msg_size);
169
170         CHECK_STRUCT(struct lnet_ni_large_status);
171         CHECK_MEMBER(struct lnet_ni_large_status, ns_status);
172         CHECK_MEMBER(struct lnet_ni_large_status, ns_nid);
173 }
174
175 void
176 check_lnet_ping_info(void)
177 {
178         BLANK_LINE();
179         COMMENT("Checks for struct lnet_ping_info and related constants");
180
181         CHECK_DEFINE(LNET_PROTO_PING_MAGIC);
182         CHECK_VALUE(LNET_PING_FEAT_INVAL);
183         CHECK_VALUE(LNET_PING_FEAT_BASE);
184         CHECK_VALUE(LNET_PING_FEAT_NI_STATUS);
185         CHECK_VALUE(LNET_PING_FEAT_RTE_DISABLED);
186         CHECK_VALUE(LNET_PING_FEAT_MULTI_RAIL);
187         CHECK_VALUE(LNET_PING_FEAT_DISCOVERY);
188         CHECK_VALUE(LNET_PING_FEAT_LARGE_ADDR);
189         CHECK_VALUE(LNET_PING_FEAT_PRIMARY_LARGE);
190         CHECK_VALUE(LNET_PING_FEAT_BITS);
191
192         CHECK_STRUCT(struct lnet_ping_info);
193         CHECK_MEMBER(struct lnet_ping_info, pi_magic);
194         CHECK_MEMBER(struct lnet_ping_info, pi_features);
195         CHECK_MEMBER(struct lnet_ping_info, pi_pid);
196         CHECK_MEMBER(struct lnet_ping_info, pi_nnis);
197         CHECK_MEMBER(struct lnet_ping_info, pi_ni);
198 }
199
200 void
201 system_string(char *cmdline, char *str, int len)
202 {
203         int   fds[2];
204         int   rc;
205         pid_t pid;
206
207         rc = pipe (fds);
208         if (rc != 0)
209                 abort ();
210
211         pid = fork ();
212         if (pid == 0) {
213                 /* child */
214                 int   fd = fileno(stdout);
215
216                 rc = dup2(fds[1], fd);
217                 if (rc != fd)
218                         abort();
219
220                 exit(system(cmdline));
221                 /* notreached */
222         } else if ((int)pid < 0) {
223                 abort();
224         } else {
225                 FILE *f = fdopen (fds[0], "r");
226
227                 if (f == NULL)
228                         abort();
229
230                 close(fds[1]);
231
232                 if (fgets(str, len, f) == NULL)
233                         abort();
234
235                 if (waitpid(pid, &rc, 0) != pid)
236                         abort();
237
238                 if (!WIFEXITED(rc) ||
239                     WEXITSTATUS(rc) != 0)
240                         abort();
241
242                 if (strnlen(str, len) == len)
243                         str[len - 1] = 0;
244
245                 if (str[strlen(str) - 1] == '\n')
246                         str[strlen(str) - 1] = 0;
247
248                 fclose(f);
249         }
250 }
251
252 int
253 main (int argc, char **argv)
254 {
255         char unameinfo[256];
256         char gccinfo[256];
257
258         system_string("uname -a", unameinfo, sizeof(unameinfo));
259         system_string("gcc -v 2>&1 | tail -1", gccinfo, sizeof(gccinfo));
260
261         printf ("void lnet_assert_wire_constants (void)\n"
262                 "{\n"
263                 "        /* Wire protocol assertions generated by 'wirecheck'\n"
264                 "         * running on %s\n"
265                 "         * with %s */\n"
266                 "\n", unameinfo, gccinfo);
267
268         BLANK_LINE ();
269
270         COMMENT ("Constants...");
271
272         CHECK_DEFINE (LNET_PROTO_RA_MAGIC);
273
274         CHECK_DEFINE (LNET_PROTO_TCP_MAGIC);
275         CHECK_DEFINE (LNET_PROTO_TCP_VERSION_MAJOR);
276         CHECK_DEFINE (LNET_PROTO_TCP_VERSION_MINOR);
277
278         CHECK_VALUE (LNET_MSG_ACK);
279         CHECK_VALUE (LNET_MSG_PUT);
280         CHECK_VALUE (LNET_MSG_GET);
281         CHECK_VALUE (LNET_MSG_REPLY);
282         CHECK_VALUE (LNET_MSG_HELLO);
283
284         check_lnet_handle_wire();
285         check_lnet_magicversion();
286         check_lnet_hdr_nid4();
287         check_lnet_ni_status();
288         check_lnet_ping_info();
289
290         printf ("}\n\n");
291
292         return (0);
293 }