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 734880
parse_decimal(const char *p, const char **err)
60
{
61
        double v;
62
63 734880
        v = SF_Parse_Decimal(&p, 0, err);
64 734880
        if (errno == 0 && *p != '\0') {
65 40
                errno = EINVAL;
66 40
                *err = "Invalid number";
67 40
        }
68 734880
        return (v);
69
}
70
71
static int
72 7409200
tweak_generic_double(struct vsb *vsb, const char *arg, const struct parspec *pp,
73
    parse_double_f parse, const char *fmt)
74
{
75 7409200
        volatile double u, minv = VRT_DECIMAL_MIN, maxv = VRT_DECIMAL_MAX;
76 7409200
        volatile double *dest = pp->priv;
77
        const char *err;
78
79 7409200
        if (arg != NULL && arg != JSON_FMT) {
80 3755560
                if (pp->min != NULL) {
81 3755560
                        minv = parse(pp->min, &err);
82 3755560
                        if (errno) {
83 0
                                VSB_printf(vsb, "Min: %s (%s)\n", err, pp->min);
84 0
                                return (-1);
85
                        }
86 3755560
                }
87 3755560
                if (pp->max != NULL) {
88 1223960
                        maxv = parse(pp->max, &err);
89 1223960
                        if (errno) {
90 0
                                VSB_printf(vsb, "Max: %s (%s)\n", err, pp->max);
91 0
                                return (-1);
92
                        }
93 1223960
                }
94
95 3755560
                u = parse(arg, &err);
96 3755560
                if (errno) {
97 160
                        VSB_printf(vsb, "%s (%s)\n", err, arg);
98 160
                        return (-1);
99
                }
100 3755400
                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 3755360
                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 3755320
                *dest = u;
111 3755320
        } else {
112 3653640
                VSB_printf(vsb, fmt, *dest);
113
        }
114 7408960
        return (0);
115 7409200
}
116
117
/*--------------------------------------------------------------------*/
118
119
static double
120 8000200
parse_duration(const char *p, const char **err)
121
{
122
        double v, r;
123
124 8000200
        v = SF_Parse_Decimal(&p, 0, err);
125 8000200
        if (*p == '\0')
126 7347120
                return (v);
127
128 653080
        r = VNUM_duration_unit(v, p, NULL);
129 653080
        if (isnan(r)) {
130 80
                errno = EINVAL;
131 80
                *err = "Invalid duration unit";
132 80
        }
133
134 653080
        return (r);
135 8000200
}
136
137
int v_matchproto_(tweak_t)
138 1884600
tweak_timeout(struct vsb *vsb, const struct parspec *par, const char *arg)
139
{
140 1884600
        volatile double *dest = par->priv;
141
142 1884600
        if (arg != NULL && !strcmp(arg, "never")) {
143 40
                *dest = INFINITY;
144 40
                return (0);
145
        }
146
147 1884560
        if (*dest == INFINITY && arg == NULL) {
148 0
                VSB_cat(vsb, "never");
149 0
                return (0);
150
        }
151
152 1884560
        if (*dest == INFINITY && arg == JSON_FMT) {
153 0
                VSB_cat(vsb, "\"never\"");
154 0
                return (0);
155
        }
156
157 1884560
        return (tweak_generic_double(vsb, arg, par, parse_duration, "%.3f"));
158 1884600
}
159
160
int v_matchproto_(tweak_t)
161 5156760
tweak_duration(struct vsb *vsb, const struct parspec *par, const char *arg)
162
{
163
164 5156760
        return (tweak_generic_double(vsb, arg, par, parse_duration, "%.3f"));
165
}
166
167
/*--------------------------------------------------------------------*/
168
169
int v_matchproto_(tweak_t)
170 245240
tweak_double(struct vsb *vsb, const struct parspec *par, const char *arg)
171
{
172
173 245240
        return (tweak_generic_double(vsb, arg, par, parse_decimal, "%g"));
174
}
175
176
/*--------------------------------------------------------------------*/
177
178
static int
179 483320
parse_boolean(struct vsb *vsb, const char *arg)
180
{
181
182 483320
        if (!strcasecmp(arg, "off"))
183 156240
                return (0);
184 327080
        if (!strcasecmp(arg, "disable"))
185 40
                return (0);
186 327040
        if (!strcasecmp(arg, "no"))
187 40
                return (0);
188 327000
        if (!strcasecmp(arg, "false"))
189 160
                return (0);
190 326840
        if (!strcasecmp(arg, "on"))
191 326280
                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 483320
}
202
203
int v_matchproto_(tweak_t)
204 897320
tweak_boolean(struct vsb *vsb, const struct parspec *par, const char *arg)
205
{
206
        volatile unsigned *dest;
207
        int val;
208
209 897320
        dest = par->priv;
210 897320
        if (arg != NULL && arg != JSON_FMT) {
211 483320
                val = parse_boolean(vsb, arg);
212 483320
                if (val < 0)
213 40
                        return (-1);
214 483280
                *dest = val;
215 897280
        } else if (arg == JSON_FMT) {
216 1200
                VSB_printf(vsb, "%s", *dest ? "true" : "false");
217 1200
        } else {
218 412800
                VSB_printf(vsb, "%s", *dest ? "on" : "off");
219
        }
220 897280
        return (0);
221 897320
}
222
223
/*--------------------------------------------------------------------*/
224
225
static int
226 5769600
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 5769600
        unsigned u, minv = 0, maxv = 0;
231
        char *p;
232
233 5769600
        if (arg != NULL && arg != JSON_FMT) {
234 3019480
                if (min != NULL) {
235 2651440
                        p = NULL;
236 2651440
                        minv = strtoul(min, &p, 0);
237 2651440
                        if (*arg == '\0' || *p != '\0') {
238 0
                                VSB_printf(vsb, "Illegal Min: %s\n", min);
239 0
                                return (-1);
240
                        }
241 2651440
                }
242 3019480
                if (max != NULL) {
243 1100480
                        p = NULL;
244 1100480
                        maxv = strtoul(max, &p, 0);
245 1100480
                        if (*arg == '\0' || *p != '\0') {
246 0
                                VSB_printf(vsb, "Illegal Max: %s\n", max);
247 0
                                return (-1);
248
                        }
249 1100480
                }
250 3019480
                p = NULL;
251 3019480
                if (!strcasecmp(arg, "unlimited"))
252 40
                        u = UINT_MAX;
253
                else {
254 3019440
                        u = strtoul(arg, &p, 0);
255 3019440
                        if (*arg == '\0' || *p != '\0') {
256 80
                                VSB_printf(vsb, "Not a number (%s)\n", arg);
257 80
                                return (-1);
258
                        }
259
                }
260 3019400
                if (min != NULL && u < minv) {
261 120
                        VSB_printf(vsb, "Must be at least %s", min);
262 120
                        if (min_reason != NULL)
263 80
                                VSB_printf(vsb, " (%s)", min_reason);
264 120
                        VSB_putc(vsb, '\n');
265 120
                        return (-1);
266
                }
267 3019280
                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 3019040
                *dest = u;
275 5769160
        } else if (*dest == UINT_MAX && arg != JSON_FMT) {
276 200
                VSB_cat(vsb, "unlimited");
277 200
        } else {
278 2749920
                VSB_printf(vsb, "%u", *dest);
279
        }
280 5769160
        return (0);
281 5769600
}
282
283
/*--------------------------------------------------------------------*/
284
285
int v_matchproto_(tweak_t)
286 5524200
tweak_uint(struct vsb *vsb, const struct parspec *par, const char *arg)
287
{
288
        volatile unsigned *dest;
289
290 5524200
        dest = par->priv;
291 11048400
        return (tweak_generic_uint(vsb, dest, arg, par->min, par->max,
292 5524200
            par->dyn_min_reason, par->dyn_max_reason));
293
}
294
295
/*--------------------------------------------------------------------*/
296
297
static void
298 2376560
fmt_bytes(struct vsb *vsb, uintmax_t t)
299
{
300
        const char *p;
301
302 2376560
        if (t == 0 || t & 0xff) {
303 816840
                VSB_printf(vsb, "%jub", t);
304 816840
                return;
305
        }
306 2132960
        for (p = "kMGTPEZY"; *p; p++) {
307 2132960
                if (t & 0x300) {
308 204840
                        VSB_printf(vsb, "%.2f%c", t / 1024.0, *p);
309 204840
                        return;
310
                }
311 1928120
                t /= 1024;
312 1928120
                if (t & 0x0ff) {
313 1354880
                        VSB_printf(vsb, "%ju%c", t, *p);
314 1354880
                        return;
315
                }
316 573240
        }
317 0
        VSB_cat(vsb, "(bogus number)");
318 2376560
}
319
320
static int
321 4858600
tweak_generic_bytes(struct vsb *vsb, volatile ssize_t *dest, const char *arg,
322
    const char *min, const char *max)
323
{
324 4858600
        uintmax_t r, rmin = 0, rmax = 0;
325
        const char *p;
326
327 4858600
        if (arg != NULL && arg != JSON_FMT) {
328 2479160
                if (min != NULL) {
329 2479160
                        p = VNUM_2bytes(min, &rmin, 0);
330 2479160
                        if (p != NULL) {
331 0
                                VSB_printf(vsb, "Invalid min-val: %s\n", min);
332 0
                                return (-1);
333
                        }
334 2479160
                }
335 2479160
                if (max != NULL) {
336 1335560
                        p = VNUM_2bytes(max, &rmax, 0);
337 1335560
                        if (p != NULL) {
338 0
                                VSB_printf(vsb, "Invalid max-val: %s\n", max);
339 0
                                return (-1);
340
                        }
341 1335560
                }
342 2479160
                p = VNUM_2bytes(arg, &r, 0);
343 2479160
                if (p != NULL) {
344 80
                        VSB_cat(vsb, "Could not convert to bytes.\n");
345 80
                        VSB_printf(vsb, "%s\n", p);
346 80
                        VSB_cat(vsb, "  Try something like '80k' or '120M'\n");
347 80
                        return (-1);
348
                }
349 2479080
                if ((uintmax_t)((ssize_t)r) != r) {
350 0
                        fmt_bytes(vsb, r);
351 0
                        VSB_cat(vsb, " is too large for this architecture.\n");
352 0
                        return (-1);
353
                }
354 2479080
                if (max != NULL && r > rmax) {
355 40
                        VSB_printf(vsb, "Must be no more than %s\n", max);
356 40
                        VSB_cat(vsb, "\n");
357 40
                        return (-1);
358
                }
359 2479040
                if (min != NULL && r < rmin) {
360 40
                        VSB_printf(vsb, "Must be at least %s\n", min);
361 40
                        return (-1);
362
                }
363 2479000
                *dest = r;
364 4858440
        } else if (arg == JSON_FMT) {
365 2880
                VSB_printf(vsb, "%zd", *dest);
366 2880
        } else {
367 2376560
                fmt_bytes(vsb, *dest);
368
        }
369 4858440
        return (0);
370 4858600
}
371
372
/*--------------------------------------------------------------------*/
373
374
int v_matchproto_(tweak_t)
375 938280
tweak_bytes(struct vsb *vsb, const struct parspec *par, const char *arg)
376
{
377
        volatile ssize_t *dest;
378
379 938280
        dest = par->priv;
380 938280
        return (tweak_generic_bytes(vsb, dest, arg, par->min, par->max));
381
}
382
383
/*--------------------------------------------------------------------*/
384
385
int v_matchproto_(tweak_t)
386 3511240
tweak_bytes_u(struct vsb *vsb, const struct parspec *par, const char *arg)
387
{
388
        volatile unsigned *d1;
389
        volatile ssize_t dest;
390
391 3511240
        d1 = par->priv;
392 3511240
        dest = *d1;
393 3511240
        if (tweak_generic_bytes(vsb, &dest, arg, par->min, par->max))
394 160
                return (-1);
395 3511080
        *d1 = dest;
396 3511080
        return (0);
397 3511240
}
398
399
/*--------------------------------------------------------------------
400
 * vsl_buffer and vsl_reclen have dependencies.
401
 */
402
403
int v_matchproto_(tweak_t)
404 163880
tweak_vsl_buffer(struct vsb *vsb, const struct parspec *par, const char *arg)
405
{
406
        volatile unsigned *d1;
407
        volatile ssize_t dest;
408
409 163880
        d1 = par->priv;
410 163880
        dest = *d1;
411 163880
        if (tweak_generic_bytes(vsb, &dest, arg, par->min, par->max))
412 0
                return (-1);
413 163880
        *d1 = dest;
414 163880
        MCF_ParamConf(MCF_MAXIMUM, "vsl_reclen", "%u", *d1 - 12);
415 163880
        return (0);
416 163880
}
417
418
int v_matchproto_(tweak_t)
419 245200
tweak_vsl_reclen(struct vsb *vsb, const struct parspec *par, const char *arg)
420
{
421
        volatile unsigned *d1;
422
        volatile ssize_t dest;
423
424 245200
        d1 = par->priv;
425 245200
        dest = *d1;
426 245200
        if (tweak_generic_bytes(vsb, &dest, arg, par->min, par->max))
427 0
                return (-1);
428 245200
        *d1 = dest;
429 245200
        MCF_ParamConf(MCF_MINIMUM, "vsl_buffer", "%u", *d1 + 12);
430 245200
        return (0);
431 245200
}
432
433
/*--------------------------------------------------------------------*/
434
435
int v_matchproto_(tweak_t)
436 448680
tweak_string(struct vsb *vsb, const struct parspec *par, const char *arg)
437
{
438 448680
        char **p = TRUST_ME(par->priv);
439
440 448680
        AN(p);
441 448680
        if (arg == NULL) {
442 206440
                VSB_quote(vsb, *p, -1, 0);
443 448680
        } else if (arg == JSON_FMT) {
444 600
                VSB_putc(vsb, '"');
445 600
                VSB_quote(vsb, *p, -1, VSB_QUOTE_JSON);
446 600
                VSB_putc(vsb, '"');
447 600
        } else {
448 241640
                REPLACE(*p, arg);
449
        }
450 448680
        return (0);
451
}
452
453
/*--------------------------------------------------------------------*/
454
455
int v_matchproto_(tweak_t)
456 247160
tweak_poolparam(struct vsb *vsb, const struct parspec *par, const char *arg)
457
{
458
        volatile struct poolparam *pp, px;
459
        struct parspec pt;
460
        char **av;
461 247160
        int retval = 0;
462
463 247160
        pp = par->priv;
464 247160
        if (arg == JSON_FMT) {
465 440
                VSB_cat(vsb, "{\n");
466 440
                VSB_indent(vsb, 8);
467 440
                VSB_printf(vsb, "\"min_pool\": %u,\n", pp->min_pool);
468 440
                VSB_printf(vsb, "\"max_pool\": %u,\n", pp->max_pool);
469 440
                VSB_printf(vsb, "\"max_age\": %g\n", pp->max_age);
470 440
                VSB_indent(vsb, -4);
471 440
                VSB_cat(vsb, "}");
472 247160
        } else if (arg == NULL) {
473 247840
                VSB_printf(vsb, "%u,%u,%g",
474 123920
                    pp->min_pool, pp->max_pool, pp->max_age);
475 123920
        } else {
476 122800
                av = VAV_Parse(arg, NULL, ARGV_COMMA);
477 122800
                do {
478 122800
                        if (av[0] != NULL) {
479 40
                                VSB_printf(vsb, "Parse error: %s", av[0]);
480 40
                                retval = -1;
481 40
                                break;
482
                        }
483 122760
                        if (av[1] == NULL || av[2] == NULL || av[3] == NULL) {
484 40
                                VSB_cat(vsb,
485
                                    "Three fields required:"
486
                                    " min_pool, max_pool and max_age\n");
487 40
                                retval = -1;
488 40
                                break;
489
                        }
490 122720
                        px = *pp;
491 245440
                        retval = tweak_generic_uint(vsb, &px.min_pool, av[1],
492 122720
                            par->min, par->max, par->dyn_min_reason,
493 122720
                            par->dyn_max_reason);
494 122720
                        if (retval)
495 40
                                break;
496 245360
                        retval = tweak_generic_uint(vsb, &px.max_pool, av[2],
497 122680
                            par->min, par->max, par->dyn_min_reason,
498 122680
                            par->dyn_max_reason);
499 122680
                        if (retval)
500 40
                                break;
501 122640
                        pt.priv = &px.max_age;
502 122640
                        pt.min = "0";
503 122640
                        pt.max = "1000000";
504 122640
                        retval = tweak_generic_double(vsb, av[3], &pt,
505
                            parse_decimal, "%.0f");
506 122640
                        if (retval)
507 40
                                break;
508 122600
                        if (px.min_pool > px.max_pool) {
509 40
                                VSB_cat(vsb,
510
                                    "min_pool cannot be larger"
511
                                    " than max_pool\n");
512 40
                                retval = -1;
513 40
                                break;
514
                        }
515 122560
                        *pp = px;
516 122560
                } while (0);
517 122800
                VAV_Free(av);
518
        }
519 247160
        return (retval);
520
}
521
522
/*--------------------------------------------------------------------
523
 * Thread pool tweaks.
524
 *
525
 * The min/max values automatically update the opposites appropriate
526
 * limit, so they don't end up crossing.
527
 */
528
529
int v_matchproto_(tweak_t)
530 283240
tweak_thread_pool_min(struct vsb *vsb, const struct parspec *par,
531
    const char *arg)
532
{
533 283240
        if (tweak_uint(vsb, par, arg))
534 80
                return (-1);
535
536 283160
        MCF_ParamConf(MCF_MINIMUM, "thread_pool_max",
537 283160
            "%u", mgt_param.wthread_min);
538 283160
        MCF_ParamConf(MCF_MAXIMUM, "thread_pool_reserve",
539 283160
            "%u", mgt_param.wthread_min * 950 / 1000);
540 283160
        return (0);
541 283240
}
542
543
int v_matchproto_(tweak_t)
544 82840
tweak_thread_pool_max(struct vsb *vsb, const struct parspec *par,
545
    const char *arg)
546
{
547
548 82840
        if (tweak_uint(vsb, par, arg))
549 80
                return (-1);
550
551 82760
        MCF_ParamConf(MCF_MAXIMUM, "thread_pool_min",
552 82760
            "%u", mgt_param.wthread_max);
553 82760
        return (0);
554 82840
}
555
556
/*--------------------------------------------------------------------
557
 * Tweak storage
558
 */
559
560
int v_matchproto_(tweak_t)
561 82200
tweak_storage(struct vsb *vsb, const struct parspec *par, const char *arg)
562
{
563
        struct stevedore *stv;
564
565
        /* XXX: If we want to remove the MUST_RESTART flag from the
566
         * h2_rxbuf_storage parameter, we could have a mechanism here
567
         * that when the child is running calls out through CLI to change
568
         * the stevedore being used. */
569
570 82200
        if (arg == NULL || arg == JSON_FMT)
571 41400
                return (tweak_string(vsb, par, arg));
572
573 40800
        if (!strcmp(arg, "Transient")) {
574
                /* Always allow setting to the special name
575
                 * "Transient". There will always be a stevedore with this
576
                 * name, but it may not have been configured at the time
577
                 * this is called. */
578 40760
        } else {
579
                /* Only allow setting the value to a known configured
580
                 * stevedore */
581 40
                STV_Foreach(stv) {
582 40
                        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
583 40
                        if (!strcmp(stv->ident, arg))
584 40
                                break;
585
                }
586 40
                if (stv == NULL) {
587 0
                        VSB_printf(vsb, "unknown storage backend '%s'", arg);
588 0
                        return (-1);
589
                }
590
        }
591 40800
        return (tweak_string(vsb, par, arg));
592 82200
}
593
594
/*--------------------------------------------------------------------
595
 * Tweak alias
596
 */
597
598
int v_matchproto_(tweak_t)
599 240
tweak_alias(struct vsb *vsb, const struct parspec *par, const char *arg)
600
{
601
        const struct parspec *orig;
602
        struct parspec alias[1];
603
604 240
        orig = TRUST_ME(par->priv);
605 240
        AN(orig);
606 240
        memcpy(alias, orig, sizeof *orig);
607 240
        alias->name = par->name;
608 240
        alias->priv = TRUST_ME(orig);
609 240
        return (alias->func(vsb, alias, arg));
610
}
611
612
/*--------------------------------------------------------------------
613
 * Tweak bits
614
 */
615
616
enum bit_do {BSET, BCLR, BTST};
617
618
static int
619 13072080
bit(uint8_t *p, unsigned no, enum bit_do act)
620
{
621
        uint8_t b;
622
623 13072080
        p += (no >> 3);
624 13072080
        b = (0x80 >> (no & 7));
625 13072080
        if (act == BSET)
626 782160
                *p |= b;
627 12289920
        else if (act == BCLR)
628 113280
                *p &= ~b;
629 13072080
        return (*p & b);
630
}
631
632
static inline void
633 203920
bit_clear(uint8_t *p, unsigned l)
634
{
635
636 203920
        memset(p, 0, ((size_t)l + 7) >> 3);
637 203920
}
638
639
/*--------------------------------------------------------------------
640
 */
641
642
static int
643 290320
bit_tweak(struct vsb *vsb, uint8_t *p, unsigned l, const char *arg,
644
    const char * const *tags, const char *desc, char sign)
645
{
646
        int i, n;
647
        unsigned j;
648
        char **av;
649
        const char *s;
650
651 290320
        av = VAV_Parse(arg, &n, ARGV_COMMA);
652 290320
        if (av[0] != NULL) {
653 40
                VSB_printf(vsb, "Cannot parse: %s\n", av[0]);
654 40
                VAV_Free(av);
655 40
                return (-1);
656
        }
657 1389640
        for (i = 1; av[i] != NULL; i++) {
658 1099440
                s = av[i];
659 1099440
                if (sign == '+' && !strcmp(s, "none")) {
660 163080
                        bit_clear(p, l);
661 163080
                        continue;
662
                }
663 936360
                if (sign == '-' && !strcmp(s, "all")) {
664 40840
                        bit_clear(p, l);
665 40840
                        continue;
666
                }
667 895520
                if (*s != '-' && *s != '+') {
668 40
                        VSB_printf(vsb, "Missing '+' or '-' (%s)\n", s);
669 40
                        VAV_Free(av);
670 40
                        return (-1);
671
                }
672 45968080
                for (j = 0; j < l; j++) {
673 45968040
                        if (tags[j] != NULL && !strcasecmp(s + 1, tags[j]))
674 895440
                                break;
675 45072600
                }
676 895480
                if (tags[j] == NULL) {
677 40
                        VSB_printf(vsb, "Unknown %s (%s)\n", desc, s);
678 40
                        VAV_Free(av);
679 40
                        return (-1);
680
                }
681 895440
                assert(j < l);
682 895440
                if (s[0] == sign)
683 782160
                        (void)bit(p, j, BSET);
684
                else
685 113280
                        (void)bit(p, j, BCLR);
686 895440
        }
687 290200
        VAV_Free(av);
688 290200
        return (0);
689 290320
}
690
691
692
/*--------------------------------------------------------------------
693
 */
694
695
static int
696 498120
tweak_generic_bits(struct vsb *vsb, const struct parspec *par, const char *arg,
697
    uint8_t *p, unsigned l, const char * const *tags, const char *desc,
698
    char sign)
699
{
700
        unsigned j;
701
702 498120
        if (arg != NULL && !strcmp(arg, "default")) {
703
                /* XXX: deprecated in favor of param.reset */
704 0
                return (tweak_generic_bits(vsb, par, par->def, p, l, tags,
705 0
                    desc, sign));
706
        }
707
708 498120
        if (arg != NULL && arg != JSON_FMT)
709 290320
                return (bit_tweak(vsb, p, l, arg, tags, desc, sign));
710
711 207800
        if (arg == JSON_FMT)
712 760
                VSB_putc(vsb, '"');
713 207800
        VSB_cat(vsb, sign == '+' ? "none" : "all");
714 12384440
        for (j = 0; j < l; j++) {
715 12176640
                if (bit(p, j, BTST))
716 747760
                        VSB_printf(vsb, ",%c%s", sign, tags[j]);
717 12176640
        }
718 207800
        if (arg == JSON_FMT)
719 760
                VSB_putc(vsb, '"');
720 207800
        return (0);
721 498120
}
722
723
/*--------------------------------------------------------------------
724
 * The vsl_mask parameter
725
 */
726
727
static const char * const VSL_tags[256] = {
728
#  define SLTM(foo,flags,sdesc,ldesc) [SLT_##foo] = #foo,
729
#  include "tbl/vsl_tags.h"
730
};
731
732
int v_matchproto_(tweak_t)
733 121000
tweak_vsl_mask(struct vsb *vsb, const struct parspec *par, const char *arg)
734
{
735
736 121000
        return (tweak_generic_bits(vsb, par, arg, mgt_param.vsl_mask,
737
            SLT__Reserved, VSL_tags, "VSL tag", '-'));
738
}
739
740
/*--------------------------------------------------------------------
741
 * The debug parameter
742
 */
743
744
static const char * const debug_tags[] = {
745
#  define DEBUG_BIT(U, l, d) [DBG_##U] = #l,
746
#  include "tbl/debug_bits.h"
747
       NULL
748
};
749
750
int v_matchproto_(tweak_t)
751 124560
tweak_debug(struct vsb *vsb, const struct parspec *par, const char *arg)
752
{
753
754 124560
        return (tweak_generic_bits(vsb, par, arg, mgt_param.debug_bits,
755
            DBG_Reserved, debug_tags, "debug bit", '+'));
756
}
757
758
/*--------------------------------------------------------------------
759
 * The experimental parameter
760
 */
761
762
static const char * const experimental_tags[] = {
763
#  define EXPERIMENTAL_BIT(U, l, d) [EXPERIMENT_##U] = #l,
764
#  include "tbl/experimental_bits.h"
765
       NULL
766
};
767
768
int v_matchproto_(tweak_t)
769 82240
tweak_experimental(struct vsb *vsb, const struct parspec *par, const char *arg)
770
{
771
772 82240
        return (tweak_generic_bits(vsb, par, arg, mgt_param.experimental_bits,
773
            EXPERIMENT_Reserved, experimental_tags, "experimental bit", '+'));
774
}
775
776
/*--------------------------------------------------------------------
777
 * The feature parameter
778
 */
779
780
static const char * const feature_tags[] = {
781
#  define FEATURE_BIT(U, l, d) [FEATURE_##U] = #l,
782
#  include "tbl/feature_bits.h"
783
       NULL
784
};
785
786
int v_matchproto_(tweak_t)
787 86960
tweak_feature(struct vsb *vsb, const struct parspec *par, const char *arg)
788
{
789
790 86960
        return (tweak_generic_bits(vsb, par, arg, mgt_param.feature_bits,
791
            FEATURE_Reserved, feature_tags, "feature bit", '+'));
792
}
793
794
/*--------------------------------------------------------------------
795
 * The vcc_feature parameter
796
 */
797
798
static const char * const vcc_feature_tags[] = {
799
#  define VCC_FEATURE_BIT(U, l, d) [VCC_FEATURE_##U] = #l,
800
#  include "tbl/vcc_feature_bits.h"
801
       NULL
802
};
803
804
int v_matchproto_(tweak_t)
805 83360
tweak_vcc_feature(struct vsb *vsb, const struct parspec *par, const char *arg)
806
{
807
        const struct parspec *orig;
808
        char buf[32];
809
        int val;
810
811 83360
        if (arg != NULL && arg != JSON_FMT &&
812 41840
            strcmp(par->name, "vcc_feature")) {
813 0
                orig = TRUST_ME(par->priv);
814 0
                val = parse_boolean(vsb, arg);
815 0
                if (val < 0)
816 0
                        return (-1);
817 0
                bprintf(buf, "%c%s", val ? '+' : '-',
818
                    par->name + strlen("vcc_"));
819 0
                return (tweak_vcc_feature(vsb, orig, buf));
820
        }
821 83360
        return (tweak_generic_bits(vsb, par, arg, mgt_param.vcc_feature_bits,
822
            VCC_FEATURE_Reserved, vcc_feature_tags, "vcc_feature bit", '+'));
823 83360
}