Initial commit of tldk code.
[tldk.git] / examples / udpfwd / 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_udp.h>
42 #include <tle_udp_impl.h>
43 #include <tle_event.h>
44
45 #define MAX_PKT_BURST   0x20
46
47 /*
48  * BE related structures.
49  */
50
51 struct netbe_port {
52         uint32_t id;
53         uint32_t lcore;
54         uint32_t mtu;
55         uint32_t rx_offload;
56         uint32_t tx_offload;
57         uint32_t ipv4;
58         struct in6_addr ipv6;
59         struct ether_addr mac;
60 };
61
62 struct netbe_dest {
63         uint32_t line;
64         uint32_t port;
65         uint32_t mtu;
66         uint32_t prfx;
67         uint16_t family;
68         union {
69                 struct in_addr ipv4;
70                 struct in6_addr ipv6;
71         };
72         struct ether_addr mac;
73 };
74
75 struct netbe_dest_prm {
76         uint32_t nb_dest;
77         struct netbe_dest *dest;
78 };
79
80 struct pkt_buf {
81         uint32_t num;
82         struct rte_mbuf *pkt[2 * MAX_PKT_BURST];
83 };
84
85 struct netbe_dev {
86         uint16_t rxqid;
87         uint16_t txqid;
88         struct netbe_port port;
89         struct tle_udp_dev *dev;
90         struct {
91                 uint64_t in;
92                 uint64_t up;
93                 uint64_t drop;
94         } rx_stat;
95         struct {
96                 uint64_t down;
97                 uint64_t out;
98                 uint64_t drop;
99         } tx_stat;
100         struct pkt_buf tx_buf;
101 };
102
103 /* 8 bit LPM user data. */
104 #define LCORE_MAX_DST   (UINT8_MAX + 1)
105
106 struct netbe_lcore {
107         uint32_t id;
108         struct rte_lpm *lpm4;
109         struct rte_lpm6 *lpm6;
110         struct rte_ip_frag_tbl *ftbl;
111         struct tle_udp_ctx *ctx;
112         uint32_t prt_num;
113         uint32_t dst4_num;
114         uint32_t dst6_num;
115         struct netbe_dev prt[RTE_MAX_ETHPORTS];
116         struct tle_udp_dest dst4[LCORE_MAX_DST];
117         struct tle_udp_dest dst6[LCORE_MAX_DST];
118         struct rte_ip_frag_death_row death_row;
119 };
120
121 struct netbe_cfg {
122         uint32_t promisc;
123         uint32_t prt_num;
124         uint32_t cpu_num;
125         struct netbe_port prt[RTE_MAX_ETHPORTS];
126         struct netbe_lcore cpu[RTE_MAX_LCORE];
127 };
128
129 /*
130  * FE related structures.
131  */
132
133 enum {
134         RXONLY,
135         TXONLY,
136         RXTX,
137         FWD,
138 };
139
140 struct netfe_sprm {
141         uint32_t bidx;  /* BE index to use. */
142         struct tle_udp_stream_param prm;
143 };
144
145 struct netfe_stream_prm {
146         uint32_t lcore;
147         uint32_t line;
148         uint16_t op;
149         uint16_t txlen; /* valid/used only for TXONLY op. */
150         struct netfe_sprm sprm;
151         struct netfe_sprm fprm;  /* valid/used only for FWD op. */
152 };
153
154 struct netfe_lcore_prm {
155         uint32_t max_streams;
156         uint32_t nb_streams;
157         struct netfe_stream_prm *stream;
158 };
159
160 struct netfe_stream {
161         struct tle_udp_stream *s;
162         struct tle_event *rxev;
163         struct tle_event *txev;
164         uint16_t op;
165         uint16_t family;
166         uint16_t txlen;
167         struct {
168                 uint64_t rxp;
169                 uint64_t txp;
170                 uint64_t fwp;
171                 uint64_t drops;
172                 uint64_t rxev[TLE_SEV_NUM];
173                 uint64_t txev[TLE_SEV_NUM];
174         } stat;
175         struct pkt_buf pbuf;
176         struct sockaddr_storage raddr;
177         struct netfe_sprm fwdprm;
178 };
179
180 struct netfe_lcore {
181         uint32_t snum;  /* max number of streams */
182         uint32_t sidx;  /* last open stream index */
183         struct tle_evq *rxeq;
184         struct tle_evq *txeq;
185         struct rte_hash *fw4h;
186         struct rte_hash *fw6h;
187         struct netfe_stream *fs;
188 };
189
190 /*
191  * debug/trace macros.
192  */
193
194 #define DUMMY_MACRO     do {} while (0)
195
196 #ifdef NETFE_DEBUG
197 #define NETFE_TRACE(fmt, arg...)        printf(fmt, ##arg)
198 #define NETFE_PKT_DUMP(p)               rte_pktmbuf_dump(stdout, (p), 64)
199 #else
200 #define NETFE_TRACE(fmt, arg...)        DUMMY_MACRO
201 #define NETFE_PKT_DUMP(p)               DUMMY_MACRO
202 #endif
203
204 #ifdef NETBE_DEBUG
205 #define NETBE_TRACE(fmt, arg...)        printf(fmt, ##arg)
206 #define NETBE_PKT_DUMP(p)               rte_pktmbuf_dump(stdout, (p), 64)
207 #else
208 #define NETBE_TRACE(fmt, arg...)        DUMMY_MACRO
209 #define NETBE_PKT_DUMP(p)               DUMMY_MACRO
210 #endif
211
212 #define FUNC_STAT(v, c) do { \
213         static uint64_t nb_call, nb_data; \
214         nb_call++; \
215         nb_data += (v); \
216         if ((nb_call & ((c) - 1)) == 0) { \
217                 printf("%s#%d@%u: nb_call=%lu, avg(" #v ")=%#Lf\n", \
218                         __func__, __LINE__, rte_lcore_id(), nb_call, \
219                         (long double)nb_data / nb_call); \
220                 nb_call = 0; \
221                 nb_data = 0; \
222         } \
223 } while (0)
224
225 #define FUNC_TM_STAT(v, c) do { \
226         static uint64_t nb_call, nb_data; \
227         static uint64_t cts, pts, sts; \
228         cts = rte_rdtsc(); \
229         if (pts != 0) \
230                 sts += cts - pts; \
231         pts = cts; \
232         nb_call++; \
233         nb_data += (v); \
234         if ((nb_call & ((c) - 1)) == 0) { \
235                 printf("%s#%d@%u: nb_call=%lu, " \
236                         "avg(" #v ")=%#Lf, " \
237                         "avg(cycles)=%#Lf, " \
238                         "avg(cycles/" #v ")=%#Lf\n", \
239                         __func__, __LINE__, rte_lcore_id(), nb_call, \
240                         (long double)nb_data / nb_call, \
241                         (long double)sts / nb_call, \
242                         (long double)sts / nb_data); \
243                 nb_call = 0; \
244                 nb_data = 0; \
245                 sts = 0; \
246         } \
247 } while (0)
248
249 int setup_rx_cb(const struct netbe_port *uprt, struct netbe_lcore *lc);
250
251 #endif /* __NETBE_H__ */