make: also credit involved people in mail message
[netsniff-ng.git] / ring_rx.c
blobd89d7624746fc06ac030fea8035989a1a3d1a1a7
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009, 2010 Daniel Borkmann.
4 * Subject to the GPL, version 2.
5 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <sys/mman.h>
12 #include <sys/types.h>
13 #include <sys/socket.h>
14 #include <arpa/inet.h>
15 #include <linux/if_ether.h>
17 #include "xmalloc.h"
18 #include "die.h"
19 #include "ring_rx.h"
20 #include "built_in.h"
22 void destroy_rx_ring(int sock, struct ring *ring)
24 int ret;
26 munmap(ring->mm_space, ring->mm_len);
27 ring->mm_len = 0;
29 fmemset(&ring->layout, 0, sizeof(ring->layout));
30 ret = setsockopt(sock, SOL_PACKET, PACKET_RX_RING, &ring->layout,
31 sizeof(ring->layout));
32 if (unlikely(ret))
33 panic("Cannot destroy the RX_RING: %s!\n", strerror(errno));
35 xfree(ring->frames);
38 void setup_rx_ring_layout(int sock, struct ring *ring, unsigned int size,
39 int jumbo_support)
41 fmemset(&ring->layout, 0, sizeof(ring->layout));
43 ring->layout.tp_block_size = (jumbo_support ?
44 getpagesize() << 4 :
45 getpagesize() << 2);
46 ring->layout.tp_frame_size = (jumbo_support ?
47 TPACKET_ALIGNMENT << 12 :
48 TPACKET_ALIGNMENT << 7);
49 ring->layout.tp_block_nr = size / ring->layout.tp_block_size;
50 ring->layout.tp_frame_nr = ring->layout.tp_block_size /
51 ring->layout.tp_frame_size *
52 ring->layout.tp_block_nr;
54 bug_on(ring->layout.tp_block_size < ring->layout.tp_frame_size);
55 bug_on((ring->layout.tp_block_size % ring->layout.tp_frame_size) != 0);
56 bug_on((ring->layout.tp_block_size % getpagesize()) != 0);
59 void create_rx_ring(int sock, struct ring *ring, int verbose)
61 int ret;
63 set_sockopt_tpacket(sock);
64 retry:
65 ret = setsockopt(sock, SOL_PACKET, PACKET_RX_RING, &ring->layout,
66 sizeof(ring->layout));
67 if (errno == ENOMEM && ring->layout.tp_block_nr > 1) {
68 ring->layout.tp_block_nr >>= 1;
69 ring->layout.tp_frame_nr = ring->layout.tp_block_size /
70 ring->layout.tp_frame_size *
71 ring->layout.tp_block_nr;
72 goto retry;
75 if (ret < 0)
76 panic("Cannot allocate RX_RING!\n");
78 ring->mm_len = ring->layout.tp_block_size * ring->layout.tp_block_nr;
80 if (verbose) {
81 printf("RX: %.2Lf MiB, %u Frames, each %u Byte allocated\n",
82 (long double) ring->mm_len / (1 << 20),
83 ring->layout.tp_frame_nr, ring->layout.tp_frame_size);
87 void mmap_rx_ring(int sock, struct ring *ring)
89 ring->mm_space = mmap(0, ring->mm_len, PROT_READ | PROT_WRITE,
90 MAP_SHARED | MAP_LOCKED | MAP_POPULATE, sock, 0);
91 if (ring->mm_space == MAP_FAILED) {
92 destroy_rx_ring(sock, ring);
93 panic("Cannot mmap RX_RING!\n");
97 void alloc_rx_ring_frames(struct ring *ring)
99 int i;
100 size_t len = ring->layout.tp_frame_nr * sizeof(*ring->frames);
102 ring->frames = xmalloc_aligned(len, CO_CACHE_LINE_SIZE);
103 fmemset(ring->frames, 0, len);
105 for (i = 0; i < ring->layout.tp_frame_nr; ++i) {
106 ring->frames[i].iov_len = ring->layout.tp_frame_size;
107 ring->frames[i].iov_base = ring->mm_space +
108 (i * ring->layout.tp_frame_size);
112 void bind_rx_ring(int sock, struct ring *ring, int ifindex)
114 int ret;
116 * The RX_RING registers itself to the networking stack with
117 * dev_add_pack(), so we have one single RX_RING for all devs
118 * otherwise you'll get the packet twice.
120 fmemset(&ring->s_ll, 0, sizeof(ring->s_ll));
122 ring->s_ll.sll_family = AF_PACKET;
123 ring->s_ll.sll_protocol = htons(ETH_P_ALL);
124 ring->s_ll.sll_ifindex = ifindex;
125 ring->s_ll.sll_hatype = 0;
126 ring->s_ll.sll_halen = 0;
127 ring->s_ll.sll_pkttype = 0;
129 ret = bind(sock, (struct sockaddr *) &ring->s_ll, sizeof(ring->s_ll));
130 if (ret < 0) {
131 destroy_rx_ring(sock, ring);
132 panic("Cannot bind RX_RING!\n");