xref: /linux/tools/testing/selftests/net/psock_tpacket.c (revision 6b16f5d12202a23d875915349cc031c07fe1b3ec)
1 /*
2  * Copyright 2013 Red Hat, Inc.
3  * Author: Daniel Borkmann <dborkman@redhat.com>
4  *         Chetan Loke <loke.chetan@gmail.com> (TPACKET_V3 usage example)
5  *
6  * A basic test of packet socket's TPACKET_V1/TPACKET_V2/TPACKET_V3 behavior.
7  *
8  * Control:
9  *   Test the setup of the TPACKET socket with different patterns that are
10  *   known to fail (TODO) resp. succeed (OK).
11  *
12  * Datapath:
13  *   Open a pair of packet sockets and send resp. receive an a priori known
14  *   packet pattern accross the sockets and check if it was received resp.
15  *   sent correctly. Fanout in combination with RX_RING is currently not
16  *   tested here.
17  *
18  *   The test currently runs for
19  *   - TPACKET_V1: RX_RING, TX_RING
20  *   - TPACKET_V2: RX_RING, TX_RING
21  *   - TPACKET_V3: RX_RING
22  *
23  * License (GPLv2):
24  *
25  * This program is free software; you can redistribute it and/or modify it
26  * under the terms and conditions of the GNU General Public License,
27  * version 2, as published by the Free Software Foundation.
28  *
29  * This program is distributed in the hope it will be useful, but WITHOUT
30  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
31  * FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for
32  * more details.
33  *
34  * You should have received a copy of the GNU General Public License along with
35  * this program; if not, write to the Free Software Foundation, Inc.,
36  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
37  */
38 
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <sys/socket.h>
44 #include <sys/mman.h>
45 #include <linux/if_packet.h>
46 #include <linux/filter.h>
47 #include <ctype.h>
48 #include <fcntl.h>
49 #include <unistd.h>
50 #include <bits/wordsize.h>
51 #include <net/ethernet.h>
52 #include <netinet/ip.h>
53 #include <arpa/inet.h>
54 #include <stdint.h>
55 #include <string.h>
56 #include <assert.h>
57 #include <net/if.h>
58 #include <inttypes.h>
59 #include <poll.h>
60 
61 #include "psock_lib.h"
62 
63 #include "../kselftest.h"
64 
65 #ifndef bug_on
66 # define bug_on(cond)		assert(!(cond))
67 #endif
68 
69 #ifndef __aligned_tpacket
70 # define __aligned_tpacket	__attribute__((aligned(TPACKET_ALIGNMENT)))
71 #endif
72 
73 #ifndef __align_tpacket
74 # define __align_tpacket(x)	__attribute__((aligned(TPACKET_ALIGN(x))))
75 #endif
76 
77 #define NUM_PACKETS		100
78 #define ALIGN_8(x)		(((x) + 8 - 1) & ~(8 - 1))
79 
80 struct ring {
81 	struct iovec *rd;
82 	uint8_t *mm_space;
83 	size_t mm_len, rd_len;
84 	struct sockaddr_ll ll;
85 	void (*walk)(int sock, struct ring *ring);
86 	int type, rd_num, flen, version;
87 	union {
88 		struct tpacket_req  req;
89 		struct tpacket_req3 req3;
90 	};
91 };
92 
93 struct block_desc {
94 	uint32_t version;
95 	uint32_t offset_to_priv;
96 	struct tpacket_hdr_v1 h1;
97 };
98 
99 union frame_map {
100 	struct {
101 		struct tpacket_hdr tp_h __aligned_tpacket;
102 		struct sockaddr_ll s_ll __align_tpacket(sizeof(struct tpacket_hdr));
103 	} *v1;
104 	struct {
105 		struct tpacket2_hdr tp_h __aligned_tpacket;
106 		struct sockaddr_ll s_ll __align_tpacket(sizeof(struct tpacket2_hdr));
107 	} *v2;
108 	void *raw;
109 };
110 
111 static unsigned int total_packets, total_bytes;
112 
113 static int pfsocket(int ver)
114 {
115 	int ret, sock = socket(PF_PACKET, SOCK_RAW, 0);
116 	if (sock == -1) {
117 		perror("socket");
118 		exit(1);
119 	}
120 
121 	ret = setsockopt(sock, SOL_PACKET, PACKET_VERSION, &ver, sizeof(ver));
122 	if (ret == -1) {
123 		perror("setsockopt");
124 		exit(1);
125 	}
126 
127 	return sock;
128 }
129 
130 static void status_bar_update(void)
131 {
132 	if (total_packets % 10 == 0) {
133 		fprintf(stderr, ".");
134 		fflush(stderr);
135 	}
136 }
137 
138 static void test_payload(void *pay, size_t len)
139 {
140 	struct ethhdr *eth = pay;
141 
142 	if (len < sizeof(struct ethhdr)) {
143 		fprintf(stderr, "test_payload: packet too "
144 			"small: %zu bytes!\n", len);
145 		exit(1);
146 	}
147 
148 	if (eth->h_proto != htons(ETH_P_IP)) {
149 		fprintf(stderr, "test_payload: wrong ethernet "
150 			"type: 0x%x!\n", ntohs(eth->h_proto));
151 		exit(1);
152 	}
153 }
154 
155 static void create_payload(void *pay, size_t *len)
156 {
157 	int i;
158 	struct ethhdr *eth = pay;
159 	struct iphdr *ip = pay + sizeof(*eth);
160 
161 	/* Lets create some broken crap, that still passes
162 	 * our BPF filter.
163 	 */
164 
165 	*len = DATA_LEN + 42;
166 
167 	memset(pay, 0xff, ETH_ALEN * 2);
168 	eth->h_proto = htons(ETH_P_IP);
169 
170 	for (i = 0; i < sizeof(*ip); ++i)
171 		((uint8_t *) pay)[i + sizeof(*eth)] = (uint8_t) rand();
172 
173 	ip->ihl = 5;
174 	ip->version = 4;
175 	ip->protocol = 0x11;
176 	ip->frag_off = 0;
177 	ip->ttl = 64;
178 	ip->tot_len = htons((uint16_t) *len - sizeof(*eth));
179 
180 	ip->saddr = htonl(INADDR_LOOPBACK);
181 	ip->daddr = htonl(INADDR_LOOPBACK);
182 
183 	memset(pay + sizeof(*eth) + sizeof(*ip),
184 	       DATA_CHAR, DATA_LEN);
185 }
186 
187 static inline int __v1_rx_kernel_ready(struct tpacket_hdr *hdr)
188 {
189 	return ((hdr->tp_status & TP_STATUS_USER) == TP_STATUS_USER);
190 }
191 
192 static inline void __v1_rx_user_ready(struct tpacket_hdr *hdr)
193 {
194 	hdr->tp_status = TP_STATUS_KERNEL;
195 	__sync_synchronize();
196 }
197 
198 static inline int __v2_rx_kernel_ready(struct tpacket2_hdr *hdr)
199 {
200 	return ((hdr->tp_status & TP_STATUS_USER) == TP_STATUS_USER);
201 }
202 
203 static inline void __v2_rx_user_ready(struct tpacket2_hdr *hdr)
204 {
205 	hdr->tp_status = TP_STATUS_KERNEL;
206 	__sync_synchronize();
207 }
208 
209 static inline int __v1_v2_rx_kernel_ready(void *base, int version)
210 {
211 	switch (version) {
212 	case TPACKET_V1:
213 		return __v1_rx_kernel_ready(base);
214 	case TPACKET_V2:
215 		return __v2_rx_kernel_ready(base);
216 	default:
217 		bug_on(1);
218 		return 0;
219 	}
220 }
221 
222 static inline void __v1_v2_rx_user_ready(void *base, int version)
223 {
224 	switch (version) {
225 	case TPACKET_V1:
226 		__v1_rx_user_ready(base);
227 		break;
228 	case TPACKET_V2:
229 		__v2_rx_user_ready(base);
230 		break;
231 	}
232 }
233 
234 static void walk_v1_v2_rx(int sock, struct ring *ring)
235 {
236 	struct pollfd pfd;
237 	int udp_sock[2];
238 	union frame_map ppd;
239 	unsigned int frame_num = 0;
240 
241 	bug_on(ring->type != PACKET_RX_RING);
242 
243 	pair_udp_open(udp_sock, PORT_BASE);
244 
245 	memset(&pfd, 0, sizeof(pfd));
246 	pfd.fd = sock;
247 	pfd.events = POLLIN | POLLERR;
248 	pfd.revents = 0;
249 
250 	pair_udp_send(udp_sock, NUM_PACKETS);
251 
252 	while (total_packets < NUM_PACKETS * 2) {
253 		while (__v1_v2_rx_kernel_ready(ring->rd[frame_num].iov_base,
254 					       ring->version)) {
255 			ppd.raw = ring->rd[frame_num].iov_base;
256 
257 			switch (ring->version) {
258 			case TPACKET_V1:
259 				test_payload((uint8_t *) ppd.raw + ppd.v1->tp_h.tp_mac,
260 					     ppd.v1->tp_h.tp_snaplen);
261 				total_bytes += ppd.v1->tp_h.tp_snaplen;
262 				break;
263 
264 			case TPACKET_V2:
265 				test_payload((uint8_t *) ppd.raw + ppd.v2->tp_h.tp_mac,
266 					     ppd.v2->tp_h.tp_snaplen);
267 				total_bytes += ppd.v2->tp_h.tp_snaplen;
268 				break;
269 			}
270 
271 			status_bar_update();
272 			total_packets++;
273 
274 			__v1_v2_rx_user_ready(ppd.raw, ring->version);
275 
276 			frame_num = (frame_num + 1) % ring->rd_num;
277 		}
278 
279 		poll(&pfd, 1, 1);
280 	}
281 
282 	pair_udp_close(udp_sock);
283 
284 	if (total_packets != 2 * NUM_PACKETS) {
285 		fprintf(stderr, "walk_v%d_rx: received %u out of %u pkts\n",
286 			ring->version, total_packets, NUM_PACKETS);
287 		exit(1);
288 	}
289 
290 	fprintf(stderr, " %u pkts (%u bytes)", NUM_PACKETS, total_bytes >> 1);
291 }
292 
293 static inline int __v1_tx_kernel_ready(struct tpacket_hdr *hdr)
294 {
295 	return !(hdr->tp_status & (TP_STATUS_SEND_REQUEST | TP_STATUS_SENDING));
296 }
297 
298 static inline void __v1_tx_user_ready(struct tpacket_hdr *hdr)
299 {
300 	hdr->tp_status = TP_STATUS_SEND_REQUEST;
301 	__sync_synchronize();
302 }
303 
304 static inline int __v2_tx_kernel_ready(struct tpacket2_hdr *hdr)
305 {
306 	return !(hdr->tp_status & (TP_STATUS_SEND_REQUEST | TP_STATUS_SENDING));
307 }
308 
309 static inline void __v2_tx_user_ready(struct tpacket2_hdr *hdr)
310 {
311 	hdr->tp_status = TP_STATUS_SEND_REQUEST;
312 	__sync_synchronize();
313 }
314 
315 static inline int __v3_tx_kernel_ready(struct tpacket3_hdr *hdr)
316 {
317 	return !(hdr->tp_status & (TP_STATUS_SEND_REQUEST | TP_STATUS_SENDING));
318 }
319 
320 static inline void __v3_tx_user_ready(struct tpacket3_hdr *hdr)
321 {
322 	hdr->tp_status = TP_STATUS_SEND_REQUEST;
323 	__sync_synchronize();
324 }
325 
326 static inline int __tx_kernel_ready(void *base, int version)
327 {
328 	switch (version) {
329 	case TPACKET_V1:
330 		return __v1_tx_kernel_ready(base);
331 	case TPACKET_V2:
332 		return __v2_tx_kernel_ready(base);
333 	case TPACKET_V3:
334 		return __v3_tx_kernel_ready(base);
335 	default:
336 		bug_on(1);
337 		return 0;
338 	}
339 }
340 
341 static inline void __tx_user_ready(void *base, int version)
342 {
343 	switch (version) {
344 	case TPACKET_V1:
345 		__v1_tx_user_ready(base);
346 		break;
347 	case TPACKET_V2:
348 		__v2_tx_user_ready(base);
349 		break;
350 	case TPACKET_V3:
351 		__v3_tx_user_ready(base);
352 		break;
353 	}
354 }
355 
356 static void __v1_v2_set_packet_loss_discard(int sock)
357 {
358 	int ret, discard = 1;
359 
360 	ret = setsockopt(sock, SOL_PACKET, PACKET_LOSS, (void *) &discard,
361 			 sizeof(discard));
362 	if (ret == -1) {
363 		perror("setsockopt");
364 		exit(1);
365 	}
366 }
367 
368 static inline void *get_next_frame(struct ring *ring, int n)
369 {
370 	uint8_t *f0 = ring->rd[0].iov_base;
371 
372 	switch (ring->version) {
373 	case TPACKET_V1:
374 	case TPACKET_V2:
375 		return ring->rd[n].iov_base;
376 	case TPACKET_V3:
377 		return f0 + (n * ring->req3.tp_frame_size);
378 	default:
379 		bug_on(1);
380 	}
381 }
382 
383 static void walk_tx(int sock, struct ring *ring)
384 {
385 	struct pollfd pfd;
386 	int rcv_sock, ret;
387 	size_t packet_len;
388 	union frame_map ppd;
389 	char packet[1024];
390 	unsigned int frame_num = 0, got = 0;
391 	struct sockaddr_ll ll = {
392 		.sll_family = PF_PACKET,
393 		.sll_halen = ETH_ALEN,
394 	};
395 	int nframes;
396 
397 	/* TPACKET_V{1,2} sets up the ring->rd* related variables based
398 	 * on frames (e.g., rd_num is tp_frame_nr) whereas V3 sets these
399 	 * up based on blocks (e.g, rd_num is  tp_block_nr)
400 	 */
401 	if (ring->version <= TPACKET_V2)
402 		nframes = ring->rd_num;
403 	else
404 		nframes = ring->req3.tp_frame_nr;
405 
406 	bug_on(ring->type != PACKET_TX_RING);
407 	bug_on(nframes < NUM_PACKETS);
408 
409 	rcv_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
410 	if (rcv_sock == -1) {
411 		perror("socket");
412 		exit(1);
413 	}
414 
415 	pair_udp_setfilter(rcv_sock);
416 
417 	ll.sll_ifindex = if_nametoindex("lo");
418 	ret = bind(rcv_sock, (struct sockaddr *) &ll, sizeof(ll));
419 	if (ret == -1) {
420 		perror("bind");
421 		exit(1);
422 	}
423 
424 	memset(&pfd, 0, sizeof(pfd));
425 	pfd.fd = sock;
426 	pfd.events = POLLOUT | POLLERR;
427 	pfd.revents = 0;
428 
429 	total_packets = NUM_PACKETS;
430 	create_payload(packet, &packet_len);
431 
432 	while (total_packets > 0) {
433 		void *next = get_next_frame(ring, frame_num);
434 
435 		while (__tx_kernel_ready(next, ring->version) &&
436 		       total_packets > 0) {
437 			ppd.raw = next;
438 
439 			switch (ring->version) {
440 			case TPACKET_V1:
441 				ppd.v1->tp_h.tp_snaplen = packet_len;
442 				ppd.v1->tp_h.tp_len = packet_len;
443 
444 				memcpy((uint8_t *) ppd.raw + TPACKET_HDRLEN -
445 				       sizeof(struct sockaddr_ll), packet,
446 				       packet_len);
447 				total_bytes += ppd.v1->tp_h.tp_snaplen;
448 				break;
449 
450 			case TPACKET_V2:
451 				ppd.v2->tp_h.tp_snaplen = packet_len;
452 				ppd.v2->tp_h.tp_len = packet_len;
453 
454 				memcpy((uint8_t *) ppd.raw + TPACKET2_HDRLEN -
455 				       sizeof(struct sockaddr_ll), packet,
456 				       packet_len);
457 				total_bytes += ppd.v2->tp_h.tp_snaplen;
458 				break;
459 			case TPACKET_V3: {
460 				struct tpacket3_hdr *tx = next;
461 
462 				tx->tp_snaplen = packet_len;
463 				tx->tp_len = packet_len;
464 				tx->tp_next_offset = 0;
465 
466 				memcpy((uint8_t *)tx + TPACKET3_HDRLEN -
467 				       sizeof(struct sockaddr_ll), packet,
468 				       packet_len);
469 				total_bytes += tx->tp_snaplen;
470 				break;
471 			}
472 			}
473 
474 			status_bar_update();
475 			total_packets--;
476 
477 			__tx_user_ready(next, ring->version);
478 
479 			frame_num = (frame_num + 1) % nframes;
480 		}
481 
482 		poll(&pfd, 1, 1);
483 	}
484 
485 	bug_on(total_packets != 0);
486 
487 	ret = sendto(sock, NULL, 0, 0, NULL, 0);
488 	if (ret == -1) {
489 		perror("sendto");
490 		exit(1);
491 	}
492 
493 	while ((ret = recvfrom(rcv_sock, packet, sizeof(packet),
494 			       0, NULL, NULL)) > 0 &&
495 	       total_packets < NUM_PACKETS) {
496 		got += ret;
497 		test_payload(packet, ret);
498 
499 		status_bar_update();
500 		total_packets++;
501 	}
502 
503 	close(rcv_sock);
504 
505 	if (total_packets != NUM_PACKETS) {
506 		fprintf(stderr, "walk_v%d_rx: received %u out of %u pkts\n",
507 			ring->version, total_packets, NUM_PACKETS);
508 		exit(1);
509 	}
510 
511 	fprintf(stderr, " %u pkts (%u bytes)", NUM_PACKETS, got);
512 }
513 
514 static void walk_v1_v2(int sock, struct ring *ring)
515 {
516 	if (ring->type == PACKET_RX_RING)
517 		walk_v1_v2_rx(sock, ring);
518 	else
519 		walk_tx(sock, ring);
520 }
521 
522 static uint64_t __v3_prev_block_seq_num = 0;
523 
524 void __v3_test_block_seq_num(struct block_desc *pbd)
525 {
526 	if (__v3_prev_block_seq_num + 1 != pbd->h1.seq_num) {
527 		fprintf(stderr, "\nprev_block_seq_num:%"PRIu64", expected "
528 			"seq:%"PRIu64" != actual seq:%"PRIu64"\n",
529 			__v3_prev_block_seq_num, __v3_prev_block_seq_num + 1,
530 			(uint64_t) pbd->h1.seq_num);
531 		exit(1);
532 	}
533 
534 	__v3_prev_block_seq_num = pbd->h1.seq_num;
535 }
536 
537 static void __v3_test_block_len(struct block_desc *pbd, uint32_t bytes, int block_num)
538 {
539 	if (pbd->h1.num_pkts && bytes != pbd->h1.blk_len) {
540 		fprintf(stderr, "\nblock:%u with %upackets, expected "
541 			"len:%u != actual len:%u\n", block_num,
542 			pbd->h1.num_pkts, bytes, pbd->h1.blk_len);
543 		exit(1);
544 	}
545 }
546 
547 static void __v3_test_block_header(struct block_desc *pbd, const int block_num)
548 {
549 	if ((pbd->h1.block_status & TP_STATUS_USER) == 0) {
550 		fprintf(stderr, "\nblock %u: not in TP_STATUS_USER\n", block_num);
551 		exit(1);
552 	}
553 
554 	__v3_test_block_seq_num(pbd);
555 }
556 
557 static void __v3_walk_block(struct block_desc *pbd, const int block_num)
558 {
559 	int num_pkts = pbd->h1.num_pkts, i;
560 	unsigned long bytes = 0, bytes_with_padding = ALIGN_8(sizeof(*pbd));
561 	struct tpacket3_hdr *ppd;
562 
563 	__v3_test_block_header(pbd, block_num);
564 
565 	ppd = (struct tpacket3_hdr *) ((uint8_t *) pbd +
566 				       pbd->h1.offset_to_first_pkt);
567 
568 	for (i = 0; i < num_pkts; ++i) {
569 		bytes += ppd->tp_snaplen;
570 
571 		if (ppd->tp_next_offset)
572 			bytes_with_padding += ppd->tp_next_offset;
573 		else
574 			bytes_with_padding += ALIGN_8(ppd->tp_snaplen + ppd->tp_mac);
575 
576 		test_payload((uint8_t *) ppd + ppd->tp_mac, ppd->tp_snaplen);
577 
578 		status_bar_update();
579 		total_packets++;
580 
581 		ppd = (struct tpacket3_hdr *) ((uint8_t *) ppd + ppd->tp_next_offset);
582 		__sync_synchronize();
583 	}
584 
585 	__v3_test_block_len(pbd, bytes_with_padding, block_num);
586 	total_bytes += bytes;
587 }
588 
589 void __v3_flush_block(struct block_desc *pbd)
590 {
591 	pbd->h1.block_status = TP_STATUS_KERNEL;
592 	__sync_synchronize();
593 }
594 
595 static void walk_v3_rx(int sock, struct ring *ring)
596 {
597 	unsigned int block_num = 0;
598 	struct pollfd pfd;
599 	struct block_desc *pbd;
600 	int udp_sock[2];
601 
602 	bug_on(ring->type != PACKET_RX_RING);
603 
604 	pair_udp_open(udp_sock, PORT_BASE);
605 
606 	memset(&pfd, 0, sizeof(pfd));
607 	pfd.fd = sock;
608 	pfd.events = POLLIN | POLLERR;
609 	pfd.revents = 0;
610 
611 	pair_udp_send(udp_sock, NUM_PACKETS);
612 
613 	while (total_packets < NUM_PACKETS * 2) {
614 		pbd = (struct block_desc *) ring->rd[block_num].iov_base;
615 
616 		while ((pbd->h1.block_status & TP_STATUS_USER) == 0)
617 			poll(&pfd, 1, 1);
618 
619 		__v3_walk_block(pbd, block_num);
620 		__v3_flush_block(pbd);
621 
622 		block_num = (block_num + 1) % ring->rd_num;
623 	}
624 
625 	pair_udp_close(udp_sock);
626 
627 	if (total_packets != 2 * NUM_PACKETS) {
628 		fprintf(stderr, "walk_v3_rx: received %u out of %u pkts\n",
629 			total_packets, NUM_PACKETS);
630 		exit(1);
631 	}
632 
633 	fprintf(stderr, " %u pkts (%u bytes)", NUM_PACKETS, total_bytes >> 1);
634 }
635 
636 static void walk_v3(int sock, struct ring *ring)
637 {
638 	if (ring->type == PACKET_RX_RING)
639 		walk_v3_rx(sock, ring);
640 	else
641 		walk_tx(sock, ring);
642 }
643 
644 static void __v1_v2_fill(struct ring *ring, unsigned int blocks)
645 {
646 	ring->req.tp_block_size = getpagesize() << 2;
647 	ring->req.tp_frame_size = TPACKET_ALIGNMENT << 7;
648 	ring->req.tp_block_nr = blocks;
649 
650 	ring->req.tp_frame_nr = ring->req.tp_block_size /
651 				ring->req.tp_frame_size *
652 				ring->req.tp_block_nr;
653 
654 	ring->mm_len = ring->req.tp_block_size * ring->req.tp_block_nr;
655 	ring->walk = walk_v1_v2;
656 	ring->rd_num = ring->req.tp_frame_nr;
657 	ring->flen = ring->req.tp_frame_size;
658 }
659 
660 static void __v3_fill(struct ring *ring, unsigned int blocks, int type)
661 {
662 	if (type == PACKET_RX_RING) {
663 		ring->req3.tp_retire_blk_tov = 64;
664 		ring->req3.tp_sizeof_priv = 0;
665 		ring->req3.tp_feature_req_word = TP_FT_REQ_FILL_RXHASH;
666 	}
667 	ring->req3.tp_block_size = getpagesize() << 2;
668 	ring->req3.tp_frame_size = TPACKET_ALIGNMENT << 7;
669 	ring->req3.tp_block_nr = blocks;
670 
671 	ring->req3.tp_frame_nr = ring->req3.tp_block_size /
672 				 ring->req3.tp_frame_size *
673 				 ring->req3.tp_block_nr;
674 
675 	ring->mm_len = ring->req3.tp_block_size * ring->req3.tp_block_nr;
676 	ring->walk = walk_v3;
677 	ring->rd_num = ring->req3.tp_block_nr;
678 	ring->flen = ring->req3.tp_block_size;
679 }
680 
681 static void setup_ring(int sock, struct ring *ring, int version, int type)
682 {
683 	int ret = 0;
684 	unsigned int blocks = 256;
685 
686 	ring->type = type;
687 	ring->version = version;
688 
689 	switch (version) {
690 	case TPACKET_V1:
691 	case TPACKET_V2:
692 		if (type == PACKET_TX_RING)
693 			__v1_v2_set_packet_loss_discard(sock);
694 		__v1_v2_fill(ring, blocks);
695 		ret = setsockopt(sock, SOL_PACKET, type, &ring->req,
696 				 sizeof(ring->req));
697 		break;
698 
699 	case TPACKET_V3:
700 		__v3_fill(ring, blocks, type);
701 		ret = setsockopt(sock, SOL_PACKET, type, &ring->req3,
702 				 sizeof(ring->req3));
703 		break;
704 	}
705 
706 	if (ret == -1) {
707 		perror("setsockopt");
708 		exit(1);
709 	}
710 
711 	ring->rd_len = ring->rd_num * sizeof(*ring->rd);
712 	ring->rd = malloc(ring->rd_len);
713 	if (ring->rd == NULL) {
714 		perror("malloc");
715 		exit(1);
716 	}
717 
718 	total_packets = 0;
719 	total_bytes = 0;
720 }
721 
722 static void mmap_ring(int sock, struct ring *ring)
723 {
724 	int i;
725 
726 	ring->mm_space = mmap(0, ring->mm_len, PROT_READ | PROT_WRITE,
727 			      MAP_SHARED | MAP_LOCKED | MAP_POPULATE, sock, 0);
728 	if (ring->mm_space == MAP_FAILED) {
729 		perror("mmap");
730 		exit(1);
731 	}
732 
733 	memset(ring->rd, 0, ring->rd_len);
734 	for (i = 0; i < ring->rd_num; ++i) {
735 		ring->rd[i].iov_base = ring->mm_space + (i * ring->flen);
736 		ring->rd[i].iov_len = ring->flen;
737 	}
738 }
739 
740 static void bind_ring(int sock, struct ring *ring)
741 {
742 	int ret;
743 
744 	pair_udp_setfilter(sock);
745 
746 	ring->ll.sll_family = PF_PACKET;
747 	ring->ll.sll_protocol = htons(ETH_P_ALL);
748 	ring->ll.sll_ifindex = if_nametoindex("lo");
749 	ring->ll.sll_hatype = 0;
750 	ring->ll.sll_pkttype = 0;
751 	ring->ll.sll_halen = 0;
752 
753 	ret = bind(sock, (struct sockaddr *) &ring->ll, sizeof(ring->ll));
754 	if (ret == -1) {
755 		perror("bind");
756 		exit(1);
757 	}
758 }
759 
760 static void walk_ring(int sock, struct ring *ring)
761 {
762 	ring->walk(sock, ring);
763 }
764 
765 static void unmap_ring(int sock, struct ring *ring)
766 {
767 	munmap(ring->mm_space, ring->mm_len);
768 	free(ring->rd);
769 }
770 
771 static int test_kernel_bit_width(void)
772 {
773 	char in[512], *ptr;
774 	int num = 0, fd;
775 	ssize_t ret;
776 
777 	fd = open("/proc/kallsyms", O_RDONLY);
778 	if (fd == -1) {
779 		perror("open");
780 		exit(1);
781 	}
782 
783 	ret = read(fd, in, sizeof(in));
784 	if (ret <= 0) {
785 		perror("read");
786 		exit(1);
787 	}
788 
789 	close(fd);
790 
791 	ptr = in;
792 	while(!isspace(*ptr)) {
793 		num++;
794 		ptr++;
795 	}
796 
797 	return num * 4;
798 }
799 
800 static int test_user_bit_width(void)
801 {
802 	return __WORDSIZE;
803 }
804 
805 static const char *tpacket_str[] = {
806 	[TPACKET_V1] = "TPACKET_V1",
807 	[TPACKET_V2] = "TPACKET_V2",
808 	[TPACKET_V3] = "TPACKET_V3",
809 };
810 
811 static const char *type_str[] = {
812 	[PACKET_RX_RING] = "PACKET_RX_RING",
813 	[PACKET_TX_RING] = "PACKET_TX_RING",
814 };
815 
816 static int test_tpacket(int version, int type)
817 {
818 	int sock;
819 	struct ring ring;
820 
821 	fprintf(stderr, "test: %s with %s ", tpacket_str[version],
822 		type_str[type]);
823 	fflush(stderr);
824 
825 	if (version == TPACKET_V1 &&
826 	    test_kernel_bit_width() != test_user_bit_width()) {
827 		fprintf(stderr, "test: skip %s %s since user and kernel "
828 			"space have different bit width\n",
829 			tpacket_str[version], type_str[type]);
830 		return KSFT_SKIP;
831 	}
832 
833 	sock = pfsocket(version);
834 	memset(&ring, 0, sizeof(ring));
835 	setup_ring(sock, &ring, version, type);
836 	mmap_ring(sock, &ring);
837 	bind_ring(sock, &ring);
838 	walk_ring(sock, &ring);
839 	unmap_ring(sock, &ring);
840 	close(sock);
841 
842 	fprintf(stderr, "\n");
843 	return 0;
844 }
845 
846 int main(void)
847 {
848 	int ret = 0;
849 
850 	ret |= test_tpacket(TPACKET_V1, PACKET_RX_RING);
851 	ret |= test_tpacket(TPACKET_V1, PACKET_TX_RING);
852 
853 	ret |= test_tpacket(TPACKET_V2, PACKET_RX_RING);
854 	ret |= test_tpacket(TPACKET_V2, PACKET_TX_RING);
855 
856 	ret |= test_tpacket(TPACKET_V3, PACKET_RX_RING);
857 	ret |= test_tpacket(TPACKET_V3, PACKET_TX_RING);
858 
859 	if (ret)
860 		return 1;
861 
862 	printf("OK. All tests passed\n");
863 	return 0;
864 }
865