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