mbuf: replace data pointer by an offset
[dpdk.git] / lib / librte_pmd_virtio / virtqueue.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #ifndef _VIRTQUEUE_H_
35 #define _VIRTQUEUE_H_
36
37 #include <stdint.h>
38
39 #include <rte_atomic.h>
40 #include <rte_mbuf.h>
41 #include <rte_memory.h>
42 #include <rte_memzone.h>
43 #include <rte_mempool.h>
44
45 #include "virtio_pci.h"
46 #include "virtio_ring.h"
47 #include "virtio_logs.h"
48
49 #define mb()  rte_mb()
50 #define wmb() rte_wmb()
51 #define rmb() rte_rmb()
52
53 #ifdef RTE_PMD_PACKET_PREFETCH
54 #define rte_packet_prefetch(p)  rte_prefetch1(p)
55 #else
56 #define rte_packet_prefetch(p)  do {} while(0)
57 #endif
58
59 #define VIRTQUEUE_MAX_NAME_SZ 32
60
61 #define RTE_MBUF_DATA_DMA_ADDR(mb) \
62         (uint64_t) ((mb)->buf_physaddr + (mb)->data_off)
63
64 #define VTNET_SQ_RQ_QUEUE_IDX 0
65 #define VTNET_SQ_TQ_QUEUE_IDX 1
66 #define VTNET_SQ_CQ_QUEUE_IDX 2
67
68 enum { VTNET_RQ = 0, VTNET_TQ = 1, VTNET_CQ = 2 };
69 /**
70  * The maximum virtqueue size is 2^15. Use that value as the end of
71  * descriptor chain terminator since it will never be a valid index
72  * in the descriptor table. This is used to verify we are correctly
73  * handling vq_free_cnt.
74  */
75 #define VQ_RING_DESC_CHAIN_END 32768
76
77 /**
78  * Control the RX mode, ie. promiscuous, allmulti, etc...
79  * All commands require an "out" sg entry containing a 1 byte
80  * state value, zero = disable, non-zero = enable.  Commands
81  * 0 and 1 are supported with the VIRTIO_NET_F_CTRL_RX feature.
82  * Commands 2-5 are added with VIRTIO_NET_F_CTRL_RX_EXTRA.
83  */
84 #define VIRTIO_NET_CTRL_RX              0
85 #define VIRTIO_NET_CTRL_RX_PROMISC      0
86 #define VIRTIO_NET_CTRL_RX_ALLMULTI     1
87 #define VIRTIO_NET_CTRL_RX_ALLUNI       2
88 #define VIRTIO_NET_CTRL_RX_NOMULTI      3
89 #define VIRTIO_NET_CTRL_RX_NOUNI        4
90 #define VIRTIO_NET_CTRL_RX_NOBCAST      5
91
92 /**
93  * Control VLAN filtering
94  *
95  * The VLAN filter table is controlled via a simple ADD/DEL interface.
96  * VLAN IDs not added may be filtered by the hypervisor.  Del is the
97  * opposite of add.  Both commands expect an out entry containing a 2
98  * byte VLAN ID.  VLAN filtering is available with the
99  * VIRTIO_NET_F_CTRL_VLAN feature bit.
100  */
101 #define VIRTIO_NET_CTRL_VLAN     2
102 #define VIRTIO_NET_CTRL_VLAN_ADD 0
103 #define VIRTIO_NET_CTRL_VLAN_DEL 1
104
105 struct virtio_net_ctrl_hdr {
106         uint8_t class;
107         uint8_t cmd;
108 } __attribute__((packed));
109
110 typedef uint8_t virtio_net_ctrl_ack;
111
112 #define VIRTIO_NET_OK     0
113 #define VIRTIO_NET_ERR    1
114
115 #define VIRTIO_MAX_CTRL_DATA 128
116
117 struct virtio_pmd_ctrl {
118         struct virtio_net_ctrl_hdr hdr;
119         virtio_net_ctrl_ack status;
120         uint8_t data[VIRTIO_MAX_CTRL_DATA];
121 };
122
123 struct virtqueue {
124         struct virtio_hw         *hw;     /**< virtio_hw structure pointer. */
125         const struct rte_memzone *mz;     /**< mem zone to populate RX ring. */
126         const struct rte_memzone *virtio_net_hdr_mz; /**< memzone to populate hdr. */
127         struct rte_mempool       *mpool;  /**< mempool for mbuf allocation */
128         uint16_t    queue_id;             /**< DPDK queue index. */
129         uint8_t     port_id;              /**< Device port identifier. */
130
131         void        *vq_ring_virt_mem;    /**< linear address of vring*/
132         int         vq_alignment;
133         int         vq_ring_size;
134         phys_addr_t vq_ring_mem;          /**< physical address of vring */
135
136         struct vring vq_ring;    /**< vring keeping desc, used and avail */
137         uint16_t    vq_free_cnt; /**< num of desc available */
138         uint16_t    vq_nentries; /**< vring desc numbers */
139         uint16_t    vq_queue_index;       /**< PCI queue index */
140         /**
141          * Head of the free chain in the descriptor table. If
142          * there are no free descriptors, this will be set to
143          * VQ_RING_DESC_CHAIN_END.
144          */
145         uint16_t  vq_desc_head_idx;
146         uint16_t  vq_desc_tail_idx;
147         /**
148          * Last consumed descriptor in the used table,
149          * trails vq_ring.used->idx.
150          */
151         uint16_t vq_used_cons_idx;
152         uint16_t vq_avail_idx;
153         phys_addr_t virtio_net_hdr_mem; /**< hdr for each xmit packet */
154
155         /* Statistics */
156         uint64_t        packets;
157         uint64_t        bytes;
158         uint64_t        errors;
159
160         struct vq_desc_extra {
161                 void              *cookie;
162                 uint16_t          ndescs;
163         } vq_descx[0];
164 };
165
166 /* If multiqueue is provided by host, then we suppport it. */
167 #ifndef VIRTIO_NET_F_MQ
168 /* Device supports Receive Flow Steering */
169 #define VIRTIO_NET_F_MQ 0x400000
170 #define VIRTIO_NET_CTRL_MQ   4
171 #define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET        0
172 #define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN        1
173 #define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX        0x8000
174 #endif
175
176 /**
177  * This is the first element of the scatter-gather list.  If you don't
178  * specify GSO or CSUM features, you can simply ignore the header.
179  */
180 struct virtio_net_hdr {
181 #define VIRTIO_NET_HDR_F_NEEDS_CSUM 1    /**< Use csum_start,csum_offset*/
182         uint8_t flags;
183 #define VIRTIO_NET_HDR_GSO_NONE     0    /**< Not a GSO frame */
184 #define VIRTIO_NET_HDR_GSO_TCPV4    1    /**< GSO frame, IPv4 TCP (TSO) */
185 #define VIRTIO_NET_HDR_GSO_UDP      3    /**< GSO frame, IPv4 UDP (UFO) */
186 #define VIRTIO_NET_HDR_GSO_TCPV6    4    /**< GSO frame, IPv6 TCP */
187 #define VIRTIO_NET_HDR_GSO_ECN      0x80 /**< TCP has ECN set */
188         uint8_t gso_type;
189         uint16_t hdr_len;     /**< Ethernet + IP + tcp/udp hdrs */
190         uint16_t gso_size;    /**< Bytes to append to hdr_len per frame */
191         uint16_t csum_start;  /**< Position to start checksumming from */
192         uint16_t csum_offset; /**< Offset after that to place checksum */
193 };
194
195 /**
196  * This is the version of the header to use when the MRG_RXBUF
197  * feature has been negotiated.
198  */
199 struct virtio_net_hdr_mrg_rxbuf {
200         struct   virtio_net_hdr hdr;
201         uint16_t num_buffers; /**< Number of merged rx buffers */
202 };
203
204 /**
205  * Tell the backend not to interrupt us.
206  */
207 void virtqueue_disable_intr(struct virtqueue *vq);
208 /**
209  *  Dump virtqueue internal structures, for debug purpose only.
210  */
211 void virtqueue_dump(struct virtqueue *vq);
212 /**
213  *  Get all mbufs to be freed.
214  */
215 struct rte_mbuf *virtqueue_detatch_unused(struct virtqueue *vq);
216
217 static inline int
218 virtqueue_full(const struct virtqueue *vq)
219 {
220         return vq->vq_free_cnt == 0;
221 }
222
223 #define VIRTQUEUE_NUSED(vq) ((uint16_t)((vq)->vq_ring.used->idx - (vq)->vq_used_cons_idx))
224
225 static inline void
226 vq_update_avail_idx(struct virtqueue *vq)
227 {
228         rte_compiler_barrier();
229         vq->vq_ring.avail->idx = vq->vq_avail_idx;
230 }
231
232 static inline void
233 vq_update_avail_ring(struct virtqueue *vq, uint16_t desc_idx)
234 {
235         uint16_t avail_idx;
236         /*
237          * Place the head of the descriptor chain into the next slot and make
238          * it usable to the host. The chain is made available now rather than
239          * deferring to virtqueue_notify() in the hopes that if the host is
240          * currently running on another CPU, we can keep it processing the new
241          * descriptor.
242          */
243         avail_idx = (uint16_t)(vq->vq_avail_idx & (vq->vq_nentries - 1));
244         vq->vq_ring.avail->ring[avail_idx] = desc_idx;
245         vq->vq_avail_idx++;
246 }
247
248 static inline int
249 virtqueue_kick_prepare(struct virtqueue *vq)
250 {
251         return !(vq->vq_ring.used->flags & VRING_USED_F_NO_NOTIFY);
252 }
253
254 static inline void
255 virtqueue_notify(struct virtqueue *vq)
256 {
257         /*
258          * Ensure updated avail->idx is visible to host. mb() necessary?
259          * For virtio on IA, the notificaiton is through io port operation
260          * which is a serialization instruction itself.
261          */
262         VIRTIO_WRITE_REG_2(vq->hw, VIRTIO_PCI_QUEUE_NOTIFY, vq->vq_queue_index);
263 }
264
265 #ifdef RTE_LIBRTE_VIRTIO_DEBUG_DUMP
266 #define VIRTQUEUE_DUMP(vq) do { \
267         uint16_t used_idx, nused; \
268         used_idx = (vq)->vq_ring.used->idx; \
269         nused = (uint16_t)(used_idx - (vq)->vq_used_cons_idx); \
270         PMD_INIT_LOG(DEBUG, \
271           "VQ: - size=%d; free=%d; used=%d; desc_head_idx=%d;" \
272           " avail.idx=%d; used_cons_idx=%d; used.idx=%d;" \
273           " avail.flags=0x%x; used.flags=0x%x", \
274           (vq)->vq_nentries, (vq)->vq_free_cnt, nused, \
275           (vq)->vq_desc_head_idx, (vq)->vq_ring.avail->idx, \
276           (vq)->vq_used_cons_idx, (vq)->vq_ring.used->idx, \
277           (vq)->vq_ring.avail->flags, (vq)->vq_ring.used->flags); \
278 } while (0)
279 #else
280 #define VIRTQUEUE_DUMP(vq) do { } while (0)
281 #endif
282
283 #endif /* _VIRTQUEUE_H_ */