-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_lk_onload_stub.c
524 lines (420 loc) · 12.6 KB
/
test_lk_onload_stub.c
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/*
* Copyright 2023 Google LLC
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#define _GNU_SOURCE
#include <dlfcn.h>
#include <error.h>
#include <errno.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/errqueue.h>
#include <linux/net_tstamp.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>
#include <netinet/udp.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/epoll.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include "lk_onload_stub_ext.h"
static bool has_preload;
/* library support functions */
static int __fail_errno(const char *fn, int line)
{
fprintf(stderr, "%s.%d: %d (%s)\n", fn, line, errno, strerror(errno));
return 1;
}
#define fail_errno() __fail_errno(__func__, __LINE__)
static int __fail_str(const char *fn, int line, const char *str)
{
fprintf(stderr, "%s.%d: %s\n", fn, line, str);
return 1;
}
#define fail_str(s) __fail_str(__func__, __LINE__, s)
/* test functions */
static int test_dlsym(void)
{
ssize_t (*vmsplice_fn)(int fd, const struct iovec *iov,
size_t nr_segs, unsigned int flags);
/* verify that dlsym returns failure on missing symbol */
if (dlsym(RTLD_NEXT, "vmsplice_doesnotexist"))
return fail_str("dlsym: unexpected non-NULL");
/* verify that dlsym return success on a real symbol */
vmsplice_fn = dlsym(RTLD_NEXT, "vmsplice");
if (vmsplice_fn == NULL)
return fail_str("dlsym: unexpected NULL");
/* verify that the result from dlsym is likely sane */
if (vmsplice_fn(-1, NULL, 0, 0) != -1)
return fail_str("dlsym: unexpected success");
if (errno != EBADF)
return fail_errno();
return 0;
}
static int test_getsockopt_timestamping_val(int domain, int type, int val)
{
socklen_t slen;
int fd, get;
fd = socket(domain, type, 0);
if (fd == -1)
return fail_errno();
if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPING, &val, sizeof(val)))
return fail_errno();
slen = sizeof(get);
if (getsockopt(fd, SOL_SOCKET, SO_TIMESTAMPING, &get, &slen))
return fail_errno();
if (slen != sizeof(get))
return fail_str("getsockopt: invalid socklen");
if (get != val)
return fail_str("getsockopt: unexpected value");
if (close(fd))
return fail_errno();
return 0;
}
static int test_getsockopt_timestamping(int domain, int type)
{
const int hw_rx = SOF_TIMESTAMPING_RAW_HARDWARE |
SOF_TIMESTAMPING_RX_HARDWARE;
const int hw_tx = SOF_TIMESTAMPING_RAW_HARDWARE |
SOF_TIMESTAMPING_TX_HARDWARE;
const int sw_rx = SOF_TIMESTAMPING_SOFTWARE |
SOF_TIMESTAMPING_RX_SOFTWARE;
const int sw_tx = SOF_TIMESTAMPING_SOFTWARE |
SOF_TIMESTAMPING_TX_SOFTWARE;
const int hw = hw_rx | hw_tx;
const int sw = sw_rx | sw_tx;
const int rx = sw_rx | hw_rx;
const int all = hw | sw;
int ret = 0;
ret |= test_getsockopt_timestamping_val(domain, type, hw_rx);
ret |= test_getsockopt_timestamping_val(domain, type, sw_rx);
ret |= test_getsockopt_timestamping_val(domain, type, hw);
ret |= test_getsockopt_timestamping_val(domain, type, sw);
ret |= test_getsockopt_timestamping_val(domain, type, rx);
ret |= test_getsockopt_timestamping_val(domain, type, all);
return ret;
}
static int test_onload_nonaccel(int domain, int type)
{
int fd;
/* Without the LD_PRELOAD, these calls will fall back to the
* lk_onload_stub_ext.c functions, all of which return failure
*/
if (!has_preload)
return 0;
/* test onload_is_present */
if (onload_is_present())
return fail_str("onload_is_present: returns non-zero with LKOS");
/* test onload_fd_stat */
fd = socket(domain, type, 0);
if (fd == -1)
return fail_errno();
if (onload_fd_stat(fd, NULL))
return fail_str("onload_fd_stat: returns non-zero for non-accelerated socket");
if (close(fd))
return fail_errno();
/* test onload_socket_nonaccel */
fd = onload_socket_nonaccel(domain, type, 0);
if (fd == -1)
return fail_errno();
if (close(fd))
return fail_errno();
return 0;
}
static int test_onload_ordered_epoll_wait(int domain, int type)
{
#define NUM_REVENTS 2
struct epoll_event ev = { .events = EPOLLOUT }, rev[NUM_REVENTS];
struct onload_ordered_epoll_event oo_rev[NUM_REVENTS];
int fda, fdb, epoll_fd, ret, i;
fda = socket(domain, type, 0);
if (fda == -1)
return fail_errno();
fdb = socket(domain, type, 0);
if (fdb == -1)
return fail_errno();
epoll_fd = epoll_create1(0);
ev.data.fd = fda;
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fda, &ev))
return fail_errno();
ev.data.fd = fdb;
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fdb, &ev))
return fail_errno();
ret = epoll_wait(epoll_fd, rev, NUM_REVENTS, 1);
if (ret == -1)
return fail_errno();
if (ret != 2)
return fail_str("epoll_wait: count");
ret = onload_ordered_epoll_wait(epoll_fd, rev, oo_rev, NUM_REVENTS, 1);
if (has_preload) {
if (ret == -1)
return fail_errno();
if (ret != 2)
return fail_str("onload_ordered_epoll_wait: count");
for (i = 0; i < ret; i++)
if (oo_rev[i].ts.tv_sec)
return fail_str("onload_ordered_epoll_wait: non-zero ts");
} else {
if (ret != -1)
return fail_str("onload_ordered_epoll_wait: expected fail");
}
if (close(epoll_fd))
return fail_errno();
if (close(fdb))
return fail_errno();
if (close(fda))
return fail_errno();
return 0;
}
static int test_onload_stacks_api(int domain, int type)
{
char data_get_str[128];
int64_t data_get_int;
size_t len_get_str;
int fd;
/* not supported without preload: skip */
if (!has_preload)
return 0;
if (onload_set_stackname(0, 0, "test1"))
return fail_str("onload_set_stackname");
if (onload_stackname_save())
return fail_str("onload_stackname_save");
if (onload_set_stackname(0, 0, "test2"))
return fail_str("onload_set_stackname");
if (onload_stack_opt_set_int("EF_SPIN_USEC", 10))
return fail_str("onload_stack_opt_set_int");
if (onload_stack_opt_set_str("EF_SCALABLE_FILTERS", "eth0"))
return fail_str("onload_stack_opt_set_str");
if (onload_stack_opt_get_int("EF_SPIN_USEC", &data_get_int) != -1)
return fail_str("onload_stack_opt_get_int: not failing as expected");
len_get_str = sizeof(data_get_str);
if (onload_stack_opt_get_str("EF_SCALABLE_FILTERS", data_get_str, &len_get_str) != -1)
return fail_str("onload_stack_opt_get_str: not failing as expected");
fd = socket(domain, type, 0);
if (fd == -1)
return fail_errno();
if (onload_stack_opt_reset())
return fail_str("onload_stack_opt_reset");
if (onload_stackname_restore())
return fail_str("onload_stackname_restore");
if (onload_move_fd(fd))
return fail_str("onload_move_fd");
if (close(fd))
return fail_errno();
return 0;
}
static int socketpair_open(int domain, int type, int *fdt_p, int *fdr_p)
{
struct sockaddr_in addr4 = {0};
struct sockaddr_in6 addr6 = {0};
struct sockaddr *addr;
socklen_t alen;
int fdt, fdr;
fdt = socket(domain, type, 0);
if (fdt == -1)
return fail_errno();
fdr = socket(domain, type, 0);
if (fdr == -1)
return fail_errno();
if (domain == PF_INET6) {
addr6.sin6_family = domain;
addr6.sin6_port = 0;
addr6.sin6_addr = in6addr_loopback;
alen = sizeof(addr6);
addr = (void *)&addr6;
} else {
addr4.sin_family = domain;
addr4.sin_port = 0;
addr4.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
alen = sizeof(addr4);
addr = (void *)&addr4;
}
if (bind(fdr, addr, alen))
return fail_errno();
if (getsockname(fdr, addr, &alen))
return fail_errno();
if (type == SOCK_STREAM) {
if (listen(fdr, 1))
return fail_errno();
}
if (connect(fdt, addr, alen))
return fail_errno();
if (type == SOCK_STREAM) {
int fdl = fdr;
fdr = accept(fdl, NULL, NULL);
if (fdr == -1)
return fail_errno();
if (close(fdl))
return fail_errno();
}
*fdt_p = fdt;
*fdr_p = fdr;
return 0;
}
static int test_recv_msg_onepkt(int domain, int type)
{
char rxbuf[2];
int fdt, fdr, ret;
ret = socketpair_open(domain, type, &fdt, &fdr);
if (ret)
return ret;
if (write(fdt, "a", 1) != 1)
return fail_errno();
if (recv(fdr, rxbuf, sizeof(rxbuf), 0) != 1)
return fail_errno();
if (write(fdt, "a", 1) != 1)
return fail_errno();
if (recv(fdr, rxbuf, sizeof(rxbuf), ONLOAD_MSG_ONEPKT) != 1)
return fail_errno();
if (close(fdr))
return fail_errno();
if (close(fdt))
return fail_errno();
return 0;
}
static int test_setsockopt_timestamping_ctrl(int domain, int type)
{
int fd, val;
fd = socket(domain, type, 0);
if (fd == -1)
return fail_errno();
/* setsockopt will register request for timestamp types, even when
* hardware lacks support and will not generate the timestamps.
*/
val = SOF_TIMESTAMPING_SOFTWARE |
SOF_TIMESTAMPING_RX_SOFTWARE |
SOF_TIMESTAMPING_TX_SOFTWARE |
SOF_TIMESTAMPING_RAW_HARDWARE |
SOF_TIMESTAMPING_TX_HARDWARE |
SOF_TIMESTAMPING_RX_HARDWARE;
if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPING, &val, sizeof(val)))
return fail_errno();
if (close(fd))
return fail_errno();
return 0;
}
static int recvmsg_tstamp_cmsg(struct msghdr *msg)
{
struct scm_timestamping *tss;
struct cmsghdr *cm;
for (cm = CMSG_FIRSTHDR(msg); cm; cm = CMSG_NXTHDR(msg, cm)) {
if (cm->cmsg_level == SOL_SOCKET &&
cm->cmsg_type == SCM_TIMESTAMPING)
tss = (void *) CMSG_DATA(cm);
}
fprintf(stderr, "%s: sw=%lu.%lu hw=%lu.%lu\n",
__func__,
tss->ts[0].tv_sec, tss->ts[0].tv_nsec / 1000UL,
tss->ts[2].tv_sec, tss->ts[2].tv_nsec / 1000UL);
if (has_preload) {
if (tss->ts[0].tv_sec != tss->ts[2].tv_sec ||
tss->ts[0].tv_nsec != tss->ts[2].tv_nsec)
return fail_str("hw stamp does not match sw tstamp");
} else {
if (tss->ts[2].tv_sec || tss->ts[2].tv_nsec)
return fail_str("non-zero hw stamp on loopback");
}
return 0;
}
static int recvmsg_tstamp(int fd, int flags)
{
char ctrl[CMSG_SPACE(sizeof(struct scm_timestamping)) +
CMSG_SPACE(sizeof(struct sock_extended_err)) +
CMSG_SPACE(sizeof(struct sockaddr_in6))] = {0};
struct msghdr msg = {0};
struct iovec iov;
char data[2];
int ret;
iov.iov_base = data;
iov.iov_len = sizeof(data);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = ctrl;
msg.msg_controllen = sizeof(ctrl);
ret = recvmsg(fd, &msg, flags);
if (ret == -1)
return fail_errno();
if (flags == 0 && ret != 1)
return fail_str("recvmsg: wrong length: expect 1 on rx");
if (flags == MSG_ERRQUEUE && ret != 0)
return fail_str("recvmsg: wrong length: expect 0 on tx TSONLY");
ret = recvmsg_tstamp_cmsg(&msg);
if (ret)
return ret;
return 0;
}
/* Request timestamps from the loopback device
*
* Loopback does not support hardware timestamps
* - verify that these are not returned without has_preload
* - verify that these are the same as sw with has_preload
*/
static int test_setsockopt_timestamping_data(int domain, int type)
{
int fdt, fdr, ret, val;
ret = socketpair_open(domain, type, &fdt, &fdr);
if (ret)
return ret;
val = SOF_TIMESTAMPING_SOFTWARE |
SOF_TIMESTAMPING_RX_SOFTWARE |
SOF_TIMESTAMPING_TX_SOFTWARE |
SOF_TIMESTAMPING_RAW_HARDWARE |
SOF_TIMESTAMPING_TX_HARDWARE |
SOF_TIMESTAMPING_RX_HARDWARE |
SOF_TIMESTAMPING_OPT_TSONLY;
if (setsockopt(fdt, SOL_SOCKET, SO_TIMESTAMPING, &val, sizeof(val)))
return fail_errno();
if (setsockopt(fdr, SOL_SOCKET, SO_TIMESTAMPING, &val, sizeof(val)))
return fail_errno();
/* wait for static_branch netstamp_needed_key to be enabled */
usleep(10 * 1000);
if (write(fdt, "a", 1) != 1)
return fail_errno();
ret = recvmsg_tstamp(fdr, 0);
if (ret)
return ret;
ret = recvmsg_tstamp(fdt, MSG_ERRQUEUE);
if (ret)
return ret;
if (close(fdr))
return fail_errno();
if (close(fdt))
return fail_errno();
return 0;
}
int main(int argc, char **argv)
{
const int domains[] = { PF_INET, PF_INET6, 0 }, *p_domain;
const int types[] = { SOCK_STREAM, SOCK_DGRAM, 0 }, *p_type;
int ret = 0;
has_preload = getenv("LD_PRELOAD");
ret |= test_dlsym();
for (p_domain = domains; *p_domain; p_domain++) {
for (p_type = types; *p_type; p_type++) {
ret |= test_getsockopt_timestamping(*p_domain, *p_type);
ret |= test_onload_nonaccel(*p_domain, *p_type);
ret |= test_onload_ordered_epoll_wait(*p_domain, *p_type);
ret |= test_onload_stacks_api(*p_domain, *p_type);
ret |= test_recv_msg_onepkt(*p_domain, *p_type);
ret |= test_setsockopt_timestamping_ctrl(*p_domain, *p_type);
ret |= test_setsockopt_timestamping_data(*p_domain, *p_type);
}
}
return !!ret;
}