npf_gc_test.c revision 1.1.2.3 1 1.1.2.2 martin /*
2 1.1.2.2 martin * NPF connection tests.
3 1.1.2.2 martin *
4 1.1.2.2 martin * Public Domain.
5 1.1.2.2 martin */
6 1.1.2.2 martin
7 1.1.2.2 martin #ifdef _KERNEL
8 1.1.2.2 martin #include <sys/types.h>
9 1.1.2.3 martin #include <sys/kernel.h>
10 1.1.2.2 martin #include <sys/kmem.h>
11 1.1.2.2 martin #endif
12 1.1.2.2 martin
13 1.1.2.2 martin #include "npf.h"
14 1.1.2.2 martin #include "npf_impl.h"
15 1.1.2.2 martin #include "npf_conn.h"
16 1.1.2.2 martin #include "npf_test.h"
17 1.1.2.2 martin
18 1.1.2.2 martin static bool lverbose = false;
19 1.1.2.2 martin
20 1.1.2.2 martin static unsigned
21 1.1.2.2 martin count_conns(npf_conndb_t *cd)
22 1.1.2.2 martin {
23 1.1.2.2 martin npf_conn_t *head = npf_conndb_getlist(cd), *conn = head;
24 1.1.2.2 martin unsigned n = 0;
25 1.1.2.2 martin
26 1.1.2.2 martin while (conn) {
27 1.1.2.2 martin n++;
28 1.1.2.2 martin conn = npf_conndb_getnext(cd, conn);
29 1.1.2.2 martin if (conn == head) {
30 1.1.2.2 martin break;
31 1.1.2.2 martin }
32 1.1.2.2 martin }
33 1.1.2.2 martin return n;
34 1.1.2.2 martin }
35 1.1.2.2 martin
36 1.1.2.2 martin static struct mbuf *
37 1.1.2.2 martin get_packet(unsigned i)
38 1.1.2.2 martin {
39 1.1.2.2 martin struct mbuf *m;
40 1.1.2.2 martin struct ip *ip;
41 1.1.2.2 martin
42 1.1.2.2 martin m = mbuf_get_pkt(AF_INET, IPPROTO_UDP,
43 1.1.2.2 martin "10.0.0.1", "172.16.0.1", 9000, 9000);
44 1.1.2.2 martin (void)mbuf_return_hdrs(m, false, &ip);
45 1.1.2.2 martin ip->ip_src.s_addr += i;
46 1.1.2.2 martin return m;
47 1.1.2.2 martin }
48 1.1.2.2 martin
49 1.1.2.2 martin static bool
50 1.1.2.2 martin enqueue_connection(unsigned i, bool expire)
51 1.1.2.2 martin {
52 1.1.2.2 martin struct mbuf *m = get_packet(i);
53 1.1.2.2 martin npf_cache_t *npc = get_cached_pkt(m, NULL);
54 1.1.2.2 martin npf_conn_t *con;
55 1.1.2.2 martin
56 1.1.2.2 martin con = npf_conn_establish(npc, PFIL_IN, true);
57 1.1.2.2 martin CHECK_TRUE(con != NULL);
58 1.1.2.2 martin if (expire) {
59 1.1.2.2 martin npf_conn_expire(con);
60 1.1.2.2 martin }
61 1.1.2.2 martin npf_conn_release(con);
62 1.1.2.2 martin put_cached_pkt(npc);
63 1.1.2.2 martin return true;
64 1.1.2.2 martin }
65 1.1.2.2 martin
66 1.1.2.2 martin static bool
67 1.1.2.2 martin run_conn_gc(unsigned active, unsigned expired, unsigned expected)
68 1.1.2.2 martin {
69 1.1.2.2 martin npf_t *npf = npf_getkernctx();
70 1.1.2.2 martin npf_conndb_t *cd = npf_conndb_create();
71 1.1.2.2 martin unsigned total, n = 0;
72 1.1.2.2 martin
73 1.1.2.2 martin npf->conn_db = cd;
74 1.1.2.2 martin
75 1.1.2.2 martin /*
76 1.1.2.2 martin * Insert the given number of active and expired connections..
77 1.1.2.2 martin */
78 1.1.2.2 martin total = active + expired;
79 1.1.2.2 martin
80 1.1.2.2 martin while (active || expired) {
81 1.1.2.2 martin if (active) {
82 1.1.2.2 martin enqueue_connection(n++, false);
83 1.1.2.2 martin active--;
84 1.1.2.2 martin }
85 1.1.2.2 martin if (expired) {
86 1.1.2.2 martin enqueue_connection(n++, true);
87 1.1.2.2 martin expired--;
88 1.1.2.2 martin }
89 1.1.2.2 martin }
90 1.1.2.2 martin
91 1.1.2.2 martin /* Verify the count. */
92 1.1.2.2 martin n = count_conns(cd);
93 1.1.2.2 martin CHECK_TRUE(n == total);
94 1.1.2.2 martin
95 1.1.2.2 martin /*
96 1.1.2.2 martin * Run G/C. Check the remaining.
97 1.1.2.2 martin */
98 1.1.2.2 martin npf_conndb_gc(npf, cd, false, false);
99 1.1.2.2 martin n = count_conns(cd);
100 1.1.2.2 martin if (lverbose) {
101 1.1.2.2 martin printf("in conndb -- %u (expected %u)\n", n, expected);
102 1.1.2.2 martin }
103 1.1.2.2 martin CHECK_TRUE(n == expected);
104 1.1.2.2 martin
105 1.1.2.2 martin /* Flush and destroy. */
106 1.1.2.2 martin npf_conndb_gc(npf, cd, true, false);
107 1.1.2.2 martin npf_conndb_destroy(cd);
108 1.1.2.2 martin npf->conn_db = NULL;
109 1.1.2.2 martin return true;
110 1.1.2.2 martin }
111 1.1.2.2 martin
112 1.1.2.2 martin static bool
113 1.1.2.2 martin run_gc_tests(void)
114 1.1.2.2 martin {
115 1.1.2.2 martin bool ok;
116 1.1.2.2 martin int val;
117 1.1.2.2 martin
118 1.1.2.2 martin /* Check the default value. */
119 1.1.2.2 martin npfk_param_get(npf_getkernctx(), "gc.step", &val);
120 1.1.2.2 martin CHECK_TRUE(val == 256);
121 1.1.2.2 martin
122 1.1.2.2 martin /* Empty => GC => 0 in conndb. */
123 1.1.2.2 martin ok = run_conn_gc(0, 0, 0);
124 1.1.2.2 martin CHECK_TRUE(ok);
125 1.1.2.2 martin
126 1.1.2.2 martin /* 1 active => GC => 1 in conndb. */
127 1.1.2.2 martin ok = run_conn_gc(1, 0, 1);
128 1.1.2.2 martin CHECK_TRUE(ok);
129 1.1.2.2 martin
130 1.1.2.2 martin /* 1 expired => GC => 0 in conndb. */
131 1.1.2.2 martin ok = run_conn_gc(0, 1, 0);
132 1.1.2.2 martin CHECK_TRUE(ok);
133 1.1.2.2 martin
134 1.1.2.2 martin /* 1 active and 1 expired => GC => 1 in conndb. */
135 1.1.2.2 martin ok = run_conn_gc(1, 1, 1);
136 1.1.2.2 martin CHECK_TRUE(ok);
137 1.1.2.2 martin
138 1.1.2.2 martin /* 2 expired => GC => 0 in conndb. */
139 1.1.2.2 martin ok = run_conn_gc(0, 2, 0);
140 1.1.2.2 martin CHECK_TRUE(ok);
141 1.1.2.2 martin
142 1.1.2.2 martin /* 128 expired => GC => 0 in conndb. */
143 1.1.2.2 martin ok = run_conn_gc(0, 128, 0);
144 1.1.2.2 martin CHECK_TRUE(ok);
145 1.1.2.2 martin
146 1.1.2.2 martin /* 512 expired => GC => 256 in conndb. */
147 1.1.2.2 martin ok = run_conn_gc(0, 512, 256);
148 1.1.2.2 martin CHECK_TRUE(ok);
149 1.1.2.2 martin
150 1.1.2.2 martin /* 512 expired => GC => 127 in conndb. */
151 1.1.2.2 martin npfk_param_set(npf_getkernctx(), "gc.step", 128);
152 1.1.2.2 martin ok = run_conn_gc(0, 512, 384);
153 1.1.2.2 martin CHECK_TRUE(ok);
154 1.1.2.2 martin
155 1.1.2.2 martin return true;
156 1.1.2.2 martin }
157 1.1.2.2 martin
158 1.1.2.2 martin static bool
159 1.1.2.2 martin run_conndb_tests(npf_t *npf)
160 1.1.2.2 martin {
161 1.1.2.2 martin npf_conndb_t *orig_cd = npf->conn_db;
162 1.1.2.2 martin bool ok;
163 1.1.2.2 martin
164 1.1.2.2 martin npf_config_enter(npf);
165 1.1.2.2 martin npf_conn_tracking(npf, true);
166 1.1.2.2 martin npf_config_exit(npf);
167 1.1.2.2 martin
168 1.1.2.2 martin ok = run_gc_tests();
169 1.1.2.2 martin
170 1.1.2.2 martin /* We *MUST* restore the valid conndb. */
171 1.1.2.2 martin npf->conn_db = orig_cd;
172 1.1.2.2 martin return ok;
173 1.1.2.2 martin }
174 1.1.2.2 martin
175 1.1.2.2 martin
176 1.1.2.2 martin static void
177 1.1.2.2 martin worker_test_task(npf_t *npf)
178 1.1.2.2 martin {
179 1.1.2.2 martin bool *done = atomic_load_acquire(&npf->arg);
180 1.1.2.2 martin atomic_store_release(done, true);
181 1.1.2.2 martin }
182 1.1.2.2 martin
183 1.1.2.2 martin static bool
184 1.1.2.2 martin run_worker_tests(npf_t *npf)
185 1.1.2.2 martin {
186 1.1.2.2 martin unsigned n = 100;
187 1.1.2.2 martin int error;
188 1.1.2.2 martin
189 1.1.2.2 martin /* Spawn a worker thread. */
190 1.1.2.2 martin error = npf_worker_sysinit(1);
191 1.1.2.2 martin assert(error == 0);
192 1.1.2.2 martin
193 1.1.2.2 martin /*
194 1.1.2.2 martin * Enlist/discharge an instance, trying to trigger a race.
195 1.1.2.2 martin */
196 1.1.2.2 martin while (n--) {
197 1.1.2.2 martin bool task_done = false;
198 1.1.2.2 martin unsigned retry = 100;
199 1.1.2.2 martin npf_t *test_npf;
200 1.1.2.2 martin
201 1.1.2.2 martin /*
202 1.1.2.2 martin * Initialize a dummy NPF instance and add a test task.
203 1.1.2.2 martin * We will (ab)use npf_t::arg here.
204 1.1.2.2 martin *
205 1.1.2.2 martin * XXX/TODO: We should use:
206 1.1.2.2 martin *
207 1.1.2.2 martin * npfk_create(NPF_NO_GC, &npftest_mbufops,
208 1.1.2.2 martin * &npftest_ifops, &task_done);
209 1.1.2.2 martin *
210 1.1.2.2 martin * However, it resets the interface state and breaks
211 1.1.2.2 martin * other tests; to be refactor.
212 1.1.2.2 martin */
213 1.1.2.2 martin test_npf = kmem_zalloc(sizeof(npf_t), KM_SLEEP);
214 1.1.2.2 martin atomic_store_release(&test_npf->arg, &task_done);
215 1.1.2.2 martin test_npf->ebr = npf_ebr_create();
216 1.1.2.2 martin
217 1.1.2.2 martin error = npf_worker_addfunc(test_npf, worker_test_task);
218 1.1.2.2 martin assert(error == 0);
219 1.1.2.2 martin
220 1.1.2.2 martin /* Enlist the NPF instance. */
221 1.1.2.2 martin npf_worker_enlist(test_npf);
222 1.1.2.2 martin
223 1.1.2.2 martin /* Wait for the task to be done. */
224 1.1.2.2 martin while (!atomic_load_acquire(&task_done) && retry--) {
225 1.1.2.2 martin npf_worker_signal(test_npf);
226 1.1.2.2 martin kpause("gctest", false, mstohz(1), NULL);
227 1.1.2.2 martin }
228 1.1.2.2 martin
229 1.1.2.2 martin CHECK_TRUE(atomic_load_acquire(&task_done));
230 1.1.2.2 martin npf_worker_discharge(test_npf);
231 1.1.2.2 martin
232 1.1.2.2 martin /* Clear the parameter and signal again. */
233 1.1.2.2 martin atomic_store_release(&test_npf->arg, NULL);
234 1.1.2.2 martin npf_worker_signal(test_npf);
235 1.1.2.2 martin
236 1.1.2.2 martin npf_ebr_destroy(test_npf->ebr);
237 1.1.2.2 martin kmem_free(test_npf, sizeof(npf_t)); // npfk_destroy()
238 1.1.2.2 martin }
239 1.1.2.2 martin
240 1.1.2.2 martin /*
241 1.1.2.2 martin * Destroy the worker.
242 1.1.2.2 martin *
243 1.1.2.2 martin * Attempts to enlist, discharge or signal should have no effect.
244 1.1.2.2 martin */
245 1.1.2.2 martin
246 1.1.2.2 martin npf_worker_sysfini();
247 1.1.2.2 martin npf_worker_enlist(npf);
248 1.1.2.2 martin npf_worker_signal(npf);
249 1.1.2.2 martin npf_worker_discharge(npf);
250 1.1.2.2 martin return true;
251 1.1.2.2 martin }
252 1.1.2.2 martin
253 1.1.2.2 martin bool
254 1.1.2.2 martin npf_gc_test(bool verbose)
255 1.1.2.2 martin {
256 1.1.2.2 martin npf_t *npf = npf_getkernctx();
257 1.1.2.2 martin bool ok;
258 1.1.2.2 martin
259 1.1.2.2 martin lverbose = verbose;
260 1.1.2.2 martin
261 1.1.2.2 martin ok = run_conndb_tests(npf);
262 1.1.2.2 martin CHECK_TRUE(ok);
263 1.1.2.2 martin
264 1.1.2.2 martin ok = run_worker_tests(npf);
265 1.1.2.2 martin CHECK_TRUE(ok);
266 1.1.2.2 martin
267 1.1.2.2 martin return ok;
268 1.1.2.2 martin }
269