lsquic_ev_log.c revision 50aadb33
1/* Copyright (c) 2017 LiteSpeed Technologies Inc. See LICENSE. */ 2#include <arpa/inet.h> 3#include <errno.h> 4#include <inttypes.h> 5#include <stdlib.h> 6#include <string.h> 7 8#include "lsquic.h" 9#include "lsquic_types.h" 10#include "lsquic_int_types.h" 11#include "lsquic_packet_common.h" 12#include "lsquic_packet_in.h" 13#include "lsquic_packet_out.h" 14#include "lsquic_parse.h" 15#include "lsquic_frame_common.h" 16#include "lsquic_frame_reader.h" 17#include "lsquic_ev_log.h" 18 19#define LSQUIC_LOGGER_MODULE LSQLM_EVENT 20#include "lsquic_logger.h" 21 22 23/* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */ 24/* |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| */ 25/* Messages that do not include connection ID go above this point */ 26 27#define LSQUIC_LOG_CONN_ID cid 28#define LCID(a...) LSQ_LOG2(LSQ_LOG_DEBUG, a) /* LCID: log with CID */ 29 30/* Messages that are to include connection ID go below this point */ 31/* |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| */ 32/* VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV */ 33 34void 35lsquic_ev_log_packet_in (lsquic_cid_t cid, const lsquic_packet_in_t *packet_in) 36{ 37 LCID("packet in: %"PRIu64, packet_in->pi_packno); 38} 39 40 41void 42lsquic_ev_log_ack_frame_in (lsquic_cid_t cid, const struct ack_info *acki) 43{ 44 size_t sz; 45 char *buf; 46 47 if ((buf = acki2str(acki, &sz))) 48 { 49 LCID("ACK frame in: %.*s", (int) sz, buf); 50 free(buf); 51 } 52} 53 54 55void 56lsquic_ev_log_stream_frame_in (lsquic_cid_t cid, 57 const struct stream_frame *frame) 58{ 59 LCID("STREAM frame in: stream %u; offset %"PRIu64"; size %"PRIu16 60 "; fin: %d", frame->stream_id, frame->data_frame.df_offset, 61 frame->data_frame.df_size, (int) frame->data_frame.df_fin); 62} 63 64 65void 66lsquic_ev_log_stop_waiting_frame_in (lsquic_cid_t cid, lsquic_packno_t least) 67{ 68 LCID("STOP_WAITING frame in: least unacked packno %"PRIu64, least); 69} 70 71 72void 73lsquic_ev_log_window_update_frame_in (lsquic_cid_t cid, uint32_t stream_id, 74 uint64_t offset) 75{ 76 LCID("WINDOW_UPDATE frame in: stream %"PRIu32"; offset %"PRIu64, 77 stream_id, offset); 78} 79 80 81void 82lsquic_ev_log_blocked_frame_in (lsquic_cid_t cid, uint32_t stream_id) 83{ 84 LCID("BLOCKED frame in: stream %"PRIu32, stream_id); 85} 86 87 88void 89lsquic_ev_log_connection_close_frame_in (lsquic_cid_t cid, 90 uint32_t error_code, int reason_len, const char *reason) 91{ 92 LCID("CONNECTION_CLOSE frame in: error code %"PRIu32", reason: %.*s", 93 error_code, reason_len, reason); 94} 95 96 97void 98lsquic_ev_log_goaway_frame_in (lsquic_cid_t cid, uint32_t error_code, 99 uint32_t stream_id, int reason_len, const char *reason) 100{ 101 LCID("GOAWAY frame in: error code %"PRIu32", stream %"PRIu32 102 ", reason: %.*s", error_code, stream_id, reason_len, reason); 103} 104 105 106void 107lsquic_ev_log_rst_stream_frame_in (lsquic_cid_t cid, uint32_t stream_id, 108 uint64_t offset, uint32_t error_code) 109{ 110 LCID("RST_FRAME frame in: error code %"PRIu32", stream %"PRIu32 111 ", offset: %"PRIu64, error_code, stream_id, offset); 112} 113 114 115void 116lsquic_ev_log_padding_frame_in (lsquic_cid_t cid, size_t len) 117{ 118 LCID("PADDING frame in of %zd bytes", len); 119} 120 121 122void 123lsquic_ev_log_ping_frame_in (lsquic_cid_t cid) 124{ 125 LCID("PING frame in"); 126} 127 128 129void 130lsquic_ev_log_packet_created (lsquic_cid_t cid, 131 const struct lsquic_packet_out *packet_out) 132{ 133 LCID("created packet %"PRIu64"; flags: version=%d, nonce=%d, conn_id=%d", 134 packet_out->po_packno, 135 !!(packet_out->po_flags & PO_VERSION), 136 !!(packet_out->po_flags & PO_NONCE), 137 !!(packet_out->po_flags & PO_CONN_ID)); 138} 139 140 141void 142lsquic_ev_log_packet_sent (lsquic_cid_t cid, 143 const struct lsquic_packet_out *packet_out) 144{ 145 char frames[lsquic_frame_types_str_sz]; 146 if (lsquic_packet_out_verneg(packet_out)) 147 LCID("sent version negotiation packet, size %hu", 148 packet_out->po_data_sz); 149 else if (lsquic_packet_out_pubres(packet_out)) 150 LCID("sent public reset packet, size %hu", packet_out->po_data_sz); 151 else 152 LCID("sent packet %"PRIu64", size %hu, frame types: %s", 153 packet_out->po_packno, packet_out->po_enc_data_sz, 154 /* Frame types is a list of different frames types contained 155 * in the packet, no more. Count and order of frames is not 156 * printed. 157 */ 158 lsquic_frame_types_to_str(frames, sizeof(frames), 159 packet_out->po_frame_types)); 160} 161 162 163void 164lsquic_ev_log_packet_not_sent (lsquic_cid_t cid, 165 const struct lsquic_packet_out *packet_out) 166{ 167 char frames[lsquic_frame_types_str_sz]; 168 LCID("unsent packet %"PRIu64", size %hu, frame types: %s", 169 packet_out->po_packno, packet_out->po_enc_data_sz, 170 /* Frame types is a list of different frames types contained in 171 * the packet, no more. Count and order of frames is not printed. 172 */ 173 lsquic_frame_types_to_str(frames, sizeof(frames), 174 packet_out->po_frame_types)); 175} 176 177 178void 179lsquic_ev_log_http_headers_in (lsquic_cid_t cid, int is_server, 180 const struct uncompressed_headers *uh) 181{ 182 const char *cr, *p; 183 184 if (uh->uh_flags & UH_PP) 185 LCID("read push promise; stream %"PRIu32", promised stream %"PRIu32, 186 uh->uh_stream_id, uh->uh_oth_stream_id); 187 else 188 LCID("read %s headers; stream: %"PRIu32", depends on stream: %"PRIu32 189 ", weight: %hu, exclusive: %d, fin: %d", 190 is_server ? "request" : "response", 191 uh->uh_stream_id, uh->uh_oth_stream_id, uh->uh_weight, 192 (int) uh->uh_exclusive, !!(uh->uh_flags & UH_FIN)); 193 194 for (p = uh->uh_headers; p < uh->uh_headers + uh->uh_size; p = cr + 2) 195 { 196 cr = strchr(p, '\r'); 197 if (cr && cr > p) 198 LCID(" %.*s", (int) (cr - p), p); 199 else 200 break; 201 } 202} 203 204 205void 206lsquic_ev_log_generated_stream_frame (lsquic_cid_t cid, 207 const struct parse_funcs *pf, const unsigned char *buf, size_t bufsz) 208{ 209 struct stream_frame frame; 210 int len; 211 212 len = pf->pf_parse_stream_frame(buf, bufsz, &frame); 213 if (len > 0) 214 LCID("generated STREAM frame: stream %"PRIu32", offset: %"PRIu64 215 ", size: %"PRIu16", fin: %d", frame.stream_id, 216 frame.data_frame.df_offset, frame.data_frame.df_size, 217 frame.data_frame.df_fin); 218 else 219 LSQ_LOG2(LSQ_LOG_WARN, "cannot parse STREAM frame"); 220} 221 222 223void 224lsquic_ev_log_generated_ack_frame (lsquic_cid_t cid, const struct parse_funcs *pf, 225 const unsigned char *ack_buf, size_t ack_buf_sz) 226{ 227 struct ack_info acki; 228 size_t sz; 229 char *buf; 230 int len; 231 232 len = pf->pf_parse_ack_frame(ack_buf, ack_buf_sz, &acki); 233 if (len < 0) 234 { 235 LSQ_LOG2(LSQ_LOG_WARN, "cannot parse ACK frame"); 236 return; 237 } 238 239 if ((buf = acki2str(&acki, &sz))) 240 { 241 LCID("generated ACK frame: %.*s", (int) sz, buf); 242 free(buf); 243 } 244} 245 246 247void 248lsquic_ev_log_generated_stop_waiting_frame (lsquic_cid_t cid, 249 lsquic_packno_t lunack) 250{ 251 LCID("generated STOP_WAITING frame; least unacked: %"PRIu64, lunack); 252} 253 254 255void 256lsquic_ev_log_generated_http_headers (lsquic_cid_t cid, uint32_t stream_id, 257 int is_server, const struct http_prio_frame *prio_frame, 258 const struct lsquic_http_headers *headers) 259{ 260 uint32_t dep_stream_id; 261 int exclusive, i; 262 unsigned short weight; 263 264 if (is_server) 265 LCID("generated HTTP response HEADERS for stream %"PRIu32, stream_id); 266 else 267 { 268 memcpy(&dep_stream_id, prio_frame->hpf_stream_id, 4); 269 dep_stream_id = htonl(dep_stream_id); 270 exclusive = dep_stream_id >> 31; 271 dep_stream_id &= ~(1 << 31); 272 weight = prio_frame->hpf_weight + 1; 273 LCID("generated HTTP request HEADERS for stream %"PRIu32 274 ", dep stream: %"PRIu32", weight: %hu, exclusive: %d", stream_id, 275 dep_stream_id, weight, exclusive); 276 } 277 278 for (i = 0; i < headers->count; ++i) 279 LCID(" %.*s: %.*s", 280 (int) headers->headers[i].name.iov_len, 281 (char *) headers->headers[i].name.iov_base, 282 (int) headers->headers[i].value.iov_len, 283 (char *) headers->headers[i].value.iov_base); 284} 285 286 287void 288lsquic_ev_log_generated_http_push_promise (lsquic_cid_t cid, 289 uint32_t stream_id, uint32_t promised_stream_id, 290 const struct lsquic_http_headers *headers, 291 const struct lsquic_http_headers *extra_headers) 292{ 293 int i; 294 295 LCID("generated HTTP PUSH_PROMISE for stream %"PRIu32"; promised stream %" 296 PRIu32, stream_id, promised_stream_id); 297 298 for (i = 0; i < headers->count; ++i) 299 LCID(" %.*s: %.*s", 300 (int) headers->headers[i].name.iov_len, 301 (char *) headers->headers[i].name.iov_base, 302 (int) headers->headers[i].value.iov_len, 303 (char *) headers->headers[i].value.iov_base); 304 305 if (extra_headers) 306 for (i = 0; i < extra_headers->count; ++i) 307 LCID(" %.*s: %.*s", 308 (int) extra_headers->headers[i].name.iov_len, 309 (char *) extra_headers->headers[i].name.iov_base, 310 (int) extra_headers->headers[i].value.iov_len, 311 (char *) extra_headers->headers[i].value.iov_base); 312} 313 314 315