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