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