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