network_helpers.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __NETWORK_HELPERS_H
  3. #define __NETWORK_HELPERS_H
  4. #include <arpa/inet.h>
  5. #include <sys/socket.h>
  6. #include <sys/types.h>
  7. #include <linux/types.h>
  8. typedef __u16 __sum16;
  9. #include <linux/if_ether.h>
  10. #include <linux/if_packet.h>
  11. #include <linux/ip.h>
  12. #include <linux/ipv6.h>
  13. #include <linux/ethtool.h>
  14. #include <linux/sockios.h>
  15. #include <linux/err.h>
  16. #include <netinet/tcp.h>
  17. #include <bpf/bpf_endian.h>
  18. #include <net/if.h>
  19. #define MAGIC_VAL 0x1234
  20. #define NUM_ITER 100000
  21. #define VIP_NUM 5
  22. #define MAGIC_BYTES 123
  23. struct network_helper_opts {
  24. int timeout_ms;
  25. int proto;
  26. /* +ve: Passed to listen() as-is.
  27. * 0: Default when the test does not set
  28. * a particular value during the struct init.
  29. * It is changed to 1 before passing to listen().
  30. * Most tests only have one on-going connection.
  31. * -ve: It is changed to 0 before passing to listen().
  32. * It is useful to force syncookie without
  33. * changing the "tcp_syncookies" sysctl from 1 to 2.
  34. */
  35. int backlog;
  36. int (*post_socket_cb)(int fd, void *opts);
  37. void *cb_opts;
  38. };
  39. /* ipv4 test vector */
  40. struct ipv4_packet {
  41. struct ethhdr eth;
  42. struct iphdr iph;
  43. struct tcphdr tcp;
  44. } __packed;
  45. extern struct ipv4_packet pkt_v4;
  46. /* ipv6 test vector */
  47. struct ipv6_packet {
  48. struct ethhdr eth;
  49. struct ipv6hdr iph;
  50. struct tcphdr tcp;
  51. } __packed;
  52. extern struct ipv6_packet pkt_v6;
  53. int settimeo(int fd, int timeout_ms);
  54. int start_server_str(int family, int type, const char *addr_str, __u16 port,
  55. const struct network_helper_opts *opts);
  56. int start_server(int family, int type, const char *addr, __u16 port,
  57. int timeout_ms);
  58. int *start_reuseport_server(int family, int type, const char *addr_str,
  59. __u16 port, int timeout_ms,
  60. unsigned int nr_listens);
  61. int start_server_addr(int type, const struct sockaddr_storage *addr, socklen_t len,
  62. const struct network_helper_opts *opts);
  63. void free_fds(int *fds, unsigned int nr_close_fds);
  64. int client_socket(int family, int type,
  65. const struct network_helper_opts *opts);
  66. int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t len,
  67. const struct network_helper_opts *opts);
  68. int connect_to_addr_str(int family, int type, const char *addr_str, __u16 port,
  69. const struct network_helper_opts *opts);
  70. int connect_to_fd(int server_fd, int timeout_ms);
  71. int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts);
  72. int connect_fd_to_fd(int client_fd, int server_fd, int timeout_ms);
  73. int fastopen_connect(int server_fd, const char *data, unsigned int data_len,
  74. int timeout_ms);
  75. int make_sockaddr(int family, const char *addr_str, __u16 port,
  76. struct sockaddr_storage *addr, socklen_t *len);
  77. char *ping_command(int family);
  78. int get_socket_local_port(int sock_fd);
  79. int get_hw_ring_size(char *ifname, struct ethtool_ringparam *ring_param);
  80. int set_hw_ring_size(char *ifname, struct ethtool_ringparam *ring_param);
  81. struct nstoken;
  82. /**
  83. * open_netns() - Switch to specified network namespace by name.
  84. *
  85. * Returns token with which to restore the original namespace
  86. * using close_netns().
  87. */
  88. struct nstoken *open_netns(const char *name);
  89. void close_netns(struct nstoken *token);
  90. int send_recv_data(int lfd, int fd, uint32_t total_bytes);
  91. int make_netns(const char *name);
  92. int remove_netns(const char *name);
  93. static __u16 csum_fold(__u32 csum)
  94. {
  95. csum = (csum & 0xffff) + (csum >> 16);
  96. csum = (csum & 0xffff) + (csum >> 16);
  97. return (__u16)~csum;
  98. }
  99. static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
  100. __u32 len, __u8 proto,
  101. __wsum csum)
  102. {
  103. __u64 s = csum;
  104. s += (__u32)saddr;
  105. s += (__u32)daddr;
  106. s += htons(proto + len);
  107. s = (s & 0xffffffff) + (s >> 32);
  108. s = (s & 0xffffffff) + (s >> 32);
  109. return csum_fold((__u32)s);
  110. }
  111. static inline __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
  112. const struct in6_addr *daddr,
  113. __u32 len, __u8 proto,
  114. __wsum csum)
  115. {
  116. __u64 s = csum;
  117. int i;
  118. for (i = 0; i < 4; i++)
  119. s += (__u32)saddr->s6_addr32[i];
  120. for (i = 0; i < 4; i++)
  121. s += (__u32)daddr->s6_addr32[i];
  122. s += htons(proto + len);
  123. s = (s & 0xffffffff) + (s >> 32);
  124. s = (s & 0xffffffff) + (s >> 32);
  125. return csum_fold((__u32)s);
  126. }
  127. struct tmonitor_ctx;
  128. #ifdef TRAFFIC_MONITOR
  129. struct tmonitor_ctx *traffic_monitor_start(const char *netns, const char *test_name,
  130. const char *subtest_name);
  131. void traffic_monitor_stop(struct tmonitor_ctx *ctx);
  132. #else
  133. static inline struct tmonitor_ctx *traffic_monitor_start(const char *netns, const char *test_name,
  134. const char *subtest_name)
  135. {
  136. return NULL;
  137. }
  138. static inline void traffic_monitor_stop(struct tmonitor_ctx *ctx)
  139. {
  140. }
  141. #endif
  142. #endif