quic_reactor.h 7.7 KB

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