Skip to content

Commit 184f489

Browse files
Daniel Borkmanndavem330
authored andcommitted
packet: minor: add generic tpacket_uhdr to access packet headers
There is no need to add a dozen unions each time at the start of the function. So, do this once and use it instead. Thus, we can remove some duplicate code and make it more readable. Signed-off-by: Daniel Borkmann <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent f406c8b commit 184f489

File tree

1 file changed

+12
-27
lines changed

1 file changed

+12
-27
lines changed

net/packet/af_packet.c

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,16 @@ struct packet_mreq_max {
158158
unsigned char mr_address[MAX_ADDR_LEN];
159159
};
160160

161+
union tpacket_uhdr {
162+
struct tpacket_hdr *h1;
163+
struct tpacket2_hdr *h2;
164+
struct tpacket3_hdr *h3;
165+
void *raw;
166+
};
167+
161168
static int packet_set_ring(struct sock *sk, union tpacket_req_u *req_u,
162169
int closing, int tx_ring);
163170

164-
165171
#define V3_ALIGNMENT (8)
166172

167173
#define BLK_HDR_LEN (ALIGN(sizeof(struct tpacket_block_desc), V3_ALIGNMENT))
@@ -290,11 +296,7 @@ static inline __pure struct page *pgv_to_page(void *addr)
290296

291297
static void __packet_set_status(struct packet_sock *po, void *frame, int status)
292298
{
293-
union {
294-
struct tpacket_hdr *h1;
295-
struct tpacket2_hdr *h2;
296-
void *raw;
297-
} h;
299+
union tpacket_uhdr h;
298300

299301
h.raw = frame;
300302
switch (po->tp_version) {
@@ -317,11 +319,7 @@ static void __packet_set_status(struct packet_sock *po, void *frame, int status)
317319

318320
static int __packet_get_status(struct packet_sock *po, void *frame)
319321
{
320-
union {
321-
struct tpacket_hdr *h1;
322-
struct tpacket2_hdr *h2;
323-
void *raw;
324-
} h;
322+
union tpacket_uhdr h;
325323

326324
smp_rmb();
327325

@@ -347,11 +345,7 @@ static void *packet_lookup_frame(struct packet_sock *po,
347345
int status)
348346
{
349347
unsigned int pg_vec_pos, frame_offset;
350-
union {
351-
struct tpacket_hdr *h1;
352-
struct tpacket2_hdr *h2;
353-
void *raw;
354-
} h;
348+
union tpacket_uhdr h;
355349

356350
pg_vec_pos = position / rb->frames_per_block;
357351
frame_offset = position % rb->frames_per_block;
@@ -1669,12 +1663,7 @@ static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
16691663
struct sock *sk;
16701664
struct packet_sock *po;
16711665
struct sockaddr_ll *sll;
1672-
union {
1673-
struct tpacket_hdr *h1;
1674-
struct tpacket2_hdr *h2;
1675-
struct tpacket3_hdr *h3;
1676-
void *raw;
1677-
} h;
1666+
union tpacket_uhdr h;
16781667
u8 *skb_head = skb->data;
16791668
int skb_len = skb->len;
16801669
unsigned int snaplen, res;
@@ -1909,11 +1898,7 @@ static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff *skb,
19091898
void *frame, struct net_device *dev, int size_max,
19101899
__be16 proto, unsigned char *addr, int hlen)
19111900
{
1912-
union {
1913-
struct tpacket_hdr *h1;
1914-
struct tpacket2_hdr *h2;
1915-
void *raw;
1916-
} ph;
1901+
union tpacket_uhdr ph;
19171902
int to_write, offset, len, tp_len, nr_frags, len_max;
19181903
struct socket *sock = po->sk.sk_socket;
19191904
struct page *page;

0 commit comments

Comments
 (0)