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