11 #include <sys/socket.h>
12 #include <sys/select.h>
13 #include <sys/ioctl.h>
14 #include <arpa/inet.h>
17 #include <asm/types.h>
18 #include <linux/net_tstamp.h>
19 #include <linux/errqueue.h>
21 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
44 struct options sockopt;
45 struct tstamps expected;
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),
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 },
67 static struct test_case test_cases[] = {
74 { so_timestampns: 1 },
78 { so_timestamp: 1, so_timestampns: 1 },
82 { so_timestamping: SOF_TIMESTAMPING_RX_SOFTWARE },
86 /* Loopback device does not support hw timestamps. */
87 { so_timestamping: SOF_TIMESTAMPING_RX_HARDWARE },
91 { so_timestamping: SOF_TIMESTAMPING_SOFTWARE },
95 { so_timestamping: SOF_TIMESTAMPING_RX_SOFTWARE
96 | SOF_TIMESTAMPING_RX_HARDWARE },
100 { so_timestamping: SOF_TIMESTAMPING_SOFTWARE
101 | SOF_TIMESTAMPING_RX_SOFTWARE },
105 { so_timestamp: 1, so_timestamping: SOF_TIMESTAMPING_SOFTWARE
106 | SOF_TIMESTAMPING_RX_SOFTWARE },
107 { tstamp: true, swtstamp: true }
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 },
121 static int next_port = 19999;
122 static int op_size = 10 * 1024;
124 void print_test_case(struct test_case *t)
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);
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)
149 if (t->expected.swtstamp && t->expected.hwtstamp)
151 if (t->expected.hwtstamp)
158 void do_send(int src)
161 char *buf = malloc(op_size);
163 memset(buf, 'z', op_size);
164 r = write(src, buf, op_size);
166 error(1, errno, "Failed to sendmsg");
171 bool do_recv(int rcv, int read_size, struct tstamps expected)
173 const int CMSG_SIZE = 1024;
175 struct scm_timestamping *ts;
176 struct tstamps actual = {};
177 char cmsg_buf[CMSG_SIZE];
178 struct iovec recv_iov;
179 struct cmsghdr *cmsg;
185 memset(&hdr, 0, sizeof(hdr));
186 hdr.msg_iov = &recv_iov;
188 recv_iov.iov_base = malloc(read_size);
189 recv_iov.iov_len = read_size;
191 hdr.msg_control = cmsg_buf;
192 hdr.msg_controllen = sizeof(cmsg_buf);
194 r = recvmsg(rcv, &hdr, flags);
196 error(1, errno, "Failed to recvmsg");
198 error(1, 0, "Only received %d bytes of payload.", r);
200 if (hdr.msg_flags & (MSG_TRUNC | MSG_CTRUNC))
201 error(1, 0, "Message was truncated.");
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",
208 switch (cmsg->cmsg_type) {
210 actual.tstamp = true;
212 case SCM_TIMESTAMPNS:
213 actual.tstampns = true;
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;
223 error(1, 0, "Unexpected cmsg_type %d", cmsg->cmsg_type);
227 #define VALIDATE(field) \
229 if (expected.field != actual.field) { \
230 if (expected.field) \
231 error(0, 0, "Expected " #field " to be set."); \
234 "Expected " #field " to not be set."); \
245 free(recv_iov.iov_base);
250 void config_so_flags(int rcv, struct options o)
254 if (setsockopt(rcv, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
255 error(1, errno, "Failed to enable SO_REUSEADDR");
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");
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");
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");
273 bool run_test_case(struct socket_type s, struct test_case t)
275 int port = (s.type == SOCK_RAW) ? 0 : next_port++;
276 int read_size = op_size;
277 struct sockaddr_in addr;
281 src = socket(AF_INET, s.type, s.protocol);
283 error(1, errno, "Failed to open src socket");
285 dst = socket(AF_INET, s.type, s.protocol);
287 error(1, errno, "Failed to open dst socket");
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);
294 if (bind(dst, (struct sockaddr *)&addr, sizeof(addr)) < 0)
295 error(1, errno, "Failed to bind to port %d", port);
297 if (s.type == SOCK_STREAM && (listen(dst, 1) < 0))
298 error(1, errno, "Failed to listen");
300 if (connect(src, (struct sockaddr *)&addr, sizeof(addr)) < 0)
301 error(1, errno, "Failed to connect");
303 if (s.type == SOCK_STREAM) {
304 rcv = accept(dst, NULL, NULL);
306 error(1, errno, "Failed to accept");
312 config_so_flags(rcv, t.sockopt);
313 usleep(20000); /* setsockopt for SO_TIMESTAMPING is asynchronous */
316 if (s.type == SOCK_RAW)
317 read_size += 20; /* for IP header */
318 failed = do_recv(rcv, read_size, t.expected);
326 int main(int argc, char **argv)
328 bool all_protocols = true;
329 bool all_tests = true;
335 while ((opt = getopt_long(argc, argv, "", long_options,
336 &arg_index)) != -1) {
339 for (t = 0; t < ARRAY_SIZE(test_cases); t++) {
341 print_test_case(&test_cases[t]);
346 if (t >= ARRAY_SIZE(test_cases))
347 error(1, 0, "Invalid test case: %d", t);
349 test_cases[t].enabled = true;
352 op_size = atoi(optarg);
355 all_protocols = false;
356 socket_types[2].enabled = true;
359 all_protocols = false;
360 socket_types[1].enabled = true;
363 all_protocols = false;
364 socket_types[0].enabled = true;
367 error(1, 0, "Failed to parse parameters.");
371 for (s = 0; s < ARRAY_SIZE(socket_types); s++) {
372 if (!all_protocols && !socket_types[s].enabled)
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)
380 printf("Starting testcase %d...\n", t);
381 if (run_test_case(socket_types[s], test_cases[t])) {
383 printf("FAILURE in test case ");
384 print_test_case(&test_cases[t]);