| | varnish-cache/bin/varnishd/mgt/mgt_param_tweak.c |
0 |
|
/*- |
1 |
|
* Copyright (c) 2006 Verdens Gang AS |
2 |
|
* Copyright (c) 2006-2011 Varnish Software AS |
3 |
|
* All rights reserved. |
4 |
|
* |
5 |
|
* Author: Poul-Henning Kamp <phk@phk.freebsd.dk> |
6 |
|
* |
7 |
|
* SPDX-License-Identifier: BSD-2-Clause |
8 |
|
* |
9 |
|
* Redistribution and use in source and binary forms, with or without |
10 |
|
* modification, are permitted provided that the following conditions |
11 |
|
* are met: |
12 |
|
* 1. Redistributions of source code must retain the above copyright |
13 |
|
* notice, this list of conditions and the following disclaimer. |
14 |
|
* 2. Redistributions in binary form must reproduce the above copyright |
15 |
|
* notice, this list of conditions and the following disclaimer in the |
16 |
|
* documentation and/or other materials provided with the distribution. |
17 |
|
* |
18 |
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
19 |
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
20 |
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
21 |
|
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE |
22 |
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
23 |
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
24 |
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
25 |
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
26 |
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
27 |
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
28 |
|
* SUCH DAMAGE. |
29 |
|
* |
30 |
|
* Functions for tweaking parameters |
31 |
|
* |
32 |
|
*/ |
33 |
|
|
34 |
|
#include "config.h" |
35 |
|
|
36 |
|
#include <limits.h> |
37 |
|
#include <math.h> |
38 |
|
#include <stdio.h> |
39 |
|
#include <stdlib.h> |
40 |
|
#include <string.h> |
41 |
|
|
42 |
|
#include "mgt/mgt.h" |
43 |
|
|
44 |
|
#include "mgt/mgt_param.h" |
45 |
|
#include "storage/storage.h" |
46 |
|
#include "vav.h" |
47 |
|
#include "vnum.h" |
48 |
|
#include "vsl_priv.h" |
49 |
|
|
50 |
|
const char * const JSON_FMT = (const char *)&JSON_FMT; |
51 |
|
|
52 |
|
/*-------------------------------------------------------------------- |
53 |
|
* Generic handling of double typed parameters |
54 |
|
*/ |
55 |
|
|
56 |
|
typedef double parse_double_f(const char *, const char **); |
57 |
|
|
58 |
|
static double |
59 |
92220 |
parse_decimal(const char *p, const char **err) |
60 |
|
{ |
61 |
|
double v; |
62 |
|
|
63 |
92220 |
v = SF_Parse_Decimal(&p, 0, err); |
64 |
92220 |
if (errno == 0 && *p != '\0') { |
65 |
5 |
errno = EINVAL; |
66 |
5 |
*err = "Invalid number"; |
67 |
5 |
} |
68 |
92220 |
return (v); |
69 |
|
} |
70 |
|
|
71 |
|
static int |
72 |
929770 |
tweak_generic_double(struct vsb *vsb, const char *arg, const struct parspec *pp, |
73 |
|
parse_double_f parse, const char *fmt) |
74 |
|
{ |
75 |
929770 |
volatile double u, minv = VRT_DECIMAL_MIN, maxv = VRT_DECIMAL_MAX; |
76 |
929770 |
volatile double *dest = pp->priv; |
77 |
|
const char *err; |
78 |
|
|
79 |
929770 |
if (arg != NULL && arg != JSON_FMT) { |
80 |
471285 |
if (pp->min != NULL) { |
81 |
471285 |
minv = parse(pp->min, &err); |
82 |
471285 |
if (errno) { |
83 |
0 |
VSB_printf(vsb, "Min: %s (%s)\n", err, pp->min); |
84 |
0 |
return (-1); |
85 |
|
} |
86 |
471285 |
} |
87 |
471285 |
if (pp->max != NULL) { |
88 |
153595 |
maxv = parse(pp->max, &err); |
89 |
153595 |
if (errno) { |
90 |
0 |
VSB_printf(vsb, "Max: %s (%s)\n", err, pp->max); |
91 |
0 |
return (-1); |
92 |
|
} |
93 |
153595 |
} |
94 |
|
|
95 |
471285 |
u = parse(arg, &err); |
96 |
471285 |
if (errno) { |
97 |
20 |
VSB_printf(vsb, "%s (%s)\n", err, arg); |
98 |
20 |
return (-1); |
99 |
|
} |
100 |
471265 |
if (u < minv) { |
101 |
10 |
VSB_printf(vsb, |
102 |
5 |
"Must be greater or equal to %s\n", pp->min); |
103 |
5 |
return (-1); |
104 |
|
} |
105 |
471260 |
if (u > maxv) { |
106 |
10 |
VSB_printf(vsb, |
107 |
5 |
"Must be less than or equal to %s\n", pp->max); |
108 |
5 |
return (-1); |
109 |
|
} |
110 |
471255 |
*dest = u; |
111 |
471255 |
} else { |
112 |
458485 |
VSB_printf(vsb, fmt, *dest); |
113 |
|
} |
114 |
929740 |
return (0); |
115 |
929770 |
} |
116 |
|
|
117 |
|
/*--------------------------------------------------------------------*/ |
118 |
|
|
119 |
|
static double |
120 |
1003945 |
parse_duration(const char *p, const char **err) |
121 |
|
{ |
122 |
|
double v, r; |
123 |
|
|
124 |
1003945 |
v = SF_Parse_Decimal(&p, 0, err); |
125 |
1003945 |
if (*p == '\0') |
126 |
921990 |
return (v); |
127 |
|
|
128 |
81955 |
r = VNUM_duration_unit(v, p, NULL); |
129 |
81955 |
if (isnan(r)) { |
130 |
10 |
errno = EINVAL; |
131 |
10 |
*err = "Invalid duration unit"; |
132 |
10 |
} |
133 |
|
|
134 |
81955 |
return (r); |
135 |
1003945 |
} |
136 |
|
|
137 |
|
int v_matchproto_(tweak_t) |
138 |
236495 |
tweak_timeout(struct vsb *vsb, const struct parspec *par, const char *arg) |
139 |
|
{ |
140 |
236495 |
volatile double *dest = par->priv; |
141 |
|
|
142 |
236495 |
if (arg != NULL && !strcmp(arg, "never")) { |
143 |
5 |
*dest = INFINITY; |
144 |
5 |
return (0); |
145 |
|
} |
146 |
|
|
147 |
236490 |
if (*dest == INFINITY && arg == NULL) { |
148 |
0 |
VSB_cat(vsb, "never"); |
149 |
0 |
return (0); |
150 |
|
} |
151 |
|
|
152 |
236490 |
if (*dest == INFINITY && arg == JSON_FMT) { |
153 |
0 |
VSB_cat(vsb, "\"never\""); |
154 |
0 |
return (0); |
155 |
|
} |
156 |
|
|
157 |
236490 |
return (tweak_generic_double(vsb, arg, par, parse_duration, "%.3f")); |
158 |
236495 |
} |
159 |
|
|
160 |
|
int v_matchproto_(tweak_t) |
161 |
647115 |
tweak_duration(struct vsb *vsb, const struct parspec *par, const char *arg) |
162 |
|
{ |
163 |
|
|
164 |
647115 |
return (tweak_generic_double(vsb, arg, par, parse_duration, "%.3f")); |
165 |
|
} |
166 |
|
|
167 |
|
/*--------------------------------------------------------------------*/ |
168 |
|
|
169 |
|
int v_matchproto_(tweak_t) |
170 |
30775 |
tweak_double(struct vsb *vsb, const struct parspec *par, const char *arg) |
171 |
|
{ |
172 |
|
|
173 |
30775 |
return (tweak_generic_double(vsb, arg, par, parse_decimal, "%g")); |
174 |
|
} |
175 |
|
|
176 |
|
/*--------------------------------------------------------------------*/ |
177 |
|
|
178 |
|
static int |
179 |
60655 |
parse_boolean(struct vsb *vsb, const char *arg) |
180 |
|
{ |
181 |
|
|
182 |
60655 |
if (!strcasecmp(arg, "off")) |
183 |
19610 |
return (0); |
184 |
41045 |
if (!strcasecmp(arg, "disable")) |
185 |
5 |
return (0); |
186 |
41040 |
if (!strcasecmp(arg, "no")) |
187 |
5 |
return (0); |
188 |
41035 |
if (!strcasecmp(arg, "false")) |
189 |
20 |
return (0); |
190 |
41015 |
if (!strcasecmp(arg, "on")) |
191 |
40945 |
return (1); |
192 |
70 |
if (!strcasecmp(arg, "enable")) |
193 |
5 |
return (1); |
194 |
65 |
if (!strcasecmp(arg, "yes")) |
195 |
5 |
return (1); |
196 |
60 |
if (!strcasecmp(arg, "true")) |
197 |
55 |
return (1); |
198 |
|
|
199 |
5 |
VSB_cat(vsb, "use \"on\" or \"off\"\n"); |
200 |
5 |
return (-1); |
201 |
60655 |
} |
202 |
|
|
203 |
|
int v_matchproto_(tweak_t) |
204 |
112605 |
tweak_boolean(struct vsb *vsb, const struct parspec *par, const char *arg) |
205 |
|
{ |
206 |
|
volatile unsigned *dest; |
207 |
|
int val; |
208 |
|
|
209 |
112605 |
dest = par->priv; |
210 |
112605 |
if (arg != NULL && arg != JSON_FMT) { |
211 |
60655 |
val = parse_boolean(vsb, arg); |
212 |
60655 |
if (val < 0) |
213 |
5 |
return (-1); |
214 |
60650 |
*dest = val; |
215 |
112600 |
} else if (arg == JSON_FMT) { |
216 |
150 |
VSB_printf(vsb, "%s", *dest ? "true" : "false"); |
217 |
150 |
} else { |
218 |
51800 |
VSB_printf(vsb, "%s", *dest ? "on" : "off"); |
219 |
|
} |
220 |
112600 |
return (0); |
221 |
112605 |
} |
222 |
|
|
223 |
|
/*--------------------------------------------------------------------*/ |
224 |
|
|
225 |
|
static int |
226 |
770235 |
tweak_generic_uint(struct vsb *vsb, volatile unsigned *dest, const char *arg, |
227 |
|
const char *min, const char *max, |
228 |
|
const char *min_reason, const char *max_reason) |
229 |
|
{ |
230 |
770235 |
unsigned u, minv = 0, maxv = 0; |
231 |
|
char *p; |
232 |
|
|
233 |
770235 |
if (arg != NULL && arg != JSON_FMT) { |
234 |
399395 |
if (min != NULL) { |
235 |
353210 |
p = NULL; |
236 |
353210 |
minv = strtoul(min, &p, 0); |
237 |
353210 |
if (*arg == '\0' || *p != '\0') { |
238 |
0 |
VSB_printf(vsb, "Illegal Min: %s\n", min); |
239 |
0 |
return (-1); |
240 |
|
} |
241 |
353210 |
} |
242 |
399395 |
if (max != NULL) { |
243 |
148340 |
p = NULL; |
244 |
148340 |
maxv = strtoul(max, &p, 0); |
245 |
148340 |
if (*arg == '\0' || *p != '\0') { |
246 |
0 |
VSB_printf(vsb, "Illegal Max: %s\n", max); |
247 |
0 |
return (-1); |
248 |
|
} |
249 |
148340 |
} |
250 |
399395 |
p = NULL; |
251 |
399395 |
if (!strcasecmp(arg, "unlimited")) |
252 |
5 |
u = UINT_MAX; |
253 |
|
else { |
254 |
399390 |
u = strtoul(arg, &p, 0); |
255 |
399390 |
if (*arg == '\0' || *p != '\0') { |
256 |
10 |
VSB_printf(vsb, "Not a number (%s)\n", arg); |
257 |
10 |
return (-1); |
258 |
|
} |
259 |
|
} |
260 |
399385 |
if (min != NULL && u < minv) { |
261 |
20 |
VSB_printf(vsb, "Must be at least %s", min); |
262 |
20 |
if (min_reason != NULL) |
263 |
10 |
VSB_printf(vsb, " (%s)", min_reason); |
264 |
20 |
VSB_putc(vsb, '\n'); |
265 |
20 |
return (-1); |
266 |
|
} |
267 |
399365 |
if (max != NULL && u > maxv) { |
268 |
30 |
VSB_printf(vsb, "Must be no more than %s", max); |
269 |
30 |
if (max_reason != NULL) |
270 |
25 |
VSB_printf(vsb, " (%s)", max_reason); |
271 |
30 |
VSB_putc(vsb, '\n'); |
272 |
30 |
return (-1); |
273 |
|
} |
274 |
399335 |
*dest = u; |
275 |
770175 |
} else if (*dest == UINT_MAX && arg != JSON_FMT) { |
276 |
25 |
VSB_cat(vsb, "unlimited"); |
277 |
25 |
} else { |
278 |
370815 |
VSB_printf(vsb, "%u", *dest); |
279 |
|
} |
280 |
770175 |
return (0); |
281 |
770235 |
} |
282 |
|
|
283 |
|
/*--------------------------------------------------------------------*/ |
284 |
|
|
285 |
|
int v_matchproto_(tweak_t) |
286 |
713775 |
tweak_uint(struct vsb *vsb, const struct parspec *par, const char *arg) |
287 |
|
{ |
288 |
|
volatile unsigned *dest; |
289 |
|
|
290 |
713775 |
dest = par->priv; |
291 |
1427550 |
return (tweak_generic_uint(vsb, dest, arg, par->min, par->max, |
292 |
713775 |
par->dyn_min_reason, par->dyn_max_reason)); |
293 |
|
} |
294 |
|
|
295 |
|
int v_matchproto_(tweak_t) |
296 |
30780 |
tweak_uint_orzero(struct vsb *vsb, const struct parspec *par, const char *arg) |
297 |
|
{ |
298 |
|
volatile unsigned *dest; |
299 |
|
|
300 |
30780 |
dest = par->priv; |
301 |
30780 |
if (arg != NULL && arg != JSON_FMT && ! strcmp(arg, "0")) { |
302 |
5115 |
VSB_cat(vsb, "0"); |
303 |
5115 |
*dest = 0; |
304 |
5115 |
return (0); |
305 |
|
} |
306 |
51330 |
return (tweak_generic_uint(vsb, dest, arg, par->min, par->max, |
307 |
25665 |
par->dyn_min_reason, par->dyn_max_reason)); |
308 |
30780 |
} |
309 |
|
|
310 |
|
/*--------------------------------------------------------------------*/ |
311 |
|
|
312 |
|
static void |
313 |
298230 |
fmt_bytes(struct vsb *vsb, uintmax_t t) |
314 |
|
{ |
315 |
|
const char *p; |
316 |
|
|
317 |
298230 |
if (t == 0 || t & 0xff) { |
318 |
102505 |
VSB_printf(vsb, "%jub", t); |
319 |
102505 |
return; |
320 |
|
} |
321 |
267660 |
for (p = "kMGTPEZY"; *p; p++) { |
322 |
267660 |
if (t & 0x300) { |
323 |
25705 |
VSB_printf(vsb, "%.2f%c", t / 1024.0, *p); |
324 |
25705 |
return; |
325 |
|
} |
326 |
241955 |
t /= 1024; |
327 |
241955 |
if (t & 0x0ff) { |
328 |
170020 |
VSB_printf(vsb, "%ju%c", t, *p); |
329 |
170020 |
return; |
330 |
|
} |
331 |
71935 |
} |
332 |
0 |
VSB_cat(vsb, "(bogus number)"); |
333 |
298230 |
} |
334 |
|
|
335 |
|
static int |
336 |
609705 |
tweak_generic_bytes(struct vsb *vsb, volatile ssize_t *dest, const char *arg, |
337 |
|
const char *min, const char *max) |
338 |
|
{ |
339 |
609705 |
uintmax_t r, rmin = 0, rmax = 0; |
340 |
|
const char *p; |
341 |
|
|
342 |
609705 |
if (arg != NULL && arg != JSON_FMT) { |
343 |
311115 |
if (min != NULL) { |
344 |
311115 |
p = VNUM_2bytes(min, &rmin, 0); |
345 |
311115 |
if (p != NULL) { |
346 |
0 |
VSB_printf(vsb, "Invalid min-val: %s\n", min); |
347 |
0 |
return (-1); |
348 |
|
} |
349 |
311115 |
} |
350 |
311115 |
if (max != NULL) { |
351 |
167605 |
p = VNUM_2bytes(max, &rmax, 0); |
352 |
167605 |
if (p != NULL) { |
353 |
0 |
VSB_printf(vsb, "Invalid max-val: %s\n", max); |
354 |
0 |
return (-1); |
355 |
|
} |
356 |
167605 |
} |
357 |
311115 |
p = VNUM_2bytes(arg, &r, 0); |
358 |
311115 |
if (p != NULL) { |
359 |
10 |
VSB_cat(vsb, "Could not convert to bytes.\n"); |
360 |
10 |
VSB_printf(vsb, "%s\n", p); |
361 |
10 |
VSB_cat(vsb, " Try something like '80k' or '120M'\n"); |
362 |
10 |
return (-1); |
363 |
|
} |
364 |
311105 |
if ((uintmax_t)((ssize_t)r) != r) { |
365 |
0 |
fmt_bytes(vsb, r); |
366 |
0 |
VSB_cat(vsb, " is too large for this architecture.\n"); |
367 |
0 |
return (-1); |
368 |
|
} |
369 |
311105 |
if (max != NULL && r > rmax) { |
370 |
5 |
VSB_printf(vsb, "Must be no more than %s\n", max); |
371 |
5 |
VSB_cat(vsb, "\n"); |
372 |
5 |
return (-1); |
373 |
|
} |
374 |
311100 |
if (min != NULL && r < rmin) { |
375 |
5 |
VSB_printf(vsb, "Must be at least %s\n", min); |
376 |
5 |
return (-1); |
377 |
|
} |
378 |
311095 |
*dest = r; |
379 |
609685 |
} else if (arg == JSON_FMT) { |
380 |
360 |
VSB_printf(vsb, "%zd", *dest); |
381 |
360 |
} else { |
382 |
298230 |
fmt_bytes(vsb, *dest); |
383 |
|
} |
384 |
609685 |
return (0); |
385 |
609705 |
} |
386 |
|
|
387 |
|
/*--------------------------------------------------------------------*/ |
388 |
|
|
389 |
|
int v_matchproto_(tweak_t) |
390 |
117745 |
tweak_bytes(struct vsb *vsb, const struct parspec *par, const char *arg) |
391 |
|
{ |
392 |
|
volatile ssize_t *dest; |
393 |
|
|
394 |
117745 |
dest = par->priv; |
395 |
117745 |
return (tweak_generic_bytes(vsb, dest, arg, par->min, par->max)); |
396 |
|
} |
397 |
|
|
398 |
|
/*--------------------------------------------------------------------*/ |
399 |
|
|
400 |
|
int v_matchproto_(tweak_t) |
401 |
440625 |
tweak_bytes_u(struct vsb *vsb, const struct parspec *par, const char *arg) |
402 |
|
{ |
403 |
|
volatile unsigned *d1; |
404 |
|
volatile ssize_t dest; |
405 |
|
|
406 |
440625 |
d1 = par->priv; |
407 |
440625 |
dest = *d1; |
408 |
440625 |
if (tweak_generic_bytes(vsb, &dest, arg, par->min, par->max)) |
409 |
20 |
return (-1); |
410 |
440605 |
*d1 = dest; |
411 |
440605 |
return (0); |
412 |
440625 |
} |
413 |
|
|
414 |
|
/*-------------------------------------------------------------------- |
415 |
|
* vsl_buffer and vsl_reclen have dependencies. |
416 |
|
*/ |
417 |
|
|
418 |
|
int v_matchproto_(tweak_t) |
419 |
20565 |
tweak_vsl_buffer(struct vsb *vsb, const struct parspec *par, const char *arg) |
420 |
|
{ |
421 |
|
volatile unsigned *d1; |
422 |
|
volatile ssize_t dest; |
423 |
|
|
424 |
20565 |
d1 = par->priv; |
425 |
20565 |
dest = *d1; |
426 |
20565 |
if (tweak_generic_bytes(vsb, &dest, arg, par->min, par->max)) |
427 |
0 |
return (-1); |
428 |
20565 |
*d1 = dest; |
429 |
20565 |
MCF_ParamConf(MCF_MAXIMUM, "vsl_reclen", "%u", *d1 - 12); |
430 |
20565 |
return (0); |
431 |
20565 |
} |
432 |
|
|
433 |
|
int v_matchproto_(tweak_t) |
434 |
30770 |
tweak_vsl_reclen(struct vsb *vsb, const struct parspec *par, const char *arg) |
435 |
|
{ |
436 |
|
volatile unsigned *d1; |
437 |
|
volatile ssize_t dest; |
438 |
|
|
439 |
30770 |
d1 = par->priv; |
440 |
30770 |
dest = *d1; |
441 |
30770 |
if (tweak_generic_bytes(vsb, &dest, arg, par->min, par->max)) |
442 |
0 |
return (-1); |
443 |
30770 |
*d1 = dest; |
444 |
30770 |
MCF_ParamConf(MCF_MINIMUM, "vsl_buffer", "%u", *d1 + 12); |
445 |
30770 |
return (0); |
446 |
30770 |
} |
447 |
|
|
448 |
|
/*--------------------------------------------------------------------*/ |
449 |
|
|
450 |
|
int v_matchproto_(tweak_t) |
451 |
56305 |
tweak_string(struct vsb *vsb, const struct parspec *par, const char *arg) |
452 |
|
{ |
453 |
56305 |
char **p = TRUST_ME(par->priv); |
454 |
|
|
455 |
56305 |
AN(p); |
456 |
56305 |
if (arg == NULL) { |
457 |
25905 |
VSB_quote(vsb, *p, -1, 0); |
458 |
56305 |
} else if (arg == JSON_FMT) { |
459 |
75 |
VSB_putc(vsb, '"'); |
460 |
75 |
VSB_quote(vsb, *p, -1, VSB_QUOTE_JSON); |
461 |
75 |
VSB_putc(vsb, '"'); |
462 |
75 |
} else { |
463 |
30325 |
REPLACE(*p, arg); |
464 |
|
} |
465 |
56305 |
return (0); |
466 |
|
} |
467 |
|
|
468 |
|
/*--------------------------------------------------------------------*/ |
469 |
|
|
470 |
|
int v_matchproto_(tweak_t) |
471 |
31015 |
tweak_poolparam(struct vsb *vsb, const struct parspec *par, const char *arg) |
472 |
|
{ |
473 |
|
volatile struct poolparam *pp, px; |
474 |
|
struct parspec pt; |
475 |
|
char **av; |
476 |
31015 |
int retval = 0; |
477 |
|
|
478 |
31015 |
pp = par->priv; |
479 |
31015 |
if (arg == JSON_FMT) { |
480 |
55 |
VSB_cat(vsb, "{\n"); |
481 |
55 |
VSB_indent(vsb, 8); |
482 |
55 |
VSB_printf(vsb, "\"min_pool\": %u,\n", pp->min_pool); |
483 |
55 |
VSB_printf(vsb, "\"max_pool\": %u,\n", pp->max_pool); |
484 |
55 |
VSB_printf(vsb, "\"max_age\": %g\n", pp->max_age); |
485 |
55 |
VSB_indent(vsb, -4); |
486 |
55 |
VSB_cat(vsb, "}"); |
487 |
31015 |
} else if (arg == NULL) { |
488 |
31100 |
VSB_printf(vsb, "%u,%u,%g", |
489 |
15550 |
pp->min_pool, pp->max_pool, pp->max_age); |
490 |
15550 |
} else { |
491 |
15410 |
av = VAV_Parse(arg, NULL, ARGV_COMMA); |
492 |
15410 |
do { |
493 |
15410 |
if (av[0] != NULL) { |
494 |
5 |
VSB_printf(vsb, "Parse error: %s", av[0]); |
495 |
5 |
retval = -1; |
496 |
5 |
break; |
497 |
|
} |
498 |
15405 |
if (av[1] == NULL || av[2] == NULL || av[3] == NULL) { |
499 |
5 |
VSB_cat(vsb, |
500 |
|
"Three fields required:" |
501 |
|
" min_pool, max_pool and max_age\n"); |
502 |
5 |
retval = -1; |
503 |
5 |
break; |
504 |
|
} |
505 |
15400 |
px = *pp; |
506 |
30800 |
retval = tweak_generic_uint(vsb, &px.min_pool, av[1], |
507 |
15400 |
par->min, par->max, par->dyn_min_reason, |
508 |
15400 |
par->dyn_max_reason); |
509 |
15400 |
if (retval) |
510 |
5 |
break; |
511 |
30790 |
retval = tweak_generic_uint(vsb, &px.max_pool, av[2], |
512 |
15395 |
par->min, par->max, par->dyn_min_reason, |
513 |
15395 |
par->dyn_max_reason); |
514 |
15395 |
if (retval) |
515 |
5 |
break; |
516 |
15390 |
pt.priv = &px.max_age; |
517 |
15390 |
pt.min = "0"; |
518 |
15390 |
pt.max = "1000000"; |
519 |
15390 |
retval = tweak_generic_double(vsb, av[3], &pt, |
520 |
|
parse_decimal, "%.0f"); |
521 |
15390 |
if (retval) |
522 |
5 |
break; |
523 |
15385 |
if (px.min_pool > px.max_pool) { |
524 |
5 |
VSB_cat(vsb, |
525 |
|
"min_pool cannot be larger" |
526 |
|
" than max_pool\n"); |
527 |
5 |
retval = -1; |
528 |
5 |
break; |
529 |
|
} |
530 |
15380 |
*pp = px; |
531 |
15380 |
} while (0); |
532 |
15410 |
VAV_Free(av); |
533 |
|
} |
534 |
31015 |
return (retval); |
535 |
|
} |
536 |
|
|
537 |
|
/*-------------------------------------------------------------------- |
538 |
|
* Thread pool tweaks. |
539 |
|
* |
540 |
|
* The min/max values automatically update the opposites appropriate |
541 |
|
* limit, so they don't end up crossing. |
542 |
|
*/ |
543 |
|
|
544 |
|
int v_matchproto_(tweak_t) |
545 |
35545 |
tweak_thread_pool_min(struct vsb *vsb, const struct parspec *par, |
546 |
|
const char *arg) |
547 |
|
{ |
548 |
35545 |
if (tweak_uint(vsb, par, arg)) |
549 |
10 |
return (-1); |
550 |
|
|
551 |
35535 |
MCF_ParamConf(MCF_MINIMUM, "thread_pool_max", |
552 |
35535 |
"%u", mgt_param.wthread_min); |
553 |
35535 |
MCF_ParamConf(MCF_MAXIMUM, "thread_pool_reserve", |
554 |
35535 |
"%u", mgt_param.wthread_min * 950 / 1000); |
555 |
35535 |
return (0); |
556 |
35545 |
} |
557 |
|
|
558 |
|
int v_matchproto_(tweak_t) |
559 |
10395 |
tweak_thread_pool_max(struct vsb *vsb, const struct parspec *par, |
560 |
|
const char *arg) |
561 |
|
{ |
562 |
|
|
563 |
10395 |
if (tweak_uint(vsb, par, arg)) |
564 |
10 |
return (-1); |
565 |
|
|
566 |
10385 |
MCF_ParamConf(MCF_MAXIMUM, "thread_pool_min", |
567 |
10385 |
"%u", mgt_param.wthread_max); |
568 |
10385 |
return (0); |
569 |
10395 |
} |
570 |
|
|
571 |
|
/*-------------------------------------------------------------------- |
572 |
|
* Tweak storage |
573 |
|
*/ |
574 |
|
|
575 |
|
int v_matchproto_(tweak_t) |
576 |
10315 |
tweak_storage(struct vsb *vsb, const struct parspec *par, const char *arg) |
577 |
|
{ |
578 |
|
struct stevedore *stv; |
579 |
|
|
580 |
|
/* XXX: If we want to remove the MUST_RESTART flag from the |
581 |
|
* h2_rxbuf_storage parameter, we could have a mechanism here |
582 |
|
* that when the child is running calls out through CLI to change |
583 |
|
* the stevedore being used. */ |
584 |
|
|
585 |
10315 |
if (arg == NULL || arg == JSON_FMT) |
586 |
5195 |
return (tweak_string(vsb, par, arg)); |
587 |
|
|
588 |
5120 |
if (!strcmp(arg, "Transient")) { |
589 |
|
/* Always allow setting to the special name |
590 |
|
* "Transient". There will always be a stevedore with this |
591 |
|
* name, but it may not have been configured at the time |
592 |
|
* this is called. */ |
593 |
5115 |
} else { |
594 |
|
/* Only allow setting the value to a known configured |
595 |
|
* stevedore */ |
596 |
5 |
STV_Foreach(stv) { |
597 |
5 |
CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC); |
598 |
5 |
if (!strcmp(stv->ident, arg)) |
599 |
5 |
break; |
600 |
|
} |
601 |
5 |
if (stv == NULL) { |
602 |
0 |
VSB_printf(vsb, "unknown storage backend '%s'", arg); |
603 |
0 |
return (-1); |
604 |
|
} |
605 |
|
} |
606 |
5120 |
return (tweak_string(vsb, par, arg)); |
607 |
10315 |
} |
608 |
|
|
609 |
|
/*-------------------------------------------------------------------- |
610 |
|
* Tweak alias |
611 |
|
*/ |
612 |
|
|
613 |
|
int v_matchproto_(tweak_t) |
614 |
30 |
tweak_alias(struct vsb *vsb, const struct parspec *par, const char *arg) |
615 |
|
{ |
616 |
|
const struct parspec *orig; |
617 |
|
struct parspec alias[1]; |
618 |
|
|
619 |
30 |
orig = TRUST_ME(par->priv); |
620 |
30 |
AN(orig); |
621 |
30 |
memcpy(alias, orig, sizeof *orig); |
622 |
30 |
alias->name = par->name; |
623 |
30 |
alias->priv = TRUST_ME(orig); |
624 |
30 |
return (alias->func(vsb, alias, arg)); |
625 |
|
} |
626 |
|
|
627 |
|
/*-------------------------------------------------------------------- |
628 |
|
* Tweak bits |
629 |
|
*/ |
630 |
|
|
631 |
|
enum bit_do {BSET, BCLR, BTST}; |
632 |
|
|
633 |
|
static int |
634 |
1640300 |
bit(uint8_t *p, unsigned no, enum bit_do act) |
635 |
|
{ |
636 |
|
uint8_t b; |
637 |
|
|
638 |
1640300 |
p += (no >> 3); |
639 |
1640300 |
b = (0x80 >> (no & 7)); |
640 |
1640300 |
if (act == BSET) |
641 |
98160 |
*p |= b; |
642 |
1542140 |
else if (act == BCLR) |
643 |
14220 |
*p &= ~b; |
644 |
1640300 |
return (*p & b); |
645 |
|
} |
646 |
|
|
647 |
|
static inline void |
648 |
25590 |
bit_clear(uint8_t *p, unsigned l) |
649 |
|
{ |
650 |
|
|
651 |
25590 |
memset(p, 0, ((size_t)l + 7) >> 3); |
652 |
25590 |
} |
653 |
|
|
654 |
|
/*-------------------------------------------------------------------- |
655 |
|
*/ |
656 |
|
|
657 |
|
static int |
658 |
36440 |
bit_tweak(struct vsb *vsb, uint8_t *p, unsigned l, const char *arg, |
659 |
|
const char * const *tags, const char *desc, char sign) |
660 |
|
{ |
661 |
|
int i, n; |
662 |
|
unsigned j; |
663 |
|
char **av; |
664 |
|
const char *s; |
665 |
|
|
666 |
36440 |
av = VAV_Parse(arg, &n, ARGV_COMMA); |
667 |
36440 |
if (av[0] != NULL) { |
668 |
5 |
VSB_printf(vsb, "Cannot parse: %s\n", av[0]); |
669 |
5 |
VAV_Free(av); |
670 |
5 |
return (-1); |
671 |
|
} |
672 |
174405 |
for (i = 1; av[i] != NULL; i++) { |
673 |
137980 |
s = av[i]; |
674 |
137980 |
if (sign == '+' && !strcmp(s, "none")) { |
675 |
20465 |
bit_clear(p, l); |
676 |
20465 |
continue; |
677 |
|
} |
678 |
117515 |
if (sign == '-' && !strcmp(s, "all")) { |
679 |
5125 |
bit_clear(p, l); |
680 |
5125 |
continue; |
681 |
|
} |
682 |
112390 |
if (*s != '-' && *s != '+') { |
683 |
5 |
VSB_printf(vsb, "Missing '+' or '-' (%s)\n", s); |
684 |
5 |
VAV_Free(av); |
685 |
5 |
return (-1); |
686 |
|
} |
687 |
5768815 |
for (j = 0; j < l; j++) { |
688 |
5768810 |
if (tags[j] != NULL && !strcasecmp(s + 1, tags[j])) |
689 |
112380 |
break; |
690 |
5656430 |
} |
691 |
112385 |
if (tags[j] == NULL) { |
692 |
5 |
VSB_printf(vsb, "Unknown %s (%s)\n", desc, s); |
693 |
5 |
VAV_Free(av); |
694 |
5 |
return (-1); |
695 |
|
} |
696 |
112380 |
assert(j < l); |
697 |
112380 |
if (s[0] == sign) |
698 |
98160 |
(void)bit(p, j, BSET); |
699 |
|
else |
700 |
14220 |
(void)bit(p, j, BCLR); |
701 |
112380 |
} |
702 |
36425 |
VAV_Free(av); |
703 |
36425 |
return (0); |
704 |
36440 |
} |
705 |
|
|
706 |
|
|
707 |
|
/*-------------------------------------------------------------------- |
708 |
|
*/ |
709 |
|
|
710 |
|
static int |
711 |
62515 |
tweak_generic_bits(struct vsb *vsb, const struct parspec *par, const char *arg, |
712 |
|
uint8_t *p, unsigned l, const char * const *tags, const char *desc, |
713 |
|
char sign) |
714 |
|
{ |
715 |
|
unsigned j; |
716 |
|
|
717 |
62515 |
if (arg != NULL && !strcmp(arg, "default")) { |
718 |
|
/* XXX: deprecated in favor of param.reset */ |
719 |
0 |
return (tweak_generic_bits(vsb, par, par->def, p, l, tags, |
720 |
0 |
desc, sign)); |
721 |
|
} |
722 |
|
|
723 |
62515 |
if (arg != NULL && arg != JSON_FMT) |
724 |
36440 |
return (bit_tweak(vsb, p, l, arg, tags, desc, sign)); |
725 |
|
|
726 |
26075 |
if (arg == JSON_FMT) |
727 |
95 |
VSB_putc(vsb, '"'); |
728 |
26075 |
VSB_cat(vsb, sign == '+' ? "none" : "all"); |
729 |
1553995 |
for (j = 0; j < l; j++) { |
730 |
1527920 |
if (bit(p, j, BTST)) |
731 |
93830 |
VSB_printf(vsb, ",%c%s", sign, tags[j]); |
732 |
1527920 |
} |
733 |
26075 |
if (arg == JSON_FMT) |
734 |
95 |
VSB_putc(vsb, '"'); |
735 |
26075 |
return (0); |
736 |
62515 |
} |
737 |
|
|
738 |
|
/*-------------------------------------------------------------------- |
739 |
|
* The vsl_mask parameter |
740 |
|
*/ |
741 |
|
|
742 |
|
static const char * const VSL_tags[256] = { |
743 |
|
# define SLTM(foo,flags,sdesc,ldesc) [SLT_##foo] = #foo, |
744 |
|
# include "tbl/vsl_tags.h" |
745 |
|
}; |
746 |
|
|
747 |
|
int v_matchproto_(tweak_t) |
748 |
15185 |
tweak_vsl_mask(struct vsb *vsb, const struct parspec *par, const char *arg) |
749 |
|
{ |
750 |
|
|
751 |
15185 |
return (tweak_generic_bits(vsb, par, arg, mgt_param.vsl_mask, |
752 |
|
SLT__Reserved, VSL_tags, "VSL tag", '-')); |
753 |
|
} |
754 |
|
|
755 |
|
/*-------------------------------------------------------------------- |
756 |
|
* The debug parameter |
757 |
|
*/ |
758 |
|
|
759 |
|
static const char * const debug_tags[] = { |
760 |
|
# define DEBUG_BIT(U, l, d) [DBG_##U] = #l, |
761 |
|
# include "tbl/debug_bits.h" |
762 |
|
NULL |
763 |
|
}; |
764 |
|
|
765 |
|
int v_matchproto_(tweak_t) |
766 |
15640 |
tweak_debug(struct vsb *vsb, const struct parspec *par, const char *arg) |
767 |
|
{ |
768 |
|
|
769 |
15640 |
return (tweak_generic_bits(vsb, par, arg, mgt_param.debug_bits, |
770 |
|
DBG_Reserved, debug_tags, "debug bit", '+')); |
771 |
|
} |
772 |
|
|
773 |
|
/*-------------------------------------------------------------------- |
774 |
|
* The experimental parameter |
775 |
|
*/ |
776 |
|
|
777 |
|
static const char * const experimental_tags[] = { |
778 |
|
# define EXPERIMENTAL_BIT(U, l, d) [EXPERIMENT_##U] = #l, |
779 |
|
# include "tbl/experimental_bits.h" |
780 |
|
NULL |
781 |
|
}; |
782 |
|
|
783 |
|
int v_matchproto_(tweak_t) |
784 |
10320 |
tweak_experimental(struct vsb *vsb, const struct parspec *par, const char *arg) |
785 |
|
{ |
786 |
|
|
787 |
10320 |
return (tweak_generic_bits(vsb, par, arg, mgt_param.experimental_bits, |
788 |
|
EXPERIMENT_Reserved, experimental_tags, "experimental bit", '+')); |
789 |
|
} |
790 |
|
|
791 |
|
/*-------------------------------------------------------------------- |
792 |
|
* The feature parameter |
793 |
|
*/ |
794 |
|
|
795 |
|
static const char * const feature_tags[] = { |
796 |
|
# define FEATURE_BIT(U, l, d) [FEATURE_##U] = #l, |
797 |
|
# include "tbl/feature_bits.h" |
798 |
|
NULL |
799 |
|
}; |
800 |
|
|
801 |
|
int v_matchproto_(tweak_t) |
802 |
10910 |
tweak_feature(struct vsb *vsb, const struct parspec *par, const char *arg) |
803 |
|
{ |
804 |
|
|
805 |
10910 |
return (tweak_generic_bits(vsb, par, arg, mgt_param.feature_bits, |
806 |
|
FEATURE_Reserved, feature_tags, "feature bit", '+')); |
807 |
|
} |
808 |
|
|
809 |
|
/*-------------------------------------------------------------------- |
810 |
|
* The vcc_feature parameter |
811 |
|
*/ |
812 |
|
|
813 |
|
static const char * const vcc_feature_tags[] = { |
814 |
|
# define VCC_FEATURE_BIT(U, l, d) [VCC_FEATURE_##U] = #l, |
815 |
|
# include "tbl/vcc_feature_bits.h" |
816 |
|
NULL |
817 |
|
}; |
818 |
|
|
819 |
|
int v_matchproto_(tweak_t) |
820 |
10460 |
tweak_vcc_feature(struct vsb *vsb, const struct parspec *par, const char *arg) |
821 |
|
{ |
822 |
|
const struct parspec *orig; |
823 |
|
char buf[32]; |
824 |
|
int val; |
825 |
|
|
826 |
10460 |
if (arg != NULL && arg != JSON_FMT && |
827 |
5250 |
strcmp(par->name, "vcc_feature")) { |
828 |
0 |
orig = TRUST_ME(par->priv); |
829 |
0 |
val = parse_boolean(vsb, arg); |
830 |
0 |
if (val < 0) |
831 |
0 |
return (-1); |
832 |
0 |
bprintf(buf, "%c%s", val ? '+' : '-', |
833 |
|
par->name + strlen("vcc_")); |
834 |
0 |
return (tweak_vcc_feature(vsb, orig, buf)); |
835 |
|
} |
836 |
10460 |
return (tweak_generic_bits(vsb, par, arg, mgt_param.vcc_feature_bits, |
837 |
|
VCC_FEATURE_Reserved, vcc_feature_tags, "vcc_feature bit", '+')); |
838 |
10460 |
} |