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