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