Whamcloud - gitweb
Landing b_hd_newconfig on HEAD
[fs/lustre-release.git] / lnet / klnds / ptllnd / wirecheck.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2005 Cluster File Systems, Inc. All rights reserved.
5  *   Author: PJ Kirner <pjkirner@clusterfs.com>
6  *
7  *   This file is part of the Lustre file system, http://www.lustre.org
8  *   Lustre is a trademark of Cluster File Systems, Inc.
9  *
10  *   This file is confidential source code owned by Cluster File Systems.
11  *   No viewing, modification, compilation, redistribution, or any other
12  *   form of use is permitted except through a signed license agreement.
13  *
14  *   If you have not signed such an agreement, then you have no rights to
15  *   this file.  Please destroy it immediately and contact CFS.
16  *
17  */
18 #include <stdio.h>
19 #include <string.h>
20 #include <sys/types.h>
21 #include <sys/wait.h>
22
23 #include <config.h>
24
25 #include <lnet/api-support.h>
26
27 /* This ghastly hack to allows me to include lib-types.h It doesn't affect any
28  * assertions generated here (but fails-safe if it ever does) */
29 typedef struct {
30         int     counter;
31 } atomic_t;
32
33 #include <lnet/lib-types.h>
34 #include <lnet/ptllnd_wire.h>
35
36 #ifndef HAVE_STRNLEN
37 #define strnlen(s, i) strlen(s)
38 #endif
39
40 #define BLANK_LINE()                            \
41 do {                                            \
42         printf ("\n");                          \
43 } while (0)
44
45 #define COMMENT(c)                              \
46 do {                                            \
47         printf ("        /* "c" */\n");         \
48 } while (0)
49
50 #undef STRINGIFY
51 #define STRINGIFY(a) #a
52
53 #define CHECK_DEFINE(a)                                         \
54 do {                                                            \
55         printf ("        CLASSERT ("#a" == "STRINGIFY(a)");\n"); \
56 } while (0)
57
58 #define CHECK_VALUE(a)                                  \
59 do {                                                    \
60         printf ("        CLASSERT ("#a" == %d);\n", a);  \
61 } while (0)
62
63 #define CHECK_MEMBER_OFFSET(s,m)                \
64 do {                                            \
65         CHECK_VALUE((int)offsetof(s, m));       \
66 } while (0)
67
68 #define CHECK_MEMBER_SIZEOF(s,m)                \
69 do {                                            \
70         CHECK_VALUE((int)sizeof(((s *)0)->m));  \
71 } while (0)
72
73 #define CHECK_MEMBER(s,m)                       \
74 do {                                            \
75         CHECK_MEMBER_OFFSET(s, m);              \
76         CHECK_MEMBER_SIZEOF(s, m);              \
77 } while (0)
78
79 #define CHECK_STRUCT(s)                         \
80 do {                                            \
81         BLANK_LINE ();                          \
82         COMMENT ("Checks for struct "#s);       \
83         CHECK_VALUE((int)sizeof(s));            \
84 } while (0)
85
86 void
87 system_string (char *cmdline, char *str, int len)
88 {
89         int   fds[2];
90         int   rc;
91         pid_t pid;
92
93         rc = pipe (fds);
94         if (rc != 0)
95                 abort ();
96
97         pid = fork ();
98         if (pid == 0) {
99                 /* child */
100                 int   fd = fileno(stdout);
101
102                 rc = dup2(fds[1], fd);
103                 if (rc != fd)
104                         abort();
105
106                 exit(system(cmdline));
107                 /* notreached */
108         } else if ((int)pid < 0) {
109                 abort();
110         } else {
111                 FILE *f = fdopen (fds[0], "r");
112
113                 if (f == NULL)
114                         abort();
115
116                 close(fds[1]);
117
118                 if (fgets(str, len, f) == NULL)
119                         abort();
120
121                 if (waitpid(pid, &rc, 0) != pid)
122                         abort();
123
124                 if (!WIFEXITED(rc) ||
125                     WEXITSTATUS(rc) != 0)
126                         abort();
127
128                 if (strnlen(str, len) == len)
129                         str[len - 1] = 0;
130
131                 if (str[strlen(str) - 1] == '\n')
132                         str[strlen(str) - 1] = 0;
133
134                 fclose(f);
135         }
136 }
137
138 int
139 main (int argc, char **argv)
140 {
141         char unameinfo[80];
142         char gccinfo[80];
143
144         system_string("uname -a", unameinfo, sizeof(unameinfo));
145         system_string("gcc -v 2>&1 | tail -1", gccinfo, sizeof(gccinfo));
146
147         printf ("void kptllnd_assert_wire_constants (void)\n"
148                 "{\n"
149                 "        /* Wire protocol assertions generated by 'wirecheck'\n"
150                 "         * running on %s\n"
151                 "         * with %s */\n"
152                 "\n", unameinfo, gccinfo);
153
154         BLANK_LINE ();
155
156         COMMENT ("Constants...");
157         CHECK_DEFINE (PTL_RESERVED_MATCHBITS);
158         CHECK_DEFINE (LNET_MSG_MATCHBITS);
159         
160         CHECK_DEFINE (PTLLND_MSG_MAGIC);
161         CHECK_DEFINE (PTLLND_MSG_VERSION);
162
163         CHECK_DEFINE (PTLLND_RDMA_OK);
164         CHECK_DEFINE (PTLLND_RDMA_FAIL);
165
166         CHECK_DEFINE (PTLLND_MSG_TYPE_INVALID);
167         CHECK_DEFINE (PTLLND_MSG_TYPE_PUT);
168         CHECK_DEFINE (PTLLND_MSG_TYPE_GET);
169         CHECK_DEFINE (PTLLND_MSG_TYPE_IMMEDIATE);
170         CHECK_DEFINE (PTLLND_MSG_TYPE_NOOP);
171         CHECK_DEFINE (PTLLND_MSG_TYPE_HELLO);
172         CHECK_DEFINE (PTLLND_MSG_TYPE_NAK);
173
174         CHECK_STRUCT (kptl_msg_t);
175         CHECK_MEMBER (kptl_msg_t, ptlm_magic);
176         CHECK_MEMBER (kptl_msg_t, ptlm_version);
177         CHECK_MEMBER (kptl_msg_t, ptlm_type);
178         CHECK_MEMBER (kptl_msg_t, ptlm_credits);
179         CHECK_MEMBER (kptl_msg_t, ptlm_nob);
180         CHECK_MEMBER (kptl_msg_t, ptlm_cksum);
181         CHECK_MEMBER (kptl_msg_t, ptlm_srcnid);
182         CHECK_MEMBER (kptl_msg_t, ptlm_srcstamp);
183         CHECK_MEMBER (kptl_msg_t, ptlm_dstnid);
184         CHECK_MEMBER (kptl_msg_t, ptlm_dststamp);
185         CHECK_MEMBER (kptl_msg_t, ptlm_srcpid);
186         CHECK_MEMBER (kptl_msg_t, ptlm_dstpid);
187         CHECK_MEMBER (kptl_msg_t, ptlm_u.immediate);
188         CHECK_MEMBER (kptl_msg_t, ptlm_u.rdma);
189         CHECK_MEMBER (kptl_msg_t, ptlm_u.hello);
190
191         CHECK_STRUCT (kptl_immediate_msg_t);
192         CHECK_MEMBER (kptl_immediate_msg_t, kptlim_hdr);
193         CHECK_MEMBER (kptl_immediate_msg_t, kptlim_payload[13]);
194
195         CHECK_STRUCT (kptl_rdma_msg_t);
196         CHECK_MEMBER (kptl_rdma_msg_t, kptlrm_hdr);
197         CHECK_MEMBER (kptl_rdma_msg_t, kptlrm_matchbits);
198
199         CHECK_STRUCT (kptl_hello_msg_t);
200         CHECK_MEMBER (kptl_hello_msg_t, kptlhm_matchbits);
201         CHECK_MEMBER (kptl_hello_msg_t, kptlhm_max_msg_size);
202
203         printf ("}\n\n");
204
205         return (0);
206 }