6fd3d0a69b98ae68cd11573d7f3c85c747bfec90
[dpdk.git] / drivers / net / ring / rte_eth_ring.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 #include "rte_eth_ring.h"
35 #include <rte_mbuf.h>
36 #include <rte_ethdev.h>
37 #include <rte_malloc.h>
38 #include <rte_memcpy.h>
39 #include <rte_string_fns.h>
40 #include <rte_dev.h>
41 #include <rte_kvargs.h>
42
43 #define ETH_RING_NUMA_NODE_ACTION_ARG   "nodeaction"
44 #define ETH_RING_ACTION_CREATE          "CREATE"
45 #define ETH_RING_ACTION_ATTACH          "ATTACH"
46
47 static const char *ring_ethdev_driver_name = "Ring PMD";
48
49 static const char *valid_arguments[] = {
50         ETH_RING_NUMA_NODE_ACTION_ARG,
51         NULL
52 };
53
54 struct ring_queue {
55         struct rte_ring *rng;
56         rte_atomic64_t rx_pkts;
57         rte_atomic64_t tx_pkts;
58         rte_atomic64_t err_pkts;
59 };
60
61 struct pmd_internals {
62         unsigned nb_rx_queues;
63         unsigned nb_tx_queues;
64
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];
67
68         struct ether_addr address;
69 };
70
71
72 static const char *drivername = "Rings PMD";
73 static struct rte_eth_link pmd_link = {
74                 .link_speed = 10000,
75                 .link_duplex = ETH_LINK_FULL_DUPLEX,
76                 .link_status = 0
77 };
78
79 static uint16_t
80 eth_ring_rx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
81 {
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,
85                         ptrs, nb_bufs);
86         if (r->rng->flags & RING_F_SC_DEQ)
87                 r->rx_pkts.cnt += nb_rx;
88         else
89                 rte_atomic64_add(&(r->rx_pkts), nb_rx);
90         return nb_rx;
91 }
92
93 static uint16_t
94 eth_ring_tx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
95 {
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,
99                         ptrs, nb_bufs);
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;
103         } else {
104                 rte_atomic64_add(&(r->tx_pkts), nb_tx);
105                 rte_atomic64_add(&(r->err_pkts), nb_bufs - nb_tx);
106         }
107         return nb_tx;
108 }
109
110 static int
111 eth_dev_configure(struct rte_eth_dev *dev __rte_unused) { return 0; }
112
113 static int
114 eth_dev_start(struct rte_eth_dev *dev)
115 {
116         dev->data->dev_link.link_status = 1;
117         return 0;
118 }
119
120 static void
121 eth_dev_stop(struct rte_eth_dev *dev)
122 {
123         dev->data->dev_link.link_status = 0;
124 }
125
126 static int
127 eth_dev_set_link_down(struct rte_eth_dev *dev)
128 {
129         dev->data->dev_link.link_status = 0;
130         return 0;
131 }
132
133 static int
134 eth_dev_set_link_up(struct rte_eth_dev *dev)
135 {
136         dev->data->dev_link.link_status = 1;
137         return 0;
138 }
139
140 static int
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)
146 {
147         struct pmd_internals *internals = dev->data->dev_private;
148         dev->data->rx_queues[rx_queue_id] = &internals->rx_ring_queues[rx_queue_id];
149         return 0;
150 }
151
152 static int
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)
157 {
158         struct pmd_internals *internals = dev->data->dev_private;
159         dev->data->tx_queues[tx_queue_id] = &internals->tx_ring_queues[tx_queue_id];
160         return 0;
161 }
162
163
164 static void
165 eth_dev_info(struct rte_eth_dev *dev,
166                 struct rte_eth_dev_info *dev_info)
167 {
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->nb_rx_queues;
173         dev_info->max_tx_queues = (uint16_t)internals->nb_tx_queues;
174         dev_info->min_rx_bufsize = 0;
175         dev_info->pci_dev = NULL;
176 }
177
178 static void
179 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *igb_stats)
180 {
181         unsigned i;
182         unsigned long rx_total = 0, tx_total = 0, tx_err_total = 0;
183         const struct pmd_internals *internal = dev->data->dev_private;
184
185         for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
186                         i < internal->nb_rx_queues; i++) {
187                 igb_stats->q_ipackets[i] = internal->rx_ring_queues[i].rx_pkts.cnt;
188                 rx_total += igb_stats->q_ipackets[i];
189         }
190
191         for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
192                         i < internal->nb_tx_queues; i++) {
193                 igb_stats->q_opackets[i] = internal->tx_ring_queues[i].tx_pkts.cnt;
194                 igb_stats->q_errors[i] = internal->tx_ring_queues[i].err_pkts.cnt;
195                 tx_total += igb_stats->q_opackets[i];
196                 tx_err_total += igb_stats->q_errors[i];
197         }
198
199         igb_stats->ipackets = rx_total;
200         igb_stats->opackets = tx_total;
201         igb_stats->oerrors = tx_err_total;
202 }
203
204 static void
205 eth_stats_reset(struct rte_eth_dev *dev)
206 {
207         unsigned i;
208         struct pmd_internals *internal = dev->data->dev_private;
209         for (i = 0; i < internal->nb_rx_queues; i++)
210                 internal->rx_ring_queues[i].rx_pkts.cnt = 0;
211         for (i = 0; i < internal->nb_tx_queues; i++) {
212                 internal->tx_ring_queues[i].tx_pkts.cnt = 0;
213                 internal->tx_ring_queues[i].err_pkts.cnt = 0;
214         }
215 }
216
217 static void
218 eth_mac_addr_remove(struct rte_eth_dev *dev __rte_unused,
219         uint32_t index __rte_unused)
220 {
221 }
222
223 static void
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)
228 {
229 }
230
231 static void
232 eth_queue_release(void *q __rte_unused) { ; }
233 static int
234 eth_link_update(struct rte_eth_dev *dev __rte_unused,
235                 int wait_to_complete __rte_unused) { return 0; }
236
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,
253 };
254
255 static struct eth_driver rte_ring_pmd = {
256         .pci_drv = {
257                 .name = "rte_ring_pmd",
258                 .drv_flags = RTE_PCI_DRV_DETACHABLE,
259         },
260 };
261
262 static struct rte_pci_id id_table;
263
264 int
265 rte_eth_from_rings(const char *name, struct rte_ring *const rx_queues[],
266                 const unsigned nb_rx_queues,
267                 struct rte_ring *const tx_queues[],
268                 const unsigned nb_tx_queues,
269                 const unsigned numa_node)
270 {
271         struct rte_eth_dev_data *data = NULL;
272         struct rte_pci_device *pci_dev = NULL;
273         struct pmd_internals *internals = NULL;
274         struct rte_eth_dev *eth_dev = NULL;
275
276         unsigned i;
277
278         /* do some parameter checking */
279         if (rx_queues == NULL && nb_rx_queues > 0)
280                 goto error;
281         if (tx_queues == NULL && nb_tx_queues > 0)
282                 goto error;
283
284         RTE_LOG(INFO, PMD, "Creating rings-backed ethdev on numa socket %u\n",
285                         numa_node);
286
287         /* now do all data allocation - for eth_dev structure, dummy pci driver
288          * and internal (private) data
289          */
290         data = rte_zmalloc_socket(name, sizeof(*data), 0, numa_node);
291         if (data == NULL)
292                 goto error;
293
294         pci_dev = rte_zmalloc_socket(name, sizeof(*pci_dev), 0, numa_node);
295         if (pci_dev == NULL)
296                 goto error;
297
298         internals = rte_zmalloc_socket(name, sizeof(*internals), 0, numa_node);
299         if (internals == NULL)
300                 goto error;
301
302         /* reserve an ethdev entry */
303         eth_dev = rte_eth_dev_allocate(name, RTE_ETH_DEV_VIRTUAL);
304         if (eth_dev == NULL)
305                 goto error;
306
307
308         /* now put it all together
309          * - store queue data in internals,
310          * - store numa_node info in pci_driver
311          * - point eth_dev_data to internals and pci_driver
312          * - and point eth_dev structure to new eth_dev_data structure
313          */
314         /* NOTE: we'll replace the data element, of originally allocated eth_dev
315          * so the rings are local per-process */
316
317         internals->nb_rx_queues = nb_rx_queues;
318         internals->nb_tx_queues = nb_tx_queues;
319         for (i = 0; i < nb_rx_queues; i++) {
320                 internals->rx_ring_queues[i].rng = rx_queues[i];
321         }
322         for (i = 0; i < nb_tx_queues; i++) {
323                 internals->tx_ring_queues[i].rng = tx_queues[i];
324         }
325
326         rte_ring_pmd.pci_drv.name = ring_ethdev_driver_name;
327         rte_ring_pmd.pci_drv.id_table = &id_table;
328
329         pci_dev->numa_node = numa_node;
330         pci_dev->driver = &rte_ring_pmd.pci_drv;
331
332         data->dev_private = internals;
333         data->port_id = eth_dev->data->port_id;
334         memmove(data->name, eth_dev->data->name, sizeof(data->name));
335         data->nb_rx_queues = (uint16_t)nb_rx_queues;
336         data->nb_tx_queues = (uint16_t)nb_tx_queues;
337         data->dev_link = pmd_link;
338         data->mac_addrs = &internals->address;
339
340         eth_dev->data = data;
341         eth_dev->driver = &rte_ring_pmd;
342         eth_dev->dev_ops = &ops;
343         eth_dev->pci_dev = pci_dev;
344         TAILQ_INIT(&(eth_dev->link_intr_cbs));
345
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;
349
350         return data->port_id;
351
352 error:
353         rte_free(data);
354         rte_free(pci_dev);
355         rte_free(internals);
356
357         return -1;
358 }
359
360 enum dev_action{
361         DEV_CREATE,
362         DEV_ATTACH
363 };
364
365 static int
366 eth_dev_ring_create(const char *name, const unsigned numa_node,
367                 enum dev_action action)
368 {
369         /* rx and tx are so-called from point of view of first port.
370          * They are inverted from the point of view of second port
371          */
372         struct rte_ring *rxtx[RTE_PMD_RING_MAX_RX_RINGS];
373         unsigned i;
374         char rng_name[RTE_RING_NAMESIZE];
375         unsigned num_rings = RTE_MIN(RTE_PMD_RING_MAX_RX_RINGS,
376                         RTE_PMD_RING_MAX_TX_RINGS);
377
378         for (i = 0; i < num_rings; i++) {
379                 snprintf(rng_name, sizeof(rng_name), "ETH_RXTX%u_%s", i, name);
380                 rxtx[i] = (action == DEV_CREATE) ?
381                                 rte_ring_create(rng_name, 1024, numa_node,
382                                                 RING_F_SP_ENQ|RING_F_SC_DEQ) :
383                                 rte_ring_lookup(rng_name);
384                 if (rxtx[i] == NULL)
385                         return -1;
386         }
387
388         if (rte_eth_from_rings(name, rxtx, num_rings, rxtx, num_rings, numa_node) < 0)
389                 return -1;
390
391         return 0;
392 }
393
394
395 static int
396 eth_dev_ring_pair_create(const char *name, const unsigned numa_node,
397                 enum dev_action action)
398 {
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
401          */
402         struct rte_ring *rx[RTE_PMD_RING_MAX_RX_RINGS];
403         struct rte_ring *tx[RTE_PMD_RING_MAX_TX_RINGS];
404         unsigned i;
405         char rx_rng_name[RTE_RING_NAMESIZE];
406         char tx_rng_name[RTE_RING_NAMESIZE];
407         unsigned num_rings = RTE_MIN(RTE_PMD_RING_MAX_RX_RINGS,
408                         RTE_PMD_RING_MAX_TX_RINGS);
409
410         for (i = 0; i < num_rings; i++) {
411                 snprintf(rx_rng_name, sizeof(rx_rng_name), "ETH_RX%u_%s", i, name);
412                 rx[i] = (action == DEV_CREATE) ?
413                                 rte_ring_create(rx_rng_name, 1024, numa_node,
414                                                 RING_F_SP_ENQ|RING_F_SC_DEQ) :
415                                 rte_ring_lookup(rx_rng_name);
416                 if (rx[i] == NULL)
417                         return -1;
418                 snprintf(tx_rng_name, sizeof(tx_rng_name), "ETH_TX%u_%s", i, name);
419                 tx[i] = (action == DEV_CREATE) ?
420                                 rte_ring_create(tx_rng_name, 1024, numa_node,
421                                                 RING_F_SP_ENQ|RING_F_SC_DEQ):
422                                 rte_ring_lookup(tx_rng_name);
423                 if (tx[i] == NULL)
424                         return -1;
425         }
426
427         if (rte_eth_from_rings(rx_rng_name, rx, num_rings, tx, num_rings,
428                                 numa_node) < 0 ||
429                         rte_eth_from_rings(tx_rng_name, tx, num_rings, rx,
430                                 num_rings, numa_node) < 0)
431                 return -1;
432
433         return 0;
434 }
435
436 int
437 rte_eth_ring_pair_create(const char *name, const unsigned numa_node)
438 {
439         RTE_LOG(WARNING, PMD, "rte_eth_ring_pair_create is deprecated\n");
440         return eth_dev_ring_pair_create(name, numa_node, DEV_CREATE);
441 }
442
443 int
444 rte_eth_ring_pair_attach(const char *name, const unsigned numa_node)
445 {
446         RTE_LOG(WARNING, PMD, "rte_eth_ring_pair_attach is deprecated\n");
447         return eth_dev_ring_pair_create(name, numa_node, DEV_ATTACH);
448 }
449
450 struct node_action_pair {
451         char name[PATH_MAX];
452         unsigned node;
453         enum dev_action action;
454 };
455
456 struct node_action_list {
457         unsigned total;
458         unsigned count;
459         struct node_action_pair *list;
460 };
461
462 static int parse_kvlist (const char *key __rte_unused, const char *value, void *data)
463 {
464         struct node_action_list *info = data;
465         int ret;
466         char *name;
467         char *action;
468         char *node;
469         char *end;
470
471         name = strdup(value);
472
473         ret = -EINVAL;
474
475         if (!name) {
476                 RTE_LOG(WARNING, PMD, "command line paramter is empty for ring pmd!\n");
477                 goto out;
478         }
479
480         node = strchr(name, ':');
481         if (!node) {
482                 RTE_LOG(WARNING, PMD, "could not parse node value from %s", name);
483                 goto out;
484         }
485
486         *node = '\0';
487         node++;
488
489         action = strchr(node, ':');
490         if (!action) {
491                 RTE_LOG(WARNING, PMD, "could not action value from %s", node);
492                 goto out;
493         }
494
495         *action = '\0';
496         action++;
497
498         /*
499          * Need to do some sanity checking here
500          */
501
502         if (strcmp(action, ETH_RING_ACTION_ATTACH) == 0)
503                 info->list[info->count].action = DEV_ATTACH;
504         else if (strcmp(action, ETH_RING_ACTION_CREATE) == 0)
505                 info->list[info->count].action = DEV_CREATE;
506         else
507                 goto out;
508
509         errno = 0;
510         info->list[info->count].node = strtol(node, &end, 10);
511
512         if ((errno != 0) || (*end != '\0')) {
513                 RTE_LOG(WARNING, PMD, "node value %s is unparseable as a number\n", node);
514                 goto out;
515         }
516
517         snprintf(info->list[info->count].name, sizeof(info->list[info->count].name), "%s", name);
518
519         info->count++;
520
521         ret = 0;
522 out:
523         free(name);
524         return ret;
525 }
526
527 static int
528 rte_pmd_ring_devinit(const char *name, const char *params)
529 {
530         struct rte_kvargs *kvlist = NULL;
531         int ret = 0;
532         struct node_action_list *info = NULL;
533
534         RTE_LOG(INFO, PMD, "Initializing pmd_ring for %s\n", name);
535
536         if (params == NULL || params[0] == '\0') {
537                 ret = eth_dev_ring_create(name, rte_socket_id(), DEV_CREATE);
538                 if (ret == -1) {
539                         RTE_LOG(INFO, PMD,
540                                 "Attach to pmd_ring for %s\n", name);
541                         ret = eth_dev_ring_create(name, rte_socket_id(),
542                                                   DEV_ATTACH);
543                 }
544         }
545         else {
546                 kvlist = rte_kvargs_parse(params, valid_arguments);
547
548                 if (!kvlist) {
549                         RTE_LOG(INFO, PMD, "Ignoring unsupported parameters when creating"
550                                         " rings-backed ethernet device\n");
551                         ret = eth_dev_ring_create(name, rte_socket_id(),
552                                                   DEV_CREATE);
553                         if (ret == -1) {
554                                 RTE_LOG(INFO, PMD,
555                                         "Attach to pmd_ring for %s\n",
556                                         name);
557                                 ret = eth_dev_ring_create(name, rte_socket_id(),
558                                                           DEV_ATTACH);
559                         }
560                         return ret;
561                 } else {
562                         ret = rte_kvargs_count(kvlist, ETH_RING_NUMA_NODE_ACTION_ARG);
563                         info = rte_zmalloc("struct node_action_list",
564                                            sizeof(struct node_action_list) +
565                                            (sizeof(struct node_action_pair) * ret),
566                                            0);
567                         if (!info)
568                                 goto out_free;
569
570                         info->total = ret;
571                         info->list = (struct node_action_pair*)(info + 1);
572
573                         ret = rte_kvargs_process(kvlist, ETH_RING_NUMA_NODE_ACTION_ARG,
574                                                  parse_kvlist, info);
575
576                         if (ret < 0)
577                                 goto out_free;
578
579                         for (info->count = 0; info->count < info->total; info->count++) {
580                                 ret = eth_dev_ring_create(name,
581                                                           info->list[info->count].node,
582                                                           info->list[info->count].action);
583                                 if ((ret == -1) &&
584                                     (info->list[info->count].action == DEV_CREATE)) {
585                                         RTE_LOG(INFO, PMD,
586                                                 "Attach to pmd_ring for %s\n",
587                                                 name);
588                                         ret = eth_dev_ring_create(name,
589                                                         info->list[info->count].node,
590                                                         DEV_ATTACH);
591                                 }
592                         }
593                 }
594         }
595
596 out_free:
597         rte_kvargs_free(kvlist);
598         rte_free(info);
599         return ret;
600 }
601
602 static int
603 rte_pmd_ring_devuninit(const char *name)
604 {
605         struct rte_eth_dev *eth_dev = NULL;
606
607         RTE_LOG(INFO, PMD, "Un-Initializing pmd_ring for %s\n", name);
608
609         if (name == NULL)
610                 return -EINVAL;
611
612         /* find an ethdev entry */
613         eth_dev = rte_eth_dev_allocated(name);
614         if (eth_dev == NULL)
615                 return -ENODEV;
616
617         eth_dev_stop(eth_dev);
618         rte_free(eth_dev->data->dev_private);
619         rte_free(eth_dev->data);
620         rte_free(eth_dev->pci_dev);
621
622         rte_eth_dev_release_port(eth_dev);
623         return 0;
624 }
625
626 static struct rte_driver pmd_ring_drv = {
627         .name = "eth_ring",
628         .type = PMD_VDEV,
629         .init = rte_pmd_ring_devinit,
630         .uninit = rte_pmd_ring_devuninit,
631 };
632
633 PMD_REGISTER_DRIVER(pmd_ring_drv);