netsniff-ng: Restore tpacket v2 capturing
[netsniff-ng.git] / ring_rx.c
blob1b8dd23354677934d1b5164cbf22a859d6ce66c0
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;
62 #ifdef HAVE_TPACKET3
63 if (v3) {
64 /* Pass out, if this will ever change and we do crap on it! */
65 build_bug_on(offsetof(struct tpacket_req, tp_frame_nr) !=
66 offsetof(struct tpacket_req3, tp_frame_nr) &&
67 sizeof(struct tpacket_req) !=
68 offsetof(struct tpacket_req3, tp_retire_blk_tov));
70 ring->layout3.tp_retire_blk_tov = 100; /* 0: let kernel decide */
71 ring->layout3.tp_sizeof_priv = 0;
72 ring->layout3.tp_feature_req_word = 0;
74 set_sockopt_tpacket_v3(sock);
75 #else
76 if (0) {
77 #endif /* HAVE_TPACKET3 */
78 } else {
79 set_sockopt_tpacket_v2(sock);
82 ring_verify_layout(ring);
85 static void create_rx_ring(int sock, struct ring *ring, bool verbose)
87 int ret;
88 #ifdef HAVE_TPACKET3
89 bool v3 = get_sockopt_tpacket(sock) == TPACKET_V3;
90 size_t layout_size = v3 ? sizeof(ring->layout3) : sizeof(ring->layout);
91 #else
92 bool v3 = false;
93 size_t layout_size = sizeof(ring->layout);
94 #endif /* HAVE_TPACKET3 */
96 retry:
97 ret = setsockopt(sock, SOL_PACKET, PACKET_RX_RING, &ring->raw,
98 layout_size);
100 if (errno == ENOMEM && ring->layout.tp_block_nr > 1) {
101 ring->layout.tp_block_nr >>= 1;
102 ring->layout.tp_frame_nr = ring->layout.tp_block_size /
103 ring->layout.tp_frame_size *
104 ring->layout.tp_block_nr;
105 goto retry;
107 if (ret < 0)
108 panic("Cannot allocate RX_RING!\n");
110 ring->mm_len = (size_t) ring->layout.tp_block_size * ring->layout.tp_block_nr;
112 if (verbose) {
113 if (!v3) {
114 printf("RX,V2: %.2Lf MiB, %u Frames, each %u Byte allocated\n",
115 (long double) ring->mm_len / (1 << 20),
116 ring->layout.tp_frame_nr, ring->layout.tp_frame_size);
117 } else {
118 printf("RX,V3: %.2Lf MiB, %u Blocks, each %u Byte allocated\n",
119 (long double) ring->mm_len / (1 << 20),
120 ring->layout.tp_block_nr, ring->layout.tp_block_size);
125 static void alloc_rx_ring_frames(int sock, struct ring *ring)
127 int num;
128 size_t size;
129 #if HAVE_TPACKET3
130 bool v3 = get_sockopt_tpacket(sock) == TPACKET_V3;
132 if (v3) {
133 num = ring->layout3.tp_block_nr;
134 size = ring->layout3.tp_block_size;
135 #else
136 if (0) {
137 #endif /* HAVE_TPACKET3 */
138 } else {
139 num = ring->layout.tp_frame_nr;
140 size = ring->layout.tp_frame_size;
143 alloc_ring_frames_generic(ring, num, size);
146 void ring_rx_setup(struct ring *ring, int sock, size_t size, int ifindex,
147 struct pollfd *poll, bool v3, bool jumbo_support,
148 bool verbose)
150 fmemset(ring, 0, sizeof(*ring));
151 setup_rx_ring_layout(sock, ring, size, jumbo_support, v3);
152 create_rx_ring(sock, ring, verbose);
153 mmap_ring_generic(sock, ring);
154 alloc_rx_ring_frames(sock, ring);
155 bind_ring_generic(sock, ring, ifindex, false);
156 prepare_polling(sock, poll);
159 void sock_rx_net_stats(int sock, unsigned long seen)
161 int ret;
162 bool v3 = get_sockopt_tpacket(sock) == TPACKET_V3;
163 union {
164 struct tpacket_stats k2;
165 struct tpacket_stats_v3 k3;
166 } stats;
167 socklen_t slen = v3 ? sizeof(stats.k3) : sizeof(stats.k2);
169 memset(&stats, 0, sizeof(stats));
170 ret = getsockopt(sock, SOL_PACKET, PACKET_STATISTICS, &stats, &slen);
171 if (ret > -1) {
172 uint64_t packets = stats.k3.tp_packets;
173 uint64_t drops = stats.k3.tp_drops;
175 printf("\r%12"PRIu64" packets incoming (%"PRIu64" unread on exit)\n",
176 v3 ? (uint64_t)seen : packets, v3 ? packets - seen : 0);
177 printf("\r%12"PRIu64" packets passed filter\n", packets - drops);
178 printf("\r%12"PRIu64" packets failed filter (out of space)\n", drops);
179 if (stats.k3.tp_packets > 0)
180 printf("\r%12.4lf%% packet droprate\n",
181 (1.0 * drops / packets) * 100.0);