2017-12-19 15:49:01 +00:00
|
|
|
/* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
* Copyright(c) 2010-2015 Intel Corporation
|
2013-09-18 10:00:00 +00:00
|
|
|
*/
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include <rte_mbuf.h>
|
|
|
|
|
|
|
|
#include "virtqueue.h"
|
|
|
|
#include "virtio_logs.h"
|
|
|
|
#include "virtio_pci.h"
|
2017-12-11 05:13:29 +00:00
|
|
|
#include "virtio_rxtx_simple.h"
|
2013-09-18 10:00:00 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Two types of mbuf to be cleaned:
|
|
|
|
* 1) mbuf that has been consumed by backend but not used by virtio.
|
|
|
|
* 2) mbuf that hasn't been consued by backend.
|
|
|
|
*/
|
|
|
|
struct rte_mbuf *
|
2018-01-23 15:54:43 +00:00
|
|
|
virtqueue_detach_unused(struct virtqueue *vq)
|
2013-09-18 10:00:00 +00:00
|
|
|
{
|
|
|
|
struct rte_mbuf *cookie;
|
2018-01-23 15:54:40 +00:00
|
|
|
struct virtio_hw *hw;
|
|
|
|
uint16_t start, end;
|
|
|
|
int type, idx;
|
2013-09-18 10:00:00 +00:00
|
|
|
|
2018-01-23 15:54:40 +00:00
|
|
|
if (vq == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
hw = vq->hw;
|
|
|
|
type = virtio_get_queue_type(hw, vq->vq_queue_index);
|
|
|
|
start = vq->vq_avail_idx & (vq->vq_nentries - 1);
|
|
|
|
end = (vq->vq_avail_idx + vq->vq_free_cnt) & (vq->vq_nentries - 1);
|
|
|
|
|
|
|
|
for (idx = 0; idx < vq->vq_nentries; idx++) {
|
|
|
|
if (hw->use_simple_rx && type == VTNET_RQ) {
|
|
|
|
if (start <= end && idx >= start && idx < end)
|
|
|
|
continue;
|
|
|
|
if (start > end && (idx >= start || idx < end))
|
|
|
|
continue;
|
|
|
|
cookie = vq->sw_ring[idx];
|
|
|
|
if (cookie != NULL) {
|
|
|
|
vq->sw_ring[idx] = NULL;
|
|
|
|
return cookie;
|
|
|
|
}
|
|
|
|
} else {
|
2015-07-15 13:51:01 +00:00
|
|
|
cookie = vq->vq_descx[idx].cookie;
|
|
|
|
if (cookie != NULL) {
|
|
|
|
vq->vq_descx[idx].cookie = NULL;
|
|
|
|
return cookie;
|
|
|
|
}
|
2013-09-18 10:00:00 +00:00
|
|
|
}
|
2018-01-23 15:54:40 +00:00
|
|
|
}
|
|
|
|
|
2014-06-13 01:32:40 +00:00
|
|
|
return NULL;
|
2013-09-18 10:00:00 +00:00
|
|
|
}
|
2017-10-20 02:09:28 +00:00
|
|
|
|
2018-12-17 21:31:35 +00:00
|
|
|
/* Flush used descs */
|
|
|
|
static void
|
|
|
|
virtqueue_rxvq_flush_packed(struct virtqueue *vq)
|
|
|
|
{
|
|
|
|
struct vq_desc_extra *dxp;
|
|
|
|
uint16_t i;
|
|
|
|
|
2019-03-19 06:43:08 +00:00
|
|
|
struct vring_packed_desc *descs = vq->vq_packed.ring.desc;
|
2018-12-17 21:31:35 +00:00
|
|
|
int cnt = 0;
|
|
|
|
|
|
|
|
i = vq->vq_used_cons_idx;
|
|
|
|
while (desc_is_used(&descs[i], vq) && cnt++ < vq->vq_nentries) {
|
|
|
|
dxp = &vq->vq_descx[descs[i].id];
|
|
|
|
if (dxp->cookie != NULL) {
|
|
|
|
rte_pktmbuf_free(dxp->cookie);
|
|
|
|
dxp->cookie = NULL;
|
|
|
|
}
|
|
|
|
vq->vq_free_cnt++;
|
|
|
|
vq->vq_used_cons_idx++;
|
|
|
|
if (vq->vq_used_cons_idx >= vq->vq_nentries) {
|
|
|
|
vq->vq_used_cons_idx -= vq->vq_nentries;
|
2019-03-19 06:43:07 +00:00
|
|
|
vq->vq_packed.used_wrap_counter ^= 1;
|
2018-12-17 21:31:35 +00:00
|
|
|
}
|
|
|
|
i = vq->vq_used_cons_idx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-20 02:09:28 +00:00
|
|
|
/* Flush the elements in the used ring. */
|
2018-12-17 21:31:35 +00:00
|
|
|
static void
|
|
|
|
virtqueue_rxvq_flush_split(struct virtqueue *vq)
|
2017-10-20 02:09:28 +00:00
|
|
|
{
|
2017-12-11 05:13:29 +00:00
|
|
|
struct virtnet_rx *rxq = &vq->rxq;
|
|
|
|
struct virtio_hw *hw = vq->hw;
|
2017-10-20 02:09:28 +00:00
|
|
|
struct vring_used_elem *uep;
|
|
|
|
struct vq_desc_extra *dxp;
|
|
|
|
uint16_t used_idx, desc_idx;
|
|
|
|
uint16_t nb_used, i;
|
|
|
|
|
|
|
|
nb_used = VIRTQUEUE_NUSED(vq);
|
|
|
|
|
|
|
|
for (i = 0; i < nb_used; i++) {
|
|
|
|
used_idx = vq->vq_used_cons_idx & (vq->vq_nentries - 1);
|
2019-03-19 06:43:07 +00:00
|
|
|
uep = &vq->vq_split.ring.used->ring[used_idx];
|
2017-12-11 05:13:29 +00:00
|
|
|
if (hw->use_simple_rx) {
|
|
|
|
desc_idx = used_idx;
|
|
|
|
rte_pktmbuf_free(vq->sw_ring[desc_idx]);
|
|
|
|
vq->vq_free_cnt++;
|
2018-07-02 13:56:38 +00:00
|
|
|
} else if (hw->use_inorder_rx) {
|
|
|
|
desc_idx = (uint16_t)uep->id;
|
|
|
|
dxp = &vq->vq_descx[desc_idx];
|
|
|
|
if (dxp->cookie != NULL) {
|
|
|
|
rte_pktmbuf_free(dxp->cookie);
|
|
|
|
dxp->cookie = NULL;
|
|
|
|
}
|
|
|
|
vq_ring_free_inorder(vq, desc_idx, 1);
|
2017-12-11 05:13:29 +00:00
|
|
|
} else {
|
|
|
|
desc_idx = (uint16_t)uep->id;
|
|
|
|
dxp = &vq->vq_descx[desc_idx];
|
|
|
|
if (dxp->cookie != NULL) {
|
|
|
|
rte_pktmbuf_free(dxp->cookie);
|
|
|
|
dxp->cookie = NULL;
|
|
|
|
}
|
|
|
|
vq_ring_free_chain(vq, desc_idx);
|
2017-10-20 02:09:28 +00:00
|
|
|
}
|
|
|
|
vq->vq_used_cons_idx++;
|
2017-12-11 05:13:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (hw->use_simple_rx) {
|
|
|
|
while (vq->vq_free_cnt >= RTE_VIRTIO_VPMD_RX_REARM_THRESH) {
|
|
|
|
virtio_rxq_rearm_vec(rxq);
|
|
|
|
if (virtqueue_kick_prepare(vq))
|
|
|
|
virtqueue_notify(vq);
|
|
|
|
}
|
2017-10-20 02:09:28 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-17 21:31:35 +00:00
|
|
|
|
|
|
|
/* Flush the elements in the used ring. */
|
|
|
|
void
|
|
|
|
virtqueue_rxvq_flush(struct virtqueue *vq)
|
|
|
|
{
|
|
|
|
struct virtio_hw *hw = vq->hw;
|
|
|
|
|
|
|
|
if (vtpci_packed_queue(hw))
|
|
|
|
virtqueue_rxvq_flush_packed(vq);
|
|
|
|
else
|
|
|
|
virtqueue_rxvq_flush_split(vq);
|
|
|
|
}
|