1 1.1 rmind /*- 2 1.15 rmind * Copyright (c) 2009-2013 The NetBSD Foundation, Inc. 3 1.1 rmind * All rights reserved. 4 1.1 rmind * 5 1.1 rmind * This material is based upon work partially supported by The 6 1.1 rmind * NetBSD Foundation under a contract with Mindaugas Rasiukevicius. 7 1.1 rmind * 8 1.1 rmind * Redistribution and use in source and binary forms, with or without 9 1.1 rmind * modification, are permitted provided that the following conditions 10 1.1 rmind * are met: 11 1.1 rmind * 1. Redistributions of source code must retain the above copyright 12 1.1 rmind * notice, this list of conditions and the following disclaimer. 13 1.1 rmind * 2. Redistributions in binary form must reproduce the above copyright 14 1.1 rmind * notice, this list of conditions and the following disclaimer in the 15 1.1 rmind * documentation and/or other materials provided with the distribution. 16 1.1 rmind * 17 1.1 rmind * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 18 1.1 rmind * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 19 1.1 rmind * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 1.1 rmind * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 21 1.1 rmind * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 1.1 rmind * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 1.1 rmind * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 1.1 rmind * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 1.1 rmind * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 1.1 rmind * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 1.1 rmind * POSSIBILITY OF SUCH DAMAGE. 28 1.1 rmind */ 29 1.1 rmind 30 1.1 rmind /* 31 1.1 rmind * NPF main: dynamic load/initialisation and unload routines. 32 1.1 rmind */ 33 1.1 rmind 34 1.33 christos #ifdef _KERNEL 35 1.1 rmind #include <sys/cdefs.h> 36 1.44 riastrad __KERNEL_RCSID(0, "$NetBSD: npf.c,v 1.44 2020/08/27 18:50:25 riastradh Exp $"); 37 1.1 rmind 38 1.1 rmind #include <sys/param.h> 39 1.1 rmind #include <sys/types.h> 40 1.1 rmind 41 1.1 rmind #include <sys/conf.h> 42 1.2 rmind #include <sys/kmem.h> 43 1.2 rmind #include <sys/percpu.h> 44 1.42 thorpej #include <sys/xcall.h> 45 1.33 christos #endif 46 1.1 rmind 47 1.1 rmind #include "npf_impl.h" 48 1.20 rmind #include "npf_conn.h" 49 1.1 rmind 50 1.36 rmind static __read_mostly npf_t * npf_kernel_ctx = NULL; 51 1.23 christos 52 1.33 christos __dso_public int 53 1.40 rmind npfk_sysinit(unsigned nworkers) 54 1.1 rmind { 55 1.44 riastrad 56 1.17 rmind npf_bpf_sysinit(); 57 1.2 rmind npf_tableset_sysinit(); 58 1.1 rmind npf_nat_sysinit(); 59 1.44 riastrad npf_portmap_sysinit(); 60 1.33 christos return npf_worker_sysinit(nworkers); 61 1.1 rmind } 62 1.1 rmind 63 1.33 christos __dso_public void 64 1.40 rmind npfk_sysfini(void) 65 1.1 rmind { 66 1.44 riastrad 67 1.33 christos npf_worker_sysfini(); 68 1.44 riastrad npf_portmap_sysfini(); 69 1.1 rmind npf_nat_sysfini(); 70 1.1 rmind npf_tableset_sysfini(); 71 1.17 rmind npf_bpf_sysfini(); 72 1.1 rmind } 73 1.1 rmind 74 1.33 christos __dso_public npf_t * 75 1.43 rmind npfk_create(int flags, const npf_mbufops_t *mbufops, 76 1.43 rmind const npf_ifops_t *ifops, void *arg) 77 1.1 rmind { 78 1.33 christos npf_t *npf; 79 1.1 rmind 80 1.33 christos npf = kmem_zalloc(sizeof(npf_t), KM_SLEEP); 81 1.41 rmind npf->ebr = npf_ebr_create(); 82 1.33 christos npf->stats_percpu = percpu_alloc(NPF_STATS_SIZE); 83 1.33 christos npf->mbufops = mbufops; 84 1.43 rmind npf->arg = arg; 85 1.33 christos 86 1.38 rmind npf_param_init(npf); 87 1.38 rmind npf_state_sysinit(npf); 88 1.33 christos npf_ifmap_init(npf, ifops); 89 1.39 christos npf_conn_init(npf); 90 1.38 rmind npf_portmap_init(npf); 91 1.33 christos npf_alg_init(npf); 92 1.33 christos npf_ext_init(npf); 93 1.33 christos 94 1.33 christos /* Load an empty configuration. */ 95 1.33 christos npf_config_init(npf); 96 1.39 christos 97 1.39 christos if ((flags & NPF_NO_GC) == 0) { 98 1.43 rmind npf_worker_enlist(npf); 99 1.39 christos } 100 1.33 christos return npf; 101 1.33 christos } 102 1.33 christos 103 1.33 christos __dso_public void 104 1.40 rmind npfk_destroy(npf_t *npf) 105 1.33 christos { 106 1.43 rmind npf_worker_discharge(npf); 107 1.43 rmind 108 1.33 christos /* 109 1.33 christos * Destroy the current configuration. Note: at this point all 110 1.33 christos * handlers must be deactivated; we will drain any processing. 111 1.33 christos */ 112 1.33 christos npf_config_fini(npf); 113 1.1 rmind 114 1.33 christos /* Finally, safe to destroy the subsystems. */ 115 1.33 christos npf_ext_fini(npf); 116 1.33 christos npf_alg_fini(npf); 117 1.38 rmind npf_portmap_fini(npf); 118 1.33 christos npf_conn_fini(npf); 119 1.33 christos npf_ifmap_fini(npf); 120 1.38 rmind npf_state_sysfini(npf); 121 1.38 rmind npf_param_fini(npf); 122 1.1 rmind 123 1.41 rmind npf_ebr_destroy(npf->ebr); 124 1.33 christos percpu_free(npf->stats_percpu, NPF_STATS_SIZE); 125 1.33 christos kmem_free(npf, sizeof(npf_t)); 126 1.1 rmind } 127 1.1 rmind 128 1.43 rmind 129 1.43 rmind /* 130 1.43 rmind * npfk_load: (re)load the configuration. 131 1.43 rmind * 132 1.43 rmind * => Will not modify the configuration reference. 133 1.43 rmind */ 134 1.33 christos __dso_public int 135 1.43 rmind npfk_load(npf_t *npf, const void *config_ref, npf_error_t *err) 136 1.1 rmind { 137 1.43 rmind const nvlist_t *req = (const nvlist_t *)config_ref; 138 1.43 rmind nvlist_t *resp; 139 1.43 rmind int error; 140 1.43 rmind 141 1.43 rmind resp = nvlist_create(0); 142 1.43 rmind error = npfctl_run_op(npf, IOC_NPF_LOAD, req, resp); 143 1.43 rmind nvlist_destroy(resp); 144 1.43 rmind 145 1.43 rmind return error; 146 1.1 rmind } 147 1.1 rmind 148 1.33 christos __dso_public void 149 1.40 rmind npfk_gc(npf_t *npf) 150 1.1 rmind { 151 1.33 christos npf_conn_worker(npf); 152 1.1 rmind } 153 1.1 rmind 154 1.33 christos __dso_public void 155 1.40 rmind npfk_thread_register(npf_t *npf) 156 1.1 rmind { 157 1.41 rmind npf_ebr_register(npf->ebr); 158 1.1 rmind } 159 1.1 rmind 160 1.36 rmind __dso_public void 161 1.40 rmind npfk_thread_unregister(npf_t *npf) 162 1.36 rmind { 163 1.41 rmind npf_ebr_full_sync(npf->ebr); 164 1.41 rmind npf_ebr_unregister(npf->ebr); 165 1.36 rmind } 166 1.36 rmind 167 1.43 rmind __dso_public void * 168 1.43 rmind npfk_getarg(npf_t *npf) 169 1.43 rmind { 170 1.43 rmind return npf->arg; 171 1.43 rmind } 172 1.43 rmind 173 1.33 christos void 174 1.33 christos npf_setkernctx(npf_t *npf) 175 1.1 rmind { 176 1.33 christos npf_kernel_ctx = npf; 177 1.1 rmind } 178 1.2 rmind 179 1.33 christos npf_t * 180 1.33 christos npf_getkernctx(void) 181 1.13 rmind { 182 1.33 christos return npf_kernel_ctx; 183 1.13 rmind } 184 1.13 rmind 185 1.2 rmind /* 186 1.2 rmind * NPF statistics interface. 187 1.2 rmind */ 188 1.2 rmind 189 1.2 rmind void 190 1.33 christos npf_stats_inc(npf_t *npf, npf_stats_t st) 191 1.2 rmind { 192 1.33 christos uint64_t *stats = percpu_getref(npf->stats_percpu); 193 1.2 rmind stats[st]++; 194 1.33 christos percpu_putref(npf->stats_percpu); 195 1.2 rmind } 196 1.2 rmind 197 1.2 rmind void 198 1.33 christos npf_stats_dec(npf_t *npf, npf_stats_t st) 199 1.2 rmind { 200 1.33 christos uint64_t *stats = percpu_getref(npf->stats_percpu); 201 1.2 rmind stats[st]--; 202 1.33 christos percpu_putref(npf->stats_percpu); 203 1.2 rmind } 204 1.2 rmind 205 1.2 rmind static void 206 1.2 rmind npf_stats_collect(void *mem, void *arg, struct cpu_info *ci) 207 1.2 rmind { 208 1.2 rmind uint64_t *percpu_stats = mem, *full_stats = arg; 209 1.2 rmind 210 1.33 christos for (unsigned i = 0; i < NPF_STATS_COUNT; i++) { 211 1.2 rmind full_stats[i] += percpu_stats[i]; 212 1.2 rmind } 213 1.2 rmind } 214 1.2 rmind 215 1.38 rmind static void 216 1.38 rmind npf_stats_clear_cb(void *mem, void *arg, struct cpu_info *ci) 217 1.38 rmind { 218 1.38 rmind uint64_t *percpu_stats = mem; 219 1.38 rmind 220 1.38 rmind for (unsigned i = 0; i < NPF_STATS_COUNT; i++) { 221 1.38 rmind percpu_stats[i] = 0; 222 1.38 rmind } 223 1.38 rmind } 224 1.38 rmind 225 1.2 rmind /* 226 1.33 christos * npf_stats: export collected statistics. 227 1.2 rmind */ 228 1.38 rmind 229 1.33 christos __dso_public void 230 1.40 rmind npfk_stats(npf_t *npf, uint64_t *buf) 231 1.2 rmind { 232 1.33 christos memset(buf, 0, NPF_STATS_SIZE); 233 1.42 thorpej percpu_foreach_xcall(npf->stats_percpu, XC_HIGHPRI_IPL(IPL_SOFTNET), 234 1.42 thorpej npf_stats_collect, buf); 235 1.2 rmind } 236 1.38 rmind 237 1.38 rmind __dso_public void 238 1.40 rmind npfk_stats_clear(npf_t *npf) 239 1.38 rmind { 240 1.42 thorpej percpu_foreach_xcall(npf->stats_percpu, XC_HIGHPRI_IPL(IPL_SOFTNET), 241 1.42 thorpej npf_stats_clear_cb, NULL); 242 1.38 rmind } 243