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