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 const char *cc; 263d778983SMartin KaFai Lau int timeout_ms; 2743d2b88cSDaniel Borkmann bool must_fail; 283495e89cSDaniel Xu bool noconnect; 29e15a2209SDaniel Xu int type; 30e15a2209SDaniel Xu int proto; 31ed31adf6SGeliang Tang int (*post_socket_cb)(int fd, void *opts); 323d778983SMartin KaFai Lau }; 333d778983SMartin KaFai Lau 34488a23b8SStanislav Fomichev /* ipv4 test vector */ 35488a23b8SStanislav Fomichev struct ipv4_packet { 36488a23b8SStanislav Fomichev struct ethhdr eth; 37488a23b8SStanislav Fomichev struct iphdr iph; 38488a23b8SStanislav Fomichev struct tcphdr tcp; 39488a23b8SStanislav Fomichev } __packed; 40488a23b8SStanislav Fomichev extern struct ipv4_packet pkt_v4; 41488a23b8SStanislav Fomichev 42488a23b8SStanislav Fomichev /* ipv6 test vector */ 43488a23b8SStanislav Fomichev struct ipv6_packet { 44488a23b8SStanislav Fomichev struct ethhdr eth; 45488a23b8SStanislav Fomichev struct ipv6hdr iph; 46488a23b8SStanislav Fomichev struct tcphdr tcp; 47488a23b8SStanislav Fomichev } __packed; 48488a23b8SStanislav Fomichev extern struct ipv6_packet pkt_v6; 4933181bb8SStanislav Fomichev 50096eccdeSJussi Maki int settimeo(int fd, int timeout_ms); 51*6f802cb8SGeliang Tang int start_server_str(int family, int type, const char *addr_str, __u16 port, 52*6f802cb8SGeliang Tang const struct network_helper_opts *opts); 5399126abeSMartin KaFai Lau int start_server(int family, int type, const char *addr, __u16 port, 5499126abeSMartin KaFai Lau int timeout_ms); 55eed92afdSMartin KaFai Lau int *start_reuseport_server(int family, int type, const char *addr_str, 56eed92afdSMartin KaFai Lau __u16 port, int timeout_ms, 57eed92afdSMartin KaFai Lau unsigned int nr_listens); 589c598a83SGeliang Tang int start_server_addr(int type, const struct sockaddr_storage *addr, socklen_t len, 599c598a83SGeliang Tang const struct network_helper_opts *opts); 60eed92afdSMartin KaFai Lau void free_fds(int *fds, unsigned int nr_close_fds); 61db9994d0SGeliang Tang int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t len, 62db9994d0SGeliang Tang const struct network_helper_opts *opts); 6399126abeSMartin KaFai Lau int connect_to_fd(int server_fd, int timeout_ms); 643d778983SMartin KaFai Lau int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts); 6599126abeSMartin KaFai Lau int connect_fd_to_fd(int client_fd, int server_fd, int timeout_ms); 668085e1dcSMartin KaFai Lau int fastopen_connect(int server_fd, const char *data, unsigned int data_len, 678085e1dcSMartin KaFai Lau int timeout_ms); 680ab5539fSJakub Sitnicki int make_sockaddr(int family, const char *addr_str, __u16 port, 690ab5539fSJakub Sitnicki struct sockaddr_storage *addr, socklen_t *len); 70372642eaSStanislav Fomichev char *ping_command(int family); 71176ba657SAditi Ghag int get_socket_local_port(int sock_fd); 7290a695c3STushar Vyavahare int get_hw_ring_size(char *ifname, struct ethtool_ringparam *ring_param); 73bee3a7b0STushar Vyavahare int set_hw_ring_size(char *ifname, struct ethtool_ringparam *ring_param); 7433181bb8SStanislav Fomichev 75a3033884SToke Høiland-Jørgensen struct nstoken; 76a3033884SToke Høiland-Jørgensen /** 77a3033884SToke Høiland-Jørgensen * open_netns() - Switch to specified network namespace by name. 78a3033884SToke Høiland-Jørgensen * 79a3033884SToke Høiland-Jørgensen * Returns token with which to restore the original namespace 80a3033884SToke Høiland-Jørgensen * using close_netns(). 81a3033884SToke Høiland-Jørgensen */ 82a3033884SToke Høiland-Jørgensen struct nstoken *open_netns(const char *name); 83a3033884SToke Høiland-Jørgensen void close_netns(struct nstoken *token); 84dc34e44eSGeliang Tang int send_recv_data(int lfd, int fd, uint32_t total_bytes); 85f6642de0SStanislav Fomichev 86f6642de0SStanislav Fomichev static __u16 csum_fold(__u32 csum) 87f6642de0SStanislav Fomichev { 88f6642de0SStanislav Fomichev csum = (csum & 0xffff) + (csum >> 16); 89f6642de0SStanislav Fomichev csum = (csum & 0xffff) + (csum >> 16); 90f6642de0SStanislav Fomichev 91f6642de0SStanislav Fomichev return (__u16)~csum; 92f6642de0SStanislav Fomichev } 93f6642de0SStanislav Fomichev 94f6642de0SStanislav Fomichev static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr, 95f6642de0SStanislav Fomichev __u32 len, __u8 proto, 96f6642de0SStanislav Fomichev __wsum csum) 97f6642de0SStanislav Fomichev { 98f6642de0SStanislav Fomichev __u64 s = csum; 99f6642de0SStanislav Fomichev 100f6642de0SStanislav Fomichev s += (__u32)saddr; 101f6642de0SStanislav Fomichev s += (__u32)daddr; 102f6642de0SStanislav Fomichev s += htons(proto + len); 103f6642de0SStanislav Fomichev s = (s & 0xffffffff) + (s >> 32); 104f6642de0SStanislav Fomichev s = (s & 0xffffffff) + (s >> 32); 105f6642de0SStanislav Fomichev 106f6642de0SStanislav Fomichev return csum_fold((__u32)s); 107f6642de0SStanislav Fomichev } 108f6642de0SStanislav Fomichev 109f6642de0SStanislav Fomichev static inline __sum16 csum_ipv6_magic(const struct in6_addr *saddr, 110f6642de0SStanislav Fomichev const struct in6_addr *daddr, 111f6642de0SStanislav Fomichev __u32 len, __u8 proto, 112f6642de0SStanislav Fomichev __wsum csum) 113f6642de0SStanislav Fomichev { 114f6642de0SStanislav Fomichev __u64 s = csum; 115f6642de0SStanislav Fomichev int i; 116f6642de0SStanislav Fomichev 117f6642de0SStanislav Fomichev for (i = 0; i < 4; i++) 118f6642de0SStanislav Fomichev s += (__u32)saddr->s6_addr32[i]; 119f6642de0SStanislav Fomichev for (i = 0; i < 4; i++) 120f6642de0SStanislav Fomichev s += (__u32)daddr->s6_addr32[i]; 121f6642de0SStanislav Fomichev s += htons(proto + len); 122f6642de0SStanislav Fomichev s = (s & 0xffffffff) + (s >> 32); 123f6642de0SStanislav Fomichev s = (s & 0xffffffff) + (s >> 32); 124f6642de0SStanislav Fomichev 125f6642de0SStanislav Fomichev return csum_fold((__u32)s); 126f6642de0SStanislav Fomichev } 127f6642de0SStanislav Fomichev 12833181bb8SStanislav Fomichev #endif 129