net/softnic: add traffic manager 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 #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
239         return p;
240 }
241
242 static void
243 pmd_free(struct pmd_internals *p)
244 {
245         if (p == NULL)
246                 return;
247
248         softnic_tap_free(p);
249         softnic_tmgr_free(p);
250         tm_free(p);
251         softnic_link_free(p);
252         softnic_swq_free(p);
253         softnic_mempool_free(p);
254
255         rte_free(p);
256 }
257
258 static struct ether_addr eth_addr = {
259         .addr_bytes = {0},
260 };
261
262 static int
263 pmd_ethdev_register(struct rte_vdev_device *vdev,
264         struct pmd_params *params,
265         void *dev_private)
266 {
267         struct rte_eth_dev *dev;
268
269         /* Ethdev entry allocation */
270         dev = rte_eth_dev_allocate(params->name);
271         if (!dev)
272                 return -ENOMEM;
273
274         /* dev */
275         dev->rx_pkt_burst = pmd_rx_pkt_burst;
276         dev->tx_pkt_burst = pmd_tx_pkt_burst;
277         dev->tx_pkt_prepare = NULL;
278         dev->dev_ops = &pmd_ops;
279         dev->device = &vdev->device;
280
281         /* dev->data */
282         dev->data->dev_private = dev_private;
283         dev->data->dev_link.link_speed = ETH_SPEED_NUM_100G;
284         dev->data->dev_link.link_duplex = ETH_LINK_FULL_DUPLEX;
285         dev->data->dev_link.link_autoneg = ETH_LINK_FIXED;
286         dev->data->dev_link.link_status = ETH_LINK_DOWN;
287         dev->data->mac_addrs = &eth_addr;
288         dev->data->promiscuous = 1;
289         dev->data->kdrv = RTE_KDRV_NONE;
290         dev->data->numa_node = params->cpu_id;
291
292         rte_eth_dev_probing_finish(dev);
293
294         return 0;
295 }
296
297 static int
298 get_string(const char *key __rte_unused, const char *value, void *extra_args)
299 {
300         if (!value || !extra_args)
301                 return -EINVAL;
302
303         *(char **)extra_args = strdup(value);
304
305         if (!*(char **)extra_args)
306                 return -ENOMEM;
307
308         return 0;
309 }
310
311 static int
312 get_uint32(const char *key __rte_unused, const char *value, void *extra_args)
313 {
314         if (!value || !extra_args)
315                 return -EINVAL;
316
317         *(uint32_t *)extra_args = strtoull(value, NULL, 0);
318
319         return 0;
320 }
321
322 static int
323 pmd_parse_args(struct pmd_params *p, const char *params)
324 {
325         struct rte_kvargs *kvlist;
326         int ret = 0;
327
328         kvlist = rte_kvargs_parse(params, pmd_valid_args);
329         if (kvlist == NULL)
330                 return -EINVAL;
331
332         /* Set default values */
333         memset(p, 0, sizeof(*p));
334         p->firmware = SOFTNIC_FIRMWARE;
335         p->cpu_id = SOFTNIC_CPU_ID;
336         p->tm.n_queues = SOFTNIC_TM_N_QUEUES;
337         p->tm.qsize[0] = SOFTNIC_TM_QUEUE_SIZE;
338         p->tm.qsize[1] = SOFTNIC_TM_QUEUE_SIZE;
339         p->tm.qsize[2] = SOFTNIC_TM_QUEUE_SIZE;
340         p->tm.qsize[3] = SOFTNIC_TM_QUEUE_SIZE;
341
342         /* Firmware script (optional) */
343         if (rte_kvargs_count(kvlist, PMD_PARAM_FIRMWARE) == 1) {
344                 ret = rte_kvargs_process(kvlist, PMD_PARAM_FIRMWARE,
345                         &get_string, &p->firmware);
346                 if (ret < 0)
347                         goto out_free;
348         }
349
350         /* CPU ID (optional) */
351         if (rte_kvargs_count(kvlist, PMD_PARAM_CPU_ID) == 1) {
352                 ret = rte_kvargs_process(kvlist, PMD_PARAM_CPU_ID,
353                         &get_uint32, &p->cpu_id);
354                 if (ret < 0)
355                         goto out_free;
356         }
357
358         /* TM number of queues (optional) */
359         if (rte_kvargs_count(kvlist, PMD_PARAM_TM_N_QUEUES) == 1) {
360                 ret = rte_kvargs_process(kvlist, PMD_PARAM_TM_N_QUEUES,
361                         &get_uint32, &p->tm.n_queues);
362                 if (ret < 0)
363                         goto out_free;
364         }
365
366         /* TM queue size 0 .. 3 (optional) */
367         if (rte_kvargs_count(kvlist, PMD_PARAM_TM_QSIZE0) == 1) {
368                 ret = rte_kvargs_process(kvlist, PMD_PARAM_TM_QSIZE0,
369                         &get_uint32, &p->tm.qsize[0]);
370                 if (ret < 0)
371                         goto out_free;
372         }
373
374         if (rte_kvargs_count(kvlist, PMD_PARAM_TM_QSIZE1) == 1) {
375                 ret = rte_kvargs_process(kvlist, PMD_PARAM_TM_QSIZE1,
376                         &get_uint32, &p->tm.qsize[1]);
377                 if (ret < 0)
378                         goto out_free;
379         }
380
381         if (rte_kvargs_count(kvlist, PMD_PARAM_TM_QSIZE2) == 1) {
382                 ret = rte_kvargs_process(kvlist, PMD_PARAM_TM_QSIZE2,
383                         &get_uint32, &p->tm.qsize[2]);
384                 if (ret < 0)
385                         goto out_free;
386         }
387
388         if (rte_kvargs_count(kvlist, PMD_PARAM_TM_QSIZE3) == 1) {
389                 ret = rte_kvargs_process(kvlist, PMD_PARAM_TM_QSIZE3,
390                         &get_uint32, &p->tm.qsize[3]);
391                 if (ret < 0)
392                         goto out_free;
393         }
394
395 out_free:
396         rte_kvargs_free(kvlist);
397         return ret;
398 }
399
400 static int
401 pmd_probe(struct rte_vdev_device *vdev)
402 {
403         struct pmd_params p;
404         const char *params;
405         int status = 0;
406
407         void *dev_private;
408         const char *name = rte_vdev_device_name(vdev);
409
410         PMD_LOG(INFO, "Probing device \"%s\"", name);
411
412         /* Parse input arguments */
413         params = rte_vdev_device_args(vdev);
414         if (!params)
415                 return -EINVAL;
416
417         status = pmd_parse_args(&p, params);
418         if (status)
419                 return status;
420
421         p.name = name;
422
423         /* Allocate and initialize soft ethdev private data */
424         dev_private = pmd_init(&p);
425         if (dev_private == NULL)
426                 return -ENOMEM;
427
428         /* Register soft ethdev */
429         PMD_LOG(INFO, "Creating soft ethdev \"%s\"", p.name);
430
431         status = pmd_ethdev_register(vdev, &p, dev_private);
432         if (status) {
433                 pmd_free(dev_private);
434                 return status;
435         }
436
437         return 0;
438 }
439
440 static int
441 pmd_remove(struct rte_vdev_device *vdev)
442 {
443         struct rte_eth_dev *dev = NULL;
444         struct pmd_internals *p;
445
446         if (!vdev)
447                 return -EINVAL;
448
449         PMD_LOG(INFO, "Removing device \"%s\"", rte_vdev_device_name(vdev));
450
451         /* Find the ethdev entry */
452         dev = rte_eth_dev_allocated(rte_vdev_device_name(vdev));
453         if (dev == NULL)
454                 return -ENODEV;
455         p = dev->data->dev_private;
456
457         /* Free device data structures*/
458         rte_free(dev->data);
459         rte_eth_dev_release_port(dev);
460         pmd_free(p);
461
462         return 0;
463 }
464
465 static struct rte_vdev_driver pmd_softnic_drv = {
466         .probe = pmd_probe,
467         .remove = pmd_remove,
468 };
469
470 RTE_PMD_REGISTER_VDEV(net_softnic, pmd_softnic_drv);
471 RTE_PMD_REGISTER_PARAM_STRING(net_softnic,
472         PMD_PARAM_FIRMWARE "=<string> "
473         PMD_PARAM_CPU_ID "=<uint32> "
474         PMD_PARAM_TM_N_QUEUES "=<uint32> "
475         PMD_PARAM_TM_QSIZE0 "=<uint32> "
476         PMD_PARAM_TM_QSIZE1 "=<uint32> "
477         PMD_PARAM_TM_QSIZE2 "=<uint32> "
478         PMD_PARAM_TM_QSIZE3 "=<uint32>"
479 );
480
481
482 RTE_INIT(pmd_softnic_init_log)
483 {
484         pmd_softnic_logtype = rte_log_register("pmd.net.softnic");
485         if (pmd_softnic_logtype >= 0)
486                 rte_log_set_level(pmd_softnic_logtype, RTE_LOG_NOTICE);
487 }