xref: /freebsd/sys/net/netisr.h (revision 484149def80975bda4e583a1d77c4c6ee2baf074)
1c398230bSWarner Losh /*-
2d4b5cae4SRobert Watson  * Copyright (c) 2007-2009 Robert N. M. Watson
3f2d2d694SRobert Watson  * Copyright (c) 2010-2011 Juniper Networks, Inc.
4d4b5cae4SRobert Watson  * All rights reserved.
5df8bae1dSRodney W. Grimes  *
62d22f334SRobert Watson  * This software was developed by Robert N. M. Watson under contract
72d22f334SRobert Watson  * to Juniper Networks, Inc.
82d22f334SRobert Watson  *
9df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
10df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
11df8bae1dSRodney W. Grimes  * are met:
12df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
13df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
14df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
15df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
16df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
17df8bae1dSRodney W. Grimes  *
18d4b5cae4SRobert Watson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21d4b5cae4SRobert Watson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
29df8bae1dSRodney W. Grimes  *
30c3aac50fSPeter Wemm  * $FreeBSD$
31df8bae1dSRodney W. Grimes  */
32df8bae1dSRodney W. Grimes 
33cea1da3bSPaul Richards #ifndef _NET_NETISR_H_
34cea1da3bSPaul Richards #define _NET_NETISR_H_
35cea1da3bSPaul Richards 
36df8bae1dSRodney W. Grimes /*
37315f0461SRobert Watson  * The netisr (network interrupt service routine) provides a deferred
38315f0461SRobert Watson  * execution evironment in which (generally inbound) network processing can
39d4b5cae4SRobert Watson  * take place.  Protocols register handlers which will be executed directly,
40d4b5cae4SRobert Watson  * or via deferred dispatch, depending on the circumstances.
41df8bae1dSRodney W. Grimes  *
42315f0461SRobert Watson  * Historically, this was implemented by the BSD software ISR facility; it is
43315f0461SRobert Watson  * now implemented via a software ithread (SWI).
44df8bae1dSRodney W. Grimes  */
452d22f334SRobert Watson 
462d22f334SRobert Watson /*
472d22f334SRobert Watson  * Protocol numbers, which are encoded in monitoring applications and kernel
482d22f334SRobert Watson  * modules.  Internally, these are used in bit shift operations so must have
492d22f334SRobert Watson  * a value 0 < proto < 32; we currently further limit at compile-time to 16
502d22f334SRobert Watson  * for array-sizing purposes.
512d22f334SRobert Watson  */
52ed54411cSRobert Watson #define	NETISR_IP	1
53ed54411cSRobert Watson #define	NETISR_IGMP	2		/* IGMPv3 output queue */
54ed54411cSRobert Watson #define	NETISR_ROUTE	3		/* routing socket */
5545c203fcSGleb Smirnoff #define	NETISR_ARP	4		/* same as AF_LINK */
5645c203fcSGleb Smirnoff #define	NETISR_ETHER	5		/* ethernet input */
5745c203fcSGleb Smirnoff #define	NETISR_IPV6	6
5845c203fcSGleb Smirnoff #define	NETISR_NATM	7
5945c203fcSGleb Smirnoff #define	NETISR_EPAIR	8		/* if_epair(4) */
60b8bc95cdSAdrian Chadd #define	NETISR_IP_DIRECT	9	/* direct-dispatch IPv4 */
61b8bc95cdSAdrian Chadd #define	NETISR_IPV6_DIRECT	10	/* direct-dispatch IPv6 */
62df8bae1dSRodney W. Grimes 
632d22f334SRobert Watson /*
642d22f334SRobert Watson  * Protocol ordering and affinity policy constants.  See the detailed
652d22f334SRobert Watson  * discussion of policies later in the file.
662d22f334SRobert Watson  */
672d22f334SRobert Watson #define	NETISR_POLICY_SOURCE	1	/* Maintain source ordering. */
682d22f334SRobert Watson #define	NETISR_POLICY_FLOW	2	/* Maintain flow ordering. */
692d22f334SRobert Watson #define	NETISR_POLICY_CPU	3	/* Protocol determines CPU placement. */
702d22f334SRobert Watson 
712d22f334SRobert Watson /*
72f2d2d694SRobert Watson  * Protocol dispatch policy constants; selects whether and when direct
73f2d2d694SRobert Watson  * dispatch is permitted.
74f2d2d694SRobert Watson  */
75f2d2d694SRobert Watson #define	NETISR_DISPATCH_DEFAULT		0	/* Use global default. */
76f2d2d694SRobert Watson #define	NETISR_DISPATCH_DEFERRED	1	/* Always defer dispatch. */
77f2d2d694SRobert Watson #define	NETISR_DISPATCH_HYBRID		2	/* Allow hybrid dispatch. */
78f2d2d694SRobert Watson #define	NETISR_DISPATCH_DIRECT		3	/* Always direct dispatch. */
79f2d2d694SRobert Watson 
80f2d2d694SRobert Watson /*
812d22f334SRobert Watson  * Monitoring data structures, exported by sysctl(2).
822d22f334SRobert Watson  *
832d22f334SRobert Watson  * Three sysctls are defined.  First, a per-protocol structure exported by
842d22f334SRobert Watson  * net.isr.proto.
852d22f334SRobert Watson  */
862d22f334SRobert Watson #define	NETISR_NAMEMAXLEN	32
872d22f334SRobert Watson struct sysctl_netisr_proto {
882d22f334SRobert Watson 	u_int	snp_version;			/* Length of struct. */
892d22f334SRobert Watson 	char	snp_name[NETISR_NAMEMAXLEN];	/* nh_name */
902d22f334SRobert Watson 	u_int	snp_proto;			/* nh_proto */
912d22f334SRobert Watson 	u_int	snp_qlimit;			/* nh_qlimit */
922d22f334SRobert Watson 	u_int	snp_policy;			/* nh_policy */
932d22f334SRobert Watson 	u_int	snp_flags;			/* Various flags. */
94f2d2d694SRobert Watson 	u_int	snp_dispatch;			/* Dispatch policy. */
95f2d2d694SRobert Watson 	u_int	_snp_ispare[6];
962d22f334SRobert Watson };
972d22f334SRobert Watson 
982d22f334SRobert Watson /*
992d22f334SRobert Watson  * Flags for sysctl_netisr_proto.snp_flags.
1002d22f334SRobert Watson  */
1012d22f334SRobert Watson #define	NETISR_SNP_FLAGS_M2FLOW		0x00000001	/* nh_m2flow */
1022d22f334SRobert Watson #define	NETISR_SNP_FLAGS_M2CPUID	0x00000002	/* nh_m2cpuid */
103c4fbf89fSRobert Watson #define	NETISR_SNP_FLAGS_DRAINEDCPU	0x00000004	/* nh_drainedcpu */
1042d22f334SRobert Watson 
1052d22f334SRobert Watson /*
1062d22f334SRobert Watson  * Next, a structure per-workstream, with per-protocol data, exported as
1072d22f334SRobert Watson  * net.isr.workstream.
1082d22f334SRobert Watson  */
1092d22f334SRobert Watson struct sysctl_netisr_workstream {
1102d22f334SRobert Watson 	u_int	snws_version;			/* Length of struct. */
1112d22f334SRobert Watson 	u_int	snws_flags;			/* Various flags. */
1122d22f334SRobert Watson 	u_int	snws_wsid;			/* Workstream ID. */
1132d22f334SRobert Watson 	u_int	snws_cpu;			/* nws_cpu */
1142d22f334SRobert Watson 	u_int	_snws_ispare[12];
1152d22f334SRobert Watson };
1162d22f334SRobert Watson 
1172d22f334SRobert Watson /*
1182d22f334SRobert Watson  * Flags for sysctl_netisr_workstream.snws_flags
1192d22f334SRobert Watson  */
1202d22f334SRobert Watson #define	NETISR_SNWS_FLAGS_INTR		0x00000001	/* nws_intr_event */
1212d22f334SRobert Watson 
1222d22f334SRobert Watson /*
1232d22f334SRobert Watson  * Finally, a per-workstream-per-protocol structure, exported as
1242d22f334SRobert Watson  * net.isr.work.
1252d22f334SRobert Watson  */
1262d22f334SRobert Watson struct sysctl_netisr_work {
1272d22f334SRobert Watson 	u_int	snw_version;			/* Length of struct. */
1282d22f334SRobert Watson 	u_int	snw_wsid;			/* Workstream ID. */
1292d22f334SRobert Watson 	u_int	snw_proto;			/* Protocol number. */
1302d22f334SRobert Watson 	u_int	snw_len;			/* nw_len */
1312d22f334SRobert Watson 	u_int	snw_watermark;			/* nw_watermark */
1322d22f334SRobert Watson 	u_int	_snw_ispare[3];
1332d22f334SRobert Watson 
1342d22f334SRobert Watson 	uint64_t	snw_dispatched;		/* nw_dispatched */
1352d22f334SRobert Watson 	uint64_t	snw_hybrid_dispatched;	/* nw_hybrid_dispatched */
1362d22f334SRobert Watson 	uint64_t	snw_qdrops;		/* nw_qdrops */
1372d22f334SRobert Watson 	uint64_t	snw_queued;		/* nw_queued */
1382d22f334SRobert Watson 	uint64_t	snw_handled;		/* nw_handled */
1392d22f334SRobert Watson 
1402d22f334SRobert Watson 	uint64_t	_snw_llspare[7];
1412d22f334SRobert Watson };
1422d22f334SRobert Watson 
1432d22f334SRobert Watson #ifdef _KERNEL
1442d22f334SRobert Watson 
145d4b5cae4SRobert Watson /*-
146d4b5cae4SRobert Watson  * Protocols express ordering constraints and affinity preferences by
147d4b5cae4SRobert Watson  * implementing one or neither of nh_m2flow and nh_m2cpuid, which are used by
148d4b5cae4SRobert Watson  * netisr to determine which per-CPU workstream to assign mbufs to.
149d4b5cae4SRobert Watson  *
150d4b5cae4SRobert Watson  * The following policies may be used by protocols:
151d4b5cae4SRobert Watson  *
152d4b5cae4SRobert Watson  * NETISR_POLICY_SOURCE - netisr should maintain source ordering without
153d4b5cae4SRobert Watson  *                        advice from the protocol.  netisr will ignore any
154d4b5cae4SRobert Watson  *                        flow IDs present on the mbuf for the purposes of
155d4b5cae4SRobert Watson  *                        work placement.
156d4b5cae4SRobert Watson  *
157d4b5cae4SRobert Watson  * NETISR_POLICY_FLOW - netisr should maintain flow ordering as defined by
158d4b5cae4SRobert Watson  *                      the mbuf header flow ID field.  If the protocol
159d4b5cae4SRobert Watson  *                      implements nh_m2flow, then netisr will query the
160d4b5cae4SRobert Watson  *                      protocol in the event that the mbuf doesn't have a
161d4b5cae4SRobert Watson  *                      flow ID, falling back on source ordering.
162d4b5cae4SRobert Watson  *
163d4b5cae4SRobert Watson  * NETISR_POLICY_CPU - netisr will delegate all work placement decisions to
164d4b5cae4SRobert Watson  *                     the protocol, querying nh_m2cpuid for each packet.
165d4b5cae4SRobert Watson  *
166d4b5cae4SRobert Watson  * Protocols might make decisions about work placement based on an existing
167d4b5cae4SRobert Watson  * calculated flow ID on the mbuf, such as one provided in hardware, the
168d4b5cae4SRobert Watson  * receive interface pointed to by the mbuf (if any), the optional source
169d4b5cae4SRobert Watson  * identifier passed at some dispatch points, or even parse packet headers to
170d4b5cae4SRobert Watson  * calculate a flow.  Both protocol handlers may return a new mbuf pointer
171d4b5cae4SRobert Watson  * for the chain, or NULL if the packet proves invalid or m_pullup() fails.
172d4b5cae4SRobert Watson  *
173d4b5cae4SRobert Watson  * XXXRW: If we eventually support dynamic reconfiguration, there should be
174d4b5cae4SRobert Watson  * protocol handlers to notify them of CPU configuration changes so that they
175d4b5cae4SRobert Watson  * can rebalance work.
176d4b5cae4SRobert Watson  */
1771cafed39SJonathan Lemon struct mbuf;
178d4b5cae4SRobert Watson typedef void		 netisr_handler_t(struct mbuf *m);
179d4b5cae4SRobert Watson typedef struct mbuf	*netisr_m2cpuid_t(struct mbuf *m, uintptr_t source,
180d4b5cae4SRobert Watson 			 u_int *cpuid);
181d4b5cae4SRobert Watson typedef	struct mbuf	*netisr_m2flow_t(struct mbuf *m, uintptr_t source);
182ed655c8cSBjoern A. Zeeb typedef void		 netisr_drainedcpu_t(u_int cpuid);
183748e0b0aSGarrett Wollman 
184f2d2d694SRobert Watson #define	NETISR_CPUID_NONE	((u_int)-1)	/* No affinity returned. */
185f2d2d694SRobert Watson 
186d4b5cae4SRobert Watson /*
187d4b5cae4SRobert Watson  * Data structure describing a protocol handler.
188d4b5cae4SRobert Watson  */
189d4b5cae4SRobert Watson struct netisr_handler {
190d4b5cae4SRobert Watson 	const char	*nh_name;	/* Character string protocol name. */
191d4b5cae4SRobert Watson 	netisr_handler_t *nh_handler;	/* Protocol handler. */
192d4b5cae4SRobert Watson 	netisr_m2flow_t	*nh_m2flow;	/* Query flow for untagged packet. */
193d4b5cae4SRobert Watson 	netisr_m2cpuid_t *nh_m2cpuid;	/* Query CPU to process mbuf on. */
194ed655c8cSBjoern A. Zeeb 	netisr_drainedcpu_t *nh_drainedcpu; /* Callback when drained a queue. */
195d4b5cae4SRobert Watson 	u_int		 nh_proto;	/* Integer protocol ID. */
196d4b5cae4SRobert Watson 	u_int		 nh_qlimit;	/* Maximum per-CPU queue depth. */
197d4b5cae4SRobert Watson 	u_int		 nh_policy;	/* Work placement policy. */
198f2d2d694SRobert Watson 	u_int		 nh_dispatch;	/* Dispatch policy. */
199f2d2d694SRobert Watson 	u_int		 nh_ispare[4];	/* For future use. */
200d4b5cae4SRobert Watson 	void		*nh_pspare[4];	/* For future use. */
201d4b5cae4SRobert Watson };
20206cc1858SPeter Wemm 
203d4b5cae4SRobert Watson /*
204d4b5cae4SRobert Watson  * Register, unregister, and other netisr handler management functions.
205d4b5cae4SRobert Watson  */
206d4b5cae4SRobert Watson void	netisr_clearqdrops(const struct netisr_handler *nhp);
207d4b5cae4SRobert Watson void	netisr_getqdrops(const struct netisr_handler *nhp,
208d4b5cae4SRobert Watson 	    u_int64_t *qdropsp);
209d4b5cae4SRobert Watson void	netisr_getqlimit(const struct netisr_handler *nhp, u_int *qlimitp);
210d4b5cae4SRobert Watson void	netisr_register(const struct netisr_handler *nhp);
211d4b5cae4SRobert Watson int	netisr_setqlimit(const struct netisr_handler *nhp, u_int qlimit);
212d4b5cae4SRobert Watson void	netisr_unregister(const struct netisr_handler *nhp);
213*484149deSBjoern A. Zeeb #ifdef VIMAGE
214*484149deSBjoern A. Zeeb void	netisr_register_vnet(const struct netisr_handler *nhp);
215*484149deSBjoern A. Zeeb void	netisr_unregister_vnet(const struct netisr_handler *nhp);
216*484149deSBjoern A. Zeeb #endif
217cea1da3bSPaul Richards 
218d4b5cae4SRobert Watson /*
219d4b5cae4SRobert Watson  * Process a packet destined for a protocol, and attempt direct dispatch.
220d4b5cae4SRobert Watson  * Supplemental source ordering information can be passed using the _src
221d4b5cae4SRobert Watson  * variant.
222d4b5cae4SRobert Watson  */
223d4b5cae4SRobert Watson int	netisr_dispatch(u_int proto, struct mbuf *m);
224d4b5cae4SRobert Watson int	netisr_dispatch_src(u_int proto, uintptr_t source, struct mbuf *m);
225d4b5cae4SRobert Watson int	netisr_queue(u_int proto, struct mbuf *m);
226d4b5cae4SRobert Watson int	netisr_queue_src(u_int proto, uintptr_t source, struct mbuf *m);
227d4b5cae4SRobert Watson 
228d4b5cae4SRobert Watson /*
229d4b5cae4SRobert Watson  * Provide a default implementation of "map an ID to a CPU ID".
230d4b5cae4SRobert Watson  */
231d4b5cae4SRobert Watson u_int	netisr_default_flow2cpu(u_int flowid);
232d4b5cae4SRobert Watson 
233d4b5cae4SRobert Watson /*
234d4b5cae4SRobert Watson  * Utility routines to return the number of CPUs participting in netisr, and
235d4b5cae4SRobert Watson  * to return a mapping from a number to a CPU ID that can be used with the
236d4b5cae4SRobert Watson  * scheduler.
237d4b5cae4SRobert Watson  */
238d4b5cae4SRobert Watson u_int	netisr_get_cpucount(void);
239d4b5cae4SRobert Watson u_int	netisr_get_cpuid(u_int cpunumber);
240d4b5cae4SRobert Watson 
241d4b5cae4SRobert Watson /*
242d4b5cae4SRobert Watson  * Interfaces between DEVICE_POLLING and netisr.
243d4b5cae4SRobert Watson  */
244d4b5cae4SRobert Watson void	netisr_sched_poll(void);
245d4b5cae4SRobert Watson void	netisr_poll(void);
246d4b5cae4SRobert Watson void	netisr_pollmore(void);
247d4b5cae4SRobert Watson 
248d4b5cae4SRobert Watson #endif /* !_KERNEL */
249d4b5cae4SRobert Watson #endif /* !_NET_NETISR_H_ */
250