virtio: add extended stats
[dpdk.git] / drivers / net / 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 #include <rte_byteorder.h>
53
54 #include "virtio_logs.h"
55 #include "virtio_ethdev.h"
56 #include "virtio_pci.h"
57 #include "virtqueue.h"
58 #include "virtio_rxtx.h"
59
60 #ifdef RTE_LIBRTE_VIRTIO_DEBUG_DUMP
61 #define VIRTIO_DUMP_PACKET(m, len) rte_pktmbuf_dump(stdout, m, len)
62 #else
63 #define  VIRTIO_DUMP_PACKET(m, len) do { } while (0)
64 #endif
65
66
67 #define VIRTIO_SIMPLE_FLAGS ((uint32_t)ETH_TXQ_FLAGS_NOMULTSEGS | \
68         ETH_TXQ_FLAGS_NOOFFLOADS)
69
70 static int use_simple_rxtx;
71
72 static void
73 vq_ring_free_chain(struct virtqueue *vq, uint16_t desc_idx)
74 {
75         struct vring_desc *dp, *dp_tail;
76         struct vq_desc_extra *dxp;
77         uint16_t desc_idx_last = desc_idx;
78
79         dp  = &vq->vq_ring.desc[desc_idx];
80         dxp = &vq->vq_descx[desc_idx];
81         vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt + dxp->ndescs);
82         if ((dp->flags & VRING_DESC_F_INDIRECT) == 0) {
83                 while (dp->flags & VRING_DESC_F_NEXT) {
84                         desc_idx_last = dp->next;
85                         dp = &vq->vq_ring.desc[dp->next];
86                 }
87         }
88         dxp->ndescs = 0;
89
90         /*
91          * We must append the existing free chain, if any, to the end of
92          * newly freed chain. If the virtqueue was completely used, then
93          * head would be VQ_RING_DESC_CHAIN_END (ASSERTed above).
94          */
95         if (vq->vq_desc_tail_idx == VQ_RING_DESC_CHAIN_END) {
96                 vq->vq_desc_head_idx = desc_idx;
97         } else {
98                 dp_tail = &vq->vq_ring.desc[vq->vq_desc_tail_idx];
99                 dp_tail->next = desc_idx;
100         }
101
102         vq->vq_desc_tail_idx = desc_idx_last;
103         dp->next = VQ_RING_DESC_CHAIN_END;
104 }
105
106 static uint16_t
107 virtqueue_dequeue_burst_rx(struct virtqueue *vq, struct rte_mbuf **rx_pkts,
108                            uint32_t *len, uint16_t num)
109 {
110         struct vring_used_elem *uep;
111         struct rte_mbuf *cookie;
112         uint16_t used_idx, desc_idx;
113         uint16_t i;
114
115         /*  Caller does the check */
116         for (i = 0; i < num ; i++) {
117                 used_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
118                 uep = &vq->vq_ring.used->ring[used_idx];
119                 desc_idx = (uint16_t) uep->id;
120                 len[i] = uep->len;
121                 cookie = (struct rte_mbuf *)vq->vq_descx[desc_idx].cookie;
122
123                 if (unlikely(cookie == NULL)) {
124                         PMD_DRV_LOG(ERR, "vring descriptor with no mbuf cookie at %u\n",
125                                 vq->vq_used_cons_idx);
126                         break;
127                 }
128
129                 rte_prefetch0(cookie);
130                 rte_packet_prefetch(rte_pktmbuf_mtod(cookie, void *));
131                 rx_pkts[i]  = cookie;
132                 vq->vq_used_cons_idx++;
133                 vq_ring_free_chain(vq, desc_idx);
134                 vq->vq_descx[desc_idx].cookie = NULL;
135         }
136
137         return i;
138 }
139
140 #ifndef DEFAULT_TX_FREE_THRESH
141 #define DEFAULT_TX_FREE_THRESH 32
142 #endif
143
144 /* Cleanup from completed transmits. */
145 static void
146 virtio_xmit_cleanup(struct virtqueue *vq, uint16_t num)
147 {
148         uint16_t i, used_idx, desc_idx;
149         for (i = 0; i < num; i++) {
150                 struct vring_used_elem *uep;
151                 struct vq_desc_extra *dxp;
152
153                 used_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
154                 uep = &vq->vq_ring.used->ring[used_idx];
155
156                 desc_idx = (uint16_t) uep->id;
157                 dxp = &vq->vq_descx[desc_idx];
158                 vq->vq_used_cons_idx++;
159                 vq_ring_free_chain(vq, desc_idx);
160
161                 if (dxp->cookie != NULL) {
162                         rte_pktmbuf_free(dxp->cookie);
163                         dxp->cookie = NULL;
164                 }
165         }
166 }
167
168
169 static inline int
170 virtqueue_enqueue_recv_refill(struct virtqueue *vq, struct rte_mbuf *cookie)
171 {
172         struct vq_desc_extra *dxp;
173         struct virtio_hw *hw = vq->hw;
174         struct vring_desc *start_dp;
175         uint16_t needed = 1;
176         uint16_t head_idx, idx;
177
178         if (unlikely(vq->vq_free_cnt == 0))
179                 return -ENOSPC;
180         if (unlikely(vq->vq_free_cnt < needed))
181                 return -EMSGSIZE;
182
183         head_idx = vq->vq_desc_head_idx;
184         if (unlikely(head_idx >= vq->vq_nentries))
185                 return -EFAULT;
186
187         idx = head_idx;
188         dxp = &vq->vq_descx[idx];
189         dxp->cookie = (void *)cookie;
190         dxp->ndescs = needed;
191
192         start_dp = vq->vq_ring.desc;
193         start_dp[idx].addr =
194                 (uint64_t)(cookie->buf_physaddr + RTE_PKTMBUF_HEADROOM
195                 - hw->vtnet_hdr_size);
196         start_dp[idx].len =
197                 cookie->buf_len - RTE_PKTMBUF_HEADROOM + hw->vtnet_hdr_size;
198         start_dp[idx].flags =  VRING_DESC_F_WRITE;
199         idx = start_dp[idx].next;
200         vq->vq_desc_head_idx = idx;
201         if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
202                 vq->vq_desc_tail_idx = idx;
203         vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - needed);
204         vq_update_avail_ring(vq, head_idx);
205
206         return 0;
207 }
208
209 static int
210 virtqueue_enqueue_xmit(struct virtqueue *txvq, struct rte_mbuf *cookie)
211 {
212         struct vq_desc_extra *dxp;
213         struct vring_desc *start_dp;
214         uint16_t seg_num = cookie->nb_segs;
215         uint16_t needed = 1 + seg_num;
216         uint16_t head_idx, idx;
217         size_t head_size = txvq->hw->vtnet_hdr_size;
218
219         if (unlikely(txvq->vq_free_cnt == 0))
220                 return -ENOSPC;
221         if (unlikely(txvq->vq_free_cnt < needed))
222                 return -EMSGSIZE;
223         head_idx = txvq->vq_desc_head_idx;
224         if (unlikely(head_idx >= txvq->vq_nentries))
225                 return -EFAULT;
226
227         idx = head_idx;
228         dxp = &txvq->vq_descx[idx];
229         dxp->cookie = (void *)cookie;
230         dxp->ndescs = needed;
231
232         start_dp = txvq->vq_ring.desc;
233         start_dp[idx].addr =
234                 txvq->virtio_net_hdr_mem + idx * head_size;
235         start_dp[idx].len = head_size;
236         start_dp[idx].flags = VRING_DESC_F_NEXT;
237
238         for (; ((seg_num > 0) && (cookie != NULL)); seg_num--) {
239                 idx = start_dp[idx].next;
240                 start_dp[idx].addr  = RTE_MBUF_DATA_DMA_ADDR(cookie);
241                 start_dp[idx].len   = cookie->data_len;
242                 start_dp[idx].flags = VRING_DESC_F_NEXT;
243                 cookie = cookie->next;
244         }
245
246         start_dp[idx].flags &= ~VRING_DESC_F_NEXT;
247         idx = start_dp[idx].next;
248         txvq->vq_desc_head_idx = idx;
249         if (txvq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
250                 txvq->vq_desc_tail_idx = idx;
251         txvq->vq_free_cnt = (uint16_t)(txvq->vq_free_cnt - needed);
252         vq_update_avail_ring(txvq, head_idx);
253
254         return 0;
255 }
256
257 static inline struct rte_mbuf *
258 rte_rxmbuf_alloc(struct rte_mempool *mp)
259 {
260         struct rte_mbuf *m;
261
262         m = __rte_mbuf_raw_alloc(mp);
263         __rte_mbuf_sanity_check_raw(m, 0);
264
265         return m;
266 }
267
268 static void
269 virtio_dev_vring_start(struct virtqueue *vq, int queue_type)
270 {
271         struct rte_mbuf *m;
272         int i, nbufs, error, size = vq->vq_nentries;
273         struct vring *vr = &vq->vq_ring;
274         uint8_t *ring_mem = vq->vq_ring_virt_mem;
275
276         PMD_INIT_FUNC_TRACE();
277
278         /*
279          * Reinitialise since virtio port might have been stopped and restarted
280          */
281         memset(vq->vq_ring_virt_mem, 0, vq->vq_ring_size);
282         vring_init(vr, size, ring_mem, VIRTIO_PCI_VRING_ALIGN);
283         vq->vq_used_cons_idx = 0;
284         vq->vq_desc_head_idx = 0;
285         vq->vq_avail_idx = 0;
286         vq->vq_desc_tail_idx = (uint16_t)(vq->vq_nentries - 1);
287         vq->vq_free_cnt = vq->vq_nentries;
288         memset(vq->vq_descx, 0, sizeof(struct vq_desc_extra) * vq->vq_nentries);
289
290         /* Chain all the descriptors in the ring with an END */
291         for (i = 0; i < size - 1; i++)
292                 vr->desc[i].next = (uint16_t)(i + 1);
293         vr->desc[i].next = VQ_RING_DESC_CHAIN_END;
294
295         /*
296          * Disable device(host) interrupting guest
297          */
298         virtqueue_disable_intr(vq);
299
300         /* Only rx virtqueue needs mbufs to be allocated at initialization */
301         if (queue_type == VTNET_RQ) {
302                 if (vq->mpool == NULL)
303                         rte_exit(EXIT_FAILURE,
304                         "Cannot allocate initial mbufs for rx virtqueue");
305
306                 /* Allocate blank mbufs for the each rx descriptor */
307                 nbufs = 0;
308                 error = ENOSPC;
309
310                 if (use_simple_rxtx)
311                         for (i = 0; i < vq->vq_nentries; i++) {
312                                 vq->vq_ring.avail->ring[i] = i;
313                                 vq->vq_ring.desc[i].flags = VRING_DESC_F_WRITE;
314                         }
315
316                 memset(&vq->fake_mbuf, 0, sizeof(vq->fake_mbuf));
317                 for (i = 0; i < RTE_PMD_VIRTIO_RX_MAX_BURST; i++)
318                         vq->sw_ring[vq->vq_nentries + i] = &vq->fake_mbuf;
319
320                 while (!virtqueue_full(vq)) {
321                         m = rte_rxmbuf_alloc(vq->mpool);
322                         if (m == NULL)
323                                 break;
324
325                         /******************************************
326                         *         Enqueue allocated buffers        *
327                         *******************************************/
328                         if (use_simple_rxtx)
329                                 error = virtqueue_enqueue_recv_refill_simple(vq, m);
330                         else
331                                 error = virtqueue_enqueue_recv_refill(vq, m);
332                         if (error) {
333                                 rte_pktmbuf_free(m);
334                                 break;
335                         }
336                         nbufs++;
337                 }
338
339                 vq_update_avail_idx(vq);
340
341                 PMD_INIT_LOG(DEBUG, "Allocated %d bufs", nbufs);
342
343                 VIRTIO_WRITE_REG_2(vq->hw, VIRTIO_PCI_QUEUE_SEL,
344                         vq->vq_queue_index);
345                 VIRTIO_WRITE_REG_4(vq->hw, VIRTIO_PCI_QUEUE_PFN,
346                         vq->mz->phys_addr >> VIRTIO_PCI_QUEUE_ADDR_SHIFT);
347         } else if (queue_type == VTNET_TQ) {
348                 if (use_simple_rxtx) {
349                         int mid_idx  = vq->vq_nentries >> 1;
350                         for (i = 0; i < mid_idx; i++) {
351                                 vq->vq_ring.avail->ring[i] = i + mid_idx;
352                                 vq->vq_ring.desc[i + mid_idx].next = i;
353                                 vq->vq_ring.desc[i + mid_idx].addr =
354                                         vq->virtio_net_hdr_mem +
355                                                 mid_idx * vq->hw->vtnet_hdr_size;
356                                 vq->vq_ring.desc[i + mid_idx].len =
357                                         vq->hw->vtnet_hdr_size;
358                                 vq->vq_ring.desc[i + mid_idx].flags =
359                                         VRING_DESC_F_NEXT;
360                                 vq->vq_ring.desc[i].flags = 0;
361                         }
362                         for (i = mid_idx; i < vq->vq_nentries; i++)
363                                 vq->vq_ring.avail->ring[i] = i;
364                 }
365
366                 VIRTIO_WRITE_REG_2(vq->hw, VIRTIO_PCI_QUEUE_SEL,
367                         vq->vq_queue_index);
368                 VIRTIO_WRITE_REG_4(vq->hw, VIRTIO_PCI_QUEUE_PFN,
369                         vq->mz->phys_addr >> VIRTIO_PCI_QUEUE_ADDR_SHIFT);
370         } else {
371                 VIRTIO_WRITE_REG_2(vq->hw, VIRTIO_PCI_QUEUE_SEL,
372                         vq->vq_queue_index);
373                 VIRTIO_WRITE_REG_4(vq->hw, VIRTIO_PCI_QUEUE_PFN,
374                         vq->mz->phys_addr >> VIRTIO_PCI_QUEUE_ADDR_SHIFT);
375         }
376 }
377
378 void
379 virtio_dev_cq_start(struct rte_eth_dev *dev)
380 {
381         struct virtio_hw *hw = dev->data->dev_private;
382
383         if (hw->cvq) {
384                 virtio_dev_vring_start(hw->cvq, VTNET_CQ);
385                 VIRTQUEUE_DUMP((struct virtqueue *)hw->cvq);
386         }
387 }
388
389 void
390 virtio_dev_rxtx_start(struct rte_eth_dev *dev)
391 {
392         /*
393          * Start receive and transmit vrings
394          * -    Setup vring structure for all queues
395          * -    Initialize descriptor for the rx vring
396          * -    Allocate blank mbufs for the each rx descriptor
397          *
398          */
399         int i;
400
401         PMD_INIT_FUNC_TRACE();
402
403         /* Start rx vring. */
404         for (i = 0; i < dev->data->nb_rx_queues; i++) {
405                 virtio_dev_vring_start(dev->data->rx_queues[i], VTNET_RQ);
406                 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->rx_queues[i]);
407         }
408
409         /* Start tx vring. */
410         for (i = 0; i < dev->data->nb_tx_queues; i++) {
411                 virtio_dev_vring_start(dev->data->tx_queues[i], VTNET_TQ);
412                 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->tx_queues[i]);
413         }
414 }
415
416 int
417 virtio_dev_rx_queue_setup(struct rte_eth_dev *dev,
418                         uint16_t queue_idx,
419                         uint16_t nb_desc,
420                         unsigned int socket_id,
421                         __rte_unused const struct rte_eth_rxconf *rx_conf,
422                         struct rte_mempool *mp)
423 {
424         uint16_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_RQ_QUEUE_IDX;
425         struct virtqueue *vq;
426         int ret;
427
428         PMD_INIT_FUNC_TRACE();
429         ret = virtio_dev_queue_setup(dev, VTNET_RQ, queue_idx, vtpci_queue_idx,
430                         nb_desc, socket_id, &vq);
431         if (ret < 0) {
432                 PMD_INIT_LOG(ERR, "rvq initialization failed");
433                 return ret;
434         }
435
436         /* Create mempool for rx mbuf allocation */
437         vq->mpool = mp;
438
439         dev->data->rx_queues[queue_idx] = vq;
440
441         virtio_rxq_vec_setup(vq);
442
443         return 0;
444 }
445
446 void
447 virtio_dev_rx_queue_release(void *rxq)
448 {
449         virtio_dev_queue_release(rxq);
450 }
451
452 /*
453  * struct rte_eth_dev *dev: Used to update dev
454  * uint16_t nb_desc: Defaults to values read from config space
455  * unsigned int socket_id: Used to allocate memzone
456  * const struct rte_eth_txconf *tx_conf: Used to setup tx engine
457  * uint16_t queue_idx: Just used as an index in dev txq list
458  */
459 int
460 virtio_dev_tx_queue_setup(struct rte_eth_dev *dev,
461                         uint16_t queue_idx,
462                         uint16_t nb_desc,
463                         unsigned int socket_id,
464                         const struct rte_eth_txconf *tx_conf)
465 {
466         uint8_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_TQ_QUEUE_IDX;
467         struct virtio_hw *hw = dev->data->dev_private;
468         struct virtqueue *vq;
469         uint16_t tx_free_thresh;
470         int ret;
471
472         PMD_INIT_FUNC_TRACE();
473
474         if ((tx_conf->txq_flags & ETH_TXQ_FLAGS_NOXSUMS)
475             != ETH_TXQ_FLAGS_NOXSUMS) {
476                 PMD_INIT_LOG(ERR, "TX checksum offload not supported\n");
477                 return -EINVAL;
478         }
479
480         /* Use simple rx/tx func if single segment and no offloads */
481         if ((tx_conf->txq_flags & VIRTIO_SIMPLE_FLAGS) == VIRTIO_SIMPLE_FLAGS &&
482              !vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
483                 PMD_INIT_LOG(INFO, "Using simple rx/tx path");
484                 dev->tx_pkt_burst = virtio_xmit_pkts_simple;
485                 dev->rx_pkt_burst = virtio_recv_pkts_vec;
486                 use_simple_rxtx = 1;
487         }
488
489         ret = virtio_dev_queue_setup(dev, VTNET_TQ, queue_idx, vtpci_queue_idx,
490                         nb_desc, socket_id, &vq);
491         if (ret < 0) {
492                 PMD_INIT_LOG(ERR, "rvq initialization failed");
493                 return ret;
494         }
495
496         tx_free_thresh = tx_conf->tx_free_thresh;
497         if (tx_free_thresh == 0)
498                 tx_free_thresh =
499                         RTE_MIN(vq->vq_nentries / 4, DEFAULT_TX_FREE_THRESH);
500
501         if (tx_free_thresh >= (vq->vq_nentries - 3)) {
502                 RTE_LOG(ERR, PMD, "tx_free_thresh must be less than the "
503                         "number of TX entries minus 3 (%u)."
504                         " (tx_free_thresh=%u port=%u queue=%u)\n",
505                         vq->vq_nentries - 3,
506                         tx_free_thresh, dev->data->port_id, queue_idx);
507                 return -EINVAL;
508         }
509
510         vq->vq_free_thresh = tx_free_thresh;
511
512         dev->data->tx_queues[queue_idx] = vq;
513         return 0;
514 }
515
516 void
517 virtio_dev_tx_queue_release(void *txq)
518 {
519         virtio_dev_queue_release(txq);
520 }
521
522 static void
523 virtio_discard_rxbuf(struct virtqueue *vq, struct rte_mbuf *m)
524 {
525         int error;
526         /*
527          * Requeue the discarded mbuf. This should always be
528          * successful since it was just dequeued.
529          */
530         error = virtqueue_enqueue_recv_refill(vq, m);
531         if (unlikely(error)) {
532                 RTE_LOG(ERR, PMD, "cannot requeue discarded mbuf");
533                 rte_pktmbuf_free(m);
534         }
535 }
536
537 static void
538 virtio_update_packet_stats(struct virtqueue *vq, struct rte_mbuf *mbuf)
539 {
540         uint32_t s = mbuf->pkt_len;
541         struct ether_addr *ea;
542
543         if (s == 64) {
544                 vq->size_bins[1]++;
545         } else if (s > 64 && s < 1024) {
546                 uint32_t bin;
547
548                 /* count zeros, and offset into correct bin */
549                 bin = (sizeof(s) * 8) - __builtin_clz(s) - 5;
550                 vq->size_bins[bin]++;
551         } else {
552                 if (s < 64)
553                         vq->size_bins[0]++;
554                 else if (s < 1519)
555                         vq->size_bins[6]++;
556                 else if (s >= 1519)
557                         vq->size_bins[7]++;
558         }
559
560         ea = rte_pktmbuf_mtod(mbuf, struct ether_addr *);
561         vq->multicast += is_multicast_ether_addr(ea);
562         vq->broadcast += is_broadcast_ether_addr(ea);
563 }
564
565 #define VIRTIO_MBUF_BURST_SZ 64
566 #define DESC_PER_CACHELINE (RTE_CACHE_LINE_SIZE / sizeof(struct vring_desc))
567 uint16_t
568 virtio_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
569 {
570         struct virtqueue *rxvq = rx_queue;
571         struct virtio_hw *hw;
572         struct rte_mbuf *rxm, *new_mbuf;
573         uint16_t nb_used, num, nb_rx;
574         uint32_t len[VIRTIO_MBUF_BURST_SZ];
575         struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
576         int error;
577         uint32_t i, nb_enqueued;
578         const uint32_t hdr_size = sizeof(struct virtio_net_hdr);
579
580         nb_used = VIRTQUEUE_NUSED(rxvq);
581
582         virtio_rmb();
583
584         num = (uint16_t)(likely(nb_used <= nb_pkts) ? nb_used : nb_pkts);
585         num = (uint16_t)(likely(num <= VIRTIO_MBUF_BURST_SZ) ? num : VIRTIO_MBUF_BURST_SZ);
586         if (likely(num > DESC_PER_CACHELINE))
587                 num = num - ((rxvq->vq_used_cons_idx + num) % DESC_PER_CACHELINE);
588
589         if (num == 0)
590                 return 0;
591
592         num = virtqueue_dequeue_burst_rx(rxvq, rcv_pkts, len, num);
593         PMD_RX_LOG(DEBUG, "used:%d dequeue:%d", nb_used, num);
594
595         hw = rxvq->hw;
596         nb_rx = 0;
597         nb_enqueued = 0;
598
599         for (i = 0; i < num ; i++) {
600                 rxm = rcv_pkts[i];
601
602                 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
603
604                 if (unlikely(len[i] < hdr_size + ETHER_HDR_LEN)) {
605                         PMD_RX_LOG(ERR, "Packet drop");
606                         nb_enqueued++;
607                         virtio_discard_rxbuf(rxvq, rxm);
608                         rxvq->errors++;
609                         continue;
610                 }
611
612                 rxm->port = rxvq->port_id;
613                 rxm->data_off = RTE_PKTMBUF_HEADROOM;
614
615                 rxm->nb_segs = 1;
616                 rxm->next = NULL;
617                 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
618                 rxm->data_len = (uint16_t)(len[i] - hdr_size);
619
620                 if (hw->vlan_strip)
621                         rte_vlan_strip(rxm);
622
623                 VIRTIO_DUMP_PACKET(rxm, rxm->data_len);
624
625                 rx_pkts[nb_rx++] = rxm;
626
627                 rxvq->bytes += rx_pkts[nb_rx - 1]->pkt_len;
628                 virtio_update_packet_stats(rxvq, rxm);
629         }
630
631         rxvq->packets += nb_rx;
632
633         /* Allocate new mbuf for the used descriptor */
634         error = ENOSPC;
635         while (likely(!virtqueue_full(rxvq))) {
636                 new_mbuf = rte_rxmbuf_alloc(rxvq->mpool);
637                 if (unlikely(new_mbuf == NULL)) {
638                         struct rte_eth_dev *dev
639                                 = &rte_eth_devices[rxvq->port_id];
640                         dev->data->rx_mbuf_alloc_failed++;
641                         break;
642                 }
643                 error = virtqueue_enqueue_recv_refill(rxvq, new_mbuf);
644                 if (unlikely(error)) {
645                         rte_pktmbuf_free(new_mbuf);
646                         break;
647                 }
648                 nb_enqueued++;
649         }
650
651         if (likely(nb_enqueued)) {
652                 vq_update_avail_idx(rxvq);
653
654                 if (unlikely(virtqueue_kick_prepare(rxvq))) {
655                         virtqueue_notify(rxvq);
656                         PMD_RX_LOG(DEBUG, "Notified\n");
657                 }
658         }
659
660         return nb_rx;
661 }
662
663 uint16_t
664 virtio_recv_mergeable_pkts(void *rx_queue,
665                         struct rte_mbuf **rx_pkts,
666                         uint16_t nb_pkts)
667 {
668         struct virtqueue *rxvq = rx_queue;
669         struct virtio_hw *hw;
670         struct rte_mbuf *rxm, *new_mbuf;
671         uint16_t nb_used, num, nb_rx;
672         uint32_t len[VIRTIO_MBUF_BURST_SZ];
673         struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
674         struct rte_mbuf *prev;
675         int error;
676         uint32_t i, nb_enqueued;
677         uint32_t seg_num;
678         uint16_t extra_idx;
679         uint32_t seg_res;
680         const uint32_t hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
681
682         nb_used = VIRTQUEUE_NUSED(rxvq);
683
684         virtio_rmb();
685
686         if (nb_used == 0)
687                 return 0;
688
689         PMD_RX_LOG(DEBUG, "used:%d\n", nb_used);
690
691         hw = rxvq->hw;
692         nb_rx = 0;
693         i = 0;
694         nb_enqueued = 0;
695         seg_num = 0;
696         extra_idx = 0;
697         seg_res = 0;
698
699         while (i < nb_used) {
700                 struct virtio_net_hdr_mrg_rxbuf *header;
701
702                 if (nb_rx == nb_pkts)
703                         break;
704
705                 num = virtqueue_dequeue_burst_rx(rxvq, rcv_pkts, len, 1);
706                 if (num != 1)
707                         continue;
708
709                 i++;
710
711                 PMD_RX_LOG(DEBUG, "dequeue:%d\n", num);
712                 PMD_RX_LOG(DEBUG, "packet len:%d\n", len[0]);
713
714                 rxm = rcv_pkts[0];
715
716                 if (unlikely(len[0] < hdr_size + ETHER_HDR_LEN)) {
717                         PMD_RX_LOG(ERR, "Packet drop\n");
718                         nb_enqueued++;
719                         virtio_discard_rxbuf(rxvq, rxm);
720                         rxvq->errors++;
721                         continue;
722                 }
723
724                 header = (struct virtio_net_hdr_mrg_rxbuf *)((char *)rxm->buf_addr +
725                         RTE_PKTMBUF_HEADROOM - hdr_size);
726                 seg_num = header->num_buffers;
727
728                 if (seg_num == 0)
729                         seg_num = 1;
730
731                 rxm->data_off = RTE_PKTMBUF_HEADROOM;
732                 rxm->nb_segs = seg_num;
733                 rxm->next = NULL;
734                 rxm->pkt_len = (uint32_t)(len[0] - hdr_size);
735                 rxm->data_len = (uint16_t)(len[0] - hdr_size);
736
737                 rxm->port = rxvq->port_id;
738                 rx_pkts[nb_rx] = rxm;
739                 prev = rxm;
740
741                 seg_res = seg_num - 1;
742
743                 while (seg_res != 0) {
744                         /*
745                          * Get extra segments for current uncompleted packet.
746                          */
747                         uint16_t  rcv_cnt =
748                                 RTE_MIN(seg_res, RTE_DIM(rcv_pkts));
749                         if (likely(VIRTQUEUE_NUSED(rxvq) >= rcv_cnt)) {
750                                 uint32_t rx_num =
751                                         virtqueue_dequeue_burst_rx(rxvq,
752                                         rcv_pkts, len, rcv_cnt);
753                                 i += rx_num;
754                                 rcv_cnt = rx_num;
755                         } else {
756                                 PMD_RX_LOG(ERR,
757                                         "No enough segments for packet.\n");
758                                 nb_enqueued++;
759                                 virtio_discard_rxbuf(rxvq, rxm);
760                                 rxvq->errors++;
761                                 break;
762                         }
763
764                         extra_idx = 0;
765
766                         while (extra_idx < rcv_cnt) {
767                                 rxm = rcv_pkts[extra_idx];
768
769                                 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
770                                 rxm->next = NULL;
771                                 rxm->pkt_len = (uint32_t)(len[extra_idx]);
772                                 rxm->data_len = (uint16_t)(len[extra_idx]);
773
774                                 if (prev)
775                                         prev->next = rxm;
776
777                                 prev = rxm;
778                                 rx_pkts[nb_rx]->pkt_len += rxm->pkt_len;
779                                 extra_idx++;
780                         };
781                         seg_res -= rcv_cnt;
782                 }
783
784                 if (hw->vlan_strip)
785                         rte_vlan_strip(rx_pkts[nb_rx]);
786
787                 VIRTIO_DUMP_PACKET(rx_pkts[nb_rx],
788                         rx_pkts[nb_rx]->data_len);
789
790                 rxvq->bytes += rx_pkts[nb_rx]->pkt_len;
791                 virtio_update_packet_stats(rxvq, rx_pkts[nb_rx]);
792                 nb_rx++;
793         }
794
795         rxvq->packets += nb_rx;
796
797         /* Allocate new mbuf for the used descriptor */
798         error = ENOSPC;
799         while (likely(!virtqueue_full(rxvq))) {
800                 new_mbuf = rte_rxmbuf_alloc(rxvq->mpool);
801                 if (unlikely(new_mbuf == NULL)) {
802                         struct rte_eth_dev *dev
803                                 = &rte_eth_devices[rxvq->port_id];
804                         dev->data->rx_mbuf_alloc_failed++;
805                         break;
806                 }
807                 error = virtqueue_enqueue_recv_refill(rxvq, new_mbuf);
808                 if (unlikely(error)) {
809                         rte_pktmbuf_free(new_mbuf);
810                         break;
811                 }
812                 nb_enqueued++;
813         }
814
815         if (likely(nb_enqueued)) {
816                 vq_update_avail_idx(rxvq);
817
818                 if (unlikely(virtqueue_kick_prepare(rxvq))) {
819                         virtqueue_notify(rxvq);
820                         PMD_RX_LOG(DEBUG, "Notified");
821                 }
822         }
823
824         return nb_rx;
825 }
826
827 uint16_t
828 virtio_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
829 {
830         struct virtqueue *txvq = tx_queue;
831         struct rte_mbuf *txm;
832         uint16_t nb_used, nb_tx;
833         int error;
834
835         if (unlikely(nb_pkts < 1))
836                 return nb_pkts;
837
838         PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
839         nb_used = VIRTQUEUE_NUSED(txvq);
840
841         virtio_rmb();
842         if (likely(nb_used > txvq->vq_nentries - txvq->vq_free_thresh))
843                 virtio_xmit_cleanup(txvq, nb_used);
844
845         nb_tx = 0;
846
847         while (nb_tx < nb_pkts) {
848                 /* Need one more descriptor for virtio header. */
849                 int need = tx_pkts[nb_tx]->nb_segs - txvq->vq_free_cnt + 1;
850
851                 /*Positive value indicates it need free vring descriptors */
852                 if (unlikely(need > 0)) {
853                         nb_used = VIRTQUEUE_NUSED(txvq);
854                         virtio_rmb();
855                         need = RTE_MIN(need, (int)nb_used);
856
857                         virtio_xmit_cleanup(txvq, need);
858                         need = (int)tx_pkts[nb_tx]->nb_segs -
859                                 txvq->vq_free_cnt + 1;
860                 }
861
862                 /*
863                  * Zero or negative value indicates it has enough free
864                  * descriptors to use for transmitting.
865                  */
866                 if (likely(need <= 0)) {
867                         txm = tx_pkts[nb_tx];
868
869                         /* Do VLAN tag insertion */
870                         if (unlikely(txm->ol_flags & PKT_TX_VLAN_PKT)) {
871                                 error = rte_vlan_insert(&txm);
872                                 if (unlikely(error)) {
873                                         rte_pktmbuf_free(txm);
874                                         ++nb_tx;
875                                         continue;
876                                 }
877                         }
878
879                         /* Enqueue Packet buffers */
880                         error = virtqueue_enqueue_xmit(txvq, txm);
881                         if (unlikely(error)) {
882                                 if (error == ENOSPC)
883                                         PMD_TX_LOG(ERR, "virtqueue_enqueue Free count = 0");
884                                 else if (error == EMSGSIZE)
885                                         PMD_TX_LOG(ERR, "virtqueue_enqueue Free count < 1");
886                                 else
887                                         PMD_TX_LOG(ERR, "virtqueue_enqueue error: %d", error);
888                                 break;
889                         }
890                         nb_tx++;
891                         txvq->bytes += txm->pkt_len;
892                         virtio_update_packet_stats(txvq, txm);
893                 } else {
894                         PMD_TX_LOG(ERR, "No free tx descriptors to transmit");
895                         break;
896                 }
897         }
898
899         txvq->packets += nb_tx;
900
901         if (likely(nb_tx)) {
902                 vq_update_avail_idx(txvq);
903
904                 if (unlikely(virtqueue_kick_prepare(txvq))) {
905                         virtqueue_notify(txvq);
906                         PMD_TX_LOG(DEBUG, "Notified backend after xmit");
907                 }
908         }
909
910         return nb_tx;
911 }