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_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"
31 static const char *pmd_valid_args[] = {
35 PMD_PARAM_TM_N_QUEUES,
43 static const char welcome[] =
45 "Welcome to Soft NIC!\n"
48 static const char prompt[] = "softnic> ";
50 struct softnic_conn_params conn_params_default = {
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,
62 static const struct rte_eth_dev_info pmd_dev_info = {
64 .max_rx_pktlen = UINT32_MAX,
65 .max_rx_queues = UINT16_MAX,
66 .max_tx_queues = UINT16_MAX,
79 static int pmd_softnic_logtype;
81 #define PMD_LOG(level, fmt, args...) \
82 rte_log(RTE_LOG_ ## level, pmd_softnic_logtype, \
83 "%s(): " fmt "\n", __func__, ##args)
86 pmd_dev_infos_get(struct rte_eth_dev *dev __rte_unused,
87 struct rte_eth_dev_info *dev_info)
89 memcpy(dev_info, &pmd_dev_info, sizeof(*dev_info));
93 pmd_dev_configure(struct rte_eth_dev *dev __rte_unused)
99 pmd_rx_queue_setup(struct rte_eth_dev *dev,
100 uint16_t rx_queue_id,
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)
106 char name[NAME_SIZE];
107 struct pmd_internals *p = dev->data->dev_private;
108 struct softnic_swq *swq;
110 struct softnic_swq_params params = {
114 snprintf(name, sizeof(name), "RXQ%u", rx_queue_id);
116 swq = softnic_swq_create(p,
122 dev->data->rx_queues[rx_queue_id] = swq->r;
127 pmd_tx_queue_setup(struct rte_eth_dev *dev,
128 uint16_t tx_queue_id,
130 unsigned int socket_id __rte_unused,
131 const struct rte_eth_txconf *tx_conf __rte_unused)
133 char name[NAME_SIZE];
134 struct pmd_internals *p = dev->data->dev_private;
135 struct softnic_swq *swq;
137 struct softnic_swq_params params = {
141 snprintf(name, sizeof(name), "TXQ%u", tx_queue_id);
143 swq = softnic_swq_create(p,
149 dev->data->tx_queues[tx_queue_id] = swq->r;
154 pmd_dev_start(struct rte_eth_dev *dev)
156 struct pmd_internals *p = dev->data->dev_private;
160 status = softnic_cli_script_process(p,
162 conn_params_default.msg_in_len_max,
163 conn_params_default.msg_out_len_max);
168 dev->data->dev_link.link_status = ETH_LINK_UP;
174 pmd_dev_stop(struct rte_eth_dev *dev)
176 struct pmd_internals *p = dev->data->dev_private;
179 dev->data->dev_link.link_status = ETH_LINK_DOWN;
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);
187 softnic_tmgr_free(p);
188 softnic_link_free(p);
189 softnic_softnic_swq_free_keep_rxq_txq(p);
190 softnic_mempool_free(p);
192 tm_hierarchy_free(p);
196 pmd_dev_close(struct rte_eth_dev *dev __rte_unused)
202 pmd_link_update(struct rte_eth_dev *dev __rte_unused,
203 int wait_to_complete __rte_unused)
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,
214 if (filter_type == RTE_ETH_FILTER_GENERIC &&
215 filter_op == RTE_ETH_FILTER_GET) {
216 *(const void **)arg = &pmd_flow_ops;
224 pmd_tm_ops_get(struct rte_eth_dev *dev __rte_unused, void *arg)
226 *(const struct rte_tm_ops **)arg = &pmd_tm_ops;
232 pmd_mtr_ops_get(struct rte_eth_dev *dev __rte_unused, void *arg)
234 *(const struct rte_mtr_ops **)arg = &pmd_mtr_ops;
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,
254 pmd_rx_pkt_burst(void *rxq,
255 struct rte_mbuf **rx_pkts,
258 return (uint16_t)rte_ring_sc_dequeue_burst(rxq,
265 pmd_tx_pkt_burst(void *txq,
266 struct rte_mbuf **tx_pkts,
269 return (uint16_t)rte_ring_sp_enqueue_burst(txq,
276 pmd_init(struct pmd_params *params)
278 struct pmd_internals *p;
281 p = rte_zmalloc_socket(params->name,
282 sizeof(struct pmd_internals),
289 memcpy(&p->params, params, sizeof(p->params));
292 tm_hierarchy_init(p);
294 softnic_mempool_init(p);
296 softnic_link_init(p);
297 softnic_tmgr_init(p);
299 softnic_port_in_action_profile_init(p);
300 softnic_table_action_profile_init(p);
301 softnic_pipeline_init(p);
303 status = softnic_thread_init(p);
309 if (params->conn_port) {
310 struct softnic_conn_params conn_params;
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;
316 p->conn = softnic_conn_init(&conn_params);
317 if (p->conn == NULL) {
318 softnic_thread_free(p);
328 pmd_free(struct pmd_internals *p)
333 if (p->params.conn_port)
334 softnic_conn_free(p->conn);
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);
341 softnic_tmgr_free(p);
342 softnic_link_free(p);
344 softnic_mempool_free(p);
346 tm_hierarchy_free(p);
351 static struct ether_addr eth_addr = {
356 pmd_ethdev_register(struct rte_vdev_device *vdev,
357 struct pmd_params *params,
360 struct rte_eth_dev *dev;
362 /* Ethdev entry allocation */
363 dev = rte_eth_dev_allocate(params->name);
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;
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 = ð_addr;
381 dev->data->promiscuous = 1;
382 dev->data->kdrv = RTE_KDRV_NONE;
383 dev->data->numa_node = params->cpu_id;
385 rte_eth_dev_probing_finish(dev);
391 get_string(const char *key __rte_unused, const char *value, void *extra_args)
393 if (!value || !extra_args)
396 *(char **)extra_args = strdup(value);
398 if (!*(char **)extra_args)
405 get_uint32(const char *key __rte_unused, const char *value, void *extra_args)
407 if (!value || !extra_args)
410 *(uint32_t *)extra_args = strtoull(value, NULL, 0);
416 get_uint16(const char *key __rte_unused, const char *value, void *extra_args)
418 if (!value || !extra_args)
421 *(uint16_t *)extra_args = strtoull(value, NULL, 0);
427 pmd_parse_args(struct pmd_params *p, const char *params)
429 struct rte_kvargs *kvlist;
432 kvlist = rte_kvargs_parse(params, pmd_valid_args);
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;
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);
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);
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);
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;
556 struct pmd_internals *p;
561 PMD_LOG(INFO, "Removing device \"%s\"", rte_vdev_device_name(vdev));
563 /* Find the ethdev entry */
564 dev = rte_eth_dev_allocated(rte_vdev_device_name(vdev));
567 p = dev->data->dev_private;
569 /* Free device data structures*/
571 rte_eth_dev_release_port(dev);
577 static struct rte_vdev_driver pmd_softnic_drv = {
579 .remove = pmd_remove,
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>"
595 RTE_INIT(pmd_softnic_init_log)
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);
603 rte_pmd_softnic_manage(uint16_t port_id)
605 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
606 struct pmd_internals *softnic;
608 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
609 RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, 0);
612 softnic = dev->data->dev_private;
614 softnic_conn_poll_for_conn(softnic->conn);
616 softnic_conn_poll_for_msg(softnic->conn);