ngtcp2_pv.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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_pv.h"
  26. #include <string.h>
  27. #include <assert.h>
  28. #include "ngtcp2_mem.h"
  29. #include "ngtcp2_log.h"
  30. #include "ngtcp2_macro.h"
  31. #include "ngtcp2_addr.h"
  32. void ngtcp2_pv_entry_init(ngtcp2_pv_entry *pvent, const uint8_t *data,
  33. ngtcp2_tstamp expiry, uint8_t flags) {
  34. memcpy(pvent->data, data, sizeof(pvent->data));
  35. pvent->expiry = expiry;
  36. pvent->flags = flags;
  37. }
  38. int ngtcp2_pv_new(ngtcp2_pv **ppv, const ngtcp2_dcid *dcid,
  39. ngtcp2_duration timeout, uint8_t flags, ngtcp2_log *log,
  40. const ngtcp2_mem *mem) {
  41. (*ppv) = ngtcp2_mem_malloc(mem, sizeof(ngtcp2_pv));
  42. if (*ppv == NULL) {
  43. return NGTCP2_ERR_NOMEM;
  44. }
  45. ngtcp2_static_ringbuf_pv_ents_init(&(*ppv)->ents);
  46. ngtcp2_dcid_copy(&(*ppv)->dcid, dcid);
  47. (*ppv)->mem = mem;
  48. (*ppv)->log = log;
  49. (*ppv)->timeout = timeout;
  50. (*ppv)->fallback_pto = 0;
  51. (*ppv)->started_ts = UINT64_MAX;
  52. (*ppv)->probe_pkt_left = NGTCP2_PV_NUM_PROBE_PKT;
  53. (*ppv)->round = 0;
  54. (*ppv)->flags = flags;
  55. return 0;
  56. }
  57. void ngtcp2_pv_del(ngtcp2_pv *pv) {
  58. if (pv == NULL) {
  59. return;
  60. }
  61. ngtcp2_mem_free(pv->mem, pv);
  62. }
  63. void ngtcp2_pv_add_entry(ngtcp2_pv *pv, const uint8_t *data,
  64. ngtcp2_tstamp expiry, uint8_t flags,
  65. ngtcp2_tstamp ts) {
  66. ngtcp2_pv_entry *ent;
  67. assert(pv->probe_pkt_left);
  68. if (ngtcp2_ringbuf_len(&pv->ents.rb) == 0) {
  69. pv->started_ts = ts;
  70. }
  71. ent = ngtcp2_ringbuf_push_back(&pv->ents.rb);
  72. ngtcp2_pv_entry_init(ent, data, expiry, flags);
  73. pv->flags &= (uint8_t)~NGTCP2_PV_FLAG_CANCEL_TIMER;
  74. --pv->probe_pkt_left;
  75. }
  76. int ngtcp2_pv_validate(ngtcp2_pv *pv, uint8_t *pflags, const uint8_t *data) {
  77. size_t len = ngtcp2_ringbuf_len(&pv->ents.rb);
  78. size_t i;
  79. ngtcp2_pv_entry *ent;
  80. if (len == 0) {
  81. return NGTCP2_ERR_INVALID_STATE;
  82. }
  83. for (i = 0; i < len; ++i) {
  84. ent = ngtcp2_ringbuf_get(&pv->ents.rb, i);
  85. if (memcmp(ent->data, data, sizeof(ent->data)) == 0) {
  86. *pflags = ent->flags;
  87. ngtcp2_log_info(pv->log, NGTCP2_LOG_EVENT_PTV, "path has been validated");
  88. return 0;
  89. }
  90. }
  91. return NGTCP2_ERR_INVALID_ARGUMENT;
  92. }
  93. void ngtcp2_pv_handle_entry_expiry(ngtcp2_pv *pv, ngtcp2_tstamp ts) {
  94. ngtcp2_pv_entry *ent;
  95. if (ngtcp2_ringbuf_len(&pv->ents.rb) == 0) {
  96. return;
  97. }
  98. ent = ngtcp2_ringbuf_get(&pv->ents.rb, ngtcp2_ringbuf_len(&pv->ents.rb) - 1);
  99. if (ent->expiry > ts) {
  100. return;
  101. }
  102. ++pv->round;
  103. pv->probe_pkt_left = NGTCP2_PV_NUM_PROBE_PKT;
  104. }
  105. int ngtcp2_pv_should_send_probe(ngtcp2_pv *pv) {
  106. return pv->probe_pkt_left > 0;
  107. }
  108. int ngtcp2_pv_validation_timed_out(ngtcp2_pv *pv, ngtcp2_tstamp ts) {
  109. ngtcp2_tstamp t;
  110. ngtcp2_pv_entry *ent;
  111. if (pv->started_ts == UINT64_MAX) {
  112. return 0;
  113. }
  114. assert(ngtcp2_ringbuf_len(&pv->ents.rb));
  115. ent = ngtcp2_ringbuf_get(&pv->ents.rb, ngtcp2_ringbuf_len(&pv->ents.rb) - 1);
  116. t = pv->started_ts + pv->timeout;
  117. t = ngtcp2_max_uint64(t, ent->expiry);
  118. return t <= ts;
  119. }
  120. ngtcp2_tstamp ngtcp2_pv_next_expiry(ngtcp2_pv *pv) {
  121. ngtcp2_pv_entry *ent;
  122. if ((pv->flags & NGTCP2_PV_FLAG_CANCEL_TIMER) ||
  123. ngtcp2_ringbuf_len(&pv->ents.rb) == 0) {
  124. return UINT64_MAX;
  125. }
  126. ent = ngtcp2_ringbuf_get(&pv->ents.rb, ngtcp2_ringbuf_len(&pv->ents.rb) - 1);
  127. return ent->expiry;
  128. }
  129. void ngtcp2_pv_cancel_expired_timer(ngtcp2_pv *pv, ngtcp2_tstamp ts) {
  130. ngtcp2_tstamp expiry = ngtcp2_pv_next_expiry(pv);
  131. if (expiry > ts) {
  132. return;
  133. }
  134. pv->flags |= NGTCP2_PV_FLAG_CANCEL_TIMER;
  135. }