ngtcp2_pmtud.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * ngtcp2
  3. *
  4. * Copyright (c) 2022 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_pmtud.h"
  26. #include <assert.h>
  27. #include "ngtcp2_mem.h"
  28. #include "ngtcp2_macro.h"
  29. /* NGTCP2_PMTUD_PROBE_NUM_MAX is the maximum number of packets sent
  30. for each probe. */
  31. #define NGTCP2_PMTUD_PROBE_NUM_MAX 3
  32. static uint16_t pmtud_default_probes[] = {
  33. 1454 - 48, /* The well known MTU used by a domestic optic fiber
  34. service in Japan. */
  35. 1390 - 48, /* Typical Tunneled MTU */
  36. 1280 - 48, /* IPv6 minimum MTU */
  37. 1492 - 48, /* PPPoE */
  38. };
  39. int ngtcp2_pmtud_new(ngtcp2_pmtud **ppmtud, size_t max_udp_payload_size,
  40. size_t hard_max_udp_payload_size, int64_t tx_pkt_num,
  41. const uint16_t *probes, size_t probeslen,
  42. const ngtcp2_mem *mem) {
  43. ngtcp2_pmtud *pmtud = ngtcp2_mem_malloc(mem, sizeof(ngtcp2_pmtud));
  44. if (pmtud == NULL) {
  45. return NGTCP2_ERR_NOMEM;
  46. }
  47. pmtud->mem = mem;
  48. pmtud->mtu_idx = 0;
  49. pmtud->num_pkts_sent = 0;
  50. pmtud->expiry = UINT64_MAX;
  51. pmtud->tx_pkt_num = tx_pkt_num;
  52. pmtud->max_udp_payload_size = max_udp_payload_size;
  53. pmtud->hard_max_udp_payload_size = hard_max_udp_payload_size;
  54. pmtud->min_fail_udp_payload_size = SIZE_MAX;
  55. if (probeslen) {
  56. pmtud->probes = probes;
  57. pmtud->probeslen = probeslen;
  58. } else {
  59. pmtud->probes = pmtud_default_probes;
  60. pmtud->probeslen = ngtcp2_arraylen(pmtud_default_probes);
  61. }
  62. for (; pmtud->mtu_idx < pmtud->probeslen; ++pmtud->mtu_idx) {
  63. if (pmtud->probes[pmtud->mtu_idx] > pmtud->hard_max_udp_payload_size) {
  64. continue;
  65. }
  66. if (pmtud->probes[pmtud->mtu_idx] > pmtud->max_udp_payload_size) {
  67. break;
  68. }
  69. }
  70. *ppmtud = pmtud;
  71. return 0;
  72. }
  73. void ngtcp2_pmtud_del(ngtcp2_pmtud *pmtud) {
  74. if (!pmtud) {
  75. return;
  76. }
  77. ngtcp2_mem_free(pmtud->mem, pmtud);
  78. }
  79. size_t ngtcp2_pmtud_probelen(ngtcp2_pmtud *pmtud) {
  80. assert(pmtud->mtu_idx < pmtud->probeslen);
  81. return pmtud->probes[pmtud->mtu_idx];
  82. }
  83. void ngtcp2_pmtud_probe_sent(ngtcp2_pmtud *pmtud, ngtcp2_duration pto,
  84. ngtcp2_tstamp ts) {
  85. ngtcp2_tstamp timeout;
  86. if (++pmtud->num_pkts_sent < NGTCP2_PMTUD_PROBE_NUM_MAX) {
  87. timeout = pto;
  88. } else {
  89. timeout = 3 * pto;
  90. }
  91. pmtud->expiry = ts + timeout;
  92. }
  93. int ngtcp2_pmtud_require_probe(ngtcp2_pmtud *pmtud) {
  94. return pmtud->expiry == UINT64_MAX;
  95. }
  96. static void pmtud_next_probe(ngtcp2_pmtud *pmtud) {
  97. assert(pmtud->mtu_idx < pmtud->probeslen);
  98. ++pmtud->mtu_idx;
  99. pmtud->num_pkts_sent = 0;
  100. pmtud->expiry = UINT64_MAX;
  101. for (; pmtud->mtu_idx < pmtud->probeslen; ++pmtud->mtu_idx) {
  102. if (pmtud->probes[pmtud->mtu_idx] <= pmtud->max_udp_payload_size ||
  103. pmtud->probes[pmtud->mtu_idx] > pmtud->hard_max_udp_payload_size) {
  104. continue;
  105. }
  106. if (pmtud->probes[pmtud->mtu_idx] < pmtud->min_fail_udp_payload_size) {
  107. break;
  108. }
  109. }
  110. }
  111. void ngtcp2_pmtud_probe_success(ngtcp2_pmtud *pmtud, size_t payloadlen) {
  112. pmtud->max_udp_payload_size =
  113. ngtcp2_max_size(pmtud->max_udp_payload_size, payloadlen);
  114. assert(pmtud->mtu_idx < pmtud->probeslen);
  115. if (pmtud->probes[pmtud->mtu_idx] > pmtud->max_udp_payload_size) {
  116. return;
  117. }
  118. pmtud_next_probe(pmtud);
  119. }
  120. void ngtcp2_pmtud_handle_expiry(ngtcp2_pmtud *pmtud, ngtcp2_tstamp ts) {
  121. if (ts < pmtud->expiry) {
  122. return;
  123. }
  124. pmtud->expiry = UINT64_MAX;
  125. if (pmtud->num_pkts_sent < NGTCP2_PMTUD_PROBE_NUM_MAX) {
  126. return;
  127. }
  128. pmtud->min_fail_udp_payload_size = ngtcp2_min_size(
  129. pmtud->min_fail_udp_payload_size, pmtud->probes[pmtud->mtu_idx]);
  130. pmtud_next_probe(pmtud);
  131. }
  132. int ngtcp2_pmtud_finished(ngtcp2_pmtud *pmtud) {
  133. return pmtud->mtu_idx >= pmtud->probeslen;
  134. }