1*e7be843bSPierre Pronchery /*
2*e7be843bSPierre Pronchery * Copyright 2022-2025 The OpenSSL Project Authors. All Rights Reserved.
3*e7be843bSPierre Pronchery *
4*e7be843bSPierre Pronchery * Licensed under the Apache License 2.0 (the "License"). You may not use
5*e7be843bSPierre Pronchery * this file except in compliance with the License. You can obtain a copy
6*e7be843bSPierre Pronchery * in the file LICENSE in the source distribution or at
7*e7be843bSPierre Pronchery * https://www.openssl.org/source/license.html
8*e7be843bSPierre Pronchery */
9*e7be843bSPierre Pronchery #ifndef OSSL_QUIC_REACTOR_H
10*e7be843bSPierre Pronchery # define OSSL_QUIC_REACTOR_H
11*e7be843bSPierre Pronchery
12*e7be843bSPierre Pronchery # include "internal/time.h"
13*e7be843bSPierre Pronchery # include "internal/sockets.h"
14*e7be843bSPierre Pronchery # include "internal/quic_predef.h"
15*e7be843bSPierre Pronchery # include "internal/thread_arch.h"
16*e7be843bSPierre Pronchery # include "internal/rio_notifier.h"
17*e7be843bSPierre Pronchery # include <openssl/bio.h>
18*e7be843bSPierre Pronchery
19*e7be843bSPierre Pronchery # ifndef OPENSSL_NO_QUIC
20*e7be843bSPierre Pronchery
21*e7be843bSPierre Pronchery /*
22*e7be843bSPierre Pronchery * Core I/O Reactor Framework
23*e7be843bSPierre Pronchery * ==========================
24*e7be843bSPierre Pronchery *
25*e7be843bSPierre Pronchery * Manages use of async network I/O which the QUIC stack is built on. The core
26*e7be843bSPierre Pronchery * mechanic looks like this:
27*e7be843bSPierre Pronchery *
28*e7be843bSPierre Pronchery * - There is a pollable FD for both the read and write side respectively.
29*e7be843bSPierre Pronchery * Readability and writeability of these FDs respectively determines when
30*e7be843bSPierre Pronchery * network I/O is available.
31*e7be843bSPierre Pronchery *
32*e7be843bSPierre Pronchery * - The reactor can export these FDs to the user, as well as flags indicating
33*e7be843bSPierre Pronchery * whether the user should listen for readability, writeability, or neither.
34*e7be843bSPierre Pronchery *
35*e7be843bSPierre Pronchery * - The reactor can export a timeout indication to the user, indicating when
36*e7be843bSPierre Pronchery * the reactor should be called (via libssl APIs) regardless of whether
37*e7be843bSPierre Pronchery * the network socket has become ready.
38*e7be843bSPierre Pronchery *
39*e7be843bSPierre Pronchery * The reactor is based around a tick callback which is essentially the mutator
40*e7be843bSPierre Pronchery * function. The mutator attempts to do whatever it can, attempting to perform
41*e7be843bSPierre Pronchery * network I/O to the extent currently feasible. When done, the mutator returns
42*e7be843bSPierre Pronchery * information to the reactor indicating when it should be woken up again:
43*e7be843bSPierre Pronchery *
44*e7be843bSPierre Pronchery * - Should it be woken up when network RX is possible?
45*e7be843bSPierre Pronchery * - Should it be woken up when network TX is possible?
46*e7be843bSPierre Pronchery * - Should it be woken up no later than some deadline X?
47*e7be843bSPierre Pronchery *
48*e7be843bSPierre Pronchery * The intention is that ALL I/O-related SSL_* functions with side effects (e.g.
49*e7be843bSPierre Pronchery * SSL_read/SSL_write) consist of three phases:
50*e7be843bSPierre Pronchery *
51*e7be843bSPierre Pronchery * - Optionally mutate the QUIC machine's state.
52*e7be843bSPierre Pronchery * - Optionally tick the QUIC reactor.
53*e7be843bSPierre Pronchery * - Optionally mutate the QUIC machine's state.
54*e7be843bSPierre Pronchery *
55*e7be843bSPierre Pronchery * For example, SSL_write is a mutation (appending to a stream buffer) followed
56*e7be843bSPierre Pronchery * by an optional tick (generally expected as we may want to send the data
57*e7be843bSPierre Pronchery * immediately, though not strictly needed if transmission is being deferred due
58*e7be843bSPierre Pronchery * to Nagle's algorithm, etc.).
59*e7be843bSPierre Pronchery *
60*e7be843bSPierre Pronchery * SSL_read is also a mutation and in principle does not need to tick the
61*e7be843bSPierre Pronchery * reactor, but it generally will anyway to ensure that the reactor is regularly
62*e7be843bSPierre Pronchery * ticked by an application which is only reading and not writing.
63*e7be843bSPierre Pronchery *
64*e7be843bSPierre Pronchery * If the SSL object is being used in blocking mode, SSL_read may need to block
65*e7be843bSPierre Pronchery * if no data is available yet, and SSL_write may need to block if buffers
66*e7be843bSPierre Pronchery * are full.
67*e7be843bSPierre Pronchery *
68*e7be843bSPierre Pronchery * The internals of the QUIC I/O engine always use asynchronous I/O. If the
69*e7be843bSPierre Pronchery * application desires blocking semantics, we handle this by adding a blocking
70*e7be843bSPierre Pronchery * adaptation layer on top of our internal asynchronous I/O API as exposed by
71*e7be843bSPierre Pronchery * the reactor interface.
72*e7be843bSPierre Pronchery */
73*e7be843bSPierre Pronchery struct quic_tick_result_st {
74*e7be843bSPierre Pronchery OSSL_TIME tick_deadline;
75*e7be843bSPierre Pronchery char net_read_desired;
76*e7be843bSPierre Pronchery char net_write_desired;
77*e7be843bSPierre Pronchery char notify_other_threads;
78*e7be843bSPierre Pronchery };
79*e7be843bSPierre Pronchery
80*e7be843bSPierre Pronchery static ossl_inline ossl_unused void
ossl_quic_tick_result_merge_into(QUIC_TICK_RESULT * r,const QUIC_TICK_RESULT * src)81*e7be843bSPierre Pronchery ossl_quic_tick_result_merge_into(QUIC_TICK_RESULT *r,
82*e7be843bSPierre Pronchery const QUIC_TICK_RESULT *src)
83*e7be843bSPierre Pronchery {
84*e7be843bSPierre Pronchery r->net_read_desired = r->net_read_desired || src->net_read_desired;
85*e7be843bSPierre Pronchery r->net_write_desired = r->net_write_desired || src->net_write_desired;
86*e7be843bSPierre Pronchery r->notify_other_threads = r->notify_other_threads || src->notify_other_threads;
87*e7be843bSPierre Pronchery r->tick_deadline = ossl_time_min(r->tick_deadline, src->tick_deadline);
88*e7be843bSPierre Pronchery }
89*e7be843bSPierre Pronchery
90*e7be843bSPierre Pronchery struct quic_reactor_st {
91*e7be843bSPierre Pronchery /*
92*e7be843bSPierre Pronchery * BIO poll descriptors which can be polled. poll_r is a poll descriptor
93*e7be843bSPierre Pronchery * which becomes readable when the QUIC state machine can potentially do
94*e7be843bSPierre Pronchery * work, and poll_w is a poll descriptor which becomes writable when the
95*e7be843bSPierre Pronchery * QUIC state machine can potentially do work. Generally, either of these
96*e7be843bSPierre Pronchery * conditions means that SSL_tick() should be called, or another SSL
97*e7be843bSPierre Pronchery * function which implicitly calls SSL_tick() (e.g. SSL_read/SSL_write()).
98*e7be843bSPierre Pronchery */
99*e7be843bSPierre Pronchery BIO_POLL_DESCRIPTOR poll_r, poll_w;
100*e7be843bSPierre Pronchery OSSL_TIME tick_deadline; /* ossl_time_infinite() if none currently applicable */
101*e7be843bSPierre Pronchery
102*e7be843bSPierre Pronchery void (*tick_cb)(QUIC_TICK_RESULT *res, void *arg, uint32_t flags);
103*e7be843bSPierre Pronchery void *tick_cb_arg;
104*e7be843bSPierre Pronchery
105*e7be843bSPierre Pronchery /* The mutex used for ticking. Not owned by the reactor. */
106*e7be843bSPierre Pronchery CRYPTO_MUTEX *mutex;
107*e7be843bSPierre Pronchery
108*e7be843bSPierre Pronchery /* Used to notify other threads. Valid only if have_notifier is set. */
109*e7be843bSPierre Pronchery RIO_NOTIFIER notifier;
110*e7be843bSPierre Pronchery
111*e7be843bSPierre Pronchery /*
112*e7be843bSPierre Pronchery * Condvar to assist synchronising use of the notifier. Valid only if
113*e7be843bSPierre Pronchery * have_notifier is set.
114*e7be843bSPierre Pronchery */
115*e7be843bSPierre Pronchery CRYPTO_CONDVAR *notifier_cv;
116*e7be843bSPierre Pronchery
117*e7be843bSPierre Pronchery /*
118*e7be843bSPierre Pronchery * Count of the current number of blocking waiters. Like everything else,
119*e7be843bSPierre Pronchery * this is protected by the caller's mutex (i.e., the engine mutex).
120*e7be843bSPierre Pronchery */
121*e7be843bSPierre Pronchery size_t cur_blocking_waiters;
122*e7be843bSPierre Pronchery
123*e7be843bSPierre Pronchery /*
124*e7be843bSPierre Pronchery * These are true if we would like to know when we can read or write from
125*e7be843bSPierre Pronchery * the network respectively.
126*e7be843bSPierre Pronchery */
127*e7be843bSPierre Pronchery unsigned int net_read_desired : 1;
128*e7be843bSPierre Pronchery unsigned int net_write_desired : 1;
129*e7be843bSPierre Pronchery
130*e7be843bSPierre Pronchery /*
131*e7be843bSPierre Pronchery * Are the read and write poll descriptors we are currently configured with
132*e7be843bSPierre Pronchery * things we can actually poll?
133*e7be843bSPierre Pronchery */
134*e7be843bSPierre Pronchery unsigned int can_poll_r : 1;
135*e7be843bSPierre Pronchery unsigned int can_poll_w : 1;
136*e7be843bSPierre Pronchery
137*e7be843bSPierre Pronchery /* 1 if notifier is present and initialised. */
138*e7be843bSPierre Pronchery unsigned int have_notifier : 1;
139*e7be843bSPierre Pronchery
140*e7be843bSPierre Pronchery /* 1 if a block_until_pred call has put the notifier in the signalled state. */
141*e7be843bSPierre Pronchery unsigned int signalled_notifier : 1;
142*e7be843bSPierre Pronchery };
143*e7be843bSPierre Pronchery
144*e7be843bSPierre Pronchery /* Create an OS notifier? */
145*e7be843bSPierre Pronchery #define QUIC_REACTOR_FLAG_USE_NOTIFIER (1U << 0)
146*e7be843bSPierre Pronchery
147*e7be843bSPierre Pronchery int ossl_quic_reactor_init(QUIC_REACTOR *rtor,
148*e7be843bSPierre Pronchery void (*tick_cb)(QUIC_TICK_RESULT *res, void *arg,
149*e7be843bSPierre Pronchery uint32_t flags),
150*e7be843bSPierre Pronchery void *tick_cb_arg,
151*e7be843bSPierre Pronchery CRYPTO_MUTEX *mutex,
152*e7be843bSPierre Pronchery OSSL_TIME initial_tick_deadline,
153*e7be843bSPierre Pronchery uint64_t flags);
154*e7be843bSPierre Pronchery
155*e7be843bSPierre Pronchery void ossl_quic_reactor_cleanup(QUIC_REACTOR *rtor);
156*e7be843bSPierre Pronchery
157*e7be843bSPierre Pronchery void ossl_quic_reactor_set_poll_r(QUIC_REACTOR *rtor,
158*e7be843bSPierre Pronchery const BIO_POLL_DESCRIPTOR *r);
159*e7be843bSPierre Pronchery
160*e7be843bSPierre Pronchery void ossl_quic_reactor_set_poll_w(QUIC_REACTOR *rtor,
161*e7be843bSPierre Pronchery const BIO_POLL_DESCRIPTOR *w);
162*e7be843bSPierre Pronchery
163*e7be843bSPierre Pronchery const BIO_POLL_DESCRIPTOR *ossl_quic_reactor_get_poll_r(const QUIC_REACTOR *rtor);
164*e7be843bSPierre Pronchery const BIO_POLL_DESCRIPTOR *ossl_quic_reactor_get_poll_w(const QUIC_REACTOR *rtor);
165*e7be843bSPierre Pronchery
166*e7be843bSPierre Pronchery int ossl_quic_reactor_can_poll_r(const QUIC_REACTOR *rtor);
167*e7be843bSPierre Pronchery int ossl_quic_reactor_can_poll_w(const QUIC_REACTOR *rtor);
168*e7be843bSPierre Pronchery
169*e7be843bSPierre Pronchery int ossl_quic_reactor_can_support_poll_descriptor(const QUIC_REACTOR *rtor,
170*e7be843bSPierre Pronchery const BIO_POLL_DESCRIPTOR *d);
171*e7be843bSPierre Pronchery
172*e7be843bSPierre Pronchery int ossl_quic_reactor_net_read_desired(QUIC_REACTOR *rtor);
173*e7be843bSPierre Pronchery int ossl_quic_reactor_net_write_desired(QUIC_REACTOR *rtor);
174*e7be843bSPierre Pronchery
175*e7be843bSPierre Pronchery OSSL_TIME ossl_quic_reactor_get_tick_deadline(QUIC_REACTOR *rtor);
176*e7be843bSPierre Pronchery
177*e7be843bSPierre Pronchery /*
178*e7be843bSPierre Pronchery * Do whatever work can be done, and as much work as can be done. This involves
179*e7be843bSPierre Pronchery * e.g. seeing if we can read anything from the network (if we want to), seeing
180*e7be843bSPierre Pronchery * if we can write anything to the network (if we want to), etc.
181*e7be843bSPierre Pronchery *
182*e7be843bSPierre Pronchery * If the CHANNEL_ONLY flag is set, this indicates that we should only
183*e7be843bSPierre Pronchery * touch state which is synchronised by the channel mutex.
184*e7be843bSPierre Pronchery */
185*e7be843bSPierre Pronchery #define QUIC_REACTOR_TICK_FLAG_CHANNEL_ONLY (1U << 0)
186*e7be843bSPierre Pronchery
187*e7be843bSPierre Pronchery int ossl_quic_reactor_tick(QUIC_REACTOR *rtor, uint32_t flags);
188*e7be843bSPierre Pronchery
189*e7be843bSPierre Pronchery RIO_NOTIFIER *ossl_quic_reactor_get0_notifier(QUIC_REACTOR *rtor);
190*e7be843bSPierre Pronchery
191*e7be843bSPierre Pronchery /*
192*e7be843bSPierre Pronchery * Blocking I/O Adaptation Layer
193*e7be843bSPierre Pronchery * =============================
194*e7be843bSPierre Pronchery *
195*e7be843bSPierre Pronchery * The blocking I/O adaptation layer implements blocking I/O on top of our
196*e7be843bSPierre Pronchery * asynchronous core.
197*e7be843bSPierre Pronchery */
198*e7be843bSPierre Pronchery
199*e7be843bSPierre Pronchery /*
200*e7be843bSPierre Pronchery * ossl_quic_reactor_block_until_pred
201*e7be843bSPierre Pronchery * ----------------------------------
202*e7be843bSPierre Pronchery *
203*e7be843bSPierre Pronchery * The core mechanism of the Blocking I/O Adaption Layer is block_until_pred(),
204*e7be843bSPierre Pronchery * which does not return until pred() returns a value other than 0. The blocker
205*e7be843bSPierre Pronchery * uses OS I/O synchronisation primitives (e.g. poll(2)) and ticks the reactor
206*e7be843bSPierre Pronchery * until the predicate is satisfied. The blocker is not required to call pred()
207*e7be843bSPierre Pronchery * more than once between tick calls.
208*e7be843bSPierre Pronchery *
209*e7be843bSPierre Pronchery * When pred returns a non-zero value, that value is returned by this function.
210*e7be843bSPierre Pronchery * This can be used to allow pred() to indicate error conditions and short
211*e7be843bSPierre Pronchery * circuit the blocking process.
212*e7be843bSPierre Pronchery *
213*e7be843bSPierre Pronchery * A return value of -1 is reserved for network polling errors. Therefore this
214*e7be843bSPierre Pronchery * return value should not be used by pred() if ambiguity is not desired. Note
215*e7be843bSPierre Pronchery * that the predicate function can always arrange its own output mechanism, for
216*e7be843bSPierre Pronchery * example by passing a structure of its own as the argument.
217*e7be843bSPierre Pronchery *
218*e7be843bSPierre Pronchery * If the SKIP_FIRST_TICK flag is set, the first call to reactor_tick() before
219*e7be843bSPierre Pronchery * the first call to pred() is skipped. This is useful if it is known that
220*e7be843bSPierre Pronchery * ticking the reactor again will not be useful (e.g. because it has already
221*e7be843bSPierre Pronchery * been done).
222*e7be843bSPierre Pronchery *
223*e7be843bSPierre Pronchery * This function assumes a write lock is held for the entire QUIC_CHANNEL. If
224*e7be843bSPierre Pronchery * mutex is non-NULL, it must be a lock currently held for write; it will be
225*e7be843bSPierre Pronchery * unlocked during any sleep, and then relocked for write afterwards.
226*e7be843bSPierre Pronchery *
227*e7be843bSPierre Pronchery * This function must not be called by a thread currently using
228*e7be843bSPierre Pronchery * ossl_quic_reactor_(enter/leave)_blocking_section() as this function also uses
229*e7be843bSPierre Pronchery * those functions (see below); it is assumed if a caller is using those
230*e7be843bSPierre Pronchery * functions it is implementing blocking semantics itself. There is no need to
231*e7be843bSPierre Pronchery * use those functions if using this function.
232*e7be843bSPierre Pronchery *
233*e7be843bSPierre Pronchery * Precondition: If a reactor mutex is being used, it must be held (unchecked)
234*e7be843bSPierre Pronchery * Postcondition: If a reactor mutex is being used, it is held
235*e7be843bSPierre Pronchery * Invariant: The current thread does not have an outstanding
236*e7be843bSPierre Pronchery * ossl_quic_reactor_enter_blocking_section() call (unchecked)
237*e7be843bSPierre Pronchery */
238*e7be843bSPierre Pronchery #define SKIP_FIRST_TICK (1U << 0)
239*e7be843bSPierre Pronchery
240*e7be843bSPierre Pronchery int ossl_quic_reactor_block_until_pred(QUIC_REACTOR *rtor,
241*e7be843bSPierre Pronchery int (*pred)(void *arg), void *pred_arg,
242*e7be843bSPierre Pronchery uint32_t flags);
243*e7be843bSPierre Pronchery
244*e7be843bSPierre Pronchery /*
245*e7be843bSPierre Pronchery * ossl_quic_reactor_(enter/leave)_blocking_section
246*e7be843bSPierre Pronchery * ------------------------------------------------
247*e7be843bSPierre Pronchery *
248*e7be843bSPierre Pronchery * This is used by blocking code outside of the reactor itself to inform the
249*e7be843bSPierre Pronchery * reactor of when a thread begins or ends a blocking call. This is used by the
250*e7be843bSPierre Pronchery * reactor so it knows if a tick means other threads might need to be woken up
251*e7be843bSPierre Pronchery * via the notifier. The reactor mutex must be held while calling these
252*e7be843bSPierre Pronchery * functions.
253*e7be843bSPierre Pronchery *
254*e7be843bSPierre Pronchery * The number of 'active' calls to these functions (i.e., the number of enter
255*e7be843bSPierre Pronchery * calls which have yet to be matched with a subsequent leave call) must *at all
256*e7be843bSPierre Pronchery * times* equal the number of threads blocking on the reactor. In other words, a
257*e7be843bSPierre Pronchery * single thread is not permitted to use these functions "recursively". Failing
258*e7be843bSPierre Pronchery * to adhere to this rule will result in deadlock.
259*e7be843bSPierre Pronchery *
260*e7be843bSPierre Pronchery * This means that if a caller has the concept of multiple concurrent blocking
261*e7be843bSPierre Pronchery * calls on the same thread on the same reactor (which may occur in some
262*e7be843bSPierre Pronchery * SSL_poll-related circumstances) it must do its own housekeeping to ensure it
263*e7be843bSPierre Pronchery * only calls enter() once. See quic_reactor_wait_ctx.h for a utility which can
264*e7be843bSPierre Pronchery * be used to accomplish this.
265*e7be843bSPierre Pronchery *
266*e7be843bSPierre Pronchery * ossl_quic_reactor_enter_blocking_section:
267*e7be843bSPierre Pronchery * Precondition: The current thread does not have an outstanding
268*e7be843bSPierre Pronchery * ossl_quic_reactor_enter_blocking_section() call (unchecked)
269*e7be843bSPierre Pronchery * Postcondition: The current thread has an outstanding
270*e7be843bSPierre Pronchery * ossl_quic_reactor_enter_blocking_section() call
271*e7be843bSPierre Pronchery *
272*e7be843bSPierre Pronchery * ossl_quic_reactor_leave_blocking_section:
273*e7be843bSPierre Pronchery * Precondition: The current thread has an outstanding
274*e7be843bSPierre Pronchery * ossl_quic_reactor_enter_blocking_section() call (unchecked)
275*e7be843bSPierre Pronchery * Postcondition: The current thread does not have an outstanding
276*e7be843bSPierre Pronchery * ossl_quic_reactor_enter_blocking_section() call
277*e7be843bSPierre Pronchery *
278*e7be843bSPierre Pronchery */
279*e7be843bSPierre Pronchery void ossl_quic_reactor_enter_blocking_section(QUIC_REACTOR *rtor);
280*e7be843bSPierre Pronchery void ossl_quic_reactor_leave_blocking_section(QUIC_REACTOR *rtor);
281*e7be843bSPierre Pronchery
282*e7be843bSPierre Pronchery # endif
283*e7be843bSPierre Pronchery
284*e7be843bSPierre Pronchery #endif
285