xref: /linux/tools/testing/selftests/bpf/network_helpers.h (revision ddaa827dcea4544ccfffa1dd85ab0ea35cd06e6d)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __NETWORK_HELPERS_H
3 #define __NETWORK_HELPERS_H
4 #include <arpa/inet.h>
5 #include <sys/socket.h>
6 #include <sys/types.h>
7 #include <linux/types.h>
8 typedef __u16 __sum16;
9 #include <linux/if_ether.h>
10 #include <linux/if_packet.h>
11 #include <linux/if_tun.h>
12 #include <linux/ip.h>
13 #include <linux/ipv6.h>
14 #include <linux/ethtool.h>
15 #include <linux/sockios.h>
16 #include <linux/err.h>
17 #include <netinet/tcp.h>
18 #include <netinet/udp.h>
19 #include <bpf/bpf_endian.h>
20 #include <net/if.h>
21 #include <stdio.h>
22 
23 #define MAGIC_VAL 0x1234
24 #define NUM_ITER 100000
25 #define VIP_NUM 5
26 #define MAGIC_BYTES 123
27 
28 /* include/linux/net.h */
29 #ifndef SOCK_TYPE_MASK
30 #define SOCK_TYPE_MASK 0xf
31 #endif
32 
33 struct network_helper_opts {
34 	int timeout_ms;
35 	int proto;
36 	/* +ve: Passed to listen() as-is.
37 	 *   0: Default when the test does not set
38 	 *      a particular value during the struct init.
39 	 *      It is changed to 1 before passing to listen().
40 	 *      Most tests only have one on-going connection.
41 	 * -ve: It is changed to 0 before passing to listen().
42 	 *      It is useful to force syncookie without
43 	 *	changing the "tcp_syncookies" sysctl from 1 to 2.
44 	 */
45 	int backlog;
46 	int (*post_socket_cb)(int fd, void *opts);
47 	void *cb_opts;
48 };
49 
50 /* ipv4 test vector */
51 struct ipv4_packet {
52 	struct ethhdr eth;
53 	struct iphdr iph;
54 	struct tcphdr tcp;
55 } __packed;
56 extern struct ipv4_packet pkt_v4;
57 
58 /* ipv6 test vector */
59 struct ipv6_packet {
60 	struct ethhdr eth;
61 	struct ipv6hdr iph;
62 	struct tcphdr tcp;
63 } __packed;
64 extern struct ipv6_packet pkt_v6;
65 
66 int settimeo(int fd, int timeout_ms);
67 int start_server_str(int family, int type, const char *addr_str, __u16 port,
68 		     const struct network_helper_opts *opts);
69 int start_server(int family, int type, const char *addr, __u16 port,
70 		 int timeout_ms);
71 int *start_reuseport_server(int family, int type, const char *addr_str,
72 			    __u16 port, int timeout_ms,
73 			    unsigned int nr_listens);
74 int start_server_addr(int type, const struct sockaddr_storage *addr, socklen_t len,
75 		      const struct network_helper_opts *opts);
76 void free_fds(int *fds, unsigned int nr_close_fds);
77 int client_socket(int family, int type,
78 		  const struct network_helper_opts *opts);
79 int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t len,
80 		    const struct network_helper_opts *opts);
81 int connect_to_addr_str(int family, int type, const char *addr_str, __u16 port,
82 			const struct network_helper_opts *opts);
83 int connect_to_fd(int server_fd, int timeout_ms);
84 int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts);
85 int connect_fd_to_fd(int client_fd, int server_fd, int timeout_ms);
86 int fastopen_connect(int server_fd, const char *data, unsigned int data_len,
87 		     int timeout_ms);
88 int make_sockaddr(int family, const char *addr_str, __u16 port,
89 		  struct sockaddr_storage *addr, socklen_t *len);
90 char *ping_command(int family);
91 int get_socket_local_port(int sock_fd);
92 int get_hw_ring_size(char *ifname, struct ethtool_ringparam *ring_param);
93 int set_hw_ring_size(char *ifname, struct ethtool_ringparam *ring_param);
94 
95 int open_tuntap(const char *dev_name, bool need_mac);
96 
97 struct nstoken;
98 /**
99  * open_netns() - Switch to specified network namespace by name.
100  *
101  * Returns token with which to restore the original namespace
102  * using close_netns().
103  */
104 struct nstoken *open_netns(const char *name);
105 void close_netns(struct nstoken *token);
106 int send_recv_data(int lfd, int fd, uint32_t total_bytes);
107 int make_netns(const char *name);
108 int remove_netns(const char *name);
109 
110 /**
111  * append_tid() - Append thread ID to the given string.
112  *
113  * @str: string to extend
114  * @sz: string's size
115  *
116  * 8 characters are used to append the thread ID (7 digits + '\0')
117  *
118  * Returns -1 on errors, 0 otherwise
119  */
120 int append_tid(char *str, size_t sz);
121 
122 static __u16 csum_fold(__u32 csum)
123 {
124 	csum = (csum & 0xffff) + (csum >> 16);
125 	csum = (csum & 0xffff) + (csum >> 16);
126 
127 	return (__u16)~csum;
128 }
129 
130 static __wsum csum_partial(const void *buf, int len, __wsum sum)
131 {
132 	const __u8 *p = buf;
133 	int num_u16 = len >> 1;
134 	int i;
135 
136 	for (i = 0; i < num_u16; i++)
137 		sum += ((const __u16 *)p)[i];
138 
139 	/*
140 	 * RFC 1071: an odd-length buffer's trailing byte is paired with
141 	 * a zero pad byte to form the final 16-bit word.
142 	 */
143 	if (len & 1) {
144 		__u16 tail = 0;
145 
146 		__builtin_memcpy(&tail, p + len - 1, 1);
147 		sum += tail;
148 	}
149 
150 	return sum;
151 }
152 
153 static inline __sum16 build_ip_csum(struct iphdr *iph)
154 {
155 	__u32 sum = 0;
156 	__u16 *p;
157 
158 	iph->check = 0;
159 	p = (void *)iph;
160 	sum = csum_partial(p, iph->ihl << 2, 0);
161 
162 	return csum_fold(sum);
163 }
164 
165 /**
166  * csum_tcpudp_magic - compute IP pseudo-header checksum
167  *
168  * Compute the IPv4 pseudo header checksum. The helper can take a
169  * accumulated sum from the transport layer to accumulate it and directly
170  * return the transport layer
171  *
172  * @saddr: IP source address
173  * @daddr: IP dest address
174  * @len: IP data size
175  * @proto: transport layer protocol
176  * @csum: The accumulated partial sum to add to the computation
177  *
178  * Returns the folded sum
179  */
180 static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
181 					__u32 len, __u8 proto,
182 					__wsum csum)
183 {
184 	__u64 s = csum;
185 
186 	s += (__u32)saddr;
187 	s += (__u32)daddr;
188 	s += htons(proto + len);
189 	s = (s & 0xffffffff) + (s >> 32);
190 	s = (s & 0xffffffff) + (s >> 32);
191 
192 	return csum_fold((__u32)s);
193 }
194 
195 /**
196  * csum_ipv6_magic - compute IPv6 pseudo-header checksum
197  *
198  * Compute the ipv6 pseudo header checksum. The helper can take a
199  * accumulated sum from the transport layer to accumulate it and directly
200  * return the transport layer
201  *
202  * @saddr: IPv6 source address
203  * @daddr: IPv6 dest address
204  * @len: IPv6 data size
205  * @proto: transport layer protocol
206  * @csum: The accumulated partial sum to add to the computation
207  *
208  * Returns the folded sum
209  */
210 static inline __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
211 				      const struct in6_addr *daddr,
212 					__u32 len, __u8 proto,
213 					__wsum csum)
214 {
215 	__u64 s = csum;
216 	int i;
217 
218 	for (i = 0; i < 4; i++)
219 		s += (__u32)saddr->s6_addr32[i];
220 	for (i = 0; i < 4; i++)
221 		s += (__u32)daddr->s6_addr32[i];
222 	s += htons(proto + len);
223 	s = (s & 0xffffffff) + (s >> 32);
224 	s = (s & 0xffffffff) + (s >> 32);
225 
226 	return csum_fold((__u32)s);
227 }
228 
229 /**
230  * build_udp_v4_csum - compute UDP checksum for UDP over IPv4
231  *
232  * Compute the checksum to embed in UDP header, composed of the sum of IP
233  * pseudo-header checksum, UDP header checksum and UDP data checksum
234  * @iph IP header
235  * @udph UDP header, which must be immediately followed by UDP data
236  *
237  * Returns the total checksum
238  */
239 
240 static inline __sum16 build_udp_v4_csum(const struct iphdr *iph,
241 					const struct udphdr *udph)
242 {
243 	unsigned long sum;
244 
245 	sum = csum_partial(udph, ntohs(udph->len), 0);
246 	return csum_tcpudp_magic(iph->saddr, iph->daddr, ntohs(udph->len),
247 				 IPPROTO_UDP, sum);
248 }
249 
250 /**
251  * build_udp_v6_csum - compute UDP checksum for UDP over IPv6
252  *
253  * Compute the checksum to embed in UDP header, composed of the sum of IPv6
254  * pseudo-header checksum, UDP header checksum and UDP data checksum
255  * @ip6h IPv6 header
256  * @udph UDP header, which must be immediately followed by UDP data
257  *
258  * Returns the total checksum
259  */
260 static inline __sum16 build_udp_v6_csum(const struct ipv6hdr *ip6h,
261 					const struct udphdr *udph)
262 {
263 	unsigned long sum;
264 
265 	sum = csum_partial(udph, ntohs(udph->len), 0);
266 	return csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, ntohs(udph->len),
267 			       IPPROTO_UDP, sum);
268 }
269 
270 struct tmonitor_ctx;
271 
272 typedef int (*tm_print_fn_t)(const char *format, va_list args);
273 
274 /**
275  * tc_prog_attach - attach BPF program(s) to an interface
276  *
277  * Takes file descriptors pointing to at least one, at most two BPF
278  * programs, and attach those programs to an interface ingress, egress or
279  * both.
280  *
281  * @dev: string containing the interface name
282  * @ingress_fd: file descriptor of the program to attach to interface ingress
283  * @egress_fd: file descriptor of the program to attach to interface egress
284  *
285  * Returns 0 on success, -1 if no valid file descriptor has been found, if
286  * the interface name is invalid or if an error ocurred during attach.
287  */
288 int tc_prog_attach(const char *dev, int ingress_fd, int egress_fd);
289 
290 #ifdef TRAFFIC_MONITOR
291 struct tmonitor_ctx *traffic_monitor_start(const char *netns, const char *test_name,
292 					   const char *subtest_name);
293 void traffic_monitor_stop(struct tmonitor_ctx *ctx);
294 tm_print_fn_t traffic_monitor_set_print(tm_print_fn_t fn);
295 #else
296 static inline struct tmonitor_ctx *traffic_monitor_start(const char *netns, const char *test_name,
297 							 const char *subtest_name)
298 {
299 	return NULL;
300 }
301 
302 static inline void traffic_monitor_stop(struct tmonitor_ctx *ctx)
303 {
304 }
305 
306 static inline tm_print_fn_t traffic_monitor_set_print(tm_print_fn_t fn)
307 {
308 	return NULL;
309 }
310 #endif
311 
312 #endif
313