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