proto_icmpv6.h: intermediate state
[netsniff-ng.git] / src / ring_tx.c
blobdbd8c439b4c4d1707bed048a5d37a36227e948db
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)
68 int ret;
69 retry:
70 ret = setsockopt(sock, SOL_PACKET, PACKET_TX_RING, &ring->layout,
71 sizeof(ring->layout));
72 if (errno == ENOMEM && ring->layout.tp_block_nr > 1) {
73 ring->layout.tp_block_nr >>= 1;
74 ring->layout.tp_frame_nr = ring->layout.tp_block_size /
75 ring->layout.tp_frame_size *
76 ring->layout.tp_block_nr;
77 goto retry;
80 if (ret < 0)
81 panic("Cannot allocate TX_RING!\n");
83 ring->mm_len = ring->layout.tp_block_size * ring->layout.tp_block_nr;
85 printf("TX: %.2f MiB, %u Frames, each %u Byte allocated\n",
86 1.f * ring->mm_len / (1 << 20),
87 ring->layout.tp_frame_nr, ring->layout.tp_frame_size);
90 void mmap_tx_ring(int sock, struct ring *ring)
92 ring->mm_space = mmap(0, ring->mm_len, PROT_READ | PROT_WRITE,
93 MAP_SHARED | MAP_LOCKED, sock, 0);
94 if (ring->mm_space == MAP_FAILED) {
95 destroy_tx_ring(sock, ring);
96 panic("Cannot mmap TX_RING!\n");
100 void alloc_tx_ring_frames(struct ring *ring)
102 int i;
103 size_t len = ring->layout.tp_frame_nr * sizeof(*ring->frames);
105 ring->frames = xmalloc_aligned(len, CO_CACHE_LINE_SIZE);
106 fmemset(ring->frames, 0, len);
108 for (i = 0; i < ring->layout.tp_frame_nr; ++i) {
109 ring->frames[i].iov_len = ring->layout.tp_frame_size;
110 ring->frames[i].iov_base = ring->mm_space +
111 (i * ring->layout.tp_frame_size);
115 void bind_tx_ring(int sock, struct ring *ring, int ifindex)
117 int ret;
119 fmemset(&ring->s_ll, 0, sizeof(ring->s_ll));
121 ring->s_ll.sll_family = AF_PACKET;
122 ring->s_ll.sll_protocol = htons(ETH_P_ALL);
123 ring->s_ll.sll_ifindex = ifindex;
124 ring->s_ll.sll_hatype = 0;
125 ring->s_ll.sll_halen = 0;
126 ring->s_ll.sll_pkttype = 0;
128 ret = bind(sock, (struct sockaddr *) &ring->s_ll, sizeof(ring->s_ll));
129 if (ret < 0) {
130 destroy_tx_ring(sock, ring);
131 panic("Cannot bind TX_RING!\n");