xref: /linux/tools/testing/selftests/bpf/network_helpers.h (revision d7b4e3287ca3a7baf66efd9158498e551a9550da)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __NETWORK_HELPERS_H
3 #define __NETWORK_HELPERS_H
4 #include <sys/socket.h>
5 #include <sys/types.h>
6 #include <linux/types.h>
7 typedef __u16 __sum16;
8 #include <linux/if_ether.h>
9 #include <linux/if_packet.h>
10 #include <linux/ip.h>
11 #include <linux/ipv6.h>
12 #include <netinet/tcp.h>
13 #include <bpf/bpf_endian.h>
14 
15 #define MAGIC_VAL 0x1234
16 #define NUM_ITER 100000
17 #define VIP_NUM 5
18 #define MAGIC_BYTES 123
19 
20 struct network_helper_opts {
21 	const char *cc;
22 	int timeout_ms;
23 	bool must_fail;
24 	bool noconnect;
25 	int type;
26 	int proto;
27 };
28 
29 /* ipv4 test vector */
30 struct ipv4_packet {
31 	struct ethhdr eth;
32 	struct iphdr iph;
33 	struct tcphdr tcp;
34 } __packed;
35 extern struct ipv4_packet pkt_v4;
36 
37 /* ipv6 test vector */
38 struct ipv6_packet {
39 	struct ethhdr eth;
40 	struct ipv6hdr iph;
41 	struct tcphdr tcp;
42 } __packed;
43 extern struct ipv6_packet pkt_v6;
44 
45 int settimeo(int fd, int timeout_ms);
46 int start_server(int family, int type, const char *addr, __u16 port,
47 		 int timeout_ms);
48 int start_mptcp_server(int family, const char *addr, __u16 port,
49 		       int timeout_ms);
50 int *start_reuseport_server(int family, int type, const char *addr_str,
51 			    __u16 port, int timeout_ms,
52 			    unsigned int nr_listens);
53 void free_fds(int *fds, unsigned int nr_close_fds);
54 int connect_to_addr(const struct sockaddr_storage *addr, socklen_t len, int type);
55 int connect_to_fd(int server_fd, int timeout_ms);
56 int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts);
57 int connect_fd_to_fd(int client_fd, int server_fd, int timeout_ms);
58 int fastopen_connect(int server_fd, const char *data, unsigned int data_len,
59 		     int timeout_ms);
60 int make_sockaddr(int family, const char *addr_str, __u16 port,
61 		  struct sockaddr_storage *addr, socklen_t *len);
62 char *ping_command(int family);
63 int get_socket_local_port(int sock_fd);
64 
65 struct nstoken;
66 /**
67  * open_netns() - Switch to specified network namespace by name.
68  *
69  * Returns token with which to restore the original namespace
70  * using close_netns().
71  */
72 struct nstoken *open_netns(const char *name);
73 void close_netns(struct nstoken *token);
74 
75 static __u16 csum_fold(__u32 csum)
76 {
77 	csum = (csum & 0xffff) + (csum >> 16);
78 	csum = (csum & 0xffff) + (csum >> 16);
79 
80 	return (__u16)~csum;
81 }
82 
83 static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
84 					__u32 len, __u8 proto,
85 					__wsum csum)
86 {
87 	__u64 s = csum;
88 
89 	s += (__u32)saddr;
90 	s += (__u32)daddr;
91 	s += htons(proto + len);
92 	s = (s & 0xffffffff) + (s >> 32);
93 	s = (s & 0xffffffff) + (s >> 32);
94 
95 	return csum_fold((__u32)s);
96 }
97 
98 static inline __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
99 				      const struct in6_addr *daddr,
100 					__u32 len, __u8 proto,
101 					__wsum csum)
102 {
103 	__u64 s = csum;
104 	int i;
105 
106 	for (i = 0; i < 4; i++)
107 		s += (__u32)saddr->s6_addr32[i];
108 	for (i = 0; i < 4; i++)
109 		s += (__u32)daddr->s6_addr32[i];
110 	s += htons(proto + len);
111 	s = (s & 0xffffffff) + (s >> 32);
112 	s = (s & 0xffffffff) + (s >> 32);
113 
114 	return csum_fold((__u32)s);
115 }
116 
117 #endif
118