Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

tools: selftests: psock_tpacket: get rid of macro wrappers

The TPACKET_V3 test code consists of a lot of unecessary macro
wrappers that rather obfuscate what members are accessed in what
way. So get rid of them and make the code more readable. Also
credit Chetan for providing tpacket_v3 example code. Furthermore,
get rid of private offset usage, as we do not need it here.

Signed-off-by: Daniel Borkmann <dborkman@redhat.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Daniel Borkmann and committed by
David S. Miller
37beae65 c590b5e2

+20 -39
+20 -39
tools/testing/selftests/net/psock_tpacket.c
··· 1 1 /* 2 2 * Copyright 2013 Red Hat, Inc. 3 3 * Author: Daniel Borkmann <dborkman@redhat.com> 4 + * Chetan Loke <loke.chetan@gmail.com> (TPACKET_V3 usage example) 4 5 * 5 6 * A basic test of packet socket's TPACKET_V1/TPACKET_V2/TPACKET_V3 behavior. 6 7 * ··· 72 71 # define __align_tpacket(x) __attribute__((aligned(TPACKET_ALIGN(x)))) 73 72 #endif 74 73 75 - #define BLOCK_STATUS(x) ((x)->h1.block_status) 76 - #define BLOCK_NUM_PKTS(x) ((x)->h1.num_pkts) 77 - #define BLOCK_O2FP(x) ((x)->h1.offset_to_first_pkt) 78 - #define BLOCK_LEN(x) ((x)->h1.blk_len) 79 - #define BLOCK_SNUM(x) ((x)->h1.seq_num) 80 - #define BLOCK_O2PRIV(x) ((x)->offset_to_priv) 81 - #define BLOCK_PRIV(x) ((void *) ((uint8_t *) (x) + BLOCK_O2PRIV(x))) 82 - #define BLOCK_HDR_LEN (ALIGN_8(sizeof(struct block_desc))) 83 - #define ALIGN_8(x) (((x) + 8 - 1) & ~(8 - 1)) 84 - #define BLOCK_PLUS_PRIV(sz_pri) (BLOCK_HDR_LEN + ALIGN_8((sz_pri))) 85 - 86 74 #define NUM_PACKETS 100 75 + #define ALIGN_8(x) (((x) + 8 - 1) & ~(8 - 1)) 87 76 88 77 struct ring { 89 78 struct iovec *rd; ··· 467 476 468 477 void __v3_test_block_seq_num(struct block_desc *pbd) 469 478 { 470 - if (__v3_prev_block_seq_num + 1 != BLOCK_SNUM(pbd)) { 479 + if (__v3_prev_block_seq_num + 1 != pbd->h1.seq_num) { 471 480 fprintf(stderr, "\nprev_block_seq_num:%"PRIu64", expected " 472 481 "seq:%"PRIu64" != actual seq:%"PRIu64"\n", 473 482 __v3_prev_block_seq_num, __v3_prev_block_seq_num + 1, 474 - (uint64_t) BLOCK_SNUM(pbd)); 483 + (uint64_t) pbd->h1.seq_num); 475 484 exit(1); 476 485 } 477 486 478 - __v3_prev_block_seq_num = BLOCK_SNUM(pbd); 487 + __v3_prev_block_seq_num = pbd->h1.seq_num; 479 488 } 480 489 481 490 static void __v3_test_block_len(struct block_desc *pbd, uint32_t bytes, int block_num) 482 491 { 483 - if (BLOCK_NUM_PKTS(pbd)) { 484 - if (bytes != BLOCK_LEN(pbd)) { 485 - fprintf(stderr, "\nblock:%u with %upackets, expected " 486 - "len:%u != actual len:%u\n", block_num, 487 - BLOCK_NUM_PKTS(pbd), bytes, BLOCK_LEN(pbd)); 488 - exit(1); 489 - } 490 - } else { 491 - if (BLOCK_LEN(pbd) != BLOCK_PLUS_PRIV(13)) { 492 - fprintf(stderr, "\nblock:%u, expected len:%lu != " 493 - "actual len:%u\n", block_num, BLOCK_HDR_LEN, 494 - BLOCK_LEN(pbd)); 495 - exit(1); 496 - } 492 + if (pbd->h1.num_pkts && bytes != pbd->h1.blk_len) { 493 + fprintf(stderr, "\nblock:%u with %upackets, expected " 494 + "len:%u != actual len:%u\n", block_num, 495 + pbd->h1.num_pkts, bytes, pbd->h1.blk_len); 496 + exit(1); 497 497 } 498 498 } 499 499 500 500 static void __v3_test_block_header(struct block_desc *pbd, const int block_num) 501 501 { 502 - uint32_t block_status = BLOCK_STATUS(pbd); 503 - 504 - if ((block_status & TP_STATUS_USER) == 0) { 502 + if ((pbd->h1.block_status & TP_STATUS_USER) == 0) { 505 503 fprintf(stderr, "\nblock %u: not in TP_STATUS_USER\n", block_num); 506 504 exit(1); 507 505 } ··· 500 520 501 521 static void __v3_walk_block(struct block_desc *pbd, const int block_num) 502 522 { 503 - int num_pkts = BLOCK_NUM_PKTS(pbd), i; 504 - unsigned long bytes = 0; 505 - unsigned long bytes_with_padding = BLOCK_PLUS_PRIV(13); 523 + int num_pkts = pbd->h1.num_pkts, i; 524 + unsigned long bytes = 0, bytes_with_padding = ALIGN_8(sizeof(*pbd)); 506 525 struct tpacket3_hdr *ppd; 507 526 508 527 __v3_test_block_header(pbd, block_num); 509 528 510 - ppd = (struct tpacket3_hdr *) ((uint8_t *) pbd + BLOCK_O2FP(pbd)); 529 + ppd = (struct tpacket3_hdr *) ((uint8_t *) pbd + 530 + pbd->h1.offset_to_first_pkt); 531 + 511 532 for (i = 0; i < num_pkts; ++i) { 512 533 bytes += ppd->tp_snaplen; 513 534 ··· 532 551 533 552 void __v3_flush_block(struct block_desc *pbd) 534 553 { 535 - BLOCK_STATUS(pbd) = TP_STATUS_KERNEL; 554 + pbd->h1.block_status = TP_STATUS_KERNEL; 536 555 __sync_synchronize(); 537 556 } 538 557 ··· 558 577 while (total_packets < NUM_PACKETS * 2) { 559 578 pbd = (struct block_desc *) ring->rd[block_num].iov_base; 560 579 561 - while ((BLOCK_STATUS(pbd) & TP_STATUS_USER) == 0) 580 + while ((pbd->h1.block_status & TP_STATUS_USER) == 0) 562 581 poll(&pfd, 1, 1); 563 582 564 583 __v3_walk_block(pbd, block_num); ··· 605 624 static void __v3_fill(struct ring *ring, unsigned int blocks) 606 625 { 607 626 ring->req3.tp_retire_blk_tov = 64; 608 - ring->req3.tp_sizeof_priv = 13; 609 - ring->req3.tp_feature_req_word |= TP_FT_REQ_FILL_RXHASH; 627 + ring->req3.tp_sizeof_priv = 0; 628 + ring->req3.tp_feature_req_word = TP_FT_REQ_FILL_RXHASH; 610 629 611 630 ring->req3.tp_block_size = getpagesize() << 2; 612 631 ring->req3.tp_frame_size = TPACKET_ALIGNMENT << 7;