4 * Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
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 Intel Corporation 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.
34 #include "rte_eth_ring.h"
36 #include <rte_ethdev.h>
37 #include <rte_malloc.h>
38 #include <rte_memcpy.h>
39 #include <rte_memzone.h>
40 #include <rte_string_fns.h>
42 #include <rte_kvargs.h>
43 #include <rte_errno.h>
45 #define ETH_RING_NUMA_NODE_ACTION_ARG "nodeaction"
46 #define ETH_RING_ACTION_CREATE "CREATE"
47 #define ETH_RING_ACTION_ATTACH "ATTACH"
48 #define ETH_RING_INTERNAL_ARG "internal"
50 static const char *valid_arguments[] = {
51 ETH_RING_NUMA_NODE_ACTION_ARG,
52 ETH_RING_INTERNAL_ARG,
56 struct ring_internal_args {
57 struct rte_ring * const *rx_queues;
58 const unsigned int nb_rx_queues;
59 struct rte_ring * const *tx_queues;
60 const unsigned int nb_tx_queues;
61 const unsigned int numa_node;
62 void *addr; /* self addr for sanity check */
72 rte_atomic64_t rx_pkts;
73 rte_atomic64_t tx_pkts;
74 rte_atomic64_t err_pkts;
77 struct pmd_internals {
78 unsigned max_rx_queues;
79 unsigned max_tx_queues;
81 struct ring_queue rx_ring_queues[RTE_PMD_RING_MAX_RX_RINGS];
82 struct ring_queue tx_ring_queues[RTE_PMD_RING_MAX_TX_RINGS];
84 struct ether_addr address;
85 enum dev_action action;
89 static struct rte_eth_link pmd_link = {
90 .link_speed = ETH_SPEED_NUM_10G,
91 .link_duplex = ETH_LINK_FULL_DUPLEX,
92 .link_status = ETH_LINK_DOWN,
93 .link_autoneg = ETH_LINK_SPEED_AUTONEG
97 eth_ring_rx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
99 void **ptrs = (void *)&bufs[0];
100 struct ring_queue *r = q;
101 const uint16_t nb_rx = (uint16_t)rte_ring_dequeue_burst(r->rng,
102 ptrs, nb_bufs, NULL);
103 if (r->rng->flags & RING_F_SC_DEQ)
104 r->rx_pkts.cnt += nb_rx;
106 rte_atomic64_add(&(r->rx_pkts), nb_rx);
111 eth_ring_tx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
113 void **ptrs = (void *)&bufs[0];
114 struct ring_queue *r = q;
115 const uint16_t nb_tx = (uint16_t)rte_ring_enqueue_burst(r->rng,
116 ptrs, nb_bufs, NULL);
117 if (r->rng->flags & RING_F_SP_ENQ) {
118 r->tx_pkts.cnt += nb_tx;
119 r->err_pkts.cnt += nb_bufs - nb_tx;
121 rte_atomic64_add(&(r->tx_pkts), nb_tx);
122 rte_atomic64_add(&(r->err_pkts), nb_bufs - nb_tx);
128 eth_dev_configure(struct rte_eth_dev *dev __rte_unused) { return 0; }
131 eth_dev_start(struct rte_eth_dev *dev)
133 dev->data->dev_link.link_status = ETH_LINK_UP;
138 eth_dev_stop(struct rte_eth_dev *dev)
140 dev->data->dev_link.link_status = ETH_LINK_DOWN;
144 eth_dev_set_link_down(struct rte_eth_dev *dev)
146 dev->data->dev_link.link_status = ETH_LINK_DOWN;
151 eth_dev_set_link_up(struct rte_eth_dev *dev)
153 dev->data->dev_link.link_status = ETH_LINK_UP;
158 eth_rx_queue_setup(struct rte_eth_dev *dev, uint16_t rx_queue_id,
159 uint16_t nb_rx_desc __rte_unused,
160 unsigned int socket_id __rte_unused,
161 const struct rte_eth_rxconf *rx_conf __rte_unused,
162 struct rte_mempool *mb_pool __rte_unused)
164 struct pmd_internals *internals = dev->data->dev_private;
165 dev->data->rx_queues[rx_queue_id] = &internals->rx_ring_queues[rx_queue_id];
170 eth_tx_queue_setup(struct rte_eth_dev *dev, uint16_t tx_queue_id,
171 uint16_t nb_tx_desc __rte_unused,
172 unsigned int socket_id __rte_unused,
173 const struct rte_eth_txconf *tx_conf __rte_unused)
175 struct pmd_internals *internals = dev->data->dev_private;
176 dev->data->tx_queues[tx_queue_id] = &internals->tx_ring_queues[tx_queue_id];
182 eth_dev_info(struct rte_eth_dev *dev,
183 struct rte_eth_dev_info *dev_info)
185 struct pmd_internals *internals = dev->data->dev_private;
186 dev_info->max_mac_addrs = 1;
187 dev_info->max_rx_pktlen = (uint32_t)-1;
188 dev_info->max_rx_queues = (uint16_t)internals->max_rx_queues;
189 dev_info->max_tx_queues = (uint16_t)internals->max_tx_queues;
190 dev_info->min_rx_bufsize = 0;
194 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
197 unsigned long rx_total = 0, tx_total = 0, tx_err_total = 0;
198 const struct pmd_internals *internal = dev->data->dev_private;
200 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
201 i < dev->data->nb_rx_queues; i++) {
202 stats->q_ipackets[i] = internal->rx_ring_queues[i].rx_pkts.cnt;
203 rx_total += stats->q_ipackets[i];
206 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
207 i < dev->data->nb_tx_queues; i++) {
208 stats->q_opackets[i] = internal->tx_ring_queues[i].tx_pkts.cnt;
209 stats->q_errors[i] = internal->tx_ring_queues[i].err_pkts.cnt;
210 tx_total += stats->q_opackets[i];
211 tx_err_total += stats->q_errors[i];
214 stats->ipackets = rx_total;
215 stats->opackets = tx_total;
216 stats->oerrors = tx_err_total;
222 eth_stats_reset(struct rte_eth_dev *dev)
225 struct pmd_internals *internal = dev->data->dev_private;
226 for (i = 0; i < dev->data->nb_rx_queues; i++)
227 internal->rx_ring_queues[i].rx_pkts.cnt = 0;
228 for (i = 0; i < dev->data->nb_tx_queues; i++) {
229 internal->tx_ring_queues[i].tx_pkts.cnt = 0;
230 internal->tx_ring_queues[i].err_pkts.cnt = 0;
235 eth_mac_addr_remove(struct rte_eth_dev *dev __rte_unused,
236 uint32_t index __rte_unused)
241 eth_mac_addr_add(struct rte_eth_dev *dev __rte_unused,
242 struct ether_addr *mac_addr __rte_unused,
243 uint32_t index __rte_unused,
244 uint32_t vmdq __rte_unused)
250 eth_queue_release(void *q __rte_unused) { ; }
252 eth_link_update(struct rte_eth_dev *dev __rte_unused,
253 int wait_to_complete __rte_unused) { return 0; }
255 static const struct eth_dev_ops ops = {
256 .dev_start = eth_dev_start,
257 .dev_stop = eth_dev_stop,
258 .dev_set_link_up = eth_dev_set_link_up,
259 .dev_set_link_down = eth_dev_set_link_down,
260 .dev_configure = eth_dev_configure,
261 .dev_infos_get = eth_dev_info,
262 .rx_queue_setup = eth_rx_queue_setup,
263 .tx_queue_setup = eth_tx_queue_setup,
264 .rx_queue_release = eth_queue_release,
265 .tx_queue_release = eth_queue_release,
266 .link_update = eth_link_update,
267 .stats_get = eth_stats_get,
268 .stats_reset = eth_stats_reset,
269 .mac_addr_remove = eth_mac_addr_remove,
270 .mac_addr_add = eth_mac_addr_add,
273 static struct rte_vdev_driver pmd_ring_drv;
276 do_eth_dev_ring_create(const char *name,
277 struct rte_ring * const rx_queues[], const unsigned nb_rx_queues,
278 struct rte_ring *const tx_queues[], const unsigned nb_tx_queues,
279 const unsigned int numa_node, enum dev_action action,
280 struct rte_eth_dev **eth_dev_p)
282 struct rte_eth_dev_data *data = NULL;
283 struct pmd_internals *internals = NULL;
284 struct rte_eth_dev *eth_dev = NULL;
285 void **rx_queues_local = NULL;
286 void **tx_queues_local = NULL;
289 RTE_LOG(INFO, PMD, "Creating rings-backed ethdev on numa socket %u\n",
292 /* now do all data allocation - for eth_dev structure, dummy pci driver
293 * and internal (private) data
295 data = rte_zmalloc_socket(name, sizeof(*data), 0, numa_node);
301 rx_queues_local = rte_zmalloc_socket(name,
302 sizeof(void *) * nb_rx_queues, 0, numa_node);
303 if (rx_queues_local == NULL) {
308 tx_queues_local = rte_zmalloc_socket(name,
309 sizeof(void *) * nb_tx_queues, 0, numa_node);
310 if (tx_queues_local == NULL) {
315 internals = rte_zmalloc_socket(name, sizeof(*internals), 0, numa_node);
316 if (internals == NULL) {
321 /* reserve an ethdev entry */
322 eth_dev = rte_eth_dev_allocate(name);
323 if (eth_dev == NULL) {
328 /* now put it all together
329 * - store queue data in internals,
330 * - store numa_node info in eth_dev_data
331 * - point eth_dev_data to internals
332 * - and point eth_dev structure to new eth_dev_data structure
334 /* NOTE: we'll replace the data element, of originally allocated eth_dev
335 * so the rings are local per-process */
337 rte_memcpy(data, eth_dev->data, sizeof(*data));
338 data->rx_queues = rx_queues_local;
339 data->tx_queues = tx_queues_local;
341 internals->action = action;
342 internals->max_rx_queues = nb_rx_queues;
343 internals->max_tx_queues = nb_tx_queues;
344 for (i = 0; i < nb_rx_queues; i++) {
345 internals->rx_ring_queues[i].rng = rx_queues[i];
346 data->rx_queues[i] = &internals->rx_ring_queues[i];
348 for (i = 0; i < nb_tx_queues; i++) {
349 internals->tx_ring_queues[i].rng = tx_queues[i];
350 data->tx_queues[i] = &internals->tx_ring_queues[i];
353 data->dev_private = internals;
354 data->nb_rx_queues = (uint16_t)nb_rx_queues;
355 data->nb_tx_queues = (uint16_t)nb_tx_queues;
356 data->dev_link = pmd_link;
357 data->mac_addrs = &internals->address;
359 eth_dev->data = data;
360 eth_dev->dev_ops = &ops;
361 data->dev_flags = RTE_ETH_DEV_DETACHABLE;
362 data->kdrv = RTE_KDRV_NONE;
363 data->numa_node = numa_node;
365 /* finally assign rx and tx ops */
366 eth_dev->rx_pkt_burst = eth_ring_rx;
367 eth_dev->tx_pkt_burst = eth_ring_tx;
369 *eth_dev_p = eth_dev;
371 return data->port_id;
374 rte_free(rx_queues_local);
375 rte_free(tx_queues_local);
383 rte_eth_from_rings(const char *name, struct rte_ring *const rx_queues[],
384 const unsigned nb_rx_queues,
385 struct rte_ring *const tx_queues[],
386 const unsigned nb_tx_queues,
387 const unsigned numa_node)
389 struct ring_internal_args args = {
390 .rx_queues = rx_queues,
391 .nb_rx_queues = nb_rx_queues,
392 .tx_queues = tx_queues,
393 .nb_tx_queues = nb_tx_queues,
394 .numa_node = numa_node,
397 char args_str[32] = { 0 };
398 char ring_name[32] = { 0 };
399 uint16_t port_id = RTE_MAX_ETHPORTS;
402 /* do some parameter checking */
403 if (rx_queues == NULL && nb_rx_queues > 0) {
407 if (tx_queues == NULL && nb_tx_queues > 0) {
411 if (nb_rx_queues > RTE_PMD_RING_MAX_RX_RINGS) {
416 snprintf(args_str, 32, "%s=%p", ETH_RING_INTERNAL_ARG, &args);
417 snprintf(ring_name, 32, "net_ring_%s", name);
419 ret = rte_vdev_init(ring_name, args_str);
425 rte_eth_dev_get_port_by_name(ring_name, &port_id);
431 rte_eth_from_ring(struct rte_ring *r)
433 return rte_eth_from_rings(r->name, &r, 1, &r, 1,
434 r->memzone ? r->memzone->socket_id : SOCKET_ID_ANY);
438 eth_dev_ring_create(const char *name, const unsigned numa_node,
439 enum dev_action action, struct rte_eth_dev **eth_dev)
441 /* rx and tx are so-called from point of view of first port.
442 * They are inverted from the point of view of second port
444 struct rte_ring *rxtx[RTE_PMD_RING_MAX_RX_RINGS];
446 char rng_name[RTE_RING_NAMESIZE];
447 unsigned num_rings = RTE_MIN(RTE_PMD_RING_MAX_RX_RINGS,
448 RTE_PMD_RING_MAX_TX_RINGS);
450 for (i = 0; i < num_rings; i++) {
451 snprintf(rng_name, sizeof(rng_name), "ETH_RXTX%u_%s", i, name);
452 rxtx[i] = (action == DEV_CREATE) ?
453 rte_ring_create(rng_name, 1024, numa_node,
454 RING_F_SP_ENQ|RING_F_SC_DEQ) :
455 rte_ring_lookup(rng_name);
460 if (do_eth_dev_ring_create(name, rxtx, num_rings, rxtx, num_rings,
461 numa_node, action, eth_dev) < 0)
467 struct node_action_pair {
470 enum dev_action action;
473 struct node_action_list {
476 struct node_action_pair *list;
479 static int parse_kvlist (const char *key __rte_unused, const char *value, void *data)
481 struct node_action_list *info = data;
488 name = strdup(value);
493 RTE_LOG(WARNING, PMD, "command line parameter is empty for ring pmd!\n");
497 node = strchr(name, ':');
499 RTE_LOG(WARNING, PMD, "could not parse node value from %s\n",
507 action = strchr(node, ':');
509 RTE_LOG(WARNING, PMD, "could not parse action value from %s\n",
518 * Need to do some sanity checking here
521 if (strcmp(action, ETH_RING_ACTION_ATTACH) == 0)
522 info->list[info->count].action = DEV_ATTACH;
523 else if (strcmp(action, ETH_RING_ACTION_CREATE) == 0)
524 info->list[info->count].action = DEV_CREATE;
529 info->list[info->count].node = strtol(node, &end, 10);
531 if ((errno != 0) || (*end != '\0')) {
532 RTE_LOG(WARNING, PMD, "node value %s is unparseable as a number\n", node);
536 snprintf(info->list[info->count].name, sizeof(info->list[info->count].name), "%s", name);
547 parse_internal_args(const char *key __rte_unused, const char *value,
550 struct ring_internal_args **internal_args = data;
553 sscanf(value, "%p", &args);
555 *internal_args = args;
557 if ((*internal_args)->addr != args)
564 rte_pmd_ring_probe(struct rte_vdev_device *dev)
566 const char *name, *params;
567 struct rte_kvargs *kvlist = NULL;
569 struct node_action_list *info = NULL;
570 struct rte_eth_dev *eth_dev = NULL;
571 struct ring_internal_args *internal_args;
573 name = rte_vdev_device_name(dev);
574 params = rte_vdev_device_args(dev);
576 RTE_LOG(INFO, PMD, "Initializing pmd_ring for %s\n", name);
578 if (params == NULL || params[0] == '\0') {
579 ret = eth_dev_ring_create(name, rte_socket_id(), DEV_CREATE,
583 "Attach to pmd_ring for %s\n", name);
584 ret = eth_dev_ring_create(name, rte_socket_id(),
585 DEV_ATTACH, ð_dev);
588 kvlist = rte_kvargs_parse(params, valid_arguments);
591 RTE_LOG(INFO, PMD, "Ignoring unsupported parameters when creating"
592 " rings-backed ethernet device\n");
593 ret = eth_dev_ring_create(name, rte_socket_id(),
594 DEV_CREATE, ð_dev);
597 "Attach to pmd_ring for %s\n",
599 ret = eth_dev_ring_create(name, rte_socket_id(),
600 DEV_ATTACH, ð_dev);
604 eth_dev->device = &dev->device;
609 if (rte_kvargs_count(kvlist, ETH_RING_INTERNAL_ARG) == 1) {
610 ret = rte_kvargs_process(kvlist, ETH_RING_INTERNAL_ARG,
616 ret = do_eth_dev_ring_create(name,
617 internal_args->rx_queues,
618 internal_args->nb_rx_queues,
619 internal_args->tx_queues,
620 internal_args->nb_tx_queues,
621 internal_args->numa_node,
627 ret = rte_kvargs_count(kvlist, ETH_RING_NUMA_NODE_ACTION_ARG);
628 info = rte_zmalloc("struct node_action_list",
629 sizeof(struct node_action_list) +
630 (sizeof(struct node_action_pair) * ret),
636 info->list = (struct node_action_pair*)(info + 1);
638 ret = rte_kvargs_process(kvlist, ETH_RING_NUMA_NODE_ACTION_ARG,
644 for (info->count = 0; info->count < info->total; info->count++) {
645 ret = eth_dev_ring_create(info->list[info->count].name,
646 info->list[info->count].node,
647 info->list[info->count].action,
650 (info->list[info->count].action == DEV_CREATE)) {
652 "Attach to pmd_ring for %s\n",
654 ret = eth_dev_ring_create(name,
655 info->list[info->count].node,
664 eth_dev->device = &dev->device;
667 rte_kvargs_free(kvlist);
673 rte_pmd_ring_remove(struct rte_vdev_device *dev)
675 const char *name = rte_vdev_device_name(dev);
676 struct rte_eth_dev *eth_dev = NULL;
677 struct pmd_internals *internals = NULL;
678 struct ring_queue *r = NULL;
681 RTE_LOG(INFO, PMD, "Un-Initializing pmd_ring for %s\n", name);
686 /* find an ethdev entry */
687 eth_dev = rte_eth_dev_allocated(name);
691 eth_dev_stop(eth_dev);
693 internals = eth_dev->data->dev_private;
694 if (internals->action == DEV_CREATE) {
696 * it is only necessary to delete the rings in rx_queues because
697 * they are the same used in tx_queues
699 for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
700 r = eth_dev->data->rx_queues[i];
701 rte_ring_free(r->rng);
705 rte_free(eth_dev->data->rx_queues);
706 rte_free(eth_dev->data->tx_queues);
707 rte_free(eth_dev->data->dev_private);
709 rte_free(eth_dev->data);
711 rte_eth_dev_release_port(eth_dev);
715 static struct rte_vdev_driver pmd_ring_drv = {
716 .probe = rte_pmd_ring_probe,
717 .remove = rte_pmd_ring_remove,
720 RTE_PMD_REGISTER_VDEV(net_ring, pmd_ring_drv);
721 RTE_PMD_REGISTER_ALIAS(net_ring, eth_ring);
722 RTE_PMD_REGISTER_PARAM_STRING(net_ring,
723 ETH_RING_NUMA_NODE_ACTION_ARG "=name:node:action(ATTACH|CREATE)");