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