-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathovs.c
692 lines (540 loc) · 13.4 KB
/
ovs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
/*
* netifd - network interface daemon
* Copyright (C) 2013 Helmut Schaa <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <errno.h>
#include "netifd.h"
#include "device.h"
#include "interface.h"
#include "system-ovs.h"
enum {
OVS_ATTR_IFNAME,
OVS_ATTR_BASE,
OVS_ATTR_TAG,
OVS_ATTR_EMPTY,
OVS_ATTR_TYPE,
OVS_ATTR_OPTIONS,
__OVS_ATTR_MAX
};
static const struct blobmsg_policy ovs_attrs[__OVS_ATTR_MAX] = {
[OVS_ATTR_IFNAME] = { "ifname", BLOBMSG_TYPE_ARRAY },
[OVS_ATTR_BASE] = { "ovs_base", BLOBMSG_TYPE_STRING },
[OVS_ATTR_TAG] = { "ovs_tag", BLOBMSG_TYPE_INT32 },
[OVS_ATTR_EMPTY] = { "ovs_empty", BLOBMSG_TYPE_BOOL },
[OVS_ATTR_TYPE] = { "ovs_type", BLOBMSG_TYPE_STRING },
[OVS_ATTR_OPTIONS] = { "ovs_options", BLOBMSG_TYPE_STRING },
};
static const struct uci_blob_param_info ovs_attr_info[__OVS_ATTR_MAX] = {
[OVS_ATTR_IFNAME] = { .type = BLOBMSG_TYPE_STRING },
};
static const struct uci_blob_param_list ovs_attr_list = {
.n_params = __OVS_ATTR_MAX,
.params = ovs_attrs,
.info = ovs_attr_info,
.n_next = 1,
.next = { &device_attr_list },
};
static struct device *ovs_create(const char *name, struct blob_attr *attr);
static void ovs_config_init(struct device *dev);
static void ovs_free(struct device *dev);
static void ovs_dump_info(struct device *dev, struct blob_buf *b);
enum dev_change_type
ovs_reload(struct device *dev, struct blob_attr *attr);
const struct device_type ovs_device_type = {
.name = "OpenVSwitch",
.config_params = &ovs_attr_list,
.create = ovs_create,
.config_init = ovs_config_init,
.reload = ovs_reload,
.free = ovs_free,
.dump_info = ovs_dump_info,
};
struct ovs_state {
struct device dev;
device_state_cb set_state;
struct blob_attr *config_data;
struct ovs_config config;
struct blob_attr *ifnames;
bool active;
bool force_active;
struct vlist_tree ports;
int n_present;
struct ovs_base *base;
};
struct ovs_port {
struct vlist_node node;
struct ovs_state *ost;
struct device_user dev;
bool present;
char name[];
};
struct ovs_base {
struct ovs_state *ost;
struct device_user dev;
bool present;
};
static void ovs_set_present(struct ovs_state *ost);
static int
ovs_disable_base(struct ovs_base *ob)
{
if (!ob->present)
return 0;
device_release(&ob->dev);
return 0;
}
static int
ovs_enable_base(struct ovs_base *ob)
{
int ret;
if (!ob->present)
return 0;
ret = device_claim(&ob->dev);
if (ret < 0)
goto error;
return 0;
error:
ob->present = false;
return ret;
}
static void
ovs_remove_base(struct ovs_base *ob)
{
struct ovs_state *ost = ob->ost;
if (!ob->present)
return;
if (ost->dev.active)
ovs_disable_base(ob);
ob->present = false;
ovs_set_present(ost);
}
static void
ovs_free_base(struct ovs_base *ob)
{
ovs_remove_base(ob);
device_remove_user(&ob->dev);
free(ob);
}
static int
ovs_disable_port(struct ovs_port *op)
{
struct ovs_state *ost = op->ost;
if (!op->present)
return 0;
system_ovs_delport(&ost->dev, op->dev.dev);
device_release(&op->dev);
device_broadcast_event(&ost->dev, DEV_EVENT_TOPO_CHANGE);
return 0;
}
static int
ovs_enable_port(struct ovs_port *op)
{
struct ovs_state *ost = op->ost;
int ret;
if (!op->present)
return 0;
ret = device_claim(&op->dev);
if (ret < 0)
goto error;
ret = system_ovs_addport(&ost->dev, op->dev.dev);
if (ret < 0) {
D(DEVICE, "Bridge device %s could not be added\n", op->dev.dev->ifname);
goto error;
}
ret = system_ovs_settype(op->dev.dev, &ost->config);
if (ret < 0)
D(DEVICE, "Bridge type %s of %s could not be set\n", ost->config.type, op->dev.dev->ifname);
ret = system_ovs_setoptions(op->dev.dev, &ost->config);
if (ret < 0)
D(DEVICE, "Bridge options %s of %s could not be set\n", ost->config.options, op->dev.dev->ifname);
device_broadcast_event(&ost->dev, DEV_EVENT_TOPO_CHANGE);
return 0;
error:
op->present = false;
ost->n_present--;
return ret;
}
static void
ovs_remove_port(struct ovs_port *op)
{
struct ovs_state *ost = op->ost;
if (!op->present)
return;
if (ost->dev.active)
ovs_disable_port(op);
op->present = false;
op->ost->n_present--;
ovs_set_present(ost);
}
static void
ovs_free_port(struct ovs_port *op)
{
struct device *dev = op->dev.dev;
ovs_remove_port(op);
device_remove_user(&op->dev);
/*
* When reloading the config and moving a device from one bridge to
* another, the other bridge may have tried to claim this device
* before it was removed here.
* Ensure that claiming the device is retried by toggling its present
* state
*/
if (dev->present) {
device_set_present(dev, false);
device_set_present(dev, true);
}
free(op);
}
static void
ovs_port_cb(struct device_user *dev, enum device_event ev)
{
struct ovs_port *op = container_of(dev, struct ovs_port, dev);
struct ovs_state *ost = op->ost;
switch (ev) {
case DEV_EVENT_ADD:
assert(!op->present);
op->present = true;
ost->n_present++;
if (ost->dev.active)
ovs_enable_port(op);
else if (ost->n_present == 1)
ovs_set_present(ost);
break;
case DEV_EVENT_REMOVE:
if (dev->hotplug) {
vlist_delete(&ost->ports, &op->node);
return;
}
if (op->present)
ovs_remove_port(op);
break;
default:
return;
}
}
static void
ovs_base_cb(struct device_user *dev, enum device_event ev)
{
struct ovs_base *ob = container_of(dev, struct ovs_base, dev);
struct ovs_state *ost = ob->ost;
switch (ev) {
case DEV_EVENT_ADD:
ob->present = true;
ovs_enable_base(ob);
ovs_set_present(ost);
case DEV_EVENT_REMOVE:
if (ob->present)
ovs_remove_base(ob);
break;
case DEV_EVENT_TOPO_CHANGE:
/* Propagate topo changes */
device_broadcast_event(&ost->dev, DEV_EVENT_TOPO_CHANGE);
break;
default:
return;
}
}
static int
ovs_set_down(struct ovs_state *ost)
{
struct ovs_port *op;
ost->set_state(&ost->dev, false);
vlist_for_each_element(&ost->ports, op, node)
ovs_disable_port(op);
if (ost->base)
ovs_disable_base(ost->base);
system_ovs_delbr(&ost->dev);
return 0;
}
static int
ovs_set_up(struct ovs_state *ost)
{
struct ovs_port *op;
int ret;
if (!ost->force_active && !ost->n_present)
return -ENOENT;
if (ost->base)
ovs_enable_base(ost->base);
ret = system_ovs_addbr(&ost->dev, &ost->config);
if (ret < 0)
goto out;
vlist_for_each_element(&ost->ports, op, node)
ovs_enable_port(op);
if (!ost->force_active && !ost->n_present) {
/* initialization of all port interfaces failed */
system_ovs_delbr(&ost->dev);
ovs_set_present(ost);
return -ENOENT;
}
ret = ost->set_state(&ost->dev, true);
if (ret < 0)
ovs_set_down(ost);
out:
return ret;
}
static int
ovs_set_state(struct device *dev, bool up)
{
struct ovs_state *ost;
ost = container_of(dev, struct ovs_state, dev);
if (up)
return ovs_set_up(ost);
else
return ovs_set_down(ost);
}
static struct ovs_port *
ovs_create_port(struct ovs_state *ost, struct device *dev, bool hotplug)
{
struct ovs_port *op;
op = calloc(1, sizeof(*op) + strlen(dev->ifname) + 1);
op->ost = ost;
op->dev.cb = ovs_port_cb;
op->dev.hotplug = hotplug;
strcpy(op->name, dev->ifname);
op->dev.dev = dev;
vlist_add(&ost->ports, &op->node, op->name);
if (hotplug)
op->node.version = -1;
return op;
}
static void
ovs_port_update(struct vlist_tree *tree, struct vlist_node *node_new,
struct vlist_node *node_old)
{
struct ovs_port *op;
struct device *dev;
if (node_new) {
op = container_of(node_new, struct ovs_port, node);
if (node_old) {
free(op);
return;
}
dev = op->dev.dev;
op->dev.dev = NULL;
device_add_user(&op->dev, dev);
}
if (node_old) {
op = container_of(node_old, struct ovs_port, node);
ovs_free_port(op);
}
}
static void
ovs_add_port(struct ovs_state *ost, const char *name)
{
struct device *dev;
dev = device_get(name, true);
if (!dev)
return;
ovs_create_port(ost, dev, false);
}
static struct ovs_base *
ovs_create_base(struct ovs_state *ost, struct device *dev, bool hotplug)
{
struct ovs_base *ob;
ob = calloc(1, sizeof(*ob));
if (!ob)
return NULL;
ob->ost = ost;
ob->dev.cb = ovs_base_cb;
device_add_user(&ob->dev, dev);
return ob;
}
static void
ovs_add_base(struct ovs_state *ost, const char *name)
{
struct device *dev;
dev = device_get(name, true);
if (!dev)
return;
ovs_create_base(ost, dev, false);
}
static int
ovs_hotplug_add(struct device *dev, struct device *port)
{
struct ovs_state *ost = container_of(dev, struct ovs_state, dev);
ovs_create_port(ost, port, true);
return 0;
}
static int
ovs_hotplug_del(struct device *dev, struct device *port)
{
struct ovs_state *ost = container_of(dev, struct ovs_state, dev);
struct ovs_port *op;
op = vlist_find(&ost->ports, port->ifname, op, node);
if (!op)
return UBUS_STATUS_NOT_FOUND;
vlist_delete(&ost->ports, &op->node);
return 0;
}
static int
ovs_hotplug_prepare(struct device *dev)
{
struct ovs_state *ost;
ost = container_of(dev, struct ovs_state, dev);
ost->force_active = true;
ovs_set_present(ost);
return 0;
}
static const struct device_hotplug_ops ovs_ops = {
.prepare = ovs_hotplug_prepare,
.add = ovs_hotplug_add,
.del = ovs_hotplug_del
};
static void
ovs_free(struct device *dev)
{
struct ovs_state *ost;
ost = container_of(dev, struct ovs_state, dev);
if (ost->base)
ovs_free_base(ost->base);
vlist_flush_all(&ost->ports);
free(ost);
}
static void
ovs_dump_info(struct device *dev, struct blob_buf *b)
{
struct ovs_state *ost;
struct ovs_port *op;
void *list;
ost = container_of(dev, struct ovs_state, dev);
system_if_dump_info(dev, b);
list = blobmsg_open_array(b, "ovs-ports");
vlist_for_each_element(&ost->ports, op, node)
blobmsg_add_string(b, NULL, op->dev.dev->ifname);
blobmsg_close_array(b, list);
if (ost->base)
blobmsg_add_string(b, "ovs_base", ost->base->dev.dev->ifname);
}
static void
ovs_set_present(struct ovs_state *ost)
{
bool present = false;
if (ost->base) {
/* Base device has to be available first */
if (!ost->base->present)
goto out;
}
if (ost->config.empty) {
present = true;
goto out;
}
ost->force_active = false;
if (ost->n_present == 0)
goto out;
present = true;
out:
device_set_present(&ost->dev, present);
}
static void
ovs_config_init(struct device *dev)
{
struct ovs_state *ost;
struct blob_attr *cur;
int rem;
ost = container_of(dev, struct ovs_state, dev);
if (ost->config.empty) {
ost->force_active = true;
ovs_set_present(ost);
}
if (ost->config.base) {
/* Pseudo bridge, requires base */
ovs_add_base(ost, ost->config.base);
}
vlist_update(&ost->ports);
if (ost->ifnames) {
blobmsg_for_each_attr(cur, ost->ifnames, rem) {
ovs_add_port(ost, blobmsg_data(cur));
}
}
vlist_flush(&ost->ports);
}
static void
ovs_apply_settings(struct ovs_state *ost, struct blob_attr **tb)
{
struct ovs_config *cfg = &ost->config;
/* defaults */
cfg->tag = 0;
cfg->base = NULL;
cfg->empty = false;
cfg->type = NULL;
cfg->options = NULL;
if (tb[OVS_ATTR_TAG] && tb[OVS_ATTR_BASE] ) {
cfg->tag = blobmsg_get_u32(tb[OVS_ATTR_TAG]);
cfg->base = blobmsg_get_string(tb[OVS_ATTR_BASE]);
}
if (tb[OVS_ATTR_EMPTY])
cfg->empty = blobmsg_get_bool(tb[OVS_ATTR_EMPTY]);
if (tb[OVS_ATTR_TYPE])
cfg->type = blobmsg_get_string(tb[OVS_ATTR_TYPE]);
if (tb[OVS_ATTR_OPTIONS])
cfg->options = blobmsg_get_string(tb[OVS_ATTR_OPTIONS]);
}
enum dev_change_type
ovs_reload(struct device *dev, struct blob_attr *attr)
{
struct blob_attr *tb_dev[__DEV_ATTR_MAX];
struct blob_attr *tb_br[__OVS_ATTR_MAX];
enum dev_change_type ret = DEV_CONFIG_APPLIED;
unsigned long diff;
struct ovs_state *ost;
BUILD_BUG_ON(sizeof(diff) < __OVS_ATTR_MAX / 8);
BUILD_BUG_ON(sizeof(diff) < __DEV_ATTR_MAX / 8);
ost = container_of(dev, struct ovs_state, dev);
blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, tb_dev,
blob_data(attr), blob_len(attr));
blobmsg_parse(ovs_attrs, __OVS_ATTR_MAX, tb_br,
blob_data(attr), blob_len(attr));
ost->ifnames = tb_br[OVS_ATTR_IFNAME];
device_init_settings(dev, tb_dev);
ovs_apply_settings(ost, tb_br);
if (ost->config_data) {
struct blob_attr *otb_dev[__DEV_ATTR_MAX];
struct blob_attr *otb_br[__OVS_ATTR_MAX];
blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, otb_dev,
blob_data(ost->config_data), blob_len(ost->config_data));
diff = 0;
uci_blob_diff(tb_dev, otb_dev, &device_attr_list, &diff);
if (diff)
ret = DEV_CONFIG_RESTART;
blobmsg_parse(ovs_attrs, __OVS_ATTR_MAX, otb_br,
blob_data(ost->config_data), blob_len(ost->config_data));
diff = 0;
uci_blob_diff(tb_br, otb_br, &ovs_attr_list, &diff);
if (diff & ~(1 << OVS_ATTR_IFNAME))
ret = DEV_CONFIG_RESTART;
ovs_config_init(dev);
}
ost->config_data = attr;
return ret;
}
static struct device *
ovs_create(const char *name, struct blob_attr *attr)
{
struct ovs_state *ost;
struct device *dev = NULL;
ost = calloc(1, sizeof(*ost));
if (!ost)
return NULL;
dev = &ost->dev;
device_init(dev, &ovs_device_type, name);
dev->config_pending = true;
ost->set_state = dev->set_state;
dev->set_state = ovs_set_state;
dev->hotplug_ops = &ovs_ops;
vlist_init(&ost->ports, avl_strcmp, ovs_port_update);
ost->ports.keep_old = true;
ovs_reload(dev, attr);
return dev;
}