virtio: code-style cleanup
[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                 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);
142         } else {
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);
146         }
147 }
148
149 void
150 virtio_dev_rxtx_start(struct rte_eth_dev *dev)
151 {
152         /*
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
157          *
158          */
159         PMD_INIT_FUNC_TRACE();
160
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]);
164
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]);
168 }
169
170 int
171 virtio_dev_rx_queue_setup(struct rte_eth_dev *dev,
172                         uint16_t queue_idx,
173                         uint16_t nb_desc,
174                         unsigned int socket_id,
175                         __rte_unused const struct rte_eth_rxconf *rx_conf,
176                         struct rte_mempool *mp)
177 {
178         uint8_t vtpci_queue_idx = VTNET_SQ_RQ_QUEUE_IDX;
179         struct virtqueue *vq;
180         int ret;
181
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);
185         if (ret < 0) {
186                 PMD_INIT_LOG(ERR, "tvq initialization failed\n");
187                 return ret;
188         }
189
190         /* Create mempool for rx mbuf allocation */
191         vq->mpool = mp;
192
193         dev->data->rx_queues[queue_idx] = vq;
194         return (0);
195 }
196
197 /*
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
203  */
204 int
205 virtio_dev_tx_queue_setup(struct rte_eth_dev *dev,
206                         uint16_t queue_idx,
207                         uint16_t nb_desc,
208                         unsigned int socket_id,
209                         __rte_unused const struct rte_eth_txconf *tx_conf)
210 {
211         uint8_t vtpci_queue_idx = VTNET_SQ_TQ_QUEUE_IDX;
212         struct virtqueue *vq;
213         int ret;
214
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);
218         if (ret < 0) {
219                 PMD_INIT_LOG(ERR, "rvq initialization failed\n");
220                 return ret;
221         }
222
223         dev->data->tx_queues[queue_idx] = vq;
224         return (0);
225 }
226
227 static void
228 virtio_discard_rxbuf(struct virtqueue *vq, struct rte_mbuf *m)
229 {
230         int error;
231         /*
232          * Requeue the discarded mbuf. This should always be
233          * successful since it was just dequeued.
234          */
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);
239         }
240 }
241
242 #define VIRTIO_MBUF_BURST_SZ 64
243 #define DESC_PER_CACHELINE (CACHE_LINE_SIZE / sizeof(struct vring_desc))
244 uint16_t
245 virtio_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
246 {
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];
253         int error;
254         uint32_t i, nb_enqueued = 0;
255
256         nb_used = VIRTQUEUE_NUSED(rxvq);
257
258         rmb();
259
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);
264
265         if(num == 0) return 0;
266
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 ++) {
270                 rxm = rcv_pkts[i];
271
272                 PMD_RX_LOG(DEBUG, "packet len:%d\n", len[i]);
273
274                 if (unlikely(len[i]
275                         < (uint32_t)hw->vtnet_hdr_size + ETHER_HDR_LEN)) {
276                         PMD_RX_LOG(ERR, "Packet drop\n");
277                         nb_enqueued++;
278                         virtio_discard_rxbuf(rxvq, rxm);
279                         hw->eth_stats.ierrors++;
280                         continue;
281                 }
282
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));
291
292                 VIRTIO_DUMP_PACKET(rxm, rxm->pkt.data_len);
293
294                 rx_pkts[nb_rx++] = rxm;
295                 hw->eth_stats.ibytes += len[i] - sizeof(struct virtio_net_hdr);
296         }
297
298         hw->eth_stats.ipackets += nb_rx;
299
300         /* Allocate new mbuf for the used descriptor */
301         error = ENOSPC;
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++;
306                         break;
307                 }
308                 error = virtqueue_enqueue_recv_refill(rxvq, new_mbuf);
309                 if (unlikely(error)) {
310                         rte_pktmbuf_free_seg(new_mbuf);
311                         break;
312                 }
313                 nb_enqueued ++;
314         }
315         if (likely(nb_enqueued)) {
316                 if (unlikely(virtqueue_kick_prepare(rxvq))) {
317                         virtqueue_notify(rxvq);
318                         PMD_RX_LOG(DEBUG, "Notified\n");
319                 }
320         }
321
322         vq_update_avail_idx(rxvq);
323
324         return (nb_rx);
325 }
326
327 uint16_t
328 virtio_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
329 {
330         struct virtqueue *txvq = tx_queue;
331         struct rte_mbuf *txm;
332         uint16_t nb_used, nb_tx, num;
333         int error;
334         struct virtio_hw *hw;
335
336         nb_tx = 0;
337
338         if (unlikely(nb_pkts < 1))
339                 return (nb_pkts);
340
341         PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
342         nb_used = VIRTQUEUE_NUSED(txvq);
343
344         rmb();
345
346         hw = txvq->hw;
347         num = (uint16_t)(likely(nb_used < VIRTIO_MBUF_BURST_SZ) ? nb_used : VIRTIO_MBUF_BURST_SZ);
348
349         while (nb_tx < nb_pkts) {
350                 if (virtqueue_full(txvq) && num) {
351                         virtqueue_dequeue_pkt_tx(txvq);
352                         num--;
353                 }
354
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)) {
360                                 if (error == ENOSPC)
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");
364                                 else
365                                         PMD_TX_LOG(ERR, "virtqueue_enqueue error: %d\n", error);
366                                 break;
367                         }
368                         nb_tx++;
369                         hw->eth_stats.obytes += txm->pkt.data_len;
370                 } else {
371                         PMD_TX_LOG(ERR, "No free tx descriptors to transmit\n");
372                         break;
373                 }
374         }
375         vq_update_avail_idx(txvq);
376
377         hw->eth_stats.opackets += nb_tx;
378
379         if(unlikely(virtqueue_kick_prepare(txvq))) {
380                 virtqueue_notify(txvq);
381                 PMD_TX_LOG(DEBUG, "Notified backend after xmit\n");
382         }
383
384         return (nb_tx);
385 }