New upstream version 18.08
[deb_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         rte_eth_dev_info_get(port_num, &dev_info);
115         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
116                 port_conf.txmode.offloads |=
117                         DEV_TX_OFFLOAD_MBUF_FAST_FREE;
118
119         /*
120          * Standard DPDK port initialisation - config port, then set up
121          * rx and tx rings.
122          */
123         retval = rte_eth_dev_configure(port_num, rx_rings, tx_rings, &port_conf);
124         if (retval != 0)
125                 return retval;
126
127         retval = rte_eth_dev_adjust_nb_rx_tx_desc(port_num, &rx_ring_size,
128                         &tx_ring_size);
129         if (retval != 0)
130                 return retval;
131
132         for (q = 0; q < rx_rings; q++) {
133                 retval = rte_eth_rx_queue_setup(port_num, q, rx_ring_size,
134                                 rte_eth_dev_socket_id(port_num),
135                                 NULL, pktmbuf_pool);
136                 if (retval < 0)
137                         return retval;
138         }
139
140         txconf = dev_info.default_txconf;
141         txconf.offloads = port_conf.txmode.offloads;
142         for (q = 0; q < tx_rings; q++) {
143                 retval = rte_eth_tx_queue_setup(port_num, q, tx_ring_size,
144                                 rte_eth_dev_socket_id(port_num),
145                                 &txconf);
146                 if (retval < 0)
147                         return retval;
148         }
149
150         rte_eth_promiscuous_enable(port_num);
151
152         retval = rte_eth_dev_start(port_num);
153         if (retval < 0)
154                 return retval;
155
156         printf("done:\n");
157
158         return 0;
159 }
160
161 /**
162  * Set up the DPDK rings which will be used to pass packets, via
163  * pointers, between the multi-process server and node processes.
164  * Each node needs one RX queue.
165  */
166 static int
167 init_shm_rings(void)
168 {
169         unsigned int i;
170         unsigned int socket_id;
171         const char *q_name;
172         const unsigned int ringsize = NODE_QUEUE_RINGSIZE;
173
174         nodes = rte_malloc("node details",
175                 sizeof(*nodes) * num_nodes, 0);
176         if (nodes == NULL)
177                 rte_exit(EXIT_FAILURE, "Cannot allocate memory for "
178                                 "node program details\n");
179
180         for (i = 0; i < num_nodes; i++) {
181                 /* Create an RX queue for each node */
182                 socket_id = rte_socket_id();
183                 q_name = get_rx_queue_name(i);
184                 nodes[i].rx_q = rte_ring_create(q_name,
185                                 ringsize, socket_id,
186                                 RING_F_SP_ENQ | RING_F_SC_DEQ);
187                 if (nodes[i].rx_q == NULL)
188                         rte_exit(EXIT_FAILURE, "Cannot create rx ring queue "
189                                         "for node %u\n", i);
190         }
191         return 0;
192 }
193
194 /*
195  * Create EFD table which will contain all the flows
196  * that will be distributed among the nodes
197  */
198 static void
199 create_efd_table(void)
200 {
201         uint8_t socket_id = rte_socket_id();
202
203         /* create table */
204         efd_table = rte_efd_create("flow table", num_flows * 2, sizeof(uint32_t),
205                         1 << socket_id, socket_id);
206
207         if (efd_table == NULL)
208                 rte_exit(EXIT_FAILURE, "Problem creating the flow table\n");
209 }
210
211 static void
212 populate_efd_table(void)
213 {
214         unsigned int i;
215         int32_t ret;
216         uint32_t ip_dst;
217         uint8_t socket_id = rte_socket_id();
218         uint64_t node_id;
219
220         /* Add flows in table */
221         for (i = 0; i < num_flows; i++) {
222                 node_id = i % num_nodes;
223
224                 ip_dst = rte_cpu_to_be_32(i);
225                 ret = rte_efd_update(efd_table, socket_id,
226                                 (void *)&ip_dst, (efd_value_t)node_id);
227                 if (ret < 0)
228                         rte_exit(EXIT_FAILURE, "Unable to add entry %u in "
229                                         "EFD table\n", i);
230         }
231
232         printf("EFD table: Adding 0x%x keys\n", num_flows);
233 }
234
235 /* Check the link status of all ports in up to 9s, and print them finally */
236 static void
237 check_all_ports_link_status(uint16_t port_num, uint32_t port_mask)
238 {
239 #define CHECK_INTERVAL 100 /* 100ms */
240 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
241         uint8_t count, all_ports_up, print_flag = 0;
242         uint16_t portid;
243         struct rte_eth_link link;
244
245         printf("\nChecking link status");
246         fflush(stdout);
247         for (count = 0; count <= MAX_CHECK_TIME; count++) {
248                 all_ports_up = 1;
249                 for (portid = 0; portid < port_num; portid++) {
250                         if ((port_mask & (1 << info->id[portid])) == 0)
251                                 continue;
252                         memset(&link, 0, sizeof(link));
253                         rte_eth_link_get_nowait(info->id[portid], &link);
254                         /* print link status if flag set */
255                         if (print_flag == 1) {
256                                 if (link.link_status)
257                                         printf(
258                                         "Port%d Link Up. Speed %u Mbps - %s\n",
259                                                 info->id[portid],
260                                                 link.link_speed,
261                                 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
262                                         ("full-duplex") : ("half-duplex\n"));
263                                 else
264                                         printf("Port %d Link Down\n",
265                                                 info->id[portid]);
266                                 continue;
267                         }
268                         /* clear all_ports_up flag if any link down */
269                         if (link.link_status == ETH_LINK_DOWN) {
270                                 all_ports_up = 0;
271                                 break;
272                         }
273                 }
274                 /* after finally printing all link status, get out */
275                 if (print_flag == 1)
276                         break;
277
278                 if (all_ports_up == 0) {
279                         printf(".");
280                         fflush(stdout);
281                         rte_delay_ms(CHECK_INTERVAL);
282                 }
283
284                 /* set the print_flag if all ports up or timeout */
285                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
286                         print_flag = 1;
287                         printf("done\n");
288                 }
289         }
290 }
291
292 /**
293  * Main init function for the multi-process server app,
294  * calls subfunctions to do each stage of the initialisation.
295  */
296 int
297 init(int argc, char *argv[])
298 {
299         int retval;
300         const struct rte_memzone *mz;
301         uint8_t i, total_ports;
302
303         /* init EAL, parsing EAL args */
304         retval = rte_eal_init(argc, argv);
305         if (retval < 0)
306                 return -1;
307         argc -= retval;
308         argv += retval;
309
310         /* get total number of ports */
311         total_ports = rte_eth_dev_count_avail();
312
313         /* set up array for port data */
314         mz = rte_memzone_reserve(MZ_SHARED_INFO, sizeof(*info),
315                                 rte_socket_id(), NO_FLAGS);
316         if (mz == NULL)
317                 rte_exit(EXIT_FAILURE, "Cannot reserve memory zone "
318                                 "for port information\n");
319         memset(mz->addr, 0, sizeof(*info));
320         info = mz->addr;
321
322         /* parse additional, application arguments */
323         retval = parse_app_args(total_ports, argc, argv);
324         if (retval != 0)
325                 return -1;
326
327         /* initialise mbuf pools */
328         retval = init_mbuf_pools();
329         if (retval != 0)
330                 rte_exit(EXIT_FAILURE, "Cannot create needed mbuf pools\n");
331
332         /* now initialise the ports we will use */
333         for (i = 0; i < info->num_ports; i++) {
334                 retval = init_port(info->id[i]);
335                 if (retval != 0)
336                         rte_exit(EXIT_FAILURE, "Cannot initialise port %u\n",
337                                         (unsigned int) i);
338         }
339
340         check_all_ports_link_status(info->num_ports, (~0x0));
341
342         /* initialise the node queues/rings for inter-eu comms */
343         init_shm_rings();
344
345         /* Create the EFD table */
346         create_efd_table();
347
348         /* Populate the EFD table */
349         populate_efd_table();
350
351         /* Share the total number of nodes */
352         info->num_nodes = num_nodes;
353
354         /* Share the total number of flows */
355         info->num_flows = num_flows;
356         return 0;
357 }