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