net/virtio: select data handler depending on CPU flag
[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 #endif
492         /* Use simple rx/tx func if single segment and no offloads */
493         if (use_simple_rxtx &&
494             (tx_conf->txq_flags & VIRTIO_SIMPLE_FLAGS) == VIRTIO_SIMPLE_FLAGS &&
495             !vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
496                 PMD_INIT_LOG(INFO, "Using simple rx/tx path");
497                 dev->tx_pkt_burst = virtio_xmit_pkts_simple;
498                 dev->rx_pkt_burst = virtio_recv_pkts_vec;
499                 hw->use_simple_rxtx = use_simple_rxtx;
500         }
501 }
502
503 /*
504  * struct rte_eth_dev *dev: Used to update dev
505  * uint16_t nb_desc: Defaults to values read from config space
506  * unsigned int socket_id: Used to allocate memzone
507  * const struct rte_eth_txconf *tx_conf: Used to setup tx engine
508  * uint16_t queue_idx: Just used as an index in dev txq list
509  */
510 int
511 virtio_dev_tx_queue_setup(struct rte_eth_dev *dev,
512                         uint16_t queue_idx,
513                         uint16_t nb_desc,
514                         unsigned int socket_id,
515                         const struct rte_eth_txconf *tx_conf)
516 {
517         uint8_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_TQ_QUEUE_IDX;
518         struct virtnet_tx *txvq;
519         struct virtqueue *vq;
520         uint16_t tx_free_thresh;
521         int ret;
522
523         PMD_INIT_FUNC_TRACE();
524
525         if ((tx_conf->txq_flags & ETH_TXQ_FLAGS_NOXSUMS)
526             != ETH_TXQ_FLAGS_NOXSUMS) {
527                 PMD_INIT_LOG(ERR, "TX checksum offload not supported\n");
528                 return -EINVAL;
529         }
530
531         virtio_update_rxtx_handler(dev, tx_conf);
532
533         ret = virtio_dev_queue_setup(dev, VTNET_TQ, queue_idx, vtpci_queue_idx,
534                         nb_desc, socket_id, (void **)&txvq);
535         if (ret < 0) {
536                 PMD_INIT_LOG(ERR, "tvq initialization failed");
537                 return ret;
538         }
539         vq = txvq->vq;
540
541         tx_free_thresh = tx_conf->tx_free_thresh;
542         if (tx_free_thresh == 0)
543                 tx_free_thresh =
544                         RTE_MIN(vq->vq_nentries / 4, DEFAULT_TX_FREE_THRESH);
545
546         if (tx_free_thresh >= (vq->vq_nentries - 3)) {
547                 RTE_LOG(ERR, PMD, "tx_free_thresh must be less than the "
548                         "number of TX entries minus 3 (%u)."
549                         " (tx_free_thresh=%u port=%u queue=%u)\n",
550                         vq->vq_nentries - 3,
551                         tx_free_thresh, dev->data->port_id, queue_idx);
552                 return -EINVAL;
553         }
554
555         vq->vq_free_thresh = tx_free_thresh;
556
557         dev->data->tx_queues[queue_idx] = txvq;
558         return 0;
559 }
560
561 void
562 virtio_dev_tx_queue_release(void *txq)
563 {
564         struct virtnet_tx *txvq = txq;
565         struct virtqueue *vq;
566         const struct rte_memzone *mz;
567         const struct rte_memzone *hdr_mz;
568
569         if (txvq == NULL)
570                 return;
571
572         /*
573          * txvq is freed when vq is freed, and as mz should be freed after the
574          * del_queue, so we reserve the mz pointer first.
575          */
576         vq = txvq->vq;
577         mz = txvq->mz;
578         hdr_mz = txvq->virtio_net_hdr_mz;
579
580         virtio_dev_queue_release(vq);
581         rte_memzone_free(mz);
582         rte_memzone_free(hdr_mz);
583 }
584
585 static void
586 virtio_discard_rxbuf(struct virtqueue *vq, struct rte_mbuf *m)
587 {
588         int error;
589         /*
590          * Requeue the discarded mbuf. This should always be
591          * successful since it was just dequeued.
592          */
593         error = virtqueue_enqueue_recv_refill(vq, m);
594         if (unlikely(error)) {
595                 RTE_LOG(ERR, PMD, "cannot requeue discarded mbuf");
596                 rte_pktmbuf_free(m);
597         }
598 }
599
600 static void
601 virtio_update_packet_stats(struct virtnet_stats *stats, struct rte_mbuf *mbuf)
602 {
603         uint32_t s = mbuf->pkt_len;
604         struct ether_addr *ea;
605
606         if (s == 64) {
607                 stats->size_bins[1]++;
608         } else if (s > 64 && s < 1024) {
609                 uint32_t bin;
610
611                 /* count zeros, and offset into correct bin */
612                 bin = (sizeof(s) * 8) - __builtin_clz(s) - 5;
613                 stats->size_bins[bin]++;
614         } else {
615                 if (s < 64)
616                         stats->size_bins[0]++;
617                 else if (s < 1519)
618                         stats->size_bins[6]++;
619                 else if (s >= 1519)
620                         stats->size_bins[7]++;
621         }
622
623         ea = rte_pktmbuf_mtod(mbuf, struct ether_addr *);
624         if (is_multicast_ether_addr(ea)) {
625                 if (is_broadcast_ether_addr(ea))
626                         stats->broadcast++;
627                 else
628                         stats->multicast++;
629         }
630 }
631
632 #define VIRTIO_MBUF_BURST_SZ 64
633 #define DESC_PER_CACHELINE (RTE_CACHE_LINE_SIZE / sizeof(struct vring_desc))
634 uint16_t
635 virtio_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
636 {
637         struct virtnet_rx *rxvq = rx_queue;
638         struct virtqueue *vq = rxvq->vq;
639         struct virtio_hw *hw;
640         struct rte_mbuf *rxm, *new_mbuf;
641         uint16_t nb_used, num, nb_rx;
642         uint32_t len[VIRTIO_MBUF_BURST_SZ];
643         struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
644         int error;
645         uint32_t i, nb_enqueued;
646         uint32_t hdr_size;
647
648         nb_used = VIRTQUEUE_NUSED(vq);
649
650         virtio_rmb();
651
652         num = (uint16_t)(likely(nb_used <= nb_pkts) ? nb_used : nb_pkts);
653         num = (uint16_t)(likely(num <= VIRTIO_MBUF_BURST_SZ) ? num : VIRTIO_MBUF_BURST_SZ);
654         if (likely(num > DESC_PER_CACHELINE))
655                 num = num - ((vq->vq_used_cons_idx + num) % DESC_PER_CACHELINE);
656
657         num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len, num);
658         PMD_RX_LOG(DEBUG, "used:%d dequeue:%d", nb_used, num);
659
660         hw = vq->hw;
661         nb_rx = 0;
662         nb_enqueued = 0;
663         hdr_size = hw->vtnet_hdr_size;
664
665         for (i = 0; i < num ; i++) {
666                 rxm = rcv_pkts[i];
667
668                 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
669
670                 if (unlikely(len[i] < hdr_size + ETHER_HDR_LEN)) {
671                         PMD_RX_LOG(ERR, "Packet drop");
672                         nb_enqueued++;
673                         virtio_discard_rxbuf(vq, rxm);
674                         rxvq->stats.errors++;
675                         continue;
676                 }
677
678                 rxm->port = rxvq->port_id;
679                 rxm->data_off = RTE_PKTMBUF_HEADROOM;
680                 rxm->ol_flags = 0;
681                 rxm->vlan_tci = 0;
682
683                 rxm->nb_segs = 1;
684                 rxm->next = NULL;
685                 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
686                 rxm->data_len = (uint16_t)(len[i] - hdr_size);
687
688                 if (hw->vlan_strip)
689                         rte_vlan_strip(rxm);
690
691                 VIRTIO_DUMP_PACKET(rxm, rxm->data_len);
692
693                 rx_pkts[nb_rx++] = rxm;
694
695                 rxvq->stats.bytes += rx_pkts[nb_rx - 1]->pkt_len;
696                 virtio_update_packet_stats(&rxvq->stats, rxm);
697         }
698
699         rxvq->stats.packets += nb_rx;
700
701         /* Allocate new mbuf for the used descriptor */
702         error = ENOSPC;
703         while (likely(!virtqueue_full(vq))) {
704                 new_mbuf = rte_mbuf_raw_alloc(rxvq->mpool);
705                 if (unlikely(new_mbuf == NULL)) {
706                         struct rte_eth_dev *dev
707                                 = &rte_eth_devices[rxvq->port_id];
708                         dev->data->rx_mbuf_alloc_failed++;
709                         break;
710                 }
711                 error = virtqueue_enqueue_recv_refill(vq, new_mbuf);
712                 if (unlikely(error)) {
713                         rte_pktmbuf_free(new_mbuf);
714                         break;
715                 }
716                 nb_enqueued++;
717         }
718
719         if (likely(nb_enqueued)) {
720                 vq_update_avail_idx(vq);
721
722                 if (unlikely(virtqueue_kick_prepare(vq))) {
723                         virtqueue_notify(vq);
724                         PMD_RX_LOG(DEBUG, "Notified");
725                 }
726         }
727
728         return nb_rx;
729 }
730
731 uint16_t
732 virtio_recv_mergeable_pkts(void *rx_queue,
733                         struct rte_mbuf **rx_pkts,
734                         uint16_t nb_pkts)
735 {
736         struct virtnet_rx *rxvq = rx_queue;
737         struct virtqueue *vq = rxvq->vq;
738         struct virtio_hw *hw;
739         struct rte_mbuf *rxm, *new_mbuf;
740         uint16_t nb_used, num, nb_rx;
741         uint32_t len[VIRTIO_MBUF_BURST_SZ];
742         struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
743         struct rte_mbuf *prev;
744         int error;
745         uint32_t i, nb_enqueued;
746         uint32_t seg_num;
747         uint16_t extra_idx;
748         uint32_t seg_res;
749         uint32_t hdr_size;
750
751         nb_used = VIRTQUEUE_NUSED(vq);
752
753         virtio_rmb();
754
755         PMD_RX_LOG(DEBUG, "used:%d", nb_used);
756
757         hw = vq->hw;
758         nb_rx = 0;
759         i = 0;
760         nb_enqueued = 0;
761         seg_num = 0;
762         extra_idx = 0;
763         seg_res = 0;
764         hdr_size = hw->vtnet_hdr_size;
765
766         while (i < nb_used) {
767                 struct virtio_net_hdr_mrg_rxbuf *header;
768
769                 if (nb_rx == nb_pkts)
770                         break;
771
772                 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len, 1);
773                 if (num != 1)
774                         continue;
775
776                 i++;
777
778                 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
779                 PMD_RX_LOG(DEBUG, "packet len:%d", len[0]);
780
781                 rxm = rcv_pkts[0];
782
783                 if (unlikely(len[0] < hdr_size + ETHER_HDR_LEN)) {
784                         PMD_RX_LOG(ERR, "Packet drop");
785                         nb_enqueued++;
786                         virtio_discard_rxbuf(vq, rxm);
787                         rxvq->stats.errors++;
788                         continue;
789                 }
790
791                 header = (struct virtio_net_hdr_mrg_rxbuf *)((char *)rxm->buf_addr +
792                         RTE_PKTMBUF_HEADROOM - hdr_size);
793                 seg_num = header->num_buffers;
794
795                 if (seg_num == 0)
796                         seg_num = 1;
797
798                 rxm->data_off = RTE_PKTMBUF_HEADROOM;
799                 rxm->nb_segs = seg_num;
800                 rxm->next = NULL;
801                 rxm->ol_flags = 0;
802                 rxm->vlan_tci = 0;
803                 rxm->pkt_len = (uint32_t)(len[0] - hdr_size);
804                 rxm->data_len = (uint16_t)(len[0] - hdr_size);
805
806                 rxm->port = rxvq->port_id;
807                 rx_pkts[nb_rx] = rxm;
808                 prev = rxm;
809
810                 seg_res = seg_num - 1;
811
812                 while (seg_res != 0) {
813                         /*
814                          * Get extra segments for current uncompleted packet.
815                          */
816                         uint16_t  rcv_cnt =
817                                 RTE_MIN(seg_res, RTE_DIM(rcv_pkts));
818                         if (likely(VIRTQUEUE_NUSED(vq) >= rcv_cnt)) {
819                                 uint32_t rx_num =
820                                         virtqueue_dequeue_burst_rx(vq,
821                                         rcv_pkts, len, rcv_cnt);
822                                 i += rx_num;
823                                 rcv_cnt = rx_num;
824                         } else {
825                                 PMD_RX_LOG(ERR,
826                                            "No enough segments for packet.");
827                                 nb_enqueued++;
828                                 virtio_discard_rxbuf(vq, rxm);
829                                 rxvq->stats.errors++;
830                                 break;
831                         }
832
833                         extra_idx = 0;
834
835                         while (extra_idx < rcv_cnt) {
836                                 rxm = rcv_pkts[extra_idx];
837
838                                 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
839                                 rxm->next = NULL;
840                                 rxm->pkt_len = (uint32_t)(len[extra_idx]);
841                                 rxm->data_len = (uint16_t)(len[extra_idx]);
842
843                                 if (prev)
844                                         prev->next = rxm;
845
846                                 prev = rxm;
847                                 rx_pkts[nb_rx]->pkt_len += rxm->pkt_len;
848                                 extra_idx++;
849                         };
850                         seg_res -= rcv_cnt;
851                 }
852
853                 if (hw->vlan_strip)
854                         rte_vlan_strip(rx_pkts[nb_rx]);
855
856                 VIRTIO_DUMP_PACKET(rx_pkts[nb_rx],
857                         rx_pkts[nb_rx]->data_len);
858
859                 rxvq->stats.bytes += rx_pkts[nb_rx]->pkt_len;
860                 virtio_update_packet_stats(&rxvq->stats, rx_pkts[nb_rx]);
861                 nb_rx++;
862         }
863
864         rxvq->stats.packets += nb_rx;
865
866         /* Allocate new mbuf for the used descriptor */
867         error = ENOSPC;
868         while (likely(!virtqueue_full(vq))) {
869                 new_mbuf = rte_mbuf_raw_alloc(rxvq->mpool);
870                 if (unlikely(new_mbuf == NULL)) {
871                         struct rte_eth_dev *dev
872                                 = &rte_eth_devices[rxvq->port_id];
873                         dev->data->rx_mbuf_alloc_failed++;
874                         break;
875                 }
876                 error = virtqueue_enqueue_recv_refill(vq, new_mbuf);
877                 if (unlikely(error)) {
878                         rte_pktmbuf_free(new_mbuf);
879                         break;
880                 }
881                 nb_enqueued++;
882         }
883
884         if (likely(nb_enqueued)) {
885                 vq_update_avail_idx(vq);
886
887                 if (unlikely(virtqueue_kick_prepare(vq))) {
888                         virtqueue_notify(vq);
889                         PMD_RX_LOG(DEBUG, "Notified");
890                 }
891         }
892
893         return nb_rx;
894 }
895
896 uint16_t
897 virtio_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
898 {
899         struct virtnet_tx *txvq = tx_queue;
900         struct virtqueue *vq = txvq->vq;
901         struct virtio_hw *hw = vq->hw;
902         uint16_t hdr_size = hw->vtnet_hdr_size;
903         uint16_t nb_used, nb_tx;
904         int error;
905
906         if (unlikely(nb_pkts < 1))
907                 return nb_pkts;
908
909         PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
910         nb_used = VIRTQUEUE_NUSED(vq);
911
912         virtio_rmb();
913         if (likely(nb_used > vq->vq_nentries - vq->vq_free_thresh))
914                 virtio_xmit_cleanup(vq, nb_used);
915
916         for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
917                 struct rte_mbuf *txm = tx_pkts[nb_tx];
918                 int can_push = 0, use_indirect = 0, slots, need;
919
920                 /* Do VLAN tag insertion */
921                 if (unlikely(txm->ol_flags & PKT_TX_VLAN_PKT)) {
922                         error = rte_vlan_insert(&txm);
923                         if (unlikely(error)) {
924                                 rte_pktmbuf_free(txm);
925                                 continue;
926                         }
927                 }
928
929                 /* optimize ring usage */
930                 if (vtpci_with_feature(hw, VIRTIO_F_ANY_LAYOUT) &&
931                     rte_mbuf_refcnt_read(txm) == 1 &&
932                     RTE_MBUF_DIRECT(txm) &&
933                     txm->nb_segs == 1 &&
934                     rte_pktmbuf_headroom(txm) >= hdr_size &&
935                     rte_is_aligned(rte_pktmbuf_mtod(txm, char *),
936                                    __alignof__(struct virtio_net_hdr_mrg_rxbuf)))
937                         can_push = 1;
938                 else if (vtpci_with_feature(hw, VIRTIO_RING_F_INDIRECT_DESC) &&
939                          txm->nb_segs < VIRTIO_MAX_TX_INDIRECT)
940                         use_indirect = 1;
941
942                 /* How many main ring entries are needed to this Tx?
943                  * any_layout => number of segments
944                  * indirect   => 1
945                  * default    => number of segments + 1
946                  */
947                 slots = use_indirect ? 1 : (txm->nb_segs + !can_push);
948                 need = slots - vq->vq_free_cnt;
949
950                 /* Positive value indicates it need free vring descriptors */
951                 if (unlikely(need > 0)) {
952                         nb_used = VIRTQUEUE_NUSED(vq);
953                         virtio_rmb();
954                         need = RTE_MIN(need, (int)nb_used);
955
956                         virtio_xmit_cleanup(vq, need);
957                         need = slots - vq->vq_free_cnt;
958                         if (unlikely(need > 0)) {
959                                 PMD_TX_LOG(ERR,
960                                            "No free tx descriptors to transmit");
961                                 break;
962                         }
963                 }
964
965                 /* Enqueue Packet buffers */
966                 virtqueue_enqueue_xmit(txvq, txm, slots, use_indirect, can_push);
967
968                 txvq->stats.bytes += txm->pkt_len;
969                 virtio_update_packet_stats(&txvq->stats, txm);
970         }
971
972         txvq->stats.packets += nb_tx;
973
974         if (likely(nb_tx)) {
975                 vq_update_avail_idx(vq);
976
977                 if (unlikely(virtqueue_kick_prepare(vq))) {
978                         virtqueue_notify(vq);
979                         PMD_TX_LOG(DEBUG, "Notified backend after xmit");
980                 }
981         }
982
983         return nb_tx;
984 }