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