l4fwd: allow to specify TX payload contents for rxtx mode
[tldk.git] / examples / l4fwd / netbe.h
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
58 enum {
59         VERBOSE_NONE = 0,
60         VERBOSE_NUM = 9
61 };
62
63 extern int verbose;
64
65 /*
66  * BE related structures.
67  */
68
69 struct 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
83 struct 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
96 struct netbe_dest_prm {
97         uint32_t nb_dest;
98         struct netbe_dest *dest;
99 };
100
101 struct pkt_buf {
102         uint32_t num;
103         struct rte_mbuf *pkt[2 * MAX_PKT_BURST];
104 };
105
106 struct 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
128 struct 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
147 struct 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
162 enum {
163         RXONLY,
164         TXONLY,
165         RXTX,
166         ECHO,
167         FWD,
168 };
169
170 struct 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
176 struct 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
187 struct netfe_lcore_prm {
188         uint32_t max_streams;
189         uint32_t nb_streams;
190         struct netfe_stream_prm *stream;
191 };
192
193 struct 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
225 struct netfe_stream_list {
226         uint32_t num;
227         LIST_HEAD(, netfe_stream) head;
228 };
229
230 struct 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
247 struct lcore_prm {
248         struct {
249                 struct netbe_lcore *lc;
250         } be;
251         struct netfe_lcore_prm fe;
252 };
253
254 struct tx_content {
255         size_t sz;
256         uint8_t *data;
257 };
258
259 extern struct tx_content tx_content;
260
261 /*
262  * debug/trace macros.
263  */
264
265 #define DUMMY_MACRO     do {} while (0)
266
267 #ifdef NETFE_DEBUG
268 #define NETFE_TRACE(fmt, arg...)        printf(fmt, ##arg)
269 #define NETFE_PKT_DUMP(p)               rte_pktmbuf_dump(stdout, (p), 64)
270 #else
271 #define NETFE_TRACE(fmt, arg...)        DUMMY_MACRO
272 #define NETFE_PKT_DUMP(p)               DUMMY_MACRO
273 #endif
274
275 #ifdef NETBE_DEBUG
276 #define NETBE_TRACE(fmt, arg...)        printf(fmt, ##arg)
277 #define NETBE_PKT_DUMP(p)               rte_pktmbuf_dump(stdout, (p), 64)
278 #else
279 #define NETBE_TRACE(fmt, arg...)        DUMMY_MACRO
280 #define NETBE_PKT_DUMP(p)               DUMMY_MACRO
281 #endif
282
283 #define FUNC_STAT(v, c) do { \
284         static uint64_t nb_call, nb_data; \
285         nb_call++; \
286         nb_data += (v); \
287         if ((nb_call & ((c) - 1)) == 0) { \
288                 printf("%s#%d@%u: nb_call=%lu, avg(" #v ")=%#Lf\n", \
289                         __func__, __LINE__, rte_lcore_id(), nb_call, \
290                         (long double)nb_data / nb_call); \
291                 nb_call = 0; \
292                 nb_data = 0; \
293         } \
294 } while (0)
295
296 #define FUNC_TM_STAT(v, c) do { \
297         static uint64_t nb_call, nb_data; \
298         static uint64_t cts, pts, sts; \
299         cts = rte_rdtsc(); \
300         if (pts != 0) \
301                 sts += cts - pts; \
302         pts = cts; \
303         nb_call++; \
304         nb_data += (v); \
305         if ((nb_call & ((c) - 1)) == 0) { \
306                 printf("%s#%d@%u: nb_call=%lu, " \
307                         "avg(" #v ")=%#Lf, " \
308                         "avg(cycles)=%#Lf, " \
309                         "avg(cycles/" #v ")=%#Lf\n", \
310                         __func__, __LINE__, rte_lcore_id(), nb_call, \
311                         (long double)nb_data / nb_call, \
312                         (long double)sts / nb_call, \
313                         (long double)sts / nb_data); \
314                 nb_call = 0; \
315                 nb_data = 0; \
316                 sts = 0; \
317         } \
318 } while (0)
319
320 int setup_rx_cb(const struct netbe_port *uprt, struct netbe_lcore *lc,
321         uint16_t qid, uint32_t arp);
322
323 /*
324  * application function pointers
325  */
326
327 typedef int (*LCORE_MAIN_FUNCTYPE)(void *arg);
328
329 /*
330  * tle_l4p lib function pointers
331  */
332
333 typedef uint16_t (*TLE_RX_BULK_FUNCTYPE)
334         (struct tle_dev *dev, struct rte_mbuf *pkt[],
335         struct rte_mbuf *rp[], int32_t rc[], uint16_t num);
336
337 typedef uint16_t (*TLE_TX_BULK_FUNCTYPE)
338         (struct tle_dev *dev, struct rte_mbuf *pkt[], uint16_t num);
339
340 typedef uint16_t (*TLE_STREAM_RECV_FUNCTYPE)
341         (struct tle_stream *ts, struct rte_mbuf *pkt[], uint16_t num);
342
343 typedef int (*TLE_STREAM_CLOSE_FUNCTYPE)(struct tle_stream *s);
344
345 #endif /* __NETBE_H__ */