This source file includes following definitions.
- print_test_case
- do_send
- do_recv
- config_so_flags
- run_test_case
- main
1 #include <errno.h>
2 #include <error.h>
3 #include <getopt.h>
4 #include <stdbool.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <unistd.h>
9
10 #include <sys/time.h>
11 #include <sys/socket.h>
12 #include <sys/select.h>
13 #include <sys/ioctl.h>
14 #include <arpa/inet.h>
15 #include <net/if.h>
16
17 #include <asm/types.h>
18 #include <linux/net_tstamp.h>
19 #include <linux/errqueue.h>
20
21 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
22
23 struct options {
24 int so_timestamp;
25 int so_timestampns;
26 int so_timestamping;
27 };
28
29 struct tstamps {
30 bool tstamp;
31 bool tstampns;
32 bool swtstamp;
33 bool hwtstamp;
34 };
35
36 struct socket_type {
37 char *friendly_name;
38 int type;
39 int protocol;
40 bool enabled;
41 };
42
43 struct test_case {
44 struct options sockopt;
45 struct tstamps expected;
46 bool enabled;
47 };
48
49 struct sof_flag {
50 int mask;
51 char *name;
52 };
53
54 static struct sof_flag sof_flags[] = {
55 #define SOF_FLAG(f) { f, #f }
56 SOF_FLAG(SOF_TIMESTAMPING_SOFTWARE),
57 SOF_FLAG(SOF_TIMESTAMPING_RX_SOFTWARE),
58 SOF_FLAG(SOF_TIMESTAMPING_RX_HARDWARE),
59 };
60
61 static struct socket_type socket_types[] = {
62 { "ip", SOCK_RAW, IPPROTO_EGP },
63 { "udp", SOCK_DGRAM, IPPROTO_UDP },
64 { "tcp", SOCK_STREAM, IPPROTO_TCP },
65 };
66
67 static struct test_case test_cases[] = {
68 { {}, {} },
69 {
70 { so_timestamp: 1 },
71 { tstamp: true }
72 },
73 {
74 { so_timestampns: 1 },
75 { tstampns: true }
76 },
77 {
78 { so_timestamp: 1, so_timestampns: 1 },
79 { tstampns: true }
80 },
81 {
82 { so_timestamping: SOF_TIMESTAMPING_RX_SOFTWARE },
83 {}
84 },
85 {
86
87 { so_timestamping: SOF_TIMESTAMPING_RX_HARDWARE },
88 {}
89 },
90 {
91 { so_timestamping: SOF_TIMESTAMPING_SOFTWARE },
92 {}
93 },
94 {
95 { so_timestamping: SOF_TIMESTAMPING_RX_SOFTWARE
96 | SOF_TIMESTAMPING_RX_HARDWARE },
97 {}
98 },
99 {
100 { so_timestamping: SOF_TIMESTAMPING_SOFTWARE
101 | SOF_TIMESTAMPING_RX_SOFTWARE },
102 { swtstamp: true }
103 },
104 {
105 { so_timestamp: 1, so_timestamping: SOF_TIMESTAMPING_SOFTWARE
106 | SOF_TIMESTAMPING_RX_SOFTWARE },
107 { tstamp: true, swtstamp: true }
108 },
109 };
110
111 static struct option long_options[] = {
112 { "list_tests", no_argument, 0, 'l' },
113 { "test_num", required_argument, 0, 'n' },
114 { "op_size", required_argument, 0, 's' },
115 { "tcp", no_argument, 0, 't' },
116 { "udp", no_argument, 0, 'u' },
117 { "ip", no_argument, 0, 'i' },
118 { NULL, 0, NULL, 0 },
119 };
120
121 static int next_port = 19999;
122 static int op_size = 10 * 1024;
123
124 void print_test_case(struct test_case *t)
125 {
126 int f = 0;
127
128 printf("sockopts {");
129 if (t->sockopt.so_timestamp)
130 printf(" SO_TIMESTAMP ");
131 if (t->sockopt.so_timestampns)
132 printf(" SO_TIMESTAMPNS ");
133 if (t->sockopt.so_timestamping) {
134 printf(" SO_TIMESTAMPING: {");
135 for (f = 0; f < ARRAY_SIZE(sof_flags); f++)
136 if (t->sockopt.so_timestamping & sof_flags[f].mask)
137 printf(" %s |", sof_flags[f].name);
138 printf("}");
139 }
140 printf("} expected cmsgs: {");
141 if (t->expected.tstamp)
142 printf(" SCM_TIMESTAMP ");
143 if (t->expected.tstampns)
144 printf(" SCM_TIMESTAMPNS ");
145 if (t->expected.swtstamp || t->expected.hwtstamp) {
146 printf(" SCM_TIMESTAMPING {");
147 if (t->expected.swtstamp)
148 printf("0");
149 if (t->expected.swtstamp && t->expected.hwtstamp)
150 printf(",");
151 if (t->expected.hwtstamp)
152 printf("2");
153 printf("}");
154 }
155 printf("}\n");
156 }
157
158 void do_send(int src)
159 {
160 int r;
161 char *buf = malloc(op_size);
162
163 memset(buf, 'z', op_size);
164 r = write(src, buf, op_size);
165 if (r < 0)
166 error(1, errno, "Failed to sendmsg");
167
168 free(buf);
169 }
170
171 bool do_recv(int rcv, int read_size, struct tstamps expected)
172 {
173 const int CMSG_SIZE = 1024;
174
175 struct scm_timestamping *ts;
176 struct tstamps actual = {};
177 char cmsg_buf[CMSG_SIZE];
178 struct iovec recv_iov;
179 struct cmsghdr *cmsg;
180 bool failed = false;
181 struct msghdr hdr;
182 int flags = 0;
183 int r;
184
185 memset(&hdr, 0, sizeof(hdr));
186 hdr.msg_iov = &recv_iov;
187 hdr.msg_iovlen = 1;
188 recv_iov.iov_base = malloc(read_size);
189 recv_iov.iov_len = read_size;
190
191 hdr.msg_control = cmsg_buf;
192 hdr.msg_controllen = sizeof(cmsg_buf);
193
194 r = recvmsg(rcv, &hdr, flags);
195 if (r < 0)
196 error(1, errno, "Failed to recvmsg");
197 if (r != read_size)
198 error(1, 0, "Only received %d bytes of payload.", r);
199
200 if (hdr.msg_flags & (MSG_TRUNC | MSG_CTRUNC))
201 error(1, 0, "Message was truncated.");
202
203 for (cmsg = CMSG_FIRSTHDR(&hdr); cmsg != NULL;
204 cmsg = CMSG_NXTHDR(&hdr, cmsg)) {
205 if (cmsg->cmsg_level != SOL_SOCKET)
206 error(1, 0, "Unexpected cmsg_level %d",
207 cmsg->cmsg_level);
208 switch (cmsg->cmsg_type) {
209 case SCM_TIMESTAMP:
210 actual.tstamp = true;
211 break;
212 case SCM_TIMESTAMPNS:
213 actual.tstampns = true;
214 break;
215 case SCM_TIMESTAMPING:
216 ts = (struct scm_timestamping *)CMSG_DATA(cmsg);
217 actual.swtstamp = !!ts->ts[0].tv_sec;
218 if (ts->ts[1].tv_sec != 0)
219 error(0, 0, "ts[1] should not be set.");
220 actual.hwtstamp = !!ts->ts[2].tv_sec;
221 break;
222 default:
223 error(1, 0, "Unexpected cmsg_type %d", cmsg->cmsg_type);
224 }
225 }
226
227 #define VALIDATE(field) \
228 do { \
229 if (expected.field != actual.field) { \
230 if (expected.field) \
231 error(0, 0, "Expected " #field " to be set."); \
232 else \
233 error(0, 0, \
234 "Expected " #field " to not be set."); \
235 failed = true; \
236 } \
237 } while (0)
238
239 VALIDATE(tstamp);
240 VALIDATE(tstampns);
241 VALIDATE(swtstamp);
242 VALIDATE(hwtstamp);
243 #undef VALIDATE
244
245 free(recv_iov.iov_base);
246
247 return failed;
248 }
249
250 void config_so_flags(int rcv, struct options o)
251 {
252 int on = 1;
253
254 if (setsockopt(rcv, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
255 error(1, errno, "Failed to enable SO_REUSEADDR");
256
257 if (o.so_timestamp &&
258 setsockopt(rcv, SOL_SOCKET, SO_TIMESTAMP,
259 &o.so_timestamp, sizeof(o.so_timestamp)) < 0)
260 error(1, errno, "Failed to enable SO_TIMESTAMP");
261
262 if (o.so_timestampns &&
263 setsockopt(rcv, SOL_SOCKET, SO_TIMESTAMPNS,
264 &o.so_timestampns, sizeof(o.so_timestampns)) < 0)
265 error(1, errno, "Failed to enable SO_TIMESTAMPNS");
266
267 if (o.so_timestamping &&
268 setsockopt(rcv, SOL_SOCKET, SO_TIMESTAMPING,
269 &o.so_timestamping, sizeof(o.so_timestamping)) < 0)
270 error(1, errno, "Failed to set SO_TIMESTAMPING");
271 }
272
273 bool run_test_case(struct socket_type s, struct test_case t)
274 {
275 int port = (s.type == SOCK_RAW) ? 0 : next_port++;
276 int read_size = op_size;
277 struct sockaddr_in addr;
278 bool failed = false;
279 int src, dst, rcv;
280
281 src = socket(AF_INET, s.type, s.protocol);
282 if (src < 0)
283 error(1, errno, "Failed to open src socket");
284
285 dst = socket(AF_INET, s.type, s.protocol);
286 if (dst < 0)
287 error(1, errno, "Failed to open dst socket");
288
289 memset(&addr, 0, sizeof(addr));
290 addr.sin_family = AF_INET;
291 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
292 addr.sin_port = htons(port);
293
294 if (bind(dst, (struct sockaddr *)&addr, sizeof(addr)) < 0)
295 error(1, errno, "Failed to bind to port %d", port);
296
297 if (s.type == SOCK_STREAM && (listen(dst, 1) < 0))
298 error(1, errno, "Failed to listen");
299
300 if (connect(src, (struct sockaddr *)&addr, sizeof(addr)) < 0)
301 error(1, errno, "Failed to connect");
302
303 if (s.type == SOCK_STREAM) {
304 rcv = accept(dst, NULL, NULL);
305 if (rcv < 0)
306 error(1, errno, "Failed to accept");
307 close(dst);
308 } else {
309 rcv = dst;
310 }
311
312 config_so_flags(rcv, t.sockopt);
313 usleep(20000);
314 do_send(src);
315
316 if (s.type == SOCK_RAW)
317 read_size += 20;
318 failed = do_recv(rcv, read_size, t.expected);
319
320 close(rcv);
321 close(src);
322
323 return failed;
324 }
325
326 int main(int argc, char **argv)
327 {
328 bool all_protocols = true;
329 bool all_tests = true;
330 int arg_index = 0;
331 int failures = 0;
332 int s, t;
333 char opt;
334
335 while ((opt = getopt_long(argc, argv, "", long_options,
336 &arg_index)) != -1) {
337 switch (opt) {
338 case 'l':
339 for (t = 0; t < ARRAY_SIZE(test_cases); t++) {
340 printf("%d\t", t);
341 print_test_case(&test_cases[t]);
342 }
343 return 0;
344 case 'n':
345 t = atoi(optarg);
346 if (t >= ARRAY_SIZE(test_cases))
347 error(1, 0, "Invalid test case: %d", t);
348 all_tests = false;
349 test_cases[t].enabled = true;
350 break;
351 case 's':
352 op_size = atoi(optarg);
353 break;
354 case 't':
355 all_protocols = false;
356 socket_types[2].enabled = true;
357 break;
358 case 'u':
359 all_protocols = false;
360 socket_types[1].enabled = true;
361 break;
362 case 'i':
363 all_protocols = false;
364 socket_types[0].enabled = true;
365 break;
366 default:
367 error(1, 0, "Failed to parse parameters.");
368 }
369 }
370
371 for (s = 0; s < ARRAY_SIZE(socket_types); s++) {
372 if (!all_protocols && !socket_types[s].enabled)
373 continue;
374
375 printf("Testing %s...\n", socket_types[s].friendly_name);
376 for (t = 0; t < ARRAY_SIZE(test_cases); t++) {
377 if (!all_tests && !test_cases[t].enabled)
378 continue;
379
380 printf("Starting testcase %d...\n", t);
381 if (run_test_case(socket_types[s], test_cases[t])) {
382 failures++;
383 printf("FAILURE in test case ");
384 print_test_case(&test_cases[t]);
385 }
386 }
387 }
388 if (!failures)
389 printf("PASSED.\n");
390 return failures;
391 }