ngtcp2_rst.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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_rst.h"
  26. #include <assert.h>
  27. #include "ngtcp2_rtb.h"
  28. #include "ngtcp2_cc.h"
  29. #include "ngtcp2_macro.h"
  30. #include "ngtcp2_conn_stat.h"
  31. void ngtcp2_rs_init(ngtcp2_rs *rs) {
  32. rs->interval = UINT64_MAX;
  33. rs->delivered = 0;
  34. rs->prior_delivered = 0;
  35. rs->prior_ts = UINT64_MAX;
  36. rs->tx_in_flight = 0;
  37. rs->lost = 0;
  38. rs->prior_lost = 0;
  39. rs->send_elapsed = 0;
  40. rs->ack_elapsed = 0;
  41. rs->last_end_seq = -1;
  42. rs->is_app_limited = 0;
  43. }
  44. void ngtcp2_rst_init(ngtcp2_rst *rst) {
  45. ngtcp2_rs_init(&rst->rs);
  46. rst->delivered = 0;
  47. rst->delivered_ts = 0;
  48. rst->first_sent_ts = 0;
  49. rst->app_limited = 0;
  50. rst->is_cwnd_limited = 0;
  51. rst->lost = 0;
  52. rst->last_seq = -1;
  53. }
  54. void ngtcp2_rst_on_pkt_sent(ngtcp2_rst *rst, ngtcp2_rtb_entry *ent,
  55. const ngtcp2_conn_stat *cstat) {
  56. if (cstat->bytes_in_flight == 0) {
  57. rst->first_sent_ts = rst->delivered_ts = ent->ts;
  58. }
  59. ent->rst.first_sent_ts = rst->first_sent_ts;
  60. ent->rst.delivered_ts = rst->delivered_ts;
  61. ent->rst.delivered = rst->delivered;
  62. ent->rst.is_app_limited = rst->app_limited != 0;
  63. ent->rst.tx_in_flight = cstat->bytes_in_flight + ent->pktlen;
  64. ent->rst.lost = rst->lost;
  65. ent->rst.end_seq = ++rst->last_seq;
  66. }
  67. void ngtcp2_rst_on_ack_recv(ngtcp2_rst *rst, ngtcp2_conn_stat *cstat) {
  68. ngtcp2_rs *rs = &rst->rs;
  69. if (rst->app_limited && rst->delivered > rst->app_limited) {
  70. rst->app_limited = 0;
  71. }
  72. if (rs->prior_ts == UINT64_MAX) {
  73. return;
  74. }
  75. rs->interval = ngtcp2_max_uint64(rs->send_elapsed, rs->ack_elapsed);
  76. rs->delivered = rst->delivered - rs->prior_delivered;
  77. rs->lost = rst->lost - rs->prior_lost;
  78. if (rs->interval < cstat->min_rtt) {
  79. rs->interval = UINT64_MAX;
  80. return;
  81. }
  82. if (!rs->interval) {
  83. return;
  84. }
  85. cstat->delivery_rate_sec = rs->delivered * NGTCP2_SECONDS / rs->interval;
  86. }
  87. static int rst_is_newest_pkt(const ngtcp2_rst *rst, const ngtcp2_rtb_entry *ent,
  88. const ngtcp2_rs *rs) {
  89. return ent->ts > rst->first_sent_ts ||
  90. (ent->ts == rst->first_sent_ts && ent->rst.end_seq > rs->last_end_seq);
  91. }
  92. void ngtcp2_rst_update_rate_sample(ngtcp2_rst *rst, const ngtcp2_rtb_entry *ent,
  93. ngtcp2_tstamp ts) {
  94. ngtcp2_rs *rs = &rst->rs;
  95. rst->delivered += ent->pktlen;
  96. rst->delivered_ts = ts;
  97. if (rs->prior_ts == UINT64_MAX || rst_is_newest_pkt(rst, ent, rs)) {
  98. rs->prior_delivered = ent->rst.delivered;
  99. rs->prior_ts = ent->rst.delivered_ts;
  100. rs->is_app_limited = ent->rst.is_app_limited;
  101. rs->send_elapsed = ent->ts - ent->rst.first_sent_ts;
  102. rs->ack_elapsed = rst->delivered_ts - ent->rst.delivered_ts;
  103. rs->tx_in_flight = ent->rst.tx_in_flight;
  104. rs->prior_lost = ent->rst.lost;
  105. rs->last_end_seq = ent->rst.end_seq;
  106. rst->first_sent_ts = ent->ts;
  107. }
  108. }
  109. void ngtcp2_rst_update_app_limited(ngtcp2_rst *rst, ngtcp2_conn_stat *cstat) {
  110. (void)rst;
  111. (void)cstat;
  112. /* TODO Not implemented */
  113. }