netbe.h revision e151ee29
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	uint32_t rx_offload;
75	uint32_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	struct netbe_port *prt;
155	struct netbe_lcore *cpu;
156};
157
158/*
159 * FE related structures.
160 */
161
162enum {
163	RXONLY,
164	TXONLY,
165	RXTX,
166	ECHO,
167	FWD,
168};
169
170struct netfe_sprm {
171	uint32_t bidx;  /* BE index to use. */
172	struct sockaddr_storage local_addr;  /**< stream local address. */
173	struct sockaddr_storage remote_addr; /**< stream remote address. */
174};
175
176struct netfe_stream_prm {
177	uint32_t lcore;
178	uint32_t belcore;
179	uint16_t line;
180	uint16_t op;
181	uint32_t txlen; /* valid/used only for TXONLY op. */
182	uint32_t rxlen; /* Used by RXTX */
183	struct netfe_sprm sprm;
184	struct netfe_sprm fprm;  /* valid/used only for FWD op. */
185};
186
187struct netfe_lcore_prm {
188	uint32_t max_streams;
189	uint32_t nb_streams;
190	struct netfe_stream_prm *stream;
191};
192
193struct netfe_stream {
194	struct tle_stream *s;
195	struct tle_event *erev;
196	struct tle_event *rxev;
197	struct tle_event *txev;
198	uint16_t op;
199	uint16_t proto;
200	uint16_t family;
201	uint32_t txlen;
202	uint32_t rxlen;
203	uint16_t reply_count;
204	uint32_t rx_run_len;
205	uint16_t posterr; /* # of time error event handling was postponed */
206	struct {
207		uint64_t rxp;
208		uint64_t rxb;
209		uint64_t txp;
210		uint64_t txb;
211		uint64_t fwp;
212		uint64_t drops;
213		uint64_t rxev[TLE_SEV_NUM];
214		uint64_t txev[TLE_SEV_NUM];
215		uint64_t erev[TLE_SEV_NUM];
216	} stat;
217	struct pkt_buf pbuf;
218	struct sockaddr_storage laddr;
219	struct sockaddr_storage raddr;
220	struct netfe_sprm fwdprm;
221	struct netfe_stream *fwds;
222	LIST_ENTRY(netfe_stream) link;
223};
224
225struct netfe_stream_list {
226	uint32_t num;
227	LIST_HEAD(, netfe_stream) head;
228};
229
230struct netfe_lcore {
231	uint32_t snum;  /* max number of streams */
232	struct tle_evq *syneq;
233	struct tle_evq *ereq;
234	struct tle_evq *rxeq;
235	struct tle_evq *txeq;
236	struct rte_hash *fw4h;
237	struct rte_hash *fw6h;
238	struct {
239		uint64_t acc;
240		uint64_t rej;
241		uint64_t ter;
242	} tcp_stat;
243	struct netfe_stream_list free;
244	struct netfe_stream_list use;
245};
246
247struct lcore_prm {
248	struct {
249		struct netbe_lcore *lc;
250	} be;
251	struct netfe_lcore_prm fe;
252};
253
254/*
255 * debug/trace macros.
256 */
257
258#define	DUMMY_MACRO	do {} while (0)
259
260#ifdef NETFE_DEBUG
261#define	NETFE_TRACE(fmt, arg...)	printf(fmt, ##arg)
262#define	NETFE_PKT_DUMP(p)		rte_pktmbuf_dump(stdout, (p), 64)
263#else
264#define	NETFE_TRACE(fmt, arg...)	DUMMY_MACRO
265#define	NETFE_PKT_DUMP(p)		DUMMY_MACRO
266#endif
267
268#ifdef NETBE_DEBUG
269#define	NETBE_TRACE(fmt, arg...)	printf(fmt, ##arg)
270#define	NETBE_PKT_DUMP(p)		rte_pktmbuf_dump(stdout, (p), 64)
271#else
272#define	NETBE_TRACE(fmt, arg...)	DUMMY_MACRO
273#define	NETBE_PKT_DUMP(p)		DUMMY_MACRO
274#endif
275
276#define FUNC_STAT(v, c) do { \
277	static uint64_t nb_call, nb_data; \
278	nb_call++; \
279	nb_data += (v); \
280	if ((nb_call & ((c) - 1)) == 0) { \
281		printf("%s#%d@%u: nb_call=%lu, avg(" #v ")=%#Lf\n", \
282			__func__, __LINE__, rte_lcore_id(), nb_call, \
283			(long double)nb_data / nb_call); \
284		nb_call = 0; \
285		nb_data = 0; \
286	} \
287} while (0)
288
289#define FUNC_TM_STAT(v, c) do { \
290	static uint64_t nb_call, nb_data; \
291	static uint64_t cts, pts, sts; \
292	cts = rte_rdtsc(); \
293	if (pts != 0) \
294		sts += cts - pts; \
295	pts = cts; \
296	nb_call++; \
297	nb_data += (v); \
298	if ((nb_call & ((c) - 1)) == 0) { \
299		printf("%s#%d@%u: nb_call=%lu, " \
300			"avg(" #v ")=%#Lf, " \
301			"avg(cycles)=%#Lf, " \
302			"avg(cycles/" #v ")=%#Lf\n", \
303			__func__, __LINE__, rte_lcore_id(), nb_call, \
304			(long double)nb_data / nb_call, \
305			(long double)sts / nb_call, \
306			(long double)sts / nb_data); \
307		nb_call = 0; \
308		nb_data = 0; \
309		sts = 0; \
310	} \
311} while (0)
312
313int setup_rx_cb(const struct netbe_port *uprt, struct netbe_lcore *lc,
314	uint16_t qid, uint32_t arp);
315
316/*
317 * application function pointers
318 */
319
320typedef int (*LCORE_MAIN_FUNCTYPE)(void *arg);
321
322/*
323 * tle_l4p lib function pointers
324 */
325
326typedef uint16_t (*TLE_RX_BULK_FUNCTYPE)
327	(struct tle_dev *dev, struct rte_mbuf *pkt[],
328	struct rte_mbuf *rp[], int32_t rc[], uint16_t num);
329
330typedef uint16_t (*TLE_TX_BULK_FUNCTYPE)
331	(struct tle_dev *dev, struct rte_mbuf *pkt[], uint16_t num);
332
333typedef uint16_t (*TLE_STREAM_RECV_FUNCTYPE)
334	(struct tle_stream *ts, struct rte_mbuf *pkt[], uint16_t num);
335
336typedef int (*TLE_STREAM_CLOSE_FUNCTYPE)(struct tle_stream *s);
337
338#endif /* __NETBE_H__ */
339