* Add siphash file for calculating the sequence number.
[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 MAX_PKT_BURST   0x20
48
49 /* Used to allocate the memory for hash key. */
50 #define RSS_HASH_KEY_LENGTH 64
51
52 /*
53  * global variables
54  */
55
56 enum {
57         VERBOSE_NONE = 0,
58         VERBOSE_NUM = 9
59 };
60
61 extern int verbose;
62
63 /*
64  * BE related structures.
65  */
66
67 struct netbe_port {
68         uint32_t id;
69         uint32_t nb_lcore;
70         uint32_t *lcore_id;
71         uint32_t mtu;
72         uint32_t rx_offload;
73         uint32_t tx_offload;
74         uint32_t ipv4;
75         struct in6_addr ipv6;
76         struct ether_addr mac;
77         uint32_t hash_key_size;
78         uint8_t hash_key[RSS_HASH_KEY_LENGTH];
79 };
80
81 struct netbe_dest {
82         uint32_t line;
83         uint32_t port;
84         uint32_t mtu;
85         uint32_t prfx;
86         uint16_t family;
87         union {
88                 struct in_addr ipv4;
89                 struct in6_addr ipv6;
90         };
91         struct ether_addr mac;
92 };
93
94 struct netbe_dest_prm {
95         uint32_t nb_dest;
96         struct netbe_dest *dest;
97 };
98
99 struct pkt_buf {
100         uint32_t num;
101         struct rte_mbuf *pkt[2 * MAX_PKT_BURST];
102 };
103
104 struct netbe_dev {
105         uint16_t rxqid;
106         uint16_t txqid;
107         struct netbe_port port;
108         struct tle_dev *dev;
109         struct {
110                 uint64_t in;
111                 uint64_t up;
112                 uint64_t drop;
113         } rx_stat;
114         struct {
115                 uint64_t down;
116                 uint64_t out;
117                 uint64_t drop;
118         } tx_stat;
119         struct pkt_buf tx_buf;
120         struct pkt_buf arp_buf;
121 };
122
123 /* 8 bit LPM user data. */
124 #define LCORE_MAX_DST   (UINT8_MAX + 1)
125
126 struct netbe_lcore {
127         uint32_t id;
128         uint32_t proto; /**< L4 proto to handle. */
129         struct rte_lpm *lpm4;
130         struct rte_lpm6 *lpm6;
131         struct rte_ip_frag_tbl *ftbl;
132         struct tle_ctx *ctx;
133         uint32_t prtq_num;
134         uint32_t dst4_num;
135         uint32_t dst6_num;
136         struct netbe_dev *prtq;
137         struct tle_dest dst4[LCORE_MAX_DST];
138         struct tle_dest dst6[LCORE_MAX_DST];
139         struct rte_ip_frag_death_row death_row;
140         struct {
141                 uint64_t flags[UINT8_MAX + 1];
142         } tcp_stat;
143 };
144
145 struct netbe_cfg {
146         uint32_t promisc;
147         uint32_t proto;
148         uint32_t server;
149         uint32_t arp;
150         uint32_t prt_num;
151         uint32_t cpu_num;
152         struct netbe_port *prt;
153         struct netbe_lcore *cpu;
154 };
155
156 /*
157  * FE related structures.
158  */
159
160 enum {
161         RXONLY,
162         TXONLY,
163         RXTX,
164         FWD,
165 };
166
167 struct netfe_sprm {
168         uint32_t bidx;  /* BE index to use. */
169         struct sockaddr_storage local_addr;  /**< stream local address. */
170         struct sockaddr_storage remote_addr; /**< stream remote address. */
171 };
172
173 struct netfe_stream_prm {
174         uint32_t lcore;
175         uint32_t belcore;
176         uint16_t line;
177         uint16_t op;
178         uint16_t txlen; /* valid/used only for TXONLY op. */
179         struct netfe_sprm sprm;
180         struct netfe_sprm fprm;  /* valid/used only for FWD op. */
181 };
182
183 struct netfe_lcore_prm {
184         uint32_t max_streams;
185         uint32_t nb_streams;
186         struct netfe_stream_prm *stream;
187 };
188
189 struct netfe_stream {
190         struct tle_stream *s;
191         struct tle_event *erev;
192         struct tle_event *rxev;
193         struct tle_event *txev;
194         uint16_t op;
195         uint16_t proto;
196         uint16_t family;
197         uint16_t txlen;
198         uint16_t posterr; /* # of time error event handling was postponed */
199         struct {
200                 uint64_t rxp;
201                 uint64_t rxb;
202                 uint64_t txp;
203                 uint64_t txb;
204                 uint64_t fwp;
205                 uint64_t drops;
206                 uint64_t rxev[TLE_SEV_NUM];
207                 uint64_t txev[TLE_SEV_NUM];
208                 uint64_t erev[TLE_SEV_NUM];
209         } stat;
210         struct pkt_buf pbuf;
211         struct sockaddr_storage laddr;
212         struct sockaddr_storage raddr;
213         struct netfe_sprm fwdprm;
214         struct netfe_stream *fwds;
215         LIST_ENTRY(netfe_stream) link;
216 };
217
218 struct netfe_stream_list {
219         uint32_t num;
220         LIST_HEAD(, netfe_stream) head;
221 };
222
223 struct netfe_lcore {
224         uint32_t snum;  /* max number of streams */
225         struct tle_evq *syneq;
226         struct tle_evq *ereq;
227         struct tle_evq *rxeq;
228         struct tle_evq *txeq;
229         struct rte_hash *fw4h;
230         struct rte_hash *fw6h;
231         struct {
232                 uint64_t acc;
233                 uint64_t rej;
234                 uint64_t ter;
235         } tcp_stat;
236         struct netfe_stream_list free;
237         struct netfe_stream_list use;
238 };
239
240 struct lcore_prm {
241         struct {
242                 struct netbe_lcore *lc;
243         } be;
244         struct netfe_lcore_prm fe;
245 };
246
247 /*
248  * debug/trace macros.
249  */
250
251 #define DUMMY_MACRO     do {} while (0)
252
253 #ifdef NETFE_DEBUG
254 #define NETFE_TRACE(fmt, arg...)        printf(fmt, ##arg)
255 #define NETFE_PKT_DUMP(p)               rte_pktmbuf_dump(stdout, (p), 64)
256 #else
257 #define NETFE_TRACE(fmt, arg...)        DUMMY_MACRO
258 #define NETFE_PKT_DUMP(p)               DUMMY_MACRO
259 #endif
260
261 #ifdef NETBE_DEBUG
262 #define NETBE_TRACE(fmt, arg...)        printf(fmt, ##arg)
263 #define NETBE_PKT_DUMP(p)               rte_pktmbuf_dump(stdout, (p), 64)
264 #else
265 #define NETBE_TRACE(fmt, arg...)        DUMMY_MACRO
266 #define NETBE_PKT_DUMP(p)               DUMMY_MACRO
267 #endif
268
269 #define FUNC_STAT(v, c) do { \
270         static uint64_t nb_call, nb_data; \
271         nb_call++; \
272         nb_data += (v); \
273         if ((nb_call & ((c) - 1)) == 0) { \
274                 printf("%s#%d@%u: nb_call=%lu, avg(" #v ")=%#Lf\n", \
275                         __func__, __LINE__, rte_lcore_id(), nb_call, \
276                         (long double)nb_data / nb_call); \
277                 nb_call = 0; \
278                 nb_data = 0; \
279         } \
280 } while (0)
281
282 #define FUNC_TM_STAT(v, c) do { \
283         static uint64_t nb_call, nb_data; \
284         static uint64_t cts, pts, sts; \
285         cts = rte_rdtsc(); \
286         if (pts != 0) \
287                 sts += cts - pts; \
288         pts = cts; \
289         nb_call++; \
290         nb_data += (v); \
291         if ((nb_call & ((c) - 1)) == 0) { \
292                 printf("%s#%d@%u: nb_call=%lu, " \
293                         "avg(" #v ")=%#Lf, " \
294                         "avg(cycles)=%#Lf, " \
295                         "avg(cycles/" #v ")=%#Lf\n", \
296                         __func__, __LINE__, rte_lcore_id(), nb_call, \
297                         (long double)nb_data / nb_call, \
298                         (long double)sts / nb_call, \
299                         (long double)sts / nb_data); \
300                 nb_call = 0; \
301                 nb_data = 0; \
302                 sts = 0; \
303         } \
304 } while (0)
305
306 int setup_rx_cb(const struct netbe_port *uprt, struct netbe_lcore *lc,
307         uint16_t qid, uint32_t arp);
308
309 /*
310  * application function pointers
311  */
312
313 typedef int (*LCORE_MAIN_FUNCTYPE)(void *arg);
314
315 /*
316  * tle_l4p lib function pointers
317  */
318
319 typedef uint16_t (*TLE_RX_BULK_FUNCTYPE)
320         (struct tle_dev *dev, struct rte_mbuf *pkt[],
321         struct rte_mbuf *rp[], int32_t rc[], uint16_t num);
322
323 typedef uint16_t (*TLE_TX_BULK_FUNCTYPE)
324         (struct tle_dev *dev, struct rte_mbuf *pkt[], uint16_t num);
325
326 typedef uint16_t (*TLE_STREAM_RECV_FUNCTYPE)
327         (struct tle_stream *ts, struct rte_mbuf *pkt[], uint16_t num);
328
329 typedef int (*TLE_STREAM_CLOSE_FUNCTYPE)(struct tle_stream *s);
330
331 #endif /* __NETBE_H__ */