APIs.txt revision 229fce07
1# Copyright (c) 2017 - 2019 LiteSpeed Technologies Inc.  See LICENSE.
2LSQUIC APIs
3===========
4
5LSQUIC exposes the following object types to the user:
6
7    - Engine Settings   (struct lsquic_engine_settings)
8    - Stream Interface  (struct lsquic_stream_if) 
9    - Engine API        (struct lsquic_engine_api)
10    - Engine
11    - Connection
12    - Stream
13
14The first three -- engine settings, engine APIs, and stream interface --
15are used to instantiate the engine.  After engine is instantiated, the
16user code need only concern itself with engine, connections, and streams.
17
18
19Engine Settings
20---------------
21
22Engine settings is the struct lsquic_engine_settings.  It contains various
23QUIC settings and LSQUIC parameters.  The usual way to use it is to initialize
24it to default values using lsquic_engine_init_settings(), modify any values
25if necessary, and pass it as parameter to lsquic_engine_new().
26
27QUIC settings are specified by the following members:
28
29    lsquic_engine_settings      QUIC
30    member                      parameter
31    ----------------------      ---------
32    es_cfcw                     CFCW
33    es_sfcw                     SFCW
34    es_max_streams_in           MIDS
35    es_ua                       UAID
36    es_versions                 VER
37    es_idle_conn_to             ICSL
38    es_silent_close             SCLS
39    es_support_srej             COPT/SREJ
40    es_support_nstp             COPT/NSTP
41    es_support_tcid0            TCID
42
43The following parameters affect run-time behavior:
44
45    es_rw_once                  Important: affects event dispatch
46    es_handshake_to
47    es_support_push
48    es_pace_packets
49
50Other noteworthy settings:
51
52    es_max_header_list_size
53    es_progress_check
54
55To be sure your settings are good (in other words, passing this struct won't
56trip up the engine constructor), use lsquic_engine_check_settings().
57
58
59Stream Interface
60----------------
61
62The stream interface, lsquic_stream_if, specifies callbacks LSQUIC engine
63will call for connections and streams.
64
65The following callbacks should be specified for connection:
66
67    on_new_conn             This is called when connection is created.
68
69    on_goaway_received      This function is called when we receive GOAWAY
70                            frame from peer.  This callback is optional.
71
72    on_conn_closed          Connection is closed: all streams have been
73                            destroyed.
74
75The streams have four callbacks:
76
77    on_new_stream           Stream has been created.
78
79    on_read                 Stream can be read from (see Events).
80
81    on_write                Stream can be written to (see Events).
82
83    on_close                Stream has been closed.
84
85For both connections and streams, the "on new" callback return value can
86be use to specify user-supplied data.  This data pointer is optional and
87can be NULL.  It can also refer to the same data for the connection and
88its streams.  "on close" callbacks should be used to free user-supplied
89data.
90
91
92Engine API
93----------
94
95The engine API, struct lsquic_engine_api, is a combination structure to
96make calling lsquic_engine_new() manageable.  It holds references to
97struct lsquic_engine_settings and struct lsquic_stream_if, as well as:
98
99    - Interface for sending outgoing packets, ea_packets_out
100    - Interface for allocating memory for outgoing packet buffers
101      (optional).
102
103ea_packets_out is a pointer to a function of type lsquic_packets_out_f.
104The engine calls this function when it is appropriate to send out packets
105for one or more connections, which it gives to the function in a batch.
106This batch is an array of struct lsquic_out_spec.
107
108
109Engine
110------
111
112The engine is instantiated using lsquic_engine_new().  The first parameter
113is a list flags and the second parameter is the reference to the engine
114api.  The engine settings are specified, they are copied; changing
115the setting after the engine has been created will not affect engine's
116behavior.  If the settings are not specified, the engine will use default
117settings created by lsquic_engine_init_settings().
118
119Once the engine is instantiated, there are four main ways to use it to
120drive QUIC connections:
121
122    1. Create a connection using lsquic_engine_connect().
123    2. Feed it incoming packets using lsquic_engine_packet_in() function.
124    3. Process connections using one of the connection queue functions
125       (see Connection Queues).
126    4. Accept outgoing packets for sending (and send them!) using
127       ea_packets_out callback.
128
129
130Connection Management
131---------------------
132
133A connections needs to be processed once in a while.  It needs to be
134processed when one of the following is true:
135
136    - There are incoming packets;
137    - A stream is both readable by the user code and the user code wants
138      to read from it;
139    - A stream is both writeable by the user code and the user code wants
140      to write to it;
141    - User has written to stream outside of on_write() callbacks (that is
142      allowed) and now there are packets ready to be sent;
143    - A timer (pacer, retransmission, idle, etc) has expired;
144    - A control frame needs to be sent out;
145    - A stream needs to be serviced or created.
146
147Each of these use cases is handled by a single function:
148
149    lsquic_engine_process_conns()
150
151The connections to which the conditions above apply are processed (or
152"ticked") in the least recently ticked order.  After calling this function,
153you can see when is the next time a connection needs to be processed using
154
155    lsquic_engine_earliest_adv_tick()
156
157Based on this value, next event can be scheduled (in the event loop of
158your choice).
159
160Connection
161----------
162
163A connection is created using lsquic_engine_connect().  When on_new_conn()
164is called, the client code should call lsquic_conn_make_stream() one or
165more times.  One new stream will be created for each one of those calls.
166
167Several auxiliary functions are available:
168
169    - lsquic_conn_id()
170    - lsquic_conn_going_away()
171    - lsquic_conn_get_peer_ctx()
172    - lsquic_conn_get_stream_by_id()
173    - lsquic_conn_get_ctx()
174
175
176Stream
177------
178
179LSQUIC stream hides QUIC and HTTP/2 framing complexities from the user.
180What it presents is a way to send HTTP headers and, optionally, body to
181peer.  On read side, the user gets what looks like HTTP/1.1 stream.
182
183Expected usage for client is to express the desire to write to stream
184using lsquic_stream_wantwrite() call.  Once on_write() is called:
185
186    1. Write headers using lsquic_stream_send_headers()
187    2. Optionally write payload body using of of lsquic_stream_write(),
188       lsquic_stream_writev(), or lsquic_stream_writef().
189
190That done, shutdown write side using lsquic_stream_shutdown(), unregister
191for write events and register for read events using lsquic_stream_wantread().
192
193Read and parse HTTP/1.1 stream from on_read() callback until end-of-stream
194or an error is encountered.
195
196Then unregister the read event and shutdown the read side.  The stream will
197be closed after that at some point and on_close() callback will be called,
198at which point resources can be freed.  (Internally, the stream object is
199not destroyed until either all the packets carrying its data are ACKed or
200the connection is destroyed).
201
202on_read() and on_write() callbacks are dispatched differently based on the
203value of es_rw_once:
204
205If es_rw_once is false, then the callbacks are dispatched in a loop until
206the user unregisters the event or the stream becomes unreadable (or
207unwriteable).
208
209If es_rw_once is true, on_read() and on_write() are called once "per tick".
210It is the up to the user to read and write enough data.
211
212
213Events
214------
215
216Stream events are persistent: once call lsquic_stream_wantwrite() or
217lsquic_stream_wantread(), the event stays active until turned off.
218
219Note that when an error is encountered (such as a stream reset), the
220stream becomes readable and writeable: this allows user code to collect
221the error.
222
223
224Versions
225--------
226
227QUIC version are listed in enum lsquic_version.  To specify a list of
228versions, they are usually placed in a bitmask, e.g. es_versions.
229