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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
|
// SPDX-License-Identifier: GPL-2.0
#define _GNU_SOURCE
#include <arpa/inet.h>
#include <errno.h>
#include <error.h>
#include <fcntl.h>
#include <limits.h>
#include <linux/filter.h>
#include <linux/bpf.h>
#include <linux/if_packet.h>
#include <linux/if_vlan.h>
#include <linux/virtio_net.h>
#include <net/if.h>
#include <net/ethernet.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#include <poll.h>
#include <sched.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "psock_lib.h"
static bool cfg_use_bind;
static bool cfg_use_csum_off;
static bool cfg_use_csum_off_bad;
static bool cfg_use_dgram;
static bool cfg_use_gso;
static bool cfg_use_qdisc_bypass;
static bool cfg_use_vlan;
static bool cfg_use_vnet;
static bool cfg_drop;
static bool cfg_aux_data;
static bool cfg_ignore_outgoing;
static char *cfg_ifname = "lo";
static int cfg_mtu = 1500;
static int cfg_payload_len = DATA_LEN;
static int cfg_truncate_len = INT_MAX;
static uint16_t cfg_port = 8000;
/* test sending up to max mtu + 1 */
#define TEST_SZ (sizeof(struct virtio_net_hdr) + ETH_HLEN + ETH_MAX_MTU + 1)
#define BURST_CNT (1000)
static char tbuf[TEST_SZ], rbuf[TEST_SZ];
static unsigned long add_csum_hword(const uint16_t *start, int num_u16)
{
unsigned long sum = 0;
int i;
for (i = 0; i < num_u16; i++)
sum += start[i];
return sum;
}
static uint16_t build_ip_csum(const uint16_t *start, int num_u16,
unsigned long sum)
{
sum += add_csum_hword(start, num_u16);
while (sum >> 16)
sum = (sum & 0xffff) + (sum >> 16);
return ~sum;
}
static int build_vnet_header(void *header)
{
struct virtio_net_hdr *vh = header;
vh->hdr_len = ETH_HLEN + sizeof(struct iphdr) + sizeof(struct udphdr);
if (cfg_use_csum_off) {
vh->flags |= VIRTIO_NET_HDR_F_NEEDS_CSUM;
vh->csum_start = ETH_HLEN + sizeof(struct iphdr);
vh->csum_offset = __builtin_offsetof(struct udphdr, check);
/* position check field exactly one byte beyond end of packet */
if (cfg_use_csum_off_bad)
vh->csum_start += sizeof(struct udphdr) + cfg_payload_len -
vh->csum_offset - 1;
}
if (cfg_use_gso) {
vh->gso_type = VIRTIO_NET_HDR_GSO_UDP;
vh->gso_size = cfg_mtu - sizeof(struct iphdr);
}
return sizeof(*vh);
}
static int build_eth_header(void *header)
{
struct ethhdr *eth = header;
if (cfg_use_vlan) {
uint16_t *tag = header + ETH_HLEN;
eth->h_proto = htons(ETH_P_8021Q);
tag[1] = htons(ETH_P_IP);
return ETH_HLEN + 4;
}
eth->h_proto = htons(ETH_P_IP);
return ETH_HLEN;
}
static int build_ipv4_header(void *header, int payload_len)
{
struct iphdr *iph = header;
iph->ihl = 5;
iph->version = 4;
iph->ttl = 8;
iph->tot_len = htons(sizeof(*iph) + sizeof(struct udphdr) + payload_len);
iph->id = htons(1337);
iph->protocol = IPPROTO_UDP;
iph->saddr = htonl((172 << 24) | (17 << 16) | 2);
iph->daddr = htonl((172 << 24) | (17 << 16) | 1);
iph->check = build_ip_csum((void *) iph, iph->ihl << 1, 0);
return iph->ihl << 2;
}
static int build_udp_header(void *header, int payload_len)
{
const int alen = sizeof(uint32_t);
struct udphdr *udph = header;
int len = sizeof(*udph) + payload_len;
udph->source = htons(9);
udph->dest = htons(cfg_port);
udph->len = htons(len);
if (cfg_use_csum_off)
udph->check = build_ip_csum(header - (2 * alen), alen,
htons(IPPROTO_UDP) + udph->len);
else
udph->check = 0;
return sizeof(*udph);
}
static int build_packet(int payload_len)
{
int off = 0;
off += build_vnet_header(tbuf);
off += build_eth_header(tbuf + off);
off += build_ipv4_header(tbuf + off, payload_len);
off += build_udp_header(tbuf + off, payload_len);
if (off + payload_len > sizeof(tbuf))
error(1, 0, "payload length exceeds max");
memset(tbuf + off, DATA_CHAR, payload_len);
return off + payload_len;
}
static void do_bind_proto(int fd, uint16_t proto)
{
struct sockaddr_ll laddr = {0};
laddr.sll_family = AF_PACKET;
laddr.sll_protocol = htons(proto);
laddr.sll_ifindex = if_nametoindex(cfg_ifname);
if (!laddr.sll_ifindex)
error(1, errno, "if_nametoindex");
if (bind(fd, (void *)&laddr, sizeof(laddr)))
error(1, errno, "bind");
}
static void do_bind(int fd)
{
do_bind_proto(fd, ETH_P_IP);
}
static void do_send(int fd, char *buf, int len)
{
int ret;
if (!cfg_use_vnet) {
buf += sizeof(struct virtio_net_hdr);
len -= sizeof(struct virtio_net_hdr);
}
if (cfg_use_dgram) {
buf += ETH_HLEN;
len -= ETH_HLEN;
}
if (cfg_use_bind) {
ret = write(fd, buf, len);
} else {
struct sockaddr_ll laddr = {0};
laddr.sll_protocol = htons(ETH_P_IP);
laddr.sll_ifindex = if_nametoindex(cfg_ifname);
if (!laddr.sll_ifindex)
error(1, errno, "if_nametoindex");
ret = sendto(fd, buf, len, 0, (void *)&laddr, sizeof(laddr));
}
if (ret == -1)
error(1, errno, "write");
if (ret != len)
error(1, 0, "write: %u %u", ret, len);
if (!cfg_drop)
fprintf(stderr, "tx: %u\n", ret);
}
static int do_tx(void)
{
const int one = 1;
int i, fd, len;
fd = socket(PF_PACKET, cfg_use_dgram ? SOCK_DGRAM : SOCK_RAW, 0);
if (fd == -1)
error(1, errno, "socket t");
if (cfg_use_bind)
do_bind(fd);
if (cfg_use_qdisc_bypass &&
setsockopt(fd, SOL_PACKET, PACKET_QDISC_BYPASS, &one, sizeof(one)))
error(1, errno, "setsockopt qdisc bypass");
if (cfg_use_vnet &&
setsockopt(fd, SOL_PACKET, PACKET_VNET_HDR, &one, sizeof(one)))
error(1, errno, "setsockopt vnet");
len = build_packet(cfg_payload_len);
if (cfg_truncate_len < len)
len = cfg_truncate_len;
do_send(fd, tbuf, len);
if (cfg_drop)
for (i = 0; i < BURST_CNT; i++)
do_send(fd, tbuf, len);
if (close(fd))
error(1, errno, "close t");
return len;
}
static int setup_rx(void)
{
struct timeval tv = { .tv_usec = 100 * 1000 };
struct sockaddr_in raddr = {0};
int fd;
fd = socket(PF_INET, SOCK_DGRAM, 0);
if (fd == -1)
error(1, errno, "socket r");
if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)))
error(1, errno, "setsockopt rcv timeout");
raddr.sin_family = AF_INET;
raddr.sin_port = htons(cfg_port);
raddr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(fd, (void *)&raddr, sizeof(raddr)))
error(1, errno, "bind r");
return fd;
}
static void check_aux_data(struct cmsghdr *cmsg, int expected_len)
{
struct tpacket_auxdata *adata;
if (!cmsg)
error(1, 0, "auxdata null");
if (cmsg->cmsg_level != SOL_PACKET)
error(1, 0, "cmsg_level != SOL_PACKET");
if (cmsg->cmsg_type != PACKET_AUXDATA)
error(1, 0, "cmsg_type != PACKET_AUXDATA");
adata = (struct tpacket_auxdata *)CMSG_DATA(cmsg);
if (adata->tp_net != ETH_HLEN)
error(1, 0, "cmsg tp_net != ETH_HLEN");
if (adata->tp_len != expected_len)
error(1, 0, "cmsg tp_len != %u", expected_len);
if (adata->tp_snaplen != expected_len)
error(1, 0, "cmsg tp_snaplen != %u", expected_len);
}
/* expected_pkttype < 0 skips the sll_pkttype check. */
static void do_rx(int fd, int expected_len, char *expected, bool is_psock,
int expected_pkttype)
{
char cmsg_buf[1024] __attribute__((aligned(8))) = {};
bool aux = is_psock && cfg_aux_data;
struct sockaddr_ll saddr = {};
struct iovec iov = {
.iov_base = rbuf,
.iov_len = sizeof(rbuf),
};
struct msghdr msg = {
.msg_iov = &iov,
.msg_iovlen = 1,
};
int ret;
if (aux) {
msg.msg_control = cmsg_buf;
msg.msg_controllen = sizeof(cmsg_buf);
}
if (is_psock) {
msg.msg_name = &saddr;
msg.msg_namelen = sizeof(saddr);
}
ret = recvmsg(fd, &msg, 0);
if (ret == -1)
error(1, errno, "recv");
if (ret != expected_len)
error(1, 0, "recv: %u != %u", ret, expected_len);
if (memcmp(rbuf, expected, ret))
error(1, 0, "recv: data mismatch");
if (aux)
check_aux_data(CMSG_FIRSTHDR(&msg), expected_len);
if (expected_pkttype >= 0 && saddr.sll_pkttype != expected_pkttype)
error(1, 0, "recv: sll_pkttype %d != %d",
saddr.sll_pkttype, expected_pkttype);
fprintf(stderr, "rx: %u\n", ret);
}
static int setup_sniffer(void)
{
struct timeval tv = { .tv_usec = 100 * 1000 };
const int one = 1;
int fd;
fd = socket(PF_PACKET, SOCK_RAW, 0);
if (fd == -1)
error(1, errno, "socket p");
if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)))
error(1, errno, "setsockopt rcv timeout");
if (cfg_drop)
if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &one, sizeof(one)))
error(1, errno, "setsockopt SO_RCVBUF");
if (cfg_aux_data)
if (setsockopt(fd, SOL_PACKET, PACKET_AUXDATA, &one, sizeof(one)))
error(1, errno, "setsockopt PACKET_AUXDATA");
pair_udp_setfilter(fd);
/* binding to ETH_P_ALL adds the sniffer to ptype_all, which will see
* the dev_queue_xmit_nit copy. ignore_outgoing should suppress this.
*/
if (cfg_ignore_outgoing)
do_bind_proto(fd, ETH_P_ALL);
else
do_bind(fd);
return fd;
}
static void parse_opts(int argc, char **argv)
{
int c;
while ((c = getopt(argc, argv, "abcCdDgil:qt:vV")) != -1) {
switch (c) {
case 'a':
cfg_aux_data = true;
break;
case 'b':
cfg_use_bind = true;
break;
case 'c':
cfg_use_csum_off = true;
break;
case 'C':
cfg_use_csum_off_bad = true;
break;
case 'd':
cfg_use_dgram = true;
break;
case 'D':
cfg_drop = true;
break;
case 'g':
cfg_use_gso = true;
break;
case 'i':
cfg_ignore_outgoing = true;
break;
case 'l':
cfg_payload_len = strtoul(optarg, NULL, 0);
break;
case 'q':
cfg_use_qdisc_bypass = true;
break;
case 't':
cfg_truncate_len = strtoul(optarg, NULL, 0);
break;
case 'v':
cfg_use_vnet = true;
break;
case 'V':
cfg_use_vlan = true;
break;
default:
error(1, 0, "%s: parse error", argv[0]);
}
}
if (cfg_use_vlan && cfg_use_dgram)
error(1, 0, "option vlan (-V) conflicts with dgram (-d)");
if (cfg_use_csum_off && !cfg_use_vnet)
error(1, 0, "option csum offload (-c) requires vnet (-v)");
if (cfg_use_csum_off_bad && !cfg_use_csum_off)
error(1, 0, "option csum bad (-C) requires csum offload (-c)");
if (cfg_use_gso && !cfg_use_csum_off)
error(1, 0, "option gso (-g) requires csum offload (-c)");
if (cfg_aux_data && cfg_drop)
error(1, 0, "option aux data (-a) conflicts with drop (-D)");
if (cfg_ignore_outgoing && (cfg_drop || cfg_aux_data))
error(1, 0,
"option ignore outgoing (-i) conflicts with -D and -a");
}
static void check_packet_stats(int fd, unsigned int expected_packets)
{
struct tpacket_stats st = {};
socklen_t len = sizeof(st);
if (getsockopt(fd, SOL_PACKET, PACKET_STATISTICS, &st, &len))
error(1, errno, "getsockopt packet statistics");
if (cfg_drop) {
/* PACKET_STATISTICS reports all packets seen (including
* drops) in tp_packets
*/
if (st.tp_packets < st.tp_drops)
error(1, 0, "stats: tp_packets %u < tp_drops %u",
st.tp_packets, st.tp_drops);
if (st.tp_drops == 0)
error(1, 0, "stats: expected drops but tp_drops == 0");
} else {
if (st.tp_packets != expected_packets)
error(1, 0, "stats: tp_packets %u != %u",
st.tp_packets, expected_packets);
if (st.tp_drops != 0)
error(1, 0, "stats: tp_drops %u != 0", st.tp_drops);
}
/* verify clear on read */
memset(&st, 0xff, sizeof(st));
len = sizeof(st);
if (getsockopt(fd, SOL_PACKET, PACKET_STATISTICS, &st, &len))
error(1, errno, "getsockopt packet statistics");
if (st.tp_packets != 0)
error(1, 0, "stats: tp_packets %u != 0 after clear", st.tp_packets);
if (st.tp_drops != 0)
error(1, 0, "stats: tp_drops %u != 0 after clear", st.tp_drops);
}
static void set_ignore_outgoing(int fd, int val)
{
socklen_t len = sizeof(int);
int got = -1;
if (setsockopt(fd, SOL_PACKET, PACKET_IGNORE_OUTGOING,
&val, sizeof(val)))
error(1, errno, "setsockopt PACKET_IGNORE_OUTGOING %d", val);
if (getsockopt(fd, SOL_PACKET, PACKET_IGNORE_OUTGOING, &got, &len))
error(1, errno, "getsockopt PACKET_IGNORE_OUTGOING");
if (got != val)
error(1, 0, "getsockopt: expected %d got %d", val, got);
}
static void check_ignore_outgoing_range(int fd)
{
int val;
/* Values outside [0, 1] must be rejected with -EINVAL. */
val = 2;
if (setsockopt(fd, SOL_PACKET, PACKET_IGNORE_OUTGOING,
&val, sizeof(val)) != -1 || errno != EINVAL)
error(1, errno,
"setsockopt PACKET_IGNORE_OUTGOING val=2: expected EINVAL");
val = -1;
if (setsockopt(fd, SOL_PACKET, PACKET_IGNORE_OUTGOING,
&val, sizeof(val)) != -1 || errno != EINVAL)
error(1, errno,
"setsockopt PACKET_IGNORE_OUTGOING val=-1: expected EINVAL");
}
static void test_ignore_outgoing(int fds)
{
char *expected = tbuf + sizeof(struct virtio_net_hdr);
int expected_len;
/* ptype_all sniffer on loopback should produce two copies per packet
* (RX and TX).
*/
expected_len = do_tx();
expected_len -= sizeof(struct virtio_net_hdr);
do_rx(fds, expected_len, expected, true, PACKET_OUTGOING);
do_rx(fds, expected_len, expected, true, PACKET_HOST);
check_packet_stats(fds, 2);
/* 0 and 1 accepted; anything else rejected. */
set_ignore_outgoing(fds, 0);
set_ignore_outgoing(fds, 1);
check_ignore_outgoing_range(fds);
/* With PACKET_IGNORE_OUTGOING set, only the rx copy survives. */
do_tx();
do_rx(fds, expected_len, expected, true, PACKET_HOST);
if (recv(fds, rbuf, sizeof(rbuf), 0) != -1 || errno != EAGAIN)
error(1, errno, "expected EAGAIN, got extra packet");
check_packet_stats(fds, 1);
}
static void run_test(void)
{
int fdr, fds, total_len;
fdr = setup_rx();
fds = setup_sniffer();
if (cfg_ignore_outgoing) {
test_ignore_outgoing(fds);
goto out;
}
total_len = do_tx();
if (cfg_drop) {
check_packet_stats(fds, 0);
goto out;
}
/* BPF filter accepts only this length, vlan changes MAC */
if (cfg_payload_len == DATA_LEN && !cfg_use_vlan) {
do_rx(fds, total_len - sizeof(struct virtio_net_hdr),
tbuf + sizeof(struct virtio_net_hdr), true, -1);
check_packet_stats(fds, 1);
}
do_rx(fdr, cfg_payload_len, tbuf + total_len - cfg_payload_len, false, -1);
out:
if (close(fds))
error(1, errno, "close s");
if (close(fdr))
error(1, errno, "close r");
}
int main(int argc, char **argv)
{
parse_opts(argc, argv);
if (system("ip link set dev lo mtu 1500"))
error(1, errno, "ip link set mtu");
if (system("ip addr add dev lo 172.17.0.1/24"))
error(1, errno, "ip addr add");
if (system("sysctl -w net.ipv4.conf.lo.accept_local=1"))
error(1, errno, "sysctl lo.accept_local");
run_test();
fprintf(stderr, "OK\n\n");
return 0;
}
|