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