]>
Commit | Line | Data |
---|---|---|
77f65ebd WB |
1 | /* |
2 | * Copyright 2013 Google Inc. | |
3 | * Author: Willem de Bruijn (willemb@google.com) | |
4 | * | |
5 | * A basic test of packet socket fanout behavior. | |
6 | * | |
7 | * Control: | |
8 | * - create fanout fails as expected with illegal flag combinations | |
9 | * - join fanout fails as expected with diverging types or flags | |
10 | * | |
11 | * Datapath: | |
12 | * Open a pair of packet sockets and a pair of INET sockets, send a known | |
13 | * number of packets across the two INET sockets and count the number of | |
14 | * packets enqueued onto the two packet sockets. | |
15 | * | |
16 | * The test currently runs for | |
17 | * - PACKET_FANOUT_HASH | |
18 | * - PACKET_FANOUT_HASH with PACKET_FANOUT_FLAG_ROLLOVER | |
23a9072e WB |
19 | * - PACKET_FANOUT_LB |
20 | * - PACKET_FANOUT_CPU | |
77f65ebd | 21 | * - PACKET_FANOUT_ROLLOVER |
95e22792 | 22 | * - PACKET_FANOUT_CBPF |
30da679e | 23 | * - PACKET_FANOUT_EBPF |
77f65ebd WB |
24 | * |
25 | * Todo: | |
77f65ebd WB |
26 | * - functionality: PACKET_FANOUT_FLAG_DEFRAG |
27 | * | |
28 | * License (GPLv2): | |
29 | * | |
30 | * This program is free software; you can redistribute it and/or modify it | |
31 | * under the terms and conditions of the GNU General Public License, | |
32 | * version 2, as published by the Free Software Foundation. | |
33 | * | |
34 | * This program is distributed in the hope it will be useful, but WITHOUT | |
35 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
36 | * FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for | |
37 | * more details. | |
38 | * | |
39 | * You should have received a copy of the GNU General Public License along with | |
40 | * this program; if not, write to the Free Software Foundation, Inc., | |
41 | * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. | |
42 | */ | |
43 | ||
23a9072e WB |
44 | #define _GNU_SOURCE /* for sched_setaffinity */ |
45 | ||
77f65ebd WB |
46 | #include <arpa/inet.h> |
47 | #include <errno.h> | |
23a9072e | 48 | #include <fcntl.h> |
30da679e | 49 | #include <linux/unistd.h> /* for __NR_bpf */ |
77f65ebd | 50 | #include <linux/filter.h> |
30da679e | 51 | #include <linux/bpf.h> |
77f65ebd WB |
52 | #include <linux/if_packet.h> |
53 | #include <net/ethernet.h> | |
54 | #include <netinet/ip.h> | |
55 | #include <netinet/udp.h> | |
23a9072e WB |
56 | #include <poll.h> |
57 | #include <sched.h> | |
77f65ebd WB |
58 | #include <stdint.h> |
59 | #include <stdio.h> | |
60 | #include <stdlib.h> | |
61 | #include <string.h> | |
23a9072e | 62 | #include <sys/mman.h> |
77f65ebd WB |
63 | #include <sys/socket.h> |
64 | #include <sys/stat.h> | |
65 | #include <sys/types.h> | |
66 | #include <unistd.h> | |
67 | ||
23a95442 | 68 | #include "psock_lib.h" |
77f65ebd | 69 | |
23a95442 | 70 | #define RING_NUM_FRAMES 20 |
77f65ebd WB |
71 | |
72 | /* Open a socket in a given fanout mode. | |
73 | * @return -1 if mode is bad, a valid socket otherwise */ | |
74 | static int sock_fanout_open(uint16_t typeflags, int num_packets) | |
75 | { | |
76 | int fd, val; | |
77 | ||
c1f8d0f9 | 78 | fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP)); |
77f65ebd WB |
79 | if (fd < 0) { |
80 | perror("socket packet"); | |
81 | exit(1); | |
82 | } | |
83 | ||
84 | /* fanout group ID is always 0: tests whether old groups are deleted */ | |
85 | val = ((int) typeflags) << 16; | |
86 | if (setsockopt(fd, SOL_PACKET, PACKET_FANOUT, &val, sizeof(val))) { | |
87 | if (close(fd)) { | |
88 | perror("close packet"); | |
89 | exit(1); | |
90 | } | |
91 | return -1; | |
92 | } | |
93 | ||
23a95442 | 94 | pair_udp_setfilter(fd); |
77f65ebd WB |
95 | return fd; |
96 | } | |
97 | ||
c1f8d0f9 MM |
98 | static void sock_fanout_set_cbpf(int fd) |
99 | { | |
100 | struct sock_filter bpf_filter[] = { | |
101 | BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 80), /* ldb [80] */ | |
102 | BPF_STMT(BPF_RET+BPF_A, 0), /* ret A */ | |
103 | }; | |
104 | struct sock_fprog bpf_prog; | |
105 | ||
106 | bpf_prog.filter = bpf_filter; | |
107 | bpf_prog.len = sizeof(bpf_filter) / sizeof(struct sock_filter); | |
108 | ||
109 | if (setsockopt(fd, SOL_PACKET, PACKET_FANOUT_DATA, &bpf_prog, | |
110 | sizeof(bpf_prog))) { | |
111 | perror("fanout data cbpf"); | |
112 | exit(1); | |
113 | } | |
114 | } | |
115 | ||
30da679e WB |
116 | static void sock_fanout_set_ebpf(int fd) |
117 | { | |
118 | const int len_off = __builtin_offsetof(struct __sk_buff, len); | |
119 | struct bpf_insn prog[] = { | |
120 | { BPF_ALU64 | BPF_MOV | BPF_X, 6, 1, 0, 0 }, | |
121 | { BPF_LDX | BPF_W | BPF_MEM, 0, 6, len_off, 0 }, | |
122 | { BPF_JMP | BPF_JGE | BPF_K, 0, 0, 1, DATA_LEN }, | |
123 | { BPF_JMP | BPF_JA | BPF_K, 0, 0, 4, 0 }, | |
124 | { BPF_LD | BPF_B | BPF_ABS, 0, 0, 0, 0x50 }, | |
125 | { BPF_JMP | BPF_JEQ | BPF_K, 0, 0, 2, DATA_CHAR }, | |
126 | { BPF_JMP | BPF_JEQ | BPF_K, 0, 0, 1, DATA_CHAR_1 }, | |
127 | { BPF_ALU | BPF_MOV | BPF_K, 0, 0, 0, 0 }, | |
128 | { BPF_JMP | BPF_EXIT, 0, 0, 0, 0 } | |
129 | }; | |
130 | char log_buf[512]; | |
131 | union bpf_attr attr; | |
132 | int pfd; | |
133 | ||
134 | memset(&attr, 0, sizeof(attr)); | |
135 | attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER; | |
136 | attr.insns = (unsigned long) prog; | |
137 | attr.insn_cnt = sizeof(prog) / sizeof(prog[0]); | |
138 | attr.license = (unsigned long) "GPL"; | |
139 | attr.log_buf = (unsigned long) log_buf, | |
140 | attr.log_size = sizeof(log_buf), | |
141 | attr.log_level = 1, | |
142 | ||
143 | pfd = syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr)); | |
144 | if (pfd < 0) { | |
145 | perror("bpf"); | |
146 | fprintf(stderr, "bpf verifier:\n%s\n", log_buf); | |
147 | exit(1); | |
148 | } | |
149 | ||
150 | if (setsockopt(fd, SOL_PACKET, PACKET_FANOUT_DATA, &pfd, sizeof(pfd))) { | |
151 | perror("fanout data ebpf"); | |
152 | exit(1); | |
153 | } | |
154 | ||
155 | if (close(pfd)) { | |
156 | perror("close ebpf"); | |
157 | exit(1); | |
158 | } | |
159 | } | |
160 | ||
23a9072e | 161 | static char *sock_fanout_open_ring(int fd) |
77f65ebd | 162 | { |
23a9072e WB |
163 | struct tpacket_req req = { |
164 | .tp_block_size = getpagesize(), | |
165 | .tp_frame_size = getpagesize(), | |
166 | .tp_block_nr = RING_NUM_FRAMES, | |
167 | .tp_frame_nr = RING_NUM_FRAMES, | |
168 | }; | |
169 | char *ring; | |
98e821a2 | 170 | int val = TPACKET_V2; |
77f65ebd | 171 | |
98e821a2 WB |
172 | if (setsockopt(fd, SOL_PACKET, PACKET_VERSION, (void *) &val, |
173 | sizeof(val))) { | |
174 | perror("packetsock ring setsockopt version"); | |
175 | exit(1); | |
176 | } | |
23a9072e WB |
177 | if (setsockopt(fd, SOL_PACKET, PACKET_RX_RING, (void *) &req, |
178 | sizeof(req))) { | |
179 | perror("packetsock ring setsockopt"); | |
77f65ebd WB |
180 | exit(1); |
181 | } | |
23a9072e WB |
182 | |
183 | ring = mmap(0, req.tp_block_size * req.tp_block_nr, | |
184 | PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); | |
95e22792 WB |
185 | if (ring == MAP_FAILED) { |
186 | perror("packetsock ring mmap"); | |
77f65ebd WB |
187 | exit(1); |
188 | } | |
23a9072e WB |
189 | |
190 | return ring; | |
191 | } | |
192 | ||
193 | static int sock_fanout_read_ring(int fd, void *ring) | |
194 | { | |
98e821a2 | 195 | struct tpacket2_hdr *header = ring; |
23a9072e WB |
196 | int count = 0; |
197 | ||
fbf8e721 | 198 | while (count < RING_NUM_FRAMES && header->tp_status & TP_STATUS_USER) { |
23a9072e WB |
199 | count++; |
200 | header = ring + (count * getpagesize()); | |
201 | } | |
202 | ||
203 | return count; | |
204 | } | |
205 | ||
206 | static int sock_fanout_read(int fds[], char *rings[], const int expect[]) | |
207 | { | |
208 | int ret[2]; | |
209 | ||
210 | ret[0] = sock_fanout_read_ring(fds[0], rings[0]); | |
211 | ret[1] = sock_fanout_read_ring(fds[1], rings[1]); | |
77f65ebd WB |
212 | |
213 | fprintf(stderr, "info: count=%d,%d, expect=%d,%d\n", | |
214 | ret[0], ret[1], expect[0], expect[1]); | |
215 | ||
216 | if ((!(ret[0] == expect[0] && ret[1] == expect[1])) && | |
217 | (!(ret[0] == expect[1] && ret[1] == expect[0]))) { | |
218 | fprintf(stderr, "ERROR: incorrect queue lengths\n"); | |
23a9072e | 219 | return 1; |
77f65ebd | 220 | } |
23a9072e WB |
221 | |
222 | return 0; | |
77f65ebd WB |
223 | } |
224 | ||
225 | /* Test illegal mode + flag combination */ | |
226 | static void test_control_single(void) | |
227 | { | |
228 | fprintf(stderr, "test: control single socket\n"); | |
229 | ||
230 | if (sock_fanout_open(PACKET_FANOUT_ROLLOVER | | |
231 | PACKET_FANOUT_FLAG_ROLLOVER, 0) != -1) { | |
232 | fprintf(stderr, "ERROR: opened socket with dual rollover\n"); | |
233 | exit(1); | |
234 | } | |
235 | } | |
236 | ||
237 | /* Test illegal group with different modes or flags */ | |
238 | static void test_control_group(void) | |
239 | { | |
240 | int fds[2]; | |
241 | ||
242 | fprintf(stderr, "test: control multiple sockets\n"); | |
243 | ||
244 | fds[0] = sock_fanout_open(PACKET_FANOUT_HASH, 20); | |
245 | if (fds[0] == -1) { | |
246 | fprintf(stderr, "ERROR: failed to open HASH socket\n"); | |
247 | exit(1); | |
248 | } | |
249 | if (sock_fanout_open(PACKET_FANOUT_HASH | | |
250 | PACKET_FANOUT_FLAG_DEFRAG, 10) != -1) { | |
251 | fprintf(stderr, "ERROR: joined group with wrong flag defrag\n"); | |
252 | exit(1); | |
253 | } | |
254 | if (sock_fanout_open(PACKET_FANOUT_HASH | | |
255 | PACKET_FANOUT_FLAG_ROLLOVER, 10) != -1) { | |
256 | fprintf(stderr, "ERROR: joined group with wrong flag ro\n"); | |
257 | exit(1); | |
258 | } | |
259 | if (sock_fanout_open(PACKET_FANOUT_CPU, 10) != -1) { | |
260 | fprintf(stderr, "ERROR: joined group with wrong mode\n"); | |
261 | exit(1); | |
262 | } | |
263 | fds[1] = sock_fanout_open(PACKET_FANOUT_HASH, 20); | |
264 | if (fds[1] == -1) { | |
265 | fprintf(stderr, "ERROR: failed to join group\n"); | |
266 | exit(1); | |
267 | } | |
268 | if (close(fds[1]) || close(fds[0])) { | |
269 | fprintf(stderr, "ERROR: closing sockets\n"); | |
270 | exit(1); | |
271 | } | |
272 | } | |
273 | ||
23a9072e WB |
274 | static int test_datapath(uint16_t typeflags, int port_off, |
275 | const int expect1[], const int expect2[]) | |
77f65ebd WB |
276 | { |
277 | const int expect0[] = { 0, 0 }; | |
23a9072e | 278 | char *rings[2]; |
95e22792 | 279 | uint8_t type = typeflags & 0xFF; |
23a9072e | 280 | int fds[2], fds_udp[2][2], ret; |
77f65ebd WB |
281 | |
282 | fprintf(stderr, "test: datapath 0x%hx\n", typeflags); | |
283 | ||
284 | fds[0] = sock_fanout_open(typeflags, 20); | |
285 | fds[1] = sock_fanout_open(typeflags, 20); | |
286 | if (fds[0] == -1 || fds[1] == -1) { | |
287 | fprintf(stderr, "ERROR: failed open\n"); | |
288 | exit(1); | |
289 | } | |
95e22792 | 290 | if (type == PACKET_FANOUT_CBPF) |
c1f8d0f9 | 291 | sock_fanout_set_cbpf(fds[0]); |
30da679e WB |
292 | else if (type == PACKET_FANOUT_EBPF) |
293 | sock_fanout_set_ebpf(fds[0]); | |
95e22792 | 294 | |
23a9072e WB |
295 | rings[0] = sock_fanout_open_ring(fds[0]); |
296 | rings[1] = sock_fanout_open_ring(fds[1]); | |
297 | pair_udp_open(fds_udp[0], PORT_BASE); | |
298 | pair_udp_open(fds_udp[1], PORT_BASE + port_off); | |
299 | sock_fanout_read(fds, rings, expect0); | |
77f65ebd WB |
300 | |
301 | /* Send data, but not enough to overflow a queue */ | |
302 | pair_udp_send(fds_udp[0], 15); | |
95e22792 | 303 | pair_udp_send_char(fds_udp[1], 5, DATA_CHAR_1); |
23a9072e | 304 | ret = sock_fanout_read(fds, rings, expect1); |
77f65ebd WB |
305 | |
306 | /* Send more data, overflow the queue */ | |
95e22792 | 307 | pair_udp_send_char(fds_udp[0], 15, DATA_CHAR_1); |
77f65ebd | 308 | /* TODO: ensure consistent order between expect1 and expect2 */ |
23a9072e | 309 | ret |= sock_fanout_read(fds, rings, expect2); |
77f65ebd | 310 | |
23a9072e WB |
311 | if (munmap(rings[1], RING_NUM_FRAMES * getpagesize()) || |
312 | munmap(rings[0], RING_NUM_FRAMES * getpagesize())) { | |
313 | fprintf(stderr, "close rings\n"); | |
314 | exit(1); | |
315 | } | |
77f65ebd WB |
316 | if (close(fds_udp[1][1]) || close(fds_udp[1][0]) || |
317 | close(fds_udp[0][1]) || close(fds_udp[0][0]) || | |
318 | close(fds[1]) || close(fds[0])) { | |
319 | fprintf(stderr, "close datapath\n"); | |
320 | exit(1); | |
321 | } | |
23a9072e WB |
322 | |
323 | return ret; | |
324 | } | |
325 | ||
326 | static int set_cpuaffinity(int cpuid) | |
327 | { | |
328 | cpu_set_t mask; | |
329 | ||
330 | CPU_ZERO(&mask); | |
331 | CPU_SET(cpuid, &mask); | |
332 | if (sched_setaffinity(0, sizeof(mask), &mask)) { | |
333 | if (errno != EINVAL) { | |
334 | fprintf(stderr, "setaffinity %d\n", cpuid); | |
335 | exit(1); | |
336 | } | |
337 | return 1; | |
338 | } | |
339 | ||
340 | return 0; | |
77f65ebd WB |
341 | } |
342 | ||
343 | int main(int argc, char **argv) | |
344 | { | |
23a9072e WB |
345 | const int expect_hash[2][2] = { { 15, 5 }, { 20, 5 } }; |
346 | const int expect_hash_rb[2][2] = { { 15, 5 }, { 20, 15 } }; | |
347 | const int expect_lb[2][2] = { { 10, 10 }, { 18, 17 } }; | |
a2ad5d2a | 348 | const int expect_rb[2][2] = { { 15, 5 }, { 20, 15 } }; |
23a9072e WB |
349 | const int expect_cpu0[2][2] = { { 20, 0 }, { 20, 0 } }; |
350 | const int expect_cpu1[2][2] = { { 0, 20 }, { 0, 20 } }; | |
95e22792 | 351 | const int expect_bpf[2][2] = { { 15, 5 }, { 15, 20 } }; |
23a9072e | 352 | int port_off = 2, tries = 5, ret; |
77f65ebd WB |
353 | |
354 | test_control_single(); | |
355 | test_control_group(); | |
356 | ||
23a9072e WB |
357 | /* find a set of ports that do not collide onto the same socket */ |
358 | ret = test_datapath(PACKET_FANOUT_HASH, port_off, | |
359 | expect_hash[0], expect_hash[1]); | |
360 | while (ret && tries--) { | |
361 | fprintf(stderr, "info: trying alternate ports (%d)\n", tries); | |
362 | ret = test_datapath(PACKET_FANOUT_HASH, ++port_off, | |
363 | expect_hash[0], expect_hash[1]); | |
364 | } | |
365 | ||
366 | ret |= test_datapath(PACKET_FANOUT_HASH | PACKET_FANOUT_FLAG_ROLLOVER, | |
367 | port_off, expect_hash_rb[0], expect_hash_rb[1]); | |
368 | ret |= test_datapath(PACKET_FANOUT_LB, | |
369 | port_off, expect_lb[0], expect_lb[1]); | |
370 | ret |= test_datapath(PACKET_FANOUT_ROLLOVER, | |
371 | port_off, expect_rb[0], expect_rb[1]); | |
30da679e | 372 | |
95e22792 WB |
373 | ret |= test_datapath(PACKET_FANOUT_CBPF, |
374 | port_off, expect_bpf[0], expect_bpf[1]); | |
30da679e WB |
375 | ret |= test_datapath(PACKET_FANOUT_EBPF, |
376 | port_off, expect_bpf[0], expect_bpf[1]); | |
23a9072e WB |
377 | |
378 | set_cpuaffinity(0); | |
379 | ret |= test_datapath(PACKET_FANOUT_CPU, port_off, | |
380 | expect_cpu0[0], expect_cpu0[1]); | |
381 | if (!set_cpuaffinity(1)) | |
382 | /* TODO: test that choice alternates with previous */ | |
383 | ret |= test_datapath(PACKET_FANOUT_CPU, port_off, | |
384 | expect_cpu1[0], expect_cpu1[1]); | |
385 | ||
386 | if (ret) | |
387 | return 1; | |
77f65ebd WB |
388 | |
389 | printf("OK. All tests passed\n"); | |
390 | return 0; | |
391 | } |