build: flowtop: Only build ioops with GeoIP support enabled
[netsniff-ng.git] / ring_rx.c
blob7a3d21df3b6115c3b8cbf301a545a6915bca9404
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009, 2010 Daniel Borkmann.
4 * Subject to the GPL, version 2.
5 */
7 #include <inttypes.h>
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 int ret;
26 bool v3 = get_sockopt_tpacket(sock) == TPACKET_V3;
28 munmap(ring->mm_space, ring->mm_len);
29 ring->mm_len = 0;
31 xfree(ring->frames);
33 /* In general, this is freed during close(2) anyway. */
34 if (v3)
35 return;
37 fmemset(&ring->layout, 0, sizeof(ring->layout));
38 ret = setsockopt(sock, SOL_PACKET, PACKET_RX_RING, &ring->layout,
39 sizeof(ring->layout));
40 if (unlikely(ret))
41 panic("Cannot destroy the RX_RING: %s!\n", strerror(errno));
44 static void setup_rx_ring_layout(int sock, struct ring *ring, size_t size,
45 bool jumbo_support, bool v3)
47 fmemset(&ring->layout, 0, sizeof(ring->layout));
49 ring->layout.tp_block_size = (jumbo_support ?
50 RUNTIME_PAGE_SIZE << 4 :
51 RUNTIME_PAGE_SIZE << 2);
53 ring->layout.tp_frame_size = (jumbo_support ?
54 TPACKET_ALIGNMENT << 12 :
55 TPACKET_ALIGNMENT << 7);
57 ring->layout.tp_block_nr = size / ring->layout.tp_block_size;
58 ring->layout.tp_frame_nr = ring->layout.tp_block_size /
59 ring->layout.tp_frame_size *
60 ring->layout.tp_block_nr;
61 if (v3) {
62 /* Pass out, if this will ever change and we do crap on it! */
63 build_bug_on(offsetof(struct tpacket_req, tp_frame_nr) !=
64 offsetof(struct tpacket_req3, tp_frame_nr) &&
65 sizeof(struct tpacket_req) !=
66 offsetof(struct tpacket_req3, tp_retire_blk_tov));
68 ring->layout3.tp_retire_blk_tov = 100; /* 0: let kernel decide */
69 ring->layout3.tp_sizeof_priv = 0;
70 ring->layout3.tp_feature_req_word = 0;
72 set_sockopt_tpacket_v3(sock);
73 } else {
74 set_sockopt_tpacket_v2(sock);
77 ring_verify_layout(ring);
80 static void create_rx_ring(int sock, struct ring *ring, bool verbose)
82 int ret;
83 bool v3 = get_sockopt_tpacket(sock) == TPACKET_V3;
85 retry:
86 ret = setsockopt(sock, SOL_PACKET, PACKET_RX_RING, &ring->raw,
87 v3 ? sizeof(ring->layout3) : sizeof(ring->layout));
89 if (errno == ENOMEM && ring->layout.tp_block_nr > 1) {
90 ring->layout.tp_block_nr >>= 1;
91 ring->layout.tp_frame_nr = ring->layout.tp_block_size /
92 ring->layout.tp_frame_size *
93 ring->layout.tp_block_nr;
94 goto retry;
96 if (ret < 0)
97 panic("Cannot allocate RX_RING!\n");
99 ring->mm_len = (size_t) ring->layout.tp_block_size * ring->layout.tp_block_nr;
101 if (verbose) {
102 if (!v3) {
103 printf("RX,V2: %.2Lf MiB, %u Frames, each %u Byte allocated\n",
104 (long double) ring->mm_len / (1 << 20),
105 ring->layout.tp_frame_nr, ring->layout.tp_frame_size);
106 } else {
107 printf("RX,V3: %.2Lf MiB, %u Blocks, each %u Byte allocated\n",
108 (long double) ring->mm_len / (1 << 20),
109 ring->layout.tp_block_nr, ring->layout.tp_block_size);
114 static void alloc_rx_ring_frames(int sock, struct ring *ring)
116 int num;
117 size_t size;
118 bool v3 = get_sockopt_tpacket(sock) == TPACKET_V3;
120 if (v3) {
121 num = ring->layout3.tp_block_nr;
122 size = ring->layout3.tp_block_size;
123 } else {
124 num = ring->layout.tp_frame_nr;
125 size = ring->layout.tp_frame_size;
128 alloc_ring_frames_generic(ring, num, size);
131 void ring_rx_setup(struct ring *ring, int sock, size_t size, int ifindex,
132 struct pollfd *poll, bool v3, bool jumbo_support,
133 bool verbose)
135 fmemset(ring, 0, sizeof(*ring));
136 setup_rx_ring_layout(sock, ring, size, jumbo_support, v3);
137 create_rx_ring(sock, ring, verbose);
138 mmap_ring_generic(sock, ring);
139 alloc_rx_ring_frames(sock, ring);
140 bind_ring_generic(sock, ring, ifindex, false);
141 prepare_polling(sock, poll);
144 void sock_rx_net_stats(int sock, unsigned long seen)
146 int ret;
147 bool v3 = get_sockopt_tpacket(sock) == TPACKET_V3;
148 union {
149 struct tpacket_stats k2;
150 struct tpacket_stats_v3 k3;
151 } stats;
152 socklen_t slen = v3 ? sizeof(stats.k3) : sizeof(stats.k2);
154 memset(&stats, 0, sizeof(stats));
155 ret = getsockopt(sock, SOL_PACKET, PACKET_STATISTICS, &stats, &slen);
156 if (ret > -1) {
157 uint64_t packets = stats.k3.tp_packets;
158 uint64_t drops = stats.k3.tp_drops;
160 printf("\r%12"PRIu64" packets incoming (%"PRIu64" unread on exit)\n",
161 v3 ? (uint64_t)seen : packets, v3 ? packets - seen : 0);
162 printf("\r%12"PRIu64" packets passed filter\n", packets - drops);
163 printf("\r%12"PRIu64" packets failed filter (out of space)\n", drops);
164 if (stats.k3.tp_packets > 0)
165 printf("\r%12.4lf%% packet droprate\n",
166 (1.0 * drops / packets) * 100.0);