133181bb8SStanislav Fomichev /* SPDX-License-Identifier: GPL-2.0 */ 233181bb8SStanislav Fomichev #ifndef __NETWORK_HELPERS_H 333181bb8SStanislav Fomichev #define __NETWORK_HELPERS_H 433181bb8SStanislav Fomichev #include <sys/socket.h> 533181bb8SStanislav Fomichev #include <sys/types.h> 6488a23b8SStanislav Fomichev #include <linux/types.h> 7488a23b8SStanislav Fomichev typedef __u16 __sum16; 8488a23b8SStanislav Fomichev #include <linux/if_ether.h> 9488a23b8SStanislav Fomichev #include <linux/if_packet.h> 10488a23b8SStanislav Fomichev #include <linux/ip.h> 11488a23b8SStanislav Fomichev #include <linux/ipv6.h> 1290a695c3STushar Vyavahare #include <linux/ethtool.h> 1390a695c3STushar Vyavahare #include <linux/sockios.h> 14e1cdb70dSGeliang Tang #include <linux/err.h> 15488a23b8SStanislav Fomichev #include <netinet/tcp.h> 16488a23b8SStanislav Fomichev #include <bpf/bpf_endian.h> 1790a695c3STushar Vyavahare #include <net/if.h> 18488a23b8SStanislav Fomichev 19488a23b8SStanislav Fomichev #define MAGIC_VAL 0x1234 20488a23b8SStanislav Fomichev #define NUM_ITER 100000 21488a23b8SStanislav Fomichev #define VIP_NUM 5 22488a23b8SStanislav Fomichev #define MAGIC_BYTES 123 23488a23b8SStanislav Fomichev 243d778983SMartin KaFai Lau struct network_helper_opts { 253d778983SMartin KaFai Lau int timeout_ms; 2643d2b88cSDaniel Borkmann bool must_fail; 273495e89cSDaniel Xu bool noconnect; 28e15a2209SDaniel Xu int proto; 29ed31adf6SGeliang Tang int (*post_socket_cb)(int fd, void *opts); 30e078255aSGeliang Tang void *cb_opts; 313d778983SMartin KaFai Lau }; 323d778983SMartin KaFai Lau 33488a23b8SStanislav Fomichev /* ipv4 test vector */ 34488a23b8SStanislav Fomichev struct ipv4_packet { 35488a23b8SStanislav Fomichev struct ethhdr eth; 36488a23b8SStanislav Fomichev struct iphdr iph; 37488a23b8SStanislav Fomichev struct tcphdr tcp; 38488a23b8SStanislav Fomichev } __packed; 39488a23b8SStanislav Fomichev extern struct ipv4_packet pkt_v4; 40488a23b8SStanislav Fomichev 41488a23b8SStanislav Fomichev /* ipv6 test vector */ 42488a23b8SStanislav Fomichev struct ipv6_packet { 43488a23b8SStanislav Fomichev struct ethhdr eth; 44488a23b8SStanislav Fomichev struct ipv6hdr iph; 45488a23b8SStanislav Fomichev struct tcphdr tcp; 46488a23b8SStanislav Fomichev } __packed; 47488a23b8SStanislav Fomichev extern struct ipv6_packet pkt_v6; 4833181bb8SStanislav Fomichev 49096eccdeSJussi Maki int settimeo(int fd, int timeout_ms); 506f802cb8SGeliang Tang int start_server_str(int family, int type, const char *addr_str, __u16 port, 516f802cb8SGeliang Tang const struct network_helper_opts *opts); 5299126abeSMartin KaFai Lau int start_server(int family, int type, const char *addr, __u16 port, 5399126abeSMartin KaFai Lau int timeout_ms); 54eed92afdSMartin KaFai Lau int *start_reuseport_server(int family, int type, const char *addr_str, 55eed92afdSMartin KaFai Lau __u16 port, int timeout_ms, 56eed92afdSMartin KaFai Lau unsigned int nr_listens); 579c598a83SGeliang Tang int start_server_addr(int type, const struct sockaddr_storage *addr, socklen_t len, 589c598a83SGeliang Tang const struct network_helper_opts *opts); 59eed92afdSMartin KaFai Lau void free_fds(int *fds, unsigned int nr_close_fds); 60*bbca57aaSGeliang Tang int client_socket(int family, int type, 61*bbca57aaSGeliang Tang const struct network_helper_opts *opts); 62db9994d0SGeliang Tang int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t len, 63db9994d0SGeliang Tang const struct network_helper_opts *opts); 6499126abeSMartin KaFai Lau int connect_to_fd(int server_fd, int timeout_ms); 6534ad6ec9SGeliang Tang int connect_to_fd_opts(int server_fd, int type, const struct network_helper_opts *opts); 6699126abeSMartin KaFai Lau int connect_fd_to_fd(int client_fd, int server_fd, int timeout_ms); 678085e1dcSMartin KaFai Lau int fastopen_connect(int server_fd, const char *data, unsigned int data_len, 688085e1dcSMartin KaFai Lau int timeout_ms); 690ab5539fSJakub Sitnicki int make_sockaddr(int family, const char *addr_str, __u16 port, 700ab5539fSJakub Sitnicki struct sockaddr_storage *addr, socklen_t *len); 71372642eaSStanislav Fomichev char *ping_command(int family); 72176ba657SAditi Ghag int get_socket_local_port(int sock_fd); 7390a695c3STushar Vyavahare int get_hw_ring_size(char *ifname, struct ethtool_ringparam *ring_param); 74bee3a7b0STushar Vyavahare int set_hw_ring_size(char *ifname, struct ethtool_ringparam *ring_param); 7533181bb8SStanislav Fomichev 76a3033884SToke Høiland-Jørgensen struct nstoken; 77a3033884SToke Høiland-Jørgensen /** 78a3033884SToke Høiland-Jørgensen * open_netns() - Switch to specified network namespace by name. 79a3033884SToke Høiland-Jørgensen * 80a3033884SToke Høiland-Jørgensen * Returns token with which to restore the original namespace 81a3033884SToke Høiland-Jørgensen * using close_netns(). 82a3033884SToke Høiland-Jørgensen */ 83a3033884SToke Høiland-Jørgensen struct nstoken *open_netns(const char *name); 84a3033884SToke Høiland-Jørgensen void close_netns(struct nstoken *token); 85dc34e44eSGeliang Tang int send_recv_data(int lfd, int fd, uint32_t total_bytes); 86f6642de0SStanislav Fomichev 87f6642de0SStanislav Fomichev static __u16 csum_fold(__u32 csum) 88f6642de0SStanislav Fomichev { 89f6642de0SStanislav Fomichev csum = (csum & 0xffff) + (csum >> 16); 90f6642de0SStanislav Fomichev csum = (csum & 0xffff) + (csum >> 16); 91f6642de0SStanislav Fomichev 92f6642de0SStanislav Fomichev return (__u16)~csum; 93f6642de0SStanislav Fomichev } 94f6642de0SStanislav Fomichev 95f6642de0SStanislav Fomichev static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr, 96f6642de0SStanislav Fomichev __u32 len, __u8 proto, 97f6642de0SStanislav Fomichev __wsum csum) 98f6642de0SStanislav Fomichev { 99f6642de0SStanislav Fomichev __u64 s = csum; 100f6642de0SStanislav Fomichev 101f6642de0SStanislav Fomichev s += (__u32)saddr; 102f6642de0SStanislav Fomichev s += (__u32)daddr; 103f6642de0SStanislav Fomichev s += htons(proto + len); 104f6642de0SStanislav Fomichev s = (s & 0xffffffff) + (s >> 32); 105f6642de0SStanislav Fomichev s = (s & 0xffffffff) + (s >> 32); 106f6642de0SStanislav Fomichev 107f6642de0SStanislav Fomichev return csum_fold((__u32)s); 108f6642de0SStanislav Fomichev } 109f6642de0SStanislav Fomichev 110f6642de0SStanislav Fomichev static inline __sum16 csum_ipv6_magic(const struct in6_addr *saddr, 111f6642de0SStanislav Fomichev const struct in6_addr *daddr, 112f6642de0SStanislav Fomichev __u32 len, __u8 proto, 113f6642de0SStanislav Fomichev __wsum csum) 114f6642de0SStanislav Fomichev { 115f6642de0SStanislav Fomichev __u64 s = csum; 116f6642de0SStanislav Fomichev int i; 117f6642de0SStanislav Fomichev 118f6642de0SStanislav Fomichev for (i = 0; i < 4; i++) 119f6642de0SStanislav Fomichev s += (__u32)saddr->s6_addr32[i]; 120f6642de0SStanislav Fomichev for (i = 0; i < 4; i++) 121f6642de0SStanislav Fomichev s += (__u32)daddr->s6_addr32[i]; 122f6642de0SStanislav Fomichev s += htons(proto + len); 123f6642de0SStanislav Fomichev s = (s & 0xffffffff) + (s >> 32); 124f6642de0SStanislav Fomichev s = (s & 0xffffffff) + (s >> 32); 125f6642de0SStanislav Fomichev 126f6642de0SStanislav Fomichev return csum_fold((__u32)s); 127f6642de0SStanislav Fomichev } 128f6642de0SStanislav Fomichev 12933181bb8SStanislav Fomichev #endif 130