1/*
2 * Copyright (c) 2016  Intel Corporation.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#ifndef __NETBE_H__
17#define __NETBE_H__
18
19#include <stdint.h>
20#include <stddef.h>
21#include <inttypes.h>
22#include <getopt.h>
23#include <arpa/inet.h>
24#include <assert.h>
25#include <signal.h>
26
27#include <rte_config.h>
28#include <rte_common.h>
29#include <rte_eal.h>
30#include <rte_lcore.h>
31#include <rte_ethdev.h>
32#include <rte_kvargs.h>
33#include <rte_errno.h>
34#include <rte_malloc.h>
35#include <rte_cycles.h>
36#include <rte_lpm.h>
37#include <rte_lpm6.h>
38#include <rte_hash.h>
39#include <rte_ip.h>
40#include <rte_ip_frag.h>
41#include <rte_tcp.h>
42#include <rte_udp.h>
43#include <tle_tcp.h>
44#include <tle_udp.h>
45#include <tle_event.h>
46
47#define TLE_DEFAULT_MSS 536
48
49#define	MAX_PKT_BURST	0x20
50
51/* Used to allocate the memory for hash key. */
52#define RSS_HASH_KEY_LENGTH 64
53
54/*
55 * global variables
56 */
57
58enum {
59	VERBOSE_NONE = 0,
60	VERBOSE_NUM = 9
61};
62
63extern int verbose;
64
65/*
66 * BE related structures.
67 */
68
69struct netbe_port {
70	uint32_t id;
71	uint32_t nb_lcore;
72	uint32_t *lcore_id;
73	uint32_t mtu;
74	uint64_t rx_offload;
75	uint64_t tx_offload;
76	uint32_t ipv4;
77	struct in6_addr ipv6;
78	struct ether_addr mac;
79	uint32_t hash_key_size;
80	uint8_t hash_key[RSS_HASH_KEY_LENGTH];
81};
82
83struct netbe_dest {
84	uint32_t line;
85	uint32_t port;
86	uint32_t mtu;
87	uint32_t prfx;
88	uint16_t family;
89	union {
90		struct in_addr ipv4;
91		struct in6_addr ipv6;
92	};
93	struct ether_addr mac;
94};
95
96struct netbe_dest_prm {
97	uint32_t nb_dest;
98	struct netbe_dest *dest;
99};
100
101struct pkt_buf {
102	uint32_t num;
103	struct rte_mbuf *pkt[2 * MAX_PKT_BURST];
104};
105
106struct netbe_dev {
107	uint16_t rxqid;
108	uint16_t txqid;
109	struct netbe_port port;
110	struct tle_dev *dev;
111	struct {
112		uint64_t in;
113		uint64_t up;
114		uint64_t drop;
115	} rx_stat;
116	struct {
117		uint64_t down;
118		uint64_t out;
119		uint64_t drop;
120	} tx_stat;
121	struct pkt_buf tx_buf;
122	struct pkt_buf arp_buf;
123};
124
125/* 8 bit LPM user data. */
126#define	LCORE_MAX_DST	(UINT8_MAX + 1)
127
128struct netbe_lcore {
129	uint32_t id;
130	uint32_t proto; /**< L4 proto to handle. */
131	struct rte_lpm *lpm4;
132	struct rte_lpm6 *lpm6;
133	struct rte_ip_frag_tbl *ftbl;
134	struct tle_ctx *ctx;
135	uint32_t prtq_num;
136	uint32_t dst4_num;
137	uint32_t dst6_num;
138	struct netbe_dev *prtq;
139	struct tle_dest dst4[LCORE_MAX_DST];
140	struct tle_dest dst6[LCORE_MAX_DST];
141	struct rte_ip_frag_death_row death_row;
142	struct {
143		uint64_t flags[UINT8_MAX + 1];
144	} tcp_stat;
145};
146
147struct netbe_cfg {
148	uint32_t promisc;
149	uint32_t proto;
150	uint32_t server;
151	uint32_t arp;
152	uint32_t prt_num;
153	uint32_t cpu_num;
154	uint32_t mpool_buf_num;
155	struct netbe_port *prt;
156	struct netbe_lcore *cpu;
157};
158
159/*
160 * FE related structures.
161 */
162
163enum {
164	RXONLY,
165	TXONLY,
166	RXTX,
167	ECHO,
168	FWD,
169};
170
171struct netfe_sprm {
172	uint32_t bidx;  /* BE index to use. */
173	struct sockaddr_storage local_addr;  /**< stream local address. */
174	struct sockaddr_storage remote_addr; /**< stream remote address. */
175};
176
177struct netfe_stream_prm {
178	uint32_t lcore;
179	uint32_t belcore;
180	uint16_t line;
181	uint16_t op;
182	uint32_t txlen; /* valid/used only for TXONLY op. */
183	uint32_t rxlen; /* Used by RXTX */
184	struct netfe_sprm sprm;
185	struct netfe_sprm fprm;  /* valid/used only for FWD op. */
186};
187
188struct netfe_lcore_prm {
189	uint32_t max_streams;
190	uint32_t nb_streams;
191	struct netfe_stream_prm *stream;
192};
193
194struct netfe_stream {
195	struct tle_stream *s;
196	struct tle_event *erev;
197	struct tle_event *rxev;
198	struct tle_event *txev;
199	uint16_t op;
200	uint16_t proto;
201	uint16_t family;
202	uint32_t txlen;
203	uint32_t rxlen;
204	uint16_t reply_count;
205	uint32_t rx_run_len;
206	uint16_t posterr; /* # of time error event handling was postponed */
207	struct {
208		uint64_t rxp;
209		uint64_t rxb;
210		uint64_t txp;
211		uint64_t txb;
212		uint64_t fwp;
213		uint64_t drops;
214		uint64_t rxev[TLE_SEV_NUM];
215		uint64_t txev[TLE_SEV_NUM];
216		uint64_t erev[TLE_SEV_NUM];
217	} stat;
218	struct pkt_buf pbuf;
219	struct sockaddr_storage laddr;
220	struct sockaddr_storage raddr;
221	struct netfe_sprm fwdprm;
222	struct netfe_stream *fwds;
223	LIST_ENTRY(netfe_stream) link;
224};
225
226struct netfe_stream_list {
227	uint32_t num;
228	LIST_HEAD(, netfe_stream) head;
229};
230
231struct netfe_lcore {
232	uint32_t snum;  /* max number of streams */
233	struct tle_evq *syneq;
234	struct tle_evq *ereq;
235	struct tle_evq *rxeq;
236	struct tle_evq *txeq;
237	struct rte_hash *fw4h;
238	struct rte_hash *fw6h;
239	struct {
240		uint64_t acc;
241		uint64_t rej;
242		uint64_t ter;
243	} tcp_stat;
244	struct netfe_stream_list free;
245	struct netfe_stream_list use;
246};
247
248struct lcore_prm {
249	struct {
250		struct netbe_lcore *lc;
251	} be;
252	struct netfe_lcore_prm fe;
253};
254
255struct tx_content {
256	size_t sz;
257	uint8_t *data;
258};
259
260extern struct tx_content tx_content;
261
262/*
263 * debug/trace macros.
264 */
265
266#define	DUMMY_MACRO	do {} while (0)
267
268#ifdef NETFE_DEBUG
269#define	NETFE_TRACE(fmt, arg...)	printf(fmt, ##arg)
270#define	NETFE_PKT_DUMP(p)		rte_pktmbuf_dump(stdout, (p), 64)
271#else
272#define	NETFE_TRACE(fmt, arg...)	DUMMY_MACRO
273#define	NETFE_PKT_DUMP(p)		DUMMY_MACRO
274#endif
275
276#ifdef NETBE_DEBUG
277#define	NETBE_TRACE(fmt, arg...)	printf(fmt, ##arg)
278#define	NETBE_PKT_DUMP(p)		rte_pktmbuf_dump(stdout, (p), 64)
279#else
280#define	NETBE_TRACE(fmt, arg...)	DUMMY_MACRO
281#define	NETBE_PKT_DUMP(p)		DUMMY_MACRO
282#endif
283
284#define FUNC_STAT(v, c) do { \
285	static uint64_t nb_call, nb_data; \
286	nb_call++; \
287	nb_data += (v); \
288	if ((nb_call & ((c) - 1)) == 0) { \
289		printf("%s#%d@%u: nb_call=%lu, avg(" #v ")=%#Lf\n", \
290			__func__, __LINE__, rte_lcore_id(), nb_call, \
291			(long double)nb_data / nb_call); \
292		nb_call = 0; \
293		nb_data = 0; \
294	} \
295} while (0)
296
297#define FUNC_TM_STAT(v, c) do { \
298	static uint64_t nb_call, nb_data; \
299	static uint64_t cts, pts, sts; \
300	cts = rte_rdtsc(); \
301	if (pts != 0) \
302		sts += cts - pts; \
303	pts = cts; \
304	nb_call++; \
305	nb_data += (v); \
306	if ((nb_call & ((c) - 1)) == 0) { \
307		printf("%s#%d@%u: nb_call=%lu, " \
308			"avg(" #v ")=%#Lf, " \
309			"avg(cycles)=%#Lf, " \
310			"avg(cycles/" #v ")=%#Lf\n", \
311			__func__, __LINE__, rte_lcore_id(), nb_call, \
312			(long double)nb_data / nb_call, \
313			(long double)sts / nb_call, \
314			(long double)sts / nb_data); \
315		nb_call = 0; \
316		nb_data = 0; \
317		sts = 0; \
318	} \
319} while (0)
320
321int setup_rx_cb(const struct netbe_port *uprt, struct netbe_lcore *lc,
322	uint16_t qid, uint32_t arp);
323
324/*
325 * application function pointers
326 */
327
328typedef int (*LCORE_MAIN_FUNCTYPE)(void *arg);
329
330/*
331 * tle_l4p lib function pointers
332 */
333
334typedef uint16_t (*TLE_RX_BULK_FUNCTYPE)
335	(struct tle_dev *dev, struct rte_mbuf *pkt[],
336	struct rte_mbuf *rp[], int32_t rc[], uint16_t num);
337
338typedef uint16_t (*TLE_TX_BULK_FUNCTYPE)
339	(struct tle_dev *dev, struct rte_mbuf *pkt[], uint16_t num);
340
341typedef uint16_t (*TLE_STREAM_RECV_FUNCTYPE)
342	(struct tle_stream *ts, struct rte_mbuf *pkt[], uint16_t num);
343
344typedef int (*TLE_STREAM_CLOSE_FUNCTYPE)(struct tle_stream *s);
345
346#endif /* __NETBE_H__ */
347