lsquic_di_hash.c revision b62ec17f
1/* Copyright (c) 2017 - 2020 LiteSpeed Technologies Inc.  See LICENSE. */
2/*
3 * lsquic_di_hash.c -- Copy incoming data into a hash
4 *
5 * While this implementation copies the data, its memory use is limited,
6 * which makes it a good choice when we have a lot of stream frames
7 * coming in.
8 *
9 * Another difference is that incoming STREAM frames are allowed to overlap.
10 */
11
12
13#include <assert.h>
14#include <inttypes.h>
15#include <stddef.h>
16#include <stdint.h>
17#include <stdlib.h>
18#include <string.h>
19#include <sys/queue.h>
20
21#include "lsquic.h"
22#include "lsquic_int_types.h"
23#include "lsquic_types.h"
24#include "lsquic_conn_flow.h"
25#include "lsquic_packet_common.h"
26#include "lsquic_packet_in.h"
27#include "lsquic_rtt.h"
28#include "lsquic_sfcw.h"
29#include "lsquic_varint.h"
30#include "lsquic_hq.h"
31#include "lsquic_hash.h"
32#include "lsquic_stream.h"
33#include "lsquic_mm.h"
34#include "lsquic_malo.h"
35#include "lsquic_conn.h"
36#include "lsquic_conn_public.h"
37#include "lsquic_data_in_if.h"
38
39
40#define LSQUIC_LOGGER_MODULE LSQLM_DI
41#define LSQUIC_LOG_CONN_ID lsquic_conn_log_cid(hdi->hdi_conn_pub->lconn)
42#define LSQUIC_LOG_STREAM_ID hdi->hdi_stream_id
43#include "lsquic_logger.h"
44
45
46#define N_DB_SETS 57
47
48#define DB_DATA_SIZE (0x1000 - sizeof(TAILQ_ENTRY(data_block)) - \
49                                sizeof(uint64_t) - N_DB_SETS * sizeof(uint64_t))
50
51struct data_block
52{
53    TAILQ_ENTRY(data_block) db_next;
54    uint64_t                db_off;
55    uint64_t                db_set[N_DB_SETS];  /* bit for each valid byte */
56    unsigned char           db_data[DB_DATA_SIZE];
57};
58
59typedef char db_set_covers_all_db_data[(N_DB_SETS * 64 >= DB_DATA_SIZE) ?1: - 1];
60typedef char db_set_no_waste[(N_DB_SETS * 64 - 64 <= DB_DATA_SIZE)?1: - 1];
61typedef char db_block_is_4K[(sizeof(struct data_block) == 0x1000) ?1:- 1];
62
63
64TAILQ_HEAD(dblock_head, data_block);
65
66
67static const struct data_in_iface *di_if_hash_ptr;
68
69
70struct hash_data_in
71{
72    struct data_in              hdi_data_in;
73    struct lsquic_conn_public  *hdi_conn_pub;
74    uint64_t                    hdi_fin_off;
75    struct dblock_head         *hdi_buckets;
76    struct data_block          *hdi_last_block;
77    struct data_frame           hdi_data_frame;
78    lsquic_stream_id_t          hdi_stream_id;
79    unsigned                    hdi_count;
80    unsigned                    hdi_nbits;
81    enum {
82            HDI_FIN = (1 << 0),
83    }                           hdi_flags;
84};
85
86
87#define HDI_PTR(data_in) (struct hash_data_in *) \
88    ((unsigned char *) (data_in) - offsetof(struct hash_data_in, hdi_data_in))
89
90
91#define N_BUCKETS(n_bits) (1U << (n_bits))
92#define BUCKNO(n_bits, off) ((off / DB_DATA_SIZE) & (N_BUCKETS(n_bits) - 1))
93
94
95static unsigned
96my_log2 /* silly name to suppress compiler warning */ (unsigned sz)
97{
98#if __GNUC__
99    unsigned clz = __builtin_clz(sz);
100    return 32 - clz;
101#else
102    unsigned clz;
103    size_t y;
104    clz = 32;
105    y = sz >> 16;   if (y) { clz -= 16; sz = y; }
106    y = sz >>  8;   if (y) { clz -=  8; sz = y; }
107    y = sz >>  4;   if (y) { clz -=  4; sz = y; }
108    y = sz >>  2;   if (y) { clz -=  2; sz = y; }
109    y = sz >>  1;   if (y) return 32 - clz + 1;
110    return 32 - clz + sz;
111#endif
112}
113
114
115struct data_in *
116lsquic_data_in_hash_new (struct lsquic_conn_public *conn_pub,
117                        lsquic_stream_id_t stream_id, uint64_t byteage)
118{
119    struct hash_data_in *hdi;
120    unsigned n;
121
122    hdi = malloc(sizeof(*hdi));
123    if (!hdi)
124        return NULL;
125
126    hdi->hdi_data_in.di_if    = di_if_hash_ptr;
127    hdi->hdi_data_in.di_flags = 0;
128    hdi->hdi_conn_pub         = conn_pub;
129    hdi->hdi_stream_id        = stream_id;
130    hdi->hdi_fin_off          = 0;
131    hdi->hdi_flags            = 0;
132    hdi->hdi_last_block       = NULL;
133    if (byteage >= DB_DATA_SIZE /* __builtin_clz is undefined if
134                                   argument is 0 */)
135        hdi->hdi_nbits        = my_log2(byteage / DB_DATA_SIZE) + 2;
136    else
137        hdi->hdi_nbits        = 3;
138    hdi->hdi_count            = 0;
139    hdi->hdi_buckets          = malloc(sizeof(hdi->hdi_buckets[0]) *
140                                                    N_BUCKETS(hdi->hdi_nbits));
141    if (!hdi->hdi_buckets)
142    {
143        free(hdi);
144        return NULL;
145    }
146
147    for (n = 0; n < N_BUCKETS(hdi->hdi_nbits); ++n)
148        TAILQ_INIT(&hdi->hdi_buckets[n]);
149
150    return &hdi->hdi_data_in;
151}
152
153
154static void
155hash_di_destroy (struct data_in *data_in)
156{
157    struct hash_data_in *const hdi = HDI_PTR(data_in);
158    struct data_block *block;
159    unsigned n;
160
161    for (n = 0; n < N_BUCKETS(hdi->hdi_nbits); ++n)
162    {
163        while ((block = TAILQ_FIRST(&hdi->hdi_buckets[n])))
164        {
165            TAILQ_REMOVE(&hdi->hdi_buckets[n], block, db_next);
166            free(block);
167        }
168    }
169    free(hdi->hdi_buckets);
170    free(hdi);
171}
172
173
174static int
175hash_grow (struct hash_data_in *hdi)
176{
177    struct dblock_head *new_buckets, *new[2];
178    struct data_block *block;
179    unsigned n, old_nbits;
180    int idx;
181
182    old_nbits = hdi->hdi_nbits;
183    LSQ_DEBUG("doubling number of buckets to %u", N_BUCKETS(old_nbits + 1));
184    new_buckets = malloc(sizeof(hdi->hdi_buckets[0])
185                                                * N_BUCKETS(old_nbits + 1));
186    if (!new_buckets)
187    {
188        LSQ_WARN("malloc failed: potential trouble ahead");
189        return -1;
190    }
191
192    for (n = 0; n < N_BUCKETS(old_nbits); ++n)
193    {
194        new[0] = &new_buckets[n];
195        new[1] = &new_buckets[n + N_BUCKETS(old_nbits)];
196        TAILQ_INIT(new[0]);
197        TAILQ_INIT(new[1]);
198        while ((block = TAILQ_FIRST(&hdi->hdi_buckets[n])))
199        {
200            TAILQ_REMOVE(&hdi->hdi_buckets[n], block, db_next);
201            idx = (BUCKNO(old_nbits + 1, block->db_off) >> old_nbits) & 1;
202            TAILQ_INSERT_TAIL(new[idx], block, db_next);
203        }
204    }
205    free(hdi->hdi_buckets);
206    hdi->hdi_nbits   = old_nbits + 1;
207    hdi->hdi_buckets = new_buckets;
208    return 0;
209}
210
211
212static int
213hash_insert (struct hash_data_in *hdi, struct data_block *block)
214{
215    unsigned buckno;
216
217    if (hdi->hdi_count >= N_BUCKETS(hdi->hdi_nbits) / 2 && 0 != hash_grow(hdi))
218        return -1;
219
220    buckno = BUCKNO(hdi->hdi_nbits, block->db_off);
221    TAILQ_INSERT_TAIL(&hdi->hdi_buckets[buckno], block, db_next);
222    ++hdi->hdi_count;
223    return 0;
224}
225
226
227static struct data_block *
228hash_find (const struct hash_data_in *hdi, uint64_t off)
229{
230    struct data_block *block;
231    unsigned buckno;
232
233    buckno = BUCKNO(hdi->hdi_nbits, off);
234    TAILQ_FOREACH(block, &hdi->hdi_buckets[buckno], db_next)
235        if (off == block->db_off)
236            return block;
237    return NULL;
238}
239
240
241static void
242hash_remove (struct hash_data_in *hdi, struct data_block *block)
243{
244    unsigned buckno;
245
246    buckno = BUCKNO(hdi->hdi_nbits, block->db_off);
247    TAILQ_REMOVE(&hdi->hdi_buckets[buckno], block, db_next);
248    --hdi->hdi_count;
249}
250
251
252static struct data_block *
253new_block (struct hash_data_in *hdi, uint64_t off)
254{
255    struct data_block *block;
256
257    assert(0 == off % DB_DATA_SIZE);
258
259    block = malloc(sizeof(*block));
260    if (!block)
261        return NULL;
262
263    block->db_off = off;
264    if (0 != hash_insert(hdi, block))
265    {
266        free(block);
267        return NULL;
268    }
269
270    memset(block->db_set, 0, sizeof(block->db_set));
271    return block;
272}
273
274
275static unsigned
276block_write (struct data_block *block, unsigned block_off,
277                             const unsigned char *data, unsigned data_sz)
278{
279    const unsigned char *begin, *end;
280    unsigned set, bit, n_full_sets, n;
281    uint64_t mask;
282
283    assert(block_off < DB_DATA_SIZE);
284    if (data_sz > DB_DATA_SIZE - block_off)
285        data_sz = DB_DATA_SIZE - block_off;
286
287    begin = data;
288    end = begin + data_sz;
289    set = block_off >> 6;
290    bit = block_off & 0x3F;
291
292    assert(set < N_DB_SETS);
293
294    if (bit)
295    {
296        n = 64 - bit;
297        if (n > data_sz)
298            n = data_sz;
299        mask = ~((1ULL <<  bit     ) - 1)
300             &  ((1ULL << (bit + n - 1)) | ((1ULL << (bit + n - 1)) - 1));
301        block->db_set[ set ] |= mask;
302        memcpy(block->db_data + block_off, data, n);
303        data      += n;
304        block_off += n;
305        ++set;
306    }
307
308    n_full_sets = (end - data) >> 6;
309    if (n_full_sets)
310    {
311        memcpy(block->db_data + block_off, data, n_full_sets * 64);
312        data      += n_full_sets * 64;
313        block_off += n_full_sets * 64;
314        memset(&block->db_set[ set ], 0xFF, n_full_sets * 8);
315        set += n_full_sets;
316    }
317
318    if (data < end)
319    {
320        assert(end - data < 64);
321        block->db_set[ set ] |= ((1ULL << (end - data)) - 1);
322        memcpy(block->db_data + block_off, data, end - data);
323        data = end;
324    }
325
326    assert(set <= N_DB_SETS);
327
328    return data - begin;
329}
330
331
332static int
333has_bytes_after (const struct data_block *block, unsigned off)
334{
335    unsigned bit, set;
336    int has;
337
338    set = off >> 6;
339    bit = off & 0x3F;
340
341    has = 0 != (block->db_set[ set ] >> bit);
342    ++set;
343
344    for ( ; set < N_DB_SETS; ++set)
345        has += 0 != block->db_set[ set ];
346
347    return has > 0;
348}
349
350
351enum ins_frame
352lsquic_data_in_hash_insert_data_frame (struct data_in *data_in,
353                const struct data_frame *data_frame, uint64_t read_offset)
354{
355    struct hash_data_in *const hdi = HDI_PTR(data_in);
356    struct data_block *block;
357    uint64_t key, off, diff, fin_off;
358    const unsigned char *data;
359    unsigned size, nw;
360
361    if (data_frame->df_offset + data_frame->df_size < read_offset)
362    {
363        if (data_frame->df_fin)
364            return INS_FRAME_ERR;
365        else
366            return INS_FRAME_DUP;
367    }
368
369    if ((hdi->hdi_flags & HDI_FIN) &&
370         (
371          (data_frame->df_fin &&
372             data_frame->df_offset + data_frame->df_size != hdi->hdi_fin_off)
373          ||
374          data_frame->df_offset + data_frame->df_size > hdi->hdi_fin_off
375         )
376       )
377    {
378        return INS_FRAME_ERR;
379    }
380
381    if (data_frame->df_offset < read_offset)
382    {
383        diff = read_offset - data_frame->df_offset;
384        assert(diff <= data_frame->df_size);
385        size = data_frame->df_size   - diff;
386        off  = data_frame->df_offset + diff;
387        data = data_frame->df_data   + diff;
388    }
389    else
390    {
391        size = data_frame->df_size;
392        off  = data_frame->df_offset;
393        data = data_frame->df_data;
394    }
395
396    key = off - (off % DB_DATA_SIZE);
397    do
398    {
399        block = hash_find(hdi, key);
400        if (!block)
401        {
402            block = new_block(hdi, key);
403            if (!block)
404                return INS_FRAME_ERR;
405        }
406        nw = block_write(block, off % DB_DATA_SIZE, data, size);
407        size -= nw;
408        off  += nw;
409        data += nw;
410        key  += DB_DATA_SIZE;
411    }
412    while (size > 0);
413
414    if (data_frame->df_fin)
415    {
416        fin_off = data_frame->df_offset + data_frame->df_size;
417        if (has_bytes_after(block, fin_off - block->db_off) ||
418                                                        hash_find(hdi, key))
419        {
420            return INS_FRAME_ERR;
421        }
422        hdi->hdi_flags  |= HDI_FIN;
423        hdi->hdi_fin_off = fin_off;
424    }
425
426    return INS_FRAME_OK;
427}
428
429
430static enum ins_frame
431hash_di_insert_frame (struct data_in *data_in,
432                        struct stream_frame *new_frame, uint64_t read_offset)
433{
434    struct hash_data_in *const hdi = HDI_PTR(data_in);
435    const struct data_frame *const data_frame = &new_frame->data_frame;
436    enum ins_frame ins;
437
438    ins = lsquic_data_in_hash_insert_data_frame(data_in, data_frame,
439                                                                read_offset);
440    assert(ins != INS_FRAME_OVERLAP);
441    lsquic_packet_in_put(hdi->hdi_conn_pub->mm, new_frame->packet_in);
442    if (ins != INS_FRAME_OK)
443        lsquic_malo_put(new_frame);
444    return ins;
445}
446
447
448#if __GNUC__
449#   define ctz __builtin_ctzll
450#else
451static unsigned
452ctz (unsigned long long x)
453{
454    unsigned n = 0;
455    if (0 == (x & ((1ULL << 32) - 1))) { n += 32; x >>= 32; }
456    if (0 == (x & ((1ULL << 16) - 1))) { n += 16; x >>= 16; }
457    if (0 == (x & ((1ULL <<  8) - 1))) { n +=  8; x >>=  8; }
458    if (0 == (x & ((1ULL <<  4) - 1))) { n +=  4; x >>=  4; }
459    if (0 == (x & ((1ULL <<  2) - 1))) { n +=  2; x >>=  2; }
460    if (0 == (x & ((1ULL <<  1) - 1))) { n +=  1; x >>=  1; }
461    return n;
462}
463#endif
464
465
466static unsigned
467n_avail_bytes (const struct data_block *block, unsigned set, unsigned bit)
468{
469    unsigned count;
470    uint64_t part;
471
472    part = ~(block->db_set[ set ] >> bit);
473    if (part)
474    {
475        count = ctz(part);
476        if (count < 64 - bit)
477            return count;
478    }
479    else
480        count = 64;
481    ++set;
482
483    for ( ; set < N_DB_SETS && ~0ULL == block->db_set[ set ]; ++set)
484        count += 64;
485
486    if (set < N_DB_SETS)
487    {
488        part = ~block->db_set[ set ];
489        if (part)
490            count += ctz(part);
491        else
492            count += 64;
493    }
494
495    return count;
496}
497
498
499/* Data block is readable if there is at least one readable byte at
500 * `read_offset' or there is FIN at that offset.
501 */
502static int
503setup_data_frame (struct hash_data_in *hdi, const uint64_t read_offset,
504                                                    struct data_block *block)
505{
506    unsigned set, bit;
507    uint64_t offset;
508
509    offset = read_offset % DB_DATA_SIZE;
510    set = offset >> 6;
511    bit = offset & 0x3F;
512
513    if (block->db_set[ set ] & (1ULL << bit))
514    {
515        hdi->hdi_last_block             = block;
516        hdi->hdi_data_frame.df_data     = block->db_data;
517        hdi->hdi_data_frame.df_offset   = block->db_off;
518        hdi->hdi_data_frame.df_read_off = offset;
519        hdi->hdi_data_frame.df_size     = offset +
520                                                n_avail_bytes(block, set, bit);
521        hdi->hdi_data_frame.df_fin      =
522            (hdi->hdi_flags & HDI_FIN) &&
523                hdi->hdi_data_frame.df_read_off +
524                    hdi->hdi_data_frame.df_size == hdi->hdi_fin_off;
525        return 1;
526    }
527    else if ((hdi->hdi_flags & HDI_FIN) && read_offset == hdi->hdi_fin_off)
528    {
529        hdi->hdi_last_block             = block;
530        hdi->hdi_data_frame.df_data     = NULL;
531        hdi->hdi_data_frame.df_offset   = block->db_off;
532        hdi->hdi_data_frame.df_read_off = offset;
533        hdi->hdi_data_frame.df_size     = offset;
534        hdi->hdi_data_frame.df_fin      = 1;
535        return 1;
536    }
537    else
538        return 0;
539}
540
541
542static struct data_frame *
543hash_di_get_frame (struct data_in *data_in, uint64_t read_offset)
544{
545    struct hash_data_in *const hdi = HDI_PTR(data_in);
546    struct data_block *block;
547    uint64_t key;
548
549    key = read_offset - (read_offset % DB_DATA_SIZE);
550    block = hash_find(hdi, key);
551    if (!block)
552    {
553        if ((hdi->hdi_flags & HDI_FIN) && read_offset == hdi->hdi_fin_off)
554        {
555            hdi->hdi_last_block             = NULL;
556            hdi->hdi_data_frame.df_data     = NULL;
557            hdi->hdi_data_frame.df_offset   = read_offset -
558                                                    read_offset % DB_DATA_SIZE;
559            hdi->hdi_data_frame.df_read_off = 0;
560            hdi->hdi_data_frame.df_size     = 0;
561            hdi->hdi_data_frame.df_fin      = 1;
562            return &hdi->hdi_data_frame;
563        }
564        else
565            return NULL;
566    }
567
568    if (setup_data_frame(hdi, read_offset, block))
569        return &hdi->hdi_data_frame;
570    else
571        return NULL;
572}
573
574
575static void
576hash_di_frame_done (struct data_in *data_in, struct data_frame *data_frame)
577{
578    struct hash_data_in *const hdi = HDI_PTR(data_in);
579    struct data_block *const block = hdi->hdi_last_block;
580
581    if (block)
582    {
583        if (data_frame->df_read_off == DB_DATA_SIZE ||
584                            !has_bytes_after(block, data_frame->df_read_off))
585        {
586            hash_remove(hdi, block);
587            free(block);
588            if (0 == hdi->hdi_count && 0 == (hdi->hdi_flags & HDI_FIN))
589            {
590                LSQ_DEBUG("hash empty, want to switch");
591                hdi->hdi_data_in.di_flags |= DI_SWITCH_IMPL;
592            }
593        }
594    }
595    else
596        assert(data_frame->df_fin && data_frame->df_size == 0);
597}
598
599
600static int
601hash_di_empty (struct data_in *data_in)
602{
603    struct hash_data_in *const hdi = HDI_PTR(data_in);
604    return hdi->hdi_count == 0;
605}
606
607
608static struct data_in *
609hash_di_switch_impl (struct data_in *data_in, uint64_t read_offset)
610{
611    struct hash_data_in *const hdi = HDI_PTR(data_in);
612    struct data_in *new_data_in;
613
614    assert(hdi->hdi_count == 0);
615
616    new_data_in = lsquic_data_in_nocopy_new(hdi->hdi_conn_pub,
617                                                    hdi->hdi_stream_id);
618    data_in->di_if->di_destroy(data_in);
619
620    return new_data_in;
621}
622
623
624static size_t
625hash_di_mem_used (struct data_in *data_in)
626{
627    struct hash_data_in *const hdi = HDI_PTR(data_in);
628    const struct data_block *block;
629    size_t size;
630    unsigned n;
631
632    size = sizeof(*data_in);
633
634    for (n = 0; n < N_BUCKETS(hdi->hdi_nbits); ++n)
635        TAILQ_FOREACH(block, &hdi->hdi_buckets[n], db_next)
636            size += sizeof(*block);
637
638    size += N_BUCKETS(hdi->hdi_nbits) * sizeof(hdi->hdi_buckets[0]);
639
640    return size;
641}
642
643
644static void
645hash_di_dump_state (struct data_in *data_in)
646{
647    const struct hash_data_in *const hdi = HDI_PTR(data_in);
648    const struct data_block *block;
649    unsigned n;
650
651    LSQ_DEBUG("hash state: flags: %X; fin off: %"PRIu64"; count: %u",
652        hdi->hdi_flags, hdi->hdi_fin_off, hdi->hdi_count);
653    for (n = 0; n < N_BUCKETS(hdi->hdi_nbits); ++n)
654        TAILQ_FOREACH(block, &hdi->hdi_buckets[n], db_next)
655            LSQ_DEBUG("block: off: %"PRIu64, block->db_off);
656}
657
658
659static uint64_t
660hash_di_readable_bytes (struct data_in *data_in, uint64_t read_offset)
661{
662    const struct data_frame *data_frame;
663    uint64_t starting_offset;
664
665    starting_offset = read_offset;
666    while (data_frame = hash_di_get_frame(data_in, read_offset),
667                data_frame && data_frame->df_size - data_frame->df_read_off)
668        read_offset += data_frame->df_size - data_frame->df_read_off;
669
670    return read_offset - starting_offset;
671}
672
673
674static const struct data_in_iface di_if_hash = {
675    .di_destroy      = hash_di_destroy,
676    .di_dump_state   = hash_di_dump_state,
677    .di_empty        = hash_di_empty,
678    .di_frame_done   = hash_di_frame_done,
679    .di_get_frame    = hash_di_get_frame,
680    .di_insert_frame = hash_di_insert_frame,
681    .di_mem_used     = hash_di_mem_used,
682    .di_own_on_ok    = 0,
683    .di_readable_bytes
684                     = hash_di_readable_bytes,
685    .di_switch_impl  = hash_di_switch_impl,
686};
687
688static const struct data_in_iface *di_if_hash_ptr = &di_if_hash;
689