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>
64 #include "psock_lib.h"
66 #define RING_NUM_FRAMES 20
68 /* Open a socket in a given fanout mode.
69 * @return -1 if mode is bad, a valid socket otherwise */
70 static int sock_fanout_open(uint16_t typeflags
, int num_packets
)
74 fd
= socket(PF_PACKET
, SOCK_DGRAM
, htons(ETH_P_IP
));
76 perror("socket packet");
80 /* fanout group ID is always 0: tests whether old groups are deleted */
81 val
= ((int) typeflags
) << 16;
82 if (setsockopt(fd
, SOL_PACKET
, PACKET_FANOUT
, &val
, sizeof(val
))) {
84 perror("close packet");
90 pair_udp_setfilter(fd
);
94 static char *sock_fanout_open_ring(int fd
)
96 struct tpacket_req req
= {
97 .tp_block_size
= getpagesize(),
98 .tp_frame_size
= getpagesize(),
99 .tp_block_nr
= RING_NUM_FRAMES
,
100 .tp_frame_nr
= RING_NUM_FRAMES
,
103 int val
= TPACKET_V2
;
105 if (setsockopt(fd
, SOL_PACKET
, PACKET_VERSION
, (void *) &val
,
107 perror("packetsock ring setsockopt version");
110 if (setsockopt(fd
, SOL_PACKET
, PACKET_RX_RING
, (void *) &req
,
112 perror("packetsock ring setsockopt");
116 ring
= mmap(0, req
.tp_block_size
* req
.tp_block_nr
,
117 PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
119 fprintf(stderr
, "packetsock ring mmap\n");
126 static int sock_fanout_read_ring(int fd
, void *ring
)
128 struct tpacket2_hdr
*header
= ring
;
131 while (header
->tp_status
& TP_STATUS_USER
&& count
< RING_NUM_FRAMES
) {
133 header
= ring
+ (count
* getpagesize());
139 static int sock_fanout_read(int fds
[], char *rings
[], const int expect
[])
143 ret
[0] = sock_fanout_read_ring(fds
[0], rings
[0]);
144 ret
[1] = sock_fanout_read_ring(fds
[1], rings
[1]);
146 fprintf(stderr
, "info: count=%d,%d, expect=%d,%d\n",
147 ret
[0], ret
[1], expect
[0], expect
[1]);
149 if ((!(ret
[0] == expect
[0] && ret
[1] == expect
[1])) &&
150 (!(ret
[0] == expect
[1] && ret
[1] == expect
[0]))) {
151 fprintf(stderr
, "ERROR: incorrect queue lengths\n");
158 /* Test illegal mode + flag combination */
159 static void test_control_single(void)
161 fprintf(stderr
, "test: control single socket\n");
163 if (sock_fanout_open(PACKET_FANOUT_ROLLOVER
|
164 PACKET_FANOUT_FLAG_ROLLOVER
, 0) != -1) {
165 fprintf(stderr
, "ERROR: opened socket with dual rollover\n");
170 /* Test illegal group with different modes or flags */
171 static void test_control_group(void)
175 fprintf(stderr
, "test: control multiple sockets\n");
177 fds
[0] = sock_fanout_open(PACKET_FANOUT_HASH
, 20);
179 fprintf(stderr
, "ERROR: failed to open HASH socket\n");
182 if (sock_fanout_open(PACKET_FANOUT_HASH
|
183 PACKET_FANOUT_FLAG_DEFRAG
, 10) != -1) {
184 fprintf(stderr
, "ERROR: joined group with wrong flag defrag\n");
187 if (sock_fanout_open(PACKET_FANOUT_HASH
|
188 PACKET_FANOUT_FLAG_ROLLOVER
, 10) != -1) {
189 fprintf(stderr
, "ERROR: joined group with wrong flag ro\n");
192 if (sock_fanout_open(PACKET_FANOUT_CPU
, 10) != -1) {
193 fprintf(stderr
, "ERROR: joined group with wrong mode\n");
196 fds
[1] = sock_fanout_open(PACKET_FANOUT_HASH
, 20);
198 fprintf(stderr
, "ERROR: failed to join group\n");
201 if (close(fds
[1]) || close(fds
[0])) {
202 fprintf(stderr
, "ERROR: closing sockets\n");
207 static int test_datapath(uint16_t typeflags
, int port_off
,
208 const int expect1
[], const int expect2
[])
210 const int expect0
[] = { 0, 0 };
212 int fds
[2], fds_udp
[2][2], ret
;
214 fprintf(stderr
, "test: datapath 0x%hx\n", typeflags
);
216 fds
[0] = sock_fanout_open(typeflags
, 20);
217 fds
[1] = sock_fanout_open(typeflags
, 20);
218 if (fds
[0] == -1 || fds
[1] == -1) {
219 fprintf(stderr
, "ERROR: failed open\n");
222 rings
[0] = sock_fanout_open_ring(fds
[0]);
223 rings
[1] = sock_fanout_open_ring(fds
[1]);
224 pair_udp_open(fds_udp
[0], PORT_BASE
);
225 pair_udp_open(fds_udp
[1], PORT_BASE
+ port_off
);
226 sock_fanout_read(fds
, rings
, expect0
);
228 /* Send data, but not enough to overflow a queue */
229 pair_udp_send(fds_udp
[0], 15);
230 pair_udp_send(fds_udp
[1], 5);
231 ret
= sock_fanout_read(fds
, rings
, expect1
);
233 /* Send more data, overflow the queue */
234 pair_udp_send(fds_udp
[0], 15);
235 /* TODO: ensure consistent order between expect1 and expect2 */
236 ret
|= sock_fanout_read(fds
, rings
, expect2
);
238 if (munmap(rings
[1], RING_NUM_FRAMES
* getpagesize()) ||
239 munmap(rings
[0], RING_NUM_FRAMES
* getpagesize())) {
240 fprintf(stderr
, "close rings\n");
243 if (close(fds_udp
[1][1]) || close(fds_udp
[1][0]) ||
244 close(fds_udp
[0][1]) || close(fds_udp
[0][0]) ||
245 close(fds
[1]) || close(fds
[0])) {
246 fprintf(stderr
, "close datapath\n");
253 static int set_cpuaffinity(int cpuid
)
258 CPU_SET(cpuid
, &mask
);
259 if (sched_setaffinity(0, sizeof(mask
), &mask
)) {
260 if (errno
!= EINVAL
) {
261 fprintf(stderr
, "setaffinity %d\n", cpuid
);
270 int main(int argc
, char **argv
)
272 const int expect_hash
[2][2] = { { 15, 5 }, { 20, 5 } };
273 const int expect_hash_rb
[2][2] = { { 15, 5 }, { 20, 15 } };
274 const int expect_lb
[2][2] = { { 10, 10 }, { 18, 17 } };
275 const int expect_rb
[2][2] = { { 20, 0 }, { 20, 15 } };
276 const int expect_cpu0
[2][2] = { { 20, 0 }, { 20, 0 } };
277 const int expect_cpu1
[2][2] = { { 0, 20 }, { 0, 20 } };
278 int port_off
= 2, tries
= 5, ret
;
280 test_control_single();
281 test_control_group();
283 /* find a set of ports that do not collide onto the same socket */
284 ret
= test_datapath(PACKET_FANOUT_HASH
, port_off
,
285 expect_hash
[0], expect_hash
[1]);
286 while (ret
&& tries
--) {
287 fprintf(stderr
, "info: trying alternate ports (%d)\n", tries
);
288 ret
= test_datapath(PACKET_FANOUT_HASH
, ++port_off
,
289 expect_hash
[0], expect_hash
[1]);
292 ret
|= test_datapath(PACKET_FANOUT_HASH
| PACKET_FANOUT_FLAG_ROLLOVER
,
293 port_off
, expect_hash_rb
[0], expect_hash_rb
[1]);
294 ret
|= test_datapath(PACKET_FANOUT_LB
,
295 port_off
, expect_lb
[0], expect_lb
[1]);
296 ret
|= test_datapath(PACKET_FANOUT_ROLLOVER
,
297 port_off
, expect_rb
[0], expect_rb
[1]);
300 ret
|= test_datapath(PACKET_FANOUT_CPU
, port_off
,
301 expect_cpu0
[0], expect_cpu0
[1]);
302 if (!set_cpuaffinity(1))
303 /* TODO: test that choice alternates with previous */
304 ret
|= test_datapath(PACKET_FANOUT_CPU
, port_off
,
305 expect_cpu1
[0], expect_cpu1
[1]);
310 printf("OK. All tests passed\n");