remove trailing whitespaces
[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(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         rte_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         PMD_INIT_FUNC_TRACE();
179
180         /* Start rx vring. */
181         for (i = 0; i < dev->data->nb_rx_queues; i++) {
182                 virtio_dev_vring_start(dev, dev->data->rx_queues[i], VTNET_RQ);
183                 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->rx_queues[i]);
184         }
185
186         /* Start tx vring. */
187         for (i = 0; i < dev->data->nb_tx_queues; i++) {
188                 virtio_dev_vring_start(dev, dev->data->tx_queues[i], VTNET_TQ);
189                 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->tx_queues[i]);
190         }
191 }
192
193 int
194 virtio_dev_rx_queue_setup(struct rte_eth_dev *dev,
195                         uint16_t queue_idx,
196                         uint16_t nb_desc,
197                         unsigned int socket_id,
198                         __rte_unused const struct rte_eth_rxconf *rx_conf,
199                         struct rte_mempool *mp)
200 {
201         uint8_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_RQ_QUEUE_IDX;
202         struct virtqueue *vq;
203         int ret;
204
205         PMD_INIT_FUNC_TRACE();
206         ret = virtio_dev_queue_setup(dev, VTNET_RQ, queue_idx, vtpci_queue_idx,
207                         nb_desc, socket_id, &vq);
208         if (ret < 0) {
209                 PMD_INIT_LOG(ERR, "tvq initialization failed\n");
210                 return ret;
211         }
212
213         /* Create mempool for rx mbuf allocation */
214         vq->mpool = mp;
215
216         dev->data->rx_queues[queue_idx] = vq;
217         return (0);
218 }
219
220 /*
221  * struct rte_eth_dev *dev: Used to update dev
222  * uint16_t nb_desc: Defaults to values read from config space
223  * unsigned int socket_id: Used to allocate memzone
224  * const struct rte_eth_txconf *tx_conf: Used to setup tx engine
225  * uint16_t queue_idx: Just used as an index in dev txq list
226  */
227 int
228 virtio_dev_tx_queue_setup(struct rte_eth_dev *dev,
229                         uint16_t queue_idx,
230                         uint16_t nb_desc,
231                         unsigned int socket_id,
232                         __rte_unused const struct rte_eth_txconf *tx_conf)
233 {
234         uint8_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_TQ_QUEUE_IDX;
235         struct virtqueue *vq;
236         int ret;
237
238         PMD_INIT_FUNC_TRACE();
239         ret = virtio_dev_queue_setup(dev, VTNET_TQ, queue_idx, vtpci_queue_idx,
240                         nb_desc, socket_id, &vq);
241         if (ret < 0) {
242                 PMD_INIT_LOG(ERR, "rvq initialization failed\n");
243                 return ret;
244         }
245
246         dev->data->tx_queues[queue_idx] = vq;
247         return (0);
248 }
249
250 static void
251 virtio_discard_rxbuf(struct virtqueue *vq, struct rte_mbuf *m)
252 {
253         int error;
254         /*
255          * Requeue the discarded mbuf. This should always be
256          * successful since it was just dequeued.
257          */
258         error = virtqueue_enqueue_recv_refill(vq, m);
259         if (unlikely(error)) {
260                 RTE_LOG(ERR, PMD, "cannot requeue discarded mbuf");
261                 rte_pktmbuf_free_seg(m);
262         }
263 }
264
265 #define VIRTIO_MBUF_BURST_SZ 64
266 #define DESC_PER_CACHELINE (CACHE_LINE_SIZE / sizeof(struct vring_desc))
267 uint16_t
268 virtio_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
269 {
270         struct virtqueue *rxvq = rx_queue;
271         struct virtio_hw *hw = rxvq->hw;
272         struct rte_mbuf *rxm, *new_mbuf;
273         uint16_t nb_used, num, nb_rx = 0;
274         uint32_t len[VIRTIO_MBUF_BURST_SZ];
275         struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
276         int error;
277         uint32_t i, nb_enqueued = 0;
278
279         nb_used = VIRTQUEUE_NUSED(rxvq);
280
281         rmb();
282
283         num = (uint16_t)(likely(nb_used <= nb_pkts) ? nb_used : nb_pkts);
284         num = (uint16_t)(likely(num <= VIRTIO_MBUF_BURST_SZ) ? num : VIRTIO_MBUF_BURST_SZ);
285         if (likely(num > DESC_PER_CACHELINE))
286                 num = num - ((rxvq->vq_used_cons_idx + num) % DESC_PER_CACHELINE);
287
288         if(num == 0) return 0;
289
290         num = virtqueue_dequeue_burst_rx(rxvq, rcv_pkts, len, num);
291         PMD_RX_LOG(DEBUG, "used:%d dequeue:%d\n", nb_used, num);
292         for (i = 0; i < num ; i ++) {
293                 rxm = rcv_pkts[i];
294
295                 PMD_RX_LOG(DEBUG, "packet len:%d\n", len[i]);
296
297                 if (unlikely(len[i]
298                         < (uint32_t)hw->vtnet_hdr_size + ETHER_HDR_LEN)) {
299                         PMD_RX_LOG(ERR, "Packet drop\n");
300                         nb_enqueued++;
301                         virtio_discard_rxbuf(rxvq, rxm);
302                         hw->eth_stats.ierrors++;
303                         continue;
304                 }
305
306                 rxm->pkt.in_port = rxvq->port_id;
307                 rxm->pkt.data = (char *)rxm->buf_addr + RTE_PKTMBUF_HEADROOM;
308                 rxm->pkt.nb_segs = 1;
309                 rxm->pkt.next = NULL;
310                 rxm->pkt.pkt_len  = (uint32_t)(len[i]
311                         - sizeof(struct virtio_net_hdr));
312                 rxm->pkt.data_len = (uint16_t)(len[i]
313                         - sizeof(struct virtio_net_hdr));
314
315                 VIRTIO_DUMP_PACKET(rxm, rxm->pkt.data_len);
316
317                 rx_pkts[nb_rx++] = rxm;
318                 hw->eth_stats.ibytes += len[i] - sizeof(struct virtio_net_hdr);
319                 hw->eth_stats.q_ibytes[rxvq->queue_id] += len[i]
320                         - sizeof(struct virtio_net_hdr);
321         }
322
323         hw->eth_stats.ipackets += nb_rx;
324         hw->eth_stats.q_ipackets[rxvq->queue_id] += nb_rx;
325
326         /* Allocate new mbuf for the used descriptor */
327         error = ENOSPC;
328         while (likely(!virtqueue_full(rxvq))) {
329                 new_mbuf = rte_rxmbuf_alloc(rxvq->mpool);
330                 if (unlikely(new_mbuf == NULL)) {
331                         hw->eth_stats.rx_nombuf++;
332                         break;
333                 }
334                 error = virtqueue_enqueue_recv_refill(rxvq, new_mbuf);
335                 if (unlikely(error)) {
336                         rte_pktmbuf_free_seg(new_mbuf);
337                         break;
338                 }
339                 nb_enqueued ++;
340         }
341         if (likely(nb_enqueued)) {
342                 if (unlikely(virtqueue_kick_prepare(rxvq))) {
343                         virtqueue_notify(rxvq);
344                         PMD_RX_LOG(DEBUG, "Notified\n");
345                 }
346         }
347
348         vq_update_avail_idx(rxvq);
349
350         return (nb_rx);
351 }
352
353 uint16_t
354 virtio_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
355 {
356         struct virtqueue *txvq = tx_queue;
357         struct rte_mbuf *txm;
358         uint16_t nb_used, nb_tx, num;
359         int error;
360         struct virtio_hw *hw;
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         hw = txvq->hw;
373         num = (uint16_t)(likely(nb_used < VIRTIO_MBUF_BURST_SZ) ? nb_used : VIRTIO_MBUF_BURST_SZ);
374
375         while (nb_tx < nb_pkts) {
376                 if (virtqueue_full(txvq) && num) {
377                         virtqueue_dequeue_pkt_tx(txvq);
378                         num--;
379                 }
380
381                 if(!virtqueue_full(txvq)) {
382                         txm = tx_pkts[nb_tx];
383                         /* Enqueue Packet buffers */
384                         error = virtqueue_enqueue_xmit(txvq, txm);
385                         if (unlikely(error)) {
386                                 if (error == ENOSPC)
387                                         PMD_TX_LOG(ERR, "virtqueue_enqueue Free count = 0\n");
388                                 else if (error == EMSGSIZE)
389                                         PMD_TX_LOG(ERR, "virtqueue_enqueue Free count < 1\n");
390                                 else
391                                         PMD_TX_LOG(ERR, "virtqueue_enqueue error: %d\n", error);
392                                 break;
393                         }
394                         nb_tx++;
395                         hw->eth_stats.obytes += txm->pkt.data_len;
396                         hw->eth_stats.q_obytes[txvq->queue_id]
397                                 += txm->pkt.data_len;
398                 } else {
399                         PMD_TX_LOG(ERR, "No free tx descriptors to transmit\n");
400                         break;
401                 }
402         }
403         vq_update_avail_idx(txvq);
404
405         hw->eth_stats.opackets += nb_tx;
406         hw->eth_stats.q_opackets[txvq->queue_id] += nb_tx;
407
408         if(unlikely(virtqueue_kick_prepare(txvq))) {
409                 virtqueue_notify(txvq);
410                 PMD_TX_LOG(DEBUG, "Notified backend after xmit\n");
411         }
412
413         return (nb_tx);
414 }