npf_worker.c revision 1.2 1 1.2 christos /* $NetBSD: npf_worker.c,v 1.2 2016/12/26 23:05:06 christos Exp $ */
2 1.1 rmind
3 1.1 rmind /*-
4 1.2 christos * Copyright (c) 2010-2015 The NetBSD Foundation, Inc.
5 1.1 rmind * All rights reserved.
6 1.1 rmind *
7 1.1 rmind * This material is based upon work partially supported by The
8 1.1 rmind * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
9 1.1 rmind *
10 1.1 rmind * Redistribution and use in source and binary forms, with or without
11 1.1 rmind * modification, are permitted provided that the following conditions
12 1.1 rmind * are met:
13 1.1 rmind * 1. Redistributions of source code must retain the above copyright
14 1.1 rmind * notice, this list of conditions and the following disclaimer.
15 1.1 rmind * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 rmind * notice, this list of conditions and the following disclaimer in the
17 1.1 rmind * documentation and/or other materials provided with the distribution.
18 1.1 rmind *
19 1.1 rmind * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 rmind * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 rmind * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 rmind * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 rmind * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 rmind * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 rmind * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 rmind * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 rmind * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 rmind * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 rmind * POSSIBILITY OF SUCH DAMAGE.
30 1.1 rmind */
31 1.1 rmind
32 1.2 christos #ifdef _KERNEL
33 1.1 rmind #include <sys/cdefs.h>
34 1.2 christos __KERNEL_RCSID(0, "$NetBSD: npf_worker.c,v 1.2 2016/12/26 23:05:06 christos Exp $");
35 1.1 rmind
36 1.1 rmind #include <sys/param.h>
37 1.1 rmind #include <sys/types.h>
38 1.1 rmind
39 1.1 rmind #include <sys/mutex.h>
40 1.2 christos #include <sys/kmem.h>
41 1.1 rmind #include <sys/kernel.h>
42 1.1 rmind #include <sys/kthread.h>
43 1.2 christos #include <sys/cprng.h>
44 1.2 christos #endif
45 1.1 rmind
46 1.1 rmind #include "npf_impl.h"
47 1.1 rmind
48 1.2 christos typedef struct npf_worker {
49 1.2 christos kmutex_t worker_lock;
50 1.2 christos kcondvar_t worker_cv;
51 1.2 christos npf_workfunc_t work_funcs[NPF_MAX_WORKS];
52 1.2 christos bool worker_exit;
53 1.2 christos lwp_t * worker_lwp;
54 1.2 christos npf_t * instances;
55 1.2 christos } npf_worker_t;
56 1.1 rmind
57 1.2 christos #define W_INTERVAL mstohz(1 * 1000)
58 1.1 rmind
59 1.2 christos static void npf_worker(void *) __dead;
60 1.1 rmind
61 1.2 christos static npf_worker_t * npf_workers __read_mostly;
62 1.2 christos static unsigned npf_worker_count __read_mostly;
63 1.1 rmind
64 1.1 rmind int
65 1.2 christos npf_worker_sysinit(unsigned nworkers)
66 1.1 rmind {
67 1.2 christos npf_workers = kmem_zalloc(sizeof(npf_worker_t) * nworkers, KM_SLEEP);
68 1.2 christos npf_worker_count = nworkers;
69 1.2 christos
70 1.2 christos for (unsigned i = 0; i < nworkers; i++) {
71 1.2 christos npf_worker_t *wrk = &npf_workers[i];
72 1.2 christos
73 1.2 christos mutex_init(&wrk->worker_lock, MUTEX_DEFAULT, IPL_SOFTNET);
74 1.2 christos cv_init(&wrk->worker_cv, "npfgccv");
75 1.2 christos
76 1.2 christos if (kthread_create(PRI_NONE, KTHREAD_MPSAFE |
77 1.2 christos KTHREAD_MUSTJOIN, NULL, npf_worker, wrk, &wrk->worker_lwp,
78 1.2 christos "npfgc-%u", i)) {
79 1.2 christos npf_worker_sysfini();
80 1.2 christos return ENOMEM;
81 1.2 christos }
82 1.1 rmind }
83 1.1 rmind return 0;
84 1.1 rmind }
85 1.1 rmind
86 1.1 rmind void
87 1.1 rmind npf_worker_sysfini(void)
88 1.1 rmind {
89 1.2 christos for (unsigned i = 0; i < npf_worker_count; i++) {
90 1.2 christos npf_worker_t *wrk = &npf_workers[i];
91 1.2 christos
92 1.2 christos /* Notify the worker and wait for the exit. */
93 1.2 christos mutex_enter(&wrk->worker_lock);
94 1.2 christos wrk->worker_exit = true;
95 1.2 christos cv_broadcast(&wrk->worker_cv);
96 1.2 christos mutex_exit(&wrk->worker_lock);
97 1.2 christos
98 1.2 christos if (wrk->worker_lwp) {
99 1.2 christos kthread_join(wrk->worker_lwp);
100 1.2 christos }
101 1.1 rmind
102 1.2 christos /* LWP has exited, destroy the structures. */
103 1.2 christos cv_destroy(&wrk->worker_cv);
104 1.2 christos mutex_destroy(&wrk->worker_lock);
105 1.2 christos }
106 1.2 christos kmem_free(npf_workers, sizeof(npf_worker_t) * npf_worker_count);
107 1.1 rmind }
108 1.1 rmind
109 1.1 rmind void
110 1.2 christos npf_worker_signal(npf_t *npf)
111 1.1 rmind {
112 1.2 christos const unsigned idx = npf->worker_id;
113 1.2 christos npf_worker_t *wrk = &npf_workers[idx];
114 1.2 christos
115 1.2 christos mutex_enter(&wrk->worker_lock);
116 1.2 christos cv_signal(&wrk->worker_cv);
117 1.2 christos mutex_exit(&wrk->worker_lock);
118 1.1 rmind }
119 1.1 rmind
120 1.1 rmind static bool
121 1.2 christos npf_worker_testset(npf_worker_t *wrk, npf_workfunc_t find, npf_workfunc_t set)
122 1.1 rmind {
123 1.1 rmind for (u_int i = 0; i < NPF_MAX_WORKS; i++) {
124 1.2 christos if (wrk->work_funcs[i] == find) {
125 1.2 christos wrk->work_funcs[i] = set;
126 1.1 rmind return true;
127 1.1 rmind }
128 1.1 rmind }
129 1.1 rmind return false;
130 1.1 rmind }
131 1.1 rmind
132 1.1 rmind void
133 1.2 christos npf_worker_register(npf_t *npf, npf_workfunc_t func)
134 1.1 rmind {
135 1.2 christos const unsigned idx = cprng_fast32() % npf_worker_count;
136 1.2 christos npf_worker_t *wrk = &npf_workers[idx];
137 1.2 christos
138 1.2 christos mutex_enter(&wrk->worker_lock);
139 1.2 christos
140 1.2 christos npf->worker_id = idx;
141 1.2 christos npf->worker_entry = wrk->instances;
142 1.2 christos wrk->instances = npf;
143 1.2 christos
144 1.2 christos npf_worker_testset(wrk, NULL, func);
145 1.2 christos mutex_exit(&wrk->worker_lock);
146 1.1 rmind }
147 1.1 rmind
148 1.1 rmind void
149 1.2 christos npf_worker_unregister(npf_t *npf, npf_workfunc_t func)
150 1.1 rmind {
151 1.2 christos const unsigned idx = npf->worker_id;
152 1.2 christos npf_worker_t *wrk = &npf_workers[idx];
153 1.2 christos npf_t *instance;
154 1.2 christos
155 1.2 christos mutex_enter(&wrk->worker_lock);
156 1.2 christos npf_worker_testset(wrk, func, NULL);
157 1.2 christos if ((instance = wrk->instances) == npf) {
158 1.2 christos wrk->instances = instance->worker_entry;
159 1.2 christos } else while (instance) {
160 1.2 christos if (instance->worker_entry == npf) {
161 1.2 christos instance->worker_entry = npf->worker_entry;
162 1.2 christos break;
163 1.2 christos }
164 1.2 christos instance = instance->worker_entry;
165 1.1 rmind }
166 1.2 christos mutex_exit(&wrk->worker_lock);
167 1.1 rmind }
168 1.1 rmind
169 1.1 rmind static void
170 1.1 rmind npf_worker(void *arg)
171 1.1 rmind {
172 1.2 christos npf_worker_t *wrk = arg;
173 1.2 christos
174 1.2 christos KASSERT(wrk != NULL);
175 1.2 christos KASSERT(!wrk->worker_exit);
176 1.2 christos
177 1.2 christos while (!wrk->worker_exit) {
178 1.2 christos npf_t *npf;
179 1.2 christos
180 1.2 christos npf = wrk->instances;
181 1.2 christos while (npf) {
182 1.2 christos u_int i = NPF_MAX_WORKS;
183 1.2 christos npf_workfunc_t work;
184 1.2 christos
185 1.2 christos /* Run the jobs. */
186 1.2 christos while (i--) {
187 1.2 christos if ((work = wrk->work_funcs[i]) != NULL) {
188 1.2 christos work(npf);
189 1.2 christos }
190 1.1 rmind }
191 1.2 christos /* Next .. */
192 1.2 christos npf = npf->worker_entry;
193 1.1 rmind }
194 1.2 christos if (wrk->worker_exit)
195 1.1 rmind break;
196 1.1 rmind
197 1.1 rmind /* Sleep and periodically wake up, unless we get notified. */
198 1.2 christos mutex_enter(&wrk->worker_lock);
199 1.2 christos cv_timedwait(&wrk->worker_cv, &wrk->worker_lock, W_INTERVAL);
200 1.2 christos mutex_exit(&wrk->worker_lock);
201 1.1 rmind }
202 1.1 rmind kthread_exit(0);
203 1.1 rmind }
204