forked from openvswitch/ovs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbridge.c
3987 lines (3388 loc) · 126 KB
/
bridge.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
/* Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <config.h>
#include "bridge.h"
#include <assert.h>
#include <errno.h>
#include <inttypes.h>
#include <stdlib.h>
#include "bitmap.h"
#include "bond.h"
#include "cfm.h"
#include "coverage.h"
#include "daemon.h"
#include "dirs.h"
#include "dynamic-string.h"
#include "hash.h"
#include "hmap.h"
#include "hmapx.h"
#include "jsonrpc.h"
#include "lacp.h"
#include "list.h"
#include "mac-learning.h"
#include "meta-flow.h"
#include "netdev.h"
#include "ofp-print.h"
#include "ofp-util.h"
#include "ofpbuf.h"
#include "ofproto/ofproto.h"
#include "poll-loop.h"
#include "sha1.h"
#include "shash.h"
#include "smap.h"
#include "socket-util.h"
#include "stream.h"
#include "stream-ssl.h"
#include "sset.h"
#include "system-stats.h"
#include "timeval.h"
#include "util.h"
#include "unixctl.h"
#include "vlandev.h"
#include "lib/vswitch-idl.h"
#include "xenserver.h"
#include "vlog.h"
#include "sflow_api.h"
#include "vlan-bitmap.h"
VLOG_DEFINE_THIS_MODULE(bridge);
COVERAGE_DEFINE(bridge_reconfigure);
/* Configuration of an uninstantiated iface. */
struct if_cfg {
struct hmap_node hmap_node; /* Node in bridge's if_cfg_todo. */
const struct ovsrec_interface *cfg; /* Interface record. */
const struct ovsrec_port *parent; /* Parent port record. */
int64_t ofport; /* Requested OpenFlow port number. */
};
/* OpenFlow port slated for removal from ofproto. */
struct ofpp_garbage {
struct list list_node; /* Node in bridge's ofpp_garbage. */
uint16_t ofp_port; /* Port to be deleted. */
};
struct iface {
/* These members are always valid. */
struct list port_elem; /* Element in struct port's "ifaces" list. */
struct hmap_node name_node; /* In struct bridge's "iface_by_name" hmap. */
struct port *port; /* Containing port. */
char *name; /* Host network device name. */
/* These members are valid only after bridge_reconfigure() causes them to
* be initialized. */
struct hmap_node ofp_port_node; /* In struct bridge's "ifaces" hmap. */
int ofp_port; /* OpenFlow port number, -1 if unknown. */
struct netdev *netdev; /* Network device. */
const char *type; /* Usually same as cfg->type. */
const struct ovsrec_interface *cfg;
};
struct mirror {
struct uuid uuid; /* UUID of this "mirror" record in database. */
struct hmap_node hmap_node; /* In struct bridge's "mirrors" hmap. */
struct bridge *bridge;
char *name;
const struct ovsrec_mirror *cfg;
};
struct port {
struct hmap_node hmap_node; /* Element in struct bridge's "ports" hmap. */
struct bridge *bridge;
char *name;
const struct ovsrec_port *cfg;
/* An ordinary bridge port has 1 interface.
* A bridge port for bonding has at least 2 interfaces. */
struct list ifaces; /* List of "struct iface"s. */
};
struct bridge {
struct hmap_node node; /* In 'all_bridges'. */
char *name; /* User-specified arbitrary name. */
char *type; /* Datapath type. */
uint8_t ea[ETH_ADDR_LEN]; /* Bridge Ethernet Address. */
uint8_t default_ea[ETH_ADDR_LEN]; /* Default MAC. */
const struct ovsrec_bridge *cfg;
/* OpenFlow switch processing. */
struct ofproto *ofproto; /* OpenFlow switch. */
/* Bridge ports. */
struct hmap ports; /* "struct port"s indexed by name. */
struct hmap ifaces; /* "struct iface"s indexed by ofp_port. */
struct hmap iface_by_name; /* "struct iface"s indexed by name. */
struct list ofpp_garbage; /* "struct ofpp_garbage" slated for removal. */
struct hmap if_cfg_todo; /* "struct if_cfg"s slated for creation.
Indexed on 'cfg->name'. */
/* Port mirroring. */
struct hmap mirrors; /* "struct mirror" indexed by UUID. */
/* Synthetic local port if necessary. */
struct ovsrec_port synth_local_port;
struct ovsrec_interface synth_local_iface;
struct ovsrec_interface *synth_local_ifacep;
};
/* All bridges, indexed by name. */
static struct hmap all_bridges = HMAP_INITIALIZER(&all_bridges);
/* OVSDB IDL used to obtain configuration. */
static struct ovsdb_idl *idl;
/* Most recently processed IDL sequence number. */
static unsigned int idl_seqno;
/* Each time this timer expires, the bridge fetches interface and mirror
* statistics and pushes them into the database. */
#define IFACE_STATS_INTERVAL (5 * 1000) /* In milliseconds. */
static long long int iface_stats_timer = LLONG_MIN;
/* In some datapaths, creating and destroying OpenFlow ports can be extremely
* expensive. This can cause bridge_reconfigure() to take a long time during
* which no other work can be done. To deal with this problem, we limit port
* adds and deletions to a window of OFP_PORT_ACTION_WINDOW milliseconds per
* call to bridge_reconfigure(). If there is more work to do after the limit
* is reached, 'need_reconfigure', is flagged and it's done on the next loop.
* This allows the rest of the code to catch up on important things like
* forwarding packets. */
#define OFP_PORT_ACTION_WINDOW 10
static bool reconfiguring = false;
static void add_del_bridges(const struct ovsrec_open_vswitch *);
static void bridge_update_ofprotos(void);
static void bridge_create(const struct ovsrec_bridge *);
static void bridge_destroy(struct bridge *);
static struct bridge *bridge_lookup(const char *name);
static unixctl_cb_func bridge_unixctl_dump_flows;
static unixctl_cb_func bridge_unixctl_reconnect;
static size_t bridge_get_controllers(const struct bridge *br,
struct ovsrec_controller ***controllersp);
static void bridge_add_del_ports(struct bridge *,
const unsigned long int *splinter_vlans);
static void bridge_refresh_ofp_port(struct bridge *);
static void bridge_configure_datapath_id(struct bridge *);
static void bridge_configure_flow_eviction_threshold(struct bridge *);
static void bridge_configure_netflow(struct bridge *);
static void bridge_configure_forward_bpdu(struct bridge *);
static void bridge_configure_mac_table(struct bridge *);
static void bridge_configure_sflow(struct bridge *, int *sflow_bridge_number);
static void bridge_configure_stp(struct bridge *);
static void bridge_configure_tables(struct bridge *);
static void bridge_configure_dp_desc(struct bridge *);
static void bridge_configure_remotes(struct bridge *,
const struct sockaddr_in *managers,
size_t n_managers);
static void bridge_pick_local_hw_addr(struct bridge *,
uint8_t ea[ETH_ADDR_LEN],
struct iface **hw_addr_iface);
static uint64_t bridge_pick_datapath_id(struct bridge *,
const uint8_t bridge_ea[ETH_ADDR_LEN],
struct iface *hw_addr_iface);
static void bridge_queue_if_cfg(struct bridge *,
const struct ovsrec_interface *,
const struct ovsrec_port *);
static uint64_t dpid_from_hash(const void *, size_t nbytes);
static bool bridge_has_bond_fake_iface(const struct bridge *,
const char *name);
static bool port_is_bond_fake_iface(const struct port *);
static unixctl_cb_func qos_unixctl_show;
static struct port *port_create(struct bridge *, const struct ovsrec_port *);
static void port_del_ifaces(struct port *);
static void port_destroy(struct port *);
static struct port *port_lookup(const struct bridge *, const char *name);
static void port_configure(struct port *);
static struct lacp_settings *port_configure_lacp(struct port *,
struct lacp_settings *);
static void port_configure_bond(struct port *, struct bond_settings *,
uint32_t *bond_stable_ids);
static bool port_is_synthetic(const struct port *);
static void reconfigure_system_stats(const struct ovsrec_open_vswitch *);
static void run_system_stats(void);
static void bridge_configure_mirrors(struct bridge *);
static struct mirror *mirror_create(struct bridge *,
const struct ovsrec_mirror *);
static void mirror_destroy(struct mirror *);
static bool mirror_configure(struct mirror *);
static void mirror_refresh_stats(struct mirror *);
static void iface_configure_lacp(struct iface *, struct lacp_slave_settings *);
static bool iface_create(struct bridge *, struct if_cfg *, int ofp_port);
static bool iface_is_internal(const struct ovsrec_interface *iface,
const struct ovsrec_bridge *br);
static const char *iface_get_type(const struct ovsrec_interface *,
const struct ovsrec_bridge *);
static void iface_destroy(struct iface *);
static struct iface *iface_lookup(const struct bridge *, const char *name);
static struct iface *iface_find(const char *name);
static struct if_cfg *if_cfg_lookup(const struct bridge *, const char *name);
static struct iface *iface_from_ofp_port(const struct bridge *,
uint16_t ofp_port);
static void iface_set_mac(struct iface *);
static void iface_set_ofport(const struct ovsrec_interface *, int64_t ofport);
static void iface_clear_db_record(const struct ovsrec_interface *if_cfg);
static void iface_configure_qos(struct iface *, const struct ovsrec_qos *);
static void iface_configure_cfm(struct iface *);
static void iface_refresh_cfm_stats(struct iface *);
static void iface_refresh_stats(struct iface *);
static void iface_refresh_status(struct iface *);
static bool iface_is_synthetic(const struct iface *);
static int64_t iface_pick_ofport(const struct ovsrec_interface *);
/* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
*
* This is deprecated. It is only for compatibility with broken device drivers
* in old versions of Linux that do not properly support VLANs when VLAN
* devices are not used. When broken device drivers are no longer in
* widespread use, we will delete these interfaces. */
/* True if VLAN splinters are enabled on any interface, false otherwise.*/
static bool vlan_splinters_enabled_anywhere;
static bool vlan_splinters_is_enabled(const struct ovsrec_interface *);
static unsigned long int *collect_splinter_vlans(
const struct ovsrec_open_vswitch *);
static void configure_splinter_port(struct port *);
static void add_vlan_splinter_ports(struct bridge *,
const unsigned long int *splinter_vlans,
struct shash *ports);
static void
bridge_init_ofproto(const struct ovsrec_open_vswitch *cfg)
{
struct shash iface_hints;
static bool initialized = false;
int i;
if (initialized) {
return;
}
shash_init(&iface_hints);
if (cfg) {
for (i = 0; i < cfg->n_bridges; i++) {
const struct ovsrec_bridge *br_cfg = cfg->bridges[i];
int j;
for (j = 0; j < br_cfg->n_ports; j++) {
struct ovsrec_port *port_cfg = br_cfg->ports[j];
int k;
for (k = 0; k < port_cfg->n_interfaces; k++) {
struct ovsrec_interface *if_cfg = port_cfg->interfaces[k];
struct iface_hint *iface_hint;
iface_hint = xmalloc(sizeof *iface_hint);
iface_hint->br_name = br_cfg->name;
iface_hint->br_type = br_cfg->datapath_type;
iface_hint->ofp_port = iface_pick_ofport(if_cfg);
shash_add(&iface_hints, if_cfg->name, iface_hint);
}
}
}
}
ofproto_init(&iface_hints);
shash_destroy_free_data(&iface_hints);
initialized = true;
}
/* Public functions. */
/* Initializes the bridge module, configuring it to obtain its configuration
* from an OVSDB server accessed over 'remote', which should be a string in a
* form acceptable to ovsdb_idl_create(). */
void
bridge_init(const char *remote)
{
/* Create connection to database. */
idl = ovsdb_idl_create(remote, &ovsrec_idl_class, true);
idl_seqno = ovsdb_idl_get_seqno(idl);
ovsdb_idl_set_lock(idl, "ovs_vswitchd");
ovsdb_idl_verify_write_only(idl);
ovsdb_idl_omit_alert(idl, &ovsrec_open_vswitch_col_cur_cfg);
ovsdb_idl_omit_alert(idl, &ovsrec_open_vswitch_col_statistics);
ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_external_ids);
ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_ovs_version);
ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_db_version);
ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_system_type);
ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_system_version);
ovsdb_idl_omit_alert(idl, &ovsrec_bridge_col_datapath_id);
ovsdb_idl_omit_alert(idl, &ovsrec_bridge_col_status);
ovsdb_idl_omit(idl, &ovsrec_bridge_col_external_ids);
ovsdb_idl_omit_alert(idl, &ovsrec_port_col_status);
ovsdb_idl_omit_alert(idl, &ovsrec_port_col_statistics);
ovsdb_idl_omit(idl, &ovsrec_port_col_external_ids);
ovsdb_idl_omit(idl, &ovsrec_port_col_fake_bridge);
ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_admin_state);
ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_duplex);
ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_link_speed);
ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_link_state);
ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_link_resets);
ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_mtu);
ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_ofport);
ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_statistics);
ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_status);
ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_cfm_fault);
ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_cfm_fault_status);
ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_cfm_remote_mpids);
ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_cfm_health);
ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_cfm_remote_opstate);
ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_lacp_current);
ovsdb_idl_omit(idl, &ovsrec_interface_col_external_ids);
ovsdb_idl_omit_alert(idl, &ovsrec_controller_col_is_connected);
ovsdb_idl_omit_alert(idl, &ovsrec_controller_col_role);
ovsdb_idl_omit_alert(idl, &ovsrec_controller_col_status);
ovsdb_idl_omit(idl, &ovsrec_controller_col_external_ids);
ovsdb_idl_omit(idl, &ovsrec_qos_col_external_ids);
ovsdb_idl_omit(idl, &ovsrec_queue_col_external_ids);
ovsdb_idl_omit(idl, &ovsrec_mirror_col_external_ids);
ovsdb_idl_omit_alert(idl, &ovsrec_mirror_col_statistics);
ovsdb_idl_omit(idl, &ovsrec_netflow_col_external_ids);
ovsdb_idl_omit(idl, &ovsrec_sflow_col_external_ids);
ovsdb_idl_omit(idl, &ovsrec_manager_col_external_ids);
ovsdb_idl_omit(idl, &ovsrec_manager_col_inactivity_probe);
ovsdb_idl_omit(idl, &ovsrec_manager_col_is_connected);
ovsdb_idl_omit(idl, &ovsrec_manager_col_max_backoff);
ovsdb_idl_omit(idl, &ovsrec_manager_col_status);
ovsdb_idl_omit(idl, &ovsrec_ssl_col_external_ids);
/* Register unixctl commands. */
unixctl_command_register("qos/show", "interface", 1, 1,
qos_unixctl_show, NULL);
unixctl_command_register("bridge/dump-flows", "bridge", 1, 1,
bridge_unixctl_dump_flows, NULL);
unixctl_command_register("bridge/reconnect", "[bridge]", 0, 1,
bridge_unixctl_reconnect, NULL);
lacp_init();
bond_init();
cfm_init();
stp_init();
}
void
bridge_exit(void)
{
struct bridge *br, *next_br;
HMAP_FOR_EACH_SAFE (br, next_br, node, &all_bridges) {
bridge_destroy(br);
}
ovsdb_idl_destroy(idl);
}
/* Looks at the list of managers in 'ovs_cfg' and extracts their remote IP
* addresses and ports into '*managersp' and '*n_managersp'. The caller is
* responsible for freeing '*managersp' (with free()).
*
* You may be asking yourself "why does ovs-vswitchd care?", because
* ovsdb-server is responsible for connecting to the managers, and ovs-vswitchd
* should not be and in fact is not directly involved in that. But
* ovs-vswitchd needs to make sure that ovsdb-server can reach the managers, so
* it has to tell in-band control where the managers are to enable that.
* (Thus, only managers connected in-band are collected.)
*/
static void
collect_in_band_managers(const struct ovsrec_open_vswitch *ovs_cfg,
struct sockaddr_in **managersp, size_t *n_managersp)
{
struct sockaddr_in *managers = NULL;
size_t n_managers = 0;
struct sset targets;
size_t i;
/* Collect all of the potential targets from the "targets" columns of the
* rows pointed to by "manager_options", excluding any that are
* out-of-band. */
sset_init(&targets);
for (i = 0; i < ovs_cfg->n_manager_options; i++) {
struct ovsrec_manager *m = ovs_cfg->manager_options[i];
if (m->connection_mode && !strcmp(m->connection_mode, "out-of-band")) {
sset_find_and_delete(&targets, m->target);
} else {
sset_add(&targets, m->target);
}
}
/* Now extract the targets' IP addresses. */
if (!sset_is_empty(&targets)) {
const char *target;
managers = xmalloc(sset_count(&targets) * sizeof *managers);
SSET_FOR_EACH (target, &targets) {
struct sockaddr_in *sin = &managers[n_managers];
if (stream_parse_target_with_default_ports(target,
JSONRPC_TCP_PORT,
JSONRPC_SSL_PORT,
sin)) {
n_managers++;
}
}
}
sset_destroy(&targets);
*managersp = managers;
*n_managersp = n_managers;
}
static void
bridge_reconfigure(const struct ovsrec_open_vswitch *ovs_cfg)
{
unsigned long int *splinter_vlans;
struct bridge *br;
COVERAGE_INC(bridge_reconfigure);
assert(!reconfiguring);
reconfiguring = true;
/* Destroy "struct bridge"s, "struct port"s, and "struct iface"s according
* to 'ovs_cfg' while update the "if_cfg_queue", with only very minimal
* configuration otherwise.
*
* This is mostly an update to bridge data structures. Nothing is pushed
* down to ofproto or lower layers. */
add_del_bridges(ovs_cfg);
splinter_vlans = collect_splinter_vlans(ovs_cfg);
HMAP_FOR_EACH (br, node, &all_bridges) {
bridge_add_del_ports(br, splinter_vlans);
}
free(splinter_vlans);
/* Delete datapaths that are no longer configured, and create ones which
* don't exist but should. */
bridge_update_ofprotos();
/* Make sure each "struct iface" has a correct ofp_port in its ofproto. */
HMAP_FOR_EACH (br, node, &all_bridges) {
bridge_refresh_ofp_port(br);
}
/* Clear database records for "if_cfg"s which haven't been instantiated. */
HMAP_FOR_EACH (br, node, &all_bridges) {
struct if_cfg *if_cfg;
HMAP_FOR_EACH (if_cfg, hmap_node, &br->if_cfg_todo) {
iface_clear_db_record(if_cfg->cfg);
}
}
reconfigure_system_stats(ovs_cfg);
}
static bool
bridge_reconfigure_ofp(void)
{
long long int deadline;
struct bridge *br;
time_refresh();
deadline = time_msec() + OFP_PORT_ACTION_WINDOW;
/* The kernel will reject any attempt to add a given port to a datapath if
* that port already belongs to a different datapath, so we must do all
* port deletions before any port additions. */
HMAP_FOR_EACH (br, node, &all_bridges) {
struct ofpp_garbage *garbage, *next;
LIST_FOR_EACH_SAFE (garbage, next, list_node, &br->ofpp_garbage) {
/* It's a bit dangerous to call bridge_run_fast() here as ofproto's
* internal datastructures may not be consistent. Eventually, when
* port additions and deletions are cheaper, these calls should be
* removed. */
bridge_run_fast();
ofproto_port_del(br->ofproto, garbage->ofp_port);
list_remove(&garbage->list_node);
free(garbage);
time_refresh();
if (time_msec() >= deadline) {
return false;
}
bridge_run_fast();
}
}
HMAP_FOR_EACH (br, node, &all_bridges) {
struct if_cfg *if_cfg, *next;
HMAP_FOR_EACH_SAFE (if_cfg, next, hmap_node, &br->if_cfg_todo) {
iface_create(br, if_cfg, -1);
time_refresh();
if (time_msec() >= deadline) {
return false;
}
}
}
return true;
}
static bool
bridge_reconfigure_continue(const struct ovsrec_open_vswitch *ovs_cfg)
{
struct sockaddr_in *managers;
int sflow_bridge_number;
size_t n_managers;
struct bridge *br;
bool done;
assert(reconfiguring);
done = bridge_reconfigure_ofp();
/* Complete the configuration. */
sflow_bridge_number = 0;
collect_in_band_managers(ovs_cfg, &managers, &n_managers);
HMAP_FOR_EACH (br, node, &all_bridges) {
struct port *port;
/* We need the datapath ID early to allow LACP ports to use it as the
* default system ID. */
bridge_configure_datapath_id(br);
HMAP_FOR_EACH (port, hmap_node, &br->ports) {
struct iface *iface;
port_configure(port);
LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
iface_configure_cfm(iface);
iface_configure_qos(iface, port->cfg->qos);
iface_set_mac(iface);
}
}
bridge_configure_mirrors(br);
bridge_configure_flow_eviction_threshold(br);
bridge_configure_forward_bpdu(br);
bridge_configure_mac_table(br);
bridge_configure_remotes(br, managers, n_managers);
bridge_configure_netflow(br);
bridge_configure_sflow(br, &sflow_bridge_number);
bridge_configure_stp(br);
bridge_configure_tables(br);
bridge_configure_dp_desc(br);
}
free(managers);
if (done) {
/* ovs-vswitchd has completed initialization, so allow the process that
* forked us to exit successfully. */
daemonize_complete();
reconfiguring = false;
VLOG_INFO("%s (Open vSwitch) %s", program_name, VERSION);
}
return done;
}
/* Delete ofprotos which aren't configured or have the wrong type. Create
* ofprotos which don't exist but need to. */
static void
bridge_update_ofprotos(void)
{
struct bridge *br, *next;
struct sset names;
struct sset types;
const char *type;
/* Delete ofprotos with no bridge or with the wrong type. */
sset_init(&names);
sset_init(&types);
ofproto_enumerate_types(&types);
SSET_FOR_EACH (type, &types) {
const char *name;
ofproto_enumerate_names(type, &names);
SSET_FOR_EACH (name, &names) {
br = bridge_lookup(name);
if (!br || strcmp(type, br->type)) {
ofproto_delete(name, type);
}
}
}
sset_destroy(&names);
sset_destroy(&types);
/* Add ofprotos for bridges which don't have one yet. */
HMAP_FOR_EACH_SAFE (br, next, node, &all_bridges) {
struct bridge *br2;
int error;
if (br->ofproto) {
continue;
}
/* Remove ports from any datapath with the same name as 'br'. If we
* don't do this, creating 'br''s ofproto will fail because a port with
* the same name as its local port already exists. */
HMAP_FOR_EACH (br2, node, &all_bridges) {
struct ofproto_port ofproto_port;
if (!br2->ofproto) {
continue;
}
if (!ofproto_port_query_by_name(br2->ofproto, br->name,
&ofproto_port)) {
error = ofproto_port_del(br2->ofproto, ofproto_port.ofp_port);
if (error) {
VLOG_ERR("failed to delete port %s: %s", ofproto_port.name,
strerror(error));
}
ofproto_port_destroy(&ofproto_port);
}
}
error = ofproto_create(br->name, br->type, &br->ofproto);
if (error) {
VLOG_ERR("failed to create bridge %s: %s", br->name,
strerror(error));
bridge_destroy(br);
}
}
}
static void
port_configure(struct port *port)
{
const struct ovsrec_port *cfg = port->cfg;
struct bond_settings bond_settings;
struct lacp_settings lacp_settings;
struct ofproto_bundle_settings s;
struct iface *iface;
if (cfg->vlan_mode && !strcmp(cfg->vlan_mode, "splinter")) {
configure_splinter_port(port);
return;
}
/* Get name. */
s.name = port->name;
/* Get slaves. */
s.n_slaves = 0;
s.slaves = xmalloc(list_size(&port->ifaces) * sizeof *s.slaves);
LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
s.slaves[s.n_slaves++] = iface->ofp_port;
}
/* Get VLAN tag. */
s.vlan = -1;
if (cfg->tag && *cfg->tag >= 0 && *cfg->tag <= 4095) {
s.vlan = *cfg->tag;
}
/* Get VLAN trunks. */
s.trunks = NULL;
if (cfg->n_trunks) {
s.trunks = vlan_bitmap_from_array(cfg->trunks, cfg->n_trunks);
}
/* Get VLAN mode. */
if (cfg->vlan_mode) {
if (!strcmp(cfg->vlan_mode, "access")) {
s.vlan_mode = PORT_VLAN_ACCESS;
} else if (!strcmp(cfg->vlan_mode, "trunk")) {
s.vlan_mode = PORT_VLAN_TRUNK;
} else if (!strcmp(cfg->vlan_mode, "native-tagged")) {
s.vlan_mode = PORT_VLAN_NATIVE_TAGGED;
} else if (!strcmp(cfg->vlan_mode, "native-untagged")) {
s.vlan_mode = PORT_VLAN_NATIVE_UNTAGGED;
} else {
/* This "can't happen" because ovsdb-server should prevent it. */
VLOG_ERR("unknown VLAN mode %s", cfg->vlan_mode);
s.vlan_mode = PORT_VLAN_TRUNK;
}
} else {
if (s.vlan >= 0) {
s.vlan_mode = PORT_VLAN_ACCESS;
if (cfg->n_trunks) {
VLOG_ERR("port %s: ignoring trunks in favor of implicit vlan",
port->name);
}
} else {
s.vlan_mode = PORT_VLAN_TRUNK;
}
}
s.use_priority_tags = smap_get_bool(&cfg->other_config, "priority-tags",
false);
/* Get LACP settings. */
s.lacp = port_configure_lacp(port, &lacp_settings);
if (s.lacp) {
size_t i = 0;
s.lacp_slaves = xmalloc(s.n_slaves * sizeof *s.lacp_slaves);
LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
iface_configure_lacp(iface, &s.lacp_slaves[i++]);
}
} else {
s.lacp_slaves = NULL;
}
/* Get bond settings. */
if (s.n_slaves > 1) {
s.bond = &bond_settings;
s.bond_stable_ids = xmalloc(s.n_slaves * sizeof *s.bond_stable_ids);
port_configure_bond(port, &bond_settings, s.bond_stable_ids);
} else {
s.bond = NULL;
s.bond_stable_ids = NULL;
LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
netdev_set_miimon_interval(iface->netdev, 0);
}
}
/* Register. */
ofproto_bundle_register(port->bridge->ofproto, port, &s);
/* Clean up. */
free(s.slaves);
free(s.trunks);
free(s.lacp_slaves);
free(s.bond_stable_ids);
}
/* Pick local port hardware address and datapath ID for 'br'. */
static void
bridge_configure_datapath_id(struct bridge *br)
{
uint8_t ea[ETH_ADDR_LEN];
uint64_t dpid;
struct iface *local_iface;
struct iface *hw_addr_iface;
char *dpid_string;
bridge_pick_local_hw_addr(br, ea, &hw_addr_iface);
local_iface = iface_from_ofp_port(br, OFPP_LOCAL);
if (local_iface) {
int error = netdev_set_etheraddr(local_iface->netdev, ea);
if (error) {
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
VLOG_ERR_RL(&rl, "bridge %s: failed to set bridge "
"Ethernet address: %s",
br->name, strerror(error));
}
}
memcpy(br->ea, ea, ETH_ADDR_LEN);
dpid = bridge_pick_datapath_id(br, ea, hw_addr_iface);
if (dpid != ofproto_get_datapath_id(br->ofproto)) {
VLOG_INFO("bridge %s: using datapath ID %016"PRIx64, br->name, dpid);
ofproto_set_datapath_id(br->ofproto, dpid);
}
dpid_string = xasprintf("%016"PRIx64, dpid);
ovsrec_bridge_set_datapath_id(br->cfg, dpid_string);
free(dpid_string);
}
/* Returns a bitmap of "enum ofputil_protocol"s that are allowed for use with
* 'br'. */
static uint32_t
bridge_get_allowed_versions(struct bridge *br)
{
if (!br->cfg->n_protocols)
return 0;
return ofputil_versions_from_strings(br->cfg->protocols,
br->cfg->n_protocols);
}
/* Set NetFlow configuration on 'br'. */
static void
bridge_configure_netflow(struct bridge *br)
{
struct ovsrec_netflow *cfg = br->cfg->netflow;
struct netflow_options opts;
if (!cfg) {
ofproto_set_netflow(br->ofproto, NULL);
return;
}
memset(&opts, 0, sizeof opts);
/* Get default NetFlow configuration from datapath.
* Apply overrides from 'cfg'. */
ofproto_get_netflow_ids(br->ofproto, &opts.engine_type, &opts.engine_id);
if (cfg->engine_type) {
opts.engine_type = *cfg->engine_type;
}
if (cfg->engine_id) {
opts.engine_id = *cfg->engine_id;
}
/* Configure active timeout interval. */
opts.active_timeout = cfg->active_timeout;
if (!opts.active_timeout) {
opts.active_timeout = -1;
} else if (opts.active_timeout < 0) {
VLOG_WARN("bridge %s: active timeout interval set to negative "
"value, using default instead (%d seconds)", br->name,
NF_ACTIVE_TIMEOUT_DEFAULT);
opts.active_timeout = -1;
}
/* Add engine ID to interface number to disambiguate bridgs? */
opts.add_id_to_iface = cfg->add_id_to_interface;
if (opts.add_id_to_iface) {
if (opts.engine_id > 0x7f) {
VLOG_WARN("bridge %s: NetFlow port mangling may conflict with "
"another vswitch, choose an engine id less than 128",
br->name);
}
if (hmap_count(&br->ports) > 508) {
VLOG_WARN("bridge %s: NetFlow port mangling will conflict with "
"another port when more than 508 ports are used",
br->name);
}
}
/* Collectors. */
sset_init(&opts.collectors);
sset_add_array(&opts.collectors, cfg->targets, cfg->n_targets);
/* Configure. */
if (ofproto_set_netflow(br->ofproto, &opts)) {
VLOG_ERR("bridge %s: problem setting netflow collectors", br->name);
}
sset_destroy(&opts.collectors);
}
/* Set sFlow configuration on 'br'. */
static void
bridge_configure_sflow(struct bridge *br, int *sflow_bridge_number)
{
const struct ovsrec_sflow *cfg = br->cfg->sflow;
struct ovsrec_controller **controllers;
struct ofproto_sflow_options oso;
size_t n_controllers;
size_t i;
if (!cfg) {
ofproto_set_sflow(br->ofproto, NULL);
return;
}
memset(&oso, 0, sizeof oso);
sset_init(&oso.targets);
sset_add_array(&oso.targets, cfg->targets, cfg->n_targets);
oso.sampling_rate = SFL_DEFAULT_SAMPLING_RATE;
if (cfg->sampling) {
oso.sampling_rate = *cfg->sampling;
}
oso.polling_interval = SFL_DEFAULT_POLLING_INTERVAL;
if (cfg->polling) {
oso.polling_interval = *cfg->polling;
}
oso.header_len = SFL_DEFAULT_HEADER_SIZE;
if (cfg->header) {
oso.header_len = *cfg->header;
}
oso.sub_id = (*sflow_bridge_number)++;
oso.agent_device = cfg->agent;
oso.control_ip = NULL;
n_controllers = bridge_get_controllers(br, &controllers);
for (i = 0; i < n_controllers; i++) {
if (controllers[i]->local_ip) {
oso.control_ip = controllers[i]->local_ip;
break;
}
}
ofproto_set_sflow(br->ofproto, &oso);
sset_destroy(&oso.targets);
}
static void
port_configure_stp(const struct ofproto *ofproto, struct port *port,
struct ofproto_port_stp_settings *port_s,
int *port_num_counter, unsigned long *port_num_bitmap)
{
const char *config_str;
struct iface *iface;
if (!smap_get_bool(&port->cfg->other_config, "stp-enable", true)) {
port_s->enable = false;
return;
} else {
port_s->enable = true;
}
/* STP over bonds is not supported. */
if (!list_is_singleton(&port->ifaces)) {
VLOG_ERR("port %s: cannot enable STP on bonds, disabling",
port->name);
port_s->enable = false;
return;
}
iface = CONTAINER_OF(list_front(&port->ifaces), struct iface, port_elem);
/* Internal ports shouldn't participate in spanning tree, so
* skip them. */
if (!strcmp(iface->type, "internal")) {
VLOG_DBG("port %s: disable STP on internal ports", port->name);
port_s->enable = false;
return;
}
/* STP on mirror output ports is not supported. */
if (ofproto_is_mirror_output_bundle(ofproto, port)) {
VLOG_DBG("port %s: disable STP on mirror ports", port->name);
port_s->enable = false;
return;
}
config_str = smap_get(&port->cfg->other_config, "stp-port-num");
if (config_str) {
unsigned long int port_num = strtoul(config_str, NULL, 0);
int port_idx = port_num - 1;
if (port_num < 1 || port_num > STP_MAX_PORTS) {
VLOG_ERR("port %s: invalid stp-port-num", port->name);
port_s->enable = false;
return;
}
if (bitmap_is_set(port_num_bitmap, port_idx)) {
VLOG_ERR("port %s: duplicate stp-port-num %lu, disabling",
port->name, port_num);
port_s->enable = false;
return;
}