aboutsummaryrefslogtreecommitdiffstats
path: root/examples/l4fwd/netbe.h
blob: 6d9aa35ee030c8c989193a7a281183c8f07fe7cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
 * Copyright (c) 2016  Intel Corporation.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef __NETBE_H__
#define __NETBE_H__

#include <stdint.h>
#include <stddef.h>
#include <inttypes.h>
#include <getopt.h>
#include <arpa/inet.h>
#include <assert.h>
#include <signal.h>

#include <rte_config.h>
#include <rte_common.h>
#include <rte_eal.h>
#include <rte_lcore.h>
#include <rte_ethdev.h>
#include <rte_kvargs.h>
#include <rte_errno.h>
#include <rte_malloc.h>
#include <rte_cycles.h>
#include <rte_lpm.h>
#include <rte_lpm6.h>
#include <rte_hash.h>
#include <rte_ip.h>
#include <rte_ip_frag.h>
#include <rte_tcp.h>
#include <rte_udp.h>
#include <tle_tcp.h>
#include <tle_udp.h>
#include <tle_event.h>

#define TLE_DEFAULT_MSS 536

#define	MAX_PKT_BURST	0x20

/* Used to allocate the memory for hash key. */
#define RSS_HASH_KEY_LENGTH 64

/*
 * global variables
 */

enum {
	VERBOSE_NONE = 0,
	VERBOSE_NUM = 9
};

extern int verbose;

/*
 * BE related structures.
 */

struct netbe_port {
	uint32_t id;
	uint32_t nb_lcore;
	uint32_t *lcore_id;
	uint32_t mtu;
	uint64_t rx_offload;
	uint64_t tx_offload;
	uint32_t ipv4;
	struct in6_addr ipv6;
	struct ether_addr mac;
	uint32_t hash_key_size;
	uint8_t hash_key[RSS_HASH_KEY_LENGTH];
};

struct netbe_dest {
	uint32_t line;
	uint32_t port;
	uint32_t mtu;
	uint32_t prfx;
	uint16_t family;
	union {
		struct in_addr ipv4;
		struct in6_addr ipv6;
	};
	struct ether_addr mac;
};

struct netbe_dest_prm {
	uint32_t nb_dest;
	struct netbe_dest *dest;
};

struct pkt_buf {
	uint32_t num;
	struct rte_mbuf *pkt[2 * MAX_PKT_BURST];
};

struct netbe_dev {
	uint16_t rxqid;
	uint16_t txqid;
	struct netbe_port port;
	struct tle_dev *dev;
	struct {
		uint64_t in;
		uint64_t up;
		uint64_t drop;
	} rx_stat;
	struct {
		uint64_t down;
		uint64_t out;
		uint64_t drop;
	} tx_stat;
	struct pkt_buf tx_buf;
	struct pkt_buf arp_buf;
};

/* 8 bit LPM user data. */
#define	LCORE_MAX_DST	(UINT8_MAX + 1)

struct netbe_lcore {
	uint32_t id;
	uint32_t proto; /**< L4 proto to handle. */
	struct rte_lpm *lpm4;
	struct rte_lpm6 *lpm6;
	struct rte_ip_frag_tbl *ftbl;
	struct tle_ctx *ctx;
	uint32_t prtq_num;
	uint32_t dst4_num;
	uint32_t dst6_num;
	struct netbe_dev *prtq;
	struct tle_dest dst4[LCORE_MAX_DST];
	struct tle_dest dst6[LCORE_MAX_DST];
	struct rte_ip_frag_death_row death_row;
	struct {
		uint64_t flags[UINT8_MAX + 1];
	} tcp_stat;
};

struct netbe_cfg {
	uint32_t promisc;
	uint32_t proto;
	uint32_t server;
	uint32_t arp;
	uint32_t prt_num;
	uint32_t cpu_num;
	struct netbe_port *prt;
	struct netbe_lcore *cpu;
};

/*
 * FE related structures.
 */

enum {
	RXONLY,
	TXONLY,
	RXTX,
	ECHO,
	FWD,
};

struct netfe_sprm {
	uint32_t bidx;  /* BE index to use. */
	struct sockaddr_storage local_addr;  /**< stream local address. */
	struct sockaddr_storage remote_addr; /**< stream remote address. */
};

struct netfe_stream_prm {
	uint32_t lcore;
	uint32_t belcore;
	uint16_t line;
	uint16_t op;
	uint32_t txlen; /* valid/used only for TXONLY op. */
	uint32_t rxlen; /* Used by RXTX */
	struct netfe_sprm sprm;
	struct netfe_sprm fprm;  /* valid/used only for FWD op. */
};

struct netfe_lcore_prm {
	uint32_t max_streams;
	uint32_t nb_streams;
	struct netfe_stream_prm *stream;
};

struct netfe_stream {
	struct tle_stream *s;
	struct tle_event *erev;
	struct tle_event *rxev;
	struct tle_event *txev;
	uint16_t op;
	uint16_t proto;
	uint16_t family;
	uint32_t txlen;
	uint32_t rxlen;
	uint16_t reply_count;
	uint32_t rx_run_len;
	uint16_t posterr; /* # of time error event handling was postponed */
	struct {
		uint64_t rxp;
		uint64_t rxb;
		uint64_t txp;
		uint64_t txb;
		uint64_t fwp;
		uint64_t drops;
		uint64_t rxev[TLE_SEV_NUM];
		uint64_t txev[TLE_SEV_NUM];
		uint64_t erev[TLE_SEV_NUM];
	} stat;
	struct pkt_buf pbuf;
	struct sockaddr_storage laddr;
	struct sockaddr_storage raddr;
	struct netfe_sprm fwdprm;
	struct netfe_stream *fwds;
	LIST_ENTRY(netfe_stream) link;
};

struct netfe_stream_list {
	uint32_t num;
	LIST_HEAD(, netfe_stream) head;
};

struct netfe_lcore {
	uint32_t snum;  /* max number of streams */
	struct tle_evq *syneq;
	struct tle_evq *ereq;
	struct tle_evq *rxeq;
	struct tle_evq *txeq;
	struct rte_hash *fw4h;
	struct rte_hash *fw6h;
	struct {
		uint64_t acc;
		uint64_t rej;
		uint64_t ter;
	} tcp_stat;
	struct netfe_stream_list free;
	struct netfe_stream_list use;
};

struct lcore_prm {
	struct {
		struct netbe_lcore *lc;
	} be;
	struct netfe_lcore_prm fe;
};

struct tx_content {
	size_t sz;
	uint8_t *data;
};

extern struct tx_content tx_content;

/*
 * debug/trace macros.
 */

#define	DUMMY_MACRO	do {} while (0)

#ifdef NETFE_DEBUG
#define	NETFE_TRACE(fmt, arg...)	printf(fmt, ##arg)
#define	NETFE_PKT_DUMP(p)		rte_pktmbuf_dump(stdout, (p), 64)
#else
#define	NETFE_TRACE(fmt, arg...)	DUMMY_MACRO
#define	NETFE_PKT_DUMP(p)		DUMMY_MACRO
#endif

#ifdef NETBE_DEBUG
#define	NETBE_TRACE(fmt, arg...)	printf(fmt, ##arg)
#define	NETBE_PKT_DUMP(p)		rte_pktmbuf_dump(stdout, (p), 64)
#else
#define	NETBE_TRACE(fmt, arg...)	DUMMY_MACRO
#define	NETBE_PKT_DUMP(p)		DUMMY_MACRO
#endif

#define FUNC_STAT(v, c) do { \
	static uint64_t nb_call, nb_data; \
	nb_call++; \
	nb_data += (v); \
	if ((nb_call & ((c) - 1)) == 0) { \
		printf("%s#%d@%u: nb_call=%lu, avg(" #v ")=%#Lf\n", \
			__func__, __LINE__, rte_lcore_id(), nb_call, \
			(long double)nb_data / nb_call); \
		nb_call = 0; \
		nb_data = 0; \
	} \
} while (0)

#define FUNC_TM_STAT(v, c) do { \
	static uint64_t nb_call, nb_data; \
	static uint64_t cts, pts, sts; \
	cts = rte_rdtsc(); \
	if (pts != 0) \
		sts += cts - pts; \
	pts = cts; \
	nb_call++; \
	nb_data += (v); \
	if ((nb_call & ((c) - 1)) == 0) { \
		printf("%s#%d@%u: nb_call=%lu, " \
			"avg(" #v ")=%#Lf, " \
			"avg(cycles)=%#Lf, " \
			"avg(cycles/" #v ")=%#Lf\n", \
			__func__, __LINE__, rte_lcore_id(), nb_call, \
			(long double)nb_data / nb_call, \
			(long double)sts / nb_call, \
			(long double)sts / nb_data); \
		nb_call = 0; \
		nb_data = 0; \
		sts = 0; \
	} \
} while (0)

int setup_rx_cb(const struct netbe_port *uprt, struct netbe_lcore *lc,
	uint16_t qid, uint32_t arp);

/*
 * application function pointers
 */

typedef int (*LCORE_MAIN_FUNCTYPE)(void *arg);

/*
 * tle_l4p lib function pointers
 */

typedef uint16_t (*TLE_RX_BULK_FUNCTYPE)
	(struct tle_dev *dev, struct rte_mbuf *pkt[],
	struct rte_mbuf *rp[], int32_t rc[], uint16_t num);

typedef uint16_t (*TLE_TX_BULK_FUNCTYPE)
	(struct tle_dev *dev, struct rte_mbuf *pkt[], uint16_t num);

typedef uint16_t (*TLE_STREAM_RECV_FUNCTYPE)
	(struct tle_stream *ts, struct rte_mbuf *pkt[], uint16_t num);

typedef int (*TLE_STREAM_CLOSE_FUNCTYPE)(struct tle_stream *s);

#endif /* __NETBE_H__ */