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