]> git.ipfire.org Git - thirdparty/bird.git/blob - conf/conf.c
Implements MRTdump feature.
[thirdparty/bird.git] / conf / conf.c
1 /*
2 * BIRD Internet Routing Daemon -- Configuration File Handling
3 *
4 * (c) 1998--2000 Martin Mares <mj@ucw.cz>
5 *
6 * Can be freely distributed and used under the terms of the GNU GPL.
7 */
8
9 /**
10 * DOC: Configuration manager
11 *
12 * Configuration of BIRD is complex, yet straightforward. There are three
13 * modules taking care of the configuration: config manager (which takes care
14 * of storage of the config information and controls switching between configs),
15 * lexical analyzer and parser.
16 *
17 * The configuration manager stores each config as a &config structure
18 * accompanied by a linear pool from which all information associated
19 * with the config and pointed to by the &config structure is allocated.
20 *
21 * There can exist up to four different configurations at one time: an active
22 * one (pointed to by @config), configuration we are just switching from
23 * (@old_config), one queued for the next reconfiguration (@future_config;
24 * if it's non-%NULL and the user wants to reconfigure once again, we just
25 * free the previous queued config and replace it with the new one) and
26 * finally a config being parsed (@new_config).
27 *
28 * Loading of new configuration is very simple: just call config_alloc()
29 * to get a new &config structure, then use config_parse() to parse a
30 * configuration file and fill all fields of the structure
31 * and finally ask the config manager to switch to the new
32 * config by calling config_commit().
33 *
34 * CLI commands are parsed in a very similar way -- there is also a stripped-down
35 * &config structure associated with them and they are lex-ed and parsed by the
36 * same functions, only a special fake token is prepended before the command
37 * text to make the parser recognize only the rules corresponding to CLI commands.
38 */
39
40 #include <setjmp.h>
41 #include <stdarg.h>
42
43 #undef LOCAL_DEBUG
44
45 #include "nest/bird.h"
46 #include "nest/route.h"
47 #include "nest/protocol.h"
48 #include "nest/iface.h"
49 #include "lib/resource.h"
50 #include "lib/string.h"
51 #include "lib/event.h"
52 #include "lib/timer.h"
53 #include "conf/conf.h"
54 #include "filter/filter.h"
55
56 static jmp_buf conf_jmpbuf;
57
58 struct config *config, *new_config, *old_config, *future_config;
59 static event *config_event;
60 int shutting_down;
61 bird_clock_t boot_time;
62
63 /**
64 * config_alloc - allocate a new configuration
65 * @name: name of the config
66 *
67 * This function creates new &config structure, attaches a resource
68 * pool and a linear memory pool to it and makes it available for
69 * further use. Returns a pointer to the structure.
70 */
71 struct config *
72 config_alloc(byte *name)
73 {
74 pool *p = rp_new(&root_pool, "Config");
75 linpool *l = lp_new(p, 4080);
76 struct config *c = lp_allocz(l, sizeof(struct config));
77
78 c->mrtdump_file = -1; /* Hack, this should be sysdep-specific */
79 c->pool = p;
80 cfg_mem = c->mem = l;
81 c->file_name = cfg_strdup(name);
82 c->load_time = now;
83 if (!boot_time)
84 boot_time = now;
85 return c;
86 }
87
88 /**
89 * config_parse - parse a configuration
90 * @c: configuration
91 *
92 * config_parse() reads input by calling a hook function pointed to
93 * by @cf_read_hook and parses it according to the configuration
94 * grammar. It also calls all the preconfig and postconfig hooks
95 * before, resp. after parsing.
96 *
97 * Result: 1 if the config has been parsed successfully, 0 if any
98 * error has occurred (such as anybody calling cf_error()) and
99 * the @err_msg field has been set to the error message.
100 */
101 int
102 config_parse(struct config *c)
103 {
104 DBG("Parsing configuration file `%s'\n", c->file_name);
105 new_config = c;
106 cfg_mem = c->mem;
107 if (setjmp(conf_jmpbuf))
108 return 0;
109 cf_lex_init(0);
110 sysdep_preconfig(c);
111 protos_preconfig(c);
112 rt_preconfig(c);
113 cf_parse();
114 protos_postconfig(c);
115 #ifdef IPV6
116 if (!c->router_id)
117 cf_error("Router ID must be configured manually on IPv6 routers");
118 #endif
119 return 1;
120 }
121
122 /**
123 * cli_parse - parse a CLI command
124 * @c: temporary config structure
125 *
126 * cli_parse() is similar to config_parse(), but instead of a configuration,
127 * it parses a CLI command. See the CLI module for more information.
128 */
129 int
130 cli_parse(struct config *c)
131 {
132 new_config = c;
133 c->sym_fallback = config->sym_hash;
134 cfg_mem = c->mem;
135 if (setjmp(conf_jmpbuf))
136 return 0;
137 cf_lex_init(1);
138 cf_parse();
139 return 1;
140 }
141
142 /**
143 * config_free - free a configuration
144 * @c: configuration to be freed
145 *
146 * This function takes a &config structure and frees all resources
147 * associated with it.
148 */
149 void
150 config_free(struct config *c)
151 {
152 rfree(c->pool);
153 }
154
155 void
156 config_add_obstacle(struct config *c)
157 {
158 DBG("+++ adding obstacle %d\n", c->obstacle_count);
159 c->obstacle_count++;
160 }
161
162 void
163 config_del_obstacle(struct config *c)
164 {
165 DBG("+++ deleting obstacle %d\n", c->obstacle_count);
166 c->obstacle_count--;
167 if (!c->obstacle_count)
168 {
169 ASSERT(config_event);
170 ev_schedule(config_event);
171 }
172 }
173
174 static int
175 global_commit(struct config *new, struct config *old)
176 {
177 if (!old)
178 return 0;
179
180 if (!ipa_equal(old->listen_bgp_addr, new->listen_bgp_addr) ||
181 (old->listen_bgp_port != new->listen_bgp_port) ||
182 (old->listen_bgp_flags != new->listen_bgp_flags))
183 log(L_WARN "Reconfiguration of BGP listening socket not implemented, please restart BIRD.");
184
185 if (!new->router_id)
186 new->router_id = old->router_id;
187 if (new->router_id != old->router_id)
188 return 1;
189 return 0;
190 }
191
192 static int
193 config_do_commit(struct config *c, int type)
194 {
195 int force_restart, nobs;
196
197 DBG("do_commit\n");
198 old_config = config;
199 config = new_config = c;
200 if (old_config)
201 old_config->obstacle_count++;
202 DBG("sysdep_commit\n");
203 force_restart = sysdep_commit(c, old_config);
204 DBG("global_commit\n");
205 force_restart |= global_commit(c, old_config);
206 DBG("rt_commit\n");
207 rt_commit(c, old_config);
208 DBG("protos_commit\n");
209 protos_commit(c, old_config, force_restart, type);
210 new_config = NULL; /* Just to be sure nobody uses that now */
211 if (old_config)
212 nobs = --old_config->obstacle_count;
213 else
214 nobs = 0;
215 DBG("do_commit finished with %d obstacles remaining\n", nobs);
216 return !nobs;
217 }
218
219 static void
220 config_done(void *unused UNUSED)
221 {
222 struct config *c;
223
224 DBG("config_done\n");
225 for(;;)
226 {
227 if (config->shutdown)
228 sysdep_shutdown_done();
229 log(L_INFO "Reconfigured");
230 if (old_config)
231 {
232 config_free(old_config);
233 old_config = NULL;
234 }
235 if (!future_config)
236 break;
237 c = future_config;
238 future_config = NULL;
239 log(L_INFO "Switching to queued configuration...");
240 if (!config_do_commit(c, RECONFIG_HARD))
241 break;
242 }
243 }
244
245 /**
246 * config_commit - commit a configuration
247 * @c: new configuration
248 * @type: type of reconfiguration (RECONFIG_SOFT or RECONFIG_HARD)
249 *
250 * When a configuration is parsed and prepared for use, the
251 * config_commit() function starts the process of reconfiguration.
252 * It checks whether there is already a reconfiguration in progress
253 * in which case it just queues the new config for later processing.
254 * Else it notifies all modules about the new configuration by calling
255 * their commit() functions which can either accept it immediately
256 * or call config_add_obstacle() to report that they need some time
257 * to complete the reconfiguration. After all such obstacles are removed
258 * using config_del_obstacle(), the old configuration is freed and
259 * everything runs according to the new one.
260 *
261 * Result: %CONF_DONE if the configuration has been accepted immediately,
262 * %CONF_PROGRESS if it will take some time to switch to it, %CONF_QUEUED
263 * if it's been queued due to another reconfiguration being in progress now
264 * or %CONF_SHUTDOWN if BIRD is in shutdown mode and no new configurations
265 * are accepted.
266 */
267 int
268 config_commit(struct config *c, int type)
269 {
270 if (!config) /* First-time configuration */
271 {
272 config_do_commit(c, RECONFIG_HARD);
273 return CONF_DONE;
274 }
275 if (old_config) /* Reconfiguration already in progress */
276 {
277 if (shutting_down == 2)
278 {
279 log(L_INFO "New configuration discarded due to shutdown");
280 config_free(c);
281 return CONF_SHUTDOWN;
282 }
283 if (future_config)
284 {
285 log(L_INFO "Queueing new configuration, ignoring the one already queued");
286 config_free(future_config);
287 }
288 else
289 log(L_INFO "Queued new configuration");
290 future_config = c;
291 return CONF_QUEUED;
292 }
293 if (config_do_commit(c, type))
294 {
295 config_done(NULL);
296 return CONF_DONE;
297 }
298 if (!config_event)
299 {
300 config_event = ev_new(&root_pool);
301 config_event->hook = config_done;
302 }
303 return CONF_PROGRESS;
304 }
305
306 /**
307 * order_shutdown - order BIRD shutdown
308 *
309 * This function initiates shutdown of BIRD. It's accomplished by asking
310 * for switching to an empty configuration.
311 */
312 void
313 order_shutdown(void)
314 {
315 struct config *c;
316
317 if (shutting_down)
318 return;
319 log(L_INFO "Shutting down");
320 c = lp_alloc(config->mem, sizeof(struct config));
321 memcpy(c, config, sizeof(struct config));
322 init_list(&c->protos);
323 init_list(&c->tables);
324 c->shutdown = 1;
325 shutting_down = 1;
326 config_commit(c, RECONFIG_HARD);
327 shutting_down = 2;
328 }
329
330 /**
331 * cf_error - report a configuration error
332 * @msg: printf-like format string
333 *
334 * cf_error() can be called during execution of config_parse(), that is
335 * from the parser, a preconfig hook or a postconfig hook, to report an
336 * error in the configuration.
337 */
338 void
339 cf_error(char *msg, ...)
340 {
341 char buf[1024];
342 va_list args;
343
344 va_start(args, msg);
345 if (bvsnprintf(buf, sizeof(buf), msg, args) < 0)
346 strcpy(buf, "<bug: error message too long>");
347 new_config->err_msg = cfg_strdup(buf);
348 new_config->err_lino = conf_lino;
349 longjmp(conf_jmpbuf, 1);
350 }
351
352 /**
353 * cfg_strdup - copy a string to config memory
354 * @c: string to copy
355 *
356 * cfg_strdup() creates a new copy of the string in the memory
357 * pool associated with the configuration being currently parsed.
358 * It's often used when a string literal occurs in the configuration
359 * and we want to preserve it for further use.
360 */
361 char *
362 cfg_strdup(char *c)
363 {
364 int l = strlen(c) + 1;
365 char *z = cfg_allocu(l);
366 memcpy(z, c, l);
367 return z;
368 }