lsquic_frame_writer.c revision 55613f44
1/* Copyright (c) 2017 - 2020 LiteSpeed Technologies Inc.  See LICENSE. */
2/*
3 * lsquic_frame_writer.c -- write frames to HEADERS stream.
4 *
5 * The frame is first written to list of frame_buf's (frabs) and then
6 * out to the stream.  This is done because frame's size is written out
7 * to the stream and we may not have enough room in the stream to fit
8 * the whole frame.
9 */
10
11#ifndef WIN32
12#include <arpa/inet.h>
13#endif
14#include <assert.h>
15#include <errno.h>
16#include <inttypes.h>
17#include <stdlib.h>
18#include <string.h>
19#include <sys/queue.h>
20
21#include "lshpack.h"
22#include "lsquic_mm.h"
23#include "lsquic.h"
24#include "lsquic_int_types.h"
25#include "lsquic_hash.h"
26#include "lsquic_conn.h"
27
28#include "lsquic_frame_writer.h"
29#include "lsquic_frame_common.h"
30#include "lsquic_frab_list.h"
31#include "lsquic_ev_log.h"
32
33#define LSQUIC_LOGGER_MODULE LSQLM_FRAME_WRITER
34#define LSQUIC_LOG_CONN_ID lsquic_conn_log_cid(\
35                                        lsquic_stream_conn(fw->fw_stream))
36#include "lsquic_logger.h"
37
38/* Size of the buffer passed to lshpack_enc_encode() -- this limits the size
39 * of a single compressed header field.
40 */
41#define MAX_COMP_HEADER_FIELD_SIZE (64 * 1024)
42
43
44struct lsquic_frame_writer
45{
46    struct lsquic_stream       *fw_stream;
47    fw_writef_f                 fw_writef;
48    struct lsquic_mm           *fw_mm;
49    struct lshpack_enc         *fw_henc;
50#if LSQUIC_CONN_STATS
51    struct conn_stats          *fw_conn_stats;
52#endif
53    struct frab_list            fw_fral;
54    unsigned                    fw_max_frame_sz;
55    uint32_t                    fw_max_header_list_sz;  /* 0 means unlimited */
56    enum {
57        FW_SERVER   = (1 << 0),
58    }                           fw_flags;
59};
60
61
62/* RFC 7540, Section 4.2 */
63#define MIN_MAX_FRAME_SIZE  (1 << 14)
64#define MAX_MAX_FRAME_SIZE ((1 << 24) - 1)
65
66#define MAX(a, b) ((a) > (b) ? (a) : (b))
67#define SETTINGS_FRAME_SZ 6
68#define ABS_MIN_FRAME_SIZE MAX(SETTINGS_FRAME_SZ, \
69                                            sizeof(struct http_prio_frame))
70
71static void *
72fw_alloc (void *ctx, size_t size)
73{
74    return lsquic_mm_get_4k(ctx);
75}
76
77
78struct lsquic_frame_writer *
79lsquic_frame_writer_new (struct lsquic_mm *mm, struct lsquic_stream *stream,
80     unsigned max_frame_sz, struct lshpack_enc *henc, fw_writef_f writef,
81#if LSQUIC_CONN_STATS
82     struct conn_stats *conn_stats,
83#endif
84     int is_server)
85{
86    struct lsquic_frame_writer *fw;
87
88    /* When frame writer is instantiated, limit the maximum size to
89     * MIN_MAX_FRAME_SIZE.  The reference implementation has this value
90     * hardcoded and QUIC does not provide a mechanism to advertise a
91     * different value.
92     */
93    if (0 == max_frame_sz)
94        max_frame_sz = MIN_MAX_FRAME_SIZE;
95    else
96        LSQ_LOG1(LSQ_LOG_WARN, "max frame size specified to be %u bytes "
97            "-- this better be test code!", max_frame_sz);
98
99    if (!is_server && max_frame_sz < ABS_MIN_FRAME_SIZE)
100    {
101        LSQ_LOG1(LSQ_LOG_ERROR, "max frame size must be at least %zd bytes, "
102            "which is the size of priority information that client always "
103            "writes", ABS_MIN_FRAME_SIZE);
104        return NULL;
105    }
106
107    fw = malloc(sizeof(*fw));
108    if (!fw)
109        return NULL;
110
111    fw->fw_mm           = mm;
112    fw->fw_henc         = henc;
113    fw->fw_stream       = stream;
114    fw->fw_writef       = writef;
115    fw->fw_max_frame_sz = max_frame_sz;
116    fw->fw_max_header_list_sz = 0;
117    if (is_server)
118        fw->fw_flags    = FW_SERVER;
119    else
120        fw->fw_flags    = 0;
121#if LSQUIC_CONN_STATS
122    fw->fw_conn_stats   = conn_stats;
123#endif
124    lsquic_frab_list_init(&fw->fw_fral, 0x1000, fw_alloc,
125        (void (*)(void *, void *)) lsquic_mm_put_4k, mm);
126    return fw;
127}
128
129
130void
131lsquic_frame_writer_destroy (struct lsquic_frame_writer *fw)
132{
133    lsquic_frab_list_cleanup(&fw->fw_fral);
134    free(fw);
135}
136
137
138int
139lsquic_frame_writer_have_leftovers (const struct lsquic_frame_writer *fw)
140{
141    return !lsquic_frab_list_empty(&fw->fw_fral);
142}
143
144
145int
146lsquic_frame_writer_flush (struct lsquic_frame_writer *fw)
147{
148    struct lsquic_reader reader = {
149        .lsqr_read  = lsquic_frab_list_read,
150        .lsqr_size  = lsquic_frab_list_size,
151        .lsqr_ctx   = &fw->fw_fral,
152    };
153    ssize_t nw;
154
155    nw = fw->fw_writef(fw->fw_stream, &reader);
156
157    if (nw >= 0)
158        return 0;
159    else
160        return -1;
161}
162
163
164struct header_framer_ctx
165{
166    struct lsquic_frame_writer
167               *hfc_fw;
168    struct {
169        struct frame_buf   *frab;
170        unsigned short      off;
171    }           hfc_header_ptr;     /* Points to byte *after* current frame header */
172    unsigned    hfc_max_frame_sz;   /* Maximum frame size.  We always fill it. */
173    unsigned    hfc_cur_sz;         /* Number of bytes in the current frame. */
174    unsigned    hfc_n_frames;       /* Number of frames written. */
175    lsquic_stream_id_t
176                hfc_stream_id;      /* Stream ID */
177    enum http_frame_header_flags
178                hfc_first_flags;
179    enum http_frame_type
180                hfc_frame_type;
181};
182
183
184static void
185hfc_init (struct header_framer_ctx *hfc, struct lsquic_frame_writer *fw,
186      unsigned max_frame_sz, enum http_frame_type frame_type,
187      lsquic_stream_id_t stream_id, enum http_frame_header_flags first_flags)
188{
189    memset(hfc, 0, sizeof(*hfc));
190    hfc->hfc_fw           = fw;
191    hfc->hfc_frame_type   = frame_type;
192    hfc->hfc_stream_id    = stream_id;
193    hfc->hfc_first_flags  = first_flags;
194    hfc->hfc_max_frame_sz = max_frame_sz;
195    hfc->hfc_cur_sz       = max_frame_sz;
196}
197
198
199static void
200hfc_save_ptr (struct header_framer_ctx *hfc)
201{
202    hfc->hfc_header_ptr.frab = TAILQ_LAST(&hfc->hfc_fw->fw_fral.fl_frabs, frame_buf_head);
203    hfc->hfc_header_ptr.off = hfc->hfc_header_ptr.frab->frab_size;
204}
205
206
207static void
208hfc_terminate_frame (struct header_framer_ctx *hfc,
209                     enum http_frame_header_flags flags)
210{
211    union {
212        struct http_frame_header fh;
213        unsigned char            buf[ sizeof(struct http_frame_header) ];
214    } u;
215    uint32_t stream_id;
216    struct frame_buf *frab;
217
218    /* Construct the frame */
219    u.fh.hfh_length[0] = hfc->hfc_cur_sz >> 16;
220    u.fh.hfh_length[1] = hfc->hfc_cur_sz >> 8;
221    u.fh.hfh_length[2] = hfc->hfc_cur_sz;
222    u.fh.hfh_flags     = flags;
223    if (1 == hfc->hfc_n_frames)
224    {
225        u.fh.hfh_type  = hfc->hfc_frame_type;
226        u.fh.hfh_flags |= hfc->hfc_first_flags;
227    }
228    else
229        u.fh.hfh_type  = HTTP_FRAME_CONTINUATION;
230    stream_id = htonl(hfc->hfc_stream_id);
231    memcpy(u.fh.hfh_stream_id, &stream_id, sizeof(stream_id));
232
233    if (hfc->hfc_header_ptr.off >= sizeof(u.fh))
234    {   /* Write in a single chunk */
235        assert(0 == memcmp("123456789", hfc->hfc_header_ptr.frab->frab_buf +
236                    hfc->hfc_header_ptr.off - sizeof(u.buf), sizeof(u.buf)));
237        memcpy(hfc->hfc_header_ptr.frab->frab_buf + hfc->hfc_header_ptr.off -
238                    sizeof(u.buf), u.buf, sizeof(u.buf));
239    }
240    else
241    {   /* Write across frab boundary */
242        memcpy(hfc->hfc_header_ptr.frab->frab_buf,
243            u.buf + sizeof(u.buf) - hfc->hfc_header_ptr.off,
244            hfc->hfc_header_ptr.off);
245        frab = TAILQ_PREV(hfc->hfc_header_ptr.frab, frame_buf_head, frab_next);
246        memcpy(frab->frab_buf + frab->frab_size - sizeof(u.buf) +
247            hfc->hfc_header_ptr.off, u.buf,
248            sizeof(u.buf) - hfc->hfc_header_ptr.off);
249    }
250}
251
252
253static int
254hfc_write (struct header_framer_ctx *hfc, const void *buf, size_t sz)
255{
256    const unsigned char *p = buf;
257    unsigned avail;
258    int s;
259
260    while (sz > 0)
261    {
262        if (hfc->hfc_max_frame_sz == hfc->hfc_cur_sz)
263        {
264            if (hfc->hfc_n_frames > 0)
265                hfc_terminate_frame(hfc, 0);
266            s = lsquic_frab_list_write(&hfc->hfc_fw->fw_fral, "123456789",
267                                        sizeof(struct http_frame_header));
268            if (s < 0)
269                return s;
270            ++hfc->hfc_n_frames;
271            hfc_save_ptr(hfc);
272            hfc->hfc_cur_sz = 0;
273        }
274
275        avail = hfc->hfc_max_frame_sz - hfc->hfc_cur_sz;
276        if (sz < avail)
277            avail = sz;
278        if (avail)
279        {
280            s = lsquic_frab_list_write(&hfc->hfc_fw->fw_fral, p, avail);
281            if (s < 0)
282                return s;
283            hfc->hfc_cur_sz += avail;
284            sz -= avail;
285            p += avail;
286        }
287    }
288
289    return 0;
290}
291
292
293static unsigned
294count_uppercase (const unsigned char *buf, size_t sz)
295{
296    static const unsigned char uppercase[0x100] = {
297        ['A'] = 1, ['B'] = 1, ['C'] = 1, ['D'] = 1, ['E'] = 1, ['F'] = 1,
298        ['G'] = 1, ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, ['L'] = 1,
299        ['M'] = 1, ['N'] = 1, ['O'] = 1, ['P'] = 1, ['Q'] = 1, ['R'] = 1,
300        ['S'] = 1, ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, ['X'] = 1,
301        ['Y'] = 1, ['Z'] = 1,
302    };
303    unsigned n_uppercase, i;
304    n_uppercase = 0;
305    for (i = 0; i < sz; ++i)
306        n_uppercase += uppercase[ buf[i] ];
307    return n_uppercase;
308}
309
310
311static uint32_t
312calc_headers_size (const struct lsquic_http_headers *headers)
313{
314    int i;
315    uint32_t size = 0;
316    for (i = 0; i < headers->count; ++i)
317        if (headers->headers[i].buf)
318            size += 32 + headers->headers[i].name_len +
319                         headers->headers[i].val_len;
320    return size;
321}
322
323
324static int
325have_oversize_strings (const struct lsquic_http_headers *headers)
326{
327#if LSXPACK_MAX_STRLEN > LSHPACK_MAX_STRLEN
328    int i, have;
329    for (i = 0, have = 0; i < headers->count; ++i)
330    {
331        if (headers->headers[i].buf)
332        {
333            have |= headers->headers[i].name_len > LSHPACK_MAX_STRLEN;
334            have |= headers->headers[i].val_len > LSHPACK_MAX_STRLEN;
335        }
336    }
337    return have;
338#else
339    return 0;
340#endif
341}
342
343
344static int
345check_headers_size (const struct lsquic_frame_writer *fw,
346                    const struct lsquic_http_headers *headers)
347{
348    uint32_t headers_sz;
349    headers_sz = calc_headers_size(headers);
350
351    if (headers_sz <= fw->fw_max_header_list_sz)
352        return 0;
353    else if (fw->fw_flags & FW_SERVER)
354    {
355        LSQ_INFO("Sending headers larger (%u bytes) than max allowed (%u)",
356            headers_sz, fw->fw_max_header_list_sz);
357        return 0;
358    }
359    else
360    {
361        LSQ_INFO("Headers size %u is larger than max allowed (%u)",
362            headers_sz, fw->fw_max_header_list_sz);
363        errno = EMSGSIZE;
364        return -1;
365    }
366}
367
368
369static int
370check_headers_case (const struct lsquic_frame_writer *fw,
371                    const struct lsquic_http_headers *headers)
372{
373    unsigned n_uppercase;
374    int i;
375    n_uppercase = 0;
376    for (i = 0; i < headers->count; ++i)
377        if (headers->headers[i].buf)
378            n_uppercase += count_uppercase((unsigned char *)
379                                lsxpack_header_get_name(&headers->headers[i]),
380                                headers->headers[i].name_len);
381    if (n_uppercase)
382    {
383        LSQ_INFO("Uppercase letters in header names");
384        errno = EINVAL;
385        return -1;
386    }
387    return 0;
388}
389
390
391static int
392write_headers (struct lsquic_frame_writer *fw,
393               const struct lsquic_http_headers *headers,
394               struct header_framer_ctx *hfc, unsigned char *buf,
395               const unsigned buf_sz)
396{
397    unsigned char *end;
398    int i, s;
399    for (i = 0; i < headers->count; ++i)
400    {
401        if (headers->headers[i].buf == NULL)
402            continue;
403        end = lshpack_enc_encode(fw->fw_henc, buf, buf + buf_sz,
404                                                    &headers->headers[i]);
405        if (end > buf)
406        {
407            s = hfc_write(hfc, buf, end - buf);
408            if (s < 0)
409                return s;
410#if LSQUIC_CONN_STATS
411            fw->fw_conn_stats->out.headers_uncomp +=
412                headers->headers[i].name_len
413                    + headers->headers[i].val_len;
414            fw->fw_conn_stats->out.headers_comp += end - buf;
415#endif
416        }
417        else
418        {
419            /* Ignore errors, matching HTTP2 behavior in our server code */
420        }
421    }
422
423    return 0;
424}
425
426
427int
428lsquic_frame_writer_write_headers (struct lsquic_frame_writer *fw,
429                                   lsquic_stream_id_t stream_id,
430                                   const struct lsquic_http_headers *headers,
431                                   int eos, unsigned weight)
432{
433    struct header_framer_ctx hfc;
434    int s;
435    struct http_prio_frame prio_frame;
436    enum http_frame_header_flags flags;
437    unsigned char *buf;
438
439    /* Internal function: weight must be valid here */
440    assert(weight >= 1 && weight <= 256);
441
442    if (fw->fw_max_header_list_sz && 0 != check_headers_size(fw, headers))
443        return -1;
444
445    if (0 != check_headers_case(fw, headers))
446        return -1;
447
448    if (have_oversize_strings(headers))
449        return -1;
450
451    if (eos)
452        flags = HFHF_END_STREAM;
453    else
454        flags = 0;
455
456    if (!(fw->fw_flags & FW_SERVER))
457        flags |= HFHF_PRIORITY;
458
459    hfc_init(&hfc, fw, fw->fw_max_frame_sz, HTTP_FRAME_HEADERS, stream_id,
460                                                                        flags);
461
462    if (!(fw->fw_flags & FW_SERVER))
463    {
464        memset(&prio_frame.hpf_stream_id, 0, sizeof(prio_frame.hpf_stream_id));
465        prio_frame.hpf_weight = weight - 1;
466        s = hfc_write(&hfc, &prio_frame, sizeof(struct http_prio_frame));
467        if (s < 0)
468            return s;
469    }
470
471    buf = malloc(MAX_COMP_HEADER_FIELD_SIZE);
472    if (!buf)
473        return -1;
474    s = write_headers(fw, headers, &hfc, buf, MAX_COMP_HEADER_FIELD_SIZE);
475    free(buf);
476    if (0 == s)
477    {
478        EV_LOG_GENERATED_HTTP_HEADERS(LSQUIC_LOG_CONN_ID, stream_id,
479                            fw->fw_flags & FW_SERVER, &prio_frame, headers);
480        hfc_terminate_frame(&hfc, HFHF_END_HEADERS);
481        return lsquic_frame_writer_flush(fw);
482    }
483    else
484        return s;
485}
486
487
488int
489lsquic_frame_writer_write_promise (struct lsquic_frame_writer *fw,
490    lsquic_stream_id_t stream_id64, lsquic_stream_id_t promised_stream_id64,
491    const struct lsquic_http_headers *headers)
492{
493    uint32_t stream_id = stream_id64;
494    uint32_t promised_stream_id = promised_stream_id64;
495    struct header_framer_ctx hfc;
496    struct http_push_promise_frame push_frame;
497    unsigned char *buf;
498    int s;
499
500    if (fw->fw_max_header_list_sz && 0 != check_headers_size(fw, headers))
501        return -1;
502
503    if (0 != check_headers_case(fw, headers))
504        return -1;
505
506    if (have_oversize_strings(headers))
507        return -1;
508
509    hfc_init(&hfc, fw, fw->fw_max_frame_sz, HTTP_FRAME_PUSH_PROMISE,
510                                                            stream_id, 0);
511
512    promised_stream_id = htonl(promised_stream_id);
513    memcpy(push_frame.hppf_promised_id, &promised_stream_id, 4);
514    s = hfc_write(&hfc, &push_frame, sizeof(struct http_push_promise_frame));
515    if (s < 0)
516        return s;
517
518    buf = malloc(MAX_COMP_HEADER_FIELD_SIZE);
519    if (!buf)
520        return -1;
521
522    s = write_headers(fw, headers, &hfc, buf, MAX_COMP_HEADER_FIELD_SIZE);
523    if (s != 0)
524    {
525        free(buf);
526        return -1;
527    }
528
529    free(buf);
530
531    if (0 == s)
532    {
533        EV_LOG_GENERATED_HTTP_PUSH_PROMISE(LSQUIC_LOG_CONN_ID, stream_id,
534                            htonl(promised_stream_id), headers);
535        hfc_terminate_frame(&hfc, HFHF_END_HEADERS);
536        return lsquic_frame_writer_flush(fw);
537    }
538    else
539        return -1;
540}
541
542
543void
544lsquic_frame_writer_max_header_list_size (struct lsquic_frame_writer *fw,
545                                          uint32_t max_size)
546{
547    LSQ_DEBUG("set max_header_list_sz to %u", max_size);
548    fw->fw_max_header_list_sz = max_size;
549}
550
551
552static int
553write_settings (struct lsquic_frame_writer *fw,
554    const struct lsquic_http2_setting *settings, unsigned n_settings)
555{
556    struct http_frame_header fh;
557    unsigned payload_length;
558    uint32_t val;
559    uint16_t id;
560    int s;
561
562    payload_length = n_settings * 6;
563
564    memset(&fh, 0, sizeof(fh));
565    fh.hfh_type  = HTTP_FRAME_SETTINGS;
566    fh.hfh_length[0] = payload_length >> 16;
567    fh.hfh_length[1] = payload_length >> 8;
568    fh.hfh_length[2] = payload_length;
569
570    s = lsquic_frab_list_write(&fw->fw_fral, &fh, sizeof(fh));
571    if (s != 0)
572        return s;
573
574    do
575    {
576        id  = htons(settings->id);
577        val = htonl(settings->value);
578        if (0 != (s = lsquic_frab_list_write(&fw->fw_fral, &id, sizeof(id))) ||
579            0 != (s = lsquic_frab_list_write(&fw->fw_fral, &val, sizeof(val))))
580            return s;
581        EV_LOG_CONN_EVENT(LSQUIC_LOG_CONN_ID, "wrote HTTP SETTINGS frame: "
582            "%s=%"PRIu32, lsquic_http_setting_id2str(settings->id),
583            settings->value);
584        ++settings;
585    }
586    while (--n_settings);
587
588    return 0;
589}
590
591int
592lsquic_frame_writer_write_settings (struct lsquic_frame_writer *fw,
593    const struct lsquic_http2_setting *settings, unsigned n_settings)
594{
595    unsigned settings_per_frame;
596    unsigned n;
597
598    if (0 == n_settings)
599    {
600        errno = EINVAL;
601        return -1;
602    }
603
604    settings_per_frame = fw->fw_max_frame_sz / SETTINGS_FRAME_SZ;
605    n = 0;
606
607    do {
608        if (settings_per_frame > n_settings - n)
609            settings_per_frame = n_settings - n;
610        if (0 != write_settings(fw, &settings[n], settings_per_frame))
611            return -1;
612        n += settings_per_frame;
613    } while (n < n_settings);
614
615    return lsquic_frame_writer_flush(fw);
616}
617
618
619int
620lsquic_frame_writer_write_priority (struct lsquic_frame_writer *fw,
621        lsquic_stream_id_t stream_id64, int exclusive,
622        lsquic_stream_id_t stream_dep_id64, unsigned weight)
623{
624    uint32_t stream_id = stream_id64;
625    uint32_t stream_dep_id = stream_dep_id64;
626    unsigned char buf[ sizeof(struct http_frame_header) +
627                                        sizeof(struct http_prio_frame) ];
628    struct http_frame_header *fh         = (void *) &buf[0];
629    struct http_prio_frame   *prio_frame = (void *) &buf[sizeof(*fh)];
630    int s;
631
632    if (stream_dep_id & (1UL << 31))
633    {
634        LSQ_WARN("stream ID too high (%u): cannot write PRIORITY frame",
635            stream_dep_id);
636        return -1;
637    }
638
639    if (weight < 1 || weight > 256)
640        return -1;
641
642    memset(fh, 0, sizeof(*fh));
643    fh->hfh_type      = HTTP_FRAME_PRIORITY;
644    fh->hfh_length[2] = sizeof(struct http_prio_frame);
645    stream_id = htonl(stream_id);
646    memcpy(fh->hfh_stream_id, &stream_id, 4);
647
648    stream_dep_id |= !!exclusive << 31;
649    stream_id = htonl(stream_dep_id);
650    memcpy(prio_frame->hpf_stream_id, &stream_id, 4);
651    prio_frame->hpf_weight = weight - 1;
652
653    s = lsquic_frab_list_write(&fw->fw_fral, buf, sizeof(buf));
654    if (s != 0)
655        return s;
656
657    EV_LOG_CONN_EVENT(LSQUIC_LOG_CONN_ID, "wrote HTTP PRIORITY frame: "
658        "stream %"PRIu32"; weight: %u; exclusive: %d",
659        htonl(stream_id), weight, !!exclusive);
660
661    return lsquic_frame_writer_flush(fw);
662}
663
664
665size_t
666lsquic_frame_writer_mem_used (const struct lsquic_frame_writer *fw)
667{
668    size_t size;
669
670    size = sizeof(*fw)
671         + lsquic_frab_list_mem_used(&fw->fw_fral)
672         - sizeof(fw->fw_fral);
673
674    return size;
675}
676