1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. */
3 
4 #include <linux/bpf.h>
5 #include <linux/if_link.h>
6 #include <arpa/inet.h>
7 #include <assert.h>
8 #include <errno.h>
9 #include <signal.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <unistd.h>
14 #include <libgen.h>
15 #include <sys/resource.h>
16 #include <net/if.h>
17 #include <sys/types.h>
18 #include <sys/socket.h>
19 #include <netdb.h>
20 
21 #include "bpf/bpf.h"
22 #include "bpf/libbpf.h"
23 
24 #include "xdping.h"
25 #include "testing_helpers.h"
26 
27 static int ifindex;
28 static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
29 
30 static void cleanup(int sig)
31 {
32 	bpf_set_link_xdp_fd(ifindex, -1, xdp_flags);
33 	if (sig)
34 		exit(1);
35 }
36 
37 static int get_stats(int fd, __u16 count, __u32 raddr)
38 {
39 	struct pinginfo pinginfo = { 0 };
40 	char inaddrbuf[INET_ADDRSTRLEN];
41 	struct in_addr inaddr;
42 	__u16 i;
43 
44 	inaddr.s_addr = raddr;
45 
46 	printf("\nXDP RTT data:\n");
47 
48 	if (bpf_map_lookup_elem(fd, &raddr, &pinginfo)) {
49 		perror("bpf_map_lookup elem");
50 		return 1;
51 	}
52 
53 	for (i = 0; i < count; i++) {
54 		if (pinginfo.times[i] == 0)
55 			break;
56 
57 		printf("64 bytes from %s: icmp_seq=%d ttl=64 time=%#.5f ms\n",
58 		       inet_ntop(AF_INET, &inaddr, inaddrbuf,
59 				 sizeof(inaddrbuf)),
60 		       count + i + 1,
61 		       (double)pinginfo.times[i]/1000000);
62 	}
63 
64 	if (i < count) {
65 		fprintf(stderr, "Expected %d samples, got %d.\n", count, i);
66 		return 1;
67 	}
68 
69 	bpf_map_delete_elem(fd, &raddr);
70 
71 	return 0;
72 }
73 
74 static void show_usage(const char *prog)
75 {
76 	fprintf(stderr,
77 		"usage: %s [OPTS] -I interface destination\n\n"
78 		"OPTS:\n"
79 		"    -c count		Stop after sending count requests\n"
80 		"			(default %d, max %d)\n"
81 		"    -I interface	interface name\n"
82 		"    -N			Run in driver mode\n"
83 		"    -s			Server mode\n"
84 		"    -S			Run in skb mode\n",
85 		prog, XDPING_DEFAULT_COUNT, XDPING_MAX_COUNT);
86 }
87 
88 int main(int argc, char **argv)
89 {
90 	__u32 mode_flags = XDP_FLAGS_DRV_MODE | XDP_FLAGS_SKB_MODE;
91 	struct addrinfo *a, hints = { .ai_family = AF_INET };
92 	struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
93 	__u16 count = XDPING_DEFAULT_COUNT;
94 	struct pinginfo pinginfo = { 0 };
95 	const char *optstr = "c:I:NsS";
96 	struct bpf_program *main_prog;
97 	int prog_fd = -1, map_fd = -1;
98 	struct sockaddr_in rin;
99 	struct bpf_object *obj;
100 	struct bpf_map *map;
101 	char *ifname = NULL;
102 	char filename[256];
103 	int opt, ret = 1;
104 	__u32 raddr = 0;
105 	int server = 0;
106 	char cmd[256];
107 
108 	while ((opt = getopt(argc, argv, optstr)) != -1) {
109 		switch (opt) {
110 		case 'c':
111 			count = atoi(optarg);
112 			if (count < 1 || count > XDPING_MAX_COUNT) {
113 				fprintf(stderr,
114 					"min count is 1, max count is %d\n",
115 					XDPING_MAX_COUNT);
116 				return 1;
117 			}
118 			break;
119 		case 'I':
120 			ifname = optarg;
121 			ifindex = if_nametoindex(ifname);
122 			if (!ifindex) {
123 				fprintf(stderr, "Could not get interface %s\n",
124 					ifname);
125 				return 1;
126 			}
127 			break;
128 		case 'N':
129 			xdp_flags |= XDP_FLAGS_DRV_MODE;
130 			break;
131 		case 's':
132 			/* use server program */
133 			server = 1;
134 			break;
135 		case 'S':
136 			xdp_flags |= XDP_FLAGS_SKB_MODE;
137 			break;
138 		default:
139 			show_usage(basename(argv[0]));
140 			return 1;
141 		}
142 	}
143 
144 	if (!ifname) {
145 		show_usage(basename(argv[0]));
146 		return 1;
147 	}
148 	if (!server && optind == argc) {
149 		show_usage(basename(argv[0]));
150 		return 1;
151 	}
152 
153 	if ((xdp_flags & mode_flags) == mode_flags) {
154 		fprintf(stderr, "-N or -S can be specified, not both.\n");
155 		show_usage(basename(argv[0]));
156 		return 1;
157 	}
158 
159 	if (!server) {
160 		/* Only supports IPv4; see hints initiailization above. */
161 		if (getaddrinfo(argv[optind], NULL, &hints, &a) || !a) {
162 			fprintf(stderr, "Could not resolve %s\n", argv[optind]);
163 			return 1;
164 		}
165 		memcpy(&rin, a->ai_addr, sizeof(rin));
166 		raddr = rin.sin_addr.s_addr;
167 		freeaddrinfo(a);
168 	}
169 
170 	if (setrlimit(RLIMIT_MEMLOCK, &r)) {
171 		perror("setrlimit(RLIMIT_MEMLOCK)");
172 		return 1;
173 	}
174 
175 	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
176 
177 	if (bpf_prog_test_load(filename, BPF_PROG_TYPE_XDP, &obj, &prog_fd)) {
178 		fprintf(stderr, "load of %s failed\n", filename);
179 		return 1;
180 	}
181 
182 	main_prog = bpf_object__find_program_by_name(obj,
183 						     server ? "xdping_server" : "xdping_client");
184 	if (main_prog)
185 		prog_fd = bpf_program__fd(main_prog);
186 	if (!main_prog || prog_fd < 0) {
187 		fprintf(stderr, "could not find xdping program");
188 		return 1;
189 	}
190 
191 	map = bpf_object__next_map(obj, NULL);
192 	if (map)
193 		map_fd = bpf_map__fd(map);
194 	if (!map || map_fd < 0) {
195 		fprintf(stderr, "Could not find ping map");
196 		goto done;
197 	}
198 
199 	signal(SIGINT, cleanup);
200 	signal(SIGTERM, cleanup);
201 
202 	printf("Setting up XDP for %s, please wait...\n", ifname);
203 
204 	printf("XDP setup disrupts network connectivity, hit Ctrl+C to quit\n");
205 
206 	if (bpf_set_link_xdp_fd(ifindex, prog_fd, xdp_flags) < 0) {
207 		fprintf(stderr, "Link set xdp fd failed for %s\n", ifname);
208 		goto done;
209 	}
210 
211 	if (server) {
212 		close(prog_fd);
213 		close(map_fd);
214 		printf("Running server on %s; press Ctrl+C to exit...\n",
215 		       ifname);
216 		do { } while (1);
217 	}
218 
219 	/* Start xdping-ing from last regular ping reply, e.g. for a count
220 	 * of 10 ICMP requests, we start xdping-ing using reply with seq number
221 	 * 10.  The reason the last "real" ping RTT is much higher is that
222 	 * the ping program sees the ICMP reply associated with the last
223 	 * XDP-generated packet, so ping doesn't get a reply until XDP is done.
224 	 */
225 	pinginfo.seq = htons(count);
226 	pinginfo.count = count;
227 
228 	if (bpf_map_update_elem(map_fd, &raddr, &pinginfo, BPF_ANY)) {
229 		fprintf(stderr, "could not communicate with BPF map: %s\n",
230 			strerror(errno));
231 		cleanup(0);
232 		goto done;
233 	}
234 
235 	/* We need to wait for XDP setup to complete. */
236 	sleep(10);
237 
238 	snprintf(cmd, sizeof(cmd), "ping -c %d -I %s %s",
239 		 count, ifname, argv[optind]);
240 
241 	printf("\nNormal ping RTT data\n");
242 	printf("[Ignore final RTT; it is distorted by XDP using the reply]\n");
243 
244 	ret = system(cmd);
245 
246 	if (!ret)
247 		ret = get_stats(map_fd, count, raddr);
248 
249 	cleanup(0);
250 
251 done:
252 	if (prog_fd > 0)
253 		close(prog_fd);
254 	if (map_fd > 0)
255 		close(map_fd);
256 
257 	return ret;
258 }
259