1ec885003f4ea54dbfdaf7a8336332be4bd2b458
[dpdk.git] / lib / librte_vhost / vhost_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 <stdbool.h>
36 #include <linux/virtio_net.h>
37
38 #include <rte_mbuf.h>
39 #include <rte_memcpy.h>
40 #include <rte_virtio_net.h>
41
42 #include "vhost-net.h"
43
44 #define MAX_PKT_BURST 32
45
46 static bool
47 is_valid_virt_queue_idx(uint32_t idx, int is_tx, uint32_t qp_nb)
48 {
49         return (is_tx ^ (idx & 1)) == 0 && idx < qp_nb * VIRTIO_QNUM;
50 }
51
52 /**
53  * This function adds buffers to the virtio devices RX virtqueue. Buffers can
54  * be received from the physical port or from another virtio device. A packet
55  * count is returned to indicate the number of packets that are succesfully
56  * added to the RX queue. This function works when the mbuf is scattered, but
57  * it doesn't support the mergeable feature.
58  */
59 static inline uint32_t __attribute__((always_inline))
60 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
61         struct rte_mbuf **pkts, uint32_t count)
62 {
63         struct vhost_virtqueue *vq;
64         struct vring_desc *desc;
65         struct rte_mbuf *buff;
66         /* The virtio_hdr is initialised to 0. */
67         struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0, 0, 0, 0, 0, 0}, 0};
68         uint64_t buff_addr = 0;
69         uint64_t buff_hdr_addr = 0;
70         uint32_t head[MAX_PKT_BURST];
71         uint32_t head_idx, packet_success = 0;
72         uint16_t avail_idx, res_cur_idx;
73         uint16_t res_base_idx, res_end_idx;
74         uint16_t free_entries;
75         uint8_t success = 0;
76
77         LOG_DEBUG(VHOST_DATA, "(%"PRIu64") virtio_dev_rx()\n", dev->device_fh);
78         if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->virt_qp_nb))) {
79                 RTE_LOG(ERR, VHOST_DATA,
80                         "%s (%"PRIu64"): virtqueue idx:%d invalid.\n",
81                         __func__, dev->device_fh, queue_id);
82                 return 0;
83         }
84
85         vq = dev->virtqueue[queue_id];
86         count = (count > MAX_PKT_BURST) ? MAX_PKT_BURST : count;
87
88         /*
89          * As many data cores may want access to available buffers,
90          * they need to be reserved.
91          */
92         do {
93                 res_base_idx = vq->last_used_idx_res;
94                 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
95
96                 free_entries = (avail_idx - res_base_idx);
97                 /*check that we have enough buffers*/
98                 if (unlikely(count > free_entries))
99                         count = free_entries;
100
101                 if (count == 0)
102                         return 0;
103
104                 res_end_idx = res_base_idx + count;
105                 /* vq->last_used_idx_res is atomically updated. */
106                 /* TODO: Allow to disable cmpset if no concurrency in application. */
107                 success = rte_atomic16_cmpset(&vq->last_used_idx_res,
108                                 res_base_idx, res_end_idx);
109         } while (unlikely(success == 0));
110         res_cur_idx = res_base_idx;
111         LOG_DEBUG(VHOST_DATA, "(%"PRIu64") Current Index %d| End Index %d\n",
112                         dev->device_fh, res_cur_idx, res_end_idx);
113
114         /* Prefetch available ring to retrieve indexes. */
115         rte_prefetch0(&vq->avail->ring[res_cur_idx & (vq->size - 1)]);
116
117         /* Retrieve all of the head indexes first to avoid caching issues. */
118         for (head_idx = 0; head_idx < count; head_idx++)
119                 head[head_idx] = vq->avail->ring[(res_cur_idx + head_idx) &
120                                         (vq->size - 1)];
121
122         /*Prefetch descriptor index. */
123         rte_prefetch0(&vq->desc[head[packet_success]]);
124
125         while (res_cur_idx != res_end_idx) {
126                 uint32_t offset = 0, vb_offset = 0;
127                 uint32_t pkt_len, len_to_cpy, data_len, total_copied = 0;
128                 uint8_t hdr = 0, uncompleted_pkt = 0;
129
130                 /* Get descriptor from available ring */
131                 desc = &vq->desc[head[packet_success]];
132
133                 buff = pkts[packet_success];
134
135                 /* Convert from gpa to vva (guest physical addr -> vhost virtual addr) */
136                 buff_addr = gpa_to_vva(dev, desc->addr);
137                 /* Prefetch buffer address. */
138                 rte_prefetch0((void *)(uintptr_t)buff_addr);
139
140                 /* Copy virtio_hdr to packet and increment buffer address */
141                 buff_hdr_addr = buff_addr;
142
143                 /*
144                  * If the descriptors are chained the header and data are
145                  * placed in separate buffers.
146                  */
147                 if ((desc->flags & VRING_DESC_F_NEXT) &&
148                         (desc->len == vq->vhost_hlen)) {
149                         desc = &vq->desc[desc->next];
150                         /* Buffer address translation. */
151                         buff_addr = gpa_to_vva(dev, desc->addr);
152                 } else {
153                         vb_offset += vq->vhost_hlen;
154                         hdr = 1;
155                 }
156
157                 pkt_len = rte_pktmbuf_pkt_len(buff);
158                 data_len = rte_pktmbuf_data_len(buff);
159                 len_to_cpy = RTE_MIN(data_len,
160                         hdr ? desc->len - vq->vhost_hlen : desc->len);
161                 while (total_copied < pkt_len) {
162                         /* Copy mbuf data to buffer */
163                         rte_memcpy((void *)(uintptr_t)(buff_addr + vb_offset),
164                                 rte_pktmbuf_mtod_offset(buff, const void *, offset),
165                                 len_to_cpy);
166                         PRINT_PACKET(dev, (uintptr_t)(buff_addr + vb_offset),
167                                 len_to_cpy, 0);
168
169                         offset += len_to_cpy;
170                         vb_offset += len_to_cpy;
171                         total_copied += len_to_cpy;
172
173                         /* The whole packet completes */
174                         if (total_copied == pkt_len)
175                                 break;
176
177                         /* The current segment completes */
178                         if (offset == data_len) {
179                                 buff = buff->next;
180                                 offset = 0;
181                                 data_len = rte_pktmbuf_data_len(buff);
182                         }
183
184                         /* The current vring descriptor done */
185                         if (vb_offset == desc->len) {
186                                 if (desc->flags & VRING_DESC_F_NEXT) {
187                                         desc = &vq->desc[desc->next];
188                                         buff_addr = gpa_to_vva(dev, desc->addr);
189                                         vb_offset = 0;
190                                 } else {
191                                         /* Room in vring buffer is not enough */
192                                         uncompleted_pkt = 1;
193                                         break;
194                                 }
195                         }
196                         len_to_cpy = RTE_MIN(data_len - offset, desc->len - vb_offset);
197                 }
198
199                 /* Update used ring with desc information */
200                 vq->used->ring[res_cur_idx & (vq->size - 1)].id =
201                                                         head[packet_success];
202
203                 /* Drop the packet if it is uncompleted */
204                 if (unlikely(uncompleted_pkt == 1))
205                         vq->used->ring[res_cur_idx & (vq->size - 1)].len =
206                                                         vq->vhost_hlen;
207                 else
208                         vq->used->ring[res_cur_idx & (vq->size - 1)].len =
209                                                         pkt_len + vq->vhost_hlen;
210
211                 res_cur_idx++;
212                 packet_success++;
213
214                 if (unlikely(uncompleted_pkt == 1))
215                         continue;
216
217                 rte_memcpy((void *)(uintptr_t)buff_hdr_addr,
218                         (const void *)&virtio_hdr, vq->vhost_hlen);
219
220                 PRINT_PACKET(dev, (uintptr_t)buff_hdr_addr, vq->vhost_hlen, 1);
221
222                 if (res_cur_idx < res_end_idx) {
223                         /* Prefetch descriptor index. */
224                         rte_prefetch0(&vq->desc[head[packet_success]]);
225                 }
226         }
227
228         rte_compiler_barrier();
229
230         /* Wait until it's our turn to add our buffer to the used ring. */
231         while (unlikely(vq->last_used_idx != res_base_idx))
232                 rte_pause();
233
234         *(volatile uint16_t *)&vq->used->idx += count;
235         vq->last_used_idx = res_end_idx;
236
237         /* flush used->idx update before we read avail->flags. */
238         rte_mb();
239
240         /* Kick the guest if necessary. */
241         if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
242                 eventfd_write(vq->callfd, (eventfd_t)1);
243         return count;
244 }
245
246 static inline uint32_t __attribute__((always_inline))
247 copy_from_mbuf_to_vring(struct virtio_net *dev, uint32_t queue_id,
248                         uint16_t res_base_idx, uint16_t res_end_idx,
249                         struct rte_mbuf *pkt)
250 {
251         uint32_t vec_idx = 0;
252         uint32_t entry_success = 0;
253         struct vhost_virtqueue *vq;
254         /* The virtio_hdr is initialised to 0. */
255         struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {
256                 {0, 0, 0, 0, 0, 0}, 0};
257         uint16_t cur_idx = res_base_idx;
258         uint64_t vb_addr = 0;
259         uint64_t vb_hdr_addr = 0;
260         uint32_t seg_offset = 0;
261         uint32_t vb_offset = 0;
262         uint32_t seg_avail;
263         uint32_t vb_avail;
264         uint32_t cpy_len, entry_len;
265
266         if (pkt == NULL)
267                 return 0;
268
269         LOG_DEBUG(VHOST_DATA, "(%"PRIu64") Current Index %d| "
270                 "End Index %d\n",
271                 dev->device_fh, cur_idx, res_end_idx);
272
273         /*
274          * Convert from gpa to vva
275          * (guest physical addr -> vhost virtual addr)
276          */
277         vq = dev->virtqueue[queue_id];
278         vb_addr = gpa_to_vva(dev, vq->buf_vec[vec_idx].buf_addr);
279         vb_hdr_addr = vb_addr;
280
281         /* Prefetch buffer address. */
282         rte_prefetch0((void *)(uintptr_t)vb_addr);
283
284         virtio_hdr.num_buffers = res_end_idx - res_base_idx;
285
286         LOG_DEBUG(VHOST_DATA, "(%"PRIu64") RX: Num merge buffers %d\n",
287                 dev->device_fh, virtio_hdr.num_buffers);
288
289         rte_memcpy((void *)(uintptr_t)vb_hdr_addr,
290                 (const void *)&virtio_hdr, vq->vhost_hlen);
291
292         PRINT_PACKET(dev, (uintptr_t)vb_hdr_addr, vq->vhost_hlen, 1);
293
294         seg_avail = rte_pktmbuf_data_len(pkt);
295         vb_offset = vq->vhost_hlen;
296         vb_avail = vq->buf_vec[vec_idx].buf_len - vq->vhost_hlen;
297
298         entry_len = vq->vhost_hlen;
299
300         if (vb_avail == 0) {
301                 uint32_t desc_idx =
302                         vq->buf_vec[vec_idx].desc_idx;
303
304                 if ((vq->desc[desc_idx].flags
305                         & VRING_DESC_F_NEXT) == 0) {
306                         /* Update used ring with desc information */
307                         vq->used->ring[cur_idx & (vq->size - 1)].id
308                                 = vq->buf_vec[vec_idx].desc_idx;
309                         vq->used->ring[cur_idx & (vq->size - 1)].len
310                                 = entry_len;
311
312                         entry_len = 0;
313                         cur_idx++;
314                         entry_success++;
315                 }
316
317                 vec_idx++;
318                 vb_addr = gpa_to_vva(dev, vq->buf_vec[vec_idx].buf_addr);
319
320                 /* Prefetch buffer address. */
321                 rte_prefetch0((void *)(uintptr_t)vb_addr);
322                 vb_offset = 0;
323                 vb_avail = vq->buf_vec[vec_idx].buf_len;
324         }
325
326         cpy_len = RTE_MIN(vb_avail, seg_avail);
327
328         while (cpy_len > 0) {
329                 /* Copy mbuf data to vring buffer */
330                 rte_memcpy((void *)(uintptr_t)(vb_addr + vb_offset),
331                         rte_pktmbuf_mtod_offset(pkt, const void *, seg_offset),
332                         cpy_len);
333
334                 PRINT_PACKET(dev,
335                         (uintptr_t)(vb_addr + vb_offset),
336                         cpy_len, 0);
337
338                 seg_offset += cpy_len;
339                 vb_offset += cpy_len;
340                 seg_avail -= cpy_len;
341                 vb_avail -= cpy_len;
342                 entry_len += cpy_len;
343
344                 if (seg_avail != 0) {
345                         /*
346                          * The virtio buffer in this vring
347                          * entry reach to its end.
348                          * But the segment doesn't complete.
349                          */
350                         if ((vq->desc[vq->buf_vec[vec_idx].desc_idx].flags &
351                                 VRING_DESC_F_NEXT) == 0) {
352                                 /* Update used ring with desc information */
353                                 vq->used->ring[cur_idx & (vq->size - 1)].id
354                                         = vq->buf_vec[vec_idx].desc_idx;
355                                 vq->used->ring[cur_idx & (vq->size - 1)].len
356                                         = entry_len;
357                                 entry_len = 0;
358                                 cur_idx++;
359                                 entry_success++;
360                         }
361
362                         vec_idx++;
363                         vb_addr = gpa_to_vva(dev,
364                                 vq->buf_vec[vec_idx].buf_addr);
365                         vb_offset = 0;
366                         vb_avail = vq->buf_vec[vec_idx].buf_len;
367                         cpy_len = RTE_MIN(vb_avail, seg_avail);
368                 } else {
369                         /*
370                          * This current segment complete, need continue to
371                          * check if the whole packet complete or not.
372                          */
373                         pkt = pkt->next;
374                         if (pkt != NULL) {
375                                 /*
376                                  * There are more segments.
377                                  */
378                                 if (vb_avail == 0) {
379                                         /*
380                                          * This current buffer from vring is
381                                          * used up, need fetch next buffer
382                                          * from buf_vec.
383                                          */
384                                         uint32_t desc_idx =
385                                                 vq->buf_vec[vec_idx].desc_idx;
386
387                                         if ((vq->desc[desc_idx].flags &
388                                                 VRING_DESC_F_NEXT) == 0) {
389                                                 uint16_t wrapped_idx =
390                                                         cur_idx & (vq->size - 1);
391                                                 /*
392                                                  * Update used ring with the
393                                                  * descriptor information
394                                                  */
395                                                 vq->used->ring[wrapped_idx].id
396                                                         = desc_idx;
397                                                 vq->used->ring[wrapped_idx].len
398                                                         = entry_len;
399                                                 entry_success++;
400                                                 entry_len = 0;
401                                                 cur_idx++;
402                                         }
403
404                                         /* Get next buffer from buf_vec. */
405                                         vec_idx++;
406                                         vb_addr = gpa_to_vva(dev,
407                                                 vq->buf_vec[vec_idx].buf_addr);
408                                         vb_avail =
409                                                 vq->buf_vec[vec_idx].buf_len;
410                                         vb_offset = 0;
411                                 }
412
413                                 seg_offset = 0;
414                                 seg_avail = rte_pktmbuf_data_len(pkt);
415                                 cpy_len = RTE_MIN(vb_avail, seg_avail);
416                         } else {
417                                 /*
418                                  * This whole packet completes.
419                                  */
420                                 /* Update used ring with desc information */
421                                 vq->used->ring[cur_idx & (vq->size - 1)].id
422                                         = vq->buf_vec[vec_idx].desc_idx;
423                                 vq->used->ring[cur_idx & (vq->size - 1)].len
424                                         = entry_len;
425                                 entry_success++;
426                                 break;
427                         }
428                 }
429         }
430
431         return entry_success;
432 }
433
434 static inline void __attribute__((always_inline))
435 update_secure_len(struct vhost_virtqueue *vq, uint32_t id,
436         uint32_t *secure_len, uint32_t *vec_idx)
437 {
438         uint16_t wrapped_idx = id & (vq->size - 1);
439         uint32_t idx = vq->avail->ring[wrapped_idx];
440         uint8_t next_desc;
441         uint32_t len = *secure_len;
442         uint32_t vec_id = *vec_idx;
443
444         do {
445                 next_desc = 0;
446                 len += vq->desc[idx].len;
447                 vq->buf_vec[vec_id].buf_addr = vq->desc[idx].addr;
448                 vq->buf_vec[vec_id].buf_len = vq->desc[idx].len;
449                 vq->buf_vec[vec_id].desc_idx = idx;
450                 vec_id++;
451
452                 if (vq->desc[idx].flags & VRING_DESC_F_NEXT) {
453                         idx = vq->desc[idx].next;
454                         next_desc = 1;
455                 }
456         } while (next_desc);
457
458         *secure_len = len;
459         *vec_idx = vec_id;
460 }
461
462 /*
463  * This function works for mergeable RX.
464  */
465 static inline uint32_t __attribute__((always_inline))
466 virtio_dev_merge_rx(struct virtio_net *dev, uint16_t queue_id,
467         struct rte_mbuf **pkts, uint32_t count)
468 {
469         struct vhost_virtqueue *vq;
470         uint32_t pkt_idx = 0, entry_success = 0;
471         uint16_t avail_idx;
472         uint16_t res_base_idx, res_cur_idx;
473         uint8_t success = 0;
474
475         LOG_DEBUG(VHOST_DATA, "(%"PRIu64") virtio_dev_merge_rx()\n",
476                 dev->device_fh);
477         if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->virt_qp_nb))) {
478                 RTE_LOG(ERR, VHOST_DATA,
479                         "%s (%"PRIu64"): virtqueue idx:%d invalid.\n",
480                         __func__, dev->device_fh, queue_id);
481                 return 0;
482         }
483
484         vq = dev->virtqueue[queue_id];
485         count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
486
487         if (count == 0)
488                 return 0;
489
490         for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
491                 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + vq->vhost_hlen;
492
493                 do {
494                         /*
495                          * As many data cores may want access to available
496                          * buffers, they need to be reserved.
497                          */
498                         uint32_t secure_len = 0;
499                         uint32_t vec_idx = 0;
500
501                         res_base_idx = vq->last_used_idx_res;
502                         res_cur_idx = res_base_idx;
503
504                         do {
505                                 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
506                                 if (unlikely(res_cur_idx == avail_idx)) {
507                                         LOG_DEBUG(VHOST_DATA,
508                                                 "(%"PRIu64") Failed "
509                                                 "to get enough desc from "
510                                                 "vring\n",
511                                                 dev->device_fh);
512                                         goto merge_rx_exit;
513                                 } else {
514                                         update_secure_len(vq, res_cur_idx, &secure_len, &vec_idx);
515                                         res_cur_idx++;
516                                 }
517                         } while (pkt_len > secure_len);
518
519                         /* vq->last_used_idx_res is atomically updated. */
520                         success = rte_atomic16_cmpset(&vq->last_used_idx_res,
521                                                         res_base_idx,
522                                                         res_cur_idx);
523                 } while (success == 0);
524
525                 entry_success = copy_from_mbuf_to_vring(dev, queue_id,
526                         res_base_idx, res_cur_idx, pkts[pkt_idx]);
527
528                 rte_compiler_barrier();
529
530                 /*
531                  * Wait until it's our turn to add our buffer
532                  * to the used ring.
533                  */
534                 while (unlikely(vq->last_used_idx != res_base_idx))
535                         rte_pause();
536
537                 *(volatile uint16_t *)&vq->used->idx += entry_success;
538                 vq->last_used_idx = res_cur_idx;
539         }
540
541 merge_rx_exit:
542         if (likely(pkt_idx)) {
543                 /* flush used->idx update before we read avail->flags. */
544                 rte_mb();
545
546                 /* Kick the guest if necessary. */
547                 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
548                         eventfd_write(vq->callfd, (eventfd_t)1);
549         }
550
551         return pkt_idx;
552 }
553
554 uint16_t
555 rte_vhost_enqueue_burst(struct virtio_net *dev, uint16_t queue_id,
556         struct rte_mbuf **pkts, uint16_t count)
557 {
558         if (unlikely(dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)))
559                 return virtio_dev_merge_rx(dev, queue_id, pkts, count);
560         else
561                 return virtio_dev_rx(dev, queue_id, pkts, count);
562 }
563
564 uint16_t
565 rte_vhost_dequeue_burst(struct virtio_net *dev, uint16_t queue_id,
566         struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
567 {
568         struct rte_mbuf *m, *prev;
569         struct vhost_virtqueue *vq;
570         struct vring_desc *desc;
571         uint64_t vb_addr = 0;
572         uint32_t head[MAX_PKT_BURST];
573         uint32_t used_idx;
574         uint32_t i;
575         uint16_t free_entries, entry_success = 0;
576         uint16_t avail_idx;
577
578         if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->virt_qp_nb))) {
579                 RTE_LOG(ERR, VHOST_DATA,
580                         "%s (%"PRIu64"): virtqueue idx:%d invalid.\n",
581                         __func__, dev->device_fh, queue_id);
582                 return 0;
583         }
584
585         vq = dev->virtqueue[queue_id];
586         avail_idx =  *((volatile uint16_t *)&vq->avail->idx);
587
588         /* If there are no available buffers then return. */
589         if (vq->last_used_idx == avail_idx)
590                 return 0;
591
592         LOG_DEBUG(VHOST_DATA, "%s (%"PRIu64")\n", __func__,
593                 dev->device_fh);
594
595         /* Prefetch available ring to retrieve head indexes. */
596         rte_prefetch0(&vq->avail->ring[vq->last_used_idx & (vq->size - 1)]);
597
598         /*get the number of free entries in the ring*/
599         free_entries = (avail_idx - vq->last_used_idx);
600
601         free_entries = RTE_MIN(free_entries, count);
602         /* Limit to MAX_PKT_BURST. */
603         free_entries = RTE_MIN(free_entries, MAX_PKT_BURST);
604
605         LOG_DEBUG(VHOST_DATA, "(%"PRIu64") Buffers available %d\n",
606                         dev->device_fh, free_entries);
607         /* Retrieve all of the head indexes first to avoid caching issues. */
608         for (i = 0; i < free_entries; i++)
609                 head[i] = vq->avail->ring[(vq->last_used_idx + i) & (vq->size - 1)];
610
611         /* Prefetch descriptor index. */
612         rte_prefetch0(&vq->desc[head[entry_success]]);
613         rte_prefetch0(&vq->used->ring[vq->last_used_idx & (vq->size - 1)]);
614
615         while (entry_success < free_entries) {
616                 uint32_t vb_avail, vb_offset;
617                 uint32_t seg_avail, seg_offset;
618                 uint32_t cpy_len;
619                 uint32_t seg_num = 0;
620                 struct rte_mbuf *cur;
621                 uint8_t alloc_err = 0;
622
623                 desc = &vq->desc[head[entry_success]];
624
625                 /* Discard first buffer as it is the virtio header */
626                 if (desc->flags & VRING_DESC_F_NEXT) {
627                         desc = &vq->desc[desc->next];
628                         vb_offset = 0;
629                         vb_avail = desc->len;
630                 } else {
631                         vb_offset = vq->vhost_hlen;
632                         vb_avail = desc->len - vb_offset;
633                 }
634
635                 /* Buffer address translation. */
636                 vb_addr = gpa_to_vva(dev, desc->addr);
637                 /* Prefetch buffer address. */
638                 rte_prefetch0((void *)(uintptr_t)vb_addr);
639
640                 used_idx = vq->last_used_idx & (vq->size - 1);
641
642                 if (entry_success < (free_entries - 1)) {
643                         /* Prefetch descriptor index. */
644                         rte_prefetch0(&vq->desc[head[entry_success+1]]);
645                         rte_prefetch0(&vq->used->ring[(used_idx + 1) & (vq->size - 1)]);
646                 }
647
648                 /* Update used index buffer information. */
649                 vq->used->ring[used_idx].id = head[entry_success];
650                 vq->used->ring[used_idx].len = 0;
651
652                 /* Allocate an mbuf and populate the structure. */
653                 m = rte_pktmbuf_alloc(mbuf_pool);
654                 if (unlikely(m == NULL)) {
655                         RTE_LOG(ERR, VHOST_DATA,
656                                 "Failed to allocate memory for mbuf.\n");
657                         break;
658                 }
659                 seg_offset = 0;
660                 seg_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
661                 cpy_len = RTE_MIN(vb_avail, seg_avail);
662
663                 PRINT_PACKET(dev, (uintptr_t)vb_addr, desc->len, 0);
664
665                 seg_num++;
666                 cur = m;
667                 prev = m;
668                 while (cpy_len != 0) {
669                         rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *, seg_offset),
670                                 (void *)((uintptr_t)(vb_addr + vb_offset)),
671                                 cpy_len);
672
673                         seg_offset += cpy_len;
674                         vb_offset += cpy_len;
675                         vb_avail -= cpy_len;
676                         seg_avail -= cpy_len;
677
678                         if (vb_avail != 0) {
679                                 /*
680                                  * The segment reachs to its end,
681                                  * while the virtio buffer in TX vring has
682                                  * more data to be copied.
683                                  */
684                                 cur->data_len = seg_offset;
685                                 m->pkt_len += seg_offset;
686                                 /* Allocate mbuf and populate the structure. */
687                                 cur = rte_pktmbuf_alloc(mbuf_pool);
688                                 if (unlikely(cur == NULL)) {
689                                         RTE_LOG(ERR, VHOST_DATA, "Failed to "
690                                                 "allocate memory for mbuf.\n");
691                                         rte_pktmbuf_free(m);
692                                         alloc_err = 1;
693                                         break;
694                                 }
695
696                                 seg_num++;
697                                 prev->next = cur;
698                                 prev = cur;
699                                 seg_offset = 0;
700                                 seg_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
701                         } else {
702                                 if (desc->flags & VRING_DESC_F_NEXT) {
703                                         /*
704                                          * There are more virtio buffers in
705                                          * same vring entry need to be copied.
706                                          */
707                                         if (seg_avail == 0) {
708                                                 /*
709                                                  * The current segment hasn't
710                                                  * room to accomodate more
711                                                  * data.
712                                                  */
713                                                 cur->data_len = seg_offset;
714                                                 m->pkt_len += seg_offset;
715                                                 /*
716                                                  * Allocate an mbuf and
717                                                  * populate the structure.
718                                                  */
719                                                 cur = rte_pktmbuf_alloc(mbuf_pool);
720                                                 if (unlikely(cur == NULL)) {
721                                                         RTE_LOG(ERR,
722                                                                 VHOST_DATA,
723                                                                 "Failed to "
724                                                                 "allocate memory "
725                                                                 "for mbuf\n");
726                                                         rte_pktmbuf_free(m);
727                                                         alloc_err = 1;
728                                                         break;
729                                                 }
730                                                 seg_num++;
731                                                 prev->next = cur;
732                                                 prev = cur;
733                                                 seg_offset = 0;
734                                                 seg_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
735                                         }
736
737                                         desc = &vq->desc[desc->next];
738
739                                         /* Buffer address translation. */
740                                         vb_addr = gpa_to_vva(dev, desc->addr);
741                                         /* Prefetch buffer address. */
742                                         rte_prefetch0((void *)(uintptr_t)vb_addr);
743                                         vb_offset = 0;
744                                         vb_avail = desc->len;
745
746                                         PRINT_PACKET(dev, (uintptr_t)vb_addr,
747                                                 desc->len, 0);
748                                 } else {
749                                         /* The whole packet completes. */
750                                         cur->data_len = seg_offset;
751                                         m->pkt_len += seg_offset;
752                                         vb_avail = 0;
753                                 }
754                         }
755
756                         cpy_len = RTE_MIN(vb_avail, seg_avail);
757                 }
758
759                 if (unlikely(alloc_err == 1))
760                         break;
761
762                 m->nb_segs = seg_num;
763
764                 pkts[entry_success] = m;
765                 vq->last_used_idx++;
766                 entry_success++;
767         }
768
769         rte_compiler_barrier();
770         vq->used->idx += entry_success;
771         /* Kick guest if required. */
772         if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
773                 eventfd_write(vq->callfd, (eventfd_t)1);
774         return entry_success;
775 }