Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2010-2016 Intel Corporation
3 : : */
4 : :
5 : : #include <stdint.h>
6 : : #include <stdbool.h>
7 : : #include <linux/virtio_net.h>
8 : :
9 : : #include <rte_mbuf.h>
10 : : #include <rte_memcpy.h>
11 : : #include <rte_net.h>
12 : : #include <rte_ether.h>
13 : : #include <rte_ip.h>
14 : : #include <rte_dmadev.h>
15 : : #include <rte_vhost.h>
16 : : #include <rte_tcp.h>
17 : : #include <rte_udp.h>
18 : : #include <rte_sctp.h>
19 : : #include <rte_arp.h>
20 : : #include <rte_spinlock.h>
21 : : #include <rte_malloc.h>
22 : : #include <rte_vhost_async.h>
23 : :
24 : : #include "iotlb.h"
25 : : #include "vhost.h"
26 : :
27 : : #define MAX_BATCH_LEN 256
28 : :
29 : : static __rte_always_inline uint16_t
30 : : async_poll_dequeue_completed(struct virtio_net *dev, struct vhost_virtqueue *vq,
31 : : struct rte_mbuf **pkts, uint16_t count, int16_t dma_id,
32 : : uint16_t vchan_id, bool legacy_ol_flags);
33 : :
34 : : /* DMA device copy operation tracking array. */
35 : : struct async_dma_info dma_copy_track[RTE_DMADEV_DEFAULT_MAX];
36 : :
37 : : static __rte_always_inline bool
38 : : rxvq_is_mergeable(struct virtio_net *dev)
39 : : {
40 : 0 : return dev->features & (1ULL << VIRTIO_NET_F_MRG_RXBUF);
41 : : }
42 : :
43 : : static __rte_always_inline bool
44 : : virtio_net_is_inorder(struct virtio_net *dev)
45 : : {
46 : 0 : return dev->features & (1ULL << VIRTIO_F_IN_ORDER);
47 : : }
48 : :
49 : : static bool
50 : : is_valid_virt_queue_idx(uint32_t idx, int is_tx, uint32_t nr_vring)
51 : : {
52 [ # # # # : 0 : return (is_tx ^ (idx & 1)) == 0 && idx < nr_vring;
# # # # #
# ]
53 : : }
54 : :
55 : : static inline void
56 : 0 : vhost_queue_stats_update(const struct virtio_net *dev, struct vhost_virtqueue *vq,
57 : : struct rte_mbuf **pkts, uint16_t count)
58 : : __rte_shared_locks_required(&vq->access_lock)
59 : : {
60 : : struct virtqueue_stats *stats = &vq->stats;
61 : : int i;
62 : :
63 [ # # ]: 0 : if (!(dev->flags & VIRTIO_DEV_STATS_ENABLED))
64 : : return;
65 : :
66 [ # # ]: 0 : for (i = 0; i < count; i++) {
67 : : const struct rte_ether_addr *ea;
68 : 0 : const struct rte_mbuf *pkt = pkts[i];
69 : 0 : uint32_t pkt_len = rte_pktmbuf_pkt_len(pkt);
70 : :
71 : 0 : stats->packets++;
72 : 0 : stats->bytes += pkt_len;
73 : :
74 [ # # ]: 0 : if (pkt_len >= 1024)
75 [ # # ]: 0 : stats->size_bins[6 + (pkt_len > 1518)]++;
76 [ # # ]: 0 : else if (pkt_len <= 64)
77 : 0 : stats->size_bins[pkt_len >> 6]++;
78 : : else
79 : 0 : stats->size_bins[32UL - rte_clz32(pkt_len) - 5]++;
80 : :
81 [ # # ]: 0 : ea = rte_pktmbuf_mtod(pkt, const struct rte_ether_addr *);
82 : : RTE_BUILD_BUG_ON(offsetof(struct virtqueue_stats, broadcast) !=
83 : : offsetof(struct virtqueue_stats, multicast) + sizeof(uint64_t));
84 [ # # ]: 0 : if (unlikely(rte_is_multicast_ether_addr(ea)))
85 : 0 : (&stats->multicast)[rte_is_broadcast_ether_addr(ea)]++;
86 : : }
87 : : }
88 : :
89 : : static __rte_always_inline int64_t
90 : : vhost_async_dma_transfer_one(struct virtio_net *dev, struct vhost_virtqueue *vq,
91 : : int16_t dma_id, uint16_t vchan_id, uint16_t flag_idx,
92 : : struct vhost_iov_iter *pkt)
93 : : __rte_shared_locks_required(&vq->access_lock)
94 : : {
95 : 0 : struct async_dma_vchan_info *dma_info = &dma_copy_track[dma_id].vchans[vchan_id];
96 : 0 : uint16_t ring_mask = dma_info->ring_mask;
97 : : static bool vhost_async_dma_copy_log;
98 : :
99 : :
100 : 0 : struct vhost_iovec *iov = pkt->iov;
101 : : int copy_idx = 0;
102 : 0 : uint32_t nr_segs = pkt->nr_segs;
103 : : uint16_t i;
104 : :
105 [ # # # # : 0 : if (rte_dma_burst_capacity(dma_id, vchan_id) < nr_segs)
# # # # #
# # # ]
106 : : return -1;
107 : :
108 [ # # # # : 0 : for (i = 0; i < nr_segs; i++) {
# # # # #
# # # ]
109 : 0 : copy_idx = rte_dma_copy(dma_id, vchan_id, (rte_iova_t)iov[i].src_addr,
110 : 0 : (rte_iova_t)iov[i].dst_addr, iov[i].len, RTE_DMA_OP_FLAG_LLC);
111 : : /**
112 : : * Since all memory is pinned and DMA vChannel
113 : : * ring has enough space, failure should be a
114 : : * rare case. If failure happens, it means DMA
115 : : * device encounters serious errors; in this
116 : : * case, please stop async data-path and check
117 : : * what has happened to DMA device.
118 : : */
119 [ # # # # : 0 : if (unlikely(copy_idx < 0)) {
# # # # #
# # # ]
120 [ # # # # : 0 : if (!vhost_async_dma_copy_log) {
# # # # #
# # # ]
121 : 0 : VHOST_DATA_LOG(dev->ifname, ERR,
122 : : "DMA copy failed for channel %d:%u",
123 : : dma_id, vchan_id);
124 : 0 : vhost_async_dma_copy_log = true;
125 : : }
126 : : return -1;
127 : : }
128 : : }
129 : :
130 : : /**
131 : : * Only store packet completion flag address in the last copy's
132 : : * slot, and other slots are set to NULL.
133 : : */
134 : 0 : dma_info->pkts_cmpl_flag_addr[copy_idx & ring_mask] = &vq->async->pkts_cmpl_flag[flag_idx];
135 : :
136 : 0 : return nr_segs;
137 : : }
138 : :
139 : : static __rte_always_inline uint16_t
140 : : vhost_async_dma_transfer(struct virtio_net *dev, struct vhost_virtqueue *vq,
141 : : int16_t dma_id, uint16_t vchan_id, uint16_t head_idx,
142 : : struct vhost_iov_iter *pkts, uint16_t nr_pkts)
143 : : __rte_shared_locks_required(&vq->access_lock)
144 : : {
145 : 0 : struct async_dma_vchan_info *dma_info = &dma_copy_track[dma_id].vchans[vchan_id];
146 : : int64_t ret, nr_copies = 0;
147 : : uint16_t pkt_idx;
148 : :
149 : 0 : rte_spinlock_lock(&dma_info->dma_lock);
150 : :
151 [ # # # # : 0 : for (pkt_idx = 0; pkt_idx < nr_pkts; pkt_idx++) {
# # # # #
# # # ]
152 : 0 : ret = vhost_async_dma_transfer_one(dev, vq, dma_id, vchan_id, head_idx,
153 : 0 : &pkts[pkt_idx]);
154 [ # # # # : 0 : if (unlikely(ret < 0))
# # # # #
# # # ]
155 : : break;
156 : :
157 : 0 : nr_copies += ret;
158 : 0 : head_idx++;
159 [ # # # # : 0 : if (head_idx >= vq->size)
# # # # #
# # # ]
160 : 0 : head_idx -= vq->size;
161 : : }
162 : :
163 [ # # # # : 0 : if (likely(nr_copies > 0))
# # # # #
# # # ]
164 : : rte_dma_submit(dma_id, vchan_id);
165 : :
166 : : rte_spinlock_unlock(&dma_info->dma_lock);
167 : :
168 : : return pkt_idx;
169 : : }
170 : :
171 : : static __rte_always_inline uint16_t
172 : : vhost_async_dma_check_completed(struct virtio_net *dev, int16_t dma_id, uint16_t vchan_id,
173 : : uint16_t max_pkts)
174 : : {
175 : 0 : struct async_dma_vchan_info *dma_info = &dma_copy_track[dma_id].vchans[vchan_id];
176 : 0 : uint16_t ring_mask = dma_info->ring_mask;
177 : 0 : uint16_t last_idx = 0;
178 : : uint16_t nr_copies;
179 : : uint16_t copy_idx;
180 : : uint16_t i;
181 : 0 : bool has_error = false;
182 : : static bool vhost_async_dma_complete_log;
183 : :
184 : 0 : rte_spinlock_lock(&dma_info->dma_lock);
185 : :
186 : : /**
187 : : * Print error log for debugging, if DMA reports error during
188 : : * DMA transfer. We do not handle error in vhost level.
189 : : */
190 : : nr_copies = rte_dma_completed(dma_id, vchan_id, max_pkts, &last_idx, &has_error);
191 [ # # # # : 0 : if (unlikely(!vhost_async_dma_complete_log && has_error)) {
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ]
192 : 0 : VHOST_DATA_LOG(dev->ifname, ERR,
193 : : "DMA completion failure on channel %d:%u",
194 : : dma_id, vchan_id);
195 : 0 : vhost_async_dma_complete_log = true;
196 [ # # # # : 0 : } else if (nr_copies == 0) {
# # # # #
# # # # #
# # # # ]
197 : 0 : goto out;
198 : : }
199 : :
200 : 0 : copy_idx = last_idx - nr_copies + 1;
201 [ # # # # : 0 : for (i = 0; i < nr_copies; i++) {
# # # # #
# # # # #
# # # # ]
202 : : bool *flag;
203 : :
204 : 0 : flag = dma_info->pkts_cmpl_flag_addr[copy_idx & ring_mask];
205 [ # # # # : 0 : if (flag) {
# # # # #
# # # # #
# # # # ]
206 : : /**
207 : : * Mark the packet flag as received. The flag
208 : : * could belong to another virtqueue but write
209 : : * is atomic.
210 : : */
211 : 0 : *flag = true;
212 : 0 : dma_info->pkts_cmpl_flag_addr[copy_idx & ring_mask] = NULL;
213 : : }
214 : 0 : copy_idx++;
215 : : }
216 : :
217 : 0 : out:
218 : : rte_spinlock_unlock(&dma_info->dma_lock);
219 : : return nr_copies;
220 : : }
221 : :
222 : : static inline void
223 : 0 : do_data_copy_enqueue(struct virtio_net *dev, struct vhost_virtqueue *vq)
224 : : __rte_shared_locks_required(&vq->iotlb_lock)
225 : : {
226 : 0 : struct batch_copy_elem *elem = vq->batch_copy_elems;
227 : 0 : uint16_t count = vq->batch_copy_nb_elems;
228 : : int i;
229 : :
230 [ # # ]: 0 : for (i = 0; i < count; i++) {
231 [ # # ]: 0 : rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
232 : 0 : vhost_log_cache_write_iova(dev, vq, elem[i].log_addr,
233 [ # # ]: 0 : elem[i].len);
234 : : PRINT_PACKET(dev, (uintptr_t)elem[i].dst, elem[i].len, 0);
235 : : }
236 : :
237 : 0 : vq->batch_copy_nb_elems = 0;
238 : 0 : }
239 : :
240 : : static inline void
241 : 0 : do_data_copy_dequeue(struct vhost_virtqueue *vq)
242 : : {
243 : 0 : struct batch_copy_elem *elem = vq->batch_copy_elems;
244 : 0 : uint16_t count = vq->batch_copy_nb_elems;
245 : : int i;
246 : :
247 [ # # ]: 0 : for (i = 0; i < count; i++)
248 [ # # ]: 0 : rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
249 : :
250 : 0 : vq->batch_copy_nb_elems = 0;
251 : 0 : }
252 : :
253 : : static __rte_always_inline void
254 : : do_flush_shadow_used_ring_split(struct virtio_net *dev,
255 : : struct vhost_virtqueue *vq,
256 : : uint16_t to, uint16_t from, uint16_t size)
257 : : {
258 : 0 : rte_memcpy(&vq->used->ring[to],
259 [ # # # # : 0 : &vq->shadow_used_split[from],
# # ]
260 : : size * sizeof(struct vring_used_elem));
261 [ # # # # : 0 : vhost_log_cache_used_vring(dev, vq,
# # # # #
# # # ]
262 : : offsetof(struct vring_used, ring[to]),
263 : : size * sizeof(struct vring_used_elem));
264 : : }
265 : :
266 : : static __rte_always_inline void
267 : : flush_shadow_used_ring_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
268 : : {
269 : 0 : uint16_t used_idx = vq->last_used_idx & (vq->size - 1);
270 : :
271 [ # # # # : 0 : if (used_idx + vq->shadow_used_idx <= vq->size) {
# # ]
272 : : do_flush_shadow_used_ring_split(dev, vq, used_idx, 0,
273 : : vq->shadow_used_idx);
274 : : } else {
275 : : uint16_t size;
276 : :
277 : : /* update used ring interval [used_idx, vq->size] */
278 [ # # # # : 0 : size = vq->size - used_idx;
# # ]
279 : : do_flush_shadow_used_ring_split(dev, vq, used_idx, 0, size);
280 : :
281 : : /* update the left half used ring interval [0, left_size] */
282 : : do_flush_shadow_used_ring_split(dev, vq, 0, size,
283 [ # # # # : 0 : vq->shadow_used_idx - size);
# # ]
284 : : }
285 [ # # # # : 0 : vq->last_used_idx += vq->shadow_used_idx;
# # ]
286 : :
287 : : vhost_log_cache_sync(dev, vq);
288 : :
289 : 0 : rte_atomic_fetch_add_explicit((unsigned short __rte_atomic *)&vq->used->idx,
290 : : vq->shadow_used_idx, rte_memory_order_release);
291 [ # # # # : 0 : vq->shadow_used_idx = 0;
# # ]
292 : : vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
293 : : sizeof(vq->used->idx));
294 : : }
295 : :
296 : : static __rte_always_inline void
297 : : update_shadow_used_ring_split(struct vhost_virtqueue *vq,
298 : : uint16_t desc_idx, uint32_t len)
299 : : {
300 : 0 : uint16_t i = vq->shadow_used_idx++;
301 : :
302 : 0 : vq->shadow_used_split[i].id = desc_idx;
303 : 0 : vq->shadow_used_split[i].len = len;
304 : : }
305 : :
306 : : static __rte_always_inline void
307 : : vhost_flush_enqueue_shadow_packed(struct virtio_net *dev,
308 : : struct vhost_virtqueue *vq)
309 : : {
310 : : int i;
311 : 0 : uint16_t used_idx = vq->last_used_idx;
312 : : uint16_t head_idx = vq->last_used_idx;
313 : : uint16_t head_flags = 0;
314 : :
315 : : /* Split loop in two to save memory barriers */
316 [ # # # # : 0 : for (i = 0; i < vq->shadow_used_idx; i++) {
# # ]
317 : 0 : vq->desc_packed[used_idx].id = vq->shadow_used_packed[i].id;
318 : 0 : vq->desc_packed[used_idx].len = vq->shadow_used_packed[i].len;
319 : :
320 : 0 : used_idx += vq->shadow_used_packed[i].count;
321 [ # # # # : 0 : if (used_idx >= vq->size)
# # ]
322 : 0 : used_idx -= vq->size;
323 : : }
324 : :
325 : : /* The ordering for storing desc flags needs to be enforced. */
326 : : rte_atomic_thread_fence(rte_memory_order_release);
327 : :
328 [ # # # # : 0 : for (i = 0; i < vq->shadow_used_idx; i++) {
# # ]
329 : : uint16_t flags;
330 : :
331 [ # # # # : 0 : if (vq->shadow_used_packed[i].len)
# # ]
332 : : flags = VRING_DESC_F_WRITE;
333 : : else
334 : : flags = 0;
335 : :
336 [ # # # # : 0 : if (vq->used_wrap_counter) {
# # ]
337 : : flags |= VRING_DESC_F_USED;
338 : 0 : flags |= VRING_DESC_F_AVAIL;
339 : : } else {
340 : : flags &= ~VRING_DESC_F_USED;
341 : : flags &= ~VRING_DESC_F_AVAIL;
342 : : }
343 : :
344 [ # # # # : 0 : if (i > 0) {
# # ]
345 : 0 : vq->desc_packed[vq->last_used_idx].flags = flags;
346 : :
347 : 0 : vhost_log_cache_used_vring(dev, vq,
348 [ # # # # : 0 : vq->last_used_idx *
# # ]
349 : : sizeof(struct vring_packed_desc),
350 : : sizeof(struct vring_packed_desc));
351 : : } else {
352 : 0 : head_idx = vq->last_used_idx;
353 : : head_flags = flags;
354 : : }
355 : :
356 [ # # # # : 0 : vq_inc_last_used_packed(vq, vq->shadow_used_packed[i].count);
# # ]
357 : : }
358 : :
359 [ # # # # : 0 : vq->desc_packed[head_idx].flags = head_flags;
# # ]
360 : :
361 : : vhost_log_cache_used_vring(dev, vq,
362 : : head_idx *
363 : : sizeof(struct vring_packed_desc),
364 : : sizeof(struct vring_packed_desc));
365 : :
366 [ # # # # : 0 : vq->shadow_used_idx = 0;
# # ]
367 : : vhost_log_cache_sync(dev, vq);
368 : : }
369 : :
370 : : static __rte_always_inline void
371 : : vhost_flush_dequeue_shadow_packed(struct virtio_net *dev,
372 : : struct vhost_virtqueue *vq)
373 : : {
374 : 0 : struct vring_used_elem_packed *used_elem = &vq->shadow_used_packed[0];
375 : :
376 : 0 : vq->desc_packed[vq->shadow_last_used_idx].id = used_elem->id;
377 : : /* desc flags is the synchronization point for virtio packed vring */
378 : 0 : rte_atomic_store_explicit(
379 : : (unsigned short __rte_atomic *)&vq->desc_packed[vq->shadow_last_used_idx].flags,
380 : : used_elem->flags, rte_memory_order_release);
381 : :
382 [ # # # # ]: 0 : vhost_log_cache_used_vring(dev, vq, vq->shadow_last_used_idx *
383 : : sizeof(struct vring_packed_desc),
384 : : sizeof(struct vring_packed_desc));
385 [ # # # # ]: 0 : vq->shadow_used_idx = 0;
386 : : vhost_log_cache_sync(dev, vq);
387 : : }
388 : :
389 : : static __rte_always_inline void
390 : : vhost_flush_enqueue_batch_packed(struct virtio_net *dev,
391 : : struct vhost_virtqueue *vq,
392 : : uint64_t *lens,
393 : : uint16_t *ids)
394 : : {
395 : : uint16_t i;
396 : : uint16_t flags;
397 : : uint16_t last_used_idx;
398 : : struct vring_packed_desc *desc_base;
399 : :
400 : 0 : last_used_idx = vq->last_used_idx;
401 : 0 : desc_base = &vq->desc_packed[last_used_idx];
402 : :
403 [ # # ]: 0 : flags = PACKED_DESC_ENQUEUE_USED_FLAG(vq->used_wrap_counter);
404 : :
405 [ # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
406 : 0 : desc_base[i].id = ids[i];
407 : 0 : desc_base[i].len = lens[i];
408 : : }
409 : :
410 : : rte_atomic_thread_fence(rte_memory_order_release);
411 : :
412 [ # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
413 : 0 : desc_base[i].flags = flags;
414 : : }
415 : :
416 : : vhost_log_cache_used_vring(dev, vq, last_used_idx *
417 : : sizeof(struct vring_packed_desc),
418 : : sizeof(struct vring_packed_desc) *
419 : : PACKED_BATCH_SIZE);
420 : : vhost_log_cache_sync(dev, vq);
421 : :
422 : : vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
423 : : }
424 : :
425 : : static __rte_always_inline void
426 : : vhost_async_shadow_enqueue_packed_batch(struct vhost_virtqueue *vq,
427 : : uint64_t *lens,
428 : : uint16_t *ids)
429 : : __rte_exclusive_locks_required(&vq->access_lock)
430 : : {
431 : : uint16_t i;
432 : 0 : struct vhost_async *async = vq->async;
433 : :
434 [ # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
435 : 0 : async->buffers_packed[async->buffer_idx_packed].id = ids[i];
436 : 0 : async->buffers_packed[async->buffer_idx_packed].len = lens[i];
437 : 0 : async->buffers_packed[async->buffer_idx_packed].count = 1;
438 : 0 : async->buffer_idx_packed++;
439 [ # # ]: 0 : if (async->buffer_idx_packed >= vq->size)
440 : 0 : async->buffer_idx_packed -= vq->size;
441 : : }
442 : : }
443 : :
444 : : static __rte_always_inline void
445 : : vhost_async_shadow_dequeue_packed_batch(struct vhost_virtqueue *vq, uint16_t *ids)
446 : : __rte_shared_locks_required(&vq->access_lock)
447 : : {
448 : : uint16_t i;
449 : 0 : struct vhost_async *async = vq->async;
450 : :
451 [ # # # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
452 : 0 : async->buffers_packed[async->buffer_idx_packed].id = ids[i];
453 : 0 : async->buffers_packed[async->buffer_idx_packed].len = 0;
454 : 0 : async->buffers_packed[async->buffer_idx_packed].count = 1;
455 : :
456 : 0 : async->buffer_idx_packed++;
457 [ # # # # ]: 0 : if (async->buffer_idx_packed >= vq->size)
458 : 0 : async->buffer_idx_packed -= vq->size;
459 : : }
460 : : }
461 : :
462 : : static __rte_always_inline void
463 : : vhost_shadow_dequeue_batch_packed_inorder(struct vhost_virtqueue *vq,
464 : : uint16_t id)
465 : : {
466 : 0 : vq->shadow_used_packed[0].id = id;
467 : :
468 : 0 : if (!vq->shadow_used_idx) {
469 : 0 : vq->shadow_last_used_idx = vq->last_used_idx;
470 : 0 : vq->shadow_used_packed[0].flags =
471 [ # # # # ]: 0 : PACKED_DESC_DEQUEUE_USED_FLAG(vq->used_wrap_counter);
472 : 0 : vq->shadow_used_packed[0].len = 0;
473 : 0 : vq->shadow_used_packed[0].count = 1;
474 : 0 : vq->shadow_used_idx++;
475 : : }
476 : :
477 : : vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
478 : : }
479 : :
480 : : static __rte_always_inline void
481 : : vhost_shadow_dequeue_batch_packed(struct virtio_net *dev,
482 : : struct vhost_virtqueue *vq,
483 : : uint16_t *ids)
484 : : {
485 : : uint16_t flags;
486 : : uint16_t i;
487 : : uint16_t begin;
488 : :
489 [ # # # # ]: 0 : flags = PACKED_DESC_DEQUEUE_USED_FLAG(vq->used_wrap_counter);
490 : :
491 [ # # # # ]: 0 : if (!vq->shadow_used_idx) {
492 : 0 : vq->shadow_last_used_idx = vq->last_used_idx;
493 : 0 : vq->shadow_used_packed[0].id = ids[0];
494 : 0 : vq->shadow_used_packed[0].len = 0;
495 : 0 : vq->shadow_used_packed[0].count = 1;
496 : 0 : vq->shadow_used_packed[0].flags = flags;
497 : 0 : vq->shadow_used_idx++;
498 : : begin = 1;
499 : : } else
500 : : begin = 0;
501 : :
502 [ # # # # ]: 0 : vhost_for_each_try_unroll(i, begin, PACKED_BATCH_SIZE) {
503 : 0 : vq->desc_packed[vq->last_used_idx + i].id = ids[i];
504 : 0 : vq->desc_packed[vq->last_used_idx + i].len = 0;
505 : : }
506 : :
507 : : rte_atomic_thread_fence(rte_memory_order_release);
508 [ # # # # ]: 0 : vhost_for_each_try_unroll(i, begin, PACKED_BATCH_SIZE)
509 : 0 : vq->desc_packed[vq->last_used_idx + i].flags = flags;
510 : :
511 [ # # # # ]: 0 : vhost_log_cache_used_vring(dev, vq, vq->last_used_idx *
512 : : sizeof(struct vring_packed_desc),
513 : : sizeof(struct vring_packed_desc) *
514 : : PACKED_BATCH_SIZE);
515 : : vhost_log_cache_sync(dev, vq);
516 : :
517 : : vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
518 : : }
519 : :
520 : : static __rte_always_inline void
521 : : vhost_shadow_dequeue_single_packed(struct vhost_virtqueue *vq,
522 : : uint16_t buf_id,
523 : : uint16_t count)
524 : : {
525 : : uint16_t flags;
526 : :
527 : 0 : flags = vq->desc_packed[vq->last_used_idx].flags;
528 [ # # # # ]: 0 : if (vq->used_wrap_counter) {
529 : : flags |= VRING_DESC_F_USED;
530 : 0 : flags |= VRING_DESC_F_AVAIL;
531 : : } else {
532 : : flags &= ~VRING_DESC_F_USED;
533 : 0 : flags &= ~VRING_DESC_F_AVAIL;
534 : : }
535 : :
536 [ # # # # ]: 0 : if (!vq->shadow_used_idx) {
537 : 0 : vq->shadow_last_used_idx = vq->last_used_idx;
538 : :
539 : 0 : vq->shadow_used_packed[0].id = buf_id;
540 : 0 : vq->shadow_used_packed[0].len = 0;
541 : 0 : vq->shadow_used_packed[0].flags = flags;
542 : 0 : vq->shadow_used_idx++;
543 : : } else {
544 : 0 : vq->desc_packed[vq->last_used_idx].id = buf_id;
545 : 0 : vq->desc_packed[vq->last_used_idx].len = 0;
546 : 0 : vq->desc_packed[vq->last_used_idx].flags = flags;
547 : : }
548 : :
549 : : vq_inc_last_used_packed(vq, count);
550 : : }
551 : :
552 : : static __rte_always_inline void
553 : : vhost_shadow_dequeue_single_packed_inorder(struct vhost_virtqueue *vq,
554 : : uint16_t buf_id,
555 : : uint16_t count)
556 : : {
557 : : uint16_t flags;
558 : :
559 : 0 : vq->shadow_used_packed[0].id = buf_id;
560 : :
561 : 0 : flags = vq->desc_packed[vq->last_used_idx].flags;
562 [ # # # # ]: 0 : if (vq->used_wrap_counter) {
563 : : flags |= VRING_DESC_F_USED;
564 : 0 : flags |= VRING_DESC_F_AVAIL;
565 : : } else {
566 : : flags &= ~VRING_DESC_F_USED;
567 : 0 : flags &= ~VRING_DESC_F_AVAIL;
568 : : }
569 : :
570 [ # # # # ]: 0 : if (!vq->shadow_used_idx) {
571 : 0 : vq->shadow_last_used_idx = vq->last_used_idx;
572 : 0 : vq->shadow_used_packed[0].len = 0;
573 : 0 : vq->shadow_used_packed[0].flags = flags;
574 : 0 : vq->shadow_used_idx++;
575 : : }
576 : :
577 : : vq_inc_last_used_packed(vq, count);
578 : : }
579 : :
580 : : static __rte_always_inline void
581 : : vhost_shadow_enqueue_packed(struct vhost_virtqueue *vq,
582 : : uint32_t *len,
583 : : uint16_t *id,
584 : : uint16_t *count,
585 : : uint16_t num_buffers)
586 : : {
587 : : uint16_t i;
588 : :
589 [ # # ]: 0 : for (i = 0; i < num_buffers; i++) {
590 : : /* enqueue shadow flush action aligned with batch num */
591 [ # # ]: 0 : if (!vq->shadow_used_idx)
592 : 0 : vq->shadow_aligned_idx = vq->last_used_idx &
593 : : PACKED_BATCH_MASK;
594 : 0 : vq->shadow_used_packed[vq->shadow_used_idx].id = id[i];
595 : 0 : vq->shadow_used_packed[vq->shadow_used_idx].len = len[i];
596 : 0 : vq->shadow_used_packed[vq->shadow_used_idx].count = count[i];
597 : 0 : vq->shadow_aligned_idx += count[i];
598 : 0 : vq->shadow_used_idx++;
599 : : }
600 : : }
601 : :
602 : : static __rte_always_inline void
603 : : vhost_async_shadow_enqueue_packed(struct vhost_virtqueue *vq,
604 : : uint32_t *len,
605 : : uint16_t *id,
606 : : uint16_t *count,
607 : : uint16_t num_buffers)
608 : : __rte_exclusive_locks_required(&vq->access_lock)
609 : : {
610 : : uint16_t i;
611 : 0 : struct vhost_async *async = vq->async;
612 : :
613 [ # # ]: 0 : for (i = 0; i < num_buffers; i++) {
614 : 0 : async->buffers_packed[async->buffer_idx_packed].id = id[i];
615 : 0 : async->buffers_packed[async->buffer_idx_packed].len = len[i];
616 : 0 : async->buffers_packed[async->buffer_idx_packed].count = count[i];
617 : 0 : async->buffer_idx_packed++;
618 [ # # ]: 0 : if (async->buffer_idx_packed >= vq->size)
619 : 0 : async->buffer_idx_packed -= vq->size;
620 : : }
621 : : }
622 : :
623 : : static __rte_always_inline void
624 : : vhost_shadow_enqueue_single_packed(struct virtio_net *dev,
625 : : struct vhost_virtqueue *vq,
626 : : uint32_t *len,
627 : : uint16_t *id,
628 : : uint16_t *count,
629 : : uint16_t num_buffers)
630 : : __rte_shared_locks_required(&vq->iotlb_lock)
631 : : {
632 : : vhost_shadow_enqueue_packed(vq, len, id, count, num_buffers);
633 : :
634 [ # # ]: 0 : if (vq->shadow_aligned_idx >= PACKED_BATCH_SIZE) {
635 : 0 : do_data_copy_enqueue(dev, vq);
636 : : vhost_flush_enqueue_shadow_packed(dev, vq);
637 : : }
638 : : }
639 : :
640 : : /* avoid write operation when necessary, to lessen cache issues */
641 : : #define ASSIGN_UNLESS_EQUAL(var, val) do { \
642 : : if ((var) != (val)) \
643 : : (var) = (val); \
644 : : } while (0)
645 : :
646 : : static __rte_always_inline void
647 : : virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
648 : : {
649 : 0 : uint64_t csum_l4 = m_buf->ol_flags & RTE_MBUF_F_TX_L4_MASK;
650 : :
651 [ # # # # : 0 : if (m_buf->ol_flags & RTE_MBUF_F_TX_TCP_SEG)
# # # # ]
652 : 0 : csum_l4 |= RTE_MBUF_F_TX_TCP_CKSUM;
653 : :
654 [ # # # # : 0 : if (csum_l4) {
# # # # #
# # # ]
655 : : /*
656 : : * Pseudo-header checksum must be set as per Virtio spec.
657 : : *
658 : : * Note: We don't propagate rte_net_intel_cksum_prepare()
659 : : * errors, as it would have an impact on performance, and an
660 : : * error would mean the packet is dropped by the guest instead
661 : : * of being dropped here.
662 : : */
663 : 0 : rte_net_intel_cksum_prepare(m_buf);
664 : :
665 : 0 : net_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
666 : 0 : net_hdr->csum_start = m_buf->l2_len + m_buf->l3_len;
667 : :
668 [ # # # # : 0 : switch (csum_l4) {
# # # # #
# # # # #
# # # # #
# # # #
# ]
669 : 0 : case RTE_MBUF_F_TX_TCP_CKSUM:
670 : 0 : net_hdr->csum_offset = (offsetof(struct rte_tcp_hdr,
671 : : cksum));
672 : 0 : break;
673 : 0 : case RTE_MBUF_F_TX_UDP_CKSUM:
674 : 0 : net_hdr->csum_offset = (offsetof(struct rte_udp_hdr,
675 : : dgram_cksum));
676 : 0 : break;
677 : 0 : case RTE_MBUF_F_TX_SCTP_CKSUM:
678 : 0 : net_hdr->csum_offset = (offsetof(struct rte_sctp_hdr,
679 : : cksum));
680 : 0 : break;
681 : : }
682 : : } else {
683 [ # # # # : 0 : ASSIGN_UNLESS_EQUAL(net_hdr->csum_start, 0);
# # # # #
# # # ]
684 [ # # # # : 0 : ASSIGN_UNLESS_EQUAL(net_hdr->csum_offset, 0);
# # # # #
# # # ]
685 [ # # # # : 0 : ASSIGN_UNLESS_EQUAL(net_hdr->flags, 0);
# # # # #
# # # ]
686 : : }
687 : :
688 : : /* IP cksum verification cannot be bypassed, then calculate here */
689 [ # # # # : 0 : if (m_buf->ol_flags & RTE_MBUF_F_TX_IP_CKSUM) {
# # # # #
# # # ]
690 : : struct rte_ipv4_hdr *ipv4_hdr;
691 : :
692 : 0 : ipv4_hdr = rte_pktmbuf_mtod_offset(m_buf, struct rte_ipv4_hdr *,
693 : : m_buf->l2_len);
694 : 0 : ipv4_hdr->hdr_checksum = 0;
695 : 0 : ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
696 : : }
697 : :
698 [ # # # # : 0 : if (m_buf->ol_flags & RTE_MBUF_F_TX_TCP_SEG) {
# # # # #
# # # ]
699 [ # # # # : 0 : if (m_buf->ol_flags & RTE_MBUF_F_TX_IPV4)
# # # # #
# # # ]
700 : 0 : net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
701 : : else
702 : 0 : net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
703 : 0 : net_hdr->gso_size = m_buf->tso_segsz;
704 : 0 : net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len
705 : 0 : + m_buf->l4_len;
706 [ # # # # : 0 : } else if (m_buf->ol_flags & RTE_MBUF_F_TX_UDP_SEG) {
# # # # #
# # # ]
707 : 0 : net_hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
708 : 0 : net_hdr->gso_size = m_buf->tso_segsz;
709 : 0 : net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len +
710 : 0 : m_buf->l4_len;
711 : : } else {
712 [ # # # # : 0 : ASSIGN_UNLESS_EQUAL(net_hdr->gso_type, 0);
# # # # #
# # # ]
713 [ # # # # : 0 : ASSIGN_UNLESS_EQUAL(net_hdr->gso_size, 0);
# # # # #
# # # ]
714 [ # # # # : 0 : ASSIGN_UNLESS_EQUAL(net_hdr->hdr_len, 0);
# # # # #
# # # ]
715 : : }
716 : : }
717 : :
718 : : static __rte_always_inline int
719 : : map_one_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
720 : : struct buf_vector *buf_vec, uint16_t *vec_idx,
721 : : uint64_t desc_iova, uint64_t desc_len, uint8_t perm)
722 : : __rte_shared_locks_required(&vq->iotlb_lock)
723 : : {
724 : : uint16_t vec_id = *vec_idx;
725 : :
726 [ # # # # : 0 : while (desc_len) {
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ]
727 : : uint64_t desc_addr;
728 : 0 : uint64_t desc_chunck_len = desc_len;
729 : :
730 [ # # # # : 0 : if (unlikely(vec_id >= BUF_VECTOR_MAX))
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ]
731 : 0 : return -1;
732 : :
733 : : desc_addr = vhost_iova_to_vva(dev, vq,
734 : : desc_iova,
735 : : &desc_chunck_len,
736 : : perm);
737 [ # # # # : 0 : if (unlikely(!desc_addr))
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ]
738 : : return -1;
739 : :
740 : 0 : rte_prefetch0((void *)(uintptr_t)desc_addr);
741 : :
742 : 0 : buf_vec[vec_id].buf_iova = desc_iova;
743 : 0 : buf_vec[vec_id].buf_addr = desc_addr;
744 : 0 : buf_vec[vec_id].buf_len = desc_chunck_len;
745 : :
746 : 0 : desc_len -= desc_chunck_len;
747 : 0 : desc_iova += desc_chunck_len;
748 : 0 : vec_id++;
749 : : }
750 : : *vec_idx = vec_id;
751 : :
752 : : return 0;
753 : : }
754 : :
755 : : static __rte_always_inline int
756 : : fill_vec_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
757 : : uint32_t avail_idx, uint16_t *vec_idx,
758 : : struct buf_vector *buf_vec, uint16_t *desc_chain_head,
759 : : uint32_t *desc_chain_len, uint8_t perm)
760 : : __rte_shared_locks_required(&vq->iotlb_lock)
761 : : {
762 : 0 : uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)];
763 : : uint16_t vec_id = *vec_idx;
764 : : uint32_t len = 0;
765 : : uint64_t dlen;
766 : 0 : uint32_t nr_descs = vq->size;
767 : : uint32_t cnt = 0;
768 : 0 : struct vring_desc *descs = vq->desc;
769 : : struct vring_desc *idesc = NULL;
770 : :
771 : 0 : if (unlikely(idx >= vq->size))
772 : : return -1;
773 : :
774 : : *desc_chain_head = idx;
775 : :
776 [ # # # # : 0 : if (vq->desc[idx].flags & VRING_DESC_F_INDIRECT) {
# # # # #
# ]
777 : 0 : dlen = vq->desc[idx].len;
778 : 0 : nr_descs = dlen / sizeof(struct vring_desc);
779 [ # # # # : 0 : if (unlikely(nr_descs > vq->size))
# # # # #
# ]
780 : : return -1;
781 : :
782 : 0 : descs = (struct vring_desc *)(uintptr_t)
783 [ # # # # : 0 : vhost_iova_to_vva(dev, vq, vq->desc[idx].addr,
# # # # #
# ]
784 : : &dlen,
785 : : VHOST_ACCESS_RO);
786 [ # # # # : 0 : if (unlikely(!descs))
# # # # #
# ]
787 : : return -1;
788 : :
789 [ # # # # : 0 : if (unlikely(dlen < vq->desc[idx].len)) {
# # # # #
# ]
790 : : /*
791 : : * The indirect desc table is not contiguous
792 : : * in process VA space, we have to copy it.
793 : : */
794 : 0 : idesc = vhost_alloc_copy_ind_table(dev, vq,
795 : 0 : vq->desc[idx].addr, vq->desc[idx].len);
796 [ # # # # : 0 : if (unlikely(!idesc))
# # # # #
# ]
797 : : return -1;
798 : :
799 : : descs = idesc;
800 : : }
801 : :
802 : : idx = 0;
803 : : }
804 : :
805 : : while (1) {
806 [ # # # # : 0 : if (unlikely(idx >= nr_descs || cnt++ >= nr_descs)) {
# # # # #
# # # # #
# # # # #
# ]
807 : : free_ind_table(idesc);
808 : 0 : return -1;
809 : : }
810 : :
811 : 0 : dlen = descs[idx].len;
812 : 0 : len += dlen;
813 : :
814 [ # # # # : 0 : if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
# # # # #
# ]
815 : : descs[idx].addr, dlen,
816 : : perm))) {
817 : : free_ind_table(idesc);
818 : 0 : return -1;
819 : : }
820 : :
821 [ # # # # : 0 : if ((descs[idx].flags & VRING_DESC_F_NEXT) == 0)
# # # # #
# ]
822 : : break;
823 : :
824 : 0 : idx = descs[idx].next;
825 : : }
826 : :
827 : : *desc_chain_len = len;
828 : : *vec_idx = vec_id;
829 : :
830 [ # # # # : 0 : if (unlikely(!!idesc))
# # # # #
# ]
831 : : free_ind_table(idesc);
832 : :
833 : : return 0;
834 : : }
835 : :
836 : : /*
837 : : * Returns -1 on fail, 0 on success
838 : : */
839 : : static inline int
840 : 0 : reserve_avail_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
841 : : uint64_t size, struct buf_vector *buf_vec,
842 : : uint16_t *num_buffers, uint16_t avail_head,
843 : : uint16_t *nr_vec)
844 : : __rte_shared_locks_required(&vq->iotlb_lock)
845 : : {
846 : : uint16_t cur_idx;
847 : : uint16_t vec_idx = 0;
848 : : uint16_t max_tries, tries = 0;
849 : :
850 : : uint16_t head_idx = 0;
851 : : uint32_t len = 0;
852 : :
853 : 0 : *num_buffers = 0;
854 : 0 : cur_idx = vq->last_avail_idx;
855 : :
856 [ # # ]: 0 : if (rxvq_is_mergeable(dev))
857 : 0 : max_tries = vq->size - 1;
858 : : else
859 : : max_tries = 1;
860 : :
861 [ # # ]: 0 : while (size > 0) {
862 [ # # ]: 0 : if (unlikely(cur_idx == avail_head))
863 : : return -1;
864 : : /*
865 : : * if we tried all available ring items, and still
866 : : * can't get enough buf, it means something abnormal
867 : : * happened.
868 : : */
869 [ # # ]: 0 : if (unlikely(++tries > max_tries))
870 : : return -1;
871 : :
872 [ # # # # ]: 0 : if (unlikely(fill_vec_buf_split(dev, vq, cur_idx,
873 : : &vec_idx, buf_vec,
874 : : &head_idx, &len,
875 : : VHOST_ACCESS_RW) < 0))
876 : : return -1;
877 : 0 : len = RTE_MIN(len, size);
878 : : update_shadow_used_ring_split(vq, head_idx, len);
879 : 0 : size -= len;
880 : :
881 : 0 : cur_idx++;
882 : 0 : *num_buffers += 1;
883 : : }
884 : :
885 : 0 : *nr_vec = vec_idx;
886 : :
887 : 0 : return 0;
888 : : }
889 : :
890 : : static __rte_always_inline int
891 : : fill_vec_buf_packed_indirect(struct virtio_net *dev,
892 : : struct vhost_virtqueue *vq,
893 : : struct vring_packed_desc *desc, uint16_t *vec_idx,
894 : : struct buf_vector *buf_vec, uint32_t *len, uint8_t perm)
895 : : __rte_shared_locks_required(&vq->iotlb_lock)
896 : : {
897 : : uint16_t i;
898 : : uint32_t nr_descs;
899 : : uint16_t vec_id = *vec_idx;
900 : : uint64_t dlen;
901 : : struct vring_packed_desc *descs, *idescs = NULL;
902 : :
903 : 0 : dlen = desc->len;
904 : 0 : descs = (struct vring_packed_desc *)(uintptr_t)
905 [ # # # # : 0 : vhost_iova_to_vva(dev, vq, desc->addr, &dlen, VHOST_ACCESS_RO);
# # # # #
# # # ]
906 [ # # # # : 0 : if (unlikely(!descs))
# # # # #
# # # ]
907 : : return -1;
908 : :
909 [ # # # # : 0 : if (unlikely(dlen < desc->len)) {
# # # # #
# # # ]
910 : : /*
911 : : * The indirect desc table is not contiguous
912 : : * in process VA space, we have to copy it.
913 : : */
914 : 0 : idescs = vhost_alloc_copy_ind_table(dev,
915 : 0 : vq, desc->addr, desc->len);
916 [ # # # # : 0 : if (unlikely(!idescs))
# # # # #
# # # ]
917 : : return -1;
918 : :
919 : : descs = idescs;
920 : : }
921 : :
922 : 0 : nr_descs = desc->len / sizeof(struct vring_packed_desc);
923 [ # # # # : 0 : if (unlikely(nr_descs >= vq->size)) {
# # # # #
# # # ]
924 : : free_ind_table(idescs);
925 : 0 : return -1;
926 : : }
927 : :
928 [ # # # # : 0 : for (i = 0; i < nr_descs; i++) {
# # # # #
# # # ]
929 [ # # # # : 0 : if (unlikely(vec_id >= BUF_VECTOR_MAX)) {
# # # # #
# # # ]
930 : : free_ind_table(idescs);
931 : 0 : return -1;
932 : : }
933 : :
934 : 0 : dlen = descs[i].len;
935 : 0 : *len += dlen;
936 [ # # # # : 0 : if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
# # # # #
# # # ]
937 : : descs[i].addr, dlen,
938 : : perm)))
939 : : return -1;
940 : : }
941 : : *vec_idx = vec_id;
942 : :
943 [ # # # # : 0 : if (unlikely(!!idescs))
# # # # #
# # # ]
944 : : free_ind_table(idescs);
945 : :
946 : : return 0;
947 : : }
948 : :
949 : : static __rte_always_inline int
950 : : fill_vec_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
951 : : uint16_t avail_idx, uint16_t *desc_count,
952 : : struct buf_vector *buf_vec, uint16_t *vec_idx,
953 : : uint16_t *buf_id, uint32_t *len, uint8_t perm)
954 : : __rte_shared_locks_required(&vq->iotlb_lock)
955 : : {
956 : 0 : bool wrap_counter = vq->avail_wrap_counter;
957 : 0 : struct vring_packed_desc *descs = vq->desc_packed;
958 : : uint16_t vec_id = *vec_idx;
959 : : uint64_t dlen;
960 : :
961 [ # # # # ]: 0 : if (avail_idx < vq->last_avail_idx)
962 : 0 : wrap_counter ^= 1;
963 : :
964 : : /*
965 : : * Perform a load-acquire barrier in desc_is_avail to
966 : : * enforce the ordering between desc flags and desc
967 : : * content.
968 : : */
969 [ # # # # : 0 : if (unlikely(!desc_is_avail(&descs[avail_idx], wrap_counter)))
# # # # #
# # # ]
970 : : return -1;
971 : :
972 : : *desc_count = 0;
973 : : *len = 0;
974 : :
975 : : while (1) {
976 [ # # # # : 0 : if (unlikely(vec_id >= BUF_VECTOR_MAX))
# # # # #
# # # ]
977 : : return -1;
978 : :
979 [ # # # # : 0 : if (unlikely(*desc_count >= vq->size))
# # # # #
# # # ]
980 : : return -1;
981 : :
982 : 0 : *desc_count += 1;
983 : 0 : *buf_id = descs[avail_idx].id;
984 : :
985 [ # # # # : 0 : if (descs[avail_idx].flags & VRING_DESC_F_INDIRECT) {
# # # # #
# # # ]
986 [ # # # # : 0 : if (unlikely(fill_vec_buf_packed_indirect(dev, vq,
# # # # #
# # # ]
987 : : &descs[avail_idx],
988 : : &vec_id, buf_vec,
989 : : len, perm) < 0))
990 : : return -1;
991 : : } else {
992 : 0 : dlen = descs[avail_idx].len;
993 : 0 : *len += dlen;
994 : :
995 [ # # # # : 0 : if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
# # # # #
# # # ]
996 : : descs[avail_idx].addr,
997 : : dlen,
998 : : perm)))
999 : : return -1;
1000 : : }
1001 : :
1002 [ # # # # : 0 : if ((descs[avail_idx].flags & VRING_DESC_F_NEXT) == 0)
# # # # #
# # # ]
1003 : : break;
1004 : :
1005 [ # # # # : 0 : if (++avail_idx >= vq->size) {
# # # # #
# # # ]
1006 : 0 : avail_idx -= vq->size;
1007 : : wrap_counter ^= 1;
1008 : : }
1009 : : }
1010 : :
1011 : : *vec_idx = vec_id;
1012 : :
1013 : : return 0;
1014 : : }
1015 : :
1016 : : static __rte_noinline void
1017 : 0 : copy_vnet_hdr_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
1018 : : struct buf_vector *buf_vec,
1019 : : struct virtio_net_hdr_mrg_rxbuf *hdr)
1020 : : __rte_shared_locks_required(&vq->iotlb_lock)
1021 : : {
1022 : : uint64_t len;
1023 : 0 : uint64_t remain = dev->vhost_hlen;
1024 : 0 : uint64_t src = (uint64_t)(uintptr_t)hdr, dst;
1025 : 0 : uint64_t iova = buf_vec->buf_iova;
1026 : :
1027 [ # # ]: 0 : while (remain) {
1028 : 0 : len = RTE_MIN(remain,
1029 : : buf_vec->buf_len);
1030 : 0 : dst = buf_vec->buf_addr;
1031 [ # # ]: 0 : rte_memcpy((void *)(uintptr_t)dst,
1032 : : (void *)(uintptr_t)src,
1033 : : len);
1034 : :
1035 : : PRINT_PACKET(dev, (uintptr_t)dst,
1036 : : (uint32_t)len, 0);
1037 : : vhost_log_cache_write_iova(dev, vq,
1038 : : iova, len);
1039 : :
1040 : 0 : remain -= len;
1041 : 0 : iova += len;
1042 : 0 : src += len;
1043 : 0 : buf_vec++;
1044 : : }
1045 : 0 : }
1046 : :
1047 : : static __rte_always_inline int
1048 : : async_iter_initialize(struct virtio_net *dev, struct vhost_async *async)
1049 : : {
1050 : : struct vhost_iov_iter *iter;
1051 : :
1052 [ # # # # : 0 : if (unlikely(async->iovec_idx >= VHOST_MAX_ASYNC_VEC)) {
# # ]
1053 : 0 : VHOST_DATA_LOG(dev->ifname, ERR, "no more async iovec available");
1054 : 0 : return -1;
1055 : : }
1056 : :
1057 : 0 : iter = async->iov_iter + async->iter_idx;
1058 : 0 : iter->iov = async->iovec + async->iovec_idx;
1059 : 0 : iter->nr_segs = 0;
1060 : :
1061 : 0 : return 0;
1062 : : }
1063 : :
1064 : : static __rte_always_inline int
1065 : : async_iter_add_iovec(struct virtio_net *dev, struct vhost_async *async,
1066 : : void *src, void *dst, size_t len)
1067 : : {
1068 : : struct vhost_iov_iter *iter;
1069 : : struct vhost_iovec *iovec;
1070 : :
1071 [ # # # # : 0 : if (unlikely(async->iovec_idx >= VHOST_MAX_ASYNC_VEC)) {
# # ]
1072 : : static bool vhost_max_async_vec_log;
1073 : :
1074 [ # # # # : 0 : if (!vhost_max_async_vec_log) {
# # # # #
# # # # #
# # # # ]
1075 : 0 : VHOST_DATA_LOG(dev->ifname, ERR, "no more async iovec available");
1076 : 0 : vhost_max_async_vec_log = true;
1077 : : }
1078 : :
1079 : : return -1;
1080 : : }
1081 : :
1082 : 0 : iter = async->iov_iter + async->iter_idx;
1083 : 0 : iovec = async->iovec + async->iovec_idx;
1084 : :
1085 : 0 : iovec->src_addr = src;
1086 : 0 : iovec->dst_addr = dst;
1087 : 0 : iovec->len = len;
1088 : :
1089 : 0 : iter->nr_segs++;
1090 : 0 : async->iovec_idx++;
1091 : :
1092 : 0 : return 0;
1093 : : }
1094 : :
1095 : : static __rte_always_inline void
1096 : : async_iter_finalize(struct vhost_async *async)
1097 : : {
1098 : 0 : async->iter_idx++;
1099 : 0 : }
1100 : :
1101 : : static __rte_always_inline void
1102 : : async_iter_cancel(struct vhost_async *async)
1103 : : {
1104 : : struct vhost_iov_iter *iter;
1105 : :
1106 : 0 : iter = async->iov_iter + async->iter_idx;
1107 : 0 : async->iovec_idx -= iter->nr_segs;
1108 : 0 : iter->nr_segs = 0;
1109 : 0 : iter->iov = NULL;
1110 : 0 : }
1111 : :
1112 : : static __rte_always_inline void
1113 : : async_iter_reset(struct vhost_async *async)
1114 : : {
1115 : 0 : async->iter_idx = 0;
1116 : 0 : async->iovec_idx = 0;
1117 : : }
1118 : :
1119 : : static __rte_always_inline int
1120 : : async_fill_seg(struct virtio_net *dev, struct vhost_virtqueue *vq,
1121 : : struct rte_mbuf *m, uint32_t mbuf_offset,
1122 : : uint64_t buf_iova, uint32_t cpy_len, bool to_desc)
1123 : : __rte_shared_locks_required(&vq->access_lock)
1124 : : __rte_shared_locks_required(&vq->iotlb_lock)
1125 : : {
1126 : 0 : struct vhost_async *async = vq->async;
1127 : : uint64_t mapped_len;
1128 : : uint32_t buf_offset = 0;
1129 : : void *src, *dst;
1130 : : void *host_iova;
1131 : :
1132 [ # # # # : 0 : while (cpy_len) {
# # # # #
# # # ]
1133 [ # # # # : 0 : host_iova = (void *)(uintptr_t)gpa_to_first_hpa(dev,
# # # # #
# # # ]
1134 : : buf_iova + buf_offset, cpy_len, &mapped_len);
1135 [ # # # # : 0 : if (unlikely(!host_iova)) {
# # # # #
# # # ]
1136 : 0 : VHOST_DATA_LOG(dev->ifname, ERR,
1137 : : "%s: failed to get host iova.",
1138 : : __func__);
1139 : : return -1;
1140 : : }
1141 : :
1142 : : if (to_desc) {
1143 [ # # # # ]: 0 : src = (void *)(uintptr_t)rte_pktmbuf_iova_offset(m, mbuf_offset);
1144 : : dst = host_iova;
1145 : : } else {
1146 : : src = host_iova;
1147 [ # # # # : 0 : dst = (void *)(uintptr_t)rte_pktmbuf_iova_offset(m, mbuf_offset);
# # # # ]
1148 : : }
1149 : :
1150 [ # # # # : 0 : if (unlikely(async_iter_add_iovec(dev, async, src, dst, (size_t)mapped_len)))
# # # # #
# # # ]
1151 : : return -1;
1152 : :
1153 : 0 : cpy_len -= (uint32_t)mapped_len;
1154 : 0 : mbuf_offset += (uint32_t)mapped_len;
1155 : 0 : buf_offset += (uint32_t)mapped_len;
1156 : : }
1157 : :
1158 : : return 0;
1159 : : }
1160 : :
1161 : : static __rte_always_inline void
1162 : : sync_fill_seg(struct virtio_net *dev, struct vhost_virtqueue *vq,
1163 : : struct rte_mbuf *m, uint32_t mbuf_offset,
1164 : : uint64_t buf_addr, uint64_t buf_iova, uint32_t cpy_len, bool to_desc)
1165 : : __rte_shared_locks_required(&vq->iotlb_lock)
1166 : : {
1167 : 0 : struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
1168 : :
1169 [ # # # # : 0 : if (likely(cpy_len > MAX_BATCH_LEN || vq->batch_copy_nb_elems >= vq->size)) {
# # # # #
# # # ]
1170 : : if (to_desc) {
1171 : 0 : rte_memcpy((void *)((uintptr_t)(buf_addr)),
1172 [ # # # # ]: 0 : rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
1173 : : cpy_len);
1174 : : vhost_log_cache_write_iova(dev, vq, buf_iova, cpy_len);
1175 : : PRINT_PACKET(dev, (uintptr_t)(buf_addr), cpy_len, 0);
1176 : : } else {
1177 [ # # # # : 0 : rte_memcpy(rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
# # # # ]
1178 : : (void *)((uintptr_t)(buf_addr)),
1179 : : cpy_len);
1180 : : }
1181 : : } else {
1182 : : if (to_desc) {
1183 : 0 : batch_copy[vq->batch_copy_nb_elems].dst =
1184 : 0 : (void *)((uintptr_t)(buf_addr));
1185 : 0 : batch_copy[vq->batch_copy_nb_elems].src =
1186 : 0 : rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
1187 : 0 : batch_copy[vq->batch_copy_nb_elems].log_addr = buf_iova;
1188 : : } else {
1189 : 0 : batch_copy[vq->batch_copy_nb_elems].dst =
1190 : 0 : rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
1191 : 0 : batch_copy[vq->batch_copy_nb_elems].src =
1192 : 0 : (void *)((uintptr_t)(buf_addr));
1193 : : }
1194 : 0 : batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
1195 : 0 : vq->batch_copy_nb_elems++;
1196 : : }
1197 : : }
1198 : :
1199 : : static __rte_always_inline int
1200 : : mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
1201 : : struct rte_mbuf *m, struct buf_vector *buf_vec,
1202 : : uint16_t nr_vec, uint16_t num_buffers, bool is_async)
1203 : : __rte_shared_locks_required(&vq->access_lock)
1204 : : __rte_shared_locks_required(&vq->iotlb_lock)
1205 : : {
1206 : : uint32_t vec_idx = 0;
1207 : : uint32_t mbuf_offset, mbuf_avail;
1208 : : uint32_t buf_offset, buf_avail;
1209 : : uint64_t buf_addr, buf_iova, buf_len;
1210 : : uint32_t cpy_len;
1211 : : uint64_t hdr_addr;
1212 : : struct rte_mbuf *hdr_mbuf;
1213 : : struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
1214 : 0 : struct vhost_async *async = vq->async;
1215 : :
1216 : 0 : if (unlikely(m == NULL))
1217 : : return -1;
1218 : :
1219 : 0 : buf_addr = buf_vec[vec_idx].buf_addr;
1220 : 0 : buf_iova = buf_vec[vec_idx].buf_iova;
1221 : 0 : buf_len = buf_vec[vec_idx].buf_len;
1222 : :
1223 [ # # # # : 0 : if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1))
# # # # #
# # # # #
# # ]
1224 : : return -1;
1225 : :
1226 : : hdr_mbuf = m;
1227 : : hdr_addr = buf_addr;
1228 [ # # # # : 0 : if (unlikely(buf_len < dev->vhost_hlen)) {
# # # # ]
1229 : : memset(&tmp_hdr, 0, sizeof(struct virtio_net_hdr_mrg_rxbuf));
1230 : : hdr = &tmp_hdr;
1231 : : } else
1232 : 0 : hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
1233 : :
1234 : : VHOST_DATA_LOG(dev->ifname, DEBUG, "RX: num merge buffers %d", num_buffers);
1235 : :
1236 [ # # # # : 0 : if (unlikely(buf_len < dev->vhost_hlen)) {
# # # # ]
1237 : 0 : buf_offset = dev->vhost_hlen - buf_len;
1238 : : vec_idx++;
1239 : 0 : buf_addr = buf_vec[vec_idx].buf_addr;
1240 : 0 : buf_iova = buf_vec[vec_idx].buf_iova;
1241 : 0 : buf_len = buf_vec[vec_idx].buf_len;
1242 : 0 : buf_avail = buf_len - buf_offset;
1243 : : } else {
1244 : : buf_offset = dev->vhost_hlen;
1245 : 0 : buf_avail = buf_len - dev->vhost_hlen;
1246 : : }
1247 : :
1248 [ # # # # ]: 0 : mbuf_avail = rte_pktmbuf_data_len(m);
1249 : : mbuf_offset = 0;
1250 : :
1251 : : if (is_async) {
1252 : : if (async_iter_initialize(dev, async))
1253 : 0 : return -1;
1254 : : }
1255 : :
1256 [ # # # # : 0 : while (mbuf_avail != 0 || m->next != NULL) {
# # # # #
# # # # #
# # ]
1257 : : /* done with current buf, get the next one */
1258 [ # # # # : 0 : if (buf_avail == 0) {
# # # # ]
1259 : 0 : vec_idx++;
1260 [ # # # # : 0 : if (unlikely(vec_idx >= nr_vec))
# # # # ]
1261 : 0 : goto error;
1262 : :
1263 : 0 : buf_addr = buf_vec[vec_idx].buf_addr;
1264 : 0 : buf_iova = buf_vec[vec_idx].buf_iova;
1265 : 0 : buf_len = buf_vec[vec_idx].buf_len;
1266 : :
1267 : : buf_offset = 0;
1268 : : buf_avail = buf_len;
1269 : : }
1270 : :
1271 : : /* done with current mbuf, get the next one */
1272 [ # # # # : 0 : if (mbuf_avail == 0) {
# # # # ]
1273 : 0 : m = m->next;
1274 : :
1275 : : mbuf_offset = 0;
1276 : 0 : mbuf_avail = rte_pktmbuf_data_len(m);
1277 : : }
1278 : :
1279 [ # # # # : 0 : if (hdr_addr) {
# # # # ]
1280 : : virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
1281 [ # # # # : 0 : if (rxvq_is_mergeable(dev))
# # # # ]
1282 [ # # # # : 0 : ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
# # # # ]
1283 : : num_buffers);
1284 : :
1285 [ # # # # : 0 : if (unlikely(hdr == &tmp_hdr)) {
# # # # ]
1286 : 0 : copy_vnet_hdr_to_desc(dev, vq, buf_vec, hdr);
1287 : : } else {
1288 : : PRINT_PACKET(dev, (uintptr_t)hdr_addr,
1289 : : dev->vhost_hlen, 0);
1290 : 0 : vhost_log_cache_write_iova(dev, vq,
1291 : : buf_vec[0].buf_iova,
1292 [ # # # # : 0 : dev->vhost_hlen);
# # # # ]
1293 : : }
1294 : :
1295 : : hdr_addr = 0;
1296 : : }
1297 : :
1298 : 0 : cpy_len = RTE_MIN(buf_avail, mbuf_avail);
1299 : :
1300 : : if (is_async) {
1301 [ # # # # ]: 0 : if (async_fill_seg(dev, vq, m, mbuf_offset,
1302 : : buf_iova + buf_offset, cpy_len, true) < 0)
1303 : 0 : goto error;
1304 : : } else {
1305 [ # # # # ]: 0 : sync_fill_seg(dev, vq, m, mbuf_offset,
1306 : : buf_addr + buf_offset,
1307 : : buf_iova + buf_offset, cpy_len, true);
1308 : : }
1309 : :
1310 : 0 : mbuf_avail -= cpy_len;
1311 : 0 : mbuf_offset += cpy_len;
1312 : 0 : buf_avail -= cpy_len;
1313 : 0 : buf_offset += cpy_len;
1314 : : }
1315 : :
1316 : : if (is_async)
1317 : : async_iter_finalize(async);
1318 : :
1319 : : return 0;
1320 : 0 : error:
1321 : : if (is_async)
1322 : : async_iter_cancel(async);
1323 : :
1324 : : return -1;
1325 : : }
1326 : :
1327 : : static __rte_always_inline int
1328 : : vhost_enqueue_single_packed(struct virtio_net *dev,
1329 : : struct vhost_virtqueue *vq,
1330 : : struct rte_mbuf *pkt,
1331 : : struct buf_vector *buf_vec,
1332 : : uint16_t *nr_descs)
1333 : : __rte_shared_locks_required(&vq->access_lock)
1334 : : __rte_shared_locks_required(&vq->iotlb_lock)
1335 : 0 : {
1336 : : uint16_t nr_vec = 0;
1337 : 0 : uint16_t avail_idx = vq->last_avail_idx;
1338 : : uint16_t max_tries, tries = 0;
1339 : : uint16_t buf_id = 0;
1340 : : uint32_t len = 0;
1341 : : uint16_t desc_count;
1342 : 0 : uint64_t size = pkt->pkt_len + sizeof(struct virtio_net_hdr_mrg_rxbuf);
1343 : : uint16_t num_buffers = 0;
1344 : 0 : uint32_t buffer_len[vq->size];
1345 : 0 : uint16_t buffer_buf_id[vq->size];
1346 : 0 : uint16_t buffer_desc_count[vq->size];
1347 : :
1348 : 0 : if (rxvq_is_mergeable(dev))
1349 : 0 : max_tries = vq->size - 1;
1350 : : else
1351 : : max_tries = 1;
1352 : :
1353 [ # # ]: 0 : while (size > 0) {
1354 : : /*
1355 : : * if we tried all available ring items, and still
1356 : : * can't get enough buf, it means something abnormal
1357 : : * happened.
1358 : : */
1359 [ # # ]: 0 : if (unlikely(++tries > max_tries))
1360 : : return -1;
1361 : :
1362 [ # # ]: 0 : if (unlikely(fill_vec_buf_packed(dev, vq,
1363 : : avail_idx, &desc_count,
1364 : : buf_vec, &nr_vec,
1365 : : &buf_id, &len,
1366 : : VHOST_ACCESS_RW) < 0))
1367 : : return -1;
1368 : :
1369 : 0 : len = RTE_MIN(len, size);
1370 : 0 : size -= len;
1371 : :
1372 : 0 : buffer_len[num_buffers] = len;
1373 : 0 : buffer_buf_id[num_buffers] = buf_id;
1374 : 0 : buffer_desc_count[num_buffers] = desc_count;
1375 : : num_buffers += 1;
1376 : :
1377 : 0 : *nr_descs += desc_count;
1378 : 0 : avail_idx += desc_count;
1379 [ # # ]: 0 : if (avail_idx >= vq->size)
1380 : 0 : avail_idx -= vq->size;
1381 : : }
1382 : :
1383 : : if (mbuf_to_desc(dev, vq, pkt, buf_vec, nr_vec, num_buffers, false) < 0)
1384 : : return -1;
1385 : :
1386 : : vhost_shadow_enqueue_single_packed(dev, vq, buffer_len, buffer_buf_id,
1387 : : buffer_desc_count, num_buffers);
1388 : :
1389 : : return 0;
1390 : : }
1391 : :
1392 : : static __rte_noinline uint32_t
1393 : 0 : virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
1394 : : struct rte_mbuf **pkts, uint32_t count)
1395 : : __rte_shared_locks_required(&vq->access_lock)
1396 : : __rte_shared_locks_required(&vq->iotlb_lock)
1397 : : {
1398 : : uint32_t pkt_idx = 0;
1399 : : uint16_t num_buffers;
1400 : : struct buf_vector buf_vec[BUF_VECTOR_MAX];
1401 : : uint16_t avail_head;
1402 : :
1403 : : /*
1404 : : * The ordering between avail index and
1405 : : * desc reads needs to be enforced.
1406 : : */
1407 : 0 : avail_head = rte_atomic_load_explicit((unsigned short __rte_atomic *)&vq->avail->idx,
1408 : : rte_memory_order_acquire);
1409 : :
1410 : 0 : rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1411 : :
1412 [ # # ]: 0 : for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
1413 : 0 : uint64_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
1414 : 0 : uint16_t nr_vec = 0;
1415 : :
1416 [ # # ]: 0 : if (unlikely(reserve_avail_buf_split(dev, vq,
1417 : : pkt_len, buf_vec, &num_buffers,
1418 : : avail_head, &nr_vec) < 0)) {
1419 : : VHOST_DATA_LOG(dev->ifname, DEBUG,
1420 : : "failed to get enough desc from vring");
1421 : 0 : vq->shadow_used_idx -= num_buffers;
1422 : 0 : break;
1423 : : }
1424 : :
1425 : : VHOST_DATA_LOG(dev->ifname, DEBUG,
1426 : : "current index %d | end index %d",
1427 : : vq->last_avail_idx, vq->last_avail_idx + num_buffers);
1428 : :
1429 [ # # ]: 0 : if (mbuf_to_desc(dev, vq, pkts[pkt_idx], buf_vec, nr_vec,
1430 : : num_buffers, false) < 0) {
1431 : 0 : vq->shadow_used_idx -= num_buffers;
1432 : 0 : break;
1433 : : }
1434 : :
1435 [ # # ]: 0 : vq->last_avail_idx += num_buffers;
1436 : : vhost_virtqueue_reconnect_log_split(vq);
1437 : : }
1438 : :
1439 : 0 : do_data_copy_enqueue(dev, vq);
1440 : :
1441 [ # # ]: 0 : if (likely(vq->shadow_used_idx)) {
1442 : : flush_shadow_used_ring_split(dev, vq);
1443 : : vhost_vring_call_split(dev, vq);
1444 : : }
1445 : :
1446 : 0 : return pkt_idx;
1447 : : }
1448 : :
1449 : : static __rte_always_inline int
1450 : : virtio_dev_rx_sync_batch_check(struct virtio_net *dev,
1451 : : struct vhost_virtqueue *vq,
1452 : : struct rte_mbuf **pkts,
1453 : : uint64_t *desc_addrs,
1454 : : uint64_t *lens)
1455 : : __rte_shared_locks_required(&vq->iotlb_lock)
1456 : : {
1457 : 0 : bool wrap_counter = vq->avail_wrap_counter;
1458 : : struct vring_packed_desc *descs = vq->desc_packed;
1459 : : uint16_t avail_idx = vq->last_avail_idx;
1460 : : uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1461 : : uint16_t i;
1462 : :
1463 : 0 : if (unlikely(avail_idx & PACKED_BATCH_MASK))
1464 : : return -1;
1465 : :
1466 [ # # ]: 0 : if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
1467 : : return -1;
1468 : :
1469 [ # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1470 [ # # ]: 0 : if (unlikely(pkts[i]->next != NULL))
1471 : : return -1;
1472 [ # # ]: 0 : if (unlikely(!desc_is_avail(&descs[avail_idx + i],
1473 : : wrap_counter)))
1474 : : return -1;
1475 : : }
1476 : :
1477 [ # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1478 : 0 : lens[i] = descs[avail_idx + i].len;
1479 : :
1480 [ # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1481 [ # # ]: 0 : if (unlikely(pkts[i]->pkt_len > (lens[i] - buf_offset)))
1482 : : return -1;
1483 : : }
1484 : :
1485 [ # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1486 : 0 : desc_addrs[i] = vhost_iova_to_vva(dev, vq,
1487 : 0 : descs[avail_idx + i].addr,
1488 [ # # ]: 0 : &lens[i],
1489 : : VHOST_ACCESS_RW);
1490 : :
1491 [ # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1492 [ # # ]: 0 : if (unlikely(!desc_addrs[i]))
1493 : : return -1;
1494 [ # # ]: 0 : if (unlikely(lens[i] != descs[avail_idx + i].len))
1495 : : return -1;
1496 : : }
1497 : :
1498 : : return 0;
1499 : : }
1500 : :
1501 : : static __rte_always_inline int
1502 : : virtio_dev_rx_async_batch_check(struct vhost_virtqueue *vq,
1503 : : struct rte_mbuf **pkts,
1504 : : uint64_t *desc_addrs,
1505 : : uint64_t *lens,
1506 : : int16_t dma_id,
1507 : : uint16_t vchan_id)
1508 : : {
1509 : 0 : bool wrap_counter = vq->avail_wrap_counter;
1510 : : struct vring_packed_desc *descs = vq->desc_packed;
1511 : : uint16_t avail_idx = vq->last_avail_idx;
1512 : : uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1513 : : uint16_t i;
1514 : :
1515 : 0 : if (unlikely(avail_idx & PACKED_BATCH_MASK))
1516 : : return -1;
1517 : :
1518 [ # # ]: 0 : if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
1519 : : return -1;
1520 : :
1521 [ # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1522 [ # # ]: 0 : if (unlikely(pkts[i]->next != NULL))
1523 : : return -1;
1524 [ # # ]: 0 : if (unlikely(!desc_is_avail(&descs[avail_idx + i],
1525 : : wrap_counter)))
1526 : : return -1;
1527 : : }
1528 : :
1529 [ # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1530 : 0 : lens[i] = descs[avail_idx + i].len;
1531 : :
1532 [ # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1533 [ # # ]: 0 : if (unlikely(pkts[i]->pkt_len > (lens[i] - buf_offset)))
1534 : : return -1;
1535 : : }
1536 : :
1537 [ # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1538 : 0 : desc_addrs[i] = descs[avail_idx + i].addr;
1539 : :
1540 [ # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1541 [ # # ]: 0 : if (unlikely(!desc_addrs[i]))
1542 : : return -1;
1543 [ # # ]: 0 : if (unlikely(lens[i] != descs[avail_idx + i].len))
1544 : : return -1;
1545 : : }
1546 : :
1547 [ # # ]: 0 : if (rte_dma_burst_capacity(dma_id, vchan_id) < PACKED_BATCH_SIZE)
1548 : : return -1;
1549 : :
1550 : : return 0;
1551 : : }
1552 : :
1553 : : static __rte_always_inline void
1554 : : virtio_dev_rx_batch_packed_copy(struct virtio_net *dev,
1555 : : struct vhost_virtqueue *vq,
1556 : : struct rte_mbuf **pkts,
1557 : : uint64_t *desc_addrs,
1558 : : uint64_t *lens)
1559 : : __rte_shared_locks_required(&vq->iotlb_lock)
1560 : : {
1561 : : uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1562 : : struct virtio_net_hdr_mrg_rxbuf *hdrs[PACKED_BATCH_SIZE];
1563 : 0 : struct vring_packed_desc *descs = vq->desc_packed;
1564 : 0 : uint16_t avail_idx = vq->last_avail_idx;
1565 : : uint16_t ids[PACKED_BATCH_SIZE];
1566 : : uint16_t i;
1567 : :
1568 [ # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1569 : 0 : rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
1570 : 0 : hdrs[i] = (struct virtio_net_hdr_mrg_rxbuf *)
1571 : : (uintptr_t)desc_addrs[i];
1572 : 0 : lens[i] = pkts[i]->pkt_len +
1573 : : sizeof(struct virtio_net_hdr_mrg_rxbuf);
1574 : : }
1575 : :
1576 [ # # ]: 0 : if (rxvq_is_mergeable(dev)) {
1577 [ # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1578 [ # # ]: 0 : ASSIGN_UNLESS_EQUAL(hdrs[i]->num_buffers, 1);
1579 : : }
1580 : : }
1581 : :
1582 [ # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1583 [ # # ]: 0 : virtio_enqueue_offload(pkts[i], &hdrs[i]->hdr);
1584 : :
1585 : : vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
1586 : :
1587 [ # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1588 : 0 : rte_memcpy((void *)(uintptr_t)(desc_addrs[i] + buf_offset),
1589 : 0 : rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
1590 [ # # ]: 0 : pkts[i]->pkt_len);
1591 : : }
1592 : :
1593 [ # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1594 : 0 : vhost_log_cache_write_iova(dev, vq, descs[avail_idx + i].addr,
1595 [ # # ]: 0 : lens[i]);
1596 : :
1597 [ # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1598 : 0 : ids[i] = descs[avail_idx + i].id;
1599 : :
1600 : : vhost_flush_enqueue_batch_packed(dev, vq, lens, ids);
1601 : : }
1602 : :
1603 : : static __rte_always_inline int
1604 : : virtio_dev_rx_sync_batch_packed(struct virtio_net *dev,
1605 : : struct vhost_virtqueue *vq,
1606 : : struct rte_mbuf **pkts)
1607 : : __rte_shared_locks_required(&vq->iotlb_lock)
1608 : : {
1609 : : uint64_t desc_addrs[PACKED_BATCH_SIZE];
1610 : : uint64_t lens[PACKED_BATCH_SIZE];
1611 : :
1612 [ # # ]: 0 : if (virtio_dev_rx_sync_batch_check(dev, vq, pkts, desc_addrs, lens) == -1)
1613 : : return -1;
1614 : :
1615 [ # # ]: 0 : if (vq->shadow_used_idx) {
1616 : 0 : do_data_copy_enqueue(dev, vq);
1617 : : vhost_flush_enqueue_shadow_packed(dev, vq);
1618 : : }
1619 : :
1620 : : virtio_dev_rx_batch_packed_copy(dev, vq, pkts, desc_addrs, lens);
1621 : :
1622 : : return 0;
1623 : : }
1624 : :
1625 : : static __rte_always_inline int16_t
1626 : : virtio_dev_rx_single_packed(struct virtio_net *dev,
1627 : : struct vhost_virtqueue *vq,
1628 : : struct rte_mbuf *pkt)
1629 : : __rte_shared_locks_required(&vq->access_lock)
1630 : : __rte_shared_locks_required(&vq->iotlb_lock)
1631 : : {
1632 : : struct buf_vector buf_vec[BUF_VECTOR_MAX];
1633 : : uint16_t nr_descs = 0;
1634 : :
1635 [ # # ]: 0 : if (unlikely(vhost_enqueue_single_packed(dev, vq, pkt, buf_vec,
1636 : : &nr_descs) < 0)) {
1637 : : VHOST_DATA_LOG(dev->ifname, DEBUG, "failed to get enough desc from vring");
1638 : : return -1;
1639 : : }
1640 : :
1641 : : VHOST_DATA_LOG(dev->ifname, DEBUG,
1642 : : "current index %d | end index %d",
1643 : : vq->last_avail_idx, vq->last_avail_idx + nr_descs);
1644 : :
1645 : : vq_inc_last_avail_packed(vq, nr_descs);
1646 : :
1647 : : return 0;
1648 : : }
1649 : :
1650 : : static __rte_noinline uint32_t
1651 : 0 : virtio_dev_rx_packed(struct virtio_net *dev,
1652 : : struct vhost_virtqueue *__rte_restrict vq,
1653 : : struct rte_mbuf **__rte_restrict pkts,
1654 : : uint32_t count)
1655 : : __rte_shared_locks_required(&vq->access_lock)
1656 : : __rte_shared_locks_required(&vq->iotlb_lock)
1657 : : {
1658 : : uint32_t pkt_idx = 0;
1659 : :
1660 : : do {
1661 : 0 : rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
1662 : :
1663 [ # # ]: 0 : if (count - pkt_idx >= PACKED_BATCH_SIZE) {
1664 : 0 : if (!virtio_dev_rx_sync_batch_packed(dev, vq,
1665 [ # # ]: 0 : &pkts[pkt_idx])) {
1666 : 0 : pkt_idx += PACKED_BATCH_SIZE;
1667 : 0 : continue;
1668 : : }
1669 : : }
1670 : :
1671 [ # # ]: 0 : if (virtio_dev_rx_single_packed(dev, vq, pkts[pkt_idx]))
1672 : : break;
1673 : 0 : pkt_idx++;
1674 : :
1675 [ # # ]: 0 : } while (pkt_idx < count);
1676 : :
1677 [ # # ]: 0 : if (vq->shadow_used_idx) {
1678 : 0 : do_data_copy_enqueue(dev, vq);
1679 : : vhost_flush_enqueue_shadow_packed(dev, vq);
1680 : : }
1681 : :
1682 [ # # ]: 0 : if (pkt_idx)
1683 : : vhost_vring_call_packed(dev, vq);
1684 : :
1685 : 0 : return pkt_idx;
1686 : : }
1687 : :
1688 : : static void
1689 : 0 : virtio_dev_vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
1690 : : {
1691 : 0 : rte_rwlock_write_lock(&vq->access_lock);
1692 : : vhost_user_iotlb_rd_lock(vq);
1693 [ # # ]: 0 : if (!vq->access_ok)
1694 : 0 : vring_translate(dev, vq);
1695 : : vhost_user_iotlb_rd_unlock(vq);
1696 : : rte_rwlock_write_unlock(&vq->access_lock);
1697 : 0 : }
1698 : :
1699 : : static __rte_always_inline uint32_t
1700 : : virtio_dev_rx(struct virtio_net *dev, struct vhost_virtqueue *vq,
1701 : : struct rte_mbuf **pkts, uint32_t count)
1702 : : {
1703 : : uint32_t nb_tx = 0;
1704 : :
1705 : : VHOST_DATA_LOG(dev->ifname, DEBUG, "%s", __func__);
1706 : 0 : rte_rwlock_read_lock(&vq->access_lock);
1707 : :
1708 [ # # ]: 0 : if (unlikely(!vq->enabled))
1709 : 0 : goto out_access_unlock;
1710 : :
1711 : : vhost_user_iotlb_rd_lock(vq);
1712 : :
1713 [ # # ]: 0 : if (unlikely(!vq->access_ok)) {
1714 : : vhost_user_iotlb_rd_unlock(vq);
1715 : : rte_rwlock_read_unlock(&vq->access_lock);
1716 : :
1717 : 0 : virtio_dev_vring_translate(dev, vq);
1718 : 0 : goto out_no_unlock;
1719 : : }
1720 : :
1721 : 0 : count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
1722 [ # # ]: 0 : if (count == 0)
1723 : 0 : goto out;
1724 : :
1725 [ # # ]: 0 : if (vq_is_packed(dev))
1726 : 0 : nb_tx = virtio_dev_rx_packed(dev, vq, pkts, count);
1727 : : else
1728 : 0 : nb_tx = virtio_dev_rx_split(dev, vq, pkts, count);
1729 : :
1730 : 0 : vhost_queue_stats_update(dev, vq, pkts, nb_tx);
1731 : :
1732 : 0 : out:
1733 : : vhost_user_iotlb_rd_unlock(vq);
1734 : :
1735 : 0 : out_access_unlock:
1736 : : rte_rwlock_read_unlock(&vq->access_lock);
1737 : :
1738 : 0 : out_no_unlock:
1739 : : return nb_tx;
1740 : : }
1741 : :
1742 : : uint16_t
1743 [ # # ]: 0 : rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
1744 : : struct rte_mbuf **__rte_restrict pkts, uint16_t count)
1745 : : {
1746 : : struct virtio_net *dev = get_device(vid);
1747 : :
1748 [ # # ]: 0 : if (!dev)
1749 : : return 0;
1750 : :
1751 [ # # ]: 0 : if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
1752 : 0 : VHOST_DATA_LOG(dev->ifname, ERR,
1753 : : "%s: built-in vhost net backend is disabled.",
1754 : : __func__);
1755 : 0 : return 0;
1756 : : }
1757 : :
1758 [ # # # # ]: 0 : if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
1759 : 0 : VHOST_DATA_LOG(dev->ifname, ERR,
1760 : : "%s: invalid virtqueue idx %d.",
1761 : : __func__, queue_id);
1762 : 0 : return 0;
1763 : : }
1764 : :
1765 : 0 : return virtio_dev_rx(dev, dev->virtqueue[queue_id], pkts, count);
1766 : : }
1767 : :
1768 : : static __rte_always_inline uint16_t
1769 : : async_get_first_inflight_pkt_idx(struct vhost_virtqueue *vq)
1770 : : __rte_shared_locks_required(&vq->access_lock)
1771 : : {
1772 : 0 : struct vhost_async *async = vq->async;
1773 : :
1774 [ # # # # : 0 : if (async->pkts_idx >= async->pkts_inflight_n)
# # # # #
# # # # #
# # # # ]
1775 : 0 : return async->pkts_idx - async->pkts_inflight_n;
1776 : : else
1777 : 0 : return vq->size - async->pkts_inflight_n + async->pkts_idx;
1778 : : }
1779 : :
1780 : : static __rte_always_inline void
1781 : : store_dma_desc_info_split(struct vring_used_elem *s_ring, struct vring_used_elem *d_ring,
1782 : : uint16_t ring_size, uint16_t s_idx, uint16_t d_idx, uint16_t count)
1783 : : {
1784 : : size_t elem_size = sizeof(struct vring_used_elem);
1785 : :
1786 : 0 : if (d_idx + count <= ring_size) {
1787 [ # # ]: 0 : rte_memcpy(d_ring + d_idx, s_ring + s_idx, count * elem_size);
1788 : : } else {
1789 : 0 : uint16_t size = ring_size - d_idx;
1790 : :
1791 [ # # ]: 0 : rte_memcpy(d_ring + d_idx, s_ring + s_idx, size * elem_size);
1792 [ # # ]: 0 : rte_memcpy(d_ring, s_ring + s_idx + size, (count - size) * elem_size);
1793 : : }
1794 : : }
1795 : :
1796 : : static __rte_noinline uint32_t
1797 : 0 : virtio_dev_rx_async_submit_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
1798 : : struct rte_mbuf **pkts, uint32_t count, int16_t dma_id, uint16_t vchan_id)
1799 : : __rte_exclusive_locks_required(&vq->access_lock)
1800 : : __rte_shared_locks_required(&vq->iotlb_lock)
1801 : : {
1802 : : struct buf_vector buf_vec[BUF_VECTOR_MAX];
1803 : : uint32_t pkt_idx = 0;
1804 : : uint16_t num_buffers;
1805 : : uint16_t avail_head;
1806 : :
1807 : 0 : struct vhost_async *async = vq->async;
1808 : 0 : struct async_inflight_info *pkts_info = async->pkts_info;
1809 : : uint32_t pkt_err = 0;
1810 : : uint16_t n_xfer;
1811 : : uint16_t slot_idx = 0;
1812 : :
1813 : : /*
1814 : : * The ordering between avail index and desc reads need to be enforced.
1815 : : */
1816 : 0 : avail_head = rte_atomic_load_explicit((unsigned short __rte_atomic *)&vq->avail->idx,
1817 : : rte_memory_order_acquire);
1818 : :
1819 : 0 : rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1820 : :
1821 : : async_iter_reset(async);
1822 : :
1823 [ # # ]: 0 : for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
1824 : 0 : uint64_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
1825 : 0 : uint16_t nr_vec = 0;
1826 : :
1827 [ # # ]: 0 : if (unlikely(reserve_avail_buf_split(dev, vq, pkt_len, buf_vec,
1828 : : &num_buffers, avail_head, &nr_vec) < 0)) {
1829 : : VHOST_DATA_LOG(dev->ifname, DEBUG,
1830 : : "failed to get enough desc from vring");
1831 : 0 : vq->shadow_used_idx -= num_buffers;
1832 : 0 : break;
1833 : : }
1834 : :
1835 : : VHOST_DATA_LOG(dev->ifname, DEBUG,
1836 : : "current index %d | end index %d",
1837 : : vq->last_avail_idx, vq->last_avail_idx + num_buffers);
1838 : :
1839 [ # # ]: 0 : if (mbuf_to_desc(dev, vq, pkts[pkt_idx], buf_vec, nr_vec, num_buffers, true) < 0) {
1840 : 0 : vq->shadow_used_idx -= num_buffers;
1841 : 0 : break;
1842 : : }
1843 : :
1844 : 0 : slot_idx = (async->pkts_idx + pkt_idx) & (vq->size - 1);
1845 : 0 : pkts_info[slot_idx].descs = num_buffers;
1846 : 0 : pkts_info[slot_idx].mbuf = pkts[pkt_idx];
1847 : :
1848 [ # # ]: 0 : vq->last_avail_idx += num_buffers;
1849 : : vhost_virtqueue_reconnect_log_split(vq);
1850 : : }
1851 : :
1852 [ # # ]: 0 : if (unlikely(pkt_idx == 0))
1853 : : return 0;
1854 : :
1855 : 0 : n_xfer = vhost_async_dma_transfer(dev, vq, dma_id, vchan_id, async->pkts_idx,
1856 : 0 : async->iov_iter, pkt_idx);
1857 : :
1858 : 0 : pkt_err = pkt_idx - n_xfer;
1859 [ # # ]: 0 : if (unlikely(pkt_err)) {
1860 : : uint16_t num_descs = 0;
1861 : :
1862 : : VHOST_DATA_LOG(dev->ifname, DEBUG,
1863 : : "%s: failed to transfer %u packets for queue %u.",
1864 : : __func__, pkt_err, vq->index);
1865 : :
1866 : : /* update number of completed packets */
1867 : : pkt_idx = n_xfer;
1868 : :
1869 : : /* calculate the sum of descriptors to revert */
1870 [ # # ]: 0 : while (pkt_err-- > 0) {
1871 : 0 : num_descs += pkts_info[slot_idx & (vq->size - 1)].descs;
1872 : 0 : slot_idx--;
1873 : : }
1874 : :
1875 : : /* recover shadow used ring and available ring */
1876 : 0 : vq->shadow_used_idx -= num_descs;
1877 [ # # ]: 0 : vq->last_avail_idx -= num_descs;
1878 : : vhost_virtqueue_reconnect_log_split(vq);
1879 : : }
1880 : :
1881 : : /* keep used descriptors */
1882 [ # # ]: 0 : if (likely(vq->shadow_used_idx)) {
1883 : 0 : uint16_t to = async->desc_idx_split & (vq->size - 1);
1884 : :
1885 [ # # ]: 0 : store_dma_desc_info_split(vq->shadow_used_split,
1886 : : async->descs_split, vq->size, 0, to,
1887 : : vq->shadow_used_idx);
1888 : :
1889 : 0 : async->desc_idx_split += vq->shadow_used_idx;
1890 : :
1891 : 0 : async->pkts_idx += pkt_idx;
1892 [ # # ]: 0 : if (async->pkts_idx >= vq->size)
1893 : 0 : async->pkts_idx -= vq->size;
1894 : :
1895 : 0 : async->pkts_inflight_n += pkt_idx;
1896 : 0 : vq->shadow_used_idx = 0;
1897 : : }
1898 : :
1899 : : return pkt_idx;
1900 : : }
1901 : :
1902 : :
1903 : : static __rte_always_inline int
1904 : : vhost_enqueue_async_packed(struct virtio_net *dev,
1905 : : struct vhost_virtqueue *vq,
1906 : : struct rte_mbuf *pkt,
1907 : : struct buf_vector *buf_vec,
1908 : : uint16_t *nr_descs,
1909 : : uint16_t *nr_buffers)
1910 : : __rte_exclusive_locks_required(&vq->access_lock)
1911 : : __rte_shared_locks_required(&vq->iotlb_lock)
1912 : 0 : {
1913 : : uint16_t nr_vec = 0;
1914 : 0 : uint16_t avail_idx = vq->last_avail_idx;
1915 : : uint16_t max_tries, tries = 0;
1916 : : uint16_t buf_id = 0;
1917 : : uint32_t len = 0;
1918 : : uint16_t desc_count = 0;
1919 : 0 : uint64_t size = pkt->pkt_len + sizeof(struct virtio_net_hdr_mrg_rxbuf);
1920 : 0 : uint32_t buffer_len[vq->size];
1921 : 0 : uint16_t buffer_buf_id[vq->size];
1922 : 0 : uint16_t buffer_desc_count[vq->size];
1923 : :
1924 : 0 : if (rxvq_is_mergeable(dev))
1925 : 0 : max_tries = vq->size - 1;
1926 : : else
1927 : : max_tries = 1;
1928 : :
1929 : : do {
1930 : : /*
1931 : : * if we tried all available ring items, and still
1932 : : * can't get enough buf, it means something abnormal
1933 : : * happened.
1934 : : */
1935 [ # # ]: 0 : if (unlikely(++tries > max_tries))
1936 : : return -1;
1937 : :
1938 [ # # ]: 0 : if (unlikely(fill_vec_buf_packed(dev, vq,
1939 : : avail_idx, &desc_count,
1940 : : buf_vec, &nr_vec,
1941 : : &buf_id, &len,
1942 : : VHOST_ACCESS_RW) < 0))
1943 : : return -1;
1944 : :
1945 : 0 : len = RTE_MIN(len, size);
1946 : 0 : size -= len;
1947 : :
1948 : 0 : buffer_len[*nr_buffers] = len;
1949 : 0 : buffer_buf_id[*nr_buffers] = buf_id;
1950 : 0 : buffer_desc_count[*nr_buffers] = desc_count;
1951 : : *nr_buffers += 1;
1952 : 0 : *nr_descs += desc_count;
1953 : 0 : avail_idx += desc_count;
1954 [ # # ]: 0 : if (avail_idx >= vq->size)
1955 : 0 : avail_idx -= vq->size;
1956 [ # # ]: 0 : } while (size > 0);
1957 : :
1958 [ # # ]: 0 : if (unlikely(mbuf_to_desc(dev, vq, pkt, buf_vec, nr_vec, *nr_buffers, true) < 0))
1959 : : return -1;
1960 : :
1961 : : vhost_async_shadow_enqueue_packed(vq, buffer_len, buffer_buf_id,
1962 : : buffer_desc_count, *nr_buffers);
1963 : :
1964 : : return 0;
1965 : : }
1966 : :
1967 : : static __rte_always_inline int16_t
1968 : : virtio_dev_rx_async_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
1969 : : struct rte_mbuf *pkt, uint16_t *nr_descs, uint16_t *nr_buffers)
1970 : : __rte_exclusive_locks_required(&vq->access_lock)
1971 : : __rte_shared_locks_required(&vq->iotlb_lock)
1972 : : {
1973 : : struct buf_vector buf_vec[BUF_VECTOR_MAX];
1974 : :
1975 [ # # ]: 0 : if (unlikely(vhost_enqueue_async_packed(dev, vq, pkt, buf_vec,
1976 : : nr_descs, nr_buffers) < 0)) {
1977 : : VHOST_DATA_LOG(dev->ifname, DEBUG, "failed to get enough desc from vring");
1978 : 0 : return -1;
1979 : : }
1980 : :
1981 : : VHOST_DATA_LOG(dev->ifname, DEBUG,
1982 : : "current index %d | end index %d",
1983 : : vq->last_avail_idx, vq->last_avail_idx + *nr_descs);
1984 : :
1985 : : return 0;
1986 : : }
1987 : :
1988 : : static __rte_always_inline void
1989 : : virtio_dev_rx_async_packed_batch_enqueue(struct virtio_net *dev,
1990 : : struct vhost_virtqueue *vq,
1991 : : struct rte_mbuf **pkts,
1992 : : uint64_t *desc_addrs,
1993 : : uint64_t *lens)
1994 : : __rte_exclusive_locks_required(&vq->access_lock)
1995 : : __rte_shared_locks_required(&vq->iotlb_lock)
1996 : : {
1997 : : uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1998 : : struct virtio_net_hdr_mrg_rxbuf *hdrs[PACKED_BATCH_SIZE];
1999 : 0 : struct vring_packed_desc *descs = vq->desc_packed;
2000 : 0 : struct vhost_async *async = vq->async;
2001 : 0 : uint16_t avail_idx = vq->last_avail_idx;
2002 : : uint32_t mbuf_offset = 0;
2003 : : uint16_t ids[PACKED_BATCH_SIZE];
2004 : : uint64_t mapped_len[PACKED_BATCH_SIZE];
2005 : : void *host_iova[PACKED_BATCH_SIZE];
2006 : : uintptr_t desc;
2007 : : uint16_t i;
2008 : :
2009 [ # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2010 : 0 : rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
2011 [ # # ]: 0 : desc = vhost_iova_to_vva(dev, vq, desc_addrs[i], &lens[i], VHOST_ACCESS_RW);
2012 : 0 : hdrs[i] = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)desc;
2013 : 0 : lens[i] = pkts[i]->pkt_len +
2014 : : sizeof(struct virtio_net_hdr_mrg_rxbuf);
2015 : : }
2016 : :
2017 [ # # ]: 0 : if (rxvq_is_mergeable(dev)) {
2018 [ # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2019 [ # # ]: 0 : ASSIGN_UNLESS_EQUAL(hdrs[i]->num_buffers, 1);
2020 : : }
2021 : : }
2022 : :
2023 [ # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2024 [ # # ]: 0 : virtio_enqueue_offload(pkts[i], &hdrs[i]->hdr);
2025 : :
2026 : : vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
2027 : :
2028 [ # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2029 : 0 : host_iova[i] = (void *)(uintptr_t)gpa_to_first_hpa(dev,
2030 [ # # ]: 0 : desc_addrs[i] + buf_offset, lens[i], &mapped_len[i]);
2031 : : }
2032 : :
2033 [ # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2034 : : async_iter_initialize(dev, async);
2035 : 0 : async_iter_add_iovec(dev, async,
2036 [ # # ]: 0 : (void *)(uintptr_t)rte_pktmbuf_iova_offset(pkts[i], mbuf_offset),
2037 : : host_iova[i],
2038 : : mapped_len[i]);
2039 : 0 : async->iter_idx++;
2040 : : }
2041 : :
2042 [ # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2043 [ # # ]: 0 : vhost_log_cache_write_iova(dev, vq, descs[avail_idx + i].addr, lens[i]);
2044 : :
2045 [ # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2046 : 0 : ids[i] = descs[avail_idx + i].id;
2047 : :
2048 : : vhost_async_shadow_enqueue_packed_batch(vq, lens, ids);
2049 : : }
2050 : :
2051 : : static __rte_always_inline int
2052 : : virtio_dev_rx_async_packed_batch(struct virtio_net *dev,
2053 : : struct vhost_virtqueue *vq,
2054 : : struct rte_mbuf **pkts,
2055 : : int16_t dma_id, uint16_t vchan_id)
2056 : : __rte_exclusive_locks_required(&vq->access_lock)
2057 : : __rte_shared_locks_required(&vq->iotlb_lock)
2058 : : {
2059 : : uint64_t desc_addrs[PACKED_BATCH_SIZE];
2060 : : uint64_t lens[PACKED_BATCH_SIZE];
2061 : :
2062 : : if (virtio_dev_rx_async_batch_check(vq, pkts, desc_addrs, lens, dma_id, vchan_id) == -1)
2063 : : return -1;
2064 : :
2065 : : virtio_dev_rx_async_packed_batch_enqueue(dev, vq, pkts, desc_addrs, lens);
2066 : :
2067 : : return 0;
2068 : : }
2069 : :
2070 : : static __rte_always_inline void
2071 : : dma_error_handler_packed(struct vhost_virtqueue *vq, uint16_t slot_idx,
2072 : : uint32_t nr_err, uint32_t *pkt_idx)
2073 : : __rte_exclusive_locks_required(&vq->access_lock)
2074 : : {
2075 : : uint16_t descs_err = 0;
2076 : : uint16_t buffers_err = 0;
2077 : 0 : struct vhost_async *async = vq->async;
2078 : 0 : struct async_inflight_info *pkts_info = vq->async->pkts_info;
2079 : :
2080 : : *pkt_idx -= nr_err;
2081 : : /* calculate the sum of buffers and descs of DMA-error packets. */
2082 [ # # ]: 0 : while (nr_err-- > 0) {
2083 : 0 : descs_err += pkts_info[slot_idx % vq->size].descs;
2084 : 0 : buffers_err += pkts_info[slot_idx % vq->size].nr_buffers;
2085 : 0 : slot_idx--;
2086 : : }
2087 : :
2088 [ # # ]: 0 : if (vq->last_avail_idx >= descs_err) {
2089 : 0 : vq->last_avail_idx -= descs_err;
2090 : : } else {
2091 : 0 : vq->last_avail_idx = vq->last_avail_idx + vq->size - descs_err;
2092 : 0 : vq->avail_wrap_counter ^= 1;
2093 : : }
2094 : : vhost_virtqueue_reconnect_log_packed(vq);
2095 : :
2096 [ # # ]: 0 : if (async->buffer_idx_packed >= buffers_err)
2097 : 0 : async->buffer_idx_packed -= buffers_err;
2098 : : else
2099 : 0 : async->buffer_idx_packed = async->buffer_idx_packed + vq->size - buffers_err;
2100 : : }
2101 : :
2102 : : static __rte_noinline uint32_t
2103 : 0 : virtio_dev_rx_async_submit_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
2104 : : struct rte_mbuf **pkts, uint32_t count, int16_t dma_id, uint16_t vchan_id)
2105 : : __rte_exclusive_locks_required(&vq->access_lock)
2106 : : __rte_shared_locks_required(&vq->iotlb_lock)
2107 : : {
2108 : : uint32_t pkt_idx = 0;
2109 : : uint16_t n_xfer;
2110 : : uint16_t num_buffers;
2111 : : uint16_t num_descs;
2112 : :
2113 : 0 : struct vhost_async *async = vq->async;
2114 : 0 : struct async_inflight_info *pkts_info = async->pkts_info;
2115 : : uint32_t pkt_err = 0;
2116 : : uint16_t slot_idx = 0;
2117 : : uint16_t i;
2118 : :
2119 : : do {
2120 : 0 : rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
2121 : :
2122 [ # # ]: 0 : if (count - pkt_idx >= PACKED_BATCH_SIZE) {
2123 [ # # ]: 0 : if (!virtio_dev_rx_async_packed_batch(dev, vq, &pkts[pkt_idx],
2124 : : dma_id, vchan_id)) {
2125 [ # # ]: 0 : for (i = 0; i < PACKED_BATCH_SIZE; i++) {
2126 : 0 : slot_idx = (async->pkts_idx + pkt_idx) % vq->size;
2127 : 0 : pkts_info[slot_idx].descs = 1;
2128 : 0 : pkts_info[slot_idx].nr_buffers = 1;
2129 : 0 : pkts_info[slot_idx].mbuf = pkts[pkt_idx];
2130 : 0 : pkt_idx++;
2131 : : }
2132 : 0 : continue;
2133 : : }
2134 : : }
2135 : :
2136 : : num_buffers = 0;
2137 : : num_descs = 0;
2138 [ # # # # ]: 0 : if (unlikely(virtio_dev_rx_async_packed(dev, vq, pkts[pkt_idx],
2139 : : &num_descs, &num_buffers) < 0))
2140 : : break;
2141 : :
2142 : 0 : slot_idx = (async->pkts_idx + pkt_idx) % vq->size;
2143 : :
2144 : 0 : pkts_info[slot_idx].descs = num_descs;
2145 : 0 : pkts_info[slot_idx].nr_buffers = num_buffers;
2146 : 0 : pkts_info[slot_idx].mbuf = pkts[pkt_idx];
2147 : :
2148 [ # # ]: 0 : pkt_idx++;
2149 : : vq_inc_last_avail_packed(vq, num_descs);
2150 [ # # ]: 0 : } while (pkt_idx < count);
2151 : :
2152 [ # # ]: 0 : if (unlikely(pkt_idx == 0))
2153 : : return 0;
2154 : :
2155 : 0 : n_xfer = vhost_async_dma_transfer(dev, vq, dma_id, vchan_id, async->pkts_idx,
2156 : 0 : async->iov_iter, pkt_idx);
2157 : :
2158 : : async_iter_reset(async);
2159 : :
2160 : 0 : pkt_err = pkt_idx - n_xfer;
2161 [ # # ]: 0 : if (unlikely(pkt_err)) {
2162 : : VHOST_DATA_LOG(dev->ifname, DEBUG,
2163 : : "%s: failed to transfer %u packets for queue %u.",
2164 : : __func__, pkt_err, vq->index);
2165 : : dma_error_handler_packed(vq, slot_idx, pkt_err, &pkt_idx);
2166 : : }
2167 : :
2168 : 0 : async->pkts_idx += pkt_idx;
2169 [ # # ]: 0 : if (async->pkts_idx >= vq->size)
2170 : 0 : async->pkts_idx -= vq->size;
2171 : :
2172 : 0 : async->pkts_inflight_n += pkt_idx;
2173 : :
2174 : 0 : return pkt_idx;
2175 : : }
2176 : :
2177 : : static __rte_always_inline void
2178 : : write_back_completed_descs_split(struct vhost_virtqueue *vq, uint16_t n_descs)
2179 : : __rte_shared_locks_required(&vq->access_lock)
2180 : : {
2181 : 0 : struct vhost_async *async = vq->async;
2182 : : uint16_t nr_left = n_descs;
2183 : : uint16_t nr_copy;
2184 : : uint16_t to, from;
2185 : :
2186 : : do {
2187 : 0 : from = async->last_desc_idx_split & (vq->size - 1);
2188 [ # # # # : 0 : nr_copy = nr_left + from <= vq->size ? nr_left : vq->size - from;
# # # # #
# # # # #
# # # # ]
2189 : 0 : to = vq->last_used_idx & (vq->size - 1);
2190 : :
2191 [ # # # # : 0 : if (to + nr_copy <= vq->size) {
# # # # #
# # # # #
# # # # ]
2192 [ # # # # : 0 : rte_memcpy(&vq->used->ring[to], &async->descs_split[from],
# # # # #
# # # # #
# # # # ]
2193 : : nr_copy * sizeof(struct vring_used_elem));
2194 : : } else {
2195 : 0 : uint16_t size = vq->size - to;
2196 : :
2197 [ # # # # : 0 : rte_memcpy(&vq->used->ring[to], &async->descs_split[from],
# # # # #
# # # # #
# # # # ]
2198 : : size * sizeof(struct vring_used_elem));
2199 : 0 : rte_memcpy(&vq->used->ring[0], &async->descs_split[from + size],
2200 [ # # # # : 0 : (nr_copy - size) * sizeof(struct vring_used_elem));
# # # # #
# # # # #
# # # # ]
2201 : : }
2202 : :
2203 : 0 : async->last_desc_idx_split += nr_copy;
2204 : 0 : vq->last_used_idx += nr_copy;
2205 : 0 : nr_left -= nr_copy;
2206 [ # # # # : 0 : } while (nr_left > 0);
# # # # #
# # # # #
# # # # ]
2207 : : }
2208 : :
2209 : : static __rte_always_inline void
2210 : : write_back_completed_descs_packed(struct vhost_virtqueue *vq,
2211 : : uint16_t n_buffers)
2212 : : __rte_shared_locks_required(&vq->access_lock)
2213 : : {
2214 : 0 : struct vhost_async *async = vq->async;
2215 : 0 : uint16_t from = async->last_buffer_idx_packed;
2216 : 0 : uint16_t used_idx = vq->last_used_idx;
2217 : : uint16_t head_idx = vq->last_used_idx;
2218 : : uint16_t head_flags = 0;
2219 : : uint16_t i;
2220 : :
2221 : : /* Split loop in two to save memory barriers */
2222 [ # # # # : 0 : for (i = 0; i < n_buffers; i++) {
# # # # #
# # # # #
# # # # ]
2223 : 0 : vq->desc_packed[used_idx].id = async->buffers_packed[from].id;
2224 : 0 : vq->desc_packed[used_idx].len = async->buffers_packed[from].len;
2225 : :
2226 : 0 : used_idx += async->buffers_packed[from].count;
2227 [ # # # # : 0 : if (used_idx >= vq->size)
# # # # #
# # # # #
# # # # ]
2228 : 0 : used_idx -= vq->size;
2229 : :
2230 : 0 : from++;
2231 [ # # # # : 0 : if (from >= vq->size)
# # # # #
# # # # #
# # # # ]
2232 : : from = 0;
2233 : : }
2234 : :
2235 : : /* The ordering for storing desc flags needs to be enforced. */
2236 : : rte_atomic_thread_fence(rte_memory_order_release);
2237 : :
2238 : 0 : from = async->last_buffer_idx_packed;
2239 : :
2240 [ # # # # : 0 : for (i = 0; i < n_buffers; i++) {
# # # # #
# # # # #
# # # # ]
2241 : : uint16_t flags;
2242 : :
2243 [ # # # # : 0 : if (async->buffers_packed[from].len)
# # # # #
# # # # #
# # # # ]
2244 : : flags = VRING_DESC_F_WRITE;
2245 : : else
2246 : : flags = 0;
2247 : :
2248 [ # # # # : 0 : if (vq->used_wrap_counter) {
# # # # #
# # # # #
# # # # ]
2249 : : flags |= VRING_DESC_F_USED;
2250 : 0 : flags |= VRING_DESC_F_AVAIL;
2251 : : } else {
2252 : : flags &= ~VRING_DESC_F_USED;
2253 : : flags &= ~VRING_DESC_F_AVAIL;
2254 : : }
2255 : :
2256 [ # # # # : 0 : if (i > 0) {
# # # # #
# # # # #
# # # # ]
2257 : 0 : vq->desc_packed[vq->last_used_idx].flags = flags;
2258 : : } else {
2259 : 0 : head_idx = vq->last_used_idx;
2260 : : head_flags = flags;
2261 : : }
2262 : :
2263 [ # # # # : 0 : vq_inc_last_used_packed(vq, async->buffers_packed[from].count);
# # # # #
# # # # #
# # # # ]
2264 : :
2265 : 0 : from++;
2266 [ # # # # : 0 : if (from == vq->size)
# # # # #
# # # # #
# # # # ]
2267 : : from = 0;
2268 : : }
2269 : :
2270 : 0 : vq->desc_packed[head_idx].flags = head_flags;
2271 : 0 : async->last_buffer_idx_packed = from;
2272 : : }
2273 : :
2274 : : static __rte_always_inline uint16_t
2275 : : vhost_poll_enqueue_completed(struct virtio_net *dev, struct vhost_virtqueue *vq,
2276 : : struct rte_mbuf **pkts, uint16_t count, int16_t dma_id, uint16_t vchan_id)
2277 : : __rte_shared_locks_required(&vq->access_lock)
2278 : : {
2279 : : struct vhost_async *async = vq->async;
2280 : 0 : struct async_inflight_info *pkts_info = async->pkts_info;
2281 : : uint16_t nr_cpl_pkts = 0;
2282 : : uint16_t n_descs = 0, n_buffers = 0;
2283 : : uint16_t start_idx, from, i;
2284 : :
2285 : : /* Check completed copies for the given DMA vChannel */
2286 : : vhost_async_dma_check_completed(dev, dma_id, vchan_id, VHOST_DMA_MAX_COPY_COMPLETE);
2287 : :
2288 : : start_idx = async_get_first_inflight_pkt_idx(vq);
2289 : : /**
2290 : : * Calculate the number of copy completed packets.
2291 : : * Note that there may be completed packets even if
2292 : : * no copies are reported done by the given DMA vChannel,
2293 : : * as it's possible that a virtqueue uses multiple DMA
2294 : : * vChannels.
2295 : : */
2296 : : from = start_idx;
2297 [ # # # # : 0 : while (vq->async->pkts_cmpl_flag[from] && count--) {
# # # # #
# # # ]
2298 : 0 : vq->async->pkts_cmpl_flag[from] = false;
2299 : 0 : from++;
2300 [ # # # # : 0 : if (from >= vq->size)
# # ]
2301 : 0 : from -= vq->size;
2302 : 0 : nr_cpl_pkts++;
2303 : : }
2304 : :
2305 [ # # # # : 0 : if (nr_cpl_pkts == 0)
# # ]
2306 : : return 0;
2307 : :
2308 [ # # # # : 0 : for (i = 0; i < nr_cpl_pkts; i++) {
# # ]
2309 : 0 : from = (start_idx + i) % vq->size;
2310 : : /* Only used with packed ring */
2311 : 0 : n_buffers += pkts_info[from].nr_buffers;
2312 : : /* Only used with split ring */
2313 : 0 : n_descs += pkts_info[from].descs;
2314 : 0 : pkts[i] = pkts_info[from].mbuf;
2315 : : }
2316 : :
2317 : 0 : async->pkts_inflight_n -= nr_cpl_pkts;
2318 : :
2319 [ # # # # : 0 : if (likely(vq->enabled && vq->access_ok)) {
# # # # #
# # # ]
2320 [ # # # # : 0 : if (vq_is_packed(dev)) {
# # ]
2321 : : write_back_completed_descs_packed(vq, n_buffers);
2322 : : vhost_vring_call_packed(dev, vq);
2323 : : } else {
2324 : 0 : write_back_completed_descs_split(vq, n_descs);
2325 : 0 : rte_atomic_fetch_add_explicit(
2326 : : (unsigned short __rte_atomic *)&vq->used->idx,
2327 : : n_descs, rte_memory_order_release);
2328 : : vhost_vring_call_split(dev, vq);
2329 : : }
2330 : : } else {
2331 [ # # # # : 0 : if (vq_is_packed(dev)) {
# # ]
2332 : 0 : async->last_buffer_idx_packed += n_buffers;
2333 [ # # # # : 0 : if (async->last_buffer_idx_packed >= vq->size)
# # ]
2334 : 0 : async->last_buffer_idx_packed -= vq->size;
2335 : : } else {
2336 : 0 : async->last_desc_idx_split += n_descs;
2337 : : }
2338 : : }
2339 : :
2340 : : return nr_cpl_pkts;
2341 : : }
2342 : :
2343 : : uint16_t
2344 [ # # ]: 0 : rte_vhost_poll_enqueue_completed(int vid, uint16_t queue_id,
2345 : : struct rte_mbuf **pkts, uint16_t count, int16_t dma_id,
2346 : : uint16_t vchan_id)
2347 : : {
2348 : : struct virtio_net *dev = get_device(vid);
2349 : : struct vhost_virtqueue *vq;
2350 : : uint16_t n_pkts_cpl = 0;
2351 : :
2352 [ # # ]: 0 : if (unlikely(!dev))
2353 : : return 0;
2354 : :
2355 : : VHOST_DATA_LOG(dev->ifname, DEBUG, "%s", __func__);
2356 [ # # # # ]: 0 : if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
2357 : 0 : VHOST_DATA_LOG(dev->ifname, ERR,
2358 : : "%s: invalid virtqueue idx %d.",
2359 : : __func__, queue_id);
2360 : 0 : return 0;
2361 : : }
2362 : :
2363 [ # # # # ]: 0 : if (unlikely(!dma_copy_track[dma_id].vchans ||
2364 : : !dma_copy_track[dma_id].vchans[vchan_id].pkts_cmpl_flag_addr)) {
2365 : 0 : VHOST_DATA_LOG(dev->ifname, ERR,
2366 : : "%s: invalid channel %d:%u.",
2367 : : __func__, dma_id, vchan_id);
2368 : 0 : return 0;
2369 : : }
2370 : :
2371 : 0 : vq = dev->virtqueue[queue_id];
2372 : :
2373 [ # # ]: 0 : if (rte_rwlock_read_trylock(&vq->access_lock)) {
2374 : : VHOST_DATA_LOG(dev->ifname, DEBUG,
2375 : : "%s: virtqueue %u is busy.",
2376 : : __func__, queue_id);
2377 : : return 0;
2378 : : }
2379 : :
2380 [ # # ]: 0 : if (unlikely(!vq->async)) {
2381 : 0 : VHOST_DATA_LOG(dev->ifname, ERR,
2382 : : "%s: async not registered for virtqueue %d.",
2383 : : __func__, queue_id);
2384 : 0 : goto out;
2385 : : }
2386 : :
2387 : 0 : n_pkts_cpl = vhost_poll_enqueue_completed(dev, vq, pkts, count, dma_id, vchan_id);
2388 : :
2389 : 0 : vhost_queue_stats_update(dev, vq, pkts, n_pkts_cpl);
2390 : 0 : vq->stats.inflight_completed += n_pkts_cpl;
2391 : :
2392 : 0 : out:
2393 : : rte_rwlock_read_unlock(&vq->access_lock);
2394 : :
2395 : 0 : return n_pkts_cpl;
2396 : : }
2397 : :
2398 : : uint16_t
2399 [ # # ]: 0 : rte_vhost_clear_queue_thread_unsafe(int vid, uint16_t queue_id,
2400 : : struct rte_mbuf **pkts, uint16_t count, int16_t dma_id,
2401 : : uint16_t vchan_id)
2402 : : {
2403 : : struct virtio_net *dev = get_device(vid);
2404 : : struct vhost_virtqueue *vq;
2405 : : uint16_t n_pkts_cpl = 0;
2406 : :
2407 [ # # ]: 0 : if (!dev)
2408 : : return 0;
2409 : :
2410 : : VHOST_DATA_LOG(dev->ifname, DEBUG, "%s", __func__);
2411 [ # # ]: 0 : if (unlikely(queue_id >= dev->nr_vring)) {
2412 : 0 : VHOST_DATA_LOG(dev->ifname, ERR, "%s: invalid virtqueue idx %d.",
2413 : : __func__, queue_id);
2414 : 0 : return 0;
2415 : : }
2416 : :
2417 [ # # ]: 0 : if (unlikely(dma_id < 0 || dma_id >= RTE_DMADEV_DEFAULT_MAX)) {
2418 : 0 : VHOST_DATA_LOG(dev->ifname, ERR, "%s: invalid dma id %d.",
2419 : : __func__, dma_id);
2420 : 0 : return 0;
2421 : : }
2422 : :
2423 : 0 : vq = dev->virtqueue[queue_id];
2424 : :
2425 : 0 : vq_assert_lock(dev, vq);
2426 : :
2427 [ # # ]: 0 : if (unlikely(!vq->async)) {
2428 : 0 : VHOST_DATA_LOG(dev->ifname, ERR,
2429 : : "%s: async not registered for virtqueue %d.",
2430 : : __func__, queue_id);
2431 : 0 : return 0;
2432 : : }
2433 : :
2434 [ # # # # ]: 0 : if (unlikely(!dma_copy_track[dma_id].vchans ||
2435 : : !dma_copy_track[dma_id].vchans[vchan_id].pkts_cmpl_flag_addr)) {
2436 : 0 : VHOST_DATA_LOG(dev->ifname, ERR,
2437 : : "%s: invalid channel %d:%u.",
2438 : : __func__, dma_id, vchan_id);
2439 : 0 : return 0;
2440 : : }
2441 : :
2442 [ # # ]: 0 : if ((queue_id & 1) == 0)
2443 : 0 : n_pkts_cpl = vhost_poll_enqueue_completed(dev, vq, pkts, count,
2444 : : dma_id, vchan_id);
2445 : : else
2446 : 0 : n_pkts_cpl = async_poll_dequeue_completed(dev, vq, pkts, count,
2447 : 0 : dma_id, vchan_id, dev->flags & VIRTIO_DEV_LEGACY_OL_FLAGS);
2448 : :
2449 : 0 : vhost_queue_stats_update(dev, vq, pkts, n_pkts_cpl);
2450 : 0 : vq->stats.inflight_completed += n_pkts_cpl;
2451 : :
2452 : 0 : return n_pkts_cpl;
2453 : : }
2454 : :
2455 : : uint16_t
2456 [ # # ]: 0 : rte_vhost_clear_queue(int vid, uint16_t queue_id, struct rte_mbuf **pkts,
2457 : : uint16_t count, int16_t dma_id, uint16_t vchan_id)
2458 : : {
2459 : : struct virtio_net *dev = get_device(vid);
2460 : : struct vhost_virtqueue *vq;
2461 : : uint16_t n_pkts_cpl = 0;
2462 : :
2463 [ # # ]: 0 : if (!dev)
2464 : : return 0;
2465 : :
2466 : : VHOST_DATA_LOG(dev->ifname, DEBUG, "%s", __func__);
2467 [ # # ]: 0 : if (unlikely(queue_id >= dev->nr_vring)) {
2468 : 0 : VHOST_DATA_LOG(dev->ifname, ERR, "%s: invalid virtqueue idx %u.",
2469 : : __func__, queue_id);
2470 : 0 : return 0;
2471 : : }
2472 : :
2473 [ # # ]: 0 : if (unlikely(dma_id < 0 || dma_id >= RTE_DMADEV_DEFAULT_MAX)) {
2474 : 0 : VHOST_DATA_LOG(dev->ifname, ERR, "%s: invalid dma id %d.",
2475 : : __func__, dma_id);
2476 : 0 : return 0;
2477 : : }
2478 : :
2479 : 0 : vq = dev->virtqueue[queue_id];
2480 : :
2481 [ # # ]: 0 : if (rte_rwlock_read_trylock(&vq->access_lock)) {
2482 : : VHOST_DATA_LOG(dev->ifname, DEBUG, "%s: virtqueue %u is busy.",
2483 : : __func__, queue_id);
2484 : : return 0;
2485 : : }
2486 : :
2487 [ # # ]: 0 : if (unlikely(!vq->async)) {
2488 : 0 : VHOST_DATA_LOG(dev->ifname, ERR, "%s: async not registered for queue id %u.",
2489 : : __func__, queue_id);
2490 : 0 : goto out_access_unlock;
2491 : : }
2492 : :
2493 [ # # # # ]: 0 : if (unlikely(!dma_copy_track[dma_id].vchans ||
2494 : : !dma_copy_track[dma_id].vchans[vchan_id].pkts_cmpl_flag_addr)) {
2495 : 0 : VHOST_DATA_LOG(dev->ifname, ERR, "%s: invalid channel %d:%u.",
2496 : : __func__, dma_id, vchan_id);
2497 : 0 : goto out_access_unlock;
2498 : : }
2499 : :
2500 [ # # ]: 0 : if ((queue_id & 1) == 0)
2501 : 0 : n_pkts_cpl = vhost_poll_enqueue_completed(dev, vq, pkts, count,
2502 : : dma_id, vchan_id);
2503 : : else
2504 : 0 : n_pkts_cpl = async_poll_dequeue_completed(dev, vq, pkts, count,
2505 : 0 : dma_id, vchan_id, dev->flags & VIRTIO_DEV_LEGACY_OL_FLAGS);
2506 : :
2507 : 0 : vhost_queue_stats_update(dev, vq, pkts, n_pkts_cpl);
2508 : 0 : vq->stats.inflight_completed += n_pkts_cpl;
2509 : :
2510 : 0 : out_access_unlock:
2511 : : rte_rwlock_read_unlock(&vq->access_lock);
2512 : :
2513 : 0 : return n_pkts_cpl;
2514 : : }
2515 : :
2516 : : static __rte_always_inline uint32_t
2517 : : virtio_dev_rx_async_submit(struct virtio_net *dev, struct vhost_virtqueue *vq,
2518 : : struct rte_mbuf **pkts, uint32_t count, int16_t dma_id, uint16_t vchan_id)
2519 : : {
2520 : : uint32_t nb_tx = 0;
2521 : :
2522 : : VHOST_DATA_LOG(dev->ifname, DEBUG, "%s", __func__);
2523 : :
2524 [ # # ]: 0 : if (unlikely(!dma_copy_track[dma_id].vchans ||
2525 : : !dma_copy_track[dma_id].vchans[vchan_id].pkts_cmpl_flag_addr)) {
2526 : 0 : VHOST_DATA_LOG(dev->ifname, ERR,
2527 : : "%s: invalid channel %d:%u.",
2528 : : __func__, dma_id, vchan_id);
2529 : 0 : return 0;
2530 : : }
2531 : :
2532 : 0 : rte_rwlock_write_lock(&vq->access_lock);
2533 : :
2534 [ # # # # ]: 0 : if (unlikely(!vq->enabled || !vq->async))
2535 : 0 : goto out_access_unlock;
2536 : :
2537 : : vhost_user_iotlb_rd_lock(vq);
2538 : :
2539 [ # # ]: 0 : if (unlikely(!vq->access_ok)) {
2540 : : vhost_user_iotlb_rd_unlock(vq);
2541 : : rte_rwlock_write_unlock(&vq->access_lock);
2542 : :
2543 : 0 : virtio_dev_vring_translate(dev, vq);
2544 : 0 : goto out_no_unlock;
2545 : : }
2546 : :
2547 : 0 : count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
2548 [ # # ]: 0 : if (count == 0)
2549 : 0 : goto out;
2550 : :
2551 [ # # ]: 0 : if (vq_is_packed(dev))
2552 : 0 : nb_tx = virtio_dev_rx_async_submit_packed(dev, vq, pkts, count,
2553 : : dma_id, vchan_id);
2554 : : else
2555 : 0 : nb_tx = virtio_dev_rx_async_submit_split(dev, vq, pkts, count,
2556 : : dma_id, vchan_id);
2557 : :
2558 : 0 : vq->stats.inflight_submitted += nb_tx;
2559 : :
2560 : 0 : out:
2561 : : vhost_user_iotlb_rd_unlock(vq);
2562 : :
2563 : 0 : out_access_unlock:
2564 : : rte_rwlock_write_unlock(&vq->access_lock);
2565 : :
2566 : : out_no_unlock:
2567 : : return nb_tx;
2568 : : }
2569 : :
2570 : : uint16_t
2571 [ # # ]: 0 : rte_vhost_submit_enqueue_burst(int vid, uint16_t queue_id,
2572 : : struct rte_mbuf **pkts, uint16_t count, int16_t dma_id,
2573 : : uint16_t vchan_id)
2574 : : {
2575 : : struct virtio_net *dev = get_device(vid);
2576 : :
2577 [ # # ]: 0 : if (!dev)
2578 : : return 0;
2579 : :
2580 [ # # ]: 0 : if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
2581 : 0 : VHOST_DATA_LOG(dev->ifname, ERR,
2582 : : "%s: built-in vhost net backend is disabled.",
2583 : : __func__);
2584 : 0 : return 0;
2585 : : }
2586 : :
2587 [ # # # # ]: 0 : if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
2588 : 0 : VHOST_DATA_LOG(dev->ifname, ERR,
2589 : : "%s: invalid virtqueue idx %d.",
2590 : : __func__, queue_id);
2591 : 0 : return 0;
2592 : : }
2593 : :
2594 [ # # ]: 0 : return virtio_dev_rx_async_submit(dev, dev->virtqueue[queue_id], pkts, count,
2595 : : dma_id, vchan_id);
2596 : : }
2597 : :
2598 : : static inline bool
2599 : : virtio_net_with_host_offload(struct virtio_net *dev)
2600 : : {
2601 [ # # # # : 0 : if (dev->features &
# # # # ]
2602 : : ((1ULL << VIRTIO_NET_F_CSUM) |
2603 : : (1ULL << VIRTIO_NET_F_HOST_ECN) |
2604 : : (1ULL << VIRTIO_NET_F_HOST_TSO4) |
2605 : : (1ULL << VIRTIO_NET_F_HOST_TSO6) |
2606 : : (1ULL << VIRTIO_NET_F_HOST_UFO)))
2607 : : return true;
2608 : :
2609 : : return false;
2610 : : }
2611 : :
2612 : : static int
2613 : 0 : parse_headers(struct rte_mbuf *m, uint8_t *l4_proto)
2614 : : {
2615 : : struct rte_ipv4_hdr *ipv4_hdr;
2616 : : struct rte_ipv6_hdr *ipv6_hdr;
2617 : : struct rte_ether_hdr *eth_hdr;
2618 : : uint16_t ethertype;
2619 : 0 : uint16_t data_len = rte_pktmbuf_data_len(m);
2620 : :
2621 [ # # ]: 0 : if (data_len < sizeof(struct rte_ether_hdr))
2622 : : return -EINVAL;
2623 : :
2624 : 0 : eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
2625 : :
2626 : 0 : m->l2_len = sizeof(struct rte_ether_hdr);
2627 [ # # ]: 0 : ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
2628 : :
2629 [ # # ]: 0 : if (ethertype == RTE_ETHER_TYPE_VLAN) {
2630 [ # # ]: 0 : if (data_len < sizeof(struct rte_ether_hdr) +
2631 : : sizeof(struct rte_vlan_hdr))
2632 : 0 : goto error;
2633 : :
2634 : : struct rte_vlan_hdr *vlan_hdr =
2635 : : (struct rte_vlan_hdr *)(eth_hdr + 1);
2636 : :
2637 : 0 : m->l2_len += sizeof(struct rte_vlan_hdr);
2638 [ # # ]: 0 : ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
2639 : : }
2640 : :
2641 [ # # # ]: 0 : switch (ethertype) {
2642 : 0 : case RTE_ETHER_TYPE_IPV4:
2643 [ # # ]: 0 : if (data_len < m->l2_len + sizeof(struct rte_ipv4_hdr))
2644 : 0 : goto error;
2645 [ # # ]: 0 : ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv4_hdr *,
2646 : : m->l2_len);
2647 : 0 : m->l3_len = rte_ipv4_hdr_len(ipv4_hdr);
2648 [ # # ]: 0 : if (data_len < m->l2_len + m->l3_len)
2649 : 0 : goto error;
2650 : 0 : m->ol_flags |= RTE_MBUF_F_TX_IPV4;
2651 : 0 : *l4_proto = ipv4_hdr->next_proto_id;
2652 : 0 : break;
2653 : 0 : case RTE_ETHER_TYPE_IPV6:
2654 [ # # ]: 0 : if (data_len < m->l2_len + sizeof(struct rte_ipv6_hdr))
2655 : 0 : goto error;
2656 : 0 : ipv6_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv6_hdr *,
2657 : : m->l2_len);
2658 : 0 : m->l3_len = sizeof(struct rte_ipv6_hdr);
2659 : 0 : m->ol_flags |= RTE_MBUF_F_TX_IPV6;
2660 : 0 : *l4_proto = ipv6_hdr->proto;
2661 : 0 : break;
2662 : 0 : default:
2663 : : /* a valid L3 header is needed for further L4 parsing */
2664 : 0 : goto error;
2665 : : }
2666 : :
2667 : : /* both CSUM and GSO need a valid L4 header */
2668 [ # # # # ]: 0 : switch (*l4_proto) {
2669 : 0 : case IPPROTO_TCP:
2670 [ # # ]: 0 : if (data_len < m->l2_len + m->l3_len +
2671 : : sizeof(struct rte_tcp_hdr))
2672 : 0 : goto error;
2673 : : break;
2674 : 0 : case IPPROTO_UDP:
2675 [ # # ]: 0 : if (data_len < m->l2_len + m->l3_len +
2676 : : sizeof(struct rte_udp_hdr))
2677 : 0 : goto error;
2678 : : break;
2679 : 0 : case IPPROTO_SCTP:
2680 [ # # ]: 0 : if (data_len < m->l2_len + m->l3_len +
2681 : : sizeof(struct rte_sctp_hdr))
2682 : 0 : goto error;
2683 : : break;
2684 : 0 : default:
2685 : 0 : goto error;
2686 : : }
2687 : :
2688 : : return 0;
2689 : :
2690 : 0 : error:
2691 : 0 : m->l2_len = 0;
2692 : 0 : m->l3_len = 0;
2693 : 0 : m->ol_flags = 0;
2694 : 0 : return -EINVAL;
2695 : : }
2696 : :
2697 : : static __rte_always_inline void
2698 : : vhost_dequeue_offload_legacy(struct virtio_net *dev, struct virtio_net_hdr *hdr,
2699 : : struct rte_mbuf *m)
2700 : : {
2701 : 0 : uint8_t l4_proto = 0;
2702 : : struct rte_tcp_hdr *tcp_hdr = NULL;
2703 : : uint16_t tcp_len;
2704 : 0 : uint16_t data_len = rte_pktmbuf_data_len(m);
2705 : :
2706 [ # # # # : 0 : if (parse_headers(m, &l4_proto) < 0)
# # # # #
# # # #
# ]
2707 : 0 : return;
2708 : :
2709 [ # # # # : 0 : if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
# # # # #
# # # #
# ]
2710 [ # # # # : 0 : if (hdr->csum_start == (m->l2_len + m->l3_len)) {
# # # # #
# # # #
# ]
2711 [ # # # # : 0 : switch (hdr->csum_offset) {
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
2712 : 0 : case (offsetof(struct rte_tcp_hdr, cksum)):
2713 [ # # # # : 0 : if (l4_proto != IPPROTO_TCP)
# # # # #
# # # #
# ]
2714 : 0 : goto error;
2715 : 0 : m->ol_flags |= RTE_MBUF_F_TX_TCP_CKSUM;
2716 : 0 : break;
2717 : 0 : case (offsetof(struct rte_udp_hdr, dgram_cksum)):
2718 [ # # # # : 0 : if (l4_proto != IPPROTO_UDP)
# # # # #
# # # #
# ]
2719 : 0 : goto error;
2720 : 0 : m->ol_flags |= RTE_MBUF_F_TX_UDP_CKSUM;
2721 : 0 : break;
2722 : 0 : case (offsetof(struct rte_sctp_hdr, cksum)):
2723 [ # # # # : 0 : if (l4_proto != IPPROTO_SCTP)
# # # # #
# # # #
# ]
2724 : 0 : goto error;
2725 : 0 : m->ol_flags |= RTE_MBUF_F_TX_SCTP_CKSUM;
2726 : 0 : break;
2727 : 0 : default:
2728 : 0 : goto error;
2729 : : }
2730 : : } else {
2731 : 0 : goto error;
2732 : : }
2733 : : }
2734 : :
2735 [ # # # # : 0 : if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
# # # # #
# # # #
# ]
2736 [ # # # # : 0 : switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
# # # # #
# # # # #
# # # # #
# # ]
2737 : 0 : case VIRTIO_NET_HDR_GSO_TCPV4:
2738 : : case VIRTIO_NET_HDR_GSO_TCPV6:
2739 [ # # # # : 0 : if (l4_proto != IPPROTO_TCP)
# # # # #
# # # #
# ]
2740 : 0 : goto error;
2741 : 0 : tcp_hdr = rte_pktmbuf_mtod_offset(m,
2742 : : struct rte_tcp_hdr *,
2743 : : m->l2_len + m->l3_len);
2744 : 0 : tcp_len = (tcp_hdr->data_off & 0xf0) >> 2;
2745 [ # # # # : 0 : if (data_len < m->l2_len + m->l3_len + tcp_len)
# # # # #
# # # #
# ]
2746 : 0 : goto error;
2747 : 0 : m->ol_flags |= RTE_MBUF_F_TX_TCP_SEG;
2748 : 0 : m->tso_segsz = hdr->gso_size;
2749 : 0 : m->l4_len = tcp_len;
2750 : 0 : break;
2751 : 0 : case VIRTIO_NET_HDR_GSO_UDP:
2752 [ # # # # : 0 : if (l4_proto != IPPROTO_UDP)
# # # # #
# # # #
# ]
2753 : 0 : goto error;
2754 : 0 : m->ol_flags |= RTE_MBUF_F_TX_UDP_SEG;
2755 : 0 : m->tso_segsz = hdr->gso_size;
2756 : 0 : m->l4_len = sizeof(struct rte_udp_hdr);
2757 : 0 : break;
2758 : 0 : default:
2759 : 0 : VHOST_DATA_LOG(dev->ifname, WARNING,
2760 : : "unsupported gso type %u.",
2761 : : hdr->gso_type);
2762 : 0 : goto error;
2763 : : }
2764 : : }
2765 : : return;
2766 : :
2767 : 0 : error:
2768 : 0 : m->l2_len = 0;
2769 : 0 : m->l3_len = 0;
2770 : 0 : m->ol_flags = 0;
2771 : : }
2772 : :
2773 : : static __rte_always_inline void
2774 : : vhost_dequeue_offload(struct virtio_net *dev, struct virtio_net_hdr *hdr,
2775 : : struct rte_mbuf *m, bool legacy_ol_flags)
2776 : : {
2777 : : struct rte_net_hdr_lens hdr_lens;
2778 : : int l4_supported = 0;
2779 : : uint32_t ptype;
2780 : :
2781 [ # # # # : 0 : if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
# # # # #
# # # # #
# # # # #
# ]
2782 : 0 : return;
2783 : :
2784 [ # # # # ]: 0 : if (legacy_ol_flags) {
2785 : : vhost_dequeue_offload_legacy(dev, hdr, m);
2786 : : return;
2787 : : }
2788 : :
2789 : : m->ol_flags |= RTE_MBUF_F_RX_IP_CKSUM_UNKNOWN;
2790 : :
2791 : 0 : ptype = rte_net_get_ptype(m, &hdr_lens, RTE_PTYPE_ALL_MASK);
2792 : 0 : m->packet_type = ptype;
2793 [ # # # # : 0 : if ((ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_TCP ||
# # # # #
# # # #
# ]
2794 [ # # # # : 0 : (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_UDP ||
# # # # #
# # # #
# ]
2795 : : (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_SCTP)
2796 : : l4_supported = 1;
2797 : :
2798 : : /* According to Virtio 1.1 spec, the device only needs to look at
2799 : : * VIRTIO_NET_HDR_F_NEEDS_CSUM in the packet transmission path.
2800 : : * This differs from the processing incoming packets path where the
2801 : : * driver could rely on VIRTIO_NET_HDR_F_DATA_VALID flag set by the
2802 : : * device.
2803 : : *
2804 : : * 5.1.6.2.1 Driver Requirements: Packet Transmission
2805 : : * The driver MUST NOT set the VIRTIO_NET_HDR_F_DATA_VALID and
2806 : : * VIRTIO_NET_HDR_F_RSC_INFO bits in flags.
2807 : : *
2808 : : * 5.1.6.2.2 Device Requirements: Packet Transmission
2809 : : * The device MUST ignore flag bits that it does not recognize.
2810 : : */
2811 [ # # # # : 0 : if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
# # # # #
# # # #
# ]
2812 : : uint32_t hdrlen;
2813 : :
2814 : 0 : hdrlen = hdr_lens.l2_len + hdr_lens.l3_len + hdr_lens.l4_len;
2815 [ # # # # : 0 : if (hdr->csum_start <= hdrlen && l4_supported != 0) {
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
2816 : 0 : m->ol_flags |= RTE_MBUF_F_RX_L4_CKSUM_NONE;
2817 : : } else {
2818 : : /* Unknown proto or tunnel, do sw cksum. We can assume
2819 : : * the cksum field is in the first segment since the
2820 : : * buffers we provided to the host are large enough.
2821 : : * In case of SCTP, this will be wrong since it's a CRC
2822 : : * but there's nothing we can do.
2823 : : */
2824 : 0 : uint16_t csum = 0, off;
2825 : :
2826 [ # # # # : 0 : if (hdr->csum_start >= rte_pktmbuf_pkt_len(m))
# # # # #
# # # #
# ]
2827 : 0 : return;
2828 : :
2829 [ # # # # : 0 : if (rte_raw_cksum_mbuf(m, hdr->csum_start,
# # # # #
# # # #
# ]
2830 : : rte_pktmbuf_pkt_len(m) - hdr->csum_start, &csum) < 0)
2831 : : return;
2832 [ # # # # : 0 : if (likely(csum != 0xffff))
# # # # #
# # # #
# ]
2833 : 0 : csum = ~csum;
2834 : 0 : off = hdr->csum_offset + hdr->csum_start;
2835 [ # # # # : 0 : if (rte_pktmbuf_data_len(m) >= off + 1)
# # # # #
# # # #
# ]
2836 : 0 : *rte_pktmbuf_mtod_offset(m, uint16_t *, off) = csum;
2837 : : }
2838 : : }
2839 : :
2840 [ # # # # : 0 : if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
# # # # #
# # # #
# ]
2841 [ # # # # : 0 : if (hdr->gso_size == 0)
# # # # #
# # # #
# ]
2842 : : return;
2843 : :
2844 [ # # # # : 0 : switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
# # # # #
# # # # #
# # # # #
# # ]
2845 : 0 : case VIRTIO_NET_HDR_GSO_TCPV4:
2846 : : case VIRTIO_NET_HDR_GSO_TCPV6:
2847 [ # # # # : 0 : if ((ptype & RTE_PTYPE_L4_MASK) != RTE_PTYPE_L4_TCP)
# # # # #
# # # #
# ]
2848 : : break;
2849 : 0 : m->ol_flags |= RTE_MBUF_F_RX_LRO | RTE_MBUF_F_RX_L4_CKSUM_NONE;
2850 : 0 : m->tso_segsz = hdr->gso_size;
2851 : 0 : break;
2852 : 0 : case VIRTIO_NET_HDR_GSO_UDP:
2853 [ # # # # : 0 : if ((ptype & RTE_PTYPE_L4_MASK) != RTE_PTYPE_L4_UDP)
# # # # #
# # # #
# ]
2854 : : break;
2855 : 0 : m->ol_flags |= RTE_MBUF_F_RX_LRO | RTE_MBUF_F_RX_L4_CKSUM_NONE;
2856 : 0 : m->tso_segsz = hdr->gso_size;
2857 : 0 : break;
2858 : : default:
2859 : : break;
2860 : : }
2861 : : }
2862 : : }
2863 : :
2864 : : static __rte_noinline void
2865 : 0 : copy_vnet_hdr_from_desc(struct virtio_net_hdr *hdr,
2866 : : struct buf_vector *buf_vec)
2867 : : {
2868 : : uint64_t len;
2869 : : uint64_t remain = sizeof(struct virtio_net_hdr);
2870 : : uint64_t src;
2871 : 0 : uint64_t dst = (uint64_t)(uintptr_t)hdr;
2872 : :
2873 [ # # ]: 0 : while (remain) {
2874 : 0 : len = RTE_MIN(remain, buf_vec->buf_len);
2875 : 0 : src = buf_vec->buf_addr;
2876 [ # # ]: 0 : rte_memcpy((void *)(uintptr_t)dst,
2877 : : (void *)(uintptr_t)src, len);
2878 : :
2879 : 0 : remain -= len;
2880 : 0 : dst += len;
2881 : 0 : buf_vec++;
2882 : : }
2883 : 0 : }
2884 : :
2885 : : static __rte_always_inline int
2886 : : desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
2887 : : struct buf_vector *buf_vec, uint16_t nr_vec,
2888 : : struct rte_mbuf *m, struct rte_mempool *mbuf_pool,
2889 : : bool legacy_ol_flags, uint16_t slot_idx, bool is_async)
2890 : : __rte_shared_locks_required(&vq->access_lock)
2891 : : __rte_shared_locks_required(&vq->iotlb_lock)
2892 : : {
2893 : : uint32_t buf_avail, buf_offset, buf_len;
2894 : : uint64_t buf_addr, buf_iova;
2895 : : uint32_t mbuf_avail, mbuf_offset;
2896 [ # # # # ]: 0 : uint32_t hdr_remain = dev->vhost_hlen;
2897 : : uint32_t cpy_len;
2898 : : struct rte_mbuf *cur = m, *prev = m;
2899 : : struct virtio_net_hdr tmp_hdr;
2900 : : struct virtio_net_hdr *hdr = NULL;
2901 : : uint16_t vec_idx;
2902 [ # # # # ]: 0 : struct vhost_async *async = vq->async;
2903 : : struct async_inflight_info *pkts_info;
2904 : :
2905 : : /*
2906 : : * The caller has checked the descriptors chain is larger than the
2907 : : * header size.
2908 : : */
2909 : :
2910 : : if (virtio_net_with_host_offload(dev)) {
2911 [ # # # # : 0 : if (unlikely(buf_vec[0].buf_len < sizeof(struct virtio_net_hdr))) {
# # # # #
# # # # #
# # ]
2912 : : /*
2913 : : * No luck, the virtio-net header doesn't fit
2914 : : * in a contiguous virtual area.
2915 : : */
2916 : 0 : copy_vnet_hdr_from_desc(&tmp_hdr, buf_vec);
2917 : : hdr = &tmp_hdr;
2918 : : } else {
2919 : 0 : hdr = (struct virtio_net_hdr *)((uintptr_t)buf_vec[0].buf_addr);
2920 : : }
2921 : : }
2922 : :
2923 [ # # # # : 0 : for (vec_idx = 0; vec_idx < nr_vec; vec_idx++) {
# # # # #
# # # # #
# # ]
2924 [ # # # # : 0 : if (buf_vec[vec_idx].buf_len > hdr_remain)
# # # # #
# # # # #
# # ]
2925 : : break;
2926 : :
2927 : 0 : hdr_remain -= buf_vec[vec_idx].buf_len;
2928 : : }
2929 : :
2930 : 0 : buf_addr = buf_vec[vec_idx].buf_addr;
2931 : 0 : buf_iova = buf_vec[vec_idx].buf_iova;
2932 : 0 : buf_len = buf_vec[vec_idx].buf_len;
2933 : : buf_offset = hdr_remain;
2934 : 0 : buf_avail = buf_vec[vec_idx].buf_len - hdr_remain;
2935 : :
2936 : : PRINT_PACKET(dev,
2937 : : (uintptr_t)(buf_addr + buf_offset),
2938 : : (uint32_t)buf_avail, 0);
2939 : :
2940 : : mbuf_offset = 0;
2941 : 0 : mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
2942 : :
2943 : : if (is_async) {
2944 [ # # # # : 0 : pkts_info = async->pkts_info;
# # # # ]
2945 : : if (async_iter_initialize(dev, async))
2946 : 0 : return -1;
2947 : : }
2948 : :
2949 : : while (1) {
2950 : 0 : cpy_len = RTE_MIN(buf_avail, mbuf_avail);
2951 : :
2952 : : if (is_async) {
2953 [ # # # # : 0 : if (async_fill_seg(dev, vq, cur, mbuf_offset,
# # # # ]
2954 : : buf_iova + buf_offset, cpy_len, false) < 0)
2955 : 0 : goto error;
2956 [ # # # # : 0 : } else if (likely(hdr && cur == m)) {
# # # # ]
2957 : 0 : rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *, mbuf_offset),
2958 [ # # # # : 0 : (void *)((uintptr_t)(buf_addr + buf_offset)),
# # # # ]
2959 : : cpy_len);
2960 : : } else {
2961 [ # # # # : 0 : sync_fill_seg(dev, vq, cur, mbuf_offset,
# # # # ]
2962 : : buf_addr + buf_offset,
2963 : : buf_iova + buf_offset, cpy_len, false);
2964 : : }
2965 : :
2966 : 0 : mbuf_avail -= cpy_len;
2967 : 0 : mbuf_offset += cpy_len;
2968 : 0 : buf_avail -= cpy_len;
2969 : 0 : buf_offset += cpy_len;
2970 : :
2971 : : /* This buf reaches to its end, get the next one */
2972 [ # # # # : 0 : if (buf_avail == 0) {
# # # # #
# # # # #
# # ]
2973 [ # # # # : 0 : if (++vec_idx >= nr_vec)
# # # # #
# # # # #
# # ]
2974 : : break;
2975 : :
2976 : 0 : buf_addr = buf_vec[vec_idx].buf_addr;
2977 : 0 : buf_iova = buf_vec[vec_idx].buf_iova;
2978 : 0 : buf_len = buf_vec[vec_idx].buf_len;
2979 : :
2980 : : buf_offset = 0;
2981 : : buf_avail = buf_len;
2982 : :
2983 : : PRINT_PACKET(dev, (uintptr_t)buf_addr,
2984 : : (uint32_t)buf_avail, 0);
2985 : : }
2986 : :
2987 : : /*
2988 : : * This mbuf reaches to its end, get a new one
2989 : : * to hold more data.
2990 : : */
2991 [ # # # # : 0 : if (mbuf_avail == 0) {
# # # # #
# # # # #
# # ]
2992 : 0 : cur = rte_pktmbuf_alloc(mbuf_pool);
2993 [ # # # # : 0 : if (unlikely(cur == NULL)) {
# # # # #
# # # # #
# # ]
2994 : 0 : vq->stats.mbuf_alloc_failed++;
2995 : 0 : VHOST_DATA_LOG(dev->ifname, ERR,
2996 : : "failed to allocate memory for mbuf.");
2997 : 0 : goto error;
2998 : : }
2999 : :
3000 : 0 : prev->next = cur;
3001 : 0 : prev->data_len = mbuf_offset;
3002 : 0 : m->nb_segs += 1;
3003 : 0 : m->pkt_len += mbuf_offset;
3004 : : prev = cur;
3005 : :
3006 : : mbuf_offset = 0;
3007 : 0 : mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
3008 : : }
3009 : : }
3010 : :
3011 : 0 : prev->data_len = mbuf_offset;
3012 : 0 : m->pkt_len += mbuf_offset;
3013 : :
3014 : : if (is_async) {
3015 : : async_iter_finalize(async);
3016 [ # # # # : 0 : if (hdr)
# # # # ]
3017 : 0 : pkts_info[slot_idx].nethdr = *hdr;
3018 [ # # # # : 0 : } else if (hdr) {
# # # # ]
3019 : : vhost_dequeue_offload(dev, hdr, m, legacy_ol_flags);
3020 : : }
3021 : :
3022 : : return 0;
3023 : 0 : error:
3024 : : if (is_async)
3025 : : async_iter_cancel(async);
3026 : :
3027 : : return -1;
3028 : : }
3029 : :
3030 : : static void
3031 : 0 : virtio_dev_extbuf_free(void *addr __rte_unused, void *opaque)
3032 : : {
3033 : 0 : rte_free(opaque);
3034 : 0 : }
3035 : :
3036 : : static int
3037 : 0 : virtio_dev_extbuf_alloc(struct virtio_net *dev, struct rte_mbuf *pkt, uint32_t size)
3038 : : {
3039 : : struct rte_mbuf_ext_shared_info *shinfo = NULL;
3040 : : uint32_t total_len = RTE_PKTMBUF_HEADROOM + size;
3041 : : uint16_t buf_len;
3042 : : rte_iova_t iova;
3043 : : void *buf;
3044 : :
3045 : : total_len += sizeof(*shinfo) + sizeof(uintptr_t);
3046 : 0 : total_len = RTE_ALIGN_CEIL(total_len, sizeof(uintptr_t));
3047 : :
3048 [ # # ]: 0 : if (unlikely(total_len > UINT16_MAX))
3049 : : return -ENOSPC;
3050 : :
3051 : 0 : buf_len = total_len;
3052 : 0 : buf = rte_malloc(NULL, buf_len, RTE_CACHE_LINE_SIZE);
3053 [ # # ]: 0 : if (unlikely(buf == NULL))
3054 : : return -ENOMEM;
3055 : :
3056 : : /* Initialize shinfo */
3057 : : shinfo = rte_pktmbuf_ext_shinfo_init_helper(buf, &buf_len,
3058 : : virtio_dev_extbuf_free, buf);
3059 [ # # ]: 0 : if (unlikely(shinfo == NULL)) {
3060 : 0 : rte_free(buf);
3061 : 0 : VHOST_DATA_LOG(dev->ifname, ERR, "failed to init shinfo");
3062 : 0 : return -1;
3063 : : }
3064 : :
3065 : 0 : iova = rte_malloc_virt2iova(buf);
3066 : : rte_pktmbuf_attach_extbuf(pkt, buf, iova, buf_len, shinfo);
3067 : : rte_pktmbuf_reset_headroom(pkt);
3068 : :
3069 : 0 : return 0;
3070 : : }
3071 : :
3072 : : /*
3073 : : * Prepare a host supported pktmbuf.
3074 : : */
3075 : : static __rte_always_inline int
3076 : : virtio_dev_pktmbuf_prep(struct virtio_net *dev, struct rte_mbuf *pkt,
3077 : : uint32_t data_len)
3078 : : {
3079 [ # # # # : 0 : if (rte_pktmbuf_tailroom(pkt) >= data_len)
# # # # #
# # # # #
# # # # #
# # # #
# ]
3080 : : return 0;
3081 : :
3082 : : /* attach an external buffer if supported */
3083 [ # # # # : 0 : if (dev->extbuf && !virtio_dev_extbuf_alloc(dev, pkt, data_len))
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
3084 : : return 0;
3085 : :
3086 : : /* check if chained buffers are allowed */
3087 [ # # # # : 0 : if (!dev->linearbuf)
# # # # #
# # # # #
# # # # #
# # # #
# ]
3088 : 0 : return 0;
3089 : :
3090 : : return -1;
3091 : : }
3092 : :
3093 : : __rte_always_inline
3094 : : static uint16_t
3095 : : virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
3096 : : struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count,
3097 : : bool legacy_ol_flags)
3098 : : __rte_shared_locks_required(&vq->access_lock)
3099 : : __rte_shared_locks_required(&vq->iotlb_lock)
3100 : : {
3101 : : uint16_t i;
3102 : : uint16_t avail_entries;
3103 : : static bool allocerr_warned;
3104 : :
3105 : : /*
3106 : : * The ordering between avail index and
3107 : : * desc reads needs to be enforced.
3108 : : */
3109 : 0 : avail_entries = rte_atomic_load_explicit((unsigned short __rte_atomic *)&vq->avail->idx,
3110 : 0 : rte_memory_order_acquire) - vq->last_avail_idx;
3111 : 0 : if (avail_entries == 0)
3112 : : return 0;
3113 : :
3114 : 0 : rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
3115 : :
3116 : : VHOST_DATA_LOG(dev->ifname, DEBUG, "%s", __func__);
3117 : :
3118 : 0 : count = RTE_MIN(count, MAX_PKT_BURST);
3119 : 0 : count = RTE_MIN(count, avail_entries);
3120 : : VHOST_DATA_LOG(dev->ifname, DEBUG, "about to dequeue %u buffers", count);
3121 : :
3122 [ # # # # ]: 0 : if (rte_pktmbuf_alloc_bulk(mbuf_pool, pkts, count)) {
3123 : 0 : vq->stats.mbuf_alloc_failed += count;
3124 : 0 : return 0;
3125 : : }
3126 : :
3127 [ # # # # ]: 0 : for (i = 0; i < count; i++) {
3128 : : struct buf_vector buf_vec[BUF_VECTOR_MAX];
3129 : : uint16_t head_idx;
3130 : : uint32_t buf_len;
3131 : : uint16_t nr_vec = 0;
3132 : : int err;
3133 : :
3134 [ # # # # : 0 : if (unlikely(fill_vec_buf_split(dev, vq,
# # # # ]
3135 : : vq->last_avail_idx + i,
3136 : : &nr_vec, buf_vec,
3137 : : &head_idx, &buf_len,
3138 : : VHOST_ACCESS_RO) < 0))
3139 : : break;
3140 : :
3141 : : update_shadow_used_ring_split(vq, head_idx, 0);
3142 : :
3143 [ # # # # ]: 0 : if (unlikely(buf_len <= dev->vhost_hlen))
3144 : : break;
3145 : :
3146 : 0 : buf_len -= dev->vhost_hlen;
3147 : :
3148 [ # # # # ]: 0 : err = virtio_dev_pktmbuf_prep(dev, pkts[i], buf_len);
3149 [ # # # # ]: 0 : if (unlikely(err)) {
3150 : : /*
3151 : : * mbuf allocation fails for jumbo packets when external
3152 : : * buffer allocation is not allowed and linear buffer
3153 : : * is required. Drop this packet.
3154 : : */
3155 [ # # # # ]: 0 : if (!allocerr_warned) {
3156 : 0 : VHOST_DATA_LOG(dev->ifname, ERR,
3157 : : "failed mbuf alloc of size %d from %s.",
3158 : : buf_len, mbuf_pool->name);
3159 : 0 : allocerr_warned = true;
3160 : : }
3161 : : break;
3162 : : }
3163 : :
3164 [ # # # # ]: 0 : err = desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
3165 : : mbuf_pool, legacy_ol_flags, 0, false);
3166 [ # # # # ]: 0 : if (unlikely(err)) {
3167 [ # # # # ]: 0 : if (!allocerr_warned) {
3168 : 0 : VHOST_DATA_LOG(dev->ifname, ERR, "failed to copy desc to mbuf.");
3169 : 0 : allocerr_warned = true;
3170 : : }
3171 : : break;
3172 : : }
3173 : : }
3174 : :
3175 [ # # # # ]: 0 : if (unlikely(count != i))
3176 : 0 : rte_pktmbuf_free_bulk(&pkts[i], count - i);
3177 : :
3178 [ # # # # ]: 0 : if (likely(vq->shadow_used_idx)) {
3179 [ # # # # ]: 0 : vq->last_avail_idx += vq->shadow_used_idx;
3180 : : vhost_virtqueue_reconnect_log_split(vq);
3181 : 0 : do_data_copy_dequeue(vq);
3182 : : flush_shadow_used_ring_split(dev, vq);
3183 : : vhost_vring_call_split(dev, vq);
3184 : : }
3185 : :
3186 : : return i;
3187 : : }
3188 : :
3189 : : __rte_noinline
3190 : : static uint16_t
3191 : 0 : virtio_dev_tx_split_legacy(struct virtio_net *dev,
3192 : : struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
3193 : : struct rte_mbuf **pkts, uint16_t count)
3194 : : __rte_shared_locks_required(&vq->access_lock)
3195 : : __rte_shared_locks_required(&vq->iotlb_lock)
3196 : : {
3197 [ # # ]: 0 : return virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count, true);
3198 : : }
3199 : :
3200 : : __rte_noinline
3201 : : static uint16_t
3202 : 0 : virtio_dev_tx_split_compliant(struct virtio_net *dev,
3203 : : struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
3204 : : struct rte_mbuf **pkts, uint16_t count)
3205 : : __rte_shared_locks_required(&vq->access_lock)
3206 : : __rte_shared_locks_required(&vq->iotlb_lock)
3207 : : {
3208 [ # # ]: 0 : return virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count, false);
3209 : : }
3210 : :
3211 : : static __rte_always_inline int
3212 : : vhost_reserve_avail_batch_packed(struct virtio_net *dev,
3213 : : struct vhost_virtqueue *vq,
3214 : : struct rte_mbuf **pkts,
3215 : : uint16_t avail_idx,
3216 : : uintptr_t *desc_addrs,
3217 : : uint16_t *ids)
3218 : : __rte_shared_locks_required(&vq->iotlb_lock)
3219 : : {
3220 : 0 : bool wrap = vq->avail_wrap_counter;
3221 : : struct vring_packed_desc *descs = vq->desc_packed;
3222 : : uint64_t lens[PACKED_BATCH_SIZE];
3223 : : uint64_t buf_lens[PACKED_BATCH_SIZE];
3224 : : uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
3225 : : uint16_t flags, i;
3226 : :
3227 : 0 : if (unlikely(avail_idx & PACKED_BATCH_MASK))
3228 : : return -1;
3229 [ # # # # ]: 0 : if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
3230 : : return -1;
3231 : :
3232 [ # # # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
3233 : 0 : flags = descs[avail_idx + i].flags;
3234 [ # # # # : 0 : if (unlikely((wrap != !!(flags & VRING_DESC_F_AVAIL)) ||
# # # # #
# # # ]
3235 : : (wrap == !!(flags & VRING_DESC_F_USED)) ||
3236 : : (flags & PACKED_DESC_SINGLE_DEQUEUE_FLAG)))
3237 : : return -1;
3238 : : }
3239 : :
3240 : : rte_atomic_thread_fence(rte_memory_order_acquire);
3241 : :
3242 [ # # # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
3243 : 0 : lens[i] = descs[avail_idx + i].len;
3244 : :
3245 [ # # # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
3246 : 0 : desc_addrs[i] = vhost_iova_to_vva(dev, vq,
3247 : 0 : descs[avail_idx + i].addr,
3248 [ # # # # ]: 0 : &lens[i], VHOST_ACCESS_RW);
3249 : : }
3250 : :
3251 [ # # # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
3252 [ # # # # ]: 0 : if (unlikely(!desc_addrs[i]))
3253 : : return -1;
3254 [ # # # # ]: 0 : if (unlikely((lens[i] != descs[avail_idx + i].len)))
3255 : : return -1;
3256 : : }
3257 : :
3258 [ # # # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
3259 [ # # # # ]: 0 : if (virtio_dev_pktmbuf_prep(dev, pkts[i], lens[i]))
3260 : 0 : goto err;
3261 : : }
3262 : :
3263 [ # # # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
3264 : 0 : buf_lens[i] = pkts[i]->buf_len - pkts[i]->data_off;
3265 : :
3266 [ # # # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
3267 [ # # # # ]: 0 : if (unlikely(buf_lens[i] < (lens[i] - buf_offset)))
3268 : 0 : goto err;
3269 : : }
3270 : :
3271 [ # # # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
3272 : 0 : pkts[i]->pkt_len = lens[i] - buf_offset;
3273 : 0 : pkts[i]->data_len = pkts[i]->pkt_len;
3274 : 0 : ids[i] = descs[avail_idx + i].id;
3275 : : }
3276 : :
3277 : : return 0;
3278 : :
3279 : : err:
3280 : : return -1;
3281 : : }
3282 : :
3283 : : static __rte_always_inline int
3284 : : vhost_async_tx_batch_packed_check(struct virtio_net *dev,
3285 : : struct vhost_virtqueue *vq,
3286 : : struct rte_mbuf **pkts,
3287 : : uint16_t avail_idx,
3288 : : uintptr_t *desc_addrs,
3289 : : uint64_t *lens,
3290 : : uint16_t *ids,
3291 : : int16_t dma_id,
3292 : : uint16_t vchan_id)
3293 : : {
3294 : 0 : bool wrap = vq->avail_wrap_counter;
3295 : : struct vring_packed_desc *descs = vq->desc_packed;
3296 : : uint64_t buf_lens[PACKED_BATCH_SIZE];
3297 : : uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
3298 : : uint16_t flags, i;
3299 : :
3300 : 0 : if (unlikely(avail_idx & PACKED_BATCH_MASK))
3301 : : return -1;
3302 [ # # # # ]: 0 : if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
3303 : : return -1;
3304 : :
3305 [ # # # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
3306 : 0 : flags = descs[avail_idx + i].flags;
3307 [ # # # # : 0 : if (unlikely((wrap != !!(flags & VRING_DESC_F_AVAIL)) ||
# # # # #
# # # ]
3308 : : (wrap == !!(flags & VRING_DESC_F_USED)) ||
3309 : : (flags & PACKED_DESC_SINGLE_DEQUEUE_FLAG)))
3310 : : return -1;
3311 : : }
3312 : :
3313 : : rte_atomic_thread_fence(rte_memory_order_acquire);
3314 : :
3315 [ # # # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
3316 : 0 : lens[i] = descs[avail_idx + i].len;
3317 : :
3318 [ # # # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
3319 : 0 : desc_addrs[i] = descs[avail_idx + i].addr;
3320 : : }
3321 : :
3322 [ # # # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
3323 [ # # # # ]: 0 : if (unlikely(!desc_addrs[i]))
3324 : : return -1;
3325 [ # # # # ]: 0 : if (unlikely((lens[i] != descs[avail_idx + i].len)))
3326 : : return -1;
3327 : : }
3328 : :
3329 [ # # # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
3330 [ # # # # ]: 0 : if (virtio_dev_pktmbuf_prep(dev, pkts[i], lens[i]))
3331 : 0 : goto err;
3332 : : }
3333 : :
3334 [ # # # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
3335 : 0 : buf_lens[i] = pkts[i]->buf_len - pkts[i]->data_off;
3336 : :
3337 [ # # # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
3338 [ # # # # ]: 0 : if (unlikely(buf_lens[i] < (lens[i] - buf_offset)))
3339 : 0 : goto err;
3340 : : }
3341 : :
3342 [ # # # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
3343 : 0 : pkts[i]->pkt_len = lens[i] - buf_offset;
3344 : 0 : pkts[i]->data_len = pkts[i]->pkt_len;
3345 : 0 : ids[i] = descs[avail_idx + i].id;
3346 : : }
3347 : :
3348 [ # # # # ]: 0 : if (rte_dma_burst_capacity(dma_id, vchan_id) < PACKED_BATCH_SIZE)
3349 : : return -1;
3350 : :
3351 : : return 0;
3352 : :
3353 : : err:
3354 : : return -1;
3355 : : }
3356 : :
3357 : : static __rte_always_inline int
3358 : : virtio_dev_tx_batch_packed(struct virtio_net *dev,
3359 : : struct vhost_virtqueue *vq,
3360 : : struct rte_mbuf **pkts,
3361 : : bool legacy_ol_flags)
3362 : : __rte_shared_locks_required(&vq->iotlb_lock)
3363 : : {
3364 : : uint16_t avail_idx = vq->last_avail_idx;
3365 : : uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
3366 : : struct virtio_net_hdr *hdr;
3367 : : uintptr_t desc_addrs[PACKED_BATCH_SIZE];
3368 : : uint16_t ids[PACKED_BATCH_SIZE];
3369 : : uint16_t i;
3370 : :
3371 [ # # # # ]: 0 : if (vhost_reserve_avail_batch_packed(dev, vq, pkts, avail_idx,
3372 : : desc_addrs, ids))
3373 : : return -1;
3374 : :
3375 [ # # # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
3376 : 0 : rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
3377 : :
3378 [ # # # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
3379 : 0 : rte_memcpy(rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
3380 : 0 : (void *)(uintptr_t)(desc_addrs[i] + buf_offset),
3381 [ # # # # ]: 0 : pkts[i]->pkt_len);
3382 : :
3383 : : if (virtio_net_with_host_offload(dev)) {
3384 [ # # # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
3385 : 0 : hdr = (struct virtio_net_hdr *)(desc_addrs[i]);
3386 [ # # # # ]: 0 : vhost_dequeue_offload(dev, hdr, pkts[i], legacy_ol_flags);
3387 : : }
3388 : : }
3389 : :
3390 [ # # # # ]: 0 : if (virtio_net_is_inorder(dev))
3391 : : vhost_shadow_dequeue_batch_packed_inorder(vq,
3392 [ # # # # ]: 0 : ids[PACKED_BATCH_SIZE - 1]);
3393 : : else
3394 : : vhost_shadow_dequeue_batch_packed(dev, vq, ids);
3395 : :
3396 : : vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
3397 : :
3398 : : return 0;
3399 : : }
3400 : :
3401 : : static __rte_always_inline int
3402 : : vhost_dequeue_single_packed(struct virtio_net *dev,
3403 : : struct vhost_virtqueue *vq,
3404 : : struct rte_mempool *mbuf_pool,
3405 : : struct rte_mbuf *pkts,
3406 : : uint16_t *buf_id,
3407 : : uint16_t *desc_count,
3408 : : bool legacy_ol_flags)
3409 : : __rte_shared_locks_required(&vq->access_lock)
3410 : : __rte_shared_locks_required(&vq->iotlb_lock)
3411 : : {
3412 : : struct buf_vector buf_vec[BUF_VECTOR_MAX];
3413 : : uint32_t buf_len;
3414 : : uint16_t nr_vec = 0;
3415 : : int err;
3416 : : static bool allocerr_warned;
3417 : :
3418 [ # # # # ]: 0 : if (unlikely(fill_vec_buf_packed(dev, vq,
3419 : : vq->last_avail_idx, desc_count,
3420 : : buf_vec, &nr_vec,
3421 : : buf_id, &buf_len,
3422 : : VHOST_ACCESS_RO) < 0))
3423 : : return -1;
3424 : :
3425 [ # # # # ]: 0 : if (unlikely(buf_len <= dev->vhost_hlen))
3426 : : return -1;
3427 : :
3428 [ # # # # ]: 0 : buf_len -= dev->vhost_hlen;
3429 : :
3430 [ # # # # ]: 0 : if (unlikely(virtio_dev_pktmbuf_prep(dev, pkts, buf_len))) {
3431 [ # # # # ]: 0 : if (!allocerr_warned) {
3432 : 0 : VHOST_DATA_LOG(dev->ifname, ERR,
3433 : : "failed mbuf alloc of size %d from %s.",
3434 : : buf_len, mbuf_pool->name);
3435 : 0 : allocerr_warned = true;
3436 : : }
3437 : : return -1;
3438 : : }
3439 : :
3440 : : err = desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts,
3441 : : mbuf_pool, legacy_ol_flags, 0, false);
3442 [ # # # # ]: 0 : if (unlikely(err)) {
3443 [ # # # # ]: 0 : if (!allocerr_warned) {
3444 : 0 : VHOST_DATA_LOG(dev->ifname, ERR, "failed to copy desc to mbuf.");
3445 : 0 : allocerr_warned = true;
3446 : : }
3447 : : return -1;
3448 : : }
3449 : :
3450 : : return 0;
3451 : : }
3452 : :
3453 : : static __rte_always_inline int
3454 : : virtio_dev_tx_single_packed(struct virtio_net *dev,
3455 : : struct vhost_virtqueue *vq,
3456 : : struct rte_mempool *mbuf_pool,
3457 : : struct rte_mbuf *pkts,
3458 : : bool legacy_ol_flags)
3459 : : __rte_shared_locks_required(&vq->access_lock)
3460 : : __rte_shared_locks_required(&vq->iotlb_lock)
3461 : : {
3462 : :
3463 : : uint16_t buf_id, desc_count = 0;
3464 : : int ret;
3465 : :
3466 : : ret = vhost_dequeue_single_packed(dev, vq, mbuf_pool, pkts, &buf_id,
3467 : : &desc_count, legacy_ol_flags);
3468 : :
3469 [ # # # # ]: 0 : if (likely(desc_count > 0)) {
3470 [ # # # # ]: 0 : if (virtio_net_is_inorder(dev))
3471 : : vhost_shadow_dequeue_single_packed_inorder(vq, buf_id,
3472 : : desc_count);
3473 : : else
3474 : : vhost_shadow_dequeue_single_packed(vq, buf_id,
3475 : : desc_count);
3476 : :
3477 : : vq_inc_last_avail_packed(vq, desc_count);
3478 : : }
3479 : :
3480 : : return ret;
3481 : : }
3482 : :
3483 : : static __rte_always_inline uint16_t
3484 : : get_nb_avail_entries_packed(const struct vhost_virtqueue *__rte_restrict vq,
3485 : : uint16_t max_nb_avail_entries)
3486 : : {
3487 : 0 : const struct vring_packed_desc *descs = vq->desc_packed;
3488 : 0 : bool avail_wrap = vq->avail_wrap_counter;
3489 : 0 : uint16_t avail_idx = vq->last_avail_idx;
3490 : : uint16_t nb_avail_entries = 0;
3491 : : uint16_t flags;
3492 : :
3493 [ # # # # ]: 0 : while (nb_avail_entries < max_nb_avail_entries) {
3494 : 0 : flags = descs[avail_idx].flags;
3495 : :
3496 [ # # # # ]: 0 : if ((avail_wrap != !!(flags & VRING_DESC_F_AVAIL)) ||
3497 [ # # # # ]: 0 : (avail_wrap == !!(flags & VRING_DESC_F_USED)))
3498 : : return nb_avail_entries;
3499 : :
3500 [ # # # # ]: 0 : if (!(flags & VRING_DESC_F_NEXT))
3501 : 0 : ++nb_avail_entries;
3502 : :
3503 [ # # # # ]: 0 : if (unlikely(++avail_idx >= vq->size)) {
3504 : 0 : avail_idx -= vq->size;
3505 : 0 : avail_wrap = !avail_wrap;
3506 : : }
3507 : : }
3508 : :
3509 : : return nb_avail_entries;
3510 : : }
3511 : :
3512 : : __rte_always_inline
3513 : : static uint16_t
3514 : : virtio_dev_tx_packed(struct virtio_net *dev,
3515 : : struct vhost_virtqueue *__rte_restrict vq,
3516 : : struct rte_mempool *mbuf_pool,
3517 : : struct rte_mbuf **__rte_restrict pkts,
3518 : : uint32_t count,
3519 : : bool legacy_ol_flags)
3520 : : __rte_shared_locks_required(&vq->access_lock)
3521 : : __rte_shared_locks_required(&vq->iotlb_lock)
3522 : : {
3523 : : uint32_t pkt_idx = 0;
3524 : :
3525 : 0 : count = get_nb_avail_entries_packed(vq, count);
3526 [ # # # # ]: 0 : if (count == 0)
3527 : : return 0;
3528 : :
3529 [ # # # # ]: 0 : if (rte_pktmbuf_alloc_bulk(mbuf_pool, pkts, count)) {
3530 : 0 : vq->stats.mbuf_alloc_failed += count;
3531 : 0 : return 0;
3532 : : }
3533 : :
3534 : : do {
3535 : 0 : rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
3536 : :
3537 [ # # # # ]: 0 : if (count - pkt_idx >= PACKED_BATCH_SIZE) {
3538 : 0 : if (!virtio_dev_tx_batch_packed(dev, vq,
3539 [ # # # # ]: 0 : &pkts[pkt_idx],
3540 : : legacy_ol_flags)) {
3541 : 0 : pkt_idx += PACKED_BATCH_SIZE;
3542 : 0 : continue;
3543 : : }
3544 : : }
3545 : :
3546 [ # # # # ]: 0 : if (virtio_dev_tx_single_packed(dev, vq, mbuf_pool,
3547 : 0 : pkts[pkt_idx],
3548 : : legacy_ol_flags))
3549 : : break;
3550 : 0 : pkt_idx++;
3551 [ # # # # ]: 0 : } while (pkt_idx < count);
3552 : :
3553 [ # # # # ]: 0 : if (pkt_idx != count)
3554 : 0 : rte_pktmbuf_free_bulk(&pkts[pkt_idx], count - pkt_idx);
3555 : :
3556 [ # # # # ]: 0 : if (vq->shadow_used_idx) {
3557 : 0 : do_data_copy_dequeue(vq);
3558 : :
3559 : : vhost_flush_dequeue_shadow_packed(dev, vq);
3560 : : vhost_vring_call_packed(dev, vq);
3561 : : }
3562 : :
3563 : 0 : return pkt_idx;
3564 : : }
3565 : :
3566 : : __rte_noinline
3567 : : static uint16_t
3568 : 0 : virtio_dev_tx_packed_legacy(struct virtio_net *dev,
3569 : : struct vhost_virtqueue *__rte_restrict vq, struct rte_mempool *mbuf_pool,
3570 : : struct rte_mbuf **__rte_restrict pkts, uint32_t count)
3571 : : __rte_shared_locks_required(&vq->access_lock)
3572 : : __rte_shared_locks_required(&vq->iotlb_lock)
3573 : : {
3574 : 0 : return virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count, true);
3575 : : }
3576 : :
3577 : : __rte_noinline
3578 : : static uint16_t
3579 : 0 : virtio_dev_tx_packed_compliant(struct virtio_net *dev,
3580 : : struct vhost_virtqueue *__rte_restrict vq, struct rte_mempool *mbuf_pool,
3581 : : struct rte_mbuf **__rte_restrict pkts, uint32_t count)
3582 : : __rte_shared_locks_required(&vq->access_lock)
3583 : : __rte_shared_locks_required(&vq->iotlb_lock)
3584 : : {
3585 : 0 : return virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count, false);
3586 : : }
3587 : :
3588 : : uint16_t
3589 [ # # ]: 0 : rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
3590 : : struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
3591 : : {
3592 : : struct virtio_net *dev;
3593 : : struct rte_mbuf *rarp_mbuf = NULL;
3594 : : struct vhost_virtqueue *vq;
3595 : : int16_t success = 1;
3596 : :
3597 : : dev = get_device(vid);
3598 [ # # ]: 0 : if (!dev)
3599 : : return 0;
3600 : :
3601 [ # # ]: 0 : if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
3602 : 0 : VHOST_DATA_LOG(dev->ifname, ERR,
3603 : : "%s: built-in vhost net backend is disabled.",
3604 : : __func__);
3605 : 0 : return 0;
3606 : : }
3607 : :
3608 [ # # # # ]: 0 : if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
3609 : 0 : VHOST_DATA_LOG(dev->ifname, ERR,
3610 : : "%s: invalid virtqueue idx %d.",
3611 : : __func__, queue_id);
3612 : 0 : return 0;
3613 : : }
3614 : :
3615 : 0 : vq = dev->virtqueue[queue_id];
3616 : :
3617 [ # # ]: 0 : if (unlikely(rte_rwlock_read_trylock(&vq->access_lock) != 0))
3618 : : return 0;
3619 : :
3620 [ # # ]: 0 : if (unlikely(!vq->enabled)) {
3621 : : count = 0;
3622 : 0 : goto out_access_unlock;
3623 : : }
3624 : :
3625 : : vhost_user_iotlb_rd_lock(vq);
3626 : :
3627 [ # # ]: 0 : if (unlikely(!vq->access_ok)) {
3628 : : vhost_user_iotlb_rd_unlock(vq);
3629 : : rte_rwlock_read_unlock(&vq->access_lock);
3630 : :
3631 : 0 : virtio_dev_vring_translate(dev, vq);
3632 : 0 : goto out_no_unlock;
3633 : : }
3634 : :
3635 : : /*
3636 : : * Construct a RARP broadcast packet, and inject it to the "pkts"
3637 : : * array, to looks like that guest actually send such packet.
3638 : : *
3639 : : * Check user_send_rarp() for more information.
3640 : : *
3641 : : * broadcast_rarp shares a cacheline in the virtio_net structure
3642 : : * with some fields that are accessed during enqueue and
3643 : : * rte_atomic_compare_exchange_strong_explicit causes a write if performed compare
3644 : : * and exchange. This could result in false sharing between enqueue
3645 : : * and dequeue.
3646 : : *
3647 : : * Prevent unnecessary false sharing by reading broadcast_rarp first
3648 : : * and only performing compare and exchange if the read indicates it
3649 : : * is likely to be set.
3650 : : */
3651 [ # # # # ]: 0 : if (unlikely(rte_atomic_load_explicit(&dev->broadcast_rarp, rte_memory_order_acquire) &&
3652 : : rte_atomic_compare_exchange_strong_explicit(&dev->broadcast_rarp,
3653 : : &success, 0, rte_memory_order_release, rte_memory_order_relaxed))) {
3654 : :
3655 : 0 : rarp_mbuf = rte_net_make_rarp_packet(mbuf_pool, &dev->mac);
3656 [ # # ]: 0 : if (rarp_mbuf == NULL) {
3657 : 0 : VHOST_DATA_LOG(dev->ifname, ERR, "failed to make RARP packet.");
3658 : : count = 0;
3659 : 0 : goto out;
3660 : : }
3661 : : /*
3662 : : * Inject it to the head of "pkts" array, so that switch's mac
3663 : : * learning table will get updated first.
3664 : : */
3665 : 0 : pkts[0] = rarp_mbuf;
3666 : 0 : vhost_queue_stats_update(dev, vq, pkts, 1);
3667 : 0 : pkts++;
3668 : 0 : count -= 1;
3669 : : }
3670 : :
3671 [ # # ]: 0 : if (vq_is_packed(dev)) {
3672 [ # # ]: 0 : if (dev->flags & VIRTIO_DEV_LEGACY_OL_FLAGS)
3673 : 0 : count = virtio_dev_tx_packed_legacy(dev, vq, mbuf_pool, pkts, count);
3674 : : else
3675 : 0 : count = virtio_dev_tx_packed_compliant(dev, vq, mbuf_pool, pkts, count);
3676 : : } else {
3677 [ # # ]: 0 : if (dev->flags & VIRTIO_DEV_LEGACY_OL_FLAGS)
3678 : 0 : count = virtio_dev_tx_split_legacy(dev, vq, mbuf_pool, pkts, count);
3679 : : else
3680 : 0 : count = virtio_dev_tx_split_compliant(dev, vq, mbuf_pool, pkts, count);
3681 : : }
3682 : :
3683 : 0 : vhost_queue_stats_update(dev, vq, pkts, count);
3684 : :
3685 : 0 : out:
3686 : : vhost_user_iotlb_rd_unlock(vq);
3687 : :
3688 [ # # ]: 0 : out_access_unlock:
3689 : : rte_rwlock_read_unlock(&vq->access_lock);
3690 : :
3691 [ # # ]: 0 : if (unlikely(rarp_mbuf != NULL))
3692 : 0 : count += 1;
3693 : :
3694 : 0 : out_no_unlock:
3695 : : return count;
3696 : : }
3697 : :
3698 : : static __rte_always_inline uint16_t
3699 : : async_poll_dequeue_completed(struct virtio_net *dev, struct vhost_virtqueue *vq,
3700 : : struct rte_mbuf **pkts, uint16_t count, int16_t dma_id,
3701 : : uint16_t vchan_id, bool legacy_ol_flags)
3702 : : __rte_shared_locks_required(&vq->access_lock)
3703 : : {
3704 : : uint16_t start_idx, from, i;
3705 : : uint16_t nr_cpl_pkts = 0;
3706 : 0 : struct async_inflight_info *pkts_info = vq->async->pkts_info;
3707 : :
3708 : : vhost_async_dma_check_completed(dev, dma_id, vchan_id, VHOST_DMA_MAX_COPY_COMPLETE);
3709 : :
3710 : : start_idx = async_get_first_inflight_pkt_idx(vq);
3711 : :
3712 : : from = start_idx;
3713 [ # # # # : 0 : while (vq->async->pkts_cmpl_flag[from] && count--) {
# # # # #
# # # # #
# # # # #
# # # #
# ]
3714 : 0 : vq->async->pkts_cmpl_flag[from] = false;
3715 : 0 : from = (from + 1) % vq->size;
3716 : 0 : nr_cpl_pkts++;
3717 : : }
3718 : :
3719 [ # # # # : 0 : if (nr_cpl_pkts == 0)
# # # # #
# # # ]
3720 : : return 0;
3721 : :
3722 [ # # # # : 0 : for (i = 0; i < nr_cpl_pkts; i++) {
# # # # #
# # # ]
3723 : 0 : from = (start_idx + i) % vq->size;
3724 [ # # # # : 0 : pkts[i] = pkts_info[from].mbuf;
# # # # #
# # # ]
3725 : :
3726 : : if (virtio_net_with_host_offload(dev))
3727 : : vhost_dequeue_offload(dev, &pkts_info[from].nethdr, pkts[i],
3728 : : legacy_ol_flags);
3729 : : }
3730 : :
3731 : : /* write back completed descs to used ring and update used idx */
3732 [ # # # # : 0 : if (vq_is_packed(dev)) {
# # # # #
# # # ]
3733 : : write_back_completed_descs_packed(vq, nr_cpl_pkts);
3734 : : vhost_vring_call_packed(dev, vq);
3735 : : } else {
3736 : 0 : write_back_completed_descs_split(vq, nr_cpl_pkts);
3737 : 0 : rte_atomic_fetch_add_explicit((unsigned short __rte_atomic *)&vq->used->idx,
3738 : : nr_cpl_pkts, rte_memory_order_release);
3739 : : vhost_vring_call_split(dev, vq);
3740 : : }
3741 : 0 : vq->async->pkts_inflight_n -= nr_cpl_pkts;
3742 : :
3743 : 0 : return nr_cpl_pkts;
3744 : : }
3745 : :
3746 : : static __rte_always_inline uint16_t
3747 : : virtio_dev_tx_async_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
3748 : : struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count,
3749 : : int16_t dma_id, uint16_t vchan_id, bool legacy_ol_flags)
3750 : : __rte_shared_locks_required(&vq->access_lock)
3751 : : __rte_shared_locks_required(&vq->iotlb_lock)
3752 : : {
3753 : : static bool allocerr_warned;
3754 : : bool dropped = false;
3755 : : uint16_t avail_entries;
3756 : : uint16_t pkt_idx, slot_idx = 0;
3757 : : uint16_t nr_done_pkts = 0;
3758 : : uint16_t pkt_err = 0;
3759 : : uint16_t n_xfer;
3760 : 0 : struct vhost_async *async = vq->async;
3761 : 0 : struct async_inflight_info *pkts_info = async->pkts_info;
3762 : : struct rte_mbuf *pkts_prealloc[MAX_PKT_BURST];
3763 : : uint16_t pkts_size = count;
3764 : :
3765 : : /**
3766 : : * The ordering between avail index and
3767 : : * desc reads needs to be enforced.
3768 : : */
3769 : 0 : avail_entries = rte_atomic_load_explicit((unsigned short __rte_atomic *)&vq->avail->idx,
3770 : 0 : rte_memory_order_acquire) - vq->last_avail_idx;
3771 : 0 : if (avail_entries == 0)
3772 : 0 : goto out;
3773 : :
3774 : 0 : rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
3775 : :
3776 : : async_iter_reset(async);
3777 : :
3778 : 0 : count = RTE_MIN(count, MAX_PKT_BURST);
3779 : 0 : count = RTE_MIN(count, avail_entries);
3780 : : VHOST_DATA_LOG(dev->ifname, DEBUG, "about to dequeue %u buffers", count);
3781 : :
3782 [ # # # # ]: 0 : if (rte_pktmbuf_alloc_bulk(mbuf_pool, pkts_prealloc, count)) {
3783 : 0 : vq->stats.mbuf_alloc_failed += count;
3784 : 0 : goto out;
3785 : : }
3786 : :
3787 [ # # # # ]: 0 : for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
3788 : : uint16_t head_idx = 0;
3789 : : uint16_t nr_vec = 0;
3790 : : uint16_t to;
3791 : : uint32_t buf_len;
3792 : : int err;
3793 : : struct buf_vector buf_vec[BUF_VECTOR_MAX];
3794 : 0 : struct rte_mbuf *pkt = pkts_prealloc[pkt_idx];
3795 : :
3796 [ # # # # : 0 : if (unlikely(fill_vec_buf_split(dev, vq, vq->last_avail_idx,
# # # # ]
3797 : : &nr_vec, buf_vec,
3798 : : &head_idx, &buf_len,
3799 : : VHOST_ACCESS_RO) < 0)) {
3800 : : dropped = true;
3801 : 0 : break;
3802 : : }
3803 : :
3804 [ # # # # ]: 0 : if (unlikely(buf_len <= dev->vhost_hlen)) {
3805 : : dropped = true;
3806 : : break;
3807 : : }
3808 : :
3809 [ # # # # ]: 0 : buf_len -= dev->vhost_hlen;
3810 : :
3811 : : err = virtio_dev_pktmbuf_prep(dev, pkt, buf_len);
3812 [ # # # # ]: 0 : if (unlikely(err)) {
3813 : : /**
3814 : : * mbuf allocation fails for jumbo packets when external
3815 : : * buffer allocation is not allowed and linear buffer
3816 : : * is required. Drop this packet.
3817 : : */
3818 [ # # # # ]: 0 : if (!allocerr_warned) {
3819 : 0 : VHOST_DATA_LOG(dev->ifname, ERR,
3820 : : "%s: Failed mbuf alloc of size %d from %s",
3821 : : __func__, buf_len, mbuf_pool->name);
3822 : 0 : allocerr_warned = true;
3823 : : }
3824 : : dropped = true;
3825 : 0 : slot_idx--;
3826 : 0 : break;
3827 : : }
3828 : :
3829 [ # # # # ]: 0 : slot_idx = (async->pkts_idx + pkt_idx) & (vq->size - 1);
3830 : : err = desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkt, mbuf_pool,
3831 : : legacy_ol_flags, slot_idx, true);
3832 [ # # # # ]: 0 : if (unlikely(err)) {
3833 [ # # # # ]: 0 : if (!allocerr_warned) {
3834 : 0 : VHOST_DATA_LOG(dev->ifname, ERR,
3835 : : "%s: Failed to offload copies to async channel.",
3836 : : __func__);
3837 : 0 : allocerr_warned = true;
3838 : : }
3839 : : dropped = true;
3840 : 0 : slot_idx--;
3841 : 0 : break;
3842 : : }
3843 : :
3844 : 0 : pkts_info[slot_idx].mbuf = pkt;
3845 : :
3846 : : /* store used descs */
3847 : 0 : to = async->desc_idx_split & (vq->size - 1);
3848 : 0 : async->descs_split[to].id = head_idx;
3849 : 0 : async->descs_split[to].len = 0;
3850 : 0 : async->desc_idx_split++;
3851 : :
3852 [ # # # # ]: 0 : vq->last_avail_idx++;
3853 : : vhost_virtqueue_reconnect_log_split(vq);
3854 : : }
3855 : :
3856 [ # # # # ]: 0 : if (unlikely(dropped))
3857 : 0 : rte_pktmbuf_free_bulk(&pkts_prealloc[pkt_idx], count - pkt_idx);
3858 : :
3859 : 0 : n_xfer = vhost_async_dma_transfer(dev, vq, dma_id, vchan_id, async->pkts_idx,
3860 : 0 : async->iov_iter, pkt_idx);
3861 : :
3862 : 0 : async->pkts_inflight_n += n_xfer;
3863 : :
3864 : 0 : pkt_err = pkt_idx - n_xfer;
3865 [ # # # # ]: 0 : if (unlikely(pkt_err)) {
3866 : : VHOST_DATA_LOG(dev->ifname, DEBUG, "%s: failed to transfer data.",
3867 : : __func__);
3868 : :
3869 : : pkt_idx = n_xfer;
3870 : : /* recover available ring */
3871 [ # # # # ]: 0 : vq->last_avail_idx -= pkt_err;
3872 : : vhost_virtqueue_reconnect_log_split(vq);
3873 : :
3874 : : /**
3875 : : * recover async channel copy related structures and free pktmbufs
3876 : : * for error pkts.
3877 : : */
3878 : 0 : async->desc_idx_split -= pkt_err;
3879 [ # # # # ]: 0 : while (pkt_err-- > 0) {
3880 : 0 : rte_pktmbuf_free(pkts_info[slot_idx & (vq->size - 1)].mbuf);
3881 : 0 : slot_idx--;
3882 : : }
3883 : : }
3884 : :
3885 : 0 : async->pkts_idx += pkt_idx;
3886 [ # # # # ]: 0 : if (async->pkts_idx >= vq->size)
3887 : 0 : async->pkts_idx -= vq->size;
3888 : :
3889 : 0 : out:
3890 : : /* DMA device may serve other queues, unconditionally check completed. */
3891 : : nr_done_pkts = async_poll_dequeue_completed(dev, vq, pkts, pkts_size,
3892 : : dma_id, vchan_id, legacy_ol_flags);
3893 : :
3894 : : return nr_done_pkts;
3895 : : }
3896 : :
3897 : : __rte_noinline
3898 : : static uint16_t
3899 : 0 : virtio_dev_tx_async_split_legacy(struct virtio_net *dev,
3900 : : struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
3901 : : struct rte_mbuf **pkts, uint16_t count,
3902 : : int16_t dma_id, uint16_t vchan_id)
3903 : : __rte_shared_locks_required(&vq->access_lock)
3904 : : __rte_shared_locks_required(&vq->iotlb_lock)
3905 : : {
3906 [ # # ]: 0 : return virtio_dev_tx_async_split(dev, vq, mbuf_pool,
3907 : : pkts, count, dma_id, vchan_id, true);
3908 : : }
3909 : :
3910 : : __rte_noinline
3911 : : static uint16_t
3912 : 0 : virtio_dev_tx_async_split_compliant(struct virtio_net *dev,
3913 : : struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
3914 : : struct rte_mbuf **pkts, uint16_t count,
3915 : : int16_t dma_id, uint16_t vchan_id)
3916 : : __rte_shared_locks_required(&vq->access_lock)
3917 : : __rte_shared_locks_required(&vq->iotlb_lock)
3918 : : {
3919 [ # # ]: 0 : return virtio_dev_tx_async_split(dev, vq, mbuf_pool,
3920 : : pkts, count, dma_id, vchan_id, false);
3921 : : }
3922 : :
3923 : : static __rte_always_inline void
3924 : : vhost_async_shadow_dequeue_single_packed(struct vhost_virtqueue *vq,
3925 : : uint16_t buf_id, uint16_t count)
3926 : : __rte_shared_locks_required(&vq->access_lock)
3927 : : {
3928 : 0 : struct vhost_async *async = vq->async;
3929 : 0 : uint16_t idx = async->buffer_idx_packed;
3930 : :
3931 : 0 : async->buffers_packed[idx].id = buf_id;
3932 : 0 : async->buffers_packed[idx].len = 0;
3933 : 0 : async->buffers_packed[idx].count = count;
3934 : :
3935 : 0 : async->buffer_idx_packed++;
3936 : 0 : if (async->buffer_idx_packed >= vq->size)
3937 : 0 : async->buffer_idx_packed -= vq->size;
3938 : :
3939 : : }
3940 : :
3941 : : static __rte_always_inline int
3942 : : virtio_dev_tx_async_single_packed(struct virtio_net *dev,
3943 : : struct vhost_virtqueue *vq,
3944 : : struct rte_mempool *mbuf_pool,
3945 : : struct rte_mbuf *pkts,
3946 : : uint16_t slot_idx,
3947 : : bool legacy_ol_flags)
3948 : : __rte_shared_locks_required(&vq->access_lock)
3949 : : __rte_shared_locks_required(&vq->iotlb_lock)
3950 : : {
3951 : : int err;
3952 : : uint16_t buf_id, desc_count = 0;
3953 : : uint16_t nr_vec = 0;
3954 : : uint32_t buf_len;
3955 : : struct buf_vector buf_vec[BUF_VECTOR_MAX];
3956 : 0 : struct vhost_async *async = vq->async;
3957 : 0 : struct async_inflight_info *pkts_info = async->pkts_info;
3958 : : static bool allocerr_warned;
3959 : :
3960 [ # # # # ]: 0 : if (unlikely(fill_vec_buf_packed(dev, vq, vq->last_avail_idx, &desc_count,
3961 : : buf_vec, &nr_vec, &buf_id, &buf_len,
3962 : : VHOST_ACCESS_RO) < 0))
3963 : : return -1;
3964 : :
3965 [ # # # # ]: 0 : if (unlikely(virtio_dev_pktmbuf_prep(dev, pkts, buf_len))) {
3966 [ # # # # ]: 0 : if (!allocerr_warned) {
3967 : 0 : VHOST_DATA_LOG(dev->ifname, ERR, "Failed mbuf alloc of size %d from %s.",
3968 : : buf_len, mbuf_pool->name);
3969 : :
3970 : 0 : allocerr_warned = true;
3971 : : }
3972 : : return -1;
3973 : : }
3974 : :
3975 : : err = desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts, mbuf_pool,
3976 : : legacy_ol_flags, slot_idx, true);
3977 [ # # # # ]: 0 : if (unlikely(err)) {
3978 : 0 : rte_pktmbuf_free(pkts);
3979 [ # # # # ]: 0 : if (!allocerr_warned) {
3980 : 0 : VHOST_DATA_LOG(dev->ifname, ERR, "Failed to copy desc to mbuf on.");
3981 : 0 : allocerr_warned = true;
3982 : : }
3983 : : return -1;
3984 : : }
3985 : :
3986 [ # # # # ]: 0 : pkts_info[slot_idx].descs = desc_count;
3987 : :
3988 : : /* update async shadow packed ring */
3989 : : vhost_async_shadow_dequeue_single_packed(vq, buf_id, desc_count);
3990 : :
3991 : : vq_inc_last_avail_packed(vq, desc_count);
3992 : :
3993 : : return err;
3994 : : }
3995 : :
3996 : : static __rte_always_inline int
3997 : : virtio_dev_tx_async_packed_batch(struct virtio_net *dev,
3998 : : struct vhost_virtqueue *vq,
3999 : : struct rte_mbuf **pkts, uint16_t slot_idx,
4000 : : uint16_t dma_id, uint16_t vchan_id)
4001 : : __rte_shared_locks_required(&vq->access_lock)
4002 : : __rte_shared_locks_required(&vq->iotlb_lock)
4003 : : {
4004 : : uint16_t avail_idx = vq->last_avail_idx;
4005 : : uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
4006 : 0 : struct vhost_async *async = vq->async;
4007 : 0 : struct async_inflight_info *pkts_info = async->pkts_info;
4008 : : struct virtio_net_hdr *hdr;
4009 : : uint32_t mbuf_offset = 0;
4010 : : uintptr_t desc_addrs[PACKED_BATCH_SIZE];
4011 : : uint64_t desc_vva;
4012 : : uint64_t lens[PACKED_BATCH_SIZE];
4013 : : void *host_iova[PACKED_BATCH_SIZE];
4014 : : uint64_t mapped_len[PACKED_BATCH_SIZE];
4015 : : uint16_t ids[PACKED_BATCH_SIZE];
4016 : : uint16_t i;
4017 : :
4018 : 0 : if (vhost_async_tx_batch_packed_check(dev, vq, pkts, avail_idx,
4019 : : desc_addrs, lens, ids, dma_id, vchan_id))
4020 : : return -1;
4021 : :
4022 [ # # # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
4023 : 0 : rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
4024 : :
4025 [ # # # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
4026 : 0 : host_iova[i] = (void *)(uintptr_t)gpa_to_first_hpa(dev,
4027 [ # # # # ]: 0 : desc_addrs[i] + buf_offset, pkts[i]->pkt_len, &mapped_len[i]);
4028 : : }
4029 : :
4030 [ # # # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
4031 : : async_iter_initialize(dev, async);
4032 : 0 : async_iter_add_iovec(dev, async,
4033 : : host_iova[i],
4034 [ # # # # ]: 0 : (void *)(uintptr_t)rte_pktmbuf_iova_offset(pkts[i], mbuf_offset),
4035 : : mapped_len[i]);
4036 : 0 : async->iter_idx++;
4037 : : }
4038 : :
4039 : : if (virtio_net_with_host_offload(dev)) {
4040 [ # # # # ]: 0 : vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
4041 : 0 : desc_vva = vhost_iova_to_vva(dev, vq, desc_addrs[i],
4042 [ # # # # ]: 0 : &lens[i], VHOST_ACCESS_RO);
4043 : 0 : hdr = (struct virtio_net_hdr *)(uintptr_t)desc_vva;
4044 : 0 : pkts_info[slot_idx + i].nethdr = *hdr;
4045 : : }
4046 : : }
4047 : :
4048 : : vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
4049 : :
4050 : : vhost_async_shadow_dequeue_packed_batch(vq, ids);
4051 : :
4052 : : return 0;
4053 : : }
4054 : :
4055 : : static __rte_always_inline uint16_t
4056 : : virtio_dev_tx_async_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
4057 : : struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts,
4058 : : uint16_t count, uint16_t dma_id, uint16_t vchan_id, bool legacy_ol_flags)
4059 : : __rte_shared_locks_required(&vq->access_lock)
4060 : : __rte_shared_locks_required(&vq->iotlb_lock)
4061 : : {
4062 : : uint32_t pkt_idx = 0;
4063 : : uint16_t slot_idx = 0;
4064 : : uint16_t nr_done_pkts = 0;
4065 : : uint16_t pkt_err = 0;
4066 : : uint32_t n_xfer;
4067 : : uint16_t i;
4068 : 0 : struct vhost_async *async = vq->async;
4069 : 0 : struct async_inflight_info *pkts_info = async->pkts_info;
4070 : : struct rte_mbuf *pkts_prealloc[MAX_PKT_BURST];
4071 : :
4072 : : VHOST_DATA_LOG(dev->ifname, DEBUG, "(%d) about to dequeue %u buffers", dev->vid, count);
4073 : :
4074 : : async_iter_reset(async);
4075 : :
4076 [ # # # # ]: 0 : if (rte_pktmbuf_alloc_bulk(mbuf_pool, pkts_prealloc, count)) {
4077 : 0 : vq->stats.mbuf_alloc_failed += count;
4078 : 0 : goto out;
4079 : : }
4080 : :
4081 : : do {
4082 : 0 : struct rte_mbuf *pkt = pkts_prealloc[pkt_idx];
4083 : :
4084 : 0 : rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
4085 : :
4086 : 0 : slot_idx = (async->pkts_idx + pkt_idx) % vq->size;
4087 [ # # # # ]: 0 : if (count - pkt_idx >= PACKED_BATCH_SIZE) {
4088 [ # # # # ]: 0 : if (!virtio_dev_tx_async_packed_batch(dev, vq, &pkts_prealloc[pkt_idx],
4089 : : slot_idx, dma_id, vchan_id)) {
4090 [ # # # # ]: 0 : for (i = 0; i < PACKED_BATCH_SIZE; i++) {
4091 : 0 : slot_idx = (async->pkts_idx + pkt_idx) % vq->size;
4092 : 0 : pkts_info[slot_idx].descs = 1;
4093 : 0 : pkts_info[slot_idx].nr_buffers = 1;
4094 : 0 : pkts_info[slot_idx].mbuf = pkts_prealloc[pkt_idx];
4095 : 0 : pkt_idx++;
4096 : : }
4097 : 0 : continue;
4098 : : }
4099 : : }
4100 : :
4101 [ # # # # ]: 0 : if (unlikely(virtio_dev_tx_async_single_packed(dev, vq, mbuf_pool, pkt,
4102 : : slot_idx, legacy_ol_flags))) {
4103 : 0 : rte_pktmbuf_free_bulk(&pkts_prealloc[pkt_idx], count - pkt_idx);
4104 : :
4105 [ # # # # ]: 0 : if (slot_idx == 0)
4106 : 0 : slot_idx = vq->size - 1;
4107 : : else
4108 : 0 : slot_idx--;
4109 : :
4110 : : break;
4111 : : }
4112 : :
4113 : 0 : pkts_info[slot_idx].mbuf = pkt;
4114 : 0 : pkt_idx++;
4115 [ # # # # ]: 0 : } while (pkt_idx < count);
4116 : :
4117 : 0 : n_xfer = vhost_async_dma_transfer(dev, vq, dma_id, vchan_id, async->pkts_idx,
4118 : 0 : async->iov_iter, pkt_idx);
4119 : :
4120 : 0 : async->pkts_inflight_n += n_xfer;
4121 : :
4122 : 0 : pkt_err = pkt_idx - n_xfer;
4123 : :
4124 [ # # # # ]: 0 : if (unlikely(pkt_err)) {
4125 : : uint16_t descs_err = 0;
4126 : :
4127 : 0 : pkt_idx -= pkt_err;
4128 : :
4129 : : /**
4130 : : * recover DMA-copy related structures and free pktmbuf for DMA-error pkts.
4131 : : */
4132 [ # # # # ]: 0 : if (async->buffer_idx_packed >= pkt_err)
4133 : 0 : async->buffer_idx_packed -= pkt_err;
4134 : : else
4135 : 0 : async->buffer_idx_packed += vq->size - pkt_err;
4136 : :
4137 [ # # # # ]: 0 : while (pkt_err-- > 0) {
4138 : 0 : rte_pktmbuf_free(pkts_info[slot_idx].mbuf);
4139 : 0 : descs_err += pkts_info[slot_idx].descs;
4140 : :
4141 [ # # # # ]: 0 : if (slot_idx == 0)
4142 : 0 : slot_idx = vq->size - 1;
4143 : : else
4144 : 0 : slot_idx--;
4145 : : }
4146 : :
4147 : : /* recover available ring */
4148 [ # # # # ]: 0 : if (vq->last_avail_idx >= descs_err) {
4149 : 0 : vq->last_avail_idx -= descs_err;
4150 : : } else {
4151 : 0 : vq->last_avail_idx += vq->size - descs_err;
4152 : 0 : vq->avail_wrap_counter ^= 1;
4153 : : }
4154 : : vhost_virtqueue_reconnect_log_packed(vq);
4155 : : }
4156 : :
4157 : 0 : async->pkts_idx += pkt_idx;
4158 [ # # # # ]: 0 : if (async->pkts_idx >= vq->size)
4159 : 0 : async->pkts_idx -= vq->size;
4160 : :
4161 : 0 : out:
4162 : 0 : nr_done_pkts = async_poll_dequeue_completed(dev, vq, pkts, count,
4163 : : dma_id, vchan_id, legacy_ol_flags);
4164 : :
4165 : : return nr_done_pkts;
4166 : : }
4167 : :
4168 : : __rte_noinline
4169 : : static uint16_t
4170 : 0 : virtio_dev_tx_async_packed_legacy(struct virtio_net *dev, struct vhost_virtqueue *vq,
4171 : : struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts,
4172 : : uint16_t count, uint16_t dma_id, uint16_t vchan_id)
4173 : : __rte_shared_locks_required(&vq->access_lock)
4174 : : __rte_shared_locks_required(&vq->iotlb_lock)
4175 : : {
4176 : 0 : return virtio_dev_tx_async_packed(dev, vq, mbuf_pool,
4177 : : pkts, count, dma_id, vchan_id, true);
4178 : : }
4179 : :
4180 : : __rte_noinline
4181 : : static uint16_t
4182 : 0 : virtio_dev_tx_async_packed_compliant(struct virtio_net *dev, struct vhost_virtqueue *vq,
4183 : : struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts,
4184 : : uint16_t count, uint16_t dma_id, uint16_t vchan_id)
4185 : : __rte_shared_locks_required(&vq->access_lock)
4186 : : __rte_shared_locks_required(&vq->iotlb_lock)
4187 : : {
4188 : 0 : return virtio_dev_tx_async_packed(dev, vq, mbuf_pool,
4189 : : pkts, count, dma_id, vchan_id, false);
4190 : : }
4191 : :
4192 : : uint16_t
4193 [ # # ]: 0 : rte_vhost_async_try_dequeue_burst(int vid, uint16_t queue_id,
4194 : : struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count,
4195 : : int *nr_inflight, int16_t dma_id, uint16_t vchan_id)
4196 : : {
4197 : : struct virtio_net *dev;
4198 : : struct rte_mbuf *rarp_mbuf = NULL;
4199 : : struct vhost_virtqueue *vq;
4200 : : int16_t success = 1;
4201 : :
4202 : : dev = get_device(vid);
4203 [ # # ]: 0 : if (!dev || !nr_inflight)
4204 : : return 0;
4205 : :
4206 : 0 : *nr_inflight = -1;
4207 : :
4208 [ # # ]: 0 : if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
4209 : 0 : VHOST_DATA_LOG(dev->ifname, ERR, "%s: built-in vhost net backend is disabled.",
4210 : : __func__);
4211 : 0 : return 0;
4212 : : }
4213 : :
4214 [ # # # # ]: 0 : if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
4215 : 0 : VHOST_DATA_LOG(dev->ifname, ERR, "%s: invalid virtqueue idx %d.",
4216 : : __func__, queue_id);
4217 : 0 : return 0;
4218 : : }
4219 : :
4220 [ # # ]: 0 : if (unlikely(dma_id < 0 || dma_id >= RTE_DMADEV_DEFAULT_MAX)) {
4221 : 0 : VHOST_DATA_LOG(dev->ifname, ERR, "%s: invalid dma id %d.",
4222 : : __func__, dma_id);
4223 : 0 : return 0;
4224 : : }
4225 : :
4226 [ # # # # ]: 0 : if (unlikely(!dma_copy_track[dma_id].vchans ||
4227 : : !dma_copy_track[dma_id].vchans[vchan_id].pkts_cmpl_flag_addr)) {
4228 : 0 : VHOST_DATA_LOG(dev->ifname, ERR, "%s: invalid channel %d:%u.",
4229 : : __func__, dma_id, vchan_id);
4230 : 0 : return 0;
4231 : : }
4232 : :
4233 : 0 : vq = dev->virtqueue[queue_id];
4234 : :
4235 [ # # ]: 0 : if (unlikely(rte_rwlock_read_trylock(&vq->access_lock) != 0))
4236 : : return 0;
4237 : :
4238 [ # # ]: 0 : if (unlikely(vq->enabled == 0)) {
4239 : : count = 0;
4240 : 0 : goto out_access_unlock;
4241 : : }
4242 : :
4243 [ # # ]: 0 : if (unlikely(!vq->async)) {
4244 : 0 : VHOST_DATA_LOG(dev->ifname, ERR, "%s: async not registered for queue id %d.",
4245 : : __func__, queue_id);
4246 : : count = 0;
4247 : 0 : goto out_access_unlock;
4248 : : }
4249 : :
4250 : : vhost_user_iotlb_rd_lock(vq);
4251 : :
4252 [ # # ]: 0 : if (unlikely(vq->access_ok == 0)) {
4253 : : vhost_user_iotlb_rd_unlock(vq);
4254 : : rte_rwlock_read_unlock(&vq->access_lock);
4255 : :
4256 : 0 : virtio_dev_vring_translate(dev, vq);
4257 : : count = 0;
4258 : 0 : goto out_no_unlock;
4259 : : }
4260 : :
4261 : : /*
4262 : : * Construct a RARP broadcast packet, and inject it to the "pkts"
4263 : : * array, to looks like that guest actually send such packet.
4264 : : *
4265 : : * Check user_send_rarp() for more information.
4266 : : *
4267 : : * broadcast_rarp shares a cacheline in the virtio_net structure
4268 : : * with some fields that are accessed during enqueue and
4269 : : * rte_atomic_compare_exchange_strong_explicit causes a write if performed compare
4270 : : * and exchange. This could result in false sharing between enqueue
4271 : : * and dequeue.
4272 : : *
4273 : : * Prevent unnecessary false sharing by reading broadcast_rarp first
4274 : : * and only performing compare and exchange if the read indicates it
4275 : : * is likely to be set.
4276 : : */
4277 [ # # # # ]: 0 : if (unlikely(rte_atomic_load_explicit(&dev->broadcast_rarp, rte_memory_order_acquire) &&
4278 : : rte_atomic_compare_exchange_strong_explicit(&dev->broadcast_rarp,
4279 : : &success, 0, rte_memory_order_release, rte_memory_order_relaxed))) {
4280 : :
4281 : 0 : rarp_mbuf = rte_net_make_rarp_packet(mbuf_pool, &dev->mac);
4282 [ # # ]: 0 : if (rarp_mbuf == NULL) {
4283 : 0 : VHOST_DATA_LOG(dev->ifname, ERR, "failed to make RARP packet.");
4284 : : count = 0;
4285 : 0 : goto out;
4286 : : }
4287 : : /*
4288 : : * Inject it to the head of "pkts" array, so that switch's mac
4289 : : * learning table will get updated first.
4290 : : */
4291 : 0 : pkts[0] = rarp_mbuf;
4292 : 0 : vhost_queue_stats_update(dev, vq, pkts, 1);
4293 : 0 : pkts++;
4294 : 0 : count -= 1;
4295 : : }
4296 : :
4297 [ # # ]: 0 : if (vq_is_packed(dev)) {
4298 [ # # ]: 0 : if (dev->flags & VIRTIO_DEV_LEGACY_OL_FLAGS)
4299 : 0 : count = virtio_dev_tx_async_packed_legacy(dev, vq, mbuf_pool,
4300 : : pkts, count, dma_id, vchan_id);
4301 : : else
4302 : 0 : count = virtio_dev_tx_async_packed_compliant(dev, vq, mbuf_pool,
4303 : : pkts, count, dma_id, vchan_id);
4304 : : } else {
4305 [ # # ]: 0 : if (dev->flags & VIRTIO_DEV_LEGACY_OL_FLAGS)
4306 : 0 : count = virtio_dev_tx_async_split_legacy(dev, vq, mbuf_pool,
4307 : : pkts, count, dma_id, vchan_id);
4308 : : else
4309 : 0 : count = virtio_dev_tx_async_split_compliant(dev, vq, mbuf_pool,
4310 : : pkts, count, dma_id, vchan_id);
4311 : : }
4312 : :
4313 : 0 : *nr_inflight = vq->async->pkts_inflight_n;
4314 : 0 : vhost_queue_stats_update(dev, vq, pkts, count);
4315 : :
4316 : 0 : out:
4317 : : vhost_user_iotlb_rd_unlock(vq);
4318 : :
4319 [ # # ]: 0 : out_access_unlock:
4320 : : rte_rwlock_read_unlock(&vq->access_lock);
4321 : :
4322 [ # # ]: 0 : if (unlikely(rarp_mbuf != NULL))
4323 : 0 : count += 1;
4324 : :
4325 : 0 : out_no_unlock:
4326 : : return count;
4327 : : }
|