Whamcloud - gitweb
LU-1346 gnilnd: remove libcfs abstractions
[fs/lustre-release.git] / lnet / klnds / ptllnd / 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) 2007, 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  * lnet/klnds/ptllnd/wirecheck.c
35  *
36  * Author: PJ Kirner <pjkirner@clusterfs.com>
37  */
38 #include <stdio.h>
39 #include <string.h>
40 #include <sys/types.h>
41 #include <sys/wait.h>
42
43 #include <config.h>
44
45 #include <lnet/api-support.h>
46
47 /* This ghastly hack to allows me to include lib-types.h It doesn't affect any
48  * assertions generated here (but fails-safe if it ever does) */
49 typedef struct {
50         int     counter;
51 } cfs_atomic_t;
52
53 #include <lnet/lib-types.h>
54 #include <lnet/ptllnd_wire.h>
55
56 #ifndef HAVE_STRNLEN
57 #define strnlen(s, i) strlen(s)
58 #endif
59
60 #define BLANK_LINE()                            \
61 do {                                            \
62         printf ("\n");                          \
63 } while (0)
64
65 #define COMMENT(c)                              \
66 do {                                            \
67         printf ("        /* "c" */\n");         \
68 } while (0)
69
70 #undef STRINGIFY
71 #define STRINGIFY(a) #a
72
73 #define CHECK_DEFINE(a)                                         \
74 do {                                                            \
75         printf ("        CLASSERT ("#a" == "STRINGIFY(a)");\n"); \
76 } while (0)
77
78 #define CHECK_VALUE(a)                                  \
79 do {                                                    \
80         printf ("        CLASSERT ("#a" == %d);\n", a);  \
81 } while (0)
82
83 #define CHECK_MEMBER_OFFSET(s,m)                \
84 do {                                            \
85         CHECK_VALUE((int)offsetof(s, m));       \
86 } while (0)
87
88 #define CHECK_MEMBER_SIZEOF(s,m)                \
89 do {                                            \
90         CHECK_VALUE((int)sizeof(((s *)0)->m));  \
91 } while (0)
92
93 #define CHECK_MEMBER(s,m)                       \
94 do {                                            \
95         CHECK_MEMBER_OFFSET(s, m);              \
96         CHECK_MEMBER_SIZEOF(s, m);              \
97 } while (0)
98
99 #define CHECK_STRUCT(s)                         \
100 do {                                            \
101         BLANK_LINE ();                          \
102         COMMENT ("Checks for struct "#s);       \
103         CHECK_VALUE((int)sizeof(s));            \
104 } while (0)
105
106 void
107 system_string (char *cmdline, char *str, int len)
108 {
109         int   fds[2];
110         int   rc;
111         pid_t pid;
112
113         rc = pipe (fds);
114         if (rc != 0)
115                 abort ();
116
117         pid = fork ();
118         if (pid == 0) {
119                 /* child */
120                 int   fd = fileno(stdout);
121
122                 rc = dup2(fds[1], fd);
123                 if (rc != fd)
124                         abort();
125
126                 exit(system(cmdline));
127                 /* notreached */
128         } else if ((int)pid < 0) {
129                 abort();
130         } else {
131                 FILE *f = fdopen (fds[0], "r");
132
133                 if (f == NULL)
134                         abort();
135
136                 close(fds[1]);
137
138                 if (fgets(str, len, f) == NULL)
139                         abort();
140
141                 if (waitpid(pid, &rc, 0) != pid)
142                         abort();
143
144                 if (!WIFEXITED(rc) ||
145                     WEXITSTATUS(rc) != 0)
146                         abort();
147
148                 if (strnlen(str, len) == len)
149                         str[len - 1] = 0;
150
151                 if (str[strlen(str) - 1] == '\n')
152                         str[strlen(str) - 1] = 0;
153
154                 fclose(f);
155         }
156 }
157
158 int
159 main (int argc, char **argv)
160 {
161         char unameinfo[80];
162         char gccinfo[80];
163
164         system_string("uname -a", unameinfo, sizeof(unameinfo));
165         system_string("gcc -v 2>&1 | tail -1", gccinfo, sizeof(gccinfo));
166
167         printf ("void kptllnd_assert_wire_constants (void)\n"
168                 "{\n"
169                 "        /* Wire protocol assertions generated by 'wirecheck'\n"
170                 "         * running on %s\n"
171                 "         * with %s */\n"
172                 "\n", unameinfo, gccinfo);
173
174         BLANK_LINE ();
175
176         COMMENT ("Constants...");
177         CHECK_DEFINE (PTL_RESERVED_MATCHBITS);
178         CHECK_DEFINE (LNET_MSG_MATCHBITS);
179         
180         CHECK_DEFINE (PTLLND_MSG_MAGIC);
181         CHECK_DEFINE (PTLLND_MSG_VERSION);
182
183         CHECK_DEFINE (PTLLND_RDMA_OK);
184         CHECK_DEFINE (PTLLND_RDMA_FAIL);
185
186         CHECK_DEFINE (PTLLND_MSG_TYPE_INVALID);
187         CHECK_DEFINE (PTLLND_MSG_TYPE_PUT);
188         CHECK_DEFINE (PTLLND_MSG_TYPE_GET);
189         CHECK_DEFINE (PTLLND_MSG_TYPE_IMMEDIATE);
190         CHECK_DEFINE (PTLLND_MSG_TYPE_NOOP);
191         CHECK_DEFINE (PTLLND_MSG_TYPE_HELLO);
192         CHECK_DEFINE (PTLLND_MSG_TYPE_NAK);
193
194         CHECK_STRUCT (kptl_msg_t);
195         CHECK_MEMBER (kptl_msg_t, ptlm_magic);
196         CHECK_MEMBER (kptl_msg_t, ptlm_version);
197         CHECK_MEMBER (kptl_msg_t, ptlm_type);
198         CHECK_MEMBER (kptl_msg_t, ptlm_credits);
199         CHECK_MEMBER (kptl_msg_t, ptlm_nob);
200         CHECK_MEMBER (kptl_msg_t, ptlm_cksum);
201         CHECK_MEMBER (kptl_msg_t, ptlm_srcnid);
202         CHECK_MEMBER (kptl_msg_t, ptlm_srcstamp);
203         CHECK_MEMBER (kptl_msg_t, ptlm_dstnid);
204         CHECK_MEMBER (kptl_msg_t, ptlm_dststamp);
205         CHECK_MEMBER (kptl_msg_t, ptlm_srcpid);
206         CHECK_MEMBER (kptl_msg_t, ptlm_dstpid);
207         CHECK_MEMBER (kptl_msg_t, ptlm_u.immediate);
208         CHECK_MEMBER (kptl_msg_t, ptlm_u.rdma);
209         CHECK_MEMBER (kptl_msg_t, ptlm_u.hello);
210
211         CHECK_STRUCT (kptl_immediate_msg_t);
212         CHECK_MEMBER (kptl_immediate_msg_t, kptlim_hdr);
213         CHECK_MEMBER (kptl_immediate_msg_t, kptlim_payload[13]);
214
215         CHECK_STRUCT (kptl_rdma_msg_t);
216         CHECK_MEMBER (kptl_rdma_msg_t, kptlrm_hdr);
217         CHECK_MEMBER (kptl_rdma_msg_t, kptlrm_matchbits);
218
219         CHECK_STRUCT (kptl_hello_msg_t);
220         CHECK_MEMBER (kptl_hello_msg_t, kptlhm_matchbits);
221         CHECK_MEMBER (kptl_hello_msg_t, kptlhm_max_msg_size);
222
223         printf ("}\n\n");
224
225         return (0);
226 }