net/ice: fix memory leak when releasing VSI
[dpdk.git] / examples / l2fwd-event / l2fwd_event_generic.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2019 Marvell International Ltd.
3  */
4
5 #include <stdbool.h>
6 #include <getopt.h>
7
8 #include <rte_cycles.h>
9 #include <rte_ethdev.h>
10 #include <rte_eventdev.h>
11 #include <rte_event_eth_rx_adapter.h>
12 #include <rte_event_eth_tx_adapter.h>
13 #include <rte_lcore.h>
14 #include <rte_spinlock.h>
15
16 #include "l2fwd_common.h"
17 #include "l2fwd_event.h"
18
19 static uint32_t
20 l2fwd_event_device_setup_generic(struct l2fwd_resources *rsrc)
21 {
22         struct l2fwd_event_resources *evt_rsrc = rsrc->evt_rsrc;
23         struct rte_event_dev_config event_d_conf = {
24                 .nb_events_limit  = 4096,
25                 .nb_event_queue_flows = 1024,
26                 .nb_event_port_dequeue_depth = 128,
27                 .nb_event_port_enqueue_depth = 128
28         };
29         struct rte_event_dev_info dev_info;
30         const uint8_t event_d_id = 0; /* Always use first event device only */
31         uint32_t event_queue_cfg = 0;
32         uint16_t ethdev_count = 0;
33         uint16_t num_workers = 0;
34         uint16_t port_id;
35         int ret;
36
37         RTE_ETH_FOREACH_DEV(port_id) {
38                 if ((rsrc->enabled_port_mask & (1 << port_id)) == 0)
39                         continue;
40                 ethdev_count++;
41         }
42
43         /* Event device configurtion */
44         rte_event_dev_info_get(event_d_id, &dev_info);
45
46         /* Enable implicit release */
47         if (dev_info.event_dev_cap & RTE_EVENT_DEV_CAP_IMPLICIT_RELEASE_DISABLE)
48                 evt_rsrc->disable_implicit_release = 0;
49
50         if (dev_info.event_dev_cap & RTE_EVENT_DEV_CAP_QUEUE_ALL_TYPES)
51                 event_queue_cfg |= RTE_EVENT_QUEUE_CFG_ALL_TYPES;
52
53         /* One queue for each ethdev port + one Tx adapter Single link queue. */
54         event_d_conf.nb_event_queues = ethdev_count + 1;
55         if (dev_info.max_event_queues < event_d_conf.nb_event_queues)
56                 event_d_conf.nb_event_queues = dev_info.max_event_queues;
57
58         if (dev_info.max_num_events < event_d_conf.nb_events_limit)
59                 event_d_conf.nb_events_limit = dev_info.max_num_events;
60
61         if (dev_info.max_event_queue_flows < event_d_conf.nb_event_queue_flows)
62                 event_d_conf.nb_event_queue_flows =
63                                                 dev_info.max_event_queue_flows;
64
65         if (dev_info.max_event_port_dequeue_depth <
66                                 event_d_conf.nb_event_port_dequeue_depth)
67                 event_d_conf.nb_event_port_dequeue_depth =
68                                 dev_info.max_event_port_dequeue_depth;
69
70         if (dev_info.max_event_port_enqueue_depth <
71                                 event_d_conf.nb_event_port_enqueue_depth)
72                 event_d_conf.nb_event_port_enqueue_depth =
73                                 dev_info.max_event_port_enqueue_depth;
74
75         /* Ignore Master core and service cores. */
76         num_workers = rte_lcore_count() - 1 - rte_service_lcore_count();
77         if (dev_info.max_event_ports < num_workers)
78                 num_workers = dev_info.max_event_ports;
79
80         event_d_conf.nb_event_ports = num_workers;
81         evt_rsrc->evp.nb_ports = num_workers;
82         evt_rsrc->evq.nb_queues = event_d_conf.nb_event_queues;
83
84         evt_rsrc->has_burst = !!(dev_info.event_dev_cap &
85                                     RTE_EVENT_DEV_CAP_BURST_MODE);
86
87         ret = rte_event_dev_configure(event_d_id, &event_d_conf);
88         if (ret < 0)
89                 rte_panic("Error in configuring event device\n");
90
91         evt_rsrc->event_d_id = event_d_id;
92         return event_queue_cfg;
93 }
94
95 static void
96 l2fwd_event_port_setup_generic(struct l2fwd_resources *rsrc)
97 {
98         struct l2fwd_event_resources *evt_rsrc = rsrc->evt_rsrc;
99         uint8_t event_d_id = evt_rsrc->event_d_id;
100         struct rte_event_port_conf event_p_conf = {
101                 .dequeue_depth = 32,
102                 .enqueue_depth = 32,
103                 .new_event_threshold = 4096
104         };
105         struct rte_event_port_conf def_p_conf;
106         uint8_t event_p_id;
107         int32_t ret;
108
109         evt_rsrc->evp.event_p_id = (uint8_t *)malloc(sizeof(uint8_t) *
110                                         evt_rsrc->evp.nb_ports);
111         if (!evt_rsrc->evp.event_p_id)
112                 rte_panic("No space is available\n");
113
114         memset(&def_p_conf, 0, sizeof(struct rte_event_port_conf));
115         ret = rte_event_port_default_conf_get(event_d_id, 0, &def_p_conf);
116         if (ret < 0)
117                 rte_panic("Error to get default configuration of event port\n");
118
119         if (def_p_conf.new_event_threshold < event_p_conf.new_event_threshold)
120                 event_p_conf.new_event_threshold =
121                         def_p_conf.new_event_threshold;
122
123         if (def_p_conf.dequeue_depth < event_p_conf.dequeue_depth)
124                 event_p_conf.dequeue_depth = def_p_conf.dequeue_depth;
125
126         if (def_p_conf.enqueue_depth < event_p_conf.enqueue_depth)
127                 event_p_conf.enqueue_depth = def_p_conf.enqueue_depth;
128
129         event_p_conf.disable_implicit_release =
130                 evt_rsrc->disable_implicit_release;
131         evt_rsrc->deq_depth = def_p_conf.dequeue_depth;
132
133         for (event_p_id = 0; event_p_id < evt_rsrc->evp.nb_ports;
134                                                                 event_p_id++) {
135                 ret = rte_event_port_setup(event_d_id, event_p_id,
136                                            &event_p_conf);
137                 if (ret < 0)
138                         rte_panic("Error in configuring event port %d\n",
139                                   event_p_id);
140
141                 ret = rte_event_port_link(event_d_id, event_p_id,
142                                           evt_rsrc->evq.event_q_id,
143                                           NULL,
144                                           evt_rsrc->evq.nb_queues - 1);
145                 if (ret != (evt_rsrc->evq.nb_queues - 1))
146                         rte_panic("Error in linking event port %d to queues\n",
147                                   event_p_id);
148                 evt_rsrc->evp.event_p_id[event_p_id] = event_p_id;
149         }
150         /* init spinlock */
151         rte_spinlock_init(&evt_rsrc->evp.lock);
152
153         evt_rsrc->def_p_conf = event_p_conf;
154 }
155
156 static void
157 l2fwd_event_queue_setup_generic(struct l2fwd_resources *rsrc,
158                           uint32_t event_queue_cfg)
159 {
160         struct l2fwd_event_resources *evt_rsrc = rsrc->evt_rsrc;
161         uint8_t event_d_id = evt_rsrc->event_d_id;
162         struct rte_event_queue_conf event_q_conf = {
163                 .nb_atomic_flows = 1024,
164                 .nb_atomic_order_sequences = 1024,
165                 .event_queue_cfg = event_queue_cfg,
166                 .priority = RTE_EVENT_DEV_PRIORITY_NORMAL
167         };
168         struct rte_event_queue_conf def_q_conf;
169         uint8_t event_q_id;
170         int32_t ret;
171
172         event_q_conf.schedule_type = rsrc->sched_type;
173         evt_rsrc->evq.event_q_id = (uint8_t *)malloc(sizeof(uint8_t) *
174                                         evt_rsrc->evq.nb_queues);
175         if (!evt_rsrc->evq.event_q_id)
176                 rte_panic("Memory allocation failure\n");
177
178         ret = rte_event_queue_default_conf_get(event_d_id, 0, &def_q_conf);
179         if (ret < 0)
180                 rte_panic("Error to get default config of event queue\n");
181
182         if (def_q_conf.nb_atomic_flows < event_q_conf.nb_atomic_flows)
183                 event_q_conf.nb_atomic_flows = def_q_conf.nb_atomic_flows;
184
185         for (event_q_id = 0; event_q_id < (evt_rsrc->evq.nb_queues - 1);
186                                                                 event_q_id++) {
187                 ret = rte_event_queue_setup(event_d_id, event_q_id,
188                                             &event_q_conf);
189                 if (ret < 0)
190                         rte_panic("Error in configuring event queue\n");
191                 evt_rsrc->evq.event_q_id[event_q_id] = event_q_id;
192         }
193
194         event_q_conf.event_queue_cfg |= RTE_EVENT_QUEUE_CFG_SINGLE_LINK;
195         event_q_conf.priority = RTE_EVENT_DEV_PRIORITY_HIGHEST,
196         ret = rte_event_queue_setup(event_d_id, event_q_id, &event_q_conf);
197         if (ret < 0)
198                 rte_panic("Error in configuring event queue for Tx adapter\n");
199         evt_rsrc->evq.event_q_id[event_q_id] = event_q_id;
200 }
201
202 static void
203 l2fwd_rx_tx_adapter_setup_generic(struct l2fwd_resources *rsrc)
204 {
205         struct l2fwd_event_resources *evt_rsrc = rsrc->evt_rsrc;
206         struct rte_event_eth_rx_adapter_queue_conf eth_q_conf;
207         uint8_t event_d_id = evt_rsrc->event_d_id;
208         uint8_t rx_adptr_id = 0;
209         uint8_t tx_adptr_id = 0;
210         uint8_t tx_port_id = 0;
211         uint16_t port_id;
212         uint32_t service_id;
213         int32_t ret, i = 0;
214
215         memset(&eth_q_conf, 0, sizeof(eth_q_conf));
216         eth_q_conf.ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL;
217
218         /* Rx adapter setup */
219         evt_rsrc->rx_adptr.nb_rx_adptr = 1;
220         evt_rsrc->rx_adptr.rx_adptr = (uint8_t *)malloc(sizeof(uint8_t) *
221                                         evt_rsrc->rx_adptr.nb_rx_adptr);
222         if (!evt_rsrc->rx_adptr.rx_adptr) {
223                 free(evt_rsrc->evp.event_p_id);
224                 free(evt_rsrc->evq.event_q_id);
225                 rte_panic("Failed to allocate memery for Rx adapter\n");
226         }
227
228         ret = rte_event_eth_rx_adapter_create(rx_adptr_id, event_d_id,
229                                               &evt_rsrc->def_p_conf);
230         if (ret)
231                 rte_panic("Failed to create rx adapter\n");
232
233         /* Configure user requested sched type */
234         eth_q_conf.ev.sched_type = rsrc->sched_type;
235         RTE_ETH_FOREACH_DEV(port_id) {
236                 if ((rsrc->enabled_port_mask & (1 << port_id)) == 0)
237                         continue;
238                 eth_q_conf.ev.queue_id = evt_rsrc->evq.event_q_id[i];
239                 ret = rte_event_eth_rx_adapter_queue_add(rx_adptr_id, port_id,
240                                                          -1, &eth_q_conf);
241                 if (ret)
242                         rte_panic("Failed to add queues to Rx adapter\n");
243                 if (i < evt_rsrc->evq.nb_queues)
244                         i++;
245         }
246
247         ret = rte_event_eth_rx_adapter_service_id_get(rx_adptr_id, &service_id);
248         if (ret != -ESRCH && ret != 0)
249                 rte_panic("Error getting the service ID for rx adptr\n");
250
251         rte_service_runstate_set(service_id, 1);
252         rte_service_set_runstate_mapped_check(service_id, 0);
253         evt_rsrc->rx_adptr.service_id = service_id;
254
255         ret = rte_event_eth_rx_adapter_start(rx_adptr_id);
256         if (ret)
257                 rte_panic("Rx adapter[%d] start Failed\n", rx_adptr_id);
258
259         evt_rsrc->rx_adptr.rx_adptr[0] = rx_adptr_id;
260
261         /* Tx adapter setup */
262         evt_rsrc->tx_adptr.nb_tx_adptr = 1;
263         evt_rsrc->tx_adptr.tx_adptr = (uint8_t *)malloc(sizeof(uint8_t) *
264                                         evt_rsrc->tx_adptr.nb_tx_adptr);
265         if (!evt_rsrc->tx_adptr.tx_adptr) {
266                 free(evt_rsrc->rx_adptr.rx_adptr);
267                 free(evt_rsrc->evp.event_p_id);
268                 free(evt_rsrc->evq.event_q_id);
269                 rte_panic("Failed to allocate memery for Rx adapter\n");
270         }
271
272         ret = rte_event_eth_tx_adapter_create(tx_adptr_id, event_d_id,
273                                               &evt_rsrc->def_p_conf);
274         if (ret)
275                 rte_panic("Failed to create tx adapter\n");
276
277         RTE_ETH_FOREACH_DEV(port_id) {
278                 if ((rsrc->enabled_port_mask & (1 << port_id)) == 0)
279                         continue;
280                 ret = rte_event_eth_tx_adapter_queue_add(tx_adptr_id, port_id,
281                                                          -1);
282                 if (ret)
283                         rte_panic("Failed to add queues to Tx adapter\n");
284         }
285
286         ret = rte_event_eth_tx_adapter_service_id_get(tx_adptr_id, &service_id);
287         if (ret != -ESRCH && ret != 0)
288                 rte_panic("Failed to get Tx adapter service ID\n");
289
290         rte_service_runstate_set(service_id, 1);
291         rte_service_set_runstate_mapped_check(service_id, 0);
292         evt_rsrc->tx_adptr.service_id = service_id;
293
294         ret = rte_event_eth_tx_adapter_event_port_get(tx_adptr_id, &tx_port_id);
295         if (ret)
296                 rte_panic("Failed to get Tx adapter port id: %d\n", ret);
297
298         ret = rte_event_port_link(event_d_id, tx_port_id,
299                                   &evt_rsrc->evq.event_q_id[
300                                         evt_rsrc->evq.nb_queues - 1],
301                                   NULL, 1);
302         if (ret != 1)
303                 rte_panic("Unable to link Tx adapter port to Tx queue:err=%d\n",
304                          ret);
305
306         ret = rte_event_eth_tx_adapter_start(tx_adptr_id);
307         if (ret)
308                 rte_panic("Tx adapter[%d] start Failed\n", tx_adptr_id);
309
310         evt_rsrc->tx_adptr.tx_adptr[0] = tx_adptr_id;
311 }
312
313 void
314 l2fwd_event_set_generic_ops(struct event_setup_ops *ops)
315 {
316         ops->event_device_setup = l2fwd_event_device_setup_generic;
317         ops->event_queue_setup = l2fwd_event_queue_setup_generic;
318         ops->event_port_setup = l2fwd_event_port_setup_generic;
319         ops->adapter_setup = l2fwd_rx_tx_adapter_setup_generic;
320 }