net/softnic: add software queue object
[dpdk.git] / drivers / net / softnic / rte_eth_softnic.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 Intel Corporation
3  */
4
5 #include <stdint.h>
6 #include <stdlib.h>
7 #include <string.h>
8
9 #include <rte_ethdev_driver.h>
10 #include <rte_ethdev_vdev.h>
11 #include <rte_malloc.h>
12 #include <rte_bus_vdev.h>
13 #include <rte_kvargs.h>
14 #include <rte_errno.h>
15 #include <rte_ring.h>
16 #include <rte_tm_driver.h>
17
18 #include "rte_eth_softnic.h"
19 #include "rte_eth_softnic_internals.h"
20
21 #define PMD_PARAM_FIRMWARE                                 "firmware"
22 #define PMD_PARAM_CPU_ID                                   "cpu_id"
23
24 static const char *pmd_valid_args[] = {
25         PMD_PARAM_FIRMWARE,
26         PMD_PARAM_CPU_ID,
27         NULL
28 };
29
30 static const struct rte_eth_dev_info pmd_dev_info = {
31         .min_rx_bufsize = 0,
32         .max_rx_pktlen = UINT32_MAX,
33         .max_rx_queues = UINT16_MAX,
34         .max_tx_queues = UINT16_MAX,
35         .rx_desc_lim = {
36                 .nb_max = UINT16_MAX,
37                 .nb_min = 0,
38                 .nb_align = 1,
39         },
40         .tx_desc_lim = {
41                 .nb_max = UINT16_MAX,
42                 .nb_min = 0,
43                 .nb_align = 1,
44         },
45         .rx_offload_capa = DEV_RX_OFFLOAD_CRC_STRIP,
46 };
47
48 static int pmd_softnic_logtype;
49
50 #define PMD_LOG(level, fmt, args...) \
51         rte_log(RTE_LOG_ ## level, pmd_softnic_logtype, \
52                 "%s(): " fmt "\n", __func__, ##args)
53
54 static void
55 pmd_dev_infos_get(struct rte_eth_dev *dev __rte_unused,
56         struct rte_eth_dev_info *dev_info)
57 {
58         memcpy(dev_info, &pmd_dev_info, sizeof(*dev_info));
59 }
60
61 static int
62 pmd_dev_configure(struct rte_eth_dev *dev __rte_unused)
63 {
64         return 0;
65 }
66
67 static int
68 pmd_rx_queue_setup(struct rte_eth_dev *dev,
69         uint16_t rx_queue_id,
70         uint16_t nb_rx_desc,
71         unsigned int socket_id,
72         const struct rte_eth_rxconf *rx_conf __rte_unused,
73         struct rte_mempool *mb_pool __rte_unused)
74 {
75         uint32_t size = RTE_ETH_NAME_MAX_LEN + strlen("_rxq") + 4;
76         char name[size];
77         struct rte_ring *r;
78
79         snprintf(name, sizeof(name), "%s_rxq%04x",
80                 dev->data->name,
81                 rx_queue_id);
82
83         r = rte_ring_create(name,
84                 nb_rx_desc,
85                 socket_id,
86                 RING_F_SP_ENQ | RING_F_SC_DEQ);
87         if (r == NULL)
88                 return -1;
89
90         dev->data->rx_queues[rx_queue_id] = r;
91         return 0;
92 }
93
94 static int
95 pmd_tx_queue_setup(struct rte_eth_dev *dev,
96         uint16_t tx_queue_id,
97         uint16_t nb_tx_desc,
98         unsigned int socket_id,
99         const struct rte_eth_txconf *tx_conf __rte_unused)
100 {
101         uint32_t size = RTE_ETH_NAME_MAX_LEN + strlen("_txq") + 4;
102         char name[size];
103         struct rte_ring *r;
104
105         snprintf(name, sizeof(name), "%s_txq%04x",
106                 dev->data->name,
107                 tx_queue_id);
108
109         r = rte_ring_create(name,
110                 nb_tx_desc,
111                 socket_id,
112                 RING_F_SP_ENQ | RING_F_SC_DEQ);
113         if (r == NULL)
114                 return -1;
115
116         dev->data->tx_queues[tx_queue_id] = r;
117         return 0;
118 }
119
120 static int
121 pmd_dev_start(struct rte_eth_dev *dev)
122 {
123         dev->data->dev_link.link_status = ETH_LINK_UP;
124
125         return 0;
126 }
127
128 static void
129 pmd_dev_stop(struct rte_eth_dev *dev)
130 {
131         dev->data->dev_link.link_status = ETH_LINK_DOWN;
132 }
133
134 static void
135 pmd_dev_close(struct rte_eth_dev *dev)
136 {
137         uint32_t i;
138
139         /* RX queues */
140         for (i = 0; i < dev->data->nb_rx_queues; i++)
141                 rte_ring_free((struct rte_ring *)dev->data->rx_queues[i]);
142
143         /* TX queues */
144         for (i = 0; i < dev->data->nb_tx_queues; i++)
145                 rte_ring_free((struct rte_ring *)dev->data->tx_queues[i]);
146 }
147
148 static int
149 pmd_link_update(struct rte_eth_dev *dev __rte_unused,
150         int wait_to_complete __rte_unused)
151 {
152         return 0;
153 }
154
155 static int
156 pmd_tm_ops_get(struct rte_eth_dev *dev __rte_unused, void *arg)
157 {
158         *(const struct rte_tm_ops **)arg = NULL;
159
160         return 0;
161 }
162
163 static const struct eth_dev_ops pmd_ops = {
164         .dev_configure = pmd_dev_configure,
165         .dev_start = pmd_dev_start,
166         .dev_stop = pmd_dev_stop,
167         .dev_close = pmd_dev_close,
168         .link_update = pmd_link_update,
169         .dev_infos_get = pmd_dev_infos_get,
170         .rx_queue_setup = pmd_rx_queue_setup,
171         .tx_queue_setup = pmd_tx_queue_setup,
172         .tm_ops_get = pmd_tm_ops_get,
173 };
174
175 static uint16_t
176 pmd_rx_pkt_burst(void *rxq,
177         struct rte_mbuf **rx_pkts,
178         uint16_t nb_pkts)
179 {
180         return (uint16_t)rte_ring_sc_dequeue_burst(rxq,
181                 (void **)rx_pkts,
182                 nb_pkts,
183                 NULL);
184 }
185
186 static uint16_t
187 pmd_tx_pkt_burst(void *txq,
188         struct rte_mbuf **tx_pkts,
189         uint16_t nb_pkts)
190 {
191         return (uint16_t)rte_ring_sp_enqueue_burst(txq,
192                 (void **)tx_pkts,
193                 nb_pkts,
194                 NULL);
195 }
196
197 int
198 rte_pmd_softnic_run(uint16_t port_id __rte_unused)
199 {
200         return 0;
201 }
202
203 static void *
204 pmd_init(struct pmd_params *params)
205 {
206         struct pmd_internals *p;
207
208         p = rte_zmalloc_socket(params->name,
209                 sizeof(struct pmd_internals),
210                 0,
211                 params->cpu_id);
212         if (p == NULL)
213                 return NULL;
214
215         /* Params */
216         memcpy(&p->params, params, sizeof(p->params));
217
218         /* Resources */
219         softnic_swq_init(p);
220         return p;
221 }
222
223 static void
224 pmd_free(struct pmd_internals *p)
225 {
226         if (p == NULL)
227                 return;
228
229         softnic_swq_free(p);
230         rte_free(p);
231 }
232
233 static struct ether_addr eth_addr = {
234         .addr_bytes = {0},
235 };
236
237 static int
238 pmd_ethdev_register(struct rte_vdev_device *vdev,
239         struct pmd_params *params,
240         void *dev_private)
241 {
242         struct rte_eth_dev *dev;
243
244         /* Ethdev entry allocation */
245         dev = rte_eth_dev_allocate(params->name);
246         if (!dev)
247                 return -ENOMEM;
248
249         /* dev */
250         dev->rx_pkt_burst = pmd_rx_pkt_burst;
251         dev->tx_pkt_burst = pmd_tx_pkt_burst;
252         dev->tx_pkt_prepare = NULL;
253         dev->dev_ops = &pmd_ops;
254         dev->device = &vdev->device;
255
256         /* dev->data */
257         dev->data->dev_private = dev_private;
258         dev->data->dev_link.link_speed = ETH_SPEED_NUM_100G;
259         dev->data->dev_link.link_duplex = ETH_LINK_FULL_DUPLEX;
260         dev->data->dev_link.link_autoneg = ETH_LINK_FIXED;
261         dev->data->dev_link.link_status = ETH_LINK_DOWN;
262         dev->data->mac_addrs = &eth_addr;
263         dev->data->promiscuous = 1;
264         dev->data->kdrv = RTE_KDRV_NONE;
265         dev->data->numa_node = params->cpu_id;
266
267         rte_eth_dev_probing_finish(dev);
268
269         return 0;
270 }
271
272 static int
273 get_string(const char *key __rte_unused, const char *value, void *extra_args)
274 {
275         if (!value || !extra_args)
276                 return -EINVAL;
277
278         *(char **)extra_args = strdup(value);
279
280         if (!*(char **)extra_args)
281                 return -ENOMEM;
282
283         return 0;
284 }
285
286 static int
287 get_uint32(const char *key __rte_unused, const char *value, void *extra_args)
288 {
289         if (!value || !extra_args)
290                 return -EINVAL;
291
292         *(uint32_t *)extra_args = strtoull(value, NULL, 0);
293
294         return 0;
295 }
296
297 static int
298 pmd_parse_args(struct pmd_params *p, const char *params)
299 {
300         struct rte_kvargs *kvlist;
301         int ret = 0;
302
303         kvlist = rte_kvargs_parse(params, pmd_valid_args);
304         if (kvlist == NULL)
305                 return -EINVAL;
306
307         /* Set default values */
308         memset(p, 0, sizeof(*p));
309         p->firmware = SOFTNIC_FIRMWARE;
310         p->cpu_id = SOFTNIC_CPU_ID;
311
312         /* Firmware script (optional) */
313         if (rte_kvargs_count(kvlist, PMD_PARAM_FIRMWARE) == 1) {
314                 ret = rte_kvargs_process(kvlist, PMD_PARAM_FIRMWARE,
315                         &get_string, &p->firmware);
316                 if (ret < 0)
317                         goto out_free;
318         }
319
320         /* CPU ID (optional) */
321         if (rte_kvargs_count(kvlist, PMD_PARAM_CPU_ID) == 1) {
322                 ret = rte_kvargs_process(kvlist, PMD_PARAM_CPU_ID,
323                         &get_uint32, &p->cpu_id);
324                 if (ret < 0)
325                         goto out_free;
326         }
327
328 out_free:
329         rte_kvargs_free(kvlist);
330         return ret;
331 }
332
333 static int
334 pmd_probe(struct rte_vdev_device *vdev)
335 {
336         struct pmd_params p;
337         const char *params;
338         int status = 0;
339
340         void *dev_private;
341         const char *name = rte_vdev_device_name(vdev);
342
343         PMD_LOG(INFO, "Probing device \"%s\"", name);
344
345         /* Parse input arguments */
346         params = rte_vdev_device_args(vdev);
347         if (!params)
348                 return -EINVAL;
349
350         status = pmd_parse_args(&p, params);
351         if (status)
352                 return status;
353
354         p.name = name;
355
356         /* Allocate and initialize soft ethdev private data */
357         dev_private = pmd_init(&p);
358         if (dev_private == NULL)
359                 return -ENOMEM;
360
361         /* Register soft ethdev */
362         PMD_LOG(INFO, "Creating soft ethdev \"%s\"", p.name);
363
364         status = pmd_ethdev_register(vdev, &p, dev_private);
365         if (status) {
366                 pmd_free(dev_private);
367                 return status;
368         }
369
370         return 0;
371 }
372
373 static int
374 pmd_remove(struct rte_vdev_device *vdev)
375 {
376         struct rte_eth_dev *dev = NULL;
377         struct pmd_internals *p;
378
379         if (!vdev)
380                 return -EINVAL;
381
382         PMD_LOG(INFO, "Removing device \"%s\"", rte_vdev_device_name(vdev));
383
384         /* Find the ethdev entry */
385         dev = rte_eth_dev_allocated(rte_vdev_device_name(vdev));
386         if (dev == NULL)
387                 return -ENODEV;
388         p = dev->data->dev_private;
389
390         /* Free device data structures*/
391         rte_free(dev->data);
392         rte_eth_dev_release_port(dev);
393         pmd_free(p);
394
395         return 0;
396 }
397
398 static struct rte_vdev_driver pmd_softnic_drv = {
399         .probe = pmd_probe,
400         .remove = pmd_remove,
401 };
402
403 RTE_PMD_REGISTER_VDEV(net_softnic, pmd_softnic_drv);
404 RTE_PMD_REGISTER_PARAM_STRING(net_softnic,
405         PMD_PARAM_FIRMWARE "=<string> "
406         PMD_PARAM_CPU_ID "=<uint32>");
407
408 RTE_INIT(pmd_softnic_init_log)
409 {
410         pmd_softnic_logtype = rte_log_register("pmd.net.softnic");
411         if (pmd_softnic_logtype >= 0)
412                 rte_log_set_level(pmd_softnic_logtype, RTE_LOG_NOTICE);
413 }