1 /* SPDX-License-Identifier: GPL-2.0
2  * Copyright(c) 2020 Intel Corporation.
3  */
4 
5 #ifndef XDPXCEIVER_H_
6 #define XDPXCEIVER_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 #define MAX_INTERFACES 2
21 #define MAX_INTERFACE_NAME_CHARS 7
22 #define MAX_INTERFACES_NAMESPACE_CHARS 10
23 #define MAX_SOCKETS 2
24 #define MAX_TEST_NAME_SIZE 32
25 #define MAX_TEARDOWN_ITER 10
26 #define PKT_HDR_SIZE (sizeof(struct ethhdr) + sizeof(struct iphdr) + \
27 			sizeof(struct udphdr))
28 #define MIN_PKT_SIZE 64
29 #define ETH_FCS_SIZE 4
30 #define PKT_SIZE (MIN_PKT_SIZE - ETH_FCS_SIZE)
31 #define IP_PKT_SIZE (PKT_SIZE - sizeof(struct ethhdr))
32 #define IP_PKT_VER 0x4
33 #define IP_PKT_TOS 0x9
34 #define UDP_PKT_SIZE (IP_PKT_SIZE - sizeof(struct iphdr))
35 #define UDP_PKT_DATA_SIZE (UDP_PKT_SIZE - sizeof(struct udphdr))
36 #define USLEEP_MAX 10000
37 #define SOCK_RECONF_CTR 10
38 #define BATCH_SIZE 64
39 #define POLL_TMOUT 1000
40 #define DEFAULT_PKT_CNT (4 * 1024)
41 #define DEFAULT_UMEM_BUFFERS (DEFAULT_PKT_CNT / 4)
42 #define UMEM_SIZE (DEFAULT_UMEM_BUFFERS * XSK_UMEM__DEFAULT_FRAME_SIZE)
43 #define RX_FULL_RXQSIZE 32
44 #define UMEM_HEADROOM_TEST_SIZE 128
45 #define XSK_UMEM__INVALID_FRAME_SIZE (XSK_UMEM__DEFAULT_FRAME_SIZE + 1)
46 
47 #define print_verbose(x...) do { if (opt_verbose) ksft_print_msg(x); } while (0)
48 
49 enum test_mode {
50 	TEST_MODE_SKB,
51 	TEST_MODE_DRV,
52 	TEST_MODE_MAX
53 };
54 
55 enum test_type {
56 	TEST_TYPE_RUN_TO_COMPLETION,
57 	TEST_TYPE_RUN_TO_COMPLETION_2K_FRAME,
58 	TEST_TYPE_RUN_TO_COMPLETION_SINGLE_PKT,
59 	TEST_TYPE_POLL,
60 	TEST_TYPE_UNALIGNED,
61 	TEST_TYPE_ALIGNED_INV_DESC,
62 	TEST_TYPE_ALIGNED_INV_DESC_2K_FRAME,
63 	TEST_TYPE_UNALIGNED_INV_DESC,
64 	TEST_TYPE_HEADROOM,
65 	TEST_TYPE_TEARDOWN,
66 	TEST_TYPE_BIDI,
67 	TEST_TYPE_STATS,
68 	TEST_TYPE_BPF_RES,
69 	TEST_TYPE_MAX
70 };
71 
72 enum stat_test_type {
73 	STAT_TEST_RX_DROPPED,
74 	STAT_TEST_TX_INVALID,
75 	STAT_TEST_RX_FULL,
76 	STAT_TEST_RX_FILL_EMPTY,
77 	STAT_TEST_TYPE_MAX
78 };
79 
80 static bool opt_pkt_dump;
81 static int test_type;
82 
83 static bool opt_verbose;
84 static int stat_test_type;
85 
86 struct xsk_umem_info {
87 	struct xsk_ring_prod fq;
88 	struct xsk_ring_cons cq;
89 	struct xsk_umem *umem;
90 	u32 num_frames;
91 	u32 frame_headroom;
92 	void *buffer;
93 	u32 frame_size;
94 	bool unaligned_mode;
95 };
96 
97 struct xsk_socket_info {
98 	struct xsk_ring_cons rx;
99 	struct xsk_ring_prod tx;
100 	struct xsk_umem_info *umem;
101 	struct xsk_socket *xsk;
102 	u32 outstanding_tx;
103 	u32 rxqsize;
104 };
105 
106 struct pkt {
107 	u64 addr;
108 	u32 len;
109 	u32 payload;
110 	bool valid;
111 };
112 
113 struct pkt_stream {
114 	u32 nb_pkts;
115 	u32 rx_pkt_nb;
116 	struct pkt *pkts;
117 	bool use_addr_for_fill;
118 };
119 
120 typedef void *(*thread_func_t)(void *arg);
121 
122 struct ifobject {
123 	char ifname[MAX_INTERFACE_NAME_CHARS];
124 	char nsname[MAX_INTERFACES_NAMESPACE_CHARS];
125 	struct xsk_socket_info *xsk;
126 	struct xsk_socket_info *xsk_arr;
127 	struct xsk_umem_info *umem;
128 	struct xsk_umem_info *umem_arr;
129 	thread_func_t func_ptr;
130 	struct pkt_stream *pkt_stream;
131 	int ns_fd;
132 	u32 dst_ip;
133 	u32 src_ip;
134 	u32 xdp_flags;
135 	u32 bind_flags;
136 	u16 src_port;
137 	u16 dst_port;
138 	bool tx_on;
139 	bool rx_on;
140 	bool use_poll;
141 	bool pacing_on;
142 	u8 dst_mac[ETH_ALEN];
143 	u8 src_mac[ETH_ALEN];
144 };
145 
146 struct test_spec {
147 	struct ifobject *ifobj_tx;
148 	struct ifobject *ifobj_rx;
149 	struct pkt_stream *pkt_stream_default;
150 	u16 total_steps;
151 	u16 current_step;
152 	u16 nb_sockets;
153 	char name[MAX_TEST_NAME_SIZE];
154 };
155 
156 pthread_barrier_t barr;
157 pthread_mutex_t pacing_mutex = PTHREAD_MUTEX_INITIALIZER;
158 pthread_cond_t pacing_cond = PTHREAD_COND_INITIALIZER;
159 
160 u32 pkts_in_flight;
161 
162 #endif				/* XDPXCEIVER_H */
163