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,
56 rte_atomic64_t rx_pkts;
57 rte_atomic64_t tx_pkts;
58 rte_atomic64_t err_pkts;
61 struct pmd_internals {
62 unsigned max_rx_queues;
63 unsigned max_tx_queues;
65 struct ring_queue rx_ring_queues[RTE_PMD_RING_MAX_RX_RINGS];
66 struct ring_queue tx_ring_queues[RTE_PMD_RING_MAX_TX_RINGS];
68 struct ether_addr address;
72 static const char *drivername = "Rings PMD";
73 static struct rte_eth_link pmd_link = {
75 .link_duplex = ETH_LINK_FULL_DUPLEX,
80 eth_ring_rx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
82 void **ptrs = (void *)&bufs[0];
83 struct ring_queue *r = q;
84 const uint16_t nb_rx = (uint16_t)rte_ring_dequeue_burst(r->rng,
86 if (r->rng->flags & RING_F_SC_DEQ)
87 r->rx_pkts.cnt += nb_rx;
89 rte_atomic64_add(&(r->rx_pkts), nb_rx);
94 eth_ring_tx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
96 void **ptrs = (void *)&bufs[0];
97 struct ring_queue *r = q;
98 const uint16_t nb_tx = (uint16_t)rte_ring_enqueue_burst(r->rng,
100 if (r->rng->flags & RING_F_SP_ENQ) {
101 r->tx_pkts.cnt += nb_tx;
102 r->err_pkts.cnt += nb_bufs - nb_tx;
104 rte_atomic64_add(&(r->tx_pkts), nb_tx);
105 rte_atomic64_add(&(r->err_pkts), nb_bufs - nb_tx);
111 eth_dev_configure(struct rte_eth_dev *dev __rte_unused) { return 0; }
114 eth_dev_start(struct rte_eth_dev *dev)
116 dev->data->dev_link.link_status = 1;
121 eth_dev_stop(struct rte_eth_dev *dev)
123 dev->data->dev_link.link_status = 0;
127 eth_dev_set_link_down(struct rte_eth_dev *dev)
129 dev->data->dev_link.link_status = 0;
134 eth_dev_set_link_up(struct rte_eth_dev *dev)
136 dev->data->dev_link.link_status = 1;
141 eth_rx_queue_setup(struct rte_eth_dev *dev, uint16_t rx_queue_id,
142 uint16_t nb_rx_desc __rte_unused,
143 unsigned int socket_id __rte_unused,
144 const struct rte_eth_rxconf *rx_conf __rte_unused,
145 struct rte_mempool *mb_pool __rte_unused)
147 struct pmd_internals *internals = dev->data->dev_private;
148 dev->data->rx_queues[rx_queue_id] = &internals->rx_ring_queues[rx_queue_id];
153 eth_tx_queue_setup(struct rte_eth_dev *dev, uint16_t tx_queue_id,
154 uint16_t nb_tx_desc __rte_unused,
155 unsigned int socket_id __rte_unused,
156 const struct rte_eth_txconf *tx_conf __rte_unused)
158 struct pmd_internals *internals = dev->data->dev_private;
159 dev->data->tx_queues[tx_queue_id] = &internals->tx_ring_queues[tx_queue_id];
165 eth_dev_info(struct rte_eth_dev *dev,
166 struct rte_eth_dev_info *dev_info)
168 struct pmd_internals *internals = dev->data->dev_private;
169 dev_info->driver_name = drivername;
170 dev_info->max_mac_addrs = 1;
171 dev_info->max_rx_pktlen = (uint32_t)-1;
172 dev_info->max_rx_queues = (uint16_t)internals->max_rx_queues;
173 dev_info->max_tx_queues = (uint16_t)internals->max_tx_queues;
174 dev_info->min_rx_bufsize = 0;
175 dev_info->pci_dev = NULL;
179 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
182 unsigned long rx_total = 0, tx_total = 0, tx_err_total = 0;
183 const struct pmd_internals *internal = dev->data->dev_private;
185 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
186 i < dev->data->nb_rx_queues; i++) {
187 stats->q_ipackets[i] = internal->rx_ring_queues[i].rx_pkts.cnt;
188 rx_total += stats->q_ipackets[i];
191 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
192 i < dev->data->nb_tx_queues; i++) {
193 stats->q_opackets[i] = internal->tx_ring_queues[i].tx_pkts.cnt;
194 stats->q_errors[i] = internal->tx_ring_queues[i].err_pkts.cnt;
195 tx_total += stats->q_opackets[i];
196 tx_err_total += stats->q_errors[i];
199 stats->ipackets = rx_total;
200 stats->opackets = tx_total;
201 stats->oerrors = tx_err_total;
205 eth_stats_reset(struct rte_eth_dev *dev)
208 struct pmd_internals *internal = dev->data->dev_private;
209 for (i = 0; i < dev->data->nb_rx_queues; i++)
210 internal->rx_ring_queues[i].rx_pkts.cnt = 0;
211 for (i = 0; i < dev->data->nb_tx_queues; i++) {
212 internal->tx_ring_queues[i].tx_pkts.cnt = 0;
213 internal->tx_ring_queues[i].err_pkts.cnt = 0;
218 eth_mac_addr_remove(struct rte_eth_dev *dev __rte_unused,
219 uint32_t index __rte_unused)
224 eth_mac_addr_add(struct rte_eth_dev *dev __rte_unused,
225 struct ether_addr *mac_addr __rte_unused,
226 uint32_t index __rte_unused,
227 uint32_t vmdq __rte_unused)
232 eth_queue_release(void *q __rte_unused) { ; }
234 eth_link_update(struct rte_eth_dev *dev __rte_unused,
235 int wait_to_complete __rte_unused) { return 0; }
237 static const struct eth_dev_ops ops = {
238 .dev_start = eth_dev_start,
239 .dev_stop = eth_dev_stop,
240 .dev_set_link_up = eth_dev_set_link_up,
241 .dev_set_link_down = eth_dev_set_link_down,
242 .dev_configure = eth_dev_configure,
243 .dev_infos_get = eth_dev_info,
244 .rx_queue_setup = eth_rx_queue_setup,
245 .tx_queue_setup = eth_tx_queue_setup,
246 .rx_queue_release = eth_queue_release,
247 .tx_queue_release = eth_queue_release,
248 .link_update = eth_link_update,
249 .stats_get = eth_stats_get,
250 .stats_reset = eth_stats_reset,
251 .mac_addr_remove = eth_mac_addr_remove,
252 .mac_addr_add = eth_mac_addr_add,
256 rte_eth_from_rings(const char *name, struct rte_ring *const rx_queues[],
257 const unsigned nb_rx_queues,
258 struct rte_ring *const tx_queues[],
259 const unsigned nb_tx_queues,
260 const unsigned numa_node)
262 struct rte_eth_dev_data *data = NULL;
263 struct pmd_internals *internals = NULL;
264 struct rte_eth_dev *eth_dev = NULL;
267 /* do some parameter checking */
268 if (rx_queues == NULL && nb_rx_queues > 0) {
272 if (tx_queues == NULL && nb_tx_queues > 0) {
276 if (nb_rx_queues > RTE_PMD_RING_MAX_RX_RINGS) {
281 RTE_LOG(INFO, PMD, "Creating rings-backed ethdev on numa socket %u\n",
284 /* now do all data allocation - for eth_dev structure, dummy pci driver
285 * and internal (private) data
287 data = rte_zmalloc_socket(name, sizeof(*data), 0, numa_node);
293 data->rx_queues = rte_zmalloc_socket(name,
294 sizeof(void *) * nb_rx_queues, 0, numa_node);
295 if (data->rx_queues == NULL) {
300 data->tx_queues = rte_zmalloc_socket(name,
301 sizeof(void *) * nb_tx_queues, 0, numa_node);
302 if (data->tx_queues == NULL) {
307 internals = rte_zmalloc_socket(name, sizeof(*internals), 0, numa_node);
308 if (internals == NULL) {
313 /* reserve an ethdev entry */
314 eth_dev = rte_eth_dev_allocate(name, RTE_ETH_DEV_VIRTUAL);
315 if (eth_dev == NULL) {
320 /* now put it all together
321 * - store queue data in internals,
322 * - store numa_node info in eth_dev_data
323 * - point eth_dev_data to internals
324 * - and point eth_dev structure to new eth_dev_data structure
326 /* NOTE: we'll replace the data element, of originally allocated eth_dev
327 * so the rings are local per-process */
329 internals->max_rx_queues = nb_rx_queues;
330 internals->max_tx_queues = nb_tx_queues;
331 for (i = 0; i < nb_rx_queues; i++) {
332 internals->rx_ring_queues[i].rng = rx_queues[i];
333 data->rx_queues[i] = &internals->rx_ring_queues[i];
335 for (i = 0; i < nb_tx_queues; i++) {
336 internals->tx_ring_queues[i].rng = tx_queues[i];
337 data->tx_queues[i] = &internals->tx_ring_queues[i];
340 data->dev_private = internals;
341 data->port_id = eth_dev->data->port_id;
342 memmove(data->name, eth_dev->data->name, sizeof(data->name));
343 data->nb_rx_queues = (uint16_t)nb_rx_queues;
344 data->nb_tx_queues = (uint16_t)nb_tx_queues;
345 data->dev_link = pmd_link;
346 data->mac_addrs = &internals->address;
348 eth_dev->data = data;
349 eth_dev->driver = NULL;
350 eth_dev->dev_ops = &ops;
351 data->dev_flags = RTE_ETH_DEV_DETACHABLE;
352 data->kdrv = RTE_KDRV_NONE;
353 data->drv_name = drivername;
354 data->numa_node = numa_node;
356 TAILQ_INIT(&(eth_dev->link_intr_cbs));
358 /* finally assign rx and tx ops */
359 eth_dev->rx_pkt_burst = eth_ring_rx;
360 eth_dev->tx_pkt_burst = eth_ring_tx;
362 return data->port_id;
366 rte_free(data->rx_queues);
367 rte_free(data->tx_queues);
376 rte_eth_from_ring(struct rte_ring *r)
378 return rte_eth_from_rings(r->name, &r, 1, &r, 1,
379 r->memzone ? r->memzone->socket_id : SOCKET_ID_ANY);
388 eth_dev_ring_create(const char *name, const unsigned numa_node,
389 enum dev_action action)
391 /* rx and tx are so-called from point of view of first port.
392 * They are inverted from the point of view of second port
394 struct rte_ring *rxtx[RTE_PMD_RING_MAX_RX_RINGS];
396 char rng_name[RTE_RING_NAMESIZE];
397 unsigned num_rings = RTE_MIN(RTE_PMD_RING_MAX_RX_RINGS,
398 RTE_PMD_RING_MAX_TX_RINGS);
400 for (i = 0; i < num_rings; i++) {
401 snprintf(rng_name, sizeof(rng_name), "ETH_RXTX%u_%s", i, name);
402 rxtx[i] = (action == DEV_CREATE) ?
403 rte_ring_create(rng_name, 1024, numa_node,
404 RING_F_SP_ENQ|RING_F_SC_DEQ) :
405 rte_ring_lookup(rng_name);
410 if (rte_eth_from_rings(name, rxtx, num_rings, rxtx, num_rings, numa_node) < 0)
416 struct node_action_pair {
419 enum dev_action action;
422 struct node_action_list {
425 struct node_action_pair *list;
428 static int parse_kvlist (const char *key __rte_unused, const char *value, void *data)
430 struct node_action_list *info = data;
437 name = strdup(value);
442 RTE_LOG(WARNING, PMD, "command line paramter is empty for ring pmd!\n");
446 node = strchr(name, ':');
448 RTE_LOG(WARNING, PMD, "could not parse node value from %s", name);
455 action = strchr(node, ':');
457 RTE_LOG(WARNING, PMD, "could not action value from %s", node);
465 * Need to do some sanity checking here
468 if (strcmp(action, ETH_RING_ACTION_ATTACH) == 0)
469 info->list[info->count].action = DEV_ATTACH;
470 else if (strcmp(action, ETH_RING_ACTION_CREATE) == 0)
471 info->list[info->count].action = DEV_CREATE;
476 info->list[info->count].node = strtol(node, &end, 10);
478 if ((errno != 0) || (*end != '\0')) {
479 RTE_LOG(WARNING, PMD, "node value %s is unparseable as a number\n", node);
483 snprintf(info->list[info->count].name, sizeof(info->list[info->count].name), "%s", name);
494 rte_pmd_ring_devinit(const char *name, const char *params)
496 struct rte_kvargs *kvlist = NULL;
498 struct node_action_list *info = NULL;
500 RTE_LOG(INFO, PMD, "Initializing pmd_ring for %s\n", name);
502 if (params == NULL || params[0] == '\0') {
503 ret = eth_dev_ring_create(name, rte_socket_id(), DEV_CREATE);
506 "Attach to pmd_ring for %s\n", name);
507 ret = eth_dev_ring_create(name, rte_socket_id(),
512 kvlist = rte_kvargs_parse(params, valid_arguments);
515 RTE_LOG(INFO, PMD, "Ignoring unsupported parameters when creating"
516 " rings-backed ethernet device\n");
517 ret = eth_dev_ring_create(name, rte_socket_id(),
521 "Attach to pmd_ring for %s\n",
523 ret = eth_dev_ring_create(name, rte_socket_id(),
528 ret = rte_kvargs_count(kvlist, ETH_RING_NUMA_NODE_ACTION_ARG);
529 info = rte_zmalloc("struct node_action_list",
530 sizeof(struct node_action_list) +
531 (sizeof(struct node_action_pair) * ret),
537 info->list = (struct node_action_pair*)(info + 1);
539 ret = rte_kvargs_process(kvlist, ETH_RING_NUMA_NODE_ACTION_ARG,
545 for (info->count = 0; info->count < info->total; info->count++) {
546 ret = eth_dev_ring_create(name,
547 info->list[info->count].node,
548 info->list[info->count].action);
550 (info->list[info->count].action == DEV_CREATE)) {
552 "Attach to pmd_ring for %s\n",
554 ret = eth_dev_ring_create(name,
555 info->list[info->count].node,
563 rte_kvargs_free(kvlist);
569 rte_pmd_ring_devuninit(const char *name)
571 struct rte_eth_dev *eth_dev = NULL;
573 RTE_LOG(INFO, PMD, "Un-Initializing pmd_ring for %s\n", name);
578 /* find an ethdev entry */
579 eth_dev = rte_eth_dev_allocated(name);
583 eth_dev_stop(eth_dev);
586 rte_free(eth_dev->data->rx_queues);
587 rte_free(eth_dev->data->tx_queues);
588 rte_free(eth_dev->data->dev_private);
590 rte_free(eth_dev->data);
592 rte_eth_dev_release_port(eth_dev);
596 static struct rte_driver pmd_ring_drv = {
599 .init = rte_pmd_ring_devinit,
600 .uninit = rte_pmd_ring_devuninit,
603 PMD_REGISTER_DRIVER(pmd_ring_drv);