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