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