xref: /linux/tools/testing/selftests/bpf/xskxceiver.h (revision 702648721db590b3425c31ade294000e18808345)
1 /* SPDX-License-Identifier: GPL-2.0
2  * Copyright(c) 2020 Intel Corporation.
3  */
4 
5 #ifndef XSKXCEIVER_H_
6 #define XSKXCEIVER_H_
7 
8 #include "xsk_xdp_progs.skel.h"
9 
10 #ifndef SOL_XDP
11 #define SOL_XDP 283
12 #endif
13 
14 #ifndef AF_XDP
15 #define AF_XDP 44
16 #endif
17 
18 #ifndef PF_XDP
19 #define PF_XDP AF_XDP
20 #endif
21 
22 #ifndef SO_BUSY_POLL_BUDGET
23 #define SO_BUSY_POLL_BUDGET 70
24 #endif
25 
26 #ifndef SO_PREFER_BUSY_POLL
27 #define SO_PREFER_BUSY_POLL 69
28 #endif
29 
30 #define TEST_PASS 0
31 #define TEST_FAILURE -1
32 #define TEST_CONTINUE 1
33 #define MAX_INTERFACES 2
34 #define MAX_INTERFACE_NAME_CHARS 16
35 #define MAX_SOCKETS 2
36 #define MAX_TEST_NAME_SIZE 32
37 #define MAX_TEARDOWN_ITER 10
38 #define PKT_HDR_SIZE (sizeof(struct ethhdr) + sizeof(struct iphdr) + \
39 			sizeof(struct udphdr))
40 #define MIN_ETH_PKT_SIZE 64
41 #define ETH_FCS_SIZE 4
42 #define MIN_PKT_SIZE (MIN_ETH_PKT_SIZE - ETH_FCS_SIZE)
43 #define PKT_SIZE (MIN_PKT_SIZE)
44 #define IP_PKT_SIZE (PKT_SIZE - sizeof(struct ethhdr))
45 #define IP_PKT_VER 0x4
46 #define IP_PKT_TOS 0x9
47 #define UDP_PKT_SIZE (IP_PKT_SIZE - sizeof(struct iphdr))
48 #define UDP_PKT_DATA_SIZE (UDP_PKT_SIZE - sizeof(struct udphdr))
49 #define USLEEP_MAX 10000
50 #define SOCK_RECONF_CTR 10
51 #define BATCH_SIZE 64
52 #define POLL_TMOUT 1000
53 #define THREAD_TMOUT 3
54 #define DEFAULT_PKT_CNT (4 * 1024)
55 #define DEFAULT_UMEM_BUFFERS (DEFAULT_PKT_CNT / 4)
56 #define RX_FULL_RXQSIZE 32
57 #define UMEM_HEADROOM_TEST_SIZE 128
58 #define XSK_UMEM__INVALID_FRAME_SIZE (XSK_UMEM__DEFAULT_FRAME_SIZE + 1)
59 #define HUGEPAGE_SIZE (2 * 1024 * 1024)
60 
61 #define print_verbose(x...) do { if (opt_verbose) ksft_print_msg(x); } while (0)
62 
63 enum test_mode {
64 	TEST_MODE_SKB,
65 	TEST_MODE_DRV,
66 	TEST_MODE_ZC,
67 	TEST_MODE_MAX
68 };
69 
70 enum test_type {
71 	TEST_TYPE_RUN_TO_COMPLETION,
72 	TEST_TYPE_RUN_TO_COMPLETION_2K_FRAME,
73 	TEST_TYPE_RUN_TO_COMPLETION_SINGLE_PKT,
74 	TEST_TYPE_RX_POLL,
75 	TEST_TYPE_TX_POLL,
76 	TEST_TYPE_POLL_RXQ_TMOUT,
77 	TEST_TYPE_POLL_TXQ_TMOUT,
78 	TEST_TYPE_UNALIGNED,
79 	TEST_TYPE_ALIGNED_INV_DESC,
80 	TEST_TYPE_ALIGNED_INV_DESC_2K_FRAME,
81 	TEST_TYPE_UNALIGNED_INV_DESC,
82 	TEST_TYPE_UNALIGNED_INV_DESC_4K1_FRAME,
83 	TEST_TYPE_HEADROOM,
84 	TEST_TYPE_TEARDOWN,
85 	TEST_TYPE_BIDI,
86 	TEST_TYPE_STATS_RX_DROPPED,
87 	TEST_TYPE_STATS_TX_INVALID_DESCS,
88 	TEST_TYPE_STATS_RX_FULL,
89 	TEST_TYPE_STATS_FILL_EMPTY,
90 	TEST_TYPE_BPF_RES,
91 	TEST_TYPE_XDP_DROP_HALF,
92 	TEST_TYPE_XDP_METADATA_COUNT,
93 	TEST_TYPE_MAX
94 };
95 
96 static bool opt_pkt_dump;
97 static bool opt_verbose;
98 
99 struct xsk_umem_info {
100 	struct xsk_ring_prod fq;
101 	struct xsk_ring_cons cq;
102 	struct xsk_umem *umem;
103 	u32 num_frames;
104 	u32 frame_headroom;
105 	void *buffer;
106 	u32 frame_size;
107 	u32 base_addr;
108 	bool unaligned_mode;
109 };
110 
111 struct xsk_socket_info {
112 	struct xsk_ring_cons rx;
113 	struct xsk_ring_prod tx;
114 	struct xsk_umem_info *umem;
115 	struct xsk_socket *xsk;
116 	u32 outstanding_tx;
117 	u32 rxqsize;
118 };
119 
120 struct pkt {
121 	u64 addr;
122 	u32 len;
123 	u32 payload;
124 	bool valid;
125 };
126 
127 struct pkt_stream {
128 	u32 nb_pkts;
129 	u32 rx_pkt_nb;
130 	struct pkt *pkts;
131 	bool use_addr_for_fill;
132 };
133 
134 struct ifobject;
135 typedef int (*validation_func_t)(struct ifobject *ifobj);
136 typedef void *(*thread_func_t)(void *arg);
137 
138 struct ifobject {
139 	char ifname[MAX_INTERFACE_NAME_CHARS];
140 	struct xsk_socket_info *xsk;
141 	struct xsk_socket_info *xsk_arr;
142 	struct xsk_umem_info *umem;
143 	thread_func_t func_ptr;
144 	validation_func_t validation_func;
145 	struct pkt_stream *pkt_stream;
146 	struct xsk_xdp_progs *xdp_progs;
147 	struct bpf_map *xskmap;
148 	struct bpf_program *xdp_prog;
149 	enum test_mode mode;
150 	int ifindex;
151 	u32 dst_ip;
152 	u32 src_ip;
153 	u32 bind_flags;
154 	u16 src_port;
155 	u16 dst_port;
156 	bool tx_on;
157 	bool rx_on;
158 	bool use_poll;
159 	bool busy_poll;
160 	bool use_fill_ring;
161 	bool release_rx;
162 	bool shared_umem;
163 	bool use_metadata;
164 	u8 dst_mac[ETH_ALEN];
165 	u8 src_mac[ETH_ALEN];
166 };
167 
168 struct test_spec {
169 	struct ifobject *ifobj_tx;
170 	struct ifobject *ifobj_rx;
171 	struct pkt_stream *tx_pkt_stream_default;
172 	struct pkt_stream *rx_pkt_stream_default;
173 	struct bpf_program *xdp_prog_rx;
174 	struct bpf_program *xdp_prog_tx;
175 	struct bpf_map *xskmap_rx;
176 	struct bpf_map *xskmap_tx;
177 	u16 total_steps;
178 	u16 current_step;
179 	u16 nb_sockets;
180 	bool fail;
181 	enum test_mode mode;
182 	char name[MAX_TEST_NAME_SIZE];
183 };
184 
185 pthread_barrier_t barr;
186 pthread_mutex_t pacing_mutex = PTHREAD_MUTEX_INITIALIZER;
187 pthread_cond_t pacing_cond = PTHREAD_COND_INITIALIZER;
188 
189 int pkts_in_flight;
190 
191 #endif				/* XSKXCEIVER_H_ */
192