quic_reactor.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright 2022-2024 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_QUIC_REACTOR_H
  10. # define OSSL_QUIC_REACTOR_H
  11. # include "internal/time.h"
  12. # include "internal/sockets.h"
  13. # include "internal/quic_predef.h"
  14. # include "internal/thread_arch.h"
  15. # include <openssl/bio.h>
  16. # ifndef OPENSSL_NO_QUIC
  17. /*
  18. * Core I/O Reactor Framework
  19. * ==========================
  20. *
  21. * Manages use of async network I/O which the QUIC stack is built on. The core
  22. * mechanic looks like this:
  23. *
  24. * - There is a pollable FD for both the read and write side respectively.
  25. * Readability and writeability of these FDs respectively determines when
  26. * network I/O is available.
  27. *
  28. * - The reactor can export these FDs to the user, as well as flags indicating
  29. * whether the user should listen for readability, writeability, or neither.
  30. *
  31. * - The reactor can export a timeout indication to the user, indicating when
  32. * the reactor should be called (via libssl APIs) regardless of whether
  33. * the network socket has become ready.
  34. *
  35. * The reactor is based around a tick callback which is essentially the mutator
  36. * function. The mutator attempts to do whatever it can, attempting to perform
  37. * network I/O to the extent currently feasible. When done, the mutator returns
  38. * information to the reactor indicating when it should be woken up again:
  39. *
  40. * - Should it be woken up when network RX is possible?
  41. * - Should it be woken up when network TX is possible?
  42. * - Should it be woken up no later than some deadline X?
  43. *
  44. * The intention is that ALL I/O-related SSL_* functions with side effects (e.g.
  45. * SSL_read/SSL_write) consist of three phases:
  46. *
  47. * - Optionally mutate the QUIC machine's state.
  48. * - Optionally tick the QUIC reactor.
  49. * - Optionally mutate the QUIC machine's state.
  50. *
  51. * For example, SSL_write is a mutation (appending to a stream buffer) followed
  52. * by an optional tick (generally expected as we may want to send the data
  53. * immediately, though not strictly needed if transmission is being deferred due
  54. * to Nagle's algorithm, etc.).
  55. *
  56. * SSL_read is also a mutation and in principle does not need to tick the
  57. * reactor, but it generally will anyway to ensure that the reactor is regularly
  58. * ticked by an application which is only reading and not writing.
  59. *
  60. * If the SSL object is being used in blocking mode, SSL_read may need to block
  61. * if no data is available yet, and SSL_write may need to block if buffers
  62. * are full.
  63. *
  64. * The internals of the QUIC I/O engine always use asynchronous I/O. If the
  65. * application desires blocking semantics, we handle this by adding a blocking
  66. * adaptation layer on top of our internal asynchronous I/O API as exposed by
  67. * the reactor interface.
  68. */
  69. struct quic_tick_result_st {
  70. char net_read_desired;
  71. char net_write_desired;
  72. OSSL_TIME tick_deadline;
  73. };
  74. static ossl_inline ossl_unused void
  75. ossl_quic_tick_result_merge_into(QUIC_TICK_RESULT *r,
  76. const QUIC_TICK_RESULT *src)
  77. {
  78. r->net_read_desired = r->net_read_desired || src->net_read_desired;
  79. r->net_write_desired = r->net_write_desired || src->net_write_desired;
  80. r->tick_deadline = ossl_time_min(r->tick_deadline, src->tick_deadline);
  81. }
  82. struct quic_reactor_st {
  83. /*
  84. * BIO poll descriptors which can be polled. poll_r is a poll descriptor
  85. * which becomes readable when the QUIC state machine can potentially do
  86. * work, and poll_w is a poll descriptor which becomes writable when the
  87. * QUIC state machine can potentially do work. Generally, either of these
  88. * conditions means that SSL_tick() should be called, or another SSL
  89. * function which implicitly calls SSL_tick() (e.g. SSL_read/SSL_write()).
  90. */
  91. BIO_POLL_DESCRIPTOR poll_r, poll_w;
  92. OSSL_TIME tick_deadline; /* ossl_time_infinite() if none currently applicable */
  93. void (*tick_cb)(QUIC_TICK_RESULT *res, void *arg, uint32_t flags);
  94. void *tick_cb_arg;
  95. /*
  96. * These are true if we would like to know when we can read or write from
  97. * the network respectively.
  98. */
  99. unsigned int net_read_desired : 1;
  100. unsigned int net_write_desired : 1;
  101. /*
  102. * Are the read and write poll descriptors we are currently configured with
  103. * things we can actually poll?
  104. */
  105. unsigned int can_poll_r : 1;
  106. unsigned int can_poll_w : 1;
  107. };
  108. void ossl_quic_reactor_init(QUIC_REACTOR *rtor,
  109. void (*tick_cb)(QUIC_TICK_RESULT *res, void *arg,
  110. uint32_t flags),
  111. void *tick_cb_arg,
  112. OSSL_TIME initial_tick_deadline);
  113. void ossl_quic_reactor_set_poll_r(QUIC_REACTOR *rtor,
  114. const BIO_POLL_DESCRIPTOR *r);
  115. void ossl_quic_reactor_set_poll_w(QUIC_REACTOR *rtor,
  116. const BIO_POLL_DESCRIPTOR *w);
  117. const BIO_POLL_DESCRIPTOR *ossl_quic_reactor_get_poll_r(const QUIC_REACTOR *rtor);
  118. const BIO_POLL_DESCRIPTOR *ossl_quic_reactor_get_poll_w(const QUIC_REACTOR *rtor);
  119. int ossl_quic_reactor_can_poll_r(const QUIC_REACTOR *rtor);
  120. int ossl_quic_reactor_can_poll_w(const QUIC_REACTOR *rtor);
  121. int ossl_quic_reactor_can_support_poll_descriptor(const QUIC_REACTOR *rtor,
  122. const BIO_POLL_DESCRIPTOR *d);
  123. int ossl_quic_reactor_net_read_desired(QUIC_REACTOR *rtor);
  124. int ossl_quic_reactor_net_write_desired(QUIC_REACTOR *rtor);
  125. OSSL_TIME ossl_quic_reactor_get_tick_deadline(QUIC_REACTOR *rtor);
  126. /*
  127. * Do whatever work can be done, and as much work as can be done. This involves
  128. * e.g. seeing if we can read anything from the network (if we want to), seeing
  129. * if we can write anything to the network (if we want to), etc.
  130. *
  131. * If the CHANNEL_ONLY flag is set, this indicates that we should only
  132. * touch state which is synchronised by the channel mutex.
  133. */
  134. #define QUIC_REACTOR_TICK_FLAG_CHANNEL_ONLY (1U << 0)
  135. int ossl_quic_reactor_tick(QUIC_REACTOR *rtor, uint32_t flags);
  136. /*
  137. * Blocking I/O Adaptation Layer
  138. * =============================
  139. *
  140. * The blocking I/O adaptation layer implements blocking I/O on top of our
  141. * asynchronous core.
  142. *
  143. * The core mechanism is block_until_pred(), which does not return until pred()
  144. * returns a value other than 0. The blocker uses OS I/O synchronisation
  145. * primitives (e.g. poll(2)) and ticks the reactor until the predicate is
  146. * satisfied. The blocker is not required to call pred() more than once between
  147. * tick calls.
  148. *
  149. * When pred returns a non-zero value, that value is returned by this function.
  150. * This can be used to allow pred() to indicate error conditions and short
  151. * circuit the blocking process.
  152. *
  153. * A return value of -1 is reserved for network polling errors. Therefore this
  154. * return value should not be used by pred() if ambiguity is not desired. Note
  155. * that the predicate function can always arrange its own output mechanism, for
  156. * example by passing a structure of its own as the argument.
  157. *
  158. * If the SKIP_FIRST_TICK flag is set, the first call to reactor_tick() before
  159. * the first call to pred() is skipped. This is useful if it is known that
  160. * ticking the reactor again will not be useful (e.g. because it has already
  161. * been done).
  162. *
  163. * This function assumes a write lock is held for the entire QUIC_CHANNEL. If
  164. * mutex is non-NULL, it must be a lock currently held for write; it will be
  165. * unlocked during any sleep, and then relocked for write afterwards.
  166. *
  167. * Precondition: mutex is NULL or is held for write (unchecked)
  168. * Postcondition: mutex is NULL or is held for write (unless
  169. * CRYPTO_THREAD_write_lock fails)
  170. */
  171. #define SKIP_FIRST_TICK (1U << 0)
  172. int ossl_quic_reactor_block_until_pred(QUIC_REACTOR *rtor,
  173. int (*pred)(void *arg), void *pred_arg,
  174. uint32_t flags,
  175. CRYPTO_MUTEX *mutex);
  176. # endif
  177. #endif