ngtcp2_frame_chain.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * ngtcp2
  3. *
  4. * Copyright (c) 2023 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_FRAME_CHAIN_H
  26. #define NGTCP2_FRAME_CHAIN_H
  27. #ifdef HAVE_CONFIG_H
  28. # include <config.h>
  29. #endif /* defined(HAVE_CONFIG_H) */
  30. #include <ngtcp2/ngtcp2.h>
  31. #include "ngtcp2_pkt.h"
  32. #include "ngtcp2_objalloc.h"
  33. /* NGTCP2_FRAME_CHAIN_BINDER_FLAG_NONE indicates that no flag is
  34. set. */
  35. #define NGTCP2_FRAME_CHAIN_BINDER_FLAG_NONE 0x00u
  36. /* NGTCP2_FRAME_CHAIN_BINDER_FLAG_ACK indicates that an information
  37. which a frame carries has been acknowledged. */
  38. #define NGTCP2_FRAME_CHAIN_BINDER_FLAG_ACK 0x01u
  39. /*
  40. * ngtcp2_frame_chain_binder binds 2 or more of ngtcp2_frame_chain to
  41. * share the acknowledgement state. In general, all
  42. * ngtcp2_frame_chains bound to the same binder must have the same
  43. * information.
  44. */
  45. typedef struct ngtcp2_frame_chain_binder {
  46. size_t refcount;
  47. /* flags is bitwise OR of zero or more of
  48. NGTCP2_FRAME_CHAIN_BINDER_FLAG_*. */
  49. uint32_t flags;
  50. } ngtcp2_frame_chain_binder;
  51. int ngtcp2_frame_chain_binder_new(ngtcp2_frame_chain_binder **pbinder,
  52. const ngtcp2_mem *mem);
  53. typedef struct ngtcp2_frame_chain ngtcp2_frame_chain;
  54. /*
  55. * ngtcp2_frame_chain chains frames in a single packet.
  56. */
  57. struct ngtcp2_frame_chain {
  58. union {
  59. struct {
  60. ngtcp2_frame_chain *next;
  61. ngtcp2_frame_chain_binder *binder;
  62. ngtcp2_frame fr;
  63. };
  64. ngtcp2_opl_entry oplent;
  65. };
  66. };
  67. ngtcp2_objalloc_decl(frame_chain, ngtcp2_frame_chain, oplent)
  68. /*
  69. * ngtcp2_bind_frame_chains binds two frame chains |a| and |b| using
  70. * new or existing ngtcp2_frame_chain_binder. |a| might have non-NULL
  71. * a->binder. |b| must not have non-NULL b->binder.
  72. *
  73. * This function returns 0 if it succeeds, or one of the following
  74. * negative error codes:
  75. *
  76. * NGTCP2_ERR_NOMEM
  77. * Out of memory
  78. */
  79. int ngtcp2_bind_frame_chains(ngtcp2_frame_chain *a, ngtcp2_frame_chain *b,
  80. const ngtcp2_mem *mem);
  81. /* NGTCP2_MAX_STREAM_DATACNT is the maximum number of ngtcp2_vec that
  82. a ngtcp2_stream can include. */
  83. #define NGTCP2_MAX_STREAM_DATACNT 256
  84. /*
  85. * ngtcp2_frame_chain_new allocates ngtcp2_frame_chain object and
  86. * assigns its pointer to |*pfrc|.
  87. *
  88. * This function returns 0 if it succeeds, or one of the following
  89. * negative error codes:
  90. *
  91. * NGTCP2_ERR_NOMEM
  92. * Out of memory.
  93. */
  94. int ngtcp2_frame_chain_new(ngtcp2_frame_chain **pfrc, const ngtcp2_mem *mem);
  95. /*
  96. * ngtcp2_frame_chain_objalloc_new behaves like
  97. * ngtcp2_frame_chain_new, but it uses |objalloc| to allocate the object.
  98. */
  99. int ngtcp2_frame_chain_objalloc_new(ngtcp2_frame_chain **pfrc,
  100. ngtcp2_objalloc *objalloc);
  101. /*
  102. * ngtcp2_frame_chain_extralen_new works like ngtcp2_frame_chain_new,
  103. * but it allocates extra memory |extralen| in order to extend
  104. * ngtcp2_frame.
  105. */
  106. int ngtcp2_frame_chain_extralen_new(ngtcp2_frame_chain **pfrc, size_t extralen,
  107. const ngtcp2_mem *mem);
  108. /* NGTCP2_FRAME_CHAIN_STREAM_AVAIL is the number of additional bytes
  109. available after ngtcp2_stream when it is embedded in
  110. ngtcp2_frame. */
  111. #define NGTCP2_FRAME_CHAIN_STREAM_AVAIL \
  112. (sizeof(ngtcp2_frame) - sizeof(ngtcp2_stream))
  113. /* NGTCP2_FRAME_CHAIN_STREAM_DATACNT_THRES is the number of datacnt
  114. that changes allocation method. If datacnt is more than this
  115. value, ngtcp2_frame_chain is allocated without ngtcp2_objalloc.
  116. Otherwise, it is allocated using ngtcp2_objalloc. */
  117. #define NGTCP2_FRAME_CHAIN_STREAM_DATACNT_THRES \
  118. (NGTCP2_FRAME_CHAIN_STREAM_AVAIL / sizeof(ngtcp2_vec) + 1)
  119. /*
  120. * ngtcp2_frame_chain_stream_datacnt_objalloc_new works like
  121. * ngtcp2_frame_chain_new, but it allocates enough data to store
  122. * additional |datacnt| - 1 ngtcp2_vec object after ngtcp2_stream
  123. * object. If no additional space is required, in other words,
  124. * |datacnt| <= NGTCP2_FRAME_CHAIN_STREAM_DATACNT_THRES,
  125. * ngtcp2_frame_chain_objalloc_new is called internally. Otherwise,
  126. * ngtcp2_frame_chain_extralen_new is used and objalloc is not used.
  127. * Therefore, it is important to call ngtcp2_frame_chain_objalloc_del
  128. * without changing datacnt field.
  129. */
  130. int ngtcp2_frame_chain_stream_datacnt_objalloc_new(ngtcp2_frame_chain **pfrc,
  131. size_t datacnt,
  132. ngtcp2_objalloc *objalloc,
  133. const ngtcp2_mem *mem);
  134. int ngtcp2_frame_chain_new_token_objalloc_new(ngtcp2_frame_chain **pfrc,
  135. const uint8_t *token,
  136. size_t tokenlen,
  137. ngtcp2_objalloc *objalloc,
  138. const ngtcp2_mem *mem);
  139. /*
  140. * ngtcp2_frame_chain_del deallocates |frc|. It also deallocates the
  141. * memory pointed by |frc|.
  142. */
  143. void ngtcp2_frame_chain_del(ngtcp2_frame_chain *frc, const ngtcp2_mem *mem);
  144. /*
  145. * ngtcp2_frame_chain_objalloc_del adds |frc| to |objalloc| for reuse.
  146. * It might just delete |frc| depending on the frame type and the size
  147. * of |frc|.
  148. */
  149. void ngtcp2_frame_chain_objalloc_del(ngtcp2_frame_chain *frc,
  150. ngtcp2_objalloc *objalloc,
  151. const ngtcp2_mem *mem);
  152. /*
  153. * ngtcp2_frame_chain_init initializes |frc|.
  154. */
  155. void ngtcp2_frame_chain_init(ngtcp2_frame_chain *frc);
  156. /*
  157. * ngtcp2_frame_chain_list_objalloc_del adds all ngtcp2_frame_chain
  158. * linked from |frc| to |objalloc| for reuse. Depending on the frame type
  159. * and its size, ngtcp2_frame_chain might be deleted instead.
  160. */
  161. void ngtcp2_frame_chain_list_objalloc_del(ngtcp2_frame_chain *frc,
  162. ngtcp2_objalloc *objalloc,
  163. const ngtcp2_mem *mem);
  164. #endif /* !defined(NGTCP2_FRAME_CHAIN_H) */