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 USLEEP_MAX 10000
42 #define SOCK_RECONF_CTR 10
43 #define BATCH_SIZE 64
44 #define POLL_TMOUT 1000
45 #define THREAD_TMOUT 3
46 #define DEFAULT_PKT_CNT (4 * 1024)
47 #define DEFAULT_UMEM_BUFFERS (DEFAULT_PKT_CNT / 4)
48 #define RX_FULL_RXQSIZE 32
49 #define UMEM_HEADROOM_TEST_SIZE 128
50 #define XSK_UMEM__INVALID_FRAME_SIZE (XSK_UMEM__DEFAULT_FRAME_SIZE + 1)
51 #define HUGEPAGE_SIZE (2 * 1024 * 1024)
52 #define PKT_DUMP_NB_TO_PRINT 16
53 
54 #define print_verbose(x...) do { if (opt_verbose) ksft_print_msg(x); } while (0)
55 
56 enum test_mode {
57 	TEST_MODE_SKB,
58 	TEST_MODE_DRV,
59 	TEST_MODE_ZC,
60 	TEST_MODE_MAX
61 };
62 
63 enum test_type {
64 	TEST_TYPE_RUN_TO_COMPLETION,
65 	TEST_TYPE_RUN_TO_COMPLETION_2K_FRAME,
66 	TEST_TYPE_RUN_TO_COMPLETION_SINGLE_PKT,
67 	TEST_TYPE_RX_POLL,
68 	TEST_TYPE_TX_POLL,
69 	TEST_TYPE_POLL_RXQ_TMOUT,
70 	TEST_TYPE_POLL_TXQ_TMOUT,
71 	TEST_TYPE_UNALIGNED,
72 	TEST_TYPE_ALIGNED_INV_DESC,
73 	TEST_TYPE_ALIGNED_INV_DESC_2K_FRAME,
74 	TEST_TYPE_UNALIGNED_INV_DESC,
75 	TEST_TYPE_UNALIGNED_INV_DESC_4K1_FRAME,
76 	TEST_TYPE_HEADROOM,
77 	TEST_TYPE_TEARDOWN,
78 	TEST_TYPE_BIDI,
79 	TEST_TYPE_STATS_RX_DROPPED,
80 	TEST_TYPE_STATS_TX_INVALID_DESCS,
81 	TEST_TYPE_STATS_RX_FULL,
82 	TEST_TYPE_STATS_FILL_EMPTY,
83 	TEST_TYPE_BPF_RES,
84 	TEST_TYPE_XDP_DROP_HALF,
85 	TEST_TYPE_XDP_METADATA_COUNT,
86 	TEST_TYPE_MAX
87 };
88 
89 static bool opt_verbose;
90 
91 struct xsk_umem_info {
92 	struct xsk_ring_prod fq;
93 	struct xsk_ring_cons cq;
94 	struct xsk_umem *umem;
95 	u64 next_buffer;
96 	u32 num_frames;
97 	u32 frame_headroom;
98 	void *buffer;
99 	u32 frame_size;
100 	u32 base_addr;
101 	bool unaligned_mode;
102 };
103 
104 struct xsk_socket_info {
105 	struct xsk_ring_cons rx;
106 	struct xsk_ring_prod tx;
107 	struct xsk_umem_info *umem;
108 	struct xsk_socket *xsk;
109 	u32 outstanding_tx;
110 	u32 rxqsize;
111 };
112 
113 struct pkt {
114 	int offset;
115 	u32 len;
116 	u32 pkt_nb;
117 	bool valid;
118 };
119 
120 struct pkt_stream {
121 	u32 nb_pkts;
122 	u32 current_pkt_nb;
123 	struct pkt *pkts;
124 	u32 max_pkt_len;
125 };
126 
127 struct ifobject;
128 typedef int (*validation_func_t)(struct ifobject *ifobj);
129 typedef void *(*thread_func_t)(void *arg);
130 
131 struct ifobject {
132 	char ifname[MAX_INTERFACE_NAME_CHARS];
133 	struct xsk_socket_info *xsk;
134 	struct xsk_socket_info *xsk_arr;
135 	struct xsk_umem_info *umem;
136 	thread_func_t func_ptr;
137 	validation_func_t validation_func;
138 	struct pkt_stream *pkt_stream;
139 	struct xsk_xdp_progs *xdp_progs;
140 	struct bpf_map *xskmap;
141 	struct bpf_program *xdp_prog;
142 	enum test_mode mode;
143 	int ifindex;
144 	u32 bind_flags;
145 	bool tx_on;
146 	bool rx_on;
147 	bool use_poll;
148 	bool busy_poll;
149 	bool use_fill_ring;
150 	bool release_rx;
151 	bool shared_umem;
152 	bool use_metadata;
153 	bool unaligned_supp;
154 	u8 dst_mac[ETH_ALEN];
155 	u8 src_mac[ETH_ALEN];
156 };
157 
158 struct test_spec {
159 	struct ifobject *ifobj_tx;
160 	struct ifobject *ifobj_rx;
161 	struct pkt_stream *tx_pkt_stream_default;
162 	struct pkt_stream *rx_pkt_stream_default;
163 	struct bpf_program *xdp_prog_rx;
164 	struct bpf_program *xdp_prog_tx;
165 	struct bpf_map *xskmap_rx;
166 	struct bpf_map *xskmap_tx;
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 
178 int pkts_in_flight;
179 
180 #endif				/* XSKXCEIVER_H_ */
181