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