ring_buf.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #ifndef OSSL_INTERNAL_RING_BUF_H
  10. # define OSSL_INTERNAL_RING_BUF_H
  11. # pragma once
  12. # include <openssl/e_os2.h> /* For 'ossl_inline' */
  13. # include "internal/safe_math.h"
  14. /*
  15. * ==================================================================
  16. * Byte-wise ring buffer which supports pushing and popping blocks of multiple
  17. * bytes at a time. The logical offset of each byte for the purposes of a QUIC
  18. * stream is tracked. Bytes can be popped from the ring buffer in two stages;
  19. * first they are popped, and then they are culled. Bytes which have been popped
  20. * but not yet culled will not be overwritten, and can be restored.
  21. */
  22. struct ring_buf {
  23. void *start;
  24. size_t alloc; /* size of buffer allocation in bytes */
  25. /*
  26. * Logical offset of the head (where we append to). This is the current size
  27. * of the QUIC stream. This increases monotonically.
  28. */
  29. uint64_t head_offset;
  30. /*
  31. * Logical offset of the cull tail. Data is no longer needed and is
  32. * deallocated as the cull tail advances, which occurs as data is
  33. * acknowledged. This increases monotonically.
  34. */
  35. uint64_t ctail_offset;
  36. };
  37. OSSL_SAFE_MATH_UNSIGNED(u64, uint64_t)
  38. #define MAX_OFFSET (((uint64_t)1) << 62) /* QUIC-imposed limit */
  39. static ossl_inline int ring_buf_init(struct ring_buf *r)
  40. {
  41. r->start = NULL;
  42. r->alloc = 0;
  43. r->head_offset = r->ctail_offset = 0;
  44. return 1;
  45. }
  46. static ossl_inline void ring_buf_destroy(struct ring_buf *r, int cleanse)
  47. {
  48. if (cleanse)
  49. OPENSSL_clear_free(r->start, r->alloc);
  50. else
  51. OPENSSL_free(r->start);
  52. r->start = NULL;
  53. r->alloc = 0;
  54. }
  55. static ossl_inline size_t ring_buf_used(struct ring_buf *r)
  56. {
  57. return (size_t)(r->head_offset - r->ctail_offset);
  58. }
  59. static ossl_inline size_t ring_buf_avail(struct ring_buf *r)
  60. {
  61. return r->alloc - ring_buf_used(r);
  62. }
  63. static ossl_inline int ring_buf_write_at(struct ring_buf *r,
  64. uint64_t logical_offset,
  65. const unsigned char *buf,
  66. size_t buf_len)
  67. {
  68. size_t avail, idx, l;
  69. unsigned char *start = r->start;
  70. int i, err = 0;
  71. avail = ring_buf_avail(r);
  72. if (logical_offset < r->ctail_offset
  73. || safe_add_u64(logical_offset, buf_len, &err)
  74. > safe_add_u64(r->head_offset, avail, &err)
  75. || safe_add_u64(r->head_offset, buf_len, &err)
  76. > MAX_OFFSET
  77. || err)
  78. return 0;
  79. for (i = 0; buf_len > 0 && i < 2; ++i) {
  80. idx = logical_offset % r->alloc;
  81. l = r->alloc - idx;
  82. if (buf_len < l)
  83. l = buf_len;
  84. memcpy(start + idx, buf, l);
  85. if (r->head_offset < logical_offset + l)
  86. r->head_offset = logical_offset + l;
  87. logical_offset += l;
  88. buf += l;
  89. buf_len -= l;
  90. }
  91. assert(buf_len == 0);
  92. return 1;
  93. }
  94. static ossl_inline size_t ring_buf_push(struct ring_buf *r,
  95. const unsigned char *buf,
  96. size_t buf_len)
  97. {
  98. size_t pushed = 0, avail, idx, l;
  99. unsigned char *start = r->start;
  100. for (;;) {
  101. avail = ring_buf_avail(r);
  102. if (buf_len > avail)
  103. buf_len = avail;
  104. if (buf_len > MAX_OFFSET - r->head_offset)
  105. buf_len = (size_t)(MAX_OFFSET - r->head_offset);
  106. if (buf_len == 0)
  107. break;
  108. idx = r->head_offset % r->alloc;
  109. l = r->alloc - idx;
  110. if (buf_len < l)
  111. l = buf_len;
  112. memcpy(start + idx, buf, l);
  113. r->head_offset += l;
  114. buf += l;
  115. buf_len -= l;
  116. pushed += l;
  117. }
  118. return pushed;
  119. }
  120. static ossl_inline const unsigned char *ring_buf_get_ptr(const struct ring_buf *r,
  121. uint64_t logical_offset,
  122. size_t *max_len)
  123. {
  124. unsigned char *start = r->start;
  125. size_t idx;
  126. if (logical_offset >= r->head_offset || logical_offset < r->ctail_offset)
  127. return NULL;
  128. idx = logical_offset % r->alloc;
  129. *max_len = r->alloc - idx;
  130. return start + idx;
  131. }
  132. /*
  133. * Retrieves data out of the read side of the ring buffer starting at the given
  134. * logical offset. *buf is set to point to a contiguous span of bytes and
  135. * *buf_len is set to the number of contiguous bytes. After this function
  136. * returns, there may or may not be more bytes available at the logical offset
  137. * of (logical_offset + *buf_len) by calling this function again. If the logical
  138. * offset is out of the range retained by the ring buffer, returns 0, else
  139. * returns 1. A logical offset at the end of the range retained by the ring
  140. * buffer is not considered an error and is returned with a *buf_len of 0.
  141. *
  142. * The ring buffer state is not changed.
  143. */
  144. static ossl_inline int ring_buf_get_buf_at(const struct ring_buf *r,
  145. uint64_t logical_offset,
  146. const unsigned char **buf,
  147. size_t *buf_len)
  148. {
  149. const unsigned char *start = r->start;
  150. size_t idx, l;
  151. if (logical_offset > r->head_offset || logical_offset < r->ctail_offset)
  152. return 0;
  153. if (r->alloc == 0) {
  154. *buf = NULL;
  155. *buf_len = 0;
  156. return 1;
  157. }
  158. idx = logical_offset % r->alloc;
  159. l = (size_t)(r->head_offset - logical_offset);
  160. if (l > r->alloc - idx)
  161. l = r->alloc - idx;
  162. *buf = start + idx;
  163. *buf_len = l;
  164. return 1;
  165. }
  166. static ossl_inline void ring_buf_cpop_range(struct ring_buf *r,
  167. uint64_t start, uint64_t end,
  168. int cleanse)
  169. {
  170. assert(end >= start);
  171. if (start > r->ctail_offset || end >= MAX_OFFSET)
  172. return;
  173. if (cleanse && r->alloc > 0 && end > r->ctail_offset) {
  174. size_t idx = r->ctail_offset % r->alloc;
  175. uint64_t cleanse_end = end + 1;
  176. size_t l;
  177. if (cleanse_end > r->head_offset)
  178. cleanse_end = r->head_offset;
  179. l = (size_t)(cleanse_end - r->ctail_offset);
  180. if (l > r->alloc - idx) {
  181. OPENSSL_cleanse((unsigned char *)r->start + idx, r->alloc - idx);
  182. l -= r->alloc - idx;
  183. idx = 0;
  184. }
  185. if (l > 0)
  186. OPENSSL_cleanse((unsigned char *)r->start + idx, l);
  187. }
  188. r->ctail_offset = end + 1;
  189. /* Allow culling unpushed data */
  190. if (r->head_offset < r->ctail_offset)
  191. r->head_offset = r->ctail_offset;
  192. }
  193. static ossl_inline int ring_buf_resize(struct ring_buf *r, size_t num_bytes,
  194. int cleanse)
  195. {
  196. struct ring_buf rnew = {0};
  197. const unsigned char *src = NULL;
  198. size_t src_len = 0, copied = 0;
  199. if (num_bytes == r->alloc)
  200. return 1;
  201. if (num_bytes < ring_buf_used(r))
  202. return 0;
  203. rnew.start = OPENSSL_malloc(num_bytes);
  204. if (rnew.start == NULL)
  205. return 0;
  206. rnew.alloc = num_bytes;
  207. rnew.head_offset = r->head_offset - ring_buf_used(r);
  208. rnew.ctail_offset = rnew.head_offset;
  209. for (;;) {
  210. if (!ring_buf_get_buf_at(r, r->ctail_offset + copied, &src, &src_len)) {
  211. OPENSSL_free(rnew.start);
  212. return 0;
  213. }
  214. if (src_len == 0)
  215. break;
  216. if (ring_buf_push(&rnew, src, src_len) != src_len) {
  217. OPENSSL_free(rnew.start);
  218. return 0;
  219. }
  220. copied += src_len;
  221. }
  222. assert(rnew.head_offset == r->head_offset);
  223. rnew.ctail_offset = r->ctail_offset;
  224. ring_buf_destroy(r, cleanse);
  225. memcpy(r, &rnew, sizeof(*r));
  226. return 1;
  227. }
  228. #endif /* OSSL_INTERNAL_RING_BUF_H */