curvetun: Fix issues detected by the Coverity scanner
[netsniff-ng.git] / ring_rx.c
blob11d1d080f219257e5924b943a44afb125e948ee5
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009, 2010 Daniel Borkmann.
4 * Copyright 2014 Tobias Klauser.
5 * Subject to the GPL, version 2.
6 */
8 #include <inttypes.h>
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 "xmalloc.h"
20 #include "die.h"
21 #include "ring_rx.h"
22 #include "built_in.h"
25 * tpacket v3 data structures and constants are not available for older kernel
26 * versions which only support tpacket v2, thus we need protect access to them.
28 #ifdef HAVE_TPACKET3
29 static inline bool is_tpacket_v3(int sock)
31 return get_sockopt_tpacket(sock) == TPACKET_V3;
34 static inline size_t get_ring_layout_size(struct ring *ring, bool v3)
36 return v3 ? sizeof(ring->layout3) : sizeof(ring->layout);
39 static inline void setup_rx_ring_layout_v3(struct ring *ring)
41 /* Pass out, if this will ever change and we do crap on it! */
42 build_bug_on(offsetof(struct tpacket_req, tp_frame_nr) !=
43 offsetof(struct tpacket_req3, tp_frame_nr) &&
44 sizeof(struct tpacket_req) !=
45 offsetof(struct tpacket_req3, tp_retire_blk_tov));
47 ring->layout3.tp_retire_blk_tov = 100; /* 0: let kernel decide */
48 ring->layout3.tp_sizeof_priv = 0;
49 ring->layout3.tp_feature_req_word = 0;
52 static inline int rx_ring_get_num(struct ring *ring, bool v3)
54 return v3 ? ring->layout3.tp_block_nr : ring->layout.tp_frame_nr;
57 static inline size_t rx_ring_get_size(struct ring *ring, bool v3)
59 return v3 ? ring->layout3.tp_block_size : ring->layout.tp_frame_size;
62 int get_rx_net_stats(int sock, uint64_t *packets, uint64_t *drops, bool v3)
64 int ret;
65 union {
66 struct tpacket_stats k2;
67 struct tpacket_stats_v3 k3;
68 } stats;
69 socklen_t slen = v3 ? sizeof(stats.k3) : sizeof(stats.k2);
71 memset(&stats, 0, sizeof(stats));
72 ret = getsockopt(sock, SOL_PACKET, PACKET_STATISTICS, &stats, &slen);
73 if (ret == 0) {
74 *packets = stats.k3.tp_packets;
75 *drops = stats.k3.tp_drops;
77 return ret;
79 #else
80 static inline bool is_tpacket_v3(int sock __maybe_unused)
82 return false;
85 static inline size_t get_ring_layout_size(struct ring *ring, bool v3 __maybe_unused)
87 return sizeof(ring->layout);
90 static inline void setup_rx_ring_layout_v3(struct ring *ring __maybe_unused)
94 static inline int rx_ring_get_num(struct ring *ring, bool v3 __maybe_unused)
96 return ring->layout.tp_frame_nr;
99 static inline size_t rx_ring_get_size(struct ring *ring, bool v3 __maybe_unused)
101 return ring->layout.tp_frame_size;
104 int get_rx_net_stats(int sock, uint64_t *packets, uint64_t *drops,
105 bool v3 __maybe_unused)
107 int ret;
108 struct tpacket_stats stats;
109 socklen_t slen = sizeof(stats);
111 memset(&stats, 0, sizeof(stats));
112 ret = getsockopt(sock, SOL_PACKET, PACKET_STATISTICS, &stats, &slen);
113 if (ret == 0) {
114 *packets = stats.tp_packets;
115 *drops = stats.tp_drops;
117 return ret;
119 #endif /* HAVE_TPACKET3 */
121 void destroy_rx_ring(int sock, struct ring *ring)
123 int ret;
124 bool v3 = is_tpacket_v3(sock);
126 munmap(ring->mm_space, ring->mm_len);
127 ring->mm_len = 0;
129 xfree(ring->frames);
131 /* In general, this is freed during close(2) anyway. */
132 if (v3)
133 return;
135 fmemset(&ring->layout, 0, sizeof(ring->layout));
136 ret = setsockopt(sock, SOL_PACKET, PACKET_RX_RING, &ring->layout,
137 sizeof(ring->layout));
138 if (unlikely(ret))
139 panic("Cannot destroy the RX_RING: %s!\n", strerror(errno));
142 static void setup_rx_ring_layout(int sock, struct ring *ring, size_t size,
143 bool jumbo_support, bool v3)
145 fmemset(&ring->layout, 0, sizeof(ring->layout));
147 ring->layout.tp_block_size = (jumbo_support ?
148 RUNTIME_PAGE_SIZE << 4 :
149 RUNTIME_PAGE_SIZE << 2);
151 ring->layout.tp_frame_size = (jumbo_support ?
152 TPACKET_ALIGNMENT << 12 :
153 TPACKET_ALIGNMENT << 7);
155 ring->layout.tp_block_nr = size / ring->layout.tp_block_size;
156 ring->layout.tp_frame_nr = ring->layout.tp_block_size /
157 ring->layout.tp_frame_size *
158 ring->layout.tp_block_nr;
160 if (v3) {
161 setup_rx_ring_layout_v3(ring);
162 set_sockopt_tpacket_v3(sock);
163 } else {
164 set_sockopt_tpacket_v2(sock);
167 ring_verify_layout(ring);
170 static void create_rx_ring(int sock, struct ring *ring, bool verbose)
172 int ret;
173 bool v3 = is_tpacket_v3(sock);
174 size_t layout_size = get_ring_layout_size(ring, v3);
176 retry:
177 ret = setsockopt(sock, SOL_PACKET, PACKET_RX_RING, &ring->raw,
178 layout_size);
180 if (errno == ENOMEM && ring->layout.tp_block_nr > 1) {
181 ring->layout.tp_block_nr >>= 1;
182 ring->layout.tp_frame_nr = ring->layout.tp_block_size /
183 ring->layout.tp_frame_size *
184 ring->layout.tp_block_nr;
185 goto retry;
187 if (ret < 0)
188 panic("Cannot allocate RX_RING!\n");
190 ring->mm_len = (size_t) ring->layout.tp_block_size * ring->layout.tp_block_nr;
192 if (verbose) {
193 if (!v3) {
194 printf("RX,V2: %.2Lf MiB, %u Frames, each %u Byte allocated\n",
195 (long double) ring->mm_len / (1 << 20),
196 ring->layout.tp_frame_nr, ring->layout.tp_frame_size);
197 } else {
198 printf("RX,V3: %.2Lf MiB, %u Blocks, each %u Byte allocated\n",
199 (long double) ring->mm_len / (1 << 20),
200 ring->layout.tp_block_nr, ring->layout.tp_block_size);
205 static void alloc_rx_ring_frames(int sock, struct ring *ring)
207 bool v3 = is_tpacket_v3(sock);
209 alloc_ring_frames_generic(ring, rx_ring_get_num(ring, v3),
210 rx_ring_get_size(ring, v3));
213 void join_fanout_group(int sock, uint32_t fanout_group, uint32_t fanout_type)
215 uint32_t fanout_opt = 0;
216 int ret;
218 if (fanout_group == 0)
219 return;
221 fanout_opt = (fanout_group & 0xffff) | (fanout_type << 16);
223 ret = setsockopt(sock, SOL_PACKET, PACKET_FANOUT, &fanout_opt,
224 sizeof(fanout_opt));
225 if (ret < 0)
226 panic("Cannot set fanout ring mode!\n");
229 void ring_rx_setup(struct ring *ring, int sock, size_t size, int ifindex,
230 struct pollfd *poll, bool v3, bool jumbo_support,
231 bool verbose, uint32_t fanout_group, uint32_t fanout_type)
233 fmemset(ring, 0, sizeof(*ring));
234 setup_rx_ring_layout(sock, ring, size, jumbo_support, v3);
235 create_rx_ring(sock, ring, verbose);
236 mmap_ring_generic(sock, ring);
237 alloc_rx_ring_frames(sock, ring);
238 bind_ring_generic(sock, ring, ifindex, false);
239 join_fanout_group(sock, fanout_group, fanout_type);
240 prepare_polling(sock, poll);