litespeed-quic/test/unittests/test_elision.c
Dmitri Tikhonov c51ce3387f Latest changes
- [API Change] Sendfile-like functionality is gone.  The stream no
  longer opens files and deals with file descriptors.  (Among other
  things, this makes the code more portable.)  Three writing functions
  are provided:

    lsquic_stream_write
    lsquic_stream_writev
    lsquic_stream_writef    (NEW)

  lsquic_stream_writef() is given an abstract reader that has function
  pointers for size() and read() functions which the user can implement.
  This is the most flexible way.  lsquic_stream_write() and
  lsquic_stream_writev() are now both implemented as wrappers around
  lsquic_stream_writef().

- [OPTIMIZATION] When writing to stream, be it within or without the
  on_write() callback, place data directly into packet buffer,
  bypassing auxiliary data structures.  This reduces amount of memory
  required, for the amount of data that can be written is limited
  by the congestion window.

  To support writes outside the on_write() callback, we keep N
  outgoing packet buffers per connection which can be written to
  by any stream.  One half of these are reserved for the highest
  priority stream(s), the other half for all other streams.  This way,
  low-priority streams cannot write instead of high-priority streams
  and, on the other hand, low-priority streams get a chance to send
  their packets out.

  The algorithm is as follows:

  - When user writes to stream outside of the callback:
    - If this is the highest priority stream, place it onto the
      reserved N/2 queue or fail.
        (The actual size of this queue is dynamic -- MAX(N/2, CWND) --
         rather than N/2, allowing high-priority streams to write as
         much as can be sent.)
    - If the stream is not the highest priority, try to place the
      data onto the reserved N/2 queue or fail.
  - When tick occurs *and* more packets can be scheduled:
    - Transfer packets from the high N/2 queue to the scheduled
      queue.
    - If more scheduling is allowed:
      - Call on_write callbacks for highest-priority streams,
        placing resulting packets directly onto the scheduled queue.
    - If more scheduling is allowed:
      - Transfer packets from the low N/2 queue to the scheduled
        queue.
    - If more scheduling is allowed:
      - Call on_write callbacks for non-highest-priority streams,
        placing resulting packets directly onto the scheduled queue

  The number N is currently 20, but it could be varied based on
  resource usage.

- If stream is created due to incoming headers, make headers readable
  from on_new.

- Outgoing packets are no longer marked non-writeable to prevent placing
  more than one STREAM frame from the same stream into a single packet.
  This property is maintained via code flow and an explicit check.
  Packets for stream data are allocated using a special function.

- STREAM frame elision is cheaper, as we only perform it if a reset
  stream has outgoing packets referencing it.

- lsquic_packet_out_t is smaller, as stream_rec elements are now
  inside a union.
2017-10-31 09:35:58 -04:00

327 lines
12 KiB
C

/* Copyright (c) 2017 LiteSpeed Technologies Inc. See LICENSE. */
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/queue.h>
#include "lsquic.h"
#include "lsquic_int_types.h"
#include "lsquic_packet_common.h"
#include "lsquic_packet_out.h"
#include "lsquic_parse.h"
#include "lsquic_conn_flow.h"
#include "lsquic_sfcw.h"
#include "lsquic_stream.h"
#include "lsquic_types.h"
#include "lsquic_malo.h"
#include "lsquic_mm.h"
#include "lsquic_engine_public.h"
#include "lsquic_logger.h"
static const struct parse_funcs *const pf = select_pf_by_ver(LSQVER_037);
static struct {
char buf[0x100];
size_t bufsz;
uint64_t off;
} stream_contents;
void
setup_stream_contents (uint64_t off, const char *str)
{
stream_contents.bufsz = strlen(str);
stream_contents.off = off;
memcpy(stream_contents.buf, str, stream_contents.bufsz);
}
int
lsquic_stream_tosend_fin (const lsquic_stream_t *stream)
{
return 0;
}
uint64_t
lsquic_stream_tosend_offset (const lsquic_stream_t *stream)
{
return stream_contents.off;
}
size_t
lsquic_stream_tosend_read (lsquic_stream_t *stream, void *buf, size_t len,
int *reached_fin)
{
if (stream_contents.bufsz < len)
len = stream_contents.bufsz;
memcpy(buf, stream_contents.buf, len);
*reached_fin = lsquic_stream_tosend_fin(stream);
return len;
}
size_t
lsquic_stream_tosend_sz (const lsquic_stream_t *stream)
{
return stream_contents.bufsz;
}
void
lsquic_stream_acked (lsquic_stream_t *stream)
{
--stream->n_unacked;
}
static void
elide_single_stream_frame (void)
{
struct packet_out_srec_iter posi;
struct lsquic_engine_public enpub;
lsquic_stream_t streams[1];
lsquic_packet_out_t *packet_out;
int len, off = 0;
memset(streams, 0, sizeof(streams));
memset(&enpub, 0, sizeof(enpub));
lsquic_mm_init(&enpub.enp_mm);
packet_out = lsquic_mm_get_packet_out(&enpub.enp_mm, NULL, QUIC_MAX_PAYLOAD_SZ);
setup_stream_contents(123, "Dude, where is my car?");
len = pf->pf_gen_stream_frame(packet_out->po_data + packet_out->po_data_sz,
lsquic_packet_out_avail(packet_out),
streams[0].id, lsquic_stream_tosend_offset(&streams[0]),
(gsf_fin_f) lsquic_stream_tosend_fin,
(gsf_size_f) lsquic_stream_tosend_sz,
(gsf_read_f) lsquic_stream_tosend_read,
&streams[0]);
packet_out->po_data_sz += len;
packet_out->po_frame_types |= (1 << QUIC_FRAME_STREAM);
lsquic_packet_out_add_stream(packet_out, &enpub.enp_mm, &streams[0],
QUIC_FRAME_STREAM, off, len);
assert(1 == streams[0].n_unacked);
assert(posi_first(&posi, packet_out));
streams[0].stream_flags |= STREAM_RST_SENT;
lsquic_packet_out_elide_reset_stream_frames(packet_out, 0);
assert(0 == streams[0].n_unacked);
assert(0 == packet_out->po_frame_types);
assert(!posi_first(&posi, packet_out));
lsquic_packet_out_destroy(packet_out, &enpub);
lsquic_mm_cleanup(&enpub.enp_mm);
}
/* This test is more involved. We will construct the following packet:
*
* | ACK | STREAM A | STREAM B | STREAM C | RST A | STREAM D | STREAM E
*
* and elide STREAM A, STREAM C, and STREAM E to get
*
* | ACK | STREAM B | RST A | STREAM D |
*
* If `chop_regen' is set, ACK is dropped (this tests what happens when
* packet is resent).
*
* This should test most of the corner cases.
*/
static void
elide_three_stream_frames (int chop_regen)
{
struct packet_out_srec_iter posi;
struct lsquic_engine_public enpub;
lsquic_stream_t streams[5];
lsquic_packet_out_t *packet_out, *ref_out;
struct stream_rec *srec;
unsigned short b_off, d_off;
int len;
memset(streams, 0, sizeof(streams));
memset(&enpub, 0, sizeof(enpub));
lsquic_mm_init(&enpub.enp_mm);
/* First, we construct the reference packet. We will only use it to
* compare payload and sizes:
*/
{
ref_out = lsquic_mm_get_packet_out(&enpub.enp_mm, NULL, QUIC_MAX_PAYLOAD_SZ);
/* This is fake data for regeneration */
strcpy((char *) ref_out->po_data, "REGEN");
ref_out->po_data_sz = ref_out->po_regen_sz = 5;
/* STREAM B */
setup_stream_contents(123, "BBBBBBBBBB");
streams[0].id = 'B';
len = pf->pf_gen_stream_frame(ref_out->po_data + ref_out->po_data_sz,
lsquic_packet_out_avail(ref_out),
streams[0].id, lsquic_stream_tosend_offset(&streams[0]),
(gsf_fin_f) lsquic_stream_tosend_fin,
(gsf_size_f) lsquic_stream_tosend_sz,
(gsf_read_f) lsquic_stream_tosend_read,
&streams[0]);
b_off = ref_out->po_data_sz;
ref_out->po_data_sz += len;
len = pf->pf_gen_rst_frame(ref_out->po_data + ref_out->po_data_sz,
lsquic_packet_out_avail(ref_out), 'A', 133, 0);
ref_out->po_data_sz += len;
/* STREAM D */
setup_stream_contents(123, "DDDDDDDDDD");
streams[0].id = 'D';
len = pf->pf_gen_stream_frame(ref_out->po_data + ref_out->po_data_sz,
lsquic_packet_out_avail(ref_out),
streams[0].id, lsquic_stream_tosend_offset(&streams[0]),
(gsf_fin_f) lsquic_stream_tosend_fin,
(gsf_size_f) lsquic_stream_tosend_sz,
(gsf_read_f) lsquic_stream_tosend_read,
&streams[0]);
d_off = ref_out->po_data_sz;
ref_out->po_data_sz += len;
}
/* Construct packet from which we will elide streams. Here, we attach
* stream objects to the packet.
*/
{
packet_out = lsquic_mm_get_packet_out(&enpub.enp_mm, NULL, QUIC_MAX_PAYLOAD_SZ);
/* This is fake data for regeneration */
strcpy((char *) packet_out->po_data, "REGEN");
packet_out->po_data_sz = packet_out->po_regen_sz = 5;
/* STREAM A */
setup_stream_contents(123, "AAAAAAAAAA");
streams[0].id = 'A';
len = pf->pf_gen_stream_frame(packet_out->po_data + packet_out->po_data_sz,
lsquic_packet_out_avail(packet_out),
streams[0].id, lsquic_stream_tosend_offset(&streams[0]),
(gsf_fin_f) lsquic_stream_tosend_fin,
(gsf_size_f) lsquic_stream_tosend_sz,
(gsf_read_f) lsquic_stream_tosend_read,
&streams[0]);
lsquic_packet_out_add_stream(packet_out, &enpub.enp_mm, &streams[0],
QUIC_FRAME_STREAM, packet_out->po_data_sz, len);
packet_out->po_data_sz += len;
/* STREAM B */
setup_stream_contents(123, "BBBBBBBBBB");
streams[1].id = 'B';
len = pf->pf_gen_stream_frame(packet_out->po_data + packet_out->po_data_sz,
lsquic_packet_out_avail(packet_out),
streams[1].id, lsquic_stream_tosend_offset(&streams[1]),
(gsf_fin_f) lsquic_stream_tosend_fin,
(gsf_size_f) lsquic_stream_tosend_sz,
(gsf_read_f) lsquic_stream_tosend_read,
&streams[1]);
lsquic_packet_out_add_stream(packet_out, &enpub.enp_mm, &streams[1],
QUIC_FRAME_STREAM, packet_out->po_data_sz, len);
packet_out->po_data_sz += len;
/* STREAM C */
setup_stream_contents(123, "CCCCCCCCCC");
streams[2].id = 'C';
len = pf->pf_gen_stream_frame(packet_out->po_data + packet_out->po_data_sz,
lsquic_packet_out_avail(packet_out),
streams[2].id, lsquic_stream_tosend_offset(&streams[2]),
(gsf_fin_f) lsquic_stream_tosend_fin,
(gsf_size_f) lsquic_stream_tosend_sz,
(gsf_read_f) lsquic_stream_tosend_read,
&streams[2]);
lsquic_packet_out_add_stream(packet_out, &enpub.enp_mm, &streams[2],
QUIC_FRAME_STREAM, packet_out->po_data_sz, len);
packet_out->po_data_sz += len;
/* Reset A */
len = pf->pf_gen_rst_frame(packet_out->po_data + packet_out->po_data_sz,
lsquic_packet_out_avail(packet_out), 'A', 133, 0);
lsquic_packet_out_add_stream(packet_out, &enpub.enp_mm, &streams[0],
QUIC_FRAME_RST_STREAM, 0, 0);
packet_out->po_data_sz += len;
/* STREAM D */
setup_stream_contents(123, "DDDDDDDDDD");
streams[3].id = 'D';
len = pf->pf_gen_stream_frame(packet_out->po_data + packet_out->po_data_sz,
lsquic_packet_out_avail(packet_out),
streams[3].id, lsquic_stream_tosend_offset(&streams[3]),
(gsf_fin_f) lsquic_stream_tosend_fin,
(gsf_size_f) lsquic_stream_tosend_sz,
(gsf_read_f) lsquic_stream_tosend_read,
&streams[3]);
lsquic_packet_out_add_stream(packet_out, &enpub.enp_mm, &streams[3],
QUIC_FRAME_STREAM, packet_out->po_data_sz, len);
packet_out->po_data_sz += len;
/* STREAM E */
setup_stream_contents(123, "EEEEEEEEEE");
streams[4].id = 'E';
len = pf->pf_gen_stream_frame(packet_out->po_data + packet_out->po_data_sz,
lsquic_packet_out_avail(packet_out),
streams[4].id, lsquic_stream_tosend_offset(&streams[4]),
(gsf_fin_f) lsquic_stream_tosend_fin,
(gsf_size_f) lsquic_stream_tosend_sz,
(gsf_read_f) lsquic_stream_tosend_read,
&streams[4]);
lsquic_packet_out_add_stream(packet_out, &enpub.enp_mm, &streams[4],
QUIC_FRAME_STREAM, packet_out->po_data_sz, len);
packet_out->po_data_sz += len;
packet_out->po_frame_types = (1 << QUIC_FRAME_STREAM) | (1 << QUIC_FRAME_RST_STREAM);
}
/* Reset streams A, C, and E: */
streams[0].stream_flags |= STREAM_RST_SENT;
streams[2].stream_flags |= STREAM_RST_SENT;
streams[4].stream_flags |= STREAM_RST_SENT;
if (chop_regen)
lsquic_packet_out_chop_regen(packet_out);
lsquic_packet_out_elide_reset_stream_frames(packet_out, 0);
assert(ref_out->po_data_sz == packet_out->po_data_sz + (chop_regen ? 5 : 0));
assert(ref_out->po_regen_sz == packet_out->po_regen_sz + (chop_regen ? 5 : 0));
if (chop_regen)
assert(0 == memcmp(ref_out->po_data + 5, packet_out->po_data, packet_out->po_data_sz));
else
assert(0 == memcmp(ref_out->po_data, packet_out->po_data, packet_out->po_data_sz));
assert(1 == streams[0].n_unacked); /* Still has RST outstanding */
assert(1 == streams[1].n_unacked);
assert(0 == streams[2].n_unacked);
assert(1 == streams[3].n_unacked);
assert(0 == streams[4].n_unacked);
assert(packet_out->po_frame_types == ((1 << QUIC_FRAME_STREAM) | (1 << QUIC_FRAME_RST_STREAM)));
srec = posi_first(&posi, packet_out);
assert(srec->sr_stream == &streams[0]);
assert(srec->sr_frame_types == (1 << QUIC_FRAME_RST_STREAM));
srec = posi_next(&posi);
assert(srec->sr_stream == &streams[1]);
assert(srec->sr_frame_types == (1 << QUIC_FRAME_STREAM));
assert(srec->sr_off == b_off - (chop_regen ? 5 : 0));
srec = posi_next(&posi);
assert(srec->sr_stream == &streams[3]);
assert(srec->sr_frame_types == (1 << QUIC_FRAME_STREAM));
assert(srec->sr_off == d_off - (chop_regen ? 5 : 0));
srec = posi_next(&posi);
assert(!srec);
lsquic_packet_out_destroy(packet_out, &enpub);
lsquic_packet_out_destroy(ref_out, &enpub);
lsquic_mm_cleanup(&enpub.enp_mm);
}
int
main (void)
{
/* TODO-ENDIAN: test with every PF */
elide_single_stream_frame();
elide_three_stream_frames(0);
elide_three_stream_frames(1);
return 0;
}