]> git.ipfire.org Git - thirdparty/bird.git/blob - conf/conf.c
First attempt on dynamic reconfiguration. There are still lots of bugs
[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 #include <setjmp.h>
10 #include <stdarg.h>
11
12 #define LOCAL_DEBUG
13
14 #include "nest/bird.h"
15 #include "nest/route.h"
16 #include "nest/protocol.h"
17 #include "nest/iface.h"
18 #include "lib/resource.h"
19 #include "lib/string.h"
20 #include "lib/event.h"
21 #include "conf/conf.h"
22 #include "filter/filter.h"
23
24 static jmp_buf conf_jmpbuf;
25
26 struct config *config, *new_config, *old_config, *future_config;
27 static event *config_event;
28
29 struct config *
30 config_alloc(byte *name)
31 {
32 pool *p = rp_new(&root_pool, "Config");
33 linpool *l = lp_new(p, 4080);
34 struct config *c = lp_allocz(l, sizeof(struct config));
35
36 c->pool = p;
37 cfg_mem = c->mem = l;
38 c->file_name = cfg_strdup(name);
39 return c;
40 }
41
42 int
43 config_parse(struct config *c)
44 {
45 debug("Parsing configuration file `%s'\n", c->file_name);
46 new_config = c;
47 cfg_mem = c->mem;
48 if (setjmp(conf_jmpbuf))
49 return 0;
50 cf_lex_init(0);
51 sysdep_preconfig(c);
52 protos_preconfig(c);
53 rt_preconfig(c);
54 cf_parse();
55 filters_postconfig(); /* FIXME: Do we really need this? */
56 protos_postconfig(c);
57 #ifdef IPV6
58 if (!c->router_id)
59 cf_error("Router ID must be configured manually on IPv6 routers");
60 #endif
61 return 1;
62 }
63
64 int
65 cli_parse(struct config *c)
66 {
67 new_config = c;
68 c->sym_fallback = config->sym_hash;
69 cfg_mem = c->mem;
70 if (setjmp(conf_jmpbuf))
71 return 0;
72 cf_lex_init(1);
73 cf_parse();
74 return 1;
75 }
76
77 void
78 config_free(struct config *c)
79 {
80 rfree(c->pool);
81 }
82
83 void
84 config_add_obstacle(struct config *c)
85 {
86 DBG("+++ adding obstacle %d\n", c->obstacle_count);
87 c->obstacle_count++;
88 }
89
90 void
91 config_del_obstacle(struct config *c)
92 {
93 DBG("+++ deleting obstacle %d\n", c->obstacle_count);
94 c->obstacle_count--;
95 if (!c->obstacle_count)
96 {
97 ASSERT(config_event);
98 ev_schedule(config_event);
99 }
100 }
101
102 static int
103 global_commit(struct config *c, struct config *old)
104 {
105 if (!old)
106 return 0;
107 if (c->router_id != old->router_id)
108 return 1;
109 return 0;
110 }
111
112 static int
113 config_do_commit(struct config *c)
114 {
115 int force_restart, nobs;
116
117 DBG("do_commit\n");
118 old_config = config;
119 config = c;
120 if (old_config)
121 old_config->obstacle_count++;
122 DBG("sysdep_commit\n");
123 force_restart = sysdep_commit(c, old_config);
124 DBG("global_commit\n");
125 force_restart |= global_commit(c, old_config);
126 DBG("rt_commit\n");
127 rt_commit(c, old_config);
128 DBG("protos_commit\n");
129 protos_commit(c, old_config, force_restart);
130 new_config = NULL; /* Just to be sure nobody uses that now */
131 if (old_config)
132 nobs = --old_config->obstacle_count;
133 else
134 nobs = 0;
135 DBG("do_commit finished with %d obstacles remaining\n", nobs);
136 return !nobs;
137 }
138
139 static int
140 config_done(void *unused)
141 {
142 struct config *c;
143
144 DBG("config_done\n");
145 for(;;)
146 {
147 log(L_INFO "Reconfigured");
148 if (old_config)
149 {
150 config_free(old_config);
151 old_config = NULL;
152 }
153 if (!future_config)
154 break;
155 c = future_config;
156 future_config = NULL;
157 log(L_INFO "Switching to queued configuration...");
158 if (!config_do_commit(c))
159 break;
160 }
161 return 0;
162 }
163
164 int
165 config_commit(struct config *c)
166 {
167 if (!config) /* First-time configuration */
168 {
169 config_do_commit(c);
170 return CONF_DONE;
171 }
172 if (old_config) /* Reconfiguration already in progress */
173 {
174 if (future_config)
175 {
176 log(L_INFO "Queueing new configuration, ignoring the one already queued");
177 config_free(future_config);
178 }
179 else
180 log(L_INFO "Queued new configuration");
181 future_config = c;
182 return CONF_QUEUED;
183 }
184 if (config_do_commit(c))
185 {
186 config_done(NULL);
187 return CONF_DONE;
188 }
189 if (!config_event)
190 {
191 config_event = ev_new(&root_pool);
192 config_event->hook = config_done;
193 }
194 return CONF_PROGRESS;
195 }
196
197 void
198 cf_error(char *msg, ...)
199 {
200 char buf[1024];
201 va_list args;
202
203 va_start(args, msg);
204 if (bvsnprintf(buf, sizeof(buf), msg, args) < 0)
205 strcpy(buf, "<bug: error message too long>");
206 new_config->err_msg = cfg_strdup(buf);
207 new_config->err_lino = conf_lino;
208 longjmp(conf_jmpbuf, 1);
209 }
210
211 char *
212 cfg_strdup(char *c)
213 {
214 int l = strlen(c) + 1;
215 char *z = cfg_allocu(l);
216 memcpy(z, c, l);
217 return z;
218 }