1139823Simp/*
217352Swollman * Copyright (C) 2011-2014 Matteo Landi, Luigi Rizzo. All rights reserved.
317352Swollman *
417352Swollman * Redistribution and use in source and binary forms, with or without
517352Swollman * modification, are permitted provided that the following conditions
617352Swollman * are met:
717352Swollman *
817352Swollman *   1. Redistributions of source code must retain the above copyright
917352Swollman *      notice, this list of conditions and the following disclaimer.
1017352Swollman *   2. Redistributions in binary form must reproduce the above copyright
1117352Swollman *      notice, this list of conditions and the following disclaimer in the
1217352Swollman *      documentation and/or other materials provided with the distribution.
1317352Swollman *
1417352Swollman * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``S IS''AND
1517352Swollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1617352Swollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1717352Swollman * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1817352Swollman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1917352Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2017352Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2117352Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2217352Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2317352Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2417352Swollman * SUCH DAMAGE.
2517352Swollman */
2617352Swollman/* $FreeBSD: stable/11/sys/net/netmap_legacy.h 341477 2018-12-04 17:40:56Z vmaffione $ */
2717352Swollman
2817352Swollman#ifndef _NET_NETMAP_LEGACY_H_
2950477Speter#define _NET_NETMAP_LEGACY_H_
3017352Swollman
3117352Swollman/*
3217352Swollman * ioctl names and related fields
3317352Swollman *
3417352Swollman * NIOCTXSYNC, NIOCRXSYNC synchronize tx or rx queues,
35295126Sglebius *	whose identity is set in NIOCREGIF through nr_ringid.
3618892Sbde *	These are non blocking and take no argument.
3717352Swollman *
3817352Swollman * NIOCGINFO takes a struct ifreq, the interface name is the input,
3917352Swollman *	the outputs are number of queues and number of descriptor
40257176Sglebius *	for each queue (useful to set number of threads etc.).
4117352Swollman *	The info returned is only advisory and may change before
42185571Sbz *	the interface is bound to a file descriptor.
4317352Swollman *
4417352Swollman * NIOCREGIF takes an interface name within a struct nmre,
4517352Swollman *	and activates netmap mode on the interface (if possible).
4617352Swollman *
4717352Swollman * The argument to NIOCGINFO/NIOCREGIF overlays struct ifreq so we
4817352Swollman * can pass it down to other NIC-related ioctls.
4917352Swollman *
5017352Swollman * The actual argument (struct nmreq) has a number of options to request
5117352Swollman * different functions.
5217352Swollman * The following are used in NIOCREGIF when nr_cmd == 0:
5317352Swollman *
5417352Swollman * nr_name	(in)
5517352Swollman *	The name of the port (em0, valeXXX:YYY, etc.)
5617352Swollman *	limited to IFNAMSIZ for backward compatibility.
5717352Swollman *
5817352Swollman * nr_version	(in/out)
5917352Swollman *	Must match NETMAP_API as used in the kernel, error otherwise.
6017352Swollman *	Always returns the desired value on output.
6117352Swollman *
6217352Swollman * nr_tx_slots, nr_tx_slots, nr_tx_rings, nr_rx_rings (in/out)
6317352Swollman *	On input, non-zero values may be used to reconfigure the port
6417352Swollman *	according to the requested values, but this is not guaranteed.
6517352Swollman *	On output the actual values in use are reported.
6617352Swollman *
6744078Sdfr * nr_ringid (in)
68227309Sed *	Indicates how rings should be bound to the file descriptors.
6917352Swollman *	If nr_flags != 0, then the low bits (in NETMAP_RING_MASK)
7017352Swollman *	are used to indicate the ring number, and nr_flags specifies
71274225Sglebius *	the actual rings to bind. NETMAP_NO_TX_POLL is unaffected.
72274225Sglebius *
73274225Sglebius *	NOTE: THE FOLLOWING (nr_flags == 0) IS DEPRECATED:
74183550Szec *	If nr_flags == 0, NETMAP_HW_RING and NETMAP_SW_RING control
7517352Swollman *	the binding as follows:
7662573Sphk *	0 (default)			binds all physical rings
7717352Swollman *	NETMAP_HW_RING | ring number	binds a single ring pair
7817352Swollman *	NETMAP_SW_RING			binds only the host tx/rx rings
79121816Sbrooks *
8017352Swollman *	NETMAP_NO_TX_POLL can be OR-ed to make select()/poll() push
8117352Swollman *		packets on tx rings only if POLLOUT is set.
8217352Swollman *		The default is to push any pending packet.
83154023Sharti *
84154023Sharti *	NETMAP_DO_RX_POLL can be OR-ed to make select()/poll() release
8517352Swollman *		packets on rx rings also when POLLIN is NOT set.
8617352Swollman *		The default is to touch the rx ring only with POLLIN.
8717352Swollman *		Note that this is the opposite of TX because it
88191367Srwatson *		reflects the common usage.
89191367Srwatson *
90191367Srwatson *	NOTE: NETMAP_PRIV_MEM IS DEPRECATED, use nr_arg2 instead.
91191367Srwatson *	NETMAP_PRIV_MEM is set on return for ports that do not use
92191367Srwatson *		the global memory allocator.
9317352Swollman *		This information is not significant and applications
9417352Swollman *		should look at the region id in nr_arg2
9517352Swollman *
96191367Srwatson * nr_flags	is the recommended mode to indicate which rings should
97191367Srwatson *		be bound to a file descriptor. Values are NR_REG_*
9817352Swollman *
9917352Swollman * nr_arg1 (in)	The number of extra rings to be reserved.
100145953Scperciva *		Especially when allocating a VALE port the system only
101121816Sbrooks *		allocates the amount of memory needed for the port.
10217352Swollman *		If more shared memory rings are desired (e.g. for pipes),
103270870Sglebius *		the first invocation for the same basename/allocator
104270870Sglebius *		should specify a suitable number. Memory cannot be
105270870Sglebius *		extended after the first allocation without closing
106151569Scsjp *		all ports on the same region.
10717352Swollman *
10817352Swollman * nr_arg2 (in/out) The identity of the memory region used.
109271856Sglebius *		On input, 0 means the system decides autonomously,
110271856Sglebius *		other values may try to select a specific region.
11117352Swollman *		On return the actual value is reported.
11217352Swollman *		Region '1' is the global allocator, normally shared
11317352Swollman *		by all interfaces. Other values are private regions.
114191367Srwatson *		If two ports the same region zero-copy is possible.
11517352Swollman *
11617352Swollman * nr_arg3 (in/out)	number of extra buffers to be allocated.
11717352Swollman *
11817352Swollman *
11917352Swollman *
120191367Srwatson * nr_cmd (in)	if non-zero indicates a special command:
12117352Swollman *	NETMAP_BDG_ATTACH	 and nr_name = vale*:ifname
12217352Swollman *		attaches the NIC to the switch; nr_ringid specifies
12317352Swollman *		which rings to use. Used by vale-ctl -a ...
124191367Srwatson *	    nr_arg1 = NETMAP_BDG_HOST also attaches the host port
125182904Sbms *		as in vale-ctl -h ...
126154023Sharti *
127154023Sharti *	NETMAP_BDG_DETACH	and nr_name = vale*:ifname
128154023Sharti *		disconnects a previously attached NIC.
129154023Sharti *		Used by vale-ctl -d ...
130191367Srwatson *
131191367Srwatson *	NETMAP_BDG_LIST
132191367Srwatson *		list the configuration of VALE switches.
133191367Srwatson *
134154023Sharti *	NETMAP_BDG_VNET_HDR
135154023Sharti *		Set the virtio-net header length used by the client
136154023Sharti *		of a VALE switch port.
137154023Sharti *
138154023Sharti *	NETMAP_BDG_NEWIF
139154023Sharti *		create a persistent VALE port with name nr_name.
140154023Sharti *		Used by vale-ctl -n ...
141154023Sharti *
142154023Sharti *	NETMAP_BDG_DELIF
143191367Srwatson *		delete a persistent VALE port. Used by vale-ctl -d ...
14417352Swollman *
145191367Srwatson * nr_arg1, nr_arg2, nr_arg3  (in/out)		command specific
146191367Srwatson *
147191367Srwatson *
14817352Swollman *
14917352Swollman */
150227309Sed
15117352Swollman
15217352Swollman/*
153 * struct nmreq overlays a struct ifreq (just the name)
154 */
155struct nmreq {
156	char		nr_name[IFNAMSIZ];
157	uint32_t	nr_version;	/* API version */
158	uint32_t	nr_offset;	/* nifp offset in the shared region */
159	uint32_t	nr_memsize;	/* size of the shared region */
160	uint32_t	nr_tx_slots;	/* slots in tx rings */
161	uint32_t	nr_rx_slots;	/* slots in rx rings */
162	uint16_t	nr_tx_rings;	/* number of tx rings */
163	uint16_t	nr_rx_rings;	/* number of rx rings */
164
165	uint16_t	nr_ringid;	/* ring(s) we care about */
166#define NETMAP_HW_RING		0x4000	/* single NIC ring pair */
167#define NETMAP_SW_RING		0x2000	/* only host ring pair */
168
169#define NETMAP_RING_MASK	0x0fff	/* the ring number */
170
171#define NETMAP_NO_TX_POLL	0x1000	/* no automatic txsync on poll */
172
173#define NETMAP_DO_RX_POLL	0x8000	/* DO automatic rxsync on poll */
174
175	uint16_t	nr_cmd;
176#define NETMAP_BDG_ATTACH	1	/* attach the NIC */
177#define NETMAP_BDG_DETACH	2	/* detach the NIC */
178#define NETMAP_BDG_REGOPS	3	/* register bridge callbacks */
179#define NETMAP_BDG_LIST		4	/* get bridge's info */
180#define NETMAP_BDG_VNET_HDR     5       /* set the port virtio-net-hdr length */
181#define NETMAP_BDG_NEWIF	6	/* create a virtual port */
182#define NETMAP_BDG_DELIF	7	/* destroy a virtual port */
183#define NETMAP_PT_HOST_CREATE	8	/* create ptnetmap kthreads */
184#define NETMAP_PT_HOST_DELETE	9	/* delete ptnetmap kthreads */
185#define NETMAP_BDG_POLLING_ON	10	/* delete polling kthread */
186#define NETMAP_BDG_POLLING_OFF	11	/* delete polling kthread */
187#define NETMAP_VNET_HDR_GET	12      /* get the port virtio-net-hdr length */
188	uint16_t	nr_arg1;	/* reserve extra rings in NIOCREGIF */
189#define NETMAP_BDG_HOST		1	/* nr_arg1 value for NETMAP_BDG_ATTACH */
190
191	uint16_t	nr_arg2;	/* id of the memory allocator */
192	uint32_t	nr_arg3;	/* req. extra buffers in NIOCREGIF */
193	uint32_t	nr_flags;	/* specify NR_REG_* mode and other flags */
194#define NR_REG_MASK		0xf /* to extract NR_REG_* mode from nr_flags */
195	/* various modes, extends nr_ringid */
196	uint32_t	spare2[1];
197};
198
199#ifdef _WIN32
200/*
201 * Windows does not have _IOWR(). _IO(), _IOW() and _IOR() are defined
202 * in ws2def.h but not sure if they are in the form we need.
203 * We therefore redefine them in a convenient way to use for DeviceIoControl
204 * signatures.
205 */
206#undef _IO	// ws2def.h
207#define _WIN_NM_IOCTL_TYPE 40000
208#define _IO(_c, _n)	CTL_CODE(_WIN_NM_IOCTL_TYPE, ((_n) + 0x800) , \
209		METHOD_BUFFERED, FILE_ANY_ACCESS  )
210#define _IO_direct(_c, _n)	CTL_CODE(_WIN_NM_IOCTL_TYPE, ((_n) + 0x800) , \
211		METHOD_OUT_DIRECT, FILE_ANY_ACCESS  )
212
213#define _IOWR(_c, _n, _s)	_IO(_c, _n)
214
215/* We havesome internal sysctl in addition to the externally visible ones */
216#define NETMAP_MMAP _IO_direct('i', 160)	// note METHOD_OUT_DIRECT
217#define NETMAP_POLL _IO('i', 162)
218
219/* and also two setsockopt for sysctl emulation */
220#define NETMAP_SETSOCKOPT _IO('i', 140)
221#define NETMAP_GETSOCKOPT _IO('i', 141)
222
223
224/* These linknames are for the Netmap Core Driver */
225#define NETMAP_NT_DEVICE_NAME			L"\\Device\\NETMAP"
226#define NETMAP_DOS_DEVICE_NAME			L"\\DosDevices\\netmap"
227
228/* Definition of a structure used to pass a virtual address within an IOCTL */
229typedef struct _MEMORY_ENTRY {
230	PVOID       pUsermodeVirtualAddress;
231} MEMORY_ENTRY, *PMEMORY_ENTRY;
232
233typedef struct _POLL_REQUEST_DATA {
234	int events;
235	int timeout;
236	int revents;
237} POLL_REQUEST_DATA;
238#endif /* _WIN32 */
239
240/*
241 * Opaque structure that is passed to an external kernel
242 * module via ioctl(fd, NIOCCONFIG, req) for a user-owned
243 * bridge port (at this point ephemeral VALE interface).
244 */
245#define NM_IFRDATA_LEN 256
246struct nm_ifreq {
247	char nifr_name[IFNAMSIZ];
248	char data[NM_IFRDATA_LEN];
249};
250
251/*
252 * FreeBSD uses the size value embedded in the _IOWR to determine
253 * how much to copy in/out. So we need it to match the actual
254 * data structure we pass. We put some spares in the structure
255 * to ease compatibility with other versions
256 */
257#define NIOCGINFO	_IOWR('i', 145, struct nmreq) /* return IF info */
258#define NIOCREGIF	_IOWR('i', 146, struct nmreq) /* interface register */
259#define NIOCCONFIG	_IOWR('i',150, struct nm_ifreq) /* for ext. modules */
260
261#endif /* _NET_NETMAP_LEGACY_H_ */
262