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