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