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