xref: /freebsd/sys/net/netmap_legacy.h (revision 2ff91c17)
12ff91c17SVincenzo Maffione /*-
22ff91c17SVincenzo Maffione  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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  * $FreeBSD$
342ff91c17SVincenzo Maffione  *
352ff91c17SVincenzo Maffione  * ioctl names and related fields
362ff91c17SVincenzo Maffione  *
372ff91c17SVincenzo Maffione  * NIOCTXSYNC, NIOCRXSYNC synchronize tx or rx queues,
382ff91c17SVincenzo Maffione  *	whose identity is set in NIOCREGIF through nr_ringid.
392ff91c17SVincenzo Maffione  *	These are non blocking and take no argument.
402ff91c17SVincenzo Maffione  *
412ff91c17SVincenzo Maffione  * NIOCGINFO takes a struct ifreq, the interface name is the input,
422ff91c17SVincenzo Maffione  *	the outputs are number of queues and number of descriptor
432ff91c17SVincenzo Maffione  *	for each queue (useful to set number of threads etc.).
442ff91c17SVincenzo Maffione  *	The info returned is only advisory and may change before
452ff91c17SVincenzo Maffione  *	the interface is bound to a file descriptor.
462ff91c17SVincenzo Maffione  *
472ff91c17SVincenzo Maffione  * NIOCREGIF takes an interface name within a struct nmre,
482ff91c17SVincenzo Maffione  *	and activates netmap mode on the interface (if possible).
492ff91c17SVincenzo Maffione  *
502ff91c17SVincenzo Maffione  * The argument to NIOCGINFO/NIOCREGIF overlays struct ifreq so we
512ff91c17SVincenzo Maffione  * can pass it down to other NIC-related ioctls.
522ff91c17SVincenzo Maffione  *
532ff91c17SVincenzo Maffione  * The actual argument (struct nmreq) has a number of options to request
542ff91c17SVincenzo Maffione  * different functions.
552ff91c17SVincenzo Maffione  * The following are used in NIOCREGIF when nr_cmd == 0:
562ff91c17SVincenzo Maffione  *
572ff91c17SVincenzo Maffione  * nr_name	(in)
582ff91c17SVincenzo Maffione  *	The name of the port (em0, valeXXX:YYY, etc.)
592ff91c17SVincenzo Maffione  *	limited to IFNAMSIZ for backward compatibility.
602ff91c17SVincenzo Maffione  *
612ff91c17SVincenzo Maffione  * nr_version	(in/out)
622ff91c17SVincenzo Maffione  *	Must match NETMAP_API as used in the kernel, error otherwise.
632ff91c17SVincenzo Maffione  *	Always returns the desired value on output.
642ff91c17SVincenzo Maffione  *
652ff91c17SVincenzo Maffione  * nr_tx_slots, nr_tx_slots, nr_tx_rings, nr_rx_rings (in/out)
662ff91c17SVincenzo Maffione  *	On input, non-zero values may be used to reconfigure the port
672ff91c17SVincenzo Maffione  *	according to the requested values, but this is not guaranteed.
682ff91c17SVincenzo Maffione  *	On output the actual values in use are reported.
692ff91c17SVincenzo Maffione  *
702ff91c17SVincenzo Maffione  * nr_ringid (in)
712ff91c17SVincenzo Maffione  *	Indicates how rings should be bound to the file descriptors.
722ff91c17SVincenzo Maffione  *	If nr_flags != 0, then the low bits (in NETMAP_RING_MASK)
732ff91c17SVincenzo Maffione  *	are used to indicate the ring number, and nr_flags specifies
742ff91c17SVincenzo Maffione  *	the actual rings to bind. NETMAP_NO_TX_POLL is unaffected.
752ff91c17SVincenzo Maffione  *
762ff91c17SVincenzo Maffione  *	NOTE: THE FOLLOWING (nr_flags == 0) IS DEPRECATED:
772ff91c17SVincenzo Maffione  *	If nr_flags == 0, NETMAP_HW_RING and NETMAP_SW_RING control
782ff91c17SVincenzo Maffione  *	the binding as follows:
792ff91c17SVincenzo Maffione  *	0 (default)			binds all physical rings
802ff91c17SVincenzo Maffione  *	NETMAP_HW_RING | ring number	binds a single ring pair
812ff91c17SVincenzo Maffione  *	NETMAP_SW_RING			binds only the host tx/rx rings
822ff91c17SVincenzo Maffione  *
832ff91c17SVincenzo Maffione  *	NETMAP_NO_TX_POLL can be OR-ed to make select()/poll() push
842ff91c17SVincenzo Maffione  *		packets on tx rings only if POLLOUT is set.
852ff91c17SVincenzo Maffione  *		The default is to push any pending packet.
862ff91c17SVincenzo Maffione  *
872ff91c17SVincenzo Maffione  *	NETMAP_DO_RX_POLL can be OR-ed to make select()/poll() release
882ff91c17SVincenzo Maffione  *		packets on rx rings also when POLLIN is NOT set.
892ff91c17SVincenzo Maffione  *		The default is to touch the rx ring only with POLLIN.
902ff91c17SVincenzo Maffione  *		Note that this is the opposite of TX because it
912ff91c17SVincenzo Maffione  *		reflects the common usage.
922ff91c17SVincenzo Maffione  *
932ff91c17SVincenzo Maffione  *	NOTE: NETMAP_PRIV_MEM IS DEPRECATED, use nr_arg2 instead.
942ff91c17SVincenzo Maffione  *	NETMAP_PRIV_MEM is set on return for ports that do not use
952ff91c17SVincenzo Maffione  *		the global memory allocator.
962ff91c17SVincenzo Maffione  *		This information is not significant and applications
972ff91c17SVincenzo Maffione  *		should look at the region id in nr_arg2
982ff91c17SVincenzo Maffione  *
992ff91c17SVincenzo Maffione  * nr_flags	is the recommended mode to indicate which rings should
1002ff91c17SVincenzo Maffione  *		be bound to a file descriptor. Values are NR_REG_*
1012ff91c17SVincenzo Maffione  *
1022ff91c17SVincenzo Maffione  * nr_arg1 (in)	The number of extra rings to be reserved.
1032ff91c17SVincenzo Maffione  *		Especially when allocating a VALE port the system only
1042ff91c17SVincenzo Maffione  *		allocates the amount of memory needed for the port.
1052ff91c17SVincenzo Maffione  *		If more shared memory rings are desired (e.g. for pipes),
1062ff91c17SVincenzo Maffione  *		the first invocation for the same basename/allocator
1072ff91c17SVincenzo Maffione  *		should specify a suitable number. Memory cannot be
1082ff91c17SVincenzo Maffione  *		extended after the first allocation without closing
1092ff91c17SVincenzo Maffione  *		all ports on the same region.
1102ff91c17SVincenzo Maffione  *
1112ff91c17SVincenzo Maffione  * nr_arg2 (in/out) The identity of the memory region used.
1122ff91c17SVincenzo Maffione  *		On input, 0 means the system decides autonomously,
1132ff91c17SVincenzo Maffione  *		other values may try to select a specific region.
1142ff91c17SVincenzo Maffione  *		On return the actual value is reported.
1152ff91c17SVincenzo Maffione  *		Region '1' is the global allocator, normally shared
1162ff91c17SVincenzo Maffione  *		by all interfaces. Other values are private regions.
1172ff91c17SVincenzo Maffione  *		If two ports the same region zero-copy is possible.
1182ff91c17SVincenzo Maffione  *
1192ff91c17SVincenzo Maffione  * nr_arg3 (in/out)	number of extra buffers to be allocated.
1202ff91c17SVincenzo Maffione  *
1212ff91c17SVincenzo Maffione  *
1222ff91c17SVincenzo Maffione  *
1232ff91c17SVincenzo Maffione  * nr_cmd (in)	if non-zero indicates a special command:
1242ff91c17SVincenzo Maffione  *	NETMAP_BDG_ATTACH	 and nr_name = vale*:ifname
1252ff91c17SVincenzo Maffione  *		attaches the NIC to the switch; nr_ringid specifies
1262ff91c17SVincenzo Maffione  *		which rings to use. Used by vale-ctl -a ...
1272ff91c17SVincenzo Maffione  *	    nr_arg1 = NETMAP_BDG_HOST also attaches the host port
1282ff91c17SVincenzo Maffione  *		as in vale-ctl -h ...
1292ff91c17SVincenzo Maffione  *
1302ff91c17SVincenzo Maffione  *	NETMAP_BDG_DETACH	and nr_name = vale*:ifname
1312ff91c17SVincenzo Maffione  *		disconnects a previously attached NIC.
1322ff91c17SVincenzo Maffione  *		Used by vale-ctl -d ...
1332ff91c17SVincenzo Maffione  *
1342ff91c17SVincenzo Maffione  *	NETMAP_BDG_LIST
1352ff91c17SVincenzo Maffione  *		list the configuration of VALE switches.
1362ff91c17SVincenzo Maffione  *
1372ff91c17SVincenzo Maffione  *	NETMAP_BDG_VNET_HDR
1382ff91c17SVincenzo Maffione  *		Set the virtio-net header length used by the client
1392ff91c17SVincenzo Maffione  *		of a VALE switch port.
1402ff91c17SVincenzo Maffione  *
1412ff91c17SVincenzo Maffione  *	NETMAP_BDG_NEWIF
1422ff91c17SVincenzo Maffione  *		create a persistent VALE port with name nr_name.
1432ff91c17SVincenzo Maffione  *		Used by vale-ctl -n ...
1442ff91c17SVincenzo Maffione  *
1452ff91c17SVincenzo Maffione  *	NETMAP_BDG_DELIF
1462ff91c17SVincenzo Maffione  *		delete a persistent VALE port. Used by vale-ctl -d ...
1472ff91c17SVincenzo Maffione  *
1482ff91c17SVincenzo Maffione  * nr_arg1, nr_arg2, nr_arg3  (in/out)		command specific
1492ff91c17SVincenzo Maffione  *
1502ff91c17SVincenzo Maffione  *
1512ff91c17SVincenzo Maffione  *
1522ff91c17SVincenzo Maffione  */
1532ff91c17SVincenzo Maffione 
1542ff91c17SVincenzo Maffione 
1552ff91c17SVincenzo Maffione /*
1562ff91c17SVincenzo Maffione  * struct nmreq overlays a struct ifreq (just the name)
1572ff91c17SVincenzo Maffione  */
1582ff91c17SVincenzo Maffione struct nmreq {
1592ff91c17SVincenzo Maffione 	char		nr_name[IFNAMSIZ];
1602ff91c17SVincenzo Maffione 	uint32_t	nr_version;	/* API version */
1612ff91c17SVincenzo Maffione 	uint32_t	nr_offset;	/* nifp offset in the shared region */
1622ff91c17SVincenzo Maffione 	uint32_t	nr_memsize;	/* size of the shared region */
1632ff91c17SVincenzo Maffione 	uint32_t	nr_tx_slots;	/* slots in tx rings */
1642ff91c17SVincenzo Maffione 	uint32_t	nr_rx_slots;	/* slots in rx rings */
1652ff91c17SVincenzo Maffione 	uint16_t	nr_tx_rings;	/* number of tx rings */
1662ff91c17SVincenzo Maffione 	uint16_t	nr_rx_rings;	/* number of rx rings */
1672ff91c17SVincenzo Maffione 
1682ff91c17SVincenzo Maffione 	uint16_t	nr_ringid;	/* ring(s) we care about */
1692ff91c17SVincenzo Maffione #define NETMAP_HW_RING		0x4000	/* single NIC ring pair */
1702ff91c17SVincenzo Maffione #define NETMAP_SW_RING		0x2000	/* only host ring pair */
1712ff91c17SVincenzo Maffione 
1722ff91c17SVincenzo Maffione #define NETMAP_RING_MASK	0x0fff	/* the ring number */
1732ff91c17SVincenzo Maffione 
1742ff91c17SVincenzo Maffione #define NETMAP_NO_TX_POLL	0x1000	/* no automatic txsync on poll */
1752ff91c17SVincenzo Maffione 
1762ff91c17SVincenzo Maffione #define NETMAP_DO_RX_POLL	0x8000	/* DO automatic rxsync on poll */
1772ff91c17SVincenzo Maffione 
1782ff91c17SVincenzo Maffione 	uint16_t	nr_cmd;
1792ff91c17SVincenzo Maffione #define NETMAP_BDG_ATTACH	1	/* attach the NIC */
1802ff91c17SVincenzo Maffione #define NETMAP_BDG_DETACH	2	/* detach the NIC */
1812ff91c17SVincenzo Maffione #define NETMAP_BDG_REGOPS	3	/* register bridge callbacks */
1822ff91c17SVincenzo Maffione #define NETMAP_BDG_LIST		4	/* get bridge's info */
1832ff91c17SVincenzo Maffione #define NETMAP_BDG_VNET_HDR     5       /* set the port virtio-net-hdr length */
1842ff91c17SVincenzo Maffione #define NETMAP_BDG_NEWIF	6	/* create a virtual port */
1852ff91c17SVincenzo Maffione #define NETMAP_BDG_DELIF	7	/* destroy a virtual port */
1862ff91c17SVincenzo Maffione #define NETMAP_PT_HOST_CREATE	8	/* create ptnetmap kthreads */
1872ff91c17SVincenzo Maffione #define NETMAP_PT_HOST_DELETE	9	/* delete ptnetmap kthreads */
1882ff91c17SVincenzo Maffione #define NETMAP_BDG_POLLING_ON	10	/* delete polling kthread */
1892ff91c17SVincenzo Maffione #define NETMAP_BDG_POLLING_OFF	11	/* delete polling kthread */
1902ff91c17SVincenzo Maffione #define NETMAP_VNET_HDR_GET	12      /* get the port virtio-net-hdr length */
1912ff91c17SVincenzo Maffione 	uint16_t	nr_arg1;	/* reserve extra rings in NIOCREGIF */
1922ff91c17SVincenzo Maffione #define NETMAP_BDG_HOST		1	/* nr_arg1 value for NETMAP_BDG_ATTACH */
1932ff91c17SVincenzo Maffione 
1942ff91c17SVincenzo Maffione 	uint16_t	nr_arg2;	/* id of the memory allocator */
1952ff91c17SVincenzo Maffione 	uint32_t	nr_arg3;	/* req. extra buffers in NIOCREGIF */
1962ff91c17SVincenzo Maffione 	uint32_t	nr_flags;	/* specify NR_REG_* mode and other flags */
1972ff91c17SVincenzo Maffione #define NR_REG_MASK		0xf /* to extract NR_REG_* mode from nr_flags */
1982ff91c17SVincenzo Maffione 	/* various modes, extends nr_ringid */
1992ff91c17SVincenzo Maffione 	uint32_t	spare2[1];
2002ff91c17SVincenzo Maffione };
2012ff91c17SVincenzo Maffione 
2022ff91c17SVincenzo Maffione #ifdef _WIN32
2032ff91c17SVincenzo Maffione /*
2042ff91c17SVincenzo Maffione  * Windows does not have _IOWR(). _IO(), _IOW() and _IOR() are defined
2052ff91c17SVincenzo Maffione  * in ws2def.h but not sure if they are in the form we need.
2062ff91c17SVincenzo Maffione  * We therefore redefine them in a convenient way to use for DeviceIoControl
2072ff91c17SVincenzo Maffione  * signatures.
2082ff91c17SVincenzo Maffione  */
2092ff91c17SVincenzo Maffione #undef _IO	// ws2def.h
2102ff91c17SVincenzo Maffione #define _WIN_NM_IOCTL_TYPE 40000
2112ff91c17SVincenzo Maffione #define _IO(_c, _n)	CTL_CODE(_WIN_NM_IOCTL_TYPE, ((_n) + 0x800) , \
2122ff91c17SVincenzo Maffione 		METHOD_BUFFERED, FILE_ANY_ACCESS  )
2132ff91c17SVincenzo Maffione #define _IO_direct(_c, _n)	CTL_CODE(_WIN_NM_IOCTL_TYPE, ((_n) + 0x800) , \
2142ff91c17SVincenzo Maffione 		METHOD_OUT_DIRECT, FILE_ANY_ACCESS  )
2152ff91c17SVincenzo Maffione 
2162ff91c17SVincenzo Maffione #define _IOWR(_c, _n, _s)	_IO(_c, _n)
2172ff91c17SVincenzo Maffione 
2182ff91c17SVincenzo Maffione /* We havesome internal sysctl in addition to the externally visible ones */
2192ff91c17SVincenzo Maffione #define NETMAP_MMAP _IO_direct('i', 160)	// note METHOD_OUT_DIRECT
2202ff91c17SVincenzo Maffione #define NETMAP_POLL _IO('i', 162)
2212ff91c17SVincenzo Maffione 
2222ff91c17SVincenzo Maffione /* and also two setsockopt for sysctl emulation */
2232ff91c17SVincenzo Maffione #define NETMAP_SETSOCKOPT _IO('i', 140)
2242ff91c17SVincenzo Maffione #define NETMAP_GETSOCKOPT _IO('i', 141)
2252ff91c17SVincenzo Maffione 
2262ff91c17SVincenzo Maffione 
2272ff91c17SVincenzo Maffione /* These linknames are for the Netmap Core Driver */
2282ff91c17SVincenzo Maffione #define NETMAP_NT_DEVICE_NAME			L"\\Device\\NETMAP"
2292ff91c17SVincenzo Maffione #define NETMAP_DOS_DEVICE_NAME			L"\\DosDevices\\netmap"
2302ff91c17SVincenzo Maffione 
2312ff91c17SVincenzo Maffione /* Definition of a structure used to pass a virtual address within an IOCTL */
2322ff91c17SVincenzo Maffione typedef struct _MEMORY_ENTRY {
2332ff91c17SVincenzo Maffione 	PVOID       pUsermodeVirtualAddress;
2342ff91c17SVincenzo Maffione } MEMORY_ENTRY, *PMEMORY_ENTRY;
2352ff91c17SVincenzo Maffione 
2362ff91c17SVincenzo Maffione typedef struct _POLL_REQUEST_DATA {
2372ff91c17SVincenzo Maffione 	int events;
2382ff91c17SVincenzo Maffione 	int timeout;
2392ff91c17SVincenzo Maffione 	int revents;
2402ff91c17SVincenzo Maffione } POLL_REQUEST_DATA;
2412ff91c17SVincenzo Maffione #endif /* _WIN32 */
2422ff91c17SVincenzo Maffione 
2432ff91c17SVincenzo Maffione /*
2442ff91c17SVincenzo Maffione  * Opaque structure that is passed to an external kernel
2452ff91c17SVincenzo Maffione  * module via ioctl(fd, NIOCCONFIG, req) for a user-owned
2462ff91c17SVincenzo Maffione  * bridge port (at this point ephemeral VALE interface).
2472ff91c17SVincenzo Maffione  */
2482ff91c17SVincenzo Maffione #define NM_IFRDATA_LEN 256
2492ff91c17SVincenzo Maffione struct nm_ifreq {
2502ff91c17SVincenzo Maffione 	char nifr_name[IFNAMSIZ];
2512ff91c17SVincenzo Maffione 	char data[NM_IFRDATA_LEN];
2522ff91c17SVincenzo Maffione };
2532ff91c17SVincenzo Maffione 
2542ff91c17SVincenzo Maffione /*
2552ff91c17SVincenzo Maffione  * FreeBSD uses the size value embedded in the _IOWR to determine
2562ff91c17SVincenzo Maffione  * how much to copy in/out. So we need it to match the actual
2572ff91c17SVincenzo Maffione  * data structure we pass. We put some spares in the structure
2582ff91c17SVincenzo Maffione  * to ease compatibility with other versions
2592ff91c17SVincenzo Maffione  */
2602ff91c17SVincenzo Maffione #define NIOCGINFO	_IOWR('i', 145, struct nmreq) /* return IF info */
2612ff91c17SVincenzo Maffione #define NIOCREGIF	_IOWR('i', 146, struct nmreq) /* interface register */
2622ff91c17SVincenzo Maffione #define NIOCCONFIG	_IOWR('i',150, struct nm_ifreq) /* for ext. modules */
2632ff91c17SVincenzo Maffione 
2642ff91c17SVincenzo Maffione #endif /* _NET_NETMAP_LEGACY_H_ */
265