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_SOCKS 1
24 #define MAX_TEARDOWN_ITER 10
25 #define MAX_BIDI_ITER 2
26 #define MAX_BPF_ITER 2
27 #define PKT_HDR_SIZE (sizeof(struct ethhdr) + sizeof(struct iphdr) + \
28 			sizeof(struct udphdr))
29 #define MIN_PKT_SIZE 64
30 #define ETH_FCS_SIZE 4
31 #define PKT_SIZE (MIN_PKT_SIZE - ETH_FCS_SIZE)
32 #define IP_PKT_SIZE (PKT_SIZE - sizeof(struct ethhdr))
33 #define IP_PKT_VER 0x4
34 #define IP_PKT_TOS 0x9
35 #define UDP_PKT_SIZE (IP_PKT_SIZE - sizeof(struct iphdr))
36 #define UDP_PKT_DATA_SIZE (UDP_PKT_SIZE - sizeof(struct udphdr))
37 #define USLEEP_MAX 10000
38 #define SOCK_RECONF_CTR 10
39 #define BATCH_SIZE 8
40 #define POLL_TMOUT 1000
41 #define DEFAULT_PKT_CNT (4 * 1024)
42 #define RX_FULL_RXQSIZE 32
43 #define XSK_UMEM__INVALID_FRAME_SIZE (XSK_UMEM__DEFAULT_FRAME_SIZE + 1)
44 
45 #define print_verbose(x...) do { if (opt_verbose) ksft_print_msg(x); } while (0)
46 
47 enum test_mode {
48 	TEST_MODE_SKB,
49 	TEST_MODE_DRV,
50 	TEST_MODE_MAX
51 };
52 
53 enum test_type {
54 	TEST_TYPE_NOPOLL,
55 	TEST_TYPE_POLL,
56 	TEST_TYPE_TEARDOWN,
57 	TEST_TYPE_BIDI,
58 	TEST_TYPE_STATS,
59 	TEST_TYPE_BPF_RES,
60 	TEST_TYPE_MAX
61 };
62 
63 enum stat_test_type {
64 	STAT_TEST_RX_DROPPED,
65 	STAT_TEST_TX_INVALID,
66 	STAT_TEST_RX_FULL,
67 	STAT_TEST_RX_FILL_EMPTY,
68 	STAT_TEST_TYPE_MAX
69 };
70 
71 static int configured_mode;
72 static bool opt_pkt_dump;
73 static u32 num_frames = DEFAULT_PKT_CNT / 4;
74 static bool second_step;
75 static int test_type;
76 
77 static bool opt_verbose;
78 
79 static u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
80 static u32 xdp_bind_flags = XDP_USE_NEED_WAKEUP | XDP_COPY;
81 static int stat_test_type;
82 static u32 rxqsize;
83 static u32 frame_headroom;
84 
85 struct xsk_umem_info {
86 	struct xsk_ring_prod fq;
87 	struct xsk_ring_cons cq;
88 	struct xsk_umem *umem;
89 	void *buffer;
90 };
91 
92 struct xsk_socket_info {
93 	struct xsk_ring_cons rx;
94 	struct xsk_ring_prod tx;
95 	struct xsk_umem_info *umem;
96 	struct xsk_socket *xsk;
97 	u32 outstanding_tx;
98 };
99 
100 struct flow_vector {
101 	enum fvector {
102 		tx,
103 		rx,
104 	} vector;
105 };
106 
107 struct pkt {
108 	u64 addr;
109 	u32 len;
110 	u32 payload;
111 };
112 
113 struct pkt_stream {
114 	u32 nb_pkts;
115 	struct pkt *pkts;
116 };
117 
118 struct ifobject {
119 	char ifname[MAX_INTERFACE_NAME_CHARS];
120 	char nsname[MAX_INTERFACES_NAMESPACE_CHARS];
121 	struct xsk_socket_info *xsk;
122 	struct xsk_socket_info **xsk_arr;
123 	struct xsk_umem_info **umem_arr;
124 	struct xsk_umem_info *umem;
125 	void *(*func_ptr)(void *arg);
126 	struct flow_vector fv;
127 	struct pkt_stream *pkt_stream;
128 	int ns_fd;
129 	u32 dst_ip;
130 	u32 src_ip;
131 	u16 src_port;
132 	u16 dst_port;
133 	u8 dst_mac[ETH_ALEN];
134 	u8 src_mac[ETH_ALEN];
135 };
136 
137 static struct ifobject *ifdict[MAX_INTERFACES];
138 static struct ifobject *ifdict_rx;
139 static struct ifobject *ifdict_tx;
140 
141 /*threads*/
142 pthread_barrier_t barr;
143 pthread_t t0, t1;
144 
145 #endif				/* XDPXCEIVER_H */
146