Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / lnet / utils / wirecheck.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  */
4 #include <stdio.h>
5 #include <sys/types.h>
6 #include <sys/wait.h>
7 #include <lnet/lib-lnet.h>
8
9 #include <string.h>
10
11 #ifndef HAVE_STRNLEN
12 #define strnlen(s, i) strlen(s)
13 #endif
14
15 #define BLANK_LINE()                            \
16 do {                                            \
17         printf ("\n");                          \
18 } while (0)
19
20 #define COMMENT(c)                              \
21 do {                                            \
22         printf ("        /* "c" */\n");         \
23 } while (0)
24
25 #define STRINGIFY(a) #a
26
27 #define CHECK_DEFINE(a)                                                 \
28 do {                                                                    \
29         printf ("        CLASSERT ("#a" == "STRINGIFY(a)");\n");        \
30 } while (0)
31
32 #define CHECK_VALUE(a)                                  \
33 do {                                                    \
34         printf ("        CLASSERT ("#a" == %d);\n", a); \
35 } while (0)
36
37 #define CHECK_MEMBER_OFFSET(s,m)                \
38 do {                                            \
39         CHECK_VALUE((int)offsetof(s, m));       \
40 } while (0)
41
42 #define CHECK_MEMBER_SIZEOF(s,m)                \
43 do {                                            \
44         CHECK_VALUE((int)sizeof(((s *)0)->m));  \
45 } while (0)
46
47 #define CHECK_MEMBER(s,m)                       \
48 do {                                            \
49         CHECK_MEMBER_OFFSET(s, m);              \
50         CHECK_MEMBER_SIZEOF(s, m);              \
51 } while (0)
52
53 #define CHECK_STRUCT(s)                         \
54 do {                                            \
55         BLANK_LINE ();                          \
56         COMMENT ("Checks for struct "#s);       \
57         CHECK_VALUE((int)sizeof(s));            \
58 } while (0)
59
60 void
61 check_lnet_handle_wire (void)
62 {
63         CHECK_STRUCT (lnet_handle_wire_t);
64         CHECK_MEMBER (lnet_handle_wire_t, wh_interface_cookie);
65         CHECK_MEMBER (lnet_handle_wire_t, wh_object_cookie);
66 }
67
68 void
69 check_lnet_magicversion (void)
70 {
71         CHECK_STRUCT (lnet_magicversion_t);
72         CHECK_MEMBER (lnet_magicversion_t, magic);
73         CHECK_MEMBER (lnet_magicversion_t, version_major);
74         CHECK_MEMBER (lnet_magicversion_t, version_minor);
75 }
76
77 void
78 check_lnet_hdr (void)
79 {
80         CHECK_STRUCT (lnet_hdr_t);
81         CHECK_MEMBER (lnet_hdr_t, dest_nid);
82         CHECK_MEMBER (lnet_hdr_t, src_nid);
83         CHECK_MEMBER (lnet_hdr_t, dest_pid);
84         CHECK_MEMBER (lnet_hdr_t, src_pid);
85         CHECK_MEMBER (lnet_hdr_t, type);
86         CHECK_MEMBER (lnet_hdr_t, payload_length);
87         CHECK_MEMBER (lnet_hdr_t, msg);
88
89         BLANK_LINE ();
90         COMMENT ("Ack");
91         CHECK_MEMBER (lnet_hdr_t, msg.ack.dst_wmd);
92         CHECK_MEMBER (lnet_hdr_t, msg.ack.match_bits);
93         CHECK_MEMBER (lnet_hdr_t, msg.ack.mlength);
94
95         BLANK_LINE ();
96         COMMENT ("Put");
97         CHECK_MEMBER (lnet_hdr_t, msg.put.ack_wmd);
98         CHECK_MEMBER (lnet_hdr_t, msg.put.match_bits);
99         CHECK_MEMBER (lnet_hdr_t, msg.put.hdr_data);
100         CHECK_MEMBER (lnet_hdr_t, msg.put.ptl_index);
101         CHECK_MEMBER (lnet_hdr_t, msg.put.offset);
102
103         BLANK_LINE ();
104         COMMENT ("Get");
105         CHECK_MEMBER (lnet_hdr_t, msg.get.return_wmd);
106         CHECK_MEMBER (lnet_hdr_t, msg.get.match_bits);
107         CHECK_MEMBER (lnet_hdr_t, msg.get.ptl_index);
108         CHECK_MEMBER (lnet_hdr_t, msg.get.src_offset);
109         CHECK_MEMBER (lnet_hdr_t, msg.get.sink_length);
110
111         BLANK_LINE ();
112         COMMENT ("Reply");
113         CHECK_MEMBER (lnet_hdr_t, msg.reply.dst_wmd);
114
115         BLANK_LINE ();
116         COMMENT ("Hello");
117         CHECK_MEMBER (lnet_hdr_t, msg.hello.incarnation);
118         CHECK_MEMBER (lnet_hdr_t, msg.hello.type);
119 }
120
121 void
122 system_string (char *cmdline, char *str, int len)
123 {
124         int   fds[2];
125         int   rc;
126         pid_t pid;
127
128         rc = pipe (fds);
129         if (rc != 0)
130                 abort ();
131
132         pid = fork ();
133         if (pid == 0) {
134                 /* child */
135                 int   fd = fileno(stdout);
136
137                 rc = dup2(fds[1], fd);
138                 if (rc != fd)
139                         abort();
140
141                 exit(system(cmdline));
142                 /* notreached */
143         } else if ((int)pid < 0) {
144                 abort();
145         } else {
146                 FILE *f = fdopen (fds[0], "r");
147
148                 if (f == NULL)
149                         abort();
150
151                 close(fds[1]);
152
153                 if (fgets(str, len, f) == NULL)
154                         abort();
155
156                 if (waitpid(pid, &rc, 0) != pid)
157                         abort();
158
159                 if (!WIFEXITED(rc) ||
160                     WEXITSTATUS(rc) != 0)
161                         abort();
162
163                 if (strnlen(str, len) == len)
164                         str[len - 1] = 0;
165
166                 if (str[strlen(str) - 1] == '\n')
167                         str[strlen(str) - 1] = 0;
168
169                 fclose(f);
170         }
171 }
172
173 int
174 main (int argc, char **argv)
175 {
176         char unameinfo[256];
177         char gccinfo[256];
178
179         system_string("uname -a", unameinfo, sizeof(unameinfo));
180         system_string("gcc -v 2>&1 | tail -1", gccinfo, sizeof(gccinfo));
181
182         printf ("void lnet_assert_wire_constants (void)\n"
183                 "{\n"
184                 "        /* Wire protocol assertions generated by 'wirecheck'\n"
185                 "         * running on %s\n"
186                 "         * with %s */\n"
187                 "\n", unameinfo, gccinfo);
188
189         BLANK_LINE ();
190
191         COMMENT ("Constants...");
192
193         CHECK_DEFINE (LNET_PROTO_OPENIB_MAGIC);
194         CHECK_DEFINE (LNET_PROTO_RA_MAGIC);
195
196         CHECK_DEFINE (LNET_PROTO_TCP_MAGIC);
197         CHECK_DEFINE (LNET_PROTO_TCP_VERSION_MAJOR);
198         CHECK_DEFINE (LNET_PROTO_TCP_VERSION_MINOR);
199
200         CHECK_VALUE (LNET_MSG_ACK);
201         CHECK_VALUE (LNET_MSG_PUT);
202         CHECK_VALUE (LNET_MSG_GET);
203         CHECK_VALUE (LNET_MSG_REPLY);
204         CHECK_VALUE (LNET_MSG_HELLO);
205
206         check_lnet_handle_wire ();
207         check_lnet_magicversion ();
208         check_lnet_hdr ();
209
210         printf ("}\n\n");
211
212         return (0);
213 }