npf_worker.c revision 1.6 1 1.1 rmind /*-
2 1.2 christos * Copyright (c) 2010-2015 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.2 christos #ifdef _KERNEL
31 1.1 rmind #include <sys/cdefs.h>
32 1.6 rmind __KERNEL_RCSID(0, "$NetBSD: npf_worker.c,v 1.6 2019/01/19 21:19:32 rmind Exp $");
33 1.1 rmind
34 1.1 rmind #include <sys/param.h>
35 1.1 rmind #include <sys/types.h>
36 1.1 rmind
37 1.1 rmind #include <sys/mutex.h>
38 1.2 christos #include <sys/kmem.h>
39 1.1 rmind #include <sys/kernel.h>
40 1.1 rmind #include <sys/kthread.h>
41 1.2 christos #include <sys/cprng.h>
42 1.2 christos #endif
43 1.1 rmind
44 1.1 rmind #include "npf_impl.h"
45 1.1 rmind
46 1.2 christos typedef struct npf_worker {
47 1.2 christos kmutex_t worker_lock;
48 1.2 christos kcondvar_t worker_cv;
49 1.2 christos npf_workfunc_t work_funcs[NPF_MAX_WORKS];
50 1.2 christos bool worker_exit;
51 1.2 christos lwp_t * worker_lwp;
52 1.2 christos npf_t * instances;
53 1.2 christos } npf_worker_t;
54 1.1 rmind
55 1.2 christos #define W_INTERVAL mstohz(1 * 1000)
56 1.1 rmind
57 1.2 christos static void npf_worker(void *) __dead;
58 1.1 rmind
59 1.2 christos static npf_worker_t * npf_workers __read_mostly;
60 1.2 christos static unsigned npf_worker_count __read_mostly;
61 1.1 rmind
62 1.1 rmind int
63 1.2 christos npf_worker_sysinit(unsigned nworkers)
64 1.1 rmind {
65 1.6 rmind if (nworkers) {
66 1.6 rmind const size_t len = sizeof(npf_worker_t) * nworkers;
67 1.6 rmind npf_workers = kmem_zalloc(len, KM_SLEEP);
68 1.6 rmind } else {
69 1.6 rmind npf_workers = NULL;
70 1.6 rmind }
71 1.2 christos npf_worker_count = nworkers;
72 1.2 christos
73 1.2 christos for (unsigned i = 0; i < nworkers; i++) {
74 1.2 christos npf_worker_t *wrk = &npf_workers[i];
75 1.2 christos
76 1.2 christos mutex_init(&wrk->worker_lock, MUTEX_DEFAULT, IPL_SOFTNET);
77 1.2 christos cv_init(&wrk->worker_cv, "npfgccv");
78 1.2 christos
79 1.3 rmind if (kthread_create(PRI_NONE, KTHREAD_MPSAFE | KTHREAD_MUSTJOIN,
80 1.3 rmind NULL, npf_worker, wrk, &wrk->worker_lwp, "npfgc-%u", i)) {
81 1.2 christos npf_worker_sysfini();
82 1.2 christos return ENOMEM;
83 1.2 christos }
84 1.1 rmind }
85 1.1 rmind return 0;
86 1.1 rmind }
87 1.1 rmind
88 1.1 rmind void
89 1.1 rmind npf_worker_sysfini(void)
90 1.1 rmind {
91 1.2 christos for (unsigned i = 0; i < npf_worker_count; i++) {
92 1.2 christos npf_worker_t *wrk = &npf_workers[i];
93 1.2 christos
94 1.2 christos /* Notify the worker and wait for the exit. */
95 1.2 christos mutex_enter(&wrk->worker_lock);
96 1.2 christos wrk->worker_exit = true;
97 1.2 christos cv_broadcast(&wrk->worker_cv);
98 1.2 christos mutex_exit(&wrk->worker_lock);
99 1.2 christos
100 1.2 christos if (wrk->worker_lwp) {
101 1.2 christos kthread_join(wrk->worker_lwp);
102 1.2 christos }
103 1.1 rmind
104 1.2 christos /* LWP has exited, destroy the structures. */
105 1.2 christos cv_destroy(&wrk->worker_cv);
106 1.2 christos mutex_destroy(&wrk->worker_lock);
107 1.2 christos }
108 1.6 rmind if (npf_workers) {
109 1.6 rmind const size_t len = sizeof(npf_worker_t) * npf_worker_count;
110 1.6 rmind kmem_free(npf_workers, len);
111 1.6 rmind }
112 1.1 rmind }
113 1.1 rmind
114 1.1 rmind void
115 1.2 christos npf_worker_signal(npf_t *npf)
116 1.1 rmind {
117 1.2 christos const unsigned idx = npf->worker_id;
118 1.2 christos npf_worker_t *wrk = &npf_workers[idx];
119 1.2 christos
120 1.2 christos mutex_enter(&wrk->worker_lock);
121 1.2 christos cv_signal(&wrk->worker_cv);
122 1.2 christos mutex_exit(&wrk->worker_lock);
123 1.1 rmind }
124 1.1 rmind
125 1.1 rmind static bool
126 1.2 christos npf_worker_testset(npf_worker_t *wrk, npf_workfunc_t find, npf_workfunc_t set)
127 1.1 rmind {
128 1.1 rmind for (u_int i = 0; i < NPF_MAX_WORKS; i++) {
129 1.2 christos if (wrk->work_funcs[i] == find) {
130 1.2 christos wrk->work_funcs[i] = set;
131 1.1 rmind return true;
132 1.1 rmind }
133 1.1 rmind }
134 1.1 rmind return false;
135 1.1 rmind }
136 1.1 rmind
137 1.1 rmind void
138 1.2 christos npf_worker_register(npf_t *npf, npf_workfunc_t func)
139 1.1 rmind {
140 1.6 rmind npf_worker_t *wrk;
141 1.6 rmind unsigned idx;
142 1.6 rmind
143 1.6 rmind if (!npf_worker_count) {
144 1.6 rmind return;
145 1.6 rmind }
146 1.2 christos
147 1.6 rmind idx = cprng_fast32() % npf_worker_count;
148 1.6 rmind wrk = &npf_workers[idx];
149 1.2 christos mutex_enter(&wrk->worker_lock);
150 1.2 christos
151 1.2 christos npf->worker_id = idx;
152 1.2 christos npf->worker_entry = wrk->instances;
153 1.2 christos wrk->instances = npf;
154 1.2 christos
155 1.2 christos npf_worker_testset(wrk, NULL, func);
156 1.2 christos mutex_exit(&wrk->worker_lock);
157 1.1 rmind }
158 1.1 rmind
159 1.1 rmind void
160 1.2 christos npf_worker_unregister(npf_t *npf, npf_workfunc_t func)
161 1.1 rmind {
162 1.2 christos const unsigned idx = npf->worker_id;
163 1.4 rmind npf_worker_t *wrk;
164 1.2 christos npf_t *instance;
165 1.2 christos
166 1.6 rmind if (!npf_worker_count) {
167 1.4 rmind return;
168 1.6 rmind }
169 1.4 rmind wrk = &npf_workers[idx];
170 1.6 rmind
171 1.2 christos mutex_enter(&wrk->worker_lock);
172 1.2 christos npf_worker_testset(wrk, func, NULL);
173 1.2 christos if ((instance = wrk->instances) == npf) {
174 1.2 christos wrk->instances = instance->worker_entry;
175 1.2 christos } else while (instance) {
176 1.2 christos if (instance->worker_entry == npf) {
177 1.2 christos instance->worker_entry = npf->worker_entry;
178 1.2 christos break;
179 1.2 christos }
180 1.2 christos instance = instance->worker_entry;
181 1.1 rmind }
182 1.2 christos mutex_exit(&wrk->worker_lock);
183 1.1 rmind }
184 1.1 rmind
185 1.1 rmind static void
186 1.1 rmind npf_worker(void *arg)
187 1.1 rmind {
188 1.2 christos npf_worker_t *wrk = arg;
189 1.2 christos
190 1.2 christos KASSERT(wrk != NULL);
191 1.2 christos
192 1.2 christos while (!wrk->worker_exit) {
193 1.2 christos npf_t *npf;
194 1.2 christos
195 1.2 christos npf = wrk->instances;
196 1.2 christos while (npf) {
197 1.2 christos u_int i = NPF_MAX_WORKS;
198 1.2 christos npf_workfunc_t work;
199 1.2 christos
200 1.6 rmind if (!npf->sync_registered) {
201 1.6 rmind npf_thread_register(npf);
202 1.6 rmind npf->sync_registered = true;
203 1.6 rmind }
204 1.6 rmind
205 1.2 christos /* Run the jobs. */
206 1.2 christos while (i--) {
207 1.2 christos if ((work = wrk->work_funcs[i]) != NULL) {
208 1.2 christos work(npf);
209 1.2 christos }
210 1.1 rmind }
211 1.2 christos /* Next .. */
212 1.2 christos npf = npf->worker_entry;
213 1.1 rmind }
214 1.2 christos if (wrk->worker_exit)
215 1.1 rmind break;
216 1.1 rmind
217 1.1 rmind /* Sleep and periodically wake up, unless we get notified. */
218 1.2 christos mutex_enter(&wrk->worker_lock);
219 1.2 christos cv_timedwait(&wrk->worker_cv, &wrk->worker_lock, W_INTERVAL);
220 1.2 christos mutex_exit(&wrk->worker_lock);
221 1.1 rmind }
222 1.1 rmind kthread_exit(0);
223 1.1 rmind }
224