netisr.h revision 193219
1/*-
2 * Copyright (c) 2007-2009 Robert N. M. Watson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/sys/net/netisr.h 193219 2009-06-01 10:41:38Z rwatson $
27 */
28
29#ifndef _NET_NETISR_H_
30#define _NET_NETISR_H_
31#ifdef _KERNEL
32
33/*
34 * The netisr (network interrupt service routine) provides a deferred
35 * execution evironment in which (generally inbound) network processing can
36 * take place.  Protocols register handlers which will be executed directly,
37 * or via deferred dispatch, depending on the circumstances.
38 *
39 * Historically, this was implemented by the BSD software ISR facility; it is
40 * now implemented via a software ithread (SWI).
41 */
42#define	NETISR_POLL	0		/* polling callback, must be first */
43#define	NETISR_IP	2		/* same as AF_INET */
44#define	NETISR_IGMP	3		/* IGMPv3 output queue */
45#define	NETISR_ROUTE	14		/* routing socket */
46#define	NETISR_AARP	15		/* Appletalk ARP */
47#define	NETISR_ATALK2	16		/* Appletalk phase 2 */
48#define	NETISR_ATALK1	17		/* Appletalk phase 1 */
49#define	NETISR_ARP	18		/* same as AF_LINK */
50#define	NETISR_IPX	23		/* same as AF_IPX */
51#define	NETISR_ETHER	24		/* ethernet input */
52#define	NETISR_IPV6	27
53#define	NETISR_NATM	28
54#define	NETISR_POLLMORE	31		/* polling callback, must be last */
55
56/*-
57 * Protocols express ordering constraints and affinity preferences by
58 * implementing one or neither of nh_m2flow and nh_m2cpuid, which are used by
59 * netisr to determine which per-CPU workstream to assign mbufs to.
60 *
61 * The following policies may be used by protocols:
62 *
63 * NETISR_POLICY_SOURCE - netisr should maintain source ordering without
64 *                        advice from the protocol.  netisr will ignore any
65 *                        flow IDs present on the mbuf for the purposes of
66 *                        work placement.
67 *
68 * NETISR_POLICY_FLOW - netisr should maintain flow ordering as defined by
69 *                      the mbuf header flow ID field.  If the protocol
70 *                      implements nh_m2flow, then netisr will query the
71 *                      protocol in the event that the mbuf doesn't have a
72 *                      flow ID, falling back on source ordering.
73 *
74 * NETISR_POLICY_CPU - netisr will delegate all work placement decisions to
75 *                     the protocol, querying nh_m2cpuid for each packet.
76 *
77 * Protocols might make decisions about work placement based on an existing
78 * calculated flow ID on the mbuf, such as one provided in hardware, the
79 * receive interface pointed to by the mbuf (if any), the optional source
80 * identifier passed at some dispatch points, or even parse packet headers to
81 * calculate a flow.  Both protocol handlers may return a new mbuf pointer
82 * for the chain, or NULL if the packet proves invalid or m_pullup() fails.
83 *
84 * XXXRW: If we eventually support dynamic reconfiguration, there should be
85 * protocol handlers to notify them of CPU configuration changes so that they
86 * can rebalance work.
87 */
88struct mbuf;
89typedef void		 netisr_handler_t (struct mbuf *m);
90typedef struct mbuf	*netisr_m2cpuid_t(struct mbuf *m, uintptr_t source,
91			 u_int *cpuid);
92typedef	struct mbuf	*netisr_m2flow_t(struct mbuf *m, uintptr_t source);
93
94#define	NETISR_POLICY_SOURCE	1	/* Maintain source ordering. */
95#define	NETISR_POLICY_FLOW	2	/* Maintain flow ordering. */
96#define	NETISR_POLICY_CPU	3	/* Protocol determines CPU placement. */
97
98/*
99 * Data structure describing a protocol handler.
100 */
101struct netisr_handler {
102	const char	*nh_name;	/* Character string protocol name. */
103	netisr_handler_t *nh_handler;	/* Protocol handler. */
104	netisr_m2flow_t	*nh_m2flow;	/* Query flow for untagged packet. */
105	netisr_m2cpuid_t *nh_m2cpuid;	/* Query CPU to process mbuf on. */
106	u_int		 nh_proto;	/* Integer protocol ID. */
107	u_int		 nh_qlimit;	/* Maximum per-CPU queue depth. */
108	u_int		 nh_policy;	/* Work placement policy. */
109	u_int		 nh_ispare[5];	/* For future use. */
110	void		*nh_pspare[4];	/* For future use. */
111};
112
113/*
114 * Register, unregister, and other netisr handler management functions.
115 */
116void	netisr_clearqdrops(const struct netisr_handler *nhp);
117void	netisr_getqdrops(const struct netisr_handler *nhp,
118	    u_int64_t *qdropsp);
119void	netisr_getqlimit(const struct netisr_handler *nhp, u_int *qlimitp);
120void	netisr_register(const struct netisr_handler *nhp);
121int	netisr_setqlimit(const struct netisr_handler *nhp, u_int qlimit);
122void	netisr_unregister(const struct netisr_handler *nhp);
123
124/*
125 * Process a packet destined for a protocol, and attempt direct dispatch.
126 * Supplemental source ordering information can be passed using the _src
127 * variant.
128 */
129int	netisr_dispatch(u_int proto, struct mbuf *m);
130int	netisr_dispatch_src(u_int proto, uintptr_t source, struct mbuf *m);
131int	netisr_queue(u_int proto, struct mbuf *m);
132int	netisr_queue_src(u_int proto, uintptr_t source, struct mbuf *m);
133
134/*
135 * Provide a default implementation of "map an ID to a CPU ID".
136 */
137u_int	netisr_default_flow2cpu(u_int flowid);
138
139/*
140 * Utility routines to return the number of CPUs participting in netisr, and
141 * to return a mapping from a number to a CPU ID that can be used with the
142 * scheduler.
143 */
144u_int	netisr_get_cpucount(void);
145u_int	netisr_get_cpuid(u_int cpunumber);
146
147/*
148 * Interfaces between DEVICE_POLLING and netisr.
149 */
150void	netisr_sched_poll(void);
151void	netisr_poll(void);
152void	netisr_pollmore(void);
153
154#endif /* !_KERNEL */
155#endif /* !_NET_NETISR_H_ */
156