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