c51b811338a5ee87fbf00f98bf648e14eddb032a
[dpdk.git] / lib / librte_pmd_virtio / virtio_rxtx.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 #include <stdint.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <errno.h>
39
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>
46 #include <rte_mbuf.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>
52
53 #include "virtio_logs.h"
54 #include "virtio_ethdev.h"
55 #include "virtqueue.h"
56
57 #ifdef RTE_LIBRTE_VIRTIO_DEBUG_DUMP
58 #define VIRTIO_DUMP_PACKET(m, len) rte_pktmbuf_dump(stdout, m, len)
59 #else
60 #define  VIRTIO_DUMP_PACKET(m, len) do { } while (0)
61 #endif
62
63 static inline struct rte_mbuf *
64 rte_rxmbuf_alloc(struct rte_mempool *mp)
65 {
66         struct rte_mbuf *m;
67
68         m = __rte_mbuf_raw_alloc(mp);
69         __rte_mbuf_sanity_check_raw(m, RTE_MBUF_PKT, 0);
70
71         return m;
72 }
73
74 static void
75 virtio_dev_vring_start(struct rte_eth_dev *dev, struct virtqueue *vq, int queue_type)
76 {
77         struct rte_mbuf *m;
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();
83
84         /*
85          * Reinitialise since virtio port might have been stopped and restarted
86          */
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;
91         vq->vq_avail_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);
95
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;
100
101         /*
102          * Disable device(host) interrupting guest
103          */
104         virtqueue_disable_intr(vq);
105
106         snprintf(vq_name, sizeof(vq_name), "port_%d_rx_vq",
107                                         dev->data->port_id);
108         PMD_INIT_LOG(DEBUG, "vq name: %s\n", vq->vq_name);
109
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");
115
116                 /* Allocate blank mbufs for the each rx descriptor */
117                 nbufs = 0;
118                 error = ENOSPC;
119                 while (!virtqueue_full(vq)) {
120                         m = rte_rxmbuf_alloc(vq->mpool);
121                         if (m == NULL)
122                                 break;
123
124                         /******************************************
125                         *         Enqueue allocated buffers        *
126                         *******************************************/
127                         error = virtqueue_enqueue_recv_refill(vq, m);
128
129                         if (error) {
130                                 rte_pktmbuf_free_seg(m);
131                                 break;
132                         }
133                         nbufs++;
134                 }
135
136                 vq_update_avail_idx(vq);
137
138                 PMD_INIT_LOG(DEBUG, "Allocated %d bufs\n", nbufs);
139
140                 VIRTIO_WRITE_REG_2(vq->hw, VIRTIO_PCI_QUEUE_SEL,
141                         vq->vq_queue_index);
142                 VIRTIO_WRITE_REG_4(vq->hw, VIRTIO_PCI_QUEUE_PFN,
143                         vq->mz->phys_addr >> VIRTIO_PCI_QUEUE_ADDR_SHIFT);
144         } else if (queue_type == VTNET_TQ) {
145                 VIRTIO_WRITE_REG_2(vq->hw, VIRTIO_PCI_QUEUE_SEL,
146                         vq->vq_queue_index);
147                 VIRTIO_WRITE_REG_4(vq->hw, VIRTIO_PCI_QUEUE_PFN,
148                         vq->mz->phys_addr >> VIRTIO_PCI_QUEUE_ADDR_SHIFT);
149         } else {
150                 VIRTIO_WRITE_REG_2(vq->hw, VIRTIO_PCI_QUEUE_SEL,
151                         vq->vq_queue_index);
152                 VIRTIO_WRITE_REG_4(vq->hw, VIRTIO_PCI_QUEUE_PFN,
153                         vq->mz->phys_addr >> VIRTIO_PCI_QUEUE_ADDR_SHIFT);
154         }
155 }
156
157 void
158 virtio_dev_cq_start(struct rte_eth_dev *dev)
159 {
160         struct virtio_hw *hw
161                 = VIRTIO_DEV_PRIVATE_TO_HW(dev->data->dev_private);
162
163         virtio_dev_vring_start(dev, hw->cvq, VTNET_CQ);
164         VIRTQUEUE_DUMP((struct virtqueue *)hw->cvq);
165 }
166
167 void
168 virtio_dev_rxtx_start(struct rte_eth_dev *dev)
169 {
170         /*
171          * Start receive and transmit vrings
172          * -    Setup vring structure for all queues
173          * -    Initialize descriptor for the rx vring
174          * -    Allocate blank mbufs for the each rx descriptor
175          *
176          */
177         int i;
178
179         PMD_INIT_FUNC_TRACE();
180
181         /* Start rx vring. */
182         for (i = 0; i < dev->data->nb_rx_queues; i++) {
183                 virtio_dev_vring_start(dev, dev->data->rx_queues[i], VTNET_RQ);
184                 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->rx_queues[i]);
185         }
186
187         /* Start tx vring. */
188         for (i = 0; i < dev->data->nb_tx_queues; i++) {
189                 virtio_dev_vring_start(dev, dev->data->tx_queues[i], VTNET_TQ);
190                 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->tx_queues[i]);
191         }
192 }
193
194 int
195 virtio_dev_rx_queue_setup(struct rte_eth_dev *dev,
196                         uint16_t queue_idx,
197                         uint16_t nb_desc,
198                         unsigned int socket_id,
199                         __rte_unused const struct rte_eth_rxconf *rx_conf,
200                         struct rte_mempool *mp)
201 {
202         uint8_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_RQ_QUEUE_IDX;
203         struct virtqueue *vq;
204         int ret;
205
206         PMD_INIT_FUNC_TRACE();
207         ret = virtio_dev_queue_setup(dev, VTNET_RQ, queue_idx, vtpci_queue_idx,
208                         nb_desc, socket_id, &vq);
209         if (ret < 0) {
210                 PMD_INIT_LOG(ERR, "tvq initialization failed\n");
211                 return ret;
212         }
213
214         /* Create mempool for rx mbuf allocation */
215         vq->mpool = mp;
216
217         dev->data->rx_queues[queue_idx] = vq;
218         return 0;
219 }
220
221 /*
222  * struct rte_eth_dev *dev: Used to update dev
223  * uint16_t nb_desc: Defaults to values read from config space
224  * unsigned int socket_id: Used to allocate memzone
225  * const struct rte_eth_txconf *tx_conf: Used to setup tx engine
226  * uint16_t queue_idx: Just used as an index in dev txq list
227  */
228 int
229 virtio_dev_tx_queue_setup(struct rte_eth_dev *dev,
230                         uint16_t queue_idx,
231                         uint16_t nb_desc,
232                         unsigned int socket_id,
233                         __rte_unused const struct rte_eth_txconf *tx_conf)
234 {
235         uint8_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_TQ_QUEUE_IDX;
236         struct virtqueue *vq;
237         int ret;
238
239         PMD_INIT_FUNC_TRACE();
240         ret = virtio_dev_queue_setup(dev, VTNET_TQ, queue_idx, vtpci_queue_idx,
241                         nb_desc, socket_id, &vq);
242         if (ret < 0) {
243                 PMD_INIT_LOG(ERR, "rvq initialization failed\n");
244                 return ret;
245         }
246
247         dev->data->tx_queues[queue_idx] = vq;
248         return 0;
249 }
250
251 static void
252 virtio_discard_rxbuf(struct virtqueue *vq, struct rte_mbuf *m)
253 {
254         int error;
255         /*
256          * Requeue the discarded mbuf. This should always be
257          * successful since it was just dequeued.
258          */
259         error = virtqueue_enqueue_recv_refill(vq, m);
260         if (unlikely(error)) {
261                 RTE_LOG(ERR, PMD, "cannot requeue discarded mbuf");
262                 rte_pktmbuf_free_seg(m);
263         }
264 }
265
266 #define VIRTIO_MBUF_BURST_SZ 64
267 #define DESC_PER_CACHELINE (CACHE_LINE_SIZE / sizeof(struct vring_desc))
268 uint16_t
269 virtio_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
270 {
271         struct virtqueue *rxvq = rx_queue;
272         struct virtio_hw *hw = rxvq->hw;
273         struct rte_mbuf *rxm, *new_mbuf;
274         uint16_t nb_used, num, nb_rx = 0;
275         uint32_t len[VIRTIO_MBUF_BURST_SZ];
276         struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
277         int error;
278         uint32_t i, nb_enqueued = 0;
279
280         nb_used = VIRTQUEUE_NUSED(rxvq);
281
282         rmb();
283
284         num = (uint16_t)(likely(nb_used <= nb_pkts) ? nb_used : nb_pkts);
285         num = (uint16_t)(likely(num <= VIRTIO_MBUF_BURST_SZ) ? num : VIRTIO_MBUF_BURST_SZ);
286         if (likely(num > DESC_PER_CACHELINE))
287                 num = num - ((rxvq->vq_used_cons_idx + num) % DESC_PER_CACHELINE);
288
289         if (num == 0)
290                 return 0;
291
292         num = virtqueue_dequeue_burst_rx(rxvq, rcv_pkts, len, num);
293         PMD_RX_LOG(DEBUG, "used:%d dequeue:%d\n", nb_used, num);
294         for (i = 0; i < num ; i++) {
295                 rxm = rcv_pkts[i];
296
297                 PMD_RX_LOG(DEBUG, "packet len:%d\n", len[i]);
298
299                 if (unlikely(len[i]
300                              < (uint32_t)hw->vtnet_hdr_size + ETHER_HDR_LEN)) {
301                         PMD_RX_LOG(ERR, "Packet drop\n");
302                         nb_enqueued++;
303                         virtio_discard_rxbuf(rxvq, rxm);
304                         rxvq->errors++;
305                         continue;
306                 }
307
308                 rxm->pkt.in_port = rxvq->port_id;
309                 rxm->pkt.data = (char *)rxm->buf_addr + RTE_PKTMBUF_HEADROOM;
310                 rxm->pkt.nb_segs = 1;
311                 rxm->pkt.next = NULL;
312                 rxm->pkt.pkt_len  = (uint32_t)(len[i]
313                                                - sizeof(struct virtio_net_hdr));
314                 rxm->pkt.data_len = (uint16_t)(len[i]
315                                                - sizeof(struct virtio_net_hdr));
316
317                 VIRTIO_DUMP_PACKET(rxm, rxm->pkt.data_len);
318
319                 rx_pkts[nb_rx++] = rxm;
320                 rxvq->bytes += len[i] - sizeof(struct virtio_net_hdr);
321         }
322
323         rxvq->packets += nb_rx;
324
325         /* Allocate new mbuf for the used descriptor */
326         error = ENOSPC;
327         while (likely(!virtqueue_full(rxvq))) {
328                 new_mbuf = rte_rxmbuf_alloc(rxvq->mpool);
329                 if (unlikely(new_mbuf == NULL)) {
330                         struct rte_eth_dev *dev
331                                 = &rte_eth_devices[rxvq->port_id];
332                         dev->data->rx_mbuf_alloc_failed++;
333                         break;
334                 }
335                 error = virtqueue_enqueue_recv_refill(rxvq, new_mbuf);
336                 if (unlikely(error)) {
337                         rte_pktmbuf_free_seg(new_mbuf);
338                         break;
339                 }
340                 nb_enqueued++;
341         }
342         if (likely(nb_enqueued)) {
343                 if (unlikely(virtqueue_kick_prepare(rxvq))) {
344                         virtqueue_notify(rxvq);
345                         PMD_RX_LOG(DEBUG, "Notified\n");
346                 }
347         }
348
349         vq_update_avail_idx(rxvq);
350
351         return nb_rx;
352 }
353
354 uint16_t
355 virtio_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
356 {
357         struct virtqueue *txvq = tx_queue;
358         struct rte_mbuf *txm;
359         uint16_t nb_used, nb_tx, num;
360         int error;
361
362         nb_tx = 0;
363
364         if (unlikely(nb_pkts < 1))
365                 return nb_pkts;
366
367         PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
368         nb_used = VIRTQUEUE_NUSED(txvq);
369
370         rmb();
371
372         num = (uint16_t)(likely(nb_used < VIRTIO_MBUF_BURST_SZ) ? nb_used : VIRTIO_MBUF_BURST_SZ);
373
374         while (nb_tx < nb_pkts) {
375                 if (virtqueue_full(txvq) && num) {
376                         virtqueue_dequeue_pkt_tx(txvq);
377                         num--;
378                 }
379
380                 if (!virtqueue_full(txvq)) {
381                         txm = tx_pkts[nb_tx];
382                         /* Enqueue Packet buffers */
383                         error = virtqueue_enqueue_xmit(txvq, txm);
384                         if (unlikely(error)) {
385                                 if (error == ENOSPC)
386                                         PMD_TX_LOG(ERR, "virtqueue_enqueue Free count = 0\n");
387                                 else if (error == EMSGSIZE)
388                                         PMD_TX_LOG(ERR, "virtqueue_enqueue Free count < 1\n");
389                                 else
390                                         PMD_TX_LOG(ERR, "virtqueue_enqueue error: %d\n", error);
391                                 break;
392                         }
393                         nb_tx++;
394                         txvq->bytes += txm->pkt.data_len;
395                 } else {
396                         PMD_TX_LOG(ERR, "No free tx descriptors to transmit\n");
397                         break;
398                 }
399         }
400         vq_update_avail_idx(txvq);
401
402         txvq->packets += nb_tx;
403
404         if (unlikely(virtqueue_kick_prepare(txvq))) {
405                 virtqueue_notify(txvq);
406                 PMD_TX_LOG(DEBUG, "Notified backend after xmit\n");
407         }
408
409         return nb_tx;
410 }