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>
18 #include "rte_eth_softnic.h"
19 #include "rte_eth_softnic_internals.h"
21 #define PMD_PARAM_FIRMWARE "firmware"
22 #define PMD_PARAM_CONN_PORT "conn_port"
23 #define PMD_PARAM_CPU_ID "cpu_id"
24 #define PMD_PARAM_TM_N_QUEUES "tm_n_queues"
25 #define PMD_PARAM_TM_QSIZE0 "tm_qsize0"
26 #define PMD_PARAM_TM_QSIZE1 "tm_qsize1"
27 #define PMD_PARAM_TM_QSIZE2 "tm_qsize2"
28 #define PMD_PARAM_TM_QSIZE3 "tm_qsize3"
30 static const char *pmd_valid_args[] = {
34 PMD_PARAM_TM_N_QUEUES,
42 static const char welcome[] =
44 "Welcome to Soft NIC!\n"
47 static const char prompt[] = "softnic> ";
49 struct softnic_conn_params conn_params_default = {
54 .buf_size = 1024 * 1024,
55 .msg_in_len_max = 1024,
56 .msg_out_len_max = 1024 * 1024,
57 .msg_handle = softnic_cli_process,
58 .msg_handle_arg = NULL,
61 static const struct rte_eth_dev_info pmd_dev_info = {
63 .max_rx_pktlen = UINT32_MAX,
64 .max_rx_queues = UINT16_MAX,
65 .max_tx_queues = UINT16_MAX,
78 static int pmd_softnic_logtype;
80 #define PMD_LOG(level, fmt, args...) \
81 rte_log(RTE_LOG_ ## level, pmd_softnic_logtype, \
82 "%s(): " fmt "\n", __func__, ##args)
85 pmd_dev_infos_get(struct rte_eth_dev *dev __rte_unused,
86 struct rte_eth_dev_info *dev_info)
88 memcpy(dev_info, &pmd_dev_info, sizeof(*dev_info));
92 pmd_dev_configure(struct rte_eth_dev *dev __rte_unused)
98 pmd_rx_queue_setup(struct rte_eth_dev *dev,
101 unsigned int socket_id __rte_unused,
102 const struct rte_eth_rxconf *rx_conf __rte_unused,
103 struct rte_mempool *mb_pool __rte_unused)
105 char name[NAME_SIZE];
106 struct pmd_internals *p = dev->data->dev_private;
107 struct softnic_swq *swq;
109 struct softnic_swq_params params = {
113 snprintf(name, sizeof(name), "RXQ%u", rx_queue_id);
115 swq = softnic_swq_create(p,
121 dev->data->rx_queues[rx_queue_id] = swq->r;
126 pmd_tx_queue_setup(struct rte_eth_dev *dev,
127 uint16_t tx_queue_id,
129 unsigned int socket_id __rte_unused,
130 const struct rte_eth_txconf *tx_conf __rte_unused)
132 char name[NAME_SIZE];
133 struct pmd_internals *p = dev->data->dev_private;
134 struct softnic_swq *swq;
136 struct softnic_swq_params params = {
140 snprintf(name, sizeof(name), "TXQ%u", tx_queue_id);
142 swq = softnic_swq_create(p,
148 dev->data->tx_queues[tx_queue_id] = swq->r;
153 pmd_dev_start(struct rte_eth_dev *dev)
155 struct pmd_internals *p = dev->data->dev_private;
159 status = softnic_cli_script_process(p,
161 conn_params_default.msg_in_len_max,
162 conn_params_default.msg_out_len_max);
167 dev->data->dev_link.link_status = ETH_LINK_UP;
173 pmd_dev_stop(struct rte_eth_dev *dev)
175 struct pmd_internals *p = dev->data->dev_private;
178 dev->data->dev_link.link_status = ETH_LINK_DOWN;
181 softnic_pipeline_disable_all(p);
182 softnic_pipeline_free(p);
183 softnic_table_action_profile_free(p);
184 softnic_port_in_action_profile_free(p);
186 softnic_tmgr_free(p);
187 softnic_link_free(p);
188 softnic_softnic_swq_free_keep_rxq_txq(p);
189 softnic_mempool_free(p);
191 tm_hierarchy_free(p);
195 pmd_dev_close(struct rte_eth_dev *dev __rte_unused)
201 pmd_link_update(struct rte_eth_dev *dev __rte_unused,
202 int wait_to_complete __rte_unused)
208 pmd_tm_ops_get(struct rte_eth_dev *dev __rte_unused, void *arg)
210 *(const struct rte_tm_ops **)arg = &pmd_tm_ops;
215 static const struct eth_dev_ops pmd_ops = {
216 .dev_configure = pmd_dev_configure,
217 .dev_start = pmd_dev_start,
218 .dev_stop = pmd_dev_stop,
219 .dev_close = pmd_dev_close,
220 .link_update = pmd_link_update,
221 .dev_infos_get = pmd_dev_infos_get,
222 .rx_queue_setup = pmd_rx_queue_setup,
223 .tx_queue_setup = pmd_tx_queue_setup,
224 .tm_ops_get = pmd_tm_ops_get,
228 pmd_rx_pkt_burst(void *rxq,
229 struct rte_mbuf **rx_pkts,
232 return (uint16_t)rte_ring_sc_dequeue_burst(rxq,
239 pmd_tx_pkt_burst(void *txq,
240 struct rte_mbuf **tx_pkts,
243 return (uint16_t)rte_ring_sp_enqueue_burst(txq,
250 pmd_init(struct pmd_params *params)
252 struct pmd_internals *p;
255 p = rte_zmalloc_socket(params->name,
256 sizeof(struct pmd_internals),
263 memcpy(&p->params, params, sizeof(p->params));
266 tm_hierarchy_init(p);
268 softnic_mempool_init(p);
270 softnic_link_init(p);
271 softnic_tmgr_init(p);
273 softnic_port_in_action_profile_init(p);
274 softnic_table_action_profile_init(p);
275 softnic_pipeline_init(p);
277 status = softnic_thread_init(p);
283 if (params->conn_port) {
284 struct softnic_conn_params conn_params;
286 memcpy(&conn_params, &conn_params_default, sizeof(conn_params));
287 conn_params.port = p->params.conn_port;
288 conn_params.msg_handle_arg = p;
290 p->conn = softnic_conn_init(&conn_params);
291 if (p->conn == NULL) {
292 softnic_thread_free(p);
302 pmd_free(struct pmd_internals *p)
307 if (p->params.conn_port)
308 softnic_conn_free(p->conn);
310 softnic_thread_free(p);
311 softnic_pipeline_free(p);
312 softnic_table_action_profile_free(p);
313 softnic_port_in_action_profile_free(p);
315 softnic_tmgr_free(p);
316 softnic_link_free(p);
318 softnic_mempool_free(p);
320 tm_hierarchy_free(p);
325 static struct ether_addr eth_addr = {
330 pmd_ethdev_register(struct rte_vdev_device *vdev,
331 struct pmd_params *params,
334 struct rte_eth_dev *dev;
336 /* Ethdev entry allocation */
337 dev = rte_eth_dev_allocate(params->name);
342 dev->rx_pkt_burst = pmd_rx_pkt_burst;
343 dev->tx_pkt_burst = pmd_tx_pkt_burst;
344 dev->tx_pkt_prepare = NULL;
345 dev->dev_ops = &pmd_ops;
346 dev->device = &vdev->device;
349 dev->data->dev_private = dev_private;
350 dev->data->dev_link.link_speed = ETH_SPEED_NUM_100G;
351 dev->data->dev_link.link_duplex = ETH_LINK_FULL_DUPLEX;
352 dev->data->dev_link.link_autoneg = ETH_LINK_FIXED;
353 dev->data->dev_link.link_status = ETH_LINK_DOWN;
354 dev->data->mac_addrs = ð_addr;
355 dev->data->promiscuous = 1;
356 dev->data->kdrv = RTE_KDRV_NONE;
357 dev->data->numa_node = params->cpu_id;
359 rte_eth_dev_probing_finish(dev);
365 get_string(const char *key __rte_unused, const char *value, void *extra_args)
367 if (!value || !extra_args)
370 *(char **)extra_args = strdup(value);
372 if (!*(char **)extra_args)
379 get_uint32(const char *key __rte_unused, const char *value, void *extra_args)
381 if (!value || !extra_args)
384 *(uint32_t *)extra_args = strtoull(value, NULL, 0);
390 get_uint16(const char *key __rte_unused, const char *value, void *extra_args)
392 if (!value || !extra_args)
395 *(uint16_t *)extra_args = strtoull(value, NULL, 0);
401 pmd_parse_args(struct pmd_params *p, const char *params)
403 struct rte_kvargs *kvlist;
406 kvlist = rte_kvargs_parse(params, pmd_valid_args);
410 /* Set default values */
411 memset(p, 0, sizeof(*p));
412 p->firmware = SOFTNIC_FIRMWARE;
413 p->cpu_id = SOFTNIC_CPU_ID;
414 p->tm.n_queues = SOFTNIC_TM_N_QUEUES;
415 p->tm.qsize[0] = SOFTNIC_TM_QUEUE_SIZE;
416 p->tm.qsize[1] = SOFTNIC_TM_QUEUE_SIZE;
417 p->tm.qsize[2] = SOFTNIC_TM_QUEUE_SIZE;
418 p->tm.qsize[3] = SOFTNIC_TM_QUEUE_SIZE;
420 /* Firmware script (optional) */
421 if (rte_kvargs_count(kvlist, PMD_PARAM_FIRMWARE) == 1) {
422 ret = rte_kvargs_process(kvlist, PMD_PARAM_FIRMWARE,
423 &get_string, &p->firmware);
428 /* Connection listening port (optional) */
429 if (rte_kvargs_count(kvlist, PMD_PARAM_CONN_PORT) == 1) {
430 ret = rte_kvargs_process(kvlist, PMD_PARAM_CONN_PORT,
431 &get_uint16, &p->conn_port);
436 /* CPU ID (optional) */
437 if (rte_kvargs_count(kvlist, PMD_PARAM_CPU_ID) == 1) {
438 ret = rte_kvargs_process(kvlist, PMD_PARAM_CPU_ID,
439 &get_uint32, &p->cpu_id);
444 /* TM number of queues (optional) */
445 if (rte_kvargs_count(kvlist, PMD_PARAM_TM_N_QUEUES) == 1) {
446 ret = rte_kvargs_process(kvlist, PMD_PARAM_TM_N_QUEUES,
447 &get_uint32, &p->tm.n_queues);
452 /* TM queue size 0 .. 3 (optional) */
453 if (rte_kvargs_count(kvlist, PMD_PARAM_TM_QSIZE0) == 1) {
454 ret = rte_kvargs_process(kvlist, PMD_PARAM_TM_QSIZE0,
455 &get_uint32, &p->tm.qsize[0]);
460 if (rte_kvargs_count(kvlist, PMD_PARAM_TM_QSIZE1) == 1) {
461 ret = rte_kvargs_process(kvlist, PMD_PARAM_TM_QSIZE1,
462 &get_uint32, &p->tm.qsize[1]);
467 if (rte_kvargs_count(kvlist, PMD_PARAM_TM_QSIZE2) == 1) {
468 ret = rte_kvargs_process(kvlist, PMD_PARAM_TM_QSIZE2,
469 &get_uint32, &p->tm.qsize[2]);
474 if (rte_kvargs_count(kvlist, PMD_PARAM_TM_QSIZE3) == 1) {
475 ret = rte_kvargs_process(kvlist, PMD_PARAM_TM_QSIZE3,
476 &get_uint32, &p->tm.qsize[3]);
482 rte_kvargs_free(kvlist);
487 pmd_probe(struct rte_vdev_device *vdev)
494 const char *name = rte_vdev_device_name(vdev);
496 PMD_LOG(INFO, "Probing device \"%s\"", name);
498 /* Parse input arguments */
499 params = rte_vdev_device_args(vdev);
503 status = pmd_parse_args(&p, params);
509 /* Allocate and initialize soft ethdev private data */
510 dev_private = pmd_init(&p);
511 if (dev_private == NULL)
514 /* Register soft ethdev */
515 PMD_LOG(INFO, "Creating soft ethdev \"%s\"", p.name);
517 status = pmd_ethdev_register(vdev, &p, dev_private);
519 pmd_free(dev_private);
527 pmd_remove(struct rte_vdev_device *vdev)
529 struct rte_eth_dev *dev = NULL;
530 struct pmd_internals *p;
535 PMD_LOG(INFO, "Removing device \"%s\"", rte_vdev_device_name(vdev));
537 /* Find the ethdev entry */
538 dev = rte_eth_dev_allocated(rte_vdev_device_name(vdev));
541 p = dev->data->dev_private;
543 /* Free device data structures*/
545 rte_eth_dev_release_port(dev);
551 static struct rte_vdev_driver pmd_softnic_drv = {
553 .remove = pmd_remove,
556 RTE_PMD_REGISTER_VDEV(net_softnic, pmd_softnic_drv);
557 RTE_PMD_REGISTER_PARAM_STRING(net_softnic,
558 PMD_PARAM_FIRMWARE "=<string> "
559 PMD_PARAM_CONN_PORT "=<uint16> "
560 PMD_PARAM_CPU_ID "=<uint32> "
561 PMD_PARAM_TM_N_QUEUES "=<uint32> "
562 PMD_PARAM_TM_QSIZE0 "=<uint32> "
563 PMD_PARAM_TM_QSIZE1 "=<uint32> "
564 PMD_PARAM_TM_QSIZE2 "=<uint32> "
565 PMD_PARAM_TM_QSIZE3 "=<uint32>"
569 RTE_INIT(pmd_softnic_init_log)
571 pmd_softnic_logtype = rte_log_register("pmd.net.softnic");
572 if (pmd_softnic_logtype >= 0)
573 rte_log_set_level(pmd_softnic_logtype, RTE_LOG_NOTICE);
577 rte_pmd_softnic_manage(uint16_t port_id)
579 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
580 struct pmd_internals *softnic;
582 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
583 RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, 0);
586 softnic = dev->data->dev_private;
588 softnic_conn_poll_for_conn(softnic->conn);
590 softnic_conn_poll_for_msg(softnic->conn);