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