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