c7c955055958c477e0416854e5a9816052e3272e
[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         /* Kick the guest if necessary. */
182         if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
183                 eventfd_write((int)vq->kickfd, 1);
184         return count;
185 }
186
187 static inline uint32_t __attribute__((always_inline))
188 copy_from_mbuf_to_vring(struct virtio_net *dev, uint16_t res_base_idx,
189         uint16_t res_end_idx, struct rte_mbuf *pkt)
190 {
191         uint32_t vec_idx = 0;
192         uint32_t entry_success = 0;
193         struct vhost_virtqueue *vq;
194         /* The virtio_hdr is initialised to 0. */
195         struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {
196                 {0, 0, 0, 0, 0, 0}, 0};
197         uint16_t cur_idx = res_base_idx;
198         uint64_t vb_addr = 0;
199         uint64_t vb_hdr_addr = 0;
200         uint32_t seg_offset = 0;
201         uint32_t vb_offset = 0;
202         uint32_t seg_avail;
203         uint32_t vb_avail;
204         uint32_t cpy_len, entry_len;
205
206         if (pkt == NULL)
207                 return 0;
208
209         LOG_DEBUG(VHOST_DATA, "(%"PRIu64") Current Index %d| "
210                 "End Index %d\n",
211                 dev->device_fh, cur_idx, res_end_idx);
212
213         /*
214          * Convert from gpa to vva
215          * (guest physical addr -> vhost virtual addr)
216          */
217         vq = dev->virtqueue[VIRTIO_RXQ];
218         vb_addr =
219                 gpa_to_vva(dev, vq->buf_vec[vec_idx].buf_addr);
220         vb_hdr_addr = vb_addr;
221
222         /* Prefetch buffer address. */
223         rte_prefetch0((void *)(uintptr_t)vb_addr);
224
225         virtio_hdr.num_buffers = res_end_idx - res_base_idx;
226
227         LOG_DEBUG(VHOST_DATA, "(%"PRIu64") RX: Num merge buffers %d\n",
228                 dev->device_fh, virtio_hdr.num_buffers);
229
230         rte_memcpy((void *)(uintptr_t)vb_hdr_addr,
231                 (const void *)&virtio_hdr, vq->vhost_hlen);
232
233         PRINT_PACKET(dev, (uintptr_t)vb_hdr_addr, vq->vhost_hlen, 1);
234
235         seg_avail = rte_pktmbuf_data_len(pkt);
236         vb_offset = vq->vhost_hlen;
237         vb_avail =
238                 vq->buf_vec[vec_idx].buf_len - vq->vhost_hlen;
239
240         entry_len = vq->vhost_hlen;
241
242         if (vb_avail == 0) {
243                 uint32_t desc_idx =
244                         vq->buf_vec[vec_idx].desc_idx;
245                 vq->desc[desc_idx].len = vq->vhost_hlen;
246
247                 if ((vq->desc[desc_idx].flags
248                         & VRING_DESC_F_NEXT) == 0) {
249                         /* Update used ring with desc information */
250                         vq->used->ring[cur_idx & (vq->size - 1)].id
251                                 = vq->buf_vec[vec_idx].desc_idx;
252                         vq->used->ring[cur_idx & (vq->size - 1)].len
253                                 = entry_len;
254
255                         entry_len = 0;
256                         cur_idx++;
257                         entry_success++;
258                 }
259
260                 vec_idx++;
261                 vb_addr =
262                         gpa_to_vva(dev, vq->buf_vec[vec_idx].buf_addr);
263
264                 /* Prefetch buffer address. */
265                 rte_prefetch0((void *)(uintptr_t)vb_addr);
266                 vb_offset = 0;
267                 vb_avail = vq->buf_vec[vec_idx].buf_len;
268         }
269
270         cpy_len = RTE_MIN(vb_avail, seg_avail);
271
272         while (cpy_len > 0) {
273                 /* Copy mbuf data to vring buffer */
274                 rte_memcpy((void *)(uintptr_t)(vb_addr + vb_offset),
275                         (const void *)(rte_pktmbuf_mtod(pkt, char*) + seg_offset),
276                         cpy_len);
277
278                 PRINT_PACKET(dev,
279                         (uintptr_t)(vb_addr + vb_offset),
280                         cpy_len, 0);
281
282                 seg_offset += cpy_len;
283                 vb_offset += cpy_len;
284                 seg_avail -= cpy_len;
285                 vb_avail -= cpy_len;
286                 entry_len += cpy_len;
287
288                 if (seg_avail != 0) {
289                         /*
290                          * The virtio buffer in this vring
291                          * entry reach to its end.
292                          * But the segment doesn't complete.
293                          */
294                         if ((vq->desc[vq->buf_vec[vec_idx].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                                 entry_len = 0;
302                                 cur_idx++;
303                                 entry_success++;
304                         }
305
306                         vec_idx++;
307                         vb_addr = gpa_to_vva(dev,
308                                 vq->buf_vec[vec_idx].buf_addr);
309                         vb_offset = 0;
310                         vb_avail = vq->buf_vec[vec_idx].buf_len;
311                         cpy_len = RTE_MIN(vb_avail, seg_avail);
312                 } else {
313                         /*
314                          * This current segment complete, need continue to
315                          * check if the whole packet complete or not.
316                          */
317                         pkt = pkt->next;
318                         if (pkt != NULL) {
319                                 /*
320                                  * There are more segments.
321                                  */
322                                 if (vb_avail == 0) {
323                                         /*
324                                          * This current buffer from vring is
325                                          * used up, need fetch next buffer
326                                          * from buf_vec.
327                                          */
328                                         uint32_t desc_idx =
329                                                 vq->buf_vec[vec_idx].desc_idx;
330                                         vq->desc[desc_idx].len = vb_offset;
331
332                                         if ((vq->desc[desc_idx].flags &
333                                                 VRING_DESC_F_NEXT) == 0) {
334                                                 uint16_t wrapped_idx =
335                                                         cur_idx & (vq->size - 1);
336                                                 /*
337                                                  * Update used ring with the
338                                                  * descriptor information
339                                                  */
340                                                 vq->used->ring[wrapped_idx].id
341                                                         = desc_idx;
342                                                 vq->used->ring[wrapped_idx].len
343                                                         = entry_len;
344                                                 entry_success++;
345                                                 entry_len = 0;
346                                                 cur_idx++;
347                                         }
348
349                                         /* Get next buffer from buf_vec. */
350                                         vec_idx++;
351                                         vb_addr = gpa_to_vva(dev,
352                                                 vq->buf_vec[vec_idx].buf_addr);
353                                         vb_avail =
354                                                 vq->buf_vec[vec_idx].buf_len;
355                                         vb_offset = 0;
356                                 }
357
358                                 seg_offset = 0;
359                                 seg_avail = rte_pktmbuf_data_len(pkt);
360                                 cpy_len = RTE_MIN(vb_avail, seg_avail);
361                         } else {
362                                 /*
363                                  * This whole packet completes.
364                                  */
365                                 uint32_t desc_idx =
366                                         vq->buf_vec[vec_idx].desc_idx;
367                                 vq->desc[desc_idx].len = vb_offset;
368
369                                 while (vq->desc[desc_idx].flags &
370                                         VRING_DESC_F_NEXT) {
371                                         desc_idx = vq->desc[desc_idx].next;
372                                          vq->desc[desc_idx].len = 0;
373                                 }
374
375                                 /* Update used ring with desc information */
376                                 vq->used->ring[cur_idx & (vq->size - 1)].id
377                                         = vq->buf_vec[vec_idx].desc_idx;
378                                 vq->used->ring[cur_idx & (vq->size - 1)].len
379                                         = entry_len;
380                                 entry_len = 0;
381                                 cur_idx++;
382                                 entry_success++;
383                                 seg_avail = 0;
384                                 cpy_len = RTE_MIN(vb_avail, seg_avail);
385                         }
386                 }
387         }
388
389         return entry_success;
390 }
391
392 /*
393  * This function works for mergeable RX.
394  */
395 static inline uint32_t __attribute__((always_inline))
396 virtio_dev_merge_rx(struct virtio_net *dev, uint16_t queue_id,
397         struct rte_mbuf **pkts, uint32_t count)
398 {
399         struct vhost_virtqueue *vq;
400         uint32_t pkt_idx = 0, entry_success = 0;
401         uint16_t avail_idx, res_cur_idx;
402         uint16_t res_base_idx, res_end_idx;
403         uint8_t success = 0;
404
405         LOG_DEBUG(VHOST_DATA, "(%"PRIu64") virtio_dev_merge_rx()\n",
406                 dev->device_fh);
407         if (unlikely(queue_id != VIRTIO_RXQ)) {
408                 LOG_DEBUG(VHOST_DATA, "mq isn't supported in this version.\n");
409         }
410
411         vq = dev->virtqueue[VIRTIO_RXQ];
412         count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
413
414         if (count == 0)
415                 return 0;
416
417         for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
418                 uint32_t secure_len = 0;
419                 uint16_t need_cnt;
420                 uint32_t vec_idx = 0;
421                 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + vq->vhost_hlen;
422                 uint16_t i, id;
423
424                 do {
425                         /*
426                          * As many data cores may want access to available
427                          * buffers, they need to be reserved.
428                          */
429                         res_base_idx = vq->last_used_idx_res;
430                         res_cur_idx = res_base_idx;
431
432                         do {
433                                 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
434                                 if (unlikely(res_cur_idx == avail_idx)) {
435                                         LOG_DEBUG(VHOST_DATA,
436                                                 "(%"PRIu64") Failed "
437                                                 "to get enough desc from "
438                                                 "vring\n",
439                                                 dev->device_fh);
440                                         return pkt_idx;
441                                 } else {
442                                         uint16_t wrapped_idx =
443                                                 (res_cur_idx) & (vq->size - 1);
444                                         uint32_t idx =
445                                                 vq->avail->ring[wrapped_idx];
446                                         uint8_t next_desc;
447
448                                         do {
449                                                 next_desc = 0;
450                                                 secure_len += vq->desc[idx].len;
451                                                 if (vq->desc[idx].flags &
452                                                         VRING_DESC_F_NEXT) {
453                                                         idx = vq->desc[idx].next;
454                                                         next_desc = 1;
455                                                 }
456                                         } while (next_desc);
457
458                                         res_cur_idx++;
459                                 }
460                         } while (pkt_len > secure_len);
461
462                         /* vq->last_used_idx_res is atomically updated. */
463                         success = rte_atomic16_cmpset(&vq->last_used_idx_res,
464                                                         res_base_idx,
465                                                         res_cur_idx);
466                 } while (success == 0);
467
468                 id = res_base_idx;
469                 need_cnt = res_cur_idx - res_base_idx;
470
471                 for (i = 0; i < need_cnt; i++, id++) {
472                         uint16_t wrapped_idx = id & (vq->size - 1);
473                         uint32_t idx = vq->avail->ring[wrapped_idx];
474                         uint8_t next_desc;
475                         do {
476                                 next_desc = 0;
477                                 vq->buf_vec[vec_idx].buf_addr =
478                                         vq->desc[idx].addr;
479                                 vq->buf_vec[vec_idx].buf_len =
480                                         vq->desc[idx].len;
481                                 vq->buf_vec[vec_idx].desc_idx = idx;
482                                 vec_idx++;
483
484                                 if (vq->desc[idx].flags & VRING_DESC_F_NEXT) {
485                                         idx = vq->desc[idx].next;
486                                         next_desc = 1;
487                                 }
488                         } while (next_desc);
489                 }
490
491                 res_end_idx = res_cur_idx;
492
493                 entry_success = copy_from_mbuf_to_vring(dev, res_base_idx,
494                         res_end_idx, pkts[pkt_idx]);
495
496                 rte_compiler_barrier();
497
498                 /*
499                  * Wait until it's our turn to add our buffer
500                  * to the used ring.
501                  */
502                 while (unlikely(vq->last_used_idx != res_base_idx))
503                         rte_pause();
504
505                 *(volatile uint16_t *)&vq->used->idx += entry_success;
506                 vq->last_used_idx = res_end_idx;
507
508                 /* Kick the guest if necessary. */
509                 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
510                         eventfd_write((int)vq->kickfd, 1);
511         }
512
513         return count;
514 }
515
516 uint16_t
517 rte_vhost_enqueue_burst(struct virtio_net *dev, uint16_t queue_id,
518         struct rte_mbuf **pkts, uint16_t count)
519 {
520         if (unlikely(dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)))
521                 return virtio_dev_merge_rx(dev, queue_id, pkts, count);
522         else
523                 return virtio_dev_rx(dev, queue_id, pkts, count);
524 }
525
526 uint16_t
527 rte_vhost_dequeue_burst(struct virtio_net *dev, uint16_t queue_id,
528         struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
529 {
530         struct rte_mbuf *m, *prev;
531         struct vhost_virtqueue *vq;
532         struct vring_desc *desc;
533         uint64_t vb_addr = 0;
534         uint32_t head[MAX_PKT_BURST];
535         uint32_t used_idx;
536         uint32_t i;
537         uint16_t free_entries, entry_success = 0;
538         uint16_t avail_idx;
539
540         if (unlikely(queue_id != VIRTIO_TXQ)) {
541                 LOG_DEBUG(VHOST_DATA, "mq isn't supported in this version.\n");
542                 return 0;
543         }
544
545         vq = dev->virtqueue[VIRTIO_TXQ];
546         avail_idx =  *((volatile uint16_t *)&vq->avail->idx);
547
548         /* If there are no available buffers then return. */
549         if (vq->last_used_idx == avail_idx)
550                 return 0;
551
552         LOG_DEBUG(VHOST_DATA, "%s (%"PRIu64")\n", __func__,
553                 dev->device_fh);
554
555         /* Prefetch available ring to retrieve head indexes. */
556         rte_prefetch0(&vq->avail->ring[vq->last_used_idx & (vq->size - 1)]);
557
558         /*get the number of free entries in the ring*/
559         free_entries = (avail_idx - vq->last_used_idx);
560
561         free_entries = RTE_MIN(free_entries, count);
562         /* Limit to MAX_PKT_BURST. */
563         free_entries = RTE_MIN(free_entries, MAX_PKT_BURST);
564
565         LOG_DEBUG(VHOST_DATA, "(%"PRIu64") Buffers available %d\n",
566                         dev->device_fh, free_entries);
567         /* Retrieve all of the head indexes first to avoid caching issues. */
568         for (i = 0; i < free_entries; i++)
569                 head[i] = vq->avail->ring[(vq->last_used_idx + i) & (vq->size - 1)];
570
571         /* Prefetch descriptor index. */
572         rte_prefetch0(&vq->desc[head[entry_success]]);
573         rte_prefetch0(&vq->used->ring[vq->last_used_idx & (vq->size - 1)]);
574
575         while (entry_success < free_entries) {
576                 uint32_t vb_avail, vb_offset;
577                 uint32_t seg_avail, seg_offset;
578                 uint32_t cpy_len;
579                 uint32_t seg_num = 0;
580                 struct rte_mbuf *cur;
581                 uint8_t alloc_err = 0;
582
583                 desc = &vq->desc[head[entry_success]];
584
585                 /* Discard first buffer as it is the virtio header */
586                 desc = &vq->desc[desc->next];
587
588                 /* Buffer address translation. */
589                 vb_addr = gpa_to_vva(dev, desc->addr);
590                 /* Prefetch buffer address. */
591                 rte_prefetch0((void *)(uintptr_t)vb_addr);
592
593                 used_idx = vq->last_used_idx & (vq->size - 1);
594
595                 if (entry_success < (free_entries - 1)) {
596                         /* Prefetch descriptor index. */
597                         rte_prefetch0(&vq->desc[head[entry_success+1]]);
598                         rte_prefetch0(&vq->used->ring[(used_idx + 1) & (vq->size - 1)]);
599                 }
600
601                 /* Update used index buffer information. */
602                 vq->used->ring[used_idx].id = head[entry_success];
603                 vq->used->ring[used_idx].len = 0;
604
605                 vb_offset = 0;
606                 vb_avail = desc->len;
607                 /* Allocate an mbuf and populate the structure. */
608                 m = rte_pktmbuf_alloc(mbuf_pool);
609                 if (unlikely(m == NULL)) {
610                         RTE_LOG(ERR, VHOST_DATA,
611                                 "Failed to allocate memory for mbuf.\n");
612                         return entry_success;
613                 }
614                 seg_offset = 0;
615                 seg_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
616                 cpy_len = RTE_MIN(vb_avail, seg_avail);
617
618                 PRINT_PACKET(dev, (uintptr_t)vb_addr, desc->len, 0);
619
620                 seg_num++;
621                 cur = m;
622                 prev = m;
623                 while (cpy_len != 0) {
624                         rte_memcpy((void *)(rte_pktmbuf_mtod(cur, char *) + seg_offset),
625                                 (void *)((uintptr_t)(vb_addr + vb_offset)),
626                                 cpy_len);
627
628                         seg_offset += cpy_len;
629                         vb_offset += cpy_len;
630                         vb_avail -= cpy_len;
631                         seg_avail -= cpy_len;
632
633                         if (vb_avail != 0) {
634                                 /*
635                                  * The segment reachs to its end,
636                                  * while the virtio buffer in TX vring has
637                                  * more data to be copied.
638                                  */
639                                 cur->data_len = seg_offset;
640                                 m->pkt_len += seg_offset;
641                                 /* Allocate mbuf and populate the structure. */
642                                 cur = rte_pktmbuf_alloc(mbuf_pool);
643                                 if (unlikely(cur == NULL)) {
644                                         RTE_LOG(ERR, VHOST_DATA, "Failed to "
645                                                 "allocate memory for mbuf.\n");
646                                         rte_pktmbuf_free(m);
647                                         alloc_err = 1;
648                                         break;
649                                 }
650
651                                 seg_num++;
652                                 prev->next = cur;
653                                 prev = cur;
654                                 seg_offset = 0;
655                                 seg_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
656                         } else {
657                                 if (desc->flags & VRING_DESC_F_NEXT) {
658                                         /*
659                                          * There are more virtio buffers in
660                                          * same vring entry need to be copied.
661                                          */
662                                         if (seg_avail == 0) {
663                                                 /*
664                                                  * The current segment hasn't
665                                                  * room to accomodate more
666                                                  * data.
667                                                  */
668                                                 cur->data_len = seg_offset;
669                                                 m->pkt_len += seg_offset;
670                                                 /*
671                                                  * Allocate an mbuf and
672                                                  * populate the structure.
673                                                  */
674                                                 cur = rte_pktmbuf_alloc(mbuf_pool);
675                                                 if (unlikely(cur == NULL)) {
676                                                         RTE_LOG(ERR,
677                                                                 VHOST_DATA,
678                                                                 "Failed to "
679                                                                 "allocate memory "
680                                                                 "for mbuf\n");
681                                                         rte_pktmbuf_free(m);
682                                                         alloc_err = 1;
683                                                         break;
684                                                 }
685                                                 seg_num++;
686                                                 prev->next = cur;
687                                                 prev = cur;
688                                                 seg_offset = 0;
689                                                 seg_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
690                                         }
691
692                                         desc = &vq->desc[desc->next];
693
694                                         /* Buffer address translation. */
695                                         vb_addr = gpa_to_vva(dev, desc->addr);
696                                         /* Prefetch buffer address. */
697                                         rte_prefetch0((void *)(uintptr_t)vb_addr);
698                                         vb_offset = 0;
699                                         vb_avail = desc->len;
700
701                                         PRINT_PACKET(dev, (uintptr_t)vb_addr,
702                                                 desc->len, 0);
703                                 } else {
704                                         /* The whole packet completes. */
705                                         cur->data_len = seg_offset;
706                                         m->pkt_len += seg_offset;
707                                         vb_avail = 0;
708                                 }
709                         }
710
711                         cpy_len = RTE_MIN(vb_avail, seg_avail);
712                 }
713
714                 if (unlikely(alloc_err == 1))
715                         break;
716
717                 m->nb_segs = seg_num;
718
719                 pkts[entry_success] = m;
720                 vq->last_used_idx++;
721                 entry_success++;
722         }
723
724         rte_compiler_barrier();
725         vq->used->idx += entry_success;
726         /* Kick guest if required. */
727         if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
728                 eventfd_write((int)vq->kickfd, 1);
729         return entry_success;
730 }