f68d2eb604595968ae18e792a219a1d7d954e5fd
[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 #define PMD_PARAM_SCRIPT                                   "script"
24 #define PMD_PARAM_CONN_PORT                                "conn_port"
25 #define PMD_PARAM_CPU_ID                                   "cpu_id"
26 #define PMD_PARAM_TM_N_QUEUES                              "tm_n_queues"
27 #define PMD_PARAM_TM_QSIZE0                                "tm_qsize0"
28 #define PMD_PARAM_TM_QSIZE1                                "tm_qsize1"
29 #define PMD_PARAM_TM_QSIZE2                                "tm_qsize2"
30 #define PMD_PARAM_TM_QSIZE3                                "tm_qsize3"
31
32 static const char *pmd_valid_args[] = {
33         PMD_PARAM_FIRMWARE,
34         PMD_PARAM_CPU_ID,
35         PMD_PARAM_TM_N_QUEUES,
36         PMD_PARAM_TM_QSIZE0,
37         PMD_PARAM_TM_QSIZE1,
38         PMD_PARAM_TM_QSIZE2,
39         PMD_PARAM_TM_QSIZE3,
40         NULL
41 };
42
43 static const struct rte_eth_dev_info pmd_dev_info = {
44         .min_rx_bufsize = 0,
45         .max_rx_pktlen = UINT32_MAX,
46         .max_rx_queues = UINT16_MAX,
47         .max_tx_queues = UINT16_MAX,
48         .rx_desc_lim = {
49                 .nb_max = UINT16_MAX,
50                 .nb_min = 0,
51                 .nb_align = 1,
52         },
53         .tx_desc_lim = {
54                 .nb_max = UINT16_MAX,
55                 .nb_min = 0,
56                 .nb_align = 1,
57         },
58         .rx_offload_capa = DEV_RX_OFFLOAD_CRC_STRIP,
59 };
60
61 static int pmd_softnic_logtype;
62
63 #define PMD_LOG(level, fmt, args...) \
64         rte_log(RTE_LOG_ ## level, pmd_softnic_logtype, \
65                 "%s(): " fmt "\n", __func__, ##args)
66
67 static void
68 pmd_dev_infos_get(struct rte_eth_dev *dev __rte_unused,
69         struct rte_eth_dev_info *dev_info)
70 {
71         memcpy(dev_info, &pmd_dev_info, sizeof(*dev_info));
72 }
73
74 static int
75 pmd_dev_configure(struct rte_eth_dev *dev __rte_unused)
76 {
77         return 0;
78 }
79
80 static int
81 pmd_rx_queue_setup(struct rte_eth_dev *dev,
82         uint16_t rx_queue_id,
83         uint16_t nb_rx_desc,
84         unsigned int socket_id,
85         const struct rte_eth_rxconf *rx_conf __rte_unused,
86         struct rte_mempool *mb_pool __rte_unused)
87 {
88         uint32_t size = RTE_ETH_NAME_MAX_LEN + strlen("_rxq") + 4;
89         char name[size];
90         struct rte_ring *r;
91
92         snprintf(name, sizeof(name), "%s_rxq%04x",
93                 dev->data->name,
94                 rx_queue_id);
95
96         r = rte_ring_create(name,
97                 nb_rx_desc,
98                 socket_id,
99                 RING_F_SP_ENQ | RING_F_SC_DEQ);
100         if (r == NULL)
101                 return -1;
102
103         dev->data->rx_queues[rx_queue_id] = r;
104         return 0;
105 }
106
107 static int
108 pmd_tx_queue_setup(struct rte_eth_dev *dev,
109         uint16_t tx_queue_id,
110         uint16_t nb_tx_desc,
111         unsigned int socket_id,
112         const struct rte_eth_txconf *tx_conf __rte_unused)
113 {
114         uint32_t size = RTE_ETH_NAME_MAX_LEN + strlen("_txq") + 4;
115         char name[size];
116         struct rte_ring *r;
117
118         snprintf(name, sizeof(name), "%s_txq%04x",
119                 dev->data->name,
120                 tx_queue_id);
121
122         r = rte_ring_create(name,
123                 nb_tx_desc,
124                 socket_id,
125                 RING_F_SP_ENQ | RING_F_SC_DEQ);
126         if (r == NULL)
127                 return -1;
128
129         dev->data->tx_queues[tx_queue_id] = r;
130         return 0;
131 }
132
133 static int
134 pmd_dev_start(struct rte_eth_dev *dev)
135 {
136         dev->data->dev_link.link_status = ETH_LINK_UP;
137
138         return 0;
139 }
140
141 static void
142 pmd_dev_stop(struct rte_eth_dev *dev)
143 {
144         dev->data->dev_link.link_status = ETH_LINK_DOWN;
145 }
146
147 static void
148 pmd_dev_close(struct rte_eth_dev *dev)
149 {
150         uint32_t i;
151
152         /* RX queues */
153         for (i = 0; i < dev->data->nb_rx_queues; i++)
154                 rte_ring_free((struct rte_ring *)dev->data->rx_queues[i]);
155
156         /* TX queues */
157         for (i = 0; i < dev->data->nb_tx_queues; i++)
158                 rte_ring_free((struct rte_ring *)dev->data->tx_queues[i]);
159 }
160
161 static int
162 pmd_link_update(struct rte_eth_dev *dev __rte_unused,
163         int wait_to_complete __rte_unused)
164 {
165         return 0;
166 }
167
168 static int
169 pmd_tm_ops_get(struct rte_eth_dev *dev __rte_unused, void *arg)
170 {
171         *(const struct rte_tm_ops **)arg = &pmd_tm_ops;
172
173         return 0;
174 }
175
176 static const struct eth_dev_ops pmd_ops = {
177         .dev_configure = pmd_dev_configure,
178         .dev_start = pmd_dev_start,
179         .dev_stop = pmd_dev_stop,
180         .dev_close = pmd_dev_close,
181         .link_update = pmd_link_update,
182         .dev_infos_get = pmd_dev_infos_get,
183         .rx_queue_setup = pmd_rx_queue_setup,
184         .tx_queue_setup = pmd_tx_queue_setup,
185         .tm_ops_get = pmd_tm_ops_get,
186 };
187
188 static uint16_t
189 pmd_rx_pkt_burst(void *rxq,
190         struct rte_mbuf **rx_pkts,
191         uint16_t nb_pkts)
192 {
193         return (uint16_t)rte_ring_sc_dequeue_burst(rxq,
194                 (void **)rx_pkts,
195                 nb_pkts,
196                 NULL);
197 }
198
199 static uint16_t
200 pmd_tx_pkt_burst(void *txq,
201         struct rte_mbuf **tx_pkts,
202         uint16_t nb_pkts)
203 {
204         return (uint16_t)rte_ring_sp_enqueue_burst(txq,
205                 (void **)tx_pkts,
206                 nb_pkts,
207                 NULL);
208 }
209
210 int
211 rte_pmd_softnic_run(uint16_t port_id __rte_unused)
212 {
213         return 0;
214 }
215
216 static void *
217 pmd_init(struct pmd_params *params)
218 {
219         struct pmd_internals *p;
220
221         p = rte_zmalloc_socket(params->name,
222                 sizeof(struct pmd_internals),
223                 0,
224                 params->cpu_id);
225         if (p == NULL)
226                 return NULL;
227
228         /* Params */
229         memcpy(&p->params, params, sizeof(p->params));
230
231         /* Resources */
232         softnic_mempool_init(p);
233         softnic_swq_init(p);
234         softnic_link_init(p);
235         tm_init(p);
236         softnic_tmgr_init(p);
237         softnic_tap_init(p);
238         softnic_port_in_action_profile_init(p);
239         softnic_table_action_profile_init(p);
240         softnic_pipeline_init(p);
241
242         return p;
243 }
244
245 static void
246 pmd_free(struct pmd_internals *p)
247 {
248         if (p == NULL)
249                 return;
250
251         softnic_pipeline_free(p);
252         softnic_table_action_profile_free(p);
253         softnic_port_in_action_profile_free(p);
254         softnic_tap_free(p);
255         softnic_tmgr_free(p);
256         tm_free(p);
257         softnic_link_free(p);
258         softnic_swq_free(p);
259         softnic_mempool_free(p);
260
261         rte_free(p);
262 }
263
264 static struct ether_addr eth_addr = {
265         .addr_bytes = {0},
266 };
267
268 static int
269 pmd_ethdev_register(struct rte_vdev_device *vdev,
270         struct pmd_params *params,
271         void *dev_private)
272 {
273         struct rte_eth_dev *dev;
274
275         /* Ethdev entry allocation */
276         dev = rte_eth_dev_allocate(params->name);
277         if (!dev)
278                 return -ENOMEM;
279
280         /* dev */
281         dev->rx_pkt_burst = pmd_rx_pkt_burst;
282         dev->tx_pkt_burst = pmd_tx_pkt_burst;
283         dev->tx_pkt_prepare = NULL;
284         dev->dev_ops = &pmd_ops;
285         dev->device = &vdev->device;
286
287         /* dev->data */
288         dev->data->dev_private = dev_private;
289         dev->data->dev_link.link_speed = ETH_SPEED_NUM_100G;
290         dev->data->dev_link.link_duplex = ETH_LINK_FULL_DUPLEX;
291         dev->data->dev_link.link_autoneg = ETH_LINK_FIXED;
292         dev->data->dev_link.link_status = ETH_LINK_DOWN;
293         dev->data->mac_addrs = &eth_addr;
294         dev->data->promiscuous = 1;
295         dev->data->kdrv = RTE_KDRV_NONE;
296         dev->data->numa_node = params->cpu_id;
297
298         rte_eth_dev_probing_finish(dev);
299
300         return 0;
301 }
302
303 static int
304 get_string(const char *key __rte_unused, const char *value, void *extra_args)
305 {
306         if (!value || !extra_args)
307                 return -EINVAL;
308
309         *(char **)extra_args = strdup(value);
310
311         if (!*(char **)extra_args)
312                 return -ENOMEM;
313
314         return 0;
315 }
316
317 static int
318 get_uint32(const char *key __rte_unused, const char *value, void *extra_args)
319 {
320         if (!value || !extra_args)
321                 return -EINVAL;
322
323         *(uint32_t *)extra_args = strtoull(value, NULL, 0);
324
325         return 0;
326 }
327
328 static int
329 pmd_parse_args(struct pmd_params *p, const char *params)
330 {
331         struct rte_kvargs *kvlist;
332         int ret = 0;
333
334         kvlist = rte_kvargs_parse(params, pmd_valid_args);
335         if (kvlist == NULL)
336                 return -EINVAL;
337
338         /* Set default values */
339         memset(p, 0, sizeof(*p));
340         p->firmware = SOFTNIC_FIRMWARE;
341         p->cpu_id = SOFTNIC_CPU_ID;
342         p->tm.n_queues = SOFTNIC_TM_N_QUEUES;
343         p->tm.qsize[0] = SOFTNIC_TM_QUEUE_SIZE;
344         p->tm.qsize[1] = SOFTNIC_TM_QUEUE_SIZE;
345         p->tm.qsize[2] = SOFTNIC_TM_QUEUE_SIZE;
346         p->tm.qsize[3] = SOFTNIC_TM_QUEUE_SIZE;
347
348         /* Firmware script (optional) */
349         if (rte_kvargs_count(kvlist, PMD_PARAM_FIRMWARE) == 1) {
350                 ret = rte_kvargs_process(kvlist, PMD_PARAM_FIRMWARE,
351                         &get_string, &p->firmware);
352                 if (ret < 0)
353                         goto out_free;
354         }
355
356         /* CPU ID (optional) */
357         if (rte_kvargs_count(kvlist, PMD_PARAM_CPU_ID) == 1) {
358                 ret = rte_kvargs_process(kvlist, PMD_PARAM_CPU_ID,
359                         &get_uint32, &p->cpu_id);
360                 if (ret < 0)
361                         goto out_free;
362         }
363
364         /* TM number of queues (optional) */
365         if (rte_kvargs_count(kvlist, PMD_PARAM_TM_N_QUEUES) == 1) {
366                 ret = rte_kvargs_process(kvlist, PMD_PARAM_TM_N_QUEUES,
367                         &get_uint32, &p->tm.n_queues);
368                 if (ret < 0)
369                         goto out_free;
370         }
371
372         /* TM queue size 0 .. 3 (optional) */
373         if (rte_kvargs_count(kvlist, PMD_PARAM_TM_QSIZE0) == 1) {
374                 ret = rte_kvargs_process(kvlist, PMD_PARAM_TM_QSIZE0,
375                         &get_uint32, &p->tm.qsize[0]);
376                 if (ret < 0)
377                         goto out_free;
378         }
379
380         if (rte_kvargs_count(kvlist, PMD_PARAM_TM_QSIZE1) == 1) {
381                 ret = rte_kvargs_process(kvlist, PMD_PARAM_TM_QSIZE1,
382                         &get_uint32, &p->tm.qsize[1]);
383                 if (ret < 0)
384                         goto out_free;
385         }
386
387         if (rte_kvargs_count(kvlist, PMD_PARAM_TM_QSIZE2) == 1) {
388                 ret = rte_kvargs_process(kvlist, PMD_PARAM_TM_QSIZE2,
389                         &get_uint32, &p->tm.qsize[2]);
390                 if (ret < 0)
391                         goto out_free;
392         }
393
394         if (rte_kvargs_count(kvlist, PMD_PARAM_TM_QSIZE3) == 1) {
395                 ret = rte_kvargs_process(kvlist, PMD_PARAM_TM_QSIZE3,
396                         &get_uint32, &p->tm.qsize[3]);
397                 if (ret < 0)
398                         goto out_free;
399         }
400
401 out_free:
402         rte_kvargs_free(kvlist);
403         return ret;
404 }
405
406 static int
407 pmd_probe(struct rte_vdev_device *vdev)
408 {
409         struct pmd_params p;
410         const char *params;
411         int status = 0;
412
413         void *dev_private;
414         const char *name = rte_vdev_device_name(vdev);
415
416         PMD_LOG(INFO, "Probing device \"%s\"", name);
417
418         /* Parse input arguments */
419         params = rte_vdev_device_args(vdev);
420         if (!params)
421                 return -EINVAL;
422
423         status = pmd_parse_args(&p, params);
424         if (status)
425                 return status;
426
427         p.name = name;
428
429         /* Allocate and initialize soft ethdev private data */
430         dev_private = pmd_init(&p);
431         if (dev_private == NULL)
432                 return -ENOMEM;
433
434         /* Register soft ethdev */
435         PMD_LOG(INFO, "Creating soft ethdev \"%s\"", p.name);
436
437         status = pmd_ethdev_register(vdev, &p, dev_private);
438         if (status) {
439                 pmd_free(dev_private);
440                 return status;
441         }
442
443         return 0;
444 }
445
446 static int
447 pmd_remove(struct rte_vdev_device *vdev)
448 {
449         struct rte_eth_dev *dev = NULL;
450         struct pmd_internals *p;
451
452         if (!vdev)
453                 return -EINVAL;
454
455         PMD_LOG(INFO, "Removing device \"%s\"", rte_vdev_device_name(vdev));
456
457         /* Find the ethdev entry */
458         dev = rte_eth_dev_allocated(rte_vdev_device_name(vdev));
459         if (dev == NULL)
460                 return -ENODEV;
461         p = dev->data->dev_private;
462
463         /* Free device data structures*/
464         rte_free(dev->data);
465         rte_eth_dev_release_port(dev);
466         pmd_free(p);
467
468         return 0;
469 }
470
471 static struct rte_vdev_driver pmd_softnic_drv = {
472         .probe = pmd_probe,
473         .remove = pmd_remove,
474 };
475
476 RTE_PMD_REGISTER_VDEV(net_softnic, pmd_softnic_drv);
477 RTE_PMD_REGISTER_PARAM_STRING(net_softnic,
478         PMD_PARAM_FIRMWARE "=<string> "
479         PMD_PARAM_CPU_ID "=<uint32> "
480         PMD_PARAM_TM_N_QUEUES "=<uint32> "
481         PMD_PARAM_TM_QSIZE0 "=<uint32> "
482         PMD_PARAM_TM_QSIZE1 "=<uint32> "
483         PMD_PARAM_TM_QSIZE2 "=<uint32> "
484         PMD_PARAM_TM_QSIZE3 "=<uint32>"
485 );
486
487
488 RTE_INIT(pmd_softnic_init_log)
489 {
490         pmd_softnic_logtype = rte_log_register("pmd.net.softnic");
491         if (pmd_softnic_logtype >= 0)
492                 rte_log_set_level(pmd_softnic_logtype, RTE_LOG_NOTICE);
493 }