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 #include "psock_lib.h"
65 
66 #define RING_NUM_FRAMES			20
67 
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)
71 {
72 	int fd, val;
73 
74 	fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
75 	if (fd < 0) {
76 		perror("socket packet");
77 		exit(1);
78 	}
79 
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))) {
83 		if (close(fd)) {
84 			perror("close packet");
85 			exit(1);
86 		}
87 		return -1;
88 	}
89 
90 	pair_udp_setfilter(fd);
91 	return fd;
92 }
93 
94 static char *sock_fanout_open_ring(int fd)
95 {
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,
101 	};
102 	char *ring;
103 	int val = TPACKET_V2;
104 
105 	if (setsockopt(fd, SOL_PACKET, PACKET_VERSION, (void *) &val,
106 		       sizeof(val))) {
107 		perror("packetsock ring setsockopt version");
108 		exit(1);
109 	}
110 	if (setsockopt(fd, SOL_PACKET, PACKET_RX_RING, (void *) &req,
111 		       sizeof(req))) {
112 		perror("packetsock ring setsockopt");
113 		exit(1);
114 	}
115 
116 	ring = mmap(0, req.tp_block_size * req.tp_block_nr,
117 		    PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
118 	if (!ring) {
119 		fprintf(stderr, "packetsock ring mmap\n");
120 		exit(1);
121 	}
122 
123 	return ring;
124 }
125 
126 static int sock_fanout_read_ring(int fd, void *ring)
127 {
128 	struct tpacket2_hdr *header = ring;
129 	int count = 0;
130 
131 	while (count < RING_NUM_FRAMES && header->tp_status & TP_STATUS_USER) {
132 		count++;
133 		header = ring + (count * getpagesize());
134 	}
135 
136 	return count;
137 }
138 
139 static int sock_fanout_read(int fds[], char *rings[], const int expect[])
140 {
141 	int ret[2];
142 
143 	ret[0] = sock_fanout_read_ring(fds[0], rings[0]);
144 	ret[1] = sock_fanout_read_ring(fds[1], rings[1]);
145 
146 	fprintf(stderr, "info: count=%d,%d, expect=%d,%d\n",
147 			ret[0], ret[1], expect[0], expect[1]);
148 
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");
152 		return 1;
153 	}
154 
155 	return 0;
156 }
157 
158 /* Test illegal mode + flag combination */
159 static void test_control_single(void)
160 {
161 	fprintf(stderr, "test: control single socket\n");
162 
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");
166 		exit(1);
167 	}
168 }
169 
170 /* Test illegal group with different modes or flags */
171 static void test_control_group(void)
172 {
173 	int fds[2];
174 
175 	fprintf(stderr, "test: control multiple sockets\n");
176 
177 	fds[0] = sock_fanout_open(PACKET_FANOUT_HASH, 20);
178 	if (fds[0] == -1) {
179 		fprintf(stderr, "ERROR: failed to open HASH socket\n");
180 		exit(1);
181 	}
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");
185 		exit(1);
186 	}
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");
190 		exit(1);
191 	}
192 	if (sock_fanout_open(PACKET_FANOUT_CPU, 10) != -1) {
193 		fprintf(stderr, "ERROR: joined group with wrong mode\n");
194 		exit(1);
195 	}
196 	fds[1] = sock_fanout_open(PACKET_FANOUT_HASH, 20);
197 	if (fds[1] == -1) {
198 		fprintf(stderr, "ERROR: failed to join group\n");
199 		exit(1);
200 	}
201 	if (close(fds[1]) || close(fds[0])) {
202 		fprintf(stderr, "ERROR: closing sockets\n");
203 		exit(1);
204 	}
205 }
206 
207 static int test_datapath(uint16_t typeflags, int port_off,
208 			 const int expect1[], const int expect2[])
209 {
210 	const int expect0[] = { 0, 0 };
211 	char *rings[2];
212 	int fds[2], fds_udp[2][2], ret;
213 
214 	fprintf(stderr, "test: datapath 0x%hx\n", typeflags);
215 
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");
220 		exit(1);
221 	}
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);
227 
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);
232 
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);
237 
238 	if (munmap(rings[1], RING_NUM_FRAMES * getpagesize()) ||
239 	    munmap(rings[0], RING_NUM_FRAMES * getpagesize())) {
240 		fprintf(stderr, "close rings\n");
241 		exit(1);
242 	}
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");
247 		exit(1);
248 	}
249 
250 	return ret;
251 }
252 
253 static int set_cpuaffinity(int cpuid)
254 {
255 	cpu_set_t mask;
256 
257 	CPU_ZERO(&mask);
258 	CPU_SET(cpuid, &mask);
259 	if (sched_setaffinity(0, sizeof(mask), &mask)) {
260 		if (errno != EINVAL) {
261 			fprintf(stderr, "setaffinity %d\n", cpuid);
262 			exit(1);
263 		}
264 		return 1;
265 	}
266 
267 	return 0;
268 }
269 
270 int main(int argc, char **argv)
271 {
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;
279 
280 	test_control_single();
281 	test_control_group();
282 
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]);
290 	}
291 
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]);
298 
299 	set_cpuaffinity(0);
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]);
306 
307 	if (ret)
308 		return 1;
309 
310 	printf("OK. All tests passed\n");
311 	return 0;
312 }
313