1 /* $NetBSD: npf.c,v 1.33 2016/12/26 23:05:06 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 2009-2013 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This material is based upon work partially supported by The 8 * NetBSD Foundation under a contract with Mindaugas Rasiukevicius. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * NPF main: dynamic load/initialisation and unload routines. 34 */ 35 36 #ifdef _KERNEL 37 #include <sys/cdefs.h> 38 __KERNEL_RCSID(0, "$NetBSD: npf.c,v 1.33 2016/12/26 23:05:06 christos Exp $"); 39 40 #include <sys/param.h> 41 #include <sys/types.h> 42 43 #include <sys/conf.h> 44 #include <sys/kmem.h> 45 #include <sys/percpu.h> 46 #endif 47 48 #include "npf_impl.h" 49 #include "npf_conn.h" 50 51 __read_mostly static npf_t * npf_kernel_ctx = NULL; 52 53 __dso_public int 54 npf_sysinit(unsigned nworkers) 55 { 56 npf_bpf_sysinit(); 57 npf_tableset_sysinit(); 58 npf_nat_sysinit(); 59 return npf_worker_sysinit(nworkers); 60 } 61 62 __dso_public void 63 npf_sysfini(void) 64 { 65 npf_worker_sysfini(); 66 npf_nat_sysfini(); 67 npf_tableset_sysfini(); 68 npf_bpf_sysfini(); 69 } 70 71 __dso_public npf_t * 72 npf_create(int flags, const npf_mbufops_t *mbufops, const npf_ifops_t *ifops) 73 { 74 npf_t *npf; 75 76 npf = kmem_zalloc(sizeof(npf_t), KM_SLEEP); 77 npf->qsbr = pserialize_create(); 78 if (!npf->qsbr) { 79 kmem_free(npf, sizeof(npf_t)); 80 return NULL; 81 } 82 npf->stats_percpu = percpu_alloc(NPF_STATS_SIZE); 83 npf->mbufops = mbufops; 84 85 npf_ifmap_init(npf, ifops); 86 npf_conn_init(npf, flags); 87 npf_alg_init(npf); 88 npf_ext_init(npf); 89 90 /* Load an empty configuration. */ 91 npf_config_init(npf); 92 return npf; 93 } 94 95 __dso_public void 96 npf_destroy(npf_t *npf) 97 { 98 /* 99 * Destroy the current configuration. Note: at this point all 100 * handlers must be deactivated; we will drain any processing. 101 */ 102 npf_config_fini(npf); 103 104 /* Finally, safe to destroy the subsystems. */ 105 npf_ext_fini(npf); 106 npf_alg_fini(npf); 107 npf_conn_fini(npf); 108 npf_ifmap_fini(npf); 109 110 pserialize_destroy(npf->qsbr); 111 percpu_free(npf->stats_percpu, NPF_STATS_SIZE); 112 kmem_free(npf, sizeof(npf_t)); 113 } 114 115 __dso_public int 116 npf_load(npf_t *npf, void *ref, npf_error_t *err) 117 { 118 return npfctl_load(npf, 0, ref); 119 } 120 121 __dso_public void 122 npf_gc(npf_t *npf) 123 { 124 npf_conn_worker(npf); 125 } 126 127 __dso_public void 128 npf_thread_register(npf_t *npf) 129 { 130 pserialize_register(npf->qsbr); 131 } 132 133 void 134 npf_setkernctx(npf_t *npf) 135 { 136 npf_kernel_ctx = npf; 137 } 138 139 npf_t * 140 npf_getkernctx(void) 141 { 142 return npf_kernel_ctx; 143 } 144 145 /* 146 * NPF statistics interface. 147 */ 148 149 void 150 npf_stats_inc(npf_t *npf, npf_stats_t st) 151 { 152 uint64_t *stats = percpu_getref(npf->stats_percpu); 153 stats[st]++; 154 percpu_putref(npf->stats_percpu); 155 } 156 157 void 158 npf_stats_dec(npf_t *npf, npf_stats_t st) 159 { 160 uint64_t *stats = percpu_getref(npf->stats_percpu); 161 stats[st]--; 162 percpu_putref(npf->stats_percpu); 163 } 164 165 static void 166 npf_stats_collect(void *mem, void *arg, struct cpu_info *ci) 167 { 168 uint64_t *percpu_stats = mem, *full_stats = arg; 169 170 for (unsigned i = 0; i < NPF_STATS_COUNT; i++) { 171 full_stats[i] += percpu_stats[i]; 172 } 173 } 174 175 /* 176 * npf_stats: export collected statistics. 177 */ 178 __dso_public void 179 npf_stats(npf_t *npf, uint64_t *buf) 180 { 181 memset(buf, 0, NPF_STATS_SIZE); 182 percpu_foreach(npf->stats_percpu, npf_stats_collect, buf); 183 } 184