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
24 * - functionality: PACKET_FANOUT_FLAG_DEFRAG
28 * This program is free software; you can redistribute it and/or modify it
29 * under the terms and conditions of the GNU General Public License,
30 * version 2, as published by the Free Software Foundation.
32 * This program is distributed in the hope it will be useful, but WITHOUT
33 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
34 * FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for
37 * You should have received a copy of the GNU General Public License along with
38 * this program; if not, write to the Free Software Foundation, Inc.,
39 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
42 #define _GNU_SOURCE /* for sched_setaffinity */
44 #include <arpa/inet.h>
47 #include <linux/filter.h>
48 #include <linux/if_packet.h>
49 #include <net/ethernet.h>
50 #include <netinet/ip.h>
51 #include <netinet/udp.h>
59 #include <sys/socket.h>
61 #include <sys/types.h>
66 #define RING_NUM_FRAMES 20
67 #define PORT_BASE 8000
69 static void pair_udp_open(int fds
[], uint16_t port
)
71 struct sockaddr_in saddr
, daddr
;
73 fds
[0] = socket(PF_INET
, SOCK_DGRAM
, 0);
74 fds
[1] = socket(PF_INET
, SOCK_DGRAM
, 0);
75 if (fds
[0] == -1 || fds
[1] == -1) {
76 fprintf(stderr
, "ERROR: socket dgram\n");
80 memset(&saddr
, 0, sizeof(saddr
));
81 saddr
.sin_family
= AF_INET
;
82 saddr
.sin_port
= htons(port
);
83 saddr
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
85 memset(&daddr
, 0, sizeof(daddr
));
86 daddr
.sin_family
= AF_INET
;
87 daddr
.sin_port
= htons(port
+ 1);
88 daddr
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
90 /* must bind both to get consistent hash result */
91 if (bind(fds
[1], (void *) &daddr
, sizeof(daddr
))) {
95 if (bind(fds
[0], (void *) &saddr
, sizeof(saddr
))) {
99 if (connect(fds
[0], (void *) &daddr
, sizeof(daddr
))) {
105 static void pair_udp_send(int fds
[], int num
)
107 char buf
[DATA_LEN
], rbuf
[DATA_LEN
];
109 memset(buf
, DATA_CHAR
, sizeof(buf
));
111 /* Should really handle EINTR and EAGAIN */
112 if (write(fds
[0], buf
, sizeof(buf
)) != sizeof(buf
)) {
113 fprintf(stderr
, "ERROR: send failed left=%d\n", num
);
116 if (read(fds
[1], rbuf
, sizeof(rbuf
)) != sizeof(rbuf
)) {
117 fprintf(stderr
, "ERROR: recv failed left=%d\n", num
);
120 if (memcmp(buf
, rbuf
, sizeof(buf
))) {
121 fprintf(stderr
, "ERROR: data failed left=%d\n", num
);
127 static void sock_fanout_setfilter(int fd
)
129 struct sock_filter bpf_filter
[] = {
130 { 0x80, 0, 0, 0x00000000 }, /* LD pktlen */
131 { 0x35, 0, 5, DATA_LEN
}, /* JGE DATA_LEN [f goto nomatch]*/
132 { 0x30, 0, 0, 0x00000050 }, /* LD ip[80] */
133 { 0x15, 0, 3, DATA_CHAR
}, /* JEQ DATA_CHAR [f goto nomatch]*/
134 { 0x30, 0, 0, 0x00000051 }, /* LD ip[81] */
135 { 0x15, 0, 1, DATA_CHAR
}, /* JEQ DATA_CHAR [f goto nomatch]*/
136 { 0x6, 0, 0, 0x00000060 }, /* RET match */
137 /* nomatch */ { 0x6, 0, 0, 0x00000000 }, /* RET no match */
139 struct sock_fprog bpf_prog
;
141 bpf_prog
.filter
= bpf_filter
;
142 bpf_prog
.len
= sizeof(bpf_filter
) / sizeof(struct sock_filter
);
143 if (setsockopt(fd
, SOL_SOCKET
, SO_ATTACH_FILTER
, &bpf_prog
,
145 perror("setsockopt SO_ATTACH_FILTER");
150 /* Open a socket in a given fanout mode.
151 * @return -1 if mode is bad, a valid socket otherwise */
152 static int sock_fanout_open(uint16_t typeflags
, int num_packets
)
156 fd
= socket(PF_PACKET
, SOCK_DGRAM
, htons(ETH_P_IP
));
158 perror("socket packet");
162 /* fanout group ID is always 0: tests whether old groups are deleted */
163 val
= ((int) typeflags
) << 16;
164 if (setsockopt(fd
, SOL_PACKET
, PACKET_FANOUT
, &val
, sizeof(val
))) {
166 perror("close packet");
172 sock_fanout_setfilter(fd
);
176 static char *sock_fanout_open_ring(int fd
)
178 struct tpacket_req req
= {
179 .tp_block_size
= getpagesize(),
180 .tp_frame_size
= getpagesize(),
181 .tp_block_nr
= RING_NUM_FRAMES
,
182 .tp_frame_nr
= RING_NUM_FRAMES
,
186 if (setsockopt(fd
, SOL_PACKET
, PACKET_RX_RING
, (void *) &req
,
188 perror("packetsock ring setsockopt");
192 ring
= mmap(0, req
.tp_block_size
* req
.tp_block_nr
,
193 PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
195 fprintf(stderr
, "packetsock ring mmap\n");
202 static int sock_fanout_read_ring(int fd
, void *ring
)
204 struct tpacket_hdr
*header
= ring
;
207 while (header
->tp_status
& TP_STATUS_USER
&& count
< RING_NUM_FRAMES
) {
209 header
= ring
+ (count
* getpagesize());
215 static int sock_fanout_read(int fds
[], char *rings
[], const int expect
[])
219 ret
[0] = sock_fanout_read_ring(fds
[0], rings
[0]);
220 ret
[1] = sock_fanout_read_ring(fds
[1], rings
[1]);
222 fprintf(stderr
, "info: count=%d,%d, expect=%d,%d\n",
223 ret
[0], ret
[1], expect
[0], expect
[1]);
225 if ((!(ret
[0] == expect
[0] && ret
[1] == expect
[1])) &&
226 (!(ret
[0] == expect
[1] && ret
[1] == expect
[0]))) {
227 fprintf(stderr
, "ERROR: incorrect queue lengths\n");
234 /* Test illegal mode + flag combination */
235 static void test_control_single(void)
237 fprintf(stderr
, "test: control single socket\n");
239 if (sock_fanout_open(PACKET_FANOUT_ROLLOVER
|
240 PACKET_FANOUT_FLAG_ROLLOVER
, 0) != -1) {
241 fprintf(stderr
, "ERROR: opened socket with dual rollover\n");
246 /* Test illegal group with different modes or flags */
247 static void test_control_group(void)
251 fprintf(stderr
, "test: control multiple sockets\n");
253 fds
[0] = sock_fanout_open(PACKET_FANOUT_HASH
, 20);
255 fprintf(stderr
, "ERROR: failed to open HASH socket\n");
258 if (sock_fanout_open(PACKET_FANOUT_HASH
|
259 PACKET_FANOUT_FLAG_DEFRAG
, 10) != -1) {
260 fprintf(stderr
, "ERROR: joined group with wrong flag defrag\n");
263 if (sock_fanout_open(PACKET_FANOUT_HASH
|
264 PACKET_FANOUT_FLAG_ROLLOVER
, 10) != -1) {
265 fprintf(stderr
, "ERROR: joined group with wrong flag ro\n");
268 if (sock_fanout_open(PACKET_FANOUT_CPU
, 10) != -1) {
269 fprintf(stderr
, "ERROR: joined group with wrong mode\n");
272 fds
[1] = sock_fanout_open(PACKET_FANOUT_HASH
, 20);
274 fprintf(stderr
, "ERROR: failed to join group\n");
277 if (close(fds
[1]) || close(fds
[0])) {
278 fprintf(stderr
, "ERROR: closing sockets\n");
283 static int test_datapath(uint16_t typeflags
, int port_off
,
284 const int expect1
[], const int expect2
[])
286 const int expect0
[] = { 0, 0 };
288 int fds
[2], fds_udp
[2][2], ret
;
290 fprintf(stderr
, "test: datapath 0x%hx\n", typeflags
);
292 fds
[0] = sock_fanout_open(typeflags
, 20);
293 fds
[1] = sock_fanout_open(typeflags
, 20);
294 if (fds
[0] == -1 || fds
[1] == -1) {
295 fprintf(stderr
, "ERROR: failed open\n");
298 rings
[0] = sock_fanout_open_ring(fds
[0]);
299 rings
[1] = sock_fanout_open_ring(fds
[1]);
300 pair_udp_open(fds_udp
[0], PORT_BASE
);
301 pair_udp_open(fds_udp
[1], PORT_BASE
+ port_off
);
302 sock_fanout_read(fds
, rings
, expect0
);
304 /* Send data, but not enough to overflow a queue */
305 pair_udp_send(fds_udp
[0], 15);
306 pair_udp_send(fds_udp
[1], 5);
307 ret
= sock_fanout_read(fds
, rings
, expect1
);
309 /* Send more data, overflow the queue */
310 pair_udp_send(fds_udp
[0], 15);
311 /* TODO: ensure consistent order between expect1 and expect2 */
312 ret
|= sock_fanout_read(fds
, rings
, expect2
);
314 if (munmap(rings
[1], RING_NUM_FRAMES
* getpagesize()) ||
315 munmap(rings
[0], RING_NUM_FRAMES
* getpagesize())) {
316 fprintf(stderr
, "close rings\n");
319 if (close(fds_udp
[1][1]) || close(fds_udp
[1][0]) ||
320 close(fds_udp
[0][1]) || close(fds_udp
[0][0]) ||
321 close(fds
[1]) || close(fds
[0])) {
322 fprintf(stderr
, "close datapath\n");
329 static int set_cpuaffinity(int cpuid
)
334 CPU_SET(cpuid
, &mask
);
335 if (sched_setaffinity(0, sizeof(mask
), &mask
)) {
336 if (errno
!= EINVAL
) {
337 fprintf(stderr
, "setaffinity %d\n", cpuid
);
346 int main(int argc
, char **argv
)
348 const int expect_hash
[2][2] = { { 15, 5 }, { 20, 5 } };
349 const int expect_hash_rb
[2][2] = { { 15, 5 }, { 20, 15 } };
350 const int expect_lb
[2][2] = { { 10, 10 }, { 18, 17 } };
351 const int expect_rb
[2][2] = { { 20, 0 }, { 20, 15 } };
352 const int expect_cpu0
[2][2] = { { 20, 0 }, { 20, 0 } };
353 const int expect_cpu1
[2][2] = { { 0, 20 }, { 0, 20 } };
354 int port_off
= 2, tries
= 5, ret
;
356 test_control_single();
357 test_control_group();
359 /* find a set of ports that do not collide onto the same socket */
360 ret
= test_datapath(PACKET_FANOUT_HASH
, port_off
,
361 expect_hash
[0], expect_hash
[1]);
362 while (ret
&& tries
--) {
363 fprintf(stderr
, "info: trying alternate ports (%d)\n", tries
);
364 ret
= test_datapath(PACKET_FANOUT_HASH
, ++port_off
,
365 expect_hash
[0], expect_hash
[1]);
368 ret
|= test_datapath(PACKET_FANOUT_HASH
| PACKET_FANOUT_FLAG_ROLLOVER
,
369 port_off
, expect_hash_rb
[0], expect_hash_rb
[1]);
370 ret
|= test_datapath(PACKET_FANOUT_LB
,
371 port_off
, expect_lb
[0], expect_lb
[1]);
372 ret
|= test_datapath(PACKET_FANOUT_ROLLOVER
,
373 port_off
, expect_rb
[0], expect_rb
[1]);
376 ret
|= test_datapath(PACKET_FANOUT_CPU
, port_off
,
377 expect_cpu0
[0], expect_cpu0
[1]);
378 if (!set_cpuaffinity(1))
379 /* TODO: test that choice alternates with previous */
380 ret
|= test_datapath(PACKET_FANOUT_CPU
, port_off
,
381 expect_cpu1
[0], expect_cpu1
[1]);
386 printf("OK. All tests passed\n");