netisr.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
  3. *
  4. * Copyright (c) 2007-2009 Robert N. M. Watson
  5. * Copyright (c) 2010-2011 Juniper Networks, Inc.
  6. * All rights reserved.
  7. *
  8. * This software was developed by Robert N. M. Watson under contract
  9. * to Juniper Networks, Inc.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  21. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * $FreeBSD$
  33. */
  34. #ifndef _NET_NETISR_H_
  35. #define _NET_NETISR_H_
  36. /*
  37. * The netisr (network interrupt service routine) provides a deferred
  38. * execution evironment in which (generally inbound) network processing can
  39. * take place. Protocols register handlers which will be executed directly,
  40. * or via deferred dispatch, depending on the circumstances.
  41. *
  42. * Historically, this was implemented by the BSD software ISR facility; it is
  43. * now implemented via a software ithread (SWI).
  44. */
  45. /*
  46. * Protocol numbers, which are encoded in monitoring applications and kernel
  47. * modules. Internally, these are used in bit shift operations so must have
  48. * a value 0 < proto < 32; we currently further limit at compile-time to 16
  49. * for array-sizing purposes.
  50. */
  51. #define NETISR_IP 1
  52. #define NETISR_IGMP 2 /* IGMPv3 output queue */
  53. #define NETISR_ROUTE 3 /* routing socket */
  54. #define NETISR_ARP 4 /* same as AF_LINK */
  55. #define NETISR_ETHER 5 /* ethernet input */
  56. #define NETISR_IPV6 6
  57. #define NETISR_EPAIR 8 /* if_epair(4) */
  58. #define NETISR_IP_DIRECT 9 /* direct-dispatch IPv4 */
  59. #define NETISR_IPV6_DIRECT 10 /* direct-dispatch IPv6 */
  60. /*
  61. * Protocol ordering and affinity policy constants. See the detailed
  62. * discussion of policies later in the file.
  63. */
  64. #define NETISR_POLICY_SOURCE 1 /* Maintain source ordering. */
  65. #define NETISR_POLICY_FLOW 2 /* Maintain flow ordering. */
  66. #define NETISR_POLICY_CPU 3 /* Protocol determines CPU placement. */
  67. /*
  68. * Protocol dispatch policy constants; selects whether and when direct
  69. * dispatch is permitted.
  70. */
  71. #define NETISR_DISPATCH_DEFAULT 0 /* Use global default. */
  72. #define NETISR_DISPATCH_DEFERRED 1 /* Always defer dispatch. */
  73. #define NETISR_DISPATCH_HYBRID 2 /* Allow hybrid dispatch. */
  74. #define NETISR_DISPATCH_DIRECT 3 /* Always direct dispatch. */
  75. /*
  76. * Monitoring data structures, exported by sysctl(2).
  77. *
  78. * Three sysctls are defined. First, a per-protocol structure exported by
  79. * net.isr.proto.
  80. */
  81. #define NETISR_NAMEMAXLEN 32
  82. struct sysctl_netisr_proto {
  83. u_int snp_version; /* Length of struct. */
  84. char snp_name[NETISR_NAMEMAXLEN]; /* nh_name */
  85. u_int snp_proto; /* nh_proto */
  86. u_int snp_qlimit; /* nh_qlimit */
  87. u_int snp_policy; /* nh_policy */
  88. u_int snp_flags; /* Various flags. */
  89. u_int snp_dispatch; /* Dispatch policy. */
  90. u_int _snp_ispare[6];
  91. };
  92. /*
  93. * Flags for sysctl_netisr_proto.snp_flags.
  94. */
  95. #define NETISR_SNP_FLAGS_M2FLOW 0x00000001 /* nh_m2flow */
  96. #define NETISR_SNP_FLAGS_M2CPUID 0x00000002 /* nh_m2cpuid */
  97. #define NETISR_SNP_FLAGS_DRAINEDCPU 0x00000004 /* nh_drainedcpu */
  98. /*
  99. * Next, a structure per-workstream, with per-protocol data, exported as
  100. * net.isr.workstream.
  101. */
  102. struct sysctl_netisr_workstream {
  103. u_int snws_version; /* Length of struct. */
  104. u_int snws_flags; /* Various flags. */
  105. u_int snws_wsid; /* Workstream ID. */
  106. u_int snws_cpu; /* nws_cpu */
  107. u_int _snws_ispare[12];
  108. };
  109. /*
  110. * Flags for sysctl_netisr_workstream.snws_flags
  111. */
  112. #define NETISR_SNWS_FLAGS_INTR 0x00000001 /* nws_intr_event */
  113. /*
  114. * Finally, a per-workstream-per-protocol structure, exported as
  115. * net.isr.work.
  116. */
  117. struct sysctl_netisr_work {
  118. u_int snw_version; /* Length of struct. */
  119. u_int snw_wsid; /* Workstream ID. */
  120. u_int snw_proto; /* Protocol number. */
  121. u_int snw_len; /* nw_len */
  122. u_int snw_watermark; /* nw_watermark */
  123. u_int _snw_ispare[3];
  124. uint64_t snw_dispatched; /* nw_dispatched */
  125. uint64_t snw_hybrid_dispatched; /* nw_hybrid_dispatched */
  126. uint64_t snw_qdrops; /* nw_qdrops */
  127. uint64_t snw_queued; /* nw_queued */
  128. uint64_t snw_handled; /* nw_handled */
  129. uint64_t _snw_llspare[7];
  130. };
  131. #ifdef _KERNEL
  132. /*-
  133. * Protocols express ordering constraints and affinity preferences by
  134. * implementing one or neither of nh_m2flow and nh_m2cpuid, which are used by
  135. * netisr to determine which per-CPU workstream to assign mbufs to.
  136. *
  137. * The following policies may be used by protocols:
  138. *
  139. * NETISR_POLICY_SOURCE - netisr should maintain source ordering without
  140. * advice from the protocol. netisr will ignore any
  141. * flow IDs present on the mbuf for the purposes of
  142. * work placement.
  143. *
  144. * NETISR_POLICY_FLOW - netisr should maintain flow ordering as defined by
  145. * the mbuf header flow ID field. If the protocol
  146. * implements nh_m2flow, then netisr will query the
  147. * protocol in the event that the mbuf doesn't have a
  148. * flow ID, falling back on source ordering.
  149. *
  150. * NETISR_POLICY_CPU - netisr will delegate all work placement decisions to
  151. * the protocol, querying nh_m2cpuid for each packet.
  152. *
  153. * Protocols might make decisions about work placement based on an existing
  154. * calculated flow ID on the mbuf, such as one provided in hardware, the
  155. * receive interface pointed to by the mbuf (if any), the optional source
  156. * identifier passed at some dispatch points, or even parse packet headers to
  157. * calculate a flow. Both protocol handlers may return a new mbuf pointer
  158. * for the chain, or NULL if the packet proves invalid or m_pullup() fails.
  159. *
  160. * XXXRW: If we eventually support dynamic reconfiguration, there should be
  161. * protocol handlers to notify them of CPU configuration changes so that they
  162. * can rebalance work.
  163. */
  164. struct mbuf;
  165. typedef void netisr_handler_t(struct mbuf *m);
  166. typedef struct mbuf *netisr_m2cpuid_t(struct mbuf *m, uintptr_t source,
  167. u_int *cpuid);
  168. typedef struct mbuf *netisr_m2flow_t(struct mbuf *m, uintptr_t source);
  169. typedef void netisr_drainedcpu_t(u_int cpuid);
  170. #define NETISR_CPUID_NONE ((u_int)-1) /* No affinity returned. */
  171. /*
  172. * Data structure describing a protocol handler.
  173. */
  174. struct netisr_handler {
  175. const char *nh_name; /* Character string protocol name. */
  176. netisr_handler_t *nh_handler; /* Protocol handler. */
  177. netisr_m2flow_t *nh_m2flow; /* Query flow for untagged packet. */
  178. netisr_m2cpuid_t *nh_m2cpuid; /* Query CPU to process mbuf on. */
  179. netisr_drainedcpu_t *nh_drainedcpu; /* Callback when drained a queue. */
  180. u_int nh_proto; /* Integer protocol ID. */
  181. u_int nh_qlimit; /* Maximum per-CPU queue depth. */
  182. u_int nh_policy; /* Work placement policy. */
  183. u_int nh_dispatch; /* Dispatch policy. */
  184. u_int nh_ispare[4]; /* For future use. */
  185. void *nh_pspare[4]; /* For future use. */
  186. };
  187. /*
  188. * Register, unregister, and other netisr handler management functions.
  189. */
  190. void netisr_clearqdrops(const struct netisr_handler *nhp);
  191. void netisr_getqdrops(const struct netisr_handler *nhp,
  192. u_int64_t *qdropsp);
  193. void netisr_getqlimit(const struct netisr_handler *nhp, u_int *qlimitp);
  194. void netisr_register(const struct netisr_handler *nhp);
  195. int netisr_setqlimit(const struct netisr_handler *nhp, u_int qlimit);
  196. void netisr_unregister(const struct netisr_handler *nhp);
  197. #ifdef VIMAGE
  198. void netisr_register_vnet(const struct netisr_handler *nhp);
  199. void netisr_unregister_vnet(const struct netisr_handler *nhp);
  200. #endif
  201. /*
  202. * Process a packet destined for a protocol, and attempt direct dispatch.
  203. * Supplemental source ordering information can be passed using the _src
  204. * variant.
  205. */
  206. int netisr_dispatch(u_int proto, struct mbuf *m);
  207. int netisr_dispatch_src(u_int proto, uintptr_t source, struct mbuf *m);
  208. int netisr_queue(u_int proto, struct mbuf *m);
  209. int netisr_queue_src(u_int proto, uintptr_t source, struct mbuf *m);
  210. /*
  211. * Provide a default implementation of "map an ID to a CPU ID".
  212. */
  213. u_int netisr_default_flow2cpu(u_int flowid);
  214. /*
  215. * Utility routines to return the number of CPUs participting in netisr, and
  216. * to return a mapping from a number to a CPU ID that can be used with the
  217. * scheduler.
  218. */
  219. u_int netisr_get_cpucount(void);
  220. u_int netisr_get_cpuid(u_int cpunumber);
  221. /*
  222. * Interfaces between DEVICE_POLLING and netisr.
  223. */
  224. void netisr_sched_poll(void);
  225. void netisr_poll(void);
  226. void netisr_pollmore(void);
  227. #endif /* !_KERNEL */
  228. #endif /* !_NET_NETISR_H_ */