xref: /freebsd/sys/net/netmap_virt.h (revision def7fe87e9b28032572ca6f820a260677fd0c2d5)
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 #define NETMAP_VIRT_CSB_SIZE   4096
36 
37 /* ptnetmap features */
38 #define PTNETMAP_F_BASE            1
39 #define PTNETMAP_F_FULL            2 /* not used */
40 #define PTNETMAP_F_VNET_HDR        4
41 
42 /*
43  * ptnetmap_memdev: device used to expose memory into the guest VM
44  *
45  * These macros are used in the hypervisor frontend (QEMU, bhyve) and in the
46  * guest device driver.
47  */
48 
49 /* PCI identifiers and PCI BARs for the ptnetmap memdev
50  * and ptnetmap network interface. */
51 #define PTNETMAP_MEMDEV_NAME            "ptnetmap-memdev"
52 #define PTNETMAP_PCI_VENDOR_ID          0x3333  /* TODO change vendor_id */
53 #define PTNETMAP_PCI_DEVICE_ID          0x0001  /* memory device */
54 #define PTNETMAP_PCI_NETIF_ID           0x0002  /* ptnet network interface */
55 #define PTNETMAP_IO_PCI_BAR             0
56 #define PTNETMAP_MEM_PCI_BAR            1
57 #define PTNETMAP_MSIX_PCI_BAR           2
58 
59 /* Registers for the ptnetmap memdev */
60 /* 32 bit r/o */
61 #define PTNETMAP_IO_PCI_MEMSIZE         0	/* size of the netmap memory shared
62 						 * between guest and host */
63 /* 16 bit r/o */
64 #define PTNETMAP_IO_PCI_HOSTID          4	/* memory allocator ID in netmap host */
65 #define PTNETMAP_IO_SIZE                6
66 
67 /*
68  * ptnetmap configuration
69  *
70  * The hypervisor (QEMU or bhyve) sends this struct to the host netmap
71  * module through an ioctl() command when it wants to start the ptnetmap
72  * kthreads.
73  */
74 struct ptnetmap_cfg {
75 #define PTNETMAP_CFG_FEAT_CSB           0x0001
76 #define PTNETMAP_CFG_FEAT_EVENTFD       0x0002
77 #define PTNETMAP_CFG_FEAT_IOCTL		0x0004
78 	uint32_t features;
79 	void *ptrings;				/* ptrings inside CSB */
80 	uint32_t num_rings;			/* number of entries */
81 	struct ptnet_ring_cfg entries[0];	/* per-ptring configuration */
82 };
83 
84 /*
85  * Functions used to write ptnetmap_cfg from/to the nmreq.
86  * The user-space application writes the pointer of ptnetmap_cfg
87  * (user-space buffer) starting from nr_arg1 field, so that the kernel
88  * can read it with copyin (copy_from_user).
89  */
90 static inline void
91 ptnetmap_write_cfg(struct nmreq *nmr, struct ptnetmap_cfg *cfg)
92 {
93 	uintptr_t *nmr_ptncfg = (uintptr_t *)&nmr->nr_arg1;
94 	*nmr_ptncfg = (uintptr_t)cfg;
95 }
96 
97 /* ptnetmap control commands */
98 #define PTNETMAP_PTCTL_CONFIG	1
99 #define PTNETMAP_PTCTL_FINALIZE	2
100 #define PTNETMAP_PTCTL_IFNEW	3
101 #define PTNETMAP_PTCTL_IFDELETE	4
102 #define PTNETMAP_PTCTL_RINGSCREATE	5
103 #define PTNETMAP_PTCTL_RINGSDELETE	6
104 #define PTNETMAP_PTCTL_DEREF	7
105 #define PTNETMAP_PTCTL_TXSYNC	8
106 #define PTNETMAP_PTCTL_RXSYNC	9
107 #define PTNETMAP_PTCTL_REGIF        10
108 #define PTNETMAP_PTCTL_UNREGIF      11
109 #define PTNETMAP_PTCTL_HOSTMEMID	12
110 
111 
112 /* I/O registers for the ptnet device. */
113 #define PTNET_IO_PTFEAT		0
114 #define PTNET_IO_PTCTL		4
115 #define PTNET_IO_PTSTS		8
116 #define PTNET_IO_MAC_LO		12
117 #define PTNET_IO_MAC_HI		16
118 #define PTNET_IO_CSBBAH         20
119 #define PTNET_IO_CSBBAL         24
120 #define PTNET_IO_NIFP_OFS	28
121 #define PTNET_IO_NUM_TX_RINGS	32
122 #define PTNET_IO_NUM_RX_RINGS	36
123 #define PTNET_IO_NUM_TX_SLOTS	40
124 #define PTNET_IO_NUM_RX_SLOTS	44
125 #define PTNET_IO_VNET_HDR_LEN	48
126 #define PTNET_IO_END		52
127 #define PTNET_IO_KICK_BASE	128
128 #define PTNET_IO_MASK           0xff
129 
130 /* If defined, CSB is allocated by the guest, not by the host. */
131 #define PTNET_CSB_ALLOC
132 
133 /* ptnetmap ring fields shared between guest and host */
134 struct ptnet_ring {
135 	/* XXX revise the layout to minimize cache bounces. */
136 	uint32_t head;		  /* GW+ HR+ the head of the guest netmap_ring */
137 	uint32_t cur;		  /* GW+ HR+ the cur of the guest netmap_ring */
138 	uint32_t guest_need_kick; /* GW+ HR+ host-->guest notification enable */
139 	uint32_t sync_flags;	  /* GW+ HR+ the flags of the guest [tx|rx]sync() */
140 	uint32_t hwcur;		  /* GR+ HW+ the hwcur of the host netmap_kring */
141 	uint32_t hwtail;	  /* GR+ HW+ the hwtail of the host netmap_kring */
142 	uint32_t host_need_kick;  /* GR+ HW+ guest-->host notification enable */
143 	char pad[4];
144 };
145 
146 /* CSB for the ptnet device. */
147 struct ptnet_csb {
148 	struct ptnet_ring rings[NETMAP_VIRT_CSB_SIZE/sizeof(struct ptnet_ring)];
149 };
150 
151 #if defined (WITH_PTNETMAP_HOST) || defined (WITH_PTNETMAP_GUEST)
152 
153 /* return l_elem - r_elem with wraparound */
154 static inline uint32_t
155 ptn_sub(uint32_t l_elem, uint32_t r_elem, uint32_t num_slots)
156 {
157     int64_t res;
158 
159     res = (int64_t)(l_elem) - r_elem;
160 
161     return (res < 0) ? res + num_slots : res;
162 }
163 #endif /* WITH_PTNETMAP_HOST || WITH_PTNETMAP_GUEST */
164 
165 #ifdef WITH_PTNETMAP_GUEST
166 
167 /* ptnetmap_memdev routines used to talk with ptnetmap_memdev device driver */
168 struct ptnetmap_memdev;
169 int nm_os_pt_memdev_iomap(struct ptnetmap_memdev *, vm_paddr_t *, void **);
170 void nm_os_pt_memdev_iounmap(struct ptnetmap_memdev *);
171 
172 /* Guest driver: Write kring pointers (cur, head) to the CSB.
173  * This routine is coupled with ptnetmap_host_read_kring_csb(). */
174 static inline void
175 ptnetmap_guest_write_kring_csb(struct ptnet_ring *ptr, uint32_t cur,
176 			       uint32_t head)
177 {
178     /*
179      * We need to write cur and head to the CSB but we cannot do it atomically.
180      * There is no way we can prevent the host from reading the updated value
181      * of one of the two and the old value of the other. However, if we make
182      * sure that the host never reads a value of head more recent than the
183      * value of cur we are safe. We can allow the host to read a value of cur
184      * more recent than the value of head, since in the netmap ring cur can be
185      * ahead of head and cur cannot wrap around head because it must be behind
186      * tail. Inverting the order of writes below could instead result into the
187      * host to think head went ahead of cur, which would cause the sync
188      * prologue to fail.
189      *
190      * The following memory barrier scheme is used to make this happen:
191      *
192      *          Guest              Host
193      *
194      *          STORE(cur)         LOAD(head)
195      *          mb() <-----------> mb()
196      *          STORE(head)        LOAD(cur)
197      */
198     ptr->cur = cur;
199     mb();
200     ptr->head = head;
201 }
202 
203 /* Guest driver: Read kring pointers (hwcur, hwtail) from the CSB.
204  * This routine is coupled with ptnetmap_host_write_kring_csb(). */
205 static inline void
206 ptnetmap_guest_read_kring_csb(struct ptnet_ring *ptr, struct netmap_kring *kring)
207 {
208     /*
209      * We place a memory barrier to make sure that the update of hwtail never
210      * overtakes the update of hwcur.
211      * (see explanation in ptnetmap_host_write_kring_csb).
212      */
213     kring->nr_hwtail = ptr->hwtail;
214     mb();
215     kring->nr_hwcur = ptr->hwcur;
216 }
217 
218 #endif /* WITH_PTNETMAP_GUEST */
219 
220 #ifdef WITH_PTNETMAP_HOST
221 /*
222  * ptnetmap kernel thread routines
223  * */
224 
225 /* Functions to read and write CSB fields in the host */
226 #if defined (linux)
227 #define CSB_READ(csb, field, r) (get_user(r, &csb->field))
228 #define CSB_WRITE(csb, field, v) (put_user(v, &csb->field))
229 #else  /* ! linux */
230 #define CSB_READ(csb, field, r) (r = fuword32(&csb->field))
231 #define CSB_WRITE(csb, field, v) (suword32(&csb->field, v))
232 #endif /* ! linux */
233 
234 /* Host netmap: Write kring pointers (hwcur, hwtail) to the CSB.
235  * This routine is coupled with ptnetmap_guest_read_kring_csb(). */
236 static inline void
237 ptnetmap_host_write_kring_csb(struct ptnet_ring __user *ptr, uint32_t hwcur,
238         uint32_t hwtail)
239 {
240     /*
241      * The same scheme used in ptnetmap_guest_write_kring_csb() applies here.
242      * We allow the guest to read a value of hwcur more recent than the value
243      * of hwtail, since this would anyway result in a consistent view of the
244      * ring state (and hwcur can never wraparound hwtail, since hwcur must be
245      * behind head).
246      *
247      * The following memory barrier scheme is used to make this happen:
248      *
249      *          Guest                Host
250      *
251      *          STORE(hwcur)         LOAD(hwtail)
252      *          mb() <-------------> mb()
253      *          STORE(hwtail)        LOAD(hwcur)
254      */
255     CSB_WRITE(ptr, hwcur, hwcur);
256     mb();
257     CSB_WRITE(ptr, hwtail, hwtail);
258 }
259 
260 /* Host netmap: Read kring pointers (head, cur, sync_flags) from the CSB.
261  * This routine is coupled with ptnetmap_guest_write_kring_csb(). */
262 static inline void
263 ptnetmap_host_read_kring_csb(struct ptnet_ring __user *ptr,
264 			     struct netmap_ring *shadow_ring,
265 			     uint32_t num_slots)
266 {
267     /*
268      * We place a memory barrier to make sure that the update of head never
269      * overtakes the update of cur.
270      * (see explanation in ptnetmap_guest_write_kring_csb).
271      */
272     CSB_READ(ptr, head, shadow_ring->head);
273     mb();
274     CSB_READ(ptr, cur, shadow_ring->cur);
275     CSB_READ(ptr, sync_flags, shadow_ring->flags);
276 }
277 
278 #endif /* WITH_PTNETMAP_HOST */
279 
280 #endif /* NETMAP_VIRT_H */
281