docs: add Jon as minor contributor
[netsniff-ng.git] / src / ring_rx.c
blobd05a50501eb1e1231a18df0e422678608e87614d
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;
59 retry:
60 ret = setsockopt(sock, SOL_PACKET, PACKET_RX_RING, &ring->layout,
61 sizeof(ring->layout));
62 if (errno == ENOMEM && ring->layout.tp_block_nr > 1) {
63 ring->layout.tp_block_nr >>= 1;
64 ring->layout.tp_frame_nr = ring->layout.tp_block_size /
65 ring->layout.tp_frame_size *
66 ring->layout.tp_block_nr;
67 goto retry;
70 if (ret < 0)
71 panic("Cannot allocate RX_RING!\n");
73 ring->mm_len = ring->layout.tp_block_size * ring->layout.tp_block_nr;
75 printf("RX: %.2Lf MiB, %u Frames, each %u Byte allocated\n",
76 (long double) ring->mm_len / (1 << 20),
77 ring->layout.tp_frame_nr, ring->layout.tp_frame_size);
80 void mmap_rx_ring(int sock, struct ring *ring)
82 ring->mm_space = mmap(0, ring->mm_len, PROT_READ | PROT_WRITE,
83 MAP_SHARED | MAP_LOCKED | MAP_POPULATE, sock, 0);
84 if (ring->mm_space == MAP_FAILED) {
85 destroy_rx_ring(sock, ring);
86 panic("Cannot mmap RX_RING!\n");
90 void alloc_rx_ring_frames(struct ring *ring)
92 int i;
93 size_t len = ring->layout.tp_frame_nr * sizeof(*ring->frames);
95 ring->frames = xmalloc_aligned(len, CO_CACHE_LINE_SIZE);
96 fmemset(ring->frames, 0, len);
98 for (i = 0; i < ring->layout.tp_frame_nr; ++i) {
99 ring->frames[i].iov_len = ring->layout.tp_frame_size;
100 ring->frames[i].iov_base = ring->mm_space +
101 (i * ring->layout.tp_frame_size);
105 void bind_rx_ring(int sock, struct ring *ring, int ifindex)
107 int ret;
109 * The RX_RING registers itself to the networking stack with
110 * dev_add_pack(), so we have one single RX_RING for all devs
111 * otherwise you'll get the packet twice.
113 fmemset(&ring->s_ll, 0, sizeof(ring->s_ll));
115 ring->s_ll.sll_family = AF_PACKET;
116 ring->s_ll.sll_protocol = htons(ETH_P_ALL);
117 ring->s_ll.sll_ifindex = ifindex;
118 ring->s_ll.sll_hatype = 0;
119 ring->s_ll.sll_halen = 0;
120 ring->s_ll.sll_pkttype = 0;
122 ret = bind(sock, (struct sockaddr *) &ring->s_ll, sizeof(ring->s_ll));
123 if (ret < 0) {
124 destroy_rx_ring(sock, ring);
125 panic("Cannot bind RX_RING!\n");