lsquic_conn.h revision b8fa6195
1/* Copyright (c) 2017 - 2020 LiteSpeed Technologies Inc. See LICENSE. */ 2/* 3 * lsquic_conn.h -- Connection interface 4 * 5 * There are two types of connections: full (lsquic_full_conn.h) and mini 6 * (lsquic_mini_conn.h). The function pointers and struct in this header 7 * file provide a unified interface engine.c can use to interact with 8 * either of the connection types. For this to work, struct lsquic_conn 9 * must be the first element of struct full_conn and struct mini_conn. 10 */ 11#ifndef LSQUIC_CONN_H 12#define LSQUIC_CONN_H 13 14#include <sys/queue.h> 15#ifndef WIN32 16#include <sys/socket.h> 17#include <netinet/in.h> 18#else 19#include <ws2ipdef.h> 20#endif 21 22struct lsquic_conn; 23struct lsquic_engine_public; 24struct lsquic_packet_out; 25struct lsquic_packet_in; 26struct sockaddr; 27struct parse_funcs; 28struct attq_elem; 29#if LSQUIC_CONN_STATS 30struct conn_stats; 31#endif 32 33enum lsquic_conn_flags { 34 LSCONN_TICKED = (1 << 0), 35 LSCONN_HAS_OUTGOING = (1 << 1), 36 LSCONN_HASHED = (1 << 2), 37 LSCONN_MINI = (1 << 3), /* This is a mini connection */ 38 LSCONN_IMMED_CLOSE = (1 << 4), 39 LSCONN_UNUSED_5 = (1 << 5), 40 LSCONN_HANDSHAKE_DONE = (1 << 6), 41 LSCONN_CLOSING = (1 << 7), 42 LSCONN_PEER_GOING_AWAY= (1 << 8), 43 LSCONN_TCID0 = (1 << 9), 44 LSCONN_VER_SET = (1 <<10), /* cn_version is set */ 45 LSCONN_EVANESCENT = (1 <<11), /* evanescent connection */ 46 LSCONN_TICKABLE = (1 <<12), /* Connection is in the Tickable Queue */ 47 LSCONN_COI_ACTIVE = (1 <<13), 48 LSCONN_COI_INACTIVE = (1 <<14), 49 LSCONN_SEND_BLOCKED = (1 <<15), /* Send connection blocked frame */ 50 LSCONN_PROMOTED = (1 <<16), /* Promoted. Only set if LSCONN_MINI is set */ 51 LSCONN_NEVER_TICKABLE = (1 <<17), /* Do not put onto the Tickable Queue */ 52 LSCONN_UNUSED_18 = (1 <<18), 53 LSCONN_ATTQ = (1 <<19), 54 LSCONN_SKIP_ON_PROC = (1 <<20), 55 LSCONN_UNUSED_21 = (1 <<21), 56 LSCONN_SERVER = (1 <<22), 57 LSCONN_IETF = (1 <<23), 58 LSCONN_RETRY_CONN = (1 <<24), /* This is a retry connection */ 59}; 60 61/* A connection may have things to send and be closed at the same time. 62 */ 63enum tick_st { 64 TICK_SEND = (1 << 0), 65 TICK_CLOSE = (1 << 1), 66 TICK_PROMOTE = (1 << 2), /* Promote mini connection to full connection */ 67}; 68 69#define TICK_QUIET 0 70 71struct network_path 72{ 73 union { 74 unsigned char buf[sizeof(struct sockaddr_in6)]; 75 struct sockaddr sockaddr; 76 } np_local_addr_u; 77#define np_local_addr np_local_addr_u.buf 78 unsigned char np_peer_addr[sizeof(struct sockaddr_in6)]; 79 void *np_peer_ctx; 80 lsquic_cid_t np_dcid; 81 unsigned short np_pack_size; 82 unsigned char np_path_id; 83}; 84 85#define NP_LOCAL_SA(path_) (&(path_)->np_local_addr_u.sockaddr) 86#define NP_PEER_SA(path_) ((struct sockaddr *) (path_)->np_peer_addr) 87#define NP_IS_IPv6(path_) (AF_INET6 == NP_LOCAL_SA(path_)->sa_family) 88 89struct conn_iface 90{ 91 enum tick_st 92 (*ci_tick) (struct lsquic_conn *, lsquic_time_t now); 93 94 void 95 (*ci_packet_in) (struct lsquic_conn *, struct lsquic_packet_in *); 96 97 /* Note: all packets "checked out" by calling this method should be 98 * returned back to the connection via ci_packet_sent() or 99 * ci_packet_not_sent() calls before the connection is ticked next. 100 * The connection, in turn, should not perform any extra processing 101 * (especially schedule more packets) during any of these method 102 * calls. This is because the checked out packets are not accounted 103 * for by the congestion controller. 104 */ 105 struct lsquic_packet_out * 106 (*ci_next_packet_to_send) (struct lsquic_conn *, size_t); 107 108 void 109 (*ci_packet_sent) (struct lsquic_conn *, struct lsquic_packet_out *); 110 111 void 112 (*ci_packet_not_sent) (struct lsquic_conn *, struct lsquic_packet_out *); 113 114 void 115 (*ci_packet_too_large) (struct lsquic_conn *, struct lsquic_packet_out *); 116 117 void 118 (*ci_hsk_done) (struct lsquic_conn *, enum lsquic_hsk_status); 119 120 void 121 (*ci_destroy) (struct lsquic_conn *); 122 123 int 124 (*ci_is_tickable) (struct lsquic_conn *); 125 126 lsquic_time_t 127 (*ci_next_tick_time) (struct lsquic_conn *, unsigned *why); 128 129 int 130 (*ci_can_write_ack) (struct lsquic_conn *); 131 132 /* No return status: best effort */ 133 void 134 (*ci_write_ack) (struct lsquic_conn *, struct lsquic_packet_out *); 135 136#if LSQUIC_CONN_STATS 137 const struct conn_stats * 138 (*ci_get_stats) (struct lsquic_conn *); 139#endif 140 141 void 142 (*ci_client_call_on_new) (struct lsquic_conn *); 143 144 enum LSQUIC_CONN_STATUS 145 (*ci_status) (struct lsquic_conn *, char *errbuf, size_t bufsz); 146 147 unsigned 148 (*ci_n_avail_streams) (const struct lsquic_conn *); 149 150 unsigned 151 (*ci_n_pending_streams) (const struct lsquic_conn *); 152 153 unsigned 154 (*ci_cancel_pending_streams) (struct lsquic_conn *, unsigned n); 155 156 void 157 (*ci_going_away) (struct lsquic_conn *); 158 159 int 160 (*ci_is_push_enabled) (struct lsquic_conn *); 161 162 /* Optional: only used by gQUIC frames reader */ 163 /* If stream is already closed, NULL is returned */ 164 struct lsquic_stream * 165 (*ci_get_stream_by_id) (struct lsquic_conn *, lsquic_stream_id_t stream_id); 166 167 struct lsquic_engine * 168 (*ci_get_engine) (struct lsquic_conn *); 169 170 struct lsquic_conn_ctx * 171 (*ci_get_ctx) (const struct lsquic_conn *); 172 173 void 174 (*ci_set_ctx) (struct lsquic_conn *, struct lsquic_conn_ctx *); 175 176 void 177 (*ci_make_stream) (struct lsquic_conn *); 178 179 void 180 (*ci_abort) (struct lsquic_conn *); 181 182 void 183 (*ci_retire_cid) (struct lsquic_conn *); 184 185 void 186 (*ci_close) (struct lsquic_conn *); 187 188 void 189 (*ci_stateless_reset) (struct lsquic_conn *); 190 191 int 192 (*ci_crypto_keysize) (const struct lsquic_conn *); 193 194 int 195 (*ci_crypto_alg_keysize) (const struct lsquic_conn *); 196 197 enum lsquic_crypto_ver 198 (*ci_crypto_ver) (const struct lsquic_conn *); 199 200 const char * 201 (*ci_crypto_cipher) (const struct lsquic_conn *); 202 203 int 204 (*ci_push_stream) (struct lsquic_conn *, void *hset, struct lsquic_stream *, 205 const struct lsquic_http_headers *headers); 206 207 /* Use this to abort the connection when unlikely errors occur */ 208 void 209 (*ci_internal_error) (struct lsquic_conn *, const char *format, ...) 210#if __GNUC__ 211 __attribute__((format(printf, 2, 3))) 212#endif 213 ; 214 215 /* Abort connection with error */ 216 void 217 (*ci_abort_error) (struct lsquic_conn *, int is_app, unsigned error_code, 218 const char *format, ...) 219#if __GNUC__ 220 __attribute__((format(printf, 4, 5))) 221#endif 222 ; 223 224 void 225 (*ci_tls_alert) (struct lsquic_conn *, uint8_t); 226 227 /* Returns 0 if connection is to be deleted immediately */ 228 lsquic_time_t 229 (*ci_drain_time) (const struct lsquic_conn *); 230 231 /* Returns true if it's time to report the connection's CIDs' liveness */ 232 int 233 (*ci_report_live) (struct lsquic_conn *, lsquic_time_t now); 234 235 /* If `local_sa' is NULL, return default path */ 236 struct network_path * 237 (*ci_get_path) (struct lsquic_conn *, const struct sockaddr *local_sa); 238 239 unsigned char 240 (*ci_record_addrs) (struct lsquic_conn *, void *peer_ctx, 241 const struct sockaddr *local_sa, const struct sockaddr *peer_sa); 242 243 const lsquic_cid_t * 244 (*ci_get_log_cid) (const struct lsquic_conn *); 245 246 /* Optional method. Only used by the IETF client code. */ 247 void 248 (*ci_drop_crypto_streams) (struct lsquic_conn *); 249 250 /* Optional method. Only used by IETF connections */ 251 void 252 (*ci_count_garbage) (struct lsquic_conn *, size_t); 253 254 /* Optional method. Must be implemented if connection sends MTU probes */ 255 void 256 (*ci_mtu_probe_acked) (struct lsquic_conn *, 257 const struct lsquic_packet_out *); 258 259 /* Optional method. It is called when RTO occurs. */ 260 void 261 (*ci_retx_timeout) (struct lsquic_conn *); 262}; 263 264#define LSCONN_CCE_BITS 3 265#define LSCONN_MAX_CCES (1 << LSCONN_CCE_BITS) 266 267struct conn_cid_elem 268{ 269 struct lsquic_hash_elem cce_hash_el; /* Must be first element */ 270 lsquic_cid_t cce_cid; 271 union { 272 unsigned seqno; 273 unsigned short port; 274 } cce_u; 275#define cce_seqno cce_u.seqno 276#define cce_port cce_u.port 277 enum conn_cce_flags { 278 CCE_USED = 1 << 0, /* Connection ID has been used */ 279 CCE_SEQNO = 1 << 1, /* cce_seqno is set (CIDs in Initial 280 * packets have no sequence number). 281 */ 282 CCE_REG = 1 << 2, /* CID has been registered */ 283 CCE_PORT = 1 << 3, /* It's not a CID element at all: 284 * cce_port is the hash value. 285 */ 286 } cce_flags; 287}; 288 289struct lsquic_conn 290{ 291 void *cn_enc_session; 292 const struct enc_session_funcs_common 293 *cn_esf_c; 294 union { 295 const struct enc_session_funcs_gquic *g; 296 const struct enc_session_funcs_iquic *i; 297 } cn_esf; 298#define cn_cid cn_cces[0].cce_cid 299 STAILQ_ENTRY(lsquic_conn) cn_next_closed_conn; 300 /* This and cn_next_closed_conn could be made into a union, as new full 301 * connections are never closed. 302 */ 303 STAILQ_ENTRY(lsquic_conn) cn_next_new_full; 304 TAILQ_ENTRY(lsquic_conn) cn_next_ticked; 305 TAILQ_ENTRY(lsquic_conn) cn_next_out; 306 TAILQ_ENTRY(lsquic_conn) cn_next_pr; 307 const struct conn_iface *cn_if; 308 const struct parse_funcs *cn_pf; 309 struct attq_elem *cn_attq_elem; 310 lsquic_time_t cn_last_sent; 311 lsquic_time_t cn_last_ticked; 312 struct conn_cid_elem *cn_cces; /* At least one is available */ 313 enum lsquic_conn_flags cn_flags; 314 enum lsquic_version cn_version:8; 315 unsigned char cn_cces_mask; /* Those that are set */ 316 unsigned char cn_n_cces; /* Number of CCEs in cn_cces */ 317 unsigned char cn_cur_cce_idx; 318#if LSQUIC_TEST 319 struct conn_cid_elem cn_cces_buf[8]; 320#define LSCONN_INITIALIZER_CID(lsconn_, cid_) { \ 321 .cn_cces = (lsconn_).cn_cces_buf, \ 322 .cn_cces_buf[0].cce_seqno = 0, \ 323 .cn_cces_buf[0].cce_flags = CCE_SEQNO, \ 324 .cn_cces_buf[0].cce_cid = (cid_), \ 325 .cn_n_cces = 8, .cn_cces_mask = 1, } 326#define LSCONN_INITIALIZER_CIDLEN(lsconn_, len_) { \ 327 .cn_cces = (lsconn_).cn_cces_buf, \ 328 .cn_cces_buf[0].cce_seqno = 0, \ 329 .cn_cces_buf[0].cce_flags = CCE_SEQNO, \ 330 .cn_cces_buf[0].cce_cid = { .len = len_ }, \ 331 .cn_n_cces = 8, .cn_cces_mask = 1, } 332#define LSCONN_INITIALIZE(lsconn_) do { \ 333 (lsconn_)->cn_cces = (lsconn_)->cn_cces_buf; \ 334 (lsconn_)->cn_n_cces = 8; (lsconn_)->cn_cces_mask = 1; } while (0) 335#endif 336}; 337 338#define END_OF_CCES(conn) ((conn)->cn_cces + (conn)->cn_n_cces) 339 340#define CN_SCID(conn) (&(conn)->cn_cces[(conn)->cn_cur_cce_idx].cce_cid) 341 342unsigned char 343lsquic_conn_record_sockaddr (lsquic_conn_t *lconn, void *peer_ctx, 344 const struct sockaddr *local_sa, const struct sockaddr *peer_sa); 345 346int 347lsquic_conn_decrypt_packet (lsquic_conn_t *lconn, 348 struct lsquic_engine_public *, struct lsquic_packet_in *); 349 350int 351lsquic_conn_copy_and_release_pi_data (const lsquic_conn_t *conn, 352 struct lsquic_engine_public *, struct lsquic_packet_in *); 353 354void 355lsquic_generate_cid (lsquic_cid_t *cid, size_t len); 356 357void 358lsquic_generate_cid_gquic (lsquic_cid_t *cid); 359 360void 361lsquic_conn_retire_cid (lsquic_conn_t *lconn); 362 363#define lsquic_conn_adv_time(c) ((c)->cn_attq_elem->ae_adv_time) 364 365#if LSQUIC_CONN_STATS 366struct conn_stats { 367 /* All counters are of the same type, unsigned long, because we cast the 368 * struct to an array to update the aggregate. 369 */ 370 unsigned long n_ticks; /* How many time connection was ticked */ 371 struct { 372 unsigned long stream_data_sz; /* Sum of all STREAM frames payload */ 373 unsigned long stream_frames; /* Number of STREAM frames */ 374 unsigned long packets, /* Incoming packets */ 375 undec_packets, /* Undecryptable packets */ 376 dup_packets, /* Duplicate packets */ 377 err_packets; /* Error packets(?) */ 378 unsigned long n_acks, 379 n_acks_proc, 380 n_acks_merged; 381 unsigned long bytes; /* Overall bytes in */ 382 unsigned long headers_uncomp; /* Sum of uncompressed header bytes */ 383 unsigned long headers_comp; /* Sum of compressed header bytes */ 384 } in; 385 struct { 386 unsigned long stream_data_sz; 387 unsigned long stream_frames; 388 unsigned long acks; 389 unsigned long packets; /* Number of sent packets */ 390 unsigned long acked_via_loss; /* Number of packets acked via loss record */ 391 unsigned long retx_packets; /* Number of retransmitted packets */ 392 unsigned long bytes; /* Overall bytes out */ 393 unsigned long headers_uncomp; /* Sum of uncompressed header bytes */ 394 unsigned long headers_comp; /* Sum of compressed header bytes */ 395 } out; 396}; 397#endif 398 399#endif 400