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