xref: /freebsd/sys/net/netmap_virt.h (revision ab40f58ccfe6c07ebefddc72f4661a52fe746353)
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 *csb_gh;		/* CSB for guest --> host communication */
89 	void *csb_hg;		/* CSB for host --> guest communication */
90 	/* Configuration entries are allocated right after the struct. */
91 };
92 
93 /* Configuration of a ptnetmap ring for QEMU. */
94 struct ptnetmap_cfgentry_qemu {
95 	uint32_t ioeventfd;	/* to intercept guest register access */
96 	uint32_t irqfd;		/* to inject guest interrupts */
97 };
98 
99 /* Configuration of a ptnetmap ring for bhyve. */
100 struct ptnetmap_cfgentry_bhyve {
101 	uint64_t wchan;		/* tsleep() parameter, to wake up kthread */
102 	uint32_t ioctl_fd;	/* ioctl fd */
103 	/* ioctl parameters to send irq */
104 	uint32_t ioctl_cmd;
105 	/* vmm.ko MSIX parameters for IOCTL */
106 	struct {
107 		uint64_t        msg_data;
108 		uint64_t        addr;
109 	} ioctl_data;
110 };
111 
112 /*
113  * Pass a pointer to a userspace buffer to be passed to kernelspace for write
114  * or read. Used by NETMAP_PT_HOST_CREATE.
115  * XXX deprecated
116  */
117 static inline void
118 nmreq_pointer_put(struct nmreq *nmr, void *userptr)
119 {
120 	uintptr_t *pp = (uintptr_t *)&nmr->nr_arg1;
121 	*pp = (uintptr_t)userptr;
122 }
123 
124 static inline void *
125 nmreq_pointer_get(const struct nmreq *nmr)
126 {
127 	const uintptr_t *pp = (const uintptr_t *)&nmr->nr_arg1;
128 	return (void *)*pp;
129 }
130 
131 /* ptnetmap features */
132 #define PTNETMAP_F_VNET_HDR        1
133 
134 /* I/O registers for the ptnet device. */
135 #define PTNET_IO_PTFEAT		0
136 #define PTNET_IO_PTCTL		4
137 #define PTNET_IO_MAC_LO		8
138 #define PTNET_IO_MAC_HI		12
139 #define PTNET_IO_CSBBAH		16 /* deprecated */
140 #define PTNET_IO_CSBBAL		20 /* deprecated */
141 #define PTNET_IO_NIFP_OFS	24
142 #define PTNET_IO_NUM_TX_RINGS	28
143 #define PTNET_IO_NUM_RX_RINGS	32
144 #define PTNET_IO_NUM_TX_SLOTS	36
145 #define PTNET_IO_NUM_RX_SLOTS	40
146 #define PTNET_IO_VNET_HDR_LEN	44
147 #define PTNET_IO_HOSTMEMID	48
148 #define PTNET_IO_CSB_GH_BAH     52
149 #define PTNET_IO_CSB_GH_BAL     56
150 #define PTNET_IO_CSB_HG_BAH     60
151 #define PTNET_IO_CSB_HG_BAL     64
152 #define PTNET_IO_END		68
153 #define PTNET_IO_KICK_BASE	128
154 #define PTNET_IO_MASK		0xff
155 
156 /* ptnetmap control commands (values for PTCTL register) */
157 #define PTNETMAP_PTCTL_CREATE		1
158 #define PTNETMAP_PTCTL_DELETE		2
159 
160 /* ptnetmap synchronization variables shared between guest and host */
161 struct ptnet_csb_gh {
162 	uint32_t head;		  /* GW+ HR+ the head of the guest netmap_ring */
163 	uint32_t cur;		  /* GW+ HR+ the cur of the guest netmap_ring */
164 	uint32_t guest_need_kick; /* GW+ HR+ host-->guest notification enable */
165 	uint32_t sync_flags;	  /* GW+ HR+ the flags of the guest [tx|rx]sync() */
166 	char pad[48];		  /* pad to a 64 bytes cacheline */
167 };
168 struct ptnet_csb_hg {
169 	uint32_t hwcur;		  /* GR+ HW+ the hwcur of the host netmap_kring */
170 	uint32_t hwtail;	  /* GR+ HW+ the hwtail of the host netmap_kring */
171 	uint32_t host_need_kick;  /* GR+ HW+ guest-->host notification enable */
172 	char pad[4+48];
173 };
174 
175 #ifdef WITH_PTNETMAP_GUEST
176 
177 /* ptnetmap_memdev routines used to talk with ptnetmap_memdev device driver */
178 struct ptnetmap_memdev;
179 int nm_os_pt_memdev_iomap(struct ptnetmap_memdev *, vm_paddr_t *, void **,
180                           uint64_t *);
181 void nm_os_pt_memdev_iounmap(struct ptnetmap_memdev *);
182 uint32_t nm_os_pt_memdev_ioread(struct ptnetmap_memdev *, unsigned int);
183 
184 /* Guest driver: Write kring pointers (cur, head) to the CSB.
185  * This routine is coupled with ptnetmap_host_read_kring_csb(). */
186 static inline void
187 ptnetmap_guest_write_kring_csb(struct ptnet_csb_gh *ptr, uint32_t cur,
188 			       uint32_t head)
189 {
190     /*
191      * We need to write cur and head to the CSB but we cannot do it atomically.
192      * There is no way we can prevent the host from reading the updated value
193      * of one of the two and the old value of the other. However, if we make
194      * sure that the host never reads a value of head more recent than the
195      * value of cur we are safe. We can allow the host to read a value of cur
196      * more recent than the value of head, since in the netmap ring cur can be
197      * ahead of head and cur cannot wrap around head because it must be behind
198      * tail. Inverting the order of writes below could instead result into the
199      * host to think head went ahead of cur, which would cause the sync
200      * prologue to fail.
201      *
202      * The following memory barrier scheme is used to make this happen:
203      *
204      *          Guest              Host
205      *
206      *          STORE(cur)         LOAD(head)
207      *          mb() <-----------> mb()
208      *          STORE(head)        LOAD(cur)
209      */
210     ptr->cur = cur;
211     mb();
212     ptr->head = head;
213 }
214 
215 /* Guest driver: Read kring pointers (hwcur, hwtail) from the CSB.
216  * This routine is coupled with ptnetmap_host_write_kring_csb(). */
217 static inline void
218 ptnetmap_guest_read_kring_csb(struct ptnet_csb_hg *pthg, struct netmap_kring *kring)
219 {
220     /*
221      * We place a memory barrier to make sure that the update of hwtail never
222      * overtakes the update of hwcur.
223      * (see explanation in ptnetmap_host_write_kring_csb).
224      */
225     kring->nr_hwtail = pthg->hwtail;
226     mb();
227     kring->nr_hwcur = pthg->hwcur;
228 }
229 
230 #endif /* WITH_PTNETMAP_GUEST */
231 
232 #ifdef WITH_PTNETMAP_HOST
233 /*
234  * ptnetmap kernel thread routines
235  * */
236 
237 /* Functions to read and write CSB fields in the host */
238 #if defined (linux)
239 #define CSB_READ(csb, field, r) (get_user(r, &csb->field))
240 #define CSB_WRITE(csb, field, v) (put_user(v, &csb->field))
241 #else  /* ! linux */
242 #define CSB_READ(csb, field, r) (r = fuword32(&csb->field))
243 #define CSB_WRITE(csb, field, v) (suword32(&csb->field, v))
244 #endif /* ! linux */
245 
246 /* Host netmap: Write kring pointers (hwcur, hwtail) to the CSB.
247  * This routine is coupled with ptnetmap_guest_read_kring_csb(). */
248 static inline void
249 ptnetmap_host_write_kring_csb(struct ptnet_csb_hg __user *ptr, uint32_t hwcur,
250         uint32_t hwtail)
251 {
252     /*
253      * The same scheme used in ptnetmap_guest_write_kring_csb() applies here.
254      * We allow the guest to read a value of hwcur more recent than the value
255      * of hwtail, since this would anyway result in a consistent view of the
256      * ring state (and hwcur can never wraparound hwtail, since hwcur must be
257      * behind head).
258      *
259      * The following memory barrier scheme is used to make this happen:
260      *
261      *          Guest                Host
262      *
263      *          STORE(hwcur)         LOAD(hwtail)
264      *          mb() <-------------> mb()
265      *          STORE(hwtail)        LOAD(hwcur)
266      */
267     CSB_WRITE(ptr, hwcur, hwcur);
268     mb();
269     CSB_WRITE(ptr, hwtail, hwtail);
270 }
271 
272 /* Host netmap: Read kring pointers (head, cur, sync_flags) from the CSB.
273  * This routine is coupled with ptnetmap_guest_write_kring_csb(). */
274 static inline void
275 ptnetmap_host_read_kring_csb(struct ptnet_csb_gh __user *ptr,
276 			     struct netmap_ring *shadow_ring,
277 			     uint32_t num_slots)
278 {
279     /*
280      * We place a memory barrier to make sure that the update of head never
281      * overtakes the update of cur.
282      * (see explanation in ptnetmap_guest_write_kring_csb).
283      */
284     CSB_READ(ptr, head, shadow_ring->head);
285     mb();
286     CSB_READ(ptr, cur, shadow_ring->cur);
287     CSB_READ(ptr, sync_flags, shadow_ring->flags);
288 }
289 
290 #endif /* WITH_PTNETMAP_HOST */
291 
292 #endif /* NETMAP_VIRT_H */
293