]>
Commit | Line | Data |
---|---|---|
96673e06 DL |
1 | /* |
2 | * FRR switchable defaults. | |
3 | * Copyright (C) 2017-2019 David Lamparter for NetDEF, Inc. | |
4 | * | |
5 | * Permission to use, copy, modify, and distribute this software for any | |
6 | * purpose with or without fee is hereby granted, provided that the above | |
7 | * copyright notice and this permission notice appear in all copies. | |
8 | * | |
9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
16 | */ | |
17 | ||
18 | #ifndef _FRR_DEFAULTS_H | |
19 | #define _FRR_DEFAULTS_H | |
20 | ||
f210b3e3 DL |
21 | #include <stdbool.h> |
22 | ||
ac4adef4 DL |
23 | #include "compiler.h" |
24 | ||
17e38209 RW |
25 | #ifdef __cplusplus |
26 | extern "C" { | |
27 | #endif | |
28 | ||
ac4adef4 DL |
29 | /* frr_default wraps information about a default that has different |
30 | * values depending on FRR version or default-set | |
31 | * | |
32 | * frr_default_entry describes one match rule and the resulting value; | |
33 | * entries are evaluated in order and the first matching is used. | |
34 | * | |
35 | * If both match_version and match_profile are specified, they must both | |
36 | * match. A NULL value matches everything. | |
37 | */ | |
38 | struct frr_default_entry { | |
39 | /* syntax: "(<|<=|==|>=|>) [whitespace] version", e.g. | |
40 | * ">= 6.1-dev" "<6.0" | |
41 | */ | |
42 | const char *match_version; | |
43 | /* exact profile string to compare against */ | |
44 | const char *match_profile; | |
45 | ||
46 | /* value to use */ | |
f210b3e3 | 47 | bool val_bool; |
ac4adef4 DL |
48 | const char *val_str; |
49 | long val_long; | |
50 | unsigned long val_ulong; | |
51 | float val_float; | |
52 | }; | |
53 | ||
54 | /* one struct frr_default exists for each malleable default value */ | |
55 | struct frr_default { | |
56 | struct frr_default *next; | |
57 | ||
58 | /* for UI/debug use */ | |
59 | const char *name; | |
60 | ||
61 | /* the following two sets of variables differ because the written | |
62 | * config always targets the *current* FRR version | |
63 | * | |
64 | * e.g. if you load a config that has "frr version 5.0" on 6.0 | |
65 | * *dflt_long => set to the default value in 5.0 | |
66 | * *save_long => set to the default value in 6.0 | |
67 | * config save will write "frr version 6.0" with 6.0 defaults | |
68 | */ | |
69 | ||
70 | /* variable holding the default value for reading/use */ | |
f210b3e3 | 71 | bool *dflt_bool; |
ac4adef4 DL |
72 | const char **dflt_str; |
73 | long *dflt_long; | |
74 | unsigned long *dflt_ulong; | |
75 | float *dflt_float; | |
76 | ||
77 | /* variable to use when comparing for config save */ | |
f210b3e3 | 78 | bool *save_bool; |
ac4adef4 DL |
79 | const char **save_str; |
80 | long *save_long; | |
81 | unsigned long *save_ulong; | |
82 | float *save_float; | |
83 | ||
84 | struct frr_default_entry entries[]; | |
85 | }; | |
86 | ||
87 | #define _FRR_CFG_DEFAULT(type, typname, varname, ...) \ | |
88 | static type DFLT_##varname; \ | |
89 | static type SAVE_##varname; \ | |
90 | static struct frr_default _dflt_##varname = { \ | |
91 | .name = #varname, \ | |
92 | .dflt_##typname = &DFLT_##varname, \ | |
93 | .save_##typname = &SAVE_##varname, \ | |
94 | .entries = { __VA_ARGS__ }, \ | |
95 | }; \ | |
96 | static void _dfltinit_##varname(void) \ | |
97 | __attribute__((_CONSTRUCTOR(1000))); \ | |
98 | static void _dfltinit_##varname(void) \ | |
99 | { \ | |
100 | frr_default_add(&_dflt_##varname); \ | |
67b0f40c DL |
101 | } \ |
102 | MACRO_REQUIRE_SEMICOLON() /* end */ | |
ac4adef4 DL |
103 | |
104 | /* use: | |
105 | * FRR_CFG_DEFAULT_LONG(SHARP_BLUNTNESS, | |
106 | * { .val_long = 2, .match_version = ">= 10.0" }, | |
107 | * { .val_long = 1, .match_profile = "datacenter" }, | |
108 | * { .val_long = 0 }, | |
109 | * ) | |
110 | * | |
111 | * This will create DFLT_SHARP_BLUNTNESS and SAVE_SHARP_BLUNTNESS variables. | |
112 | * | |
113 | * Note: preprocessor defines cannot be used as variable names because they | |
114 | * will be expanded and blow up with a compile error. Use an enum or add an | |
115 | * extra _ at the beginning (e.g. _SHARP_BLUNTNESS => DFLT__SHARP_BLUNTNESS) | |
116 | */ | |
f210b3e3 DL |
117 | #define FRR_CFG_DEFAULT_BOOL(varname, ...) \ |
118 | _FRR_CFG_DEFAULT(bool, bool, varname, ## __VA_ARGS__) | |
ac4adef4 DL |
119 | #define FRR_CFG_DEFAULT_LONG(varname, ...) \ |
120 | _FRR_CFG_DEFAULT(long, long, varname, ## __VA_ARGS__) | |
121 | #define FRR_CFG_DEFAULT_ULONG(varname, ...) \ | |
122 | _FRR_CFG_DEFAULT(unsigned long, ulong, varname, ## __VA_ARGS__) | |
123 | #define FRR_CFG_DEFAULT_FLOAT(varname, ...) \ | |
124 | _FRR_CFG_DEFAULT(float, float, varname, ## __VA_ARGS__) | |
125 | #define FRR_CFG_DEFAULT_STR(varname, ...) \ | |
126 | _FRR_CFG_DEFAULT(const char *, str, varname, ## __VA_ARGS__) | |
127 | ||
128 | ||
129 | /* daemons don't need to call any of these, libfrr handles that */ | |
130 | extern void frr_default_add(struct frr_default *dflt); | |
131 | extern void frr_defaults_version_set(const char *version); | |
132 | extern void frr_defaults_profile_set(const char *profile); | |
133 | extern const char *frr_defaults_version(void); | |
134 | extern const char *frr_defaults_profile(void); | |
135 | extern void frr_defaults_apply(void); | |
136 | ||
137 | extern const char *frr_defaults_profiles[]; | |
138 | extern bool frr_defaults_profile_valid(const char *profile); | |
139 | ||
96673e06 DL |
140 | /* like strcmp(), but with version ordering */ |
141 | extern int frr_version_cmp(const char *aa, const char *bb); | |
142 | ||
17e38209 RW |
143 | #ifdef __cplusplus |
144 | } | |
145 | #endif | |
146 | ||
96673e06 | 147 | #endif /* _FRR_DEFAULTS_H */ |