4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
40 #include <rte_memory.h>
41 #include <rte_memzone.h>
42 #include <rte_branch_prediction.h>
43 #include <rte_mempool.h>
44 #include <rte_malloc.h>
46 #include <rte_ether.h>
47 #include <rte_ethdev.h>
48 #include <rte_prefetch.h>
49 #include <rte_string_fns.h>
50 #include <rte_errno.h>
52 #include "virtio_logs.h"
53 #include "virtio_ethdev.h"
54 #include "virtqueue.h"
56 #ifdef RTE_LIBRTE_VIRTIO_DEBUG_DUMP
57 #define VIRTIO_DUMP_PACKET(m, len) rte_pktmbuf_dump(m, len)
59 #define VIRTIO_DUMP_PACKET(m, len) do { } while (0)
62 static inline struct rte_mbuf *
63 rte_rxmbuf_alloc(struct rte_mempool *mp)
67 m = __rte_mbuf_raw_alloc(mp);
68 __rte_mbuf_sanity_check_raw(m, RTE_MBUF_PKT, 0);
74 virtio_dev_vring_start(struct rte_eth_dev *dev, struct virtqueue *vq, int queue_type)
77 int i, nbufs, error, size = vq->vq_nentries;
78 struct vring *vr = &vq->vq_ring;
79 uint8_t *ring_mem = vq->vq_ring_virt_mem;
80 char vq_name[VIRTQUEUE_MAX_NAME_SZ];
81 PMD_INIT_FUNC_TRACE();
84 * Reinitialise since virtio port might have been stopped and restarted
86 memset(vq->vq_ring_virt_mem, 0, vq->vq_ring_size);
87 vring_init(vr, size, ring_mem, vq->vq_alignment);
88 vq->vq_used_cons_idx = 0;
89 vq->vq_desc_head_idx = 0;
90 vq->vq_free_cnt = vq->vq_nentries;
91 memset(vq->vq_descx, 0, sizeof(struct vq_desc_extra) * vq->vq_nentries);
93 /* Chain all the descriptors in the ring with an END */
94 for (i = 0; i < size - 1; i++)
95 vr->desc[i].next = (uint16_t)(i + 1);
96 vr->desc[i].next = VQ_RING_DESC_CHAIN_END;
99 * Disable device(host) interrupting guest
101 virtqueue_disable_intr(vq);
103 rte_snprintf(vq_name, sizeof(vq_name), "port_%d_rx_vq",
105 PMD_INIT_LOG(DEBUG, "vq name: %s\n", vq->vq_name);
107 /* Only rx virtqueue needs mbufs to be allocated at initialization */
108 if (queue_type == VTNET_RQ) {
109 if (vq->mpool == NULL)
110 rte_exit(EXIT_FAILURE, "Cannot allocate initial mbufs for rx virtqueue\n");
111 /* Allocate blank mbufs for the each rx descriptor */
114 while (!virtqueue_full(vq)) {
115 m = rte_rxmbuf_alloc(vq->mpool);
118 /******************************************
119 * Enqueue allocated buffers *
120 *******************************************/
121 error = virtqueue_enqueue_recv_refill(vq, m);
123 rte_pktmbuf_free_seg(m);
128 PMD_INIT_LOG(DEBUG, "Allocated %d bufs\n", nbufs);
129 VIRTIO_WRITE_REG_2(vq->hw, VIRTIO_PCI_QUEUE_SEL, VTNET_SQ_RQ_QUEUE_IDX);
130 VIRTIO_WRITE_REG_4(vq->hw, VIRTIO_PCI_QUEUE_PFN,
131 vq->mz->phys_addr >> VIRTIO_PCI_QUEUE_ADDR_SHIFT);
133 VIRTIO_WRITE_REG_2(vq->hw, VIRTIO_PCI_QUEUE_SEL, VTNET_SQ_TQ_QUEUE_IDX);
134 VIRTIO_WRITE_REG_4(vq->hw, VIRTIO_PCI_QUEUE_PFN,
135 vq->mz->phys_addr >> VIRTIO_PCI_QUEUE_ADDR_SHIFT);
140 virtio_dev_rxtx_start(struct rte_eth_dev *dev)
143 * Start recieve and transmit vrings
144 * - Setup vring structure for all queues
145 * - Initialize descriptor for the rx vring
146 * - Allocate blank mbufs for the each rx descriptor
149 PMD_INIT_FUNC_TRACE();
151 /* Start rx vring: by default we have 1 rx virtqueue. */
152 virtio_dev_vring_start(dev, dev->data->rx_queues[0], VTNET_RQ);
153 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->rx_queues[0]);
155 /* Start tx vring: by default we have 1 tx virtqueue. */
156 virtio_dev_vring_start(dev, dev->data->tx_queues[0], VTNET_TQ);
157 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->tx_queues[0]);
161 virtio_dev_rx_queue_setup(struct rte_eth_dev *dev,
164 unsigned int socket_id,
165 __rte_unused const struct rte_eth_rxconf *rx_conf,
166 struct rte_mempool *mp)
168 uint8_t vtpci_queue_idx = VTNET_SQ_RQ_QUEUE_IDX;
169 struct virtqueue *vq;
172 PMD_INIT_FUNC_TRACE();
173 ret = virtio_dev_queue_setup(dev, VTNET_RQ, queue_idx, vtpci_queue_idx,
174 nb_desc, socket_id, &vq);
176 PMD_INIT_LOG(ERR, "tvq initialization failed\n");
179 /* Create mempool for rx mbuf allocation */
182 dev->data->rx_queues[queue_idx] = vq;
187 * struct rte_eth_dev *dev: Used to update dev
188 * uint16_t nb_desc: Defaults to values read from config space
189 * unsigned int socket_id: Used to allocate memzone
190 * const struct rte_eth_txconf *tx_conf: Used to setup tx engine
191 * uint16_t queue_idx: Just used as an index in dev txq list
194 virtio_dev_tx_queue_setup(struct rte_eth_dev *dev,
197 unsigned int socket_id,
198 __rte_unused const struct rte_eth_txconf *tx_conf)
200 uint8_t vtpci_queue_idx = VTNET_SQ_TQ_QUEUE_IDX;
201 struct virtqueue *vq;
204 PMD_INIT_FUNC_TRACE();
205 ret = virtio_dev_queue_setup(dev, VTNET_TQ, queue_idx, vtpci_queue_idx,
206 nb_desc, socket_id, &vq);
208 PMD_INIT_LOG(ERR, "rvq initialization failed\n");
212 dev->data->tx_queues[queue_idx] = vq;
217 virtio_discard_rxbuf(struct virtqueue *vq, struct rte_mbuf *m)
221 * Requeue the discarded mbuf. This should always be
222 * successful since it was just dequeued.
224 error = virtqueue_enqueue_recv_refill(vq, m);
225 if (unlikely(error)) {
226 RTE_LOG(ERR, PMD, "cannot requeue discarded mbuf");
227 rte_pktmbuf_free_seg(m);
231 #define VIRTIO_MBUF_BURST_SZ 64
233 virtio_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
235 struct virtqueue *rxvq = rx_queue;
236 struct virtio_hw *hw = rxvq->hw;
237 struct rte_mbuf *rxm, *new_mbuf;
238 uint16_t nb_used, num, nb_rx = 0;
239 uint32_t len[VIRTIO_MBUF_BURST_SZ];
240 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
242 uint32_t i, nb_enqueued = 0;
244 nb_used = VIRTQUEUE_NUSED(rxvq);
248 num = (uint16_t)(likely(nb_used <= nb_pkts) ? nb_used : nb_pkts);
249 num = (uint16_t)(likely(num <= VIRTIO_MBUF_BURST_SZ) ? num : VIRTIO_MBUF_BURST_SZ);
250 if(num == 0) return 0;
251 num = virtqueue_dequeue_burst(rxvq, rcv_pkts, len, num);
252 PMD_RX_LOG(DEBUG, "used:%d dequeue:%d\n", nb_used, num);
253 for (i = 0; i < num ; i ++) {
255 PMD_RX_LOG(DEBUG, "packet len:%d\n", len[i]);
256 if (unlikely(len[i] < (uint32_t)hw->vtnet_hdr_size + ETHER_HDR_LEN)) {
257 PMD_RX_LOG(ERR, "Packet drop\n");
259 virtio_discard_rxbuf(rxvq, rxm);
260 hw->eth_stats.ierrors++;
263 rxm->pkt.in_port = rxvq->port_id;
264 rxm->pkt.data = (char *)rxm->buf_addr + RTE_PKTMBUF_HEADROOM;
265 rxm->pkt.nb_segs = 1;
266 rxm->pkt.next = NULL;
267 rxm->pkt.pkt_len = (uint32_t)(len[i] - sizeof(struct virtio_net_hdr));
268 rxm->pkt.data_len = (uint16_t)(len[i] - sizeof(struct virtio_net_hdr));
269 VIRTIO_DUMP_PACKET(rxm, rxm->pkt.data_len);
270 rx_pkts[nb_rx++] = rxm;
271 hw->eth_stats.ibytes += len[i] - sizeof(struct virtio_net_hdr);
273 hw->eth_stats.ipackets += nb_rx;
275 /* Allocate new mbuf for the used descriptor */
277 while (likely(!virtqueue_full(rxvq))) {
278 new_mbuf = rte_rxmbuf_alloc(rxvq->mpool);
279 if (unlikely(new_mbuf == NULL)) {
280 hw->eth_stats.rx_nombuf++;
283 error = virtqueue_enqueue_recv_refill(rxvq, new_mbuf);
284 if (unlikely(error)) {
285 rte_pktmbuf_free_seg(new_mbuf);
290 if(likely(nb_enqueued)) {
291 if(unlikely(virtqueue_kick_prepare(rxvq))) {
292 virtqueue_notify(rxvq);
293 PMD_RX_LOG(DEBUG, "Notified\n");
300 virtio_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
302 struct virtqueue *txvq = tx_queue;
303 struct rte_mbuf *txm;
304 uint16_t nb_used, nb_tx, count, num, i;
306 uint32_t len[VIRTIO_MBUF_BURST_SZ];
307 struct rte_mbuf *snd_pkts[VIRTIO_MBUF_BURST_SZ];
308 struct virtio_hw *hw;
312 if (unlikely(nb_pkts < 1))
315 PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
316 nb_used = VIRTQUEUE_NUSED(txvq);
321 num = (uint16_t)(likely(nb_used < VIRTIO_MBUF_BURST_SZ) ? nb_used : VIRTIO_MBUF_BURST_SZ);
322 num = virtqueue_dequeue_burst(txvq, snd_pkts, len, num);
323 for (i = 0; i < num ; i ++) {
324 rte_pktmbuf_free_seg(snd_pkts[i]);
327 while (count++ < nb_pkts) {
328 if(!virtqueue_full(txvq)) {
329 txm = tx_pkts[nb_tx];
330 /************************************************/
331 /***** Enqueue Packet buffers *****/
332 /************************************************/
333 error = virtqueue_enqueue_xmit(txvq, txm);
334 if (unlikely(error)) {
335 // rte_pktmbuf_free_seg(txm); /* the upper application will free this packet */
337 PMD_TX_LOG(ERR, "virtqueue_enqueue Free count = 0\n");
338 else if (error == EMSGSIZE)
339 PMD_TX_LOG(ERR, "virtqueue_enqueue Free count < 1\n");
341 PMD_TX_LOG(ERR, "virtqueue_enqueue error: %d\n", error);
345 hw->eth_stats.obytes += txm->pkt.data_len;
347 PMD_TX_LOG(ERR, "No free tx descriptors to transmit\n");
348 virtqueue_notify(txvq);
352 hw->eth_stats.opackets += nb_tx;
354 if(unlikely(virtqueue_kick_prepare(txvq))) {
355 virtqueue_notify(txvq);
356 PMD_TX_LOG(DEBUG, "Notified backend after xmit\n");