xref: /linux/tools/testing/selftests/bpf/xdp_lb_bench_common.h (revision 0eaed89c18aeedf0898baf2dbf5ff027c6795152)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
3 
4 #ifndef XDP_LB_BENCH_COMMON_H
5 #define XDP_LB_BENCH_COMMON_H
6 
7 #define F_IPV6		(1 << 0)
8 #define F_LRU_BYPASS	(1 << 1)
9 
10 #define CH_RING_SIZE	65537		/* per-VIP consistent hash ring slots */
11 #define MAX_VIPS	16
12 #define CH_RINGS_SIZE	(MAX_VIPS * CH_RING_SIZE)
13 #define MAX_REALS	512
14 #define DEFAULT_LRU_SIZE 100000		/* connection tracking cache size */
15 #define ONE_SEC		1000000000U	/* 1 sec in nanosec */
16 #define MAX_CONN_RATE	100000000	/* high enough to never trigger in bench */
17 #define LRU_UDP_TIMEOUT	30000000000ULL	/* 30 sec in nanosec */
18 #define PCKT_FRAGMENTED	0x3FFF
19 #define KNUTH_HASH_MULT	2654435761U
20 #define IPIP_V4_PREFIX	4268		/* 172.16/12 in network order */
21 #define IPIP_V6_PREFIX1	1		/* 0100::/64 (RFC 6666 discard) */
22 #define IPIP_V6_PREFIX2	0
23 #define IPIP_V6_PREFIX3	0
24 
25 /* Stats indices (0..MAX_VIPS-1 are per-VIP packet/byte counters) */
26 #define STATS_LRU	(MAX_VIPS + 0)	/* v1: total VIP packets, v2: LRU misses */
27 #define STATS_XDP_TX	(MAX_VIPS + 1)
28 #define STATS_XDP_PASS	(MAX_VIPS + 2)
29 #define STATS_XDP_DROP	(MAX_VIPS + 3)
30 #define STATS_NEW_CONN	(MAX_VIPS + 4)	/* v1: conn count, v2: last reset ts */
31 #define STATS_LRU_MISS	(MAX_VIPS + 5)	/* v1: TCP LRU misses */
32 #define STATS_SIZE	(MAX_VIPS + 6)
33 
34 #ifdef __BPF__
35 #define lb_htons(x)	bpf_htons(x)
36 #define LB_INLINE	static __always_inline
37 #else
38 #define lb_htons(x)	htons(x)
39 #define LB_INLINE	static inline
40 #endif
41 
42 LB_INLINE __be32 create_encap_ipv4_src(__u16 port, __be32 src)
43 {
44 	__u32 ip_suffix = lb_htons(port);
45 
46 	ip_suffix <<= 16;
47 	ip_suffix ^= src;
48 	return (0xFFFF0000 & ip_suffix) | IPIP_V4_PREFIX;
49 }
50 
51 LB_INLINE void create_encap_ipv6_src(__u16 port, __be32 src, __be32 *saddr)
52 {
53 	saddr[0] = IPIP_V6_PREFIX1;
54 	saddr[1] = IPIP_V6_PREFIX2;
55 	saddr[2] = IPIP_V6_PREFIX3;
56 	saddr[3] = src ^ port;
57 }
58 
59 struct flow_key {
60 	union {
61 		__be32 src;
62 		__be32 srcv6[4];
63 	};
64 	union {
65 		__be32 dst;
66 		__be32 dstv6[4];
67 	};
68 	union {
69 		__u32 ports;
70 		__u16 port16[2];
71 	};
72 	__u8 proto;
73 	__u8 pad[3];
74 };
75 
76 struct vip_definition {
77 	union {
78 		__be32 vip;
79 		__be32 vipv6[4];
80 	};
81 	__u16 port;
82 	__u8 proto;
83 	__u8 pad;
84 };
85 
86 struct vip_meta {
87 	__u32 flags;
88 	__u32 vip_num;
89 };
90 
91 struct real_pos_lru {
92 	__u32 pos;
93 	__u64 atime;
94 };
95 
96 struct real_definition {
97 	__be32 dst;
98 	__be32 dstv6[4];
99 	__u8   flags;
100 };
101 
102 struct lb_stats {
103 	__u64 v1;
104 	__u64 v2;
105 };
106 
107 struct ctl_value {
108 	__u8 mac[6];
109 	__u8 pad[2];
110 };
111 
112 #endif /* XDP_LB_BENCH_COMMON_H */
113