lsquic_stream.h revision 229fce07
1/* Copyright (c) 2017 - 2019 LiteSpeed Technologies Inc.  See LICENSE. */
2#ifndef LSQUIC_STREAM_H
3#define LSQUIC_STREAM_H
4
5#define LSQUIC_STREAM_HANDSHAKE 1
6#define LSQUIC_STREAM_HEADERS   3
7
8#define LSQUIC_STREAM_DEFAULT_PRIO 16   /* RFC 7540, Section 5.3.5 */
9
10struct lsquic_stream_if;
11struct lsquic_stream_ctx;
12struct lsquic_conn_public;
13struct stream_frame;
14struct uncompressed_headers;
15
16TAILQ_HEAD(lsquic_streams_tailq, lsquic_stream);
17
18#ifndef LSQUIC_KEEP_STREAM_HISTORY
19#   ifdef NDEBUG
20#       define LSQUIC_KEEP_STREAM_HISTORY 0
21#   else
22#       define LSQUIC_KEEP_STREAM_HISTORY 1
23#   endif
24#endif
25
26#if LSQUIC_KEEP_STREAM_HISTORY
27#define SM_HIST_BITS 6
28#define SM_HIST_IDX_MASK ((1 << SM_HIST_BITS) - 1)
29typedef unsigned char sm_hist_idx_t;
30#endif
31
32struct lsquic_stream
33{
34    uint32_t                        id;
35    enum stream_flags {
36        STREAM_WANT_READ    = (1 << 0),
37        STREAM_WANT_WRITE   = (1 << 1),
38        STREAM_FIN_RECVD    = (1 << 2),     /* Received STREAM frame with FIN bit set */
39        STREAM_RST_RECVD    = (1 << 3),     /* Received RST frame */
40        STREAM_SEND_WUF     = (1 << 4),     /* WUF: Window Update Frame */
41        STREAM_LAST_WRITE_OK= (1 << 5),     /* Used to break out of write event dispatch loop */
42        STREAM_SEND_BLOCKED = (1 << 6),
43        STREAM_SEND_RST     = (1 << 7),     /* Error: want to send RST_STREAM */
44        STREAM_U_READ_DONE  = (1 << 8),     /* User is done reading (shutdown was called) */
45        STREAM_U_WRITE_DONE = (1 << 9),     /* User is done writing (shutdown was called) */
46        STREAM_FIN_SENT     = (1 <<10),     /* FIN was written to network */
47        STREAM_RST_SENT     = (1 <<11),     /* RST_STREAM was written to network */
48        STREAM_WANT_FLUSH   = (1 <<12),     /* Flush until sm_flush_to is hit */
49        STREAM_FIN_REACHED  = (1 <<13),     /* User read data up to FIN */
50        STREAM_FINISHED     = (1 <<14),     /* Stream is finished */
51        STREAM_ONCLOSE_DONE = (1 <<15),     /* on_close has been called */
52        STREAM_CALL_ONCLOSE = (1 <<16),
53        STREAM_FREE_STREAM  = (1 <<17),
54        STREAM_USE_HEADERS  = (1 <<18),
55        STREAM_HEADERS_SENT = (1 <<19),
56        STREAM_HAVE_UH      = (1 <<20),     /* Have uncompressed headers */
57        STREAM_CONN_LIMITED = (1 <<21),
58        STREAM_HEAD_IN_FIN  = (1 <<22),     /* Incoming headers has FIN bit set */
59        STREAM_ABORT_CONN   = (1 <<23),     /* Unrecoverable error occurred */
60        STREAM_FRAMES_ELIDED= (1 <<24),
61        STREAM_FORCE_FINISH = (1 <<25),     /* Replaces FIN sent and received */
62        STREAM_ONNEW_DONE   = (1 <<26),     /* on_new_stream has been called */
63        STREAM_AUTOSWITCH   = (1 <<27),
64        STREAM_RW_ONCE      = (1 <<28),     /* When set, read/write events are dispatched once per call */
65    }                               stream_flags;
66
67    /* There are more than one reason that a stream may be put onto
68     * connections's sending_streams queue.  Note that writing STREAM
69     * frames is done separately.
70     */
71    #define STREAM_SENDING_FLAGS (STREAM_SEND_WUF| \
72                                          STREAM_SEND_RST|STREAM_SEND_BLOCKED)
73
74    #define STREAM_WRITE_Q_FLAGS (STREAM_WANT_FLUSH|STREAM_WANT_WRITE)
75
76    /* Any of these flags will cause user-facing read and write and
77     * shutdown calls to return an error.  They also make the stream
78     * both readable and writeable, as we want the user to collect
79     * the error.
80     */
81    #define STREAM_RST_FLAGS (STREAM_RST_RECVD|STREAM_RST_SENT|\
82                                                        STREAM_SEND_RST)
83
84    #define STREAM_SERVICE_FLAGS (STREAM_CALL_ONCLOSE|STREAM_FREE_STREAM|\
85                                                            STREAM_ABORT_CONN)
86
87    const struct lsquic_stream_if  *stream_if;
88    struct lsquic_stream_ctx       *st_ctx;
89    struct lsquic_conn_public      *conn_pub;
90    TAILQ_ENTRY(lsquic_stream)      next_send_stream, next_read_stream,
91                                        next_write_stream, next_service_stream,
92                                        next_prio_stream;
93
94    uint32_t                        error_code;
95    uint64_t                        tosend_off;
96    uint64_t                        max_send_off;
97
98    /* From the network, we get frames, which we keep on a list ordered
99     * by offset.
100     */
101    struct data_in                 *data_in;
102    uint64_t                        read_offset;
103    lsquic_sfcw_t                   fc;
104
105    /** If @ref STREAM_WANT_FLUSH is set, flush until this offset. */
106    uint64_t                        sm_flush_to;
107
108    /* Last offset sent in BLOCKED frame */
109    uint64_t                        blocked_off;
110
111    struct uncompressed_headers    *uh,
112                                   *push_req;
113
114    unsigned char                  *sm_buf;
115    void                           *sm_onnew_arg;
116
117    unsigned                        n_unacked;
118    unsigned short                  sm_n_buffered;  /* Amount of data in sm_buf */
119
120    unsigned char                   sm_priority;  /* 0: high; 255: low */
121#if LSQUIC_KEEP_STREAM_HISTORY
122    sm_hist_idx_t                   sm_hist_idx;
123#endif
124
125#if LSQUIC_KEEP_STREAM_HISTORY
126    /* Stream history: see enum stream_history_event */
127    unsigned char                   sm_hist_buf[ 1 << SM_HIST_BITS ];
128#endif
129};
130
131enum stream_ctor_flags
132{
133    SCF_CALL_ON_NEW   = (1 << 0), /* Call on_new_stream() immediately */
134    SCF_USE_DI_HASH   = (1 << 1), /* Use hash-based data input.  If not set,
135                                   * the nocopy data input is used.
136                                   */
137    SCF_DI_AUTOSWITCH = (1 << 2), /* Automatically switch between nocopy
138                                   * and hash-based to data input for optimal
139                                   * performance.
140                                   */
141    SCF_DISP_RW_ONCE  = (1 << 3),
142};
143
144lsquic_stream_t *
145lsquic_stream_new_ext (uint32_t id, struct lsquic_conn_public *conn_pub,
146                       const struct lsquic_stream_if *, void *stream_if_ctx,
147                       unsigned initial_sfrw, unsigned initial_send_off,
148                       enum stream_ctor_flags);
149
150#define lsquic_stream_new(id, pub, sm_if, sm_if_ctx, cfcw, send_off)        \
151        lsquic_stream_new_ext(id, pub, sm_if, sm_if_ctx, cfcw, send_off,    \
152                              (SCF_CALL_ON_NEW|SCF_DI_AUTOSWITCH))
153
154void
155lsquic_stream_call_on_new (lsquic_stream_t *);
156
157void
158lsquic_stream_destroy (lsquic_stream_t *);
159
160#define lsquic_stream_is_reset(stream) \
161    (!!((stream)->stream_flags & STREAM_RST_FLAGS))
162
163/* Data that from the network gets inserted into the stream using
164 * lsquic_stream_frame_in() function.  Returns 0 on success, -1 on
165 * failure.  The latter may be caused by flow control violation or
166 * invalid stream frame data, e.g. overlapping segments.
167 *
168 * Note that the caller does gives up control of `frame' no matter
169 * what this function returns.
170 *
171 * This data is read by the user using lsquic_stream_read() function.
172 */
173int
174lsquic_stream_frame_in (lsquic_stream_t *, struct stream_frame *frame);
175
176/* Only one (at least for now) uncompressed header structure is allowed to be
177 * passed in, and only in HTTP mode.
178 */
179int
180lsquic_stream_uh_in (lsquic_stream_t *, struct uncompressed_headers *);
181
182void
183lsquic_stream_push_req (lsquic_stream_t *,
184                        struct uncompressed_headers *push_req);
185
186int
187lsquic_stream_rst_in (lsquic_stream_t *, uint64_t offset, uint32_t error_code);
188
189ssize_t
190lsquic_stream_read (lsquic_stream_t *stream, void *buf, size_t len);
191
192uint64_t
193lsquic_stream_read_offset (const lsquic_stream_t *stream);
194
195/* Return true if we sent all available data to the network and write
196 * end of the stream was closed.
197 */
198int
199lsquic_stream_tosend_fin (const lsquic_stream_t *stream);
200
201/* Data to be sent out to the network is written using lsquic_stream_write().
202 */
203ssize_t
204lsquic_stream_write (lsquic_stream_t *stream, const void *buf, size_t len);
205
206void
207lsquic_stream_window_update (lsquic_stream_t *stream, uint64_t offset);
208
209int
210lsquic_stream_set_max_send_off (lsquic_stream_t *stream, unsigned offset);
211
212/* The caller should only call this function if STREAM_SEND_WUF is set and
213 * it must generate a window update frame using this value.
214 */
215uint64_t
216lsquic_stream_fc_recv_off (lsquic_stream_t *stream);
217
218void
219lsquic_stream_dispatch_read_events (lsquic_stream_t *);
220
221void
222lsquic_stream_dispatch_write_events (lsquic_stream_t *);
223
224void
225lsquic_stream_blocked_frame_sent (lsquic_stream_t *);
226
227void
228lsquic_stream_rst_frame_sent (lsquic_stream_t *);
229
230void
231lsquic_stream_stream_frame_sent (lsquic_stream_t *);
232
233void
234lsquic_stream_reset (lsquic_stream_t *, uint32_t error_code);
235
236void
237lsquic_stream_reset_ext (lsquic_stream_t *, uint32_t error_code, int close);
238
239void
240lsquic_stream_call_on_close (lsquic_stream_t *);
241
242void
243lsquic_stream_shutdown_internal (lsquic_stream_t *);
244
245void
246lsquic_stream_received_goaway (lsquic_stream_t *);
247
248void
249lsquic_stream_acked (lsquic_stream_t *);
250
251#define lsquic_stream_is_closed(s)                                          \
252    (((s)->stream_flags & (STREAM_U_READ_DONE|STREAM_U_WRITE_DONE))         \
253                            == (STREAM_U_READ_DONE|STREAM_U_WRITE_DONE))
254int
255lsquic_stream_update_sfcw (lsquic_stream_t *, uint64_t max_off);
256
257int
258lsquic_stream_set_priority_internal (lsquic_stream_t *, unsigned priority);
259
260/* The following flags are checked to see whether progress was made: */
261#define STREAM_RW_PROG_FLAGS (                                              \
262    STREAM_U_READ_DONE  /* User closed read side of the stream */           \
263   |STREAM_FIN_REACHED  /* User reached FIN.  We check this because it */   \
264                        /*   may have been a result of zero-byte read. */   \
265)
266
267/* Stream progress status is used to judge whether a connection made progress
268 * during Pending RW Queue processing.  We only check for stream read progress,
269 * as the write progress is defined as any new data packetized for sending.
270 */
271struct stream_read_prog_status
272{
273    uint64_t                srps_read_offset;
274    enum stream_flags       srps_flags;
275};
276
277#define lsquic_stream_is_critical(stream) (                                 \
278    (stream)->id == LSQUIC_STREAM_HANDSHAKE ||                              \
279    ((stream)->id == LSQUIC_STREAM_HEADERS &&                               \
280        (stream)->stream_flags & STREAM_USE_HEADERS))
281
282size_t
283lsquic_stream_mem_used (const struct lsquic_stream *);
284
285lsquic_cid_t
286lsquic_stream_cid (const struct lsquic_stream *);
287
288#define lsquic_stream_has_data_to_flush(stream) ((stream)->sm_n_buffered > 0)
289
290int
291lsquic_stream_readable (const lsquic_stream_t *);
292
293size_t
294lsquic_stream_write_avail (const struct lsquic_stream *);
295
296#ifndef NDEBUG
297size_t
298lsquic_stream_flush_threshold (const struct lsquic_stream *);
299#endif
300
301#endif
302