summaryrefslogtreecommitdiff
path: root/ring_rx.c
blob: 99f8da0c23cc4c92c268aee3876bac7369385499 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
 * netsniff-ng - the packet sniffing beast
 * Copyright 2009, 2010 Daniel Borkmann.
 * Copyright 2014, 2015 Tobias Klauser.
 * Subject to the GPL, version 2.
 */

#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <linux/if_ether.h>

#include "xmalloc.h"
#include "die.h"
#include "ring_rx.h"
#include "built_in.h"

/*
 * tpacket v3 data structures and constants are not available for older kernel
 * versions which only support tpacket v2, thus we need protect access to them.
 */
#ifdef HAVE_TPACKET3
static inline bool is_tpacket_v3(int sock)
{
	return get_sockopt_tpacket(sock) == TPACKET_V3;
}

static inline size_t get_ring_layout_size(struct ring *ring, bool v3)
{
	return v3 ? sizeof(ring->layout3) : sizeof(ring->layout);
}

static inline void setup_rx_ring_layout_v3(struct ring *ring)
{
	/* Pass out, if this will ever change and we do crap on it! */
	build_bug_on(offsetof(struct tpacket_req, tp_frame_nr) !=
		     offsetof(struct tpacket_req3, tp_frame_nr) &&
		     sizeof(struct tpacket_req) !=
		     offsetof(struct tpacket_req3, tp_retire_blk_tov));

	ring->layout3.tp_retire_blk_tov = 100; /* 0: let kernel decide */
	ring->layout3.tp_sizeof_priv = 0;
	ring->layout3.tp_feature_req_word = 0;
}

static inline int rx_ring_get_num(struct ring *ring, bool v3)
{
	return v3 ? ring->layout3.tp_block_nr : ring->layout.tp_frame_nr;
}

static inline size_t rx_ring_get_size(struct ring *ring, bool v3)
{
	return v3 ? ring->layout3.tp_block_size : ring->layout.tp_frame_size;
}

int get_rx_net_stats(int sock, uint64_t *packets, uint64_t *drops, bool v3)
{
	int ret;
	union {
		struct tpacket_stats	k2;
		struct tpacket_stats_v3 k3;
	} stats;
	socklen_t slen = v3 ? sizeof(stats.k3) : sizeof(stats.k2);

	memset(&stats, 0, sizeof(stats));
	ret = getsockopt(sock, SOL_PACKET, PACKET_STATISTICS, &stats, &slen);
	if (ret == 0) {
		*packets = stats.k3.tp_packets;
		*drops = stats.k3.tp_drops;
	}
	return ret;
}
#else
static inline bool is_tpacket_v3(int sock __maybe_unused)
{
	return false;
}

static inline size_t get_ring_layout_size(struct ring *ring, bool v3 __maybe_unused)
{
	return sizeof(ring->layout);
}

static inline void setup_rx_ring_layout_v3(struct ring *ring __maybe_unused)
{
}

static inline size_t rx_ring_get_num(struct ring *ring, bool v3 __maybe_unused)
{
	return ring->layout.tp_frame_nr;
}

static inline size_t rx_ring_get_size(struct ring *ring, bool v3 __maybe_unused)
{
	return ring->layout.tp_frame_size;
}

int get_rx_net_stats(int sock, uint64_t *packets, uint64_t *drops,
		     bool v3 __maybe_unused)
{
	int ret;
	struct tpacket_stats stats;
	socklen_t slen = sizeof(stats);

	memset(&stats, 0, sizeof(stats));
	ret = getsockopt(sock, SOL_PACKET, PACKET_STATISTICS, &stats, &slen);
	if (ret == 0) {
		*packets = stats.tp_packets;
		*drops = stats.tp_drops;
	}
	return ret;
}
#endif /* HAVE_TPACKET3 */

void destroy_rx_ring(int sock, struct ring *ring)
{
	int ret;
	bool v3 = is_tpacket_v3(sock);

	munmap(ring->mm_space, ring->mm_len);
	ring->mm_len = 0;

	xfree(ring->frames);

	/* In general, this is freed during close(2) anyway. */
	if (v3)
		return;

	fmemset(&ring->layout, 0, sizeof(ring->layout));
	ret = setsockopt(sock, SOL_PACKET, PACKET_RX_RING, &ring->layout,
			 sizeof(ring->layout));
	if (unlikely(ret))
		panic("Cannot destroy the RX_RING: %s!\n", strerror(errno));
}

static void setup_rx_ring_layout(int sock, struct ring *ring, size_t size,
				 bool jumbo_support, bool v3)
{
	setup_ring_layout_generic(ring, size, jumbo_support);

	if (v3) {
		setup_rx_ring_layout_v3(ring);
		set_sockopt_tpacket_v3(sock);
	} else {
		set_sockopt_tpacket_v2(sock);
	}

	ring_verify_layout(ring);
}

static void create_rx_ring(int sock, struct ring *ring, bool verbose)
{
	int ret;
	bool v3 = is_tpacket_v3(sock);
	size_t layout_size = get_ring_layout_size(ring, v3);

retry:
	ret = setsockopt(sock, SOL_PACKET, PACKET_RX_RING, &ring->raw,
			 layout_size);

	if (errno == ENOMEM && ring->layout.tp_block_nr > 1) {
		shrink_ring_layout_generic(ring);
		goto retry;
	}
	if (ret < 0)
		panic("Cannot allocate RX_RING!\n");

	ring->mm_len = (size_t) ring->layout.tp_block_size * ring->layout.tp_block_nr;

	if (verbose) {
		if (!v3) {
			printf("RX,V2: %.2Lf MiB, %u Frames, each %u Byte allocated\n",
			       (long double) ring->mm_len / (1 << 20),
			       ring->layout.tp_frame_nr, ring->layout.tp_frame_size);
		} else {
			printf("RX,V3: %.2Lf MiB, %u Blocks, each %u Byte allocated\n",
			       (long double) ring->mm_len / (1 << 20),
			       ring->layout.tp_block_nr, ring->layout.tp_block_size);
		}
	}
}

static void alloc_rx_ring_frames(int sock, struct ring *ring)
{
	bool v3 = is_tpacket_v3(sock);

	alloc_ring_frames_generic(ring, rx_ring_get_num(ring, v3),
				  rx_ring_get_size(ring, v3));
}

static void join_fanout_group(int sock, uint32_t fanout_group, uint32_t fanout_type)
{
	uint32_t fanout_opt = 0;
	int ret;

	if (fanout_group == 0)
		return;

#if defined(PACKET_FANOUT)
	fanout_opt = (fanout_group & 0xffff) | (fanout_type << 16);

	ret = setsockopt(sock, SOL_PACKET, PACKET_FANOUT, &fanout_opt,
			 sizeof(fanout_opt));
	if (ret < 0)
		panic("Cannot set fanout ring mode!\n");
#else
	panic("fanout ring mode is not available!\n");
#endif
}

void ring_rx_setup(struct ring *ring, int sock, size_t size, int ifindex,
		   struct pollfd *poll, bool v3, bool jumbo_support,
		   bool verbose, uint32_t fanout_group, uint32_t fanout_type)
{
	fmemset(ring, 0, sizeof(*ring));
	setup_rx_ring_layout(sock, ring, size, jumbo_support, v3);
	create_rx_ring(sock, ring, verbose);
	mmap_ring_generic(sock, ring);
	alloc_rx_ring_frames(sock, ring);
	bind_ring_generic(sock, ring, ifindex, false);
	join_fanout_group(sock, fanout_group, fanout_type);
	prepare_polling(sock, poll);
}
cgi/linux/net-next.git/plain/tools/perf/util/cgroup.h?h=nds-private-remove&id=5c178d81b69f08ca3195427a6ea9a46d9af23127'>plain -rw-r--r--cloexec.c1950logplain -rw-r--r--cloexec.h251logplain -rw-r--r--color.c4787logplain -rw-r--r--color.h1647logplain -rw-r--r--comm.c2239logplain -rw-r--r--comm.h561logplain -rw-r--r--config.c16262logplain -rw-r--r--config.h2113logplain -rw-r--r--counts.c1026logplain -rw-r--r--counts.h790logplain -rw-r--r--cpumap.c12627logplain -rw-r--r--cpumap.h1954logplain -rw-r--r--cs-etm.h2061logplain -rw-r--r--ctype.c2018logplain -rw-r--r--data-convert-bt.c36671logplain -rw-r--r--data-convert-bt.h302logplain -rw-r--r--data-convert.h141logplain -rw-r--r--data.c3459logplain -rw-r--r--data.h1369logplain -rw-r--r--db-export.c11434logplain -rw-r--r--db-export.h3816logplain -rw-r--r--debug.c4417logplain -rw-r--r--debug.h2023logplain -rw-r--r--demangle-java.c4219logplain -rw-r--r--demangle-java.h249logplain -rw-r--r--demangle-rust.c6602logplain -rw-r--r--demangle-rust.h170logplain -rw-r--r--drv_configs.c1834logplain -rw-r--r--drv_configs.h844logplain -rw-r--r--dso.c31994logplain -rw-r--r--dso.h10399logplain -rw-r--r--dwarf-aux.c33828logplain -rw-r--r--dwarf-aux.h5101logplain -rw-r--r--dwarf-regs.c1816logplain -rw-r--r--env.c1884logplain -rw-r--r--env.h1268logplain -rw-r--r--event.c36670logplain -rw-r--r--event.h15997logplain -rw-r--r--evlist.c47104logplain -rw-r--r--evlist.h12584logplain -rw-r--r--evsel.c63917logplain -rw-r--r--evsel.h13041logplain -rw-r--r--evsel_fprintf.c5831logplain -rw-r--r--find-vdso-map.c581logplain -rw-r--r--genelf.c11653logplain -rw-r--r--genelf.h1814logplain -rw-r--r--genelf_debug.c14374logplain -rwxr-xr-xgenerate-cmdlist.sh1141logplain -rw-r--r--group.h122logplain -rw-r--r--header.c73410logplain -rw-r--r--header.h4365logplain -rw-r--r--help-unknown-cmd.c3221logplain