ngtcp2_qlog.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223
  1. /*
  2. * ngtcp2
  3. *
  4. * Copyright (c) 2019 ngtcp2 contributors
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #include "ngtcp2_qlog.h"
  26. #include <assert.h>
  27. #include "ngtcp2_str.h"
  28. #include "ngtcp2_vec.h"
  29. #include "ngtcp2_conv.h"
  30. #include "ngtcp2_net.h"
  31. #include "ngtcp2_unreachable.h"
  32. #include "ngtcp2_conn_stat.h"
  33. void ngtcp2_qlog_init(ngtcp2_qlog *qlog, ngtcp2_qlog_write write,
  34. ngtcp2_tstamp ts, void *user_data) {
  35. qlog->write = write;
  36. qlog->ts = qlog->last_ts = ts;
  37. qlog->user_data = user_data;
  38. }
  39. #define write_verbatim(DEST, S) ngtcp2_cpymem((DEST), (S), sizeof(S) - 1)
  40. static uint8_t *write_string_impl(uint8_t *p, const uint8_t *data,
  41. size_t datalen) {
  42. *p++ = '"';
  43. if (datalen) {
  44. p = ngtcp2_cpymem(p, data, datalen);
  45. }
  46. *p++ = '"';
  47. return p;
  48. }
  49. #define write_string(DEST, S) \
  50. write_string_impl((DEST), (const uint8_t *)(S), sizeof(S) - 1)
  51. #define NGTCP2_LOWER_XDIGITS "0123456789abcdef"
  52. static uint8_t *write_hex(uint8_t *p, const uint8_t *data, size_t datalen) {
  53. const uint8_t *b = data, *end = data + datalen;
  54. *p++ = '"';
  55. for (; b != end; ++b) {
  56. *p++ = (uint8_t)NGTCP2_LOWER_XDIGITS[*b >> 4];
  57. *p++ = (uint8_t)NGTCP2_LOWER_XDIGITS[*b & 0xf];
  58. }
  59. *p++ = '"';
  60. return p;
  61. }
  62. static uint8_t *write_cid(uint8_t *p, const ngtcp2_cid *cid) {
  63. return write_hex(p, cid->data, cid->datalen);
  64. }
  65. static uint8_t *write_number(uint8_t *p, uint64_t n) {
  66. size_t nlen = 0;
  67. uint64_t t;
  68. uint8_t *res;
  69. if (n == 0) {
  70. *p++ = '0';
  71. return p;
  72. }
  73. for (t = n; t; t /= 10, ++nlen)
  74. ;
  75. p += nlen;
  76. res = p;
  77. for (; n; n /= 10) {
  78. *--p = (uint8_t)((n % 10) + '0');
  79. }
  80. return res;
  81. }
  82. static uint8_t *write_tstamp(uint8_t *p, ngtcp2_tstamp ts) {
  83. return write_number(p, ts / NGTCP2_MILLISECONDS);
  84. }
  85. static uint8_t *write_duration(uint8_t *p, ngtcp2_duration duration) {
  86. return write_number(p, duration / NGTCP2_MILLISECONDS);
  87. }
  88. static uint8_t *write_bool(uint8_t *p, int b) {
  89. if (b) {
  90. return ngtcp2_cpymem(p, "true", sizeof("true") - 1);
  91. }
  92. return ngtcp2_cpymem(p, "false", sizeof("false") - 1);
  93. }
  94. static uint8_t *write_pair_impl(uint8_t *p, const uint8_t *name, size_t namelen,
  95. const ngtcp2_vec *value) {
  96. p = write_string_impl(p, name, namelen);
  97. *p++ = ':';
  98. return write_string_impl(p, value->base, value->len);
  99. }
  100. #define write_pair(DEST, NAME, VALUE) \
  101. write_pair_impl((DEST), (const uint8_t *)(NAME), sizeof(NAME) - 1, (VALUE))
  102. static uint8_t *write_pair_hex_impl(uint8_t *p, const uint8_t *name,
  103. size_t namelen, const uint8_t *value,
  104. size_t valuelen) {
  105. p = write_string_impl(p, name, namelen);
  106. *p++ = ':';
  107. return write_hex(p, value, valuelen);
  108. }
  109. #define write_pair_hex(DEST, NAME, VALUE, VALUELEN) \
  110. write_pair_hex_impl((DEST), (const uint8_t *)(NAME), sizeof(NAME) - 1, \
  111. (VALUE), (VALUELEN))
  112. static uint8_t *write_pair_number_impl(uint8_t *p, const uint8_t *name,
  113. size_t namelen, uint64_t value) {
  114. p = write_string_impl(p, name, namelen);
  115. *p++ = ':';
  116. return write_number(p, value);
  117. }
  118. #define write_pair_number(DEST, NAME, VALUE) \
  119. write_pair_number_impl((DEST), (const uint8_t *)(NAME), sizeof(NAME) - 1, \
  120. (VALUE))
  121. static uint8_t *write_pair_duration_impl(uint8_t *p, const uint8_t *name,
  122. size_t namelen,
  123. ngtcp2_duration duration) {
  124. p = write_string_impl(p, name, namelen);
  125. *p++ = ':';
  126. return write_duration(p, duration);
  127. }
  128. #define write_pair_duration(DEST, NAME, VALUE) \
  129. write_pair_duration_impl((DEST), (const uint8_t *)(NAME), sizeof(NAME) - 1, \
  130. (VALUE))
  131. static uint8_t *write_pair_tstamp_impl(uint8_t *p, const uint8_t *name,
  132. size_t namelen, ngtcp2_tstamp ts) {
  133. p = write_string_impl(p, name, namelen);
  134. *p++ = ':';
  135. return write_tstamp(p, ts);
  136. }
  137. #define write_pair_tstamp(DEST, NAME, VALUE) \
  138. write_pair_tstamp_impl((DEST), (const uint8_t *)(NAME), sizeof(NAME) - 1, \
  139. (VALUE))
  140. static uint8_t *write_pair_bool_impl(uint8_t *p, const uint8_t *name,
  141. size_t namelen, int b) {
  142. p = write_string_impl(p, name, namelen);
  143. *p++ = ':';
  144. return write_bool(p, b);
  145. }
  146. #define write_pair_bool(DEST, NAME, VALUE) \
  147. write_pair_bool_impl((DEST), (const uint8_t *)(NAME), sizeof(NAME) - 1, \
  148. (VALUE))
  149. static uint8_t *write_pair_cid_impl(uint8_t *p, const uint8_t *name,
  150. size_t namelen, const ngtcp2_cid *cid) {
  151. p = write_string_impl(p, name, namelen);
  152. *p++ = ':';
  153. return write_cid(p, cid);
  154. }
  155. #define write_pair_cid(DEST, NAME, VALUE) \
  156. write_pair_cid_impl((DEST), (const uint8_t *)(NAME), sizeof(NAME) - 1, \
  157. (VALUE))
  158. #define ngtcp2_make_vec_lit(S) {(uint8_t *)(S), sizeof((S)) - 1}
  159. static uint8_t *write_common_fields(uint8_t *p, const ngtcp2_cid *odcid) {
  160. p = write_verbatim(
  161. p, "\"common_fields\":{\"protocol_type\":[\"QUIC\"],\"time_format\":"
  162. "\"relative\",\"reference_time\":0,\"group_id\":");
  163. p = write_cid(p, odcid);
  164. *p++ = '}';
  165. return p;
  166. }
  167. static uint8_t *write_trace(uint8_t *p, int server, const ngtcp2_cid *odcid) {
  168. p = write_verbatim(
  169. p, "\"trace\":{\"vantage_point\":{\"name\":\"ngtcp2\",\"type\":");
  170. if (server) {
  171. p = write_string(p, "server");
  172. } else {
  173. p = write_string(p, "client");
  174. }
  175. p = write_verbatim(p, "},");
  176. p = write_common_fields(p, odcid);
  177. *p++ = '}';
  178. return p;
  179. }
  180. void ngtcp2_qlog_start(ngtcp2_qlog *qlog, const ngtcp2_cid *odcid, int server) {
  181. uint8_t buf[1024];
  182. uint8_t *p = buf;
  183. if (!qlog->write) {
  184. return;
  185. }
  186. p = write_verbatim(
  187. p, "\x1e{\"qlog_format\":\"JSON-SEQ\",\"qlog_version\":\"0.3\",");
  188. p = write_trace(p, server, odcid);
  189. p = write_verbatim(p, "}\n");
  190. qlog->write(qlog->user_data, NGTCP2_QLOG_WRITE_FLAG_NONE, buf,
  191. (size_t)(p - buf));
  192. }
  193. void ngtcp2_qlog_end(ngtcp2_qlog *qlog) {
  194. uint8_t buf[1] = {0};
  195. if (!qlog->write) {
  196. return;
  197. }
  198. qlog->write(qlog->user_data, NGTCP2_QLOG_WRITE_FLAG_FIN, &buf, 0);
  199. }
  200. static ngtcp2_vec vec_pkt_type_initial = ngtcp2_make_vec_lit("initial");
  201. static ngtcp2_vec vec_pkt_type_handshake = ngtcp2_make_vec_lit("handshake");
  202. static ngtcp2_vec vec_pkt_type_0rtt = ngtcp2_make_vec_lit("0RTT");
  203. static ngtcp2_vec vec_pkt_type_1rtt = ngtcp2_make_vec_lit("1RTT");
  204. static ngtcp2_vec vec_pkt_type_retry = ngtcp2_make_vec_lit("retry");
  205. static ngtcp2_vec vec_pkt_type_version_negotiation =
  206. ngtcp2_make_vec_lit("version_negotiation");
  207. static ngtcp2_vec vec_pkt_type_stateless_reset =
  208. ngtcp2_make_vec_lit("stateless_reset");
  209. static ngtcp2_vec vec_pkt_type_unknown = ngtcp2_make_vec_lit("unknown");
  210. static const ngtcp2_vec *qlog_pkt_type(const ngtcp2_pkt_hd *hd) {
  211. if (hd->flags & NGTCP2_PKT_FLAG_LONG_FORM) {
  212. switch (hd->type) {
  213. case NGTCP2_PKT_INITIAL:
  214. return &vec_pkt_type_initial;
  215. case NGTCP2_PKT_HANDSHAKE:
  216. return &vec_pkt_type_handshake;
  217. case NGTCP2_PKT_0RTT:
  218. return &vec_pkt_type_0rtt;
  219. case NGTCP2_PKT_RETRY:
  220. return &vec_pkt_type_retry;
  221. default:
  222. return &vec_pkt_type_unknown;
  223. }
  224. }
  225. switch (hd->type) {
  226. case NGTCP2_PKT_VERSION_NEGOTIATION:
  227. return &vec_pkt_type_version_negotiation;
  228. case NGTCP2_PKT_STATELESS_RESET:
  229. return &vec_pkt_type_stateless_reset;
  230. case NGTCP2_PKT_1RTT:
  231. return &vec_pkt_type_1rtt;
  232. default:
  233. return &vec_pkt_type_unknown;
  234. }
  235. }
  236. static uint8_t *write_pkt_hd(uint8_t *p, const ngtcp2_pkt_hd *hd) {
  237. /*
  238. * {"packet_type":"version_negotiation","packet_number":"0000000000000000000","token":{"data":""}}
  239. */
  240. #define NGTCP2_QLOG_PKT_HD_OVERHEAD 95
  241. *p++ = '{';
  242. p = write_pair(p, "packet_type", qlog_pkt_type(hd));
  243. *p++ = ',';
  244. p = write_pair_number(p, "packet_number", (uint64_t)hd->pkt_num);
  245. if (hd->type == NGTCP2_PKT_INITIAL && hd->tokenlen) {
  246. p = write_verbatim(p, ",\"token\":{");
  247. p = write_pair_hex(p, "data", hd->token, hd->tokenlen);
  248. *p++ = '}';
  249. }
  250. /* TODO Write DCIL and DCID */
  251. /* TODO Write SCIL and SCID */
  252. *p++ = '}';
  253. return p;
  254. }
  255. static uint8_t *write_padding_frame(uint8_t *p, const ngtcp2_padding *fr) {
  256. (void)fr;
  257. /* {"frame_type":"padding"} */
  258. #define NGTCP2_QLOG_PADDING_FRAME_OVERHEAD 24
  259. return write_verbatim(p, "{\"frame_type\":\"padding\"}");
  260. }
  261. static uint8_t *write_ping_frame(uint8_t *p, const ngtcp2_ping *fr) {
  262. (void)fr;
  263. /* {"frame_type":"ping"} */
  264. #define NGTCP2_QLOG_PING_FRAME_OVERHEAD 21
  265. return write_verbatim(p, "{\"frame_type\":\"ping\"}");
  266. }
  267. static uint8_t *write_ack_frame(uint8_t *p, const ngtcp2_ack *fr) {
  268. int64_t largest_ack, min_ack;
  269. size_t i;
  270. const ngtcp2_ack_range *range;
  271. /*
  272. * {"frame_type":"ack","ack_delay":0000000000000000000,"acked_ranges":[]}
  273. *
  274. * each range:
  275. * [0000000000000000000,0000000000000000000],
  276. *
  277. * ecn:
  278. * ,"ect1":0000000000000000000,"ect0":0000000000000000000,"ce":0000000000000000000
  279. */
  280. #define NGTCP2_QLOG_ACK_FRAME_BASE_OVERHEAD 70
  281. #define NGTCP2_QLOG_ACK_FRAME_RANGE_OVERHEAD 42
  282. #define NGTCP2_QLOG_ACK_FRAME_ECN_OVERHEAD 79
  283. p = write_verbatim(p, "{\"frame_type\":\"ack\",");
  284. p = write_pair_duration(p, "ack_delay", fr->ack_delay_unscaled);
  285. p = write_verbatim(p, ",\"acked_ranges\":[");
  286. largest_ack = fr->largest_ack;
  287. min_ack = fr->largest_ack - (int64_t)fr->first_ack_range;
  288. *p++ = '[';
  289. p = write_number(p, (uint64_t)min_ack);
  290. if (largest_ack != min_ack) {
  291. *p++ = ',';
  292. p = write_number(p, (uint64_t)largest_ack);
  293. }
  294. *p++ = ']';
  295. for (i = 0; i < fr->rangecnt; ++i) {
  296. range = &fr->ranges[i];
  297. largest_ack = min_ack - (int64_t)range->gap - 2;
  298. min_ack = largest_ack - (int64_t)range->len;
  299. *p++ = ',';
  300. *p++ = '[';
  301. p = write_number(p, (uint64_t)min_ack);
  302. if (largest_ack != min_ack) {
  303. *p++ = ',';
  304. p = write_number(p, (uint64_t)largest_ack);
  305. }
  306. *p++ = ']';
  307. }
  308. *p++ = ']';
  309. if (fr->type == NGTCP2_FRAME_ACK_ECN) {
  310. *p++ = ',';
  311. p = write_pair_number(p, "ect1", fr->ecn.ect1);
  312. *p++ = ',';
  313. p = write_pair_number(p, "ect0", fr->ecn.ect0);
  314. *p++ = ',';
  315. p = write_pair_number(p, "ce", fr->ecn.ce);
  316. }
  317. *p++ = '}';
  318. return p;
  319. }
  320. static uint8_t *write_reset_stream_frame(uint8_t *p,
  321. const ngtcp2_reset_stream *fr) {
  322. /*
  323. * {"frame_type":"reset_stream","stream_id":0000000000000000000,"error_code":0000000000000000000,"final_size":0000000000000000000}
  324. */
  325. #define NGTCP2_QLOG_RESET_STREAM_FRAME_OVERHEAD 127
  326. p = write_verbatim(p, "{\"frame_type\":\"reset_stream\",");
  327. p = write_pair_number(p, "stream_id", (uint64_t)fr->stream_id);
  328. *p++ = ',';
  329. p = write_pair_number(p, "error_code", fr->app_error_code);
  330. *p++ = ',';
  331. p = write_pair_number(p, "final_size", fr->final_size);
  332. *p++ = '}';
  333. return p;
  334. }
  335. static uint8_t *write_stop_sending_frame(uint8_t *p,
  336. const ngtcp2_stop_sending *fr) {
  337. /*
  338. * {"frame_type":"stop_sending","stream_id":0000000000000000000,"error_code":0000000000000000000}
  339. */
  340. #define NGTCP2_QLOG_STOP_SENDING_FRAME_OVERHEAD 94
  341. p = write_verbatim(p, "{\"frame_type\":\"stop_sending\",");
  342. p = write_pair_number(p, "stream_id", (uint64_t)fr->stream_id);
  343. *p++ = ',';
  344. p = write_pair_number(p, "error_code", fr->app_error_code);
  345. *p++ = '}';
  346. return p;
  347. }
  348. static uint8_t *write_crypto_frame(uint8_t *p, const ngtcp2_stream *fr) {
  349. /*
  350. * {"frame_type":"crypto","offset":0000000000000000000,"length":0000000000000000000}
  351. */
  352. #define NGTCP2_QLOG_CRYPTO_FRAME_OVERHEAD 81
  353. p = write_verbatim(p, "{\"frame_type\":\"crypto\",");
  354. p = write_pair_number(p, "offset", fr->offset);
  355. *p++ = ',';
  356. p = write_pair_number(p, "length", ngtcp2_vec_len(fr->data, fr->datacnt));
  357. *p++ = '}';
  358. return p;
  359. }
  360. static uint8_t *write_new_token_frame(uint8_t *p, const ngtcp2_new_token *fr) {
  361. /*
  362. * {"frame_type":"new_token","length":0000000000000000000,"token":{"data":""}}
  363. */
  364. #define NGTCP2_QLOG_NEW_TOKEN_FRAME_OVERHEAD 75
  365. p = write_verbatim(p, "{\"frame_type\":\"new_token\",");
  366. p = write_pair_number(p, "length", fr->tokenlen);
  367. p = write_verbatim(p, ",\"token\":{");
  368. p = write_pair_hex(p, "data", fr->token, fr->tokenlen);
  369. *p++ = '}';
  370. *p++ = '}';
  371. return p;
  372. }
  373. static uint8_t *write_stream_frame(uint8_t *p, const ngtcp2_stream *fr) {
  374. /*
  375. * {"frame_type":"stream","stream_id":0000000000000000000,"offset":0000000000000000000,"length":0000000000000000000,"fin":true}
  376. */
  377. #define NGTCP2_QLOG_STREAM_FRAME_OVERHEAD 124
  378. p = write_verbatim(p, "{\"frame_type\":\"stream\",");
  379. p = write_pair_number(p, "stream_id", (uint64_t)fr->stream_id);
  380. *p++ = ',';
  381. p = write_pair_number(p, "offset", fr->offset);
  382. *p++ = ',';
  383. p = write_pair_number(p, "length", ngtcp2_vec_len(fr->data, fr->datacnt));
  384. if (fr->fin) {
  385. *p++ = ',';
  386. p = write_pair_bool(p, "fin", 1);
  387. }
  388. *p++ = '}';
  389. return p;
  390. }
  391. static uint8_t *write_max_data_frame(uint8_t *p, const ngtcp2_max_data *fr) {
  392. /*
  393. * {"frame_type":"max_data","maximum":0000000000000000000}
  394. */
  395. #define NGTCP2_QLOG_MAX_DATA_FRAME_OVERHEAD 55
  396. p = write_verbatim(p, "{\"frame_type\":\"max_data\",");
  397. p = write_pair_number(p, "maximum", fr->max_data);
  398. *p++ = '}';
  399. return p;
  400. }
  401. static uint8_t *write_max_stream_data_frame(uint8_t *p,
  402. const ngtcp2_max_stream_data *fr) {
  403. /*
  404. * {"frame_type":"max_stream_data","stream_id":0000000000000000000,"maximum":0000000000000000000}
  405. */
  406. #define NGTCP2_QLOG_MAX_STREAM_DATA_FRAME_OVERHEAD 94
  407. p = write_verbatim(p, "{\"frame_type\":\"max_stream_data\",");
  408. p = write_pair_number(p, "stream_id", (uint64_t)fr->stream_id);
  409. *p++ = ',';
  410. p = write_pair_number(p, "maximum", fr->max_stream_data);
  411. *p++ = '}';
  412. return p;
  413. }
  414. static uint8_t *write_max_streams_frame(uint8_t *p,
  415. const ngtcp2_max_streams *fr) {
  416. /*
  417. * {"frame_type":"max_streams","stream_type":"unidirectional","maximum":0000000000000000000}
  418. */
  419. #define NGTCP2_QLOG_MAX_STREAMS_FRAME_OVERHEAD 89
  420. p = write_verbatim(p, "{\"frame_type\":\"max_streams\",\"stream_type\":");
  421. if (fr->type == NGTCP2_FRAME_MAX_STREAMS_BIDI) {
  422. p = write_string(p, "bidirectional");
  423. } else {
  424. p = write_string(p, "unidirectional");
  425. }
  426. *p++ = ',';
  427. p = write_pair_number(p, "maximum", fr->max_streams);
  428. *p++ = '}';
  429. return p;
  430. }
  431. static uint8_t *write_data_blocked_frame(uint8_t *p,
  432. const ngtcp2_data_blocked *fr) {
  433. /*
  434. * {"frame_type":"data_blocked","limit":0000000000000000000}
  435. */
  436. #define NGTCP2_QLOG_DATA_BLOCKED_FRAME_OVERHEAD 57
  437. p = write_verbatim(p, "{\"frame_type\":\"data_blocked\",");
  438. p = write_pair_number(p, "limit", fr->offset);
  439. *p++ = '}';
  440. return p;
  441. }
  442. static uint8_t *
  443. write_stream_data_blocked_frame(uint8_t *p,
  444. const ngtcp2_stream_data_blocked *fr) {
  445. /*
  446. * {"frame_type":"stream_data_blocked","stream_id":0000000000000000000,"limit":0000000000000000000}
  447. */
  448. #define NGTCP2_QLOG_STREAM_DATA_BLOCKED_FRAME_OVERHEAD 96
  449. p = write_verbatim(p, "{\"frame_type\":\"stream_data_blocked\",");
  450. p = write_pair_number(p, "stream_id", (uint64_t)fr->stream_id);
  451. *p++ = ',';
  452. p = write_pair_number(p, "limit", fr->offset);
  453. *p++ = '}';
  454. return p;
  455. }
  456. static uint8_t *write_streams_blocked_frame(uint8_t *p,
  457. const ngtcp2_streams_blocked *fr) {
  458. /*
  459. * {"frame_type":"streams_blocked","stream_type":"unidirectional","limit":0000000000000000000}
  460. */
  461. #define NGTCP2_QLOG_STREAMS_BLOCKED_FRAME_OVERHEAD 91
  462. p = write_verbatim(p, "{\"frame_type\":\"streams_blocked\",\"stream_type\":");
  463. if (fr->type == NGTCP2_FRAME_STREAMS_BLOCKED_BIDI) {
  464. p = write_string(p, "bidirectional");
  465. } else {
  466. p = write_string(p, "unidirectional");
  467. }
  468. *p++ = ',';
  469. p = write_pair_number(p, "limit", fr->max_streams);
  470. *p++ = '}';
  471. return p;
  472. }
  473. static uint8_t *
  474. write_new_connection_id_frame(uint8_t *p, const ngtcp2_new_connection_id *fr) {
  475. /*
  476. * {"frame_type":"new_connection_id","sequence_number":0000000000000000000,"retire_prior_to":0000000000000000000,"connection_id_length":0000000000000000000,"connection_id":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","stateless_reset_token":{"data":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}}
  477. */
  478. #define NGTCP2_QLOG_NEW_CONNECTION_ID_FRAME_OVERHEAD 280
  479. p = write_verbatim(p, "{\"frame_type\":\"new_connection_id\",");
  480. p = write_pair_number(p, "sequence_number", fr->seq);
  481. *p++ = ',';
  482. p = write_pair_number(p, "retire_prior_to", fr->retire_prior_to);
  483. *p++ = ',';
  484. p = write_pair_number(p, "connection_id_length", fr->cid.datalen);
  485. *p++ = ',';
  486. p = write_pair_cid(p, "connection_id", &fr->cid);
  487. p = write_verbatim(p, ",\"stateless_reset_token\":{");
  488. p = write_pair_hex(p, "data", fr->stateless_reset_token,
  489. sizeof(fr->stateless_reset_token));
  490. *p++ = '}';
  491. *p++ = '}';
  492. return p;
  493. }
  494. static uint8_t *
  495. write_retire_connection_id_frame(uint8_t *p,
  496. const ngtcp2_retire_connection_id *fr) {
  497. /*
  498. * {"frame_type":"retire_connection_id","sequence_number":0000000000000000000}
  499. */
  500. #define NGTCP2_QLOG_RETIRE_CONNECTION_ID_FRAME_OVERHEAD 75
  501. p = write_verbatim(p, "{\"frame_type\":\"retire_connection_id\",");
  502. p = write_pair_number(p, "sequence_number", fr->seq);
  503. *p++ = '}';
  504. return p;
  505. }
  506. static uint8_t *write_path_challenge_frame(uint8_t *p,
  507. const ngtcp2_path_challenge *fr) {
  508. /*
  509. * {"frame_type":"path_challenge","data":"xxxxxxxxxxxxxxxx"}
  510. */
  511. #define NGTCP2_QLOG_PATH_CHALLENGE_FRAME_OVERHEAD 57
  512. p = write_verbatim(p, "{\"frame_type\":\"path_challenge\",");
  513. p = write_pair_hex(p, "data", fr->data, sizeof(fr->data));
  514. *p++ = '}';
  515. return p;
  516. }
  517. static uint8_t *write_path_response_frame(uint8_t *p,
  518. const ngtcp2_path_response *fr) {
  519. /*
  520. * {"frame_type":"path_response","data":"xxxxxxxxxxxxxxxx"}
  521. */
  522. #define NGTCP2_QLOG_PATH_RESPONSE_FRAME_OVERHEAD 56
  523. p = write_verbatim(p, "{\"frame_type\":\"path_response\",");
  524. p = write_pair_hex(p, "data", fr->data, sizeof(fr->data));
  525. *p++ = '}';
  526. return p;
  527. }
  528. static uint8_t *
  529. write_connection_close_frame(uint8_t *p, const ngtcp2_connection_close *fr) {
  530. /*
  531. * {"frame_type":"connection_close","error_space":"application","error_code":0000000000000000000,"raw_error_code":0000000000000000000}
  532. */
  533. #define NGTCP2_QLOG_CONNECTION_CLOSE_FRAME_OVERHEAD 131
  534. p =
  535. write_verbatim(p, "{\"frame_type\":\"connection_close\",\"error_space\":");
  536. if (fr->type == NGTCP2_FRAME_CONNECTION_CLOSE) {
  537. p = write_string(p, "transport");
  538. } else {
  539. p = write_string(p, "application");
  540. }
  541. *p++ = ',';
  542. p = write_pair_number(p, "error_code", fr->error_code);
  543. *p++ = ',';
  544. p = write_pair_number(p, "raw_error_code", fr->error_code);
  545. /* TODO Write reason by escaping non-printables */
  546. /* TODO Write trigger_frame_type */
  547. *p++ = '}';
  548. return p;
  549. }
  550. static uint8_t *write_handshake_done_frame(uint8_t *p,
  551. const ngtcp2_handshake_done *fr) {
  552. (void)fr;
  553. /*
  554. * {"frame_type":"handshake_done"}
  555. */
  556. #define NGTCP2_QLOG_HANDSHAKE_DONE_FRAME_OVERHEAD 31
  557. return write_verbatim(p, "{\"frame_type\":\"handshake_done\"}");
  558. }
  559. static uint8_t *write_datagram_frame(uint8_t *p, const ngtcp2_datagram *fr) {
  560. /*
  561. * {"frame_type":"datagram","length":0000000000000000000}
  562. */
  563. #define NGTCP2_QLOG_DATAGRAM_FRAME_OVERHEAD 54
  564. p = write_verbatim(p, "{\"frame_type\":\"datagram\",");
  565. p = write_pair_number(p, "length", ngtcp2_vec_len(fr->data, fr->datacnt));
  566. *p++ = '}';
  567. return p;
  568. }
  569. static uint8_t *qlog_write_time(ngtcp2_qlog *qlog, uint8_t *p) {
  570. return write_pair_tstamp(p, "time", qlog->last_ts - qlog->ts);
  571. }
  572. static void qlog_pkt_write_start(ngtcp2_qlog *qlog, int sent) {
  573. uint8_t *p;
  574. if (!qlog->write) {
  575. return;
  576. }
  577. ngtcp2_buf_reset(&qlog->buf);
  578. p = qlog->buf.last;
  579. *p++ = '\x1e';
  580. *p++ = '{';
  581. p = qlog_write_time(qlog, p);
  582. p = write_verbatim(p, ",\"name\":");
  583. if (sent) {
  584. p = write_string(p, "transport:packet_sent");
  585. } else {
  586. p = write_string(p, "transport:packet_received");
  587. }
  588. p = write_verbatim(p, ",\"data\":{\"frames\":[");
  589. qlog->buf.last = p;
  590. }
  591. static void qlog_pkt_write_end(ngtcp2_qlog *qlog, const ngtcp2_pkt_hd *hd,
  592. size_t pktlen) {
  593. uint8_t *p = qlog->buf.last;
  594. if (!qlog->write) {
  595. return;
  596. }
  597. /*
  598. * ],"header":,"raw":{"length":0000000000000000000}}}
  599. *
  600. * plus, terminating LF
  601. */
  602. #define NGTCP2_QLOG_PKT_WRITE_END_OVERHEAD \
  603. (1 + 50 + NGTCP2_QLOG_PKT_HD_OVERHEAD)
  604. if (ngtcp2_buf_left(&qlog->buf) <
  605. NGTCP2_QLOG_PKT_WRITE_END_OVERHEAD + hd->tokenlen * 2) {
  606. return;
  607. }
  608. assert(ngtcp2_buf_len(&qlog->buf));
  609. /* Eat last ',' */
  610. if (*(p - 1) == ',') {
  611. --p;
  612. }
  613. p = write_verbatim(p, "],\"header\":");
  614. p = write_pkt_hd(p, hd);
  615. p = write_verbatim(p, ",\"raw\":{\"length\":");
  616. p = write_number(p, pktlen);
  617. p = write_verbatim(p, "}}}\n");
  618. qlog->buf.last = p;
  619. qlog->write(qlog->user_data, NGTCP2_QLOG_WRITE_FLAG_NONE, qlog->buf.pos,
  620. ngtcp2_buf_len(&qlog->buf));
  621. }
  622. void ngtcp2_qlog_write_frame(ngtcp2_qlog *qlog, const ngtcp2_frame *fr) {
  623. uint8_t *p = qlog->buf.last;
  624. if (!qlog->write) {
  625. return;
  626. }
  627. switch (fr->type) {
  628. case NGTCP2_FRAME_PADDING:
  629. if (ngtcp2_buf_left(&qlog->buf) < NGTCP2_QLOG_PADDING_FRAME_OVERHEAD + 1) {
  630. return;
  631. }
  632. p = write_padding_frame(p, &fr->padding);
  633. break;
  634. case NGTCP2_FRAME_PING:
  635. if (ngtcp2_buf_left(&qlog->buf) < NGTCP2_QLOG_PING_FRAME_OVERHEAD + 1) {
  636. return;
  637. }
  638. p = write_ping_frame(p, &fr->ping);
  639. break;
  640. case NGTCP2_FRAME_ACK:
  641. case NGTCP2_FRAME_ACK_ECN:
  642. if (ngtcp2_buf_left(&qlog->buf) <
  643. NGTCP2_QLOG_ACK_FRAME_BASE_OVERHEAD +
  644. (size_t)(fr->type == NGTCP2_FRAME_ACK_ECN
  645. ? NGTCP2_QLOG_ACK_FRAME_ECN_OVERHEAD
  646. : 0) +
  647. NGTCP2_QLOG_ACK_FRAME_RANGE_OVERHEAD * (1 + fr->ack.rangecnt) + 1) {
  648. return;
  649. }
  650. p = write_ack_frame(p, &fr->ack);
  651. break;
  652. case NGTCP2_FRAME_RESET_STREAM:
  653. if (ngtcp2_buf_left(&qlog->buf) <
  654. NGTCP2_QLOG_RESET_STREAM_FRAME_OVERHEAD + 1) {
  655. return;
  656. }
  657. p = write_reset_stream_frame(p, &fr->reset_stream);
  658. break;
  659. case NGTCP2_FRAME_STOP_SENDING:
  660. if (ngtcp2_buf_left(&qlog->buf) <
  661. NGTCP2_QLOG_STOP_SENDING_FRAME_OVERHEAD + 1) {
  662. return;
  663. }
  664. p = write_stop_sending_frame(p, &fr->stop_sending);
  665. break;
  666. case NGTCP2_FRAME_CRYPTO:
  667. if (ngtcp2_buf_left(&qlog->buf) < NGTCP2_QLOG_CRYPTO_FRAME_OVERHEAD + 1) {
  668. return;
  669. }
  670. p = write_crypto_frame(p, &fr->stream);
  671. break;
  672. case NGTCP2_FRAME_NEW_TOKEN:
  673. if (ngtcp2_buf_left(&qlog->buf) <
  674. NGTCP2_QLOG_NEW_TOKEN_FRAME_OVERHEAD + fr->new_token.tokenlen * 2 + 1) {
  675. return;
  676. }
  677. p = write_new_token_frame(p, &fr->new_token);
  678. break;
  679. case NGTCP2_FRAME_STREAM:
  680. if (ngtcp2_buf_left(&qlog->buf) < NGTCP2_QLOG_STREAM_FRAME_OVERHEAD + 1) {
  681. return;
  682. }
  683. p = write_stream_frame(p, &fr->stream);
  684. break;
  685. case NGTCP2_FRAME_MAX_DATA:
  686. if (ngtcp2_buf_left(&qlog->buf) < NGTCP2_QLOG_MAX_DATA_FRAME_OVERHEAD + 1) {
  687. return;
  688. }
  689. p = write_max_data_frame(p, &fr->max_data);
  690. break;
  691. case NGTCP2_FRAME_MAX_STREAM_DATA:
  692. if (ngtcp2_buf_left(&qlog->buf) <
  693. NGTCP2_QLOG_MAX_STREAM_DATA_FRAME_OVERHEAD + 1) {
  694. return;
  695. }
  696. p = write_max_stream_data_frame(p, &fr->max_stream_data);
  697. break;
  698. case NGTCP2_FRAME_MAX_STREAMS_BIDI:
  699. case NGTCP2_FRAME_MAX_STREAMS_UNI:
  700. if (ngtcp2_buf_left(&qlog->buf) <
  701. NGTCP2_QLOG_MAX_STREAMS_FRAME_OVERHEAD + 1) {
  702. return;
  703. }
  704. p = write_max_streams_frame(p, &fr->max_streams);
  705. break;
  706. case NGTCP2_FRAME_DATA_BLOCKED:
  707. if (ngtcp2_buf_left(&qlog->buf) <
  708. NGTCP2_QLOG_DATA_BLOCKED_FRAME_OVERHEAD + 1) {
  709. return;
  710. }
  711. p = write_data_blocked_frame(p, &fr->data_blocked);
  712. break;
  713. case NGTCP2_FRAME_STREAM_DATA_BLOCKED:
  714. if (ngtcp2_buf_left(&qlog->buf) <
  715. NGTCP2_QLOG_STREAM_DATA_BLOCKED_FRAME_OVERHEAD + 1) {
  716. return;
  717. }
  718. p = write_stream_data_blocked_frame(p, &fr->stream_data_blocked);
  719. break;
  720. case NGTCP2_FRAME_STREAMS_BLOCKED_BIDI:
  721. case NGTCP2_FRAME_STREAMS_BLOCKED_UNI:
  722. if (ngtcp2_buf_left(&qlog->buf) <
  723. NGTCP2_QLOG_STREAMS_BLOCKED_FRAME_OVERHEAD + 1) {
  724. return;
  725. }
  726. p = write_streams_blocked_frame(p, &fr->streams_blocked);
  727. break;
  728. case NGTCP2_FRAME_NEW_CONNECTION_ID:
  729. if (ngtcp2_buf_left(&qlog->buf) <
  730. NGTCP2_QLOG_NEW_CONNECTION_ID_FRAME_OVERHEAD + 1) {
  731. return;
  732. }
  733. p = write_new_connection_id_frame(p, &fr->new_connection_id);
  734. break;
  735. case NGTCP2_FRAME_RETIRE_CONNECTION_ID:
  736. if (ngtcp2_buf_left(&qlog->buf) <
  737. NGTCP2_QLOG_RETIRE_CONNECTION_ID_FRAME_OVERHEAD + 1) {
  738. return;
  739. }
  740. p = write_retire_connection_id_frame(p, &fr->retire_connection_id);
  741. break;
  742. case NGTCP2_FRAME_PATH_CHALLENGE:
  743. if (ngtcp2_buf_left(&qlog->buf) <
  744. NGTCP2_QLOG_PATH_CHALLENGE_FRAME_OVERHEAD + 1) {
  745. return;
  746. }
  747. p = write_path_challenge_frame(p, &fr->path_challenge);
  748. break;
  749. case NGTCP2_FRAME_PATH_RESPONSE:
  750. if (ngtcp2_buf_left(&qlog->buf) <
  751. NGTCP2_QLOG_PATH_RESPONSE_FRAME_OVERHEAD + 1) {
  752. return;
  753. }
  754. p = write_path_response_frame(p, &fr->path_response);
  755. break;
  756. case NGTCP2_FRAME_CONNECTION_CLOSE:
  757. case NGTCP2_FRAME_CONNECTION_CLOSE_APP:
  758. if (ngtcp2_buf_left(&qlog->buf) <
  759. NGTCP2_QLOG_CONNECTION_CLOSE_FRAME_OVERHEAD + 1) {
  760. return;
  761. }
  762. p = write_connection_close_frame(p, &fr->connection_close);
  763. break;
  764. case NGTCP2_FRAME_HANDSHAKE_DONE:
  765. if (ngtcp2_buf_left(&qlog->buf) <
  766. NGTCP2_QLOG_HANDSHAKE_DONE_FRAME_OVERHEAD + 1) {
  767. return;
  768. }
  769. p = write_handshake_done_frame(p, &fr->handshake_done);
  770. break;
  771. case NGTCP2_FRAME_DATAGRAM:
  772. case NGTCP2_FRAME_DATAGRAM_LEN:
  773. if (ngtcp2_buf_left(&qlog->buf) < NGTCP2_QLOG_DATAGRAM_FRAME_OVERHEAD + 1) {
  774. return;
  775. }
  776. p = write_datagram_frame(p, &fr->datagram);
  777. break;
  778. default:
  779. ngtcp2_unreachable();
  780. }
  781. *p++ = ',';
  782. qlog->buf.last = p;
  783. }
  784. void ngtcp2_qlog_pkt_received_start(ngtcp2_qlog *qlog) {
  785. qlog_pkt_write_start(qlog, /* sent = */ 0);
  786. }
  787. void ngtcp2_qlog_pkt_received_end(ngtcp2_qlog *qlog, const ngtcp2_pkt_hd *hd,
  788. size_t pktlen) {
  789. qlog_pkt_write_end(qlog, hd, pktlen);
  790. }
  791. void ngtcp2_qlog_pkt_sent_start(ngtcp2_qlog *qlog) {
  792. qlog_pkt_write_start(qlog, /* sent = */ 1);
  793. }
  794. void ngtcp2_qlog_pkt_sent_end(ngtcp2_qlog *qlog, const ngtcp2_pkt_hd *hd,
  795. size_t pktlen) {
  796. qlog_pkt_write_end(qlog, hd, pktlen);
  797. }
  798. void ngtcp2_qlog_parameters_set_transport_params(
  799. ngtcp2_qlog *qlog, const ngtcp2_transport_params *params, int server,
  800. ngtcp2_qlog_side side) {
  801. uint8_t buf[1024];
  802. uint8_t *p = buf;
  803. const ngtcp2_preferred_addr *paddr;
  804. const ngtcp2_sockaddr_in *sa_in;
  805. const ngtcp2_sockaddr_in6 *sa_in6;
  806. if (!qlog->write) {
  807. return;
  808. }
  809. *p++ = '\x1e';
  810. *p++ = '{';
  811. p = qlog_write_time(qlog, p);
  812. p = write_verbatim(
  813. p, ",\"name\":\"transport:parameters_set\",\"data\":{\"owner\":");
  814. if (side == NGTCP2_QLOG_SIDE_LOCAL) {
  815. p = write_string(p, "local");
  816. } else {
  817. p = write_string(p, "remote");
  818. }
  819. *p++ = ',';
  820. p = write_pair_cid(p, "initial_source_connection_id", &params->initial_scid);
  821. *p++ = ',';
  822. if (side == (server ? NGTCP2_QLOG_SIDE_LOCAL : NGTCP2_QLOG_SIDE_REMOTE)) {
  823. p = write_pair_cid(p, "original_destination_connection_id",
  824. &params->original_dcid);
  825. *p++ = ',';
  826. }
  827. if (params->retry_scid_present) {
  828. p = write_pair_cid(p, "retry_source_connection_id", &params->retry_scid);
  829. *p++ = ',';
  830. }
  831. if (params->stateless_reset_token_present) {
  832. p = write_verbatim(p, "\"stateless_reset_token\":{");
  833. p = write_pair_hex(p, "data", params->stateless_reset_token,
  834. sizeof(params->stateless_reset_token));
  835. *p++ = '}';
  836. *p++ = ',';
  837. }
  838. p = write_pair_bool(p, "disable_active_migration",
  839. params->disable_active_migration);
  840. *p++ = ',';
  841. p = write_pair_duration(p, "max_idle_timeout", params->max_idle_timeout);
  842. *p++ = ',';
  843. p =
  844. write_pair_number(p, "max_udp_payload_size", params->max_udp_payload_size);
  845. *p++ = ',';
  846. p = write_pair_number(p, "ack_delay_exponent", params->ack_delay_exponent);
  847. *p++ = ',';
  848. p = write_pair_duration(p, "max_ack_delay", params->max_ack_delay);
  849. *p++ = ',';
  850. p = write_pair_number(p, "active_connection_id_limit",
  851. params->active_connection_id_limit);
  852. *p++ = ',';
  853. p = write_pair_number(p, "initial_max_data", params->initial_max_data);
  854. *p++ = ',';
  855. p = write_pair_number(p, "initial_max_stream_data_bidi_local",
  856. params->initial_max_stream_data_bidi_local);
  857. *p++ = ',';
  858. p = write_pair_number(p, "initial_max_stream_data_bidi_remote",
  859. params->initial_max_stream_data_bidi_remote);
  860. *p++ = ',';
  861. p = write_pair_number(p, "initial_max_stream_data_uni",
  862. params->initial_max_stream_data_uni);
  863. *p++ = ',';
  864. p = write_pair_number(p, "initial_max_streams_bidi",
  865. params->initial_max_streams_bidi);
  866. *p++ = ',';
  867. p = write_pair_number(p, "initial_max_streams_uni",
  868. params->initial_max_streams_uni);
  869. if (params->preferred_addr_present) {
  870. *p++ = ',';
  871. paddr = &params->preferred_addr;
  872. p = write_string(p, "preferred_address");
  873. *p++ = ':';
  874. *p++ = '{';
  875. if (paddr->ipv4_present) {
  876. sa_in = &paddr->ipv4;
  877. p = write_pair_hex(p, "ip_v4", (const uint8_t *)&sa_in->sin_addr,
  878. sizeof(sa_in->sin_addr));
  879. *p++ = ',';
  880. p = write_pair_number(p, "port_v4", ngtcp2_ntohs(sa_in->sin_port));
  881. *p++ = ',';
  882. }
  883. if (paddr->ipv6_present) {
  884. sa_in6 = &paddr->ipv6;
  885. p = write_pair_hex(p, "ip_v6", (const uint8_t *)&sa_in6->sin6_addr,
  886. sizeof(sa_in6->sin6_addr));
  887. *p++ = ',';
  888. p = write_pair_number(p, "port_v6", ngtcp2_ntohs(sa_in6->sin6_port));
  889. *p++ = ',';
  890. }
  891. p = write_pair_cid(p, "connection_id", &paddr->cid);
  892. p = write_verbatim(p, ",\"stateless_reset_token\":{");
  893. p = write_pair_hex(p, "data", paddr->stateless_reset_token,
  894. sizeof(paddr->stateless_reset_token));
  895. *p++ = '}';
  896. *p++ = '}';
  897. }
  898. *p++ = ',';
  899. p = write_pair_number(p, "max_datagram_frame_size",
  900. params->max_datagram_frame_size);
  901. *p++ = ',';
  902. p = write_pair_bool(p, "grease_quic_bit", params->grease_quic_bit);
  903. p = write_verbatim(p, "}}\n");
  904. qlog->write(qlog->user_data, NGTCP2_QLOG_WRITE_FLAG_NONE, buf,
  905. (size_t)(p - buf));
  906. }
  907. void ngtcp2_qlog_metrics_updated(ngtcp2_qlog *qlog,
  908. const ngtcp2_conn_stat *cstat) {
  909. uint8_t buf[1024];
  910. uint8_t *p = buf;
  911. if (!qlog->write) {
  912. return;
  913. }
  914. *p++ = '\x1e';
  915. *p++ = '{';
  916. p = qlog_write_time(qlog, p);
  917. p = write_verbatim(p, ",\"name\":\"recovery:metrics_updated\",\"data\":{");
  918. if (cstat->min_rtt != UINT64_MAX) {
  919. p = write_pair_duration(p, "min_rtt", cstat->min_rtt);
  920. *p++ = ',';
  921. }
  922. p = write_pair_duration(p, "smoothed_rtt", cstat->smoothed_rtt);
  923. *p++ = ',';
  924. p = write_pair_duration(p, "latest_rtt", cstat->latest_rtt);
  925. *p++ = ',';
  926. p = write_pair_duration(p, "rtt_variance", cstat->rttvar);
  927. *p++ = ',';
  928. p = write_pair_number(p, "pto_count", cstat->pto_count);
  929. *p++ = ',';
  930. p = write_pair_number(p, "congestion_window", cstat->cwnd);
  931. *p++ = ',';
  932. p = write_pair_number(p, "bytes_in_flight", cstat->bytes_in_flight);
  933. if (cstat->ssthresh != UINT64_MAX) {
  934. *p++ = ',';
  935. p = write_pair_number(p, "ssthresh", cstat->ssthresh);
  936. }
  937. p = write_verbatim(p, "}}\n");
  938. qlog->write(qlog->user_data, NGTCP2_QLOG_WRITE_FLAG_NONE, buf,
  939. (size_t)(p - buf));
  940. }
  941. void ngtcp2_qlog_pkt_lost(ngtcp2_qlog *qlog, ngtcp2_rtb_entry *ent) {
  942. uint8_t buf[256];
  943. uint8_t *p = buf;
  944. ngtcp2_pkt_hd hd = {0};
  945. if (!qlog->write) {
  946. return;
  947. }
  948. *p++ = '\x1e';
  949. *p++ = '{';
  950. p = qlog_write_time(qlog, p);
  951. p = write_verbatim(
  952. p, ",\"name\":\"recovery:packet_lost\",\"data\":{\"header\":");
  953. hd.type = ent->hd.type;
  954. hd.flags = ent->hd.flags;
  955. hd.pkt_num = ent->hd.pkt_num;
  956. p = write_pkt_hd(p, &hd);
  957. p = write_verbatim(p, "}}\n");
  958. qlog->write(qlog->user_data, NGTCP2_QLOG_WRITE_FLAG_NONE, buf,
  959. (size_t)(p - buf));
  960. }
  961. void ngtcp2_qlog_retry_pkt_received(ngtcp2_qlog *qlog, const ngtcp2_pkt_hd *hd,
  962. const ngtcp2_pkt_retry *retry) {
  963. uint8_t rawbuf[1024];
  964. ngtcp2_buf buf;
  965. if (!qlog->write) {
  966. return;
  967. }
  968. ngtcp2_buf_init(&buf, rawbuf, sizeof(rawbuf));
  969. *buf.last++ = '\x1e';
  970. *buf.last++ = '{';
  971. buf.last = qlog_write_time(qlog, buf.last);
  972. buf.last = write_verbatim(
  973. buf.last, ",\"name\":\"transport:packet_received\",\"data\":{\"header\":");
  974. if (ngtcp2_buf_left(&buf) < NGTCP2_QLOG_PKT_HD_OVERHEAD + hd->tokenlen * 2 +
  975. sizeof(",\"retry_token\":{\"data\":\"\"}}}\n") -
  976. 1 + retry->tokenlen * 2) {
  977. return;
  978. }
  979. buf.last = write_pkt_hd(buf.last, hd);
  980. buf.last = write_verbatim(buf.last, ",\"retry_token\":{");
  981. buf.last = write_pair_hex(buf.last, "data", retry->token, retry->tokenlen);
  982. buf.last = write_verbatim(buf.last, "}}}\n");
  983. qlog->write(qlog->user_data, NGTCP2_QLOG_WRITE_FLAG_NONE, buf.pos,
  984. ngtcp2_buf_len(&buf));
  985. }
  986. void ngtcp2_qlog_stateless_reset_pkt_received(
  987. ngtcp2_qlog *qlog, const ngtcp2_pkt_stateless_reset *sr) {
  988. uint8_t buf[256];
  989. uint8_t *p = buf;
  990. ngtcp2_pkt_hd hd = {0};
  991. if (!qlog->write) {
  992. return;
  993. }
  994. hd.type = NGTCP2_PKT_STATELESS_RESET;
  995. *p++ = '\x1e';
  996. *p++ = '{';
  997. p = qlog_write_time(qlog, p);
  998. p = write_verbatim(
  999. p, ",\"name\":\"transport:packet_received\",\"data\":{\"header\":");
  1000. p = write_pkt_hd(p, &hd);
  1001. *p++ = ',';
  1002. p = write_pair_hex(p, "stateless_reset_token", sr->stateless_reset_token,
  1003. NGTCP2_STATELESS_RESET_TOKENLEN);
  1004. p = write_verbatim(p, "}}\n");
  1005. qlog->write(qlog->user_data, NGTCP2_QLOG_WRITE_FLAG_NONE, buf,
  1006. (size_t)(p - buf));
  1007. }
  1008. void ngtcp2_qlog_version_negotiation_pkt_received(ngtcp2_qlog *qlog,
  1009. const ngtcp2_pkt_hd *hd,
  1010. const uint32_t *sv,
  1011. size_t nsv) {
  1012. uint8_t rawbuf[512];
  1013. ngtcp2_buf buf;
  1014. size_t i;
  1015. uint32_t v;
  1016. if (!qlog->write) {
  1017. return;
  1018. }
  1019. ngtcp2_buf_init(&buf, rawbuf, sizeof(rawbuf));
  1020. *buf.last++ = '\x1e';
  1021. *buf.last++ = '{';
  1022. buf.last = qlog_write_time(qlog, buf.last);
  1023. buf.last = write_verbatim(
  1024. buf.last, ",\"name\":\"transport:packet_received\",\"data\":{\"header\":");
  1025. buf.last = write_pkt_hd(buf.last, hd);
  1026. buf.last = write_verbatim(buf.last, ",\"supported_versions\":[");
  1027. if (nsv) {
  1028. if (ngtcp2_buf_left(&buf) <
  1029. (sizeof("\"xxxxxxxx\",") - 1) * nsv - 1 + sizeof("]}}\n") - 1) {
  1030. return;
  1031. }
  1032. v = ngtcp2_htonl(sv[0]);
  1033. buf.last = write_hex(buf.last, (const uint8_t *)&v, sizeof(v));
  1034. for (i = 1; i < nsv; ++i) {
  1035. *buf.last++ = ',';
  1036. v = ngtcp2_htonl(sv[i]);
  1037. buf.last = write_hex(buf.last, (const uint8_t *)&v, sizeof(v));
  1038. }
  1039. }
  1040. buf.last = write_verbatim(buf.last, "]}}\n");
  1041. qlog->write(qlog->user_data, NGTCP2_QLOG_WRITE_FLAG_NONE, buf.pos,
  1042. ngtcp2_buf_len(&buf));
  1043. }