ngtcp2_pv.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #ifndef NGTCP2_PV_H
  26. #define NGTCP2_PV_H
  27. #ifdef HAVE_CONFIG_H
  28. # include <config.h>
  29. #endif /* defined(HAVE_CONFIG_H) */
  30. #include <ngtcp2/ngtcp2.h>
  31. #include "ngtcp2_cid.h"
  32. #include "ngtcp2_ringbuf.h"
  33. /* NGTCP2_PV_MAX_ENTRIES is the maximum number of entries that
  34. ngtcp2_pv can contain. It must be power of 2. */
  35. #define NGTCP2_PV_MAX_ENTRIES 8
  36. /* NGTCP2_PV_NUM_PROBE_PKT is the number of probe packets containing
  37. PATH_CHALLENGE sent at a time. */
  38. #define NGTCP2_PV_NUM_PROBE_PKT 2
  39. typedef struct ngtcp2_log ngtcp2_log;
  40. typedef struct ngtcp2_frame_chain ngtcp2_frame_chain;
  41. /* NGTCP2_PV_ENTRY_FLAG_NONE indicates that no flag is set. */
  42. #define NGTCP2_PV_ENTRY_FLAG_NONE 0x00u
  43. /* NGTCP2_PV_ENTRY_FLAG_UNDERSIZED indicates that UDP datagram which
  44. contains PATH_CHALLENGE is undersized (< 1200 bytes) */
  45. #define NGTCP2_PV_ENTRY_FLAG_UNDERSIZED 0x01u
  46. typedef struct ngtcp2_pv_entry {
  47. /* expiry is the timestamp when this PATH_CHALLENGE expires. */
  48. ngtcp2_tstamp expiry;
  49. /* flags is zero or more of NGTCP2_PV_ENTRY_FLAG_*. */
  50. uint8_t flags;
  51. /* data is a byte string included in PATH_CHALLENGE. */
  52. uint8_t data[8];
  53. } ngtcp2_pv_entry;
  54. void ngtcp2_pv_entry_init(ngtcp2_pv_entry *pvent, const uint8_t *data,
  55. ngtcp2_tstamp expiry, uint8_t flags);
  56. /* NGTCP2_PV_FLAG_NONE indicates no flag is set. */
  57. #define NGTCP2_PV_FLAG_NONE 0x00u
  58. /* NGTCP2_PV_FLAG_DONT_CARE indicates that the outcome of path
  59. validation should be ignored entirely. */
  60. #define NGTCP2_PV_FLAG_DONT_CARE 0x01u
  61. /* NGTCP2_PV_FLAG_CANCEL_TIMER indicates that the expiry timer is
  62. cancelled. */
  63. #define NGTCP2_PV_FLAG_CANCEL_TIMER 0x02u
  64. /* NGTCP2_PV_FLAG_FALLBACK_ON_FAILURE indicates that fallback DCID is
  65. available in ngtcp2_pv. If path validation fails, fallback to the
  66. fallback DCID. If path validation succeeds, fallback DCID is
  67. retired if it does not equal to the current DCID. */
  68. #define NGTCP2_PV_FLAG_FALLBACK_ON_FAILURE 0x04u
  69. /* NGTCP2_PV_FLAG_PREFERRED_ADDR indicates that client is migrating to
  70. server's preferred address. This flag is only used by client. */
  71. #define NGTCP2_PV_FLAG_PREFERRED_ADDR 0x10u
  72. typedef struct ngtcp2_pv ngtcp2_pv;
  73. ngtcp2_static_ringbuf_def(pv_ents, NGTCP2_PV_MAX_ENTRIES,
  74. sizeof(ngtcp2_pv_entry))
  75. /*
  76. * ngtcp2_pv is the context of a single path validation.
  77. */
  78. struct ngtcp2_pv {
  79. const ngtcp2_mem *mem;
  80. ngtcp2_log *log;
  81. /* dcid is DCID and path this path validation uses. */
  82. ngtcp2_dcid dcid;
  83. /* fallback_dcid is the usually validated DCID and used as a
  84. fallback if this path validation fails. */
  85. ngtcp2_dcid fallback_dcid;
  86. /* ents is the ring buffer of ngtcp2_pv_entry */
  87. ngtcp2_static_ringbuf_pv_ents ents;
  88. /* timeout is the duration within which this path validation should
  89. succeed. */
  90. ngtcp2_duration timeout;
  91. /* fallback_pto is PTO of fallback connection. */
  92. ngtcp2_duration fallback_pto;
  93. /* started_ts is the timestamp this path validation starts. */
  94. ngtcp2_tstamp started_ts;
  95. /* round is the number of times that probe_pkt_left is reset. */
  96. size_t round;
  97. /* probe_pkt_left is the number of probe packets containing
  98. PATH_CHALLENGE which can be send without waiting for an
  99. expiration of a previous flight. */
  100. size_t probe_pkt_left;
  101. /* flags is bitwise-OR of zero or more of NGTCP2_PV_FLAG_*. */
  102. uint8_t flags;
  103. };
  104. /*
  105. * ngtcp2_pv_new creates new ngtcp2_pv object and assigns its pointer
  106. * to |*ppv|. This function makes a copy of |dcid|. |timeout| is a
  107. * duration within which this path validation must succeed.
  108. *
  109. * This function returns 0 if it succeeds, or one of the following
  110. * negative error codes:
  111. *
  112. * NGTCP2_ERR_NOMEM
  113. * Out of memory
  114. */
  115. int ngtcp2_pv_new(ngtcp2_pv **ppv, const ngtcp2_dcid *dcid,
  116. ngtcp2_duration timeout, uint8_t flags, ngtcp2_log *log,
  117. const ngtcp2_mem *mem);
  118. /*
  119. * ngtcp2_pv_del deallocates |pv|. This function frees memory |pv|
  120. * points too.
  121. */
  122. void ngtcp2_pv_del(ngtcp2_pv *pv);
  123. /*
  124. * ngtcp2_pv_add_entry adds new entry with |data|. |expiry| is the
  125. * expiry time of the entry.
  126. */
  127. void ngtcp2_pv_add_entry(ngtcp2_pv *pv, const uint8_t *data,
  128. ngtcp2_tstamp expiry, uint8_t flags, ngtcp2_tstamp ts);
  129. /*
  130. * ngtcp2_pv_full returns nonzero if |pv| is full of ngtcp2_pv_entry.
  131. */
  132. int ngtcp2_pv_full(ngtcp2_pv *pv);
  133. /*
  134. * ngtcp2_pv_validate validates that the received |data| matches the
  135. * one of the existing entry. The flag of ngtcp2_pv_entry that
  136. * matches |data| is assigned to |*pflags| if this function succeeds.
  137. *
  138. * This function returns 0 if it succeeds, or one of the following
  139. * negative error codes:
  140. *
  141. * NGTCP2_ERR_PATH_VALIDATION_FAILED
  142. * path validation has failed and must be abandoned
  143. * NGTCP2_ERR_INVALID_STATE
  144. * |pv| includes no entry
  145. * NGTCP2_ERR_INVALID_ARGUMENT
  146. * |pv| does not have an entry which has |data| and |path|
  147. */
  148. int ngtcp2_pv_validate(ngtcp2_pv *pv, uint8_t *pflags, const uint8_t *data);
  149. /*
  150. * ngtcp2_pv_handle_entry_expiry checks expiry of existing entries.
  151. */
  152. void ngtcp2_pv_handle_entry_expiry(ngtcp2_pv *pv, ngtcp2_tstamp ts);
  153. /*
  154. * ngtcp2_pv_should_send_probe returns nonzero if new entry can be
  155. * added by ngtcp2_pv_add_entry.
  156. */
  157. int ngtcp2_pv_should_send_probe(ngtcp2_pv *pv);
  158. /*
  159. * ngtcp2_pv_validation_timed_out returns nonzero if the path
  160. * validation fails because of timeout.
  161. */
  162. int ngtcp2_pv_validation_timed_out(ngtcp2_pv *pv, ngtcp2_tstamp ts);
  163. /*
  164. * ngtcp2_pv_next_expiry returns the earliest expiry.
  165. */
  166. ngtcp2_tstamp ngtcp2_pv_next_expiry(ngtcp2_pv *pv);
  167. /*
  168. * ngtcp2_pv_cancel_expired_timer cancels the expired timer.
  169. */
  170. void ngtcp2_pv_cancel_expired_timer(ngtcp2_pv *pv, ngtcp2_tstamp ts);
  171. #endif /* !defined(NGTCP2_PV_H) */