docs: minor update in readme description, several sections
[netsniff-ng.git] / src / ring_rx.c
blob58d99629ffb3be2343dbf80e5d3f7813877a26bc
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2009, 2010 Daniel Borkmann.
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 "xmalloc.h"
19 #include "die.h"
20 #include "ring_rx.h"
21 #include "built_in.h"
23 void destroy_rx_ring(int sock, struct ring *ring)
25 fmemset(&ring->layout, 0, sizeof(ring->layout));
26 setsockopt(sock, SOL_PACKET, PACKET_RX_RING, &ring->layout,
27 sizeof(ring->layout));
29 munmap(ring->mm_space, ring->mm_len);
30 ring->mm_len = 0;
32 xfree(ring->frames);
35 void setup_rx_ring_layout(int sock, struct ring *ring, unsigned int size,
36 int jumbo_support)
38 fmemset(&ring->layout, 0, sizeof(ring->layout));
40 ring->layout.tp_block_size = (jumbo_support ?
41 getpagesize() << 4 :
42 getpagesize() << 2);
43 ring->layout.tp_frame_size = (jumbo_support ?
44 TPACKET_ALIGNMENT << 12 :
45 TPACKET_ALIGNMENT << 7);
46 ring->layout.tp_block_nr = size / ring->layout.tp_block_size;
47 ring->layout.tp_frame_nr = ring->layout.tp_block_size /
48 ring->layout.tp_frame_size *
49 ring->layout.tp_block_nr;
51 bug_on(ring->layout.tp_block_size < ring->layout.tp_frame_size);
52 bug_on((ring->layout.tp_block_size % ring->layout.tp_frame_size) != 0);
53 bug_on((ring->layout.tp_block_size % getpagesize()) != 0);
56 void create_rx_ring(int sock, struct ring *ring)
58 int ret;
60 set_sockopt_tpacket(sock);
61 retry:
62 ret = setsockopt(sock, SOL_PACKET, PACKET_RX_RING, &ring->layout,
63 sizeof(ring->layout));
64 if (errno == ENOMEM && ring->layout.tp_block_nr > 1) {
65 ring->layout.tp_block_nr >>= 1;
66 ring->layout.tp_frame_nr = ring->layout.tp_block_size /
67 ring->layout.tp_frame_size *
68 ring->layout.tp_block_nr;
69 goto retry;
72 if (ret < 0)
73 panic("Cannot allocate RX_RING!\n");
75 ring->mm_len = ring->layout.tp_block_size * ring->layout.tp_block_nr;
77 printf("RX: %.2Lf MiB, %u Frames, each %u Byte allocated\n",
78 (long double) ring->mm_len / (1 << 20),
79 ring->layout.tp_frame_nr, ring->layout.tp_frame_size);
82 void mmap_rx_ring(int sock, struct ring *ring)
84 ring->mm_space = mmap(0, ring->mm_len, PROT_READ | PROT_WRITE,
85 MAP_SHARED | MAP_LOCKED | MAP_POPULATE, sock, 0);
86 if (ring->mm_space == MAP_FAILED) {
87 destroy_rx_ring(sock, ring);
88 panic("Cannot mmap RX_RING!\n");
92 void alloc_rx_ring_frames(struct ring *ring)
94 int i;
95 size_t len = ring->layout.tp_frame_nr * sizeof(*ring->frames);
97 ring->frames = xmalloc_aligned(len, CO_CACHE_LINE_SIZE);
98 fmemset(ring->frames, 0, len);
100 for (i = 0; i < ring->layout.tp_frame_nr; ++i) {
101 ring->frames[i].iov_len = ring->layout.tp_frame_size;
102 ring->frames[i].iov_base = ring->mm_space +
103 (i * ring->layout.tp_frame_size);
107 void bind_rx_ring(int sock, struct ring *ring, int ifindex)
109 int ret;
111 * The RX_RING registers itself to the networking stack with
112 * dev_add_pack(), so we have one single RX_RING for all devs
113 * otherwise you'll get the packet twice.
115 fmemset(&ring->s_ll, 0, sizeof(ring->s_ll));
117 ring->s_ll.sll_family = AF_PACKET;
118 ring->s_ll.sll_protocol = htons(ETH_P_ALL);
119 ring->s_ll.sll_ifindex = ifindex;
120 ring->s_ll.sll_hatype = 0;
121 ring->s_ll.sll_halen = 0;
122 ring->s_ll.sll_pkttype = 0;
124 ret = bind(sock, (struct sockaddr *) &ring->s_ll, sizeof(ring->s_ll));
125 if (ret < 0) {
126 destroy_rx_ring(sock, ring);
127 panic("Cannot bind RX_RING!\n");