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