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