summaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/networking/timestamping/rxtimestamp.c
blob: c6428f1ac22fb4965b0c25171d1ab9009fdb399d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#include <errno.h>
#include <error.h>
#include <getopt.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/time.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <net/if.h>

#include <asm/types.h>
#include <linux/net_tstamp.h>
#include <linux/errqueue.h>

#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))

struct options {
	int so_timestamp;
	int so_timestampns;
	int so_timestamping;
};

struct tstamps {
	bool tstamp;
	bool tstampns;
	bool swtstamp;
	bool hwtstamp;
};

struct socket_type {
	char *friendly_name;
	int type;
	int protocol;
	bool enabled;
};

struct test_case {
	struct options sockopt;
	struct tstamps expected;
	bool enabled;
};

struct sof_flag {
	int mask;
	char *name;
};

static struct sof_flag sof_flags[] = {
#define SOF_FLAG(f) { f, #f }
	SOF_FLAG(SOF_TIMESTAMPING_SOFTWARE),
	SOF_FLAG(SOF_TIMESTAMPING_RX_SOFTWARE),
	SOF_FLAG(SOF_TIMESTAMPING_RX_HARDWARE),
};

static struct socket_type socket_types[] = {
	{ "ip",		SOCK_RAW,	IPPROTO_EGP },
	{ "udp",	SOCK_DGRAM,	IPPROTO_UDP },
	{ "tcp",	SOCK_STREAM,	IPPROTO_TCP },
};

static struct test_case test_cases[] = {
	{ {}, {} },
	{
		{ so_timestamp: 1 },
		{ tstamp: true }
	},
	{
		{ so_timestampns: 1 },
		{ tstampns: true }
	},
	{
		{ so_timestamp: 1, so_timestampns: 1 },
		{ tstampns: true }
	},
	{
		{ so_timestamping: SOF_TIMESTAMPING_RX_SOFTWARE },
		{}
	},
	{
		/* Loopback device does not support hw timestamps. */
		{ so_timestamping: SOF_TIMESTAMPING_RX_HARDWARE },
		{}
	},
	{
		{ so_timestamping: SOF_TIMESTAMPING_SOFTWARE },
		{}
	},
	{
		{ so_timestamping: SOF_TIMESTAMPING_RX_SOFTWARE
			| SOF_TIMESTAMPING_RX_HARDWARE },
		{}
	},
	{
		{ so_timestamping: SOF_TIMESTAMPING_SOFTWARE
			| SOF_TIMESTAMPING_RX_SOFTWARE },
		{ swtstamp: true }
	},
	{
		{ so_timestamp: 1, so_timestamping: SOF_TIMESTAMPING_SOFTWARE
			| SOF_TIMESTAMPING_RX_SOFTWARE },
		{ tstamp: true, swtstamp: true }
	},
};

static struct option long_options[] = {
	{ "list_tests", no_argument, 0, 'l' },
	{ "test_num", required_argument, 0, 'n' },
	{ "op_size", required_argument, 0, 's' },
	{ "tcp", no_argument, 0, 't' },
	{ "udp", no_argument, 0, 'u' },
	{ "ip", no_argument, 0, 'i' },
	{ NULL, 0, NULL, 0 },
};

static int next_port = 19999;
static int op_size = 10 * 1024;

void print_test_case(struct test_case *t)
{
	int f = 0;

	printf("sockopts {");
	if (t->sockopt.so_timestamp)
		printf(" SO_TIMESTAMP ");
	if (t->sockopt.so_timestampns)
		printf(" SO_TIMESTAMPNS ");
	if (t->sockopt.so_timestamping) {
		printf(" SO_TIMESTAMPING: {");
		for (f = 0; f < ARRAY_SIZE(sof_flags); f++)
			if (t->sockopt.so_timestamping & sof_flags[f].mask)
				printf(" %s |", sof_flags[f].name);
		printf("}");
	}
	printf("} expected cmsgs: {");
	if (t->expected.tstamp)
		printf(" SCM_TIMESTAMP ");
	if (t->expected.tstampns)
		printf(" SCM_TIMESTAMPNS ");
	if (t->expected.swtstamp || t->expected.hwtstamp) {
		printf(" SCM_TIMESTAMPING {");
		if (t->expected.swtstamp)
			printf("0");
		if (t->expected.swtstamp && t->expected.hwtstamp)
			printf(",");
		if (t->expected.hwtstamp)
			printf("2");
		printf("}");
	}
	printf("}\n");
}

void do_send(int src)
{
	int r;
	char *buf = malloc(op_size);

	memset(buf, 'z', op_size);
	r = write(src, buf, op_size);
	if (r < 0)
		error(1, errno, "Failed to sendmsg");

	free(buf);
}

bool do_recv(int rcv, int read_size, struct tstamps expected)
{
	const int CMSG_SIZE = 1024;

	struct scm_timestamping *ts;
	struct tstamps actual = {};
	char cmsg_buf[CMSG_SIZE];
	struct iovec recv_iov;
	struct cmsghdr *cmsg;
	bool failed = false;
	struct msghdr hdr;
	int flags = 0;
	int r;

	memset(&hdr, 0, sizeof(hdr));
	hdr.msg_iov = &recv_iov;
	hdr.msg_iovlen = 1;
	recv_iov.iov_base = malloc(read_size);
	recv_iov.iov_len = read_size;

	hdr.msg_control = cmsg_buf;
	hdr.msg_controllen = sizeof(cmsg_buf);

	r = recvmsg(rcv, &hdr, flags);
	if (r < 0)
		error(1, errno, "Failed to recvmsg");
	if (r != read_size)
		error(1, 0, "Only received %d bytes of payload.", r);

	if (hdr.msg_flags & (MSG_TRUNC | MSG_CTRUNC))
		error(1, 0, "Message was truncated.");

	for (cmsg = CMSG_FIRSTHDR(&hdr); cmsg != NULL;
	     cmsg = CMSG_NXTHDR(&hdr, cmsg)) {
		if (cmsg->cmsg_level != SOL_SOCKET)
			error(1, 0, "Unexpected cmsg_level %d",
			      cmsg->cmsg_level);
		switch (cmsg->cmsg_type) {
		case SCM_TIMESTAMP:
			actual.tstamp = true;
			break;
		case SCM_TIMESTAMPNS:
			actual.tstampns = true;
			break;
		case SCM_TIMESTAMPING:
			ts = (struct scm_timestamping *)CMSG_DATA(cmsg);
			actual.swtstamp = !!ts->ts[0].tv_sec;
			if (ts->ts[1].tv_sec != 0)
				error(0, 0, "ts[1] should not be set.");
			actual.hwtstamp = !!ts->ts[2].tv_sec;
			break;
		default:
			error(1, 0, "Unexpected cmsg_type %d", cmsg->cmsg_type);
		}
	}

#define VALIDATE(field) \
	do { \
		if (expected.field != actual.field) { \
			if (expected.field) \
				error(0, 0, "Expected " #field " to be set."); \
			else \
				error(0, 0, \
				      "Expected " #field " to not be set."); \
			failed = true; \
		} \
	} while (0)

	VALIDATE(tstamp);
	VALIDATE(tstampns);
	VALIDATE(swtstamp);
	VALIDATE(hwtstamp);
#undef VALIDATE

	free(recv_iov.iov_base);

	return failed;
}

void config_so_flags(int rcv, struct options o)
{
	int on = 1;

	if (setsockopt(rcv, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
		error(1, errno, "Failed to enable SO_REUSEADDR");

	if (o.so_timestamp &&
	    setsockopt(rcv, SOL_SOCKET, SO_TIMESTAMP,
		       &o.so_timestamp, sizeof(o.so_timestamp)) < 0)
		error(1, errno, "Failed to enable SO_TIMESTAMP");

	if (o.so_timestampns &&
	    setsockopt(rcv, SOL_SOCKET, SO_TIMESTAMPNS,
		       &o.so_timestampns, sizeof(o.so_timestampns)) < 0)
		error(1, errno, "Failed to enable SO_TIMESTAMPNS");

	if (o.so_timestamping &&
	    setsockopt(rcv, SOL_SOCKET, SO_TIMESTAMPING,
		       &o.so_timestamping, sizeof(o.so_timestamping)) < 0)
		error(1, errno, "Failed to set SO_TIMESTAMPING");
}

bool run_test_case(struct socket_type s, struct test_case t)
{
	int port = (s.type == SOCK_RAW) ? 0 : next_port++;
	int read_size = op_size;
	struct sockaddr_in addr;
	bool failed = false;
	int src, dst, rcv;

	src = socket(AF_INET, s.type, s.protocol);
	if (src < 0)
		error(1, errno, "Failed to open src socket");

	dst = socket(AF_INET, s.type, s.protocol);
	if (dst < 0)
		error(1, errno, "Failed to open dst socket");

	memset(&addr, 0, sizeof(addr));
	addr.sin_family = AF_INET;
	addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
	addr.sin_port = htons(port);

	if (bind(dst, (struct sockaddr *)&addr, sizeof(addr)) < 0)
		error(1, errno, "Failed to bind to port %d", port);

	if (s.type == SOCK_STREAM && (listen(dst, 1) < 0))
		error(1, errno, "Failed to listen");

	if (connect(src, (struct sockaddr *)&addr, sizeof(addr)) < 0)
		error(1, errno, "Failed to connect");

	if (s.type == SOCK_STREAM) {
		rcv = accept(dst, NULL, NULL);
		if (rcv < 0)
			error(1, errno, "Failed to accept");
		close(dst);
	} else {
		rcv = dst;
	}

	config_so_flags(rcv, t.sockopt);
	usleep(20000); /* setsockopt for SO_TIMESTAMPING is asynchronous */
	do_send(src);

	if (s.type == SOCK_RAW)
		read_size += 20;  /* for IP header */
	failed = do_recv(rcv, read_size, t.expected);

	close(rcv);
	close(src);

	return failed;
}

int main(int argc, char **argv)
{
	bool all_protocols = true;
	bool all_tests = true;
	int arg_index = 0;
	int failures = 0;
	int s, t, opt;

	while ((opt = getopt_long(argc, argv, "", long_options,
				  &arg_index)) != -1) {
		switch (opt) {
		case 'l':
			for (t = 0; t < ARRAY_SIZE(test_cases); t++) {
				printf("%d\t", t);
				print_test_case(&test_cases[t]);
			}
			return 0;
		case 'n':
			t = atoi(optarg);
			if (t >= ARRAY_SIZE(test_cases))
				error(1, 0, "Invalid test case: %d", t);
			all_tests = false;
			test_cases[t].enabled = true;
			break;
		case 's':
			op_size = atoi(optarg);
			break;
		case 't':
			all_protocols = false;
			socket_types[2].enabled = true;
			break;
		case 'u':
			all_protocols = false;
			socket_types[1].enabled = true;
			break;
		case 'i':
			all_protocols = false;
			socket_types[0].enabled = true;
			break;
		default:
			error(1, 0, "Failed to parse parameters.");
		}
	}

	for (s = 0; s < ARRAY_SIZE(socket_types); s++) {
		if (!all_protocols && !socket_types[s].enabled)
			continue;

		printf("Testing %s...\n", socket_types[s].friendly_name);
		for (t = 0; t < ARRAY_SIZE(test_cases); t++) {
			if (!all_tests && !test_cases[t].enabled)
				continue;

			printf("Starting testcase %d...\n", t);
			if (run_test_case(socket_types[s], test_cases[t])) {
				failures++;
				printf("FAILURE in test case ");
				print_test_case(&test_cases[t]);
			}
		}
	}
	if (!failures)
		printf("PASSED.\n");
	return failures;
}