ethdev: add return value to stats get dev op
[dpdk.git] / test / test / virtual_pmd.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_mbuf.h>
35 #include <rte_ethdev.h>
36 #include <rte_pci.h>
37 #include <rte_malloc.h>
38 #include <rte_memcpy.h>
39 #include <rte_memory.h>
40 #include <rte_ring.h>
41
42 #include "virtual_pmd.h"
43
44 #define MAX_PKT_BURST 512
45
46 static const char *virtual_ethdev_driver_name = "Virtual PMD";
47
48 struct virtual_ethdev_private {
49         struct eth_dev_ops dev_ops;
50         struct rte_eth_stats eth_stats;
51
52         struct rte_ring *rx_queue;
53         struct rte_ring *tx_queue;
54
55         int tx_burst_fail_count;
56 };
57
58 struct virtual_ethdev_queue {
59         int port_id;
60         int queue_id;
61 };
62
63 static int
64 virtual_ethdev_start_success(struct rte_eth_dev *eth_dev __rte_unused)
65 {
66         eth_dev->data->dev_started = 1;
67
68         return 0;
69 }
70
71 static int
72 virtual_ethdev_start_fail(struct rte_eth_dev *eth_dev __rte_unused)
73 {
74         eth_dev->data->dev_started = 0;
75
76         return -1;
77 }
78 static void  virtual_ethdev_stop(struct rte_eth_dev *eth_dev __rte_unused)
79 {
80         void *pkt = NULL;
81         struct virtual_ethdev_private *prv = eth_dev->data->dev_private;
82
83         eth_dev->data->dev_link.link_status = ETH_LINK_DOWN;
84         eth_dev->data->dev_started = 0;
85         while (rte_ring_dequeue(prv->rx_queue, &pkt) != -ENOENT)
86                 rte_pktmbuf_free(pkt);
87
88         while (rte_ring_dequeue(prv->tx_queue, &pkt) != -ENOENT)
89                 rte_pktmbuf_free(pkt);
90 }
91
92 static void
93 virtual_ethdev_close(struct rte_eth_dev *dev __rte_unused)
94 {}
95
96 static int
97 virtual_ethdev_configure_success(struct rte_eth_dev *dev __rte_unused)
98 {
99         return 0;
100 }
101
102 static int
103 virtual_ethdev_configure_fail(struct rte_eth_dev *dev __rte_unused)
104 {
105         return -1;
106 }
107
108 static void
109 virtual_ethdev_info_get(struct rte_eth_dev *dev __rte_unused,
110                 struct rte_eth_dev_info *dev_info)
111 {
112         dev_info->driver_name = virtual_ethdev_driver_name;
113         dev_info->max_mac_addrs = 1;
114
115         dev_info->max_rx_pktlen = (uint32_t)2048;
116
117         dev_info->max_rx_queues = (uint16_t)128;
118         dev_info->max_tx_queues = (uint16_t)512;
119
120         dev_info->min_rx_bufsize = 0;
121 }
122
123 static int
124 virtual_ethdev_rx_queue_setup_success(struct rte_eth_dev *dev,
125                 uint16_t rx_queue_id, uint16_t nb_rx_desc __rte_unused,
126                 unsigned int socket_id,
127                 const struct rte_eth_rxconf *rx_conf __rte_unused,
128                 struct rte_mempool *mb_pool __rte_unused)
129 {
130         struct virtual_ethdev_queue *rx_q;
131
132         rx_q = (struct virtual_ethdev_queue *)rte_zmalloc_socket(NULL,
133                         sizeof(struct virtual_ethdev_queue), 0, socket_id);
134
135         if (rx_q == NULL)
136                 return -1;
137
138         rx_q->port_id = dev->data->port_id;
139         rx_q->queue_id = rx_queue_id;
140
141         dev->data->rx_queues[rx_queue_id] = rx_q;
142
143         return 0;
144 }
145
146 static int
147 virtual_ethdev_rx_queue_setup_fail(struct rte_eth_dev *dev __rte_unused,
148                 uint16_t rx_queue_id __rte_unused, 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)
152 {
153         return -1;
154 }
155
156 static int
157 virtual_ethdev_tx_queue_setup_success(struct rte_eth_dev *dev,
158                 uint16_t tx_queue_id, uint16_t nb_tx_desc __rte_unused,
159                 unsigned int socket_id,
160                 const struct rte_eth_txconf *tx_conf __rte_unused)
161 {
162         struct virtual_ethdev_queue *tx_q;
163
164         tx_q = (struct virtual_ethdev_queue *)rte_zmalloc_socket(NULL,
165                         sizeof(struct virtual_ethdev_queue), 0, socket_id);
166
167         if (tx_q == NULL)
168                 return -1;
169
170         tx_q->port_id = dev->data->port_id;
171         tx_q->queue_id = tx_queue_id;
172
173         dev->data->tx_queues[tx_queue_id] = tx_q;
174
175         return 0;
176 }
177
178 static int
179 virtual_ethdev_tx_queue_setup_fail(struct rte_eth_dev *dev __rte_unused,
180                 uint16_t tx_queue_id __rte_unused, uint16_t nb_tx_desc __rte_unused,
181                 unsigned int socket_id __rte_unused,
182                 const struct rte_eth_txconf *tx_conf __rte_unused)
183 {
184         return -1;
185 }
186
187 static void
188 virtual_ethdev_rx_queue_release(void *q __rte_unused)
189 {
190 }
191
192 static void
193 virtual_ethdev_tx_queue_release(void *q __rte_unused)
194 {
195 }
196
197 static int
198 virtual_ethdev_link_update_success(struct rte_eth_dev *bonded_eth_dev,
199                 int wait_to_complete __rte_unused)
200 {
201         if (!bonded_eth_dev->data->dev_started)
202                 bonded_eth_dev->data->dev_link.link_status = ETH_LINK_DOWN;
203
204         return 0;
205 }
206
207 static int
208 virtual_ethdev_link_update_fail(struct rte_eth_dev *bonded_eth_dev __rte_unused,
209                 int wait_to_complete __rte_unused)
210 {
211         return -1;
212 }
213
214 static int
215 virtual_ethdev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
216 {
217         struct virtual_ethdev_private *dev_private = dev->data->dev_private;
218
219         if (stats)
220                 rte_memcpy(stats, &dev_private->eth_stats, sizeof(*stats));
221
222         return 0;
223 }
224
225 static void
226 virtual_ethdev_stats_reset(struct rte_eth_dev *dev)
227 {
228         struct virtual_ethdev_private *dev_private = dev->data->dev_private;
229         void *pkt = NULL;
230
231         while (rte_ring_dequeue(dev_private->tx_queue, &pkt) == -ENOBUFS)
232                         rte_pktmbuf_free(pkt);
233
234         /* Reset internal statistics */
235         memset(&dev_private->eth_stats, 0, sizeof(dev_private->eth_stats));
236 }
237
238 static void
239 virtual_ethdev_promiscuous_mode_enable(struct rte_eth_dev *dev __rte_unused)
240 {}
241
242 static void
243 virtual_ethdev_promiscuous_mode_disable(struct rte_eth_dev *dev __rte_unused)
244 {}
245
246
247 static const struct eth_dev_ops virtual_ethdev_default_dev_ops = {
248         .dev_configure = virtual_ethdev_configure_success,
249         .dev_start = virtual_ethdev_start_success,
250         .dev_stop = virtual_ethdev_stop,
251         .dev_close = virtual_ethdev_close,
252         .dev_infos_get = virtual_ethdev_info_get,
253         .rx_queue_setup = virtual_ethdev_rx_queue_setup_success,
254         .tx_queue_setup = virtual_ethdev_tx_queue_setup_success,
255         .rx_queue_release = virtual_ethdev_rx_queue_release,
256         .tx_queue_release = virtual_ethdev_tx_queue_release,
257         .link_update = virtual_ethdev_link_update_success,
258         .stats_get = virtual_ethdev_stats_get,
259         .stats_reset = virtual_ethdev_stats_reset,
260         .promiscuous_enable = virtual_ethdev_promiscuous_mode_enable,
261         .promiscuous_disable = virtual_ethdev_promiscuous_mode_disable
262 };
263
264
265 void
266 virtual_ethdev_start_fn_set_success(uint16_t port_id, uint8_t success)
267 {
268         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
269         struct virtual_ethdev_private *dev_private = dev->data->dev_private;
270         struct eth_dev_ops *dev_ops = &dev_private->dev_ops;
271
272         if (success)
273                 dev_ops->dev_start = virtual_ethdev_start_success;
274         else
275                 dev_ops->dev_start = virtual_ethdev_start_fail;
276
277 }
278
279 void
280 virtual_ethdev_configure_fn_set_success(uint16_t port_id, uint8_t success)
281 {
282         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
283         struct virtual_ethdev_private *dev_private = dev->data->dev_private;
284         struct eth_dev_ops *dev_ops = &dev_private->dev_ops;
285
286         if (success)
287                 dev_ops->dev_configure = virtual_ethdev_configure_success;
288         else
289                 dev_ops->dev_configure = virtual_ethdev_configure_fail;
290 }
291
292 void
293 virtual_ethdev_rx_queue_setup_fn_set_success(uint16_t port_id, uint8_t success)
294 {
295         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
296         struct virtual_ethdev_private *dev_private = dev->data->dev_private;
297         struct eth_dev_ops *dev_ops = &dev_private->dev_ops;
298
299         if (success)
300                 dev_ops->rx_queue_setup = virtual_ethdev_rx_queue_setup_success;
301         else
302                 dev_ops->rx_queue_setup = virtual_ethdev_rx_queue_setup_fail;
303 }
304
305 void
306 virtual_ethdev_tx_queue_setup_fn_set_success(uint16_t port_id, uint8_t success)
307 {
308         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
309         struct virtual_ethdev_private *dev_private = dev->data->dev_private;
310         struct eth_dev_ops *dev_ops = &dev_private->dev_ops;
311
312         if (success)
313                 dev_ops->tx_queue_setup = virtual_ethdev_tx_queue_setup_success;
314         else
315                 dev_ops->tx_queue_setup = virtual_ethdev_tx_queue_setup_fail;
316 }
317
318 void
319 virtual_ethdev_link_update_fn_set_success(uint16_t port_id, uint8_t success)
320 {
321         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
322         struct virtual_ethdev_private *dev_private = dev->data->dev_private;
323         struct eth_dev_ops *dev_ops = &dev_private->dev_ops;
324
325         if (success)
326                 dev_ops->link_update = virtual_ethdev_link_update_success;
327         else
328                 dev_ops->link_update = virtual_ethdev_link_update_fail;
329 }
330
331
332 static uint16_t
333 virtual_ethdev_rx_burst_success(void *queue __rte_unused,
334                                                          struct rte_mbuf **bufs,
335                                                          uint16_t nb_pkts)
336 {
337         struct rte_eth_dev *vrtl_eth_dev;
338         struct virtual_ethdev_queue *pq_map;
339         struct virtual_ethdev_private *dev_private;
340
341         int rx_count, i;
342
343         pq_map = (struct virtual_ethdev_queue *)queue;
344         vrtl_eth_dev = &rte_eth_devices[pq_map->port_id];
345         dev_private = vrtl_eth_dev->data->dev_private;
346
347         rx_count = rte_ring_dequeue_burst(dev_private->rx_queue, (void **) bufs,
348                         nb_pkts, NULL);
349
350         /* increments ipackets count */
351         dev_private->eth_stats.ipackets += rx_count;
352
353         /* increments ibytes count */
354         for (i = 0; i < rx_count; i++)
355                 dev_private->eth_stats.ibytes += rte_pktmbuf_pkt_len(bufs[i]);
356
357         return rx_count;
358 }
359
360 static uint16_t
361 virtual_ethdev_rx_burst_fail(void *queue __rte_unused,
362                                                          struct rte_mbuf **bufs __rte_unused,
363                                                          uint16_t nb_pkts __rte_unused)
364 {
365         return 0;
366 }
367
368 static uint16_t
369 virtual_ethdev_tx_burst_success(void *queue, struct rte_mbuf **bufs,
370                 uint16_t nb_pkts)
371 {
372         struct virtual_ethdev_queue *tx_q = queue;
373
374         struct rte_eth_dev *vrtl_eth_dev;
375         struct virtual_ethdev_private *dev_private;
376
377         int i;
378
379         vrtl_eth_dev = &rte_eth_devices[tx_q->port_id];
380         dev_private = vrtl_eth_dev->data->dev_private;
381
382         if (!vrtl_eth_dev->data->dev_link.link_status)
383                 nb_pkts = 0;
384         else
385                 nb_pkts = rte_ring_enqueue_burst(dev_private->tx_queue, (void **)bufs,
386                                 nb_pkts, NULL);
387
388         /* increment opacket count */
389         dev_private->eth_stats.opackets += nb_pkts;
390
391         /* increment obytes count */
392         for (i = 0; i < nb_pkts; i++)
393                 dev_private->eth_stats.obytes += rte_pktmbuf_pkt_len(bufs[i]);
394
395         return nb_pkts;
396 }
397
398 static uint16_t
399 virtual_ethdev_tx_burst_fail(void *queue, struct rte_mbuf **bufs,
400                 uint16_t nb_pkts)
401 {
402         struct rte_eth_dev *vrtl_eth_dev = NULL;
403         struct virtual_ethdev_queue *tx_q = NULL;
404         struct virtual_ethdev_private *dev_private = NULL;
405
406         int i;
407
408         tx_q = queue;
409         vrtl_eth_dev = &rte_eth_devices[tx_q->port_id];
410         dev_private = vrtl_eth_dev->data->dev_private;
411
412         if (dev_private->tx_burst_fail_count < nb_pkts) {
413                 int successfully_txd = nb_pkts - dev_private->tx_burst_fail_count;
414
415                 /* increment opacket count */
416                 dev_private->eth_stats.opackets += successfully_txd;
417
418                 /* free packets in burst */
419                 for (i = 0; i < successfully_txd; i++) {
420                         /* free packets in burst */
421                         if (bufs[i] != NULL)
422                                 rte_pktmbuf_free(bufs[i]);
423
424                         bufs[i] = NULL;
425                 }
426
427                 return successfully_txd;
428         }
429
430         return 0;
431 }
432
433
434 void
435 virtual_ethdev_rx_burst_fn_set_success(uint16_t port_id, uint8_t success)
436 {
437         struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
438
439         if (success)
440                 vrtl_eth_dev->rx_pkt_burst = virtual_ethdev_rx_burst_success;
441         else
442                 vrtl_eth_dev->rx_pkt_burst = virtual_ethdev_rx_burst_fail;
443 }
444
445
446 void
447 virtual_ethdev_tx_burst_fn_set_success(uint16_t port_id, uint8_t success)
448 {
449         struct virtual_ethdev_private *dev_private = NULL;
450         struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
451
452         dev_private = vrtl_eth_dev->data->dev_private;
453
454         if (success)
455                 vrtl_eth_dev->tx_pkt_burst = virtual_ethdev_tx_burst_success;
456         else
457                 vrtl_eth_dev->tx_pkt_burst = virtual_ethdev_tx_burst_fail;
458
459         dev_private->tx_burst_fail_count = 0;
460 }
461
462 void
463 virtual_ethdev_tx_burst_fn_set_tx_pkt_fail_count(uint16_t port_id,
464                 uint8_t packet_fail_count)
465 {
466         struct virtual_ethdev_private *dev_private = NULL;
467         struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
468
469
470         dev_private = vrtl_eth_dev->data->dev_private;
471         dev_private->tx_burst_fail_count = packet_fail_count;
472 }
473
474 void
475 virtual_ethdev_set_link_status(uint16_t port_id, uint8_t link_status)
476 {
477         struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
478
479         vrtl_eth_dev->data->dev_link.link_status = link_status;
480 }
481
482 void
483 virtual_ethdev_simulate_link_status_interrupt(uint16_t port_id,
484                 uint8_t link_status)
485 {
486         struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
487
488         vrtl_eth_dev->data->dev_link.link_status = link_status;
489
490         _rte_eth_dev_callback_process(vrtl_eth_dev, RTE_ETH_EVENT_INTR_LSC,
491                                       NULL, NULL);
492 }
493
494 int
495 virtual_ethdev_add_mbufs_to_rx_queue(uint16_t port_id,
496                 struct rte_mbuf **pkt_burst, int burst_length)
497 {
498         struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
499         struct virtual_ethdev_private *dev_private =
500                         vrtl_eth_dev->data->dev_private;
501
502         return rte_ring_enqueue_burst(dev_private->rx_queue, (void **)pkt_burst,
503                         burst_length, NULL);
504 }
505
506 int
507 virtual_ethdev_get_mbufs_from_tx_queue(uint16_t port_id,
508                 struct rte_mbuf **pkt_burst, int burst_length)
509 {
510         struct virtual_ethdev_private *dev_private;
511         struct rte_eth_dev *vrtl_eth_dev = &rte_eth_devices[port_id];
512
513         dev_private = vrtl_eth_dev->data->dev_private;
514         return rte_ring_dequeue_burst(dev_private->tx_queue, (void **)pkt_burst,
515                 burst_length, NULL);
516 }
517
518
519 int
520 virtual_ethdev_create(const char *name, struct ether_addr *mac_addr,
521                 uint8_t socket_id, uint8_t isr_support)
522 {
523         struct rte_pci_device *pci_dev = NULL;
524         struct rte_eth_dev *eth_dev = NULL;
525         struct rte_pci_driver *pci_drv = NULL;
526         struct rte_pci_id *id_table = NULL;
527         struct virtual_ethdev_private *dev_private = NULL;
528         char name_buf[RTE_RING_NAMESIZE];
529
530
531         /* now do all data allocation - for eth_dev structure, dummy pci driver
532          * and internal (dev_private) data
533          */
534
535         pci_dev = rte_zmalloc_socket(name, sizeof(*pci_dev), 0, socket_id);
536         if (pci_dev == NULL)
537                 goto err;
538
539         pci_drv = rte_zmalloc_socket(name, sizeof(*pci_drv), 0, socket_id);
540         if (pci_drv == NULL)
541                 goto err;
542
543         id_table = rte_zmalloc_socket(name, sizeof(*id_table), 0, socket_id);
544         if (id_table == NULL)
545                 goto err;
546         id_table->device_id = 0xBEEF;
547
548         dev_private = rte_zmalloc_socket(name, sizeof(*dev_private), 0, socket_id);
549         if (dev_private == NULL)
550                 goto err;
551
552         snprintf(name_buf, sizeof(name_buf), "%s_rxQ", name);
553         dev_private->rx_queue = rte_ring_create(name_buf, MAX_PKT_BURST, socket_id,
554                         0);
555         if (dev_private->rx_queue == NULL)
556                 goto err;
557
558         snprintf(name_buf, sizeof(name_buf), "%s_txQ", name);
559         dev_private->tx_queue = rte_ring_create(name_buf, MAX_PKT_BURST, socket_id,
560                         0);
561         if (dev_private->tx_queue == NULL)
562                 goto err;
563
564         /* reserve an ethdev entry */
565         eth_dev = rte_eth_dev_allocate(name);
566         if (eth_dev == NULL)
567                 goto err;
568
569         pci_dev->device.numa_node = socket_id;
570         pci_dev->device.name = eth_dev->data->name;
571         pci_drv->driver.name = virtual_ethdev_driver_name;
572         pci_drv->id_table = id_table;
573
574         if (isr_support)
575                 pci_drv->drv_flags |= RTE_PCI_DRV_INTR_LSC;
576         else
577                 pci_drv->drv_flags &= ~RTE_PCI_DRV_INTR_LSC;
578
579
580         eth_dev->device = &pci_dev->device;
581         eth_dev->device->driver = &pci_drv->driver;
582
583         eth_dev->data->nb_rx_queues = (uint16_t)1;
584         eth_dev->data->nb_tx_queues = (uint16_t)1;
585
586         eth_dev->data->dev_link.link_status = ETH_LINK_DOWN;
587         eth_dev->data->dev_link.link_speed = ETH_SPEED_NUM_10G;
588         eth_dev->data->dev_link.link_duplex = ETH_LINK_FULL_DUPLEX;
589
590         eth_dev->data->mac_addrs = rte_zmalloc(name, ETHER_ADDR_LEN, 0);
591         if (eth_dev->data->mac_addrs == NULL)
592                 goto err;
593
594         memcpy(eth_dev->data->mac_addrs, mac_addr,
595                         sizeof(*eth_dev->data->mac_addrs));
596
597         eth_dev->data->dev_started = 0;
598         eth_dev->data->promiscuous = 0;
599         eth_dev->data->scattered_rx = 0;
600         eth_dev->data->all_multicast = 0;
601
602         eth_dev->data->dev_private = dev_private;
603
604         /* Copy default device operation functions */
605         dev_private->dev_ops = virtual_ethdev_default_dev_ops;
606         eth_dev->dev_ops = &dev_private->dev_ops;
607
608         pci_dev->device.driver = &pci_drv->driver;
609         eth_dev->device = &pci_dev->device;
610
611         eth_dev->rx_pkt_burst = virtual_ethdev_rx_burst_success;
612         eth_dev->tx_pkt_burst = virtual_ethdev_tx_burst_success;
613
614         return eth_dev->data->port_id;
615
616 err:
617         rte_free(pci_dev);
618         rte_free(pci_drv);
619         rte_free(id_table);
620         rte_free(dev_private);
621
622         return -1;
623 }