1c398230bSWarner Losh /*- 2d4b5cae4SRobert Watson * Copyright (c) 2007-2009 Robert N. M. Watson 32d22f334SRobert Watson * Copyright (c) 2010 Juniper Networks, Inc. 4d4b5cae4SRobert Watson * All rights reserved. 5df8bae1dSRodney W. Grimes * 62d22f334SRobert Watson * This software was developed by Robert N. M. Watson under contract 72d22f334SRobert Watson * to Juniper Networks, Inc. 82d22f334SRobert Watson * 9df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 10df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 11df8bae1dSRodney W. Grimes * are met: 12df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 13df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 14df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 15df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 16df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 17df8bae1dSRodney W. Grimes * 18d4b5cae4SRobert Watson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21d4b5cae4SRobert Watson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28df8bae1dSRodney W. Grimes * SUCH DAMAGE. 29df8bae1dSRodney W. Grimes * 30c3aac50fSPeter Wemm * $FreeBSD$ 31df8bae1dSRodney W. Grimes */ 32df8bae1dSRodney W. Grimes 33cea1da3bSPaul Richards #ifndef _NET_NETISR_H_ 34cea1da3bSPaul Richards #define _NET_NETISR_H_ 35cea1da3bSPaul Richards 36df8bae1dSRodney W. Grimes /* 37315f0461SRobert Watson * The netisr (network interrupt service routine) provides a deferred 38315f0461SRobert Watson * execution evironment in which (generally inbound) network processing can 39d4b5cae4SRobert Watson * take place. Protocols register handlers which will be executed directly, 40d4b5cae4SRobert Watson * or via deferred dispatch, depending on the circumstances. 41df8bae1dSRodney W. Grimes * 42315f0461SRobert Watson * Historically, this was implemented by the BSD software ISR facility; it is 43315f0461SRobert Watson * now implemented via a software ithread (SWI). 44df8bae1dSRodney W. Grimes */ 452d22f334SRobert Watson 462d22f334SRobert Watson /* 472d22f334SRobert Watson * Protocol numbers, which are encoded in monitoring applications and kernel 482d22f334SRobert Watson * modules. Internally, these are used in bit shift operations so must have 492d22f334SRobert Watson * a value 0 < proto < 32; we currently further limit at compile-time to 16 502d22f334SRobert Watson * for array-sizing purposes. 512d22f334SRobert Watson */ 52ed54411cSRobert Watson #define NETISR_IP 1 53ed54411cSRobert Watson #define NETISR_IGMP 2 /* IGMPv3 output queue */ 54ed54411cSRobert Watson #define NETISR_ROUTE 3 /* routing socket */ 55ed54411cSRobert Watson #define NETISR_AARP 4 /* Appletalk ARP */ 56ed54411cSRobert Watson #define NETISR_ATALK2 5 /* Appletalk phase 2 */ 57ed54411cSRobert Watson #define NETISR_ATALK1 6 /* Appletalk phase 1 */ 58ed54411cSRobert Watson #define NETISR_ARP 7 /* same as AF_LINK */ 59ed54411cSRobert Watson #define NETISR_IPX 8 /* same as AF_IPX */ 60ed54411cSRobert Watson #define NETISR_ETHER 9 /* ethernet input */ 61ed54411cSRobert Watson #define NETISR_IPV6 10 62ed54411cSRobert Watson #define NETISR_NATM 11 63d0ea4743SBjoern A. Zeeb #define NETISR_EPAIR 12 /* if_epair(4) */ 64df8bae1dSRodney W. Grimes 652d22f334SRobert Watson /* 662d22f334SRobert Watson * Protocol ordering and affinity policy constants. See the detailed 672d22f334SRobert Watson * discussion of policies later in the file. 682d22f334SRobert Watson */ 692d22f334SRobert Watson #define NETISR_POLICY_SOURCE 1 /* Maintain source ordering. */ 702d22f334SRobert Watson #define NETISR_POLICY_FLOW 2 /* Maintain flow ordering. */ 712d22f334SRobert Watson #define NETISR_POLICY_CPU 3 /* Protocol determines CPU placement. */ 722d22f334SRobert Watson 732d22f334SRobert Watson /* 742d22f334SRobert Watson * Monitoring data structures, exported by sysctl(2). 752d22f334SRobert Watson * 762d22f334SRobert Watson * Three sysctls are defined. First, a per-protocol structure exported by 772d22f334SRobert Watson * net.isr.proto. 782d22f334SRobert Watson */ 792d22f334SRobert Watson #define NETISR_NAMEMAXLEN 32 802d22f334SRobert Watson struct sysctl_netisr_proto { 812d22f334SRobert Watson u_int snp_version; /* Length of struct. */ 822d22f334SRobert Watson char snp_name[NETISR_NAMEMAXLEN]; /* nh_name */ 832d22f334SRobert Watson u_int snp_proto; /* nh_proto */ 842d22f334SRobert Watson u_int snp_qlimit; /* nh_qlimit */ 852d22f334SRobert Watson u_int snp_policy; /* nh_policy */ 862d22f334SRobert Watson u_int snp_flags; /* Various flags. */ 872d22f334SRobert Watson u_int _snp_ispare[7]; 882d22f334SRobert Watson }; 892d22f334SRobert Watson 902d22f334SRobert Watson /* 912d22f334SRobert Watson * Flags for sysctl_netisr_proto.snp_flags. 922d22f334SRobert Watson */ 932d22f334SRobert Watson #define NETISR_SNP_FLAGS_M2FLOW 0x00000001 /* nh_m2flow */ 942d22f334SRobert Watson #define NETISR_SNP_FLAGS_M2CPUID 0x00000002 /* nh_m2cpuid */ 952d22f334SRobert Watson #define NETISR_SNP_FLAGS_DRAINEDCPU 0x00000003 /* nh_drainedcpu */ 962d22f334SRobert Watson 972d22f334SRobert Watson /* 982d22f334SRobert Watson * Next, a structure per-workstream, with per-protocol data, exported as 992d22f334SRobert Watson * net.isr.workstream. 1002d22f334SRobert Watson */ 1012d22f334SRobert Watson struct sysctl_netisr_workstream { 1022d22f334SRobert Watson u_int snws_version; /* Length of struct. */ 1032d22f334SRobert Watson u_int snws_flags; /* Various flags. */ 1042d22f334SRobert Watson u_int snws_wsid; /* Workstream ID. */ 1052d22f334SRobert Watson u_int snws_cpu; /* nws_cpu */ 1062d22f334SRobert Watson u_int _snws_ispare[12]; 1072d22f334SRobert Watson }; 1082d22f334SRobert Watson 1092d22f334SRobert Watson /* 1102d22f334SRobert Watson * Flags for sysctl_netisr_workstream.snws_flags 1112d22f334SRobert Watson */ 1122d22f334SRobert Watson #define NETISR_SNWS_FLAGS_INTR 0x00000001 /* nws_intr_event */ 1132d22f334SRobert Watson 1142d22f334SRobert Watson /* 1152d22f334SRobert Watson * Finally, a per-workstream-per-protocol structure, exported as 1162d22f334SRobert Watson * net.isr.work. 1172d22f334SRobert Watson */ 1182d22f334SRobert Watson struct sysctl_netisr_work { 1192d22f334SRobert Watson u_int snw_version; /* Length of struct. */ 1202d22f334SRobert Watson u_int snw_wsid; /* Workstream ID. */ 1212d22f334SRobert Watson u_int snw_proto; /* Protocol number. */ 1222d22f334SRobert Watson u_int snw_len; /* nw_len */ 1232d22f334SRobert Watson u_int snw_watermark; /* nw_watermark */ 1242d22f334SRobert Watson u_int _snw_ispare[3]; 1252d22f334SRobert Watson 1262d22f334SRobert Watson uint64_t snw_dispatched; /* nw_dispatched */ 1272d22f334SRobert Watson uint64_t snw_hybrid_dispatched; /* nw_hybrid_dispatched */ 1282d22f334SRobert Watson uint64_t snw_qdrops; /* nw_qdrops */ 1292d22f334SRobert Watson uint64_t snw_queued; /* nw_queued */ 1302d22f334SRobert Watson uint64_t snw_handled; /* nw_handled */ 1312d22f334SRobert Watson 1322d22f334SRobert Watson uint64_t _snw_llspare[7]; 1332d22f334SRobert Watson }; 1342d22f334SRobert Watson 1352d22f334SRobert Watson #ifdef _KERNEL 1362d22f334SRobert Watson 137d4b5cae4SRobert Watson /*- 138d4b5cae4SRobert Watson * Protocols express ordering constraints and affinity preferences by 139d4b5cae4SRobert Watson * implementing one or neither of nh_m2flow and nh_m2cpuid, which are used by 140d4b5cae4SRobert Watson * netisr to determine which per-CPU workstream to assign mbufs to. 141d4b5cae4SRobert Watson * 142d4b5cae4SRobert Watson * The following policies may be used by protocols: 143d4b5cae4SRobert Watson * 144d4b5cae4SRobert Watson * NETISR_POLICY_SOURCE - netisr should maintain source ordering without 145d4b5cae4SRobert Watson * advice from the protocol. netisr will ignore any 146d4b5cae4SRobert Watson * flow IDs present on the mbuf for the purposes of 147d4b5cae4SRobert Watson * work placement. 148d4b5cae4SRobert Watson * 149d4b5cae4SRobert Watson * NETISR_POLICY_FLOW - netisr should maintain flow ordering as defined by 150d4b5cae4SRobert Watson * the mbuf header flow ID field. If the protocol 151d4b5cae4SRobert Watson * implements nh_m2flow, then netisr will query the 152d4b5cae4SRobert Watson * protocol in the event that the mbuf doesn't have a 153d4b5cae4SRobert Watson * flow ID, falling back on source ordering. 154d4b5cae4SRobert Watson * 155d4b5cae4SRobert Watson * NETISR_POLICY_CPU - netisr will delegate all work placement decisions to 156d4b5cae4SRobert Watson * the protocol, querying nh_m2cpuid for each packet. 157d4b5cae4SRobert Watson * 158d4b5cae4SRobert Watson * Protocols might make decisions about work placement based on an existing 159d4b5cae4SRobert Watson * calculated flow ID on the mbuf, such as one provided in hardware, the 160d4b5cae4SRobert Watson * receive interface pointed to by the mbuf (if any), the optional source 161d4b5cae4SRobert Watson * identifier passed at some dispatch points, or even parse packet headers to 162d4b5cae4SRobert Watson * calculate a flow. Both protocol handlers may return a new mbuf pointer 163d4b5cae4SRobert Watson * for the chain, or NULL if the packet proves invalid or m_pullup() fails. 164d4b5cae4SRobert Watson * 165d4b5cae4SRobert Watson * XXXRW: If we eventually support dynamic reconfiguration, there should be 166d4b5cae4SRobert Watson * protocol handlers to notify them of CPU configuration changes so that they 167d4b5cae4SRobert Watson * can rebalance work. 168d4b5cae4SRobert Watson */ 1691cafed39SJonathan Lemon struct mbuf; 170d4b5cae4SRobert Watson typedef void netisr_handler_t (struct mbuf *m); 171d4b5cae4SRobert Watson typedef struct mbuf *netisr_m2cpuid_t(struct mbuf *m, uintptr_t source, 172d4b5cae4SRobert Watson u_int *cpuid); 173d4b5cae4SRobert Watson typedef struct mbuf *netisr_m2flow_t(struct mbuf *m, uintptr_t source); 174ed655c8cSBjoern A. Zeeb typedef void netisr_drainedcpu_t(u_int cpuid); 175748e0b0aSGarrett Wollman 176d4b5cae4SRobert Watson /* 177d4b5cae4SRobert Watson * Data structure describing a protocol handler. 178d4b5cae4SRobert Watson */ 179d4b5cae4SRobert Watson struct netisr_handler { 180d4b5cae4SRobert Watson const char *nh_name; /* Character string protocol name. */ 181d4b5cae4SRobert Watson netisr_handler_t *nh_handler; /* Protocol handler. */ 182d4b5cae4SRobert Watson netisr_m2flow_t *nh_m2flow; /* Query flow for untagged packet. */ 183d4b5cae4SRobert Watson netisr_m2cpuid_t *nh_m2cpuid; /* Query CPU to process mbuf on. */ 184ed655c8cSBjoern A. Zeeb netisr_drainedcpu_t *nh_drainedcpu; /* Callback when drained a queue. */ 185d4b5cae4SRobert Watson u_int nh_proto; /* Integer protocol ID. */ 186d4b5cae4SRobert Watson u_int nh_qlimit; /* Maximum per-CPU queue depth. */ 187d4b5cae4SRobert Watson u_int nh_policy; /* Work placement policy. */ 188d4b5cae4SRobert Watson u_int nh_ispare[5]; /* For future use. */ 189d4b5cae4SRobert Watson void *nh_pspare[4]; /* For future use. */ 190d4b5cae4SRobert Watson }; 19106cc1858SPeter Wemm 192d4b5cae4SRobert Watson /* 193d4b5cae4SRobert Watson * Register, unregister, and other netisr handler management functions. 194d4b5cae4SRobert Watson */ 195d4b5cae4SRobert Watson void netisr_clearqdrops(const struct netisr_handler *nhp); 196d4b5cae4SRobert Watson void netisr_getqdrops(const struct netisr_handler *nhp, 197d4b5cae4SRobert Watson u_int64_t *qdropsp); 198d4b5cae4SRobert Watson void netisr_getqlimit(const struct netisr_handler *nhp, u_int *qlimitp); 199d4b5cae4SRobert Watson void netisr_register(const struct netisr_handler *nhp); 200d4b5cae4SRobert Watson int netisr_setqlimit(const struct netisr_handler *nhp, u_int qlimit); 201d4b5cae4SRobert Watson void netisr_unregister(const struct netisr_handler *nhp); 202cea1da3bSPaul Richards 203d4b5cae4SRobert Watson /* 204d4b5cae4SRobert Watson * Process a packet destined for a protocol, and attempt direct dispatch. 205d4b5cae4SRobert Watson * Supplemental source ordering information can be passed using the _src 206d4b5cae4SRobert Watson * variant. 207d4b5cae4SRobert Watson */ 208d4b5cae4SRobert Watson int netisr_dispatch(u_int proto, struct mbuf *m); 209d4b5cae4SRobert Watson int netisr_dispatch_src(u_int proto, uintptr_t source, struct mbuf *m); 210d4b5cae4SRobert Watson int netisr_queue(u_int proto, struct mbuf *m); 211d4b5cae4SRobert Watson int netisr_queue_src(u_int proto, uintptr_t source, struct mbuf *m); 212d4b5cae4SRobert Watson 213d4b5cae4SRobert Watson /* 214d4b5cae4SRobert Watson * Provide a default implementation of "map an ID to a CPU ID". 215d4b5cae4SRobert Watson */ 216d4b5cae4SRobert Watson u_int netisr_default_flow2cpu(u_int flowid); 217d4b5cae4SRobert Watson 218d4b5cae4SRobert Watson /* 219d4b5cae4SRobert Watson * Utility routines to return the number of CPUs participting in netisr, and 220d4b5cae4SRobert Watson * to return a mapping from a number to a CPU ID that can be used with the 221d4b5cae4SRobert Watson * scheduler. 222d4b5cae4SRobert Watson */ 223d4b5cae4SRobert Watson u_int netisr_get_cpucount(void); 224d4b5cae4SRobert Watson u_int netisr_get_cpuid(u_int cpunumber); 225d4b5cae4SRobert Watson 226d4b5cae4SRobert Watson /* 227d4b5cae4SRobert Watson * Interfaces between DEVICE_POLLING and netisr. 228d4b5cae4SRobert Watson */ 229d4b5cae4SRobert Watson void netisr_sched_poll(void); 230d4b5cae4SRobert Watson void netisr_poll(void); 231d4b5cae4SRobert Watson void netisr_pollmore(void); 232d4b5cae4SRobert Watson 233d4b5cae4SRobert Watson #endif /* !_KERNEL */ 234d4b5cae4SRobert Watson #endif /* !_NET_NETISR_H_ */ 235