update Intel copyright years to 2014
[dpdk.git] / lib / librte_pmd_ring / rte_eth_ring.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2014 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
41 struct ring_queue {
42         struct rte_ring *rng;
43         rte_atomic64_t rx_pkts;
44         rte_atomic64_t tx_pkts;
45         rte_atomic64_t err_pkts;
46 };
47
48 struct pmd_internals {
49         unsigned nb_rx_queues;
50         unsigned nb_tx_queues;
51
52         struct ring_queue rx_ring_queues[RTE_PMD_RING_MAX_RX_RINGS];
53         struct ring_queue tx_ring_queues[RTE_PMD_RING_MAX_TX_RINGS];
54 };
55
56
57 static struct ether_addr eth_addr = { .addr_bytes = {0} };
58 static const char *drivername = "Rings PMD";
59 static struct rte_eth_link pmd_link = {
60                 .link_speed = 10000,
61                 .link_duplex = ETH_LINK_FULL_DUPLEX,
62                 .link_status = 0
63 };
64
65 static uint16_t
66 eth_ring_rx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
67 {
68         void **ptrs = (void *)&bufs[0];
69         struct ring_queue *r = q;
70         const uint16_t nb_rx = (uint16_t)rte_ring_dequeue_burst(r->rng, 
71                         ptrs, nb_bufs);
72         if (r->rng->flags & RING_F_SC_DEQ)
73                 r->rx_pkts.cnt += nb_rx;
74         else
75                 rte_atomic64_add(&(r->rx_pkts), nb_rx);
76         return nb_rx;
77 }
78
79 static uint16_t
80 eth_ring_tx(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_tx = (uint16_t)rte_ring_enqueue_burst(r->rng, 
85                         ptrs, nb_bufs);
86         if (r->rng->flags & RING_F_SP_ENQ) {
87                 r->tx_pkts.cnt += nb_tx;
88                 r->err_pkts.cnt += nb_bufs - nb_tx;
89         } else {
90                 rte_atomic64_add(&(r->tx_pkts), nb_tx);
91                 rte_atomic64_add(&(r->err_pkts), nb_bufs - nb_tx);
92         }
93         return nb_tx;
94 }
95
96 static int
97 eth_dev_configure(struct rte_eth_dev *dev __rte_unused) { return 0; }
98
99 static int
100 eth_dev_start(struct rte_eth_dev *dev)
101 {
102         dev->data->dev_link.link_status = 1;
103         return 0;
104 }
105
106 static void
107 eth_dev_stop(struct rte_eth_dev *dev)
108 {
109         dev->data->dev_link.link_status = 0;
110 }
111
112 static int
113 eth_rx_queue_setup(struct rte_eth_dev *dev,uint16_t rx_queue_id,
114                                     uint16_t nb_rx_desc __rte_unused,
115                                     unsigned int socket_id __rte_unused,
116                                     const struct rte_eth_rxconf *rx_conf __rte_unused,
117                                     struct rte_mempool *mb_pool __rte_unused)
118 {
119         struct pmd_internals *internals = dev->data->dev_private;
120         dev->data->rx_queues[rx_queue_id] = &internals->rx_ring_queues[rx_queue_id];
121         return 0;
122 }
123
124 static int
125 eth_tx_queue_setup(struct rte_eth_dev *dev, uint16_t tx_queue_id,
126                                     uint16_t nb_tx_desc __rte_unused,
127                                     unsigned int socket_id __rte_unused,
128                                     const struct rte_eth_txconf *tx_conf __rte_unused)
129 {
130         struct pmd_internals *internals = dev->data->dev_private;
131         dev->data->tx_queues[tx_queue_id] = &internals->tx_ring_queues[tx_queue_id];
132         return 0;
133 }
134
135
136 static void
137 eth_dev_info(struct rte_eth_dev *dev,
138                 struct rte_eth_dev_info *dev_info)
139 {
140         struct pmd_internals *internals = dev->data->dev_private;
141         dev_info->driver_name = drivername;
142         dev_info->max_mac_addrs = 1;
143         dev_info->max_rx_pktlen = (uint32_t)-1;
144         dev_info->max_rx_queues = (uint16_t)internals->nb_rx_queues;
145         dev_info->max_tx_queues = (uint16_t)internals->nb_tx_queues;
146         dev_info->min_rx_bufsize = 0;
147         dev_info->pci_dev = NULL;
148 }
149
150 static void
151 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *igb_stats)
152 {
153         unsigned i;
154         unsigned long rx_total = 0, tx_total = 0, tx_err_total = 0;
155         const struct pmd_internals *internal = dev->data->dev_private;
156
157         memset(igb_stats, 0, sizeof(*igb_stats));
158         for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
159                         i < internal->nb_rx_queues; i++) {
160                 igb_stats->q_ipackets[i] = internal->rx_ring_queues[i].rx_pkts.cnt;
161                 rx_total += igb_stats->q_ipackets[i];
162         }
163
164         for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
165                         i < internal->nb_tx_queues; i++) {
166                 igb_stats->q_opackets[i] = internal->tx_ring_queues[i].tx_pkts.cnt;
167                 igb_stats->q_errors[i] = internal->tx_ring_queues[i].err_pkts.cnt;
168                 tx_total += igb_stats->q_opackets[i];
169                 tx_err_total += igb_stats->q_errors[i];
170         }
171
172         igb_stats->ipackets = rx_total;
173         igb_stats->opackets = tx_total;
174         igb_stats->oerrors = tx_err_total;
175 }
176
177 static void
178 eth_stats_reset(struct rte_eth_dev *dev)
179 {
180         unsigned i;
181         struct pmd_internals *internal = dev->data->dev_private;
182         for (i = 0; i < internal->nb_rx_queues; i++)
183                 internal->rx_ring_queues[i].rx_pkts.cnt = 0;
184         for (i = 0; i < internal->nb_tx_queues; i++) {
185                 internal->tx_ring_queues[i].tx_pkts.cnt = 0;
186                 internal->tx_ring_queues[i].err_pkts.cnt = 0;
187         }
188 }
189
190 static void
191 eth_queue_release(void *q __rte_unused) { ; }
192 static int
193 eth_link_update(struct rte_eth_dev *dev __rte_unused,
194                 int wait_to_complete __rte_unused) { return 0; }
195
196 static struct eth_dev_ops ops = {
197                 .dev_start = eth_dev_start,
198                 .dev_stop = eth_dev_stop,
199                 .dev_configure = eth_dev_configure,
200                 .dev_infos_get = eth_dev_info,
201                 .rx_queue_setup = eth_rx_queue_setup,
202                 .tx_queue_setup = eth_tx_queue_setup,
203                 .rx_queue_release = eth_queue_release,
204                 .tx_queue_release = eth_queue_release,
205                 .link_update = eth_link_update,
206                 .stats_get = eth_stats_get,
207                 .stats_reset = eth_stats_reset,
208 };
209
210 int
211 rte_eth_from_rings(struct rte_ring *const rx_queues[],
212                 const unsigned nb_rx_queues,
213                 struct rte_ring *const tx_queues[],
214                 const unsigned nb_tx_queues,
215                 const unsigned numa_node)
216 {
217         struct rte_eth_dev_data *data = NULL;
218         struct rte_pci_device *pci_dev = NULL;
219         struct pmd_internals *internals = NULL;
220         struct rte_eth_dev *eth_dev = NULL;
221         unsigned i;
222
223         /* do some paramter checking */
224         if (rx_queues == NULL && nb_rx_queues > 0)
225                 goto error;
226         if (tx_queues == NULL && nb_tx_queues > 0)
227                 goto error;
228
229         RTE_LOG(INFO, PMD, "Creating rings-backed ethdev on numa socket %u\n",
230                         numa_node);
231
232         /* now do all data allocation - for eth_dev structure, dummy pci driver
233          * and internal (private) data
234          */
235         data = rte_zmalloc_socket(NULL, sizeof(*data), 0, numa_node);
236         if (data == NULL)
237                 goto error;
238
239         pci_dev = rte_zmalloc_socket(NULL, sizeof(*pci_dev), 0, numa_node);
240         if (pci_dev == NULL)
241                 goto error;
242
243         internals = rte_zmalloc_socket(NULL, sizeof(*internals), 0, numa_node);
244         if (internals == NULL)
245                 goto error;
246
247         /* reserve an ethdev entry */
248         eth_dev = rte_eth_dev_allocate();
249         if (eth_dev == NULL)
250                 goto error;
251
252         /* now put it all together
253          * - store queue data in internals,
254          * - store numa_node info in pci_driver
255          * - point eth_dev_data to internals and pci_driver
256          * - and point eth_dev structure to new eth_dev_data structure
257          */
258         /* NOTE: we'll replace the data element, of originally allocated eth_dev
259          * so the rings are local per-process */
260
261         internals->nb_rx_queues = nb_rx_queues;
262         internals->nb_tx_queues = nb_tx_queues;
263         for (i = 0; i < nb_rx_queues; i++) {
264                 internals->rx_ring_queues[i].rng = rx_queues[i];
265         }
266         for (i = 0; i < nb_tx_queues; i++) {
267                 internals->tx_ring_queues[i].rng = tx_queues[i];
268         }
269
270         pci_dev->numa_node = numa_node;
271
272         data->dev_private = internals;
273         data->port_id = eth_dev->data->port_id;
274         data->nb_rx_queues = (uint16_t)nb_rx_queues;
275         data->nb_tx_queues = (uint16_t)nb_tx_queues;
276         data->dev_link = pmd_link;
277         data->mac_addrs = &eth_addr;
278
279         eth_dev ->data = data;
280         eth_dev ->dev_ops = &ops;
281         eth_dev ->pci_dev = pci_dev;
282
283         /* finally assign rx and tx ops */
284         eth_dev->rx_pkt_burst = eth_ring_rx;
285         eth_dev->tx_pkt_burst = eth_ring_tx;
286
287         return 0;
288
289 error:
290         if (data)
291                 rte_free(data);
292         if (pci_dev)
293                 rte_free(pci_dev);
294         if (internals)
295                 rte_free(internals);
296         return -1;
297 }
298
299 enum dev_action{
300         DEV_CREATE,
301         DEV_ATTACH
302 };
303
304 static int
305 eth_dev_ring_create(const char *name, const unsigned numa_node,
306                 enum dev_action action)
307 {
308         /* rx and tx are so-called from point of view of first port.
309          * They are inverted from the point of view of second port
310          */
311         struct rte_ring *rxtx[RTE_PMD_RING_MAX_RX_RINGS];
312         unsigned i;
313         char rng_name[RTE_RING_NAMESIZE];
314         unsigned num_rings = RTE_MIN(RTE_PMD_RING_MAX_RX_RINGS,
315                         RTE_PMD_RING_MAX_TX_RINGS);
316
317         for (i = 0; i < num_rings; i++) {
318                 rte_snprintf(rng_name, sizeof(rng_name), "ETH_RXTX%u_%s", i, name);
319                 rxtx[i] = (action == DEV_CREATE) ?
320                                 rte_ring_create(rng_name, 1024, numa_node,
321                                                 RING_F_SP_ENQ|RING_F_SC_DEQ) :
322                                 rte_ring_lookup(rng_name);
323                 if (rxtx[i] == NULL)
324                         return -1;
325         }
326
327         if (rte_eth_from_rings(rxtx, num_rings, rxtx, num_rings, numa_node))
328                 return -1;
329
330         return 0;
331 }
332
333
334 static int
335 eth_dev_ring_pair_create(const char *name, const unsigned numa_node,
336                 enum dev_action action)
337 {
338         /* rx and tx are so-called from point of view of first port.
339          * They are inverted from the point of view of second port
340          */
341         struct rte_ring *rx[RTE_PMD_RING_MAX_RX_RINGS];
342         struct rte_ring *tx[RTE_PMD_RING_MAX_TX_RINGS];
343         unsigned i;
344         char rng_name[RTE_RING_NAMESIZE];
345         unsigned num_rings = RTE_MIN(RTE_PMD_RING_MAX_RX_RINGS,
346                         RTE_PMD_RING_MAX_TX_RINGS);
347
348         for (i = 0; i < num_rings; i++) {
349                 rte_snprintf(rng_name, sizeof(rng_name), "ETH_RX%u_%s", i, name);
350                 rx[i] = (action == DEV_CREATE) ?
351                                 rte_ring_create(rng_name, 1024, numa_node,
352                                                 RING_F_SP_ENQ|RING_F_SC_DEQ) :
353                                 rte_ring_lookup(rng_name);
354                 if (rx[i] == NULL)
355                         return -1;
356                 rte_snprintf(rng_name, sizeof(rng_name), "ETH_TX%u_%s", i, name);
357                 tx[i] = (action == DEV_CREATE) ?
358                                 rte_ring_create(rng_name, 1024, numa_node,
359                                                 RING_F_SP_ENQ|RING_F_SC_DEQ):
360                                 rte_ring_lookup(rng_name);
361                 if (tx[i] == NULL)
362                         return -1;
363         }
364
365         if (rte_eth_from_rings(rx, num_rings, tx, num_rings, numa_node) ||
366                         rte_eth_from_rings(tx, num_rings, rx, num_rings, numa_node) )
367                 return -1;
368
369         return 0;
370 }
371
372 int
373 rte_eth_ring_pair_create(const char *name, const unsigned numa_node)
374 {
375         return eth_dev_ring_pair_create(name, numa_node, DEV_CREATE);
376 }
377
378 int
379 rte_eth_ring_pair_attach(const char *name, const unsigned numa_node)
380 {
381         return eth_dev_ring_pair_create(name, numa_node, DEV_ATTACH);
382 }
383
384 int
385 rte_pmd_ring_init(const char *name, const char *params)
386 {
387         if (params == NULL)
388                 eth_dev_ring_create(name, rte_socket_id(), DEV_CREATE);
389         else {
390                 RTE_LOG(INFO, PMD, "Ignoring unsupported parameters when creating"
391                                 " rings-backed ethernet device\n");
392                 eth_dev_ring_create(name, rte_socket_id(), DEV_CREATE);
393         }
394         return 0;
395 }