xref: /openbmc/linux/tools/testing/selftests/net/rxtimestamp.c (revision 03ab8e6297acd1bc0eedaa050e2a1635c576fd11)
15ef5c90eSJian Yang #include <errno.h>
25ef5c90eSJian Yang #include <error.h>
35ef5c90eSJian Yang #include <getopt.h>
45ef5c90eSJian Yang #include <stdbool.h>
55ef5c90eSJian Yang #include <stdio.h>
65ef5c90eSJian Yang #include <stdlib.h>
75ef5c90eSJian Yang #include <string.h>
85ef5c90eSJian Yang #include <unistd.h>
95ef5c90eSJian Yang 
105ef5c90eSJian Yang #include <sys/time.h>
115ef5c90eSJian Yang #include <sys/socket.h>
125ef5c90eSJian Yang #include <sys/select.h>
135ef5c90eSJian Yang #include <sys/ioctl.h>
145ef5c90eSJian Yang #include <arpa/inet.h>
155ef5c90eSJian Yang #include <net/if.h>
165ef5c90eSJian Yang 
175ef5c90eSJian Yang #include <asm/types.h>
185ef5c90eSJian Yang #include <linux/net_tstamp.h>
195ef5c90eSJian Yang #include <linux/errqueue.h>
205ef5c90eSJian Yang 
21*1329e40eSShuah Khan #include "../kselftest.h"
225ef5c90eSJian Yang 
235ef5c90eSJian Yang struct options {
245ef5c90eSJian Yang 	int so_timestamp;
255ef5c90eSJian Yang 	int so_timestampns;
265ef5c90eSJian Yang 	int so_timestamping;
275ef5c90eSJian Yang };
285ef5c90eSJian Yang 
295ef5c90eSJian Yang struct tstamps {
305ef5c90eSJian Yang 	bool tstamp;
315ef5c90eSJian Yang 	bool tstampns;
325ef5c90eSJian Yang 	bool swtstamp;
335ef5c90eSJian Yang 	bool hwtstamp;
345ef5c90eSJian Yang };
355ef5c90eSJian Yang 
365ef5c90eSJian Yang struct socket_type {
375ef5c90eSJian Yang 	char *friendly_name;
385ef5c90eSJian Yang 	int type;
395ef5c90eSJian Yang 	int protocol;
405ef5c90eSJian Yang 	bool enabled;
415ef5c90eSJian Yang };
425ef5c90eSJian Yang 
435ef5c90eSJian Yang struct test_case {
445ef5c90eSJian Yang 	struct options sockopt;
455ef5c90eSJian Yang 	struct tstamps expected;
465ef5c90eSJian Yang 	bool enabled;
470558c396Stannerlove 	bool warn_on_fail;
485ef5c90eSJian Yang };
495ef5c90eSJian Yang 
505ef5c90eSJian Yang struct sof_flag {
515ef5c90eSJian Yang 	int mask;
525ef5c90eSJian Yang 	char *name;
535ef5c90eSJian Yang };
545ef5c90eSJian Yang 
555ef5c90eSJian Yang static struct sof_flag sof_flags[] = {
565ef5c90eSJian Yang #define SOF_FLAG(f) { f, #f }
575ef5c90eSJian Yang 	SOF_FLAG(SOF_TIMESTAMPING_SOFTWARE),
585ef5c90eSJian Yang 	SOF_FLAG(SOF_TIMESTAMPING_RX_SOFTWARE),
595ef5c90eSJian Yang 	SOF_FLAG(SOF_TIMESTAMPING_RX_HARDWARE),
605ef5c90eSJian Yang };
615ef5c90eSJian Yang 
625ef5c90eSJian Yang static struct socket_type socket_types[] = {
635ef5c90eSJian Yang 	{ "ip",		SOCK_RAW,	IPPROTO_EGP },
645ef5c90eSJian Yang 	{ "udp",	SOCK_DGRAM,	IPPROTO_UDP },
655ef5c90eSJian Yang 	{ "tcp",	SOCK_STREAM,	IPPROTO_TCP },
665ef5c90eSJian Yang };
675ef5c90eSJian Yang 
685ef5c90eSJian Yang static struct test_case test_cases[] = {
695ef5c90eSJian Yang 	{ {}, {} },
705ef5c90eSJian Yang 	{
71f551e2fdSTanner Love 		{ .so_timestamp = 1 },
72f551e2fdSTanner Love 		{ .tstamp = true }
735ef5c90eSJian Yang 	},
745ef5c90eSJian Yang 	{
75f551e2fdSTanner Love 		{ .so_timestampns = 1 },
76f551e2fdSTanner Love 		{ .tstampns = true }
775ef5c90eSJian Yang 	},
785ef5c90eSJian Yang 	{
79f551e2fdSTanner Love 		{ .so_timestamp = 1, .so_timestampns = 1 },
80f551e2fdSTanner Love 		{ .tstampns = true }
815ef5c90eSJian Yang 	},
825ef5c90eSJian Yang 	{
83f551e2fdSTanner Love 		{ .so_timestamping = SOF_TIMESTAMPING_RX_SOFTWARE },
845ef5c90eSJian Yang 		{}
855ef5c90eSJian Yang 	},
865ef5c90eSJian Yang 	{
875ef5c90eSJian Yang 		/* Loopback device does not support hw timestamps. */
88f551e2fdSTanner Love 		{ .so_timestamping = SOF_TIMESTAMPING_RX_HARDWARE },
895ef5c90eSJian Yang 		{}
905ef5c90eSJian Yang 	},
915ef5c90eSJian Yang 	{
92f551e2fdSTanner Love 		{ .so_timestamping = SOF_TIMESTAMPING_SOFTWARE },
93f551e2fdSTanner Love 		.warn_on_fail = true
945ef5c90eSJian Yang 	},
955ef5c90eSJian Yang 	{
96f551e2fdSTanner Love 		{ .so_timestamping = SOF_TIMESTAMPING_RX_SOFTWARE
975ef5c90eSJian Yang 			| SOF_TIMESTAMPING_RX_HARDWARE },
985ef5c90eSJian Yang 		{}
995ef5c90eSJian Yang 	},
1005ef5c90eSJian Yang 	{
101f551e2fdSTanner Love 		{ .so_timestamping = SOF_TIMESTAMPING_SOFTWARE
1025ef5c90eSJian Yang 			| SOF_TIMESTAMPING_RX_SOFTWARE },
103f551e2fdSTanner Love 		{ .swtstamp = true }
1045ef5c90eSJian Yang 	},
1055ef5c90eSJian Yang 	{
106f551e2fdSTanner Love 		{ .so_timestamp = 1, .so_timestamping = SOF_TIMESTAMPING_SOFTWARE
1075ef5c90eSJian Yang 			| SOF_TIMESTAMPING_RX_SOFTWARE },
108f551e2fdSTanner Love 		{ .tstamp = true, .swtstamp = true }
1095ef5c90eSJian Yang 	},
1105ef5c90eSJian Yang };
1115ef5c90eSJian Yang 
1125ef5c90eSJian Yang static struct option long_options[] = {
1135ef5c90eSJian Yang 	{ "list_tests", no_argument, 0, 'l' },
1145ef5c90eSJian Yang 	{ "test_num", required_argument, 0, 'n' },
1155ef5c90eSJian Yang 	{ "op_size", required_argument, 0, 's' },
1165ef5c90eSJian Yang 	{ "tcp", no_argument, 0, 't' },
1175ef5c90eSJian Yang 	{ "udp", no_argument, 0, 'u' },
1185ef5c90eSJian Yang 	{ "ip", no_argument, 0, 'i' },
1190558c396Stannerlove 	{ "strict", no_argument, 0, 'S' },
120b0d754efStannerlove 	{ "ipv4", no_argument, 0, '4' },
121b0d754efStannerlove 	{ "ipv6", no_argument, 0, '6' },
122865a6cbbStannerlove 	{ NULL, 0, NULL, 0 },
1235ef5c90eSJian Yang };
1245ef5c90eSJian Yang 
1255ef5c90eSJian Yang static int next_port = 19999;
1265ef5c90eSJian Yang static int op_size = 10 * 1024;
1275ef5c90eSJian Yang 
print_test_case(struct test_case * t)1285ef5c90eSJian Yang void print_test_case(struct test_case *t)
1295ef5c90eSJian Yang {
1305ef5c90eSJian Yang 	int f = 0;
1315ef5c90eSJian Yang 
1325ef5c90eSJian Yang 	printf("sockopts {");
1335ef5c90eSJian Yang 	if (t->sockopt.so_timestamp)
1345ef5c90eSJian Yang 		printf(" SO_TIMESTAMP ");
1355ef5c90eSJian Yang 	if (t->sockopt.so_timestampns)
1365ef5c90eSJian Yang 		printf(" SO_TIMESTAMPNS ");
1375ef5c90eSJian Yang 	if (t->sockopt.so_timestamping) {
1385ef5c90eSJian Yang 		printf(" SO_TIMESTAMPING: {");
1395ef5c90eSJian Yang 		for (f = 0; f < ARRAY_SIZE(sof_flags); f++)
1405ef5c90eSJian Yang 			if (t->sockopt.so_timestamping & sof_flags[f].mask)
1415ef5c90eSJian Yang 				printf(" %s |", sof_flags[f].name);
1425ef5c90eSJian Yang 		printf("}");
1435ef5c90eSJian Yang 	}
1445ef5c90eSJian Yang 	printf("} expected cmsgs: {");
1455ef5c90eSJian Yang 	if (t->expected.tstamp)
1465ef5c90eSJian Yang 		printf(" SCM_TIMESTAMP ");
1475ef5c90eSJian Yang 	if (t->expected.tstampns)
1485ef5c90eSJian Yang 		printf(" SCM_TIMESTAMPNS ");
1495ef5c90eSJian Yang 	if (t->expected.swtstamp || t->expected.hwtstamp) {
1505ef5c90eSJian Yang 		printf(" SCM_TIMESTAMPING {");
1515ef5c90eSJian Yang 		if (t->expected.swtstamp)
1525ef5c90eSJian Yang 			printf("0");
1535ef5c90eSJian Yang 		if (t->expected.swtstamp && t->expected.hwtstamp)
1545ef5c90eSJian Yang 			printf(",");
1555ef5c90eSJian Yang 		if (t->expected.hwtstamp)
1565ef5c90eSJian Yang 			printf("2");
1575ef5c90eSJian Yang 		printf("}");
1585ef5c90eSJian Yang 	}
1595ef5c90eSJian Yang 	printf("}\n");
1605ef5c90eSJian Yang }
1615ef5c90eSJian Yang 
do_send(int src)1625ef5c90eSJian Yang void do_send(int src)
1635ef5c90eSJian Yang {
1645ef5c90eSJian Yang 	int r;
1655ef5c90eSJian Yang 	char *buf = malloc(op_size);
1665ef5c90eSJian Yang 
1675ef5c90eSJian Yang 	memset(buf, 'z', op_size);
1685ef5c90eSJian Yang 	r = write(src, buf, op_size);
1695ef5c90eSJian Yang 	if (r < 0)
1705ef5c90eSJian Yang 		error(1, errno, "Failed to sendmsg");
1715ef5c90eSJian Yang 
1725ef5c90eSJian Yang 	free(buf);
1735ef5c90eSJian Yang }
1745ef5c90eSJian Yang 
do_recv(int rcv,int read_size,struct tstamps expected)1755ef5c90eSJian Yang bool do_recv(int rcv, int read_size, struct tstamps expected)
1765ef5c90eSJian Yang {
1775ef5c90eSJian Yang 	const int CMSG_SIZE = 1024;
1785ef5c90eSJian Yang 
1795ef5c90eSJian Yang 	struct scm_timestamping *ts;
1805ef5c90eSJian Yang 	struct tstamps actual = {};
1815ef5c90eSJian Yang 	char cmsg_buf[CMSG_SIZE];
1825ef5c90eSJian Yang 	struct iovec recv_iov;
1835ef5c90eSJian Yang 	struct cmsghdr *cmsg;
1845ef5c90eSJian Yang 	bool failed = false;
1855ef5c90eSJian Yang 	struct msghdr hdr;
1865ef5c90eSJian Yang 	int flags = 0;
1875ef5c90eSJian Yang 	int r;
1885ef5c90eSJian Yang 
1895ef5c90eSJian Yang 	memset(&hdr, 0, sizeof(hdr));
1905ef5c90eSJian Yang 	hdr.msg_iov = &recv_iov;
1915ef5c90eSJian Yang 	hdr.msg_iovlen = 1;
1925ef5c90eSJian Yang 	recv_iov.iov_base = malloc(read_size);
1935ef5c90eSJian Yang 	recv_iov.iov_len = read_size;
1945ef5c90eSJian Yang 
1955ef5c90eSJian Yang 	hdr.msg_control = cmsg_buf;
1965ef5c90eSJian Yang 	hdr.msg_controllen = sizeof(cmsg_buf);
1975ef5c90eSJian Yang 
1985ef5c90eSJian Yang 	r = recvmsg(rcv, &hdr, flags);
1995ef5c90eSJian Yang 	if (r < 0)
2005ef5c90eSJian Yang 		error(1, errno, "Failed to recvmsg");
2015ef5c90eSJian Yang 	if (r != read_size)
2025ef5c90eSJian Yang 		error(1, 0, "Only received %d bytes of payload.", r);
2035ef5c90eSJian Yang 
2045ef5c90eSJian Yang 	if (hdr.msg_flags & (MSG_TRUNC | MSG_CTRUNC))
2055ef5c90eSJian Yang 		error(1, 0, "Message was truncated.");
2065ef5c90eSJian Yang 
2075ef5c90eSJian Yang 	for (cmsg = CMSG_FIRSTHDR(&hdr); cmsg != NULL;
2085ef5c90eSJian Yang 	     cmsg = CMSG_NXTHDR(&hdr, cmsg)) {
2095ef5c90eSJian Yang 		if (cmsg->cmsg_level != SOL_SOCKET)
2105ef5c90eSJian Yang 			error(1, 0, "Unexpected cmsg_level %d",
2115ef5c90eSJian Yang 			      cmsg->cmsg_level);
2125ef5c90eSJian Yang 		switch (cmsg->cmsg_type) {
2135ef5c90eSJian Yang 		case SCM_TIMESTAMP:
2145ef5c90eSJian Yang 			actual.tstamp = true;
2155ef5c90eSJian Yang 			break;
2165ef5c90eSJian Yang 		case SCM_TIMESTAMPNS:
2175ef5c90eSJian Yang 			actual.tstampns = true;
2185ef5c90eSJian Yang 			break;
2195ef5c90eSJian Yang 		case SCM_TIMESTAMPING:
2205ef5c90eSJian Yang 			ts = (struct scm_timestamping *)CMSG_DATA(cmsg);
2215ef5c90eSJian Yang 			actual.swtstamp = !!ts->ts[0].tv_sec;
2225ef5c90eSJian Yang 			if (ts->ts[1].tv_sec != 0)
2235ef5c90eSJian Yang 				error(0, 0, "ts[1] should not be set.");
2245ef5c90eSJian Yang 			actual.hwtstamp = !!ts->ts[2].tv_sec;
2255ef5c90eSJian Yang 			break;
2265ef5c90eSJian Yang 		default:
2275ef5c90eSJian Yang 			error(1, 0, "Unexpected cmsg_type %d", cmsg->cmsg_type);
2285ef5c90eSJian Yang 		}
2295ef5c90eSJian Yang 	}
2305ef5c90eSJian Yang 
2315ef5c90eSJian Yang #define VALIDATE(field) \
2325ef5c90eSJian Yang 	do { \
2335ef5c90eSJian Yang 		if (expected.field != actual.field) { \
2345ef5c90eSJian Yang 			if (expected.field) \
2355ef5c90eSJian Yang 				error(0, 0, "Expected " #field " to be set."); \
2365ef5c90eSJian Yang 			else \
2375ef5c90eSJian Yang 				error(0, 0, \
2385ef5c90eSJian Yang 				      "Expected " #field " to not be set."); \
2395ef5c90eSJian Yang 			failed = true; \
2405ef5c90eSJian Yang 		} \
2415ef5c90eSJian Yang 	} while (0)
2425ef5c90eSJian Yang 
2435ef5c90eSJian Yang 	VALIDATE(tstamp);
2445ef5c90eSJian Yang 	VALIDATE(tstampns);
2455ef5c90eSJian Yang 	VALIDATE(swtstamp);
2465ef5c90eSJian Yang 	VALIDATE(hwtstamp);
2475ef5c90eSJian Yang #undef VALIDATE
2485ef5c90eSJian Yang 
2495ef5c90eSJian Yang 	free(recv_iov.iov_base);
2505ef5c90eSJian Yang 
2515ef5c90eSJian Yang 	return failed;
2525ef5c90eSJian Yang }
2535ef5c90eSJian Yang 
config_so_flags(int rcv,struct options o)2545ef5c90eSJian Yang void config_so_flags(int rcv, struct options o)
2555ef5c90eSJian Yang {
2565ef5c90eSJian Yang 	int on = 1;
2575ef5c90eSJian Yang 
2585ef5c90eSJian Yang 	if (setsockopt(rcv, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
2595ef5c90eSJian Yang 		error(1, errno, "Failed to enable SO_REUSEADDR");
2605ef5c90eSJian Yang 
2615ef5c90eSJian Yang 	if (o.so_timestamp &&
2625ef5c90eSJian Yang 	    setsockopt(rcv, SOL_SOCKET, SO_TIMESTAMP,
2635ef5c90eSJian Yang 		       &o.so_timestamp, sizeof(o.so_timestamp)) < 0)
2645ef5c90eSJian Yang 		error(1, errno, "Failed to enable SO_TIMESTAMP");
2655ef5c90eSJian Yang 
2665ef5c90eSJian Yang 	if (o.so_timestampns &&
2675ef5c90eSJian Yang 	    setsockopt(rcv, SOL_SOCKET, SO_TIMESTAMPNS,
2685ef5c90eSJian Yang 		       &o.so_timestampns, sizeof(o.so_timestampns)) < 0)
2695ef5c90eSJian Yang 		error(1, errno, "Failed to enable SO_TIMESTAMPNS");
2705ef5c90eSJian Yang 
2715ef5c90eSJian Yang 	if (o.so_timestamping &&
2725ef5c90eSJian Yang 	    setsockopt(rcv, SOL_SOCKET, SO_TIMESTAMPING,
2735ef5c90eSJian Yang 		       &o.so_timestamping, sizeof(o.so_timestamping)) < 0)
2745ef5c90eSJian Yang 		error(1, errno, "Failed to set SO_TIMESTAMPING");
2755ef5c90eSJian Yang }
2765ef5c90eSJian Yang 
run_test_case(struct socket_type * s,int test_num,char ip_version,bool strict)277b0d754efStannerlove bool run_test_case(struct socket_type *s, int test_num, char ip_version,
278b0d754efStannerlove 		   bool strict)
2795ef5c90eSJian Yang {
280b0d754efStannerlove 	union {
281b0d754efStannerlove 		struct sockaddr_in6 addr6;
282b0d754efStannerlove 		struct sockaddr_in addr4;
283b0d754efStannerlove 		struct sockaddr addr_un;
284b0d754efStannerlove 	} addr;
2855ef5c90eSJian Yang 	int read_size = op_size;
286b0d754efStannerlove 	int src, dst, rcv, port;
287b0d754efStannerlove 	socklen_t addr_size;
2885ef5c90eSJian Yang 	bool failed = false;
2895ef5c90eSJian Yang 
290b0d754efStannerlove 	port = (s->type == SOCK_RAW) ? 0 : next_port++;
291b0d754efStannerlove 	memset(&addr, 0, sizeof(addr));
292b0d754efStannerlove 	if (ip_version == '4') {
293b0d754efStannerlove 		addr.addr4.sin_family = AF_INET;
294b0d754efStannerlove 		addr.addr4.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
295b0d754efStannerlove 		addr.addr4.sin_port = htons(port);
296b0d754efStannerlove 		addr_size = sizeof(addr.addr4);
297b0d754efStannerlove 		if (s->type == SOCK_RAW)
298b0d754efStannerlove 			read_size += 20;  /* for IPv4 header */
299b0d754efStannerlove 	} else {
300b0d754efStannerlove 		addr.addr6.sin6_family = AF_INET6;
301b0d754efStannerlove 		addr.addr6.sin6_addr = in6addr_loopback;
302b0d754efStannerlove 		addr.addr6.sin6_port = htons(port);
303b0d754efStannerlove 		addr_size = sizeof(addr.addr6);
304b0d754efStannerlove 	}
305b0d754efStannerlove 	printf("Starting testcase %d over ipv%c...\n", test_num, ip_version);
306b0d754efStannerlove 	src = socket(addr.addr_un.sa_family, s->type,
307b0d754efStannerlove 		     s->protocol);
3085ef5c90eSJian Yang 	if (src < 0)
3095ef5c90eSJian Yang 		error(1, errno, "Failed to open src socket");
3105ef5c90eSJian Yang 
311b0d754efStannerlove 	dst = socket(addr.addr_un.sa_family, s->type,
312b0d754efStannerlove 		     s->protocol);
3135ef5c90eSJian Yang 	if (dst < 0)
3145ef5c90eSJian Yang 		error(1, errno, "Failed to open dst socket");
3155ef5c90eSJian Yang 
316b0d754efStannerlove 	if (bind(dst, &addr.addr_un, addr_size) < 0)
3175ef5c90eSJian Yang 		error(1, errno, "Failed to bind to port %d", port);
3185ef5c90eSJian Yang 
319b0d754efStannerlove 	if (s->type == SOCK_STREAM && (listen(dst, 1) < 0))
3205ef5c90eSJian Yang 		error(1, errno, "Failed to listen");
3215ef5c90eSJian Yang 
322b0d754efStannerlove 	if (connect(src, &addr.addr_un, addr_size) < 0)
3235ef5c90eSJian Yang 		error(1, errno, "Failed to connect");
3245ef5c90eSJian Yang 
325b0d754efStannerlove 	if (s->type == SOCK_STREAM) {
3265ef5c90eSJian Yang 		rcv = accept(dst, NULL, NULL);
3275ef5c90eSJian Yang 		if (rcv < 0)
3285ef5c90eSJian Yang 			error(1, errno, "Failed to accept");
3295ef5c90eSJian Yang 		close(dst);
3305ef5c90eSJian Yang 	} else {
3315ef5c90eSJian Yang 		rcv = dst;
3325ef5c90eSJian Yang 	}
3335ef5c90eSJian Yang 
334b0d754efStannerlove 	config_so_flags(rcv, test_cases[test_num].sockopt);
3355ef5c90eSJian Yang 	usleep(20000); /* setsockopt for SO_TIMESTAMPING is asynchronous */
3365ef5c90eSJian Yang 	do_send(src);
3375ef5c90eSJian Yang 
338b0d754efStannerlove 	failed = do_recv(rcv, read_size, test_cases[test_num].expected);
3395ef5c90eSJian Yang 
3405ef5c90eSJian Yang 	close(rcv);
3415ef5c90eSJian Yang 	close(src);
3425ef5c90eSJian Yang 
343b0d754efStannerlove 	if (failed) {
344b0d754efStannerlove 		printf("FAILURE in testcase %d over ipv%c ", test_num,
345b0d754efStannerlove 		       ip_version);
346b0d754efStannerlove 		print_test_case(&test_cases[test_num]);
347b0d754efStannerlove 		if (!strict && test_cases[test_num].warn_on_fail)
348b0d754efStannerlove 			failed = false;
349b0d754efStannerlove 	}
3505ef5c90eSJian Yang 	return failed;
3515ef5c90eSJian Yang }
3525ef5c90eSJian Yang 
main(int argc,char ** argv)3535ef5c90eSJian Yang int main(int argc, char **argv)
3545ef5c90eSJian Yang {
3555ef5c90eSJian Yang 	bool all_protocols = true;
3565ef5c90eSJian Yang 	bool all_tests = true;
357b0d754efStannerlove 	bool cfg_ipv4 = false;
358b0d754efStannerlove 	bool cfg_ipv6 = false;
3590558c396Stannerlove 	bool strict = false;
3605ef5c90eSJian Yang 	int arg_index = 0;
3615ef5c90eSJian Yang 	int failures = 0;
362955cbe91STanner Love 	int s, t, opt;
3635ef5c90eSJian Yang 
3645ef5c90eSJian Yang 	while ((opt = getopt_long(argc, argv, "", long_options,
3655ef5c90eSJian Yang 				  &arg_index)) != -1) {
3665ef5c90eSJian Yang 		switch (opt) {
3675ef5c90eSJian Yang 		case 'l':
3685ef5c90eSJian Yang 			for (t = 0; t < ARRAY_SIZE(test_cases); t++) {
3695ef5c90eSJian Yang 				printf("%d\t", t);
3705ef5c90eSJian Yang 				print_test_case(&test_cases[t]);
3715ef5c90eSJian Yang 			}
3725ef5c90eSJian Yang 			return 0;
3735ef5c90eSJian Yang 		case 'n':
3745ef5c90eSJian Yang 			t = atoi(optarg);
3755ef5c90eSJian Yang 			if (t >= ARRAY_SIZE(test_cases))
3765ef5c90eSJian Yang 				error(1, 0, "Invalid test case: %d", t);
3775ef5c90eSJian Yang 			all_tests = false;
3785ef5c90eSJian Yang 			test_cases[t].enabled = true;
3795ef5c90eSJian Yang 			break;
3805ef5c90eSJian Yang 		case 's':
3815ef5c90eSJian Yang 			op_size = atoi(optarg);
3825ef5c90eSJian Yang 			break;
3835ef5c90eSJian Yang 		case 't':
3845ef5c90eSJian Yang 			all_protocols = false;
3855ef5c90eSJian Yang 			socket_types[2].enabled = true;
3865ef5c90eSJian Yang 			break;
3875ef5c90eSJian Yang 		case 'u':
3885ef5c90eSJian Yang 			all_protocols = false;
3895ef5c90eSJian Yang 			socket_types[1].enabled = true;
3905ef5c90eSJian Yang 			break;
3915ef5c90eSJian Yang 		case 'i':
3925ef5c90eSJian Yang 			all_protocols = false;
3935ef5c90eSJian Yang 			socket_types[0].enabled = true;
3945ef5c90eSJian Yang 			break;
3950558c396Stannerlove 		case 'S':
3960558c396Stannerlove 			strict = true;
3970558c396Stannerlove 			break;
398b0d754efStannerlove 		case '4':
399b0d754efStannerlove 			cfg_ipv4 = true;
400b0d754efStannerlove 			break;
401b0d754efStannerlove 		case '6':
402b0d754efStannerlove 			cfg_ipv6 = true;
403b0d754efStannerlove 			break;
4045ef5c90eSJian Yang 		default:
4055ef5c90eSJian Yang 			error(1, 0, "Failed to parse parameters.");
4065ef5c90eSJian Yang 		}
4075ef5c90eSJian Yang 	}
4085ef5c90eSJian Yang 
4095ef5c90eSJian Yang 	for (s = 0; s < ARRAY_SIZE(socket_types); s++) {
4105ef5c90eSJian Yang 		if (!all_protocols && !socket_types[s].enabled)
4115ef5c90eSJian Yang 			continue;
4125ef5c90eSJian Yang 
4135ef5c90eSJian Yang 		printf("Testing %s...\n", socket_types[s].friendly_name);
4145ef5c90eSJian Yang 		for (t = 0; t < ARRAY_SIZE(test_cases); t++) {
4155ef5c90eSJian Yang 			if (!all_tests && !test_cases[t].enabled)
4165ef5c90eSJian Yang 				continue;
417b0d754efStannerlove 			if (cfg_ipv4 || !cfg_ipv6)
418b0d754efStannerlove 				if (run_test_case(&socket_types[s], t, '4',
419b0d754efStannerlove 						  strict))
4205ef5c90eSJian Yang 					failures++;
421b0d754efStannerlove 			if (cfg_ipv6 || !cfg_ipv4)
422b0d754efStannerlove 				if (run_test_case(&socket_types[s], t, '6',
423b0d754efStannerlove 						  strict))
424b0d754efStannerlove 					failures++;
4255ef5c90eSJian Yang 		}
4265ef5c90eSJian Yang 	}
4275ef5c90eSJian Yang 	if (!failures)
4285ef5c90eSJian Yang 		printf("PASSED.\n");
4295ef5c90eSJian Yang 	return failures;
4305ef5c90eSJian Yang }
431