Corrected spinning loop potential in getrandombytes
[netsniff-ng-old.git] / ring_tx.c
blobdc777e5ae815ec7695bbe03fc8d8e8862b482d22
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 fmemset(&ring->layout, 0, sizeof(ring->layout));
35 setsockopt(sock, SOL_PACKET, PACKET_TX_RING, &ring->layout,
36 sizeof(ring->layout));
38 munmap(ring->mm_space, ring->mm_len);
39 ring->mm_len = 0;
41 xfree(ring->frames);
44 void setup_tx_ring_layout(int sock, struct ring *ring, unsigned int size,
45 int jumbo_support)
47 fmemset(&ring->layout, 0, sizeof(ring->layout));
49 ring->layout.tp_block_size = (jumbo_support ?
50 getpagesize() << 4 :
51 getpagesize() << 2);
52 ring->layout.tp_frame_size = (jumbo_support ?
53 TPACKET_ALIGNMENT << 12 :
54 TPACKET_ALIGNMENT << 7);
55 ring->layout.tp_block_nr = size / ring->layout.tp_block_size;
56 ring->layout.tp_frame_nr = ring->layout.tp_block_size /
57 ring->layout.tp_frame_size *
58 ring->layout.tp_block_nr;
60 bug_on(ring->layout.tp_block_size < ring->layout.tp_frame_size);
61 bug_on((ring->layout.tp_block_size % ring->layout.tp_frame_size) != 0);
62 bug_on((ring->layout.tp_block_size % getpagesize()) != 0);
65 void create_tx_ring(int sock, struct ring *ring, int verbose)
67 int ret;
69 set_sockopt_tpacket(sock);
70 retry:
71 ret = setsockopt(sock, SOL_PACKET, PACKET_TX_RING, &ring->layout,
72 sizeof(ring->layout));
73 if (errno == ENOMEM && ring->layout.tp_block_nr > 1) {
74 ring->layout.tp_block_nr >>= 1;
75 ring->layout.tp_frame_nr = ring->layout.tp_block_size /
76 ring->layout.tp_frame_size *
77 ring->layout.tp_block_nr;
78 goto retry;
81 if (ret < 0)
82 panic("Cannot allocate TX_RING!\n");
84 ring->mm_len = ring->layout.tp_block_size * ring->layout.tp_block_nr;
86 if (verbose) {
87 printf("TX: %.2Lf MiB, %u Frames, each %u Byte allocated\n",
88 (long double) ring->mm_len / (1 << 20),
89 ring->layout.tp_frame_nr, ring->layout.tp_frame_size);
93 void mmap_tx_ring(int sock, struct ring *ring)
95 ring->mm_space = mmap(0, ring->mm_len, PROT_READ | PROT_WRITE,
96 MAP_SHARED | MAP_LOCKED | MAP_POPULATE, sock, 0);
97 if (ring->mm_space == MAP_FAILED) {
98 destroy_tx_ring(sock, ring);
99 panic("Cannot mmap TX_RING!\n");
103 void alloc_tx_ring_frames(struct ring *ring)
105 int i;
106 size_t len = ring->layout.tp_frame_nr * sizeof(*ring->frames);
108 ring->frames = xmalloc_aligned(len, CO_CACHE_LINE_SIZE);
109 fmemset(ring->frames, 0, len);
111 for (i = 0; i < ring->layout.tp_frame_nr; ++i) {
112 ring->frames[i].iov_len = ring->layout.tp_frame_size;
113 ring->frames[i].iov_base = ring->mm_space +
114 (i * ring->layout.tp_frame_size);
118 void bind_tx_ring(int sock, struct ring *ring, int ifindex)
120 int ret;
122 fmemset(&ring->s_ll, 0, sizeof(ring->s_ll));
124 ring->s_ll.sll_family = AF_PACKET;
125 ring->s_ll.sll_protocol = htons(ETH_P_ALL);
126 ring->s_ll.sll_ifindex = ifindex;
127 ring->s_ll.sll_hatype = 0;
128 ring->s_ll.sll_halen = 0;
129 ring->s_ll.sll_pkttype = 0;
131 ret = bind(sock, (struct sockaddr *) &ring->s_ll, sizeof(ring->s_ll));
132 if (ret < 0) {
133 destroy_tx_ring(sock, ring);
134 panic("Cannot bind TX_RING!\n");