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 MAX_INTERFACES 2
31 #define MAX_INTERFACE_NAME_CHARS 7
32 #define MAX_INTERFACES_NAMESPACE_CHARS 10
33 #define MAX_SOCKETS 2
34 #define MAX_TEST_NAME_SIZE 32
35 #define MAX_TEARDOWN_ITER 10
36 #define PKT_HDR_SIZE (sizeof(struct ethhdr) + sizeof(struct iphdr) + \
37 			sizeof(struct udphdr))
38 #define MIN_ETH_PKT_SIZE 64
39 #define ETH_FCS_SIZE 4
40 #define MIN_PKT_SIZE (MIN_ETH_PKT_SIZE - ETH_FCS_SIZE)
41 #define PKT_SIZE (MIN_PKT_SIZE)
42 #define IP_PKT_SIZE (PKT_SIZE - sizeof(struct ethhdr))
43 #define IP_PKT_VER 0x4
44 #define IP_PKT_TOS 0x9
45 #define UDP_PKT_SIZE (IP_PKT_SIZE - sizeof(struct iphdr))
46 #define UDP_PKT_DATA_SIZE (UDP_PKT_SIZE - sizeof(struct udphdr))
47 #define USLEEP_MAX 10000
48 #define SOCK_RECONF_CTR 10
49 #define BATCH_SIZE 64
50 #define POLL_TMOUT 1000
51 #define RECV_TMOUT 3
52 #define DEFAULT_PKT_CNT (4 * 1024)
53 #define DEFAULT_UMEM_BUFFERS (DEFAULT_PKT_CNT / 4)
54 #define UMEM_SIZE (DEFAULT_UMEM_BUFFERS * XSK_UMEM__DEFAULT_FRAME_SIZE)
55 #define RX_FULL_RXQSIZE 32
56 #define UMEM_HEADROOM_TEST_SIZE 128
57 #define XSK_UMEM__INVALID_FRAME_SIZE (XSK_UMEM__DEFAULT_FRAME_SIZE + 1)
58 
59 #define print_verbose(x...) do { if (opt_verbose) ksft_print_msg(x); } while (0)
60 
61 enum test_mode {
62 	TEST_MODE_SKB,
63 	TEST_MODE_DRV,
64 	TEST_MODE_MAX
65 };
66 
67 enum test_type {
68 	TEST_TYPE_RUN_TO_COMPLETION,
69 	TEST_TYPE_RUN_TO_COMPLETION_2K_FRAME,
70 	TEST_TYPE_RUN_TO_COMPLETION_SINGLE_PKT,
71 	TEST_TYPE_POLL,
72 	TEST_TYPE_UNALIGNED,
73 	TEST_TYPE_ALIGNED_INV_DESC,
74 	TEST_TYPE_ALIGNED_INV_DESC_2K_FRAME,
75 	TEST_TYPE_UNALIGNED_INV_DESC,
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_MAX
85 };
86 
87 static bool opt_pkt_dump;
88 static bool opt_verbose;
89 
90 struct xsk_umem_info {
91 	struct xsk_ring_prod fq;
92 	struct xsk_ring_cons cq;
93 	struct xsk_umem *umem;
94 	u32 num_frames;
95 	u32 frame_headroom;
96 	void *buffer;
97 	u32 frame_size;
98 	bool unaligned_mode;
99 };
100 
101 struct xsk_socket_info {
102 	struct xsk_ring_cons rx;
103 	struct xsk_ring_prod tx;
104 	struct xsk_umem_info *umem;
105 	struct xsk_socket *xsk;
106 	u32 outstanding_tx;
107 	u32 rxqsize;
108 };
109 
110 struct pkt {
111 	u64 addr;
112 	u32 len;
113 	u32 payload;
114 	bool valid;
115 };
116 
117 struct pkt_stream {
118 	u32 nb_pkts;
119 	u32 rx_pkt_nb;
120 	struct pkt *pkts;
121 	bool use_addr_for_fill;
122 };
123 
124 struct ifobject;
125 typedef int (*validation_func_t)(struct ifobject *ifobj);
126 typedef void *(*thread_func_t)(void *arg);
127 
128 struct ifobject {
129 	char ifname[MAX_INTERFACE_NAME_CHARS];
130 	char nsname[MAX_INTERFACES_NAMESPACE_CHARS];
131 	struct xsk_socket_info *xsk;
132 	struct xsk_socket_info *xsk_arr;
133 	struct xsk_umem_info *umem;
134 	thread_func_t func_ptr;
135 	validation_func_t validation_func;
136 	struct pkt_stream *pkt_stream;
137 	int ns_fd;
138 	int xsk_map_fd;
139 	u32 dst_ip;
140 	u32 src_ip;
141 	u32 xdp_flags;
142 	u32 bind_flags;
143 	u16 src_port;
144 	u16 dst_port;
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 	u8 dst_mac[ETH_ALEN];
152 	u8 src_mac[ETH_ALEN];
153 };
154 
155 struct test_spec {
156 	struct ifobject *ifobj_tx;
157 	struct ifobject *ifobj_rx;
158 	struct pkt_stream *pkt_stream_default;
159 	u16 total_steps;
160 	u16 current_step;
161 	u16 nb_sockets;
162 	bool fail;
163 	char name[MAX_TEST_NAME_SIZE];
164 };
165 
166 pthread_barrier_t barr;
167 pthread_mutex_t pacing_mutex = PTHREAD_MUTEX_INITIALIZER;
168 pthread_cond_t pacing_cond = PTHREAD_COND_INITIALIZER;
169 
170 int pkts_in_flight;
171 
172 #endif				/* XSKXCEIVER_H_ */
173