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
19 * - PACKET_FANOUT_ROLLOVER
22 * - datapath: PACKET_FANOUT_LB
23 * - datapath: PACKET_FANOUT_CPU
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 #include <arpa/inet.h>
44 #include <linux/filter.h>
45 #include <linux/if_packet.h>
46 #include <net/ethernet.h>
47 #include <netinet/ip.h>
48 #include <netinet/udp.h>
54 #include <sys/socket.h>
56 #include <sys/types.h>
59 /* Hack: build even if local includes are old */
61 #define PACKET_FANOUT 18
62 #define PACKET_FANOUT_HASH 0
63 #define PACKET_FANOUT_LB 1
64 #define PACKET_FANOUT_CPU 2
65 #define PACKET_FANOUT_FLAG_DEFRAG 0x8000
67 #ifndef PACKET_FANOUT_ROLLOVER
68 #define PACKET_FANOUT_ROLLOVER 3
71 #ifndef PACKET_FANOUT_FLAG_ROLLOVER
72 #define PACKET_FANOUT_FLAG_ROLLOVER 0x1000
80 static void pair_udp_open(int fds
[], uint16_t port
)
82 struct sockaddr_in saddr
, daddr
;
84 fds
[0] = socket(PF_INET
, SOCK_DGRAM
, 0);
85 fds
[1] = socket(PF_INET
, SOCK_DGRAM
, 0);
86 if (fds
[0] == -1 || fds
[1] == -1) {
87 fprintf(stderr
, "ERROR: socket dgram\n");
91 memset(&saddr
, 0, sizeof(saddr
));
92 saddr
.sin_family
= AF_INET
;
93 saddr
.sin_port
= htons(port
);
94 saddr
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
96 memset(&daddr
, 0, sizeof(daddr
));
97 daddr
.sin_family
= AF_INET
;
98 daddr
.sin_port
= htons(port
+ 1);
99 daddr
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
101 /* must bind both to get consistent hash result */
102 if (bind(fds
[1], (void *) &daddr
, sizeof(daddr
))) {
106 if (bind(fds
[0], (void *) &saddr
, sizeof(saddr
))) {
110 if (connect(fds
[0], (void *) &daddr
, sizeof(daddr
))) {
116 static void pair_udp_send(int fds
[], int num
)
118 char buf
[DATA_LEN
], rbuf
[DATA_LEN
];
120 memset(buf
, DATA_CHAR
, sizeof(buf
));
122 /* Should really handle EINTR and EAGAIN */
123 if (write(fds
[0], buf
, sizeof(buf
)) != sizeof(buf
)) {
124 fprintf(stderr
, "ERROR: send failed left=%d\n", num
);
127 if (read(fds
[1], rbuf
, sizeof(rbuf
)) != sizeof(rbuf
)) {
128 fprintf(stderr
, "ERROR: recv failed left=%d\n", num
);
131 if (memcmp(buf
, rbuf
, sizeof(buf
))) {
132 fprintf(stderr
, "ERROR: data failed left=%d\n", num
);
138 static void sock_fanout_setfilter(int fd
)
140 struct sock_filter bpf_filter
[] = {
141 { 0x80, 0, 0, 0x00000000 }, /* LD pktlen */
142 { 0x35, 0, 5, DATA_LEN
}, /* JGE DATA_LEN [f goto nomatch]*/
143 { 0x30, 0, 0, 0x00000050 }, /* LD ip[80] */
144 { 0x15, 0, 3, DATA_CHAR
}, /* JEQ DATA_CHAR [f goto nomatch]*/
145 { 0x30, 0, 0, 0x00000051 }, /* LD ip[81] */
146 { 0x15, 0, 1, DATA_CHAR
}, /* JEQ DATA_CHAR [f goto nomatch]*/
147 { 0x6, 0, 0, 0x00000060 }, /* RET match */
148 /* nomatch */ { 0x6, 0, 0, 0x00000000 }, /* RET no match */
150 struct sock_fprog bpf_prog
;
152 bpf_prog
.filter
= bpf_filter
;
153 bpf_prog
.len
= sizeof(bpf_filter
) / sizeof(struct sock_filter
);
154 if (setsockopt(fd
, SOL_SOCKET
, SO_ATTACH_FILTER
, &bpf_prog
,
156 perror("setsockopt SO_ATTACH_FILTER");
161 /* Open a socket in a given fanout mode.
162 * @return -1 if mode is bad, a valid socket otherwise */
163 static int sock_fanout_open(uint16_t typeflags
, int num_packets
)
167 fd
= socket(PF_PACKET
, SOCK_DGRAM
, htons(ETH_P_IP
));
169 perror("socket packet");
173 /* fanout group ID is always 0: tests whether old groups are deleted */
174 val
= ((int) typeflags
) << 16;
175 if (setsockopt(fd
, SOL_PACKET
, PACKET_FANOUT
, &val
, sizeof(val
))) {
177 perror("close packet");
183 val
= sizeof(struct iphdr
) + sizeof(struct udphdr
) + DATA_LEN
;
185 /* hack: apparently, the above calculation is too small (TODO: fix) */
187 if (setsockopt(fd
, SOL_SOCKET
, SO_RCVBUF
, &val
, sizeof(val
))) {
188 perror("setsockopt SO_RCVBUF");
192 sock_fanout_setfilter(fd
);
196 static void sock_fanout_read(int fds
[], const int expect
[])
198 struct tpacket_stats stats
;
202 ssize
= sizeof(stats
);
203 if (getsockopt(fds
[0], SOL_PACKET
, PACKET_STATISTICS
, &stats
, &ssize
)) {
204 perror("getsockopt statistics 0");
207 ret
[0] = stats
.tp_packets
- stats
.tp_drops
;
208 ssize
= sizeof(stats
);
209 if (getsockopt(fds
[1], SOL_PACKET
, PACKET_STATISTICS
, &stats
, &ssize
)) {
210 perror("getsockopt statistics 1");
213 ret
[1] = stats
.tp_packets
- stats
.tp_drops
;
215 fprintf(stderr
, "info: count=%d,%d, expect=%d,%d\n",
216 ret
[0], ret
[1], expect
[0], expect
[1]);
218 if ((!(ret
[0] == expect
[0] && ret
[1] == expect
[1])) &&
219 (!(ret
[0] == expect
[1] && ret
[1] == expect
[0]))) {
220 fprintf(stderr
, "ERROR: incorrect queue lengths\n");
225 /* Test illegal mode + flag combination */
226 static void test_control_single(void)
228 fprintf(stderr
, "test: control single socket\n");
230 if (sock_fanout_open(PACKET_FANOUT_ROLLOVER
|
231 PACKET_FANOUT_FLAG_ROLLOVER
, 0) != -1) {
232 fprintf(stderr
, "ERROR: opened socket with dual rollover\n");
237 /* Test illegal group with different modes or flags */
238 static void test_control_group(void)
242 fprintf(stderr
, "test: control multiple sockets\n");
244 fds
[0] = sock_fanout_open(PACKET_FANOUT_HASH
, 20);
246 fprintf(stderr
, "ERROR: failed to open HASH socket\n");
249 if (sock_fanout_open(PACKET_FANOUT_HASH
|
250 PACKET_FANOUT_FLAG_DEFRAG
, 10) != -1) {
251 fprintf(stderr
, "ERROR: joined group with wrong flag defrag\n");
254 if (sock_fanout_open(PACKET_FANOUT_HASH
|
255 PACKET_FANOUT_FLAG_ROLLOVER
, 10) != -1) {
256 fprintf(stderr
, "ERROR: joined group with wrong flag ro\n");
259 if (sock_fanout_open(PACKET_FANOUT_CPU
, 10) != -1) {
260 fprintf(stderr
, "ERROR: joined group with wrong mode\n");
263 fds
[1] = sock_fanout_open(PACKET_FANOUT_HASH
, 20);
265 fprintf(stderr
, "ERROR: failed to join group\n");
268 if (close(fds
[1]) || close(fds
[0])) {
269 fprintf(stderr
, "ERROR: closing sockets\n");
274 static void test_datapath(uint16_t typeflags
,
275 const int expect1
[], const int expect2
[])
277 const int expect0
[] = { 0, 0 };
278 int fds
[2], fds_udp
[2][2];
280 fprintf(stderr
, "test: datapath 0x%hx\n", typeflags
);
282 fds
[0] = sock_fanout_open(typeflags
, 20);
283 fds
[1] = sock_fanout_open(typeflags
, 20);
284 if (fds
[0] == -1 || fds
[1] == -1) {
285 fprintf(stderr
, "ERROR: failed open\n");
288 pair_udp_open(fds_udp
[0], 8000);
289 pair_udp_open(fds_udp
[1], 8002);
290 sock_fanout_read(fds
, expect0
);
292 /* Send data, but not enough to overflow a queue */
293 pair_udp_send(fds_udp
[0], 15);
294 pair_udp_send(fds_udp
[1], 5);
295 sock_fanout_read(fds
, expect1
);
297 /* Send more data, overflow the queue */
298 pair_udp_send(fds_udp
[0], 15);
299 /* TODO: ensure consistent order between expect1 and expect2 */
300 sock_fanout_read(fds
, expect2
);
302 if (close(fds_udp
[1][1]) || close(fds_udp
[1][0]) ||
303 close(fds_udp
[0][1]) || close(fds_udp
[0][0]) ||
304 close(fds
[1]) || close(fds
[0])) {
305 fprintf(stderr
, "close datapath\n");
310 int main(int argc
, char **argv
)
312 const int expect_hash
[2][2] = { { 15, 5 }, { 5, 0 } };
313 const int expect_hash_rb
[2][2] = { { 15, 5 }, { 5, 10 } };
314 const int expect_rb
[2][2] = { { 20, 0 }, { 0, 15 } };
316 test_control_single();
317 test_control_group();
319 test_datapath(PACKET_FANOUT_HASH
, expect_hash
[0], expect_hash
[1]);
320 test_datapath(PACKET_FANOUT_HASH
| PACKET_FANOUT_FLAG_ROLLOVER
,
321 expect_hash_rb
[0], expect_hash_rb
[1]);
322 test_datapath(PACKET_FANOUT_ROLLOVER
, expect_rb
[0], expect_rb
[1]);
324 printf("OK. All tests passed\n");