netsniff-ng: Remove unnecessary initialization of struct ctx members
[netsniff-ng.git] / ring_rx.c
bloba63b1268bb5bab831fed364572016c97d4aa863f
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 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 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 void mmap_rx_ring(int sock, struct ring *ring)
116 mmap_ring_generic(sock, ring);
119 void alloc_rx_ring_frames(int sock, struct ring *ring)
121 int num;
122 size_t size;
123 bool v3 = get_sockopt_tpacket(sock) == TPACKET_V3;
125 if (v3) {
126 num = ring->layout3.tp_block_nr;
127 size = ring->layout3.tp_block_size;
128 } else {
129 num = ring->layout.tp_frame_nr;
130 size = ring->layout.tp_frame_size;
133 alloc_ring_frames_generic(ring, num, size);
136 void bind_rx_ring(int sock, struct ring *ring, int ifindex)
138 bind_ring_generic(sock, ring, ifindex, false);
141 void sock_rx_net_stats(int sock, unsigned long seen)
143 int ret;
144 bool v3 = get_sockopt_tpacket(sock) == TPACKET_V3;
145 union {
146 struct tpacket_stats k2;
147 struct tpacket_stats_v3 k3;
148 } stats;
149 socklen_t slen = v3 ? sizeof(stats.k3) : sizeof(stats.k2);
151 memset(&stats, 0, sizeof(stats));
152 ret = getsockopt(sock, SOL_PACKET, PACKET_STATISTICS, &stats, &slen);
153 if (ret > -1) {
154 uint64_t packets = stats.k3.tp_packets;
155 uint64_t drops = stats.k3.tp_drops;
157 printf("\r%12"PRIu64" packets incoming (%"PRIu64" unread on exit)\n",
158 v3 ? (uint64_t)seen : packets, v3 ? packets - seen : 0);
159 printf("\r%12"PRIu64" packets passed filter\n", packets - drops);
160 printf("\r%12"PRIu64" packets failed filter (out of space)\n", drops);
161 if (stats.k3.tp_packets > 0)
162 printf("\r%12.4lf%% packet droprate\n",
163 (1.0 * drops / packets) * 100.0);