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"
49 static const char *valid_arguments[] = {
50 ETH_RING_NUMA_NODE_ACTION_ARG,
61 rte_atomic64_t rx_pkts;
62 rte_atomic64_t tx_pkts;
63 rte_atomic64_t err_pkts;
66 struct pmd_internals {
67 unsigned max_rx_queues;
68 unsigned max_tx_queues;
70 struct ring_queue rx_ring_queues[RTE_PMD_RING_MAX_RX_RINGS];
71 struct ring_queue tx_ring_queues[RTE_PMD_RING_MAX_TX_RINGS];
73 struct ether_addr address;
74 enum dev_action action;
78 static struct rte_eth_link pmd_link = {
79 .link_speed = ETH_SPEED_NUM_10G,
80 .link_duplex = ETH_LINK_FULL_DUPLEX,
81 .link_status = ETH_LINK_DOWN,
82 .link_autoneg = ETH_LINK_SPEED_AUTONEG
86 eth_ring_rx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
88 void **ptrs = (void *)&bufs[0];
89 struct ring_queue *r = q;
90 const uint16_t nb_rx = (uint16_t)rte_ring_dequeue_burst(r->rng,
92 if (r->rng->flags & RING_F_SC_DEQ)
93 r->rx_pkts.cnt += nb_rx;
95 rte_atomic64_add(&(r->rx_pkts), nb_rx);
100 eth_ring_tx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
102 void **ptrs = (void *)&bufs[0];
103 struct ring_queue *r = q;
104 const uint16_t nb_tx = (uint16_t)rte_ring_enqueue_burst(r->rng,
106 if (r->rng->flags & RING_F_SP_ENQ) {
107 r->tx_pkts.cnt += nb_tx;
108 r->err_pkts.cnt += nb_bufs - nb_tx;
110 rte_atomic64_add(&(r->tx_pkts), nb_tx);
111 rte_atomic64_add(&(r->err_pkts), nb_bufs - nb_tx);
117 eth_dev_configure(struct rte_eth_dev *dev __rte_unused) { return 0; }
120 eth_dev_start(struct rte_eth_dev *dev)
122 dev->data->dev_link.link_status = ETH_LINK_UP;
127 eth_dev_stop(struct rte_eth_dev *dev)
129 dev->data->dev_link.link_status = ETH_LINK_DOWN;
133 eth_dev_set_link_down(struct rte_eth_dev *dev)
135 dev->data->dev_link.link_status = ETH_LINK_DOWN;
140 eth_dev_set_link_up(struct rte_eth_dev *dev)
142 dev->data->dev_link.link_status = ETH_LINK_UP;
147 eth_rx_queue_setup(struct rte_eth_dev *dev, uint16_t rx_queue_id,
148 uint16_t nb_rx_desc __rte_unused,
149 unsigned int socket_id __rte_unused,
150 const struct rte_eth_rxconf *rx_conf __rte_unused,
151 struct rte_mempool *mb_pool __rte_unused)
153 struct pmd_internals *internals = dev->data->dev_private;
154 dev->data->rx_queues[rx_queue_id] = &internals->rx_ring_queues[rx_queue_id];
159 eth_tx_queue_setup(struct rte_eth_dev *dev, uint16_t tx_queue_id,
160 uint16_t nb_tx_desc __rte_unused,
161 unsigned int socket_id __rte_unused,
162 const struct rte_eth_txconf *tx_conf __rte_unused)
164 struct pmd_internals *internals = dev->data->dev_private;
165 dev->data->tx_queues[tx_queue_id] = &internals->tx_ring_queues[tx_queue_id];
171 eth_dev_info(struct rte_eth_dev *dev,
172 struct rte_eth_dev_info *dev_info)
174 struct pmd_internals *internals = dev->data->dev_private;
175 dev_info->max_mac_addrs = 1;
176 dev_info->max_rx_pktlen = (uint32_t)-1;
177 dev_info->max_rx_queues = (uint16_t)internals->max_rx_queues;
178 dev_info->max_tx_queues = (uint16_t)internals->max_tx_queues;
179 dev_info->min_rx_bufsize = 0;
183 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
186 unsigned long rx_total = 0, tx_total = 0, tx_err_total = 0;
187 const struct pmd_internals *internal = dev->data->dev_private;
189 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
190 i < dev->data->nb_rx_queues; i++) {
191 stats->q_ipackets[i] = internal->rx_ring_queues[i].rx_pkts.cnt;
192 rx_total += stats->q_ipackets[i];
195 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
196 i < dev->data->nb_tx_queues; i++) {
197 stats->q_opackets[i] = internal->tx_ring_queues[i].tx_pkts.cnt;
198 stats->q_errors[i] = internal->tx_ring_queues[i].err_pkts.cnt;
199 tx_total += stats->q_opackets[i];
200 tx_err_total += stats->q_errors[i];
203 stats->ipackets = rx_total;
204 stats->opackets = tx_total;
205 stats->oerrors = tx_err_total;
209 eth_stats_reset(struct rte_eth_dev *dev)
212 struct pmd_internals *internal = dev->data->dev_private;
213 for (i = 0; i < dev->data->nb_rx_queues; i++)
214 internal->rx_ring_queues[i].rx_pkts.cnt = 0;
215 for (i = 0; i < dev->data->nb_tx_queues; i++) {
216 internal->tx_ring_queues[i].tx_pkts.cnt = 0;
217 internal->tx_ring_queues[i].err_pkts.cnt = 0;
222 eth_mac_addr_remove(struct rte_eth_dev *dev __rte_unused,
223 uint32_t index __rte_unused)
228 eth_mac_addr_add(struct rte_eth_dev *dev __rte_unused,
229 struct ether_addr *mac_addr __rte_unused,
230 uint32_t index __rte_unused,
231 uint32_t vmdq __rte_unused)
236 eth_queue_release(void *q __rte_unused) { ; }
238 eth_link_update(struct rte_eth_dev *dev __rte_unused,
239 int wait_to_complete __rte_unused) { return 0; }
241 static const struct eth_dev_ops ops = {
242 .dev_start = eth_dev_start,
243 .dev_stop = eth_dev_stop,
244 .dev_set_link_up = eth_dev_set_link_up,
245 .dev_set_link_down = eth_dev_set_link_down,
246 .dev_configure = eth_dev_configure,
247 .dev_infos_get = eth_dev_info,
248 .rx_queue_setup = eth_rx_queue_setup,
249 .tx_queue_setup = eth_tx_queue_setup,
250 .rx_queue_release = eth_queue_release,
251 .tx_queue_release = eth_queue_release,
252 .link_update = eth_link_update,
253 .stats_get = eth_stats_get,
254 .stats_reset = eth_stats_reset,
255 .mac_addr_remove = eth_mac_addr_remove,
256 .mac_addr_add = eth_mac_addr_add,
260 do_eth_dev_ring_create(const char *name,
261 struct rte_ring * const rx_queues[], const unsigned nb_rx_queues,
262 struct rte_ring *const tx_queues[], const unsigned nb_tx_queues,
263 const unsigned numa_node, enum dev_action action)
265 struct rte_eth_dev_data *data = NULL;
266 struct pmd_internals *internals = NULL;
267 struct rte_eth_dev *eth_dev = NULL;
270 RTE_LOG(INFO, PMD, "Creating rings-backed ethdev on numa socket %u\n",
273 /* now do all data allocation - for eth_dev structure, dummy pci driver
274 * and internal (private) data
276 data = rte_zmalloc_socket(name, sizeof(*data), 0, numa_node);
282 data->rx_queues = rte_zmalloc_socket(name,
283 sizeof(void *) * nb_rx_queues, 0, numa_node);
284 if (data->rx_queues == NULL) {
289 data->tx_queues = rte_zmalloc_socket(name,
290 sizeof(void *) * nb_tx_queues, 0, numa_node);
291 if (data->tx_queues == NULL) {
296 internals = rte_zmalloc_socket(name, sizeof(*internals), 0, numa_node);
297 if (internals == NULL) {
302 /* reserve an ethdev entry */
303 eth_dev = rte_eth_dev_allocate(name);
304 if (eth_dev == NULL) {
309 /* now put it all together
310 * - store queue data in internals,
311 * - store numa_node info in eth_dev_data
312 * - point eth_dev_data to internals
313 * - and point eth_dev structure to new eth_dev_data structure
315 /* NOTE: we'll replace the data element, of originally allocated eth_dev
316 * so the rings are local per-process */
318 internals->action = action;
319 internals->max_rx_queues = nb_rx_queues;
320 internals->max_tx_queues = nb_tx_queues;
321 for (i = 0; i < nb_rx_queues; i++) {
322 internals->rx_ring_queues[i].rng = rx_queues[i];
323 data->rx_queues[i] = &internals->rx_ring_queues[i];
325 for (i = 0; i < nb_tx_queues; i++) {
326 internals->tx_ring_queues[i].rng = tx_queues[i];
327 data->tx_queues[i] = &internals->tx_ring_queues[i];
330 data->dev_private = internals;
331 data->port_id = eth_dev->data->port_id;
332 memmove(data->name, eth_dev->data->name, sizeof(data->name));
333 data->nb_rx_queues = (uint16_t)nb_rx_queues;
334 data->nb_tx_queues = (uint16_t)nb_tx_queues;
335 data->dev_link = pmd_link;
336 data->mac_addrs = &internals->address;
338 eth_dev->data = data;
339 eth_dev->driver = NULL;
340 eth_dev->dev_ops = &ops;
341 data->dev_flags = RTE_ETH_DEV_DETACHABLE;
342 data->kdrv = RTE_KDRV_NONE;
343 data->drv_name = "Rings PMD";
344 data->numa_node = numa_node;
346 /* finally assign rx and tx ops */
347 eth_dev->rx_pkt_burst = eth_ring_rx;
348 eth_dev->tx_pkt_burst = eth_ring_tx;
350 return data->port_id;
354 rte_free(data->rx_queues);
355 rte_free(data->tx_queues);
364 rte_eth_from_rings(const char *name, struct rte_ring *const rx_queues[],
365 const unsigned nb_rx_queues,
366 struct rte_ring *const tx_queues[],
367 const unsigned nb_tx_queues,
368 const unsigned numa_node)
370 /* do some parameter checking */
371 if (rx_queues == NULL && nb_rx_queues > 0) {
375 if (tx_queues == NULL && nb_tx_queues > 0) {
379 if (nb_rx_queues > RTE_PMD_RING_MAX_RX_RINGS) {
384 return do_eth_dev_ring_create(name, rx_queues, nb_rx_queues,
385 tx_queues, nb_tx_queues, numa_node, DEV_ATTACH);
389 rte_eth_from_ring(struct rte_ring *r)
391 return rte_eth_from_rings(r->name, &r, 1, &r, 1,
392 r->memzone ? r->memzone->socket_id : SOCKET_ID_ANY);
396 eth_dev_ring_create(const char *name, const unsigned numa_node,
397 enum dev_action action)
399 /* rx and tx are so-called from point of view of first port.
400 * They are inverted from the point of view of second port
402 struct rte_ring *rxtx[RTE_PMD_RING_MAX_RX_RINGS];
404 char rng_name[RTE_RING_NAMESIZE];
405 unsigned num_rings = RTE_MIN(RTE_PMD_RING_MAX_RX_RINGS,
406 RTE_PMD_RING_MAX_TX_RINGS);
408 for (i = 0; i < num_rings; i++) {
409 snprintf(rng_name, sizeof(rng_name), "ETH_RXTX%u_%s", i, name);
410 rxtx[i] = (action == DEV_CREATE) ?
411 rte_ring_create(rng_name, 1024, numa_node,
412 RING_F_SP_ENQ|RING_F_SC_DEQ) :
413 rte_ring_lookup(rng_name);
418 if (do_eth_dev_ring_create(name, rxtx, num_rings, rxtx, num_rings,
419 numa_node, action) < 0)
425 struct node_action_pair {
428 enum dev_action action;
431 struct node_action_list {
434 struct node_action_pair *list;
437 static int parse_kvlist (const char *key __rte_unused, const char *value, void *data)
439 struct node_action_list *info = data;
446 name = strdup(value);
451 RTE_LOG(WARNING, PMD, "command line paramter is empty for ring pmd!\n");
455 node = strchr(name, ':');
457 RTE_LOG(WARNING, PMD, "could not parse node value from %s", name);
464 action = strchr(node, ':');
466 RTE_LOG(WARNING, PMD, "could not action value from %s", node);
474 * Need to do some sanity checking here
477 if (strcmp(action, ETH_RING_ACTION_ATTACH) == 0)
478 info->list[info->count].action = DEV_ATTACH;
479 else if (strcmp(action, ETH_RING_ACTION_CREATE) == 0)
480 info->list[info->count].action = DEV_CREATE;
485 info->list[info->count].node = strtol(node, &end, 10);
487 if ((errno != 0) || (*end != '\0')) {
488 RTE_LOG(WARNING, PMD, "node value %s is unparseable as a number\n", node);
492 snprintf(info->list[info->count].name, sizeof(info->list[info->count].name), "%s", name);
503 rte_pmd_ring_probe(const char *name, const char *params)
505 struct rte_kvargs *kvlist = NULL;
507 struct node_action_list *info = NULL;
509 RTE_LOG(INFO, PMD, "Initializing pmd_ring for %s\n", name);
511 if (params == NULL || params[0] == '\0') {
512 ret = eth_dev_ring_create(name, rte_socket_id(), DEV_CREATE);
515 "Attach to pmd_ring for %s\n", name);
516 ret = eth_dev_ring_create(name, rte_socket_id(),
521 kvlist = rte_kvargs_parse(params, valid_arguments);
524 RTE_LOG(INFO, PMD, "Ignoring unsupported parameters when creating"
525 " rings-backed ethernet device\n");
526 ret = eth_dev_ring_create(name, rte_socket_id(),
530 "Attach to pmd_ring for %s\n",
532 ret = eth_dev_ring_create(name, rte_socket_id(),
537 ret = rte_kvargs_count(kvlist, ETH_RING_NUMA_NODE_ACTION_ARG);
538 info = rte_zmalloc("struct node_action_list",
539 sizeof(struct node_action_list) +
540 (sizeof(struct node_action_pair) * ret),
546 info->list = (struct node_action_pair*)(info + 1);
548 ret = rte_kvargs_process(kvlist, ETH_RING_NUMA_NODE_ACTION_ARG,
554 for (info->count = 0; info->count < info->total; info->count++) {
555 ret = eth_dev_ring_create(info->list[info->count].name,
556 info->list[info->count].node,
557 info->list[info->count].action);
559 (info->list[info->count].action == DEV_CREATE)) {
561 "Attach to pmd_ring for %s\n",
563 ret = eth_dev_ring_create(name,
564 info->list[info->count].node,
572 rte_kvargs_free(kvlist);
578 rte_pmd_ring_remove(const char *name)
580 struct rte_eth_dev *eth_dev = NULL;
581 struct pmd_internals *internals = NULL;
582 struct ring_queue *r = NULL;
585 RTE_LOG(INFO, PMD, "Un-Initializing pmd_ring for %s\n", name);
590 /* find an ethdev entry */
591 eth_dev = rte_eth_dev_allocated(name);
595 eth_dev_stop(eth_dev);
597 internals = eth_dev->data->dev_private;
598 if (internals->action == DEV_CREATE) {
600 * it is only necessary to delete the rings in rx_queues because
601 * they are the same used in tx_queues
603 for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
604 r = eth_dev->data->rx_queues[i];
605 rte_ring_free(r->rng);
609 rte_free(eth_dev->data->rx_queues);
610 rte_free(eth_dev->data->tx_queues);
611 rte_free(eth_dev->data->dev_private);
613 rte_free(eth_dev->data);
615 rte_eth_dev_release_port(eth_dev);
619 static struct rte_vdev_driver pmd_ring_drv = {
620 .probe = rte_pmd_ring_probe,
621 .remove = rte_pmd_ring_remove,
624 RTE_PMD_REGISTER_VDEV(net_ring, pmd_ring_drv);
625 RTE_PMD_REGISTER_ALIAS(net_ring, eth_ring);
626 RTE_PMD_REGISTER_PARAM_STRING(net_ring,
627 ETH_RING_NUMA_NODE_ACTION_ARG "=name:node:action(ATTACH|CREATE)");