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