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_cycles.h>
41 #include <rte_memory.h>
42 #include <rte_memzone.h>
43 #include <rte_branch_prediction.h>
44 #include <rte_mempool.h>
45 #include <rte_malloc.h>
47 #include <rte_ether.h>
48 #include <rte_ethdev.h>
49 #include <rte_prefetch.h>
50 #include <rte_string_fns.h>
51 #include <rte_errno.h>
53 #include "virtio_logs.h"
54 #include "virtio_ethdev.h"
55 #include "virtqueue.h"
57 #ifdef RTE_LIBRTE_VIRTIO_DEBUG_DUMP
58 #define VIRTIO_DUMP_PACKET(m, len) rte_pktmbuf_dump(m, len)
60 #define VIRTIO_DUMP_PACKET(m, len) do { } while (0)
63 static inline struct rte_mbuf *
64 rte_rxmbuf_alloc(struct rte_mempool *mp)
68 m = __rte_mbuf_raw_alloc(mp);
69 __rte_mbuf_sanity_check_raw(m, RTE_MBUF_PKT, 0);
75 virtio_dev_vring_start(struct rte_eth_dev *dev, struct virtqueue *vq, int queue_type)
78 int i, nbufs, error, size = vq->vq_nentries;
79 struct vring *vr = &vq->vq_ring;
80 uint8_t *ring_mem = vq->vq_ring_virt_mem;
81 char vq_name[VIRTQUEUE_MAX_NAME_SZ];
82 PMD_INIT_FUNC_TRACE();
85 * Reinitialise since virtio port might have been stopped and restarted
87 memset(vq->vq_ring_virt_mem, 0, vq->vq_ring_size);
88 vring_init(vr, size, ring_mem, vq->vq_alignment);
89 vq->vq_used_cons_idx = 0;
90 vq->vq_desc_head_idx = 0;
92 vq->vq_desc_tail_idx = (uint16_t)(vq->vq_nentries - 1);
93 vq->vq_free_cnt = vq->vq_nentries;
94 memset(vq->vq_descx, 0, sizeof(struct vq_desc_extra) * vq->vq_nentries);
96 /* Chain all the descriptors in the ring with an END */
97 for (i = 0; i < size - 1; i++)
98 vr->desc[i].next = (uint16_t)(i + 1);
99 vr->desc[i].next = VQ_RING_DESC_CHAIN_END;
102 * Disable device(host) interrupting guest
104 virtqueue_disable_intr(vq);
106 rte_snprintf(vq_name, sizeof(vq_name), "port_%d_rx_vq",
108 PMD_INIT_LOG(DEBUG, "vq name: %s\n", vq->vq_name);
110 /* Only rx virtqueue needs mbufs to be allocated at initialization */
111 if (queue_type == VTNET_RQ) {
112 if (vq->mpool == NULL)
113 rte_exit(EXIT_FAILURE,
114 "Cannot allocate initial mbufs for rx virtqueue\n");
116 /* Allocate blank mbufs for the each rx descriptor */
119 while (!virtqueue_full(vq)) {
120 m = rte_rxmbuf_alloc(vq->mpool);
124 /******************************************
125 * Enqueue allocated buffers *
126 *******************************************/
127 error = virtqueue_enqueue_recv_refill(vq, m);
130 rte_pktmbuf_free_seg(m);
136 vq_update_avail_idx(vq);
138 PMD_INIT_LOG(DEBUG, "Allocated %d bufs\n", nbufs);
139 VIRTIO_WRITE_REG_2(vq->hw, VIRTIO_PCI_QUEUE_SEL, VTNET_SQ_RQ_QUEUE_IDX);
140 VIRTIO_WRITE_REG_4(vq->hw, VIRTIO_PCI_QUEUE_PFN,
141 vq->mz->phys_addr >> VIRTIO_PCI_QUEUE_ADDR_SHIFT);
143 VIRTIO_WRITE_REG_2(vq->hw, VIRTIO_PCI_QUEUE_SEL, VTNET_SQ_TQ_QUEUE_IDX);
144 VIRTIO_WRITE_REG_4(vq->hw, VIRTIO_PCI_QUEUE_PFN,
145 vq->mz->phys_addr >> VIRTIO_PCI_QUEUE_ADDR_SHIFT);
150 virtio_dev_rxtx_start(struct rte_eth_dev *dev)
153 * Start receive and transmit vrings
154 * - Setup vring structure for all queues
155 * - Initialize descriptor for the rx vring
156 * - Allocate blank mbufs for the each rx descriptor
159 PMD_INIT_FUNC_TRACE();
161 /* Start rx vring: by default we have 1 rx virtqueue. */
162 virtio_dev_vring_start(dev, dev->data->rx_queues[0], VTNET_RQ);
163 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->rx_queues[0]);
165 /* Start tx vring: by default we have 1 tx virtqueue. */
166 virtio_dev_vring_start(dev, dev->data->tx_queues[0], VTNET_TQ);
167 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->tx_queues[0]);
171 virtio_dev_rx_queue_setup(struct rte_eth_dev *dev,
174 unsigned int socket_id,
175 __rte_unused const struct rte_eth_rxconf *rx_conf,
176 struct rte_mempool *mp)
178 uint8_t vtpci_queue_idx = VTNET_SQ_RQ_QUEUE_IDX;
179 struct virtqueue *vq;
182 PMD_INIT_FUNC_TRACE();
183 ret = virtio_dev_queue_setup(dev, VTNET_RQ, queue_idx, vtpci_queue_idx,
184 nb_desc, socket_id, &vq);
186 PMD_INIT_LOG(ERR, "tvq initialization failed\n");
190 /* Create mempool for rx mbuf allocation */
193 dev->data->rx_queues[queue_idx] = vq;
198 * struct rte_eth_dev *dev: Used to update dev
199 * uint16_t nb_desc: Defaults to values read from config space
200 * unsigned int socket_id: Used to allocate memzone
201 * const struct rte_eth_txconf *tx_conf: Used to setup tx engine
202 * uint16_t queue_idx: Just used as an index in dev txq list
205 virtio_dev_tx_queue_setup(struct rte_eth_dev *dev,
208 unsigned int socket_id,
209 __rte_unused const struct rte_eth_txconf *tx_conf)
211 uint8_t vtpci_queue_idx = VTNET_SQ_TQ_QUEUE_IDX;
212 struct virtqueue *vq;
215 PMD_INIT_FUNC_TRACE();
216 ret = virtio_dev_queue_setup(dev, VTNET_TQ, queue_idx, vtpci_queue_idx,
217 nb_desc, socket_id, &vq);
219 PMD_INIT_LOG(ERR, "rvq initialization failed\n");
223 dev->data->tx_queues[queue_idx] = vq;
228 virtio_discard_rxbuf(struct virtqueue *vq, struct rte_mbuf *m)
232 * Requeue the discarded mbuf. This should always be
233 * successful since it was just dequeued.
235 error = virtqueue_enqueue_recv_refill(vq, m);
236 if (unlikely(error)) {
237 RTE_LOG(ERR, PMD, "cannot requeue discarded mbuf");
238 rte_pktmbuf_free_seg(m);
242 #define VIRTIO_MBUF_BURST_SZ 64
243 #define DESC_PER_CACHELINE (CACHE_LINE_SIZE / sizeof(struct vring_desc))
245 virtio_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
247 struct virtqueue *rxvq = rx_queue;
248 struct virtio_hw *hw = rxvq->hw;
249 struct rte_mbuf *rxm, *new_mbuf;
250 uint16_t nb_used, num, nb_rx = 0;
251 uint32_t len[VIRTIO_MBUF_BURST_SZ];
252 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
254 uint32_t i, nb_enqueued = 0;
256 nb_used = VIRTQUEUE_NUSED(rxvq);
260 num = (uint16_t)(likely(nb_used <= nb_pkts) ? nb_used : nb_pkts);
261 num = (uint16_t)(likely(num <= VIRTIO_MBUF_BURST_SZ) ? num : VIRTIO_MBUF_BURST_SZ);
262 if (likely(num > DESC_PER_CACHELINE))
263 num = num - ((rxvq->vq_used_cons_idx + num) % DESC_PER_CACHELINE);
265 if(num == 0) return 0;
267 num = virtqueue_dequeue_burst_rx(rxvq, rcv_pkts, len, num);
268 PMD_RX_LOG(DEBUG, "used:%d dequeue:%d\n", nb_used, num);
269 for (i = 0; i < num ; i ++) {
272 PMD_RX_LOG(DEBUG, "packet len:%d\n", len[i]);
275 < (uint32_t)hw->vtnet_hdr_size + ETHER_HDR_LEN)) {
276 PMD_RX_LOG(ERR, "Packet drop\n");
278 virtio_discard_rxbuf(rxvq, rxm);
279 hw->eth_stats.ierrors++;
283 rxm->pkt.in_port = rxvq->port_id;
284 rxm->pkt.data = (char *)rxm->buf_addr + RTE_PKTMBUF_HEADROOM;
285 rxm->pkt.nb_segs = 1;
286 rxm->pkt.next = NULL;
287 rxm->pkt.pkt_len = (uint32_t)(len[i]
288 - sizeof(struct virtio_net_hdr));
289 rxm->pkt.data_len = (uint16_t)(len[i]
290 - sizeof(struct virtio_net_hdr));
292 VIRTIO_DUMP_PACKET(rxm, rxm->pkt.data_len);
294 rx_pkts[nb_rx++] = rxm;
295 hw->eth_stats.ibytes += len[i] - sizeof(struct virtio_net_hdr);
298 hw->eth_stats.ipackets += nb_rx;
300 /* Allocate new mbuf for the used descriptor */
302 while (likely(!virtqueue_full(rxvq))) {
303 new_mbuf = rte_rxmbuf_alloc(rxvq->mpool);
304 if (unlikely(new_mbuf == NULL)) {
305 hw->eth_stats.rx_nombuf++;
308 error = virtqueue_enqueue_recv_refill(rxvq, new_mbuf);
309 if (unlikely(error)) {
310 rte_pktmbuf_free_seg(new_mbuf);
315 if (likely(nb_enqueued)) {
316 if (unlikely(virtqueue_kick_prepare(rxvq))) {
317 virtqueue_notify(rxvq);
318 PMD_RX_LOG(DEBUG, "Notified\n");
322 vq_update_avail_idx(rxvq);
328 virtio_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
330 struct virtqueue *txvq = tx_queue;
331 struct rte_mbuf *txm;
332 uint16_t nb_used, nb_tx, num;
334 struct virtio_hw *hw;
338 if (unlikely(nb_pkts < 1))
341 PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
342 nb_used = VIRTQUEUE_NUSED(txvq);
347 num = (uint16_t)(likely(nb_used < VIRTIO_MBUF_BURST_SZ) ? nb_used : VIRTIO_MBUF_BURST_SZ);
349 while (nb_tx < nb_pkts) {
350 if (virtqueue_full(txvq) && num) {
351 virtqueue_dequeue_pkt_tx(txvq);
355 if(!virtqueue_full(txvq)) {
356 txm = tx_pkts[nb_tx];
357 /* Enqueue Packet buffers */
358 error = virtqueue_enqueue_xmit(txvq, txm);
359 if (unlikely(error)) {
361 PMD_TX_LOG(ERR, "virtqueue_enqueue Free count = 0\n");
362 else if (error == EMSGSIZE)
363 PMD_TX_LOG(ERR, "virtqueue_enqueue Free count < 1\n");
365 PMD_TX_LOG(ERR, "virtqueue_enqueue error: %d\n", error);
369 hw->eth_stats.obytes += txm->pkt.data_len;
371 PMD_TX_LOG(ERR, "No free tx descriptors to transmit\n");
375 vq_update_avail_idx(txvq);
377 hw->eth_stats.opackets += nb_tx;
379 if(unlikely(virtqueue_kick_prepare(txvq))) {
380 virtqueue_notify(txvq);
381 PMD_TX_LOG(DEBUG, "Notified backend after xmit\n");