lsquic_ev_log.c revision 7d09751d
1/* Copyright (c) 2017 - 2020 LiteSpeed Technologies Inc. See LICENSE. */ 2#ifndef WIN32 3#include <arpa/inet.h> 4#else 5#include <vc_compat.h> 6#endif 7#include <errno.h> 8#include <inttypes.h> 9#include <stdlib.h> 10#include <string.h> 11#include <sys/queue.h> 12 13#include "lsquic.h" 14#include "lsquic_types.h" 15#include "lsquic_int_types.h" 16#include "lsquic_packet_common.h" 17#include "lsquic_packet_gquic.h" 18#include "lsquic_packet_in.h" 19#include "lsquic_packet_out.h" 20#include "lsquic_parse.h" 21#include "lsquic_frame_common.h" 22#include "lsquic_headers.h" 23#include "lsquic_str.h" 24#include "lsquic_frame_reader.h" 25#include "lsquic_enc_sess.h" 26#include "lsquic_ev_log.h" 27#include "lsquic_sizes.h" 28#include "lsquic_trans_params.h" 29#include "lsquic_util.h" 30#include "lsquic_hash.h" 31#include "lsquic_conn.h" 32 33#define LSQUIC_LOGGER_MODULE LSQLM_EVENT 34#include "lsquic_logger.h" 35 36 37/* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */ 38/* |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| */ 39/* Messages that do not include connection ID go above this point */ 40 41#define LSQUIC_LOG_CONN_ID cid 42#define LCID(...) LSQ_LOG2(LSQ_LOG_DEBUG, __VA_ARGS__) /* LCID: log with CID */ 43 44/* Messages that are to include connection ID go below this point */ 45/* |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| */ 46/* VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV */ 47 48void 49lsquic_ev_log_packet_in (const lsquic_cid_t *cid, 50 const lsquic_packet_in_t *packet_in) 51{ 52 switch (packet_in->pi_flags & ( 53 PI_FROM_MINI| 54 PI_GQUIC)) 55 { 56 case PI_FROM_MINI|PI_GQUIC: 57 LCID("packet in: %"PRIu64" (from mini)", packet_in->pi_packno); 58 break; 59 case PI_FROM_MINI: 60 LCID("packet in: %"PRIu64" (from mini), type: %s, ecn: %u", 61 packet_in->pi_packno, lsquic_hety2str[packet_in->pi_header_type], 62 lsquic_packet_in_ecn(packet_in)); 63 break; 64 case PI_GQUIC: 65 LCID("packet in: %"PRIu64", size: %u", packet_in->pi_packno, 66 (unsigned) (packet_in->pi_data_sz + GQUIC_PACKET_HASH_SZ)); 67 break; 68 default: 69 if (packet_in->pi_flags & PI_LOG_QL_BITS) 70 LCID("packet in: %"PRIu64", type: %s, size: %u; ecn: %u, spin: %d; " 71 "path: %hhu; Q: %d; L: %d", 72 packet_in->pi_packno, lsquic_hety2str[packet_in->pi_header_type], 73 (unsigned) (packet_in->pi_data_sz + IQUIC_TAG_LEN), 74 lsquic_packet_in_ecn(packet_in), 75 /* spin bit value is only valid for short packet headers */ 76 lsquic_packet_in_spin_bit(packet_in), packet_in->pi_path_id, 77 ((packet_in->pi_flags & PI_SQUARE_BIT) > 0), 78 ((packet_in->pi_flags & PI_LOSS_BIT) > 0)); 79 else 80 LCID("packet in: %"PRIu64", type: %s, size: %u; ecn: %u, spin: %d; " 81 "path: %hhu", 82 packet_in->pi_packno, lsquic_hety2str[packet_in->pi_header_type], 83 (unsigned) (packet_in->pi_data_sz + IQUIC_TAG_LEN), 84 lsquic_packet_in_ecn(packet_in), 85 /* spin bit value is only valid for short packet headers */ 86 lsquic_packet_in_spin_bit(packet_in), packet_in->pi_path_id); 87 break; 88 } 89} 90 91 92void 93lsquic_ev_log_ack_frame_in (const lsquic_cid_t *cid, 94 const struct ack_info *acki) 95{ 96 char buf[MAX_ACKI_STR_SZ]; 97 98 lsquic_acki2str(acki, buf, sizeof(buf)); 99 LCID("ACK frame in: %s", buf); 100} 101 102 103void 104lsquic_ev_log_stream_frame_in (const lsquic_cid_t *cid, 105 const struct stream_frame *frame) 106{ 107 LCID("STREAM frame in: stream %"PRIu64"; offset %"PRIu64"; size %"PRIu16 108 "; fin: %d", frame->stream_id, frame->data_frame.df_offset, 109 frame->data_frame.df_size, (int) frame->data_frame.df_fin); 110} 111 112 113void 114lsquic_ev_log_crypto_frame_in (const lsquic_cid_t *cid, 115 const struct stream_frame *frame, unsigned enc_level) 116{ 117 LCID("CRYPTO frame in: level %u; offset %"PRIu64"; size %"PRIu16, 118 enc_level, frame->data_frame.df_offset, frame->data_frame.df_size); 119} 120 121 122void 123lsquic_ev_log_stop_waiting_frame_in (const lsquic_cid_t *cid, 124 lsquic_packno_t least) 125{ 126 LCID("STOP_WAITING frame in: least unacked packno %"PRIu64, least); 127} 128 129 130void 131lsquic_ev_log_window_update_frame_in (const lsquic_cid_t *cid, 132 lsquic_stream_id_t stream_id, uint64_t offset) 133{ 134 LCID("WINDOW_UPDATE frame in: stream %"PRIu64"; offset %"PRIu64, 135 stream_id, offset); 136} 137 138 139void 140lsquic_ev_log_blocked_frame_in (const lsquic_cid_t *cid, 141 lsquic_stream_id_t stream_id) 142{ 143 LCID("BLOCKED frame in: stream %"PRIu64, stream_id); 144} 145 146 147void 148lsquic_ev_log_connection_close_frame_in (const lsquic_cid_t *cid, 149 uint64_t error_code, int reason_len, const char *reason) 150{ 151 LCID("CONNECTION_CLOSE frame in: error code %"PRIu64", reason: %.*s", 152 error_code, reason_len, reason); 153} 154 155 156void 157lsquic_ev_log_goaway_frame_in (const lsquic_cid_t *cid, uint32_t error_code, 158 lsquic_stream_id_t stream_id, int reason_len, const char *reason) 159{ 160 LCID("GOAWAY frame in: error code %"PRIu32", stream %"PRIu64 161 ", reason: %.*s", error_code, stream_id, reason_len, reason); 162} 163 164 165void 166lsquic_ev_log_rst_stream_frame_in (const lsquic_cid_t *cid, 167 lsquic_stream_id_t stream_id, uint64_t offset, uint64_t error_code) 168{ 169 LCID("RST_STREAM frame in: error code %"PRIu64", stream %"PRIu64 170 ", offset: %"PRIu64, error_code, stream_id, offset); 171} 172 173 174void 175lsquic_ev_log_stop_sending_frame_in (const lsquic_cid_t *cid, 176 lsquic_stream_id_t stream_id, uint64_t error_code) 177{ 178 LCID("STOP_SENDING frame in: error code %"PRIu64", stream %"PRIu64, 179 error_code, stream_id); 180} 181 182 183void 184lsquic_ev_log_padding_frame_in (const lsquic_cid_t *cid, size_t len) 185{ 186 LCID("PADDING frame in of %zd bytes", len); 187} 188 189 190void 191lsquic_ev_log_ping_frame_in (const lsquic_cid_t *cid) 192{ 193 LCID("PING frame in"); 194} 195 196 197void 198lsquic_ev_log_packet_created (const lsquic_cid_t *cid, 199 const struct lsquic_packet_out *packet_out) 200{ 201 LCID("created packet %"PRIu64"; flags: version=%d, nonce=%d, conn_id=%d", 202 packet_out->po_packno, 203 !!(packet_out->po_flags & PO_VERSION), 204 !!(packet_out->po_flags & PO_NONCE), 205 !!(packet_out->po_flags & PO_CONN_ID)); 206} 207 208 209void 210lsquic_ev_log_packet_sent (const lsquic_cid_t *cid, 211 const struct lsquic_packet_out *packet_out) 212{ 213 char frames[lsquic_frame_types_str_sz]; 214 if (lsquic_packet_out_verneg(packet_out)) 215 LCID("sent version negotiation packet, size %hu", 216 packet_out->po_data_sz); 217 else if (lsquic_packet_out_retry(packet_out)) 218 LCID("sent stateless retry packet, size %hu", packet_out->po_data_sz); 219 else if (lsquic_packet_out_pubres(packet_out)) 220 LCID("sent public reset packet, size %hu", packet_out->po_data_sz); 221 else if (packet_out->po_lflags & POL_GQUIC) 222 LCID("sent packet %"PRIu64", size %hu, frame types: %s", 223 packet_out->po_packno, packet_out->po_enc_data_sz, 224 /* Frame types is a list of different frames types contained 225 * in the packet, no more. Count and order of frames is not 226 * printed. 227 */ 228 lsquic_frame_types_to_str(frames, sizeof(frames), 229 packet_out->po_frame_types)); 230 else if (packet_out->po_lflags & POL_LOG_QL_BITS) 231 LCID("sent packet %"PRIu64", type %s, crypto: %s, size %hu, frame " 232 "types: %s, ecn: %u, spin: %d; kp: %u, path: %hhu, flags: %u; " 233 "Q: %u; L: %u", 234 packet_out->po_packno, lsquic_hety2str[packet_out->po_header_type], 235 lsquic_enclev2str[ lsquic_packet_out_enc_level(packet_out) ], 236 packet_out->po_enc_data_sz, 237 /* Frame types is a list of different frames types contained 238 * in the packet, no more. Count and order of frames is not 239 * printed. 240 */ 241 lsquic_frame_types_to_str(frames, sizeof(frames), 242 packet_out->po_frame_types), 243 lsquic_packet_out_ecn(packet_out), 244 /* spin bit value is only valid for short packet headers */ 245 lsquic_packet_out_spin_bit(packet_out), 246 lsquic_packet_out_kp(packet_out), 247 packet_out->po_path->np_path_id, 248 (unsigned) packet_out->po_flags, 249 lsquic_packet_out_square_bit(packet_out), 250 lsquic_packet_out_loss_bit(packet_out)); 251 else 252 LCID("sent packet %"PRIu64", type %s, crypto: %s, size %hu, frame " 253 "types: %s, ecn: %u, spin: %d; kp: %u, path: %hhu, flags: %u", 254 packet_out->po_packno, lsquic_hety2str[packet_out->po_header_type], 255 lsquic_enclev2str[ lsquic_packet_out_enc_level(packet_out) ], 256 packet_out->po_enc_data_sz, 257 /* Frame types is a list of different frames types contained 258 * in the packet, no more. Count and order of frames is not 259 * printed. 260 */ 261 lsquic_frame_types_to_str(frames, sizeof(frames), 262 packet_out->po_frame_types), 263 lsquic_packet_out_ecn(packet_out), 264 /* spin bit value is only valid for short packet headers */ 265 lsquic_packet_out_spin_bit(packet_out), 266 lsquic_packet_out_kp(packet_out), 267 packet_out->po_path->np_path_id, 268 (unsigned) packet_out->po_flags); 269} 270 271 272void 273lsquic_ev_log_packet_not_sent (const lsquic_cid_t *cid, 274 const struct lsquic_packet_out *packet_out) 275{ 276 char frames[lsquic_frame_types_str_sz]; 277 LCID("unsent packet %"PRIu64", size %hu, frame types: %s", 278 packet_out->po_packno, packet_out->po_enc_data_sz, 279 /* Frame types is a list of different frames types contained in 280 * the packet, no more. Count and order of frames is not printed. 281 */ 282 lsquic_frame_types_to_str(frames, sizeof(frames), 283 packet_out->po_frame_types)); 284} 285 286 287void 288lsquic_ev_log_http_headers_in (const lsquic_cid_t *cid, int is_server, 289 const struct uncompressed_headers *uh) 290{ 291 const struct http1x_headers *h1h; 292 const char *cr, *p; 293 294 if (uh->uh_flags & UH_PP) 295 LCID("read push promise; stream %"PRIu64", promised stream %"PRIu64, 296 uh->uh_stream_id, uh->uh_oth_stream_id); 297 else 298 LCID("read %s headers; stream: %"PRIu64", depends on stream: %"PRIu64 299 ", weight: %hu, exclusive: %d, fin: %d", 300 is_server ? "request" : "response", 301 uh->uh_stream_id, uh->uh_oth_stream_id, uh->uh_weight, 302 (int) uh->uh_exclusive, !!(uh->uh_flags & UH_FIN)); 303 304 if (uh->uh_flags & UH_H1H) 305 { 306 h1h = uh->uh_hset; 307 for (p = h1h->h1h_buf; p < h1h->h1h_buf + h1h->h1h_size; p = cr + 2) 308 { 309 cr = strchr(p, '\r'); 310 if (cr && cr > p) 311 LCID(" %.*s", (int) (cr - p), p); 312 else 313 break; 314 } 315 } 316} 317 318 319void 320lsquic_ev_log_action_stream_frame (const lsquic_cid_t *cid, 321 const struct parse_funcs *pf, const unsigned char *buf, size_t bufsz, 322 const char *what) 323{ 324 struct stream_frame frame; 325 int len; 326 327 len = pf->pf_parse_stream_frame(buf, bufsz, &frame); 328 if (len > 0) 329 LCID("%s STREAM frame: stream %"PRIu64", offset: %"PRIu64 330 ", size: %"PRIu16", fin: %d", what, frame.stream_id, 331 frame.data_frame.df_offset, frame.data_frame.df_size, 332 frame.data_frame.df_fin); 333 else 334 LSQ_LOG2(LSQ_LOG_WARN, "cannot parse STREAM frame"); 335} 336 337 338void 339lsquic_ev_log_generated_crypto_frame (const lsquic_cid_t *cid, 340 const struct parse_funcs *pf, const unsigned char *buf, size_t bufsz) 341{ 342 struct stream_frame frame; 343 int len; 344 345 len = pf->pf_parse_crypto_frame(buf, bufsz, &frame); 346 if (len > 0) 347 LCID("generated CRYPTO frame: offset: %"PRIu64", size: %"PRIu16, 348 frame.data_frame.df_offset, frame.data_frame.df_size); 349 else 350 LSQ_LOG2(LSQ_LOG_WARN, "cannot parse CRYPTO frame"); 351} 352 353 354void 355lsquic_ev_log_generated_ack_frame (const lsquic_cid_t *cid, 356 const struct parse_funcs *pf, const unsigned char *ack_buf, 357 size_t ack_buf_sz) 358{ 359 struct ack_info acki; 360 int len; 361 char buf[MAX_ACKI_STR_SZ]; 362 363 len = pf->pf_parse_ack_frame(ack_buf, ack_buf_sz, &acki, 364 TP_DEF_ACK_DELAY_EXP); 365 if (len < 0) 366 { 367 LSQ_LOG2(LSQ_LOG_WARN, "cannot parse ACK frame"); 368 return; 369 } 370 371 lsquic_acki2str(&acki, buf, sizeof(buf)); 372 LCID("generated ACK frame: %s", buf); 373} 374 375 376void 377lsquic_ev_log_generated_new_token_frame (const lsquic_cid_t *cid, 378 const struct parse_funcs *pf, const unsigned char *frame_buf, 379 size_t frame_buf_sz) 380{ 381 const unsigned char *token; 382 size_t sz; 383 char *buf; 384 int len; 385 386 len = pf->pf_parse_new_token_frame(frame_buf, frame_buf_sz, &token, &sz); 387 if (len < 0) 388 { 389 LSQ_LOG2(LSQ_LOG_WARN, "cannot parse NEW_TOKEN frame"); 390 return; 391 } 392 393 buf = malloc(sz * 2 + 1); 394 if (buf) 395 { 396 lsquic_hexstr(token, sz, buf, sz * 2 + 1); 397 LCID("generated NEW_TOKEN frame: %s", buf); 398 free(buf); 399 } 400} 401 402 403void 404lsquic_ev_log_generated_path_chal_frame (const lsquic_cid_t *cid, 405 const struct parse_funcs *pf, const unsigned char *frame_buf, 406 size_t frame_buf_sz) 407{ 408 uint64_t chal; 409 int len; 410 char hexbuf[sizeof(chal) * 2 + 1]; 411 412 len = pf->pf_parse_path_chal_frame(frame_buf, frame_buf_sz, &chal); 413 if (len > 0) 414 LCID("generated PATH_CHALLENGE(%s) frame", 415 HEXSTR((unsigned char *) &chal, sizeof(chal), hexbuf)); 416 else 417 LSQ_LOG2(LSQ_LOG_WARN, "cannot parse PATH_CHALLENGE frame"); 418} 419 420 421void 422lsquic_ev_log_generated_path_resp_frame (const lsquic_cid_t *cid, 423 const struct parse_funcs *pf, const unsigned char *frame_buf, 424 size_t frame_buf_sz) 425{ 426 uint64_t resp; 427 int len; 428 char hexbuf[sizeof(resp) * 2 + 1]; 429 430 len = pf->pf_parse_path_resp_frame(frame_buf, frame_buf_sz, &resp); 431 if (len > 0) 432 LCID("generated PATH_RESPONSE(%s) frame", 433 HEXSTR((unsigned char *) &resp, sizeof(resp), hexbuf)); 434 else 435 LSQ_LOG2(LSQ_LOG_WARN, "cannot parse PATH_RESPONSE frame"); 436} 437 438 439void 440lsquic_ev_log_generated_new_connection_id_frame (const lsquic_cid_t *cid, 441 const struct parse_funcs *pf, const unsigned char *frame_buf, 442 size_t frame_buf_sz) 443{ 444 const unsigned char *token; 445 lsquic_cid_t new_cid; 446 uint64_t seqno, retire_prior_to; 447 int len; 448 char token_buf[IQUIC_SRESET_TOKEN_SZ * 2 + 1]; 449 char cid_buf[MAX_CID_LEN * 2 + 1]; 450 451 len = pf->pf_parse_new_conn_id(frame_buf, frame_buf_sz, &seqno, 452 &retire_prior_to, &new_cid, &token); 453 if (len < 0) 454 { 455 LSQ_LOG2(LSQ_LOG_WARN, "cannot parse NEW_CONNECTION_ID frame"); 456 return; 457 } 458 459 lsquic_hexstr(new_cid.idbuf, new_cid.len, cid_buf, sizeof(cid_buf)); 460 lsquic_hexstr(token, IQUIC_SRESET_TOKEN_SZ, token_buf, sizeof(token_buf)); 461 LCID("generated NEW_CONNECTION_ID frame: seqno: %"PRIu64"; retire prior " 462 "to: %"PRIu64"; cid: %s; token: %s", seqno, retire_prior_to, 463 cid_buf, token_buf); 464} 465 466 467void 468lsquic_ev_log_generated_stop_waiting_frame (const lsquic_cid_t *cid, 469 lsquic_packno_t lunack) 470{ 471 LCID("generated STOP_WAITING frame; least unacked: %"PRIu64, lunack); 472} 473 474 475void 476lsquic_ev_log_generated_stop_sending_frame (const lsquic_cid_t *cid, 477 lsquic_stream_id_t stream_id, uint16_t error_code) 478{ 479 LCID("generated STOP_SENDING frame; stream ID: %"PRIu64"; error code: " 480 "%"PRIu16, stream_id, error_code); 481} 482 483 484void 485lsquic_ev_log_generated_http_headers (const lsquic_cid_t *cid, 486 lsquic_stream_id_t stream_id, 487 int is_server, const struct http_prio_frame *prio_frame, 488 const struct lsquic_http_headers *headers) 489{ 490 lsquic_stream_id_t dep_stream_id; 491 int exclusive, i; 492 unsigned short weight; 493 494 if (is_server) 495 LCID("generated HTTP response HEADERS for stream %"PRIu64, stream_id); 496 else 497 { 498 memcpy(&dep_stream_id, prio_frame->hpf_stream_id, 4); 499 dep_stream_id = htonl(dep_stream_id); 500 exclusive = dep_stream_id >> 31; 501 dep_stream_id &= ~(1 << 31); 502 weight = prio_frame->hpf_weight + 1; 503 LCID("generated HTTP request HEADERS for stream %"PRIu64 504 ", dep stream: %"PRIu64", weight: %hu, exclusive: %d", stream_id, 505 dep_stream_id, weight, exclusive); 506 } 507 508 for (i = 0; i < headers->count; ++i) 509 LCID(" %.*s: %.*s", 510 (int) headers->headers[i].name.iov_len, 511 (char *) headers->headers[i].name.iov_base, 512 (int) headers->headers[i].value.iov_len, 513 (char *) headers->headers[i].value.iov_base); 514} 515 516 517void 518lsquic_ev_log_generated_http_push_promise (const lsquic_cid_t *cid, 519 lsquic_stream_id_t stream_id, lsquic_stream_id_t promised_stream_id, 520 const struct lsquic_http_headers *headers, 521 const struct lsquic_http_headers *extra_headers) 522{ 523 int i; 524 525 LCID("generated HTTP PUSH_PROMISE for stream %"PRIu64"; promised stream %" 526 PRIu64, stream_id, promised_stream_id); 527 528 for (i = 0; i < headers->count; ++i) 529 LCID(" %.*s: %.*s", 530 (int) headers->headers[i].name.iov_len, 531 (char *) headers->headers[i].name.iov_base, 532 (int) headers->headers[i].value.iov_len, 533 (char *) headers->headers[i].value.iov_base); 534 535 if (extra_headers) 536 for (i = 0; i < extra_headers->count; ++i) 537 LCID(" %.*s: %.*s", 538 (int) extra_headers->headers[i].name.iov_len, 539 (char *) extra_headers->headers[i].name.iov_base, 540 (int) extra_headers->headers[i].value.iov_len, 541 (char *) extra_headers->headers[i].value.iov_base); 542} 543 544void 545lsquic_ev_log_create_connection (const lsquic_cid_t *cid, 546 const struct sockaddr *local_sa, 547 const struct sockaddr *peer_sa) 548{ 549 LCID("connection created"); 550} 551 552 553void 554lsquic_ev_log_hsk_completed (const lsquic_cid_t *cid) 555{ 556 LCID("handshake completed"); 557} 558 559 560void 561lsquic_ev_log_zero_rtt (const lsquic_cid_t *cid) 562{ 563 LCID("zero_rtt successful"); 564} 565 566 567void 568lsquic_ev_log_check_certs (const lsquic_cid_t *cid, const lsquic_str_t **certs, 569 size_t count) 570{ 571 LCID("check certs"); 572} 573 574 575void 576lsquic_ev_log_version_negotiation (const lsquic_cid_t *cid, 577 const char *action, const char *ver) 578{ 579 LCID("version negotiation: %s version %s", action, ver); 580} 581