brintos

brintos / linux-shallow public Read only

0
0
Text · 4.6 KiB · c72c16e Raw
160 lines · c
1/* SPDX-License-Identifier: GPL-2.0 */2#ifndef __NETWORK_HELPERS_H3#define __NETWORK_HELPERS_H4#include <sys/socket.h>5#include <sys/types.h>6#include <linux/types.h>7typedef __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 0x123420#define NUM_ITER 10000021#define VIP_NUM 522#define MAGIC_BYTES 12323 24struct network_helper_opts {25	int timeout_ms;26	int proto;27	/* +ve: Passed to listen() as-is.28	 *   0: Default when the test does not set29	 *      a particular value during the struct init.30	 *      It is changed to 1 before passing to listen().31	 *      Most tests only have one on-going connection.32	 * -ve: It is changed to 0 before passing to listen().33	 *      It is useful to force syncookie without34	 *	changing the "tcp_syncookies" sysctl from 1 to 2.35	 */36	int backlog;37	int (*post_socket_cb)(int fd, void *opts);38	void *cb_opts;39};40 41/* ipv4 test vector */42struct ipv4_packet {43	struct ethhdr eth;44	struct iphdr iph;45	struct tcphdr tcp;46} __packed;47extern struct ipv4_packet pkt_v4;48 49/* ipv6 test vector */50struct ipv6_packet {51	struct ethhdr eth;52	struct ipv6hdr iph;53	struct tcphdr tcp;54} __packed;55extern struct ipv6_packet pkt_v6;56 57int settimeo(int fd, int timeout_ms);58int start_server_str(int family, int type, const char *addr_str, __u16 port,59		     const struct network_helper_opts *opts);60int start_server(int family, int type, const char *addr, __u16 port,61		 int timeout_ms);62int *start_reuseport_server(int family, int type, const char *addr_str,63			    __u16 port, int timeout_ms,64			    unsigned int nr_listens);65int start_server_addr(int type, const struct sockaddr_storage *addr, socklen_t len,66		      const struct network_helper_opts *opts);67void free_fds(int *fds, unsigned int nr_close_fds);68int client_socket(int family, int type,69		  const struct network_helper_opts *opts);70int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t len,71		    const struct network_helper_opts *opts);72int connect_to_addr_str(int family, int type, const char *addr_str, __u16 port,73			const struct network_helper_opts *opts);74int connect_to_fd(int server_fd, int timeout_ms);75int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts);76int connect_fd_to_fd(int client_fd, int server_fd, int timeout_ms);77int fastopen_connect(int server_fd, const char *data, unsigned int data_len,78		     int timeout_ms);79int make_sockaddr(int family, const char *addr_str, __u16 port,80		  struct sockaddr_storage *addr, socklen_t *len);81char *ping_command(int family);82int get_socket_local_port(int sock_fd);83int get_hw_ring_size(char *ifname, struct ethtool_ringparam *ring_param);84int set_hw_ring_size(char *ifname, struct ethtool_ringparam *ring_param);85 86struct nstoken;87/**88 * open_netns() - Switch to specified network namespace by name.89 *90 * Returns token with which to restore the original namespace91 * using close_netns().92 */93struct nstoken *open_netns(const char *name);94void close_netns(struct nstoken *token);95int send_recv_data(int lfd, int fd, uint32_t total_bytes);96int make_netns(const char *name);97int remove_netns(const char *name);98 99static __u16 csum_fold(__u32 csum)100{101	csum = (csum & 0xffff) + (csum >> 16);102	csum = (csum & 0xffff) + (csum >> 16);103 104	return (__u16)~csum;105}106 107static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,108					__u32 len, __u8 proto,109					__wsum csum)110{111	__u64 s = csum;112 113	s += (__u32)saddr;114	s += (__u32)daddr;115	s += htons(proto + len);116	s = (s & 0xffffffff) + (s >> 32);117	s = (s & 0xffffffff) + (s >> 32);118 119	return csum_fold((__u32)s);120}121 122static inline __sum16 csum_ipv6_magic(const struct in6_addr *saddr,123				      const struct in6_addr *daddr,124					__u32 len, __u8 proto,125					__wsum csum)126{127	__u64 s = csum;128	int i;129 130	for (i = 0; i < 4; i++)131		s += (__u32)saddr->s6_addr32[i];132	for (i = 0; i < 4; i++)133		s += (__u32)daddr->s6_addr32[i];134	s += htons(proto + len);135	s = (s & 0xffffffff) + (s >> 32);136	s = (s & 0xffffffff) + (s >> 32);137 138	return csum_fold((__u32)s);139}140 141struct tmonitor_ctx;142 143#ifdef TRAFFIC_MONITOR144struct tmonitor_ctx *traffic_monitor_start(const char *netns, const char *test_name,145					   const char *subtest_name);146void traffic_monitor_stop(struct tmonitor_ctx *ctx);147#else148static inline struct tmonitor_ctx *traffic_monitor_start(const char *netns, const char *test_name,149							 const char *subtest_name)150{151	return NULL;152}153 154static inline void traffic_monitor_stop(struct tmonitor_ctx *ctx)155{156}157#endif158 159#endif160