xref: /freebsd/sys/net/netmap_legacy.h (revision 2ff63af9b88c7413b7d71715b5532625752a248e)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (C) 2011-2014 Matteo Landi, Luigi Rizzo. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  *   1. Redistributions of source code must retain the above copyright
11  *      notice, this list of conditions and the following disclaimer.
12  *   2. Redistributions in binary form must reproduce the above copyright
13  *      notice, this list of conditions and the following disclaimer in the
14  *      documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``S IS''AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #ifndef _NET_NETMAP_LEGACY_H_
30 #define _NET_NETMAP_LEGACY_H_
31 
32 /*
33  *
34  * ioctl names and related fields
35  *
36  * NIOCTXSYNC, NIOCRXSYNC synchronize tx or rx queues,
37  *	whose identity is set in NIOCREGIF through nr_ringid.
38  *	These are non blocking and take no argument.
39  *
40  * NIOCGINFO takes a struct ifreq, the interface name is the input,
41  *	the outputs are number of queues and number of descriptor
42  *	for each queue (useful to set number of threads etc.).
43  *	The info returned is only advisory and may change before
44  *	the interface is bound to a file descriptor.
45  *
46  * NIOCREGIF takes an interface name within a struct nmre,
47  *	and activates netmap mode on the interface (if possible).
48  *
49  * The argument to NIOCGINFO/NIOCREGIF overlays struct ifreq so we
50  * can pass it down to other NIC-related ioctls.
51  *
52  * The actual argument (struct nmreq) has a number of options to request
53  * different functions.
54  * The following are used in NIOCREGIF when nr_cmd == 0:
55  *
56  * nr_name	(in)
57  *	The name of the port (em0, valeXXX:YYY, etc.)
58  *	limited to IFNAMSIZ for backward compatibility.
59  *
60  * nr_version	(in/out)
61  *	Must match NETMAP_API as used in the kernel, error otherwise.
62  *	Always returns the desired value on output.
63  *
64  * nr_tx_slots, nr_tx_slots, nr_tx_rings, nr_rx_rings (in/out)
65  *	On input, non-zero values may be used to reconfigure the port
66  *	according to the requested values, but this is not guaranteed.
67  *	On output the actual values in use are reported.
68  *
69  * nr_ringid (in)
70  *	Indicates how rings should be bound to the file descriptors.
71  *	If nr_flags != 0, then the low bits (in NETMAP_RING_MASK)
72  *	are used to indicate the ring number, and nr_flags specifies
73  *	the actual rings to bind. NETMAP_NO_TX_POLL is unaffected.
74  *
75  *	NOTE: THE FOLLOWING (nr_flags == 0) IS DEPRECATED:
76  *	If nr_flags == 0, NETMAP_HW_RING and NETMAP_SW_RING control
77  *	the binding as follows:
78  *	0 (default)			binds all physical rings
79  *	NETMAP_HW_RING | ring number	binds a single ring pair
80  *	NETMAP_SW_RING			binds only the host tx/rx rings
81  *
82  *	NETMAP_NO_TX_POLL can be OR-ed to make select()/poll() push
83  *		packets on tx rings only if POLLOUT is set.
84  *		The default is to push any pending packet.
85  *
86  *	NETMAP_DO_RX_POLL can be OR-ed to make select()/poll() release
87  *		packets on rx rings also when POLLIN is NOT set.
88  *		The default is to touch the rx ring only with POLLIN.
89  *		Note that this is the opposite of TX because it
90  *		reflects the common usage.
91  *
92  *	NOTE: NETMAP_PRIV_MEM IS DEPRECATED, use nr_arg2 instead.
93  *	NETMAP_PRIV_MEM is set on return for ports that do not use
94  *		the global memory allocator.
95  *		This information is not significant and applications
96  *		should look at the region id in nr_arg2
97  *
98  * nr_flags	is the recommended mode to indicate which rings should
99  *		be bound to a file descriptor. Values are NR_REG_*
100  *
101  * nr_arg1 (in)	Reserved.
102  *
103  * nr_arg2 (in/out) The identity of the memory region used.
104  *		On input, 0 means the system decides autonomously,
105  *		other values may try to select a specific region.
106  *		On return the actual value is reported.
107  *		Region '1' is the global allocator, normally shared
108  *		by all interfaces. Other values are private regions.
109  *		If two ports the same region zero-copy is possible.
110  *
111  * nr_arg3 (in/out)	number of extra buffers to be allocated.
112  *
113  *
114  *
115  * nr_cmd (in)	if non-zero indicates a special command:
116  *	NETMAP_BDG_ATTACH	 and nr_name = vale*:ifname
117  *		attaches the NIC to the switch; nr_ringid specifies
118  *		which rings to use. Used by valectl -a ...
119  *	    nr_arg1 = NETMAP_BDG_HOST also attaches the host port
120  *		as in valectl -h ...
121  *
122  *	NETMAP_BDG_DETACH	and nr_name = vale*:ifname
123  *		disconnects a previously attached NIC.
124  *		Used by valectl -d ...
125  *
126  *	NETMAP_BDG_LIST
127  *		list the configuration of VALE switches.
128  *
129  *	NETMAP_BDG_VNET_HDR
130  *		Set the virtio-net header length used by the client
131  *		of a VALE switch port.
132  *
133  *	NETMAP_BDG_NEWIF
134  *		create a persistent VALE port with name nr_name.
135  *		Used by valectl -n ...
136  *
137  *	NETMAP_BDG_DELIF
138  *		delete a persistent VALE port. Used by valectl -d ...
139  *
140  * nr_arg1, nr_arg2, nr_arg3  (in/out)		command specific
141  *
142  *
143  *
144  */
145 
146 
147 /*
148  * struct nmreq overlays a struct ifreq (just the name)
149  */
150 struct nmreq {
151 	char		nr_name[IFNAMSIZ];
152 	uint32_t	nr_version;	/* API version */
153 	uint32_t	nr_offset;	/* nifp offset in the shared region */
154 	uint32_t	nr_memsize;	/* size of the shared region */
155 	uint32_t	nr_tx_slots;	/* slots in tx rings */
156 	uint32_t	nr_rx_slots;	/* slots in rx rings */
157 	uint16_t	nr_tx_rings;	/* number of tx rings */
158 	uint16_t	nr_rx_rings;	/* number of rx rings */
159 
160 	uint16_t	nr_ringid;	/* ring(s) we care about */
161 #define NETMAP_HW_RING		0x4000	/* single NIC ring pair */
162 #define NETMAP_SW_RING		0x2000	/* only host ring pair */
163 
164 #define NETMAP_RING_MASK	0x0fff	/* the ring number */
165 
166 #define NETMAP_NO_TX_POLL	0x1000	/* no automatic txsync on poll */
167 
168 #define NETMAP_DO_RX_POLL	0x8000	/* DO automatic rxsync on poll */
169 
170 	uint16_t	nr_cmd;
171 #define NETMAP_BDG_ATTACH	1	/* attach the NIC */
172 #define NETMAP_BDG_DETACH	2	/* detach the NIC */
173 #define NETMAP_BDG_REGOPS	3	/* register bridge callbacks */
174 #define NETMAP_BDG_LIST		4	/* get bridge's info */
175 #define NETMAP_BDG_VNET_HDR     5       /* set the port virtio-net-hdr length */
176 #define NETMAP_BDG_NEWIF	6	/* create a virtual port */
177 #define NETMAP_BDG_DELIF	7	/* destroy a virtual port */
178 #define NETMAP_PT_HOST_CREATE	8	/* create ptnetmap kthreads */
179 #define NETMAP_PT_HOST_DELETE	9	/* delete ptnetmap kthreads */
180 #define NETMAP_BDG_POLLING_ON	10	/* delete polling kthread */
181 #define NETMAP_BDG_POLLING_OFF	11	/* delete polling kthread */
182 #define NETMAP_VNET_HDR_GET	12      /* get the port virtio-net-hdr length */
183 	uint16_t	nr_arg1;	/* extra arguments */
184 #define NETMAP_BDG_HOST		1	/* nr_arg1 value for NETMAP_BDG_ATTACH */
185 
186 	uint16_t	nr_arg2;	/* id of the memory allocator */
187 	uint32_t	nr_arg3;	/* req. extra buffers in NIOCREGIF */
188 	uint32_t	nr_flags;	/* specify NR_REG_* mode and other flags */
189 #define NR_REG_MASK		0xf /* to extract NR_REG_* mode from nr_flags */
190 	/* various modes, extends nr_ringid */
191 	uint32_t	spare2[1];
192 };
193 
194 #ifdef _WIN32
195 /*
196  * Windows does not have _IOWR(). _IO(), _IOW() and _IOR() are defined
197  * in ws2def.h but not sure if they are in the form we need.
198  * We therefore redefine them in a convenient way to use for DeviceIoControl
199  * signatures.
200  */
201 #undef _IO	// ws2def.h
202 #define _WIN_NM_IOCTL_TYPE 40000
203 #define _IO(_c, _n)	CTL_CODE(_WIN_NM_IOCTL_TYPE, ((_n) + 0x800) , \
204 		METHOD_BUFFERED, FILE_ANY_ACCESS  )
205 #define _IO_direct(_c, _n)	CTL_CODE(_WIN_NM_IOCTL_TYPE, ((_n) + 0x800) , \
206 		METHOD_OUT_DIRECT, FILE_ANY_ACCESS  )
207 
208 #define _IOWR(_c, _n, _s)	_IO(_c, _n)
209 
210 /* We havesome internal sysctl in addition to the externally visible ones */
211 #define NETMAP_MMAP _IO_direct('i', 160)	// note METHOD_OUT_DIRECT
212 #define NETMAP_POLL _IO('i', 162)
213 
214 /* and also two setsockopt for sysctl emulation */
215 #define NETMAP_SETSOCKOPT _IO('i', 140)
216 #define NETMAP_GETSOCKOPT _IO('i', 141)
217 
218 
219 /* These linknames are for the Netmap Core Driver */
220 #define NETMAP_NT_DEVICE_NAME			L"\\Device\\NETMAP"
221 #define NETMAP_DOS_DEVICE_NAME			L"\\DosDevices\\netmap"
222 
223 /* Definition of a structure used to pass a virtual address within an IOCTL */
224 typedef struct _MEMORY_ENTRY {
225 	PVOID       pUsermodeVirtualAddress;
226 } MEMORY_ENTRY, *PMEMORY_ENTRY;
227 
228 typedef struct _POLL_REQUEST_DATA {
229 	int events;
230 	int timeout;
231 	int revents;
232 } POLL_REQUEST_DATA;
233 #endif /* _WIN32 */
234 
235 /*
236  * Opaque structure that is passed to an external kernel
237  * module via ioctl(fd, NIOCCONFIG, req) for a user-owned
238  * bridge port (at this point ephemeral VALE interface).
239  */
240 #define NM_IFRDATA_LEN 256
241 struct nm_ifreq {
242 	char nifr_name[IFNAMSIZ];
243 	char data[NM_IFRDATA_LEN];
244 };
245 
246 /*
247  * FreeBSD uses the size value embedded in the _IOWR to determine
248  * how much to copy in/out. So we need it to match the actual
249  * data structure we pass. We put some spares in the structure
250  * to ease compatibility with other versions
251  */
252 #define NIOCGINFO	_IOWR('i', 145, struct nmreq) /* return IF info */
253 #define NIOCREGIF	_IOWR('i', 146, struct nmreq) /* interface register */
254 #define NIOCCONFIG	_IOWR('i',150, struct nm_ifreq) /* for ext. modules */
255 
256 #endif /* _NET_NETMAP_LEGACY_H_ */
257