Introduce first version of TCP code.
[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         struct {
199                 uint64_t rxp;
200                 uint64_t rxb;
201                 uint64_t txp;
202                 uint64_t txb;
203                 uint64_t fwp;
204                 uint64_t drops;
205                 uint64_t rxev[TLE_SEV_NUM];
206                 uint64_t txev[TLE_SEV_NUM];
207                 uint64_t erev[TLE_SEV_NUM];
208         } stat;
209         struct pkt_buf pbuf;
210         struct sockaddr_storage laddr;
211         struct sockaddr_storage raddr;
212         struct netfe_sprm fwdprm;
213         struct netfe_stream *fwds;
214         LIST_ENTRY(netfe_stream) link;
215 };
216
217 struct netfe_stream_list {
218         uint32_t num;
219         LIST_HEAD(, netfe_stream) head;
220 };
221
222 struct netfe_lcore {
223         uint32_t snum;  /* max number of streams */
224         struct tle_evq *syneq;
225         struct tle_evq *ereq;
226         struct tle_evq *rxeq;
227         struct tle_evq *txeq;
228         struct rte_hash *fw4h;
229         struct rte_hash *fw6h;
230         struct {
231                 uint64_t acc;
232                 uint64_t rej;
233                 uint64_t ter;
234         } tcp_stat;
235         struct netfe_stream_list free;
236         struct netfe_stream_list use;
237 };
238
239 struct lcore_prm {
240         struct {
241                 struct netbe_lcore *lc;
242         } be;
243         struct netfe_lcore_prm fe;
244 };
245
246 /*
247  * debug/trace macros.
248  */
249
250 #define DUMMY_MACRO     do {} while (0)
251
252 #ifdef NETFE_DEBUG
253 #define NETFE_TRACE(fmt, arg...)        printf(fmt, ##arg)
254 #define NETFE_PKT_DUMP(p)               rte_pktmbuf_dump(stdout, (p), 64)
255 #else
256 #define NETFE_TRACE(fmt, arg...)        DUMMY_MACRO
257 #define NETFE_PKT_DUMP(p)               DUMMY_MACRO
258 #endif
259
260 #ifdef NETBE_DEBUG
261 #define NETBE_TRACE(fmt, arg...)        printf(fmt, ##arg)
262 #define NETBE_PKT_DUMP(p)               rte_pktmbuf_dump(stdout, (p), 64)
263 #else
264 #define NETBE_TRACE(fmt, arg...)        DUMMY_MACRO
265 #define NETBE_PKT_DUMP(p)               DUMMY_MACRO
266 #endif
267
268 #define FUNC_STAT(v, c) do { \
269         static uint64_t nb_call, nb_data; \
270         nb_call++; \
271         nb_data += (v); \
272         if ((nb_call & ((c) - 1)) == 0) { \
273                 printf("%s#%d@%u: nb_call=%lu, avg(" #v ")=%#Lf\n", \
274                         __func__, __LINE__, rte_lcore_id(), nb_call, \
275                         (long double)nb_data / nb_call); \
276                 nb_call = 0; \
277                 nb_data = 0; \
278         } \
279 } while (0)
280
281 #define FUNC_TM_STAT(v, c) do { \
282         static uint64_t nb_call, nb_data; \
283         static uint64_t cts, pts, sts; \
284         cts = rte_rdtsc(); \
285         if (pts != 0) \
286                 sts += cts - pts; \
287         pts = cts; \
288         nb_call++; \
289         nb_data += (v); \
290         if ((nb_call & ((c) - 1)) == 0) { \
291                 printf("%s#%d@%u: nb_call=%lu, " \
292                         "avg(" #v ")=%#Lf, " \
293                         "avg(cycles)=%#Lf, " \
294                         "avg(cycles/" #v ")=%#Lf\n", \
295                         __func__, __LINE__, rte_lcore_id(), nb_call, \
296                         (long double)nb_data / nb_call, \
297                         (long double)sts / nb_call, \
298                         (long double)sts / nb_data); \
299                 nb_call = 0; \
300                 nb_data = 0; \
301                 sts = 0; \
302         } \
303 } while (0)
304
305 int setup_rx_cb(const struct netbe_port *uprt, struct netbe_lcore *lc,
306         uint16_t qid, uint32_t arp);
307
308 /*
309  * application function pointers
310  */
311
312 typedef int (*LCORE_MAIN_FUNCTYPE)(void *arg);
313
314 /*
315  * tle_l4p lib function pointers
316  */
317
318 typedef uint16_t (*TLE_RX_BULK_FUNCTYPE)
319         (struct tle_dev *dev, struct rte_mbuf *pkt[],
320         struct rte_mbuf *rp[], int32_t rc[], uint16_t num);
321
322 typedef uint16_t (*TLE_TX_BULK_FUNCTYPE)
323         (struct tle_dev *dev, struct rte_mbuf *pkt[], uint16_t num);
324
325 typedef uint16_t (*TLE_STREAM_RECV_FUNCTYPE)
326         (struct tle_stream *ts, struct rte_mbuf *pkt[], uint16_t num);
327
328 typedef int (*TLE_STREAM_CLOSE_FUNCTYPE)(struct tle_stream *s);
329
330 #endif /* __NETBE_H__ */