Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2010-2014 Intel Corporation
3 : : */
4 : :
5 : : #ifndef _VIRTQUEUE_H_
6 : : #define _VIRTQUEUE_H_
7 : :
8 : : #include <stdint.h>
9 : :
10 : : #include <rte_atomic.h>
11 : : #include <rte_memory.h>
12 : : #include <rte_mempool.h>
13 : : #include <rte_net.h>
14 : :
15 : : #include "virtio.h"
16 : : #include "virtio_ring.h"
17 : : #include "virtio_logs.h"
18 : : #include "virtio_rxtx.h"
19 : : #include "virtio_cvq.h"
20 : :
21 : : struct rte_mbuf;
22 : :
23 : : #define DEFAULT_TX_FREE_THRESH 32
24 : : #define DEFAULT_RX_FREE_THRESH 32
25 : :
26 : : #define VIRTIO_MBUF_BURST_SZ 64
27 : : /*
28 : : * Per virtio_ring.h in Linux.
29 : : * For virtio_pci on SMP, we don't need to order with respect to MMIO
30 : : * accesses through relaxed memory I/O windows, so thread_fence is
31 : : * sufficient.
32 : : *
33 : : * For using virtio to talk to real devices (eg. vDPA) we do need real
34 : : * barriers.
35 : : */
36 : : static inline void
37 : : virtio_mb(uint8_t weak_barriers)
38 : : {
39 [ # # ]: 0 : if (weak_barriers)
40 : : rte_atomic_thread_fence(rte_memory_order_seq_cst);
41 : : else
42 : : rte_mb();
43 : : }
44 : :
45 : : static inline void
46 : : virtio_rmb(uint8_t weak_barriers)
47 : : {
48 : : if (weak_barriers)
49 : : rte_atomic_thread_fence(rte_memory_order_acquire);
50 : : else
51 : : rte_io_rmb();
52 : : }
53 : :
54 : : static inline void
55 : : virtio_wmb(uint8_t weak_barriers)
56 : : {
57 [ # # ]: 0 : if (weak_barriers)
58 : : rte_atomic_thread_fence(rte_memory_order_release);
59 : : else
60 : 0 : rte_io_wmb();
61 : : }
62 : :
63 : : static inline uint16_t
64 : : virtqueue_fetch_flags_packed(struct vring_packed_desc *dp,
65 : : uint8_t weak_barriers)
66 : : {
67 : : uint16_t flags;
68 : :
69 : 0 : if (weak_barriers) {
70 : : /* x86 prefers to using rte_io_rmb over rte_atomic_load_explicit as it reports
71 : : * a better perf(~1.5%), which comes from the saved branch by the compiler.
72 : : * The if and else branch are identical on the platforms except Arm.
73 : : */
74 : : #ifdef RTE_ARCH_ARM
75 : : flags = rte_atomic_load_explicit(&dp->flags, rte_memory_order_acquire);
76 : : #else
77 : 0 : flags = dp->flags;
78 : 0 : rte_io_rmb();
79 : : #endif
80 : : } else {
81 : 0 : flags = dp->flags;
82 : 0 : rte_io_rmb();
83 : : }
84 : :
85 : : return flags;
86 : : }
87 : :
88 : : static inline void
89 : : virtqueue_store_flags_packed(struct vring_packed_desc *dp,
90 : : uint16_t flags, uint8_t weak_barriers)
91 : : {
92 [ # # # # ]: 0 : if (weak_barriers) {
93 : : /* x86 prefers to using rte_io_wmb over rte_atomic_store_explicit as it reports
94 : : * a better perf(~1.5%), which comes from the saved branch by the compiler.
95 : : * The if and else branch are identical on the platforms except Arm.
96 : : */
97 : : #ifdef RTE_ARCH_ARM
98 : : rte_atomic_store_explicit(&dp->flags, flags, rte_memory_order_release);
99 : : #else
100 : 0 : rte_io_wmb();
101 : 0 : dp->flags = flags;
102 : : #endif
103 : : } else {
104 : 0 : rte_io_wmb();
105 : 0 : dp->flags = flags;
106 : : }
107 : : }
108 : :
109 : : #ifdef RTE_PMD_PACKET_PREFETCH
110 : : #define rte_packet_prefetch(p) rte_prefetch1(p)
111 : : #else
112 : : #define rte_packet_prefetch(p) do {} while(0)
113 : : #endif
114 : :
115 : : #define VIRTQUEUE_MAX_NAME_SZ 32
116 : :
117 : : #ifdef RTE_ARCH_32
118 : : #define VIRTIO_MBUF_ADDR_MASK(vq) ((vq)->mbuf_addr_mask)
119 : : #else
120 : : #define VIRTIO_MBUF_ADDR_MASK(vq) UINT64_MAX
121 : : #endif
122 : :
123 : : /**
124 : : * Return the IOVA (or virtual address in case of virtio-user) of mbuf
125 : : * data buffer.
126 : : *
127 : : * The address is firstly casted to the word size (sizeof(uintptr_t))
128 : : * before casting it to uint64_t. It is then masked with the expected
129 : : * address length (64 bits for virtio-pci, word size for virtio-user).
130 : : *
131 : : * This is to make it work with different combination of word size (64
132 : : * bit and 32 bit) and virtio device (virtio-pci and virtio-user).
133 : : */
134 : : #define VIRTIO_MBUF_ADDR(mb, vq) \
135 : : ((*(uint64_t *)((uintptr_t)(mb) + (vq)->mbuf_addr_offset)) & \
136 : : VIRTIO_MBUF_ADDR_MASK(vq))
137 : :
138 : : /**
139 : : * Return the physical address (or virtual address in case of
140 : : * virtio-user) of mbuf data buffer, taking care of mbuf data offset
141 : : */
142 : : #define VIRTIO_MBUF_DATA_DMA_ADDR(mb, vq) \
143 : : (VIRTIO_MBUF_ADDR(mb, vq) + (mb)->data_off)
144 : :
145 : : #define VTNET_SQ_RQ_QUEUE_IDX 0
146 : : #define VTNET_SQ_TQ_QUEUE_IDX 1
147 : : #define VTNET_SQ_CQ_QUEUE_IDX 2
148 : :
149 : : enum { VTNET_RQ = 0, VTNET_TQ = 1, VTNET_CQ = 2 };
150 : : /**
151 : : * The maximum virtqueue size is 2^15. Use that value as the end of
152 : : * descriptor chain terminator since it will never be a valid index
153 : : * in the descriptor table. This is used to verify we are correctly
154 : : * handling vq_free_cnt.
155 : : */
156 : : #define VQ_RING_DESC_CHAIN_END 32768
157 : :
158 : : #define VIRTIO_NET_OK 0
159 : : #define VIRTIO_NET_ERR 1
160 : :
161 : : struct vq_desc_extra {
162 : : void *cookie;
163 : : uint16_t ndescs;
164 : : uint16_t next;
165 : : };
166 : :
167 : : #define virtnet_rxq_to_vq(rxvq) container_of(rxvq, struct virtqueue, rxq)
168 : : #define virtnet_txq_to_vq(txvq) container_of(txvq, struct virtqueue, txq)
169 : : #define virtnet_cq_to_vq(cvq) container_of(cvq, struct virtqueue, cq)
170 : :
171 : : struct virtqueue {
172 : : struct virtio_hw *hw; /**< virtio_hw structure pointer. */
173 : : union {
174 : : struct {
175 : : /**< vring keeping desc, used and avail */
176 : : struct vring ring;
177 : : } vq_split;
178 : :
179 : : struct {
180 : : /**< vring keeping descs and events */
181 : : struct vring_packed ring;
182 : : bool used_wrap_counter;
183 : : uint16_t cached_flags; /**< cached flags for descs */
184 : : uint16_t event_flags_shadow;
185 : : } vq_packed;
186 : : };
187 : :
188 : : uint16_t vq_used_cons_idx; /**< last consumed descriptor */
189 : : uint16_t vq_nentries; /**< vring desc numbers */
190 : : uint16_t vq_free_cnt; /**< num of desc available */
191 : : uint16_t vq_avail_idx; /**< sync until needed */
192 : : uint16_t vq_free_thresh; /**< free threshold */
193 : :
194 : : /**
195 : : * Head of the free chain in the descriptor table. If
196 : : * there are no free descriptors, this will be set to
197 : : * VQ_RING_DESC_CHAIN_END.
198 : : */
199 : : uint16_t vq_desc_head_idx;
200 : : uint16_t vq_desc_tail_idx;
201 : : uint16_t vq_queue_index; /**< PCI queue index */
202 : :
203 : : void *vq_ring_virt_mem; /**< linear address of vring*/
204 : : unsigned int vq_ring_size;
205 : : uint16_t mbuf_addr_offset;
206 : : uint64_t mbuf_addr_mask;
207 : :
208 : : union {
209 : : struct virtnet_rx rxq;
210 : : struct virtnet_tx txq;
211 : : struct virtnet_ctl cq;
212 : : };
213 : :
214 : : const struct rte_memzone *mz; /**< mem zone to populate ring. */
215 : : rte_iova_t vq_ring_mem; /**< physical address of vring,
216 : : * or virtual address for virtio_user. */
217 : :
218 : : uint16_t *notify_addr;
219 : : struct vq_desc_extra vq_descx[];
220 : : };
221 : :
222 : : /* If multiqueue is provided by host, then we support it. */
223 : : #define VIRTIO_NET_CTRL_MQ 4
224 : :
225 : : #define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET 0
226 : : #define VIRTIO_NET_CTRL_MQ_RSS_CONFIG 1
227 : :
228 : : #define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN 1
229 : : #define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX 0x8000
230 : :
231 : : /**
232 : : * This is the first element of the scatter-gather list. If you don't
233 : : * specify GSO or CSUM features, you can simply ignore the header.
234 : : */
235 : : struct virtio_net_hdr {
236 : : #define VIRTIO_NET_HDR_F_NEEDS_CSUM 1 /**< Use csum_start,csum_offset*/
237 : : #define VIRTIO_NET_HDR_F_DATA_VALID 2 /**< Checksum is valid */
238 : : uint8_t flags;
239 : : #define VIRTIO_NET_HDR_GSO_NONE 0 /**< Not a GSO frame */
240 : : #define VIRTIO_NET_HDR_GSO_TCPV4 1 /**< GSO frame, IPv4 TCP (TSO) */
241 : : #define VIRTIO_NET_HDR_GSO_UDP 3 /**< GSO frame, IPv4 UDP (UFO) */
242 : : #define VIRTIO_NET_HDR_GSO_TCPV6 4 /**< GSO frame, IPv6 TCP */
243 : : #define VIRTIO_NET_HDR_GSO_ECN 0x80 /**< TCP has ECN set */
244 : : uint8_t gso_type;
245 : : uint16_t hdr_len; /**< Ethernet + IP + tcp/udp hdrs */
246 : : uint16_t gso_size; /**< Bytes to append to hdr_len per frame */
247 : : uint16_t csum_start; /**< Position to start checksumming from */
248 : : uint16_t csum_offset; /**< Offset after that to place checksum */
249 : : };
250 : :
251 : : /**
252 : : * This is the version of the header to use when the MRG_RXBUF
253 : : * feature has been negotiated.
254 : : */
255 : : struct virtio_net_hdr_mrg_rxbuf {
256 : : struct virtio_net_hdr hdr;
257 : : uint16_t num_buffers; /**< Number of merged rx buffers */
258 : : };
259 : :
260 : : /**
261 : : * This is the version of the header to use when the HASH_REPORT
262 : : * feature has been negotiated.
263 : : */
264 : : struct virtio_net_hdr_hash_report {
265 : : struct virtio_net_hdr_mrg_rxbuf hdr;
266 : : uint32_t hash_value;
267 : : #define VIRTIO_NET_HASH_REPORT_NONE 0
268 : : #define VIRTIO_NET_HASH_REPORT_IPV4 1
269 : : #define VIRTIO_NET_HASH_REPORT_TCPV4 2
270 : : #define VIRTIO_NET_HASH_REPORT_UDPV4 3
271 : : #define VIRTIO_NET_HASH_REPORT_IPV6 4
272 : : #define VIRTIO_NET_HASH_REPORT_TCPV6 5
273 : : #define VIRTIO_NET_HASH_REPORT_UDPV6 6
274 : : #define VIRTIO_NET_HASH_REPORT_IPV6_EX 7
275 : : #define VIRTIO_NET_HASH_REPORT_TCPV6_EX 8
276 : : #define VIRTIO_NET_HASH_REPORT_UDPV6_EX 9
277 : : uint16_t hash_report;
278 : : uint16_t pad_reserved;
279 : : };
280 : :
281 : : /* Region reserved to allow for transmit header and indirect ring */
282 : : #define VIRTIO_MAX_TX_INDIRECT 8
283 : : struct virtio_tx_region {
284 : : struct virtio_net_hdr_mrg_rxbuf tx_hdr;
285 : : union __rte_aligned(16) {
286 : : struct vring_desc tx_indir[VIRTIO_MAX_TX_INDIRECT];
287 : : struct vring_packed_desc
288 : : tx_packed_indir[VIRTIO_MAX_TX_INDIRECT];
289 : : };
290 : : };
291 : :
292 : : static inline int
293 : : desc_is_used(struct vring_packed_desc *desc, struct virtqueue *vq)
294 : : {
295 : : uint16_t used, avail, flags;
296 : :
297 [ # # ]: 0 : flags = virtqueue_fetch_flags_packed(desc, vq->hw->weak_barriers);
298 : 0 : used = !!(flags & VRING_PACKED_DESC_F_USED);
299 : 0 : avail = !!(flags & VRING_PACKED_DESC_F_AVAIL);
300 : :
301 [ # # # # : 0 : return avail == used && used == vq->vq_packed.used_wrap_counter;
# # # # #
# # # ]
302 : : }
303 : :
304 : : static inline void
305 : : vring_desc_init_packed(struct virtqueue *vq, int n)
306 : : {
307 : : int i;
308 [ # # # # : 0 : for (i = 0; i < n - 1; i++) {
# # ]
309 : 0 : vq->vq_packed.ring.desc[i].id = i;
310 : 0 : vq->vq_descx[i].next = i + 1;
311 : : }
312 : 0 : vq->vq_packed.ring.desc[i].id = i;
313 : 0 : vq->vq_descx[i].next = VQ_RING_DESC_CHAIN_END;
314 : 0 : }
315 : :
316 : : /* Chain all the descriptors in the ring with an END */
317 : : static inline void
318 : : vring_desc_init_split(struct vring_desc *dp, uint16_t n)
319 : : {
320 : : uint16_t i;
321 : :
322 [ # # # # ]: 0 : for (i = 0; i < n - 1; i++)
323 : 0 : dp[i].next = (uint16_t)(i + 1);
324 : 0 : dp[i].next = VQ_RING_DESC_CHAIN_END;
325 : 0 : }
326 : :
327 : : static inline void
328 : : vring_desc_init_indirect_packed(struct vring_packed_desc *dp, int n)
329 : : {
330 : : int i;
331 [ # # ]: 0 : for (i = 0; i < n; i++) {
332 : 0 : dp[i].id = (uint16_t)i;
333 : 0 : dp[i].flags = VRING_DESC_F_WRITE;
334 : : }
335 : : }
336 : :
337 : : /**
338 : : * Tell the backend not to interrupt us. Implementation for packed virtqueues.
339 : : */
340 : : static inline void
341 : : virtqueue_disable_intr_packed(struct virtqueue *vq)
342 : : {
343 [ # # # # : 0 : if (vq->vq_packed.event_flags_shadow != RING_EVENT_FLAGS_DISABLE) {
# # ]
344 : 0 : vq->vq_packed.event_flags_shadow = RING_EVENT_FLAGS_DISABLE;
345 : 0 : vq->vq_packed.ring.driver->desc_event_flags =
346 : : vq->vq_packed.event_flags_shadow;
347 : : }
348 : : }
349 : :
350 : : /**
351 : : * Tell the backend not to interrupt us. Implementation for split virtqueues.
352 : : */
353 : : static inline void
354 : : virtqueue_disable_intr_split(struct virtqueue *vq)
355 : : {
356 : 0 : vq->vq_split.ring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
357 : 0 : }
358 : :
359 : : /**
360 : : * Tell the backend not to interrupt us.
361 : : */
362 : : static inline void
363 : : virtqueue_disable_intr(struct virtqueue *vq)
364 : : {
365 [ # # # # : 0 : if (virtio_with_packed_queue(vq->hw))
# # ]
366 : : virtqueue_disable_intr_packed(vq);
367 : : else
368 : : virtqueue_disable_intr_split(vq);
369 : : }
370 : :
371 : : /**
372 : : * Tell the backend to interrupt. Implementation for packed virtqueues.
373 : : */
374 : : static inline void
375 : : virtqueue_enable_intr_packed(struct virtqueue *vq)
376 : : {
377 [ # # ]: 0 : if (vq->vq_packed.event_flags_shadow == RING_EVENT_FLAGS_DISABLE) {
378 : 0 : vq->vq_packed.event_flags_shadow = RING_EVENT_FLAGS_ENABLE;
379 : 0 : vq->vq_packed.ring.driver->desc_event_flags =
380 : : vq->vq_packed.event_flags_shadow;
381 : : }
382 : : }
383 : :
384 : : /**
385 : : * Tell the backend to interrupt. Implementation for split virtqueues.
386 : : */
387 : : static inline void
388 : : virtqueue_enable_intr_split(struct virtqueue *vq)
389 : : {
390 : 0 : vq->vq_split.ring.avail->flags &= (~VRING_AVAIL_F_NO_INTERRUPT);
391 : 0 : }
392 : :
393 : : /**
394 : : * Tell the backend to interrupt us.
395 : : */
396 : : static inline void
397 : : virtqueue_enable_intr(struct virtqueue *vq)
398 : : {
399 [ # # ]: 0 : if (virtio_with_packed_queue(vq->hw))
400 : : virtqueue_enable_intr_packed(vq);
401 : : else
402 : : virtqueue_enable_intr_split(vq);
403 : : }
404 : :
405 : : /**
406 : : * Get all mbufs to be freed.
407 : : */
408 : : struct rte_mbuf *virtqueue_detach_unused(struct virtqueue *vq);
409 : :
410 : : /* Flush the elements in the used ring. */
411 : : void virtqueue_rxvq_flush(struct virtqueue *vq);
412 : :
413 : : int virtqueue_rxvq_reset_packed(struct virtqueue *vq);
414 : :
415 : : int virtqueue_txvq_reset_packed(struct virtqueue *vq);
416 : :
417 : : void virtqueue_txq_indirect_headers_init(struct virtqueue *vq);
418 : :
419 : : struct virtqueue *virtqueue_alloc(struct virtio_hw *hw, uint16_t index,
420 : : uint16_t num, int type, int node, const char *name);
421 : :
422 : : void virtqueue_free(struct virtqueue *vq);
423 : :
424 : : static inline int
425 : : virtqueue_full(const struct virtqueue *vq)
426 : : {
427 [ # # # # : 0 : return vq->vq_free_cnt == 0;
# # # # #
# # # #
# ]
428 : : }
429 : :
430 : : static inline int
431 : : virtio_get_queue_type(struct virtio_hw *hw, uint16_t vq_idx)
432 : : {
433 [ # # # # : 0 : if (vq_idx == hw->max_queue_pairs * 2)
# # # # ]
434 : : return VTNET_CQ;
435 [ # # # # : 0 : else if (vq_idx % 2 == 0)
# # # # ]
436 : : return VTNET_RQ;
437 : : else
438 : 0 : return VTNET_TQ;
439 : : }
440 : :
441 : : /* virtqueue_nused has load-acquire or rte_io_rmb insed */
442 : : static inline uint16_t
443 : : virtqueue_nused(const struct virtqueue *vq)
444 : : {
445 : : uint16_t idx;
446 : :
447 [ # # # # : 0 : if (vq->hw->weak_barriers) {
# # # # #
# # # # #
# # # # #
# # # ]
448 : : /**
449 : : * x86 prefers to using rte_smp_rmb over rte_atomic_load_explicit as it
450 : : * reports a slightly better perf, which comes from the saved
451 : : * branch by the compiler.
452 : : * The if and else branches are identical with the smp and io
453 : : * barriers both defined as compiler barriers on x86.
454 : : */
455 : : #ifdef RTE_ARCH_X86_64
456 : 0 : idx = vq->vq_split.ring.used->idx;
457 : 0 : rte_smp_rmb();
458 : : #else
459 : : idx = rte_atomic_load_explicit(&(vq)->vq_split.ring.used->idx,
460 : : rte_memory_order_acquire);
461 : : #endif
462 : : } else {
463 : 0 : idx = vq->vq_split.ring.used->idx;
464 : 0 : rte_io_rmb();
465 : : }
466 [ # # # # : 0 : return idx - vq->vq_used_cons_idx;
# # # # #
# # # # #
# # # # ]
467 : : }
468 : :
469 : : void vq_ring_free_chain(struct virtqueue *vq, uint16_t desc_idx);
470 : : void vq_ring_free_chain_packed(struct virtqueue *vq, uint16_t used_idx);
471 : : void vq_ring_free_inorder(struct virtqueue *vq, uint16_t desc_idx,
472 : : uint16_t num);
473 : :
474 : : static inline void
475 : : vq_update_avail_idx(struct virtqueue *vq)
476 : : {
477 [ # # # # : 0 : if (vq->hw->weak_barriers) {
# # # # #
# # # # #
# # ]
478 : : /* x86 prefers to using rte_smp_wmb over rte_atomic_store_explicit as
479 : : * it reports a slightly better perf, which comes from the
480 : : * saved branch by the compiler.
481 : : * The if and else branches are identical with the smp and
482 : : * io barriers both defined as compiler barriers on x86.
483 : : */
484 : : #ifdef RTE_ARCH_X86_64
485 : 0 : rte_smp_wmb();
486 : 0 : vq->vq_split.ring.avail->idx = vq->vq_avail_idx;
487 : : #else
488 : : rte_atomic_store_explicit(&vq->vq_split.ring.avail->idx,
489 : : vq->vq_avail_idx, rte_memory_order_release);
490 : : #endif
491 : : } else {
492 : 0 : rte_io_wmb();
493 : 0 : vq->vq_split.ring.avail->idx = vq->vq_avail_idx;
494 : : }
495 : : }
496 : :
497 : : static inline void
498 : : vq_update_avail_ring(struct virtqueue *vq, uint16_t desc_idx)
499 : : {
500 : : uint16_t avail_idx;
501 : : /*
502 : : * Place the head of the descriptor chain into the next slot and make
503 : : * it usable to the host. The chain is made available now rather than
504 : : * deferring to virtqueue_notify() in the hopes that if the host is
505 : : * currently running on another CPU, we can keep it processing the new
506 : : * descriptor.
507 : : */
508 : 0 : avail_idx = (uint16_t)(vq->vq_avail_idx & (vq->vq_nentries - 1));
509 [ # # # # : 0 : if (unlikely(vq->vq_split.ring.avail->ring[avail_idx] != desc_idx))
# # # # ]
510 : 0 : vq->vq_split.ring.avail->ring[avail_idx] = desc_idx;
511 [ # # # # ]: 0 : vq->vq_avail_idx++;
512 : : }
513 : :
514 : : static inline int
515 : : virtqueue_kick_prepare(struct virtqueue *vq)
516 : : {
517 : : /*
518 : : * Ensure updated avail->idx is visible to vhost before reading
519 : : * the used->flags.
520 : : */
521 [ # # # # : 0 : virtio_mb(vq->hw->weak_barriers);
# # # # #
# ]
522 [ # # # # : 0 : return !(vq->vq_split.ring.used->flags & VRING_USED_F_NO_NOTIFY);
# # # # #
# ]
523 : : }
524 : :
525 : : static inline int
526 : : virtqueue_kick_prepare_packed(struct virtqueue *vq)
527 : : {
528 : : uint16_t flags;
529 : :
530 : : /*
531 : : * Ensure updated data is visible to vhost before reading the flags.
532 : : */
533 [ # # # # : 0 : virtio_mb(vq->hw->weak_barriers);
# # ]
534 : 0 : flags = vq->vq_packed.ring.device->desc_event_flags;
535 : :
536 [ # # # # : 0 : return flags != RING_EVENT_FLAGS_DISABLE;
# # ]
537 : : }
538 : :
539 : : /*
540 : : * virtqueue_kick_prepare*() or the virtio_wmb() should be called
541 : : * before this function to be sure that all the data is visible to vhost.
542 : : */
543 : : static inline void
544 : : virtqueue_notify(struct virtqueue *vq)
545 : : {
546 : 0 : VIRTIO_OPS(vq->hw)->notify_queue(vq->hw, vq);
547 : 0 : }
548 : :
549 : : #ifdef RTE_LIBRTE_VIRTIO_DEBUG_DUMP
550 : : #define VIRTQUEUE_DUMP(vq) do { \
551 : : uint16_t used_idx, nused; \
552 : : used_idx = rte_atomic_load_explicit(&(vq)->vq_split.ring.used->idx, \
553 : : rte_memory_order_relaxed); \
554 : : nused = (uint16_t)(used_idx - (vq)->vq_used_cons_idx); \
555 : : if (virtio_with_packed_queue((vq)->hw)) { \
556 : : PMD_INIT_LOG(DEBUG, \
557 : : "VQ: - size=%d; free=%d; used_cons_idx=%d; avail_idx=%d;" \
558 : : " cached_flags=0x%x; used_wrap_counter=%d", \
559 : : (vq)->vq_nentries, (vq)->vq_free_cnt, (vq)->vq_used_cons_idx, \
560 : : (vq)->vq_avail_idx, (vq)->vq_packed.cached_flags, \
561 : : (vq)->vq_packed.used_wrap_counter); \
562 : : break; \
563 : : } \
564 : : PMD_INIT_LOG(DEBUG, \
565 : : "VQ: - size=%d; free=%d; used=%d; desc_head_idx=%d;" \
566 : : " avail.idx=%d; used_cons_idx=%d; used.idx=%d;" \
567 : : " avail.flags=0x%x; used.flags=0x%x", \
568 : : (vq)->vq_nentries, (vq)->vq_free_cnt, nused, (vq)->vq_desc_head_idx, \
569 : : (vq)->vq_split.ring.avail->idx, (vq)->vq_used_cons_idx, \
570 : : rte_atomic_load_explicit(&(vq)->vq_split.ring.used->idx, rte_memory_order_relaxed), \
571 : : (vq)->vq_split.ring.avail->flags, (vq)->vq_split.ring.used->flags); \
572 : : } while (0)
573 : : #else
574 : : #define VIRTQUEUE_DUMP(vq) do { } while (0)
575 : : #endif
576 : :
577 : : /* avoid write operation when necessary, to lessen cache issues */
578 : : #define ASSIGN_UNLESS_EQUAL(var, val) do { \
579 : : typeof(var) *const var_ = &(var); \
580 : : typeof(val) const val_ = (val); \
581 : : if (*var_ != val_) \
582 : : *var_ = val_; \
583 : : } while (0)
584 : :
585 : : #define virtqueue_clear_net_hdr(hdr) do { \
586 : : typeof(hdr) hdr_ = (hdr); \
587 : : ASSIGN_UNLESS_EQUAL((hdr_)->csum_start, 0); \
588 : : ASSIGN_UNLESS_EQUAL((hdr_)->csum_offset, 0); \
589 : : ASSIGN_UNLESS_EQUAL((hdr_)->flags, 0); \
590 : : ASSIGN_UNLESS_EQUAL((hdr_)->gso_type, 0); \
591 : : ASSIGN_UNLESS_EQUAL((hdr_)->gso_size, 0); \
592 : : ASSIGN_UNLESS_EQUAL((hdr_)->hdr_len, 0); \
593 : : } while (0)
594 : :
595 : : static inline void
596 : 0 : virtqueue_xmit_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *cookie)
597 : : {
598 : 0 : uint64_t csum_l4 = cookie->ol_flags & RTE_MBUF_F_TX_L4_MASK;
599 [ # # ]: 0 : uint16_t o_l23_len = (cookie->ol_flags & RTE_MBUF_F_TX_TUNNEL_MASK) ?
600 : 0 : cookie->outer_l2_len + cookie->outer_l3_len : 0;
601 : :
602 [ # # ]: 0 : if (cookie->ol_flags & RTE_MBUF_F_TX_TCP_SEG)
603 : 0 : csum_l4 |= RTE_MBUF_F_TX_TCP_CKSUM;
604 : :
605 [ # # # ]: 0 : switch (csum_l4) {
606 : 0 : case RTE_MBUF_F_TX_UDP_CKSUM:
607 : 0 : hdr->csum_start = o_l23_len + cookie->l2_len + cookie->l3_len;
608 : 0 : hdr->csum_offset = offsetof(struct rte_udp_hdr, dgram_cksum);
609 : 0 : hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
610 : 0 : break;
611 : :
612 : 0 : case RTE_MBUF_F_TX_TCP_CKSUM:
613 : 0 : hdr->csum_start = o_l23_len + cookie->l2_len + cookie->l3_len;
614 : 0 : hdr->csum_offset = offsetof(struct rte_tcp_hdr, cksum);
615 : 0 : hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
616 : 0 : break;
617 : :
618 : 0 : default:
619 [ # # ]: 0 : ASSIGN_UNLESS_EQUAL(hdr->csum_start, 0);
620 [ # # ]: 0 : ASSIGN_UNLESS_EQUAL(hdr->csum_offset, 0);
621 [ # # ]: 0 : ASSIGN_UNLESS_EQUAL(hdr->flags, 0);
622 : : break;
623 : : }
624 : :
625 : : /* TCP Segmentation Offload */
626 [ # # ]: 0 : if (cookie->ol_flags & RTE_MBUF_F_TX_TCP_SEG) {
627 [ # # ]: 0 : hdr->gso_type = (cookie->ol_flags & RTE_MBUF_F_TX_IPV6) ?
628 : : VIRTIO_NET_HDR_GSO_TCPV6 :
629 : : VIRTIO_NET_HDR_GSO_TCPV4;
630 : 0 : hdr->gso_size = cookie->tso_segsz;
631 : 0 : hdr->hdr_len = o_l23_len + cookie->l2_len + cookie->l3_len +
632 : 0 : cookie->l4_len;
633 : : } else {
634 [ # # ]: 0 : ASSIGN_UNLESS_EQUAL(hdr->gso_type, 0);
635 [ # # ]: 0 : ASSIGN_UNLESS_EQUAL(hdr->gso_size, 0);
636 [ # # ]: 0 : ASSIGN_UNLESS_EQUAL(hdr->hdr_len, 0);
637 : : }
638 : 0 : }
639 : :
640 : : static inline void
641 : 0 : virtqueue_enqueue_xmit_packed(struct virtnet_tx *txvq, struct rte_mbuf *cookie,
642 : : uint16_t needed, int use_indirect, int can_push,
643 : : int in_order)
644 : : {
645 : 0 : struct virtio_tx_region *txr = txvq->hdr_mz->addr;
646 : : struct vq_desc_extra *dxp;
647 : 0 : struct virtqueue *vq = virtnet_txq_to_vq(txvq);
648 : : struct vring_packed_desc *start_dp, *head_dp;
649 : : uint16_t idx, id, head_idx, head_flags;
650 : 0 : int16_t head_size = vq->hw->vtnet_hdr_size;
651 : : struct virtio_net_hdr *hdr;
652 : : uint16_t prev;
653 : : bool prepend_header = false;
654 : 0 : uint16_t seg_num = cookie->nb_segs;
655 : :
656 [ # # ]: 0 : id = in_order ? vq->vq_avail_idx : vq->vq_desc_head_idx;
657 : :
658 : 0 : dxp = &vq->vq_descx[id];
659 : 0 : dxp->ndescs = needed;
660 : 0 : dxp->cookie = cookie;
661 : :
662 : 0 : head_idx = vq->vq_avail_idx;
663 : : idx = head_idx;
664 : : prev = head_idx;
665 : 0 : start_dp = vq->vq_packed.ring.desc;
666 : :
667 : 0 : head_dp = &vq->vq_packed.ring.desc[idx];
668 : 0 : head_flags = cookie->next ? VRING_DESC_F_NEXT : 0;
669 : 0 : head_flags |= vq->vq_packed.cached_flags;
670 : :
671 [ # # ]: 0 : if (can_push) {
672 : : /* prepend cannot fail, checked by caller */
673 : 0 : hdr = rte_pktmbuf_mtod_offset(cookie, struct virtio_net_hdr *,
674 : : -head_size);
675 : : prepend_header = true;
676 : :
677 : : /* if offload disabled, it is not zeroed below, do it now */
678 [ # # ]: 0 : if (!vq->hw->has_tx_offload)
679 [ # # # # : 0 : virtqueue_clear_net_hdr(hdr);
# # # # #
# # # ]
680 [ # # ]: 0 : } else if (use_indirect) {
681 : : /* setup tx ring slot to point to indirect
682 : : * descriptor list stored in reserved region.
683 : : *
684 : : * the first slot in indirect ring is already preset
685 : : * to point to the header in reserved region
686 : : */
687 : 0 : start_dp[idx].addr = txvq->hdr_mem + RTE_PTR_DIFF(&txr[idx].tx_packed_indir, txr);
688 : 0 : start_dp[idx].len = (seg_num + 1) * sizeof(struct vring_packed_desc);
689 : : /* Packed descriptor id needs to be restored when inorder. */
690 [ # # ]: 0 : if (in_order)
691 : 0 : start_dp[idx].id = idx;
692 : : /* reset flags for indirect desc */
693 : : head_flags = VRING_DESC_F_INDIRECT;
694 : 0 : head_flags |= vq->vq_packed.cached_flags;
695 : 0 : hdr = (struct virtio_net_hdr *)&txr[idx].tx_hdr;
696 : :
697 : : /* loop below will fill in rest of the indirect elements */
698 : : start_dp = txr[idx].tx_packed_indir;
699 : : idx = 1;
700 : : } else {
701 : : /* setup first tx ring slot to point to header
702 : : * stored in reserved region.
703 : : */
704 : 0 : start_dp[idx].addr = txvq->hdr_mem + RTE_PTR_DIFF(&txr[idx].tx_hdr, txr);
705 : 0 : start_dp[idx].len = vq->hw->vtnet_hdr_size;
706 : 0 : head_flags |= VRING_DESC_F_NEXT;
707 : : hdr = (struct virtio_net_hdr *)&txr[idx].tx_hdr;
708 : 0 : idx++;
709 [ # # ]: 0 : if (idx >= vq->vq_nentries) {
710 : 0 : idx -= vq->vq_nentries;
711 : 0 : vq->vq_packed.cached_flags ^=
712 : : VRING_PACKED_DESC_F_AVAIL_USED;
713 : : }
714 : : }
715 : :
716 [ # # ]: 0 : if (vq->hw->has_tx_offload)
717 : 0 : virtqueue_xmit_offload(hdr, cookie);
718 : :
719 : : do {
720 : : uint16_t flags;
721 : :
722 : 0 : start_dp[idx].addr = VIRTIO_MBUF_DATA_DMA_ADDR(cookie, vq);
723 : 0 : start_dp[idx].len = cookie->data_len;
724 [ # # ]: 0 : if (prepend_header) {
725 : 0 : start_dp[idx].addr -= head_size;
726 : 0 : start_dp[idx].len += head_size;
727 : : prepend_header = false;
728 : : }
729 : :
730 [ # # ]: 0 : if (likely(idx != head_idx)) {
731 : 0 : flags = cookie->next ? VRING_DESC_F_NEXT : 0;
732 : 0 : flags |= vq->vq_packed.cached_flags;
733 : 0 : start_dp[idx].flags = flags;
734 : : }
735 : : prev = idx;
736 : 0 : idx++;
737 [ # # ]: 0 : if (idx >= vq->vq_nentries) {
738 : 0 : idx -= vq->vq_nentries;
739 : 0 : vq->vq_packed.cached_flags ^=
740 : : VRING_PACKED_DESC_F_AVAIL_USED;
741 : : }
742 [ # # ]: 0 : } while ((cookie = cookie->next) != NULL);
743 : :
744 : 0 : start_dp[prev].id = id;
745 : :
746 [ # # ]: 0 : if (use_indirect) {
747 : : idx = head_idx;
748 [ # # ]: 0 : if (++idx >= vq->vq_nentries) {
749 : 0 : idx -= vq->vq_nentries;
750 : 0 : vq->vq_packed.cached_flags ^=
751 : : VRING_PACKED_DESC_F_AVAIL_USED;
752 : : }
753 : : }
754 : :
755 : 0 : vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - needed);
756 : 0 : vq->vq_avail_idx = idx;
757 : :
758 [ # # ]: 0 : if (!in_order) {
759 : 0 : vq->vq_desc_head_idx = dxp->next;
760 [ # # ]: 0 : if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
761 : 0 : vq->vq_desc_tail_idx = VQ_RING_DESC_CHAIN_END;
762 : : }
763 : :
764 : : virtqueue_store_flags_packed(head_dp, head_flags,
765 [ # # ]: 0 : vq->hw->weak_barriers);
766 : 0 : }
767 : :
768 : : static void
769 : : vq_ring_free_id_packed(struct virtqueue *vq, uint16_t id)
770 : : {
771 : : struct vq_desc_extra *dxp;
772 : :
773 : : dxp = &vq->vq_descx[id];
774 : 0 : vq->vq_free_cnt += dxp->ndescs;
775 : :
776 [ # # ]: 0 : if (vq->vq_desc_tail_idx == VQ_RING_DESC_CHAIN_END)
777 : 0 : vq->vq_desc_head_idx = id;
778 : : else
779 : 0 : vq->vq_descx[vq->vq_desc_tail_idx].next = id;
780 : :
781 : 0 : vq->vq_desc_tail_idx = id;
782 : 0 : dxp->next = VQ_RING_DESC_CHAIN_END;
783 : : }
784 : :
785 : : static void
786 : 0 : virtio_xmit_cleanup_inorder_packed(struct virtqueue *vq, uint16_t num)
787 : : {
788 : : uint16_t used_idx, id, curr_id, free_cnt = 0;
789 : 0 : uint16_t size = vq->vq_nentries;
790 : 0 : struct vring_packed_desc *desc = vq->vq_packed.ring.desc;
791 : : struct vq_desc_extra *dxp;
792 : 0 : int nb = num;
793 : :
794 : 0 : used_idx = vq->vq_used_cons_idx;
795 : : /* desc_is_used has a load-acquire or rte_io_rmb inside
796 : : * and wait for used desc in virtqueue.
797 : : */
798 [ # # # # ]: 0 : while (nb > 0 && desc_is_used(&desc[used_idx], vq)) {
799 : 0 : id = desc[used_idx].id;
800 : : do {
801 : : curr_id = used_idx;
802 : 0 : dxp = &vq->vq_descx[used_idx];
803 : 0 : used_idx += dxp->ndescs;
804 : 0 : free_cnt += dxp->ndescs;
805 : 0 : nb -= dxp->ndescs;
806 [ # # ]: 0 : if (used_idx >= size) {
807 : 0 : used_idx -= size;
808 : 0 : vq->vq_packed.used_wrap_counter ^= 1;
809 : : }
810 [ # # ]: 0 : if (dxp->cookie != NULL) {
811 : 0 : rte_pktmbuf_free(dxp->cookie);
812 : 0 : dxp->cookie = NULL;
813 : : }
814 [ # # ]: 0 : } while (curr_id != id);
815 : : }
816 : 0 : vq->vq_used_cons_idx = used_idx;
817 : 0 : vq->vq_free_cnt += free_cnt;
818 : 0 : }
819 : :
820 : : static void
821 : 0 : virtio_xmit_cleanup_normal_packed(struct virtqueue *vq, uint16_t num)
822 : : {
823 : : uint16_t used_idx, id;
824 : 0 : uint16_t size = vq->vq_nentries;
825 : 0 : struct vring_packed_desc *desc = vq->vq_packed.ring.desc;
826 : : struct vq_desc_extra *dxp;
827 : :
828 : 0 : used_idx = vq->vq_used_cons_idx;
829 : : /* desc_is_used has a load-acquire or rte_io_rmb inside
830 : : * and wait for used desc in virtqueue.
831 : : */
832 [ # # # # ]: 0 : while (num-- && desc_is_used(&desc[used_idx], vq)) {
833 : 0 : id = desc[used_idx].id;
834 : 0 : dxp = &vq->vq_descx[id];
835 : 0 : vq->vq_used_cons_idx += dxp->ndescs;
836 [ # # ]: 0 : if (vq->vq_used_cons_idx >= size) {
837 : 0 : vq->vq_used_cons_idx -= size;
838 : 0 : vq->vq_packed.used_wrap_counter ^= 1;
839 : : }
840 : : vq_ring_free_id_packed(vq, id);
841 [ # # ]: 0 : if (dxp->cookie != NULL) {
842 : 0 : rte_pktmbuf_free(dxp->cookie);
843 : 0 : dxp->cookie = NULL;
844 : : }
845 : 0 : used_idx = vq->vq_used_cons_idx;
846 : : }
847 : 0 : }
848 : :
849 : : /* Cleanup from completed transmits. */
850 : : static inline void
851 : : virtio_xmit_cleanup_packed(struct virtqueue *vq, uint16_t num, int in_order)
852 : : {
853 [ # # # # ]: 0 : if (in_order)
854 : 0 : virtio_xmit_cleanup_inorder_packed(vq, num);
855 : : else
856 : 0 : virtio_xmit_cleanup_normal_packed(vq, num);
857 : : }
858 : :
859 : : static inline void
860 : 0 : virtio_xmit_cleanup(struct virtqueue *vq, uint16_t num)
861 : : {
862 : : uint16_t i, used_idx, desc_idx;
863 [ # # ]: 0 : for (i = 0; i < num; i++) {
864 : : struct vring_used_elem *uep;
865 : : struct vq_desc_extra *dxp;
866 : :
867 : 0 : used_idx = (uint16_t)(vq->vq_used_cons_idx &
868 : 0 : (vq->vq_nentries - 1));
869 : 0 : uep = &vq->vq_split.ring.used->ring[used_idx];
870 : :
871 : 0 : desc_idx = (uint16_t)uep->id;
872 : 0 : dxp = &vq->vq_descx[desc_idx];
873 : 0 : vq->vq_used_cons_idx++;
874 : 0 : vq_ring_free_chain(vq, desc_idx);
875 : :
876 [ # # ]: 0 : if (dxp->cookie != NULL) {
877 : 0 : rte_pktmbuf_free(dxp->cookie);
878 : 0 : dxp->cookie = NULL;
879 : : }
880 : : }
881 : 0 : }
882 : :
883 : : /* Cleanup from completed inorder transmits. */
884 : : static __rte_always_inline void
885 : 0 : virtio_xmit_cleanup_inorder(struct virtqueue *vq, uint16_t num)
886 : : {
887 : 0 : uint16_t i, idx = vq->vq_used_cons_idx;
888 : : int16_t free_cnt = 0;
889 : : struct vq_desc_extra *dxp = NULL;
890 : :
891 [ # # # # : 0 : if (unlikely(num == 0))
# # # # ]
892 : : return;
893 : :
894 [ # # # # : 0 : for (i = 0; i < num; i++) {
# # # # ]
895 : 0 : dxp = &vq->vq_descx[idx++ & (vq->vq_nentries - 1)];
896 : 0 : free_cnt += dxp->ndescs;
897 [ # # # # : 0 : if (dxp->cookie != NULL) {
# # # # ]
898 : 0 : rte_pktmbuf_free(dxp->cookie);
899 : 0 : dxp->cookie = NULL;
900 : : }
901 : : }
902 : :
903 : 0 : vq->vq_free_cnt += free_cnt;
904 : 0 : vq->vq_used_cons_idx = idx;
905 : : }
906 : : #endif /* _VIRTQUEUE_H_ */
|