quic_reactor.h 7.7 KB

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