trafgen: fixed '--in -' to work again with STDIN
[netsniff-ng-new.git] / ring_rx.c
blob13c86b4f35dceb424deee4fdf978e6dea60afa86
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009, 2010 Daniel Borkmann.
4 * Copyright 2014, 2015 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 size_t 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 memset(&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 setup_ring_layout_generic(ring, size, jumbo_support);
147 if (v3) {
148 setup_rx_ring_layout_v3(ring);
149 set_sockopt_tpacket_v3(sock);
150 } else {
151 set_sockopt_tpacket_v2(sock);
154 ring_verify_layout(ring);
157 static void create_rx_ring(int sock, struct ring *ring, bool verbose)
159 int ret;
160 bool v3 = is_tpacket_v3(sock);
161 size_t layout_size = get_ring_layout_size(ring, v3);
163 retry:
164 ret = setsockopt(sock, SOL_PACKET, PACKET_RX_RING, &ring->raw,
165 layout_size);
167 if (errno == ENOMEM && ring->layout.tp_block_nr > 1) {
168 shrink_ring_layout_generic(ring);
169 goto retry;
171 if (ret < 0)
172 panic("Cannot allocate RX_RING!\n");
174 ring->mm_len = (size_t) ring->layout.tp_block_size * ring->layout.tp_block_nr;
176 if (verbose) {
177 if (!v3) {
178 printf("RX,V2: %.2Lf MiB, %u Frames, each %u Byte allocated\n",
179 (long double) ring->mm_len / (1 << 20),
180 ring->layout.tp_frame_nr, ring->layout.tp_frame_size);
181 } else {
182 printf("RX,V3: %.2Lf MiB, %u Blocks, each %u Byte allocated\n",
183 (long double) ring->mm_len / (1 << 20),
184 ring->layout.tp_block_nr, ring->layout.tp_block_size);
189 static void alloc_rx_ring_frames(int sock, struct ring *ring)
191 bool v3 = is_tpacket_v3(sock);
193 alloc_ring_frames_generic(ring, rx_ring_get_num(ring, v3),
194 rx_ring_get_size(ring, v3));
197 static void join_fanout_group(int sock, uint32_t fanout_group, uint32_t fanout_type)
199 uint32_t fanout_opt = 0;
200 int ret;
202 if (fanout_group == 0)
203 return;
205 #if defined(PACKET_FANOUT)
206 fanout_opt = (fanout_group & 0xffff) | (fanout_type << 16);
208 ret = setsockopt(sock, SOL_PACKET, PACKET_FANOUT, &fanout_opt,
209 sizeof(fanout_opt));
210 if (ret < 0)
211 panic("Cannot set fanout ring mode!\n");
212 #else
213 panic("fanout ring mode is not available!\n");
214 #endif
217 void ring_rx_setup(struct ring *ring, int sock, size_t size, int ifindex,
218 struct pollfd *poll, bool v3, bool jumbo_support,
219 bool verbose, uint32_t fanout_group, uint32_t fanout_type)
221 memset(ring, 0, sizeof(*ring));
222 setup_rx_ring_layout(sock, ring, size, jumbo_support, v3);
223 create_rx_ring(sock, ring, verbose);
224 mmap_ring_generic(sock, ring);
225 alloc_rx_ring_frames(sock, ring);
226 bind_ring_generic(sock, ring, ifindex, false);
227 join_fanout_group(sock, fanout_group, fanout_type);
228 prepare_polling(sock, poll);