| | 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 |
760800 |
parse_decimal(const char *p, const char **err) |
| 60 |
|
{ |
| 61 |
|
double v; |
| 62 |
|
|
| 63 |
760800 |
v = SF_Parse_Decimal(&p, 0, err); |
| 64 |
760800 |
if (errno == 0 && *p != '\0') { |
| 65 |
40 |
errno = EINVAL; |
| 66 |
40 |
*err = "Invalid number"; |
| 67 |
40 |
} |
| 68 |
760800 |
return (v); |
| 69 |
|
} |
| 70 |
|
|
| 71 |
|
static int |
| 72 |
7839840 |
tweak_generic_double(struct vsb *vsb, const char *arg, const struct parspec *pp, |
| 73 |
|
parse_double_f parse, const char *fmt) |
| 74 |
|
{ |
| 75 |
7839840 |
volatile double u, minv = VRT_DECIMAL_MIN, maxv = VRT_DECIMAL_MAX; |
| 76 |
7839840 |
volatile double *dest = pp->priv; |
| 77 |
|
const char *err; |
| 78 |
|
|
| 79 |
7839840 |
if (arg != NULL && arg != JSON_FMT) { |
| 80 |
3973000 |
if (pp->min != NULL) { |
| 81 |
3973000 |
minv = parse(pp->min, &err); |
| 82 |
3973000 |
if (errno) { |
| 83 |
0 |
VSB_printf(vsb, "Min: %s (%s)\n", err, pp->min); |
| 84 |
0 |
return (-1); |
| 85 |
|
} |
| 86 |
3973000 |
} |
| 87 |
3973000 |
if (pp->max != NULL) { |
| 88 |
1267240 |
maxv = parse(pp->max, &err); |
| 89 |
1267240 |
if (errno) { |
| 90 |
0 |
VSB_printf(vsb, "Max: %s (%s)\n", err, pp->max); |
| 91 |
0 |
return (-1); |
| 92 |
|
} |
| 93 |
1267240 |
} |
| 94 |
|
|
| 95 |
3973000 |
u = parse(arg, &err); |
| 96 |
3973000 |
if (errno) { |
| 97 |
160 |
VSB_printf(vsb, "%s (%s)\n", err, arg); |
| 98 |
160 |
return (-1); |
| 99 |
|
} |
| 100 |
3972840 |
if (u < minv) { |
| 101 |
80 |
VSB_printf(vsb, |
| 102 |
40 |
"Must be greater or equal to %s\n", pp->min); |
| 103 |
40 |
return (-1); |
| 104 |
|
} |
| 105 |
3972800 |
if (u > maxv) { |
| 106 |
80 |
VSB_printf(vsb, |
| 107 |
40 |
"Must be less than or equal to %s\n", pp->max); |
| 108 |
40 |
return (-1); |
| 109 |
|
} |
| 110 |
3972760 |
*dest = u; |
| 111 |
3972760 |
} else { |
| 112 |
3866840 |
VSB_printf(vsb, fmt, *dest); |
| 113 |
|
} |
| 114 |
7839600 |
return (0); |
| 115 |
7839840 |
} |
| 116 |
|
|
| 117 |
|
/*--------------------------------------------------------------------*/ |
| 118 |
|
|
| 119 |
|
static double |
| 120 |
8452440 |
parse_duration(const char *p, const char **err) |
| 121 |
|
{ |
| 122 |
|
double v, r; |
| 123 |
|
|
| 124 |
8452440 |
v = SF_Parse_Decimal(&p, 0, err); |
| 125 |
8452440 |
if (*p == '\0') |
| 126 |
7733760 |
return (v); |
| 127 |
|
|
| 128 |
718680 |
r = VNUM_duration_unit(v, p, NULL); |
| 129 |
718680 |
if (isnan(r)) { |
| 130 |
80 |
errno = EINVAL; |
| 131 |
80 |
*err = "Invalid duration unit"; |
| 132 |
80 |
} |
| 133 |
|
|
| 134 |
718680 |
return (r); |
| 135 |
8452440 |
} |
| 136 |
|
|
| 137 |
|
int v_matchproto_(tweak_t) |
| 138 |
1950840 |
tweak_timeout(struct vsb *vsb, const struct parspec *par, const char *arg) |
| 139 |
|
{ |
| 140 |
1950840 |
volatile double *dest = par->priv; |
| 141 |
|
|
| 142 |
1950840 |
if (arg != NULL && !strcmp(arg, "never")) { |
| 143 |
40 |
*dest = INFINITY; |
| 144 |
40 |
return (0); |
| 145 |
|
} |
| 146 |
|
|
| 147 |
1950800 |
if (*dest == INFINITY && arg == NULL) { |
| 148 |
0 |
VSB_cat(vsb, "never"); |
| 149 |
0 |
return (0); |
| 150 |
|
} |
| 151 |
|
|
| 152 |
1950800 |
if (*dest == INFINITY && arg == JSON_FMT) { |
| 153 |
0 |
VSB_cat(vsb, "\"never\""); |
| 154 |
0 |
return (0); |
| 155 |
|
} |
| 156 |
|
|
| 157 |
1950800 |
return (tweak_generic_double(vsb, arg, par, parse_duration, "%.3f")); |
| 158 |
1950840 |
} |
| 159 |
|
|
| 160 |
|
int v_matchproto_(tweak_t) |
| 161 |
5508200 |
tweak_duration(struct vsb *vsb, const struct parspec *par, const char *arg) |
| 162 |
|
{ |
| 163 |
|
|
| 164 |
5508200 |
return (tweak_generic_double(vsb, arg, par, parse_duration, "%.3f")); |
| 165 |
|
} |
| 166 |
|
|
| 167 |
|
/*--------------------------------------------------------------------*/ |
| 168 |
|
|
| 169 |
|
int v_matchproto_(tweak_t) |
| 170 |
253880 |
tweak_double(struct vsb *vsb, const struct parspec *par, const char *arg) |
| 171 |
|
{ |
| 172 |
|
|
| 173 |
253880 |
return (tweak_generic_double(vsb, arg, par, parse_decimal, "%g")); |
| 174 |
|
} |
| 175 |
|
|
| 176 |
|
/*--------------------------------------------------------------------*/ |
| 177 |
|
|
| 178 |
|
static int |
| 179 |
500280 |
parse_boolean(struct vsb *vsb, const char *arg) |
| 180 |
|
{ |
| 181 |
|
|
| 182 |
500280 |
if (!strcasecmp(arg, "off")) |
| 183 |
161680 |
return (0); |
| 184 |
338600 |
if (!strcasecmp(arg, "disable")) |
| 185 |
40 |
return (0); |
| 186 |
338560 |
if (!strcasecmp(arg, "no")) |
| 187 |
40 |
return (0); |
| 188 |
338520 |
if (!strcasecmp(arg, "false")) |
| 189 |
160 |
return (0); |
| 190 |
338360 |
if (!strcasecmp(arg, "on")) |
| 191 |
337800 |
return (1); |
| 192 |
560 |
if (!strcasecmp(arg, "enable")) |
| 193 |
40 |
return (1); |
| 194 |
520 |
if (!strcasecmp(arg, "yes")) |
| 195 |
40 |
return (1); |
| 196 |
480 |
if (!strcasecmp(arg, "true")) |
| 197 |
440 |
return (1); |
| 198 |
|
|
| 199 |
40 |
VSB_cat(vsb, "use \"on\" or \"off\"\n"); |
| 200 |
40 |
return (-1); |
| 201 |
500280 |
} |
| 202 |
|
|
| 203 |
|
int v_matchproto_(tweak_t) |
| 204 |
928680 |
tweak_boolean(struct vsb *vsb, const struct parspec *par, const char *arg) |
| 205 |
|
{ |
| 206 |
|
volatile unsigned *dest; |
| 207 |
|
int val; |
| 208 |
|
|
| 209 |
928680 |
dest = par->priv; |
| 210 |
928680 |
if (arg != NULL && arg != JSON_FMT) { |
| 211 |
500280 |
val = parse_boolean(vsb, arg); |
| 212 |
500280 |
if (val < 0) |
| 213 |
40 |
return (-1); |
| 214 |
500240 |
*dest = val; |
| 215 |
928640 |
} else if (arg == JSON_FMT) { |
| 216 |
1200 |
VSB_printf(vsb, "%s", *dest ? "true" : "false"); |
| 217 |
1200 |
} else { |
| 218 |
427200 |
VSB_printf(vsb, "%s", *dest ? "on" : "off"); |
| 219 |
|
} |
| 220 |
928640 |
return (0); |
| 221 |
928680 |
} |
| 222 |
|
|
| 223 |
|
/*--------------------------------------------------------------------*/ |
| 224 |
|
|
| 225 |
|
static int |
| 226 |
6353800 |
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 |
6353800 |
unsigned u, minv = 0, maxv = 0; |
| 231 |
|
char *p; |
| 232 |
|
|
| 233 |
6353800 |
if (arg != NULL && arg != JSON_FMT) { |
| 234 |
3294920 |
if (min != NULL) { |
| 235 |
2913920 |
p = NULL; |
| 236 |
2913920 |
minv = strtoul(min, &p, 0); |
| 237 |
2913920 |
if (*arg == '\0' || *p != '\0') { |
| 238 |
0 |
VSB_printf(vsb, "Illegal Min: %s\n", min); |
| 239 |
0 |
return (-1); |
| 240 |
|
} |
| 241 |
2913920 |
} |
| 242 |
3294920 |
if (max != NULL) { |
| 243 |
1223760 |
p = NULL; |
| 244 |
1223760 |
maxv = strtoul(max, &p, 0); |
| 245 |
1223760 |
if (*arg == '\0' || *p != '\0') { |
| 246 |
0 |
VSB_printf(vsb, "Illegal Max: %s\n", max); |
| 247 |
0 |
return (-1); |
| 248 |
|
} |
| 249 |
1223760 |
} |
| 250 |
3294920 |
p = NULL; |
| 251 |
3294920 |
if (!strcasecmp(arg, "unlimited")) |
| 252 |
40 |
u = UINT_MAX; |
| 253 |
|
else { |
| 254 |
3294880 |
u = strtoul(arg, &p, 0); |
| 255 |
3294880 |
if (*arg == '\0' || *p != '\0') { |
| 256 |
80 |
VSB_printf(vsb, "Not a number (%s)\n", arg); |
| 257 |
80 |
return (-1); |
| 258 |
|
} |
| 259 |
|
} |
| 260 |
3294840 |
if (min != NULL && u < minv) { |
| 261 |
160 |
VSB_printf(vsb, "Must be at least %s", min); |
| 262 |
160 |
if (min_reason != NULL) |
| 263 |
80 |
VSB_printf(vsb, " (%s)", min_reason); |
| 264 |
160 |
VSB_putc(vsb, '\n'); |
| 265 |
160 |
return (-1); |
| 266 |
|
} |
| 267 |
3294680 |
if (max != NULL && u > maxv) { |
| 268 |
240 |
VSB_printf(vsb, "Must be no more than %s", max); |
| 269 |
240 |
if (max_reason != NULL) |
| 270 |
200 |
VSB_printf(vsb, " (%s)", max_reason); |
| 271 |
240 |
VSB_putc(vsb, '\n'); |
| 272 |
240 |
return (-1); |
| 273 |
|
} |
| 274 |
3294440 |
*dest = u; |
| 275 |
6353320 |
} else if (*dest == UINT_MAX && arg != JSON_FMT) { |
| 276 |
200 |
VSB_cat(vsb, "unlimited"); |
| 277 |
200 |
} else { |
| 278 |
3058680 |
VSB_printf(vsb, "%u", *dest); |
| 279 |
|
} |
| 280 |
6353320 |
return (0); |
| 281 |
6353800 |
} |
| 282 |
|
|
| 283 |
|
/*--------------------------------------------------------------------*/ |
| 284 |
|
|
| 285 |
|
int v_matchproto_(tweak_t) |
| 286 |
5888040 |
tweak_uint(struct vsb *vsb, const struct parspec *par, const char *arg) |
| 287 |
|
{ |
| 288 |
|
volatile unsigned *dest; |
| 289 |
|
|
| 290 |
5888040 |
dest = par->priv; |
| 291 |
11776080 |
return (tweak_generic_uint(vsb, dest, arg, par->min, par->max, |
| 292 |
5888040 |
par->dyn_min_reason, par->dyn_max_reason)); |
| 293 |
|
} |
| 294 |
|
|
| 295 |
|
int v_matchproto_(tweak_t) |
| 296 |
253920 |
tweak_uint_orzero(struct vsb *vsb, const struct parspec *par, const char *arg) |
| 297 |
|
{ |
| 298 |
|
volatile unsigned *dest; |
| 299 |
|
|
| 300 |
253920 |
dest = par->priv; |
| 301 |
253920 |
if (arg != NULL && arg != JSON_FMT && ! strcmp(arg, "0")) { |
| 302 |
42200 |
VSB_cat(vsb, "0"); |
| 303 |
42200 |
*dest = 0; |
| 304 |
42200 |
return (0); |
| 305 |
|
} |
| 306 |
423440 |
return (tweak_generic_uint(vsb, dest, arg, par->min, par->max, |
| 307 |
211720 |
par->dyn_min_reason, par->dyn_max_reason)); |
| 308 |
253920 |
} |
| 309 |
|
|
| 310 |
|
/*--------------------------------------------------------------------*/ |
| 311 |
|
|
| 312 |
|
static void |
| 313 |
2460080 |
fmt_bytes(struct vsb *vsb, uintmax_t t) |
| 314 |
|
{ |
| 315 |
|
const char *p; |
| 316 |
|
|
| 317 |
2460080 |
if (t == 0 || t & 0xff) { |
| 318 |
845640 |
VSB_printf(vsb, "%jub", t); |
| 319 |
845640 |
return; |
| 320 |
|
} |
| 321 |
2207840 |
for (p = "kMGTPEZY"; *p; p++) { |
| 322 |
2207840 |
if (t & 0x300) { |
| 323 |
212040 |
VSB_printf(vsb, "%.2f%c", t / 1024.0, *p); |
| 324 |
212040 |
return; |
| 325 |
|
} |
| 326 |
1995800 |
t /= 1024; |
| 327 |
1995800 |
if (t & 0x0ff) { |
| 328 |
1402400 |
VSB_printf(vsb, "%ju%c", t, *p); |
| 329 |
1402400 |
return; |
| 330 |
|
} |
| 331 |
593400 |
} |
| 332 |
0 |
VSB_cat(vsb, "(bogus number)"); |
| 333 |
2460080 |
} |
| 334 |
|
|
| 335 |
|
static int |
| 336 |
5029560 |
tweak_generic_bytes(struct vsb *vsb, volatile ssize_t *dest, const char *arg, |
| 337 |
|
const char *min, const char *max) |
| 338 |
|
{ |
| 339 |
5029560 |
uintmax_t r, rmin = 0, rmax = 0; |
| 340 |
|
const char *p; |
| 341 |
|
|
| 342 |
5029560 |
if (arg != NULL && arg != JSON_FMT) { |
| 343 |
2566600 |
if (min != NULL) { |
| 344 |
2566600 |
p = VNUM_2bytes(min, &rmin, 0); |
| 345 |
2566600 |
if (p != NULL) { |
| 346 |
0 |
VSB_printf(vsb, "Invalid min-val: %s\n", min); |
| 347 |
0 |
return (-1); |
| 348 |
|
} |
| 349 |
2566600 |
} |
| 350 |
2566600 |
if (max != NULL) { |
| 351 |
1382600 |
p = VNUM_2bytes(max, &rmax, 0); |
| 352 |
1382600 |
if (p != NULL) { |
| 353 |
0 |
VSB_printf(vsb, "Invalid max-val: %s\n", max); |
| 354 |
0 |
return (-1); |
| 355 |
|
} |
| 356 |
1382600 |
} |
| 357 |
2566600 |
p = VNUM_2bytes(arg, &r, 0); |
| 358 |
2566600 |
if (p != NULL) { |
| 359 |
80 |
VSB_cat(vsb, "Could not convert to bytes.\n"); |
| 360 |
80 |
VSB_printf(vsb, "%s\n", p); |
| 361 |
80 |
VSB_cat(vsb, " Try something like '80k' or '120M'\n"); |
| 362 |
80 |
return (-1); |
| 363 |
|
} |
| 364 |
2566520 |
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 |
2566520 |
if (max != NULL && r > rmax) { |
| 370 |
40 |
VSB_printf(vsb, "Must be no more than %s\n", max); |
| 371 |
40 |
VSB_cat(vsb, "\n"); |
| 372 |
40 |
return (-1); |
| 373 |
|
} |
| 374 |
2566480 |
if (min != NULL && r < rmin) { |
| 375 |
40 |
VSB_printf(vsb, "Must be at least %s\n", min); |
| 376 |
40 |
return (-1); |
| 377 |
|
} |
| 378 |
2566440 |
*dest = r; |
| 379 |
5029400 |
} else if (arg == JSON_FMT) { |
| 380 |
2880 |
VSB_printf(vsb, "%zd", *dest); |
| 381 |
2880 |
} else { |
| 382 |
2460080 |
fmt_bytes(vsb, *dest); |
| 383 |
|
} |
| 384 |
5029400 |
return (0); |
| 385 |
5029560 |
} |
| 386 |
|
|
| 387 |
|
/*--------------------------------------------------------------------*/ |
| 388 |
|
|
| 389 |
|
int v_matchproto_(tweak_t) |
| 390 |
971320 |
tweak_bytes(struct vsb *vsb, const struct parspec *par, const char *arg) |
| 391 |
|
{ |
| 392 |
|
volatile ssize_t *dest; |
| 393 |
|
|
| 394 |
971320 |
dest = par->priv; |
| 395 |
971320 |
return (tweak_generic_bytes(vsb, dest, arg, par->min, par->max)); |
| 396 |
|
} |
| 397 |
|
|
| 398 |
|
/*--------------------------------------------------------------------*/ |
| 399 |
|
|
| 400 |
|
int v_matchproto_(tweak_t) |
| 401 |
3634760 |
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 |
3634760 |
d1 = par->priv; |
| 407 |
3634760 |
dest = *d1; |
| 408 |
3634760 |
if (tweak_generic_bytes(vsb, &dest, arg, par->min, par->max)) |
| 409 |
160 |
return (-1); |
| 410 |
3634600 |
*d1 = dest; |
| 411 |
3634600 |
return (0); |
| 412 |
3634760 |
} |
| 413 |
|
|
| 414 |
|
/*-------------------------------------------------------------------- |
| 415 |
|
* vsl_buffer and vsl_reclen have dependencies. |
| 416 |
|
*/ |
| 417 |
|
|
| 418 |
|
int v_matchproto_(tweak_t) |
| 419 |
169640 |
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 |
169640 |
d1 = par->priv; |
| 425 |
169640 |
dest = *d1; |
| 426 |
169640 |
if (tweak_generic_bytes(vsb, &dest, arg, par->min, par->max)) |
| 427 |
0 |
return (-1); |
| 428 |
169640 |
*d1 = dest; |
| 429 |
169640 |
MCF_ParamConf(MCF_MAXIMUM, "vsl_reclen", "%u", *d1 - 12); |
| 430 |
169640 |
return (0); |
| 431 |
169640 |
} |
| 432 |
|
|
| 433 |
|
int v_matchproto_(tweak_t) |
| 434 |
253840 |
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 |
253840 |
d1 = par->priv; |
| 440 |
253840 |
dest = *d1; |
| 441 |
253840 |
if (tweak_generic_bytes(vsb, &dest, arg, par->min, par->max)) |
| 442 |
0 |
return (-1); |
| 443 |
253840 |
*d1 = dest; |
| 444 |
253840 |
MCF_ParamConf(MCF_MINIMUM, "vsl_buffer", "%u", *d1 + 12); |
| 445 |
253840 |
return (0); |
| 446 |
253840 |
} |
| 447 |
|
|
| 448 |
|
/*--------------------------------------------------------------------*/ |
| 449 |
|
|
| 450 |
|
int v_matchproto_(tweak_t) |
| 451 |
464360 |
tweak_string(struct vsb *vsb, const struct parspec *par, const char *arg) |
| 452 |
|
{ |
| 453 |
464360 |
char **p = TRUST_ME(par->priv); |
| 454 |
|
|
| 455 |
464360 |
AN(p); |
| 456 |
464360 |
if (arg == NULL) { |
| 457 |
213640 |
VSB_quote(vsb, *p, -1, 0); |
| 458 |
464360 |
} else if (arg == JSON_FMT) { |
| 459 |
600 |
VSB_putc(vsb, '"'); |
| 460 |
600 |
VSB_quote(vsb, *p, -1, VSB_QUOTE_JSON); |
| 461 |
600 |
VSB_putc(vsb, '"'); |
| 462 |
600 |
} else { |
| 463 |
250120 |
REPLACE(*p, arg); |
| 464 |
|
} |
| 465 |
464360 |
return (0); |
| 466 |
|
} |
| 467 |
|
|
| 468 |
|
/*--------------------------------------------------------------------*/ |
| 469 |
|
|
| 470 |
|
int v_matchproto_(tweak_t) |
| 471 |
255800 |
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 |
255800 |
int retval = 0; |
| 477 |
|
|
| 478 |
255800 |
pp = par->priv; |
| 479 |
255800 |
if (arg == JSON_FMT) { |
| 480 |
440 |
VSB_cat(vsb, "{\n"); |
| 481 |
440 |
VSB_indent(vsb, 8); |
| 482 |
440 |
VSB_printf(vsb, "\"min_pool\": %u,\n", pp->min_pool); |
| 483 |
440 |
VSB_printf(vsb, "\"max_pool\": %u,\n", pp->max_pool); |
| 484 |
440 |
VSB_printf(vsb, "\"max_age\": %g\n", pp->max_age); |
| 485 |
440 |
VSB_indent(vsb, -4); |
| 486 |
440 |
VSB_cat(vsb, "}"); |
| 487 |
255800 |
} else if (arg == NULL) { |
| 488 |
256480 |
VSB_printf(vsb, "%u,%u,%g", |
| 489 |
128240 |
pp->min_pool, pp->max_pool, pp->max_age); |
| 490 |
128240 |
} else { |
| 491 |
127120 |
av = VAV_Parse(arg, NULL, ARGV_COMMA); |
| 492 |
127120 |
do { |
| 493 |
127120 |
if (av[0] != NULL) { |
| 494 |
40 |
VSB_printf(vsb, "Parse error: %s", av[0]); |
| 495 |
40 |
retval = -1; |
| 496 |
40 |
break; |
| 497 |
|
} |
| 498 |
127080 |
if (av[1] == NULL || av[2] == NULL || av[3] == NULL) { |
| 499 |
40 |
VSB_cat(vsb, |
| 500 |
|
"Three fields required:" |
| 501 |
|
" min_pool, max_pool and max_age\n"); |
| 502 |
40 |
retval = -1; |
| 503 |
40 |
break; |
| 504 |
|
} |
| 505 |
127040 |
px = *pp; |
| 506 |
254080 |
retval = tweak_generic_uint(vsb, &px.min_pool, av[1], |
| 507 |
127040 |
par->min, par->max, par->dyn_min_reason, |
| 508 |
127040 |
par->dyn_max_reason); |
| 509 |
127040 |
if (retval) |
| 510 |
40 |
break; |
| 511 |
254000 |
retval = tweak_generic_uint(vsb, &px.max_pool, av[2], |
| 512 |
127000 |
par->min, par->max, par->dyn_min_reason, |
| 513 |
127000 |
par->dyn_max_reason); |
| 514 |
127000 |
if (retval) |
| 515 |
40 |
break; |
| 516 |
126960 |
pt.priv = &px.max_age; |
| 517 |
126960 |
pt.min = "0"; |
| 518 |
126960 |
pt.max = "1000000"; |
| 519 |
126960 |
retval = tweak_generic_double(vsb, av[3], &pt, |
| 520 |
|
parse_decimal, "%.0f"); |
| 521 |
126960 |
if (retval) |
| 522 |
40 |
break; |
| 523 |
126920 |
if (px.min_pool > px.max_pool) { |
| 524 |
40 |
VSB_cat(vsb, |
| 525 |
|
"min_pool cannot be larger" |
| 526 |
|
" than max_pool\n"); |
| 527 |
40 |
retval = -1; |
| 528 |
40 |
break; |
| 529 |
|
} |
| 530 |
126880 |
*pp = px; |
| 531 |
126880 |
} while (0); |
| 532 |
127120 |
VAV_Free(av); |
| 533 |
|
} |
| 534 |
255800 |
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 |
293160 |
tweak_thread_pool_min(struct vsb *vsb, const struct parspec *par, |
| 546 |
|
const char *arg) |
| 547 |
|
{ |
| 548 |
293160 |
if (tweak_uint(vsb, par, arg)) |
| 549 |
80 |
return (-1); |
| 550 |
|
|
| 551 |
293080 |
MCF_ParamConf(MCF_MINIMUM, "thread_pool_max", |
| 552 |
293080 |
"%u", mgt_param.wthread_min); |
| 553 |
293080 |
MCF_ParamConf(MCF_MAXIMUM, "thread_pool_reserve", |
| 554 |
293080 |
"%u", mgt_param.wthread_min * 950 / 1000); |
| 555 |
293080 |
return (0); |
| 556 |
293160 |
} |
| 557 |
|
|
| 558 |
|
int v_matchproto_(tweak_t) |
| 559 |
85720 |
tweak_thread_pool_max(struct vsb *vsb, const struct parspec *par, |
| 560 |
|
const char *arg) |
| 561 |
|
{ |
| 562 |
|
|
| 563 |
85720 |
if (tweak_uint(vsb, par, arg)) |
| 564 |
80 |
return (-1); |
| 565 |
|
|
| 566 |
85640 |
MCF_ParamConf(MCF_MAXIMUM, "thread_pool_min", |
| 567 |
85640 |
"%u", mgt_param.wthread_max); |
| 568 |
85640 |
return (0); |
| 569 |
85720 |
} |
| 570 |
|
|
| 571 |
|
/*-------------------------------------------------------------------- |
| 572 |
|
* Tweak storage |
| 573 |
|
*/ |
| 574 |
|
|
| 575 |
|
int v_matchproto_(tweak_t) |
| 576 |
85080 |
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 |
85080 |
if (arg == NULL || arg == JSON_FMT) |
| 586 |
42840 |
return (tweak_string(vsb, par, arg)); |
| 587 |
|
|
| 588 |
42240 |
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 |
42200 |
} else { |
| 594 |
|
/* Only allow setting the value to a known configured |
| 595 |
|
* stevedore */ |
| 596 |
40 |
STV_Foreach(stv) { |
| 597 |
40 |
CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC); |
| 598 |
40 |
if (!strcmp(stv->ident, arg)) |
| 599 |
40 |
break; |
| 600 |
|
} |
| 601 |
40 |
if (stv == NULL) { |
| 602 |
0 |
VSB_printf(vsb, "unknown storage backend '%s'", arg); |
| 603 |
0 |
return (-1); |
| 604 |
|
} |
| 605 |
|
} |
| 606 |
42240 |
return (tweak_string(vsb, par, arg)); |
| 607 |
85080 |
} |
| 608 |
|
|
| 609 |
|
/*-------------------------------------------------------------------- |
| 610 |
|
* Tweak alias |
| 611 |
|
*/ |
| 612 |
|
|
| 613 |
|
int v_matchproto_(tweak_t) |
| 614 |
0 |
tweak_alias(struct vsb *vsb, const struct parspec *par, const char *arg) |
| 615 |
|
{ |
| 616 |
|
|
| 617 |
0 |
(void)vsb; |
| 618 |
0 |
(void)par; |
| 619 |
0 |
(void)arg; |
| 620 |
0 |
WRONG("param tweak never called directly"); |
| 621 |
0 |
} |
| 622 |
|
|
| 623 |
|
/*-------------------------------------------------------------------- |
| 624 |
|
* Tweak bits |
| 625 |
|
*/ |
| 626 |
|
|
| 627 |
|
enum bit_do {BSET, BCLR, BTST}; |
| 628 |
|
|
| 629 |
|
static int |
| 630 |
6884280 |
bit(uint8_t *p, unsigned no, enum bit_do act) |
| 631 |
|
{ |
| 632 |
|
uint8_t b; |
| 633 |
|
|
| 634 |
6884280 |
p += (no >> 3); |
| 635 |
6884280 |
b = (0x80 >> (no & 7)); |
| 636 |
6884280 |
if (act == BSET) |
| 637 |
810000 |
*p |= b; |
| 638 |
6074280 |
else if (act == BCLR) |
| 639 |
117120 |
*p &= ~b; |
| 640 |
6884280 |
return (*p & b); |
| 641 |
|
} |
| 642 |
|
|
| 643 |
|
static inline void |
| 644 |
211120 |
bit_clear(uint8_t *p, unsigned l) |
| 645 |
|
{ |
| 646 |
|
|
| 647 |
211120 |
memset(p, 0, ((size_t)l + 7) >> 3); |
| 648 |
211120 |
} |
| 649 |
|
|
| 650 |
|
static inline void |
| 651 |
80 |
bit_set(uint8_t *p, unsigned l) |
| 652 |
|
{ |
| 653 |
|
|
| 654 |
80 |
memset(p, 255, ((size_t)l + 7) >> 3); |
| 655 |
80 |
} |
| 656 |
|
|
| 657 |
|
/*-------------------------------------------------------------------- |
| 658 |
|
*/ |
| 659 |
|
|
| 660 |
|
static int |
| 661 |
300680 |
bit_tweak(struct vsb *vsb, uint8_t *p, unsigned l, const char *arg, |
| 662 |
|
const char * const *tags, const char *desc, char sign) |
| 663 |
|
{ |
| 664 |
|
int i, n; |
| 665 |
|
unsigned j; |
| 666 |
|
char **av; |
| 667 |
|
const char *s; |
| 668 |
|
|
| 669 |
300680 |
av = VAV_Parse(arg, &n, ARGV_COMMA); |
| 670 |
300680 |
if (av[0] != NULL) { |
| 671 |
40 |
VSB_printf(vsb, "Cannot parse: %s\n", av[0]); |
| 672 |
40 |
VAV_Free(av); |
| 673 |
40 |
return (-1); |
| 674 |
|
} |
| 675 |
1438960 |
for (i = 1; av[i] != NULL; i++) { |
| 676 |
1138400 |
s = av[i]; |
| 677 |
1138400 |
if (sign == '+' && !strcmp(s, "none")) { |
| 678 |
168840 |
bit_clear(p, l); |
| 679 |
168840 |
continue; |
| 680 |
|
} |
| 681 |
969560 |
if (sign == '+' && !strcmp(s, "all")) { |
| 682 |
40 |
bit_set(p, l); |
| 683 |
40 |
continue; |
| 684 |
|
} |
| 685 |
969520 |
if (sign == '-' && !strcmp(s, "all")) { |
| 686 |
42280 |
bit_clear(p, l); |
| 687 |
42280 |
continue; |
| 688 |
|
} |
| 689 |
927240 |
if (sign == '-' && !strcmp(s, "none")) { |
| 690 |
40 |
bit_set(p, l); |
| 691 |
40 |
continue; |
| 692 |
|
} |
| 693 |
927200 |
if (*s != '-' && *s != '+') { |
| 694 |
40 |
VSB_printf(vsb, "Missing '+' or '-' (%s)\n", s); |
| 695 |
40 |
VAV_Free(av); |
| 696 |
40 |
return (-1); |
| 697 |
|
} |
| 698 |
47581280 |
for (j = 0; j < l; j++) { |
| 699 |
47581240 |
if (tags[j] != NULL && !strcasecmp(s + 1, tags[j])) |
| 700 |
927120 |
break; |
| 701 |
46654120 |
} |
| 702 |
927160 |
if (tags[j] == NULL) { |
| 703 |
40 |
VSB_printf(vsb, "Unknown %s (%s)\n", desc, s); |
| 704 |
40 |
VAV_Free(av); |
| 705 |
40 |
return (-1); |
| 706 |
|
} |
| 707 |
927120 |
assert(j < l); |
| 708 |
927120 |
if (s[0] == sign) |
| 709 |
810000 |
(void)bit(p, j, BSET); |
| 710 |
|
else |
| 711 |
117120 |
(void)bit(p, j, BCLR); |
| 712 |
927120 |
} |
| 713 |
300560 |
VAV_Free(av); |
| 714 |
300560 |
return (0); |
| 715 |
300680 |
} |
| 716 |
|
|
| 717 |
|
|
| 718 |
|
/*-------------------------------------------------------------------- |
| 719 |
|
*/ |
| 720 |
|
|
| 721 |
|
static int |
| 722 |
515840 |
tweak_generic_bits(struct vsb *vsb, const struct parspec *par, const char *arg, |
| 723 |
|
uint8_t *p, unsigned l, const char * const *tags, const char *desc, |
| 724 |
|
char sign) |
| 725 |
|
{ |
| 726 |
|
unsigned j, all; |
| 727 |
|
|
| 728 |
515840 |
if (arg != NULL && !strcmp(arg, "default")) { |
| 729 |
|
/* XXX: deprecated in favor of param.reset */ |
| 730 |
0 |
return (tweak_generic_bits(vsb, par, par->def, p, l, tags, |
| 731 |
0 |
desc, sign)); |
| 732 |
|
} |
| 733 |
|
|
| 734 |
515840 |
if (arg != NULL && arg != JSON_FMT) |
| 735 |
300680 |
return (bit_tweak(vsb, p, l, arg, tags, desc, sign)); |
| 736 |
|
|
| 737 |
215160 |
all = 1; |
| 738 |
581200 |
for (j = 0; all && j < l; j++) { |
| 739 |
366040 |
if (tags[j] == NULL) |
| 740 |
56040 |
continue; |
| 741 |
310000 |
if (!bit(p, j, BTST)) |
| 742 |
214920 |
all = 0; |
| 743 |
310000 |
} |
| 744 |
|
|
| 745 |
215160 |
if (arg == JSON_FMT) |
| 746 |
840 |
VSB_putc(vsb, '"'); |
| 747 |
215160 |
if (all) |
| 748 |
240 |
VSB_cat(vsb, sign == '+' ? "all" : "none"); |
| 749 |
|
else |
| 750 |
214920 |
VSB_cat(vsb, sign == '+' ? "none" : "all"); |
| 751 |
12811080 |
for (j = 0; !all && j < l; j++) { |
| 752 |
12595920 |
if (tags[j] == NULL) |
| 753 |
6948760 |
continue; |
| 754 |
5647160 |
if (bit(p, j, BTST)) |
| 755 |
773640 |
VSB_printf(vsb, ",%c%s", sign, tags[j]); |
| 756 |
5647160 |
} |
| 757 |
215160 |
if (arg == JSON_FMT) |
| 758 |
840 |
VSB_putc(vsb, '"'); |
| 759 |
215160 |
return (0); |
| 760 |
515840 |
} |
| 761 |
|
|
| 762 |
|
/*-------------------------------------------------------------------- |
| 763 |
|
* The vsl_mask parameter |
| 764 |
|
*/ |
| 765 |
|
|
| 766 |
|
static const char * const VSL_tags[256] = { |
| 767 |
|
# define SLTM(foo,flags,sdesc,ldesc) [SLT_##foo] = #foo, |
| 768 |
|
# include "tbl/vsl_tags.h" |
| 769 |
|
}; |
| 770 |
|
|
| 771 |
|
int v_matchproto_(tweak_t) |
| 772 |
125280 |
tweak_vsl_mask(struct vsb *vsb, const struct parspec *par, const char *arg) |
| 773 |
|
{ |
| 774 |
|
|
| 775 |
125280 |
return (tweak_generic_bits(vsb, par, arg, mgt_param.vsl_mask, |
| 776 |
|
SLT__Reserved, VSL_tags, "VSL tag", '-')); |
| 777 |
|
} |
| 778 |
|
|
| 779 |
|
/*-------------------------------------------------------------------- |
| 780 |
|
* The debug parameter |
| 781 |
|
*/ |
| 782 |
|
|
| 783 |
|
static const char * const debug_tags[] = { |
| 784 |
|
# define DEBUG_BIT(U, l, d) [DBG_##U] = #l, |
| 785 |
|
# include "tbl/debug_bits.h" |
| 786 |
|
NULL |
| 787 |
|
}; |
| 788 |
|
|
| 789 |
|
int v_matchproto_(tweak_t) |
| 790 |
129120 |
tweak_debug(struct vsb *vsb, const struct parspec *par, const char *arg) |
| 791 |
|
{ |
| 792 |
|
|
| 793 |
129120 |
return (tweak_generic_bits(vsb, par, arg, mgt_param.debug_bits, |
| 794 |
|
DBG_Reserved, debug_tags, "debug bit", '+')); |
| 795 |
|
} |
| 796 |
|
|
| 797 |
|
/*-------------------------------------------------------------------- |
| 798 |
|
* The experimental parameter |
| 799 |
|
*/ |
| 800 |
|
|
| 801 |
|
static const char * const experimental_tags[] = { |
| 802 |
|
# define EXPERIMENTAL_BIT(U, l, d) [EXPERIMENT_##U] = #l, |
| 803 |
|
# include "tbl/experimental_bits.h" |
| 804 |
|
NULL |
| 805 |
|
}; |
| 806 |
|
|
| 807 |
|
int v_matchproto_(tweak_t) |
| 808 |
85120 |
tweak_experimental(struct vsb *vsb, const struct parspec *par, const char *arg) |
| 809 |
|
{ |
| 810 |
|
|
| 811 |
85120 |
return (tweak_generic_bits(vsb, par, arg, mgt_param.experimental_bits, |
| 812 |
|
EXPERIMENT_Reserved, experimental_tags, "experimental bit", '+')); |
| 813 |
|
} |
| 814 |
|
|
| 815 |
|
/*-------------------------------------------------------------------- |
| 816 |
|
* The feature parameter |
| 817 |
|
*/ |
| 818 |
|
|
| 819 |
|
static const char * const feature_tags[] = { |
| 820 |
|
# define FEATURE_BIT(U, l, d) [FEATURE_##U] = #l, |
| 821 |
|
# include "tbl/feature_bits.h" |
| 822 |
|
NULL |
| 823 |
|
}; |
| 824 |
|
|
| 825 |
|
int v_matchproto_(tweak_t) |
| 826 |
90080 |
tweak_feature(struct vsb *vsb, const struct parspec *par, const char *arg) |
| 827 |
|
{ |
| 828 |
|
|
| 829 |
90080 |
return (tweak_generic_bits(vsb, par, arg, mgt_param.feature_bits, |
| 830 |
|
FEATURE_Reserved, feature_tags, "feature bit", '+')); |
| 831 |
|
} |
| 832 |
|
|
| 833 |
|
/*-------------------------------------------------------------------- |
| 834 |
|
* The vcc_feature parameter |
| 835 |
|
*/ |
| 836 |
|
|
| 837 |
|
static const char * const vcc_feature_tags[] = { |
| 838 |
|
# define VCC_FEATURE_BIT(U, l, d) [VCC_FEATURE_##U] = #l, |
| 839 |
|
# include "tbl/vcc_feature_bits.h" |
| 840 |
|
NULL |
| 841 |
|
}; |
| 842 |
|
|
| 843 |
|
int v_matchproto_(tweak_t) |
| 844 |
86240 |
tweak_vcc_feature(struct vsb *vsb, const struct parspec *par, const char *arg) |
| 845 |
|
{ |
| 846 |
|
const struct parspec *orig; |
| 847 |
|
char buf[32]; |
| 848 |
|
int val; |
| 849 |
|
|
| 850 |
86240 |
if (arg != NULL && arg != JSON_FMT && |
| 851 |
43280 |
strcmp(par->name, "vcc_feature")) { |
| 852 |
0 |
orig = TRUST_ME(par->priv); |
| 853 |
0 |
val = parse_boolean(vsb, arg); |
| 854 |
0 |
if (val < 0) |
| 855 |
0 |
return (-1); |
| 856 |
0 |
bprintf(buf, "%c%s", val ? '+' : '-', |
| 857 |
|
par->name + strlen("vcc_")); |
| 858 |
0 |
return (tweak_vcc_feature(vsb, orig, buf)); |
| 859 |
|
} |
| 860 |
86240 |
return (tweak_generic_bits(vsb, par, arg, mgt_param.vcc_feature_bits, |
| 861 |
|
VCC_FEATURE_Reserved, vcc_feature_tags, "vcc_feature bit", '+')); |
| 862 |
86240 |
} |