EXAMPLES.txt revision 7d09751d
1# Copyright (c) 2017 - 2020 LiteSpeed Technologies Inc.  See LICENSE.
2LSQUIC Examples
3===============
4
5LSQUIC comes with several examples of how the library is used.
6
7The client and server programs described below are built on a common
8framework and share many options.
9
10Echo client and server
11----------------------
12
13Echo client and server (see test/echo_{client,server}.c) are for simple
14line-based request and reply communication.  Only one stream per connection
15is supported for simplicity.  The client reads input from stdin.
16
17MD5 client and server
18---------------------
19
20See test/md5_{client,server}.c
21
22MD5 server accepts connections, computes MD5 sum of streams' (one or more)
23payload, and sends back the checksum.  MD5 client sends one or more file
24contents to the server.  Both client and server support various options to
25exercise different aspects of LSQUIC.
26
27HTTP client and server
28----------------------
29
30See test/http_{client,server}.c
31
32This pair of programs is to demonstrate how to use HTTP features of QUIC.
33HTTP server is interoperable with proto-quic's quic_client.
34
35Usage Examples
36--------------
37
38Fetch Google's home page:
39
40    ./http_client -s www.google.com -p /
41
42The default port number is 443, but it can be specified after colon
43using the -s flag.  The value of the `host' header as well as the SNI
44value defaults to the host part of the -s option.  -H option can be
45used to override it.  For example:
46
47    ./http_client -H www.youtube.com -s www.google.com:443 -p / -M HEAD
48
49The host part can be an IP address.  Both IPv4 and IPv6 are supported.
50See ./http_client -h for a (long) list of different flags.
51
52POST a file to calculate its CRC32 checksum:
53
54    ./http_client -H www.litespeedtech.com -s 443 \
55                        -p /cgi-bin/crc32.cgi -P file-256M -M POST
56
57    HTTP/1.1 200 OK
58    content-type: text/plain
59    date: Fri, 09 Jun 2017 08:40:45 GMT
60    server: LiteSpeed
61    alt-svc: quic=":443"; v="35,37"
62
63    CRC32: 2A0E7DBB
64
65This is a good way to check that the payload gets to the other side
66correctly.  The CGI script is:
67
68    #!/usr/bin/perl
69    use String::CRC32;
70    printf "Content-type: text/plain\r\n\r\nCRC32: %X\n", crc32(*STDIN)
71
72On the command line, I do
73
74    alias crc32="perl -MString::CRC32 -e'printf qq(%X\n), crc32(<>)'"
75
76To submit several requests concurrently, one can use -n and -r options:
77
78    ./http_client -H www.litespeedtech.com -s 443 \
79                -p /cgi-bin/crc32.cgi -P file-256M -M POST -n 3 -r 10
80
81This will open three parallel connections which will make ten POST
82requests together.
83
84To perform load testing, it is good to mix sending and receiving data:
85
86    for i in {1..100}; do
87        ./http_client $COMMON_OPTS -p /cgi-bin/crc32.cgi -P file-256M \
88                                                    -M POST >out-post.$i &
89        ./http_client $COMMON_OPTS -p /docs/file-256M >out-get.$i        &
90        sleep 1
91    done
92
93If you don't want to create a hundred 256-megabyte out-get.* files, use -K
94flag to discard output.
95
96Testing Large Packet Sizes
97---------------------------------
98
99IETF QUIC supports all valid UDP packet sizes. This section outlines the
100environment setup and testing parameters necessary to test this feature.
101
102Compilation
103    - Make sure to compile the library in DEBUG mode so that the NDEBUG
104      define is off.
105    - Build both the http_client and http_server test programs.
106
107Running Instructions
108    - On the server side, define the environment variable
109      LSQUIC_CN_PACK_SIZE and set it to the intended packet size.
110      Valid sizes are up to 65507 (IPv4).
111    - Use the -W flag for http_client and http_server for the ability
112      to send packets of large size.
113    - On the client side, use the -z flag to specify the maximum size
114      of packet that the client will accept.
115
116Example Usage
117    ./http_client -p /file-1M -H www.litespeedtech.com -s 192.168.0.85:5443
118        -o version=FF000014 -z 65507 -W
119    ./http_server -c www.litespeedtech.com,certschain,privkey
120        -s 0.0.0.0:5443 -W
121
122Additional Notes
123    Since this feature does not have MTU discovery enabled at the time of
124    writing, make sure to use client and server machines that share a path
125    with the intended MTU.
126
127Control QUIC Settings via -o Flag
128---------------------------------
129
130Most of the settings in struct squic_engine_settings can be controlled
131via -o flag.  With exception of es_versions, which is a bit mask, other
132es_* options can be mapped to corresponding -o value via s/^es_//:
133
134    es_cfcw             =>  -o cwcf=12345
135    es_max_streams_in   =>  -o max_streams_in=123
136
137And so on.
138
139For example, to test version negotiation:
140
141    ./http_server -c www.litespeedtech.com,certschain,privkey \
142        -o version=Q035 -L debug 2>server.out &
143    ./http_client -H www.litespeedtech.com -p Makefile -L debug 2>client.out
144
145Above, the client will start with the default, which is the highest supported
146QUIC version, which the server should negotiate down.  You should see it from
147the log files.
148
149The code to set options via -o flag lives in set_engine_option().  It is good
150to update this function at the same time as member fields are added to struct
151lsquic_engine_settings.
152
153Control LSQUIC Behavior via Environment Variables
154-------------------------------------------------
155
156LSQUIC_PACKET_OUT_LIMIT
157
158    If set, the value of this environment variable is the maximum number
159    of packets that can be sent out in one shot.  The limit is in the test
160    program framework's ea_packets_out() callback.
161
162    It is not applicable when sendmmsg(2) is used.
163
164    Note 1: sendmmsg can be enabled using -g option, if available for your
165            platform.
166
167    Note 2: see -m option for a related packet-out limitation.
168
169LSQUIC_LOSE_PACKETS_RE
170
171    If set, this regular expression specifies the numbers of packets which
172    the sender will lose on purpose.  For example:
173
174        export LSQUIC_LOSE_PACKETS_RE='^(3|5|10)$'
175
176    The act of losing a packet is performed by changing its payload to
177    zero-filled buffer of the same size, which is almost as good as
178    not sending anything.  The latter is difficult to implement without
179    negatively affecting the regular code flow.
180
181    Only available in debug builds.
182
183LSQUIC_PACER_INTERTICK
184
185    Number of microsecods to use as constant intertick time in lieu of the
186    pacer's dynamic intertick time approximation.
187
188    Only available in debug builds.
189
190LSQUIC_CUBIC_SAMPLING_RATE
191
192    Number of microseconds between times CWND is logged at info level.
193
194    Only available in debug builds.
195
196LSQUIC_RANDOM_SEND_FAILURE
197
198    Frequency with which sending of packets fails: one out of this many
199    times on average.
200
201    Only available when compiled with -DLSQUIC_RANDOM_SEND_FAILURE=1
202
203LSQUIC_LOG_SECRETS
204
205    If set to true value, crypto secrets will be logged.  Applies to
206    IETF QUIC only.
207
208LSQUIC_COALESCE
209
210    If set to false, packets are not coalesced.  Defaults to true.
211
212LSQUIC_USE_POOLS
213
214    If set to false, all memory pooling code is replaced with calls to
215    malloc() and free().  This facilitates debugging memory issues.
216    The default is true.
217
218LSQUIC_ACK_ATTACK
219
220    If set to true, generate optimistic ACKs.
221
222Control Network-Related Stuff
223-----------------------------
224
225   -D          Set `do not fragment' flag on outgoing UDP packets.
226
227   -z BYTES    Maximum size of outgoing UDP packets.  The default is 1370
228               bytes for IPv4 socket and 1350 bytes for IPv6 socket.
229
230   -S opt=val  Socket options.  Supported options:
231                   sndbuf=12345    # Sets SO_SNDBUF
232                   rcvbuf=12345    # Sets SO_RCVBUF
233
234   -g          Use sendmmsg() to send packets.  This is only compiled in
235               if available.
236
237More Compilation Options
238------------------------
239
240-DLSQUIC_CONN_STATS=1
241
242    Track some statistics about connections -- packets in, sent, delayed,
243    stream payload per packet size ratio, and some others -- and print them
244    at NOTICE level when connection is destroyed.
245
246    Cumulative connections statistics are printed by the engine when it is
247    destroyed if lsquic_engine_api.ea_stats_fh is set.  The HTTP client
248    programs sets it when -t or -T command-line option is used.
249
250-DLSQUIC_PACKINTS_SANITY_CHECK=1
251
252    Turn on sanity checking for packet interval code.  The packet interval
253    code, shared by both send and receive history modules, contained a bug
254    which prompted me to add a checking function.
255
256-DLSQUIC_SEND_STATS=0
257
258    Turn off statistics collection performed by the send controller: number
259    of packets sent, resent, and delayed.
260
261-DLOG_PACKET_CHECKSUM=1
262
263    When turned on, CRC32 checksum of each sent and received packet is
264    logged as an event.
265
266-DLSQUIC_LOWEST_LOG_LEVEL=LSQ_LOG_WARN
267
268    If you want to go even faster: compile out some log levels entirely.
269
270-DLSQUIC_EXTRA_CHECKS=1
271
272    Add relatively expensive run-time sanity checks
273
274-DLSQUIC_RANDOM_SEND_FAILURE=1
275
276    Simulate failure to send packets to test send resumption logic.  When
277    this flag is specified, sending of packets will randomly fail, about
278    one out of every 10 attempts.  Set environment variable
279    LSQUIC_RANDOM_SEND_FAILURE to change this frequency.
280
281-DLSQUIC_ECN_BLACK_HOLE=1
282
283    When compiled with this flag, setting environment variable
284    LSQUIC_ECN_BLACK_HOLE to 1 will emulate ECN black hole: all received
285    packets with ECN markings are dropped on the floor.
286
287-DLSQUIC_ACK_ATTACK=1
288
289    Enable ACK attack mode.  See LSQUIC_ACK_ATTACK environment variable
290    entry above.
291