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