-
Notifications
You must be signed in to change notification settings - Fork 26
/
extdev.c
1375 lines (1101 loc) · 33.4 KB
/
extdev.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
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* netifd - network interface daemon
* Copyright (C) 2015 Arne Kappen <[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.
*
*
* extdev - external device handler interface
*
* This allows to integrate external daemons that configure network devices
* with netifd. At startup, netifd generates device handler stubs from
* descriptions in /lib/netifd/extdev-config and adds them to the list of
* device handlers. A device handler is an instance of struct device_type
* The descriptions are in JSON format and specify
* - names of the device type and of the external device handler on ubus,
* - whether the device is bridge-like,
* - a prefix for device names,
* - the UCI config options for devices of this type, and
* - the format of calls to dump() and info()
* These device handlers stubs act as relays forwarding calls against the
* device handler interface to the external daemon.
*/
#include <libubox/blobmsg.h>
#include <libubox/list.h>
#include <libubus.h>
#include <assert.h>
#include "netifd.h"
#include "handler.h"
#include "device.h"
#include "ubus.h"
#include "extdev.h"
#include "interface.h"
#include "system.h"
static struct blob_buf b;
static int confdir_fd = -1;
struct extdev_type {
struct device_type handler;
const char *name;
uint32_t peer_id;
struct ubus_subscriber ubus_sub;
bool subscribed;
struct ubus_event_handler obj_wait;
struct uci_blob_param_list *config_params;
char *config_strbuf;
struct uci_blob_param_list *info_params;
char *info_strbuf;
struct uci_blob_param_list *stats_params;
char *stats_strbuf;
};
struct extdev_device {
struct device dev;
struct extdev_type *etype;
const char *dep_name;
struct uloop_timeout retry;
};
struct extdev_bridge {
struct extdev_device edev;
device_state_cb set_state;
struct blob_attr *config;
bool empty;
struct blob_attr *ifnames;
bool active;
bool force_active;
struct uloop_timeout retry;
struct vlist_tree members;
int n_present;
int n_failed;
};
struct extdev_bridge_member {
struct vlist_node node;
struct extdev_bridge *parent_br;
struct device_user dev_usr;
bool present;
char *name;
};
static void __bridge_config_init(struct extdev_bridge *ebr);
static enum dev_change_type __bridge_reload(struct extdev_bridge *ebr, struct blob_attr *config);
enum {
METHOD_CREATE,
METHOD_CONFIG_INIT,
METHOD_RELOAD,
METHOD_DUMP_INFO,
METHOD_DUMP_STATS,
METHOD_CHECK_STATE,
METHOD_FREE,
METHOD_HOTPLUG_PREPARE,
METHOD_HOTPLUG_ADD,
METHOD_HOTPLUG_REMOVE,
__METHODS_MAX
};
static const char *__extdev_methods[__METHODS_MAX] = {
[METHOD_CREATE] = "create",
[METHOD_CONFIG_INIT] = "config_init",
[METHOD_RELOAD] = "reload",
[METHOD_DUMP_INFO] = "dump_info",
[METHOD_DUMP_STATS] = "dump_stats",
[METHOD_CHECK_STATE] = "check_state",
[METHOD_FREE] = "free",
[METHOD_HOTPLUG_PREPARE] = "prepare",
[METHOD_HOTPLUG_ADD] = "add",
[METHOD_HOTPLUG_REMOVE] = "remove",
};
static inline int
netifd_extdev_create(struct extdev_device *edev, struct blob_attr *msg)
{
D(DEVICE, "create %s '%s' at external device handler", edev->dev.type->name,
edev->dev.ifname);
return netifd_extdev_invoke(edev->etype->peer_id, __extdev_methods[METHOD_CREATE], msg,
NULL, NULL);
}
static inline int
netifd_extdev_reload(struct extdev_device *edev, struct blob_attr *msg)
{
D(DEVICE, "reload %s '%s' at external device handler", edev->dev.type->name,
edev->dev.ifname);
return netifd_extdev_invoke(edev->etype->peer_id, __extdev_methods[METHOD_RELOAD], msg,
NULL, NULL);
}
static inline int
netifd_extdev_free(struct extdev_device *edev, struct blob_attr *msg)
{
D(DEVICE, "delete %s '%s' with external device handler", edev->dev.type->name,
edev->dev.ifname);
return netifd_extdev_invoke(edev->etype->peer_id, __extdev_methods[METHOD_FREE], msg,
NULL, NULL);
}
static inline int
netifd_extdev_prepare(struct extdev_bridge *ebr, struct blob_attr *msg)
{
D(DEVICE, "prepare %s bridge '%s' at external device handler", ebr->edev.dev.type->name,
ebr->edev.dev.ifname);
return netifd_extdev_invoke(ebr->edev.etype->peer_id,
__extdev_methods[METHOD_HOTPLUG_PREPARE], msg, NULL, NULL);
}
static inline int
netifd_extdev_add(struct extdev_bridge *ebr, struct blob_attr *msg)
{
D(DEVICE, "add a member to %s bridge '%s' at external device handler",
ebr->edev.dev.type->name, ebr->edev.dev.ifname);
return netifd_extdev_invoke(ebr->edev.etype->peer_id,
__extdev_methods[METHOD_HOTPLUG_ADD], msg,NULL, NULL);
}
static inline int
netifd_extdev_remove(struct extdev_bridge *ebr, struct blob_attr *msg)
{
D(DEVICE, "remove a member from %s bridge '%s' at external device handler",
ebr->edev.dev.type->name, ebr->edev.dev.ifname);
return netifd_extdev_invoke(ebr->edev.etype->peer_id,
__extdev_methods[METHOD_HOTPLUG_REMOVE], msg, NULL, NULL);
}
static inline void
extdev_invocation_error(int error, const char *method, const char *devname)
{
netifd_log_message(L_CRIT, "'%s' failed for '%s': %s\n",
method, devname, ubus_strerror(error));
}
static struct ubus_method extdev_ubus_obj_methods[] = {};
static struct ubus_object_type extdev_ubus_object_type =
UBUS_OBJECT_TYPE("netifd_extdev", extdev_ubus_obj_methods);
static int
extdev_lookup_id(struct extdev_type *etype)
{
int ret = UBUS_STATUS_UNKNOWN_ERROR;
if (!etype || !etype->name)
goto error;
ret = ubus_lookup_id(ubus_ctx, etype->name, &etype->peer_id);
if (ret)
goto error;
return 0;
error:
netifd_log_message(L_CRIT, "Could not find '%s' ubus ID: %s\n",
etype->name, ubus_strerror(ret));
return ret;
}
static int
extdev_ext_ubus_obj_wait(struct ubus_event_handler *h)
{
return ubus_register_event_handler(ubus_ctx, h, "ubus.object.add");
}
static int
extdev_subscribe(struct extdev_type *etype)
{
int ret;
ret = extdev_lookup_id(etype);
if (ret) {
etype->subscribed = false;
return ret;
}
ret = ubus_subscribe(ubus_ctx, &etype->ubus_sub, etype->peer_id);
if (ret) {
etype->subscribed = false;
extdev_ext_ubus_obj_wait(&etype->obj_wait);
} else {
netifd_log_message(L_NOTICE, "subscribed to external device handler '%s'\n",
etype->name);
etype->subscribed = true;
}
return ret;
}
static void
extdev_wait_ev_cb(struct ubus_context *ctx, struct ubus_event_handler *ev_handler,
const char *type, struct blob_attr *msg)
{
static const struct blobmsg_policy wait_policy = {
"path", BLOBMSG_TYPE_STRING
};
struct blob_attr *attr;
const char *path;
struct extdev_type *etype;
etype = container_of(ev_handler, struct extdev_type, obj_wait);
if (strcmp(type, "ubus.object.add"))
return;
blobmsg_parse(&wait_policy, 1, &attr, blob_data(msg), blob_len(msg));
if (!attr)
return;
path = blobmsg_data(attr);
if (strcmp(etype->name, path))
return;
extdev_subscribe(etype);
}
static int
extdev_bridge_disable_interface(struct extdev_bridge *ebr)
{
int ret;
if (!ebr->active)
return 0;
blob_buf_init(&b, 0);
blobmsg_add_string(&b, "name", ebr->edev.dev.ifname);
ret = netifd_extdev_free(&ebr->edev, b.head);
if (ret && ret != UBUS_STATUS_NOT_FOUND)
goto error;
ebr->active = false;
return 0;
error:
extdev_invocation_error(ret, __extdev_methods[METHOD_FREE], ebr->edev.dev.ifname);
return ret;
}
static int
extdev_bridge_enable_interface(struct extdev_bridge *ebr)
{
int ret;
if (ebr->active)
return 0;
ret = netifd_extdev_create(&ebr->edev, ebr->config);
if (ret)
goto error;
ebr->active = true;
return 0;
error:
extdev_invocation_error(ret, __extdev_methods[METHOD_CREATE], ebr->edev.dev.ifname);
return ret;
}
static int
extdev_bridge_enable_member(struct extdev_bridge_member *ubm)
{
int ret;
struct extdev_bridge *ebr = ubm->parent_br;
D(DEVICE, "%s enable member %s", ebr->edev.dev.ifname, ubm->name);
if (!ubm->present)
return 0;
ret = extdev_bridge_enable_interface(ebr);
if (ret)
goto error;
ret = device_claim(&ubm->dev_usr);
if (ret < 0)
goto error;
blob_buf_init(&b, 0);
blobmsg_add_string(&b, "bridge", ebr->edev.dev.ifname);
blobmsg_add_string(&b, "member", ubm->dev_usr.dev->ifname);
/* use hotplug add as addif equivalent. Maybe we need a dedicated ubus
* method on the external handler for this sort of operation. */
ret = netifd_extdev_add(ebr, b.head);
if (ret) {
extdev_invocation_error(ret, __extdev_methods[METHOD_HOTPLUG_ADD],
ubm->dev_usr.dev->ifname);
goto error;
}
device_set_present(&ebr->edev.dev, true);
device_broadcast_event(&ebr->edev.dev, DEV_EVENT_TOPO_CHANGE);
return 0;
error:
D(DEVICE, "%s: failed to enable member '%s'", ebr->edev.dev.ifname, ubm->name);
ebr->n_failed++;
ubm->present = false;
ebr->n_present--;
return ret;
}
static int
extdev_bridge_disable_member(struct extdev_bridge_member *ubm)
{
int ret;
struct extdev_bridge *ebr = ubm->parent_br;
if (!ubm->present)
return 0;
D(DEVICE, "%s disable member %s", ubm->parent_br->edev.dev.ifname, ubm->name);
blob_buf_init(&b, 0);
blobmsg_add_string(&b, "bridge", ebr->edev.dev.ifname);
blobmsg_add_string(&b, "member", ubm->dev_usr.dev->ifname);
/* use hotplug remove as delif equivalent. Maybe we need a dedicated
* ubus method on the external handler for this sort of operation. */
ret = netifd_extdev_remove(ebr, b.head);
/* continue in case of NOT FOUND since we're trying to remove anyway */
if (ret && ret != UBUS_STATUS_NOT_FOUND)
goto error;
device_release(&ubm->dev_usr);
device_broadcast_event(&ebr->edev.dev, DEV_EVENT_TOPO_CHANGE);
return 0;
error:
extdev_invocation_error(ret, __extdev_methods[METHOD_HOTPLUG_REMOVE],
ubm->dev_usr.dev->ifname);
return ret;
}
static int
extdev_bridge_set_down(struct extdev_bridge *ebr)
{
D(DEVICE, "set %s bridge %s down", ebr->edev.dev.type->name, ebr->edev.dev.ifname);
struct extdev_bridge_member *ubm;
ebr->set_state(&ebr->edev.dev, false);
vlist_for_each_element(&ebr->members, ubm, node)
extdev_bridge_disable_member(ubm);
extdev_bridge_disable_interface(ebr);
return 0;
}
static void
extdev_bridge_check_retry(struct extdev_bridge *ebr)
{
if (!ebr->n_failed)
return;
uloop_timeout_set(&ebr->retry, 200);
}
static int
extdev_bridge_set_up(struct extdev_bridge *ebr)
{
D(DEVICE, "set %s bridge %s up", ebr->edev.dev.type->name, ebr->edev.dev.ifname);
struct extdev_bridge_member *ubm;
int ret;
if (!ebr->n_present) {
if (!ebr->force_active)
return -ENOENT;
ret = extdev_bridge_enable_interface(ebr);
if (ret)
return ret;
}
ebr->n_failed = 0;
vlist_for_each_element(&ebr->members, ubm, node)
extdev_bridge_enable_member(ubm);
extdev_bridge_check_retry(ebr);
if (!ebr->force_active && !ebr->n_present) {
extdev_bridge_disable_interface(ebr);
device_set_present(&ebr->edev.dev, false);
return -ENOENT;
}
return 0;
}
static int
extdev_bridge_set_state(struct device *dev, bool up)
{
struct extdev_bridge *ebr;
if (!dev->type->bridge_capability)
return -1;
ebr = container_of(dev, struct extdev_bridge, edev.dev);
if (up)
return extdev_bridge_set_up(ebr);
else
return extdev_bridge_set_down(ebr);
}
static void
extdev_bridge_remove_member(struct extdev_bridge_member *member)
{
struct extdev_bridge *ebr = member->parent_br;
if (!member->present)
return;
if (ebr->edev.dev.active)
extdev_bridge_disable_member(member);
member->present = false;
ebr->n_present--;
if (ebr->empty)
return;
ebr->force_active = false;
if (ebr->n_present == 0)
device_set_present(&ebr->edev.dev, false);
}
static void
extdev_bridge_member_cb(struct device_user *usr, enum device_event event)
{
int ret;
struct extdev_bridge_member *ubm;
struct extdev_bridge *ebr;
ubm = container_of(usr, struct extdev_bridge_member, dev_usr);
ebr = ubm->parent_br;
switch (event) {
case DEV_EVENT_ADD:
assert(!ubm->present);
ubm->present = true;
ebr->n_present++;
/* if this member is the first one that is brought up,
* create the bridge at the external device handler */
if (ebr->n_present == 1) {
ret = netifd_extdev_create(&ebr->edev, ebr->config);
if (ret)
goto error;
ebr->active = true;
ret = ebr->set_state(&ebr->edev.dev, true);
if (ret < 0)
extdev_bridge_set_down(ebr);
device_set_present(&ebr->edev.dev, true);
}
extdev_bridge_enable_member(ubm);
break;
case DEV_EVENT_REMOVE:
if (usr->hotplug) {
vlist_delete(&ebr->members, &ubm->node);
return;
}
if (ubm->present)
extdev_bridge_remove_member(ubm);
break;
default:
break;
}
return;
error:
netifd_log_message(L_CRIT, "Failed to create %s bridge %s: %s\n",
ebr->edev.dev.type->name, ebr->edev.dev.ifname, ubus_strerror(ret));
ubm->present = false;
ebr->n_present--;
}
static void
__bridge_enable_members(struct extdev_bridge *ebr)
{
struct extdev_bridge_member *cur;
ebr->n_failed = 0;
vlist_for_each_element(&ebr->members, cur, node) {
if (cur->present)
continue;
if (!cur->dev_usr.dev->present)
continue;
cur->present = true;
ebr->n_present++;
extdev_bridge_enable_member(cur);
}
}
static void
extdev_bridge_retry_enable_members(struct uloop_timeout *timeout)
{
struct extdev_bridge *ebr = container_of(timeout, struct extdev_bridge, retry);
D(DEVICE, "%s retry enable members", ebr->edev.dev.ifname);
__bridge_enable_members(ebr);
}
static struct extdev_bridge_member *
extdev_bridge_create_member(struct extdev_bridge *ebr, struct device *dev)
{
struct extdev_bridge_member *ubm;
char *name;
ubm = calloc_a(sizeof(*ubm), &name, strlen(dev->ifname) + 1);
if (!ubm)
return NULL;
ubm->parent_br = ebr;
ubm->name = name;
strcpy(name, dev->ifname);
ubm->dev_usr.dev = dev;
ubm->dev_usr.cb = extdev_bridge_member_cb;
vlist_add(&ebr->members, &ubm->node, ubm->name);
/* Need to look up the bridge member again as the above
* created pointer will be freed in case the bridge member
* already existed */
ubm = vlist_find(&ebr->members, dev->ifname, ubm, node);
if (!ubm)
return NULL;
return ubm;
}
static void
extdev_bridge_add_member(struct extdev_bridge *ebr, const char *name)
{
D(DEVICE, "%s add member %s", ebr->edev.dev.ifname, name);
struct device *dev;
dev = device_get(name, 1);
if (!dev)
return;
extdev_bridge_create_member(ebr, dev);
}
/* TODO: how to handle vlan arg? */
static int
extdev_hotplug_add(struct device *ebr_dev, struct device *ebm_dev, struct blob_attr *vlan)
{
D(DEVICE, "%s hotplug add member %s", ebr_dev->ifname, ebm_dev->ifname);
struct extdev_bridge *ebr;
struct extdev_bridge_member *ubm;
if (!ebr_dev->type->bridge_capability)
return UBUS_STATUS_NOT_SUPPORTED;
ebr = container_of(ebr_dev, struct extdev_bridge, edev.dev);
if (!ebr->edev.etype->subscribed)
return UBUS_STATUS_NOT_FOUND;
ubm = extdev_bridge_create_member(ebr, ebm_dev);
if (!ubm)
return UBUS_STATUS_UNKNOWN_ERROR;
device_broadcast_event(&ebr->edev.dev, DEV_EVENT_TOPO_CHANGE);
return 0;
}
static int
extdev_hotplug_remove(struct device *dev, struct device *member, struct blob_attr *vlan)
{
struct extdev_bridge *ebr;
struct extdev_bridge_member *ubm;
if (!dev->type->bridge_capability)
return UBUS_STATUS_NOT_SUPPORTED;
ebr = container_of(dev, struct extdev_bridge, edev.dev);
if (!ebr->edev.etype->subscribed)
return UBUS_STATUS_NOT_FOUND;
ubm = vlist_find(&ebr->members, member->ifname, ubm, node);
if (!ubm)
return UBUS_STATUS_NOT_FOUND;
vlist_delete(&ebr->members, &ubm->node);
extdev_bridge_remove_member(ubm);
return 0;
}
static int
extdev_hotplug_prepare(struct device *dev, struct device **bridge_dev)
{
struct extdev_bridge *ebr;
int ret;
if (!dev->type->bridge_capability)
return UBUS_STATUS_NOT_SUPPORTED;
if (bridge_dev)
*bridge_dev = dev;
ebr = container_of(dev, struct extdev_bridge, edev.dev);
blob_buf_init(&b, 0);
blobmsg_add_string(&b, "name", dev->ifname);
ret = netifd_extdev_prepare(ebr, b.head);
if (ret)
goto error;
ebr->force_active = true;
device_set_present(&ebr->edev.dev, true);
return 0;
error:
extdev_invocation_error(ret, __extdev_methods[METHOD_HOTPLUG_PREPARE], dev->ifname);
return ret;
}
static void
extdev_bridge_free_member(struct extdev_bridge_member *ubm)
{
struct device *dev = ubm->dev_usr.dev;
extdev_bridge_remove_member(ubm);
device_remove_user(&ubm->dev_usr);
if (dev->present) {
device_set_present(dev, false);
device_set_present(dev, true);
}
free(ubm);
}
static void
extdev_bridge_member_update(struct vlist_tree *tree, struct vlist_node *node_new,
struct vlist_node *node_old)
{
struct extdev_bridge_member *ubm;
struct device *dev;
if (node_new) {
ubm = container_of(node_new, struct extdev_bridge_member, node);
if (node_old) {
free(ubm);
return;
}
dev = ubm->dev_usr.dev;
ubm->dev_usr.dev = NULL;
device_add_user(&ubm->dev_usr, dev);
}
if (node_old) {
ubm = container_of(node_old, struct extdev_bridge_member, node);
extdev_bridge_free_member(ubm);
}
}
static void
bridge_dependency_retry(struct uloop_timeout *timeout)
{
struct extdev_bridge *ebr;
ebr = container_of(timeout, struct extdev_bridge, edev.retry);
__bridge_reload(ebr, NULL);
}
static void
__buf_add_all(struct blob_attr *attr)
{
struct blob_attr *cur;
size_t rem;
blobmsg_for_each_attr(cur, attr, rem)
blobmsg_add_field(&b, blobmsg_type(cur), blobmsg_name(cur), blobmsg_data(cur),
blobmsg_data_len(cur));
}
enum {
BRIDGE_EMPTY,
BRIDGE_IFNAMES,
BRIDGE_DEPENDS_ON,
__BRIDGE_MAX
};
static const struct blobmsg_policy brpol[__BRIDGE_MAX] = {
[BRIDGE_EMPTY] = { "empty", BLOBMSG_TYPE_BOOL },
[BRIDGE_IFNAMES] = { "ifname", BLOBMSG_TYPE_ARRAY },
[BRIDGE_DEPENDS_ON] = { "depends_on", BLOBMSG_TYPE_STRING },
};
static enum dev_change_type
__do_bridge_reload(struct extdev_bridge *ebr, struct blob_attr *config)
{
void *cfg_table;
int ret;
blob_buf_init(&b, 0);
cfg_table = blobmsg_open_table(&b, "old");
__buf_add_all(ebr->config);
blobmsg_close_table(&b, cfg_table);
cfg_table = blobmsg_open_table(&b, "new");
__buf_add_all(config);
blobmsg_close_table(&b, cfg_table);
ret = netifd_extdev_reload(&ebr->edev, b.head);
if (ret) {
netifd_log_message(L_WARNING, "%s config reload failed: %s\n",
ebr->edev.dev.ifname, ubus_strerror(ret));
return DEV_CONFIG_RECREATE;
} else {
return DEV_CONFIG_RESTART;
}
}
static enum dev_change_type
__bridge_reload(struct extdev_bridge *ebr, struct blob_attr *config)
{
int n_params = ebr->edev.dev.type->config_params->n_params;
struct blob_attr *tb[__BRIDGE_MAX];
const struct uci_blob_param_list *config_params;
const struct blobmsg_policy *pol;
struct blob_attr *old_tb[n_params], *brtb[n_params];
enum dev_change_type change = DEV_CONFIG_APPLIED;
struct device *dev;
unsigned long diff = 0;
if (config) {
config = blob_memdup(config);
blobmsg_parse(brpol, __BRIDGE_MAX, tb, blobmsg_data(config), blobmsg_len(config));
ebr->edev.dep_name = blobmsg_get_string(tb[BRIDGE_DEPENDS_ON]);
if (tb[BRIDGE_EMPTY] && blobmsg_get_bool(tb[BRIDGE_EMPTY]))
ebr->empty = true;
if (ebr->config) {
config_params = ebr->edev.dev.type->config_params;
pol = config_params->params;
blobmsg_parse(pol, n_params, old_tb, blobmsg_data(ebr->config),
blobmsg_len(ebr->config));
blobmsg_parse(pol, n_params, brtb, blobmsg_data(config), blobmsg_len
(config));
diff = 0;
uci_blob_diff(brtb, old_tb, config_params, &diff);
if (diff) {
if (diff & ~(1 << BRIDGE_IFNAMES)) {
change = DEV_CONFIG_RESTART;
} else {
change = __do_bridge_reload(ebr, config);
}
free(ebr->config);
}
}
ebr->ifnames = tb[BRIDGE_IFNAMES];
ebr->config = config;
}
if (ebr->edev.dep_name) {
dev = device_get(ebr->edev.dep_name, 0);
if (!(dev && dev->current_config)) {
D(DEVICE, "%s: cannot yet init config since dependency '%s' is not ready",
ebr->edev.dev.ifname, ebr->edev.dep_name);
ebr->edev.retry.cb = bridge_dependency_retry;
uloop_timeout_set(&ebr->edev.retry, 200);
return DEV_CONFIG_RESTART;
}
}
__bridge_config_init(ebr);
ebr->edev.dev.config_pending = false;
uloop_timeout_cancel(&ebr->edev.retry);
return change;
}
static enum dev_change_type
__reload(struct extdev_device *edev, struct blob_attr *config)
{
unsigned long diff = 0;
struct uci_blob_param_list *params;
params = edev->etype->config_params;
struct blob_attr *tb[params->n_params];
struct blob_attr *old_tb[params->n_params];
blobmsg_parse(params->params, params->n_params, tb, blobmsg_data(config),
blobmsg_len(config));
blobmsg_parse(params->params, params->n_params, old_tb, blobmsg_data(edev->dev.config),
blobmsg_len(edev->dev.config));
uci_blob_diff(tb, old_tb, edev->etype->config_params, &diff);
if (!diff)
return DEV_CONFIG_NO_CHANGE;
// TODO: make reload ubus call with old and new config
device_set_present(&edev->dev, false);
device_set_present(&edev->dev, true);
return DEV_CONFIG_APPLIED;
}
static enum dev_change_type
extdev_reload(struct device *dev, struct blob_attr *config)
{
struct extdev_type *etype;
struct extdev_device *edev;
struct extdev_bridge *ebr;
etype = container_of(dev->type, struct extdev_type, handler);
if (!etype->subscribed)
return DEV_CONFIG_NO_CHANGE;
edev = container_of(dev, struct extdev_device, dev);
if (dev->type->bridge_capability) {
ebr = container_of(edev, struct extdev_bridge, edev);
return __bridge_reload(ebr, config);
} else {
return __reload(edev, config);
}
}
static struct device*
__create(const char *name, struct device_type *type, struct blob_attr *config)
{
struct extdev_device *edev;
struct extdev_type *etype;
int ret;
etype = container_of(type, struct extdev_type, handler);
edev = calloc(1, sizeof(struct extdev_device));
if (!edev)
return NULL;
ret = device_init(&edev->dev, type, name);
if (ret)
goto error;
edev->etype = etype;
ret = netifd_extdev_create(edev, config);
if (ret)
goto inv_error;
edev->dev.config_pending = false;
return &edev->dev;
inv_error:
extdev_invocation_error(ret, __extdev_methods[METHOD_CREATE], name);
error:
free(edev->dev.config);
device_cleanup(&edev->dev);
free(edev);
netifd_log_message(L_WARNING, "Failed to create %s %s\n", type->name, name);
return NULL;
}
static const struct device_hotplug_ops extdev_hotplug_ops = {
.prepare = extdev_hotplug_prepare,
.add = extdev_hotplug_add,
.del = extdev_hotplug_remove
};
static struct device*
__bridge_create(const char *name, struct device_type *devtype, struct blob_attr *config)
{
struct extdev_bridge *ebr;
ebr = calloc(1, sizeof(*ebr));
if (!ebr)
return NULL;
device_init(&ebr->edev.dev, devtype, name);
ebr->edev.dev.config_pending = true;
ebr->retry.cb = extdev_bridge_retry_enable_members;
ebr->edev.etype = container_of(devtype, struct extdev_type, handler);
ebr->set_state = ebr->edev.dev.set_state;
ebr->edev.dev.set_state = extdev_bridge_set_state;
ebr->edev.dev.hotplug_ops = &extdev_hotplug_ops;
vlist_init(&ebr->members, avl_strcmp, extdev_bridge_member_update);
ebr->members.keep_old = true;
__bridge_reload(ebr, config);
return &ebr->edev.dev;
}
/* Device creation process:
* For bridges without dependencies:
* 1) The bridge state is initialized in netifd. Devices for the members are
* created and added to the members vlist by config_init automatically.
* 2) When the first bridge member device is brought up in
* extdev_bridge_enable_member the 'create' call to the external device
* handler is issued.
* 3) After successful device creation the bridge is marked "present" and a
* new attempt at adding the member is made.
* For bridges with dependencies:
* 1) The bridge state is initialized in netifd. If a dependency is expressed
* via the 'depends_on' UCI option and the dependency is not ready (i.e. it
* does not exist or config_pending == true) the call to
* __bridge_config_init() is postponed and a retry timer is started. Retries
* happen until the dependency is ready. Then, __bridge_config_init() gets
* called and the process continues as with bridges without dependencies
* For regular devices:
* 1) The device structure is created in netifd.
* 2) config_init is called automatically which issues the 'create' call to the
* external device handler.