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