Debian Codegraph @

tests/isc/netmgr_common.h

raw
1 /*
2 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3 *
4 * SPDX-License-Identifier: MPL-2.0
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public
7 * License, v. 2.0. If a copy of the MPL was not distributed with this
8 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * See the COPYRIGHT file distributed with this work for additional
11 * information regarding copyright ownership.
12 */
13
14 #include <isc/atomic.h>
15 #include <isc/netmgr.h>
16 #include <isc/refcount.h>
17 #include <isc/thread.h>
18 #include <isc/util.h>
19
20 #include "netmgr/netmgr-int.h"
21
22 /*
23 * Pick unused port outside the ephemeral port range, so we don't clash with
24 * connected sockets.
25 */
26 #define UDP_TEST_PORT 9153
27 #define TCP_TEST_PORT 9154
28 #define TLS_TEST_PORT 9155
29 #define TCPDNS_TEST_PORT 9156
30 #define TLSDNS_TEST_PORT 9157
31 #define PROXYSTREAM_TEST_PORT 9158
32 #define PROXYUDP_TEST_PORT 9159
33
34 typedef void (*stream_connect_function)(isc_nm_t *nm);
35 typedef void (*connect_func)(isc_nm_t *);
36
37 extern isc_nm_t *listen_nm;
38 extern isc_nm_t *connect_nm;
39
40 extern isc_sockaddr_t tcp_listen_addr;
41 extern isc_sockaddr_t tcp_connect_addr;
42 extern isc_tlsctx_t *tcp_listen_tlsctx;
43 extern isc_tlsctx_t *tcp_connect_tlsctx;
44 extern isc_tlsctx_client_session_cache_t *tcp_tlsctx_client_sess_cache;
45
46 extern isc_sockaddr_t udp_listen_addr;
47 extern isc_sockaddr_t udp_connect_addr;
48
49 extern uint64_t send_magic;
50 extern uint64_t stop_magic;
51
52 extern isc_region_t send_msg;
53 extern isc_region_t stop_msg;
54
55 extern atomic_bool do_send;
56
57 extern atomic_int_fast64_t nsends;
58 extern int_fast64_t esends; /* expected sends */
59
60 extern atomic_int_fast64_t ssends;
61 extern atomic_int_fast64_t sreads;
62 extern atomic_int_fast64_t saccepts;
63
64 extern atomic_int_fast64_t cconnects;
65 extern atomic_int_fast64_t csends;
66 extern atomic_int_fast64_t creads;
67 extern atomic_int_fast64_t ctimeouts;
68
69 extern int expected_ssends;
70 extern int expected_sreads;
71 extern int expected_csends;
72 extern int expected_cconnects;
73 extern int expected_creads;
74 extern int expected_ctimeouts;
75
76 extern bool ssends_shutdown;
77 extern bool sreads_shutdown;
78 extern bool csends_shutdown;
79 extern bool cconnects_shutdown;
80 extern bool creads_shutdown;
81 extern bool ctimeouts_shutdown;
82
83 #define have_expected_ssends(v) ((v) >= expected_ssends && expected_ssends >= 0)
84 #define have_expected_sreads(v) ((v) >= expected_sreads && expected_sreads >= 0)
85 #define have_expected_saccepts(v) \
86 ((v) >= expected_saccepts && expected_saccepts >= 0)
87 #define have_expected_csends(v) ((v) >= expected_csends && expected_csends >= 0)
88 #define have_expected_cconnects(v) \
89 ((v) >= expected_cconnects && expected_cconnects >= 0)
90 #define have_expected_creads(v) ((v) >= expected_creads && expected_creads >= 0)
91 #define have_expected_ctimeouts(v) \
92 ((v) >= expected_ctimeouts && expected_ctimeouts >= 0)
93
94 #define do_ssends_shutdown(lm) \
95 if (ssends_shutdown) { \
96 isc_loopmgr_shutdown(lm); \
97 }
98 #define do_sreads_shutdown(lm) \
99 if (sreads_shutdown) { \
100 isc_loopmgr_shutdown(lm); \
101 }
102 #define do_saccepts_shutdown(lm) \
103 if (saccepts_shutdown) { \
104 isc_loopmgr_shutdown(lm); \
105 }
106 #define do_csends_shutdown(lm) \
107 if (csends_shutdown) { \
108 isc_loopmgr_shutdown(lm); \
109 }
110 #define do_cconnects_shutdown(lm) \
111 if (cconnects_shutdown) { \
112 isc_loopmgr_shutdown(lm); \
113 }
114 #define do_creads_shutdown(lm) \
115 if (creads_shutdown) { \
116 isc_loopmgr_shutdown(lm); \
117 }
118 #define do_ctimeouts_shutdown(lm) \
119 if (ctimeouts_shutdown) { \
120 isc_loopmgr_shutdown(lm); \
121 }
122
123 extern isc_refcount_t active_cconnects;
124 extern isc_refcount_t active_csends;
125 extern isc_refcount_t active_creads;
126 extern isc_refcount_t active_ssends;
127 extern isc_refcount_t active_sreads;
128
129 extern isc_nmsocket_t *listen_sock;
130
131 extern isc_quota_t listener_quota;
132 extern atomic_bool check_listener_quota;
133
134 extern bool allow_send_back;
135 extern bool noanswer;
136 extern bool stream_use_TLS;
137 extern bool stream_use_PROXY;
138 extern bool stream_PROXY_over_TLS;
139 extern bool stream;
140 extern in_port_t stream_port;
141
142 extern bool udp_use_PROXY;
143
144 extern isc_nm_recv_cb_t connect_readcb;
145
146 #define NSENDS 100
147
148 /* Timeout for soft-timeout tests (0.05 seconds) */
149 #define T_SOFT 50
150
151 /* Timeouts in miliseconds */
152 #define T_INIT 120 * 1000
153 #define T_IDLE 120 * 1000
154 #define T_KEEPALIVE 120 * 1000
155 #define T_ADVERTISED 120 * 1000
156 #define T_CONNECT 30 * 1000
157
158 /* Wait for 1 second (1000 milliseconds) */
159 #define WAIT_REPEATS 1000
160 #define T_WAIT 1 /* 1 millisecond */
161
162 #define WAIT_FOR(v, op, val) \
163 { \
164 X(v); \
165 int_fast64_t __r = WAIT_REPEATS; \
166 int_fast64_t __o = 0; \
167 do { \
168 int_fast64_t __l = atomic_load(&v); \
169 if (__l op val) { \
170 break; \
171 }; \
172 if (__o == __l) { \
173 __r--; \
174 } else { \
175 __r = WAIT_REPEATS; \
176 } \
177 __o = __l; \
178 uv_sleep(T_WAIT); \
179 } while (__r > 0); \
180 X(v); \
181 P(__r); \
182 assert_true(atomic_load(&v) op val); \
183 }
184
185 #define WAIT_FOR_EQ(v, val) WAIT_FOR(v, ==, val)
186 #define WAIT_FOR_NE(v, val) WAIT_FOR(v, !=, val)
187 #define WAIT_FOR_LE(v, val) WAIT_FOR(v, <=, val)
188 #define WAIT_FOR_LT(v, val) WAIT_FOR(v, <, val)
189 #define WAIT_FOR_GE(v, val) WAIT_FOR(v, >=, val)
190 #define WAIT_FOR_GT(v, val) WAIT_FOR(v, >, val)
191
192 #define DONE() atomic_store(&do_send, false);
193
194 #define CHECK_RANGE_FULL(v) \
195 { \
196 int __v = atomic_load(&v); \
197 assert_true(__v > 1); \
198 }
199
200 #define CHECK_RANGE_HALF(v) \
201 { \
202 int __v = atomic_load(&v); \
203 assert_true(__v > 1); \
204 }
205
206 /* Enable this to print values while running tests */
207 #undef PRINT_DEBUG
208 #ifdef PRINT_DEBUG
209 #define X(v) \
210 fprintf(stderr, "%s:%s:%d:%s = %" PRId64 "\n", __func__, __FILE__, \
211 __LINE__, #v, atomic_load(&v))
212 #define P(v) fprintf(stderr, #v " = %" PRId64 "\n", v)
213 #define F() \
214 fprintf(stderr, "%u:%s(%p, %s, %p)\n", isc_tid(), __func__, handle, \
215 isc_result_totext(eresult), cbarg)
216
217 #define isc_loopmgr_shutdown(loopmgr) \
218 { \
219 fprintf(stderr, "%u:%s:%s:%d:isc_loopmgr_shutdown(%p)\n", \
220 isc_tid(), __func__, __FILE__, __LINE__, loopmgr); \
221 isc_loopmgr_shutdown(loopmgr); \
222 }
223 #else
224 #define X(v)
225 #define P(v)
226 #define F()
227 #endif
228
229 #define atomic_assert_int_eq(val, exp) assert_int_equal(atomic_load(&val), exp)
230 #define atomic_assert_int_ne(val, exp) \
231 assert_int_not_equal(atomic_load(&val), exp)
232 #define atomic_assert_int_le(val, exp) assert_true(atomic_load(&val) <= exp)
233 #define atomic_assert_int_lt(val, exp) assert_true(atomic_load(&val) > exp)
234 #define atomic_assert_int_ge(val, exp) assert_true(atomic_load(&val) >= exp)
235 #define atomic_assert_int_gt(val, exp) assert_true(atomic_load(&val) > exp)
236
237 int
238 setup_netmgr_test(void **state);
239 int
240 teardown_netmgr_test(void **state ISC_ATTR_UNUSED);
241
242 void
243 noop_recv_cb(isc_nmhandle_t *handle, isc_result_t eresult, isc_region_t *region,
244 void *cbarg);
245
246 isc_result_t
247 noop_accept_cb(isc_nmhandle_t *handle ISC_ATTR_UNUSED, isc_result_t result,
248 void *cbarg ISC_ATTR_UNUSED);
249
250 void
251 connect_send_cb(isc_nmhandle_t *handle, isc_result_t eresult, void *cbarg);
252
253 void
254 connect_send(isc_nmhandle_t *handle);
255
256 void
257 connect_read_cb(isc_nmhandle_t *handle, isc_result_t eresult,
258 isc_region_t *region, void *cbarg);
259
260 void
261 connect_connect_cb(isc_nmhandle_t *handle, isc_result_t eresult, void *cbarg);
262 void
263 connect_success_cb(isc_nmhandle_t *handle, isc_result_t eresult, void *cbarg);
264
265 void
266 listen_send_cb(isc_nmhandle_t *handle, isc_result_t eresult, void *cbarg);
267
268 void
269 listen_read_cb(isc_nmhandle_t *handle, isc_result_t eresult,
270 isc_region_t *region, void *cbarg);
271
272 isc_result_t
273 listen_accept_cb(isc_nmhandle_t *handle, isc_result_t eresult, void *cbarg);
274
275 isc_result_t
276 stream_accept_cb(isc_nmhandle_t *handle, isc_result_t eresult, void *cbarg);
277
278 void
279 timeout_retry_cb(isc_nmhandle_t *handle, isc_result_t eresult,
280 isc_region_t *region, void *cbarg);
281
282 isc_quota_t *
283 tcp_listener_init_quota(size_t nthreads);
284
285 stream_connect_function
286 get_stream_connect_function(void);
287
288 isc_result_t
289 stream_listen(isc_nm_accept_cb_t accept_cb, void *accept_cbarg, int backlog,
290 isc_quota_t *quota, isc_nmsocket_t **sockp);
291
292 void
293 stream_connect(isc_nm_cb_t cb, void *cbarg, unsigned int timeout);
294
295 void
296 set_proxyheader_info(isc_nm_proxyheader_info_t *pi);
297
298 isc_nm_proxyheader_info_t *
299 get_proxyheader_info(void);
300
301 isc_nm_proxy_type_t
302 get_proxy_type(void);
303
304 void
305 proxy_verify_endpoints(isc_nmhandle_t *handle);
306
307 int
308 stream_noop_setup(void **state ISC_ATTR_UNUSED);
309 void
310 stream_noop(void **state ISC_ATTR_UNUSED);
311 int
312 stream_noop_teardown(void **state ISC_ATTR_UNUSED);
313
314 int
315 proxystream_noop_setup(void **state);
316 int
317 proxystream_noop_teardown(void **state);
318
319 int
320 proxystreamtls_noop_setup(void **state);
321 int
322 proxystreamtls_noop_teardown(void **state);
323
324 int
325 stream_noresponse_setup(void **state ISC_ATTR_UNUSED);
326 void
327 stream_noresponse(void **state ISC_ATTR_UNUSED);
328 int
329 stream_noresponse_teardown(void **state ISC_ATTR_UNUSED);
330
331 int
332 proxystream_noresponse_setup(void **state);
333 int
334 proxystream_noresponse_teardown(void **state);
335
336 int
337 proxystreamtls_noresponse_setup(void **state);
338 int
339 proxystreamtls_noresponse_teardown(void **state);
340
341 int
342 stream_timeout_recovery_setup(void **state ISC_ATTR_UNUSED);
343 void
344 stream_timeout_recovery(void **state ISC_ATTR_UNUSED);
345 int
346 stream_timeout_recovery_teardown(void **state ISC_ATTR_UNUSED);
347
348 int
349 proxystream_timeout_recovery_setup(void **state);
350 int
351 proxystream_timeout_recovery_teardown(void **state);
352
353 int
354 proxystreamtls_timeout_recovery_setup(void **state);
355 int
356 proxystreamtls_timeout_recovery_teardown(void **state);
357
358 int
359 stream_recv_one_setup(void **state ISC_ATTR_UNUSED);
360 void
361 stream_recv_one(void **state ISC_ATTR_UNUSED);
362 int
363 stream_recv_one_teardown(void **state ISC_ATTR_UNUSED);
364
365 int
366 proxystream_recv_one_setup(void **state);
367 int
368 proxystream_recv_one_teardown(void **state);
369
370 int
371 proxystreamtls_recv_one_setup(void **state);
372 int
373 proxystreamtls_recv_one_teardown(void **state);
374
375 int
376 stream_recv_two_setup(void **state ISC_ATTR_UNUSED);
377 void
378 stream_recv_two(void **state ISC_ATTR_UNUSED);
379 int
380 stream_recv_two_teardown(void **state ISC_ATTR_UNUSED);
381
382 int
383 proxystream_recv_two_setup(void **state);
384 int
385 proxystream_recv_two_teardown(void **state);
386
387 int
388 proxystreamtls_recv_two_setup(void **state);
389 int
390 proxystreamtls_recv_two_teardown(void **state);
391
392 int
393 stream_recv_send_setup(void **state ISC_ATTR_UNUSED);
394 void
395 stream_recv_send(void **state ISC_ATTR_UNUSED);
396 int
397 stream_recv_send_teardown(void **state ISC_ATTR_UNUSED);
398 void
399 stream_recv_send_connect(void *arg);
400
401 int
402 proxystream_recv_send_setup(void **state);
403 int
404 proxystream_recv_send_teardown(void **state);
405
406 int
407 proxystreamtls_recv_send_setup(void **state);
408 int
409 proxystreamtls_recv_send_teardown(void **state);
410
411 int
412 stream_shutdownconnect_setup(void **state ISC_ATTR_UNUSED);
413 void
414 stream_shutdownconnect(void **state ISC_ATTR_UNUSED);
415 int
416 stream_shutdownconnect_teardown(void **state ISC_ATTR_UNUSED);
417
418 int
419 proxystream_shutdownconnect_setup(void **state);
420 int
421 proxystream_shutdownconnect_teardown(void **state);
422
423 int
424 proxystreamtls_shutdownconnect_setup(void **state);
425 int
426 proxystreamtls_shutdownconnect_teardown(void **state);
427
428 int
429 stream_shutdownread_setup(void **state ISC_ATTR_UNUSED);
430 void
431 stream_shutdownread(void **state ISC_ATTR_UNUSED);
432 int
433 stream_shutdownread_teardown(void **state ISC_ATTR_UNUSED);
434
435 int
436 proxystream_shutdownread_setup(void **state);
437 int
438 proxystream_shutdownread_teardown(void **state);
439
440 int
441 proxystreamtls_shutdownread_setup(void **state);
442 int
443 proxystreamtls_shutdownread_teardown(void **state);
444
445 void
446 stop_listening(void *arg ISC_ATTR_UNUSED);
447
448 /* UDP */
449
450 /* Timeout for soft-timeout tests (0.05 seconds) */
451 #define UDP_T_SOFT 50
452
453 /* Timeouts in miliseconds */
454 #define UDP_T_INIT 120 * 1000
455 #define UDP_T_IDLE 120 * 1000
456 #define UDP_T_KEEPALIVE 120 * 1000
457 #define UDP_T_ADVERTISED 120 * 1000
458 #define UDP_T_CONNECT 30 * 1000
459
460 int
461 setup_udp_test(void **state);
462
463 int
464 teardown_udp_test(void **state);
465
466 int
467 udp_noop_setup(void **state);
468
469 int
470 udp_noop_teardown(void **state);
471
472 void
473 udp_noop(void **arg ISC_ATTR_UNUSED);
474
475 int
476 proxyudp_noop_setup(void **state);
477
478 int
479 proxyudp_noop_teardown(void **state);
480
481 int
482 udp_noresponse_setup(void **state);
483
484 int
485 udp_noresponse_teardown(void **state);
486
487 void
488 udp_noresponse(void **arg ISC_ATTR_UNUSED);
489
490 int
491 proxyudp_noresponse_setup(void **state);
492
493 int
494 proxyudp_noresponse_teardown(void **state);
495
496 int
497 udp_timeout_recovery_setup(void **state);
498
499 int
500 udp_timeout_recovery_teardown(void **state);
501
502 void
503 udp_timeout_recovery(void **arg ISC_ATTR_UNUSED);
504
505 int
506 proxyudp_timeout_recovery_setup(void **state);
507
508 int
509 proxyudp_timeout_recovery_teardown(void **state);
510
511 int
512 udp_shutdown_connect_setup(void **state);
513
514 int
515 udp_shutdown_connect_teardown(void **state);
516
517 void
518 udp_shutdown_connect(void **arg ISC_ATTR_UNUSED);
519
520 int
521 proxyudp_shutdown_connect_setup(void **state);
522
523 int
524 proxyudp_shutdown_connect_teardown(void **state);
525
526 int
527 udp_shutdown_read_setup(void **state);
528
529 int
530 udp_shutdown_read_teardown(void **state);
531
532 void
533 udp_shutdown_read(void **arg ISC_ATTR_UNUSED);
534
535 int
536 proxyudp_shutdown_read_setup(void **state);
537
538 int
539 proxyudp_shutdown_read_teardown(void **state);
540
541 int
542 udp_cancel_read_setup(void **state);
543
544 int
545 udp_cancel_read_teardown(void **state);
546
547 void
548 udp_cancel_read(void **arg ISC_ATTR_UNUSED);
549
550 int
551 proxyudp_cancel_read_setup(void **state);
552
553 int
554 proxyudp_cancel_read_teardown(void **state);
555
556 int
557 udp_recv_one_setup(void **state);
558
559 int
560 udp_recv_one_teardown(void **state);
561
562 void
563 udp_recv_one(void **arg ISC_ATTR_UNUSED);
564
565 int
566 proxyudp_recv_one_setup(void **state);
567
568 int
569 proxyudp_recv_one_teardown(void **state);
570
571 int
572 udp_recv_two_setup(void **state);
573
574 int
575 udp_recv_two_teardown(void **state);
576
577 void
578 udp_recv_two(void **arg ISC_ATTR_UNUSED);
579
580 int
581 proxyudp_recv_two_setup(void **state);
582
583 int
584 proxyudp_recv_two_teardown(void **state);
585
586 int
587 udp_recv_send_setup(void **state);
588
589 int
590 udp_recv_send_teardown(void **state);
591
592 void
593 udp_recv_send(void **arg ISC_ATTR_UNUSED);
594
595 int
596 proxyudp_recv_send_setup(void **state);
597
598 int
599 proxyudp_recv_send_teardown(void **state);
600
601 int
602 udp_double_read_setup(void **state);
603
604 int
605 udp_double_read_teardown(void **state);
606
607 void
608 udp_double_read(void **arg ISC_ATTR_UNUSED);
609
610 int
611 proxyudp_double_read_setup(void **state);
612
613 int
614 proxyudp_double_read_teardown(void **state);