-
Notifications
You must be signed in to change notification settings - Fork 969
Expand file tree
/
Copy pathhaterm_init.c
More file actions
437 lines (379 loc) · 11.3 KB
/
Copy pathhaterm_init.c
File metadata and controls
437 lines (379 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#include <haproxy/api.h>
#include <haproxy/buf.h>
#include <haproxy/chunk.h>
#include <haproxy/errors.h>
#include <haproxy/global.h>
#include <haproxy/openssl-compat.h>
#include <haproxy/hbuf.h>
#include <haproxy/version.h>
static int haterm_debug;
#define QUIC_BIND_LONG_OPT "quic-bind-opts"
#define TCP_BIND_LONG_OPT "tcp-bind-opts"
/*
* This function prints the command line usage for haterm and exits
*/
static void haterm_usage(char *name)
{
fprintf(stderr,
"Usage : %s -L [<ip>]:<clear port>[:<TCP&QUIC SSL port>] [-L...]* [opts]\n"
"where <opts> may be any combination of:\n"
" -G <line> : multiple option; append <line> to the \"global\" section\n"
" -F <line> : multiple option; append <line> to the \"frontend\" section\n"
" -T <line> : multiple option; append <line> to the \"traces\" section\n"
" -C : dump the configuration and exit\n"
" -D : goes daemon\n"
" -b <keysize> : RSA key size in bits (ex: \"2048\", \"4096\"...)\n"
" -c <curves> : ECDSA curves (ex: \"P-256\", \"P-384\"...)\n"
" -v : shows version\n"
" -d : enable the traces for all http protocols\n"
" -dS : disables splice() usage even when available\n"
" -dZ : disable zero-copy forwarding\n"
#if defined(USE_QUIC)
" --" QUIC_BIND_LONG_OPT " <opts> : append options to QUIC \"bind\" lines\n"
#endif
" --" TCP_BIND_LONG_OPT " <opts> : append options to TCP \"bind\" lines\n"
"Note: QUIC is automatically enabled on the SSL port if built in.\n"
, name);
exit(1);
}
#define HATERM_FRONTEND_NAME "___haterm_frontend___"
#define HATERM_RSA_CERT_NAME "haterm.pem.rsa"
#define HATERM_ECDSA_CERT_NAME "haterm.pem.ecdsa"
static const char *haterm_cfg_dflt_str =
"defaults\n"
"\tmode haterm\n"
#if defined(USE_LINUX_SPLICE)
"\toption splice-response\n"
#endif
"\ttimeout client 25s\n";
#define HATERM_CFG_CRT_STORE_STR_FMT \
"crt-store\n" \
"\tload generate-dummy on keytype RSA bits %s crt " HATERM_RSA_CERT_NAME "\n" \
"\tload generate-dummy on keytype ECDSA curves %s crt " HATERM_ECDSA_CERT_NAME "\n\n"
static const char *haterm_cfg_traces_str =
"traces\n"
"\ttrace h1 sink stderr level user start now verbosity minimal\n"
"\ttrace h2 sink stderr level user start now verbosity minimal\n"
"\ttrace h3 sink stderr level user start now verbosity minimal\n"
"\ttrace qcm sink stderr level user start now verbosity minimal\n";
/* This function initialises the haterm HTTP benchmark server from
* <argv>. This consists in building a configuration file in memory
* using the haproxy configuration language.
* Make exit(1) the process in case of any failure.
*/
void haproxy_init_args(int argc, char **argv)
{
/* Initialize haterm fileless cfgfile from <argv> arguments array.
* Never fails.
*/
int has_bind = 0, err = 1, dump = 0, has_ssl = 0;
struct hbuf gbuf = HBUF_NULL; // "global" section
struct hbuf mbuf = HBUF_NULL; // to build the main of the cfgfile
struct hbuf fbuf = HBUF_NULL; // "frontend" section
struct hbuf tbuf = HBUF_NULL; // "traces" section
char *bits = NULL, *curves = NULL;
char *quic_bind_opt __maybe_unused = NULL, *tcp_bind_opt = NULL;
int sargc; /* saved argc */
char **sargv; /* saved argv */
fileless_mode = 1;
if (argc <= 1)
haterm_usage(progname);
if (hbuf_alloc(&mbuf) == NULL) {
ha_alert("failed to alloce a buffer.\n");
exit(1);
}
/* skip program name and start */
argc--; argv++;
/* save the arguments */
sargc = argc; sargv = argv;
#if defined(USE_LINUX_SPLICE)
global.tune.options |= GTUNE_USE_SPLICE;
#endif
/* THIS PART MUST NOT MODIFY THE ARGUMENTS */
/* Parse the arguments which must be reused to build the conf. */
while (argc > 0) {
char *opt;
if (**argv == '-') {
opt = *argv + 1;
if (*opt == '-') {
/* long options */
opt++;
#if defined(USE_QUIC)
if (strcmp(opt, QUIC_BIND_LONG_OPT) == 0) {
argv++; argc--;
if (argc <= 0 || **argv == '-')
haterm_usage(progname);
quic_bind_opt = *argv;
}
else
#endif
if (strcmp(opt, TCP_BIND_LONG_OPT) == 0) {
argv++; argc--;
if (argc <= 0 || **argv == '-')
haterm_usage(progname);
tcp_bind_opt = *argv;
}
}
}
argc--; argv++;
}
/* Restore the arguments */
argc = sargc; argv = sargv;
while (argc > 0) {
char *opt;
if (**argv == '-') {
opt = *argv + 1;
if (*opt == '-') {
/* long options */
opt++;
if (strcmp(opt, QUIC_BIND_LONG_OPT) == 0) {
argv++; argc--;
if (argc <= 0 || **argv == '-')
haterm_usage(progname);
quic_bind_opt = *argv;
}
else if (strcmp(opt, TCP_BIND_LONG_OPT) == 0) {
argv++; argc--;
if (argc <= 0 || **argv == '-')
haterm_usage(progname);
tcp_bind_opt = *argv;
}
else
haterm_usage(progname);
}
#if defined(USE_LINUX_SPLICE)
else if (*opt == 'd' && *(opt+1) == 'S') {
global.tune.options &= ~GTUNE_USE_SPLICE;
}
# if defined(HA_USE_KTLS)
else if (*opt == 'd' && *(opt+1) == 'T') {
global.tune.options |= GTUNE_NO_KTLS;
}
# endif
#endif
else if (*opt == 'd' && *(opt+1) == 'Z') {
global.tune.no_zero_copy_fwd |= NO_ZERO_COPY_FWD;
}
else if (*opt == 'd') {
/* empty option */
if (*(opt + 1))
haterm_usage(progname);
/* debug mode */
haterm_debug = 1;
}
else if (*opt == 'C') {
/* empty option */
if (*(opt + 1))
haterm_usage(progname);
dump = 1;
}
else if (*opt == 'D') {
/* empty option */
if (*(opt + 1))
haterm_usage(progname);
global.mode |= MODE_DAEMON;
}
else if (*opt == 'v') {
/* empty option */
if (*(opt + 1))
haterm_usage(progname);
printf("HATerm version " HAPROXY_VERSION " released " HAPROXY_DATE "\n");
exit(0);
}
else if (*opt == 'b') {
argv++; argc--;
if (argc <= 0 || **argv == '-')
haterm_usage(progname);
bits = *argv;
}
else if (*opt == 'c') {
argv++; argc--;
if (argc <= 0 || **argv == '-')
haterm_usage(progname);
curves = *argv;
}
else if (*opt == 'F') {
argv++; argc--;
if (argc <= 0 || **argv == '-')
haterm_usage(progname);
if (hbuf_is_null(&fbuf)) {
if (hbuf_alloc(&fbuf) == NULL) {
ha_alert("failed to allocate a buffer.\n");
goto leave;
}
hbuf_appendf(&fbuf, "frontend " HATERM_FRONTEND_NAME "\n");
hbuf_appendf(&fbuf, "\toption accept-unsafe-violations-in-http-request\n");
}
hbuf_str_append(&fbuf, *argv);
}
else if (*opt == 'G') {
argv++; argc--;
if (argc <= 0 || **argv == '-')
haterm_usage(progname);
if (hbuf_is_null(&gbuf)) {
if (hbuf_alloc(&gbuf) == NULL) {
ha_alert("failed to allocate a buffer.\n");
goto leave;
}
hbuf_appendf(&gbuf, "global\n");
}
hbuf_str_append(&gbuf, *argv);
}
else if (*opt == 'T') {
argv++; argc--;
if (argc <= 0 || **argv == '-')
haterm_usage(progname);
if (hbuf_is_null(&tbuf) && hbuf_alloc(&tbuf) == NULL) {
ha_alert("failed to allocate a buffer.\n");
goto leave;
}
haterm_debug = 1;
hbuf_str_append(&tbuf, *argv);
}
else if (*opt == 'L') {
/* binding */
int __maybe_unused ipv6 = 0;
char *ip, *port, *port1 = NULL, *port2 = NULL;
argv++; argc--;
if (argc <= 0 || **argv == '-')
haterm_usage(progname);
port = ip = *argv;
if (*ip == '[') {
/* IPv6 address */
ip++;
port = strchr(port, ']');
if (!port)
haterm_usage(progname);
*port++ = '\0';
ipv6 = 1;
}
while ((port = strchr(port, ':'))) {
*port++ = '\0';
if (!port1)
port1 = port;
else {
if (port2)
haterm_usage(progname);
port2 = port;
}
}
if (!port1)
haterm_usage(progname);
if (hbuf_is_null(&fbuf)) {
if (hbuf_alloc(&fbuf) == NULL) {
ha_alert("failed to allocate a buffer.\n");
goto leave;
}
hbuf_appendf(&fbuf, "frontend " HATERM_FRONTEND_NAME "\n");
hbuf_appendf(&fbuf, "\toption accept-unsafe-violations-in-http-request\n");
}
/* clear HTTP */
hbuf_appendf(&fbuf, "\tbind %s:%s shards by-thread%s%s\n", ip, port1,
tcp_bind_opt ? " " : "",
tcp_bind_opt ? tcp_bind_opt : "");
has_bind = 1;
if (port2) {
#if defined(USE_OPENSSL)
has_ssl = 1;
/* SSL/TCP binding */
hbuf_appendf(&fbuf, "\tbind %s:%s shards by-thread ssl "
"alpn h3,h2,http1.1,http1.0"
" crt " HATERM_RSA_CERT_NAME
" crt " HATERM_ECDSA_CERT_NAME "%s%s%s\n",
ip, port2,
tcp_bind_opt ? " " : "",
tcp_bind_opt ? tcp_bind_opt : "",
# if defined(USE_LINUX_SPLICE) && defined(HA_USE_KTLS)
" ktls on"
# else
"" /* no ktls */
# endif
);
# if defined(USE_QUIC)
/* QUIC binding */
hbuf_appendf(&fbuf, "\tbind %s@%s:%s shards by-thread ssl"
" crt " HATERM_RSA_CERT_NAME
" crt " HATERM_ECDSA_CERT_NAME "%s%s\n",
ipv6 ? "quic6" : "quic4", ip, port2,
quic_bind_opt ? " " : "",
quic_bind_opt ? quic_bind_opt : "");
# endif /* USE_QUIC */
#else /* !USE_OPENSSL */
ha_alert("SSL support not compiled in. Rebuild with USE_OPENSSL=1.\n");
goto leave;
#endif /* USE_OPENSSL */
}
}
else
haterm_usage(progname);
}
else
haterm_usage(progname);
argv++; argc--;
}
if (!has_bind) {
ha_alert("No binding! Exiting...\n");
haterm_usage(progname);
}
if (hbuf_is_null(&gbuf)) {
/* use 3MB of local cache per thread mainly for QUIC */
if (hbuf_alloc(&gbuf) == NULL) {
ha_alert("failed to allocate a buffer.\n");
goto leave;
}
hbuf_appendf(&gbuf, "global\n");
}
hbuf_appendf(&gbuf, "\ttune.memory.hot-size 3145728\n");
if (has_ssl)
hbuf_appendf(&gbuf, "\texpose-experimental-directives\n");
#if defined(USE_LINUX_SPLICE) && defined(HA_USE_KTLS)
if (has_ssl)
hbuf_appendf(&gbuf, "\ttune.pipesize 262144\n");
#endif
/* "global" section */
if (!hbuf_is_null(&gbuf))
hbuf_appendf(&mbuf, "%.*s\n", (int)gbuf.data, gbuf.area);
/* "traces" section */
if (haterm_debug) {
hbuf_appendf(&mbuf, "%s", haterm_cfg_traces_str);
if (!hbuf_is_null(&tbuf))
hbuf_appendf(&mbuf, "%.*s\n", (int)tbuf.data, tbuf.area);
}
/* "defaults" section */
hbuf_appendf(&mbuf, "%s\n", haterm_cfg_dflt_str);
/* "crt-store" section */
if (has_ssl)
hbuf_appendf(&mbuf, HATERM_CFG_CRT_STORE_STR_FMT,
bits ? bits : "2048", curves ? curves : "P-384");
/* "frontend" section */
hbuf_appendf(&mbuf, "%.*s\n", (int)fbuf.data, fbuf.area);
fileless_cfg.filename = strdup("haterm cfgfile");
fileless_cfg.content = strdup(mbuf.area);
if (!fileless_cfg.filename || !fileless_cfg.content) {
ha_alert("cfgfile strdup() failed.\n");
goto leave;
}
fileless_cfg.size = mbuf.data;
if (dump) {
fprintf(stdout, "%.*s", (int)fileless_cfg.size, fileless_cfg.content);
exit(0);
}
/* no pool debugging */
pool_debugging = 0;
err = 0;
leave:
free_hbuf(&mbuf);
free_hbuf(&gbuf);
free_hbuf(&fbuf);
free_hbuf(&tbuf);
if (err)
exit(1);
}
/* Dummy arg copier function */
char **copy_argv(int argc, char **argv)
{
char **ret = calloc(1, sizeof(*ret));
if (*ret)
*ret = strdup("");
return ret;
}