11eaf0ac3Slogwang /*- 2*d4a07e70Sfengbojiang * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3*d4a07e70Sfengbojiang * 41eaf0ac3Slogwang * Copyright (c) 2007-2009 Robert N. M. Watson 51eaf0ac3Slogwang * Copyright (c) 2010-2011 Juniper Networks, Inc. 61eaf0ac3Slogwang * All rights reserved. 71eaf0ac3Slogwang * 81eaf0ac3Slogwang * This software was developed by Robert N. M. Watson under contract 91eaf0ac3Slogwang * to Juniper Networks, Inc. 101eaf0ac3Slogwang * 111eaf0ac3Slogwang * Redistribution and use in source and binary forms, with or without 121eaf0ac3Slogwang * modification, are permitted provided that the following conditions 131eaf0ac3Slogwang * are met: 141eaf0ac3Slogwang * 1. Redistributions of source code must retain the above copyright 151eaf0ac3Slogwang * notice, this list of conditions and the following disclaimer. 161eaf0ac3Slogwang * 2. Redistributions in binary form must reproduce the above copyright 171eaf0ac3Slogwang * notice, this list of conditions and the following disclaimer in the 181eaf0ac3Slogwang * documentation and/or other materials provided with the distribution. 191eaf0ac3Slogwang * 201eaf0ac3Slogwang * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 211eaf0ac3Slogwang * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 221eaf0ac3Slogwang * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 231eaf0ac3Slogwang * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 241eaf0ac3Slogwang * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 251eaf0ac3Slogwang * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 261eaf0ac3Slogwang * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 271eaf0ac3Slogwang * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 281eaf0ac3Slogwang * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 291eaf0ac3Slogwang * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 301eaf0ac3Slogwang * SUCH DAMAGE. 311eaf0ac3Slogwang * 321eaf0ac3Slogwang * $FreeBSD$ 331eaf0ac3Slogwang */ 341eaf0ac3Slogwang 351eaf0ac3Slogwang #ifndef _NET_NETISR_H_ 361eaf0ac3Slogwang #define _NET_NETISR_H_ 371eaf0ac3Slogwang 381eaf0ac3Slogwang /* 391eaf0ac3Slogwang * The netisr (network interrupt service routine) provides a deferred 401eaf0ac3Slogwang * execution evironment in which (generally inbound) network processing can 411eaf0ac3Slogwang * take place. Protocols register handlers which will be executed directly, 421eaf0ac3Slogwang * or via deferred dispatch, depending on the circumstances. 431eaf0ac3Slogwang * 441eaf0ac3Slogwang * Historically, this was implemented by the BSD software ISR facility; it is 451eaf0ac3Slogwang * now implemented via a software ithread (SWI). 461eaf0ac3Slogwang */ 471eaf0ac3Slogwang 481eaf0ac3Slogwang /* 491eaf0ac3Slogwang * Protocol numbers, which are encoded in monitoring applications and kernel 501eaf0ac3Slogwang * modules. Internally, these are used in bit shift operations so must have 511eaf0ac3Slogwang * a value 0 < proto < 32; we currently further limit at compile-time to 16 521eaf0ac3Slogwang * for array-sizing purposes. 531eaf0ac3Slogwang */ 541eaf0ac3Slogwang #define NETISR_IP 1 551eaf0ac3Slogwang #define NETISR_IGMP 2 /* IGMPv3 output queue */ 561eaf0ac3Slogwang #define NETISR_ROUTE 3 /* routing socket */ 571eaf0ac3Slogwang #define NETISR_ARP 4 /* same as AF_LINK */ 581eaf0ac3Slogwang #define NETISR_ETHER 5 /* ethernet input */ 591eaf0ac3Slogwang #define NETISR_IPV6 6 601eaf0ac3Slogwang #define NETISR_EPAIR 8 /* if_epair(4) */ 611eaf0ac3Slogwang #define NETISR_IP_DIRECT 9 /* direct-dispatch IPv4 */ 621eaf0ac3Slogwang #define NETISR_IPV6_DIRECT 10 /* direct-dispatch IPv6 */ 631eaf0ac3Slogwang 641eaf0ac3Slogwang /* 651eaf0ac3Slogwang * Protocol ordering and affinity policy constants. See the detailed 661eaf0ac3Slogwang * discussion of policies later in the file. 671eaf0ac3Slogwang */ 681eaf0ac3Slogwang #define NETISR_POLICY_SOURCE 1 /* Maintain source ordering. */ 691eaf0ac3Slogwang #define NETISR_POLICY_FLOW 2 /* Maintain flow ordering. */ 701eaf0ac3Slogwang #define NETISR_POLICY_CPU 3 /* Protocol determines CPU placement. */ 711eaf0ac3Slogwang 721eaf0ac3Slogwang /* 731eaf0ac3Slogwang * Protocol dispatch policy constants; selects whether and when direct 741eaf0ac3Slogwang * dispatch is permitted. 751eaf0ac3Slogwang */ 761eaf0ac3Slogwang #define NETISR_DISPATCH_DEFAULT 0 /* Use global default. */ 771eaf0ac3Slogwang #define NETISR_DISPATCH_DEFERRED 1 /* Always defer dispatch. */ 781eaf0ac3Slogwang #define NETISR_DISPATCH_HYBRID 2 /* Allow hybrid dispatch. */ 791eaf0ac3Slogwang #define NETISR_DISPATCH_DIRECT 3 /* Always direct dispatch. */ 801eaf0ac3Slogwang 811eaf0ac3Slogwang /* 821eaf0ac3Slogwang * Monitoring data structures, exported by sysctl(2). 831eaf0ac3Slogwang * 841eaf0ac3Slogwang * Three sysctls are defined. First, a per-protocol structure exported by 851eaf0ac3Slogwang * net.isr.proto. 861eaf0ac3Slogwang */ 871eaf0ac3Slogwang #define NETISR_NAMEMAXLEN 32 881eaf0ac3Slogwang struct sysctl_netisr_proto { 891eaf0ac3Slogwang u_int snp_version; /* Length of struct. */ 901eaf0ac3Slogwang char snp_name[NETISR_NAMEMAXLEN]; /* nh_name */ 911eaf0ac3Slogwang u_int snp_proto; /* nh_proto */ 921eaf0ac3Slogwang u_int snp_qlimit; /* nh_qlimit */ 931eaf0ac3Slogwang u_int snp_policy; /* nh_policy */ 941eaf0ac3Slogwang u_int snp_flags; /* Various flags. */ 951eaf0ac3Slogwang u_int snp_dispatch; /* Dispatch policy. */ 961eaf0ac3Slogwang u_int _snp_ispare[6]; 971eaf0ac3Slogwang }; 981eaf0ac3Slogwang 991eaf0ac3Slogwang /* 1001eaf0ac3Slogwang * Flags for sysctl_netisr_proto.snp_flags. 1011eaf0ac3Slogwang */ 1021eaf0ac3Slogwang #define NETISR_SNP_FLAGS_M2FLOW 0x00000001 /* nh_m2flow */ 1031eaf0ac3Slogwang #define NETISR_SNP_FLAGS_M2CPUID 0x00000002 /* nh_m2cpuid */ 1041eaf0ac3Slogwang #define NETISR_SNP_FLAGS_DRAINEDCPU 0x00000004 /* nh_drainedcpu */ 1051eaf0ac3Slogwang 1061eaf0ac3Slogwang /* 1071eaf0ac3Slogwang * Next, a structure per-workstream, with per-protocol data, exported as 1081eaf0ac3Slogwang * net.isr.workstream. 1091eaf0ac3Slogwang */ 1101eaf0ac3Slogwang struct sysctl_netisr_workstream { 1111eaf0ac3Slogwang u_int snws_version; /* Length of struct. */ 1121eaf0ac3Slogwang u_int snws_flags; /* Various flags. */ 1131eaf0ac3Slogwang u_int snws_wsid; /* Workstream ID. */ 1141eaf0ac3Slogwang u_int snws_cpu; /* nws_cpu */ 1151eaf0ac3Slogwang u_int _snws_ispare[12]; 1161eaf0ac3Slogwang }; 1171eaf0ac3Slogwang 1181eaf0ac3Slogwang /* 1191eaf0ac3Slogwang * Flags for sysctl_netisr_workstream.snws_flags 1201eaf0ac3Slogwang */ 1211eaf0ac3Slogwang #define NETISR_SNWS_FLAGS_INTR 0x00000001 /* nws_intr_event */ 1221eaf0ac3Slogwang 1231eaf0ac3Slogwang /* 1241eaf0ac3Slogwang * Finally, a per-workstream-per-protocol structure, exported as 1251eaf0ac3Slogwang * net.isr.work. 1261eaf0ac3Slogwang */ 1271eaf0ac3Slogwang struct sysctl_netisr_work { 1281eaf0ac3Slogwang u_int snw_version; /* Length of struct. */ 1291eaf0ac3Slogwang u_int snw_wsid; /* Workstream ID. */ 1301eaf0ac3Slogwang u_int snw_proto; /* Protocol number. */ 1311eaf0ac3Slogwang u_int snw_len; /* nw_len */ 1321eaf0ac3Slogwang u_int snw_watermark; /* nw_watermark */ 1331eaf0ac3Slogwang u_int _snw_ispare[3]; 1341eaf0ac3Slogwang 1351eaf0ac3Slogwang uint64_t snw_dispatched; /* nw_dispatched */ 1361eaf0ac3Slogwang uint64_t snw_hybrid_dispatched; /* nw_hybrid_dispatched */ 1371eaf0ac3Slogwang uint64_t snw_qdrops; /* nw_qdrops */ 1381eaf0ac3Slogwang uint64_t snw_queued; /* nw_queued */ 1391eaf0ac3Slogwang uint64_t snw_handled; /* nw_handled */ 1401eaf0ac3Slogwang 1411eaf0ac3Slogwang uint64_t _snw_llspare[7]; 1421eaf0ac3Slogwang }; 1431eaf0ac3Slogwang 1441eaf0ac3Slogwang #ifdef _KERNEL 1451eaf0ac3Slogwang 1461eaf0ac3Slogwang /*- 1471eaf0ac3Slogwang * Protocols express ordering constraints and affinity preferences by 1481eaf0ac3Slogwang * implementing one or neither of nh_m2flow and nh_m2cpuid, which are used by 1491eaf0ac3Slogwang * netisr to determine which per-CPU workstream to assign mbufs to. 1501eaf0ac3Slogwang * 1511eaf0ac3Slogwang * The following policies may be used by protocols: 1521eaf0ac3Slogwang * 1531eaf0ac3Slogwang * NETISR_POLICY_SOURCE - netisr should maintain source ordering without 1541eaf0ac3Slogwang * advice from the protocol. netisr will ignore any 1551eaf0ac3Slogwang * flow IDs present on the mbuf for the purposes of 1561eaf0ac3Slogwang * work placement. 1571eaf0ac3Slogwang * 1581eaf0ac3Slogwang * NETISR_POLICY_FLOW - netisr should maintain flow ordering as defined by 1591eaf0ac3Slogwang * the mbuf header flow ID field. If the protocol 1601eaf0ac3Slogwang * implements nh_m2flow, then netisr will query the 1611eaf0ac3Slogwang * protocol in the event that the mbuf doesn't have a 1621eaf0ac3Slogwang * flow ID, falling back on source ordering. 1631eaf0ac3Slogwang * 1641eaf0ac3Slogwang * NETISR_POLICY_CPU - netisr will delegate all work placement decisions to 1651eaf0ac3Slogwang * the protocol, querying nh_m2cpuid for each packet. 1661eaf0ac3Slogwang * 1671eaf0ac3Slogwang * Protocols might make decisions about work placement based on an existing 1681eaf0ac3Slogwang * calculated flow ID on the mbuf, such as one provided in hardware, the 1691eaf0ac3Slogwang * receive interface pointed to by the mbuf (if any), the optional source 1701eaf0ac3Slogwang * identifier passed at some dispatch points, or even parse packet headers to 1711eaf0ac3Slogwang * calculate a flow. Both protocol handlers may return a new mbuf pointer 1721eaf0ac3Slogwang * for the chain, or NULL if the packet proves invalid or m_pullup() fails. 1731eaf0ac3Slogwang * 1741eaf0ac3Slogwang * XXXRW: If we eventually support dynamic reconfiguration, there should be 1751eaf0ac3Slogwang * protocol handlers to notify them of CPU configuration changes so that they 1761eaf0ac3Slogwang * can rebalance work. 1771eaf0ac3Slogwang */ 1781eaf0ac3Slogwang struct mbuf; 1791eaf0ac3Slogwang typedef void netisr_handler_t(struct mbuf *m); 1801eaf0ac3Slogwang typedef struct mbuf *netisr_m2cpuid_t(struct mbuf *m, uintptr_t source, 1811eaf0ac3Slogwang u_int *cpuid); 1821eaf0ac3Slogwang typedef struct mbuf *netisr_m2flow_t(struct mbuf *m, uintptr_t source); 1831eaf0ac3Slogwang typedef void netisr_drainedcpu_t(u_int cpuid); 1841eaf0ac3Slogwang 1851eaf0ac3Slogwang #define NETISR_CPUID_NONE ((u_int)-1) /* No affinity returned. */ 1861eaf0ac3Slogwang 1871eaf0ac3Slogwang /* 1881eaf0ac3Slogwang * Data structure describing a protocol handler. 1891eaf0ac3Slogwang */ 1901eaf0ac3Slogwang struct netisr_handler { 1911eaf0ac3Slogwang const char *nh_name; /* Character string protocol name. */ 1921eaf0ac3Slogwang netisr_handler_t *nh_handler; /* Protocol handler. */ 1931eaf0ac3Slogwang netisr_m2flow_t *nh_m2flow; /* Query flow for untagged packet. */ 1941eaf0ac3Slogwang netisr_m2cpuid_t *nh_m2cpuid; /* Query CPU to process mbuf on. */ 1951eaf0ac3Slogwang netisr_drainedcpu_t *nh_drainedcpu; /* Callback when drained a queue. */ 1961eaf0ac3Slogwang u_int nh_proto; /* Integer protocol ID. */ 1971eaf0ac3Slogwang u_int nh_qlimit; /* Maximum per-CPU queue depth. */ 1981eaf0ac3Slogwang u_int nh_policy; /* Work placement policy. */ 1991eaf0ac3Slogwang u_int nh_dispatch; /* Dispatch policy. */ 2001eaf0ac3Slogwang u_int nh_ispare[4]; /* For future use. */ 2011eaf0ac3Slogwang void *nh_pspare[4]; /* For future use. */ 2021eaf0ac3Slogwang }; 2031eaf0ac3Slogwang 2041eaf0ac3Slogwang /* 2051eaf0ac3Slogwang * Register, unregister, and other netisr handler management functions. 2061eaf0ac3Slogwang */ 2071eaf0ac3Slogwang void netisr_clearqdrops(const struct netisr_handler *nhp); 2081eaf0ac3Slogwang void netisr_getqdrops(const struct netisr_handler *nhp, 2091eaf0ac3Slogwang u_int64_t *qdropsp); 2101eaf0ac3Slogwang void netisr_getqlimit(const struct netisr_handler *nhp, u_int *qlimitp); 2111eaf0ac3Slogwang void netisr_register(const struct netisr_handler *nhp); 2121eaf0ac3Slogwang int netisr_setqlimit(const struct netisr_handler *nhp, u_int qlimit); 2131eaf0ac3Slogwang void netisr_unregister(const struct netisr_handler *nhp); 2141eaf0ac3Slogwang #ifdef VIMAGE 2151eaf0ac3Slogwang void netisr_register_vnet(const struct netisr_handler *nhp); 2161eaf0ac3Slogwang void netisr_unregister_vnet(const struct netisr_handler *nhp); 2171eaf0ac3Slogwang #endif 2181eaf0ac3Slogwang 2191eaf0ac3Slogwang /* 2201eaf0ac3Slogwang * Process a packet destined for a protocol, and attempt direct dispatch. 2211eaf0ac3Slogwang * Supplemental source ordering information can be passed using the _src 2221eaf0ac3Slogwang * variant. 2231eaf0ac3Slogwang */ 2241eaf0ac3Slogwang int netisr_dispatch(u_int proto, struct mbuf *m); 2251eaf0ac3Slogwang int netisr_dispatch_src(u_int proto, uintptr_t source, struct mbuf *m); 2261eaf0ac3Slogwang int netisr_queue(u_int proto, struct mbuf *m); 2271eaf0ac3Slogwang int netisr_queue_src(u_int proto, uintptr_t source, struct mbuf *m); 2281eaf0ac3Slogwang 2291eaf0ac3Slogwang /* 2301eaf0ac3Slogwang * Provide a default implementation of "map an ID to a CPU ID". 2311eaf0ac3Slogwang */ 2321eaf0ac3Slogwang u_int netisr_default_flow2cpu(u_int flowid); 2331eaf0ac3Slogwang 2341eaf0ac3Slogwang /* 2351eaf0ac3Slogwang * Utility routines to return the number of CPUs participting in netisr, and 2361eaf0ac3Slogwang * to return a mapping from a number to a CPU ID that can be used with the 2371eaf0ac3Slogwang * scheduler. 2381eaf0ac3Slogwang */ 2391eaf0ac3Slogwang u_int netisr_get_cpucount(void); 2401eaf0ac3Slogwang u_int netisr_get_cpuid(u_int cpunumber); 2411eaf0ac3Slogwang 2421eaf0ac3Slogwang /* 2431eaf0ac3Slogwang * Interfaces between DEVICE_POLLING and netisr. 2441eaf0ac3Slogwang */ 2451eaf0ac3Slogwang void netisr_sched_poll(void); 2461eaf0ac3Slogwang void netisr_poll(void); 2471eaf0ac3Slogwang void netisr_pollmore(void); 2481eaf0ac3Slogwang 2491eaf0ac3Slogwang #endif /* !_KERNEL */ 2501eaf0ac3Slogwang #endif /* !_NET_NETISR_H_ */ 251