xref: /freebsd/sys/net/netmap_legacy.h (revision 2ff63af9)
12ff91c17SVincenzo Maffione /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
32ff91c17SVincenzo Maffione  *
42ff91c17SVincenzo Maffione  * Copyright (C) 2011-2014 Matteo Landi, Luigi Rizzo. All rights reserved.
52ff91c17SVincenzo Maffione  *
62ff91c17SVincenzo Maffione  * Redistribution and use in source and binary forms, with or without
72ff91c17SVincenzo Maffione  * modification, are permitted provided that the following conditions
82ff91c17SVincenzo Maffione  * are met:
92ff91c17SVincenzo Maffione  *
102ff91c17SVincenzo Maffione  *   1. Redistributions of source code must retain the above copyright
112ff91c17SVincenzo Maffione  *      notice, this list of conditions and the following disclaimer.
122ff91c17SVincenzo Maffione  *   2. Redistributions in binary form must reproduce the above copyright
132ff91c17SVincenzo Maffione  *      notice, this list of conditions and the following disclaimer in the
142ff91c17SVincenzo Maffione  *      documentation and/or other materials provided with the distribution.
152ff91c17SVincenzo Maffione  *
162ff91c17SVincenzo Maffione  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``S IS''AND
172ff91c17SVincenzo Maffione  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
182ff91c17SVincenzo Maffione  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
192ff91c17SVincenzo Maffione  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
202ff91c17SVincenzo Maffione  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
212ff91c17SVincenzo Maffione  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
222ff91c17SVincenzo Maffione  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
232ff91c17SVincenzo Maffione  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
242ff91c17SVincenzo Maffione  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
252ff91c17SVincenzo Maffione  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
262ff91c17SVincenzo Maffione  * SUCH DAMAGE.
272ff91c17SVincenzo Maffione  */
282ff91c17SVincenzo Maffione 
292ff91c17SVincenzo Maffione #ifndef _NET_NETMAP_LEGACY_H_
302ff91c17SVincenzo Maffione #define _NET_NETMAP_LEGACY_H_
312ff91c17SVincenzo Maffione 
322ff91c17SVincenzo Maffione /*
332ff91c17SVincenzo Maffione  *
342ff91c17SVincenzo Maffione  * ioctl names and related fields
352ff91c17SVincenzo Maffione  *
362ff91c17SVincenzo Maffione  * NIOCTXSYNC, NIOCRXSYNC synchronize tx or rx queues,
372ff91c17SVincenzo Maffione  *	whose identity is set in NIOCREGIF through nr_ringid.
382ff91c17SVincenzo Maffione  *	These are non blocking and take no argument.
392ff91c17SVincenzo Maffione  *
402ff91c17SVincenzo Maffione  * NIOCGINFO takes a struct ifreq, the interface name is the input,
412ff91c17SVincenzo Maffione  *	the outputs are number of queues and number of descriptor
422ff91c17SVincenzo Maffione  *	for each queue (useful to set number of threads etc.).
432ff91c17SVincenzo Maffione  *	The info returned is only advisory and may change before
442ff91c17SVincenzo Maffione  *	the interface is bound to a file descriptor.
452ff91c17SVincenzo Maffione  *
462ff91c17SVincenzo Maffione  * NIOCREGIF takes an interface name within a struct nmre,
472ff91c17SVincenzo Maffione  *	and activates netmap mode on the interface (if possible).
482ff91c17SVincenzo Maffione  *
492ff91c17SVincenzo Maffione  * The argument to NIOCGINFO/NIOCREGIF overlays struct ifreq so we
502ff91c17SVincenzo Maffione  * can pass it down to other NIC-related ioctls.
512ff91c17SVincenzo Maffione  *
522ff91c17SVincenzo Maffione  * The actual argument (struct nmreq) has a number of options to request
532ff91c17SVincenzo Maffione  * different functions.
542ff91c17SVincenzo Maffione  * The following are used in NIOCREGIF when nr_cmd == 0:
552ff91c17SVincenzo Maffione  *
562ff91c17SVincenzo Maffione  * nr_name	(in)
572ff91c17SVincenzo Maffione  *	The name of the port (em0, valeXXX:YYY, etc.)
582ff91c17SVincenzo Maffione  *	limited to IFNAMSIZ for backward compatibility.
592ff91c17SVincenzo Maffione  *
602ff91c17SVincenzo Maffione  * nr_version	(in/out)
612ff91c17SVincenzo Maffione  *	Must match NETMAP_API as used in the kernel, error otherwise.
622ff91c17SVincenzo Maffione  *	Always returns the desired value on output.
632ff91c17SVincenzo Maffione  *
642ff91c17SVincenzo Maffione  * nr_tx_slots, nr_tx_slots, nr_tx_rings, nr_rx_rings (in/out)
652ff91c17SVincenzo Maffione  *	On input, non-zero values may be used to reconfigure the port
662ff91c17SVincenzo Maffione  *	according to the requested values, but this is not guaranteed.
672ff91c17SVincenzo Maffione  *	On output the actual values in use are reported.
682ff91c17SVincenzo Maffione  *
692ff91c17SVincenzo Maffione  * nr_ringid (in)
702ff91c17SVincenzo Maffione  *	Indicates how rings should be bound to the file descriptors.
712ff91c17SVincenzo Maffione  *	If nr_flags != 0, then the low bits (in NETMAP_RING_MASK)
722ff91c17SVincenzo Maffione  *	are used to indicate the ring number, and nr_flags specifies
732ff91c17SVincenzo Maffione  *	the actual rings to bind. NETMAP_NO_TX_POLL is unaffected.
742ff91c17SVincenzo Maffione  *
752ff91c17SVincenzo Maffione  *	NOTE: THE FOLLOWING (nr_flags == 0) IS DEPRECATED:
762ff91c17SVincenzo Maffione  *	If nr_flags == 0, NETMAP_HW_RING and NETMAP_SW_RING control
772ff91c17SVincenzo Maffione  *	the binding as follows:
782ff91c17SVincenzo Maffione  *	0 (default)			binds all physical rings
792ff91c17SVincenzo Maffione  *	NETMAP_HW_RING | ring number	binds a single ring pair
802ff91c17SVincenzo Maffione  *	NETMAP_SW_RING			binds only the host tx/rx rings
812ff91c17SVincenzo Maffione  *
822ff91c17SVincenzo Maffione  *	NETMAP_NO_TX_POLL can be OR-ed to make select()/poll() push
832ff91c17SVincenzo Maffione  *		packets on tx rings only if POLLOUT is set.
842ff91c17SVincenzo Maffione  *		The default is to push any pending packet.
852ff91c17SVincenzo Maffione  *
862ff91c17SVincenzo Maffione  *	NETMAP_DO_RX_POLL can be OR-ed to make select()/poll() release
872ff91c17SVincenzo Maffione  *		packets on rx rings also when POLLIN is NOT set.
882ff91c17SVincenzo Maffione  *		The default is to touch the rx ring only with POLLIN.
892ff91c17SVincenzo Maffione  *		Note that this is the opposite of TX because it
902ff91c17SVincenzo Maffione  *		reflects the common usage.
912ff91c17SVincenzo Maffione  *
922ff91c17SVincenzo Maffione  *	NOTE: NETMAP_PRIV_MEM IS DEPRECATED, use nr_arg2 instead.
932ff91c17SVincenzo Maffione  *	NETMAP_PRIV_MEM is set on return for ports that do not use
942ff91c17SVincenzo Maffione  *		the global memory allocator.
952ff91c17SVincenzo Maffione  *		This information is not significant and applications
962ff91c17SVincenzo Maffione  *		should look at the region id in nr_arg2
972ff91c17SVincenzo Maffione  *
982ff91c17SVincenzo Maffione  * nr_flags	is the recommended mode to indicate which rings should
992ff91c17SVincenzo Maffione  *		be bound to a file descriptor. Values are NR_REG_*
1002ff91c17SVincenzo Maffione  *
101d12354a5SVincenzo Maffione  * nr_arg1 (in)	Reserved.
1022ff91c17SVincenzo Maffione  *
1032ff91c17SVincenzo Maffione  * nr_arg2 (in/out) The identity of the memory region used.
1042ff91c17SVincenzo Maffione  *		On input, 0 means the system decides autonomously,
1052ff91c17SVincenzo Maffione  *		other values may try to select a specific region.
1062ff91c17SVincenzo Maffione  *		On return the actual value is reported.
1072ff91c17SVincenzo Maffione  *		Region '1' is the global allocator, normally shared
1082ff91c17SVincenzo Maffione  *		by all interfaces. Other values are private regions.
1092ff91c17SVincenzo Maffione  *		If two ports the same region zero-copy is possible.
1102ff91c17SVincenzo Maffione  *
1112ff91c17SVincenzo Maffione  * nr_arg3 (in/out)	number of extra buffers to be allocated.
1122ff91c17SVincenzo Maffione  *
1132ff91c17SVincenzo Maffione  *
1142ff91c17SVincenzo Maffione  *
1152ff91c17SVincenzo Maffione  * nr_cmd (in)	if non-zero indicates a special command:
1162ff91c17SVincenzo Maffione  *	NETMAP_BDG_ATTACH	 and nr_name = vale*:ifname
1172ff91c17SVincenzo Maffione  *		attaches the NIC to the switch; nr_ringid specifies
118c7c78055SVincenzo Maffione  *		which rings to use. Used by valectl -a ...
1192ff91c17SVincenzo Maffione  *	    nr_arg1 = NETMAP_BDG_HOST also attaches the host port
120c7c78055SVincenzo Maffione  *		as in valectl -h ...
1212ff91c17SVincenzo Maffione  *
1222ff91c17SVincenzo Maffione  *	NETMAP_BDG_DETACH	and nr_name = vale*:ifname
1232ff91c17SVincenzo Maffione  *		disconnects a previously attached NIC.
124c7c78055SVincenzo Maffione  *		Used by valectl -d ...
1252ff91c17SVincenzo Maffione  *
1262ff91c17SVincenzo Maffione  *	NETMAP_BDG_LIST
1272ff91c17SVincenzo Maffione  *		list the configuration of VALE switches.
1282ff91c17SVincenzo Maffione  *
1292ff91c17SVincenzo Maffione  *	NETMAP_BDG_VNET_HDR
1302ff91c17SVincenzo Maffione  *		Set the virtio-net header length used by the client
1312ff91c17SVincenzo Maffione  *		of a VALE switch port.
1322ff91c17SVincenzo Maffione  *
1332ff91c17SVincenzo Maffione  *	NETMAP_BDG_NEWIF
1342ff91c17SVincenzo Maffione  *		create a persistent VALE port with name nr_name.
135c7c78055SVincenzo Maffione  *		Used by valectl -n ...
1362ff91c17SVincenzo Maffione  *
1372ff91c17SVincenzo Maffione  *	NETMAP_BDG_DELIF
138c7c78055SVincenzo Maffione  *		delete a persistent VALE port. Used by valectl -d ...
1392ff91c17SVincenzo Maffione  *
1402ff91c17SVincenzo Maffione  * nr_arg1, nr_arg2, nr_arg3  (in/out)		command specific
1412ff91c17SVincenzo Maffione  *
1422ff91c17SVincenzo Maffione  *
1432ff91c17SVincenzo Maffione  *
1442ff91c17SVincenzo Maffione  */
1452ff91c17SVincenzo Maffione 
146a6d768d8SVincenzo Maffione 
1472ff91c17SVincenzo Maffione /*
1482ff91c17SVincenzo Maffione  * struct nmreq overlays a struct ifreq (just the name)
1492ff91c17SVincenzo Maffione  */
1502ff91c17SVincenzo Maffione struct nmreq {
1512ff91c17SVincenzo Maffione 	char		nr_name[IFNAMSIZ];
1522ff91c17SVincenzo Maffione 	uint32_t	nr_version;	/* API version */
1532ff91c17SVincenzo Maffione 	uint32_t	nr_offset;	/* nifp offset in the shared region */
1542ff91c17SVincenzo Maffione 	uint32_t	nr_memsize;	/* size of the shared region */
1552ff91c17SVincenzo Maffione 	uint32_t	nr_tx_slots;	/* slots in tx rings */
1562ff91c17SVincenzo Maffione 	uint32_t	nr_rx_slots;	/* slots in rx rings */
1572ff91c17SVincenzo Maffione 	uint16_t	nr_tx_rings;	/* number of tx rings */
1582ff91c17SVincenzo Maffione 	uint16_t	nr_rx_rings;	/* number of rx rings */
1592ff91c17SVincenzo Maffione 
1602ff91c17SVincenzo Maffione 	uint16_t	nr_ringid;	/* ring(s) we care about */
1612ff91c17SVincenzo Maffione #define NETMAP_HW_RING		0x4000	/* single NIC ring pair */
1622ff91c17SVincenzo Maffione #define NETMAP_SW_RING		0x2000	/* only host ring pair */
1632ff91c17SVincenzo Maffione 
1642ff91c17SVincenzo Maffione #define NETMAP_RING_MASK	0x0fff	/* the ring number */
1652ff91c17SVincenzo Maffione 
1662ff91c17SVincenzo Maffione #define NETMAP_NO_TX_POLL	0x1000	/* no automatic txsync on poll */
1672ff91c17SVincenzo Maffione 
1682ff91c17SVincenzo Maffione #define NETMAP_DO_RX_POLL	0x8000	/* DO automatic rxsync on poll */
1692ff91c17SVincenzo Maffione 
1702ff91c17SVincenzo Maffione 	uint16_t	nr_cmd;
1712ff91c17SVincenzo Maffione #define NETMAP_BDG_ATTACH	1	/* attach the NIC */
1722ff91c17SVincenzo Maffione #define NETMAP_BDG_DETACH	2	/* detach the NIC */
1732ff91c17SVincenzo Maffione #define NETMAP_BDG_REGOPS	3	/* register bridge callbacks */
1742ff91c17SVincenzo Maffione #define NETMAP_BDG_LIST		4	/* get bridge's info */
1752ff91c17SVincenzo Maffione #define NETMAP_BDG_VNET_HDR     5       /* set the port virtio-net-hdr length */
1762ff91c17SVincenzo Maffione #define NETMAP_BDG_NEWIF	6	/* create a virtual port */
1772ff91c17SVincenzo Maffione #define NETMAP_BDG_DELIF	7	/* destroy a virtual port */
1782ff91c17SVincenzo Maffione #define NETMAP_PT_HOST_CREATE	8	/* create ptnetmap kthreads */
1792ff91c17SVincenzo Maffione #define NETMAP_PT_HOST_DELETE	9	/* delete ptnetmap kthreads */
1802ff91c17SVincenzo Maffione #define NETMAP_BDG_POLLING_ON	10	/* delete polling kthread */
1812ff91c17SVincenzo Maffione #define NETMAP_BDG_POLLING_OFF	11	/* delete polling kthread */
1822ff91c17SVincenzo Maffione #define NETMAP_VNET_HDR_GET	12      /* get the port virtio-net-hdr length */
183d12354a5SVincenzo Maffione 	uint16_t	nr_arg1;	/* extra arguments */
1842ff91c17SVincenzo Maffione #define NETMAP_BDG_HOST		1	/* nr_arg1 value for NETMAP_BDG_ATTACH */
1852ff91c17SVincenzo Maffione 
1862ff91c17SVincenzo Maffione 	uint16_t	nr_arg2;	/* id of the memory allocator */
1872ff91c17SVincenzo Maffione 	uint32_t	nr_arg3;	/* req. extra buffers in NIOCREGIF */
1882ff91c17SVincenzo Maffione 	uint32_t	nr_flags;	/* specify NR_REG_* mode and other flags */
1892ff91c17SVincenzo Maffione #define NR_REG_MASK		0xf /* to extract NR_REG_* mode from nr_flags */
1902ff91c17SVincenzo Maffione 	/* various modes, extends nr_ringid */
1912ff91c17SVincenzo Maffione 	uint32_t	spare2[1];
1922ff91c17SVincenzo Maffione };
1932ff91c17SVincenzo Maffione 
1942ff91c17SVincenzo Maffione #ifdef _WIN32
1952ff91c17SVincenzo Maffione /*
1962ff91c17SVincenzo Maffione  * Windows does not have _IOWR(). _IO(), _IOW() and _IOR() are defined
1972ff91c17SVincenzo Maffione  * in ws2def.h but not sure if they are in the form we need.
1982ff91c17SVincenzo Maffione  * We therefore redefine them in a convenient way to use for DeviceIoControl
1992ff91c17SVincenzo Maffione  * signatures.
2002ff91c17SVincenzo Maffione  */
2012ff91c17SVincenzo Maffione #undef _IO	// ws2def.h
2022ff91c17SVincenzo Maffione #define _WIN_NM_IOCTL_TYPE 40000
2032ff91c17SVincenzo Maffione #define _IO(_c, _n)	CTL_CODE(_WIN_NM_IOCTL_TYPE, ((_n) + 0x800) , \
2042ff91c17SVincenzo Maffione 		METHOD_BUFFERED, FILE_ANY_ACCESS  )
2052ff91c17SVincenzo Maffione #define _IO_direct(_c, _n)	CTL_CODE(_WIN_NM_IOCTL_TYPE, ((_n) + 0x800) , \
2062ff91c17SVincenzo Maffione 		METHOD_OUT_DIRECT, FILE_ANY_ACCESS  )
2072ff91c17SVincenzo Maffione 
2082ff91c17SVincenzo Maffione #define _IOWR(_c, _n, _s)	_IO(_c, _n)
2092ff91c17SVincenzo Maffione 
2102ff91c17SVincenzo Maffione /* We havesome internal sysctl in addition to the externally visible ones */
2112ff91c17SVincenzo Maffione #define NETMAP_MMAP _IO_direct('i', 160)	// note METHOD_OUT_DIRECT
2122ff91c17SVincenzo Maffione #define NETMAP_POLL _IO('i', 162)
2132ff91c17SVincenzo Maffione 
2142ff91c17SVincenzo Maffione /* and also two setsockopt for sysctl emulation */
2152ff91c17SVincenzo Maffione #define NETMAP_SETSOCKOPT _IO('i', 140)
2162ff91c17SVincenzo Maffione #define NETMAP_GETSOCKOPT _IO('i', 141)
2172ff91c17SVincenzo Maffione 
218a6d768d8SVincenzo Maffione 
2192ff91c17SVincenzo Maffione /* These linknames are for the Netmap Core Driver */
2202ff91c17SVincenzo Maffione #define NETMAP_NT_DEVICE_NAME			L"\\Device\\NETMAP"
2212ff91c17SVincenzo Maffione #define NETMAP_DOS_DEVICE_NAME			L"\\DosDevices\\netmap"
2222ff91c17SVincenzo Maffione 
2232ff91c17SVincenzo Maffione /* Definition of a structure used to pass a virtual address within an IOCTL */
2242ff91c17SVincenzo Maffione typedef struct _MEMORY_ENTRY {
2252ff91c17SVincenzo Maffione 	PVOID       pUsermodeVirtualAddress;
2262ff91c17SVincenzo Maffione } MEMORY_ENTRY, *PMEMORY_ENTRY;
2272ff91c17SVincenzo Maffione 
2282ff91c17SVincenzo Maffione typedef struct _POLL_REQUEST_DATA {
2292ff91c17SVincenzo Maffione 	int events;
2302ff91c17SVincenzo Maffione 	int timeout;
2312ff91c17SVincenzo Maffione 	int revents;
2322ff91c17SVincenzo Maffione } POLL_REQUEST_DATA;
2332ff91c17SVincenzo Maffione #endif /* _WIN32 */
2342ff91c17SVincenzo Maffione 
2352ff91c17SVincenzo Maffione /*
2362ff91c17SVincenzo Maffione  * Opaque structure that is passed to an external kernel
2372ff91c17SVincenzo Maffione  * module via ioctl(fd, NIOCCONFIG, req) for a user-owned
2382ff91c17SVincenzo Maffione  * bridge port (at this point ephemeral VALE interface).
2392ff91c17SVincenzo Maffione  */
2402ff91c17SVincenzo Maffione #define NM_IFRDATA_LEN 256
2412ff91c17SVincenzo Maffione struct nm_ifreq {
2422ff91c17SVincenzo Maffione 	char nifr_name[IFNAMSIZ];
2432ff91c17SVincenzo Maffione 	char data[NM_IFRDATA_LEN];
2442ff91c17SVincenzo Maffione };
2452ff91c17SVincenzo Maffione 
2462ff91c17SVincenzo Maffione /*
2472ff91c17SVincenzo Maffione  * FreeBSD uses the size value embedded in the _IOWR to determine
2482ff91c17SVincenzo Maffione  * how much to copy in/out. So we need it to match the actual
2492ff91c17SVincenzo Maffione  * data structure we pass. We put some spares in the structure
2502ff91c17SVincenzo Maffione  * to ease compatibility with other versions
2512ff91c17SVincenzo Maffione  */
2522ff91c17SVincenzo Maffione #define NIOCGINFO	_IOWR('i', 145, struct nmreq) /* return IF info */
2532ff91c17SVincenzo Maffione #define NIOCREGIF	_IOWR('i', 146, struct nmreq) /* interface register */
2542ff91c17SVincenzo Maffione #define NIOCCONFIG	_IOWR('i',150, struct nm_ifreq) /* for ext. modules */
2552ff91c17SVincenzo Maffione 
2562ff91c17SVincenzo Maffione #endif /* _NET_NETMAP_LEGACY_H_ */
257