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"
31 #define PMD_PARAM_TM_QSIZE4 "tm_qsize4"
32 #define PMD_PARAM_TM_QSIZE5 "tm_qsize5"
33 #define PMD_PARAM_TM_QSIZE6 "tm_qsize6"
34 #define PMD_PARAM_TM_QSIZE7 "tm_qsize7"
35 #define PMD_PARAM_TM_QSIZE8 "tm_qsize8"
36 #define PMD_PARAM_TM_QSIZE9 "tm_qsize9"
37 #define PMD_PARAM_TM_QSIZE10 "tm_qsize10"
38 #define PMD_PARAM_TM_QSIZE11 "tm_qsize11"
39 #define PMD_PARAM_TM_QSIZE12 "tm_qsize12"
42 static const char * const pmd_valid_args[] = {
47 PMD_PARAM_TM_N_QUEUES,
64 static const char welcome[] =
66 "Welcome to Soft NIC!\n"
69 static const char prompt[] = "softnic> ";
71 static const struct softnic_conn_params conn_params_default = {
76 .buf_size = 1024 * 1024,
77 .msg_in_len_max = 1024,
78 .msg_out_len_max = 1024 * 1024,
79 .msg_handle = softnic_cli_process,
80 .msg_handle_arg = NULL,
83 RTE_LOG_REGISTER(pmd_softnic_logtype, pmd.net.softnic, NOTICE);
85 #define PMD_LOG(level, fmt, args...) \
86 rte_log(RTE_LOG_ ## level, pmd_softnic_logtype, \
87 "%s(): " fmt "\n", __func__, ##args)
90 pmd_dev_infos_get(struct rte_eth_dev *dev __rte_unused,
91 struct rte_eth_dev_info *dev_info)
93 dev_info->max_rx_pktlen = UINT32_MAX;
94 dev_info->max_rx_queues = UINT16_MAX;
95 dev_info->max_tx_queues = UINT16_MAX;
101 pmd_dev_configure(struct rte_eth_dev *dev __rte_unused)
107 pmd_rx_queue_setup(struct rte_eth_dev *dev,
108 uint16_t rx_queue_id,
110 unsigned int socket_id __rte_unused,
111 const struct rte_eth_rxconf *rx_conf __rte_unused,
112 struct rte_mempool *mb_pool __rte_unused)
114 char name[NAME_SIZE];
115 struct pmd_internals *p = dev->data->dev_private;
116 struct softnic_swq *swq;
118 struct softnic_swq_params params = {
122 snprintf(name, sizeof(name), "RXQ%u", rx_queue_id);
124 swq = softnic_swq_create(p,
130 dev->data->rx_queues[rx_queue_id] = swq->r;
135 pmd_tx_queue_setup(struct rte_eth_dev *dev,
136 uint16_t tx_queue_id,
138 unsigned int socket_id __rte_unused,
139 const struct rte_eth_txconf *tx_conf __rte_unused)
141 char name[NAME_SIZE];
142 struct pmd_internals *p = dev->data->dev_private;
143 struct softnic_swq *swq;
145 struct softnic_swq_params params = {
149 snprintf(name, sizeof(name), "TXQ%u", tx_queue_id);
151 swq = softnic_swq_create(p,
157 dev->data->tx_queues[tx_queue_id] = swq->r;
162 pmd_dev_start(struct rte_eth_dev *dev)
164 struct pmd_internals *p = dev->data->dev_private;
168 status = softnic_cli_script_process(p,
170 conn_params_default.msg_in_len_max,
171 conn_params_default.msg_out_len_max);
176 dev->data->dev_link.link_status = ETH_LINK_UP;
182 pmd_dev_stop(struct rte_eth_dev *dev)
184 struct pmd_internals *p = dev->data->dev_private;
187 dev->data->dev_link.link_status = ETH_LINK_DOWN;
190 softnic_pipeline_disable_all(p);
191 softnic_pipeline_free(p);
192 softnic_table_action_profile_free(p);
193 softnic_port_in_action_profile_free(p);
195 softnic_tmgr_free(p);
196 softnic_link_free(p);
197 softnic_softnic_swq_free_keep_rxq_txq(p);
198 softnic_mempool_free(p);
200 tm_hierarchy_free(p);
205 pmd_dev_close(struct rte_eth_dev *dev __rte_unused)
211 pmd_link_update(struct rte_eth_dev *dev __rte_unused,
212 int wait_to_complete __rte_unused)
218 pmd_filter_ctrl(struct rte_eth_dev *dev __rte_unused,
219 enum rte_filter_type filter_type,
220 enum rte_filter_op filter_op,
223 if (filter_type == RTE_ETH_FILTER_GENERIC &&
224 filter_op == RTE_ETH_FILTER_GET) {
225 *(const void **)arg = &pmd_flow_ops;
233 pmd_tm_ops_get(struct rte_eth_dev *dev __rte_unused, void *arg)
235 *(const struct rte_tm_ops **)arg = &pmd_tm_ops;
241 pmd_mtr_ops_get(struct rte_eth_dev *dev __rte_unused, void *arg)
243 *(const struct rte_mtr_ops **)arg = &pmd_mtr_ops;
248 static const struct eth_dev_ops pmd_ops = {
249 .dev_configure = pmd_dev_configure,
250 .dev_start = pmd_dev_start,
251 .dev_stop = pmd_dev_stop,
252 .dev_close = pmd_dev_close,
253 .link_update = pmd_link_update,
254 .dev_infos_get = pmd_dev_infos_get,
255 .rx_queue_setup = pmd_rx_queue_setup,
256 .tx_queue_setup = pmd_tx_queue_setup,
257 .filter_ctrl = pmd_filter_ctrl,
258 .tm_ops_get = pmd_tm_ops_get,
259 .mtr_ops_get = pmd_mtr_ops_get,
263 pmd_rx_pkt_burst(void *rxq,
264 struct rte_mbuf **rx_pkts,
267 return (uint16_t)rte_ring_sc_dequeue_burst(rxq,
274 pmd_tx_pkt_burst(void *txq,
275 struct rte_mbuf **tx_pkts,
278 return (uint16_t)rte_ring_sp_enqueue_burst(txq,
285 pmd_init(struct pmd_params *params)
287 struct pmd_internals *p;
290 p = rte_zmalloc_socket(params->name,
291 sizeof(struct pmd_internals),
298 memcpy(&p->params, params, sizeof(p->params));
301 tm_hierarchy_init(p);
304 softnic_mempool_init(p);
306 softnic_link_init(p);
307 softnic_tmgr_init(p);
309 softnic_cryptodev_init(p);
310 softnic_port_in_action_profile_init(p);
311 softnic_table_action_profile_init(p);
312 softnic_pipeline_init(p);
314 status = softnic_thread_init(p);
320 if (params->conn_port) {
321 struct softnic_conn_params conn_params;
323 memcpy(&conn_params, &conn_params_default, sizeof(conn_params));
324 conn_params.port = p->params.conn_port;
325 conn_params.msg_handle_arg = p;
327 p->conn = softnic_conn_init(&conn_params);
328 if (p->conn == NULL) {
329 softnic_thread_free(p);
339 pmd_free(struct pmd_internals *p)
344 if (p->params.conn_port)
345 softnic_conn_free(p->conn);
347 softnic_thread_free(p);
348 softnic_pipeline_free(p);
349 softnic_table_action_profile_free(p);
350 softnic_port_in_action_profile_free(p);
352 softnic_tmgr_free(p);
353 softnic_link_free(p);
355 softnic_mempool_free(p);
357 tm_hierarchy_free(p);
363 static struct rte_ether_addr eth_addr = {
368 pmd_ethdev_register(struct rte_vdev_device *vdev,
369 struct pmd_params *params,
372 struct rte_eth_dev *dev;
374 /* Ethdev entry allocation */
375 dev = rte_eth_dev_allocate(params->name);
380 dev->rx_pkt_burst = pmd_rx_pkt_burst;
381 dev->tx_pkt_burst = pmd_tx_pkt_burst;
382 dev->tx_pkt_prepare = NULL;
383 dev->dev_ops = &pmd_ops;
384 dev->device = &vdev->device;
387 dev->data->dev_private = dev_private;
388 dev->data->dev_link.link_speed = ETH_SPEED_NUM_100G;
389 dev->data->dev_link.link_duplex = ETH_LINK_FULL_DUPLEX;
390 dev->data->dev_link.link_autoneg = ETH_LINK_FIXED;
391 dev->data->dev_link.link_status = ETH_LINK_DOWN;
392 dev->data->mac_addrs = ð_addr;
393 dev->data->promiscuous = 1;
394 dev->data->numa_node = params->cpu_id;
396 rte_eth_dev_probing_finish(dev);
402 get_string(const char *key __rte_unused, const char *value, void *extra_args)
404 if (!value || !extra_args)
407 *(char **)extra_args = strdup(value);
409 if (!*(char **)extra_args)
416 get_uint32(const char *key __rte_unused, const char *value, void *extra_args)
418 if (!value || !extra_args)
421 *(uint32_t *)extra_args = strtoull(value, NULL, 0);
427 get_uint16(const char *key __rte_unused, const char *value, void *extra_args)
429 if (!value || !extra_args)
432 *(uint16_t *)extra_args = strtoull(value, NULL, 0);
438 pmd_parse_args(struct pmd_params *p, const char *params)
440 struct rte_kvargs *kvlist;
443 kvlist = rte_kvargs_parse(params, pmd_valid_args);
447 /* Set default values */
448 memset(p, 0, sizeof(*p));
449 p->firmware = SOFTNIC_FIRMWARE;
450 p->cpu_id = SOFTNIC_CPU_ID;
452 p->tm.n_queues = SOFTNIC_TM_N_QUEUES;
453 p->tm.qsize[0] = SOFTNIC_TM_QUEUE_SIZE;
454 p->tm.qsize[1] = SOFTNIC_TM_QUEUE_SIZE;
455 p->tm.qsize[2] = SOFTNIC_TM_QUEUE_SIZE;
456 p->tm.qsize[3] = SOFTNIC_TM_QUEUE_SIZE;
457 p->tm.qsize[4] = SOFTNIC_TM_QUEUE_SIZE;
458 p->tm.qsize[5] = SOFTNIC_TM_QUEUE_SIZE;
459 p->tm.qsize[6] = SOFTNIC_TM_QUEUE_SIZE;
460 p->tm.qsize[7] = SOFTNIC_TM_QUEUE_SIZE;
461 p->tm.qsize[8] = SOFTNIC_TM_QUEUE_SIZE;
462 p->tm.qsize[9] = SOFTNIC_TM_QUEUE_SIZE;
463 p->tm.qsize[10] = SOFTNIC_TM_QUEUE_SIZE;
464 p->tm.qsize[11] = SOFTNIC_TM_QUEUE_SIZE;
465 p->tm.qsize[12] = SOFTNIC_TM_QUEUE_SIZE;
467 /* Firmware script (optional) */
468 if (rte_kvargs_count(kvlist, PMD_PARAM_FIRMWARE) == 1) {
469 ret = rte_kvargs_process(kvlist, PMD_PARAM_FIRMWARE,
470 &get_string, &p->firmware);
475 /* Connection listening port (optional) */
476 if (rte_kvargs_count(kvlist, PMD_PARAM_CONN_PORT) == 1) {
477 ret = rte_kvargs_process(kvlist, PMD_PARAM_CONN_PORT,
478 &get_uint16, &p->conn_port);
483 /* CPU ID (optional) */
484 if (rte_kvargs_count(kvlist, PMD_PARAM_CPU_ID) == 1) {
485 ret = rte_kvargs_process(kvlist, PMD_PARAM_CPU_ID,
486 &get_uint32, &p->cpu_id);
491 /* Service cores (optional) */
492 if (rte_kvargs_count(kvlist, PMD_PARAM_SC) == 1) {
493 ret = rte_kvargs_process(kvlist, PMD_PARAM_SC,
494 &get_uint32, &p->sc);
499 /* TM number of queues (optional) */
500 if (rte_kvargs_count(kvlist, PMD_PARAM_TM_N_QUEUES) == 1) {
501 ret = rte_kvargs_process(kvlist, PMD_PARAM_TM_N_QUEUES,
502 &get_uint32, &p->tm.n_queues);
507 /* TM queue size 0 .. 3 (optional) */
508 if (rte_kvargs_count(kvlist, PMD_PARAM_TM_QSIZE0) == 1) {
509 ret = rte_kvargs_process(kvlist, PMD_PARAM_TM_QSIZE0,
510 &get_uint32, &p->tm.qsize[0]);
515 if (rte_kvargs_count(kvlist, PMD_PARAM_TM_QSIZE1) == 1) {
516 ret = rte_kvargs_process(kvlist, PMD_PARAM_TM_QSIZE1,
517 &get_uint32, &p->tm.qsize[1]);
522 if (rte_kvargs_count(kvlist, PMD_PARAM_TM_QSIZE2) == 1) {
523 ret = rte_kvargs_process(kvlist, PMD_PARAM_TM_QSIZE2,
524 &get_uint32, &p->tm.qsize[2]);
529 if (rte_kvargs_count(kvlist, PMD_PARAM_TM_QSIZE3) == 1) {
530 ret = rte_kvargs_process(kvlist, PMD_PARAM_TM_QSIZE3,
531 &get_uint32, &p->tm.qsize[3]);
536 if (rte_kvargs_count(kvlist, PMD_PARAM_TM_QSIZE4) == 1) {
537 ret = rte_kvargs_process(kvlist, PMD_PARAM_TM_QSIZE4,
538 &get_uint32, &p->tm.qsize[4]);
543 if (rte_kvargs_count(kvlist, PMD_PARAM_TM_QSIZE5) == 1) {
544 ret = rte_kvargs_process(kvlist, PMD_PARAM_TM_QSIZE5,
545 &get_uint32, &p->tm.qsize[5]);
550 if (rte_kvargs_count(kvlist, PMD_PARAM_TM_QSIZE6) == 1) {
551 ret = rte_kvargs_process(kvlist, PMD_PARAM_TM_QSIZE6,
552 &get_uint32, &p->tm.qsize[6]);
557 if (rte_kvargs_count(kvlist, PMD_PARAM_TM_QSIZE7) == 1) {
558 ret = rte_kvargs_process(kvlist, PMD_PARAM_TM_QSIZE7,
559 &get_uint32, &p->tm.qsize[7]);
563 if (rte_kvargs_count(kvlist, PMD_PARAM_TM_QSIZE8) == 1) {
564 ret = rte_kvargs_process(kvlist, PMD_PARAM_TM_QSIZE8,
565 &get_uint32, &p->tm.qsize[8]);
569 if (rte_kvargs_count(kvlist, PMD_PARAM_TM_QSIZE9) == 1) {
570 ret = rte_kvargs_process(kvlist, PMD_PARAM_TM_QSIZE9,
571 &get_uint32, &p->tm.qsize[9]);
576 if (rte_kvargs_count(kvlist, PMD_PARAM_TM_QSIZE10) == 1) {
577 ret = rte_kvargs_process(kvlist, PMD_PARAM_TM_QSIZE10,
578 &get_uint32, &p->tm.qsize[10]);
583 if (rte_kvargs_count(kvlist, PMD_PARAM_TM_QSIZE11) == 1) {
584 ret = rte_kvargs_process(kvlist, PMD_PARAM_TM_QSIZE11,
585 &get_uint32, &p->tm.qsize[11]);
590 if (rte_kvargs_count(kvlist, PMD_PARAM_TM_QSIZE12) == 1) {
591 ret = rte_kvargs_process(kvlist, PMD_PARAM_TM_QSIZE12,
592 &get_uint32, &p->tm.qsize[12]);
598 rte_kvargs_free(kvlist);
603 pmd_probe(struct rte_vdev_device *vdev)
610 const char *name = rte_vdev_device_name(vdev);
612 PMD_LOG(INFO, "Probing device \"%s\"", name);
614 /* Parse input arguments */
615 params = rte_vdev_device_args(vdev);
619 status = pmd_parse_args(&p, params);
625 /* Allocate and initialize soft ethdev private data */
626 dev_private = pmd_init(&p);
627 if (dev_private == NULL)
630 /* Register soft ethdev */
631 PMD_LOG(INFO, "Creating soft ethdev \"%s\"", p.name);
633 status = pmd_ethdev_register(vdev, &p, dev_private);
635 pmd_free(dev_private);
643 pmd_remove(struct rte_vdev_device *vdev)
645 struct rte_eth_dev *dev = NULL;
650 PMD_LOG(INFO, "Removing device \"%s\"", rte_vdev_device_name(vdev));
652 /* Find the ethdev entry */
653 dev = rte_eth_dev_allocated(rte_vdev_device_name(vdev));
657 /* Free device data structures*/
658 pmd_free(dev->data->dev_private);
659 dev->data->dev_private = NULL; /* already freed */
660 dev->data->mac_addrs = NULL; /* statically allocated */
661 rte_eth_dev_release_port(dev);
666 static struct rte_vdev_driver pmd_softnic_drv = {
668 .remove = pmd_remove,
671 RTE_PMD_REGISTER_VDEV(net_softnic, pmd_softnic_drv);
672 RTE_PMD_REGISTER_PARAM_STRING(net_softnic,
673 PMD_PARAM_FIRMWARE "=<string> "
674 PMD_PARAM_CONN_PORT "=<uint16> "
675 PMD_PARAM_CPU_ID "=<uint32> "
676 PMD_PARAM_TM_N_QUEUES "=<uint32> "
677 PMD_PARAM_TM_QSIZE0 "=<uint32> "
678 PMD_PARAM_TM_QSIZE1 "=<uint32> "
679 PMD_PARAM_TM_QSIZE2 "=<uint32> "
680 PMD_PARAM_TM_QSIZE3 "=<uint32>"
681 PMD_PARAM_TM_QSIZE4 "=<uint32> "
682 PMD_PARAM_TM_QSIZE5 "=<uint32> "
683 PMD_PARAM_TM_QSIZE6 "=<uint32> "
684 PMD_PARAM_TM_QSIZE7 "=<uint32> "
685 PMD_PARAM_TM_QSIZE8 "=<uint32> "
686 PMD_PARAM_TM_QSIZE9 "=<uint32> "
687 PMD_PARAM_TM_QSIZE10 "=<uint32> "
688 PMD_PARAM_TM_QSIZE11 "=<uint32>"
689 PMD_PARAM_TM_QSIZE12 "=<uint32>"
693 rte_pmd_softnic_manage(uint16_t port_id)
695 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
696 struct pmd_internals *softnic;
698 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
699 RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, 0);
702 softnic = dev->data->dev_private;
704 softnic_conn_poll_for_conn(softnic->conn);
706 softnic_conn_poll_for_msg(softnic->conn);