xref: /linux/tools/testing/selftests/bpf/network_helpers.h (revision 4f88dde0e1525ee69d29ba235c3965685439d0e3)
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 	int timeout_ms;
26 	int proto;
27 	/* +ve: Passed to listen() as-is.
28 	 *   0: Default when the test does not set
29 	 *      a particular value during the struct init.
30 	 *      It is changed to 1 before passing to listen().
31 	 *      Most tests only have one on-going connection.
32 	 * -ve: It is changed to 0 before passing to listen().
33 	 *      It is useful to force syncookie without
34 	 *	changing the "tcp_syncookies" sysctl from 1 to 2.
35 	 */
36 	int backlog;
37 	int (*post_socket_cb)(int fd, void *opts);
38 	void *cb_opts;
39 };
40 
41 /* ipv4 test vector */
42 struct ipv4_packet {
43 	struct ethhdr eth;
44 	struct iphdr iph;
45 	struct tcphdr tcp;
46 } __packed;
47 extern struct ipv4_packet pkt_v4;
48 
49 /* ipv6 test vector */
50 struct ipv6_packet {
51 	struct ethhdr eth;
52 	struct ipv6hdr iph;
53 	struct tcphdr tcp;
54 } __packed;
55 extern struct ipv6_packet pkt_v6;
56 
57 int settimeo(int fd, int timeout_ms);
58 int start_server_str(int family, int type, const char *addr_str, __u16 port,
59 		     const struct network_helper_opts *opts);
60 int start_server(int family, int type, const char *addr, __u16 port,
61 		 int timeout_ms);
62 int *start_reuseport_server(int family, int type, const char *addr_str,
63 			    __u16 port, int timeout_ms,
64 			    unsigned int nr_listens);
65 int start_server_addr(int type, const struct sockaddr_storage *addr, socklen_t len,
66 		      const struct network_helper_opts *opts);
67 void free_fds(int *fds, unsigned int nr_close_fds);
68 int client_socket(int family, int type,
69 		  const struct network_helper_opts *opts);
70 int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t len,
71 		    const struct network_helper_opts *opts);
72 int connect_to_addr_str(int family, int type, const char *addr_str, __u16 port,
73 			const struct network_helper_opts *opts);
74 int connect_to_fd(int server_fd, int timeout_ms);
75 int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts);
76 int connect_fd_to_fd(int client_fd, int server_fd, int timeout_ms);
77 int fastopen_connect(int server_fd, const char *data, unsigned int data_len,
78 		     int timeout_ms);
79 int make_sockaddr(int family, const char *addr_str, __u16 port,
80 		  struct sockaddr_storage *addr, socklen_t *len);
81 char *ping_command(int family);
82 int get_socket_local_port(int sock_fd);
83 int get_hw_ring_size(char *ifname, struct ethtool_ringparam *ring_param);
84 int set_hw_ring_size(char *ifname, struct ethtool_ringparam *ring_param);
85 
86 struct nstoken;
87 /**
88  * open_netns() - Switch to specified network namespace by name.
89  *
90  * Returns token with which to restore the original namespace
91  * using close_netns().
92  */
93 struct nstoken *open_netns(const char *name);
94 void close_netns(struct nstoken *token);
95 int send_recv_data(int lfd, int fd, uint32_t total_bytes);
96 
97 static __u16 csum_fold(__u32 csum)
98 {
99 	csum = (csum & 0xffff) + (csum >> 16);
100 	csum = (csum & 0xffff) + (csum >> 16);
101 
102 	return (__u16)~csum;
103 }
104 
105 static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
106 					__u32 len, __u8 proto,
107 					__wsum csum)
108 {
109 	__u64 s = csum;
110 
111 	s += (__u32)saddr;
112 	s += (__u32)daddr;
113 	s += htons(proto + len);
114 	s = (s & 0xffffffff) + (s >> 32);
115 	s = (s & 0xffffffff) + (s >> 32);
116 
117 	return csum_fold((__u32)s);
118 }
119 
120 static inline __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
121 				      const struct in6_addr *daddr,
122 					__u32 len, __u8 proto,
123 					__wsum csum)
124 {
125 	__u64 s = csum;
126 	int i;
127 
128 	for (i = 0; i < 4; i++)
129 		s += (__u32)saddr->s6_addr32[i];
130 	for (i = 0; i < 4; i++)
131 		s += (__u32)daddr->s6_addr32[i];
132 	s += htons(proto + len);
133 	s = (s & 0xffffffff) + (s >> 32);
134 	s = (s & 0xffffffff) + (s >> 32);
135 
136 	return csum_fold((__u32)s);
137 }
138 
139 #endif
140