xmalloc: put __hidden behind signature
[netsniff-ng.git] / ring_tx.c
blob8b15779e8cc104f250c17f75a1c295f07d6ee9d1
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2009, 2010 Daniel Borkmann.
5 * Copyright 2009, 2010 Emmanuel Roullit.
6 * Subject to the GPL, version 2.
7 */
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <sys/mman.h>
14 #include <sys/types.h>
15 #include <sys/socket.h>
16 #include <arpa/inet.h>
17 #include <linux/if_ether.h>
19 #include "die.h"
20 #include "xmalloc.h"
21 #include "ring_tx.h"
22 #include "built_in.h"
24 void set_packet_loss_discard(int sock)
26 int ret, discard = 1;
27 ret = setsockopt(sock, SOL_PACKET, PACKET_LOSS, (void *) &discard,
28 sizeof(discard));
29 if (ret < 0)
30 panic("setsockopt: cannot set packet loss");
33 void destroy_tx_ring(int sock, struct ring *ring)
35 fmemset(&ring->layout, 0, sizeof(ring->layout));
36 setsockopt(sock, SOL_PACKET, PACKET_TX_RING, &ring->layout,
37 sizeof(ring->layout));
39 munmap(ring->mm_space, ring->mm_len);
40 ring->mm_len = 0;
42 xfree(ring->frames);
45 void setup_tx_ring_layout(int sock, struct ring *ring, unsigned int size,
46 int jumbo_support)
48 fmemset(&ring->layout, 0, sizeof(ring->layout));
50 ring->layout.tp_block_size = (jumbo_support ?
51 getpagesize() << 4 :
52 getpagesize() << 2);
53 ring->layout.tp_frame_size = (jumbo_support ?
54 TPACKET_ALIGNMENT << 12 :
55 TPACKET_ALIGNMENT << 7);
56 ring->layout.tp_block_nr = size / ring->layout.tp_block_size;
57 ring->layout.tp_frame_nr = ring->layout.tp_block_size /
58 ring->layout.tp_frame_size *
59 ring->layout.tp_block_nr;
61 bug_on(ring->layout.tp_block_size < ring->layout.tp_frame_size);
62 bug_on((ring->layout.tp_block_size % ring->layout.tp_frame_size) != 0);
63 bug_on((ring->layout.tp_block_size % getpagesize()) != 0);
66 void create_tx_ring(int sock, struct ring *ring, int verbose)
68 int ret;
70 set_sockopt_tpacket(sock);
71 retry:
72 ret = setsockopt(sock, SOL_PACKET, PACKET_TX_RING, &ring->layout,
73 sizeof(ring->layout));
74 if (errno == ENOMEM && ring->layout.tp_block_nr > 1) {
75 ring->layout.tp_block_nr >>= 1;
76 ring->layout.tp_frame_nr = ring->layout.tp_block_size /
77 ring->layout.tp_frame_size *
78 ring->layout.tp_block_nr;
79 goto retry;
82 if (ret < 0)
83 panic("Cannot allocate TX_RING!\n");
85 ring->mm_len = ring->layout.tp_block_size * ring->layout.tp_block_nr;
87 if (verbose) {
88 printf("TX: %.2Lf MiB, %u Frames, each %u Byte allocated\n",
89 (long double) ring->mm_len / (1 << 20),
90 ring->layout.tp_frame_nr, ring->layout.tp_frame_size);
94 void mmap_tx_ring(int sock, struct ring *ring)
96 ring->mm_space = mmap(0, ring->mm_len, PROT_READ | PROT_WRITE,
97 MAP_SHARED | MAP_LOCKED | MAP_POPULATE, sock, 0);
98 if (ring->mm_space == MAP_FAILED) {
99 destroy_tx_ring(sock, ring);
100 panic("Cannot mmap TX_RING!\n");
104 void alloc_tx_ring_frames(struct ring *ring)
106 int i;
107 size_t len = ring->layout.tp_frame_nr * sizeof(*ring->frames);
109 ring->frames = xmalloc_aligned(len, CO_CACHE_LINE_SIZE);
110 fmemset(ring->frames, 0, len);
112 for (i = 0; i < ring->layout.tp_frame_nr; ++i) {
113 ring->frames[i].iov_len = ring->layout.tp_frame_size;
114 ring->frames[i].iov_base = ring->mm_space +
115 (i * ring->layout.tp_frame_size);
119 void bind_tx_ring(int sock, struct ring *ring, int ifindex)
121 int ret;
123 fmemset(&ring->s_ll, 0, sizeof(ring->s_ll));
125 ring->s_ll.sll_family = AF_PACKET;
126 ring->s_ll.sll_protocol = htons(ETH_P_ALL);
127 ring->s_ll.sll_ifindex = ifindex;
128 ring->s_ll.sll_hatype = 0;
129 ring->s_ll.sll_halen = 0;
130 ring->s_ll.sll_pkttype = 0;
132 ret = bind(sock, (struct sockaddr *) &ring->s_ll, sizeof(ring->s_ll));
133 if (ret < 0) {
134 destroy_tx_ring(sock, ring);
135 panic("Cannot bind TX_RING!\n");