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