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 TEST_SKIP 2
34 #define MAX_INTERFACES 2
35 #define MAX_INTERFACE_NAME_CHARS 16
36 #define MAX_SOCKETS 2
37 #define MAX_TEST_NAME_SIZE 32
38 #define MAX_TEARDOWN_ITER 10
39 #define PKT_HDR_SIZE (sizeof(struct ethhdr) + 2) /* Just to align the data in the packet */
40 #define MIN_PKT_SIZE 64
41 #define MAX_ETH_PKT_SIZE 1518
42 #define MAX_ETH_JUMBO_SIZE 9000
43 #define USLEEP_MAX 10000
44 #define SOCK_RECONF_CTR 10
45 #define BATCH_SIZE 64
46 #define POLL_TMOUT 1000
47 #define THREAD_TMOUT 3
48 #define DEFAULT_PKT_CNT (4 * 1024)
49 #define DEFAULT_UMEM_BUFFERS (DEFAULT_PKT_CNT / 4)
50 #define RX_FULL_RXQSIZE 32
51 #define UMEM_HEADROOM_TEST_SIZE 128
52 #define XSK_UMEM__INVALID_FRAME_SIZE (MAX_ETH_JUMBO_SIZE + 1)
53 #define XSK_UMEM__LARGE_FRAME_SIZE (3 * 1024)
54 #define XSK_UMEM__MAX_FRAME_SIZE (4 * 1024)
55 #define XSK_DESC__INVALID_OPTION (0xffff)
56 #define XSK_DESC__MAX_SKB_FRAGS 18
57 #define HUGEPAGE_SIZE (2 * 1024 * 1024)
58 #define PKT_DUMP_NB_TO_PRINT 16
59 
60 #define print_verbose(x...) do { if (opt_verbose) ksft_print_msg(x); } while (0)
61 
62 enum test_mode {
63 	TEST_MODE_SKB,
64 	TEST_MODE_DRV,
65 	TEST_MODE_ZC,
66 	TEST_MODE_MAX
67 };
68 
69 enum test_type {
70 	TEST_TYPE_RUN_TO_COMPLETION,
71 	TEST_TYPE_RUN_TO_COMPLETION_2K_FRAME,
72 	TEST_TYPE_RUN_TO_COMPLETION_SINGLE_PKT,
73 	TEST_TYPE_RX_POLL,
74 	TEST_TYPE_TX_POLL,
75 	TEST_TYPE_POLL_RXQ_TMOUT,
76 	TEST_TYPE_POLL_TXQ_TMOUT,
77 	TEST_TYPE_UNALIGNED,
78 	TEST_TYPE_ALIGNED_INV_DESC,
79 	TEST_TYPE_ALIGNED_INV_DESC_2K_FRAME,
80 	TEST_TYPE_UNALIGNED_INV_DESC,
81 	TEST_TYPE_UNALIGNED_INV_DESC_4K1_FRAME,
82 	TEST_TYPE_HEADROOM,
83 	TEST_TYPE_TEARDOWN,
84 	TEST_TYPE_BIDI,
85 	TEST_TYPE_STATS_RX_DROPPED,
86 	TEST_TYPE_STATS_TX_INVALID_DESCS,
87 	TEST_TYPE_STATS_RX_FULL,
88 	TEST_TYPE_STATS_FILL_EMPTY,
89 	TEST_TYPE_BPF_RES,
90 	TEST_TYPE_XDP_DROP_HALF,
91 	TEST_TYPE_XDP_METADATA_COUNT,
92 	TEST_TYPE_XDP_METADATA_COUNT_MB,
93 	TEST_TYPE_RUN_TO_COMPLETION_MB,
94 	TEST_TYPE_UNALIGNED_MB,
95 	TEST_TYPE_ALIGNED_INV_DESC_MB,
96 	TEST_TYPE_UNALIGNED_INV_DESC_MB,
97 	TEST_TYPE_TOO_MANY_FRAGS,
98 	TEST_TYPE_MAX
99 };
100 
101 static bool opt_verbose;
102 
103 struct xsk_umem_info {
104 	struct xsk_ring_prod fq;
105 	struct xsk_ring_cons cq;
106 	struct xsk_umem *umem;
107 	u64 next_buffer;
108 	u32 num_frames;
109 	u32 frame_headroom;
110 	void *buffer;
111 	u32 frame_size;
112 	u32 base_addr;
113 	bool unaligned_mode;
114 };
115 
116 struct xsk_socket_info {
117 	struct xsk_ring_cons rx;
118 	struct xsk_ring_prod tx;
119 	struct xsk_umem_info *umem;
120 	struct xsk_socket *xsk;
121 	u32 outstanding_tx;
122 	u32 rxqsize;
123 };
124 
125 struct pkt {
126 	int offset;
127 	u32 len;
128 	u32 pkt_nb;
129 	bool valid;
130 	u16 options;
131 };
132 
133 struct pkt_stream {
134 	u32 nb_pkts;
135 	u32 current_pkt_nb;
136 	struct pkt *pkts;
137 	u32 max_pkt_len;
138 	bool verbatim;
139 };
140 
141 struct ifobject;
142 typedef int (*validation_func_t)(struct ifobject *ifobj);
143 typedef void *(*thread_func_t)(void *arg);
144 
145 struct ifobject {
146 	char ifname[MAX_INTERFACE_NAME_CHARS];
147 	struct xsk_socket_info *xsk;
148 	struct xsk_socket_info *xsk_arr;
149 	struct xsk_umem_info *umem;
150 	thread_func_t func_ptr;
151 	validation_func_t validation_func;
152 	struct pkt_stream *pkt_stream;
153 	struct xsk_xdp_progs *xdp_progs;
154 	struct bpf_map *xskmap;
155 	struct bpf_program *xdp_prog;
156 	enum test_mode mode;
157 	int ifindex;
158 	int mtu;
159 	u32 bind_flags;
160 	u32 xdp_zc_max_segs;
161 	bool tx_on;
162 	bool rx_on;
163 	bool use_poll;
164 	bool busy_poll;
165 	bool use_fill_ring;
166 	bool release_rx;
167 	bool shared_umem;
168 	bool use_metadata;
169 	bool unaligned_supp;
170 	bool multi_buff_supp;
171 	bool multi_buff_zc_supp;
172 	u8 dst_mac[ETH_ALEN];
173 	u8 src_mac[ETH_ALEN];
174 };
175 
176 struct test_spec {
177 	struct ifobject *ifobj_tx;
178 	struct ifobject *ifobj_rx;
179 	struct pkt_stream *tx_pkt_stream_default;
180 	struct pkt_stream *rx_pkt_stream_default;
181 	struct bpf_program *xdp_prog_rx;
182 	struct bpf_program *xdp_prog_tx;
183 	struct bpf_map *xskmap_rx;
184 	struct bpf_map *xskmap_tx;
185 	int mtu;
186 	u16 total_steps;
187 	u16 current_step;
188 	u16 nb_sockets;
189 	bool fail;
190 	enum test_mode mode;
191 	char name[MAX_TEST_NAME_SIZE];
192 };
193 
194 pthread_barrier_t barr;
195 pthread_mutex_t pacing_mutex = PTHREAD_MUTEX_INITIALIZER;
196 
197 int pkts_in_flight;
198 
199 #endif				/* XSKXCEIVER_H_ */
200