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>
41 #define ETH_NULL_PACKET_SIZE_ARG "size"
42 #define ETH_NULL_PACKET_COPY_ARG "copy"
44 static unsigned default_packet_size = 64;
45 static unsigned default_packet_copy;
47 static const char *valid_arguments[] = {
48 ETH_NULL_PACKET_SIZE_ARG,
49 ETH_NULL_PACKET_COPY_ARG,
56 struct pmd_internals *internals;
58 struct rte_mempool *mb_pool;
59 struct rte_mbuf *dummy_packet;
61 rte_atomic64_t rx_pkts;
62 rte_atomic64_t tx_pkts;
63 rte_atomic64_t err_pkts;
66 struct pmd_internals {
71 unsigned nb_rx_queues;
72 unsigned nb_tx_queues;
74 struct null_queue rx_null_queues[RTE_MAX_QUEUES_PER_PORT];
75 struct null_queue tx_null_queues[RTE_MAX_QUEUES_PER_PORT];
79 static struct ether_addr eth_addr = { .addr_bytes = {0} };
80 static const char *drivername = "Null PMD";
81 static struct rte_eth_link pmd_link = {
83 .link_duplex = ETH_LINK_FULL_DUPLEX,
88 eth_null_rx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
91 struct null_queue *h = q;
94 if ((q == NULL) || (bufs == NULL))
97 packet_size = h->internals->packet_size;
98 for (i = 0; i < nb_bufs; i++) {
99 bufs[i] = rte_pktmbuf_alloc(h->mb_pool);
102 bufs[i]->data_len = (uint16_t)packet_size;
103 bufs[i]->pkt_len = packet_size;
104 bufs[i]->nb_segs = 1;
105 bufs[i]->next = NULL;
108 rte_atomic64_add(&(h->rx_pkts), i);
114 eth_null_copy_rx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
117 struct null_queue *h = q;
118 unsigned packet_size;
120 if ((q == NULL) || (bufs == NULL))
123 packet_size = h->internals->packet_size;
124 for (i = 0; i < nb_bufs; i++) {
125 bufs[i] = rte_pktmbuf_alloc(h->mb_pool);
128 rte_memcpy(rte_pktmbuf_mtod(bufs[i], void *), h->dummy_packet,
130 bufs[i]->data_len = (uint16_t)packet_size;
131 bufs[i]->pkt_len = packet_size;
132 bufs[i]->nb_segs = 1;
133 bufs[i]->next = NULL;
136 rte_atomic64_add(&(h->rx_pkts), i);
142 eth_null_tx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
145 struct null_queue *h = q;
147 if ((q == NULL) || (bufs == NULL))
150 for (i = 0; i < nb_bufs; i++)
151 rte_pktmbuf_free(bufs[i]);
153 rte_atomic64_add(&(h->tx_pkts), i);
159 eth_null_copy_tx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
162 struct null_queue *h = q;
163 unsigned packet_size;
165 if ((q == NULL) || (bufs == NULL))
168 packet_size = h->internals->packet_size;
169 for (i = 0; i < nb_bufs; i++) {
170 rte_memcpy(h->dummy_packet, rte_pktmbuf_mtod(bufs[i], void *),
172 rte_pktmbuf_free(bufs[i]);
175 rte_atomic64_add(&(h->tx_pkts), i);
181 eth_dev_configure(struct rte_eth_dev *dev) {
182 struct pmd_internals *internals;
184 internals = dev->data->dev_private;
185 internals->nb_rx_queues = dev->data->nb_rx_queues;
186 internals->nb_tx_queues = dev->data->nb_tx_queues;
192 eth_dev_start(struct rte_eth_dev *dev)
197 dev->data->dev_link.link_status = 1;
202 eth_dev_stop(struct rte_eth_dev *dev)
207 dev->data->dev_link.link_status = 0;
211 eth_rx_queue_setup(struct rte_eth_dev *dev, uint16_t rx_queue_id,
212 uint16_t nb_rx_desc __rte_unused,
213 unsigned int socket_id __rte_unused,
214 const struct rte_eth_rxconf *rx_conf __rte_unused,
215 struct rte_mempool *mb_pool)
217 struct rte_mbuf *dummy_packet;
218 struct pmd_internals *internals;
219 unsigned packet_size;
221 if ((dev == NULL) || (mb_pool == NULL))
224 internals = dev->data->dev_private;
226 if (rx_queue_id >= internals->nb_rx_queues)
229 packet_size = internals->packet_size;
231 internals->rx_null_queues[rx_queue_id].mb_pool = mb_pool;
232 dev->data->rx_queues[rx_queue_id] =
233 &internals->rx_null_queues[rx_queue_id];
234 dummy_packet = rte_zmalloc_socket(NULL,
235 packet_size, 0, internals->numa_node);
236 if (dummy_packet == NULL)
239 internals->rx_null_queues[rx_queue_id].internals = internals;
240 internals->rx_null_queues[rx_queue_id].dummy_packet = dummy_packet;
246 eth_tx_queue_setup(struct rte_eth_dev *dev, uint16_t tx_queue_id,
247 uint16_t nb_tx_desc __rte_unused,
248 unsigned int socket_id __rte_unused,
249 const struct rte_eth_txconf *tx_conf __rte_unused)
251 struct rte_mbuf *dummy_packet;
252 struct pmd_internals *internals;
253 unsigned packet_size;
258 internals = dev->data->dev_private;
260 if (tx_queue_id >= internals->nb_tx_queues)
263 packet_size = internals->packet_size;
265 dev->data->tx_queues[tx_queue_id] =
266 &internals->tx_null_queues[tx_queue_id];
267 dummy_packet = rte_zmalloc_socket(NULL,
268 packet_size, 0, internals->numa_node);
269 if (dummy_packet == NULL)
272 internals->tx_null_queues[tx_queue_id].internals = internals;
273 internals->tx_null_queues[tx_queue_id].dummy_packet = dummy_packet;
280 eth_dev_info(struct rte_eth_dev *dev,
281 struct rte_eth_dev_info *dev_info)
283 struct pmd_internals *internals;
285 if ((dev == NULL) || (dev_info == NULL))
288 internals = dev->data->dev_private;
289 dev_info->driver_name = drivername;
290 dev_info->max_mac_addrs = 1;
291 dev_info->max_rx_pktlen = (uint32_t)-1;
292 dev_info->max_rx_queues = RTE_DIM(internals->rx_null_queues);
293 dev_info->max_tx_queues = RTE_DIM(internals->tx_null_queues);
294 dev_info->min_rx_bufsize = 0;
295 dev_info->pci_dev = NULL;
299 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *igb_stats)
301 unsigned i, num_stats;
302 unsigned long rx_total = 0, tx_total = 0, tx_err_total = 0;
303 const struct pmd_internals *internal;
305 if ((dev == NULL) || (igb_stats == NULL))
308 internal = dev->data->dev_private;
309 num_stats = RTE_MIN((unsigned)RTE_ETHDEV_QUEUE_STAT_CNTRS,
310 RTE_MIN(internal->nb_rx_queues,
311 RTE_DIM(internal->rx_null_queues)));
312 for (i = 0; i < num_stats; i++) {
313 igb_stats->q_ipackets[i] =
314 internal->rx_null_queues[i].rx_pkts.cnt;
315 rx_total += igb_stats->q_ipackets[i];
318 num_stats = RTE_MIN((unsigned)RTE_ETHDEV_QUEUE_STAT_CNTRS,
319 RTE_MIN(internal->nb_tx_queues,
320 RTE_DIM(internal->tx_null_queues)));
321 for (i = 0; i < num_stats; i++) {
322 igb_stats->q_opackets[i] =
323 internal->tx_null_queues[i].tx_pkts.cnt;
324 igb_stats->q_errors[i] =
325 internal->tx_null_queues[i].err_pkts.cnt;
326 tx_total += igb_stats->q_opackets[i];
327 tx_err_total += igb_stats->q_errors[i];
330 igb_stats->ipackets = rx_total;
331 igb_stats->opackets = tx_total;
332 igb_stats->oerrors = tx_err_total;
336 eth_stats_reset(struct rte_eth_dev *dev)
339 struct pmd_internals *internal;
344 internal = dev->data->dev_private;
345 for (i = 0; i < RTE_DIM(internal->rx_null_queues); i++)
346 internal->rx_null_queues[i].rx_pkts.cnt = 0;
347 for (i = 0; i < RTE_DIM(internal->tx_null_queues); i++) {
348 internal->tx_null_queues[i].tx_pkts.cnt = 0;
349 internal->tx_null_queues[i].err_pkts.cnt = 0;
353 static struct eth_driver rte_null_pmd = {
355 .name = "rte_null_pmd",
356 .drv_flags = RTE_PCI_DRV_DETACHABLE,
361 eth_queue_release(void *q)
363 struct null_queue *nq;
369 rte_free(nq->dummy_packet);
373 eth_link_update(struct rte_eth_dev *dev __rte_unused,
374 int wait_to_complete __rte_unused) { return 0; }
376 static const struct eth_dev_ops ops = {
377 .dev_start = eth_dev_start,
378 .dev_stop = eth_dev_stop,
379 .dev_configure = eth_dev_configure,
380 .dev_infos_get = eth_dev_info,
381 .rx_queue_setup = eth_rx_queue_setup,
382 .tx_queue_setup = eth_tx_queue_setup,
383 .rx_queue_release = eth_queue_release,
384 .tx_queue_release = eth_queue_release,
385 .link_update = eth_link_update,
386 .stats_get = eth_stats_get,
387 .stats_reset = eth_stats_reset,
391 eth_dev_null_create(const char *name,
392 const unsigned numa_node,
393 unsigned packet_size,
394 unsigned packet_copy)
396 const unsigned nb_rx_queues = 1;
397 const unsigned nb_tx_queues = 1;
398 struct rte_eth_dev_data *data = NULL;
399 struct rte_pci_device *pci_dev = NULL;
400 struct pmd_internals *internals = NULL;
401 struct rte_eth_dev *eth_dev = NULL;
406 RTE_LOG(INFO, PMD, "Creating null ethdev on numa socket %u\n",
409 /* now do all data allocation - for eth_dev structure, dummy pci driver
410 * and internal (private) data
412 data = rte_zmalloc_socket(name, sizeof(*data), 0, numa_node);
416 pci_dev = rte_zmalloc_socket(name, sizeof(*pci_dev), 0, numa_node);
420 internals = rte_zmalloc_socket(name, sizeof(*internals), 0, numa_node);
421 if (internals == NULL)
424 /* reserve an ethdev entry */
425 eth_dev = rte_eth_dev_allocate(name, RTE_ETH_DEV_VIRTUAL);
429 /* now put it all together
430 * - store queue data in internals,
431 * - store numa_node info in pci_driver
432 * - point eth_dev_data to internals and pci_driver
433 * - and point eth_dev structure to new eth_dev_data structure
435 /* NOTE: we'll replace the data element, of originally allocated eth_dev
436 * so the nulls are local per-process */
438 internals->nb_rx_queues = nb_rx_queues;
439 internals->nb_tx_queues = nb_tx_queues;
440 internals->packet_size = packet_size;
441 internals->packet_copy = packet_copy;
442 internals->numa_node = numa_node;
444 pci_dev->numa_node = numa_node;
445 pci_dev->driver = &rte_null_pmd.pci_drv;
447 data->dev_private = internals;
448 data->port_id = eth_dev->data->port_id;
449 data->nb_rx_queues = (uint16_t)nb_rx_queues;
450 data->nb_tx_queues = (uint16_t)nb_tx_queues;
451 data->dev_link = pmd_link;
452 data->mac_addrs = ð_addr;
453 strncpy(data->name, eth_dev->data->name, strlen(eth_dev->data->name));
455 eth_dev->data = data;
456 eth_dev->dev_ops = &ops;
457 eth_dev->pci_dev = pci_dev;
458 eth_dev->driver = &rte_null_pmd;
459 TAILQ_INIT(ð_dev->link_intr_cbs);
461 /* finally assign rx and tx ops */
463 eth_dev->rx_pkt_burst = eth_null_copy_rx;
464 eth_dev->tx_pkt_burst = eth_null_copy_tx;
466 eth_dev->rx_pkt_burst = eth_null_rx;
467 eth_dev->tx_pkt_burst = eth_null_tx;
481 get_packet_size_arg(const char *key __rte_unused,
482 const char *value, void *extra_args)
484 const char *a = value;
485 unsigned *packet_size = extra_args;
487 if ((value == NULL) || (extra_args == NULL))
490 *packet_size = (unsigned)strtoul(a, NULL, 0);
491 if (*packet_size == UINT_MAX)
498 get_packet_copy_arg(const char *key __rte_unused,
499 const char *value, void *extra_args)
501 const char *a = value;
502 unsigned *packet_copy = extra_args;
504 if ((value == NULL) || (extra_args == NULL))
507 *packet_copy = (unsigned)strtoul(a, NULL, 0);
508 if (*packet_copy == UINT_MAX)
515 rte_pmd_null_devinit(const char *name, const char *params)
518 unsigned packet_size = default_packet_size;
519 unsigned packet_copy = default_packet_copy;
520 struct rte_kvargs *kvlist = NULL;
526 RTE_LOG(INFO, PMD, "Initializing pmd_null for %s\n", name);
528 numa_node = rte_socket_id();
530 if (params != NULL) {
531 kvlist = rte_kvargs_parse(params, valid_arguments);
535 if (rte_kvargs_count(kvlist, ETH_NULL_PACKET_SIZE_ARG) == 1) {
537 ret = rte_kvargs_process(kvlist,
538 ETH_NULL_PACKET_SIZE_ARG,
539 &get_packet_size_arg, &packet_size);
544 if (rte_kvargs_count(kvlist, ETH_NULL_PACKET_COPY_ARG) == 1) {
546 ret = rte_kvargs_process(kvlist,
547 ETH_NULL_PACKET_COPY_ARG,
548 &get_packet_copy_arg, &packet_copy);
554 RTE_LOG(INFO, PMD, "Configure pmd_null: packet size is %d, "
555 "packet copy is %s\n", packet_size,
556 packet_copy ? "enabled" : "disabled");
558 ret = eth_dev_null_create(name, numa_node, packet_size, packet_copy);
562 rte_kvargs_free(kvlist);
567 rte_pmd_null_devuninit(const char *name)
569 struct rte_eth_dev *eth_dev = NULL;
574 RTE_LOG(INFO, PMD, "Closing null ethdev on numa socket %u\n",
577 /* reserve an ethdev entry */
578 eth_dev = rte_eth_dev_allocated(name);
582 rte_free(eth_dev->data->dev_private);
583 rte_free(eth_dev->data);
584 rte_free(eth_dev->pci_dev);
586 rte_eth_dev_release_port(eth_dev);
591 static struct rte_driver pmd_null_drv = {
594 .init = rte_pmd_null_devinit,
595 .uninit = rte_pmd_null_devuninit,
598 PMD_REGISTER_DRIVER(pmd_null_drv);