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