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> 14488a23b8SStanislav Fomichev #include <netinet/tcp.h> 15488a23b8SStanislav Fomichev #include <bpf/bpf_endian.h> 1690a695c3STushar Vyavahare #include <net/if.h> 17488a23b8SStanislav Fomichev 18488a23b8SStanislav Fomichev #define MAGIC_VAL 0x1234 19488a23b8SStanislav Fomichev #define NUM_ITER 100000 20488a23b8SStanislav Fomichev #define VIP_NUM 5 21488a23b8SStanislav Fomichev #define MAGIC_BYTES 123 22488a23b8SStanislav Fomichev 233d778983SMartin KaFai Lau struct network_helper_opts { 243d778983SMartin KaFai Lau const char *cc; 253d778983SMartin KaFai Lau int timeout_ms; 2643d2b88cSDaniel Borkmann bool must_fail; 273495e89cSDaniel Xu bool noconnect; 28e15a2209SDaniel Xu int type; 29e15a2209SDaniel Xu int proto; 303d778983SMartin KaFai Lau }; 313d778983SMartin KaFai Lau 32488a23b8SStanislav Fomichev /* ipv4 test vector */ 33488a23b8SStanislav Fomichev struct ipv4_packet { 34488a23b8SStanislav Fomichev struct ethhdr eth; 35488a23b8SStanislav Fomichev struct iphdr iph; 36488a23b8SStanislav Fomichev struct tcphdr tcp; 37488a23b8SStanislav Fomichev } __packed; 38488a23b8SStanislav Fomichev extern struct ipv4_packet pkt_v4; 39488a23b8SStanislav Fomichev 40488a23b8SStanislav Fomichev /* ipv6 test vector */ 41488a23b8SStanislav Fomichev struct ipv6_packet { 42488a23b8SStanislav Fomichev struct ethhdr eth; 43488a23b8SStanislav Fomichev struct ipv6hdr iph; 44488a23b8SStanislav Fomichev struct tcphdr tcp; 45488a23b8SStanislav Fomichev } __packed; 46488a23b8SStanislav Fomichev extern struct ipv6_packet pkt_v6; 4733181bb8SStanislav Fomichev 48096eccdeSJussi Maki int settimeo(int fd, int timeout_ms); 4999126abeSMartin KaFai Lau int start_server(int family, int type, const char *addr, __u16 port, 5099126abeSMartin KaFai Lau int timeout_ms); 518039d353SNicolas Rybowski int start_mptcp_server(int family, const char *addr, __u16 port, 528039d353SNicolas Rybowski int timeout_ms); 53eed92afdSMartin KaFai Lau int *start_reuseport_server(int family, int type, const char *addr_str, 54eed92afdSMartin KaFai Lau __u16 port, int timeout_ms, 55eed92afdSMartin KaFai Lau unsigned int nr_listens); 569c598a83SGeliang Tang int start_server_addr(int type, const struct sockaddr_storage *addr, socklen_t len, 579c598a83SGeliang Tang const struct network_helper_opts *opts); 58eed92afdSMartin KaFai Lau void free_fds(int *fds, unsigned int nr_close_fds); 59*db9994d0SGeliang Tang int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t len, 60*db9994d0SGeliang Tang const struct network_helper_opts *opts); 6199126abeSMartin KaFai Lau int connect_to_fd(int server_fd, int timeout_ms); 623d778983SMartin KaFai Lau int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts); 6399126abeSMartin KaFai Lau int connect_fd_to_fd(int client_fd, int server_fd, int timeout_ms); 648085e1dcSMartin KaFai Lau int fastopen_connect(int server_fd, const char *data, unsigned int data_len, 658085e1dcSMartin KaFai Lau int timeout_ms); 660ab5539fSJakub Sitnicki int make_sockaddr(int family, const char *addr_str, __u16 port, 670ab5539fSJakub Sitnicki struct sockaddr_storage *addr, socklen_t *len); 68372642eaSStanislav Fomichev char *ping_command(int family); 69176ba657SAditi Ghag int get_socket_local_port(int sock_fd); 7090a695c3STushar Vyavahare int get_hw_ring_size(char *ifname, struct ethtool_ringparam *ring_param); 71bee3a7b0STushar Vyavahare int set_hw_ring_size(char *ifname, struct ethtool_ringparam *ring_param); 7233181bb8SStanislav Fomichev 73a3033884SToke Høiland-Jørgensen struct nstoken; 74a3033884SToke Høiland-Jørgensen /** 75a3033884SToke Høiland-Jørgensen * open_netns() - Switch to specified network namespace by name. 76a3033884SToke Høiland-Jørgensen * 77a3033884SToke Høiland-Jørgensen * Returns token with which to restore the original namespace 78a3033884SToke Høiland-Jørgensen * using close_netns(). 79a3033884SToke Høiland-Jørgensen */ 80a3033884SToke Høiland-Jørgensen struct nstoken *open_netns(const char *name); 81a3033884SToke Høiland-Jørgensen void close_netns(struct nstoken *token); 82dc34e44eSGeliang Tang int send_recv_data(int lfd, int fd, uint32_t total_bytes); 83f6642de0SStanislav Fomichev 84f6642de0SStanislav Fomichev static __u16 csum_fold(__u32 csum) 85f6642de0SStanislav Fomichev { 86f6642de0SStanislav Fomichev csum = (csum & 0xffff) + (csum >> 16); 87f6642de0SStanislav Fomichev csum = (csum & 0xffff) + (csum >> 16); 88f6642de0SStanislav Fomichev 89f6642de0SStanislav Fomichev return (__u16)~csum; 90f6642de0SStanislav Fomichev } 91f6642de0SStanislav Fomichev 92f6642de0SStanislav Fomichev static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr, 93f6642de0SStanislav Fomichev __u32 len, __u8 proto, 94f6642de0SStanislav Fomichev __wsum csum) 95f6642de0SStanislav Fomichev { 96f6642de0SStanislav Fomichev __u64 s = csum; 97f6642de0SStanislav Fomichev 98f6642de0SStanislav Fomichev s += (__u32)saddr; 99f6642de0SStanislav Fomichev s += (__u32)daddr; 100f6642de0SStanislav Fomichev s += htons(proto + len); 101f6642de0SStanislav Fomichev s = (s & 0xffffffff) + (s >> 32); 102f6642de0SStanislav Fomichev s = (s & 0xffffffff) + (s >> 32); 103f6642de0SStanislav Fomichev 104f6642de0SStanislav Fomichev return csum_fold((__u32)s); 105f6642de0SStanislav Fomichev } 106f6642de0SStanislav Fomichev 107f6642de0SStanislav Fomichev static inline __sum16 csum_ipv6_magic(const struct in6_addr *saddr, 108f6642de0SStanislav Fomichev const struct in6_addr *daddr, 109f6642de0SStanislav Fomichev __u32 len, __u8 proto, 110f6642de0SStanislav Fomichev __wsum csum) 111f6642de0SStanislav Fomichev { 112f6642de0SStanislav Fomichev __u64 s = csum; 113f6642de0SStanislav Fomichev int i; 114f6642de0SStanislav Fomichev 115f6642de0SStanislav Fomichev for (i = 0; i < 4; i++) 116f6642de0SStanislav Fomichev s += (__u32)saddr->s6_addr32[i]; 117f6642de0SStanislav Fomichev for (i = 0; i < 4; i++) 118f6642de0SStanislav Fomichev s += (__u32)daddr->s6_addr32[i]; 119f6642de0SStanislav Fomichev s += htons(proto + len); 120f6642de0SStanislav Fomichev s = (s & 0xffffffff) + (s >> 32); 121f6642de0SStanislav Fomichev s = (s & 0xffffffff) + (s >> 32); 122f6642de0SStanislav Fomichev 123f6642de0SStanislav Fomichev return csum_fold((__u32)s); 124f6642de0SStanislav Fomichev } 125f6642de0SStanislav Fomichev 12633181bb8SStanislav Fomichev #endif 127