1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017 Intel Corporation
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>
16 #include <rte_tm_driver.h>
17 #include <rte_mtr_driver.h>
19 #include "rte_eth_softnic.h"
20 #include "rte_eth_softnic_internals.h"
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"
32 static const char * const pmd_valid_args[] = {
37 PMD_PARAM_TM_N_QUEUES,
45 static const char welcome[] =
47 "Welcome to Soft NIC!\n"
50 static const char prompt[] = "softnic> ";
52 static const struct softnic_conn_params conn_params_default = {
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,
64 static int pmd_softnic_logtype;
66 #define PMD_LOG(level, fmt, args...) \
67 rte_log(RTE_LOG_ ## level, pmd_softnic_logtype, \
68 "%s(): " fmt "\n", __func__, ##args)
71 pmd_dev_infos_get(struct rte_eth_dev *dev __rte_unused,
72 struct rte_eth_dev_info *dev_info)
74 dev_info->max_rx_pktlen = UINT32_MAX;
75 dev_info->max_rx_queues = UINT16_MAX;
76 dev_info->max_tx_queues = UINT16_MAX;
80 pmd_dev_configure(struct rte_eth_dev *dev __rte_unused)
86 pmd_rx_queue_setup(struct rte_eth_dev *dev,
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)
94 struct pmd_internals *p = dev->data->dev_private;
95 struct softnic_swq *swq;
97 struct softnic_swq_params params = {
101 snprintf(name, sizeof(name), "RXQ%u", rx_queue_id);
103 swq = softnic_swq_create(p,
109 dev->data->rx_queues[rx_queue_id] = swq->r;
114 pmd_tx_queue_setup(struct rte_eth_dev *dev,
115 uint16_t tx_queue_id,
117 unsigned int socket_id __rte_unused,
118 const struct rte_eth_txconf *tx_conf __rte_unused)
120 char name[NAME_SIZE];
121 struct pmd_internals *p = dev->data->dev_private;
122 struct softnic_swq *swq;
124 struct softnic_swq_params params = {
128 snprintf(name, sizeof(name), "TXQ%u", tx_queue_id);
130 swq = softnic_swq_create(p,
136 dev->data->tx_queues[tx_queue_id] = swq->r;
141 pmd_dev_start(struct rte_eth_dev *dev)
143 struct pmd_internals *p = dev->data->dev_private;
147 status = softnic_cli_script_process(p,
149 conn_params_default.msg_in_len_max,
150 conn_params_default.msg_out_len_max);
155 dev->data->dev_link.link_status = ETH_LINK_UP;
161 pmd_dev_stop(struct rte_eth_dev *dev)
163 struct pmd_internals *p = dev->data->dev_private;
166 dev->data->dev_link.link_status = ETH_LINK_DOWN;
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);
174 softnic_tmgr_free(p);
175 softnic_link_free(p);
176 softnic_softnic_swq_free_keep_rxq_txq(p);
177 softnic_mempool_free(p);
179 tm_hierarchy_free(p);
184 pmd_dev_close(struct rte_eth_dev *dev __rte_unused)
190 pmd_link_update(struct rte_eth_dev *dev __rte_unused,
191 int wait_to_complete __rte_unused)
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,
202 if (filter_type == RTE_ETH_FILTER_GENERIC &&
203 filter_op == RTE_ETH_FILTER_GET) {
204 *(const void **)arg = &pmd_flow_ops;
212 pmd_tm_ops_get(struct rte_eth_dev *dev __rte_unused, void *arg)
214 *(const struct rte_tm_ops **)arg = &pmd_tm_ops;
220 pmd_mtr_ops_get(struct rte_eth_dev *dev __rte_unused, void *arg)
222 *(const struct rte_mtr_ops **)arg = &pmd_mtr_ops;
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,
242 pmd_rx_pkt_burst(void *rxq,
243 struct rte_mbuf **rx_pkts,
246 return (uint16_t)rte_ring_sc_dequeue_burst(rxq,
253 pmd_tx_pkt_burst(void *txq,
254 struct rte_mbuf **tx_pkts,
257 return (uint16_t)rte_ring_sp_enqueue_burst(txq,
264 pmd_init(struct pmd_params *params)
266 struct pmd_internals *p;
269 p = rte_zmalloc_socket(params->name,
270 sizeof(struct pmd_internals),
277 memcpy(&p->params, params, sizeof(p->params));
280 tm_hierarchy_init(p);
283 softnic_mempool_init(p);
285 softnic_link_init(p);
286 softnic_tmgr_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);
293 status = softnic_thread_init(p);
299 if (params->conn_port) {
300 struct softnic_conn_params conn_params;
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;
306 p->conn = softnic_conn_init(&conn_params);
307 if (p->conn == NULL) {
308 softnic_thread_free(p);
318 pmd_free(struct pmd_internals *p)
323 if (p->params.conn_port)
324 softnic_conn_free(p->conn);
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);
331 softnic_tmgr_free(p);
332 softnic_link_free(p);
334 softnic_mempool_free(p);
336 tm_hierarchy_free(p);
342 static struct rte_ether_addr eth_addr = {
347 pmd_ethdev_register(struct rte_vdev_device *vdev,
348 struct pmd_params *params,
351 struct rte_eth_dev *dev;
353 /* Ethdev entry allocation */
354 dev = rte_eth_dev_allocate(params->name);
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;
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 = ð_addr;
372 dev->data->promiscuous = 1;
373 dev->data->kdrv = RTE_KDRV_NONE;
374 dev->data->numa_node = params->cpu_id;
376 rte_eth_dev_probing_finish(dev);
382 get_string(const char *key __rte_unused, const char *value, void *extra_args)
384 if (!value || !extra_args)
387 *(char **)extra_args = strdup(value);
389 if (!*(char **)extra_args)
396 get_uint32(const char *key __rte_unused, const char *value, void *extra_args)
398 if (!value || !extra_args)
401 *(uint32_t *)extra_args = strtoull(value, NULL, 0);
407 get_uint16(const char *key __rte_unused, const char *value, void *extra_args)
409 if (!value || !extra_args)
412 *(uint16_t *)extra_args = strtoull(value, NULL, 0);
418 pmd_parse_args(struct pmd_params *p, const char *params)
420 struct rte_kvargs *kvlist;
423 kvlist = rte_kvargs_parse(params, pmd_valid_args);
427 /* Set default values */
428 memset(p, 0, sizeof(*p));
429 p->firmware = SOFTNIC_FIRMWARE;
430 p->cpu_id = SOFTNIC_CPU_ID;
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;
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);
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);
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);
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);
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);
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]);
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]);
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]);
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]);
508 rte_kvargs_free(kvlist);
513 pmd_probe(struct rte_vdev_device *vdev)
520 const char *name = rte_vdev_device_name(vdev);
522 PMD_LOG(INFO, "Probing device \"%s\"", name);
524 /* Parse input arguments */
525 params = rte_vdev_device_args(vdev);
529 status = pmd_parse_args(&p, params);
535 /* Allocate and initialize soft ethdev private data */
536 dev_private = pmd_init(&p);
537 if (dev_private == NULL)
540 /* Register soft ethdev */
541 PMD_LOG(INFO, "Creating soft ethdev \"%s\"", p.name);
543 status = pmd_ethdev_register(vdev, &p, dev_private);
545 pmd_free(dev_private);
553 pmd_remove(struct rte_vdev_device *vdev)
555 struct rte_eth_dev *dev = NULL;
560 PMD_LOG(INFO, "Removing device \"%s\"", rte_vdev_device_name(vdev));
562 /* Find the ethdev entry */
563 dev = rte_eth_dev_allocated(rte_vdev_device_name(vdev));
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);
576 static struct rte_vdev_driver pmd_softnic_drv = {
578 .remove = pmd_remove,
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>"
594 RTE_INIT(pmd_softnic_init_log)
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);
602 rte_pmd_softnic_manage(uint16_t port_id)
604 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
605 struct pmd_internals *softnic;
607 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
608 RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, 0);
611 softnic = dev->data->dev_private;
613 softnic_conn_poll_for_conn(softnic->conn);
615 softnic_conn_poll_for_msg(softnic->conn);