00e2e40599fea1370b43fd12c5875d6649ca6311
[dpdk.git] / examples / server_node_efd / server / init.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016-2017 Intel Corporation
3  */
4
5 #include <stdint.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <sys/queue.h>
9 #include <errno.h>
10 #include <stdarg.h>
11 #include <inttypes.h>
12
13 #include <rte_common.h>
14 #include <rte_memory.h>
15 #include <rte_memzone.h>
16 #include <rte_eal.h>
17 #include <rte_byteorder.h>
18 #include <rte_atomic.h>
19 #include <rte_launch.h>
20 #include <rte_per_lcore.h>
21 #include <rte_lcore.h>
22 #include <rte_branch_prediction.h>
23 #include <rte_debug.h>
24 #include <rte_ring.h>
25 #include <rte_log.h>
26 #include <rte_mempool.h>
27 #include <rte_memcpy.h>
28 #include <rte_mbuf.h>
29 #include <rte_interrupts.h>
30 #include <rte_ether.h>
31 #include <rte_ethdev.h>
32 #include <rte_malloc.h>
33 #include <rte_string_fns.h>
34 #include <rte_cycles.h>
35 #include <rte_efd.h>
36 #include <rte_hash.h>
37
38 #include "common.h"
39 #include "args.h"
40 #include "init.h"
41
42 #define MBUFS_PER_NODE 1536
43 #define MBUFS_PER_PORT 1536
44 #define MBUF_CACHE_SIZE 512
45
46 #define RTE_MP_RX_DESC_DEFAULT 512
47 #define RTE_MP_TX_DESC_DEFAULT 512
48 #define NODE_QUEUE_RINGSIZE 128
49
50 #define NO_FLAGS 0
51
52 /* The mbuf pool for packet rx */
53 struct rte_mempool *pktmbuf_pool;
54
55 /* array of info/queues for nodes */
56 struct node *nodes;
57
58 /* EFD table */
59 struct rte_efd_table *efd_table;
60
61 /* Shared info between server and nodes */
62 struct shared_info *info;
63
64 /**
65  * Initialise the mbuf pool for packet reception for the NIC, and any other
66  * buffer pools needed by the app - currently none.
67  */
68 static int
69 init_mbuf_pools(void)
70 {
71         const unsigned int num_mbufs = (num_nodes * MBUFS_PER_NODE) +
72                         (info->num_ports * MBUFS_PER_PORT);
73
74         /*
75          * Don't pass single-producer/single-consumer flags to mbuf create as it
76          * seems faster to use a cache instead
77          */
78         printf("Creating mbuf pool '%s' [%u mbufs] ...\n",
79                         PKTMBUF_POOL_NAME, num_mbufs);
80         pktmbuf_pool = rte_pktmbuf_pool_create(PKTMBUF_POOL_NAME, num_mbufs,
81                 MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
82
83         return pktmbuf_pool == NULL; /* 0  on success */
84 }
85
86 /**
87  * Initialise an individual port:
88  * - configure number of rx and tx rings
89  * - set up each rx ring, to pull from the main mbuf pool
90  * - set up each tx ring
91  * - start the port and report its status to stdout
92  */
93 static int
94 init_port(uint16_t port_num)
95 {
96         /* for port configuration all features are off by default */
97         struct rte_eth_conf port_conf = {
98                 .rxmode = {
99                         .mq_mode = ETH_MQ_RX_RSS,
100                 },
101         };
102         const uint16_t rx_rings = 1, tx_rings = num_nodes;
103         uint16_t rx_ring_size = RTE_MP_RX_DESC_DEFAULT;
104         uint16_t tx_ring_size = RTE_MP_TX_DESC_DEFAULT;
105         struct rte_eth_dev_info dev_info;
106         struct rte_eth_txconf txconf;
107
108         uint16_t q;
109         int retval;
110
111         printf("Port %u init ... ", port_num);
112         fflush(stdout);
113
114         retval = rte_eth_dev_info_get(port_num, &dev_info);
115         if (retval != 0)
116                 return retval;
117
118         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
119                 port_conf.txmode.offloads |=
120                         DEV_TX_OFFLOAD_MBUF_FAST_FREE;
121
122         /*
123          * Standard DPDK port initialisation - config port, then set up
124          * rx and tx rings.
125          */
126         retval = rte_eth_dev_configure(port_num, rx_rings, tx_rings, &port_conf);
127         if (retval != 0)
128                 return retval;
129
130         retval = rte_eth_dev_adjust_nb_rx_tx_desc(port_num, &rx_ring_size,
131                         &tx_ring_size);
132         if (retval != 0)
133                 return retval;
134
135         for (q = 0; q < rx_rings; q++) {
136                 retval = rte_eth_rx_queue_setup(port_num, q, rx_ring_size,
137                                 rte_eth_dev_socket_id(port_num),
138                                 NULL, pktmbuf_pool);
139                 if (retval < 0)
140                         return retval;
141         }
142
143         txconf = dev_info.default_txconf;
144         txconf.offloads = port_conf.txmode.offloads;
145         for (q = 0; q < tx_rings; q++) {
146                 retval = rte_eth_tx_queue_setup(port_num, q, tx_ring_size,
147                                 rte_eth_dev_socket_id(port_num),
148                                 &txconf);
149                 if (retval < 0)
150                         return retval;
151         }
152
153         retval = rte_eth_promiscuous_enable(port_num);
154         if (retval != 0)
155                 return retval;
156
157         retval = rte_eth_dev_start(port_num);
158         if (retval < 0)
159                 return retval;
160
161         printf("done:\n");
162
163         return 0;
164 }
165
166 /**
167  * Set up the DPDK rings which will be used to pass packets, via
168  * pointers, between the multi-process server and node processes.
169  * Each node needs one RX queue.
170  */
171 static int
172 init_shm_rings(void)
173 {
174         unsigned int i;
175         unsigned int socket_id;
176         const char *q_name;
177         const unsigned int ringsize = NODE_QUEUE_RINGSIZE;
178
179         nodes = rte_malloc("node details",
180                 sizeof(*nodes) * num_nodes, 0);
181         if (nodes == NULL)
182                 rte_exit(EXIT_FAILURE, "Cannot allocate memory for "
183                                 "node program details\n");
184
185         for (i = 0; i < num_nodes; i++) {
186                 /* Create an RX queue for each node */
187                 socket_id = rte_socket_id();
188                 q_name = get_rx_queue_name(i);
189                 nodes[i].rx_q = rte_ring_create(q_name,
190                                 ringsize, socket_id,
191                                 RING_F_SP_ENQ | RING_F_SC_DEQ);
192                 if (nodes[i].rx_q == NULL)
193                         rte_exit(EXIT_FAILURE, "Cannot create rx ring queue "
194                                         "for node %u\n", i);
195         }
196         return 0;
197 }
198
199 /*
200  * Create EFD table which will contain all the flows
201  * that will be distributed among the nodes
202  */
203 static void
204 create_efd_table(void)
205 {
206         uint8_t socket_id = rte_socket_id();
207
208         /* create table */
209         efd_table = rte_efd_create("flow table", num_flows * 2, sizeof(uint32_t),
210                         1 << socket_id, socket_id);
211
212         if (efd_table == NULL)
213                 rte_exit(EXIT_FAILURE, "Problem creating the flow table\n");
214 }
215
216 static void
217 populate_efd_table(void)
218 {
219         unsigned int i;
220         int32_t ret;
221         uint32_t ip_dst;
222         uint8_t socket_id = rte_socket_id();
223         uint64_t node_id;
224
225         /* Add flows in table */
226         for (i = 0; i < num_flows; i++) {
227                 node_id = i % num_nodes;
228
229                 ip_dst = rte_cpu_to_be_32(i);
230                 ret = rte_efd_update(efd_table, socket_id,
231                                 (void *)&ip_dst, (efd_value_t)node_id);
232                 if (ret < 0)
233                         rte_exit(EXIT_FAILURE, "Unable to add entry %u in "
234                                         "EFD table\n", i);
235         }
236
237         printf("EFD table: Adding 0x%x keys\n", num_flows);
238 }
239
240 /* Check the link status of all ports in up to 9s, and print them finally */
241 static void
242 check_all_ports_link_status(uint16_t port_num, uint32_t port_mask)
243 {
244 #define CHECK_INTERVAL 100 /* 100ms */
245 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
246         uint8_t count, all_ports_up, print_flag = 0;
247         uint16_t portid;
248         struct rte_eth_link link;
249         int ret;
250
251         printf("\nChecking link status");
252         fflush(stdout);
253         for (count = 0; count <= MAX_CHECK_TIME; count++) {
254                 all_ports_up = 1;
255                 for (portid = 0; portid < port_num; portid++) {
256                         if ((port_mask & (1 << info->id[portid])) == 0)
257                                 continue;
258                         memset(&link, 0, sizeof(link));
259                         ret = rte_eth_link_get_nowait(info->id[portid], &link);
260                         if (ret < 0) {
261                                 all_ports_up = 0;
262                                 if (print_flag == 1)
263                                         printf("Port %u link get failed: %s\n",
264                                                 portid, rte_strerror(-ret));
265                                 continue;
266                         }
267                         /* print link status if flag set */
268                         if (print_flag == 1) {
269                                 if (link.link_status)
270                                         printf(
271                                         "Port%d Link Up. Speed %u Mbps - %s\n",
272                                                 info->id[portid],
273                                                 link.link_speed,
274                                 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
275                                         ("full-duplex") : ("half-duplex\n"));
276                                 else
277                                         printf("Port %d Link Down\n",
278                                                 info->id[portid]);
279                                 continue;
280                         }
281                         /* clear all_ports_up flag if any link down */
282                         if (link.link_status == ETH_LINK_DOWN) {
283                                 all_ports_up = 0;
284                                 break;
285                         }
286                 }
287                 /* after finally printing all link status, get out */
288                 if (print_flag == 1)
289                         break;
290
291                 if (all_ports_up == 0) {
292                         printf(".");
293                         fflush(stdout);
294                         rte_delay_ms(CHECK_INTERVAL);
295                 }
296
297                 /* set the print_flag if all ports up or timeout */
298                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
299                         print_flag = 1;
300                         printf("done\n");
301                 }
302         }
303 }
304
305 /**
306  * Main init function for the multi-process server app,
307  * calls subfunctions to do each stage of the initialisation.
308  */
309 int
310 init(int argc, char *argv[])
311 {
312         int retval;
313         const struct rte_memzone *mz;
314         uint8_t i, total_ports;
315
316         /* init EAL, parsing EAL args */
317         retval = rte_eal_init(argc, argv);
318         if (retval < 0)
319                 return -1;
320         argc -= retval;
321         argv += retval;
322
323         /* get total number of ports */
324         total_ports = rte_eth_dev_count_avail();
325
326         /* set up array for port data */
327         mz = rte_memzone_reserve(MZ_SHARED_INFO, sizeof(*info),
328                                 rte_socket_id(), NO_FLAGS);
329         if (mz == NULL)
330                 rte_exit(EXIT_FAILURE, "Cannot reserve memory zone "
331                                 "for port information\n");
332         memset(mz->addr, 0, sizeof(*info));
333         info = mz->addr;
334
335         /* parse additional, application arguments */
336         retval = parse_app_args(total_ports, argc, argv);
337         if (retval != 0)
338                 return -1;
339
340         /* initialise mbuf pools */
341         retval = init_mbuf_pools();
342         if (retval != 0)
343                 rte_exit(EXIT_FAILURE, "Cannot create needed mbuf pools\n");
344
345         /* now initialise the ports we will use */
346         for (i = 0; i < info->num_ports; i++) {
347                 retval = init_port(info->id[i]);
348                 if (retval != 0)
349                         rte_exit(EXIT_FAILURE, "Cannot initialise port %u\n",
350                                         (unsigned int) i);
351         }
352
353         check_all_ports_link_status(info->num_ports, (~0x0));
354
355         /* initialise the node queues/rings for inter-eu comms */
356         init_shm_rings();
357
358         /* Create the EFD table */
359         create_efd_table();
360
361         /* Populate the EFD table */
362         populate_efd_table();
363
364         /* Share the total number of nodes */
365         info->num_nodes = num_nodes;
366
367         /* Share the total number of flows */
368         info->num_flows = num_flows;
369         return 0;
370 }