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