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