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