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>
54 #include <net/ethernet.h>
55 #include <netinet/ip.h>
56 #include <netinet/udp.h>
64 #include <sys/socket.h>
66 #include <sys/types.h>
69 #include "psock_lib.h"
71 #define RING_NUM_FRAMES 20
73 /* Open a socket in a given fanout mode.
74 * @return -1 if mode is bad, a valid socket otherwise */
75 static int sock_fanout_open(uint16_t typeflags
, uint16_t group_id
)
77 struct sockaddr_ll addr
= {0};
80 fd
= socket(PF_PACKET
, SOCK_RAW
, 0);
82 perror("socket packet");
86 pair_udp_setfilter(fd
);
88 addr
.sll_family
= AF_PACKET
;
89 addr
.sll_protocol
= htons(ETH_P_IP
);
90 addr
.sll_ifindex
= if_nametoindex("lo");
91 if (addr
.sll_ifindex
== 0) {
92 perror("if_nametoindex");
95 if (bind(fd
, (void *) &addr
, sizeof(addr
))) {
96 perror("bind packet");
100 val
= (((int) typeflags
) << 16) | group_id
;
101 if (setsockopt(fd
, SOL_PACKET
, PACKET_FANOUT
, &val
, sizeof(val
))) {
103 perror("close packet");
112 static void sock_fanout_set_cbpf(int fd
)
114 struct sock_filter bpf_filter
[] = {
115 BPF_STMT(BPF_LD
+BPF_B
+BPF_ABS
, 80), /* ldb [80] */
116 BPF_STMT(BPF_RET
+BPF_A
, 0), /* ret A */
118 struct sock_fprog bpf_prog
;
120 bpf_prog
.filter
= bpf_filter
;
121 bpf_prog
.len
= sizeof(bpf_filter
) / sizeof(struct sock_filter
);
123 if (setsockopt(fd
, SOL_PACKET
, PACKET_FANOUT_DATA
, &bpf_prog
,
125 perror("fanout data cbpf");
130 static void sock_fanout_getopts(int fd
, uint16_t *typeflags
, uint16_t *group_id
)
133 socklen_t sockopt_len
= sizeof(sockopt
);
135 if (getsockopt(fd
, SOL_PACKET
, PACKET_FANOUT
,
136 &sockopt
, &sockopt_len
)) {
137 perror("failed to getsockopt");
140 *typeflags
= sockopt
>> 16;
141 *group_id
= sockopt
& 0xfffff;
144 static void sock_fanout_set_ebpf(int fd
)
146 static char log_buf
[65536];
148 const int len_off
= __builtin_offsetof(struct __sk_buff
, len
);
149 struct bpf_insn prog
[] = {
150 { BPF_ALU64
| BPF_MOV
| BPF_X
, 6, 1, 0, 0 },
151 { BPF_LDX
| BPF_W
| BPF_MEM
, 0, 6, len_off
, 0 },
152 { BPF_JMP
| BPF_JGE
| BPF_K
, 0, 0, 1, DATA_LEN
},
153 { BPF_JMP
| BPF_JA
| BPF_K
, 0, 0, 4, 0 },
154 { BPF_LD
| BPF_B
| BPF_ABS
, 0, 0, 0, 0x50 },
155 { BPF_JMP
| BPF_JEQ
| BPF_K
, 0, 0, 2, DATA_CHAR
},
156 { BPF_JMP
| BPF_JEQ
| BPF_K
, 0, 0, 1, DATA_CHAR_1
},
157 { BPF_ALU
| BPF_MOV
| BPF_K
, 0, 0, 0, 0 },
158 { BPF_JMP
| BPF_EXIT
, 0, 0, 0, 0 }
163 memset(&attr
, 0, sizeof(attr
));
164 attr
.prog_type
= BPF_PROG_TYPE_SOCKET_FILTER
;
165 attr
.insns
= (unsigned long) prog
;
166 attr
.insn_cnt
= sizeof(prog
) / sizeof(prog
[0]);
167 attr
.license
= (unsigned long) "GPL";
168 attr
.log_buf
= (unsigned long) log_buf
,
169 attr
.log_size
= sizeof(log_buf
),
172 pfd
= syscall(__NR_bpf
, BPF_PROG_LOAD
, &attr
, sizeof(attr
));
175 fprintf(stderr
, "bpf verifier:\n%s\n", log_buf
);
179 if (setsockopt(fd
, SOL_PACKET
, PACKET_FANOUT_DATA
, &pfd
, sizeof(pfd
))) {
180 perror("fanout data ebpf");
185 perror("close ebpf");
190 static char *sock_fanout_open_ring(int fd
)
192 struct tpacket_req req
= {
193 .tp_block_size
= getpagesize(),
194 .tp_frame_size
= getpagesize(),
195 .tp_block_nr
= RING_NUM_FRAMES
,
196 .tp_frame_nr
= RING_NUM_FRAMES
,
199 int val
= TPACKET_V2
;
201 if (setsockopt(fd
, SOL_PACKET
, PACKET_VERSION
, (void *) &val
,
203 perror("packetsock ring setsockopt version");
206 if (setsockopt(fd
, SOL_PACKET
, PACKET_RX_RING
, (void *) &req
,
208 perror("packetsock ring setsockopt");
212 ring
= mmap(0, req
.tp_block_size
* req
.tp_block_nr
,
213 PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
214 if (ring
== MAP_FAILED
) {
215 perror("packetsock ring mmap");
222 static int sock_fanout_read_ring(int fd
, void *ring
)
224 struct tpacket2_hdr
*header
= ring
;
227 while (count
< RING_NUM_FRAMES
&& header
->tp_status
& TP_STATUS_USER
) {
229 header
= ring
+ (count
* getpagesize());
235 static int sock_fanout_read(int fds
[], char *rings
[], const int expect
[])
239 ret
[0] = sock_fanout_read_ring(fds
[0], rings
[0]);
240 ret
[1] = sock_fanout_read_ring(fds
[1], rings
[1]);
242 fprintf(stderr
, "info: count=%d,%d, expect=%d,%d\n",
243 ret
[0], ret
[1], expect
[0], expect
[1]);
245 if ((!(ret
[0] == expect
[0] && ret
[1] == expect
[1])) &&
246 (!(ret
[0] == expect
[1] && ret
[1] == expect
[0]))) {
247 fprintf(stderr
, "warning: incorrect queue lengths\n");
254 /* Test illegal mode + flag combination */
255 static void test_control_single(void)
257 fprintf(stderr
, "test: control single socket\n");
259 if (sock_fanout_open(PACKET_FANOUT_ROLLOVER
|
260 PACKET_FANOUT_FLAG_ROLLOVER
, 0) != -1) {
261 fprintf(stderr
, "ERROR: opened socket with dual rollover\n");
266 /* Test illegal group with different modes or flags */
267 static void test_control_group(void)
271 fprintf(stderr
, "test: control multiple sockets\n");
273 fds
[0] = sock_fanout_open(PACKET_FANOUT_HASH
, 0);
275 fprintf(stderr
, "ERROR: failed to open HASH socket\n");
278 if (sock_fanout_open(PACKET_FANOUT_HASH
|
279 PACKET_FANOUT_FLAG_DEFRAG
, 0) != -1) {
280 fprintf(stderr
, "ERROR: joined group with wrong flag defrag\n");
283 if (sock_fanout_open(PACKET_FANOUT_HASH
|
284 PACKET_FANOUT_FLAG_ROLLOVER
, 0) != -1) {
285 fprintf(stderr
, "ERROR: joined group with wrong flag ro\n");
288 if (sock_fanout_open(PACKET_FANOUT_CPU
, 0) != -1) {
289 fprintf(stderr
, "ERROR: joined group with wrong mode\n");
292 fds
[1] = sock_fanout_open(PACKET_FANOUT_HASH
, 0);
294 fprintf(stderr
, "ERROR: failed to join group\n");
297 if (close(fds
[1]) || close(fds
[0])) {
298 fprintf(stderr
, "ERROR: closing sockets\n");
303 /* Test creating a unique fanout group ids */
304 static void test_unique_fanout_group_ids(void)
307 uint16_t typeflags
, first_group_id
, second_group_id
;
309 fprintf(stderr
, "test: unique ids\n");
311 fds
[0] = sock_fanout_open(PACKET_FANOUT_HASH
|
312 PACKET_FANOUT_FLAG_UNIQUEID
, 0);
314 fprintf(stderr
, "ERROR: failed to create a unique id group.\n");
318 sock_fanout_getopts(fds
[0], &typeflags
, &first_group_id
);
319 if (typeflags
!= PACKET_FANOUT_HASH
) {
320 fprintf(stderr
, "ERROR: unexpected typeflags %x\n", typeflags
);
324 if (sock_fanout_open(PACKET_FANOUT_CPU
, first_group_id
) != -1) {
325 fprintf(stderr
, "ERROR: joined group with wrong type.\n");
329 fds
[1] = sock_fanout_open(PACKET_FANOUT_HASH
, first_group_id
);
332 "ERROR: failed to join previously created group.\n");
336 fds
[2] = sock_fanout_open(PACKET_FANOUT_HASH
|
337 PACKET_FANOUT_FLAG_UNIQUEID
, 0);
340 "ERROR: failed to create a second unique id group.\n");
344 sock_fanout_getopts(fds
[2], &typeflags
, &second_group_id
);
345 if (sock_fanout_open(PACKET_FANOUT_HASH
| PACKET_FANOUT_FLAG_UNIQUEID
,
346 second_group_id
) != -1) {
348 "ERROR: specified a group id when requesting unique id\n");
352 if (close(fds
[0]) || close(fds
[1]) || close(fds
[2])) {
353 fprintf(stderr
, "ERROR: closing sockets\n");
358 static int test_datapath(uint16_t typeflags
, int port_off
,
359 const int expect1
[], const int expect2
[])
361 const int expect0
[] = { 0, 0 };
363 uint8_t type
= typeflags
& 0xFF;
364 int fds
[2], fds_udp
[2][2], ret
;
366 fprintf(stderr
, "\ntest: datapath 0x%hx ports %hu,%hu\n",
367 typeflags
, PORT_BASE
, PORT_BASE
+ port_off
);
369 fds
[0] = sock_fanout_open(typeflags
, 0);
370 fds
[1] = sock_fanout_open(typeflags
, 0);
371 if (fds
[0] == -1 || fds
[1] == -1) {
372 fprintf(stderr
, "ERROR: failed open\n");
375 if (type
== PACKET_FANOUT_CBPF
)
376 sock_fanout_set_cbpf(fds
[0]);
377 else if (type
== PACKET_FANOUT_EBPF
)
378 sock_fanout_set_ebpf(fds
[0]);
380 rings
[0] = sock_fanout_open_ring(fds
[0]);
381 rings
[1] = sock_fanout_open_ring(fds
[1]);
382 pair_udp_open(fds_udp
[0], PORT_BASE
);
383 pair_udp_open(fds_udp
[1], PORT_BASE
+ port_off
);
384 sock_fanout_read(fds
, rings
, expect0
);
386 /* Send data, but not enough to overflow a queue */
387 pair_udp_send(fds_udp
[0], 15);
388 pair_udp_send_char(fds_udp
[1], 5, DATA_CHAR_1
);
389 ret
= sock_fanout_read(fds
, rings
, expect1
);
391 /* Send more data, overflow the queue */
392 pair_udp_send_char(fds_udp
[0], 15, DATA_CHAR_1
);
393 /* TODO: ensure consistent order between expect1 and expect2 */
394 ret
|= sock_fanout_read(fds
, rings
, expect2
);
396 if (munmap(rings
[1], RING_NUM_FRAMES
* getpagesize()) ||
397 munmap(rings
[0], RING_NUM_FRAMES
* getpagesize())) {
398 fprintf(stderr
, "close rings\n");
401 if (close(fds_udp
[1][1]) || close(fds_udp
[1][0]) ||
402 close(fds_udp
[0][1]) || close(fds_udp
[0][0]) ||
403 close(fds
[1]) || close(fds
[0])) {
404 fprintf(stderr
, "close datapath\n");
411 static int set_cpuaffinity(int cpuid
)
416 CPU_SET(cpuid
, &mask
);
417 if (sched_setaffinity(0, sizeof(mask
), &mask
)) {
418 if (errno
!= EINVAL
) {
419 fprintf(stderr
, "setaffinity %d\n", cpuid
);
428 int main(int argc
, char **argv
)
430 const int expect_hash
[2][2] = { { 15, 5 }, { 20, 5 } };
431 const int expect_hash_rb
[2][2] = { { 15, 5 }, { 20, 15 } };
432 const int expect_lb
[2][2] = { { 10, 10 }, { 18, 17 } };
433 const int expect_rb
[2][2] = { { 15, 5 }, { 20, 15 } };
434 const int expect_cpu0
[2][2] = { { 20, 0 }, { 20, 0 } };
435 const int expect_cpu1
[2][2] = { { 0, 20 }, { 0, 20 } };
436 const int expect_bpf
[2][2] = { { 15, 5 }, { 15, 20 } };
437 const int expect_uniqueid
[2][2] = { { 20, 20}, { 20, 20 } };
438 int port_off
= 2, tries
= 20, ret
;
440 test_control_single();
441 test_control_group();
442 test_unique_fanout_group_ids();
444 /* find a set of ports that do not collide onto the same socket */
445 ret
= test_datapath(PACKET_FANOUT_HASH
, port_off
,
446 expect_hash
[0], expect_hash
[1]);
448 fprintf(stderr
, "info: trying alternate ports (%d)\n", tries
);
449 ret
= test_datapath(PACKET_FANOUT_HASH
, ++port_off
,
450 expect_hash
[0], expect_hash
[1]);
452 fprintf(stderr
, "too many collisions\n");
457 ret
|= test_datapath(PACKET_FANOUT_HASH
| PACKET_FANOUT_FLAG_ROLLOVER
,
458 port_off
, expect_hash_rb
[0], expect_hash_rb
[1]);
459 ret
|= test_datapath(PACKET_FANOUT_LB
,
460 port_off
, expect_lb
[0], expect_lb
[1]);
461 ret
|= test_datapath(PACKET_FANOUT_ROLLOVER
,
462 port_off
, expect_rb
[0], expect_rb
[1]);
464 ret
|= test_datapath(PACKET_FANOUT_CBPF
,
465 port_off
, expect_bpf
[0], expect_bpf
[1]);
466 ret
|= test_datapath(PACKET_FANOUT_EBPF
,
467 port_off
, expect_bpf
[0], expect_bpf
[1]);
470 ret
|= test_datapath(PACKET_FANOUT_CPU
, port_off
,
471 expect_cpu0
[0], expect_cpu0
[1]);
472 if (!set_cpuaffinity(1))
473 /* TODO: test that choice alternates with previous */
474 ret
|= test_datapath(PACKET_FANOUT_CPU
, port_off
,
475 expect_cpu1
[0], expect_cpu1
[1]);
477 ret
|= test_datapath(PACKET_FANOUT_FLAG_UNIQUEID
, port_off
,
478 expect_uniqueid
[0], expect_uniqueid
[1]);
483 printf("OK. All tests passed\n");