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