xref: /f-stack/tools/compat/include/net/netisr.h (revision 1eaf0ac3)
1*1eaf0ac3Slogwang /*-
2*1eaf0ac3Slogwang  * Copyright (c) 2007-2009 Robert N. M. Watson
3*1eaf0ac3Slogwang  * Copyright (c) 2010-2011 Juniper Networks, Inc.
4*1eaf0ac3Slogwang  * All rights reserved.
5*1eaf0ac3Slogwang  *
6*1eaf0ac3Slogwang  * This software was developed by Robert N. M. Watson under contract
7*1eaf0ac3Slogwang  * to Juniper Networks, Inc.
8*1eaf0ac3Slogwang  *
9*1eaf0ac3Slogwang  * Redistribution and use in source and binary forms, with or without
10*1eaf0ac3Slogwang  * modification, are permitted provided that the following conditions
11*1eaf0ac3Slogwang  * are met:
12*1eaf0ac3Slogwang  * 1. Redistributions of source code must retain the above copyright
13*1eaf0ac3Slogwang  *    notice, this list of conditions and the following disclaimer.
14*1eaf0ac3Slogwang  * 2. Redistributions in binary form must reproduce the above copyright
15*1eaf0ac3Slogwang  *    notice, this list of conditions and the following disclaimer in the
16*1eaf0ac3Slogwang  *    documentation and/or other materials provided with the distribution.
17*1eaf0ac3Slogwang  *
18*1eaf0ac3Slogwang  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19*1eaf0ac3Slogwang  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*1eaf0ac3Slogwang  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*1eaf0ac3Slogwang  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22*1eaf0ac3Slogwang  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23*1eaf0ac3Slogwang  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24*1eaf0ac3Slogwang  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25*1eaf0ac3Slogwang  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26*1eaf0ac3Slogwang  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27*1eaf0ac3Slogwang  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28*1eaf0ac3Slogwang  * SUCH DAMAGE.
29*1eaf0ac3Slogwang  *
30*1eaf0ac3Slogwang  * $FreeBSD$
31*1eaf0ac3Slogwang  */
32*1eaf0ac3Slogwang 
33*1eaf0ac3Slogwang #ifndef _NET_NETISR_H_
34*1eaf0ac3Slogwang #define _NET_NETISR_H_
35*1eaf0ac3Slogwang 
36*1eaf0ac3Slogwang /*
37*1eaf0ac3Slogwang  * The netisr (network interrupt service routine) provides a deferred
38*1eaf0ac3Slogwang  * execution evironment in which (generally inbound) network processing can
39*1eaf0ac3Slogwang  * take place.  Protocols register handlers which will be executed directly,
40*1eaf0ac3Slogwang  * or via deferred dispatch, depending on the circumstances.
41*1eaf0ac3Slogwang  *
42*1eaf0ac3Slogwang  * Historically, this was implemented by the BSD software ISR facility; it is
43*1eaf0ac3Slogwang  * now implemented via a software ithread (SWI).
44*1eaf0ac3Slogwang  */
45*1eaf0ac3Slogwang 
46*1eaf0ac3Slogwang /*
47*1eaf0ac3Slogwang  * Protocol numbers, which are encoded in monitoring applications and kernel
48*1eaf0ac3Slogwang  * modules.  Internally, these are used in bit shift operations so must have
49*1eaf0ac3Slogwang  * a value 0 < proto < 32; we currently further limit at compile-time to 16
50*1eaf0ac3Slogwang  * for array-sizing purposes.
51*1eaf0ac3Slogwang  */
52*1eaf0ac3Slogwang #define	NETISR_IP	1
53*1eaf0ac3Slogwang #define	NETISR_IGMP	2		/* IGMPv3 output queue */
54*1eaf0ac3Slogwang #define	NETISR_ROUTE	3		/* routing socket */
55*1eaf0ac3Slogwang #define	NETISR_ARP	4		/* same as AF_LINK */
56*1eaf0ac3Slogwang #define	NETISR_ETHER	5		/* ethernet input */
57*1eaf0ac3Slogwang #define	NETISR_IPV6	6
58*1eaf0ac3Slogwang #define	NETISR_NATM	7
59*1eaf0ac3Slogwang #define	NETISR_EPAIR	8		/* if_epair(4) */
60*1eaf0ac3Slogwang #define	NETISR_IP_DIRECT	9	/* direct-dispatch IPv4 */
61*1eaf0ac3Slogwang #define	NETISR_IPV6_DIRECT	10	/* direct-dispatch IPv6 */
62*1eaf0ac3Slogwang 
63*1eaf0ac3Slogwang /*
64*1eaf0ac3Slogwang  * Protocol ordering and affinity policy constants.  See the detailed
65*1eaf0ac3Slogwang  * discussion of policies later in the file.
66*1eaf0ac3Slogwang  */
67*1eaf0ac3Slogwang #define	NETISR_POLICY_SOURCE	1	/* Maintain source ordering. */
68*1eaf0ac3Slogwang #define	NETISR_POLICY_FLOW	2	/* Maintain flow ordering. */
69*1eaf0ac3Slogwang #define	NETISR_POLICY_CPU	3	/* Protocol determines CPU placement. */
70*1eaf0ac3Slogwang 
71*1eaf0ac3Slogwang /*
72*1eaf0ac3Slogwang  * Protocol dispatch policy constants; selects whether and when direct
73*1eaf0ac3Slogwang  * dispatch is permitted.
74*1eaf0ac3Slogwang  */
75*1eaf0ac3Slogwang #define	NETISR_DISPATCH_DEFAULT		0	/* Use global default. */
76*1eaf0ac3Slogwang #define	NETISR_DISPATCH_DEFERRED	1	/* Always defer dispatch. */
77*1eaf0ac3Slogwang #define	NETISR_DISPATCH_HYBRID		2	/* Allow hybrid dispatch. */
78*1eaf0ac3Slogwang #define	NETISR_DISPATCH_DIRECT		3	/* Always direct dispatch. */
79*1eaf0ac3Slogwang 
80*1eaf0ac3Slogwang /*
81*1eaf0ac3Slogwang  * Monitoring data structures, exported by sysctl(2).
82*1eaf0ac3Slogwang  *
83*1eaf0ac3Slogwang  * Three sysctls are defined.  First, a per-protocol structure exported by
84*1eaf0ac3Slogwang  * net.isr.proto.
85*1eaf0ac3Slogwang  */
86*1eaf0ac3Slogwang #define	NETISR_NAMEMAXLEN	32
87*1eaf0ac3Slogwang struct sysctl_netisr_proto {
88*1eaf0ac3Slogwang 	u_int	snp_version;			/* Length of struct. */
89*1eaf0ac3Slogwang 	char	snp_name[NETISR_NAMEMAXLEN];	/* nh_name */
90*1eaf0ac3Slogwang 	u_int	snp_proto;			/* nh_proto */
91*1eaf0ac3Slogwang 	u_int	snp_qlimit;			/* nh_qlimit */
92*1eaf0ac3Slogwang 	u_int	snp_policy;			/* nh_policy */
93*1eaf0ac3Slogwang 	u_int	snp_flags;			/* Various flags. */
94*1eaf0ac3Slogwang 	u_int	snp_dispatch;			/* Dispatch policy. */
95*1eaf0ac3Slogwang 	u_int	_snp_ispare[6];
96*1eaf0ac3Slogwang };
97*1eaf0ac3Slogwang 
98*1eaf0ac3Slogwang /*
99*1eaf0ac3Slogwang  * Flags for sysctl_netisr_proto.snp_flags.
100*1eaf0ac3Slogwang  */
101*1eaf0ac3Slogwang #define	NETISR_SNP_FLAGS_M2FLOW		0x00000001	/* nh_m2flow */
102*1eaf0ac3Slogwang #define	NETISR_SNP_FLAGS_M2CPUID	0x00000002	/* nh_m2cpuid */
103*1eaf0ac3Slogwang #define	NETISR_SNP_FLAGS_DRAINEDCPU	0x00000004	/* nh_drainedcpu */
104*1eaf0ac3Slogwang 
105*1eaf0ac3Slogwang /*
106*1eaf0ac3Slogwang  * Next, a structure per-workstream, with per-protocol data, exported as
107*1eaf0ac3Slogwang  * net.isr.workstream.
108*1eaf0ac3Slogwang  */
109*1eaf0ac3Slogwang struct sysctl_netisr_workstream {
110*1eaf0ac3Slogwang 	u_int	snws_version;			/* Length of struct. */
111*1eaf0ac3Slogwang 	u_int	snws_flags;			/* Various flags. */
112*1eaf0ac3Slogwang 	u_int	snws_wsid;			/* Workstream ID. */
113*1eaf0ac3Slogwang 	u_int	snws_cpu;			/* nws_cpu */
114*1eaf0ac3Slogwang 	u_int	_snws_ispare[12];
115*1eaf0ac3Slogwang };
116*1eaf0ac3Slogwang 
117*1eaf0ac3Slogwang /*
118*1eaf0ac3Slogwang  * Flags for sysctl_netisr_workstream.snws_flags
119*1eaf0ac3Slogwang  */
120*1eaf0ac3Slogwang #define	NETISR_SNWS_FLAGS_INTR		0x00000001	/* nws_intr_event */
121*1eaf0ac3Slogwang 
122*1eaf0ac3Slogwang /*
123*1eaf0ac3Slogwang  * Finally, a per-workstream-per-protocol structure, exported as
124*1eaf0ac3Slogwang  * net.isr.work.
125*1eaf0ac3Slogwang  */
126*1eaf0ac3Slogwang struct sysctl_netisr_work {
127*1eaf0ac3Slogwang 	u_int	snw_version;			/* Length of struct. */
128*1eaf0ac3Slogwang 	u_int	snw_wsid;			/* Workstream ID. */
129*1eaf0ac3Slogwang 	u_int	snw_proto;			/* Protocol number. */
130*1eaf0ac3Slogwang 	u_int	snw_len;			/* nw_len */
131*1eaf0ac3Slogwang 	u_int	snw_watermark;			/* nw_watermark */
132*1eaf0ac3Slogwang 	u_int	_snw_ispare[3];
133*1eaf0ac3Slogwang 
134*1eaf0ac3Slogwang 	uint64_t	snw_dispatched;		/* nw_dispatched */
135*1eaf0ac3Slogwang 	uint64_t	snw_hybrid_dispatched;	/* nw_hybrid_dispatched */
136*1eaf0ac3Slogwang 	uint64_t	snw_qdrops;		/* nw_qdrops */
137*1eaf0ac3Slogwang 	uint64_t	snw_queued;		/* nw_queued */
138*1eaf0ac3Slogwang 	uint64_t	snw_handled;		/* nw_handled */
139*1eaf0ac3Slogwang 
140*1eaf0ac3Slogwang 	uint64_t	_snw_llspare[7];
141*1eaf0ac3Slogwang };
142*1eaf0ac3Slogwang 
143*1eaf0ac3Slogwang #ifdef _KERNEL
144*1eaf0ac3Slogwang 
145*1eaf0ac3Slogwang /*-
146*1eaf0ac3Slogwang  * Protocols express ordering constraints and affinity preferences by
147*1eaf0ac3Slogwang  * implementing one or neither of nh_m2flow and nh_m2cpuid, which are used by
148*1eaf0ac3Slogwang  * netisr to determine which per-CPU workstream to assign mbufs to.
149*1eaf0ac3Slogwang  *
150*1eaf0ac3Slogwang  * The following policies may be used by protocols:
151*1eaf0ac3Slogwang  *
152*1eaf0ac3Slogwang  * NETISR_POLICY_SOURCE - netisr should maintain source ordering without
153*1eaf0ac3Slogwang  *                        advice from the protocol.  netisr will ignore any
154*1eaf0ac3Slogwang  *                        flow IDs present on the mbuf for the purposes of
155*1eaf0ac3Slogwang  *                        work placement.
156*1eaf0ac3Slogwang  *
157*1eaf0ac3Slogwang  * NETISR_POLICY_FLOW - netisr should maintain flow ordering as defined by
158*1eaf0ac3Slogwang  *                      the mbuf header flow ID field.  If the protocol
159*1eaf0ac3Slogwang  *                      implements nh_m2flow, then netisr will query the
160*1eaf0ac3Slogwang  *                      protocol in the event that the mbuf doesn't have a
161*1eaf0ac3Slogwang  *                      flow ID, falling back on source ordering.
162*1eaf0ac3Slogwang  *
163*1eaf0ac3Slogwang  * NETISR_POLICY_CPU - netisr will delegate all work placement decisions to
164*1eaf0ac3Slogwang  *                     the protocol, querying nh_m2cpuid for each packet.
165*1eaf0ac3Slogwang  *
166*1eaf0ac3Slogwang  * Protocols might make decisions about work placement based on an existing
167*1eaf0ac3Slogwang  * calculated flow ID on the mbuf, such as one provided in hardware, the
168*1eaf0ac3Slogwang  * receive interface pointed to by the mbuf (if any), the optional source
169*1eaf0ac3Slogwang  * identifier passed at some dispatch points, or even parse packet headers to
170*1eaf0ac3Slogwang  * calculate a flow.  Both protocol handlers may return a new mbuf pointer
171*1eaf0ac3Slogwang  * for the chain, or NULL if the packet proves invalid or m_pullup() fails.
172*1eaf0ac3Slogwang  *
173*1eaf0ac3Slogwang  * XXXRW: If we eventually support dynamic reconfiguration, there should be
174*1eaf0ac3Slogwang  * protocol handlers to notify them of CPU configuration changes so that they
175*1eaf0ac3Slogwang  * can rebalance work.
176*1eaf0ac3Slogwang  */
177*1eaf0ac3Slogwang struct mbuf;
178*1eaf0ac3Slogwang typedef void		 netisr_handler_t(struct mbuf *m);
179*1eaf0ac3Slogwang typedef struct mbuf	*netisr_m2cpuid_t(struct mbuf *m, uintptr_t source,
180*1eaf0ac3Slogwang 			 u_int *cpuid);
181*1eaf0ac3Slogwang typedef	struct mbuf	*netisr_m2flow_t(struct mbuf *m, uintptr_t source);
182*1eaf0ac3Slogwang typedef void		 netisr_drainedcpu_t(u_int cpuid);
183*1eaf0ac3Slogwang 
184*1eaf0ac3Slogwang #define	NETISR_CPUID_NONE	((u_int)-1)	/* No affinity returned. */
185*1eaf0ac3Slogwang 
186*1eaf0ac3Slogwang /*
187*1eaf0ac3Slogwang  * Data structure describing a protocol handler.
188*1eaf0ac3Slogwang  */
189*1eaf0ac3Slogwang struct netisr_handler {
190*1eaf0ac3Slogwang 	const char	*nh_name;	/* Character string protocol name. */
191*1eaf0ac3Slogwang 	netisr_handler_t *nh_handler;	/* Protocol handler. */
192*1eaf0ac3Slogwang 	netisr_m2flow_t	*nh_m2flow;	/* Query flow for untagged packet. */
193*1eaf0ac3Slogwang 	netisr_m2cpuid_t *nh_m2cpuid;	/* Query CPU to process mbuf on. */
194*1eaf0ac3Slogwang 	netisr_drainedcpu_t *nh_drainedcpu; /* Callback when drained a queue. */
195*1eaf0ac3Slogwang 	u_int		 nh_proto;	/* Integer protocol ID. */
196*1eaf0ac3Slogwang 	u_int		 nh_qlimit;	/* Maximum per-CPU queue depth. */
197*1eaf0ac3Slogwang 	u_int		 nh_policy;	/* Work placement policy. */
198*1eaf0ac3Slogwang 	u_int		 nh_dispatch;	/* Dispatch policy. */
199*1eaf0ac3Slogwang 	u_int		 nh_ispare[4];	/* For future use. */
200*1eaf0ac3Slogwang 	void		*nh_pspare[4];	/* For future use. */
201*1eaf0ac3Slogwang };
202*1eaf0ac3Slogwang 
203*1eaf0ac3Slogwang /*
204*1eaf0ac3Slogwang  * Register, unregister, and other netisr handler management functions.
205*1eaf0ac3Slogwang  */
206*1eaf0ac3Slogwang void	netisr_clearqdrops(const struct netisr_handler *nhp);
207*1eaf0ac3Slogwang void	netisr_getqdrops(const struct netisr_handler *nhp,
208*1eaf0ac3Slogwang 	    u_int64_t *qdropsp);
209*1eaf0ac3Slogwang void	netisr_getqlimit(const struct netisr_handler *nhp, u_int *qlimitp);
210*1eaf0ac3Slogwang void	netisr_register(const struct netisr_handler *nhp);
211*1eaf0ac3Slogwang int	netisr_setqlimit(const struct netisr_handler *nhp, u_int qlimit);
212*1eaf0ac3Slogwang void	netisr_unregister(const struct netisr_handler *nhp);
213*1eaf0ac3Slogwang #ifdef VIMAGE
214*1eaf0ac3Slogwang void	netisr_register_vnet(const struct netisr_handler *nhp);
215*1eaf0ac3Slogwang void	netisr_unregister_vnet(const struct netisr_handler *nhp);
216*1eaf0ac3Slogwang #endif
217*1eaf0ac3Slogwang 
218*1eaf0ac3Slogwang /*
219*1eaf0ac3Slogwang  * Process a packet destined for a protocol, and attempt direct dispatch.
220*1eaf0ac3Slogwang  * Supplemental source ordering information can be passed using the _src
221*1eaf0ac3Slogwang  * variant.
222*1eaf0ac3Slogwang  */
223*1eaf0ac3Slogwang int	netisr_dispatch(u_int proto, struct mbuf *m);
224*1eaf0ac3Slogwang int	netisr_dispatch_src(u_int proto, uintptr_t source, struct mbuf *m);
225*1eaf0ac3Slogwang int	netisr_queue(u_int proto, struct mbuf *m);
226*1eaf0ac3Slogwang int	netisr_queue_src(u_int proto, uintptr_t source, struct mbuf *m);
227*1eaf0ac3Slogwang 
228*1eaf0ac3Slogwang /*
229*1eaf0ac3Slogwang  * Provide a default implementation of "map an ID to a CPU ID".
230*1eaf0ac3Slogwang  */
231*1eaf0ac3Slogwang u_int	netisr_default_flow2cpu(u_int flowid);
232*1eaf0ac3Slogwang 
233*1eaf0ac3Slogwang /*
234*1eaf0ac3Slogwang  * Utility routines to return the number of CPUs participting in netisr, and
235*1eaf0ac3Slogwang  * to return a mapping from a number to a CPU ID that can be used with the
236*1eaf0ac3Slogwang  * scheduler.
237*1eaf0ac3Slogwang  */
238*1eaf0ac3Slogwang u_int	netisr_get_cpucount(void);
239*1eaf0ac3Slogwang u_int	netisr_get_cpuid(u_int cpunumber);
240*1eaf0ac3Slogwang 
241*1eaf0ac3Slogwang /*
242*1eaf0ac3Slogwang  * Interfaces between DEVICE_POLLING and netisr.
243*1eaf0ac3Slogwang  */
244*1eaf0ac3Slogwang void	netisr_sched_poll(void);
245*1eaf0ac3Slogwang void	netisr_poll(void);
246*1eaf0ac3Slogwang void	netisr_pollmore(void);
247*1eaf0ac3Slogwang 
248*1eaf0ac3Slogwang #endif /* !_KERNEL */
249*1eaf0ac3Slogwang #endif /* !_NET_NETISR_H_ */
250