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 */ 55263152Sglebius#define NETISR_ARP 4 /* same as AF_LINK */ 56263152Sglebius#define NETISR_ETHER 5 /* ethernet input */ 57263152Sglebius#define NETISR_IPV6 6 58263152Sglebius#define NETISR_NATM 7 59263152Sglebius#define NETISR_EPAIR 8 /* if_epair(4) */ 60271300Sadrian#define NETISR_IP_DIRECT 9 /* direct-dispatch IPv4 */ 61271300Sadrian#define NETISR_IPV6_DIRECT 10 /* direct-dispatch IPv6 */ 621541Srgrimes 63204199Srwatson/* 64204199Srwatson * Protocol ordering and affinity policy constants. See the detailed 65204199Srwatson * discussion of policies later in the file. 66204199Srwatson */ 67204199Srwatson#define NETISR_POLICY_SOURCE 1 /* Maintain source ordering. */ 68204199Srwatson#define NETISR_POLICY_FLOW 2 /* Maintain flow ordering. */ 69204199Srwatson#define NETISR_POLICY_CPU 3 /* Protocol determines CPU placement. */ 70204199Srwatson 71204199Srwatson/* 72222249Srwatson * Protocol dispatch policy constants; selects whether and when direct 73222249Srwatson * dispatch is permitted. 74222249Srwatson */ 75222249Srwatson#define NETISR_DISPATCH_DEFAULT 0 /* Use global default. */ 76222249Srwatson#define NETISR_DISPATCH_DEFERRED 1 /* Always defer dispatch. */ 77222249Srwatson#define NETISR_DISPATCH_HYBRID 2 /* Allow hybrid dispatch. */ 78222249Srwatson#define NETISR_DISPATCH_DIRECT 3 /* Always direct dispatch. */ 79222249Srwatson 80222249Srwatson/* 81204199Srwatson * Monitoring data structures, exported by sysctl(2). 82204199Srwatson * 83204199Srwatson * Three sysctls are defined. First, a per-protocol structure exported by 84204199Srwatson * net.isr.proto. 85204199Srwatson */ 86204199Srwatson#define NETISR_NAMEMAXLEN 32 87204199Srwatsonstruct sysctl_netisr_proto { 88204199Srwatson u_int snp_version; /* Length of struct. */ 89204199Srwatson char snp_name[NETISR_NAMEMAXLEN]; /* nh_name */ 90204199Srwatson u_int snp_proto; /* nh_proto */ 91204199Srwatson u_int snp_qlimit; /* nh_qlimit */ 92204199Srwatson u_int snp_policy; /* nh_policy */ 93204199Srwatson u_int snp_flags; /* Various flags. */ 94222249Srwatson u_int snp_dispatch; /* Dispatch policy. */ 95222249Srwatson u_int _snp_ispare[6]; 96204199Srwatson}; 97204199Srwatson 98204199Srwatson/* 99204199Srwatson * Flags for sysctl_netisr_proto.snp_flags. 100204199Srwatson */ 101204199Srwatson#define NETISR_SNP_FLAGS_M2FLOW 0x00000001 /* nh_m2flow */ 102204199Srwatson#define NETISR_SNP_FLAGS_M2CPUID 0x00000002 /* nh_m2cpuid */ 103204208Srwatson#define NETISR_SNP_FLAGS_DRAINEDCPU 0x00000004 /* nh_drainedcpu */ 104204199Srwatson 105204199Srwatson/* 106204199Srwatson * Next, a structure per-workstream, with per-protocol data, exported as 107204199Srwatson * net.isr.workstream. 108204199Srwatson */ 109204199Srwatsonstruct sysctl_netisr_workstream { 110204199Srwatson u_int snws_version; /* Length of struct. */ 111204199Srwatson u_int snws_flags; /* Various flags. */ 112204199Srwatson u_int snws_wsid; /* Workstream ID. */ 113204199Srwatson u_int snws_cpu; /* nws_cpu */ 114204199Srwatson u_int _snws_ispare[12]; 115204199Srwatson}; 116204199Srwatson 117204199Srwatson/* 118204199Srwatson * Flags for sysctl_netisr_workstream.snws_flags 119204199Srwatson */ 120204199Srwatson#define NETISR_SNWS_FLAGS_INTR 0x00000001 /* nws_intr_event */ 121204199Srwatson 122204199Srwatson/* 123204199Srwatson * Finally, a per-workstream-per-protocol structure, exported as 124204199Srwatson * net.isr.work. 125204199Srwatson */ 126204199Srwatsonstruct sysctl_netisr_work { 127204199Srwatson u_int snw_version; /* Length of struct. */ 128204199Srwatson u_int snw_wsid; /* Workstream ID. */ 129204199Srwatson u_int snw_proto; /* Protocol number. */ 130204199Srwatson u_int snw_len; /* nw_len */ 131204199Srwatson u_int snw_watermark; /* nw_watermark */ 132204199Srwatson u_int _snw_ispare[3]; 133204199Srwatson 134204199Srwatson uint64_t snw_dispatched; /* nw_dispatched */ 135204199Srwatson uint64_t snw_hybrid_dispatched; /* nw_hybrid_dispatched */ 136204199Srwatson uint64_t snw_qdrops; /* nw_qdrops */ 137204199Srwatson uint64_t snw_queued; /* nw_queued */ 138204199Srwatson uint64_t snw_handled; /* nw_handled */ 139204199Srwatson 140204199Srwatson uint64_t _snw_llspare[7]; 141204199Srwatson}; 142204199Srwatson 143204199Srwatson#ifdef _KERNEL 144204199Srwatson 145193219Srwatson/*- 146193219Srwatson * Protocols express ordering constraints and affinity preferences by 147193219Srwatson * implementing one or neither of nh_m2flow and nh_m2cpuid, which are used by 148193219Srwatson * netisr to determine which per-CPU workstream to assign mbufs to. 149193219Srwatson * 150193219Srwatson * The following policies may be used by protocols: 151193219Srwatson * 152193219Srwatson * NETISR_POLICY_SOURCE - netisr should maintain source ordering without 153193219Srwatson * advice from the protocol. netisr will ignore any 154193219Srwatson * flow IDs present on the mbuf for the purposes of 155193219Srwatson * work placement. 156193219Srwatson * 157193219Srwatson * NETISR_POLICY_FLOW - netisr should maintain flow ordering as defined by 158193219Srwatson * the mbuf header flow ID field. If the protocol 159193219Srwatson * implements nh_m2flow, then netisr will query the 160193219Srwatson * protocol in the event that the mbuf doesn't have a 161193219Srwatson * flow ID, falling back on source ordering. 162193219Srwatson * 163193219Srwatson * NETISR_POLICY_CPU - netisr will delegate all work placement decisions to 164193219Srwatson * the protocol, querying nh_m2cpuid for each packet. 165193219Srwatson * 166193219Srwatson * Protocols might make decisions about work placement based on an existing 167193219Srwatson * calculated flow ID on the mbuf, such as one provided in hardware, the 168193219Srwatson * receive interface pointed to by the mbuf (if any), the optional source 169193219Srwatson * identifier passed at some dispatch points, or even parse packet headers to 170193219Srwatson * calculate a flow. Both protocol handlers may return a new mbuf pointer 171193219Srwatson * for the chain, or NULL if the packet proves invalid or m_pullup() fails. 172193219Srwatson * 173193219Srwatson * XXXRW: If we eventually support dynamic reconfiguration, there should be 174193219Srwatson * protocol handlers to notify them of CPU configuration changes so that they 175193219Srwatson * can rebalance work. 176193219Srwatson */ 177193219Srwatsonstruct mbuf; 178204498Srwatsontypedef void netisr_handler_t(struct mbuf *m); 179193219Srwatsontypedef struct mbuf *netisr_m2cpuid_t(struct mbuf *m, uintptr_t source, 180193219Srwatson u_int *cpuid); 181193219Srwatsontypedef struct mbuf *netisr_m2flow_t(struct mbuf *m, uintptr_t source); 182194201Sbztypedef void netisr_drainedcpu_t(u_int cpuid); 18357178Speter 184222249Srwatson#define NETISR_CPUID_NONE ((u_int)-1) /* No affinity returned. */ 185222249Srwatson 186193219Srwatson/* 187193219Srwatson * Data structure describing a protocol handler. 188193219Srwatson */ 189193219Srwatsonstruct netisr_handler { 190193219Srwatson const char *nh_name; /* Character string protocol name. */ 191193219Srwatson netisr_handler_t *nh_handler; /* Protocol handler. */ 192193219Srwatson netisr_m2flow_t *nh_m2flow; /* Query flow for untagged packet. */ 193193219Srwatson netisr_m2cpuid_t *nh_m2cpuid; /* Query CPU to process mbuf on. */ 194194201Sbz netisr_drainedcpu_t *nh_drainedcpu; /* Callback when drained a queue. */ 195193219Srwatson u_int nh_proto; /* Integer protocol ID. */ 196193219Srwatson u_int nh_qlimit; /* Maximum per-CPU queue depth. */ 197193219Srwatson u_int nh_policy; /* Work placement policy. */ 198222249Srwatson u_int nh_dispatch; /* Dispatch policy. */ 199222249Srwatson u_int nh_ispare[4]; /* For future use. */ 200193219Srwatson void *nh_pspare[4]; /* For future use. */ 201193219Srwatson}; 2028426Swollman 203193219Srwatson/* 204193219Srwatson * Register, unregister, and other netisr handler management functions. 205193219Srwatson */ 206193219Srwatsonvoid netisr_clearqdrops(const struct netisr_handler *nhp); 207193219Srwatsonvoid netisr_getqdrops(const struct netisr_handler *nhp, 208193219Srwatson u_int64_t *qdropsp); 209193219Srwatsonvoid netisr_getqlimit(const struct netisr_handler *nhp, u_int *qlimitp); 210193219Srwatsonvoid netisr_register(const struct netisr_handler *nhp); 211193219Srwatsonint netisr_setqlimit(const struct netisr_handler *nhp, u_int qlimit); 212193219Srwatsonvoid netisr_unregister(const struct netisr_handler *nhp); 213301270Sbz#ifdef VIMAGE 214301270Sbzvoid netisr_register_vnet(const struct netisr_handler *nhp); 215301270Sbzvoid netisr_unregister_vnet(const struct netisr_handler *nhp); 216301270Sbz#endif 2178426Swollman 218193219Srwatson/* 219193219Srwatson * Process a packet destined for a protocol, and attempt direct dispatch. 220193219Srwatson * Supplemental source ordering information can be passed using the _src 221193219Srwatson * variant. 222193219Srwatson */ 223193219Srwatsonint netisr_dispatch(u_int proto, struct mbuf *m); 224193219Srwatsonint netisr_dispatch_src(u_int proto, uintptr_t source, struct mbuf *m); 225193219Srwatsonint netisr_queue(u_int proto, struct mbuf *m); 226193219Srwatsonint netisr_queue_src(u_int proto, uintptr_t source, struct mbuf *m); 22711963Speter 228193219Srwatson/* 229193219Srwatson * Provide a default implementation of "map an ID to a CPU ID". 230193219Srwatson */ 231193219Srwatsonu_int netisr_default_flow2cpu(u_int flowid); 2322168Spaul 233193219Srwatson/* 234193219Srwatson * Utility routines to return the number of CPUs participting in netisr, and 235193219Srwatson * to return a mapping from a number to a CPU ID that can be used with the 236193219Srwatson * scheduler. 237193219Srwatson */ 238193219Srwatsonu_int netisr_get_cpucount(void); 239193219Srwatsonu_int netisr_get_cpuid(u_int cpunumber); 240193219Srwatson 241193219Srwatson/* 242193219Srwatson * Interfaces between DEVICE_POLLING and netisr. 243193219Srwatson */ 244193219Srwatsonvoid netisr_sched_poll(void); 245193219Srwatsonvoid netisr_poll(void); 246193219Srwatsonvoid netisr_pollmore(void); 247193219Srwatson 248193219Srwatson#endif /* !_KERNEL */ 249193219Srwatson#endif /* !_NET_NETISR_H_ */ 250