first public release
[dpdk.git] / examples / load_balancer / main.h
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without 
8  *   modification, are permitted provided that the following conditions 
9  *   are met:
10  * 
11  *     * Redistributions of source code must retain the above copyright 
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright 
14  *       notice, this list of conditions and the following disclaimer in 
15  *       the documentation and/or other materials provided with the 
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its 
18  *       contributors may be used to endorse or promote products derived 
19  *       from this software without specific prior written permission.
20  * 
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  * 
33  *  version: DPDK.L.1.2.3-3
34  */
35
36 #ifndef _MAIN_H_
37 #define _MAIN_H_
38
39 /* Logical cores */
40 #ifndef APP_MAX_SOCKETS
41 #define APP_MAX_SOCKETS 2
42 #endif
43
44 #ifndef APP_MAX_LCORES
45 #define APP_MAX_LCORES       RTE_MAX_LCORE
46 #endif
47
48 #ifndef APP_MAX_NIC_PORTS
49 #define APP_MAX_NIC_PORTS    RTE_MAX_ETHPORTS
50 #endif
51
52 #ifndef APP_MAX_RX_QUEUES_PER_NIC_PORT
53 #define APP_MAX_RX_QUEUES_PER_NIC_PORT 128
54 #endif
55
56 #ifndef APP_MAX_TX_QUEUES_PER_NIC_PORT
57 #define APP_MAX_TX_QUEUES_PER_NIC_PORT 128
58 #endif
59
60 #ifndef APP_MAX_IO_LCORES
61 #define APP_MAX_IO_LCORES 16
62 #endif
63 #if (APP_MAX_IO_LCORES > APP_MAX_LCORES)
64 #error "APP_MAX_IO_LCORES is too big"
65 #endif
66
67 #ifndef APP_MAX_NIC_RX_QUEUES_PER_IO_LCORE
68 #define APP_MAX_NIC_RX_QUEUES_PER_IO_LCORE 16
69 #endif
70
71 #ifndef APP_MAX_NIC_TX_PORTS_PER_IO_LCORE
72 #define APP_MAX_NIC_TX_PORTS_PER_IO_LCORE 16
73 #endif
74 #if (APP_MAX_NIC_TX_PORTS_PER_IO_LCORE > APP_MAX_NIC_PORTS)
75 #error "APP_MAX_NIC_TX_PORTS_PER_IO_LCORE too big"
76 #endif
77
78 #ifndef APP_MAX_WORKER_LCORES
79 #define APP_MAX_WORKER_LCORES 16
80 #endif
81 #if (APP_MAX_WORKER_LCORES > APP_MAX_LCORES)
82 #error "APP_MAX_WORKER_LCORES is too big"
83 #endif
84
85
86 /* Mempools */
87 #ifndef APP_DEFAULT_MBUF_SIZE
88 #define APP_DEFAULT_MBUF_SIZE (2048 + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
89 #endif
90
91 #ifndef APP_DEFAULT_MEMPOOL_BUFFERS
92 #define APP_DEFAULT_MEMPOOL_BUFFERS   8192
93 #endif
94
95 #ifndef APP_DEFAULT_MEMPOOL_CACHE_SIZE
96 #define APP_DEFAULT_MEMPOOL_CACHE_SIZE  256
97 #endif
98
99 /* LPM Tables */
100 #ifndef APP_MAX_LPM_RULES
101 #define APP_MAX_LPM_RULES 1024
102 #endif
103
104 /* NIC RX */
105 #ifndef APP_DEFAULT_NIC_RX_RING_SIZE
106 #define APP_DEFAULT_NIC_RX_RING_SIZE 1024
107 #endif
108
109 /*
110  * RX and TX Prefetch, Host, and Write-back threshold values should be
111  * carefully set for optimal performance. Consult the network
112  * controller's datasheet and supporting DPDK documentation for guidance
113  * on how these parameters should be set.
114  */
115 #ifndef APP_DEFAULT_NIC_RX_PTHRESH
116 #define APP_DEFAULT_NIC_RX_PTHRESH  8
117 #endif
118
119 #ifndef APP_DEFAULT_NIC_RX_HTHRESH
120 #define APP_DEFAULT_NIC_RX_HTHRESH  8
121 #endif
122
123 #ifndef APP_DEFAULT_NIC_RX_WTHRESH
124 #define APP_DEFAULT_NIC_RX_WTHRESH  4
125 #endif
126
127 #ifndef APP_DEFAULT_NIC_RX_FREE_THRESH
128 #define APP_DEFAULT_NIC_RX_FREE_THRESH  64
129 #endif
130
131 /* NIC TX */
132 #ifndef APP_DEFAULT_NIC_TX_RING_SIZE
133 #define APP_DEFAULT_NIC_TX_RING_SIZE 1024
134 #endif
135
136 /*
137  * These default values are optimized for use with the Intel(R) 82599 10 GbE
138  * Controller and the DPDK ixgbe PMD. Consider using other values for other
139  * network controllers and/or network drivers.
140  */
141 #ifndef APP_DEFAULT_NIC_TX_PTHRESH
142 #define APP_DEFAULT_NIC_TX_PTHRESH  36
143 #endif
144
145 #ifndef APP_DEFAULT_NIC_TX_HTHRESH
146 #define APP_DEFAULT_NIC_TX_HTHRESH  0
147 #endif
148
149 #ifndef APP_DEFAULT_NIC_TX_WTHRESH
150 #define APP_DEFAULT_NIC_TX_WTHRESH  0
151 #endif
152
153 #ifndef APP_DEFAULT_NIC_TX_FREE_THRESH
154 #define APP_DEFAULT_NIC_TX_FREE_THRESH  0
155 #endif
156
157 #ifndef APP_DEFAULT_NIC_TX_RS_THRESH
158 #define APP_DEFAULT_NIC_TX_RS_THRESH  0
159 #endif
160
161 /* Software Rings */
162 #ifndef APP_DEFAULT_RING_RX_SIZE
163 #define APP_DEFAULT_RING_RX_SIZE 1024
164 #endif
165
166 #ifndef APP_DEFAULT_RING_TX_SIZE
167 #define APP_DEFAULT_RING_TX_SIZE 1024
168 #endif
169
170 /* Bursts */
171 #ifndef APP_MBUF_ARRAY_SIZE
172 #define APP_MBUF_ARRAY_SIZE   512
173 #endif
174
175 #ifndef APP_DEFAULT_BURST_SIZE_IO_RX_READ
176 #define APP_DEFAULT_BURST_SIZE_IO_RX_READ  144
177 #endif
178 #if (APP_DEFAULT_BURST_SIZE_IO_RX_READ > APP_MBUF_ARRAY_SIZE)
179 #error "APP_DEFAULT_BURST_SIZE_IO_RX_READ is too big"
180 #endif
181
182 #ifndef APP_DEFAULT_BURST_SIZE_IO_RX_WRITE
183 #define APP_DEFAULT_BURST_SIZE_IO_RX_WRITE  144
184 #endif
185 #if (APP_DEFAULT_BURST_SIZE_IO_RX_WRITE > APP_MBUF_ARRAY_SIZE)
186 #error "APP_DEFAULT_BURST_SIZE_IO_RX_WRITE is too big"
187 #endif
188
189 #ifndef APP_DEFAULT_BURST_SIZE_IO_TX_READ
190 #define APP_DEFAULT_BURST_SIZE_IO_TX_READ  144
191 #endif
192 #if (APP_DEFAULT_BURST_SIZE_IO_TX_READ > APP_MBUF_ARRAY_SIZE)
193 #error "APP_DEFAULT_BURST_SIZE_IO_TX_READ is too big"
194 #endif
195
196 #ifndef APP_DEFAULT_BURST_SIZE_IO_TX_WRITE
197 #define APP_DEFAULT_BURST_SIZE_IO_TX_WRITE  144
198 #endif
199 #if (APP_DEFAULT_BURST_SIZE_IO_TX_WRITE > APP_MBUF_ARRAY_SIZE)
200 #error "APP_DEFAULT_BURST_SIZE_IO_TX_WRITE is too big"
201 #endif
202
203 #ifndef APP_DEFAULT_BURST_SIZE_WORKER_READ
204 #define APP_DEFAULT_BURST_SIZE_WORKER_READ  144
205 #endif
206 #if ((2 * APP_DEFAULT_BURST_SIZE_WORKER_READ) > APP_MBUF_ARRAY_SIZE)
207 #error "APP_DEFAULT_BURST_SIZE_WORKER_READ is too big"
208 #endif
209
210 #ifndef APP_DEFAULT_BURST_SIZE_WORKER_WRITE
211 #define APP_DEFAULT_BURST_SIZE_WORKER_WRITE  144
212 #endif
213 #if (APP_DEFAULT_BURST_SIZE_WORKER_WRITE > APP_MBUF_ARRAY_SIZE)
214 #error "APP_DEFAULT_BURST_SIZE_WORKER_WRITE is too big"
215 #endif
216
217 /* Load balancing logic */
218 #ifndef APP_DEFAULT_IO_RX_LB_POS
219 #define APP_DEFAULT_IO_RX_LB_POS 29
220 #endif
221 #if (APP_DEFAULT_IO_RX_LB_POS >= 64)
222 #error "APP_DEFAULT_IO_RX_LB_POS is too big"
223 #endif
224
225 struct app_mbuf_array {
226         struct rte_mbuf *array[APP_MBUF_ARRAY_SIZE];
227         uint32_t n_mbufs;
228 };
229
230 enum app_lcore_type {
231         e_APP_LCORE_DISABLED = 0,
232         e_APP_LCORE_IO,
233         e_APP_LCORE_WORKER
234 };
235
236 struct app_lcore_params_io {
237         /* I/O RX */
238         struct {
239                 /* NIC */
240                 struct {
241                         uint8_t port;
242                         uint8_t queue;
243                 } nic_queues[APP_MAX_NIC_RX_QUEUES_PER_IO_LCORE];
244                 uint32_t n_nic_queues;
245
246                 /* Rings */
247                 struct rte_ring *rings[APP_MAX_WORKER_LCORES];
248                 uint32_t n_rings;
249
250                 /* Internal buffers */
251                 struct app_mbuf_array mbuf_in;
252                 struct app_mbuf_array mbuf_out[APP_MAX_WORKER_LCORES];
253                 uint8_t mbuf_out_flush[APP_MAX_WORKER_LCORES];
254
255                 /* Stats */
256                 uint32_t nic_queues_count[APP_MAX_NIC_RX_QUEUES_PER_IO_LCORE];
257                 uint32_t nic_queues_iters[APP_MAX_NIC_RX_QUEUES_PER_IO_LCORE];
258                 uint32_t rings_count[APP_MAX_WORKER_LCORES];
259                 uint32_t rings_iters[APP_MAX_WORKER_LCORES];
260         } rx;
261
262         /* I/O TX */
263         struct {
264                 /* Rings */
265                 struct rte_ring *rings[APP_MAX_NIC_PORTS][APP_MAX_WORKER_LCORES];
266
267                 /* NIC */
268                 uint8_t nic_ports[APP_MAX_NIC_TX_PORTS_PER_IO_LCORE];
269                 uint32_t n_nic_ports;
270
271                 /* Internal buffers */
272                 struct app_mbuf_array mbuf_out[APP_MAX_NIC_TX_PORTS_PER_IO_LCORE];
273                 uint8_t mbuf_out_flush[APP_MAX_NIC_TX_PORTS_PER_IO_LCORE];
274
275                 /* Stats */
276                 uint32_t rings_count[APP_MAX_NIC_PORTS][APP_MAX_WORKER_LCORES];
277                 uint32_t rings_iters[APP_MAX_NIC_PORTS][APP_MAX_WORKER_LCORES];
278                 uint32_t nic_ports_count[APP_MAX_NIC_TX_PORTS_PER_IO_LCORE];
279                 uint32_t nic_ports_iters[APP_MAX_NIC_TX_PORTS_PER_IO_LCORE];
280         } tx;
281 };
282
283 struct app_lcore_params_worker {
284         /* Rings */
285         struct rte_ring *rings_in[APP_MAX_IO_LCORES];
286         uint32_t n_rings_in;
287         struct rte_ring *rings_out[APP_MAX_NIC_PORTS];
288
289         /* LPM table */
290         struct rte_lpm *lpm_table;
291         uint32_t worker_id;
292
293         /* Internal buffers */
294         struct app_mbuf_array mbuf_in;
295         struct app_mbuf_array mbuf_out[APP_MAX_NIC_PORTS];
296         uint8_t mbuf_out_flush[APP_MAX_NIC_PORTS];
297
298         /* Stats */
299         uint32_t rings_in_count[APP_MAX_IO_LCORES];
300         uint32_t rings_in_iters[APP_MAX_IO_LCORES];
301         uint32_t rings_out_count[APP_MAX_NIC_PORTS];
302         uint32_t rings_out_iters[APP_MAX_NIC_PORTS];
303 };
304
305 struct app_lcore_params {
306         union {
307                 struct app_lcore_params_io io;
308                 struct app_lcore_params_worker worker;
309         };
310         enum app_lcore_type type;
311         struct rte_mempool *pool;
312 } __rte_cache_aligned;
313
314 struct app_lpm_rule {
315         uint32_t ip;
316         uint8_t depth;
317         uint8_t if_out;
318 };
319
320 struct app_params {
321         /* lcore */
322         struct app_lcore_params lcore_params[APP_MAX_LCORES];
323
324         /* NIC */
325         uint8_t nic_rx_queue_mask[APP_MAX_NIC_PORTS][APP_MAX_RX_QUEUES_PER_NIC_PORT];
326         uint8_t nic_tx_port_mask[APP_MAX_NIC_PORTS];
327
328         /* mbuf pools */
329         struct rte_mempool *pools[APP_MAX_SOCKETS];
330
331         /* LPM tables */
332         struct rte_lpm *lpm_tables[APP_MAX_SOCKETS];
333         struct app_lpm_rule lpm_rules[APP_MAX_LPM_RULES];
334         uint32_t n_lpm_rules;
335
336         /* rings */
337         uint32_t nic_rx_ring_size;
338         uint32_t nic_tx_ring_size;
339         uint32_t ring_rx_size;
340         uint32_t ring_tx_size;
341
342         /* burst size */
343         uint32_t burst_size_io_rx_read;
344         uint32_t burst_size_io_rx_write;
345         uint32_t burst_size_io_tx_read;
346         uint32_t burst_size_io_tx_write;
347         uint32_t burst_size_worker_read;
348         uint32_t burst_size_worker_write;
349
350         /* load balancing */
351         uint8_t pos_lb;
352 } __rte_cache_aligned;
353
354 extern struct app_params app;
355
356 int app_parse_args(int argc, char **argv);
357 void app_print_usage(void);
358 void app_init(void);
359 int app_lcore_main_loop(void *arg);
360
361 int app_get_nic_rx_queues_per_port(uint8_t port);
362 int app_get_lcore_for_nic_rx(uint8_t port, uint8_t queue, uint32_t *lcore_out);
363 int app_get_lcore_for_nic_tx(uint8_t port, uint32_t *lcore_out);
364 int app_is_socket_used(uint32_t socket);
365 uint32_t app_get_lcores_io_rx(void);
366 uint32_t app_get_lcores_worker(void);
367 void app_print_params(void);
368
369 #ifdef RTE_EXEC_ENV_BAREMETAL
370 #define MAIN _main
371 #else
372 #define MAIN main
373 #endif
374
375 int MAIN(int argc, char **argv);
376
377 #endif /* _MAIN_H_ */