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
19 * - PACKET_FANOUT_LB
20 * - PACKET_FANOUT_CPU
21 * - PACKET_FANOUT_ROLLOVER
22 * - PACKET_FANOUT_CBPF
23 * - PACKET_FANOUT_EBPF
24 *
25 * Todo:
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
44 #define _GNU_SOURCE /* for sched_setaffinity */
45
46 #include <arpa/inet.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <linux/unistd.h> /* for __NR_bpf */
50 #include <linux/filter.h>
51 #include <linux/bpf.h>
52 #include <linux/if_packet.h>
53 #include <net/ethernet.h>
54 #include <netinet/ip.h>
55 #include <netinet/udp.h>
56 #include <poll.h>
57 #include <sched.h>
58 #include <stdint.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <sys/mman.h>
63 #include <sys/socket.h>
64 #include <sys/stat.h>
65 #include <sys/types.h>
66 #include <unistd.h>
67
68 #include "psock_lib.h"
69
70 #define RING_NUM_FRAMES 20
71
72 /* Open a socket in a given fanout mode.
73 * @return -1 if mode is bad, a valid socket otherwise */
sock_fanout_open(uint16_t typeflags,int num_packets)74 static int sock_fanout_open(uint16_t typeflags, int num_packets)
75 {
76 int fd, val;
77
78 fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
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
94 pair_udp_setfilter(fd);
95 return fd;
96 }
97
sock_fanout_set_ebpf(int fd)98 static void sock_fanout_set_ebpf(int fd)
99 {
100 static char log_buf[65536];
101
102 const int len_off = __builtin_offsetof(struct __sk_buff, len);
103 struct bpf_insn prog[] = {
104 { BPF_ALU64 | BPF_MOV | BPF_X, 6, 1, 0, 0 },
105 { BPF_LDX | BPF_W | BPF_MEM, 0, 6, len_off, 0 },
106 { BPF_JMP | BPF_JGE | BPF_K, 0, 0, 1, DATA_LEN },
107 { BPF_JMP | BPF_JA | BPF_K, 0, 0, 4, 0 },
108 { BPF_LD | BPF_B | BPF_ABS, 0, 0, 0, 0x50 },
109 { BPF_JMP | BPF_JEQ | BPF_K, 0, 0, 2, DATA_CHAR },
110 { BPF_JMP | BPF_JEQ | BPF_K, 0, 0, 1, DATA_CHAR_1 },
111 { BPF_ALU | BPF_MOV | BPF_K, 0, 0, 0, 0 },
112 { BPF_JMP | BPF_EXIT, 0, 0, 0, 0 }
113 };
114 union bpf_attr attr;
115 int pfd;
116
117 memset(&attr, 0, sizeof(attr));
118 attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
119 attr.insns = (unsigned long) prog;
120 attr.insn_cnt = sizeof(prog) / sizeof(prog[0]);
121 attr.license = (unsigned long) "GPL";
122 attr.log_buf = (unsigned long) log_buf,
123 attr.log_size = sizeof(log_buf),
124 attr.log_level = 1,
125
126 pfd = syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr));
127 if (pfd < 0) {
128 perror("bpf");
129 fprintf(stderr, "bpf verifier:\n%s\n", log_buf);
130 exit(1);
131 }
132
133 if (setsockopt(fd, SOL_PACKET, PACKET_FANOUT_DATA, &pfd, sizeof(pfd))) {
134 perror("fanout data ebpf");
135 exit(1);
136 }
137
138 if (close(pfd)) {
139 perror("close ebpf");
140 exit(1);
141 }
142 }
143
sock_fanout_open_ring(int fd)144 static char *sock_fanout_open_ring(int fd)
145 {
146 struct tpacket_req req = {
147 .tp_block_size = getpagesize(),
148 .tp_frame_size = getpagesize(),
149 .tp_block_nr = RING_NUM_FRAMES,
150 .tp_frame_nr = RING_NUM_FRAMES,
151 };
152 char *ring;
153 int val = TPACKET_V2;
154
155 if (setsockopt(fd, SOL_PACKET, PACKET_VERSION, (void *) &val,
156 sizeof(val))) {
157 perror("packetsock ring setsockopt version");
158 exit(1);
159 }
160 if (setsockopt(fd, SOL_PACKET, PACKET_RX_RING, (void *) &req,
161 sizeof(req))) {
162 perror("packetsock ring setsockopt");
163 exit(1);
164 }
165
166 ring = mmap(0, req.tp_block_size * req.tp_block_nr,
167 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
168 if (ring == MAP_FAILED) {
169 perror("packetsock ring mmap");
170 exit(1);
171 }
172
173 return ring;
174 }
175
sock_fanout_read_ring(int fd,void * ring)176 static int sock_fanout_read_ring(int fd, void *ring)
177 {
178 struct tpacket2_hdr *header = ring;
179 int count = 0;
180
181 while (count < RING_NUM_FRAMES && header->tp_status & TP_STATUS_USER) {
182 count++;
183 header = ring + (count * getpagesize());
184 }
185
186 return count;
187 }
188
sock_fanout_read(int fds[],char * rings[],const int expect[])189 static int sock_fanout_read(int fds[], char *rings[], const int expect[])
190 {
191 int ret[2];
192
193 ret[0] = sock_fanout_read_ring(fds[0], rings[0]);
194 ret[1] = sock_fanout_read_ring(fds[1], rings[1]);
195
196 fprintf(stderr, "info: count=%d,%d, expect=%d,%d\n",
197 ret[0], ret[1], expect[0], expect[1]);
198
199 if ((!(ret[0] == expect[0] && ret[1] == expect[1])) &&
200 (!(ret[0] == expect[1] && ret[1] == expect[0]))) {
201 fprintf(stderr, "ERROR: incorrect queue lengths\n");
202 return 1;
203 }
204
205 return 0;
206 }
207
208 /* Test illegal mode + flag combination */
test_control_single(void)209 static void test_control_single(void)
210 {
211 fprintf(stderr, "test: control single socket\n");
212
213 if (sock_fanout_open(PACKET_FANOUT_ROLLOVER |
214 PACKET_FANOUT_FLAG_ROLLOVER, 0) != -1) {
215 fprintf(stderr, "ERROR: opened socket with dual rollover\n");
216 exit(1);
217 }
218 }
219
220 /* Test illegal group with different modes or flags */
test_control_group(void)221 static void test_control_group(void)
222 {
223 int fds[2];
224
225 fprintf(stderr, "test: control multiple sockets\n");
226
227 fds[0] = sock_fanout_open(PACKET_FANOUT_HASH, 20);
228 if (fds[0] == -1) {
229 fprintf(stderr, "ERROR: failed to open HASH socket\n");
230 exit(1);
231 }
232 if (sock_fanout_open(PACKET_FANOUT_HASH |
233 PACKET_FANOUT_FLAG_DEFRAG, 10) != -1) {
234 fprintf(stderr, "ERROR: joined group with wrong flag defrag\n");
235 exit(1);
236 }
237 if (sock_fanout_open(PACKET_FANOUT_HASH |
238 PACKET_FANOUT_FLAG_ROLLOVER, 10) != -1) {
239 fprintf(stderr, "ERROR: joined group with wrong flag ro\n");
240 exit(1);
241 }
242 if (sock_fanout_open(PACKET_FANOUT_CPU, 10) != -1) {
243 fprintf(stderr, "ERROR: joined group with wrong mode\n");
244 exit(1);
245 }
246 fds[1] = sock_fanout_open(PACKET_FANOUT_HASH, 20);
247 if (fds[1] == -1) {
248 fprintf(stderr, "ERROR: failed to join group\n");
249 exit(1);
250 }
251 if (close(fds[1]) || close(fds[0])) {
252 fprintf(stderr, "ERROR: closing sockets\n");
253 exit(1);
254 }
255 }
256
test_datapath(uint16_t typeflags,int port_off,const int expect1[],const int expect2[])257 static int test_datapath(uint16_t typeflags, int port_off,
258 const int expect1[], const int expect2[])
259 {
260 const int expect0[] = { 0, 0 };
261 char *rings[2];
262 uint8_t type = typeflags & 0xFF;
263 int fds[2], fds_udp[2][2], ret;
264
265 fprintf(stderr, "test: datapath 0x%hx\n", typeflags);
266
267 fds[0] = sock_fanout_open(typeflags, 20);
268 fds[1] = sock_fanout_open(typeflags, 20);
269 if (fds[0] == -1 || fds[1] == -1) {
270 fprintf(stderr, "ERROR: failed open\n");
271 exit(1);
272 }
273 if (type == PACKET_FANOUT_CBPF)
274 sock_setfilter(fds[0], SOL_PACKET, PACKET_FANOUT_DATA);
275 else if (type == PACKET_FANOUT_EBPF)
276 sock_fanout_set_ebpf(fds[0]);
277
278 rings[0] = sock_fanout_open_ring(fds[0]);
279 rings[1] = sock_fanout_open_ring(fds[1]);
280 pair_udp_open(fds_udp[0], PORT_BASE);
281 pair_udp_open(fds_udp[1], PORT_BASE + port_off);
282 sock_fanout_read(fds, rings, expect0);
283
284 /* Send data, but not enough to overflow a queue */
285 pair_udp_send(fds_udp[0], 15);
286 pair_udp_send_char(fds_udp[1], 5, DATA_CHAR_1);
287 ret = sock_fanout_read(fds, rings, expect1);
288
289 /* Send more data, overflow the queue */
290 pair_udp_send_char(fds_udp[0], 15, DATA_CHAR_1);
291 /* TODO: ensure consistent order between expect1 and expect2 */
292 ret |= sock_fanout_read(fds, rings, expect2);
293
294 if (munmap(rings[1], RING_NUM_FRAMES * getpagesize()) ||
295 munmap(rings[0], RING_NUM_FRAMES * getpagesize())) {
296 fprintf(stderr, "close rings\n");
297 exit(1);
298 }
299 if (close(fds_udp[1][1]) || close(fds_udp[1][0]) ||
300 close(fds_udp[0][1]) || close(fds_udp[0][0]) ||
301 close(fds[1]) || close(fds[0])) {
302 fprintf(stderr, "close datapath\n");
303 exit(1);
304 }
305
306 return ret;
307 }
308
set_cpuaffinity(int cpuid)309 static int set_cpuaffinity(int cpuid)
310 {
311 cpu_set_t mask;
312
313 CPU_ZERO(&mask);
314 CPU_SET(cpuid, &mask);
315 if (sched_setaffinity(0, sizeof(mask), &mask)) {
316 if (errno != EINVAL) {
317 fprintf(stderr, "setaffinity %d\n", cpuid);
318 exit(1);
319 }
320 return 1;
321 }
322
323 return 0;
324 }
325
main(int argc,char ** argv)326 int main(int argc, char **argv)
327 {
328 const int expect_hash[2][2] = { { 15, 5 }, { 20, 5 } };
329 const int expect_hash_rb[2][2] = { { 15, 5 }, { 20, 15 } };
330 const int expect_lb[2][2] = { { 10, 10 }, { 18, 17 } };
331 const int expect_rb[2][2] = { { 15, 5 }, { 20, 15 } };
332 const int expect_cpu0[2][2] = { { 20, 0 }, { 20, 0 } };
333 const int expect_cpu1[2][2] = { { 0, 20 }, { 0, 20 } };
334 const int expect_bpf[2][2] = { { 15, 5 }, { 15, 20 } };
335 int port_off = 2, tries = 5, ret;
336
337 test_control_single();
338 test_control_group();
339
340 /* find a set of ports that do not collide onto the same socket */
341 ret = test_datapath(PACKET_FANOUT_HASH, port_off,
342 expect_hash[0], expect_hash[1]);
343 while (ret && tries--) {
344 fprintf(stderr, "info: trying alternate ports (%d)\n", tries);
345 ret = test_datapath(PACKET_FANOUT_HASH, ++port_off,
346 expect_hash[0], expect_hash[1]);
347 }
348
349 ret |= test_datapath(PACKET_FANOUT_HASH | PACKET_FANOUT_FLAG_ROLLOVER,
350 port_off, expect_hash_rb[0], expect_hash_rb[1]);
351 ret |= test_datapath(PACKET_FANOUT_LB,
352 port_off, expect_lb[0], expect_lb[1]);
353 ret |= test_datapath(PACKET_FANOUT_ROLLOVER,
354 port_off, expect_rb[0], expect_rb[1]);
355
356 ret |= test_datapath(PACKET_FANOUT_CBPF,
357 port_off, expect_bpf[0], expect_bpf[1]);
358 ret |= test_datapath(PACKET_FANOUT_EBPF,
359 port_off, expect_bpf[0], expect_bpf[1]);
360
361 set_cpuaffinity(0);
362 ret |= test_datapath(PACKET_FANOUT_CPU, port_off,
363 expect_cpu0[0], expect_cpu0[1]);
364 if (!set_cpuaffinity(1))
365 /* TODO: test that choice alternates with previous */
366 ret |= test_datapath(PACKET_FANOUT_CPU, port_off,
367 expect_cpu1[0], expect_cpu1[1]);
368
369 if (ret)
370 return 1;
371
372 printf("OK. All tests passed\n");
373 return 0;
374 }
375