net: fix psock_fanout selftest hash collision
[cascardo/linux.git] / tools / testing / selftests / net-afpacket / psock_fanout.c
1 /*
2  * Copyright 2013 Google Inc.
3  * Author: Willem de Bruijn (willemb@google.com)
4  *
5  * A basic test of packet socket fanout behavior.
6  *
7  * Control:
8  * - create fanout fails as expected with illegal flag combinations
9  * - join   fanout fails as expected with diverging types or flags
10  *
11  * Datapath:
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.
15  *
16  *   The test currently runs for
17  *   - PACKET_FANOUT_HASH
18  *   - PACKET_FANOUT_HASH with PACKET_FANOUT_FLAG_ROLLOVER
19  *   - PACKET_FANOUT_LB
20  *   - PACKET_FANOUT_CPU
21  *   - PACKET_FANOUT_ROLLOVER
22  *
23  * Todo:
24  * - functionality: PACKET_FANOUT_FLAG_DEFRAG
25  *
26  * License (GPLv2):
27  *
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.
31  *
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
35  * more details.
36  *
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.
40  */
41
42 #define _GNU_SOURCE             /* for sched_setaffinity */
43
44 #include <arpa/inet.h>
45 #include <errno.h>
46 #include <fcntl.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>
52 #include <poll.h>
53 #include <sched.h>
54 #include <stdint.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <sys/mman.h>
59 #include <sys/socket.h>
60 #include <sys/stat.h>
61 #include <sys/types.h>
62 #include <unistd.h>
63
64 #define DATA_LEN                        100
65 #define DATA_CHAR                       'a'
66 #define RING_NUM_FRAMES                 20
67 #define PORT_BASE                       8000
68
69 static void pair_udp_open(int fds[], uint16_t port)
70 {
71         struct sockaddr_in saddr, daddr;
72
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");
77                 exit(1);
78         }
79
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);
84
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);
89
90         /* must bind both to get consistent hash result */
91         if (bind(fds[1], (void *) &daddr, sizeof(daddr))) {
92                 perror("bind");
93                 exit(1);
94         }
95         if (bind(fds[0], (void *) &saddr, sizeof(saddr))) {
96                 perror("bind");
97                 exit(1);
98         }
99         if (connect(fds[0], (void *) &daddr, sizeof(daddr))) {
100                 perror("bind");
101                 exit(1);
102         }
103 }
104
105 static void pair_udp_send(int fds[], int num)
106 {
107         char buf[DATA_LEN], rbuf[DATA_LEN];
108
109         memset(buf, DATA_CHAR, sizeof(buf));
110         while (num--) {
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);
114                         exit(1);
115                 }
116                 if (read(fds[1], rbuf, sizeof(rbuf)) != sizeof(rbuf)) {
117                         fprintf(stderr, "ERROR: recv failed left=%d\n", num);
118                         exit(1);
119                 }
120                 if (memcmp(buf, rbuf, sizeof(buf))) {
121                         fprintf(stderr, "ERROR: data failed left=%d\n", num);
122                         exit(1);
123                 }
124         }
125 }
126
127 static void sock_fanout_setfilter(int fd)
128 {
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                  */
138         };
139         struct sock_fprog bpf_prog;
140
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,
144                        sizeof(bpf_prog))) {
145                 perror("setsockopt SO_ATTACH_FILTER");
146                 exit(1);
147         }
148 }
149
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)
153 {
154         int fd, val;
155
156         fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
157         if (fd < 0) {
158                 perror("socket packet");
159                 exit(1);
160         }
161
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))) {
165                 if (close(fd)) {
166                         perror("close packet");
167                         exit(1);
168                 }
169                 return -1;
170         }
171
172         sock_fanout_setfilter(fd);
173         return fd;
174 }
175
176 static char *sock_fanout_open_ring(int fd)
177 {
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,
183         };
184         char *ring;
185
186         if (setsockopt(fd, SOL_PACKET, PACKET_RX_RING, (void *) &req,
187                        sizeof(req))) {
188                 perror("packetsock ring setsockopt");
189                 exit(1);
190         }
191
192         ring = mmap(0, req.tp_block_size * req.tp_block_nr,
193                     PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
194         if (!ring) {
195                 fprintf(stderr, "packetsock ring mmap\n");
196                 exit(1);
197         }
198
199         return ring;
200 }
201
202 static int sock_fanout_read_ring(int fd, void *ring)
203 {
204         struct tpacket_hdr *header = ring;
205         int count = 0;
206
207         while (header->tp_status & TP_STATUS_USER && count < RING_NUM_FRAMES) {
208                 count++;
209                 header = ring + (count * getpagesize());
210         }
211
212         return count;
213 }
214
215 static int sock_fanout_read(int fds[], char *rings[], const int expect[])
216 {
217         int ret[2];
218
219         ret[0] = sock_fanout_read_ring(fds[0], rings[0]);
220         ret[1] = sock_fanout_read_ring(fds[1], rings[1]);
221
222         fprintf(stderr, "info: count=%d,%d, expect=%d,%d\n",
223                         ret[0], ret[1], expect[0], expect[1]);
224
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");
228                 return 1;
229         }
230
231         return 0;
232 }
233
234 /* Test illegal mode + flag combination */
235 static void test_control_single(void)
236 {
237         fprintf(stderr, "test: control single socket\n");
238
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");
242                 exit(1);
243         }
244 }
245
246 /* Test illegal group with different modes or flags */
247 static void test_control_group(void)
248 {
249         int fds[2];
250
251         fprintf(stderr, "test: control multiple sockets\n");
252
253         fds[0] = sock_fanout_open(PACKET_FANOUT_HASH, 20);
254         if (fds[0] == -1) {
255                 fprintf(stderr, "ERROR: failed to open HASH socket\n");
256                 exit(1);
257         }
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");
261                 exit(1);
262         }
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");
266                 exit(1);
267         }
268         if (sock_fanout_open(PACKET_FANOUT_CPU, 10) != -1) {
269                 fprintf(stderr, "ERROR: joined group with wrong mode\n");
270                 exit(1);
271         }
272         fds[1] = sock_fanout_open(PACKET_FANOUT_HASH, 20);
273         if (fds[1] == -1) {
274                 fprintf(stderr, "ERROR: failed to join group\n");
275                 exit(1);
276         }
277         if (close(fds[1]) || close(fds[0])) {
278                 fprintf(stderr, "ERROR: closing sockets\n");
279                 exit(1);
280         }
281 }
282
283 static int test_datapath(uint16_t typeflags, int port_off,
284                          const int expect1[], const int expect2[])
285 {
286         const int expect0[] = { 0, 0 };
287         char *rings[2];
288         int fds[2], fds_udp[2][2], ret;
289
290         fprintf(stderr, "test: datapath 0x%hx\n", typeflags);
291
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");
296                 exit(1);
297         }
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);
303
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);
308
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);
313
314         if (munmap(rings[1], RING_NUM_FRAMES * getpagesize()) ||
315             munmap(rings[0], RING_NUM_FRAMES * getpagesize())) {
316                 fprintf(stderr, "close rings\n");
317                 exit(1);
318         }
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");
323                 exit(1);
324         }
325
326         return ret;
327 }
328
329 static int set_cpuaffinity(int cpuid)
330 {
331         cpu_set_t mask;
332
333         CPU_ZERO(&mask);
334         CPU_SET(cpuid, &mask);
335         if (sched_setaffinity(0, sizeof(mask), &mask)) {
336                 if (errno != EINVAL) {
337                         fprintf(stderr, "setaffinity %d\n", cpuid);
338                         exit(1);
339                 }
340                 return 1;
341         }
342
343         return 0;
344 }
345
346 int main(int argc, char **argv)
347 {
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;
355
356         test_control_single();
357         test_control_group();
358
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]);
366         }
367
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]);
374
375         set_cpuaffinity(0);
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]);
382
383         if (ret)
384                 return 1;
385
386         printf("OK. All tests passed\n");
387         return 0;
388 }