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