update Intel copyright years to 2014
[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_memory.h>
41 #include <rte_memzone.h>
42 #include <rte_branch_prediction.h>
43 #include <rte_mempool.h>
44 #include <rte_malloc.h>
45 #include <rte_mbuf.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>
51
52 #include "virtio_logs.h"
53 #include "virtio_ethdev.h"
54 #include "virtqueue.h"
55
56 #ifdef  RTE_LIBRTE_VIRTIO_DEBUG_DUMP
57 #define VIRTIO_DUMP_PACKET(m, len) rte_pktmbuf_dump(m, len)
58 #else
59 #define  VIRTIO_DUMP_PACKET(m, len) do { } while (0)
60 #endif
61
62 static inline struct rte_mbuf *
63 rte_rxmbuf_alloc(struct rte_mempool *mp)
64 {
65         struct rte_mbuf *m;
66
67         m = __rte_mbuf_raw_alloc(mp);
68         __rte_mbuf_sanity_check_raw(m, RTE_MBUF_PKT, 0);
69
70         return (m);
71 }
72
73 static void
74 virtio_dev_vring_start(struct rte_eth_dev *dev, struct virtqueue *vq, int queue_type)
75 {
76         struct rte_mbuf *m;
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();
82
83         /*
84          * Reinitialise since virtio port might have been stopped and restarted
85          */
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);
92
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;
97
98         /*
99          * Disable device(host) interrupting guest
100          */
101         virtqueue_disable_intr(vq);
102
103         rte_snprintf(vq_name, sizeof(vq_name), "port_%d_rx_vq",
104                                         dev->data->port_id);
105         PMD_INIT_LOG(DEBUG, "vq name: %s\n", vq->vq_name);
106
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 */
112                 nbufs = 0;
113                 error = ENOSPC;
114                 while (!virtqueue_full(vq)) {
115                         m = rte_rxmbuf_alloc(vq->mpool);
116                         if (m == NULL)
117                                 break;
118                         /******************************************
119                         *         Enqueue allocated buffers        *
120                         *******************************************/
121                         error = virtqueue_enqueue_recv_refill(vq, m);
122                         if (error) {
123                                 rte_pktmbuf_free_seg(m);
124                                 break;
125                         }
126                         nbufs++;
127                 }
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);
132         } else {
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);
136         }
137 }
138
139 void
140 virtio_dev_rxtx_start(struct rte_eth_dev *dev)
141 {
142         /*
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
147          *
148          */
149         PMD_INIT_FUNC_TRACE();
150
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]);
154
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]);
158 }
159
160 int
161 virtio_dev_rx_queue_setup(struct rte_eth_dev *dev,
162                         uint16_t queue_idx,
163                         uint16_t nb_desc,
164                         unsigned int socket_id,
165                         __rte_unused const struct rte_eth_rxconf *rx_conf,
166                         struct rte_mempool *mp)
167 {
168         uint8_t vtpci_queue_idx = VTNET_SQ_RQ_QUEUE_IDX;
169         struct virtqueue *vq;
170         int ret;
171
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);
175         if (ret < 0) {
176                 PMD_INIT_LOG(ERR, "tvq initialization failed\n");
177                 return ret;
178         }
179         /* Create mempool for rx mbuf allocation */
180         vq->mpool = mp;
181
182         dev->data->rx_queues[queue_idx] = vq;
183         return (0);
184 }
185
186 /*
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
192  */
193 int
194 virtio_dev_tx_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_txconf *tx_conf)
199 {
200         uint8_t vtpci_queue_idx = VTNET_SQ_TQ_QUEUE_IDX;
201         struct virtqueue *vq;
202         int ret;
203
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);
207         if (ret < 0) {
208                 PMD_INIT_LOG(ERR, "rvq initialization failed\n");
209                 return ret;
210         }
211
212         dev->data->tx_queues[queue_idx] = vq;
213         return (0);
214 }
215
216 static void
217 virtio_discard_rxbuf(struct virtqueue *vq, struct rte_mbuf *m)
218 {
219         int error;
220         /*
221          * Requeue the discarded mbuf. This should always be
222          * successful since it was just dequeued.
223          */
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);
228         }
229 }
230
231 #define VIRTIO_MBUF_BURST_SZ 64
232 uint16_t
233 virtio_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
234 {
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];
241         int error;
242         uint32_t i, nb_enqueued = 0;
243
244         nb_used = VIRTQUEUE_NUSED(rxvq);
245
246         rmb();
247
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 ++) {
254                 rxm = rcv_pkts[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");
258                         nb_enqueued++;
259                         virtio_discard_rxbuf(rxvq, rxm);
260                         hw->eth_stats.ierrors++;
261                         continue;
262                 }
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);
272         }
273         hw->eth_stats.ipackets += nb_rx;
274
275         /* Allocate new mbuf for the used descriptor */
276         error = ENOSPC;
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++;
281                         break;
282                 }
283                 error = virtqueue_enqueue_recv_refill(rxvq, new_mbuf);
284                 if (unlikely(error)) {
285                         rte_pktmbuf_free_seg(new_mbuf);
286                         break;
287                 }
288                 nb_enqueued ++;
289         }
290         if(likely(nb_enqueued)) {
291                 if(unlikely(virtqueue_kick_prepare(rxvq))) {
292                         virtqueue_notify(rxvq);
293                         PMD_RX_LOG(DEBUG, "Notified\n");
294                 }
295         }
296         return (nb_rx);
297 }
298
299 uint16_t
300 virtio_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
301 {
302         struct virtqueue *txvq = tx_queue;
303         struct rte_mbuf *txm;
304         uint16_t nb_used, nb_tx, count, num, i;
305         int error;
306         uint32_t len[VIRTIO_MBUF_BURST_SZ];
307         struct rte_mbuf *snd_pkts[VIRTIO_MBUF_BURST_SZ];
308         struct virtio_hw *hw;
309
310         nb_tx = count = 0;
311
312         if (unlikely(nb_pkts < 1))
313                 return (nb_pkts);
314
315         PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
316         nb_used = VIRTQUEUE_NUSED(txvq);
317
318         rmb();
319
320         hw = txvq->hw;
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]);
325         }
326
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 */
336                                 if (error == ENOSPC)
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");
340                                 else
341                                         PMD_TX_LOG(ERR, "virtqueue_enqueue error: %d\n", error);
342                                 break;
343                         }
344                         nb_tx++;
345                         hw->eth_stats.obytes += txm->pkt.data_len;
346                 } else {
347                         PMD_TX_LOG(ERR, "No free tx descriptors to transmit\n");
348                         virtqueue_notify(txvq);
349                         break;
350                 }
351         }
352         hw->eth_stats.opackets += nb_tx;
353
354         if(unlikely(virtqueue_kick_prepare(txvq))) {
355                 virtqueue_notify(txvq);
356                 PMD_TX_LOG(DEBUG, "Notified backend after xmit\n");
357         }
358         return (nb_tx);
359 }