netsniff-ng: mlock: only lock current and future pages when root
[netsniff-ng.git] / ring_tx.c
blob2ad1de935e8b62eb93d03520843a06da6e58f1fe
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009, 2010 Daniel Borkmann.
4 * Copyright 2009, 2010 Emmanuel Roullit.
5 * Subject to the GPL, version 2.
6 */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
12 #include <sys/mman.h>
13 #include <sys/types.h>
14 #include <sys/socket.h>
15 #include <arpa/inet.h>
16 #include <linux/if_ether.h>
18 #include "die.h"
19 #include "xmalloc.h"
20 #include "ring_tx.h"
21 #include "built_in.h"
23 void set_packet_loss_discard(int sock)
25 int ret, discard = 1;
26 ret = setsockopt(sock, SOL_PACKET, PACKET_LOSS, (void *) &discard,
27 sizeof(discard));
28 if (ret < 0)
29 panic("setsockopt: cannot set packet loss");
32 void destroy_tx_ring(int sock, struct ring *ring)
34 int ret;
36 munmap(ring->mm_space, ring->mm_len);
37 ring->mm_len = 0;
39 fmemset(&ring->layout, 0, sizeof(ring->layout));
40 ret = setsockopt(sock, SOL_PACKET, PACKET_TX_RING, &ring->layout,
41 sizeof(ring->layout));
42 if (unlikely(ret))
43 panic("Cannot destroy the TX_RING: %s!\n", strerror(errno));
45 xfree(ring->frames);
48 void setup_tx_ring_layout(int sock, struct ring *ring, unsigned int size,
49 int jumbo_support)
51 fmemset(&ring->layout, 0, sizeof(ring->layout));
53 ring->layout.tp_block_size = (jumbo_support ?
54 getpagesize() << 4 :
55 getpagesize() << 2);
56 ring->layout.tp_frame_size = (jumbo_support ?
57 TPACKET_ALIGNMENT << 12 :
58 TPACKET_ALIGNMENT << 7);
59 ring->layout.tp_block_nr = size / ring->layout.tp_block_size;
60 ring->layout.tp_frame_nr = ring->layout.tp_block_size /
61 ring->layout.tp_frame_size *
62 ring->layout.tp_block_nr;
64 bug_on(ring->layout.tp_block_size < ring->layout.tp_frame_size);
65 bug_on((ring->layout.tp_block_size % ring->layout.tp_frame_size) != 0);
66 bug_on((ring->layout.tp_block_size % getpagesize()) != 0);
69 void create_tx_ring(int sock, struct ring *ring, int verbose)
71 int ret;
73 set_sockopt_tpacket(sock);
74 retry:
75 ret = setsockopt(sock, SOL_PACKET, PACKET_TX_RING, &ring->layout,
76 sizeof(ring->layout));
77 if (errno == ENOMEM && ring->layout.tp_block_nr > 1) {
78 ring->layout.tp_block_nr >>= 1;
79 ring->layout.tp_frame_nr = ring->layout.tp_block_size /
80 ring->layout.tp_frame_size *
81 ring->layout.tp_block_nr;
82 goto retry;
85 if (ret < 0)
86 panic("Cannot allocate TX_RING!\n");
88 ring->mm_len = ring->layout.tp_block_size * ring->layout.tp_block_nr;
90 if (verbose) {
91 printf("TX: %.2Lf MiB, %u Frames, each %u Byte allocated\n",
92 (long double) ring->mm_len / (1 << 20),
93 ring->layout.tp_frame_nr, ring->layout.tp_frame_size);
97 void mmap_tx_ring(int sock, struct ring *ring)
99 ring->mm_space = mmap(0, ring->mm_len, PROT_READ | PROT_WRITE,
100 MAP_SHARED | MAP_LOCKED | MAP_POPULATE, sock, 0);
101 if (ring->mm_space == MAP_FAILED) {
102 destroy_tx_ring(sock, ring);
103 panic("Cannot mmap TX_RING!\n");
107 void alloc_tx_ring_frames(struct ring *ring)
109 int i;
110 size_t len = ring->layout.tp_frame_nr * sizeof(*ring->frames);
112 ring->frames = xmalloc_aligned(len, CO_CACHE_LINE_SIZE);
113 fmemset(ring->frames, 0, len);
115 for (i = 0; i < ring->layout.tp_frame_nr; ++i) {
116 ring->frames[i].iov_len = ring->layout.tp_frame_size;
117 ring->frames[i].iov_base = ring->mm_space +
118 (i * ring->layout.tp_frame_size);
122 void bind_tx_ring(int sock, struct ring *ring, int ifindex)
124 int ret;
126 fmemset(&ring->s_ll, 0, sizeof(ring->s_ll));
128 ring->s_ll.sll_family = AF_PACKET;
129 ring->s_ll.sll_protocol = htons(ETH_P_ALL);
130 ring->s_ll.sll_ifindex = ifindex;
131 ring->s_ll.sll_hatype = 0;
132 ring->s_ll.sll_halen = 0;
133 ring->s_ll.sll_pkttype = 0;
135 ret = bind(sock, (struct sockaddr *) &ring->s_ll, sizeof(ring->s_ll));
136 if (ret < 0) {
137 destroy_tx_ring(sock, ring);
138 panic("Cannot bind TX_RING!\n");