litespeed-quic/test/unittests/test_frame_chop.c

202 lines
5.3 KiB
C
Raw Normal View History

/* Copyright (c) 2017 - 2018 LiteSpeed Technologies Inc. See LICENSE. */
2017-09-22 21:00:03 +00:00
/*
* Write several things to HEADERS stream and check the results. What
* varies is the amount of bytes that are written to stream every time.
* This will exercise buffering in frame writer and verify that contents
* are written out correctly no matter where frab writing leaves off
* and picks up.
*/
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef WIN32
2017-09-22 21:00:03 +00:00
#include <unistd.h>
#else
#include <getopt.h>
#endif
2017-09-22 21:00:03 +00:00
#include <sys/queue.h>
#include "lsquic.h"
#include "lshpack.h"
2017-09-22 21:00:03 +00:00
#include "lsquic_logger.h"
#include "lsquic_mm.h"
#include "lsquic_frame_common.h"
#include "lsquic_frame_writer.h"
#include "lsquic_frame_reader.h"
struct lsquic_stream
{
size_t sm_write_off,
sm_buf_sz; /* Number of bytes allocated */
size_t sm_max_write;
size_t sm_read_off;
unsigned char *sm_buf;
};
static struct lsquic_stream *
stream_new (size_t max_write)
{
struct lsquic_stream *stream = calloc(1, sizeof(*stream));
stream->sm_max_write = max_write;
return stream;
}
static void
stream_destroy (struct lsquic_stream *stream)
{
free(stream->sm_buf);
free(stream);
}
#define reset_output(max_) do { \
output.sz = 0; \
if (max_) \
output.max = max_; \
else \
output.max = sizeof(output.buf);\
} while (0)
static ssize_t
stream_write (struct lsquic_stream *stream, const void *buf, size_t sz)
{
if (sz > stream->sm_max_write)
sz = stream->sm_max_write;
if (stream->sm_write_off + sz > stream->sm_buf_sz)
{
if (stream->sm_write_off + sz < stream->sm_buf_sz * 2)
stream->sm_buf_sz *= 2;
else
stream->sm_buf_sz = stream->sm_write_off + sz;
stream->sm_buf = realloc(stream->sm_buf, stream->sm_buf_sz);
}
memcpy(stream->sm_buf + stream->sm_write_off, buf, sz);
stream->sm_write_off += sz;
return sz;
}
#define IOV(v) { .iov_base = (v), .iov_len = sizeof(v) - 1, }
static void
test_chop (unsigned max_write_sz)
{
struct lsquic_frame_writer *fw;
struct lsquic_stream *stream;
struct lsquic_mm mm;
struct lshpack_enc henc;
2017-09-22 21:00:03 +00:00
int s;
lsquic_mm_init(&mm);
lshpack_enc_init(&henc);
2017-09-22 21:00:03 +00:00
stream = stream_new(max_write_sz);
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 13:35:58 +00:00
fw = lsquic_frame_writer_new(&mm, stream, 0, &henc, stream_write, 0);
2017-09-22 21:00:03 +00:00
struct lsquic_http_header header_arr[] =
{
{ .name = IOV(":status"), .value = IOV("302") },
};
struct lsquic_http_headers headers = {
.count = 1,
.headers = header_arr,
};
s = lsquic_frame_writer_write_headers(fw, 12345, &headers, 0, 100);
assert(0 == s);
struct lsquic_http2_setting settings[] = { { 1, 2, }, { 3, 4, } };
s = lsquic_frame_writer_write_settings(fw, settings, 2);
assert(0 == s);
/* TODO: server must not send priority frames, add a check for that
* error condition.
*/
s = lsquic_frame_writer_write_priority(fw, 3, 0, 1, 256);
assert(0 == s);
while (lsquic_frame_writer_have_leftovers(fw))
{
s = lsquic_frame_writer_flush(fw);
assert(0 == s);
}
const unsigned char expected_buf[] = {
/* Length: */ 0x00, 0x00, 0x09,
/* Type: */ HTTP_FRAME_HEADERS,
/* Flags: */ HFHF_END_HEADERS|HFHF_PRIORITY,
/* Stream Id: */ 0x00, 0x00, 0x30, 0x39,
/* Dep stream id: */0x00, 0x00, 0x00, 0x00,
/* Weight: */ 100 - 1,
/* Block fragment: */
0x48, 0x82, 0x64, 0x02,
/* Length: */ 0x00, 0x00, 0x0C,
/* Type: */ HTTP_FRAME_SETTINGS,
/* Flags: */ 0x00,
/* Stream Id: */ 0x00, 0x00, 0x00, 0x00,
/* Payload: */ 0x00, 0x01, 0x00, 0x00, 0x00, 0x02,
0x00, 0x03, 0x00, 0x00, 0x00, 0x04,
/* Length: */ 0x00, 0x00, 5,
/* Type: */ HTTP_FRAME_PRIORITY,
/* Flags: */ 0x00,
/* Stream Id: */ 0x00, 0x00, 0x00, 0x03,
/* Dep stream Id: */0x00, 0x00, 0x00, 0x01,
/* Weight: */ 0xFF,
};
assert(stream->sm_write_off == sizeof(expected_buf));
assert(0 == memcmp(stream->sm_buf, expected_buf, sizeof(expected_buf)));
lsquic_frame_writer_destroy(fw);
stream_destroy(stream);
lshpack_enc_cleanup(&henc);
2017-09-22 21:00:03 +00:00
lsquic_mm_cleanup(&mm);
}
int
main (int argc, char **argv)
{
const unsigned write_sizes[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 20,
30, 100, 200, 255, 0xFFF, 0x1000, 0x100D,
UINT_MAX, };
unsigned i;
int opt, max_write_sz = -1;
while (-1 != (opt = getopt(argc, argv, "l:s:")))
{
switch (opt)
{
case 'l':
lsquic_log_to_fstream(stderr, LLTS_NONE);
lsquic_logger_lopt(optarg);
break;
case 's':
max_write_sz = atoi(optarg);
break;
default:
exit(1);
}
}
if (-1 == max_write_sz)
for (i = 0; i < sizeof(write_sizes) / sizeof(write_sizes[0]); ++i)
test_chop(write_sizes[i]);
else
test_chop(max_write_sz);
return 0;
}