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_sched.h>
17 #include <rte_tm_driver.h>
19 #include "rte_eth_softnic.h"
20 #include "rte_eth_softnic_internals.h"
23 (&rte_eth_devices[p->hard.port_id])
25 #define PMD_PARAM_SOFT_TM "soft_tm"
26 #define PMD_PARAM_SOFT_TM_RATE "soft_tm_rate"
27 #define PMD_PARAM_SOFT_TM_NB_QUEUES "soft_tm_nb_queues"
28 #define PMD_PARAM_SOFT_TM_QSIZE0 "soft_tm_qsize0"
29 #define PMD_PARAM_SOFT_TM_QSIZE1 "soft_tm_qsize1"
30 #define PMD_PARAM_SOFT_TM_QSIZE2 "soft_tm_qsize2"
31 #define PMD_PARAM_SOFT_TM_QSIZE3 "soft_tm_qsize3"
32 #define PMD_PARAM_SOFT_TM_ENQ_BSZ "soft_tm_enq_bsz"
33 #define PMD_PARAM_SOFT_TM_DEQ_BSZ "soft_tm_deq_bsz"
35 #define PMD_PARAM_HARD_NAME "hard_name"
36 #define PMD_PARAM_HARD_TX_QUEUE_ID "hard_tx_queue_id"
38 static const char *pmd_valid_args[] = {
40 PMD_PARAM_SOFT_TM_RATE,
41 PMD_PARAM_SOFT_TM_NB_QUEUES,
42 PMD_PARAM_SOFT_TM_QSIZE0,
43 PMD_PARAM_SOFT_TM_QSIZE1,
44 PMD_PARAM_SOFT_TM_QSIZE2,
45 PMD_PARAM_SOFT_TM_QSIZE3,
46 PMD_PARAM_SOFT_TM_ENQ_BSZ,
47 PMD_PARAM_SOFT_TM_DEQ_BSZ,
49 PMD_PARAM_HARD_TX_QUEUE_ID,
53 static const struct rte_eth_dev_info pmd_dev_info = {
55 .max_rx_pktlen = UINT32_MAX,
56 .max_rx_queues = UINT16_MAX,
57 .max_tx_queues = UINT16_MAX,
70 static int pmd_softnic_logtype;
72 #define PMD_LOG(level, fmt, args...) \
73 rte_log(RTE_LOG_ ## level, pmd_softnic_logtype, \
74 "%s(): " fmt "\n", __func__, ##args)
77 pmd_dev_infos_get(struct rte_eth_dev *dev __rte_unused,
78 struct rte_eth_dev_info *dev_info)
80 memcpy(dev_info, &pmd_dev_info, sizeof(*dev_info));
84 pmd_dev_configure(struct rte_eth_dev *dev)
86 struct pmd_internals *p = dev->data->dev_private;
87 struct rte_eth_dev *hard_dev = DEV_HARD(p);
89 if (dev->data->nb_rx_queues > hard_dev->data->nb_rx_queues)
92 if (p->params.hard.tx_queue_id >= hard_dev->data->nb_tx_queues)
99 pmd_rx_queue_setup(struct rte_eth_dev *dev,
100 uint16_t rx_queue_id,
101 uint16_t nb_rx_desc __rte_unused,
102 unsigned int socket_id,
103 const struct rte_eth_rxconf *rx_conf __rte_unused,
104 struct rte_mempool *mb_pool __rte_unused)
106 struct pmd_internals *p = dev->data->dev_private;
108 if (p->params.soft.intrusive == 0) {
109 struct pmd_rx_queue *rxq;
111 rxq = rte_zmalloc_socket(p->params.soft.name,
112 sizeof(struct pmd_rx_queue), 0, socket_id);
116 rxq->hard.port_id = p->hard.port_id;
117 rxq->hard.rx_queue_id = rx_queue_id;
118 dev->data->rx_queues[rx_queue_id] = rxq;
120 struct rte_eth_dev *hard_dev = DEV_HARD(p);
121 void *rxq = hard_dev->data->rx_queues[rx_queue_id];
126 dev->data->rx_queues[rx_queue_id] = rxq;
132 pmd_tx_queue_setup(struct rte_eth_dev *dev,
133 uint16_t tx_queue_id,
135 unsigned int socket_id,
136 const struct rte_eth_txconf *tx_conf __rte_unused)
138 uint32_t size = RTE_ETH_NAME_MAX_LEN + strlen("_txq") + 4;
142 snprintf(name, sizeof(name), "%s_txq%04x",
143 dev->data->name, tx_queue_id);
144 r = rte_ring_create(name, nb_tx_desc, socket_id,
145 RING_F_SP_ENQ | RING_F_SC_DEQ);
149 dev->data->tx_queues[tx_queue_id] = r;
154 pmd_dev_start(struct rte_eth_dev *dev)
156 struct pmd_internals *p = dev->data->dev_private;
159 int status = tm_start(p);
165 dev->data->dev_link.link_status = ETH_LINK_UP;
167 if (p->params.soft.intrusive) {
168 struct rte_eth_dev *hard_dev = DEV_HARD(p);
170 /* The hard_dev->rx_pkt_burst should be stable by now */
171 dev->rx_pkt_burst = hard_dev->rx_pkt_burst;
178 pmd_dev_stop(struct rte_eth_dev *dev)
180 struct pmd_internals *p = dev->data->dev_private;
182 dev->data->dev_link.link_status = ETH_LINK_DOWN;
189 pmd_dev_close(struct rte_eth_dev *dev)
194 for (i = 0; i < dev->data->nb_tx_queues; i++)
195 rte_ring_free((struct rte_ring *)dev->data->tx_queues[i]);
199 pmd_link_update(struct rte_eth_dev *dev __rte_unused,
200 int wait_to_complete __rte_unused)
206 pmd_tm_ops_get(struct rte_eth_dev *dev, void *arg)
208 *(const struct rte_tm_ops **)arg =
209 (tm_enabled(dev)) ? &pmd_tm_ops : NULL;
214 static const struct eth_dev_ops pmd_ops = {
215 .dev_configure = pmd_dev_configure,
216 .dev_start = pmd_dev_start,
217 .dev_stop = pmd_dev_stop,
218 .dev_close = pmd_dev_close,
219 .link_update = pmd_link_update,
220 .dev_infos_get = pmd_dev_infos_get,
221 .rx_queue_setup = pmd_rx_queue_setup,
222 .tx_queue_setup = pmd_tx_queue_setup,
223 .tm_ops_get = pmd_tm_ops_get,
227 pmd_rx_pkt_burst(void *rxq,
228 struct rte_mbuf **rx_pkts,
231 struct pmd_rx_queue *rx_queue = rxq;
233 return rte_eth_rx_burst(rx_queue->hard.port_id,
234 rx_queue->hard.rx_queue_id,
240 pmd_tx_pkt_burst(void *txq,
241 struct rte_mbuf **tx_pkts,
244 return (uint16_t)rte_ring_enqueue_burst(txq,
250 static __rte_always_inline int
251 run_default(struct rte_eth_dev *dev)
253 struct pmd_internals *p = dev->data->dev_private;
255 /* Persistent context: Read Only (update not required) */
256 struct rte_mbuf **pkts = p->soft.def.pkts;
257 uint16_t nb_tx_queues = dev->data->nb_tx_queues;
259 /* Persistent context: Read - Write (update required) */
260 uint32_t txq_pos = p->soft.def.txq_pos;
261 uint32_t pkts_len = p->soft.def.pkts_len;
262 uint32_t flush_count = p->soft.def.flush_count;
264 /* Not part of the persistent context */
268 /* Soft device TXQ read, Hard device TXQ write */
269 for (i = 0; i < nb_tx_queues; i++) {
270 struct rte_ring *txq = dev->data->tx_queues[txq_pos];
272 /* Read soft device TXQ burst to packet enqueue buffer */
273 pkts_len += rte_ring_sc_dequeue_burst(txq,
274 (void **)&pkts[pkts_len],
278 /* Increment soft device TXQ */
280 if (txq_pos >= nb_tx_queues)
283 /* Hard device TXQ write when complete burst is available */
284 if (pkts_len >= DEFAULT_BURST_SIZE) {
285 for (pos = 0; pos < pkts_len; )
286 pos += rte_eth_tx_burst(p->hard.port_id,
287 p->params.hard.tx_queue_id,
289 (uint16_t)(pkts_len - pos));
297 if (flush_count >= FLUSH_COUNT_THRESHOLD) {
298 for (pos = 0; pos < pkts_len; )
299 pos += rte_eth_tx_burst(p->hard.port_id,
300 p->params.hard.tx_queue_id,
302 (uint16_t)(pkts_len - pos));
308 p->soft.def.txq_pos = txq_pos;
309 p->soft.def.pkts_len = pkts_len;
310 p->soft.def.flush_count = flush_count + 1;
315 static __rte_always_inline int
316 run_tm(struct rte_eth_dev *dev)
318 struct pmd_internals *p = dev->data->dev_private;
320 /* Persistent context: Read Only (update not required) */
321 struct rte_sched_port *sched = p->soft.tm.sched;
322 struct rte_mbuf **pkts_enq = p->soft.tm.pkts_enq;
323 struct rte_mbuf **pkts_deq = p->soft.tm.pkts_deq;
324 uint32_t enq_bsz = p->params.soft.tm.enq_bsz;
325 uint32_t deq_bsz = p->params.soft.tm.deq_bsz;
326 uint16_t nb_tx_queues = dev->data->nb_tx_queues;
328 /* Persistent context: Read - Write (update required) */
329 uint32_t txq_pos = p->soft.tm.txq_pos;
330 uint32_t pkts_enq_len = p->soft.tm.pkts_enq_len;
331 uint32_t flush_count = p->soft.tm.flush_count;
333 /* Not part of the persistent context */
334 uint32_t pkts_deq_len, pos;
337 /* Soft device TXQ read, TM enqueue */
338 for (i = 0; i < nb_tx_queues; i++) {
339 struct rte_ring *txq = dev->data->tx_queues[txq_pos];
341 /* Read TXQ burst to packet enqueue buffer */
342 pkts_enq_len += rte_ring_sc_dequeue_burst(txq,
343 (void **)&pkts_enq[pkts_enq_len],
349 if (txq_pos >= nb_tx_queues)
352 /* TM enqueue when complete burst is available */
353 if (pkts_enq_len >= enq_bsz) {
354 rte_sched_port_enqueue(sched, pkts_enq, pkts_enq_len);
362 if (flush_count >= FLUSH_COUNT_THRESHOLD) {
364 rte_sched_port_enqueue(sched, pkts_enq, pkts_enq_len);
370 p->soft.tm.txq_pos = txq_pos;
371 p->soft.tm.pkts_enq_len = pkts_enq_len;
372 p->soft.tm.flush_count = flush_count + 1;
374 /* TM dequeue, Hard device TXQ write */
375 pkts_deq_len = rte_sched_port_dequeue(sched, pkts_deq, deq_bsz);
377 for (pos = 0; pos < pkts_deq_len; )
378 pos += rte_eth_tx_burst(p->hard.port_id,
379 p->params.hard.tx_queue_id,
381 (uint16_t)(pkts_deq_len - pos));
387 rte_pmd_softnic_run(uint16_t port_id)
389 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
391 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
392 RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, 0);
395 return (tm_used(dev)) ? run_tm(dev) : run_default(dev);
398 static struct ether_addr eth_addr = { .addr_bytes = {0} };
401 eth_dev_speed_max_mbps(uint32_t speed_capa)
403 uint32_t rate_mbps[32] = {
421 uint32_t pos = (speed_capa) ? (31 - __builtin_clz(speed_capa)) : 0;
422 return rate_mbps[pos];
426 default_init(struct pmd_internals *p,
427 struct pmd_params *params,
430 p->soft.def.pkts = rte_zmalloc_socket(params->soft.name,
431 2 * DEFAULT_BURST_SIZE * sizeof(struct rte_mbuf *),
435 if (p->soft.def.pkts == NULL)
442 default_free(struct pmd_internals *p)
444 rte_free(p->soft.def.pkts);
448 pmd_init(struct pmd_params *params, int numa_node)
450 struct pmd_internals *p;
453 p = rte_zmalloc_socket(params->soft.name,
454 sizeof(struct pmd_internals),
460 memcpy(&p->params, params, sizeof(p->params));
461 rte_eth_dev_get_port_by_name(params->hard.name, &p->hard.port_id);
464 status = default_init(p, params, numa_node);
466 free(p->params.hard.name);
471 /* Traffic Management (TM)*/
472 if (params->soft.flags & PMD_FEATURE_TM) {
473 status = tm_init(p, params, numa_node);
476 free(p->params.hard.name);
486 pmd_free(struct pmd_internals *p)
488 if (p->params.soft.flags & PMD_FEATURE_TM)
493 free(p->params.hard.name);
498 pmd_ethdev_register(struct rte_vdev_device *vdev,
499 struct pmd_params *params,
502 struct rte_eth_dev_info hard_info;
503 struct rte_eth_dev *soft_dev;
506 uint16_t hard_port_id;
508 rte_eth_dev_get_port_by_name(params->hard.name, &hard_port_id);
509 rte_eth_dev_info_get(hard_port_id, &hard_info);
510 hard_speed = eth_dev_speed_max_mbps(hard_info.speed_capa);
511 numa_node = rte_eth_dev_socket_id(hard_port_id);
513 /* Ethdev entry allocation */
514 soft_dev = rte_eth_dev_allocate(params->soft.name);
519 soft_dev->rx_pkt_burst = (params->soft.intrusive) ?
520 NULL : /* set up later */
522 soft_dev->tx_pkt_burst = pmd_tx_pkt_burst;
523 soft_dev->tx_pkt_prepare = NULL;
524 soft_dev->dev_ops = &pmd_ops;
525 soft_dev->device = &vdev->device;
528 soft_dev->data->dev_private = dev_private;
529 soft_dev->data->dev_link.link_speed = hard_speed;
530 soft_dev->data->dev_link.link_duplex = ETH_LINK_FULL_DUPLEX;
531 soft_dev->data->dev_link.link_autoneg = ETH_LINK_FIXED;
532 soft_dev->data->dev_link.link_status = ETH_LINK_DOWN;
533 soft_dev->data->mac_addrs = ð_addr;
534 soft_dev->data->promiscuous = 1;
535 soft_dev->data->kdrv = RTE_KDRV_NONE;
536 soft_dev->data->numa_node = numa_node;
538 rte_eth_dev_probing_finish(soft_dev);
544 get_string(const char *key __rte_unused, const char *value, void *extra_args)
546 if (!value || !extra_args)
549 *(char **)extra_args = strdup(value);
551 if (!*(char **)extra_args)
558 get_uint32(const char *key __rte_unused, const char *value, void *extra_args)
560 if (!value || !extra_args)
563 *(uint32_t *)extra_args = strtoull(value, NULL, 0);
569 pmd_parse_args(struct pmd_params *p, const char *name, const char *params)
571 struct rte_kvargs *kvlist;
574 kvlist = rte_kvargs_parse(params, pmd_valid_args);
578 /* Set default values */
579 memset(p, 0, sizeof(*p));
581 p->soft.intrusive = INTRUSIVE;
583 p->soft.tm.nb_queues = SOFTNIC_SOFT_TM_NB_QUEUES;
584 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
585 p->soft.tm.qsize[i] = SOFTNIC_SOFT_TM_QUEUE_SIZE;
586 p->soft.tm.enq_bsz = SOFTNIC_SOFT_TM_ENQ_BSZ;
587 p->soft.tm.deq_bsz = SOFTNIC_SOFT_TM_DEQ_BSZ;
588 p->hard.tx_queue_id = SOFTNIC_HARD_TX_QUEUE_ID;
590 /* SOFT: TM (optional) */
591 if (rte_kvargs_count(kvlist, PMD_PARAM_SOFT_TM) == 1) {
594 ret = rte_kvargs_process(kvlist, PMD_PARAM_SOFT_TM,
599 if (strcmp(s, "on") == 0)
600 p->soft.flags |= PMD_FEATURE_TM;
601 else if (strcmp(s, "off") == 0)
602 p->soft.flags &= ~PMD_FEATURE_TM;
611 /* SOFT: TM rate (measured in bytes/second) (optional) */
612 if (rte_kvargs_count(kvlist, PMD_PARAM_SOFT_TM_RATE) == 1) {
613 ret = rte_kvargs_process(kvlist, PMD_PARAM_SOFT_TM_RATE,
614 &get_uint32, &p->soft.tm.rate);
618 p->soft.flags |= PMD_FEATURE_TM;
621 /* SOFT: TM number of queues (optional) */
622 if (rte_kvargs_count(kvlist, PMD_PARAM_SOFT_TM_NB_QUEUES) == 1) {
623 ret = rte_kvargs_process(kvlist, PMD_PARAM_SOFT_TM_NB_QUEUES,
624 &get_uint32, &p->soft.tm.nb_queues);
628 p->soft.flags |= PMD_FEATURE_TM;
631 /* SOFT: TM queue size 0 .. 3 (optional) */
632 if (rte_kvargs_count(kvlist, PMD_PARAM_SOFT_TM_QSIZE0) == 1) {
635 ret = rte_kvargs_process(kvlist, PMD_PARAM_SOFT_TM_QSIZE0,
636 &get_uint32, &qsize);
640 p->soft.tm.qsize[0] = (uint16_t)qsize;
641 p->soft.flags |= PMD_FEATURE_TM;
644 if (rte_kvargs_count(kvlist, PMD_PARAM_SOFT_TM_QSIZE1) == 1) {
647 ret = rte_kvargs_process(kvlist, PMD_PARAM_SOFT_TM_QSIZE1,
648 &get_uint32, &qsize);
652 p->soft.tm.qsize[1] = (uint16_t)qsize;
653 p->soft.flags |= PMD_FEATURE_TM;
656 if (rte_kvargs_count(kvlist, PMD_PARAM_SOFT_TM_QSIZE2) == 1) {
659 ret = rte_kvargs_process(kvlist, PMD_PARAM_SOFT_TM_QSIZE2,
660 &get_uint32, &qsize);
664 p->soft.tm.qsize[2] = (uint16_t)qsize;
665 p->soft.flags |= PMD_FEATURE_TM;
668 if (rte_kvargs_count(kvlist, PMD_PARAM_SOFT_TM_QSIZE3) == 1) {
671 ret = rte_kvargs_process(kvlist, PMD_PARAM_SOFT_TM_QSIZE3,
672 &get_uint32, &qsize);
676 p->soft.tm.qsize[3] = (uint16_t)qsize;
677 p->soft.flags |= PMD_FEATURE_TM;
680 /* SOFT: TM enqueue burst size (optional) */
681 if (rte_kvargs_count(kvlist, PMD_PARAM_SOFT_TM_ENQ_BSZ) == 1) {
682 ret = rte_kvargs_process(kvlist, PMD_PARAM_SOFT_TM_ENQ_BSZ,
683 &get_uint32, &p->soft.tm.enq_bsz);
687 p->soft.flags |= PMD_FEATURE_TM;
690 /* SOFT: TM dequeue burst size (optional) */
691 if (rte_kvargs_count(kvlist, PMD_PARAM_SOFT_TM_DEQ_BSZ) == 1) {
692 ret = rte_kvargs_process(kvlist, PMD_PARAM_SOFT_TM_DEQ_BSZ,
693 &get_uint32, &p->soft.tm.deq_bsz);
697 p->soft.flags |= PMD_FEATURE_TM;
700 /* HARD: name (mandatory) */
701 if (rte_kvargs_count(kvlist, PMD_PARAM_HARD_NAME) == 1) {
702 ret = rte_kvargs_process(kvlist, PMD_PARAM_HARD_NAME,
703 &get_string, &p->hard.name);
711 /* HARD: tx_queue_id (optional) */
712 if (rte_kvargs_count(kvlist, PMD_PARAM_HARD_TX_QUEUE_ID) == 1) {
713 ret = rte_kvargs_process(kvlist, PMD_PARAM_HARD_TX_QUEUE_ID,
714 &get_uint32, &p->hard.tx_queue_id);
720 rte_kvargs_free(kvlist);
725 pmd_probe(struct rte_vdev_device *vdev)
731 struct rte_eth_dev_info hard_info;
733 uint16_t hard_port_id;
736 struct rte_eth_dev *eth_dev;
737 const char *name = rte_vdev_device_name(vdev);
739 PMD_LOG(INFO, "Probing device \"%s\"", name);
741 /* Parse input arguments */
742 params = rte_vdev_device_args(vdev);
744 if (rte_eal_process_type() == RTE_PROC_SECONDARY &&
745 strlen(params) == 0) {
746 eth_dev = rte_eth_dev_attach_secondary(name);
748 PMD_LOG(ERR, "Failed to probe %s", name);
751 /* TODO: request info from primary to set up Rx and Tx */
752 eth_dev->dev_ops = &pmd_ops;
753 rte_eth_dev_probing_finish(eth_dev);
760 status = pmd_parse_args(&p, rte_vdev_device_name(vdev), params);
764 /* Check input arguments */
765 if (rte_eth_dev_get_port_by_name(p.hard.name, &hard_port_id))
768 rte_eth_dev_info_get(hard_port_id, &hard_info);
769 hard_speed = eth_dev_speed_max_mbps(hard_info.speed_capa);
770 numa_node = rte_eth_dev_socket_id(hard_port_id);
772 if (p.hard.tx_queue_id >= hard_info.max_tx_queues)
775 if (p.soft.flags & PMD_FEATURE_TM) {
776 status = tm_params_check(&p, hard_speed);
782 /* Allocate and initialize soft ethdev private data */
783 dev_private = pmd_init(&p, numa_node);
784 if (dev_private == NULL)
787 /* Register soft ethdev */
789 "Creating soft ethdev \"%s\" for hard ethdev \"%s\"",
790 p.soft.name, p.hard.name);
792 status = pmd_ethdev_register(vdev, &p, dev_private);
794 pmd_free(dev_private);
802 pmd_remove(struct rte_vdev_device *vdev)
804 struct rte_eth_dev *dev = NULL;
805 struct pmd_internals *p;
810 PMD_LOG(INFO, "Removing device \"%s\"",
811 rte_vdev_device_name(vdev));
813 /* Find the ethdev entry */
814 dev = rte_eth_dev_allocated(rte_vdev_device_name(vdev));
817 p = dev->data->dev_private;
819 /* Free device data structures*/
822 rte_eth_dev_release_port(dev);
827 static struct rte_vdev_driver pmd_softnic_drv = {
829 .remove = pmd_remove,
832 RTE_PMD_REGISTER_VDEV(net_softnic, pmd_softnic_drv);
833 RTE_PMD_REGISTER_PARAM_STRING(net_softnic,
834 PMD_PARAM_SOFT_TM "=on|off "
835 PMD_PARAM_SOFT_TM_RATE "=<int> "
836 PMD_PARAM_SOFT_TM_NB_QUEUES "=<int> "
837 PMD_PARAM_SOFT_TM_QSIZE0 "=<int> "
838 PMD_PARAM_SOFT_TM_QSIZE1 "=<int> "
839 PMD_PARAM_SOFT_TM_QSIZE2 "=<int> "
840 PMD_PARAM_SOFT_TM_QSIZE3 "=<int> "
841 PMD_PARAM_SOFT_TM_ENQ_BSZ "=<int> "
842 PMD_PARAM_SOFT_TM_DEQ_BSZ "=<int> "
843 PMD_PARAM_HARD_NAME "=<string> "
844 PMD_PARAM_HARD_TX_QUEUE_ID "=<int>");
846 RTE_INIT(pmd_softnic_init_log);
848 pmd_softnic_init_log(void)
850 pmd_softnic_logtype = rte_log_register("pmd.net.softnic");
851 if (pmd_softnic_logtype >= 0)
852 rte_log_set_level(pmd_softnic_logtype, RTE_LOG_NOTICE);