]> git.proxmox.com Git - mirror_frr.git/blob - lib/defaults.h
lib: Fix missing __be16 typedef on CentOS6
[mirror_frr.git] / lib / defaults.h
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
21 #include <stdbool.h>
22
23 #include "compiler.h"
24
25 /* frr_default wraps information about a default that has different
26 * values depending on FRR version or default-set
27 *
28 * frr_default_entry describes one match rule and the resulting value;
29 * entries are evaluated in order and the first matching is used.
30 *
31 * If both match_version and match_profile are specified, they must both
32 * match. A NULL value matches everything.
33 */
34 struct frr_default_entry {
35 /* syntax: "(<|<=|==|>=|>) [whitespace] version", e.g.
36 * ">= 6.1-dev" "<6.0"
37 */
38 const char *match_version;
39 /* exact profile string to compare against */
40 const char *match_profile;
41
42 /* value to use */
43 bool val_bool;
44 const char *val_str;
45 long val_long;
46 unsigned long val_ulong;
47 float val_float;
48 };
49
50 /* one struct frr_default exists for each malleable default value */
51 struct frr_default {
52 struct frr_default *next;
53
54 /* for UI/debug use */
55 const char *name;
56
57 /* the following two sets of variables differ because the written
58 * config always targets the *current* FRR version
59 *
60 * e.g. if you load a config that has "frr version 5.0" on 6.0
61 * *dflt_long => set to the default value in 5.0
62 * *save_long => set to the default value in 6.0
63 * config save will write "frr version 6.0" with 6.0 defaults
64 */
65
66 /* variable holding the default value for reading/use */
67 bool *dflt_bool;
68 const char **dflt_str;
69 long *dflt_long;
70 unsigned long *dflt_ulong;
71 float *dflt_float;
72
73 /* variable to use when comparing for config save */
74 bool *save_bool;
75 const char **save_str;
76 long *save_long;
77 unsigned long *save_ulong;
78 float *save_float;
79
80 struct frr_default_entry entries[];
81 };
82
83 #define _FRR_CFG_DEFAULT(type, typname, varname, ...) \
84 static type DFLT_##varname; \
85 static type SAVE_##varname; \
86 static struct frr_default _dflt_##varname = { \
87 .name = #varname, \
88 .dflt_##typname = &DFLT_##varname, \
89 .save_##typname = &SAVE_##varname, \
90 .entries = { __VA_ARGS__ }, \
91 }; \
92 static void _dfltinit_##varname(void) \
93 __attribute__((_CONSTRUCTOR(1000))); \
94 static void _dfltinit_##varname(void) \
95 { \
96 frr_default_add(&_dflt_##varname); \
97 }
98
99 /* use:
100 * FRR_CFG_DEFAULT_LONG(SHARP_BLUNTNESS,
101 * { .val_long = 2, .match_version = ">= 10.0" },
102 * { .val_long = 1, .match_profile = "datacenter" },
103 * { .val_long = 0 },
104 * )
105 *
106 * This will create DFLT_SHARP_BLUNTNESS and SAVE_SHARP_BLUNTNESS variables.
107 *
108 * Note: preprocessor defines cannot be used as variable names because they
109 * will be expanded and blow up with a compile error. Use an enum or add an
110 * extra _ at the beginning (e.g. _SHARP_BLUNTNESS => DFLT__SHARP_BLUNTNESS)
111 */
112 #define FRR_CFG_DEFAULT_BOOL(varname, ...) \
113 _FRR_CFG_DEFAULT(bool, bool, varname, ## __VA_ARGS__)
114 #define FRR_CFG_DEFAULT_LONG(varname, ...) \
115 _FRR_CFG_DEFAULT(long, long, varname, ## __VA_ARGS__)
116 #define FRR_CFG_DEFAULT_ULONG(varname, ...) \
117 _FRR_CFG_DEFAULT(unsigned long, ulong, varname, ## __VA_ARGS__)
118 #define FRR_CFG_DEFAULT_FLOAT(varname, ...) \
119 _FRR_CFG_DEFAULT(float, float, varname, ## __VA_ARGS__)
120 #define FRR_CFG_DEFAULT_STR(varname, ...) \
121 _FRR_CFG_DEFAULT(const char *, str, varname, ## __VA_ARGS__)
122
123
124 /* daemons don't need to call any of these, libfrr handles that */
125 extern void frr_default_add(struct frr_default *dflt);
126 extern void frr_defaults_version_set(const char *version);
127 extern void frr_defaults_profile_set(const char *profile);
128 extern const char *frr_defaults_version(void);
129 extern const char *frr_defaults_profile(void);
130 extern void frr_defaults_apply(void);
131
132 extern const char *frr_defaults_profiles[];
133 extern bool frr_defaults_profile_valid(const char *profile);
134
135 /* like strcmp(), but with version ordering */
136 extern int frr_version_cmp(const char *aa, const char *bb);
137
138 #endif /* _FRR_DEFAULTS_H */