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