xref: /linux/tools/testing/selftests/bpf/xskxceiver.h (revision 3a38ef2b3cb6b63c105247b5ea4a9cf600e673f0)
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 #ifndef SOL_XDP
9 #define SOL_XDP 283
10 #endif
11 
12 #ifndef AF_XDP
13 #define AF_XDP 44
14 #endif
15 
16 #ifndef PF_XDP
17 #define PF_XDP AF_XDP
18 #endif
19 
20 #ifndef SO_BUSY_POLL_BUDGET
21 #define SO_BUSY_POLL_BUDGET 70
22 #endif
23 
24 #ifndef SO_PREFER_BUSY_POLL
25 #define SO_PREFER_BUSY_POLL 69
26 #endif
27 
28 #define TEST_PASS 0
29 #define TEST_FAILURE -1
30 #define TEST_CONTINUE 1
31 #define MAX_INTERFACES 2
32 #define MAX_INTERFACE_NAME_CHARS 16
33 #define MAX_INTERFACES_NAMESPACE_CHARS 16
34 #define MAX_SOCKETS 2
35 #define MAX_TEST_NAME_SIZE 32
36 #define MAX_TEARDOWN_ITER 10
37 #define PKT_HDR_SIZE (sizeof(struct ethhdr) + sizeof(struct iphdr) + \
38 			sizeof(struct udphdr))
39 #define MIN_ETH_PKT_SIZE 64
40 #define ETH_FCS_SIZE 4
41 #define MIN_PKT_SIZE (MIN_ETH_PKT_SIZE - ETH_FCS_SIZE)
42 #define PKT_SIZE (MIN_PKT_SIZE)
43 #define IP_PKT_SIZE (PKT_SIZE - sizeof(struct ethhdr))
44 #define IP_PKT_VER 0x4
45 #define IP_PKT_TOS 0x9
46 #define UDP_PKT_SIZE (IP_PKT_SIZE - sizeof(struct iphdr))
47 #define UDP_PKT_DATA_SIZE (UDP_PKT_SIZE - sizeof(struct udphdr))
48 #define USLEEP_MAX 10000
49 #define SOCK_RECONF_CTR 10
50 #define BATCH_SIZE 64
51 #define POLL_TMOUT 1000
52 #define THREAD_TMOUT 3
53 #define DEFAULT_PKT_CNT (4 * 1024)
54 #define DEFAULT_UMEM_BUFFERS (DEFAULT_PKT_CNT / 4)
55 #define UMEM_SIZE (DEFAULT_UMEM_BUFFERS * XSK_UMEM__DEFAULT_FRAME_SIZE)
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 
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_HEADROOM,
82 	TEST_TYPE_TEARDOWN,
83 	TEST_TYPE_BIDI,
84 	TEST_TYPE_STATS_RX_DROPPED,
85 	TEST_TYPE_STATS_TX_INVALID_DESCS,
86 	TEST_TYPE_STATS_RX_FULL,
87 	TEST_TYPE_STATS_FILL_EMPTY,
88 	TEST_TYPE_BPF_RES,
89 	TEST_TYPE_MAX
90 };
91 
92 static bool opt_pkt_dump;
93 static bool opt_verbose;
94 
95 struct xsk_umem_info {
96 	struct xsk_ring_prod fq;
97 	struct xsk_ring_cons cq;
98 	struct xsk_umem *umem;
99 	u32 num_frames;
100 	u32 frame_headroom;
101 	void *buffer;
102 	u32 frame_size;
103 	u32 base_addr;
104 	bool unaligned_mode;
105 };
106 
107 struct xsk_socket_info {
108 	struct xsk_ring_cons rx;
109 	struct xsk_ring_prod tx;
110 	struct xsk_umem_info *umem;
111 	struct xsk_socket *xsk;
112 	u32 outstanding_tx;
113 	u32 rxqsize;
114 };
115 
116 struct pkt {
117 	u64 addr;
118 	u32 len;
119 	u32 payload;
120 	bool valid;
121 };
122 
123 struct pkt_stream {
124 	u32 nb_pkts;
125 	u32 rx_pkt_nb;
126 	struct pkt *pkts;
127 	bool use_addr_for_fill;
128 };
129 
130 struct ifobject;
131 typedef int (*validation_func_t)(struct ifobject *ifobj);
132 typedef void *(*thread_func_t)(void *arg);
133 
134 struct ifobject {
135 	char ifname[MAX_INTERFACE_NAME_CHARS];
136 	char nsname[MAX_INTERFACES_NAMESPACE_CHARS];
137 	struct xsk_socket_info *xsk;
138 	struct xsk_socket_info *xsk_arr;
139 	struct xsk_umem_info *umem;
140 	thread_func_t func_ptr;
141 	validation_func_t validation_func;
142 	struct pkt_stream *pkt_stream;
143 	int ns_fd;
144 	int xsk_map_fd;
145 	u32 dst_ip;
146 	u32 src_ip;
147 	u32 xdp_flags;
148 	u32 bind_flags;
149 	u16 src_port;
150 	u16 dst_port;
151 	bool tx_on;
152 	bool rx_on;
153 	bool use_poll;
154 	bool busy_poll;
155 	bool use_fill_ring;
156 	bool release_rx;
157 	bool shared_umem;
158 	u8 dst_mac[ETH_ALEN];
159 	u8 src_mac[ETH_ALEN];
160 };
161 
162 struct test_spec {
163 	struct ifobject *ifobj_tx;
164 	struct ifobject *ifobj_rx;
165 	struct pkt_stream *tx_pkt_stream_default;
166 	struct pkt_stream *rx_pkt_stream_default;
167 	u16 total_steps;
168 	u16 current_step;
169 	u16 nb_sockets;
170 	bool fail;
171 	enum test_mode mode;
172 	char name[MAX_TEST_NAME_SIZE];
173 };
174 
175 pthread_barrier_t barr;
176 pthread_mutex_t pacing_mutex = PTHREAD_MUTEX_INITIALIZER;
177 pthread_cond_t pacing_cond = PTHREAD_COND_INITIALIZER;
178 
179 int pkts_in_flight;
180 
181 #endif				/* XSKXCEIVER_H_ */
182