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 UMEM_SIZE (DEFAULT_UMEM_BUFFERS * XSK_UMEM__DEFAULT_FRAME_SIZE)
57 #define RX_FULL_RXQSIZE 32
58 #define UMEM_HEADROOM_TEST_SIZE 128
59 #define XSK_UMEM__INVALID_FRAME_SIZE (XSK_UMEM__DEFAULT_FRAME_SIZE + 1)
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_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_MAX
92 };
93 
94 static bool opt_pkt_dump;
95 static bool opt_verbose;
96 
97 struct xsk_umem_info {
98 	struct xsk_ring_prod fq;
99 	struct xsk_ring_cons cq;
100 	struct xsk_umem *umem;
101 	u32 num_frames;
102 	u32 frame_headroom;
103 	void *buffer;
104 	u32 frame_size;
105 	u32 base_addr;
106 	bool unaligned_mode;
107 };
108 
109 struct xsk_socket_info {
110 	struct xsk_ring_cons rx;
111 	struct xsk_ring_prod tx;
112 	struct xsk_umem_info *umem;
113 	struct xsk_socket *xsk;
114 	u32 outstanding_tx;
115 	u32 rxqsize;
116 };
117 
118 struct pkt {
119 	u64 addr;
120 	u32 len;
121 	u32 payload;
122 	bool valid;
123 };
124 
125 struct pkt_stream {
126 	u32 nb_pkts;
127 	u32 rx_pkt_nb;
128 	struct pkt *pkts;
129 	bool use_addr_for_fill;
130 };
131 
132 struct ifobject;
133 typedef int (*validation_func_t)(struct ifobject *ifobj);
134 typedef void *(*thread_func_t)(void *arg);
135 
136 struct ifobject {
137 	char ifname[MAX_INTERFACE_NAME_CHARS];
138 	struct xsk_socket_info *xsk;
139 	struct xsk_socket_info *xsk_arr;
140 	struct xsk_umem_info *umem;
141 	thread_func_t func_ptr;
142 	validation_func_t validation_func;
143 	struct pkt_stream *pkt_stream;
144 	struct xsk_xdp_progs *xdp_progs;
145 	struct bpf_map *xskmap;
146 	struct bpf_program *xdp_prog;
147 	enum test_mode mode;
148 	int ifindex;
149 	u32 dst_ip;
150 	u32 src_ip;
151 	u32 bind_flags;
152 	u16 src_port;
153 	u16 dst_port;
154 	bool tx_on;
155 	bool rx_on;
156 	bool use_poll;
157 	bool busy_poll;
158 	bool use_fill_ring;
159 	bool release_rx;
160 	bool shared_umem;
161 	u8 dst_mac[ETH_ALEN];
162 	u8 src_mac[ETH_ALEN];
163 };
164 
165 struct test_spec {
166 	struct ifobject *ifobj_tx;
167 	struct ifobject *ifobj_rx;
168 	struct pkt_stream *tx_pkt_stream_default;
169 	struct pkt_stream *rx_pkt_stream_default;
170 	struct bpf_program *xdp_prog_rx;
171 	struct bpf_program *xdp_prog_tx;
172 	struct bpf_map *xskmap_rx;
173 	struct bpf_map *xskmap_tx;
174 	u16 total_steps;
175 	u16 current_step;
176 	u16 nb_sockets;
177 	bool fail;
178 	enum test_mode mode;
179 	char name[MAX_TEST_NAME_SIZE];
180 };
181 
182 pthread_barrier_t barr;
183 pthread_mutex_t pacing_mutex = PTHREAD_MUTEX_INITIALIZER;
184 pthread_cond_t pacing_cond = PTHREAD_COND_INITIALIZER;
185 
186 int pkts_in_flight;
187 
188 #endif				/* XSKXCEIVER_H_ */
189