npf_worker.c revision 1.3 1 1.3 rmind /* $NetBSD: npf_worker.c,v 1.3 2017/01/02 21:49:51 rmind 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.3 rmind __KERNEL_RCSID(0, "$NetBSD: npf_worker.c,v 1.3 2017/01/02 21:49:51 rmind 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.3 rmind if (kthread_create(PRI_NONE, KTHREAD_MPSAFE | KTHREAD_MUSTJOIN,
77 1.3 rmind NULL, npf_worker, wrk, &wrk->worker_lwp, "npfgc-%u", i)) {
78 1.2 christos npf_worker_sysfini();
79 1.2 christos return ENOMEM;
80 1.2 christos }
81 1.1 rmind }
82 1.1 rmind return 0;
83 1.1 rmind }
84 1.1 rmind
85 1.1 rmind void
86 1.1 rmind npf_worker_sysfini(void)
87 1.1 rmind {
88 1.2 christos for (unsigned i = 0; i < npf_worker_count; i++) {
89 1.2 christos npf_worker_t *wrk = &npf_workers[i];
90 1.2 christos
91 1.2 christos /* Notify the worker and wait for the exit. */
92 1.2 christos mutex_enter(&wrk->worker_lock);
93 1.2 christos wrk->worker_exit = true;
94 1.2 christos cv_broadcast(&wrk->worker_cv);
95 1.2 christos mutex_exit(&wrk->worker_lock);
96 1.2 christos
97 1.2 christos if (wrk->worker_lwp) {
98 1.2 christos kthread_join(wrk->worker_lwp);
99 1.2 christos }
100 1.1 rmind
101 1.2 christos /* LWP has exited, destroy the structures. */
102 1.2 christos cv_destroy(&wrk->worker_cv);
103 1.2 christos mutex_destroy(&wrk->worker_lock);
104 1.2 christos }
105 1.2 christos kmem_free(npf_workers, sizeof(npf_worker_t) * npf_worker_count);
106 1.1 rmind }
107 1.1 rmind
108 1.1 rmind void
109 1.2 christos npf_worker_signal(npf_t *npf)
110 1.1 rmind {
111 1.2 christos const unsigned idx = npf->worker_id;
112 1.2 christos npf_worker_t *wrk = &npf_workers[idx];
113 1.2 christos
114 1.2 christos mutex_enter(&wrk->worker_lock);
115 1.2 christos cv_signal(&wrk->worker_cv);
116 1.2 christos mutex_exit(&wrk->worker_lock);
117 1.1 rmind }
118 1.1 rmind
119 1.1 rmind static bool
120 1.2 christos npf_worker_testset(npf_worker_t *wrk, npf_workfunc_t find, npf_workfunc_t set)
121 1.1 rmind {
122 1.1 rmind for (u_int i = 0; i < NPF_MAX_WORKS; i++) {
123 1.2 christos if (wrk->work_funcs[i] == find) {
124 1.2 christos wrk->work_funcs[i] = set;
125 1.1 rmind return true;
126 1.1 rmind }
127 1.1 rmind }
128 1.1 rmind return false;
129 1.1 rmind }
130 1.1 rmind
131 1.1 rmind void
132 1.2 christos npf_worker_register(npf_t *npf, npf_workfunc_t func)
133 1.1 rmind {
134 1.2 christos const unsigned idx = cprng_fast32() % npf_worker_count;
135 1.2 christos npf_worker_t *wrk = &npf_workers[idx];
136 1.2 christos
137 1.2 christos mutex_enter(&wrk->worker_lock);
138 1.2 christos
139 1.2 christos npf->worker_id = idx;
140 1.2 christos npf->worker_entry = wrk->instances;
141 1.2 christos wrk->instances = npf;
142 1.2 christos
143 1.2 christos npf_worker_testset(wrk, NULL, func);
144 1.2 christos mutex_exit(&wrk->worker_lock);
145 1.1 rmind }
146 1.1 rmind
147 1.1 rmind void
148 1.2 christos npf_worker_unregister(npf_t *npf, npf_workfunc_t func)
149 1.1 rmind {
150 1.2 christos const unsigned idx = npf->worker_id;
151 1.2 christos npf_worker_t *wrk = &npf_workers[idx];
152 1.2 christos npf_t *instance;
153 1.2 christos
154 1.2 christos mutex_enter(&wrk->worker_lock);
155 1.2 christos npf_worker_testset(wrk, func, NULL);
156 1.2 christos if ((instance = wrk->instances) == npf) {
157 1.2 christos wrk->instances = instance->worker_entry;
158 1.2 christos } else while (instance) {
159 1.2 christos if (instance->worker_entry == npf) {
160 1.2 christos instance->worker_entry = npf->worker_entry;
161 1.2 christos break;
162 1.2 christos }
163 1.2 christos instance = instance->worker_entry;
164 1.1 rmind }
165 1.2 christos mutex_exit(&wrk->worker_lock);
166 1.1 rmind }
167 1.1 rmind
168 1.1 rmind static void
169 1.1 rmind npf_worker(void *arg)
170 1.1 rmind {
171 1.2 christos npf_worker_t *wrk = arg;
172 1.2 christos
173 1.2 christos KASSERT(wrk != NULL);
174 1.2 christos KASSERT(!wrk->worker_exit);
175 1.2 christos
176 1.2 christos while (!wrk->worker_exit) {
177 1.2 christos npf_t *npf;
178 1.2 christos
179 1.2 christos npf = wrk->instances;
180 1.2 christos while (npf) {
181 1.2 christos u_int i = NPF_MAX_WORKS;
182 1.2 christos npf_workfunc_t work;
183 1.2 christos
184 1.2 christos /* Run the jobs. */
185 1.2 christos while (i--) {
186 1.2 christos if ((work = wrk->work_funcs[i]) != NULL) {
187 1.2 christos work(npf);
188 1.2 christos }
189 1.1 rmind }
190 1.2 christos /* Next .. */
191 1.2 christos npf = npf->worker_entry;
192 1.1 rmind }
193 1.2 christos if (wrk->worker_exit)
194 1.1 rmind break;
195 1.1 rmind
196 1.1 rmind /* Sleep and periodically wake up, unless we get notified. */
197 1.2 christos mutex_enter(&wrk->worker_lock);
198 1.2 christos cv_timedwait(&wrk->worker_cv, &wrk->worker_lock, W_INTERVAL);
199 1.2 christos mutex_exit(&wrk->worker_lock);
200 1.1 rmind }
201 1.1 rmind kthread_exit(0);
202 1.1 rmind }
203