xref: /freebsd/sys/net/netisr.h (revision 45c203fce2d02381f76b2abf80edaea8cf7bc7f8)
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 */
55*45c203fcSGleb Smirnoff #define	NETISR_ARP	4		/* same as AF_LINK */
56*45c203fcSGleb Smirnoff #define	NETISR_ETHER	5		/* ethernet input */
57*45c203fcSGleb Smirnoff #define	NETISR_IPV6	6
58*45c203fcSGleb Smirnoff #define	NETISR_NATM	7
59*45c203fcSGleb Smirnoff #define	NETISR_EPAIR	8		/* if_epair(4) */
60df8bae1dSRodney W. Grimes 
612d22f334SRobert Watson /*
622d22f334SRobert Watson  * Protocol ordering and affinity policy constants.  See the detailed
632d22f334SRobert Watson  * discussion of policies later in the file.
642d22f334SRobert Watson  */
652d22f334SRobert Watson #define	NETISR_POLICY_SOURCE	1	/* Maintain source ordering. */
662d22f334SRobert Watson #define	NETISR_POLICY_FLOW	2	/* Maintain flow ordering. */
672d22f334SRobert Watson #define	NETISR_POLICY_CPU	3	/* Protocol determines CPU placement. */
682d22f334SRobert Watson 
692d22f334SRobert Watson /*
70f2d2d694SRobert Watson  * Protocol dispatch policy constants; selects whether and when direct
71f2d2d694SRobert Watson  * dispatch is permitted.
72f2d2d694SRobert Watson  */
73f2d2d694SRobert Watson #define	NETISR_DISPATCH_DEFAULT		0	/* Use global default. */
74f2d2d694SRobert Watson #define	NETISR_DISPATCH_DEFERRED	1	/* Always defer dispatch. */
75f2d2d694SRobert Watson #define	NETISR_DISPATCH_HYBRID		2	/* Allow hybrid dispatch. */
76f2d2d694SRobert Watson #define	NETISR_DISPATCH_DIRECT		3	/* Always direct dispatch. */
77f2d2d694SRobert Watson 
78f2d2d694SRobert Watson /*
792d22f334SRobert Watson  * Monitoring data structures, exported by sysctl(2).
802d22f334SRobert Watson  *
812d22f334SRobert Watson  * Three sysctls are defined.  First, a per-protocol structure exported by
822d22f334SRobert Watson  * net.isr.proto.
832d22f334SRobert Watson  */
842d22f334SRobert Watson #define	NETISR_NAMEMAXLEN	32
852d22f334SRobert Watson struct sysctl_netisr_proto {
862d22f334SRobert Watson 	u_int	snp_version;			/* Length of struct. */
872d22f334SRobert Watson 	char	snp_name[NETISR_NAMEMAXLEN];	/* nh_name */
882d22f334SRobert Watson 	u_int	snp_proto;			/* nh_proto */
892d22f334SRobert Watson 	u_int	snp_qlimit;			/* nh_qlimit */
902d22f334SRobert Watson 	u_int	snp_policy;			/* nh_policy */
912d22f334SRobert Watson 	u_int	snp_flags;			/* Various flags. */
92f2d2d694SRobert Watson 	u_int	snp_dispatch;			/* Dispatch policy. */
93f2d2d694SRobert Watson 	u_int	_snp_ispare[6];
942d22f334SRobert Watson };
952d22f334SRobert Watson 
962d22f334SRobert Watson /*
972d22f334SRobert Watson  * Flags for sysctl_netisr_proto.snp_flags.
982d22f334SRobert Watson  */
992d22f334SRobert Watson #define	NETISR_SNP_FLAGS_M2FLOW		0x00000001	/* nh_m2flow */
1002d22f334SRobert Watson #define	NETISR_SNP_FLAGS_M2CPUID	0x00000002	/* nh_m2cpuid */
101c4fbf89fSRobert Watson #define	NETISR_SNP_FLAGS_DRAINEDCPU	0x00000004	/* nh_drainedcpu */
1022d22f334SRobert Watson 
1032d22f334SRobert Watson /*
1042d22f334SRobert Watson  * Next, a structure per-workstream, with per-protocol data, exported as
1052d22f334SRobert Watson  * net.isr.workstream.
1062d22f334SRobert Watson  */
1072d22f334SRobert Watson struct sysctl_netisr_workstream {
1082d22f334SRobert Watson 	u_int	snws_version;			/* Length of struct. */
1092d22f334SRobert Watson 	u_int	snws_flags;			/* Various flags. */
1102d22f334SRobert Watson 	u_int	snws_wsid;			/* Workstream ID. */
1112d22f334SRobert Watson 	u_int	snws_cpu;			/* nws_cpu */
1122d22f334SRobert Watson 	u_int	_snws_ispare[12];
1132d22f334SRobert Watson };
1142d22f334SRobert Watson 
1152d22f334SRobert Watson /*
1162d22f334SRobert Watson  * Flags for sysctl_netisr_workstream.snws_flags
1172d22f334SRobert Watson  */
1182d22f334SRobert Watson #define	NETISR_SNWS_FLAGS_INTR		0x00000001	/* nws_intr_event */
1192d22f334SRobert Watson 
1202d22f334SRobert Watson /*
1212d22f334SRobert Watson  * Finally, a per-workstream-per-protocol structure, exported as
1222d22f334SRobert Watson  * net.isr.work.
1232d22f334SRobert Watson  */
1242d22f334SRobert Watson struct sysctl_netisr_work {
1252d22f334SRobert Watson 	u_int	snw_version;			/* Length of struct. */
1262d22f334SRobert Watson 	u_int	snw_wsid;			/* Workstream ID. */
1272d22f334SRobert Watson 	u_int	snw_proto;			/* Protocol number. */
1282d22f334SRobert Watson 	u_int	snw_len;			/* nw_len */
1292d22f334SRobert Watson 	u_int	snw_watermark;			/* nw_watermark */
1302d22f334SRobert Watson 	u_int	_snw_ispare[3];
1312d22f334SRobert Watson 
1322d22f334SRobert Watson 	uint64_t	snw_dispatched;		/* nw_dispatched */
1332d22f334SRobert Watson 	uint64_t	snw_hybrid_dispatched;	/* nw_hybrid_dispatched */
1342d22f334SRobert Watson 	uint64_t	snw_qdrops;		/* nw_qdrops */
1352d22f334SRobert Watson 	uint64_t	snw_queued;		/* nw_queued */
1362d22f334SRobert Watson 	uint64_t	snw_handled;		/* nw_handled */
1372d22f334SRobert Watson 
1382d22f334SRobert Watson 	uint64_t	_snw_llspare[7];
1392d22f334SRobert Watson };
1402d22f334SRobert Watson 
1412d22f334SRobert Watson #ifdef _KERNEL
1422d22f334SRobert Watson 
143d4b5cae4SRobert Watson /*-
144d4b5cae4SRobert Watson  * Protocols express ordering constraints and affinity preferences by
145d4b5cae4SRobert Watson  * implementing one or neither of nh_m2flow and nh_m2cpuid, which are used by
146d4b5cae4SRobert Watson  * netisr to determine which per-CPU workstream to assign mbufs to.
147d4b5cae4SRobert Watson  *
148d4b5cae4SRobert Watson  * The following policies may be used by protocols:
149d4b5cae4SRobert Watson  *
150d4b5cae4SRobert Watson  * NETISR_POLICY_SOURCE - netisr should maintain source ordering without
151d4b5cae4SRobert Watson  *                        advice from the protocol.  netisr will ignore any
152d4b5cae4SRobert Watson  *                        flow IDs present on the mbuf for the purposes of
153d4b5cae4SRobert Watson  *                        work placement.
154d4b5cae4SRobert Watson  *
155d4b5cae4SRobert Watson  * NETISR_POLICY_FLOW - netisr should maintain flow ordering as defined by
156d4b5cae4SRobert Watson  *                      the mbuf header flow ID field.  If the protocol
157d4b5cae4SRobert Watson  *                      implements nh_m2flow, then netisr will query the
158d4b5cae4SRobert Watson  *                      protocol in the event that the mbuf doesn't have a
159d4b5cae4SRobert Watson  *                      flow ID, falling back on source ordering.
160d4b5cae4SRobert Watson  *
161d4b5cae4SRobert Watson  * NETISR_POLICY_CPU - netisr will delegate all work placement decisions to
162d4b5cae4SRobert Watson  *                     the protocol, querying nh_m2cpuid for each packet.
163d4b5cae4SRobert Watson  *
164d4b5cae4SRobert Watson  * Protocols might make decisions about work placement based on an existing
165d4b5cae4SRobert Watson  * calculated flow ID on the mbuf, such as one provided in hardware, the
166d4b5cae4SRobert Watson  * receive interface pointed to by the mbuf (if any), the optional source
167d4b5cae4SRobert Watson  * identifier passed at some dispatch points, or even parse packet headers to
168d4b5cae4SRobert Watson  * calculate a flow.  Both protocol handlers may return a new mbuf pointer
169d4b5cae4SRobert Watson  * for the chain, or NULL if the packet proves invalid or m_pullup() fails.
170d4b5cae4SRobert Watson  *
171d4b5cae4SRobert Watson  * XXXRW: If we eventually support dynamic reconfiguration, there should be
172d4b5cae4SRobert Watson  * protocol handlers to notify them of CPU configuration changes so that they
173d4b5cae4SRobert Watson  * can rebalance work.
174d4b5cae4SRobert Watson  */
1751cafed39SJonathan Lemon struct mbuf;
176d4b5cae4SRobert Watson typedef void		 netisr_handler_t(struct mbuf *m);
177d4b5cae4SRobert Watson typedef struct mbuf	*netisr_m2cpuid_t(struct mbuf *m, uintptr_t source,
178d4b5cae4SRobert Watson 			 u_int *cpuid);
179d4b5cae4SRobert Watson typedef	struct mbuf	*netisr_m2flow_t(struct mbuf *m, uintptr_t source);
180ed655c8cSBjoern A. Zeeb typedef void		 netisr_drainedcpu_t(u_int cpuid);
181748e0b0aSGarrett Wollman 
182f2d2d694SRobert Watson #define	NETISR_CPUID_NONE	((u_int)-1)	/* No affinity returned. */
183f2d2d694SRobert Watson 
184d4b5cae4SRobert Watson /*
185d4b5cae4SRobert Watson  * Data structure describing a protocol handler.
186d4b5cae4SRobert Watson  */
187d4b5cae4SRobert Watson struct netisr_handler {
188d4b5cae4SRobert Watson 	const char	*nh_name;	/* Character string protocol name. */
189d4b5cae4SRobert Watson 	netisr_handler_t *nh_handler;	/* Protocol handler. */
190d4b5cae4SRobert Watson 	netisr_m2flow_t	*nh_m2flow;	/* Query flow for untagged packet. */
191d4b5cae4SRobert Watson 	netisr_m2cpuid_t *nh_m2cpuid;	/* Query CPU to process mbuf on. */
192ed655c8cSBjoern A. Zeeb 	netisr_drainedcpu_t *nh_drainedcpu; /* Callback when drained a queue. */
193d4b5cae4SRobert Watson 	u_int		 nh_proto;	/* Integer protocol ID. */
194d4b5cae4SRobert Watson 	u_int		 nh_qlimit;	/* Maximum per-CPU queue depth. */
195d4b5cae4SRobert Watson 	u_int		 nh_policy;	/* Work placement policy. */
196f2d2d694SRobert Watson 	u_int		 nh_dispatch;	/* Dispatch policy. */
197f2d2d694SRobert Watson 	u_int		 nh_ispare[4];	/* For future use. */
198d4b5cae4SRobert Watson 	void		*nh_pspare[4];	/* For future use. */
199d4b5cae4SRobert Watson };
20006cc1858SPeter Wemm 
201d4b5cae4SRobert Watson /*
202d4b5cae4SRobert Watson  * Register, unregister, and other netisr handler management functions.
203d4b5cae4SRobert Watson  */
204d4b5cae4SRobert Watson void	netisr_clearqdrops(const struct netisr_handler *nhp);
205d4b5cae4SRobert Watson void	netisr_getqdrops(const struct netisr_handler *nhp,
206d4b5cae4SRobert Watson 	    u_int64_t *qdropsp);
207d4b5cae4SRobert Watson void	netisr_getqlimit(const struct netisr_handler *nhp, u_int *qlimitp);
208d4b5cae4SRobert Watson void	netisr_register(const struct netisr_handler *nhp);
209d4b5cae4SRobert Watson int	netisr_setqlimit(const struct netisr_handler *nhp, u_int qlimit);
210d4b5cae4SRobert Watson void	netisr_unregister(const struct netisr_handler *nhp);
211cea1da3bSPaul Richards 
212d4b5cae4SRobert Watson /*
213d4b5cae4SRobert Watson  * Process a packet destined for a protocol, and attempt direct dispatch.
214d4b5cae4SRobert Watson  * Supplemental source ordering information can be passed using the _src
215d4b5cae4SRobert Watson  * variant.
216d4b5cae4SRobert Watson  */
217d4b5cae4SRobert Watson int	netisr_dispatch(u_int proto, struct mbuf *m);
218d4b5cae4SRobert Watson int	netisr_dispatch_src(u_int proto, uintptr_t source, struct mbuf *m);
219d4b5cae4SRobert Watson int	netisr_queue(u_int proto, struct mbuf *m);
220d4b5cae4SRobert Watson int	netisr_queue_src(u_int proto, uintptr_t source, struct mbuf *m);
221d4b5cae4SRobert Watson 
222d4b5cae4SRobert Watson /*
223d4b5cae4SRobert Watson  * Provide a default implementation of "map an ID to a CPU ID".
224d4b5cae4SRobert Watson  */
225d4b5cae4SRobert Watson u_int	netisr_default_flow2cpu(u_int flowid);
226d4b5cae4SRobert Watson 
227d4b5cae4SRobert Watson /*
228d4b5cae4SRobert Watson  * Utility routines to return the number of CPUs participting in netisr, and
229d4b5cae4SRobert Watson  * to return a mapping from a number to a CPU ID that can be used with the
230d4b5cae4SRobert Watson  * scheduler.
231d4b5cae4SRobert Watson  */
232d4b5cae4SRobert Watson u_int	netisr_get_cpucount(void);
233d4b5cae4SRobert Watson u_int	netisr_get_cpuid(u_int cpunumber);
234d4b5cae4SRobert Watson 
235d4b5cae4SRobert Watson /*
236d4b5cae4SRobert Watson  * Interfaces between DEVICE_POLLING and netisr.
237d4b5cae4SRobert Watson  */
238d4b5cae4SRobert Watson void	netisr_sched_poll(void);
239d4b5cae4SRobert Watson void	netisr_poll(void);
240d4b5cae4SRobert Watson void	netisr_pollmore(void);
241d4b5cae4SRobert Watson 
242d4b5cae4SRobert Watson #endif /* !_KERNEL */
243d4b5cae4SRobert Watson #endif /* !_NET_NETISR_H_ */
244