New upstream version 16.11.4
[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 #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 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 /* When doing TSO, the IP length is not included in the pseudo header
213  * checksum of the packet given to the PMD, but for virtio it is
214  * expected.
215  */
216 static void
217 virtio_tso_fix_cksum(struct rte_mbuf *m)
218 {
219         /* common case: header is not fragmented */
220         if (likely(rte_pktmbuf_data_len(m) >= m->l2_len + m->l3_len +
221                         m->l4_len)) {
222                 struct ipv4_hdr *iph;
223                 struct ipv6_hdr *ip6h;
224                 struct tcp_hdr *th;
225                 uint16_t prev_cksum, new_cksum, ip_len, ip_paylen;
226                 uint32_t tmp;
227
228                 iph = rte_pktmbuf_mtod_offset(m, struct ipv4_hdr *, m->l2_len);
229                 th = RTE_PTR_ADD(iph, m->l3_len);
230                 if ((iph->version_ihl >> 4) == 4) {
231                         iph->hdr_checksum = 0;
232                         iph->hdr_checksum = rte_ipv4_cksum(iph);
233                         ip_len = iph->total_length;
234                         ip_paylen = rte_cpu_to_be_16(rte_be_to_cpu_16(ip_len) -
235                                 m->l3_len);
236                 } else {
237                         ip6h = (struct ipv6_hdr *)iph;
238                         ip_paylen = ip6h->payload_len;
239                 }
240
241                 /* calculate the new phdr checksum not including ip_paylen */
242                 prev_cksum = th->cksum;
243                 tmp = prev_cksum;
244                 tmp += ip_paylen;
245                 tmp = (tmp & 0xffff) + (tmp >> 16);
246                 new_cksum = tmp;
247
248                 /* replace it in the packet */
249                 th->cksum = new_cksum;
250         }
251 }
252
253 static inline int
254 tx_offload_enabled(struct virtio_hw *hw)
255 {
256         return vtpci_with_feature(hw, VIRTIO_NET_F_CSUM) ||
257                 vtpci_with_feature(hw, VIRTIO_NET_F_HOST_TSO4) ||
258                 vtpci_with_feature(hw, VIRTIO_NET_F_HOST_TSO6);
259 }
260
261 /* avoid write operation when necessary, to lessen cache issues */
262 #define ASSIGN_UNLESS_EQUAL(var, val) do {      \
263         if ((var) != (val))                     \
264                 (var) = (val);                  \
265 } while (0)
266
267 static inline void
268 virtqueue_enqueue_xmit(struct virtnet_tx *txvq, struct rte_mbuf *cookie,
269                        uint16_t needed, int use_indirect, int can_push)
270 {
271         struct virtio_tx_region *txr = txvq->virtio_net_hdr_mz->addr;
272         struct vq_desc_extra *dxp;
273         struct virtqueue *vq = txvq->vq;
274         struct vring_desc *start_dp;
275         uint16_t seg_num = cookie->nb_segs;
276         uint16_t head_idx, idx;
277         uint16_t head_size = vq->hw->vtnet_hdr_size;
278         struct virtio_net_hdr *hdr;
279         int offload;
280
281         offload = tx_offload_enabled(vq->hw);
282         head_idx = vq->vq_desc_head_idx;
283         idx = head_idx;
284         dxp = &vq->vq_descx[idx];
285         dxp->cookie = (void *)cookie;
286         dxp->ndescs = needed;
287
288         start_dp = vq->vq_ring.desc;
289
290         if (can_push) {
291                 /* prepend cannot fail, checked by caller */
292                 hdr = (struct virtio_net_hdr *)
293                         rte_pktmbuf_prepend(cookie, head_size);
294                 /* rte_pktmbuf_prepend() counts the hdr size to the pkt length,
295                  * which is wrong. Below subtract restores correct pkt size.
296                  */
297                 cookie->pkt_len -= head_size;
298                 /* if offload disabled, it is not zeroed below, do it now */
299                 if (offload == 0) {
300                         ASSIGN_UNLESS_EQUAL(hdr->csum_start, 0);
301                         ASSIGN_UNLESS_EQUAL(hdr->csum_offset, 0);
302                         ASSIGN_UNLESS_EQUAL(hdr->flags, 0);
303                         ASSIGN_UNLESS_EQUAL(hdr->gso_type, 0);
304                         ASSIGN_UNLESS_EQUAL(hdr->gso_size, 0);
305                         ASSIGN_UNLESS_EQUAL(hdr->hdr_len, 0);
306                 }
307         } else if (use_indirect) {
308                 /* setup tx ring slot to point to indirect
309                  * descriptor list stored in reserved region.
310                  *
311                  * the first slot in indirect ring is already preset
312                  * to point to the header in reserved region
313                  */
314                 start_dp[idx].addr  = txvq->virtio_net_hdr_mem +
315                         RTE_PTR_DIFF(&txr[idx].tx_indir, txr);
316                 start_dp[idx].len   = (seg_num + 1) * sizeof(struct vring_desc);
317                 start_dp[idx].flags = VRING_DESC_F_INDIRECT;
318                 hdr = (struct virtio_net_hdr *)&txr[idx].tx_hdr;
319
320                 /* loop below will fill in rest of the indirect elements */
321                 start_dp = txr[idx].tx_indir;
322                 idx = 1;
323         } else {
324                 /* setup first tx ring slot to point to header
325                  * stored in reserved region.
326                  */
327                 start_dp[idx].addr  = txvq->virtio_net_hdr_mem +
328                         RTE_PTR_DIFF(&txr[idx].tx_hdr, txr);
329                 start_dp[idx].len   = vq->hw->vtnet_hdr_size;
330                 start_dp[idx].flags = VRING_DESC_F_NEXT;
331                 hdr = (struct virtio_net_hdr *)&txr[idx].tx_hdr;
332
333                 idx = start_dp[idx].next;
334         }
335
336         /* Checksum Offload / TSO */
337         if (offload) {
338                 if (cookie->ol_flags & PKT_TX_TCP_SEG)
339                         cookie->ol_flags |= PKT_TX_TCP_CKSUM;
340
341                 switch (cookie->ol_flags & PKT_TX_L4_MASK) {
342                 case PKT_TX_UDP_CKSUM:
343                         hdr->csum_start = cookie->l2_len + cookie->l3_len;
344                         hdr->csum_offset = offsetof(struct udp_hdr,
345                                 dgram_cksum);
346                         hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
347                         break;
348
349                 case PKT_TX_TCP_CKSUM:
350                         hdr->csum_start = cookie->l2_len + cookie->l3_len;
351                         hdr->csum_offset = offsetof(struct tcp_hdr, cksum);
352                         hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
353                         break;
354
355                 default:
356                         ASSIGN_UNLESS_EQUAL(hdr->csum_start, 0);
357                         ASSIGN_UNLESS_EQUAL(hdr->csum_offset, 0);
358                         ASSIGN_UNLESS_EQUAL(hdr->flags, 0);
359                         break;
360                 }
361
362                 /* TCP Segmentation Offload */
363                 if (cookie->ol_flags & PKT_TX_TCP_SEG) {
364                         virtio_tso_fix_cksum(cookie);
365                         hdr->gso_type = (cookie->ol_flags & PKT_TX_IPV6) ?
366                                 VIRTIO_NET_HDR_GSO_TCPV6 :
367                                 VIRTIO_NET_HDR_GSO_TCPV4;
368                         hdr->gso_size = cookie->tso_segsz;
369                         hdr->hdr_len =
370                                 cookie->l2_len +
371                                 cookie->l3_len +
372                                 cookie->l4_len;
373                 } else {
374                         ASSIGN_UNLESS_EQUAL(hdr->gso_type, 0);
375                         ASSIGN_UNLESS_EQUAL(hdr->gso_size, 0);
376                         ASSIGN_UNLESS_EQUAL(hdr->hdr_len, 0);
377                 }
378         }
379
380         do {
381                 start_dp[idx].addr  = VIRTIO_MBUF_DATA_DMA_ADDR(cookie, vq);
382                 start_dp[idx].len   = cookie->data_len;
383                 start_dp[idx].flags = cookie->next ? VRING_DESC_F_NEXT : 0;
384                 idx = start_dp[idx].next;
385         } while ((cookie = cookie->next) != NULL);
386
387         if (use_indirect)
388                 idx = vq->vq_ring.desc[head_idx].next;
389
390         vq->vq_desc_head_idx = idx;
391         if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
392                 vq->vq_desc_tail_idx = idx;
393         vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - needed);
394         vq_update_avail_ring(vq, head_idx);
395 }
396
397 void
398 virtio_dev_cq_start(struct rte_eth_dev *dev)
399 {
400         struct virtio_hw *hw = dev->data->dev_private;
401
402         if (hw->cvq && hw->cvq->vq) {
403                 VIRTQUEUE_DUMP((struct virtqueue *)hw->cvq->vq);
404         }
405 }
406
407 int
408 virtio_dev_rx_queue_setup(struct rte_eth_dev *dev,
409                         uint16_t queue_idx,
410                         uint16_t nb_desc,
411                         unsigned int socket_id __rte_unused,
412                         __rte_unused const struct rte_eth_rxconf *rx_conf,
413                         struct rte_mempool *mp)
414 {
415         uint16_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_RQ_QUEUE_IDX;
416         struct virtio_hw *hw = dev->data->dev_private;
417         struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
418         struct virtnet_rx *rxvq;
419
420         PMD_INIT_FUNC_TRACE();
421
422         if (nb_desc == 0 || nb_desc > vq->vq_nentries)
423                 nb_desc = vq->vq_nentries;
424         vq->vq_free_cnt = RTE_MIN(vq->vq_free_cnt, nb_desc);
425
426         rxvq = &vq->rxq;
427         rxvq->queue_id = queue_idx;
428         rxvq->mpool = mp;
429         if (rxvq->mpool == NULL) {
430                 rte_exit(EXIT_FAILURE,
431                         "Cannot allocate mbufs for rx virtqueue");
432         }
433         dev->data->rx_queues[queue_idx] = rxvq;
434
435         return 0;
436 }
437
438 int
439 virtio_dev_rx_queue_setup_finish(struct rte_eth_dev *dev, uint16_t queue_idx)
440 {
441         uint16_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_RQ_QUEUE_IDX;
442         struct virtio_hw *hw = dev->data->dev_private;
443         struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
444         struct virtnet_rx *rxvq = &vq->rxq;
445         struct rte_mbuf *m;
446         uint16_t desc_idx;
447         int error, nbufs;
448
449         PMD_INIT_FUNC_TRACE();
450
451         /* Allocate blank mbufs for the each rx descriptor */
452         nbufs = 0;
453
454         if (hw->use_simple_rxtx) {
455                 for (desc_idx = 0; desc_idx < vq->vq_nentries;
456                      desc_idx++) {
457                         vq->vq_ring.avail->ring[desc_idx] = desc_idx;
458                         vq->vq_ring.desc[desc_idx].flags =
459                                 VRING_DESC_F_WRITE;
460                 }
461         }
462
463         memset(&rxvq->fake_mbuf, 0, sizeof(rxvq->fake_mbuf));
464         for (desc_idx = 0; desc_idx < RTE_PMD_VIRTIO_RX_MAX_BURST;
465              desc_idx++) {
466                 vq->sw_ring[vq->vq_nentries + desc_idx] =
467                         &rxvq->fake_mbuf;
468         }
469
470         while (!virtqueue_full(vq)) {
471                 m = rte_mbuf_raw_alloc(rxvq->mpool);
472                 if (m == NULL)
473                         break;
474
475                 /* Enqueue allocated buffers */
476                 if (hw->use_simple_rxtx)
477                         error = virtqueue_enqueue_recv_refill_simple(vq, m);
478                 else
479                         error = virtqueue_enqueue_recv_refill(vq, m);
480
481                 if (error) {
482                         rte_pktmbuf_free(m);
483                         break;
484                 }
485                 nbufs++;
486         }
487
488         vq_update_avail_idx(vq);
489
490         PMD_INIT_LOG(DEBUG, "Allocated %d bufs", nbufs);
491
492         virtio_rxq_vec_setup(rxvq);
493
494         VIRTQUEUE_DUMP(vq);
495
496         return 0;
497 }
498
499 static void
500 virtio_update_rxtx_handler(struct rte_eth_dev *dev,
501                            const struct rte_eth_txconf *tx_conf)
502 {
503         uint8_t use_simple_rxtx = 0;
504         struct virtio_hw *hw = dev->data->dev_private;
505
506 #if defined RTE_ARCH_X86
507         if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE3))
508                 use_simple_rxtx = 1;
509 #elif defined RTE_ARCH_ARM64 || defined CONFIG_RTE_ARCH_ARM
510         if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_NEON))
511                 use_simple_rxtx = 1;
512 #endif
513         /* Use simple rx/tx func if single segment and no offloads */
514         if (use_simple_rxtx &&
515             (tx_conf->txq_flags & VIRTIO_SIMPLE_FLAGS) == VIRTIO_SIMPLE_FLAGS &&
516             !vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
517                 PMD_INIT_LOG(INFO, "Using simple rx/tx path");
518                 dev->tx_pkt_burst = virtio_xmit_pkts_simple;
519                 dev->rx_pkt_burst = virtio_recv_pkts_vec;
520                 hw->use_simple_rxtx = use_simple_rxtx;
521         }
522 }
523
524 /*
525  * struct rte_eth_dev *dev: Used to update dev
526  * uint16_t nb_desc: Defaults to values read from config space
527  * unsigned int socket_id: Used to allocate memzone
528  * const struct rte_eth_txconf *tx_conf: Used to setup tx engine
529  * uint16_t queue_idx: Just used as an index in dev txq list
530  */
531 int
532 virtio_dev_tx_queue_setup(struct rte_eth_dev *dev,
533                         uint16_t queue_idx,
534                         uint16_t nb_desc,
535                         unsigned int socket_id __rte_unused,
536                         const struct rte_eth_txconf *tx_conf)
537 {
538         uint8_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_TQ_QUEUE_IDX;
539         struct virtio_hw *hw = dev->data->dev_private;
540         struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
541         struct virtnet_tx *txvq;
542         uint16_t tx_free_thresh;
543
544         PMD_INIT_FUNC_TRACE();
545
546         virtio_update_rxtx_handler(dev, tx_conf);
547
548         if (nb_desc == 0 || nb_desc > vq->vq_nentries)
549                 nb_desc = vq->vq_nentries;
550         vq->vq_free_cnt = RTE_MIN(vq->vq_free_cnt, nb_desc);
551
552         txvq = &vq->txq;
553         txvq->queue_id = queue_idx;
554
555         tx_free_thresh = tx_conf->tx_free_thresh;
556         if (tx_free_thresh == 0)
557                 tx_free_thresh =
558                         RTE_MIN(vq->vq_nentries / 4, DEFAULT_TX_FREE_THRESH);
559
560         if (tx_free_thresh >= (vq->vq_nentries - 3)) {
561                 RTE_LOG(ERR, PMD, "tx_free_thresh must be less than the "
562                         "number of TX entries minus 3 (%u)."
563                         " (tx_free_thresh=%u port=%u queue=%u)\n",
564                         vq->vq_nentries - 3,
565                         tx_free_thresh, dev->data->port_id, queue_idx);
566                 return -EINVAL;
567         }
568
569         vq->vq_free_thresh = tx_free_thresh;
570
571         dev->data->tx_queues[queue_idx] = txvq;
572         return 0;
573 }
574
575 int
576 virtio_dev_tx_queue_setup_finish(struct rte_eth_dev *dev,
577                                 uint16_t queue_idx)
578 {
579         uint8_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_TQ_QUEUE_IDX;
580         struct virtio_hw *hw = dev->data->dev_private;
581         struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
582         uint16_t mid_idx = vq->vq_nentries >> 1;
583         struct virtnet_tx *txvq = &vq->txq;
584         uint16_t desc_idx;
585
586         PMD_INIT_FUNC_TRACE();
587
588         if (hw->use_simple_rxtx) {
589                 for (desc_idx = 0; desc_idx < mid_idx; desc_idx++) {
590                         vq->vq_ring.avail->ring[desc_idx] =
591                                 desc_idx + mid_idx;
592                         vq->vq_ring.desc[desc_idx + mid_idx].next =
593                                 desc_idx;
594                         vq->vq_ring.desc[desc_idx + mid_idx].addr =
595                                 txvq->virtio_net_hdr_mem +
596                                 offsetof(struct virtio_tx_region, tx_hdr);
597                         vq->vq_ring.desc[desc_idx + mid_idx].len =
598                                 vq->hw->vtnet_hdr_size;
599                         vq->vq_ring.desc[desc_idx + mid_idx].flags =
600                                 VRING_DESC_F_NEXT;
601                         vq->vq_ring.desc[desc_idx].flags = 0;
602                 }
603                 for (desc_idx = mid_idx; desc_idx < vq->vq_nentries;
604                      desc_idx++)
605                         vq->vq_ring.avail->ring[desc_idx] = desc_idx;
606         }
607
608         VIRTQUEUE_DUMP(vq);
609
610         return 0;
611 }
612
613 static void
614 virtio_discard_rxbuf(struct virtqueue *vq, struct rte_mbuf *m)
615 {
616         int error;
617         /*
618          * Requeue the discarded mbuf. This should always be
619          * successful since it was just dequeued.
620          */
621         error = virtqueue_enqueue_recv_refill(vq, m);
622         if (unlikely(error)) {
623                 RTE_LOG(ERR, PMD, "cannot requeue discarded mbuf");
624                 rte_pktmbuf_free(m);
625         }
626 }
627
628 static void
629 virtio_update_packet_stats(struct virtnet_stats *stats, struct rte_mbuf *mbuf)
630 {
631         uint32_t s = mbuf->pkt_len;
632         struct ether_addr *ea;
633
634         if (s == 64) {
635                 stats->size_bins[1]++;
636         } else if (s > 64 && s < 1024) {
637                 uint32_t bin;
638
639                 /* count zeros, and offset into correct bin */
640                 bin = (sizeof(s) * 8) - __builtin_clz(s) - 5;
641                 stats->size_bins[bin]++;
642         } else {
643                 if (s < 64)
644                         stats->size_bins[0]++;
645                 else if (s < 1519)
646                         stats->size_bins[6]++;
647                 else if (s >= 1519)
648                         stats->size_bins[7]++;
649         }
650
651         ea = rte_pktmbuf_mtod(mbuf, struct ether_addr *);
652         if (is_multicast_ether_addr(ea)) {
653                 if (is_broadcast_ether_addr(ea))
654                         stats->broadcast++;
655                 else
656                         stats->multicast++;
657         }
658 }
659
660 /* Optionally fill offload information in structure */
661 static int
662 virtio_rx_offload(struct rte_mbuf *m, struct virtio_net_hdr *hdr)
663 {
664         struct rte_net_hdr_lens hdr_lens;
665         uint32_t hdrlen, ptype;
666         int l4_supported = 0;
667
668         /* nothing to do */
669         if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
670                 return 0;
671
672         m->ol_flags |= PKT_RX_IP_CKSUM_UNKNOWN;
673
674         ptype = rte_net_get_ptype(m, &hdr_lens, RTE_PTYPE_ALL_MASK);
675         m->packet_type = ptype;
676         if ((ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_TCP ||
677             (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_UDP ||
678             (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_SCTP)
679                 l4_supported = 1;
680
681         if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
682                 hdrlen = hdr_lens.l2_len + hdr_lens.l3_len + hdr_lens.l4_len;
683                 if (hdr->csum_start <= hdrlen && l4_supported) {
684                         m->ol_flags |= PKT_RX_L4_CKSUM_NONE;
685                 } else {
686                         /* Unknown proto or tunnel, do sw cksum. We can assume
687                          * the cksum field is in the first segment since the
688                          * buffers we provided to the host are large enough.
689                          * In case of SCTP, this will be wrong since it's a CRC
690                          * but there's nothing we can do.
691                          */
692                         uint16_t csum = 0, off;
693
694                         rte_raw_cksum_mbuf(m, hdr->csum_start,
695                                 rte_pktmbuf_pkt_len(m) - hdr->csum_start,
696                                 &csum);
697                         if (likely(csum != 0xffff))
698                                 csum = ~csum;
699                         off = hdr->csum_offset + hdr->csum_start;
700                         if (rte_pktmbuf_data_len(m) >= off + 1)
701                                 *rte_pktmbuf_mtod_offset(m, uint16_t *,
702                                         off) = csum;
703                 }
704         } else if (hdr->flags & VIRTIO_NET_HDR_F_DATA_VALID && l4_supported) {
705                 m->ol_flags |= PKT_RX_L4_CKSUM_GOOD;
706         }
707
708         /* GSO request, save required information in mbuf */
709         if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
710                 /* Check unsupported modes */
711                 if ((hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN) ||
712                     (hdr->gso_size == 0)) {
713                         return -EINVAL;
714                 }
715
716                 /* Update mss lengthes in mbuf */
717                 m->tso_segsz = hdr->gso_size;
718                 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
719                         case VIRTIO_NET_HDR_GSO_TCPV4:
720                         case VIRTIO_NET_HDR_GSO_TCPV6:
721                                 m->ol_flags |= PKT_RX_LRO | \
722                                         PKT_RX_L4_CKSUM_NONE;
723                                 break;
724                         default:
725                                 return -EINVAL;
726                 }
727         }
728
729         return 0;
730 }
731
732 static inline int
733 rx_offload_enabled(struct virtio_hw *hw)
734 {
735         return vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM) ||
736                 vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) ||
737                 vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO6);
738 }
739
740 #define VIRTIO_MBUF_BURST_SZ 64
741 #define DESC_PER_CACHELINE (RTE_CACHE_LINE_SIZE / sizeof(struct vring_desc))
742 uint16_t
743 virtio_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
744 {
745         struct virtnet_rx *rxvq = rx_queue;
746         struct virtqueue *vq = rxvq->vq;
747         struct virtio_hw *hw;
748         struct rte_mbuf *rxm, *new_mbuf;
749         uint16_t nb_used, num, nb_rx;
750         uint32_t len[VIRTIO_MBUF_BURST_SZ];
751         struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
752         int error;
753         uint32_t i, nb_enqueued;
754         uint32_t hdr_size;
755         int offload;
756         struct virtio_net_hdr *hdr;
757
758         nb_used = VIRTQUEUE_NUSED(vq);
759
760         virtio_rmb();
761
762         num = (uint16_t)(likely(nb_used <= nb_pkts) ? nb_used : nb_pkts);
763         num = (uint16_t)(likely(num <= VIRTIO_MBUF_BURST_SZ) ? num : VIRTIO_MBUF_BURST_SZ);
764         if (likely(num > DESC_PER_CACHELINE))
765                 num = num - ((vq->vq_used_cons_idx + num) % DESC_PER_CACHELINE);
766
767         num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len, num);
768         PMD_RX_LOG(DEBUG, "used:%d dequeue:%d", nb_used, num);
769
770         hw = vq->hw;
771         nb_rx = 0;
772         nb_enqueued = 0;
773         hdr_size = hw->vtnet_hdr_size;
774         offload = rx_offload_enabled(hw);
775
776         for (i = 0; i < num ; i++) {
777                 rxm = rcv_pkts[i];
778
779                 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
780
781                 if (unlikely(len[i] < 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                 rxm->port = rxvq->port_id;
790                 rxm->data_off = RTE_PKTMBUF_HEADROOM;
791                 rxm->ol_flags = 0;
792                 rxm->vlan_tci = 0;
793
794                 rxm->nb_segs = 1;
795                 rxm->next = NULL;
796                 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
797                 rxm->data_len = (uint16_t)(len[i] - hdr_size);
798
799                 hdr = (struct virtio_net_hdr *)((char *)rxm->buf_addr +
800                         RTE_PKTMBUF_HEADROOM - hdr_size);
801
802                 if (hw->vlan_strip)
803                         rte_vlan_strip(rxm);
804
805                 if (offload && virtio_rx_offload(rxm, hdr) < 0) {
806                         virtio_discard_rxbuf(vq, rxm);
807                         rxvq->stats.errors++;
808                         continue;
809                 }
810
811                 VIRTIO_DUMP_PACKET(rxm, rxm->data_len);
812
813                 rx_pkts[nb_rx++] = rxm;
814
815                 rxvq->stats.bytes += rx_pkts[nb_rx - 1]->pkt_len;
816                 virtio_update_packet_stats(&rxvq->stats, rxm);
817         }
818
819         rxvq->stats.packets += nb_rx;
820
821         /* Allocate new mbuf for the used descriptor */
822         error = ENOSPC;
823         while (likely(!virtqueue_full(vq))) {
824                 new_mbuf = rte_mbuf_raw_alloc(rxvq->mpool);
825                 if (unlikely(new_mbuf == NULL)) {
826                         struct rte_eth_dev *dev
827                                 = &rte_eth_devices[rxvq->port_id];
828                         dev->data->rx_mbuf_alloc_failed++;
829                         break;
830                 }
831                 error = virtqueue_enqueue_recv_refill(vq, new_mbuf);
832                 if (unlikely(error)) {
833                         rte_pktmbuf_free(new_mbuf);
834                         break;
835                 }
836                 nb_enqueued++;
837         }
838
839         if (likely(nb_enqueued)) {
840                 vq_update_avail_idx(vq);
841
842                 if (unlikely(virtqueue_kick_prepare(vq))) {
843                         virtqueue_notify(vq);
844                         PMD_RX_LOG(DEBUG, "Notified");
845                 }
846         }
847
848         return nb_rx;
849 }
850
851 uint16_t
852 virtio_recv_mergeable_pkts(void *rx_queue,
853                         struct rte_mbuf **rx_pkts,
854                         uint16_t nb_pkts)
855 {
856         struct virtnet_rx *rxvq = rx_queue;
857         struct virtqueue *vq = rxvq->vq;
858         struct virtio_hw *hw;
859         struct rte_mbuf *rxm, *new_mbuf;
860         uint16_t nb_used, num, nb_rx;
861         uint32_t len[VIRTIO_MBUF_BURST_SZ];
862         struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
863         struct rte_mbuf *prev;
864         int error;
865         uint32_t i, nb_enqueued;
866         uint32_t seg_num;
867         uint16_t extra_idx;
868         uint32_t seg_res;
869         uint32_t hdr_size;
870         int offload;
871
872         nb_used = VIRTQUEUE_NUSED(vq);
873
874         virtio_rmb();
875
876         PMD_RX_LOG(DEBUG, "used:%d", nb_used);
877
878         hw = vq->hw;
879         nb_rx = 0;
880         i = 0;
881         nb_enqueued = 0;
882         seg_num = 0;
883         extra_idx = 0;
884         seg_res = 0;
885         hdr_size = hw->vtnet_hdr_size;
886         offload = rx_offload_enabled(hw);
887
888         while (i < nb_used) {
889                 struct virtio_net_hdr_mrg_rxbuf *header;
890
891                 if (nb_rx == nb_pkts)
892                         break;
893
894                 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len, 1);
895                 if (num != 1)
896                         continue;
897
898                 i++;
899
900                 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
901                 PMD_RX_LOG(DEBUG, "packet len:%d", len[0]);
902
903                 rxm = rcv_pkts[0];
904
905                 if (unlikely(len[0] < hdr_size + ETHER_HDR_LEN)) {
906                         PMD_RX_LOG(ERR, "Packet drop");
907                         nb_enqueued++;
908                         virtio_discard_rxbuf(vq, rxm);
909                         rxvq->stats.errors++;
910                         continue;
911                 }
912
913                 header = (struct virtio_net_hdr_mrg_rxbuf *)((char *)rxm->buf_addr +
914                         RTE_PKTMBUF_HEADROOM - hdr_size);
915                 seg_num = header->num_buffers;
916
917                 if (seg_num == 0)
918                         seg_num = 1;
919
920                 rxm->data_off = RTE_PKTMBUF_HEADROOM;
921                 rxm->nb_segs = seg_num;
922                 rxm->next = NULL;
923                 rxm->ol_flags = 0;
924                 rxm->vlan_tci = 0;
925                 rxm->pkt_len = (uint32_t)(len[0] - hdr_size);
926                 rxm->data_len = (uint16_t)(len[0] - hdr_size);
927
928                 rxm->port = rxvq->port_id;
929                 rx_pkts[nb_rx] = rxm;
930                 prev = rxm;
931
932                 if (offload && virtio_rx_offload(rxm, &header->hdr) < 0) {
933                         virtio_discard_rxbuf(vq, rxm);
934                         rxvq->stats.errors++;
935                         continue;
936                 }
937
938                 seg_res = seg_num - 1;
939
940                 while (seg_res != 0) {
941                         /*
942                          * Get extra segments for current uncompleted packet.
943                          */
944                         uint16_t  rcv_cnt =
945                                 RTE_MIN(seg_res, RTE_DIM(rcv_pkts));
946                         if (likely(VIRTQUEUE_NUSED(vq) >= rcv_cnt)) {
947                                 uint32_t rx_num =
948                                         virtqueue_dequeue_burst_rx(vq,
949                                         rcv_pkts, len, rcv_cnt);
950                                 i += rx_num;
951                                 rcv_cnt = rx_num;
952                         } else {
953                                 PMD_RX_LOG(ERR,
954                                            "No enough segments for packet.");
955                                 nb_enqueued++;
956                                 virtio_discard_rxbuf(vq, rxm);
957                                 rxvq->stats.errors++;
958                                 break;
959                         }
960
961                         extra_idx = 0;
962
963                         while (extra_idx < rcv_cnt) {
964                                 rxm = rcv_pkts[extra_idx];
965
966                                 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
967                                 rxm->next = NULL;
968                                 rxm->pkt_len = (uint32_t)(len[extra_idx]);
969                                 rxm->data_len = (uint16_t)(len[extra_idx]);
970
971                                 if (prev)
972                                         prev->next = rxm;
973
974                                 prev = rxm;
975                                 rx_pkts[nb_rx]->pkt_len += rxm->pkt_len;
976                                 extra_idx++;
977                         };
978                         seg_res -= rcv_cnt;
979                 }
980
981                 if (hw->vlan_strip)
982                         rte_vlan_strip(rx_pkts[nb_rx]);
983
984                 VIRTIO_DUMP_PACKET(rx_pkts[nb_rx],
985                         rx_pkts[nb_rx]->data_len);
986
987                 rxvq->stats.bytes += rx_pkts[nb_rx]->pkt_len;
988                 virtio_update_packet_stats(&rxvq->stats, rx_pkts[nb_rx]);
989                 nb_rx++;
990         }
991
992         rxvq->stats.packets += nb_rx;
993
994         /* Allocate new mbuf for the used descriptor */
995         error = ENOSPC;
996         while (likely(!virtqueue_full(vq))) {
997                 new_mbuf = rte_mbuf_raw_alloc(rxvq->mpool);
998                 if (unlikely(new_mbuf == NULL)) {
999                         struct rte_eth_dev *dev
1000                                 = &rte_eth_devices[rxvq->port_id];
1001                         dev->data->rx_mbuf_alloc_failed++;
1002                         break;
1003                 }
1004                 error = virtqueue_enqueue_recv_refill(vq, new_mbuf);
1005                 if (unlikely(error)) {
1006                         rte_pktmbuf_free(new_mbuf);
1007                         break;
1008                 }
1009                 nb_enqueued++;
1010         }
1011
1012         if (likely(nb_enqueued)) {
1013                 vq_update_avail_idx(vq);
1014
1015                 if (unlikely(virtqueue_kick_prepare(vq))) {
1016                         virtqueue_notify(vq);
1017                         PMD_RX_LOG(DEBUG, "Notified");
1018                 }
1019         }
1020
1021         return nb_rx;
1022 }
1023
1024 uint16_t
1025 virtio_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
1026 {
1027         struct virtnet_tx *txvq = tx_queue;
1028         struct virtqueue *vq = txvq->vq;
1029         struct virtio_hw *hw = vq->hw;
1030         uint16_t hdr_size = hw->vtnet_hdr_size;
1031         uint16_t nb_used, nb_tx;
1032         int error;
1033
1034         if (unlikely(nb_pkts < 1))
1035                 return nb_pkts;
1036
1037         PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
1038         nb_used = VIRTQUEUE_NUSED(vq);
1039
1040         virtio_rmb();
1041         if (likely(nb_used > vq->vq_nentries - vq->vq_free_thresh))
1042                 virtio_xmit_cleanup(vq, nb_used);
1043
1044         for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
1045                 struct rte_mbuf *txm = tx_pkts[nb_tx];
1046                 int can_push = 0, use_indirect = 0, slots, need;
1047
1048                 /* Do VLAN tag insertion */
1049                 if (unlikely(txm->ol_flags & PKT_TX_VLAN_PKT)) {
1050                         error = rte_vlan_insert(&txm);
1051                         if (unlikely(error)) {
1052                                 rte_pktmbuf_free(txm);
1053                                 continue;
1054                         }
1055                 }
1056
1057                 /* optimize ring usage */
1058                 if (vtpci_with_feature(hw, VIRTIO_F_ANY_LAYOUT) &&
1059                     rte_mbuf_refcnt_read(txm) == 1 &&
1060                     RTE_MBUF_DIRECT(txm) &&
1061                     txm->nb_segs == 1 &&
1062                     rte_pktmbuf_headroom(txm) >= hdr_size &&
1063                     rte_is_aligned(rte_pktmbuf_mtod(txm, char *),
1064                                    __alignof__(struct virtio_net_hdr_mrg_rxbuf)))
1065                         can_push = 1;
1066                 else if (vtpci_with_feature(hw, VIRTIO_RING_F_INDIRECT_DESC) &&
1067                          txm->nb_segs < VIRTIO_MAX_TX_INDIRECT)
1068                         use_indirect = 1;
1069
1070                 /* How many main ring entries are needed to this Tx?
1071                  * any_layout => number of segments
1072                  * indirect   => 1
1073                  * default    => number of segments + 1
1074                  */
1075                 slots = use_indirect ? 1 : (txm->nb_segs + !can_push);
1076                 need = slots - vq->vq_free_cnt;
1077
1078                 /* Positive value indicates it need free vring descriptors */
1079                 if (unlikely(need > 0)) {
1080                         nb_used = VIRTQUEUE_NUSED(vq);
1081                         virtio_rmb();
1082                         need = RTE_MIN(need, (int)nb_used);
1083
1084                         virtio_xmit_cleanup(vq, need);
1085                         need = slots - vq->vq_free_cnt;
1086                         if (unlikely(need > 0)) {
1087                                 PMD_TX_LOG(ERR,
1088                                            "No free tx descriptors to transmit");
1089                                 break;
1090                         }
1091                 }
1092
1093                 /* Enqueue Packet buffers */
1094                 virtqueue_enqueue_xmit(txvq, txm, slots, use_indirect, can_push);
1095
1096                 txvq->stats.bytes += txm->pkt_len;
1097                 virtio_update_packet_stats(&txvq->stats, txm);
1098         }
1099
1100         txvq->stats.packets += nb_tx;
1101
1102         if (likely(nb_tx)) {
1103                 vq_update_avail_idx(vq);
1104
1105                 if (unlikely(virtqueue_kick_prepare(vq))) {
1106                         virtqueue_notify(vq);
1107                         PMD_TX_LOG(DEBUG, "Notified backend after xmit");
1108                 }
1109         }
1110
1111         return nb_tx;
1112 }