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