detach stdin/stdout/stderr from child processes, implement a separate logging pipe...
[project/netifd.git] / bridge.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <assert.h>
5 #include <errno.h>
6
7 #include "netifd.h"
8 #include "device.h"
9 #include "interface.h"
10 #include "system.h"
11
12 enum {
13         BRIDGE_ATTR_IFNAME,
14         BRIDGE_ATTR_STP,
15         BRIDGE_ATTR_FORWARD_DELAY,
16         BRIDGE_ATTR_AGEING_TIME,
17         BRIDGE_ATTR_HELLO_TIME,
18         BRIDGE_ATTR_MAX_AGE,
19         __BRIDGE_ATTR_MAX
20 };
21
22 static const struct blobmsg_policy bridge_attrs[__BRIDGE_ATTR_MAX] = {
23         [BRIDGE_ATTR_IFNAME] = { "ifname", BLOBMSG_TYPE_ARRAY },
24         [BRIDGE_ATTR_STP] = { "stp", BLOBMSG_TYPE_BOOL },
25         [BRIDGE_ATTR_FORWARD_DELAY] = { "forward_delay", BLOBMSG_TYPE_INT32 },
26         [BRIDGE_ATTR_AGEING_TIME] = { "ageing_time", BLOBMSG_TYPE_INT32 },
27         [BRIDGE_ATTR_HELLO_TIME] = { "hello_time", BLOBMSG_TYPE_INT32 },
28         [BRIDGE_ATTR_MAX_AGE] = { "max_age", BLOBMSG_TYPE_INT32 },
29 };
30
31 static const union config_param_info bridge_attr_info[__BRIDGE_ATTR_MAX] = {
32         [BRIDGE_ATTR_IFNAME] = { .type = BLOBMSG_TYPE_STRING },
33 };
34
35 static const struct config_param_list bridge_attr_list = {
36         .n_params = __BRIDGE_ATTR_MAX,
37         .params = bridge_attrs,
38         .info = bridge_attr_info,
39
40         .n_next = 1,
41         .next = { &device_attr_list },
42 };
43
44 static struct device *bridge_create(struct blob_attr *attr);
45 static void bridge_config_init(struct device *dev);
46 static void bridge_free(struct device *dev);
47 static void bridge_dump_info(struct device *dev, struct blob_buf *b);
48
49 const struct device_type bridge_device_type = {
50         .name = "Bridge",
51         .config_params = &bridge_attr_list,
52
53         .create = bridge_create,
54         .config_init = bridge_config_init,
55         .free = bridge_free,
56         .dump_info = bridge_dump_info,
57 };
58
59 struct bridge_state {
60         struct device dev;
61         device_state_cb set_state;
62
63         struct bridge_config config;
64         struct blob_attr *ifnames;
65         bool active;
66
67         struct list_head members;
68         int n_present;
69 };
70
71 struct bridge_member {
72         struct list_head list;
73         struct bridge_state *bst;
74         struct device_user dev;
75         bool present;
76 };
77
78 static int
79 bridge_disable_member(struct bridge_member *bm)
80 {
81         struct bridge_state *bst = bm->bst;
82
83         if (!bm->present)
84                 return 0;
85
86         system_bridge_delif(&bst->dev, bm->dev.dev);
87         device_release(&bm->dev);
88
89         return 0;
90 }
91
92 static int
93 bridge_enable_member(struct bridge_member *bm)
94 {
95         struct bridge_state *bst = bm->bst;
96         int ret;
97
98         if (!bm->present)
99                 return 0;
100
101         ret = device_claim(&bm->dev);
102         if (ret < 0)
103                 goto error;
104
105         ret = system_bridge_addif(&bst->dev, bm->dev.dev);
106         if (ret < 0)
107                 goto error;
108
109         return 0;
110
111 error:
112         bm->present = false;
113         bst->n_present--;
114         return ret;
115 }
116
117 static void
118 bridge_member_cb(struct device_user *dev, enum device_event ev)
119 {
120         struct bridge_member *bm = container_of(dev, struct bridge_member, dev);
121         struct bridge_state *bst = bm->bst;
122
123         switch (ev) {
124         case DEV_EVENT_ADD:
125                 assert(!bm->present);
126
127                 bm->present = true;
128                 bst->n_present++;
129
130                 if (bst->dev.active)
131                         bridge_enable_member(bm);
132                 else if (bst->n_present == 1)
133                         device_set_present(&bst->dev, true);
134
135                 break;
136         case DEV_EVENT_REMOVE:
137                 if (!bm->present)
138                         return;
139
140                 if (bst->dev.active)
141                         bridge_disable_member(bm);
142
143                 bm->present = false;
144                 bm->bst->n_present--;
145                 if (bst->n_present == 0)
146                         device_set_present(&bst->dev, false);
147
148                 break;
149         default:
150                 return;
151         }
152 }
153
154 static int
155 bridge_set_down(struct bridge_state *bst)
156 {
157         struct bridge_member *bm;
158
159         bst->set_state(&bst->dev, false);
160
161         list_for_each_entry(bm, &bst->members, list)
162                 bridge_disable_member(bm);
163
164         system_bridge_delbr(&bst->dev);
165
166         return 0;
167 }
168
169 static int
170 bridge_set_up(struct bridge_state *bst)
171 {
172         struct bridge_member *bm;
173         int ret;
174
175         if (!bst->n_present)
176                 return -ENOENT;
177
178         ret = system_bridge_addbr(&bst->dev, &bst->config);
179         if (ret < 0)
180                 goto out;
181
182         list_for_each_entry(bm, &bst->members, list)
183                 bridge_enable_member(bm);
184
185         if (!bst->n_present) {
186                 /* initialization of all member interfaces failed */
187                 system_bridge_delbr(&bst->dev);
188                 device_set_present(&bst->dev, false);
189                 return -ENOENT;
190         }
191
192         ret = bst->set_state(&bst->dev, true);
193         if (ret < 0)
194                 bridge_set_down(bst);
195
196 out:
197         return ret;
198 }
199
200 static int
201 bridge_set_state(struct device *dev, bool up)
202 {
203         struct bridge_state *bst;
204
205         bst = container_of(dev, struct bridge_state, dev);
206
207         if (up)
208                 return bridge_set_up(bst);
209         else
210                 return bridge_set_down(bst);
211 }
212
213 static struct bridge_member *
214 bridge_create_member(struct bridge_state *bst, struct device *dev)
215 {
216         struct bridge_member *bm;
217
218         bm = calloc(1, sizeof(*bm));
219         bm->bst = bst;
220         bm->dev.cb = bridge_member_cb;
221         device_add_user(&bm->dev, dev);
222
223         list_add_tail(&bm->list, &bst->members);
224
225         if (bst->dev.active)
226                 bridge_enable_member(bm);
227
228         return bm;
229 }
230
231 static void
232 bridge_free_member(struct bridge_member *bm)
233 {
234         if (bm->present) {
235                 bridge_member_cb(&bm->dev, DEV_EVENT_REMOVE);
236                 bm->bst->n_present--;
237                 if (bm->bst->dev.active)
238                         bridge_disable_member(bm);
239         }
240
241         list_del(&bm->list);
242         device_remove_user(&bm->dev);
243         free(bm);
244 }
245
246 static void
247 bridge_add_member(struct bridge_state *bst, const char *name)
248 {
249         struct device *dev;
250
251         dev = device_get(name, true);
252         if (!dev)
253                 return;
254
255         bridge_create_member(bst, dev);
256 }
257
258 static int
259 bridge_hotplug_add(struct device *dev, struct device *member)
260 {
261         struct bridge_state *bst = container_of(dev, struct bridge_state, dev);
262
263         bridge_create_member(bst, member);
264
265         return 0;
266 }
267
268 static int
269 bridge_hotplug_del(struct device *dev, struct device *member)
270 {
271         struct bridge_state *bst = container_of(dev, struct bridge_state, dev);
272         struct bridge_member *bm;
273
274         list_for_each_entry(bm, &bst->members, list) {
275                 if (bm->dev.dev != member)
276                         continue;
277
278                 bridge_free_member(bm);
279                 return 0;
280         }
281
282         return -ENOENT;
283 }
284
285 static const struct device_hotplug_ops bridge_ops = {
286         .add = bridge_hotplug_add,
287         .del = bridge_hotplug_del
288 };
289
290 static void
291 bridge_free(struct device *dev)
292 {
293         struct bridge_state *bst;
294         struct bridge_member *bm;
295
296         device_cleanup(dev);
297         bst = container_of(dev, struct bridge_state, dev);
298         while (!list_empty(&bst->members)) {
299                 bm = list_first_entry(&bst->members, struct bridge_member, list);
300                 bridge_free_member(bm);
301         }
302         free(bst);
303 }
304
305 static void
306 bridge_dump_info(struct device *dev, struct blob_buf *b)
307 {
308         struct bridge_state *bst;
309         struct bridge_member *bm;
310         void *list;
311
312         bst = container_of(dev, struct bridge_state, dev);
313
314         list = blobmsg_open_array(b, "bridge-members");
315         list_for_each_entry(bm, &bst->members, list) {
316                 blobmsg_add_string(b, NULL, bm->dev.dev->ifname);
317         }
318         blobmsg_close_array(b, list);
319 }
320
321 static void
322 bridge_config_init(struct device *dev)
323 {
324         struct bridge_state *bst;
325         struct blob_attr *cur;
326         int rem;
327
328         bst = container_of(dev, struct bridge_state, dev);
329         blobmsg_for_each_attr(cur, bst->ifnames, rem) {
330                 bridge_add_member(bst, blobmsg_data(cur));
331         }
332 }
333
334 static void
335 bridge_apply_settings(struct bridge_state *bst, struct blob_attr **tb)
336 {
337         struct bridge_config *cfg = &bst->config;
338         struct blob_attr *cur;
339
340         /* defaults */
341         cfg->stp = true;
342         cfg->forward_delay = 1;
343
344         if ((cur = tb[BRIDGE_ATTR_STP]))
345                 cfg->stp = blobmsg_get_bool(cur);
346
347         if ((cur = tb[BRIDGE_ATTR_FORWARD_DELAY]))
348                 cfg->forward_delay = blobmsg_get_u32(cur);
349
350         if ((cur = tb[BRIDGE_ATTR_AGEING_TIME])) {
351                 cfg->ageing_time = blobmsg_get_u32(cur);
352                 cfg->flags |= BRIDGE_OPT_AGEING_TIME;
353         }
354
355         if ((cur = tb[BRIDGE_ATTR_HELLO_TIME])) {
356                 cfg->hello_time = blobmsg_get_u32(cur);
357                 cfg->flags |= BRIDGE_OPT_HELLO_TIME;
358         }
359
360         if ((cur = tb[BRIDGE_ATTR_MAX_AGE])) {
361                 cfg->max_age = blobmsg_get_u32(cur);
362                 cfg->flags |= BRIDGE_OPT_MAX_AGE;
363         }
364 }
365
366 static struct device *
367 bridge_create(struct blob_attr *attr)
368 {
369         struct blob_attr *tb_dev[__DEV_ATTR_MAX];
370         struct blob_attr *tb_br[__BRIDGE_ATTR_MAX];
371         struct bridge_state *bst;
372         struct device *dev = NULL;
373         const char *name;
374
375         blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, tb_dev,
376                 blob_data(attr), blob_len(attr));
377         blobmsg_parse(bridge_attrs, __BRIDGE_ATTR_MAX, tb_br,
378                 blob_data(attr), blob_len(attr));
379
380         if (!tb_dev[DEV_ATTR_NAME])
381                 return NULL;
382
383         if (!tb_br[BRIDGE_ATTR_IFNAME])
384                 return NULL;
385
386         name = blobmsg_data(tb_dev[DEV_ATTR_NAME]);
387
388         bst = calloc(1, sizeof(*bst));
389         if (!bst)
390                 return NULL;
391
392         dev = &bst->dev;
393         device_init(dev, &bridge_device_type, name);
394         device_init_settings(dev, tb_dev);
395         dev->config_pending = true;
396         bst->ifnames = tb_br[BRIDGE_ATTR_IFNAME];
397         bridge_apply_settings(bst, tb_br);
398
399         bst->set_state = dev->set_state;
400         dev->set_state = bridge_set_state;
401
402         dev->hotplug_ops = &bridge_ops;
403
404         INIT_LIST_HEAD(&bst->members);
405
406         return dev;
407 }
408
409