]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net/netmap_virt.h
Import tzdata 2018d
[FreeBSD/FreeBSD.git] / sys / net / netmap_virt.h
1 /*
2  * Copyright (C) 2013-2016 Luigi Rizzo
3  * Copyright (C) 2013-2016 Giuseppe Lettieri
4  * Copyright (C) 2013-2016 Vincenzo Maffione
5  * Copyright (C) 2015 Stefano Garzarella
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *   1. Redistributions of source code must retain the above copyright
12  *      notice, this list of conditions and the following disclaimer.
13  *   2. Redistributions in binary form must reproduce the above copyright
14  *      notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31
32 #ifndef NETMAP_VIRT_H
33 #define NETMAP_VIRT_H
34
35 /*
36  * ptnetmap_memdev: device used to expose memory into the guest VM
37  *
38  * These macros are used in the hypervisor frontend (QEMU, bhyve) and in the
39  * guest device driver.
40  */
41
42 /* PCI identifiers and PCI BARs for the ptnetmap memdev
43  * and ptnetmap network interface. */
44 #define PTNETMAP_MEMDEV_NAME            "ptnetmap-memdev"
45 #define PTNETMAP_PCI_VENDOR_ID          0x1b36  /* QEMU virtual devices */
46 #define PTNETMAP_PCI_DEVICE_ID          0x000c  /* memory device */
47 #define PTNETMAP_PCI_NETIF_ID           0x000d  /* ptnet network interface */
48 #define PTNETMAP_IO_PCI_BAR             0
49 #define PTNETMAP_MEM_PCI_BAR            1
50 #define PTNETMAP_MSIX_PCI_BAR           2
51
52 /* Registers for the ptnetmap memdev */
53 #define PTNET_MDEV_IO_MEMSIZE_LO        0       /* netmap memory size (low) */
54 #define PTNET_MDEV_IO_MEMSIZE_HI        4       /* netmap_memory_size (high) */
55 #define PTNET_MDEV_IO_MEMID             8       /* memory allocator ID in the host */
56 #define PTNET_MDEV_IO_IF_POOL_OFS       64
57 #define PTNET_MDEV_IO_IF_POOL_OBJNUM    68
58 #define PTNET_MDEV_IO_IF_POOL_OBJSZ     72
59 #define PTNET_MDEV_IO_RING_POOL_OFS     76
60 #define PTNET_MDEV_IO_RING_POOL_OBJNUM  80
61 #define PTNET_MDEV_IO_RING_POOL_OBJSZ   84
62 #define PTNET_MDEV_IO_BUF_POOL_OFS      88
63 #define PTNET_MDEV_IO_BUF_POOL_OBJNUM   92
64 #define PTNET_MDEV_IO_BUF_POOL_OBJSZ    96
65 #define PTNET_MDEV_IO_END               100
66
67 /*
68  * ptnetmap configuration
69  *
70  * The ptnet kthreads (running in host kernel-space) need to be configured
71  * in order to know how to intercept guest kicks (I/O register writes) and
72  * how to inject MSI-X interrupts to the guest. The configuration may vary
73  * depending on the hypervisor. Currently, we support QEMU/KVM on Linux and
74  * and bhyve on FreeBSD.
75  * The configuration is passed by the hypervisor to the host netmap module
76  * by means of an ioctl() with nr_cmd=NETMAP_PT_HOST_CREATE, and it is
77  * specified by the ptnetmap_cfg struct. This struct contains an header
78  * with general informations and an array of entries whose size depends
79  * on the hypervisor. The NETMAP_PT_HOST_CREATE command is issued every
80  * time the kthreads are started.
81  */
82 struct ptnetmap_cfg {
83 #define PTNETMAP_CFGTYPE_QEMU           0x1
84 #define PTNETMAP_CFGTYPE_BHYVE          0x2
85         uint16_t cfgtype;       /* how to interpret the cfg entries */
86         uint16_t entry_size;    /* size of a config entry */
87         uint32_t num_rings;     /* number of config entries */
88         void *ptrings;          /* ptrings inside CSB */
89         /* Configuration entries are allocated right after the struct. */
90 };
91
92 /* Configuration of a ptnetmap ring for QEMU. */
93 struct ptnetmap_cfgentry_qemu {
94         uint32_t ioeventfd;     /* to intercept guest register access */
95         uint32_t irqfd;         /* to inject guest interrupts */
96 };
97
98 /* Configuration of a ptnetmap ring for bhyve. */
99 struct ptnetmap_cfgentry_bhyve {
100         uint64_t wchan;         /* tsleep() parameter, to wake up kthread */
101         uint32_t ioctl_fd;      /* ioctl fd */
102         /* ioctl parameters to send irq */
103         uint32_t ioctl_cmd;
104         /* vmm.ko MSIX parameters for IOCTL */
105         struct {
106                 uint64_t        msg_data;
107                 uint64_t        addr;
108         } ioctl_data;
109 };
110
111 /*
112  * Structure filled-in by the kernel when asked for allocator info
113  * through NETMAP_POOLS_INFO_GET. Used by hypervisors supporting
114  * ptnetmap.
115  */
116 struct netmap_pools_info {
117         uint64_t memsize;       /* same as nmr->nr_memsize */
118         uint32_t memid;         /* same as nmr->nr_arg2 */
119         uint32_t if_pool_offset;
120         uint32_t if_pool_objtotal;
121         uint32_t if_pool_objsize;
122         uint32_t ring_pool_offset;
123         uint32_t ring_pool_objtotal;
124         uint32_t ring_pool_objsize;
125         uint32_t buf_pool_offset;
126         uint32_t buf_pool_objtotal;
127         uint32_t buf_pool_objsize;
128 };
129
130 /*
131  * Pass a pointer to a userspace buffer to be passed to kernelspace for write
132  * or read. Used by NETMAP_PT_HOST_CREATE and NETMAP_POOLS_INFO_GET.
133  */
134 static inline void
135 nmreq_pointer_put(struct nmreq *nmr, void *userptr)
136 {
137         uintptr_t *pp = (uintptr_t *)&nmr->nr_arg1;
138         *pp = (uintptr_t)userptr;
139 }
140
141 /* ptnetmap features */
142 #define PTNETMAP_F_VNET_HDR        1
143
144 /* I/O registers for the ptnet device. */
145 #define PTNET_IO_PTFEAT         0
146 #define PTNET_IO_PTCTL          4
147 #define PTNET_IO_MAC_LO         8
148 #define PTNET_IO_MAC_HI         12
149 #define PTNET_IO_CSBBAH         16
150 #define PTNET_IO_CSBBAL         20
151 #define PTNET_IO_NIFP_OFS       24
152 #define PTNET_IO_NUM_TX_RINGS   28
153 #define PTNET_IO_NUM_RX_RINGS   32
154 #define PTNET_IO_NUM_TX_SLOTS   36
155 #define PTNET_IO_NUM_RX_SLOTS   40
156 #define PTNET_IO_VNET_HDR_LEN   44
157 #define PTNET_IO_HOSTMEMID      48
158 #define PTNET_IO_END            52
159 #define PTNET_IO_KICK_BASE      128
160 #define PTNET_IO_MASK           0xff
161
162 /* ptnetmap control commands (values for PTCTL register) */
163 #define PTNETMAP_PTCTL_CREATE           1
164 #define PTNETMAP_PTCTL_DELETE           2
165
166 /* If defined, CSB is allocated by the guest, not by the host. */
167 #define PTNET_CSB_ALLOC
168
169 /* ptnetmap ring fields shared between guest and host */
170 struct ptnet_ring {
171         /* XXX revise the layout to minimize cache bounces. */
172         uint32_t head;            /* GW+ HR+ the head of the guest netmap_ring */
173         uint32_t cur;             /* GW+ HR+ the cur of the guest netmap_ring */
174         uint32_t guest_need_kick; /* GW+ HR+ host-->guest notification enable */
175         uint32_t sync_flags;      /* GW+ HR+ the flags of the guest [tx|rx]sync() */
176         uint32_t hwcur;           /* GR+ HW+ the hwcur of the host netmap_kring */
177         uint32_t hwtail;          /* GR+ HW+ the hwtail of the host netmap_kring */
178         uint32_t host_need_kick;  /* GR+ HW+ guest-->host notification enable */
179         char pad[4];
180 };
181
182 /* CSB for the ptnet device. */
183 struct ptnet_csb {
184 #define NETMAP_VIRT_CSB_SIZE   4096
185         struct ptnet_ring rings[NETMAP_VIRT_CSB_SIZE/sizeof(struct ptnet_ring)];
186 };
187
188 #ifdef WITH_PTNETMAP_GUEST
189
190 /* ptnetmap_memdev routines used to talk with ptnetmap_memdev device driver */
191 struct ptnetmap_memdev;
192 int nm_os_pt_memdev_iomap(struct ptnetmap_memdev *, vm_paddr_t *, void **,
193                           uint64_t *);
194 void nm_os_pt_memdev_iounmap(struct ptnetmap_memdev *);
195 uint32_t nm_os_pt_memdev_ioread(struct ptnetmap_memdev *, unsigned int);
196
197 /* Guest driver: Write kring pointers (cur, head) to the CSB.
198  * This routine is coupled with ptnetmap_host_read_kring_csb(). */
199 static inline void
200 ptnetmap_guest_write_kring_csb(struct ptnet_ring *ptr, uint32_t cur,
201                                uint32_t head)
202 {
203     /*
204      * We need to write cur and head to the CSB but we cannot do it atomically.
205      * There is no way we can prevent the host from reading the updated value
206      * of one of the two and the old value of the other. However, if we make
207      * sure that the host never reads a value of head more recent than the
208      * value of cur we are safe. We can allow the host to read a value of cur
209      * more recent than the value of head, since in the netmap ring cur can be
210      * ahead of head and cur cannot wrap around head because it must be behind
211      * tail. Inverting the order of writes below could instead result into the
212      * host to think head went ahead of cur, which would cause the sync
213      * prologue to fail.
214      *
215      * The following memory barrier scheme is used to make this happen:
216      *
217      *          Guest              Host
218      *
219      *          STORE(cur)         LOAD(head)
220      *          mb() <-----------> mb()
221      *          STORE(head)        LOAD(cur)
222      */
223     ptr->cur = cur;
224     mb();
225     ptr->head = head;
226 }
227
228 /* Guest driver: Read kring pointers (hwcur, hwtail) from the CSB.
229  * This routine is coupled with ptnetmap_host_write_kring_csb(). */
230 static inline void
231 ptnetmap_guest_read_kring_csb(struct ptnet_ring *ptr, struct netmap_kring *kring)
232 {
233     /*
234      * We place a memory barrier to make sure that the update of hwtail never
235      * overtakes the update of hwcur.
236      * (see explanation in ptnetmap_host_write_kring_csb).
237      */
238     kring->nr_hwtail = ptr->hwtail;
239     mb();
240     kring->nr_hwcur = ptr->hwcur;
241 }
242
243 #endif /* WITH_PTNETMAP_GUEST */
244
245 #ifdef WITH_PTNETMAP_HOST
246 /*
247  * ptnetmap kernel thread routines
248  * */
249
250 /* Functions to read and write CSB fields in the host */
251 #if defined (linux)
252 #define CSB_READ(csb, field, r) (get_user(r, &csb->field))
253 #define CSB_WRITE(csb, field, v) (put_user(v, &csb->field))
254 #else  /* ! linux */
255 #define CSB_READ(csb, field, r) (r = fuword32(&csb->field))
256 #define CSB_WRITE(csb, field, v) (suword32(&csb->field, v))
257 #endif /* ! linux */
258
259 /* Host netmap: Write kring pointers (hwcur, hwtail) to the CSB.
260  * This routine is coupled with ptnetmap_guest_read_kring_csb(). */
261 static inline void
262 ptnetmap_host_write_kring_csb(struct ptnet_ring __user *ptr, uint32_t hwcur,
263         uint32_t hwtail)
264 {
265     /*
266      * The same scheme used in ptnetmap_guest_write_kring_csb() applies here.
267      * We allow the guest to read a value of hwcur more recent than the value
268      * of hwtail, since this would anyway result in a consistent view of the
269      * ring state (and hwcur can never wraparound hwtail, since hwcur must be
270      * behind head).
271      *
272      * The following memory barrier scheme is used to make this happen:
273      *
274      *          Guest                Host
275      *
276      *          STORE(hwcur)         LOAD(hwtail)
277      *          mb() <-------------> mb()
278      *          STORE(hwtail)        LOAD(hwcur)
279      */
280     CSB_WRITE(ptr, hwcur, hwcur);
281     mb();
282     CSB_WRITE(ptr, hwtail, hwtail);
283 }
284
285 /* Host netmap: Read kring pointers (head, cur, sync_flags) from the CSB.
286  * This routine is coupled with ptnetmap_guest_write_kring_csb(). */
287 static inline void
288 ptnetmap_host_read_kring_csb(struct ptnet_ring __user *ptr,
289                              struct netmap_ring *shadow_ring,
290                              uint32_t num_slots)
291 {
292     /*
293      * We place a memory barrier to make sure that the update of head never
294      * overtakes the update of cur.
295      * (see explanation in ptnetmap_guest_write_kring_csb).
296      */
297     CSB_READ(ptr, head, shadow_ring->head);
298     mb();
299     CSB_READ(ptr, cur, shadow_ring->cur);
300     CSB_READ(ptr, sync_flags, shadow_ring->flags);
301 }
302
303 #endif /* WITH_PTNETMAP_HOST */
304
305 #endif /* NETMAP_VIRT_H */