4 * Copyright (C) IGEL Co.,Ltd.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of IGEL Co.,Ltd. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <rte_ethdev.h>
36 #include <rte_malloc.h>
37 #include <rte_memcpy.h>
39 #include <rte_kvargs.h>
40 #include <rte_spinlock.h>
42 #include "rte_eth_null.h"
44 #define ETH_NULL_PACKET_SIZE_ARG "size"
45 #define ETH_NULL_PACKET_COPY_ARG "copy"
47 static unsigned default_packet_size = 64;
48 static unsigned default_packet_copy;
50 static const char *valid_arguments[] = {
51 ETH_NULL_PACKET_SIZE_ARG,
52 ETH_NULL_PACKET_COPY_ARG,
59 struct pmd_internals *internals;
61 struct rte_mempool *mb_pool;
62 struct rte_mbuf *dummy_packet;
64 rte_atomic64_t rx_pkts;
65 rte_atomic64_t tx_pkts;
66 rte_atomic64_t err_pkts;
69 struct pmd_internals {
74 unsigned nb_rx_queues;
75 unsigned nb_tx_queues;
77 struct null_queue rx_null_queues[RTE_MAX_QUEUES_PER_PORT];
78 struct null_queue tx_null_queues[RTE_MAX_QUEUES_PER_PORT];
80 /** Bit mask of RSS offloads, the bit offset also means flow type */
81 uint64_t flow_type_rss_offloads;
83 rte_spinlock_t rss_lock;
86 struct rte_eth_rss_reta_entry64 reta_conf[ETH_RSS_RETA_SIZE_128 /
89 uint8_t rss_key[40]; /**< 40-byte hash key. */
93 static struct ether_addr eth_addr = { .addr_bytes = {0} };
94 static const char *drivername = "Null PMD";
95 static struct rte_eth_link pmd_link = {
97 .link_duplex = ETH_LINK_FULL_DUPLEX,
102 eth_null_rx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
105 struct null_queue *h = q;
106 unsigned packet_size;
108 if ((q == NULL) || (bufs == NULL))
111 packet_size = h->internals->packet_size;
112 for (i = 0; i < nb_bufs; i++) {
113 bufs[i] = rte_pktmbuf_alloc(h->mb_pool);
116 bufs[i]->data_len = (uint16_t)packet_size;
117 bufs[i]->pkt_len = packet_size;
118 bufs[i]->nb_segs = 1;
119 bufs[i]->next = NULL;
122 rte_atomic64_add(&(h->rx_pkts), i);
128 eth_null_copy_rx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
131 struct null_queue *h = q;
132 unsigned packet_size;
134 if ((q == NULL) || (bufs == NULL))
137 packet_size = h->internals->packet_size;
138 for (i = 0; i < nb_bufs; i++) {
139 bufs[i] = rte_pktmbuf_alloc(h->mb_pool);
142 rte_memcpy(rte_pktmbuf_mtod(bufs[i], void *), h->dummy_packet,
144 bufs[i]->data_len = (uint16_t)packet_size;
145 bufs[i]->pkt_len = packet_size;
146 bufs[i]->nb_segs = 1;
147 bufs[i]->next = NULL;
150 rte_atomic64_add(&(h->rx_pkts), i);
156 eth_null_tx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
159 struct null_queue *h = q;
161 if ((q == NULL) || (bufs == NULL))
164 for (i = 0; i < nb_bufs; i++)
165 rte_pktmbuf_free(bufs[i]);
167 rte_atomic64_add(&(h->tx_pkts), i);
173 eth_null_copy_tx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
176 struct null_queue *h = q;
177 unsigned packet_size;
179 if ((q == NULL) || (bufs == NULL))
182 packet_size = h->internals->packet_size;
183 for (i = 0; i < nb_bufs; i++) {
184 rte_memcpy(h->dummy_packet, rte_pktmbuf_mtod(bufs[i], void *),
186 rte_pktmbuf_free(bufs[i]);
189 rte_atomic64_add(&(h->tx_pkts), i);
195 eth_dev_configure(struct rte_eth_dev *dev) {
196 struct pmd_internals *internals;
198 internals = dev->data->dev_private;
199 internals->nb_rx_queues = dev->data->nb_rx_queues;
200 internals->nb_tx_queues = dev->data->nb_tx_queues;
206 eth_dev_start(struct rte_eth_dev *dev)
211 dev->data->dev_link.link_status = 1;
216 eth_dev_stop(struct rte_eth_dev *dev)
221 dev->data->dev_link.link_status = 0;
225 eth_rx_queue_setup(struct rte_eth_dev *dev, uint16_t rx_queue_id,
226 uint16_t nb_rx_desc __rte_unused,
227 unsigned int socket_id __rte_unused,
228 const struct rte_eth_rxconf *rx_conf __rte_unused,
229 struct rte_mempool *mb_pool)
231 struct rte_mbuf *dummy_packet;
232 struct pmd_internals *internals;
233 unsigned packet_size;
235 if ((dev == NULL) || (mb_pool == NULL))
238 internals = dev->data->dev_private;
240 if (rx_queue_id >= internals->nb_rx_queues)
243 packet_size = internals->packet_size;
245 internals->rx_null_queues[rx_queue_id].mb_pool = mb_pool;
246 dev->data->rx_queues[rx_queue_id] =
247 &internals->rx_null_queues[rx_queue_id];
248 dummy_packet = rte_zmalloc_socket(NULL,
249 packet_size, 0, internals->numa_node);
250 if (dummy_packet == NULL)
253 internals->rx_null_queues[rx_queue_id].internals = internals;
254 internals->rx_null_queues[rx_queue_id].dummy_packet = dummy_packet;
260 eth_tx_queue_setup(struct rte_eth_dev *dev, uint16_t tx_queue_id,
261 uint16_t nb_tx_desc __rte_unused,
262 unsigned int socket_id __rte_unused,
263 const struct rte_eth_txconf *tx_conf __rte_unused)
265 struct rte_mbuf *dummy_packet;
266 struct pmd_internals *internals;
267 unsigned packet_size;
272 internals = dev->data->dev_private;
274 if (tx_queue_id >= internals->nb_tx_queues)
277 packet_size = internals->packet_size;
279 dev->data->tx_queues[tx_queue_id] =
280 &internals->tx_null_queues[tx_queue_id];
281 dummy_packet = rte_zmalloc_socket(NULL,
282 packet_size, 0, internals->numa_node);
283 if (dummy_packet == NULL)
286 internals->tx_null_queues[tx_queue_id].internals = internals;
287 internals->tx_null_queues[tx_queue_id].dummy_packet = dummy_packet;
294 eth_dev_info(struct rte_eth_dev *dev,
295 struct rte_eth_dev_info *dev_info)
297 struct pmd_internals *internals;
299 if ((dev == NULL) || (dev_info == NULL))
302 internals = dev->data->dev_private;
303 dev_info->driver_name = drivername;
304 dev_info->max_mac_addrs = 1;
305 dev_info->max_rx_pktlen = (uint32_t)-1;
306 dev_info->max_rx_queues = RTE_DIM(internals->rx_null_queues);
307 dev_info->max_tx_queues = RTE_DIM(internals->tx_null_queues);
308 dev_info->min_rx_bufsize = 0;
309 dev_info->pci_dev = NULL;
310 dev_info->reta_size = internals->reta_size;
311 dev_info->flow_type_rss_offloads = internals->flow_type_rss_offloads;
315 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *igb_stats)
317 unsigned i, num_stats;
318 unsigned long rx_total = 0, tx_total = 0, tx_err_total = 0;
319 const struct pmd_internals *internal;
321 if ((dev == NULL) || (igb_stats == NULL))
324 internal = dev->data->dev_private;
325 num_stats = RTE_MIN((unsigned)RTE_ETHDEV_QUEUE_STAT_CNTRS,
326 RTE_MIN(internal->nb_rx_queues,
327 RTE_DIM(internal->rx_null_queues)));
328 for (i = 0; i < num_stats; i++) {
329 igb_stats->q_ipackets[i] =
330 internal->rx_null_queues[i].rx_pkts.cnt;
331 rx_total += igb_stats->q_ipackets[i];
334 num_stats = RTE_MIN((unsigned)RTE_ETHDEV_QUEUE_STAT_CNTRS,
335 RTE_MIN(internal->nb_tx_queues,
336 RTE_DIM(internal->tx_null_queues)));
337 for (i = 0; i < num_stats; i++) {
338 igb_stats->q_opackets[i] =
339 internal->tx_null_queues[i].tx_pkts.cnt;
340 igb_stats->q_errors[i] =
341 internal->tx_null_queues[i].err_pkts.cnt;
342 tx_total += igb_stats->q_opackets[i];
343 tx_err_total += igb_stats->q_errors[i];
346 igb_stats->ipackets = rx_total;
347 igb_stats->opackets = tx_total;
348 igb_stats->oerrors = tx_err_total;
352 eth_stats_reset(struct rte_eth_dev *dev)
355 struct pmd_internals *internal;
360 internal = dev->data->dev_private;
361 for (i = 0; i < RTE_DIM(internal->rx_null_queues); i++)
362 internal->rx_null_queues[i].rx_pkts.cnt = 0;
363 for (i = 0; i < RTE_DIM(internal->tx_null_queues); i++) {
364 internal->tx_null_queues[i].tx_pkts.cnt = 0;
365 internal->tx_null_queues[i].err_pkts.cnt = 0;
370 eth_queue_release(void *q)
372 struct null_queue *nq;
378 rte_free(nq->dummy_packet);
382 eth_link_update(struct rte_eth_dev *dev __rte_unused,
383 int wait_to_complete __rte_unused) { return 0; }
386 eth_rss_reta_update(struct rte_eth_dev *dev,
387 struct rte_eth_rss_reta_entry64 *reta_conf, uint16_t reta_size)
390 struct pmd_internals *internal = dev->data->dev_private;
392 if (reta_size != internal->reta_size)
395 rte_spinlock_lock(&internal->rss_lock);
397 /* Copy RETA table */
398 for (i = 0; i < (internal->reta_size / RTE_RETA_GROUP_SIZE); i++) {
399 internal->reta_conf[i].mask = reta_conf[i].mask;
400 for (j = 0; j < RTE_RETA_GROUP_SIZE; j++)
401 if ((reta_conf[i].mask >> j) & 0x01)
402 internal->reta_conf[i].reta[j] = reta_conf[i].reta[j];
405 rte_spinlock_unlock(&internal->rss_lock);
411 eth_rss_reta_query(struct rte_eth_dev *dev,
412 struct rte_eth_rss_reta_entry64 *reta_conf, uint16_t reta_size)
415 struct pmd_internals *internal = dev->data->dev_private;
417 if (reta_size != internal->reta_size)
420 rte_spinlock_lock(&internal->rss_lock);
422 /* Copy RETA table */
423 for (i = 0; i < (internal->reta_size / RTE_RETA_GROUP_SIZE); i++) {
424 for (j = 0; j < RTE_RETA_GROUP_SIZE; j++)
425 if ((reta_conf[i].mask >> j) & 0x01)
426 reta_conf[i].reta[j] = internal->reta_conf[i].reta[j];
429 rte_spinlock_unlock(&internal->rss_lock);
435 eth_rss_hash_update(struct rte_eth_dev *dev, struct rte_eth_rss_conf *rss_conf)
437 struct pmd_internals *internal = dev->data->dev_private;
439 rte_spinlock_lock(&internal->rss_lock);
441 if ((rss_conf->rss_hf & internal->flow_type_rss_offloads) != 0)
442 dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf =
443 rss_conf->rss_hf & internal->flow_type_rss_offloads;
445 if (rss_conf->rss_key)
446 rte_memcpy(internal->rss_key, rss_conf->rss_key, 40);
448 rte_spinlock_unlock(&internal->rss_lock);
454 eth_rss_hash_conf_get(struct rte_eth_dev *dev,
455 struct rte_eth_rss_conf *rss_conf)
457 struct pmd_internals *internal = dev->data->dev_private;
459 rte_spinlock_lock(&internal->rss_lock);
461 rss_conf->rss_hf = dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf;
462 if (rss_conf->rss_key)
463 rte_memcpy(rss_conf->rss_key, internal->rss_key, 40);
465 rte_spinlock_unlock(&internal->rss_lock);
470 static const struct eth_dev_ops ops = {
471 .dev_start = eth_dev_start,
472 .dev_stop = eth_dev_stop,
473 .dev_configure = eth_dev_configure,
474 .dev_infos_get = eth_dev_info,
475 .rx_queue_setup = eth_rx_queue_setup,
476 .tx_queue_setup = eth_tx_queue_setup,
477 .rx_queue_release = eth_queue_release,
478 .tx_queue_release = eth_queue_release,
479 .link_update = eth_link_update,
480 .stats_get = eth_stats_get,
481 .stats_reset = eth_stats_reset,
482 .reta_update = eth_rss_reta_update,
483 .reta_query = eth_rss_reta_query,
484 .rss_hash_update = eth_rss_hash_update,
485 .rss_hash_conf_get = eth_rss_hash_conf_get
489 eth_dev_null_create(const char *name,
490 const unsigned numa_node,
491 unsigned packet_size,
492 unsigned packet_copy)
494 const unsigned nb_rx_queues = 1;
495 const unsigned nb_tx_queues = 1;
496 struct rte_eth_dev_data *data = NULL;
497 struct pmd_internals *internals = NULL;
498 struct rte_eth_dev *eth_dev = NULL;
500 static const uint8_t default_rss_key[40] = {
501 0x6D, 0x5A, 0x56, 0xDA, 0x25, 0x5B, 0x0E, 0xC2, 0x41, 0x67, 0x25, 0x3D,
502 0x43, 0xA3, 0x8F, 0xB0, 0xD0, 0xCA, 0x2B, 0xCB, 0xAE, 0x7B, 0x30, 0xB4,
503 0x77, 0xCB, 0x2D, 0xA3, 0x80, 0x30, 0xF2, 0x0C, 0x6A, 0x42, 0xB7, 0x3B,
504 0xBE, 0xAC, 0x01, 0xFA
510 RTE_LOG(INFO, PMD, "Creating null ethdev on numa socket %u\n",
513 /* now do all data allocation - for eth_dev structure, dummy pci driver
514 * and internal (private) data
516 data = rte_zmalloc_socket(name, sizeof(*data), 0, numa_node);
520 internals = rte_zmalloc_socket(name, sizeof(*internals), 0, numa_node);
521 if (internals == NULL)
524 /* reserve an ethdev entry */
525 eth_dev = rte_eth_dev_allocate(name, RTE_ETH_DEV_VIRTUAL);
529 /* now put it all together
530 * - store queue data in internals,
531 * - store numa_node info in ethdev data
532 * - point eth_dev_data to internals
533 * - and point eth_dev structure to new eth_dev_data structure
535 /* NOTE: we'll replace the data element, of originally allocated eth_dev
536 * so the nulls are local per-process */
538 internals->nb_rx_queues = nb_rx_queues;
539 internals->nb_tx_queues = nb_tx_queues;
540 internals->packet_size = packet_size;
541 internals->packet_copy = packet_copy;
542 internals->numa_node = numa_node;
544 internals->flow_type_rss_offloads = ETH_RSS_PROTO_MASK;
545 internals->reta_size = RTE_DIM(internals->reta_conf) * RTE_RETA_GROUP_SIZE;
547 rte_memcpy(internals->rss_key, default_rss_key, 40);
549 data->dev_private = internals;
550 data->port_id = eth_dev->data->port_id;
551 data->nb_rx_queues = (uint16_t)nb_rx_queues;
552 data->nb_tx_queues = (uint16_t)nb_tx_queues;
553 data->dev_link = pmd_link;
554 data->mac_addrs = ð_addr;
555 strncpy(data->name, eth_dev->data->name, strlen(eth_dev->data->name));
557 eth_dev->data = data;
558 eth_dev->dev_ops = &ops;
560 TAILQ_INIT(ð_dev->link_intr_cbs);
562 eth_dev->driver = NULL;
563 eth_dev->data->dev_flags = RTE_ETH_DEV_DETACHABLE;
564 eth_dev->data->kdrv = RTE_KDRV_NONE;
565 eth_dev->data->drv_name = drivername;
566 eth_dev->data->numa_node = numa_node;
568 /* finally assign rx and tx ops */
570 eth_dev->rx_pkt_burst = eth_null_copy_rx;
571 eth_dev->tx_pkt_burst = eth_null_copy_tx;
573 eth_dev->rx_pkt_burst = eth_null_rx;
574 eth_dev->tx_pkt_burst = eth_null_tx;
587 get_packet_size_arg(const char *key __rte_unused,
588 const char *value, void *extra_args)
590 const char *a = value;
591 unsigned *packet_size = extra_args;
593 if ((value == NULL) || (extra_args == NULL))
596 *packet_size = (unsigned)strtoul(a, NULL, 0);
597 if (*packet_size == UINT_MAX)
604 get_packet_copy_arg(const char *key __rte_unused,
605 const char *value, void *extra_args)
607 const char *a = value;
608 unsigned *packet_copy = extra_args;
610 if ((value == NULL) || (extra_args == NULL))
613 *packet_copy = (unsigned)strtoul(a, NULL, 0);
614 if (*packet_copy == UINT_MAX)
621 rte_pmd_null_devinit(const char *name, const char *params)
624 unsigned packet_size = default_packet_size;
625 unsigned packet_copy = default_packet_copy;
626 struct rte_kvargs *kvlist = NULL;
632 RTE_LOG(INFO, PMD, "Initializing pmd_null for %s\n", name);
634 numa_node = rte_socket_id();
636 if (params != NULL) {
637 kvlist = rte_kvargs_parse(params, valid_arguments);
641 if (rte_kvargs_count(kvlist, ETH_NULL_PACKET_SIZE_ARG) == 1) {
643 ret = rte_kvargs_process(kvlist,
644 ETH_NULL_PACKET_SIZE_ARG,
645 &get_packet_size_arg, &packet_size);
650 if (rte_kvargs_count(kvlist, ETH_NULL_PACKET_COPY_ARG) == 1) {
652 ret = rte_kvargs_process(kvlist,
653 ETH_NULL_PACKET_COPY_ARG,
654 &get_packet_copy_arg, &packet_copy);
660 RTE_LOG(INFO, PMD, "Configure pmd_null: packet size is %d, "
661 "packet copy is %s\n", packet_size,
662 packet_copy ? "enabled" : "disabled");
664 ret = eth_dev_null_create(name, numa_node, packet_size, packet_copy);
668 rte_kvargs_free(kvlist);
673 rte_pmd_null_devuninit(const char *name)
675 struct rte_eth_dev *eth_dev = NULL;
680 RTE_LOG(INFO, PMD, "Closing null ethdev on numa socket %u\n",
683 /* find the ethdev entry */
684 eth_dev = rte_eth_dev_allocated(name);
688 rte_free(eth_dev->data->dev_private);
689 rte_free(eth_dev->data);
691 rte_eth_dev_release_port(eth_dev);
696 static struct rte_driver pmd_null_drv = {
699 .init = rte_pmd_null_devinit,
700 .uninit = rte_pmd_null_devuninit,
703 PMD_REGISTER_DRIVER(pmd_null_drv);