xref: /dpdk/drivers/net/virtio/virtqueue.c (revision 3998e2a07220844d3f3c17f76a781ced3efe0de0)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2015 Intel Corporation
3  */
4 #include <stdint.h>
5 
6 #include <rte_mbuf.h>
7 
8 #include "virtqueue.h"
9 #include "virtio_logs.h"
10 #include "virtio_pci.h"
11 
12 /*
13  * Two types of mbuf to be cleaned:
14  * 1) mbuf that has been consumed by backend but not used by virtio.
15  * 2) mbuf that hasn't been consued by backend.
16  */
17 struct rte_mbuf *
18 virtqueue_detatch_unused(struct virtqueue *vq)
19 {
20 	struct rte_mbuf *cookie;
21 	int idx;
22 
23 	if (vq != NULL)
24 		for (idx = 0; idx < vq->vq_nentries; idx++) {
25 			cookie = vq->vq_descx[idx].cookie;
26 			if (cookie != NULL) {
27 				vq->vq_descx[idx].cookie = NULL;
28 				return cookie;
29 			}
30 		}
31 	return NULL;
32 }
33 
34 /* Flush the elements in the used ring. */
35 void
36 virtqueue_flush(struct virtqueue *vq)
37 {
38 	struct vring_used_elem *uep;
39 	struct vq_desc_extra *dxp;
40 	uint16_t used_idx, desc_idx;
41 	uint16_t nb_used, i;
42 
43 	nb_used = VIRTQUEUE_NUSED(vq);
44 
45 	for (i = 0; i < nb_used; i++) {
46 		used_idx = vq->vq_used_cons_idx & (vq->vq_nentries - 1);
47 		uep = &vq->vq_ring.used->ring[used_idx];
48 		desc_idx = (uint16_t)uep->id;
49 		dxp = &vq->vq_descx[desc_idx];
50 		if (dxp->cookie != NULL) {
51 			rte_pktmbuf_free(dxp->cookie);
52 			dxp->cookie = NULL;
53 		}
54 		vq->vq_used_cons_idx++;
55 		vq_ring_free_chain(vq, desc_idx);
56 	}
57 }
58