lsquic_engine.c revision 96f77e20
1/* Copyright (c) 2017 - 2018 LiteSpeed Technologies Inc. See LICENSE. */ 2/* 3 * lsquic_engine.c - QUIC engine 4 */ 5 6#include <assert.h> 7#include <errno.h> 8#include <inttypes.h> 9#include <stdint.h> 10#include <stdio.h> 11#include <stdlib.h> 12#include <string.h> 13#include <sys/queue.h> 14#include <time.h> 15#ifndef WIN32 16#include <sys/time.h> 17#include <netinet/in.h> 18#include <sys/types.h> 19#include <sys/stat.h> 20#include <fcntl.h> 21#include <unistd.h> 22#include <netdb.h> 23#endif 24 25 26 27#include "lsquic.h" 28#include "lsquic_types.h" 29#include "lsquic_alarmset.h" 30#include "lsquic_parse.h" 31#include "lsquic_packet_in.h" 32#include "lsquic_packet_out.h" 33#include "lsquic_senhist.h" 34#include "lsquic_rtt.h" 35#include "lsquic_cubic.h" 36#include "lsquic_pacer.h" 37#include "lsquic_send_ctl.h" 38#include "lsquic_set.h" 39#include "lsquic_conn_flow.h" 40#include "lsquic_sfcw.h" 41#include "lsquic_stream.h" 42#include "lsquic_conn.h" 43#include "lsquic_full_conn.h" 44#include "lsquic_util.h" 45#include "lsquic_qtags.h" 46#include "lsquic_str.h" 47#include "lsquic_handshake.h" 48#include "lsquic_mm.h" 49#include "lsquic_conn_hash.h" 50#include "lsquic_engine_public.h" 51#include "lsquic_eng_hist.h" 52#include "lsquic_ev_log.h" 53#include "lsquic_version.h" 54#include "lsquic_hash.h" 55#include "lsquic_attq.h" 56#include "lsquic_min_heap.h" 57 58#define LSQUIC_LOGGER_MODULE LSQLM_ENGINE 59#include "lsquic_logger.h" 60 61 62/* The batch of outgoing packets grows and shrinks dynamically */ 63#define MAX_OUT_BATCH_SIZE 1024 64#define MIN_OUT_BATCH_SIZE 256 65#define INITIAL_OUT_BATCH_SIZE 512 66 67struct out_batch 68{ 69 lsquic_conn_t *conns [MAX_OUT_BATCH_SIZE]; 70 lsquic_packet_out_t *packets[MAX_OUT_BATCH_SIZE]; 71 struct lsquic_out_spec outs [MAX_OUT_BATCH_SIZE]; 72}; 73 74typedef struct lsquic_conn * (*conn_iter_f)(struct lsquic_engine *); 75 76static void 77process_connections (struct lsquic_engine *engine, conn_iter_f iter, 78 lsquic_time_t now); 79 80static void 81engine_incref_conn (lsquic_conn_t *conn, enum lsquic_conn_flags flag); 82 83static lsquic_conn_t * 84engine_decref_conn (lsquic_engine_t *engine, lsquic_conn_t *conn, 85 enum lsquic_conn_flags flag); 86 87static void 88force_close_conn (lsquic_engine_t *engine, lsquic_conn_t *conn); 89 90/* Nested calls to LSQUIC are not supported */ 91#define ENGINE_IN(e) do { \ 92 assert(!((e)->pub.enp_flags & ENPUB_PROC)); \ 93 (e)->pub.enp_flags |= ENPUB_PROC; \ 94} while (0) 95 96#define ENGINE_OUT(e) do { \ 97 assert((e)->pub.enp_flags & ENPUB_PROC); \ 98 (e)->pub.enp_flags &= ~ENPUB_PROC; \ 99} while (0) 100 101/* A connection can be referenced from one of six places: 102 * 103 * 1. Connection hash: a connection starts its life in one of those. 104 * 105 * 2. Outgoing queue. 106 * 107 * 3. Tickable queue 108 * 109 * 4. Advisory Tick Time queue. 110 * 111 * 5. Closing connections queue. This is a transient queue -- it only 112 * exists for the duration of process_connections() function call. 113 * 114 * 6. Ticked connections queue. Another transient queue, similar to (5). 115 * 116 * The idea is to destroy the connection when it is no longer referenced. 117 * For example, a connection tick may return TICK_SEND|TICK_CLOSE. In 118 * that case, the connection is referenced from two places: (2) and (5). 119 * After its packets are sent, it is only referenced in (5), and at the 120 * end of the function call, when it is removed from (5), reference count 121 * goes to zero and the connection is destroyed. If not all packets can 122 * be sent, at the end of the function call, the connection is referenced 123 * by (2) and will only be removed once all outgoing packets have been 124 * sent. 125 */ 126#define CONN_REF_FLAGS (LSCONN_HASHED \ 127 |LSCONN_HAS_OUTGOING \ 128 |LSCONN_TICKABLE \ 129 |LSCONN_TICKED \ 130 |LSCONN_CLOSING \ 131 |LSCONN_ATTQ) 132 133 134 135 136struct lsquic_engine 137{ 138 struct lsquic_engine_public pub; 139 enum { 140 ENG_SERVER = LSENG_SERVER, 141 ENG_HTTP = LSENG_HTTP, 142 ENG_COOLDOWN = (1 << 7), /* Cooldown: no new connections */ 143 ENG_PAST_DEADLINE 144 = (1 << 8), /* Previous call to a processing 145 * function went past time threshold. 146 */ 147#ifndef NDEBUG 148 ENG_DTOR = (1 << 26), /* Engine destructor */ 149#endif 150 } flags; 151 const struct lsquic_stream_if *stream_if; 152 void *stream_if_ctx; 153 lsquic_packets_out_f packets_out; 154 void *packets_out_ctx; 155 void *bad_handshake_ctx; 156 struct conn_hash conns_hash; 157 struct min_heap conns_tickable; 158 struct min_heap conns_out; 159 struct eng_hist history; 160 unsigned batch_size; 161 struct attq *attq; 162 /* Track time last time a packet was sent to give new connections 163 * priority lower than that of existing connections. 164 */ 165 lsquic_time_t last_sent; 166 unsigned n_conns; 167 lsquic_time_t deadline; 168 struct out_batch out_batch; 169}; 170 171 172void 173lsquic_engine_init_settings (struct lsquic_engine_settings *settings, 174 unsigned flags) 175{ 176 memset(settings, 0, sizeof(*settings)); 177 settings->es_versions = LSQUIC_DF_VERSIONS; 178 if (flags & ENG_SERVER) 179 { 180 settings->es_cfcw = LSQUIC_DF_CFCW_SERVER; 181 settings->es_sfcw = LSQUIC_DF_SFCW_SERVER; 182 settings->es_support_srej= LSQUIC_DF_SUPPORT_SREJ_SERVER; 183 } 184 else 185 { 186 settings->es_cfcw = LSQUIC_DF_CFCW_CLIENT; 187 settings->es_sfcw = LSQUIC_DF_SFCW_CLIENT; 188 settings->es_support_srej= LSQUIC_DF_SUPPORT_SREJ_CLIENT; 189 } 190 settings->es_max_streams_in = LSQUIC_DF_MAX_STREAMS_IN; 191 settings->es_idle_conn_to = LSQUIC_DF_IDLE_CONN_TO; 192 settings->es_handshake_to = LSQUIC_DF_HANDSHAKE_TO; 193 settings->es_silent_close = LSQUIC_DF_SILENT_CLOSE; 194 settings->es_max_header_list_size 195 = LSQUIC_DF_MAX_HEADER_LIST_SIZE; 196 settings->es_ua = LSQUIC_DF_UA; 197 198 settings->es_pdmd = QTAG_X509; 199 settings->es_aead = QTAG_AESG; 200 settings->es_kexs = QTAG_C255; 201 settings->es_support_push = LSQUIC_DF_SUPPORT_PUSH; 202 settings->es_support_tcid0 = LSQUIC_DF_SUPPORT_TCID0; 203 settings->es_support_nstp = LSQUIC_DF_SUPPORT_NSTP; 204 settings->es_honor_prst = LSQUIC_DF_HONOR_PRST; 205 settings->es_progress_check = LSQUIC_DF_PROGRESS_CHECK; 206 settings->es_rw_once = LSQUIC_DF_RW_ONCE; 207 settings->es_proc_time_thresh= LSQUIC_DF_PROC_TIME_THRESH; 208 settings->es_pace_packets = LSQUIC_DF_PACE_PACKETS; 209} 210 211 212/* Note: if returning an error, err_buf must be valid if non-NULL */ 213int 214lsquic_engine_check_settings (const struct lsquic_engine_settings *settings, 215 unsigned flags, 216 char *err_buf, size_t err_buf_sz) 217{ 218 if (settings->es_cfcw < LSQUIC_MIN_FCW || 219 settings->es_sfcw < LSQUIC_MIN_FCW) 220 { 221 if (err_buf) 222 snprintf(err_buf, err_buf_sz, "%s", 223 "flow control window set too low"); 224 return -1; 225 } 226 if (0 == (settings->es_versions & LSQUIC_SUPPORTED_VERSIONS)) 227 { 228 if (err_buf) 229 snprintf(err_buf, err_buf_sz, "%s", 230 "No supported QUIC versions specified"); 231 return -1; 232 } 233 if (settings->es_versions & ~LSQUIC_SUPPORTED_VERSIONS) 234 { 235 if (err_buf) 236 snprintf(err_buf, err_buf_sz, "%s", 237 "one or more unsupported QUIC version is specified"); 238 return -1; 239 } 240 return 0; 241} 242 243 244static void 245free_packet (void *ctx, unsigned char *packet_data) 246{ 247 free(packet_data); 248} 249 250 251static void * 252malloc_buf (void *ctx, size_t size) 253{ 254 return malloc(size); 255} 256 257 258static const struct lsquic_packout_mem_if stock_pmi = 259{ 260 malloc_buf, (void(*)(void *, void *)) free_packet, 261}; 262 263 264lsquic_engine_t * 265lsquic_engine_new (unsigned flags, 266 const struct lsquic_engine_api *api) 267{ 268 lsquic_engine_t *engine; 269 int tag_buf_len; 270 char err_buf[100]; 271 272 if (!api->ea_packets_out) 273 { 274 LSQ_ERROR("packets_out callback is not specified"); 275 return NULL; 276 } 277 278 if (api->ea_settings && 279 0 != lsquic_engine_check_settings(api->ea_settings, flags, 280 err_buf, sizeof(err_buf))) 281 { 282 LSQ_ERROR("cannot create engine: %s", err_buf); 283 return NULL; 284 } 285 286 engine = calloc(1, sizeof(*engine)); 287 if (!engine) 288 return NULL; 289 if (0 != lsquic_mm_init(&engine->pub.enp_mm)) 290 { 291 free(engine); 292 return NULL; 293 } 294 if (api->ea_settings) 295 engine->pub.enp_settings = *api->ea_settings; 296 else 297 lsquic_engine_init_settings(&engine->pub.enp_settings, flags); 298 tag_buf_len = gen_ver_tags(engine->pub.enp_ver_tags_buf, 299 sizeof(engine->pub.enp_ver_tags_buf), 300 engine->pub.enp_settings.es_versions); 301 if (tag_buf_len <= 0) 302 { 303 LSQ_ERROR("cannot generate version tags buffer"); 304 free(engine); 305 return NULL; 306 } 307 engine->pub.enp_ver_tags_len = tag_buf_len; 308 engine->pub.enp_flags = ENPUB_CAN_SEND; 309 310 engine->flags = flags; 311 engine->stream_if = api->ea_stream_if; 312 engine->stream_if_ctx = api->ea_stream_if_ctx; 313 engine->packets_out = api->ea_packets_out; 314 engine->packets_out_ctx = api->ea_packets_out_ctx; 315 if (api->ea_pmi) 316 { 317 engine->pub.enp_pmi = api->ea_pmi; 318 engine->pub.enp_pmi_ctx = api->ea_pmi_ctx; 319 } 320 else 321 { 322 engine->pub.enp_pmi = &stock_pmi; 323 engine->pub.enp_pmi_ctx = NULL; 324 } 325 engine->pub.enp_engine = engine; 326 conn_hash_init(&engine->conns_hash, flags & ENG_SERVER); 327 engine->attq = attq_create(); 328 eng_hist_init(&engine->history); 329 engine->batch_size = INITIAL_OUT_BATCH_SIZE; 330 331 332 LSQ_INFO("instantiated engine"); 333 return engine; 334} 335 336 337static void 338grow_batch_size (struct lsquic_engine *engine) 339{ 340 engine->batch_size <<= engine->batch_size < MAX_OUT_BATCH_SIZE; 341} 342 343 344static void 345shrink_batch_size (struct lsquic_engine *engine) 346{ 347 engine->batch_size >>= engine->batch_size > MIN_OUT_BATCH_SIZE; 348} 349 350 351/* Wrapper to make sure important things occur before the connection is 352 * really destroyed. 353 */ 354static void 355destroy_conn (struct lsquic_engine *engine, lsquic_conn_t *conn) 356{ 357 --engine->n_conns; 358 conn->cn_flags |= LSCONN_NEVER_TICKABLE; 359 conn->cn_if->ci_destroy(conn); 360} 361 362 363static int 364maybe_grow_conn_heaps (struct lsquic_engine *engine) 365{ 366 struct min_heap_elem *els; 367 unsigned count; 368 369 if (engine->n_conns < lsquic_mh_nalloc(&engine->conns_tickable)) 370 return 0; /* Nothing to do */ 371 372 if (lsquic_mh_nalloc(&engine->conns_tickable)) 373 count = lsquic_mh_nalloc(&engine->conns_tickable) * 2 * 2; 374 else 375 count = 8; 376 377 els = malloc(sizeof(els[0]) * count); 378 if (!els) 379 { 380 LSQ_ERROR("%s: malloc failed", __func__); 381 return -1; 382 } 383 384 LSQ_DEBUG("grew heaps to %u elements", count / 2); 385 memcpy(&els[0], engine->conns_tickable.mh_elems, 386 sizeof(els[0]) * lsquic_mh_count(&engine->conns_tickable)); 387 memcpy(&els[count / 2], engine->conns_out.mh_elems, 388 sizeof(els[0]) * lsquic_mh_count(&engine->conns_out)); 389 free(engine->conns_tickable.mh_elems); 390 engine->conns_tickable.mh_elems = els; 391 engine->conns_out.mh_elems = &els[count / 2]; 392 engine->conns_tickable.mh_nalloc = count / 2; 393 engine->conns_out.mh_nalloc = count / 2; 394 return 0; 395} 396 397 398static lsquic_conn_t * 399new_full_conn_client (lsquic_engine_t *engine, const char *hostname, 400 unsigned short max_packet_size) 401{ 402 lsquic_conn_t *conn; 403 unsigned flags; 404 if (0 != maybe_grow_conn_heaps(engine)) 405 return NULL; 406 flags = engine->flags & (ENG_SERVER|ENG_HTTP); 407 conn = full_conn_client_new(&engine->pub, engine->stream_if, 408 engine->stream_if_ctx, flags, hostname, max_packet_size); 409 if (!conn) 410 return NULL; 411 ++engine->n_conns; 412 return conn; 413} 414 415 416static lsquic_conn_t * 417find_conn (lsquic_engine_t *engine, lsquic_packet_in_t *packet_in, 418 struct packin_parse_state *ppstate, const struct sockaddr *sa_local) 419{ 420 lsquic_conn_t *conn; 421 422 conn = conn_hash_find_by_addr(&engine->conns_hash, sa_local); 423 if (!conn) 424 return NULL; 425 426 conn->cn_pf->pf_parse_packet_in_finish(packet_in, ppstate); 427 if ((packet_in->pi_flags & PI_CONN_ID) 428 && conn->cn_cid != packet_in->pi_conn_id) 429 { 430 LSQ_DEBUG("connection IDs do not match"); 431 return NULL; 432 } 433 434 return conn; 435} 436 437 438#if !defined(NDEBUG) && __GNUC__ 439__attribute__((weak)) 440#endif 441void 442lsquic_engine_add_conn_to_tickable (struct lsquic_engine_public *enpub, 443 lsquic_conn_t *conn) 444{ 445 if (0 == (enpub->enp_flags & ENPUB_PROC) && 446 0 == (conn->cn_flags & (LSCONN_TICKABLE|LSCONN_NEVER_TICKABLE))) 447 { 448 lsquic_engine_t *engine = (lsquic_engine_t *) enpub; 449 lsquic_mh_insert(&engine->conns_tickable, conn, conn->cn_last_ticked); 450 engine_incref_conn(conn, LSCONN_TICKABLE); 451 } 452} 453 454 455void 456lsquic_engine_add_conn_to_attq (struct lsquic_engine_public *enpub, 457 lsquic_conn_t *conn, lsquic_time_t tick_time) 458{ 459 lsquic_engine_t *const engine = (lsquic_engine_t *) enpub; 460 if (conn->cn_flags & LSCONN_TICKABLE) 461 { 462 /* Optimization: no need to add the connection to the Advisory Tick 463 * Time Queue: it is about to be ticked, after which it its next tick 464 * time may be queried again. 465 */; 466 } 467 else if (conn->cn_flags & LSCONN_ATTQ) 468 { 469 if (lsquic_conn_adv_time(conn) != tick_time) 470 { 471 attq_remove(engine->attq, conn); 472 if (0 != attq_add(engine->attq, conn, tick_time)) 473 engine_decref_conn(engine, conn, LSCONN_ATTQ); 474 } 475 } 476 else if (0 == attq_add(engine->attq, conn, tick_time)) 477 engine_incref_conn(conn, LSCONN_ATTQ); 478} 479 480 481/* Return 0 if packet is being processed by a connections, otherwise return 1 */ 482static int 483process_packet_in (lsquic_engine_t *engine, lsquic_packet_in_t *packet_in, 484 struct packin_parse_state *ppstate, const struct sockaddr *sa_local, 485 const struct sockaddr *sa_peer, void *peer_ctx) 486{ 487 lsquic_conn_t *conn; 488 489 if (lsquic_packet_in_is_prst(packet_in) 490 && !engine->pub.enp_settings.es_honor_prst) 491 { 492 lsquic_mm_put_packet_in(&engine->pub.enp_mm, packet_in); 493 LSQ_DEBUG("public reset packet: discarding"); 494 return 1; 495 } 496 497 conn = find_conn(engine, packet_in, ppstate, sa_local); 498 499 if (!conn) 500 { 501 lsquic_mm_put_packet_in(&engine->pub.enp_mm, packet_in); 502 return 1; 503 } 504 505 if (0 == (conn->cn_flags & LSCONN_TICKABLE)) 506 { 507 lsquic_mh_insert(&engine->conns_tickable, conn, conn->cn_last_ticked); 508 engine_incref_conn(conn, LSCONN_TICKABLE); 509 } 510 lsquic_conn_record_sockaddr(conn, sa_local, sa_peer); 511 lsquic_packet_in_upref(packet_in); 512 conn->cn_peer_ctx = peer_ctx; 513 conn->cn_if->ci_packet_in(conn, packet_in); 514 lsquic_packet_in_put(&engine->pub.enp_mm, packet_in); 515 return 0; 516} 517 518 519void 520lsquic_engine_destroy (lsquic_engine_t *engine) 521{ 522 lsquic_conn_t *conn; 523 524 LSQ_DEBUG("destroying engine"); 525#ifndef NDEBUG 526 engine->flags |= ENG_DTOR; 527#endif 528 529 while ((conn = lsquic_mh_pop(&engine->conns_out))) 530 { 531 assert(conn->cn_flags & LSCONN_HAS_OUTGOING); 532 (void) engine_decref_conn(engine, conn, LSCONN_HAS_OUTGOING); 533 } 534 535 while ((conn = lsquic_mh_pop(&engine->conns_tickable))) 536 { 537 assert(conn->cn_flags & LSCONN_TICKABLE); 538 (void) engine_decref_conn(engine, conn, LSCONN_TICKABLE); 539 } 540 541 for (conn = conn_hash_first(&engine->conns_hash); conn; 542 conn = conn_hash_next(&engine->conns_hash)) 543 force_close_conn(engine, conn); 544 conn_hash_cleanup(&engine->conns_hash); 545 546 assert(0 == engine->n_conns); 547 attq_destroy(engine->attq); 548 549 assert(0 == lsquic_mh_count(&engine->conns_out)); 550 assert(0 == lsquic_mh_count(&engine->conns_tickable)); 551 free(engine->conns_tickable.mh_elems); 552 free(engine); 553} 554 555 556lsquic_conn_t * 557lsquic_engine_connect (lsquic_engine_t *engine, const struct sockaddr *local_sa, 558 const struct sockaddr *peer_sa, 559 void *peer_ctx, lsquic_conn_ctx_t *conn_ctx, 560 const char *hostname, unsigned short max_packet_size) 561{ 562 lsquic_conn_t *conn; 563 ENGINE_IN(engine); 564 565 if (engine->flags & ENG_SERVER) 566 { 567 LSQ_ERROR("`%s' must only be called in client mode", __func__); 568 goto err; 569 } 570 571 if (0 == max_packet_size) 572 { 573 switch (peer_sa->sa_family) 574 { 575 case AF_INET: 576 max_packet_size = QUIC_MAX_IPv4_PACKET_SZ; 577 break; 578 default: 579 max_packet_size = QUIC_MAX_IPv6_PACKET_SZ; 580 break; 581 } 582 } 583 584 conn = new_full_conn_client(engine, hostname, max_packet_size); 585 if (!conn) 586 goto err; 587 lsquic_conn_record_sockaddr(conn, local_sa, peer_sa); 588 if (0 != conn_hash_add(&engine->conns_hash, conn)) 589 { 590 LSQ_WARN("cannot add connection %"PRIu64" to hash - destroy", 591 conn->cn_cid); 592 destroy_conn(engine, conn); 593 goto err; 594 } 595 assert(!(conn->cn_flags & 596 (CONN_REF_FLAGS 597 & ~LSCONN_TICKABLE /* This flag may be set as effect of user 598 callbacks */ 599 ))); 600 conn->cn_flags |= LSCONN_HASHED; 601 lsquic_mh_insert(&engine->conns_tickable, conn, conn->cn_last_ticked); 602 engine_incref_conn(conn, LSCONN_TICKABLE); 603 conn->cn_peer_ctx = peer_ctx; 604 lsquic_conn_set_ctx(conn, conn_ctx); 605 full_conn_client_call_on_new(conn); 606 end: 607 ENGINE_OUT(engine); 608 return conn; 609 err: 610 conn = NULL; 611 goto end; 612} 613 614 615static void 616remove_conn_from_hash (lsquic_engine_t *engine, lsquic_conn_t *conn) 617{ 618 conn_hash_remove(&engine->conns_hash, conn); 619 (void) engine_decref_conn(engine, conn, LSCONN_HASHED); 620} 621 622 623static void 624refflags2str (enum lsquic_conn_flags flags, char s[6]) 625{ 626 *s = 'C'; s += !!(flags & LSCONN_CLOSING); 627 *s = 'H'; s += !!(flags & LSCONN_HASHED); 628 *s = 'O'; s += !!(flags & LSCONN_HAS_OUTGOING); 629 *s = 'T'; s += !!(flags & LSCONN_TICKABLE); 630 *s = 'A'; s += !!(flags & LSCONN_ATTQ); 631 *s = 'K'; s += !!(flags & LSCONN_TICKED); 632 *s = '\0'; 633} 634 635 636static void 637engine_incref_conn (lsquic_conn_t *conn, enum lsquic_conn_flags flag) 638{ 639 char str[2][7]; 640 assert(flag & CONN_REF_FLAGS); 641 assert(!(conn->cn_flags & flag)); 642 conn->cn_flags |= flag; 643 LSQ_DEBUG("incref conn %"PRIu64", '%s' -> '%s'", conn->cn_cid, 644 (refflags2str(conn->cn_flags & ~flag, str[0]), str[0]), 645 (refflags2str(conn->cn_flags, str[1]), str[1])); 646} 647 648 649static lsquic_conn_t * 650engine_decref_conn (lsquic_engine_t *engine, lsquic_conn_t *conn, 651 enum lsquic_conn_flags flags) 652{ 653 char str[2][7]; 654 assert(flags & CONN_REF_FLAGS); 655 assert(conn->cn_flags & flags); 656#ifndef NDEBUG 657 if (flags & LSCONN_CLOSING) 658 assert(0 == (conn->cn_flags & LSCONN_HASHED)); 659#endif 660 conn->cn_flags &= ~flags; 661 LSQ_DEBUG("decref conn %"PRIu64", '%s' -> '%s'", conn->cn_cid, 662 (refflags2str(conn->cn_flags | flags, str[0]), str[0]), 663 (refflags2str(conn->cn_flags, str[1]), str[1])); 664 if (0 == (conn->cn_flags & CONN_REF_FLAGS)) 665 { 666 eng_hist_inc(&engine->history, 0, sl_del_full_conns); 667 destroy_conn(engine, conn); 668 return NULL; 669 } 670 else 671 return conn; 672} 673 674 675/* This is not a general-purpose function. Only call from engine dtor. */ 676static void 677force_close_conn (lsquic_engine_t *engine, lsquic_conn_t *conn) 678{ 679 assert(engine->flags & ENG_DTOR); 680 const enum lsquic_conn_flags flags = conn->cn_flags; 681 assert(conn->cn_flags & CONN_REF_FLAGS); 682 assert(!(flags & LSCONN_HAS_OUTGOING)); /* Should be removed already */ 683 assert(!(flags & LSCONN_TICKABLE)); /* Should be removed already */ 684 assert(!(flags & LSCONN_CLOSING)); /* It is in transient queue? */ 685 if (flags & LSCONN_ATTQ) 686 { 687 attq_remove(engine->attq, conn); 688 (void) engine_decref_conn(engine, conn, LSCONN_ATTQ); 689 } 690 if (flags & LSCONN_HASHED) 691 remove_conn_from_hash(engine, conn); 692} 693 694 695/* Iterator for tickable connections (those on the Tickable Queue). Before 696 * a connection is returned, it is removed from the Advisory Tick Time queue 697 * if necessary. 698 */ 699static lsquic_conn_t * 700conn_iter_next_tickable (struct lsquic_engine *engine) 701{ 702 lsquic_conn_t *conn; 703 704 conn = lsquic_mh_pop(&engine->conns_tickable); 705 706 if (conn) 707 conn = engine_decref_conn(engine, conn, LSCONN_TICKABLE); 708 if (conn && (conn->cn_flags & LSCONN_ATTQ)) 709 { 710 attq_remove(engine->attq, conn); 711 conn = engine_decref_conn(engine, conn, LSCONN_ATTQ); 712 } 713 714 return conn; 715} 716 717 718void 719lsquic_engine_process_conns (lsquic_engine_t *engine) 720{ 721 lsquic_conn_t *conn; 722 lsquic_time_t now; 723 724 ENGINE_IN(engine); 725 726 now = lsquic_time_now(); 727 while ((conn = attq_pop(engine->attq, now))) 728 { 729 conn = engine_decref_conn(engine, conn, LSCONN_ATTQ); 730 if (conn && !(conn->cn_flags & LSCONN_TICKABLE)) 731 { 732 lsquic_mh_insert(&engine->conns_tickable, conn, conn->cn_last_ticked); 733 engine_incref_conn(conn, LSCONN_TICKABLE); 734 } 735 } 736 737 process_connections(engine, conn_iter_next_tickable, now); 738 ENGINE_OUT(engine); 739} 740 741 742static int 743generate_header (const lsquic_packet_out_t *packet_out, 744 const struct parse_funcs *pf, lsquic_cid_t cid, 745 unsigned char *buf, size_t bufsz) 746{ 747 return pf->pf_gen_reg_pkt_header(buf, bufsz, 748 packet_out->po_flags & PO_CONN_ID ? &cid : NULL, 749 packet_out->po_flags & PO_VERSION ? &packet_out->po_ver_tag : NULL, 750 packet_out->po_flags & PO_NONCE ? packet_out->po_nonce : NULL, 751 packet_out->po_packno, lsquic_packet_out_packno_bits(packet_out)); 752} 753 754 755static ssize_t 756really_encrypt_packet (const lsquic_conn_t *conn, 757 const lsquic_packet_out_t *packet_out, 758 unsigned char *buf, size_t bufsz) 759{ 760 int enc, header_sz, is_hello_packet; 761 size_t packet_sz; 762 unsigned char header_buf[QUIC_MAX_PUBHDR_SZ]; 763 764 header_sz = generate_header(packet_out, conn->cn_pf, conn->cn_cid, 765 header_buf, sizeof(header_buf)); 766 if (header_sz < 0) 767 return -1; 768 769 is_hello_packet = !!(packet_out->po_flags & PO_HELLO); 770 enc = conn->cn_esf->esf_encrypt(conn->cn_enc_session, conn->cn_version, 0, 771 packet_out->po_packno, header_buf, header_sz, 772 packet_out->po_data, packet_out->po_data_sz, 773 buf, bufsz, &packet_sz, is_hello_packet); 774 if (0 == enc) 775 { 776 LSQ_DEBUG("encrypted packet %"PRIu64"; plaintext is %u bytes, " 777 "ciphertext is %zd bytes", 778 packet_out->po_packno, 779 lsquic_po_header_length(packet_out->po_flags) + 780 packet_out->po_data_sz, 781 packet_sz); 782 return packet_sz; 783 } 784 else 785 return -1; 786} 787 788 789static enum { ENCPA_OK, ENCPA_NOMEM, ENCPA_BADCRYPT, } 790encrypt_packet (lsquic_engine_t *engine, const lsquic_conn_t *conn, 791 lsquic_packet_out_t *packet_out) 792{ 793 ssize_t enc_sz; 794 size_t bufsz; 795 unsigned sent_sz; 796 unsigned char *buf; 797 798 bufsz = lsquic_po_header_length(packet_out->po_flags) + 799 packet_out->po_data_sz + QUIC_PACKET_HASH_SZ; 800 buf = engine->pub.enp_pmi->pmi_allocate(engine->pub.enp_pmi_ctx, bufsz); 801 if (!buf) 802 { 803 LSQ_DEBUG("could not allocate memory for outgoing packet of size %zd", 804 bufsz); 805 return ENCPA_NOMEM; 806 } 807 808 { 809 enc_sz = really_encrypt_packet(conn, packet_out, buf, bufsz); 810 sent_sz = enc_sz; 811 } 812 813 if (enc_sz < 0) 814 { 815 engine->pub.enp_pmi->pmi_release(engine->pub.enp_pmi_ctx, buf); 816 return ENCPA_BADCRYPT; 817 } 818 819 packet_out->po_enc_data = buf; 820 packet_out->po_enc_data_sz = enc_sz; 821 packet_out->po_sent_sz = sent_sz; 822 packet_out->po_flags |= PO_ENCRYPTED|PO_SENT_SZ; 823 824 return ENCPA_OK; 825} 826 827 828STAILQ_HEAD(conns_stailq, lsquic_conn); 829TAILQ_HEAD(conns_tailq, lsquic_conn); 830 831 832struct conns_out_iter 833{ 834 struct min_heap *coi_heap; 835 TAILQ_HEAD(, lsquic_conn) coi_active_list, 836 coi_inactive_list; 837 lsquic_conn_t *coi_next; 838#ifndef NDEBUG 839 lsquic_time_t coi_last_sent; 840#endif 841}; 842 843 844static void 845coi_init (struct conns_out_iter *iter, struct lsquic_engine *engine) 846{ 847 iter->coi_heap = &engine->conns_out; 848 iter->coi_next = NULL; 849 TAILQ_INIT(&iter->coi_active_list); 850 TAILQ_INIT(&iter->coi_inactive_list); 851#ifndef NDEBUG 852 iter->coi_last_sent = 0; 853#endif 854} 855 856 857static lsquic_conn_t * 858coi_next (struct conns_out_iter *iter) 859{ 860 lsquic_conn_t *conn; 861 862 if (lsquic_mh_count(iter->coi_heap) > 0) 863 { 864 conn = lsquic_mh_pop(iter->coi_heap); 865 TAILQ_INSERT_TAIL(&iter->coi_active_list, conn, cn_next_out); 866 conn->cn_flags |= LSCONN_COI_ACTIVE; 867#ifndef NDEBUG 868 if (iter->coi_last_sent) 869 assert(iter->coi_last_sent <= conn->cn_last_sent); 870 iter->coi_last_sent = conn->cn_last_sent; 871#endif 872 return conn; 873 } 874 else if (!TAILQ_EMPTY(&iter->coi_active_list)) 875 { 876 conn = iter->coi_next; 877 if (!conn) 878 conn = TAILQ_FIRST(&iter->coi_active_list); 879 if (conn) 880 iter->coi_next = TAILQ_NEXT(conn, cn_next_out); 881 return conn; 882 } 883 else 884 return NULL; 885} 886 887 888static void 889coi_deactivate (struct conns_out_iter *iter, lsquic_conn_t *conn) 890{ 891 if (!(conn->cn_flags & LSCONN_EVANESCENT)) 892 { 893 assert(!TAILQ_EMPTY(&iter->coi_active_list)); 894 TAILQ_REMOVE(&iter->coi_active_list, conn, cn_next_out); 895 conn->cn_flags &= ~LSCONN_COI_ACTIVE; 896 TAILQ_INSERT_TAIL(&iter->coi_inactive_list, conn, cn_next_out); 897 conn->cn_flags |= LSCONN_COI_INACTIVE; 898 } 899} 900 901 902static void 903coi_reactivate (struct conns_out_iter *iter, lsquic_conn_t *conn) 904{ 905 assert(conn->cn_flags & LSCONN_COI_INACTIVE); 906 TAILQ_REMOVE(&iter->coi_inactive_list, conn, cn_next_out); 907 conn->cn_flags &= ~LSCONN_COI_INACTIVE; 908 TAILQ_INSERT_TAIL(&iter->coi_active_list, conn, cn_next_out); 909 conn->cn_flags |= LSCONN_COI_ACTIVE; 910} 911 912 913static void 914coi_reheap (struct conns_out_iter *iter, lsquic_engine_t *engine) 915{ 916 lsquic_conn_t *conn; 917 while ((conn = TAILQ_FIRST(&iter->coi_active_list))) 918 { 919 TAILQ_REMOVE(&iter->coi_active_list, conn, cn_next_out); 920 conn->cn_flags &= ~LSCONN_COI_ACTIVE; 921 lsquic_mh_insert(iter->coi_heap, conn, conn->cn_last_sent); 922 } 923 while ((conn = TAILQ_FIRST(&iter->coi_inactive_list))) 924 { 925 TAILQ_REMOVE(&iter->coi_inactive_list, conn, cn_next_out); 926 conn->cn_flags &= ~LSCONN_COI_INACTIVE; 927 (void) engine_decref_conn(engine, conn, LSCONN_HAS_OUTGOING); 928 } 929} 930 931 932static unsigned 933send_batch (lsquic_engine_t *engine, struct conns_out_iter *conns_iter, 934 struct out_batch *batch, unsigned n_to_send) 935{ 936 int n_sent, i; 937 lsquic_time_t now; 938 939 /* Set sent time before the write to avoid underestimating RTT */ 940 now = lsquic_time_now(); 941 for (i = 0; i < (int) n_to_send; ++i) 942 batch->packets[i]->po_sent = now; 943 n_sent = engine->packets_out(engine->packets_out_ctx, batch->outs, 944 n_to_send); 945 if (n_sent >= 0) 946 LSQ_DEBUG("packets out returned %d (out of %u)", n_sent, n_to_send); 947 else 948 { 949 engine->pub.enp_flags &= ~ENPUB_CAN_SEND; 950 LSQ_DEBUG("packets out returned an error: %s", strerror(errno)); 951 EV_LOG_GENERIC_EVENT("cannot send packets"); 952 n_sent = 0; 953 } 954 if (n_sent > 0) 955 engine->last_sent = now + n_sent; 956 for (i = 0; i < n_sent; ++i) 957 { 958 eng_hist_inc(&engine->history, now, sl_packets_out); 959 EV_LOG_PACKET_SENT(batch->conns[i]->cn_cid, batch->packets[i]); 960 batch->conns[i]->cn_if->ci_packet_sent(batch->conns[i], 961 batch->packets[i]); 962 /* `i' is added to maintain relative order */ 963 batch->conns[i]->cn_last_sent = now + i; 964 /* Release packet out buffer as soon as the packet is sent 965 * successfully. If not successfully sent, we hold on to 966 * this buffer until the packet sending is attempted again 967 * or until it times out and regenerated. 968 */ 969 if (batch->packets[i]->po_flags & PO_ENCRYPTED) 970 { 971 batch->packets[i]->po_flags &= ~PO_ENCRYPTED; 972 engine->pub.enp_pmi->pmi_release(engine->pub.enp_pmi_ctx, 973 batch->packets[i]->po_enc_data); 974 batch->packets[i]->po_enc_data = NULL; /* JIC */ 975 } 976 } 977 if (LSQ_LOG_ENABLED_EXT(LSQ_LOG_DEBUG, LSQLM_EVENT)) 978 for ( ; i < (int) n_to_send; ++i) 979 EV_LOG_PACKET_NOT_SENT(batch->conns[i]->cn_cid, batch->packets[i]); 980 /* Return packets to the connection in reverse order so that the packet 981 * ordering is maintained. 982 */ 983 for (i = (int) n_to_send - 1; i >= n_sent; --i) 984 { 985 batch->conns[i]->cn_if->ci_packet_not_sent(batch->conns[i], 986 batch->packets[i]); 987 if (!(batch->conns[i]->cn_flags & (LSCONN_COI_ACTIVE|LSCONN_EVANESCENT))) 988 coi_reactivate(conns_iter, batch->conns[i]); 989 } 990 return n_sent; 991} 992 993 994/* Return 1 if went past deadline, 0 otherwise */ 995static int 996check_deadline (lsquic_engine_t *engine) 997{ 998 if (engine->pub.enp_settings.es_proc_time_thresh && 999 lsquic_time_now() > engine->deadline) 1000 { 1001 LSQ_INFO("went past threshold of %u usec, stop sending", 1002 engine->pub.enp_settings.es_proc_time_thresh); 1003 engine->flags |= ENG_PAST_DEADLINE; 1004 return 1; 1005 } 1006 else 1007 return 0; 1008} 1009 1010 1011static void 1012send_packets_out (struct lsquic_engine *engine, 1013 struct conns_tailq *ticked_conns, 1014 struct conns_stailq *closed_conns) 1015{ 1016 unsigned n, w, n_sent, n_batches_sent; 1017 lsquic_packet_out_t *packet_out; 1018 lsquic_conn_t *conn; 1019 struct out_batch *const batch = &engine->out_batch; 1020 struct conns_out_iter conns_iter; 1021 int shrink, deadline_exceeded; 1022 1023 coi_init(&conns_iter, engine); 1024 n_batches_sent = 0; 1025 n_sent = 0, n = 0; 1026 shrink = 0; 1027 deadline_exceeded = 0; 1028 1029 while ((conn = coi_next(&conns_iter))) 1030 { 1031 packet_out = conn->cn_if->ci_next_packet_to_send(conn); 1032 if (!packet_out) { 1033 LSQ_DEBUG("batched all outgoing packets for conn %"PRIu64, 1034 conn->cn_cid); 1035 coi_deactivate(&conns_iter, conn); 1036 continue; 1037 } 1038 if (!(packet_out->po_flags & (PO_ENCRYPTED|PO_NOENCRYPT))) 1039 { 1040 switch (encrypt_packet(engine, conn, packet_out)) 1041 { 1042 case ENCPA_NOMEM: 1043 /* Send what we have and wait for a more opportune moment */ 1044 conn->cn_if->ci_packet_not_sent(conn, packet_out); 1045 goto end_for; 1046 case ENCPA_BADCRYPT: 1047 /* This is pretty bad: close connection immediately */ 1048 conn->cn_if->ci_packet_not_sent(conn, packet_out); 1049 LSQ_INFO("conn %"PRIu64" has unsendable packets", conn->cn_cid); 1050 if (!(conn->cn_flags & LSCONN_EVANESCENT)) 1051 { 1052 if (!(conn->cn_flags & LSCONN_CLOSING)) 1053 { 1054 STAILQ_INSERT_TAIL(closed_conns, conn, cn_next_closed_conn); 1055 engine_incref_conn(conn, LSCONN_CLOSING); 1056 if (conn->cn_flags & LSCONN_HASHED) 1057 remove_conn_from_hash(engine, conn); 1058 } 1059 coi_deactivate(&conns_iter, conn); 1060 if (conn->cn_flags & LSCONN_TICKED) 1061 { 1062 TAILQ_REMOVE(ticked_conns, conn, cn_next_ticked); 1063 engine_decref_conn(engine, conn, LSCONN_TICKED); 1064 } 1065 } 1066 continue; 1067 case ENCPA_OK: 1068 break; 1069 } 1070 } 1071 LSQ_DEBUG("batched packet %"PRIu64" for connection %"PRIu64, 1072 packet_out->po_packno, conn->cn_cid); 1073 assert(conn->cn_flags & LSCONN_HAS_PEER_SA); 1074 if (packet_out->po_flags & PO_ENCRYPTED) 1075 { 1076 batch->outs[n].buf = packet_out->po_enc_data; 1077 batch->outs[n].sz = packet_out->po_enc_data_sz; 1078 } 1079 else 1080 { 1081 batch->outs[n].buf = packet_out->po_data; 1082 batch->outs[n].sz = packet_out->po_data_sz; 1083 } 1084 batch->outs [n].peer_ctx = conn->cn_peer_ctx; 1085 batch->outs [n].local_sa = (struct sockaddr *) conn->cn_local_addr; 1086 batch->outs [n].dest_sa = (struct sockaddr *) conn->cn_peer_addr; 1087 batch->conns [n] = conn; 1088 batch->packets[n] = packet_out; 1089 ++n; 1090 if (n == engine->batch_size) 1091 { 1092 n = 0; 1093 w = send_batch(engine, &conns_iter, batch, engine->batch_size); 1094 ++n_batches_sent; 1095 n_sent += w; 1096 if (w < engine->batch_size) 1097 { 1098 shrink = 1; 1099 break; 1100 } 1101 deadline_exceeded = check_deadline(engine); 1102 if (deadline_exceeded) 1103 break; 1104 grow_batch_size(engine); 1105 } 1106 } 1107 end_for: 1108 1109 if (n > 0) { 1110 w = send_batch(engine, &conns_iter, batch, n); 1111 n_sent += w; 1112 shrink = w < n; 1113 ++n_batches_sent; 1114 deadline_exceeded = check_deadline(engine); 1115 } 1116 1117 if (shrink) 1118 shrink_batch_size(engine); 1119 else if (n_batches_sent > 1 && !deadline_exceeded) 1120 grow_batch_size(engine); 1121 1122 coi_reheap(&conns_iter, engine); 1123 1124 LSQ_DEBUG("%s: sent %u packet%.*s", __func__, n_sent, n_sent != 1, "s"); 1125} 1126 1127 1128int 1129lsquic_engine_has_unsent_packets (lsquic_engine_t *engine) 1130{ 1131 return lsquic_mh_count(&engine->conns_out) > 0 1132 ; 1133} 1134 1135 1136static void 1137reset_deadline (lsquic_engine_t *engine, lsquic_time_t now) 1138{ 1139 engine->deadline = now + engine->pub.enp_settings.es_proc_time_thresh; 1140 engine->flags &= ~ENG_PAST_DEADLINE; 1141} 1142 1143 1144/* TODO: this is a user-facing function, account for load */ 1145void 1146lsquic_engine_send_unsent_packets (lsquic_engine_t *engine) 1147{ 1148 lsquic_conn_t *conn; 1149 struct conns_stailq closed_conns; 1150 struct conns_tailq ticked_conns = TAILQ_HEAD_INITIALIZER(ticked_conns); 1151 1152 STAILQ_INIT(&closed_conns); 1153 reset_deadline(engine, lsquic_time_now()); 1154 if (!(engine->pub.enp_flags & ENPUB_CAN_SEND)) 1155 { 1156 LSQ_DEBUG("can send again"); 1157 EV_LOG_GENERIC_EVENT("can send again"); 1158 engine->pub.enp_flags |= ENPUB_CAN_SEND; 1159 } 1160 1161 send_packets_out(engine, &ticked_conns, &closed_conns); 1162 1163 while ((conn = STAILQ_FIRST(&closed_conns))) { 1164 STAILQ_REMOVE_HEAD(&closed_conns, cn_next_closed_conn); 1165 (void) engine_decref_conn(engine, conn, LSCONN_CLOSING); 1166 } 1167 1168} 1169 1170 1171static void 1172process_connections (lsquic_engine_t *engine, conn_iter_f next_conn, 1173 lsquic_time_t now) 1174{ 1175 lsquic_conn_t *conn; 1176 enum tick_st tick_st; 1177 unsigned i; 1178 lsquic_time_t next_tick_time; 1179 struct conns_stailq closed_conns; 1180 struct conns_tailq ticked_conns; 1181 1182 eng_hist_tick(&engine->history, now); 1183 1184 STAILQ_INIT(&closed_conns); 1185 TAILQ_INIT(&ticked_conns); 1186 reset_deadline(engine, now); 1187 1188 i = 0; 1189 while ((conn = next_conn(engine)) 1190 ) 1191 { 1192 tick_st = conn->cn_if->ci_tick(conn, now); 1193 conn->cn_last_ticked = now + i /* Maintain relative order */ ++; 1194 if (tick_st & TICK_SEND) 1195 { 1196 if (!(conn->cn_flags & LSCONN_HAS_OUTGOING)) 1197 { 1198 lsquic_mh_insert(&engine->conns_out, conn, conn->cn_last_sent); 1199 engine_incref_conn(conn, LSCONN_HAS_OUTGOING); 1200 } 1201 } 1202 if (tick_st & TICK_CLOSE) 1203 { 1204 STAILQ_INSERT_TAIL(&closed_conns, conn, cn_next_closed_conn); 1205 engine_incref_conn(conn, LSCONN_CLOSING); 1206 if (conn->cn_flags & LSCONN_HASHED) 1207 remove_conn_from_hash(engine, conn); 1208 } 1209 else 1210 { 1211 TAILQ_INSERT_TAIL(&ticked_conns, conn, cn_next_ticked); 1212 engine_incref_conn(conn, LSCONN_TICKED); 1213 } 1214 } 1215 1216 if ((engine->pub.enp_flags & ENPUB_CAN_SEND) 1217 && lsquic_engine_has_unsent_packets(engine)) 1218 send_packets_out(engine, &ticked_conns, &closed_conns); 1219 1220 while ((conn = STAILQ_FIRST(&closed_conns))) { 1221 STAILQ_REMOVE_HEAD(&closed_conns, cn_next_closed_conn); 1222 (void) engine_decref_conn(engine, conn, LSCONN_CLOSING); 1223 } 1224 1225 /* TODO Heapification can be optimized by switching to the Floyd method: 1226 * https://en.wikipedia.org/wiki/Binary_heap#Building_a_heap 1227 */ 1228 while ((conn = TAILQ_FIRST(&ticked_conns))) 1229 { 1230 TAILQ_REMOVE(&ticked_conns, conn, cn_next_ticked); 1231 engine_decref_conn(engine, conn, LSCONN_TICKED); 1232 if (!(conn->cn_flags & LSCONN_TICKABLE) 1233 && conn->cn_if->ci_is_tickable(conn)) 1234 { 1235 lsquic_mh_insert(&engine->conns_tickable, conn, conn->cn_last_ticked); 1236 engine_incref_conn(conn, LSCONN_TICKABLE); 1237 } 1238 else if (!(conn->cn_flags & LSCONN_ATTQ)) 1239 { 1240 next_tick_time = conn->cn_if->ci_next_tick_time(conn); 1241 if (next_tick_time) 1242 { 1243 if (0 == attq_add(engine->attq, conn, next_tick_time)) 1244 engine_incref_conn(conn, LSCONN_ATTQ); 1245 } 1246 else 1247 assert(0); 1248 } 1249 } 1250 1251} 1252 1253 1254/* Return 0 if packet is being processed by a real connection, 1 if the 1255 * packet was processed, but not by a connection, and -1 on error. 1256 */ 1257int 1258lsquic_engine_packet_in (lsquic_engine_t *engine, 1259 const unsigned char *packet_in_data, size_t packet_in_size, 1260 const struct sockaddr *sa_local, const struct sockaddr *sa_peer, 1261 void *peer_ctx) 1262{ 1263 struct packin_parse_state ppstate; 1264 lsquic_packet_in_t *packet_in; 1265 1266 if (packet_in_size > QUIC_MAX_PACKET_SZ) 1267 { 1268 LSQ_DEBUG("Cannot handle packet_in_size(%zd) > %d packet incoming " 1269 "packet's header", packet_in_size, QUIC_MAX_PACKET_SZ); 1270 errno = E2BIG; 1271 return -1; 1272 } 1273 1274 packet_in = lsquic_mm_get_packet_in(&engine->pub.enp_mm); 1275 if (!packet_in) 1276 return -1; 1277 1278 /* Library does not modify packet_in_data, it is not referenced after 1279 * this function returns and subsequent release of pi_data is guarded 1280 * by PI_OWN_DATA flag. 1281 */ 1282 packet_in->pi_data = (unsigned char *) packet_in_data; 1283 if (0 != parse_packet_in_begin(packet_in, packet_in_size, 1284 engine->flags & ENG_SERVER, &ppstate)) 1285 { 1286 LSQ_DEBUG("Cannot parse incoming packet's header"); 1287 lsquic_mm_put_packet_in(&engine->pub.enp_mm, packet_in); 1288 errno = EINVAL; 1289 return -1; 1290 } 1291 1292 packet_in->pi_received = lsquic_time_now(); 1293 eng_hist_inc(&engine->history, packet_in->pi_received, sl_packets_in); 1294 return process_packet_in(engine, packet_in, &ppstate, sa_local, sa_peer, 1295 peer_ctx); 1296} 1297 1298 1299#if __GNUC__ && !defined(NDEBUG) 1300__attribute__((weak)) 1301#endif 1302unsigned 1303lsquic_engine_quic_versions (const lsquic_engine_t *engine) 1304{ 1305 return engine->pub.enp_settings.es_versions; 1306} 1307 1308 1309int 1310lsquic_engine_earliest_adv_tick (lsquic_engine_t *engine, int *diff) 1311{ 1312 const lsquic_time_t *next_time; 1313 lsquic_time_t now; 1314 1315 if (((engine->flags & ENG_PAST_DEADLINE) 1316 && lsquic_mh_count(&engine->conns_out)) 1317 || lsquic_mh_count(&engine->conns_tickable)) 1318 { 1319 *diff = 0; 1320 return 1; 1321 } 1322 1323 next_time = attq_next_time(engine->attq); 1324 if (!next_time) 1325 return 0; 1326 1327 now = lsquic_time_now(); 1328 *diff = (int) ((int64_t) *next_time - (int64_t) now); 1329 return 1; 1330} 1331 1332 1333unsigned 1334lsquic_engine_count_attq (lsquic_engine_t *engine, int from_now) 1335{ 1336 lsquic_time_t now; 1337 now = lsquic_time_now(); 1338 if (from_now < 0) 1339 now -= from_now; 1340 else 1341 now += from_now; 1342 return attq_count_before(engine->attq, now); 1343} 1344