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