1139823Simp/*-
2193219Srwatson * Copyright (c) 2007-2009 Robert N. M. Watson
3222249Srwatson * Copyright (c) 2010-2011 Juniper Networks, Inc.
4193219Srwatson * All rights reserved.
51541Srgrimes *
6204199Srwatson * This software was developed by Robert N. M. Watson under contract
7204199Srwatson * to Juniper Networks, Inc.
8204199Srwatson *
91541Srgrimes * Redistribution and use in source and binary forms, with or without
101541Srgrimes * modification, are permitted provided that the following conditions
111541Srgrimes * are met:
121541Srgrimes * 1. Redistributions of source code must retain the above copyright
131541Srgrimes *    notice, this list of conditions and the following disclaimer.
141541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
151541Srgrimes *    notice, this list of conditions and the following disclaimer in the
161541Srgrimes *    documentation and/or other materials provided with the distribution.
171541Srgrimes *
18193219Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
191541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
201541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21193219Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
221541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
231541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
241541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
251541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
261541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
271541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
281541Srgrimes * SUCH DAMAGE.
291541Srgrimes *
3050477Speter * $FreeBSD$
311541Srgrimes */
321541Srgrimes
332168Spaul#ifndef _NET_NETISR_H_
342168Spaul#define _NET_NETISR_H_
352168Spaul
361541Srgrimes/*
37175018Srwatson * The netisr (network interrupt service routine) provides a deferred
38175018Srwatson * execution evironment in which (generally inbound) network processing can
39193219Srwatson * take place.  Protocols register handlers which will be executed directly,
40193219Srwatson * or via deferred dispatch, depending on the circumstances.
411541Srgrimes *
42175018Srwatson * Historically, this was implemented by the BSD software ISR facility; it is
43175018Srwatson * now implemented via a software ithread (SWI).
441541Srgrimes */
45204199Srwatson
46204199Srwatson/*
47204199Srwatson * Protocol numbers, which are encoded in monitoring applications and kernel
48204199Srwatson * modules.  Internally, these are used in bit shift operations so must have
49204199Srwatson * a value 0 < proto < 32; we currently further limit at compile-time to 16
50204199Srwatson * for array-sizing purposes.
51204199Srwatson */
52193230Srwatson#define	NETISR_IP	1
53193230Srwatson#define	NETISR_IGMP	2		/* IGMPv3 output queue */
54193230Srwatson#define	NETISR_ROUTE	3		/* routing socket */
55193230Srwatson#define	NETISR_AARP	4		/* Appletalk ARP */
56193230Srwatson#define	NETISR_ATALK2	5		/* Appletalk phase 2 */
57193230Srwatson#define	NETISR_ATALK1	6		/* Appletalk phase 1 */
58193230Srwatson#define	NETISR_ARP	7		/* same as AF_LINK */
59193230Srwatson#define	NETISR_IPX	8		/* same as AF_IPX */
60193230Srwatson#define	NETISR_ETHER	9		/* ethernet input */
61193230Srwatson#define	NETISR_IPV6	10
62193230Srwatson#define	NETISR_NATM	11
63195892Sbz#define	NETISR_EPAIR	12		/* if_epair(4) */
641541Srgrimes
65204199Srwatson/*
66204199Srwatson * Protocol ordering and affinity policy constants.  See the detailed
67204199Srwatson * discussion of policies later in the file.
68204199Srwatson */
69204199Srwatson#define	NETISR_POLICY_SOURCE	1	/* Maintain source ordering. */
70204199Srwatson#define	NETISR_POLICY_FLOW	2	/* Maintain flow ordering. */
71204199Srwatson#define	NETISR_POLICY_CPU	3	/* Protocol determines CPU placement. */
72204199Srwatson
73204199Srwatson/*
74222249Srwatson * Protocol dispatch policy constants; selects whether and when direct
75222249Srwatson * dispatch is permitted.
76222249Srwatson */
77222249Srwatson#define	NETISR_DISPATCH_DEFAULT		0	/* Use global default. */
78222249Srwatson#define	NETISR_DISPATCH_DEFERRED	1	/* Always defer dispatch. */
79222249Srwatson#define	NETISR_DISPATCH_HYBRID		2	/* Allow hybrid dispatch. */
80222249Srwatson#define	NETISR_DISPATCH_DIRECT		3	/* Always direct dispatch. */
81222249Srwatson
82222249Srwatson/*
83204199Srwatson * Monitoring data structures, exported by sysctl(2).
84204199Srwatson *
85204199Srwatson * Three sysctls are defined.  First, a per-protocol structure exported by
86204199Srwatson * net.isr.proto.
87204199Srwatson */
88204199Srwatson#define	NETISR_NAMEMAXLEN	32
89204199Srwatsonstruct sysctl_netisr_proto {
90204199Srwatson	u_int	snp_version;			/* Length of struct. */
91204199Srwatson	char	snp_name[NETISR_NAMEMAXLEN];	/* nh_name */
92204199Srwatson	u_int	snp_proto;			/* nh_proto */
93204199Srwatson	u_int	snp_qlimit;			/* nh_qlimit */
94204199Srwatson	u_int	snp_policy;			/* nh_policy */
95204199Srwatson	u_int	snp_flags;			/* Various flags. */
96222249Srwatson	u_int	snp_dispatch;			/* Dispatch policy. */
97222249Srwatson	u_int	_snp_ispare[6];
98204199Srwatson};
99204199Srwatson
100204199Srwatson/*
101204199Srwatson * Flags for sysctl_netisr_proto.snp_flags.
102204199Srwatson */
103204199Srwatson#define	NETISR_SNP_FLAGS_M2FLOW		0x00000001	/* nh_m2flow */
104204199Srwatson#define	NETISR_SNP_FLAGS_M2CPUID	0x00000002	/* nh_m2cpuid */
105204208Srwatson#define	NETISR_SNP_FLAGS_DRAINEDCPU	0x00000004	/* nh_drainedcpu */
106204199Srwatson
107204199Srwatson/*
108204199Srwatson * Next, a structure per-workstream, with per-protocol data, exported as
109204199Srwatson * net.isr.workstream.
110204199Srwatson */
111204199Srwatsonstruct sysctl_netisr_workstream {
112204199Srwatson	u_int	snws_version;			/* Length of struct. */
113204199Srwatson	u_int	snws_flags;			/* Various flags. */
114204199Srwatson	u_int	snws_wsid;			/* Workstream ID. */
115204199Srwatson	u_int	snws_cpu;			/* nws_cpu */
116204199Srwatson	u_int	_snws_ispare[12];
117204199Srwatson};
118204199Srwatson
119204199Srwatson/*
120204199Srwatson * Flags for sysctl_netisr_workstream.snws_flags
121204199Srwatson */
122204199Srwatson#define	NETISR_SNWS_FLAGS_INTR		0x00000001	/* nws_intr_event */
123204199Srwatson
124204199Srwatson/*
125204199Srwatson * Finally, a per-workstream-per-protocol structure, exported as
126204199Srwatson * net.isr.work.
127204199Srwatson */
128204199Srwatsonstruct sysctl_netisr_work {
129204199Srwatson	u_int	snw_version;			/* Length of struct. */
130204199Srwatson	u_int	snw_wsid;			/* Workstream ID. */
131204199Srwatson	u_int	snw_proto;			/* Protocol number. */
132204199Srwatson	u_int	snw_len;			/* nw_len */
133204199Srwatson	u_int	snw_watermark;			/* nw_watermark */
134204199Srwatson	u_int	_snw_ispare[3];
135204199Srwatson
136204199Srwatson	uint64_t	snw_dispatched;		/* nw_dispatched */
137204199Srwatson	uint64_t	snw_hybrid_dispatched;	/* nw_hybrid_dispatched */
138204199Srwatson	uint64_t	snw_qdrops;		/* nw_qdrops */
139204199Srwatson	uint64_t	snw_queued;		/* nw_queued */
140204199Srwatson	uint64_t	snw_handled;		/* nw_handled */
141204199Srwatson
142204199Srwatson	uint64_t	_snw_llspare[7];
143204199Srwatson};
144204199Srwatson
145204199Srwatson#ifdef _KERNEL
146204199Srwatson
147193219Srwatson/*-
148193219Srwatson * Protocols express ordering constraints and affinity preferences by
149193219Srwatson * implementing one or neither of nh_m2flow and nh_m2cpuid, which are used by
150193219Srwatson * netisr to determine which per-CPU workstream to assign mbufs to.
151193219Srwatson *
152193219Srwatson * The following policies may be used by protocols:
153193219Srwatson *
154193219Srwatson * NETISR_POLICY_SOURCE - netisr should maintain source ordering without
155193219Srwatson *                        advice from the protocol.  netisr will ignore any
156193219Srwatson *                        flow IDs present on the mbuf for the purposes of
157193219Srwatson *                        work placement.
158193219Srwatson *
159193219Srwatson * NETISR_POLICY_FLOW - netisr should maintain flow ordering as defined by
160193219Srwatson *                      the mbuf header flow ID field.  If the protocol
161193219Srwatson *                      implements nh_m2flow, then netisr will query the
162193219Srwatson *                      protocol in the event that the mbuf doesn't have a
163193219Srwatson *                      flow ID, falling back on source ordering.
164193219Srwatson *
165193219Srwatson * NETISR_POLICY_CPU - netisr will delegate all work placement decisions to
166193219Srwatson *                     the protocol, querying nh_m2cpuid for each packet.
167193219Srwatson *
168193219Srwatson * Protocols might make decisions about work placement based on an existing
169193219Srwatson * calculated flow ID on the mbuf, such as one provided in hardware, the
170193219Srwatson * receive interface pointed to by the mbuf (if any), the optional source
171193219Srwatson * identifier passed at some dispatch points, or even parse packet headers to
172193219Srwatson * calculate a flow.  Both protocol handlers may return a new mbuf pointer
173193219Srwatson * for the chain, or NULL if the packet proves invalid or m_pullup() fails.
174193219Srwatson *
175193219Srwatson * XXXRW: If we eventually support dynamic reconfiguration, there should be
176193219Srwatson * protocol handlers to notify them of CPU configuration changes so that they
177193219Srwatson * can rebalance work.
178193219Srwatson */
179193219Srwatsonstruct mbuf;
180204498Srwatsontypedef void		 netisr_handler_t(struct mbuf *m);
181193219Srwatsontypedef struct mbuf	*netisr_m2cpuid_t(struct mbuf *m, uintptr_t source,
182193219Srwatson			 u_int *cpuid);
183193219Srwatsontypedef	struct mbuf	*netisr_m2flow_t(struct mbuf *m, uintptr_t source);
184194201Sbztypedef void		 netisr_drainedcpu_t(u_int cpuid);
18557178Speter
186222249Srwatson#define	NETISR_CPUID_NONE	((u_int)-1)	/* No affinity returned. */
187222249Srwatson
188193219Srwatson/*
189193219Srwatson * Data structure describing a protocol handler.
190193219Srwatson */
191193219Srwatsonstruct netisr_handler {
192193219Srwatson	const char	*nh_name;	/* Character string protocol name. */
193193219Srwatson	netisr_handler_t *nh_handler;	/* Protocol handler. */
194193219Srwatson	netisr_m2flow_t	*nh_m2flow;	/* Query flow for untagged packet. */
195193219Srwatson	netisr_m2cpuid_t *nh_m2cpuid;	/* Query CPU to process mbuf on. */
196194201Sbz	netisr_drainedcpu_t *nh_drainedcpu; /* Callback when drained a queue. */
197193219Srwatson	u_int		 nh_proto;	/* Integer protocol ID. */
198193219Srwatson	u_int		 nh_qlimit;	/* Maximum per-CPU queue depth. */
199193219Srwatson	u_int		 nh_policy;	/* Work placement policy. */
200222249Srwatson	u_int		 nh_dispatch;	/* Dispatch policy. */
201222249Srwatson	u_int		 nh_ispare[4];	/* For future use. */
202193219Srwatson	void		*nh_pspare[4];	/* For future use. */
203193219Srwatson};
2048426Swollman
205193219Srwatson/*
206193219Srwatson * Register, unregister, and other netisr handler management functions.
207193219Srwatson */
208193219Srwatsonvoid	netisr_clearqdrops(const struct netisr_handler *nhp);
209193219Srwatsonvoid	netisr_getqdrops(const struct netisr_handler *nhp,
210193219Srwatson	    u_int64_t *qdropsp);
211193219Srwatsonvoid	netisr_getqlimit(const struct netisr_handler *nhp, u_int *qlimitp);
212193219Srwatsonvoid	netisr_register(const struct netisr_handler *nhp);
213193219Srwatsonint	netisr_setqlimit(const struct netisr_handler *nhp, u_int qlimit);
214193219Srwatsonvoid	netisr_unregister(const struct netisr_handler *nhp);
2158426Swollman
216193219Srwatson/*
217193219Srwatson * Process a packet destined for a protocol, and attempt direct dispatch.
218193219Srwatson * Supplemental source ordering information can be passed using the _src
219193219Srwatson * variant.
220193219Srwatson */
221193219Srwatsonint	netisr_dispatch(u_int proto, struct mbuf *m);
222193219Srwatsonint	netisr_dispatch_src(u_int proto, uintptr_t source, struct mbuf *m);
223193219Srwatsonint	netisr_queue(u_int proto, struct mbuf *m);
224193219Srwatsonint	netisr_queue_src(u_int proto, uintptr_t source, struct mbuf *m);
22511963Speter
226193219Srwatson/*
227193219Srwatson * Provide a default implementation of "map an ID to a CPU ID".
228193219Srwatson */
229193219Srwatsonu_int	netisr_default_flow2cpu(u_int flowid);
2302168Spaul
231193219Srwatson/*
232193219Srwatson * Utility routines to return the number of CPUs participting in netisr, and
233193219Srwatson * to return a mapping from a number to a CPU ID that can be used with the
234193219Srwatson * scheduler.
235193219Srwatson */
236193219Srwatsonu_int	netisr_get_cpucount(void);
237193219Srwatsonu_int	netisr_get_cpuid(u_int cpunumber);
238193219Srwatson
239193219Srwatson/*
240193219Srwatson * Interfaces between DEVICE_POLLING and netisr.
241193219Srwatson */
242193219Srwatsonvoid	netisr_sched_poll(void);
243193219Srwatsonvoid	netisr_poll(void);
244193219Srwatsonvoid	netisr_pollmore(void);
245193219Srwatson
246193219Srwatson#endif /* !_KERNEL */
247193219Srwatson#endif /* !_NET_NETISR_H_ */
248