remove useless memzone includes
[dpdk.git] / examples / load_balancer / init.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 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
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <stdint.h>
37 #include <inttypes.h>
38 #include <sys/types.h>
39 #include <string.h>
40 #include <sys/queue.h>
41 #include <stdarg.h>
42 #include <errno.h>
43 #include <getopt.h>
44
45 #include <rte_common.h>
46 #include <rte_byteorder.h>
47 #include <rte_log.h>
48 #include <rte_memory.h>
49 #include <rte_memcpy.h>
50 #include <rte_eal.h>
51 #include <rte_launch.h>
52 #include <rte_atomic.h>
53 #include <rte_cycles.h>
54 #include <rte_prefetch.h>
55 #include <rte_lcore.h>
56 #include <rte_per_lcore.h>
57 #include <rte_branch_prediction.h>
58 #include <rte_interrupts.h>
59 #include <rte_pci.h>
60 #include <rte_random.h>
61 #include <rte_debug.h>
62 #include <rte_ether.h>
63 #include <rte_ethdev.h>
64 #include <rte_ring.h>
65 #include <rte_mempool.h>
66 #include <rte_mbuf.h>
67 #include <rte_string_fns.h>
68 #include <rte_ip.h>
69 #include <rte_tcp.h>
70 #include <rte_lpm.h>
71
72 #include "main.h"
73
74 static struct rte_eth_conf port_conf = {
75         .rxmode = {
76                 .mq_mode        = ETH_MQ_RX_RSS,
77                 .split_hdr_size = 0,
78                 .header_split   = 0, /**< Header Split disabled */
79                 .hw_ip_checksum = 1, /**< IP checksum offload enabled */
80                 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
81                 .jumbo_frame    = 0, /**< Jumbo Frame Support disabled */
82                 .hw_strip_crc   = 1, /**< CRC stripped by hardware */
83         },
84         .rx_adv_conf = {
85                 .rss_conf = {
86                         .rss_key = NULL,
87                         .rss_hf = ETH_RSS_IP,
88                 },
89         },
90         .txmode = {
91                 .mq_mode = ETH_MQ_TX_NONE,
92         },
93 };
94
95 static void
96 app_assign_worker_ids(void)
97 {
98         uint32_t lcore, worker_id;
99
100         /* Assign ID for each worker */
101         worker_id = 0;
102         for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
103                 struct app_lcore_params_worker *lp_worker = &app.lcore_params[lcore].worker;
104
105                 if (app.lcore_params[lcore].type != e_APP_LCORE_WORKER) {
106                         continue;
107                 }
108
109                 lp_worker->worker_id = worker_id;
110                 worker_id ++;
111         }
112 }
113
114 static void
115 app_init_mbuf_pools(void)
116 {
117         unsigned socket, lcore;
118
119         /* Init the buffer pools */
120         for (socket = 0; socket < APP_MAX_SOCKETS; socket ++) {
121                 char name[32];
122                 if (app_is_socket_used(socket) == 0) {
123                         continue;
124                 }
125
126                 snprintf(name, sizeof(name), "mbuf_pool_%u", socket);
127                 printf("Creating the mbuf pool for socket %u ...\n", socket);
128                 app.pools[socket] = rte_pktmbuf_pool_create(
129                         name, APP_DEFAULT_MEMPOOL_BUFFERS,
130                         APP_DEFAULT_MEMPOOL_CACHE_SIZE,
131                         0, APP_DEFAULT_MBUF_DATA_SIZE, socket);
132                 if (app.pools[socket] == NULL) {
133                         rte_panic("Cannot create mbuf pool on socket %u\n", socket);
134                 }
135         }
136
137         for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
138                 if (app.lcore_params[lcore].type == e_APP_LCORE_DISABLED) {
139                         continue;
140                 }
141
142                 socket = rte_lcore_to_socket_id(lcore);
143                 app.lcore_params[lcore].pool = app.pools[socket];
144         }
145 }
146
147 static void
148 app_init_lpm_tables(void)
149 {
150         unsigned socket, lcore;
151
152         /* Init the LPM tables */
153         for (socket = 0; socket < APP_MAX_SOCKETS; socket ++) {
154                 char name[32];
155                 uint32_t rule;
156
157                 if (app_is_socket_used(socket) == 0) {
158                         continue;
159                 }
160
161                 struct rte_lpm_config lpm_config;
162
163                 lpm_config.max_rules = APP_MAX_LPM_RULES;
164                 lpm_config.number_tbl8s = 256;
165                 lpm_config.flags = 0;
166                 snprintf(name, sizeof(name), "lpm_table_%u", socket);
167                 printf("Creating the LPM table for socket %u ...\n", socket);
168                 app.lpm_tables[socket] = rte_lpm_create(
169                         name,
170                         socket,
171                         &lpm_config);
172                 if (app.lpm_tables[socket] == NULL) {
173                         rte_panic("Unable to create LPM table on socket %u\n", socket);
174                 }
175
176                 for (rule = 0; rule < app.n_lpm_rules; rule ++) {
177                         int ret;
178
179                         ret = rte_lpm_add(app.lpm_tables[socket],
180                                 app.lpm_rules[rule].ip,
181                                 app.lpm_rules[rule].depth,
182                                 app.lpm_rules[rule].if_out);
183
184                         if (ret < 0) {
185                                 rte_panic("Unable to add entry %u (%x/%u => %u) to the LPM table on socket %u (%d)\n",
186                                         (unsigned) rule,
187                                         (unsigned) app.lpm_rules[rule].ip,
188                                         (unsigned) app.lpm_rules[rule].depth,
189                                         (unsigned) app.lpm_rules[rule].if_out,
190                                         socket,
191                                         ret);
192                         }
193                 }
194
195         }
196
197         for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
198                 if (app.lcore_params[lcore].type != e_APP_LCORE_WORKER) {
199                         continue;
200                 }
201
202                 socket = rte_lcore_to_socket_id(lcore);
203                 app.lcore_params[lcore].worker.lpm_table = app.lpm_tables[socket];
204         }
205 }
206
207 static void
208 app_init_rings_rx(void)
209 {
210         unsigned lcore;
211
212         /* Initialize the rings for the RX side */
213         for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
214                 struct app_lcore_params_io *lp_io = &app.lcore_params[lcore].io;
215                 unsigned socket_io, lcore_worker;
216
217                 if ((app.lcore_params[lcore].type != e_APP_LCORE_IO) ||
218                     (lp_io->rx.n_nic_queues == 0)) {
219                         continue;
220                 }
221
222                 socket_io = rte_lcore_to_socket_id(lcore);
223
224                 for (lcore_worker = 0; lcore_worker < APP_MAX_LCORES; lcore_worker ++) {
225                         char name[32];
226                         struct app_lcore_params_worker *lp_worker = &app.lcore_params[lcore_worker].worker;
227                         struct rte_ring *ring = NULL;
228
229                         if (app.lcore_params[lcore_worker].type != e_APP_LCORE_WORKER) {
230                                 continue;
231                         }
232
233                         printf("Creating ring to connect I/O lcore %u (socket %u) with worker lcore %u ...\n",
234                                 lcore,
235                                 socket_io,
236                                 lcore_worker);
237                         snprintf(name, sizeof(name), "app_ring_rx_s%u_io%u_w%u",
238                                 socket_io,
239                                 lcore,
240                                 lcore_worker);
241                         ring = rte_ring_create(
242                                 name,
243                                 app.ring_rx_size,
244                                 socket_io,
245                                 RING_F_SP_ENQ | RING_F_SC_DEQ);
246                         if (ring == NULL) {
247                                 rte_panic("Cannot create ring to connect I/O core %u with worker core %u\n",
248                                         lcore,
249                                         lcore_worker);
250                         }
251
252                         lp_io->rx.rings[lp_io->rx.n_rings] = ring;
253                         lp_io->rx.n_rings ++;
254
255                         lp_worker->rings_in[lp_worker->n_rings_in] = ring;
256                         lp_worker->n_rings_in ++;
257                 }
258         }
259
260         for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
261                 struct app_lcore_params_io *lp_io = &app.lcore_params[lcore].io;
262
263                 if ((app.lcore_params[lcore].type != e_APP_LCORE_IO) ||
264                     (lp_io->rx.n_nic_queues == 0)) {
265                         continue;
266                 }
267
268                 if (lp_io->rx.n_rings != app_get_lcores_worker()) {
269                         rte_panic("Algorithmic error (I/O RX rings)\n");
270                 }
271         }
272
273         for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
274                 struct app_lcore_params_worker *lp_worker = &app.lcore_params[lcore].worker;
275
276                 if (app.lcore_params[lcore].type != e_APP_LCORE_WORKER) {
277                         continue;
278                 }
279
280                 if (lp_worker->n_rings_in != app_get_lcores_io_rx()) {
281                         rte_panic("Algorithmic error (worker input rings)\n");
282                 }
283         }
284 }
285
286 static void
287 app_init_rings_tx(void)
288 {
289         unsigned lcore;
290
291         /* Initialize the rings for the TX side */
292         for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
293                 struct app_lcore_params_worker *lp_worker = &app.lcore_params[lcore].worker;
294                 unsigned port;
295
296                 if (app.lcore_params[lcore].type != e_APP_LCORE_WORKER) {
297                         continue;
298                 }
299
300                 for (port = 0; port < APP_MAX_NIC_PORTS; port ++) {
301                         char name[32];
302                         struct app_lcore_params_io *lp_io = NULL;
303                         struct rte_ring *ring;
304                         uint32_t socket_io, lcore_io;
305
306                         if (app.nic_tx_port_mask[port] == 0) {
307                                 continue;
308                         }
309
310                         if (app_get_lcore_for_nic_tx(port, &lcore_io) < 0) {
311                                 rte_panic("Algorithmic error (no I/O core to handle TX of port %u)\n",
312                                         port);
313                         }
314
315                         lp_io = &app.lcore_params[lcore_io].io;
316                         socket_io = rte_lcore_to_socket_id(lcore_io);
317
318                         printf("Creating ring to connect worker lcore %u with TX port %u (through I/O lcore %u) (socket %u) ...\n",
319                                 lcore, port, (unsigned)lcore_io, (unsigned)socket_io);
320                         snprintf(name, sizeof(name), "app_ring_tx_s%u_w%u_p%u", socket_io, lcore, port);
321                         ring = rte_ring_create(
322                                 name,
323                                 app.ring_tx_size,
324                                 socket_io,
325                                 RING_F_SP_ENQ | RING_F_SC_DEQ);
326                         if (ring == NULL) {
327                                 rte_panic("Cannot create ring to connect worker core %u with TX port %u\n",
328                                         lcore,
329                                         port);
330                         }
331
332                         lp_worker->rings_out[port] = ring;
333                         lp_io->tx.rings[port][lp_worker->worker_id] = ring;
334                 }
335         }
336
337         for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
338                 struct app_lcore_params_io *lp_io = &app.lcore_params[lcore].io;
339                 unsigned i;
340
341                 if ((app.lcore_params[lcore].type != e_APP_LCORE_IO) ||
342                     (lp_io->tx.n_nic_ports == 0)) {
343                         continue;
344                 }
345
346                 for (i = 0; i < lp_io->tx.n_nic_ports; i ++){
347                         unsigned port, j;
348
349                         port = lp_io->tx.nic_ports[i];
350                         for (j = 0; j < app_get_lcores_worker(); j ++) {
351                                 if (lp_io->tx.rings[port][j] == NULL) {
352                                         rte_panic("Algorithmic error (I/O TX rings)\n");
353                                 }
354                         }
355                 }
356         }
357 }
358
359 /* Check the link status of all ports in up to 9s, and print them finally */
360 static void
361 check_all_ports_link_status(uint16_t port_num, uint32_t port_mask)
362 {
363 #define CHECK_INTERVAL 100 /* 100ms */
364 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
365         uint16_t portid;
366         uint8_t count, all_ports_up, print_flag = 0;
367         struct rte_eth_link link;
368         uint32_t n_rx_queues, n_tx_queues;
369
370         printf("\nChecking link status");
371         fflush(stdout);
372         for (count = 0; count <= MAX_CHECK_TIME; count++) {
373                 all_ports_up = 1;
374                 for (portid = 0; portid < port_num; portid++) {
375                         if ((port_mask & (1 << portid)) == 0)
376                                 continue;
377                         n_rx_queues = app_get_nic_rx_queues_per_port(portid);
378                         n_tx_queues = app.nic_tx_port_mask[portid];
379                         if ((n_rx_queues == 0) && (n_tx_queues == 0))
380                                 continue;
381                         memset(&link, 0, sizeof(link));
382                         rte_eth_link_get_nowait(portid, &link);
383                         /* print link status if flag set */
384                         if (print_flag == 1) {
385                                 if (link.link_status)
386                                         printf(
387                                         "Port%d Link Up - speed %uMbps - %s\n",
388                                                 portid, link.link_speed,
389                                 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
390                                         ("full-duplex") : ("half-duplex\n"));
391                                 else
392                                         printf("Port %d Link Down\n", portid);
393                                 continue;
394                         }
395                         /* clear all_ports_up flag if any link down */
396                         if (link.link_status == ETH_LINK_DOWN) {
397                                 all_ports_up = 0;
398                                 break;
399                         }
400                 }
401                 /* after finally printing all link status, get out */
402                 if (print_flag == 1)
403                         break;
404
405                 if (all_ports_up == 0) {
406                         printf(".");
407                         fflush(stdout);
408                         rte_delay_ms(CHECK_INTERVAL);
409                 }
410
411                 /* set the print_flag if all ports up or timeout */
412                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
413                         print_flag = 1;
414                         printf("done\n");
415                 }
416         }
417 }
418
419 static void
420 app_init_nics(void)
421 {
422         unsigned socket;
423         uint32_t lcore;
424         uint16_t port;
425         uint8_t queue;
426         int ret;
427         uint32_t n_rx_queues, n_tx_queues;
428
429         /* Init NIC ports and queues, then start the ports */
430         for (port = 0; port < APP_MAX_NIC_PORTS; port ++) {
431                 struct rte_mempool *pool;
432                 uint16_t nic_rx_ring_size;
433                 uint16_t nic_tx_ring_size;
434
435                 n_rx_queues = app_get_nic_rx_queues_per_port(port);
436                 n_tx_queues = app.nic_tx_port_mask[port];
437
438                 if ((n_rx_queues == 0) && (n_tx_queues == 0)) {
439                         continue;
440                 }
441
442                 /* Init port */
443                 printf("Initializing NIC port %u ...\n", port);
444                 ret = rte_eth_dev_configure(
445                         port,
446                         (uint8_t) n_rx_queues,
447                         (uint8_t) n_tx_queues,
448                         &port_conf);
449                 if (ret < 0) {
450                         rte_panic("Cannot init NIC port %u (%d)\n", port, ret);
451                 }
452                 rte_eth_promiscuous_enable(port);
453
454                 nic_rx_ring_size = app.nic_rx_ring_size;
455                 nic_tx_ring_size = app.nic_tx_ring_size;
456                 ret = rte_eth_dev_adjust_nb_rx_tx_desc(
457                         port, &nic_rx_ring_size, &nic_tx_ring_size);
458                 if (ret < 0) {
459                         rte_panic("Cannot adjust number of descriptors for port %u (%d)\n",
460                                   port, ret);
461                 }
462                 app.nic_rx_ring_size = nic_rx_ring_size;
463                 app.nic_tx_ring_size = nic_tx_ring_size;
464
465                 /* Init RX queues */
466                 for (queue = 0; queue < APP_MAX_RX_QUEUES_PER_NIC_PORT; queue ++) {
467                         if (app.nic_rx_queue_mask[port][queue] == 0) {
468                                 continue;
469                         }
470
471                         app_get_lcore_for_nic_rx(port, queue, &lcore);
472                         socket = rte_lcore_to_socket_id(lcore);
473                         pool = app.lcore_params[lcore].pool;
474
475                         printf("Initializing NIC port %u RX queue %u ...\n",
476                                 port, queue);
477                         ret = rte_eth_rx_queue_setup(
478                                 port,
479                                 queue,
480                                 (uint16_t) app.nic_rx_ring_size,
481                                 socket,
482                                 NULL,
483                                 pool);
484                         if (ret < 0) {
485                                 rte_panic("Cannot init RX queue %u for port %u (%d)\n",
486                                           queue, port, ret);
487                         }
488                 }
489
490                 /* Init TX queues */
491                 if (app.nic_tx_port_mask[port] == 1) {
492                         app_get_lcore_for_nic_tx(port, &lcore);
493                         socket = rte_lcore_to_socket_id(lcore);
494                         printf("Initializing NIC port %u TX queue 0 ...\n",
495                                 port);
496                         ret = rte_eth_tx_queue_setup(
497                                 port,
498                                 0,
499                                 (uint16_t) app.nic_tx_ring_size,
500                                 socket,
501                                 NULL);
502                         if (ret < 0) {
503                                 rte_panic("Cannot init TX queue 0 for port %d (%d)\n",
504                                         port,
505                                         ret);
506                         }
507                 }
508
509                 /* Start port */
510                 ret = rte_eth_dev_start(port);
511                 if (ret < 0) {
512                         rte_panic("Cannot start port %d (%d)\n", port, ret);
513                 }
514         }
515
516         check_all_ports_link_status(APP_MAX_NIC_PORTS, (~0x0));
517 }
518
519 void
520 app_init(void)
521 {
522         app_assign_worker_ids();
523         app_init_mbuf_pools();
524         app_init_lpm_tables();
525         app_init_rings_rx();
526         app_init_rings_tx();
527         app_init_nics();
528
529         printf("Initialization completed.\n");
530 }