2 * Copyright 2013 Google Inc.
3 * Author: Willem de Bruijn (willemb@google.com)
5 * A basic test of packet socket fanout behavior.
8 * - create fanout fails as expected with illegal flag combinations
9 * - join fanout fails as expected with diverging types or flags
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.
16 * The test currently runs for
17 * - PACKET_FANOUT_HASH
18 * - PACKET_FANOUT_HASH with PACKET_FANOUT_FLAG_ROLLOVER
21 * - PACKET_FANOUT_ROLLOVER
22 * - PACKET_FANOUT_CBPF
23 * - PACKET_FANOUT_EBPF
26 * - functionality: PACKET_FANOUT_FLAG_DEFRAG
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.
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
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.
44 #define _GNU_SOURCE /* for sched_setaffinity */
46 #include <arpa/inet.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>
63 #include <sys/socket.h>
65 #include <sys/types.h>
68 #include "psock_lib.h"
70 #define RING_NUM_FRAMES 20
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
)
78 fd
= socket(PF_PACKET
, SOCK_DGRAM
, htons(ETH_P_IP
));
80 perror("socket packet");
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
))) {
88 perror("close packet");
94 pair_udp_setfilter(fd
);
98 static void sock_fanout_set_ebpf(int fd
)
100 const int len_off
= __builtin_offsetof(struct __sk_buff
, len
);
101 struct bpf_insn prog
[] = {
102 { BPF_ALU64
| BPF_MOV
| BPF_X
, 6, 1, 0, 0 },
103 { BPF_LDX
| BPF_W
| BPF_MEM
, 0, 6, len_off
, 0 },
104 { BPF_JMP
| BPF_JGE
| BPF_K
, 0, 0, 1, DATA_LEN
},
105 { BPF_JMP
| BPF_JA
| BPF_K
, 0, 0, 4, 0 },
106 { BPF_LD
| BPF_B
| BPF_ABS
, 0, 0, 0, 0x50 },
107 { BPF_JMP
| BPF_JEQ
| BPF_K
, 0, 0, 2, DATA_CHAR
},
108 { BPF_JMP
| BPF_JEQ
| BPF_K
, 0, 0, 1, DATA_CHAR_1
},
109 { BPF_ALU
| BPF_MOV
| BPF_K
, 0, 0, 0, 0 },
110 { BPF_JMP
| BPF_EXIT
, 0, 0, 0, 0 }
116 memset(&attr
, 0, sizeof(attr
));
117 attr
.prog_type
= BPF_PROG_TYPE_SOCKET_FILTER
;
118 attr
.insns
= (unsigned long) prog
;
119 attr
.insn_cnt
= sizeof(prog
) / sizeof(prog
[0]);
120 attr
.license
= (unsigned long) "GPL";
121 attr
.log_buf
= (unsigned long) log_buf
,
122 attr
.log_size
= sizeof(log_buf
),
125 pfd
= syscall(__NR_bpf
, BPF_PROG_LOAD
, &attr
, sizeof(attr
));
128 fprintf(stderr
, "bpf verifier:\n%s\n", log_buf
);
132 if (setsockopt(fd
, SOL_PACKET
, PACKET_FANOUT_DATA
, &pfd
, sizeof(pfd
))) {
133 perror("fanout data ebpf");
138 perror("close ebpf");
143 static char *sock_fanout_open_ring(int fd
)
145 struct tpacket_req req
= {
146 .tp_block_size
= getpagesize(),
147 .tp_frame_size
= getpagesize(),
148 .tp_block_nr
= RING_NUM_FRAMES
,
149 .tp_frame_nr
= RING_NUM_FRAMES
,
152 int val
= TPACKET_V2
;
154 if (setsockopt(fd
, SOL_PACKET
, PACKET_VERSION
, (void *) &val
,
156 perror("packetsock ring setsockopt version");
159 if (setsockopt(fd
, SOL_PACKET
, PACKET_RX_RING
, (void *) &req
,
161 perror("packetsock ring setsockopt");
165 ring
= mmap(0, req
.tp_block_size
* req
.tp_block_nr
,
166 PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
167 if (ring
== MAP_FAILED
) {
168 perror("packetsock ring mmap");
175 static int sock_fanout_read_ring(int fd
, void *ring
)
177 struct tpacket2_hdr
*header
= ring
;
180 while (count
< RING_NUM_FRAMES
&& header
->tp_status
& TP_STATUS_USER
) {
182 header
= ring
+ (count
* getpagesize());
188 static int sock_fanout_read(int fds
[], char *rings
[], const int expect
[])
192 ret
[0] = sock_fanout_read_ring(fds
[0], rings
[0]);
193 ret
[1] = sock_fanout_read_ring(fds
[1], rings
[1]);
195 fprintf(stderr
, "info: count=%d,%d, expect=%d,%d\n",
196 ret
[0], ret
[1], expect
[0], expect
[1]);
198 if ((!(ret
[0] == expect
[0] && ret
[1] == expect
[1])) &&
199 (!(ret
[0] == expect
[1] && ret
[1] == expect
[0]))) {
200 fprintf(stderr
, "ERROR: incorrect queue lengths\n");
207 /* Test illegal mode + flag combination */
208 static void test_control_single(void)
210 fprintf(stderr
, "test: control single socket\n");
212 if (sock_fanout_open(PACKET_FANOUT_ROLLOVER
|
213 PACKET_FANOUT_FLAG_ROLLOVER
, 0) != -1) {
214 fprintf(stderr
, "ERROR: opened socket with dual rollover\n");
219 /* Test illegal group with different modes or flags */
220 static void test_control_group(void)
224 fprintf(stderr
, "test: control multiple sockets\n");
226 fds
[0] = sock_fanout_open(PACKET_FANOUT_HASH
, 20);
228 fprintf(stderr
, "ERROR: failed to open HASH socket\n");
231 if (sock_fanout_open(PACKET_FANOUT_HASH
|
232 PACKET_FANOUT_FLAG_DEFRAG
, 10) != -1) {
233 fprintf(stderr
, "ERROR: joined group with wrong flag defrag\n");
236 if (sock_fanout_open(PACKET_FANOUT_HASH
|
237 PACKET_FANOUT_FLAG_ROLLOVER
, 10) != -1) {
238 fprintf(stderr
, "ERROR: joined group with wrong flag ro\n");
241 if (sock_fanout_open(PACKET_FANOUT_CPU
, 10) != -1) {
242 fprintf(stderr
, "ERROR: joined group with wrong mode\n");
245 fds
[1] = sock_fanout_open(PACKET_FANOUT_HASH
, 20);
247 fprintf(stderr
, "ERROR: failed to join group\n");
250 if (close(fds
[1]) || close(fds
[0])) {
251 fprintf(stderr
, "ERROR: closing sockets\n");
256 static int test_datapath(uint16_t typeflags
, int port_off
,
257 const int expect1
[], const int expect2
[])
259 const int expect0
[] = { 0, 0 };
261 uint8_t type
= typeflags
& 0xFF;
262 int fds
[2], fds_udp
[2][2], ret
;
264 fprintf(stderr
, "test: datapath 0x%hx\n", typeflags
);
266 fds
[0] = sock_fanout_open(typeflags
, 20);
267 fds
[1] = sock_fanout_open(typeflags
, 20);
268 if (fds
[0] == -1 || fds
[1] == -1) {
269 fprintf(stderr
, "ERROR: failed open\n");
272 if (type
== PACKET_FANOUT_CBPF
)
273 sock_setfilter(fds
[0], SOL_PACKET
, PACKET_FANOUT_DATA
);
274 else if (type
== PACKET_FANOUT_EBPF
)
275 sock_fanout_set_ebpf(fds
[0]);
277 rings
[0] = sock_fanout_open_ring(fds
[0]);
278 rings
[1] = sock_fanout_open_ring(fds
[1]);
279 pair_udp_open(fds_udp
[0], PORT_BASE
);
280 pair_udp_open(fds_udp
[1], PORT_BASE
+ port_off
);
281 sock_fanout_read(fds
, rings
, expect0
);
283 /* Send data, but not enough to overflow a queue */
284 pair_udp_send(fds_udp
[0], 15);
285 pair_udp_send_char(fds_udp
[1], 5, DATA_CHAR_1
);
286 ret
= sock_fanout_read(fds
, rings
, expect1
);
288 /* Send more data, overflow the queue */
289 pair_udp_send_char(fds_udp
[0], 15, DATA_CHAR_1
);
290 /* TODO: ensure consistent order between expect1 and expect2 */
291 ret
|= sock_fanout_read(fds
, rings
, expect2
);
293 if (munmap(rings
[1], RING_NUM_FRAMES
* getpagesize()) ||
294 munmap(rings
[0], RING_NUM_FRAMES
* getpagesize())) {
295 fprintf(stderr
, "close rings\n");
298 if (close(fds_udp
[1][1]) || close(fds_udp
[1][0]) ||
299 close(fds_udp
[0][1]) || close(fds_udp
[0][0]) ||
300 close(fds
[1]) || close(fds
[0])) {
301 fprintf(stderr
, "close datapath\n");
308 static int set_cpuaffinity(int cpuid
)
313 CPU_SET(cpuid
, &mask
);
314 if (sched_setaffinity(0, sizeof(mask
), &mask
)) {
315 if (errno
!= EINVAL
) {
316 fprintf(stderr
, "setaffinity %d\n", cpuid
);
325 int main(int argc
, char **argv
)
327 const int expect_hash
[2][2] = { { 15, 5 }, { 20, 5 } };
328 const int expect_hash_rb
[2][2] = { { 15, 5 }, { 20, 15 } };
329 const int expect_lb
[2][2] = { { 10, 10 }, { 18, 17 } };
330 const int expect_rb
[2][2] = { { 15, 5 }, { 20, 15 } };
331 const int expect_cpu0
[2][2] = { { 20, 0 }, { 20, 0 } };
332 const int expect_cpu1
[2][2] = { { 0, 20 }, { 0, 20 } };
333 const int expect_bpf
[2][2] = { { 15, 5 }, { 15, 20 } };
334 int port_off
= 2, tries
= 5, ret
;
336 test_control_single();
337 test_control_group();
339 /* find a set of ports that do not collide onto the same socket */
340 ret
= test_datapath(PACKET_FANOUT_HASH
, port_off
,
341 expect_hash
[0], expect_hash
[1]);
342 while (ret
&& tries
--) {
343 fprintf(stderr
, "info: trying alternate ports (%d)\n", tries
);
344 ret
= test_datapath(PACKET_FANOUT_HASH
, ++port_off
,
345 expect_hash
[0], expect_hash
[1]);
348 ret
|= test_datapath(PACKET_FANOUT_HASH
| PACKET_FANOUT_FLAG_ROLLOVER
,
349 port_off
, expect_hash_rb
[0], expect_hash_rb
[1]);
350 ret
|= test_datapath(PACKET_FANOUT_LB
,
351 port_off
, expect_lb
[0], expect_lb
[1]);
352 ret
|= test_datapath(PACKET_FANOUT_ROLLOVER
,
353 port_off
, expect_rb
[0], expect_rb
[1]);
355 ret
|= test_datapath(PACKET_FANOUT_CBPF
,
356 port_off
, expect_bpf
[0], expect_bpf
[1]);
357 ret
|= test_datapath(PACKET_FANOUT_EBPF
,
358 port_off
, expect_bpf
[0], expect_bpf
[1]);
361 ret
|= test_datapath(PACKET_FANOUT_CPU
, port_off
,
362 expect_cpu0
[0], expect_cpu0
[1]);
363 if (!set_cpuaffinity(1))
364 /* TODO: test that choice alternates with previous */
365 ret
|= test_datapath(PACKET_FANOUT_CPU
, port_off
,
366 expect_cpu1
[0], expect_cpu1
[1]);
371 printf("OK. All tests passed\n");