Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
  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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
// SPDX-License-Identifier: GPL-2.0

/* Test hardware checksum offload: Rx + Tx, IPv4 + IPv6, TCP + UDP.
 *
 * The test runs on two machines to exercise the NIC. For this reason it
 * is not integrated in kselftests.
 *
 *     CMD=$((./csum -[46] -[tu] -S $SADDR -D $DADDR -[RT] -r 1 $EXTRA_ARGS))
 *
 * Rx:
 *
 * The sender sends packets with a known checksum field using PF_INET(6)
 * SOCK_RAW sockets.
 *
 * good packet: $CMD [-t]
 * bad packet:  $CMD [-t] -E
 *
 * The receiver reads UDP packets with a UDP socket. This is not an
 * option for TCP packets ('-t'). Optionally insert an iptables filter
 * to avoid these entering the real protocol stack.
 *
 * The receiver also reads all packets with a PF_PACKET socket, to
 * observe whether both good and bad packets arrive on the host. And to
 * read the optional TP_STATUS_CSUM_VALID bit. This requires setting
 * option PACKET_AUXDATA, and works only for CHECKSUM_UNNECESSARY.
 *
 * Tx:
 *
 * The sender needs to build CHECKSUM_PARTIAL packets to exercise tx
 * checksum offload.
 *
 * The sender can sends packets with a UDP socket.
 *
 * Optionally crafts a packet that sums up to zero to verify that the
 * device writes negative zero 0xFFFF in this case to distinguish from
 * 0x0000 (checksum disabled), as required by RFC 768. Hit this case
 * by choosing a specific source port.
 *
 * good packet: $CMD -U
 * zero csum:   $CMD -U -Z
 *
 * The sender can also build packets with PF_PACKET with PACKET_VNET_HDR,
 * to cover more protocols. PF_PACKET requires passing src and dst mac
 * addresses.
 *
 * good packet: $CMD -s $smac -d $dmac -p [-t]
 *
 * Argument '-z' sends UDP packets with a 0x000 checksum disabled field,
 * to verify that the NIC passes these packets unmodified.
 *
 * Argument '-e' adds a transport mode encapsulation header between
 * network and transport header. This will fail for devices that parse
 *  headers. Should work on devices that implement protocol agnostic tx
 * checksum offload (NETIF_F_HW_CSUM).
 *
 * Argument '-r $SEED' optionally randomizes header, payload and length
 * to increase coverage between packets sent. SEED 1 further chooses a
 * different seed for each run (and logs this for reproducibility). It
 * is advised to enable this for extra coverage in continuous testing.
 */

#define _GNU_SOURCE

#include <arpa/inet.h>
#include <asm/byteorder.h>
#include <errno.h>
#include <error.h>
#include <linux/filter.h>
#include <linux/if_packet.h>
#include <linux/ipv6.h>
#include <linux/virtio_net.h>
#include <net/ethernet.h>
#include <net/if.h>
#include <netinet/if_ether.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <poll.h>
#include <sched.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

#include "kselftest.h"

static bool cfg_bad_csum;
static int cfg_family = PF_INET6;
static int cfg_num_pkt = 4;
static bool cfg_do_rx = true;
static bool cfg_do_tx = true;
static bool cfg_encap;
static char *cfg_ifname = "eth0";
static char *cfg_mac_dst;
static char *cfg_mac_src;
static int cfg_proto = IPPROTO_UDP;
static int cfg_payload_char = 'a';
static int cfg_payload_len = 100;
static uint16_t cfg_port_dst = 34000;
static uint16_t cfg_port_src = 33000;
static uint16_t cfg_port_src_encap = 33001;
static unsigned int cfg_random_seed;
static int cfg_rcvbuf = 1 << 22;	/* be able to queue large cfg_num_pkt */
static bool cfg_send_pfpacket;
static bool cfg_send_udp;
static int cfg_timeout_ms = 2000;
static bool cfg_zero_disable; /* skip checksum: set to zero (udp only) */
static bool cfg_zero_sum;     /* create packet that adds up to zero */

static struct sockaddr_in cfg_daddr4 = {.sin_family = AF_INET};
static struct sockaddr_in cfg_saddr4 = {.sin_family = AF_INET};
static struct sockaddr_in6 cfg_daddr6 = {.sin6_family = AF_INET6};
static struct sockaddr_in6 cfg_saddr6 = {.sin6_family = AF_INET6};

#define ENC_HEADER_LEN	(sizeof(struct udphdr) + sizeof(struct udp_encap_hdr))
#define MAX_HEADER_LEN	(sizeof(struct ipv6hdr) + ENC_HEADER_LEN + sizeof(struct tcphdr))
#define MAX_PAYLOAD_LEN 1024

/* Trivial demo encap. Stand-in for transport layer protocols like ESP or PSP */
struct udp_encap_hdr {
	uint8_t nexthdr;
	uint8_t padding[3];
};

/* Ipaddrs, for pseudo csum. Global var is ugly, pass through funcs was worse */
static void *iph_addr_p;

static unsigned long gettimeofday_ms(void)
{
	struct timeval tv;

	gettimeofday(&tv, NULL);
	return (tv.tv_sec * 1000UL) + (tv.tv_usec / 1000UL);
}

static uint32_t checksum_nofold(char *data, size_t len, uint32_t sum)
{
	uint16_t *words = (uint16_t *)data;
	int i;

	for (i = 0; i < len / 2; i++)
		sum += words[i];

	if (len & 1)
		sum += ((unsigned char *)data)[len - 1];

	return sum;
}

static uint16_t checksum_fold(void *data, size_t len, uint32_t sum)
{
	sum = checksum_nofold(data, len, sum);

	while (sum > 0xFFFF)
		sum = (sum & 0xFFFF) + (sum >> 16);

	return ~sum;
}

static uint16_t checksum(void *th, uint16_t proto, size_t len)
{
	uint32_t sum;
	int alen;

	alen = cfg_family == PF_INET6 ? 32 : 8;

	sum = checksum_nofold(iph_addr_p, alen, 0);
	sum += htons(proto);
	sum += htons(len);

	/* With CHECKSUM_PARTIAL kernel expects non-inverted pseudo csum */
	if (cfg_do_tx && cfg_send_pfpacket)
		return ~checksum_fold(NULL, 0, sum);
	else
		return checksum_fold(th, len, sum);
}

static void *build_packet_ipv4(void *_iph, uint8_t proto, unsigned int len)
{
	struct iphdr *iph = _iph;

	memset(iph, 0, sizeof(*iph));

	iph->version = 4;
	iph->ihl = 5;
	iph->ttl = 8;
	iph->protocol = proto;
	iph->saddr = cfg_saddr4.sin_addr.s_addr;
	iph->daddr = cfg_daddr4.sin_addr.s_addr;
	iph->tot_len = htons(sizeof(*iph) + len);
	iph->check = checksum_fold(iph, sizeof(*iph), 0);

	iph_addr_p = &iph->saddr;

	return iph + 1;
}

static void *build_packet_ipv6(void *_ip6h, uint8_t proto, unsigned int len)
{
	struct ipv6hdr *ip6h = _ip6h;

	memset(ip6h, 0, sizeof(*ip6h));

	ip6h->version = 6;
	ip6h->payload_len = htons(len);
	ip6h->nexthdr = proto;
	ip6h->hop_limit = 64;
	ip6h->saddr = cfg_saddr6.sin6_addr;
	ip6h->daddr = cfg_daddr6.sin6_addr;

	iph_addr_p = &ip6h->saddr;

	return ip6h + 1;
}

static void *build_packet_udp(void *_uh)
{
	struct udphdr *uh = _uh;

	uh->source = htons(cfg_port_src);
	uh->dest = htons(cfg_port_dst);
	uh->len = htons(sizeof(*uh) + cfg_payload_len);
	uh->check = 0;

	/* choose source port so that uh->check adds up to zero */
	if (cfg_zero_sum) {
		uh->source = 0;
		uh->source = checksum(uh, IPPROTO_UDP, sizeof(*uh) + cfg_payload_len);

		fprintf(stderr, "tx: changing sport: %hu -> %hu\n",
			cfg_port_src, ntohs(uh->source));
		cfg_port_src = ntohs(uh->source);
	}

	if (cfg_zero_disable)
		uh->check = 0;
	else
		uh->check = checksum(uh, IPPROTO_UDP, sizeof(*uh) + cfg_payload_len);

	if (cfg_bad_csum)
		uh->check = ~uh->check;

	fprintf(stderr, "tx: sending checksum: 0x%x\n", uh->check);
	return uh + 1;
}

static void *build_packet_tcp(void *_th)
{
	struct tcphdr *th = _th;

	th->source = htons(cfg_port_src);
	th->dest = htons(cfg_port_dst);
	th->doff = 5;
	th->check = 0;

	th->check = checksum(th, IPPROTO_TCP, sizeof(*th) + cfg_payload_len);

	if (cfg_bad_csum)
		th->check = ~th->check;

	fprintf(stderr, "tx: sending checksum: 0x%x\n", th->check);
	return th + 1;
}

static char *build_packet_udp_encap(void *_uh)
{
	struct udphdr *uh = _uh;
	struct udp_encap_hdr *eh = _uh + sizeof(*uh);

	/* outer dst == inner dst, to simplify BPF filter
	 * outer src != inner src, to demultiplex on recv
	 */
	uh->dest = htons(cfg_port_dst);
	uh->source = htons(cfg_port_src_encap);
	uh->check = 0;
	uh->len = htons(sizeof(*uh) +
			sizeof(*eh) +
			sizeof(struct tcphdr) +
			cfg_payload_len);

	eh->nexthdr = IPPROTO_TCP;

	return build_packet_tcp(eh + 1);
}

static char *build_packet(char *buf, int max_len, int *len)
{
	uint8_t proto;
	char *off;
	int tlen;

	if (cfg_random_seed) {
		int *buf32 = (void *)buf;
		int i;

		for (i = 0; i < (max_len / sizeof(int)); i++)
			buf32[i] = rand();
	} else {
		memset(buf, cfg_payload_char, max_len);
	}

	if (cfg_proto == IPPROTO_UDP)
		tlen = sizeof(struct udphdr) + cfg_payload_len;
	else
		tlen = sizeof(struct tcphdr) + cfg_payload_len;

	if (cfg_encap) {
		proto = IPPROTO_UDP;
		tlen += ENC_HEADER_LEN;
	} else {
		proto = cfg_proto;
	}

	if (cfg_family == PF_INET)
		off = build_packet_ipv4(buf, proto, tlen);
	else
		off = build_packet_ipv6(buf, proto, tlen);

	if (cfg_encap)
		off = build_packet_udp_encap(off);
	else if (cfg_proto == IPPROTO_UDP)
		off = build_packet_udp(off);
	else
		off = build_packet_tcp(off);

	/* only pass the payload, but still compute headers for cfg_zero_sum */
	if (cfg_send_udp) {
		*len = cfg_payload_len;
		return off;
	}

	*len = off - buf + cfg_payload_len;
	return buf;
}

static int open_inet(int ipproto, int protocol)
{
	int fd;

	fd = socket(cfg_family, ipproto, protocol);
	if (fd == -1)
		error(1, errno, "socket inet");

	if (cfg_family == PF_INET6) {
		/* may have been updated by cfg_zero_sum */
		cfg_saddr6.sin6_port = htons(cfg_port_src);

		if (bind(fd, (void *)&cfg_saddr6, sizeof(cfg_saddr6)))
			error(1, errno, "bind dgram 6");
		if (connect(fd, (void *)&cfg_daddr6, sizeof(cfg_daddr6)))
			error(1, errno, "connect dgram 6");
	} else {
		/* may have been updated by cfg_zero_sum */
		cfg_saddr4.sin_port = htons(cfg_port_src);

		if (bind(fd, (void *)&cfg_saddr4, sizeof(cfg_saddr4)))
			error(1, errno, "bind dgram 4");
		if (connect(fd, (void *)&cfg_daddr4, sizeof(cfg_daddr4)))
			error(1, errno, "connect dgram 4");
	}

	return fd;
}

static int open_packet(void)
{
	int fd, one = 1;

	fd = socket(PF_PACKET, SOCK_RAW, 0);
	if (fd == -1)
		error(1, errno, "socket packet");

	if (setsockopt(fd, SOL_PACKET, PACKET_VNET_HDR, &one, sizeof(one)))
		error(1, errno, "setsockopt packet_vnet_ndr");

	return fd;
}

static void send_inet(int fd, const char *buf, int len)
{
	int ret;

	ret = write(fd, buf, len);
	if (ret == -1)
		error(1, errno, "write");
	if (ret != len)
		error(1, 0, "write: %d", ret);
}

static void eth_str_to_addr(const char *str, unsigned char *eth)
{
	if (sscanf(str, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
		   &eth[0], &eth[1], &eth[2], &eth[3], &eth[4], &eth[5]) != 6)
		error(1, 0, "cannot parse mac addr %s", str);
}

static void send_packet(int fd, const char *buf, int len)
{
	struct virtio_net_hdr vh = {0};
	struct sockaddr_ll addr = {0};
	struct msghdr msg = {0};
	struct ethhdr eth;
	struct iovec iov[3];
	int ret;

	addr.sll_family = AF_PACKET;
	addr.sll_halen = ETH_ALEN;
	addr.sll_ifindex = if_nametoindex(cfg_ifname);
	if (!addr.sll_ifindex)
		error(1, errno, "if_nametoindex %s", cfg_ifname);

	vh.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
	if (cfg_family == PF_INET6) {
		vh.csum_start = sizeof(struct ethhdr) + sizeof(struct ipv6hdr);
		addr.sll_protocol = htons(ETH_P_IPV6);
	} else {
		vh.csum_start = sizeof(struct ethhdr) + sizeof(struct iphdr);
		addr.sll_protocol = htons(ETH_P_IP);
	}

	if (cfg_encap)
		vh.csum_start += ENC_HEADER_LEN;

	if (cfg_proto == IPPROTO_TCP) {
		vh.csum_offset = __builtin_offsetof(struct tcphdr, check);
		vh.hdr_len = vh.csum_start + sizeof(struct tcphdr);
	} else {
		vh.csum_offset = __builtin_offsetof(struct udphdr, check);
		vh.hdr_len = vh.csum_start + sizeof(struct udphdr);
	}

	eth_str_to_addr(cfg_mac_src, eth.h_source);
	eth_str_to_addr(cfg_mac_dst, eth.h_dest);
	eth.h_proto = addr.sll_protocol;

	iov[0].iov_base = &vh;
	iov[0].iov_len = sizeof(vh);

	iov[1].iov_base = &eth;
	iov[1].iov_len = sizeof(eth);

	iov[2].iov_base = (void *)buf;
	iov[2].iov_len = len;

	msg.msg_iov = iov;
	msg.msg_iovlen = ARRAY_SIZE(iov);

	msg.msg_name = &addr;
	msg.msg_namelen = sizeof(addr);

	ret = sendmsg(fd, &msg, 0);
	if (ret == -1)
		error(1, errno, "sendmsg packet");
	if (ret != sizeof(vh) + sizeof(eth) + len)
		error(1, errno, "sendmsg packet: %u", ret);
}

static int recv_prepare_udp(void)
{
	int fd;

	fd = socket(cfg_family, SOCK_DGRAM, 0);
	if (fd == -1)
		error(1, errno, "socket r");

	if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF,
		       &cfg_rcvbuf, sizeof(cfg_rcvbuf)))
		error(1, errno, "setsockopt SO_RCVBUF r");

	if (cfg_family == PF_INET6) {
		if (bind(fd, (void *)&cfg_daddr6, sizeof(cfg_daddr6)))
			error(1, errno, "bind r");
	} else {
		if (bind(fd, (void *)&cfg_daddr4, sizeof(cfg_daddr4)))
			error(1, errno, "bind r");
	}

	return fd;
}

/* Filter out all traffic that is not cfg_proto with our destination port.
 *
 * Otherwise background noise may cause PF_PACKET receive queue overflow,
 * dropping the expected packets and failing the test.
 */
static void __recv_prepare_packet_filter(int fd, int off_nexthdr, int off_dport)
{
	struct sock_filter filter[] = {
		BPF_STMT(BPF_LD + BPF_B + BPF_ABS, SKF_AD_OFF + SKF_AD_PKTTYPE),
		BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, PACKET_HOST, 0, 4),
		BPF_STMT(BPF_LD + BPF_B + BPF_ABS, off_nexthdr),
		BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, cfg_encap ? IPPROTO_UDP : cfg_proto, 0, 2),
		BPF_STMT(BPF_LD + BPF_H + BPF_ABS, off_dport),
		BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, cfg_port_dst, 1, 0),
		BPF_STMT(BPF_RET + BPF_K, 0),
		BPF_STMT(BPF_RET + BPF_K, 0xFFFF),
	};
	struct sock_fprog prog = {};

	prog.filter = filter;
	prog.len = ARRAY_SIZE(filter);
	if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &prog, sizeof(prog)))
		error(1, errno, "setsockopt filter");
}

static void recv_prepare_packet_filter(int fd)
{
	const int off_dport = offsetof(struct tcphdr, dest); /* same for udp */

	if (cfg_family == AF_INET)
		__recv_prepare_packet_filter(fd, offsetof(struct iphdr, protocol),
					     sizeof(struct iphdr) + off_dport);
	else
		__recv_prepare_packet_filter(fd, offsetof(struct ipv6hdr, nexthdr),
					     sizeof(struct ipv6hdr) + off_dport);
}

static void recv_prepare_packet_bind(int fd)
{
	struct sockaddr_ll laddr = {0};

	laddr.sll_family = AF_PACKET;

	if (cfg_family == PF_INET)
		laddr.sll_protocol = htons(ETH_P_IP);
	else
		laddr.sll_protocol = htons(ETH_P_IPV6);

	laddr.sll_ifindex = if_nametoindex(cfg_ifname);
	if (!laddr.sll_ifindex)
		error(1, 0, "if_nametoindex %s", cfg_ifname);

	if (bind(fd, (void *)&laddr, sizeof(laddr)))
		error(1, errno, "bind pf_packet");
}

static int recv_prepare_packet(void)
{
	int fd, one = 1;

	fd = socket(PF_PACKET, SOCK_DGRAM, 0);
	if (fd == -1)
		error(1, errno, "socket p");

	if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF,
		       &cfg_rcvbuf, sizeof(cfg_rcvbuf)))
		error(1, errno, "setsockopt SO_RCVBUF p");

	/* enable auxdata to recv checksum status (valid vs unknown) */
	if (setsockopt(fd, SOL_PACKET, PACKET_AUXDATA, &one, sizeof(one)))
		error(1, errno, "setsockopt auxdata");

	/* install filter to restrict packet flow to match */
	recv_prepare_packet_filter(fd);

	/* bind to address family to start packet flow */
	recv_prepare_packet_bind(fd);

	return fd;
}

static int recv_udp(int fd)
{
	static char buf[MAX_PAYLOAD_LEN];
	int ret, count = 0;

	while (1) {
		ret = recv(fd, buf, sizeof(buf), MSG_DONTWAIT);
		if (ret == -1 && errno == EAGAIN)
			break;
		if (ret == -1)
			error(1, errno, "recv r");

		fprintf(stderr, "rx: udp: len=%u\n", ret);
		count++;
	}

	return count;
}

static int recv_verify_csum(void *th, int len, uint16_t sport, uint16_t csum_field)
{
	uint16_t csum;

	csum = checksum(th, cfg_proto, len);

	fprintf(stderr, "rx: pkt: sport=%hu len=%u csum=0x%hx verify=0x%hx\n",
		sport, len, csum_field, csum);

	/* csum must be zero unless cfg_bad_csum indicates bad csum */
	if (csum && !cfg_bad_csum) {
		fprintf(stderr, "pkt: bad csum\n");
		return 1;
	} else if (cfg_bad_csum && !csum) {
		fprintf(stderr, "pkt: good csum, while bad expected\n");
		return 1;
	}

	if (cfg_zero_sum && csum_field != 0xFFFF) {
		fprintf(stderr, "pkt: zero csum: field should be 0xFFFF, is 0x%hx\n", csum_field);
		return 1;
	}

	return 0;
}

static int recv_verify_packet_tcp(void *th, int len)
{
	struct tcphdr *tcph = th;

	if (len < sizeof(*tcph) || tcph->dest != htons(cfg_port_dst))
		return -1;

	return recv_verify_csum(th, len, ntohs(tcph->source), tcph->check);
}

static int recv_verify_packet_udp_encap(void *th, int len)
{
	struct udp_encap_hdr *eh = th;

	if (len < sizeof(*eh) || eh->nexthdr != IPPROTO_TCP)
		return -1;

	return recv_verify_packet_tcp(eh + 1, len - sizeof(*eh));
}

static int recv_verify_packet_udp(void *th, int len)
{
	struct udphdr *udph = th;

	if (len < sizeof(*udph))
		return -1;

	if (udph->dest != htons(cfg_port_dst))
		return -1;

	if (udph->source == htons(cfg_port_src_encap))
		return recv_verify_packet_udp_encap(udph + 1,
						    len - sizeof(*udph));

	return recv_verify_csum(th, len, ntohs(udph->source), udph->check);
}

static int recv_verify_packet_ipv4(void *nh, int len)
{
	struct iphdr *iph = nh;
	uint16_t proto = cfg_encap ? IPPROTO_UDP : cfg_proto;

	if (len < sizeof(*iph) || iph->protocol != proto)
		return -1;

	iph_addr_p = &iph->saddr;
	if (proto == IPPROTO_TCP)
		return recv_verify_packet_tcp(iph + 1, len - sizeof(*iph));
	else
		return recv_verify_packet_udp(iph + 1, len - sizeof(*iph));
}

static int recv_verify_packet_ipv6(void *nh, int len)
{
	struct ipv6hdr *ip6h = nh;
	uint16_t proto = cfg_encap ? IPPROTO_UDP : cfg_proto;

	if (len < sizeof(*ip6h) || ip6h->nexthdr != proto)
		return -1;

	iph_addr_p = &ip6h->saddr;

	if (proto == IPPROTO_TCP)
		return recv_verify_packet_tcp(ip6h + 1, len - sizeof(*ip6h));
	else
		return recv_verify_packet_udp(ip6h + 1, len - sizeof(*ip6h));
}

/* return whether auxdata includes TP_STATUS_CSUM_VALID */
static bool recv_verify_packet_csum(struct msghdr *msg)
{
	struct tpacket_auxdata *aux = NULL;
	struct cmsghdr *cm;

	if (msg->msg_flags & MSG_CTRUNC)
		error(1, 0, "cmsg: truncated");

	for (cm = CMSG_FIRSTHDR(msg); cm; cm = CMSG_NXTHDR(msg, cm)) {
		if (cm->cmsg_level != SOL_PACKET ||
		    cm->cmsg_type != PACKET_AUXDATA)
			error(1, 0, "cmsg: level=%d type=%d\n",
			      cm->cmsg_level, cm->cmsg_type);

		if (cm->cmsg_len != CMSG_LEN(sizeof(struct tpacket_auxdata)))
			error(1, 0, "cmsg: len=%lu expected=%lu",
			      cm->cmsg_len, CMSG_LEN(sizeof(struct tpacket_auxdata)));

		aux = (void *)CMSG_DATA(cm);
	}

	if (!aux)
		error(1, 0, "cmsg: no auxdata");

	return aux->tp_status & TP_STATUS_CSUM_VALID;
}

static int recv_packet(int fd)
{
	static char _buf[MAX_HEADER_LEN + MAX_PAYLOAD_LEN];
	unsigned long total = 0, bad_csums = 0, bad_validations = 0;
	char ctrl[CMSG_SPACE(sizeof(struct tpacket_auxdata))];
	struct pkt *buf = (void *)_buf;
	struct msghdr msg = {0};
	struct iovec iov;
	int len, ret;

	iov.iov_base = _buf;
	iov.iov_len = sizeof(_buf);

	msg.msg_iov = &iov;
	msg.msg_iovlen = 1;

	msg.msg_control = ctrl;
	msg.msg_controllen = sizeof(ctrl);

	while (1) {
		msg.msg_flags = 0;

		len = recvmsg(fd, &msg, MSG_DONTWAIT);
		if (len == -1 && errno == EAGAIN)
			break;
		if (len == -1)
			error(1, errno, "recv p");

		if (cfg_family == PF_INET6)
			ret = recv_verify_packet_ipv6(buf, len);
		else
			ret = recv_verify_packet_ipv4(buf, len);

		if (ret == -1 /* skip: non-matching */)
			continue;

		total++;
		if (ret == 1)
			bad_csums++;

		/* Fail if kernel returns valid for known bad csum.
		 * Do not fail if kernel does not validate a good csum:
		 * Absence of validation does not imply invalid.
		 */
		if (recv_verify_packet_csum(&msg) && cfg_bad_csum) {
			fprintf(stderr, "cmsg: expected bad csum, pf_packet returns valid\n");
			bad_validations++;
		}
	}

	if (bad_csums || bad_validations)
		error(1, 0, "rx: errors at pf_packet: total=%lu bad_csums=%lu bad_valids=%lu\n",
		      total, bad_csums, bad_validations);

	return total;
}

static void parse_args(int argc, char *const argv[])
{
	const char *daddr = NULL, *saddr = NULL;
	int c;

	while ((c = getopt(argc, argv, "46d:D:eEi:l:L:n:r:PRs:S:tTuUzZ")) != -1) {
		switch (c) {
		case '4':
			cfg_family = PF_INET;
			break;
		case '6':
			cfg_family = PF_INET6;
			break;
		case 'd':
			cfg_mac_dst = optarg;
			break;
		case 'D':
			daddr = optarg;
			break;
		case 'e':
			cfg_encap = true;
			break;
		case 'E':
			cfg_bad_csum = true;
			break;
		case 'i':
			cfg_ifname = optarg;
			break;
		case 'l':
			cfg_payload_len = strtol(optarg, NULL, 0);
			break;
		case 'L':
			cfg_timeout_ms = strtol(optarg, NULL, 0) * 1000;
			break;
		case 'n':
			cfg_num_pkt = strtol(optarg, NULL, 0);
			break;
		case 'r':
			cfg_random_seed = strtol(optarg, NULL, 0);
			break;
		case 'P':
			cfg_send_pfpacket = true;
			break;
		case 'R':
			/* only Rx: used with two machine tests */
			cfg_do_tx = false;
			break;
		case 's':
			cfg_mac_src = optarg;
			break;
		case 'S':
			saddr = optarg;
			break;
		case 't':
			cfg_proto = IPPROTO_TCP;
			break;
		case 'T':
			/* only Tx: used with two machine tests */
			cfg_do_rx = false;
			break;
		case 'u':
			cfg_proto = IPPROTO_UDP;
			break;
		case 'U':
			/* send using real udp socket,
			 * to exercise tx checksum offload
			 */
			cfg_send_udp = true;
			break;
		case 'z':
			cfg_zero_disable = true;
			break;
		case 'Z':
			cfg_zero_sum = true;
			break;
		default:
			error(1, 0, "unknown arg %c", c);
		}
	}

	if (!daddr || !saddr)
		error(1, 0, "Must pass -D <daddr> and -S <saddr>");

	if (cfg_do_tx && cfg_send_pfpacket && (!cfg_mac_src || !cfg_mac_dst))
		error(1, 0, "Transmit with pf_packet requires mac addresses");

	if (cfg_payload_len > MAX_PAYLOAD_LEN)
		error(1, 0, "Payload length exceeds max");

	if (cfg_proto != IPPROTO_UDP && (cfg_zero_sum || cfg_zero_disable))
		error(1, 0, "Only UDP supports zero csum");

	if (cfg_zero_sum && !cfg_send_udp)
		error(1, 0, "Zero checksum conversion requires -U for tx csum offload");
	if (cfg_zero_sum && cfg_bad_csum)
		error(1, 0, "Cannot combine zero checksum conversion and invalid checksum");
	if (cfg_zero_sum && cfg_random_seed)
		error(1, 0, "Cannot combine zero checksum conversion with randomization");

	if (cfg_family == PF_INET6) {
		cfg_saddr6.sin6_port = htons(cfg_port_src);
		cfg_daddr6.sin6_port = htons(cfg_port_dst);

		if (inet_pton(cfg_family, daddr, &cfg_daddr6.sin6_addr) != 1)
			error(1, errno, "Cannot parse ipv6 -D");
		if (inet_pton(cfg_family, saddr, &cfg_saddr6.sin6_addr) != 1)
			error(1, errno, "Cannot parse ipv6 -S");
	} else {
		cfg_saddr4.sin_port = htons(cfg_port_src);
		cfg_daddr4.sin_port = htons(cfg_port_dst);

		if (inet_pton(cfg_family, daddr, &cfg_daddr4.sin_addr) != 1)
			error(1, errno, "Cannot parse ipv4 -D");
		if (inet_pton(cfg_family, saddr, &cfg_saddr4.sin_addr) != 1)
			error(1, errno, "Cannot parse ipv4 -S");
	}

	if (cfg_do_tx && cfg_random_seed) {
		/* special case: time-based seed */
		if (cfg_random_seed == 1)
			cfg_random_seed = (unsigned int)gettimeofday_ms();
		srand(cfg_random_seed);
		fprintf(stderr, "randomization seed: %u\n", cfg_random_seed);
	}
}

static void do_tx(void)
{
	static char _buf[MAX_HEADER_LEN + MAX_PAYLOAD_LEN];
	char *buf;
	int fd, len, i;

	buf = build_packet(_buf, sizeof(_buf), &len);

	if (cfg_send_pfpacket)
		fd = open_packet();
	else if (cfg_send_udp)
		fd = open_inet(SOCK_DGRAM, 0);
	else
		fd = open_inet(SOCK_RAW, IPPROTO_RAW);

	for (i = 0; i < cfg_num_pkt; i++) {
		if (cfg_send_pfpacket)
			send_packet(fd, buf, len);
		else
			send_inet(fd, buf, len);

		/* randomize each packet individually to increase coverage */
		if (cfg_random_seed) {
			cfg_payload_len = rand() % MAX_PAYLOAD_LEN;
			buf = build_packet(_buf, sizeof(_buf), &len);
		}
	}

	if (close(fd))
		error(1, errno, "close tx");
}

static void do_rx(int fdp, int fdr)
{
	unsigned long count_udp = 0, count_pkt = 0;
	long tleft, tstop;
	struct pollfd pfd;

	tstop = gettimeofday_ms() + cfg_timeout_ms;
	tleft = cfg_timeout_ms;

	do {
		pfd.events = POLLIN;
		pfd.fd = fdp;
		if (poll(&pfd, 1, tleft) == -1)
			error(1, errno, "poll");

		if (pfd.revents & POLLIN)
			count_pkt += recv_packet(fdp);

		if (cfg_proto == IPPROTO_UDP)
			count_udp += recv_udp(fdr);

		tleft = tstop - gettimeofday_ms();
	} while (tleft > 0);

	if (close(fdr))
		error(1, errno, "close r");
	if (close(fdp))
		error(1, errno, "close p");

	if (count_pkt < cfg_num_pkt)
		error(1, 0, "rx: missing packets at pf_packet: %lu < %u",
		      count_pkt, cfg_num_pkt);

	if (cfg_proto == IPPROTO_UDP) {
		if (cfg_bad_csum && count_udp)
			error(1, 0, "rx: unexpected packets at udp");
		if (!cfg_bad_csum && !count_udp)
			error(1, 0, "rx: missing packets at udp");
	}
}

int main(int argc, char *const argv[])
{
	int fdp = -1, fdr = -1;		/* -1 to silence -Wmaybe-uninitialized */

	parse_args(argc, argv);

	/* open receive sockets before transmitting */
	if (cfg_do_rx) {
		fdp = recv_prepare_packet();
		fdr = recv_prepare_udp();
	}

	if (cfg_do_tx)
		do_tx();

	if (cfg_do_rx)
		do_rx(fdp, fdr);

	fprintf(stderr, "OK\n");
	return 0;
}