011a67af21090a64aa8cabf8e9987c89ae2e07cb
[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
14 #include "virtio_pci.h"
15 #include "virtio_ring.h"
16 #include "virtio_logs.h"
17 #include "virtio_rxtx.h"
18
19 struct rte_mbuf;
20
21 #define DEFAULT_RX_FREE_THRESH 32
22
23 /*
24  * Per virtio_ring.h in Linux.
25  *     For virtio_pci on SMP, we don't need to order with respect to MMIO
26  *     accesses through relaxed memory I/O windows, so smp_mb() et al are
27  *     sufficient.
28  *
29  *     For using virtio to talk to real devices (eg. vDPA) we do need real
30  *     barriers.
31  */
32 static inline void
33 virtio_mb(uint8_t weak_barriers)
34 {
35         if (weak_barriers)
36                 rte_smp_mb();
37         else
38                 rte_mb();
39 }
40
41 static inline void
42 virtio_rmb(uint8_t weak_barriers)
43 {
44         if (weak_barriers)
45                 rte_smp_rmb();
46         else
47                 rte_cio_rmb();
48 }
49
50 static inline void
51 virtio_wmb(uint8_t weak_barriers)
52 {
53         if (weak_barriers)
54                 rte_smp_wmb();
55         else
56                 rte_cio_wmb();
57 }
58
59 static inline uint16_t
60 virtqueue_fetch_flags_packed(struct vring_packed_desc *dp,
61                               uint8_t weak_barriers)
62 {
63         uint16_t flags;
64
65         if (weak_barriers) {
66 /* x86 prefers to using rte_smp_rmb over __atomic_load_n as it reports
67  * a better perf(~1.5%), which comes from the saved branch by the compiler.
68  * The if and else branch are identical with the smp and cio barriers both
69  * defined as compiler barriers on x86.
70  */
71 #ifdef RTE_ARCH_X86_64
72                 flags = dp->flags;
73                 rte_smp_rmb();
74 #else
75                 flags = __atomic_load_n(&dp->flags, __ATOMIC_ACQUIRE);
76 #endif
77         } else {
78                 flags = dp->flags;
79                 rte_cio_rmb();
80         }
81
82         return flags;
83 }
84
85 static inline void
86 virtqueue_store_flags_packed(struct vring_packed_desc *dp,
87                               uint16_t flags, uint8_t weak_barriers)
88 {
89         if (weak_barriers) {
90 /* x86 prefers to using rte_smp_wmb over __atomic_store_n as it reports
91  * a better perf(~1.5%), which comes from the saved branch by the compiler.
92  * The if and else branch are identical with the smp and cio barriers both
93  * defined as compiler barriers on x86.
94  */
95 #ifdef RTE_ARCH_X86_64
96                 rte_smp_wmb();
97                 dp->flags = flags;
98 #else
99                 __atomic_store_n(&dp->flags, flags, __ATOMIC_RELEASE);
100 #endif
101         } else {
102                 rte_cio_wmb();
103                 dp->flags = flags;
104         }
105 }
106 #ifdef RTE_PMD_PACKET_PREFETCH
107 #define rte_packet_prefetch(p)  rte_prefetch1(p)
108 #else
109 #define rte_packet_prefetch(p)  do {} while(0)
110 #endif
111
112 #define VIRTQUEUE_MAX_NAME_SZ 32
113
114 #ifdef RTE_VIRTIO_USER
115 /**
116  * Return the physical address (or virtual address in case of
117  * virtio-user) of mbuf data buffer.
118  *
119  * The address is firstly casted to the word size (sizeof(uintptr_t))
120  * before casting it to uint64_t. This is to make it work with different
121  * combination of word size (64 bit and 32 bit) and virtio device
122  * (virtio-pci and virtio-user).
123  */
124 #define VIRTIO_MBUF_ADDR(mb, vq) \
125         ((uint64_t)(*(uintptr_t *)((uintptr_t)(mb) + (vq)->offset)))
126 #else
127 #define VIRTIO_MBUF_ADDR(mb, vq) ((mb)->buf_iova)
128 #endif
129
130 /**
131  * Return the physical address (or virtual address in case of
132  * virtio-user) of mbuf data buffer, taking care of mbuf data offset
133  */
134 #define VIRTIO_MBUF_DATA_DMA_ADDR(mb, vq) \
135         (VIRTIO_MBUF_ADDR(mb, vq) + (mb)->data_off)
136
137 #define VTNET_SQ_RQ_QUEUE_IDX 0
138 #define VTNET_SQ_TQ_QUEUE_IDX 1
139 #define VTNET_SQ_CQ_QUEUE_IDX 2
140
141 enum { VTNET_RQ = 0, VTNET_TQ = 1, VTNET_CQ = 2 };
142 /**
143  * The maximum virtqueue size is 2^15. Use that value as the end of
144  * descriptor chain terminator since it will never be a valid index
145  * in the descriptor table. This is used to verify we are correctly
146  * handling vq_free_cnt.
147  */
148 #define VQ_RING_DESC_CHAIN_END 32768
149
150 /**
151  * Control the RX mode, ie. promiscuous, allmulti, etc...
152  * All commands require an "out" sg entry containing a 1 byte
153  * state value, zero = disable, non-zero = enable.  Commands
154  * 0 and 1 are supported with the VIRTIO_NET_F_CTRL_RX feature.
155  * Commands 2-5 are added with VIRTIO_NET_F_CTRL_RX_EXTRA.
156  */
157 #define VIRTIO_NET_CTRL_RX              0
158 #define VIRTIO_NET_CTRL_RX_PROMISC      0
159 #define VIRTIO_NET_CTRL_RX_ALLMULTI     1
160 #define VIRTIO_NET_CTRL_RX_ALLUNI       2
161 #define VIRTIO_NET_CTRL_RX_NOMULTI      3
162 #define VIRTIO_NET_CTRL_RX_NOUNI        4
163 #define VIRTIO_NET_CTRL_RX_NOBCAST      5
164
165 /**
166  * Control the MAC
167  *
168  * The MAC filter table is managed by the hypervisor, the guest should
169  * assume the size is infinite.  Filtering should be considered
170  * non-perfect, ie. based on hypervisor resources, the guest may
171  * received packets from sources not specified in the filter list.
172  *
173  * In addition to the class/cmd header, the TABLE_SET command requires
174  * two out scatterlists.  Each contains a 4 byte count of entries followed
175  * by a concatenated byte stream of the ETH_ALEN MAC addresses.  The
176  * first sg list contains unicast addresses, the second is for multicast.
177  * This functionality is present if the VIRTIO_NET_F_CTRL_RX feature
178  * is available.
179  *
180  * The ADDR_SET command requests one out scatterlist, it contains a
181  * 6 bytes MAC address. This functionality is present if the
182  * VIRTIO_NET_F_CTRL_MAC_ADDR feature is available.
183  */
184 struct virtio_net_ctrl_mac {
185         uint32_t entries;
186         uint8_t macs[][RTE_ETHER_ADDR_LEN];
187 } __rte_packed;
188
189 #define VIRTIO_NET_CTRL_MAC    1
190 #define VIRTIO_NET_CTRL_MAC_TABLE_SET        0
191 #define VIRTIO_NET_CTRL_MAC_ADDR_SET         1
192
193 /**
194  * Control VLAN filtering
195  *
196  * The VLAN filter table is controlled via a simple ADD/DEL interface.
197  * VLAN IDs not added may be filtered by the hypervisor.  Del is the
198  * opposite of add.  Both commands expect an out entry containing a 2
199  * byte VLAN ID.  VLAN filtering is available with the
200  * VIRTIO_NET_F_CTRL_VLAN feature bit.
201  */
202 #define VIRTIO_NET_CTRL_VLAN     2
203 #define VIRTIO_NET_CTRL_VLAN_ADD 0
204 #define VIRTIO_NET_CTRL_VLAN_DEL 1
205
206 /*
207  * Control link announce acknowledgement
208  *
209  * The command VIRTIO_NET_CTRL_ANNOUNCE_ACK is used to indicate that
210  * driver has recevied the notification; device would clear the
211  * VIRTIO_NET_S_ANNOUNCE bit in the status field after it receives
212  * this command.
213  */
214 #define VIRTIO_NET_CTRL_ANNOUNCE     3
215 #define VIRTIO_NET_CTRL_ANNOUNCE_ACK 0
216
217 struct virtio_net_ctrl_hdr {
218         uint8_t class;
219         uint8_t cmd;
220 } __rte_packed;
221
222 typedef uint8_t virtio_net_ctrl_ack;
223
224 #define VIRTIO_NET_OK     0
225 #define VIRTIO_NET_ERR    1
226
227 #define VIRTIO_MAX_CTRL_DATA 2048
228
229 struct virtio_pmd_ctrl {
230         struct virtio_net_ctrl_hdr hdr;
231         virtio_net_ctrl_ack status;
232         uint8_t data[VIRTIO_MAX_CTRL_DATA];
233 };
234
235 struct vq_desc_extra {
236         void *cookie;
237         uint16_t ndescs;
238         uint16_t next;
239 };
240
241 struct virtqueue {
242         struct virtio_hw  *hw; /**< virtio_hw structure pointer. */
243         union {
244                 struct {
245                         /**< vring keeping desc, used and avail */
246                         struct vring ring;
247                 } vq_split;
248
249                 struct {
250                         /**< vring keeping descs and events */
251                         struct vring_packed ring;
252                         bool used_wrap_counter;
253                         uint16_t cached_flags; /**< cached flags for descs */
254                         uint16_t event_flags_shadow;
255                 } vq_packed;
256         };
257
258         uint16_t vq_used_cons_idx; /**< last consumed descriptor */
259         uint16_t vq_nentries;  /**< vring desc numbers */
260         uint16_t vq_free_cnt;  /**< num of desc available */
261         uint16_t vq_avail_idx; /**< sync until needed */
262         uint16_t vq_free_thresh; /**< free threshold */
263
264         void *vq_ring_virt_mem;  /**< linear address of vring*/
265         unsigned int vq_ring_size;
266
267         union {
268                 struct virtnet_rx rxq;
269                 struct virtnet_tx txq;
270                 struct virtnet_ctl cq;
271         };
272
273         rte_iova_t vq_ring_mem; /**< physical address of vring,
274                                  * or virtual address for virtio_user. */
275
276         /**
277          * Head of the free chain in the descriptor table. If
278          * there are no free descriptors, this will be set to
279          * VQ_RING_DESC_CHAIN_END.
280          */
281         uint16_t  vq_desc_head_idx;
282         uint16_t  vq_desc_tail_idx;
283         uint16_t  vq_queue_index;   /**< PCI queue index */
284         uint16_t offset; /**< relative offset to obtain addr in mbuf */
285         uint16_t  *notify_addr;
286         struct rte_mbuf **sw_ring;  /**< RX software ring. */
287         struct vq_desc_extra vq_descx[0];
288 };
289
290 /* If multiqueue is provided by host, then we suppport it. */
291 #define VIRTIO_NET_CTRL_MQ   4
292 #define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET        0
293 #define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN        1
294 #define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX        0x8000
295
296 /**
297  * This is the first element of the scatter-gather list.  If you don't
298  * specify GSO or CSUM features, you can simply ignore the header.
299  */
300 struct virtio_net_hdr {
301 #define VIRTIO_NET_HDR_F_NEEDS_CSUM 1    /**< Use csum_start,csum_offset*/
302 #define VIRTIO_NET_HDR_F_DATA_VALID 2    /**< Checksum is valid */
303         uint8_t flags;
304 #define VIRTIO_NET_HDR_GSO_NONE     0    /**< Not a GSO frame */
305 #define VIRTIO_NET_HDR_GSO_TCPV4    1    /**< GSO frame, IPv4 TCP (TSO) */
306 #define VIRTIO_NET_HDR_GSO_UDP      3    /**< GSO frame, IPv4 UDP (UFO) */
307 #define VIRTIO_NET_HDR_GSO_TCPV6    4    /**< GSO frame, IPv6 TCP */
308 #define VIRTIO_NET_HDR_GSO_ECN      0x80 /**< TCP has ECN set */
309         uint8_t gso_type;
310         uint16_t hdr_len;     /**< Ethernet + IP + tcp/udp hdrs */
311         uint16_t gso_size;    /**< Bytes to append to hdr_len per frame */
312         uint16_t csum_start;  /**< Position to start checksumming from */
313         uint16_t csum_offset; /**< Offset after that to place checksum */
314 };
315
316 /**
317  * This is the version of the header to use when the MRG_RXBUF
318  * feature has been negotiated.
319  */
320 struct virtio_net_hdr_mrg_rxbuf {
321         struct   virtio_net_hdr hdr;
322         uint16_t num_buffers; /**< Number of merged rx buffers */
323 };
324
325 /* Region reserved to allow for transmit header and indirect ring */
326 #define VIRTIO_MAX_TX_INDIRECT 8
327 struct virtio_tx_region {
328         struct virtio_net_hdr_mrg_rxbuf tx_hdr;
329         struct vring_desc tx_indir[VIRTIO_MAX_TX_INDIRECT]
330                 __rte_aligned(16);
331 };
332
333 static inline int
334 desc_is_used(struct vring_packed_desc *desc, struct virtqueue *vq)
335 {
336         uint16_t used, avail, flags;
337
338         flags = virtqueue_fetch_flags_packed(desc, vq->hw->weak_barriers);
339         used = !!(flags & VRING_PACKED_DESC_F_USED);
340         avail = !!(flags & VRING_PACKED_DESC_F_AVAIL);
341
342         return avail == used && used == vq->vq_packed.used_wrap_counter;
343 }
344
345 static inline void
346 vring_desc_init_packed(struct virtqueue *vq, int n)
347 {
348         int i;
349         for (i = 0; i < n - 1; i++) {
350                 vq->vq_packed.ring.desc[i].id = i;
351                 vq->vq_descx[i].next = i + 1;
352         }
353         vq->vq_packed.ring.desc[i].id = i;
354         vq->vq_descx[i].next = VQ_RING_DESC_CHAIN_END;
355 }
356
357 /* Chain all the descriptors in the ring with an END */
358 static inline void
359 vring_desc_init_split(struct vring_desc *dp, uint16_t n)
360 {
361         uint16_t i;
362
363         for (i = 0; i < n - 1; i++)
364                 dp[i].next = (uint16_t)(i + 1);
365         dp[i].next = VQ_RING_DESC_CHAIN_END;
366 }
367
368 /**
369  * Tell the backend not to interrupt us. Implementation for packed virtqueues.
370  */
371 static inline void
372 virtqueue_disable_intr_packed(struct virtqueue *vq)
373 {
374         if (vq->vq_packed.event_flags_shadow != RING_EVENT_FLAGS_DISABLE) {
375                 vq->vq_packed.event_flags_shadow = RING_EVENT_FLAGS_DISABLE;
376                 vq->vq_packed.ring.driver->desc_event_flags =
377                         vq->vq_packed.event_flags_shadow;
378         }
379 }
380
381 /**
382  * Tell the backend not to interrupt us. Implementation for split virtqueues.
383  */
384 static inline void
385 virtqueue_disable_intr_split(struct virtqueue *vq)
386 {
387         vq->vq_split.ring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
388 }
389
390 /**
391  * Tell the backend not to interrupt us.
392  */
393 static inline void
394 virtqueue_disable_intr(struct virtqueue *vq)
395 {
396         if (vtpci_packed_queue(vq->hw))
397                 virtqueue_disable_intr_packed(vq);
398         else
399                 virtqueue_disable_intr_split(vq);
400 }
401
402 /**
403  * Tell the backend to interrupt. Implementation for packed virtqueues.
404  */
405 static inline void
406 virtqueue_enable_intr_packed(struct virtqueue *vq)
407 {
408         if (vq->vq_packed.event_flags_shadow == RING_EVENT_FLAGS_DISABLE) {
409                 vq->vq_packed.event_flags_shadow = RING_EVENT_FLAGS_ENABLE;
410                 vq->vq_packed.ring.driver->desc_event_flags =
411                         vq->vq_packed.event_flags_shadow;
412         }
413 }
414
415 /**
416  * Tell the backend to interrupt. Implementation for split virtqueues.
417  */
418 static inline void
419 virtqueue_enable_intr_split(struct virtqueue *vq)
420 {
421         vq->vq_split.ring.avail->flags &= (~VRING_AVAIL_F_NO_INTERRUPT);
422 }
423
424 /**
425  * Tell the backend to interrupt us.
426  */
427 static inline void
428 virtqueue_enable_intr(struct virtqueue *vq)
429 {
430         if (vtpci_packed_queue(vq->hw))
431                 virtqueue_enable_intr_packed(vq);
432         else
433                 virtqueue_enable_intr_split(vq);
434 }
435
436 /**
437  *  Dump virtqueue internal structures, for debug purpose only.
438  */
439 void virtqueue_dump(struct virtqueue *vq);
440 /**
441  *  Get all mbufs to be freed.
442  */
443 struct rte_mbuf *virtqueue_detach_unused(struct virtqueue *vq);
444
445 /* Flush the elements in the used ring. */
446 void virtqueue_rxvq_flush(struct virtqueue *vq);
447
448 int virtqueue_rxvq_reset_packed(struct virtqueue *vq);
449
450 int virtqueue_txvq_reset_packed(struct virtqueue *vq);
451
452 static inline int
453 virtqueue_full(const struct virtqueue *vq)
454 {
455         return vq->vq_free_cnt == 0;
456 }
457
458 static inline int
459 virtio_get_queue_type(struct virtio_hw *hw, uint16_t vtpci_queue_idx)
460 {
461         if (vtpci_queue_idx == hw->max_queue_pairs * 2)
462                 return VTNET_CQ;
463         else if (vtpci_queue_idx % 2 == 0)
464                 return VTNET_RQ;
465         else
466                 return VTNET_TQ;
467 }
468
469 #define VIRTQUEUE_NUSED(vq) ((uint16_t)((vq)->vq_split.ring.used->idx - \
470                                         (vq)->vq_used_cons_idx))
471
472 void vq_ring_free_chain(struct virtqueue *vq, uint16_t desc_idx);
473 void vq_ring_free_chain_packed(struct virtqueue *vq, uint16_t used_idx);
474 void vq_ring_free_inorder(struct virtqueue *vq, uint16_t desc_idx,
475                           uint16_t num);
476
477 static inline void
478 vq_update_avail_idx(struct virtqueue *vq)
479 {
480         virtio_wmb(vq->hw->weak_barriers);
481         vq->vq_split.ring.avail->idx = vq->vq_avail_idx;
482 }
483
484 static inline void
485 vq_update_avail_ring(struct virtqueue *vq, uint16_t desc_idx)
486 {
487         uint16_t avail_idx;
488         /*
489          * Place the head of the descriptor chain into the next slot and make
490          * it usable to the host. The chain is made available now rather than
491          * deferring to virtqueue_notify() in the hopes that if the host is
492          * currently running on another CPU, we can keep it processing the new
493          * descriptor.
494          */
495         avail_idx = (uint16_t)(vq->vq_avail_idx & (vq->vq_nentries - 1));
496         if (unlikely(vq->vq_split.ring.avail->ring[avail_idx] != desc_idx))
497                 vq->vq_split.ring.avail->ring[avail_idx] = desc_idx;
498         vq->vq_avail_idx++;
499 }
500
501 static inline int
502 virtqueue_kick_prepare(struct virtqueue *vq)
503 {
504         /*
505          * Ensure updated avail->idx is visible to vhost before reading
506          * the used->flags.
507          */
508         virtio_mb(vq->hw->weak_barriers);
509         return !(vq->vq_split.ring.used->flags & VRING_USED_F_NO_NOTIFY);
510 }
511
512 static inline int
513 virtqueue_kick_prepare_packed(struct virtqueue *vq)
514 {
515         uint16_t flags;
516
517         /*
518          * Ensure updated data is visible to vhost before reading the flags.
519          */
520         virtio_mb(vq->hw->weak_barriers);
521         flags = vq->vq_packed.ring.device->desc_event_flags;
522
523         return flags != RING_EVENT_FLAGS_DISABLE;
524 }
525
526 /*
527  * virtqueue_kick_prepare*() or the virtio_wmb() should be called
528  * before this function to be sure that all the data is visible to vhost.
529  */
530 static inline void
531 virtqueue_notify(struct virtqueue *vq)
532 {
533         VTPCI_OPS(vq->hw)->notify_queue(vq->hw, vq);
534 }
535
536 #ifdef RTE_LIBRTE_VIRTIO_DEBUG_DUMP
537 #define VIRTQUEUE_DUMP(vq) do { \
538         uint16_t used_idx, nused; \
539         used_idx = (vq)->vq_split.ring.used->idx; \
540         nused = (uint16_t)(used_idx - (vq)->vq_used_cons_idx); \
541         if (vtpci_packed_queue((vq)->hw)) { \
542                 PMD_INIT_LOG(DEBUG, \
543                 "VQ: - size=%d; free=%d; used_cons_idx=%d; avail_idx=%d;" \
544                 " cached_flags=0x%x; used_wrap_counter=%d", \
545                 (vq)->vq_nentries, (vq)->vq_free_cnt, (vq)->vq_used_cons_idx, \
546                 (vq)->vq_avail_idx, (vq)->vq_packed.cached_flags, \
547                 (vq)->vq_packed.used_wrap_counter); \
548                 break; \
549         } \
550         PMD_INIT_LOG(DEBUG, \
551           "VQ: - size=%d; free=%d; used=%d; desc_head_idx=%d;" \
552           " avail.idx=%d; used_cons_idx=%d; used.idx=%d;" \
553           " avail.flags=0x%x; used.flags=0x%x", \
554           (vq)->vq_nentries, (vq)->vq_free_cnt, nused, \
555           (vq)->vq_desc_head_idx, (vq)->vq_split.ring.avail->idx, \
556           (vq)->vq_used_cons_idx, (vq)->vq_split.ring.used->idx, \
557           (vq)->vq_split.ring.avail->flags, (vq)->vq_split.ring.used->flags); \
558 } while (0)
559 #else
560 #define VIRTQUEUE_DUMP(vq) do { } while (0)
561 #endif
562
563 #endif /* _VIRTQUEUE_H_ */