forked from openvswitch/ovs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bridge.c
4213 lines (3593 loc) · 136 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, 2013, 2014 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 <errno.h>
#include <inttypes.h>
#include <stdlib.h>
#include "async-append.h"
#include "bfd.h"
#include "bitmap.h"
#include "cfm.h"
#include "connectivity.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/bond.h"
#include "ofproto/ofproto.h"
#include "poll-loop.h"
#include "seq.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);
struct iface {
/* These members are always valid.
*
* They are immutable: they never change between iface_create() and
* iface_destroy(). */
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 hmap_node ofp_port_node; /* In struct bridge's "ifaces" hmap. */
struct port *port; /* Containing port. */
char *name; /* Host network device name. */
struct netdev *netdev; /* Network device. */
ofp_port_t ofp_port; /* OpenFlow port number. */
/* These members are valid only within bridge_reconfigure(). */
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. */
/* Port mirroring. */
struct hmap mirrors; /* "struct mirror" indexed by UUID. */
/* Used during reconfiguration. */
struct shash wanted_ports;
/* 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;
/* We want to complete daemonization, fully detaching from our parent process,
* only after we have completed our initial configuration, committed our state
* to the database, and received confirmation back from the database server
* that it applied the commit. This allows our parent process to know that,
* post-detach, ephemeral fields such as datapath-id and ofport are very likely
* to have already been filled in. (It is only "very likely" rather than
* certain because there is always a slim possibility that the transaction will
* fail or that some other client has added new bridges, ports, etc. while
* ovs-vswitchd was configuring using an old configuration.)
*
* We only need to do this once for our initial configuration at startup, so
* 'initial_config_done' tracks whether we've already done it. While we are
* waiting for a response to our commit, 'daemonize_txn' tracks the transaction
* itself and is otherwise NULL. */
static bool initial_config_done;
static struct ovsdb_idl_txn *daemonize_txn;
/* Most recently processed IDL sequence number. */
static unsigned int idl_seqno;
/* Track changes to port connectivity. */
static uint64_t connectivity_seqno = LLONG_MIN;
/* 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 void add_del_bridges(const struct ovsrec_open_vswitch *);
static void bridge_run__(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_collect_wanted_ports(struct bridge *,
const unsigned long *splinter_vlans,
struct shash *wanted_ports);
static void bridge_delete_ofprotos(void);
static void bridge_delete_or_reconfigure_ports(struct bridge *);
static void bridge_del_ports(struct bridge *,
const struct shash *wanted_ports);
static void bridge_add_ports(struct bridge *,
const struct shash *wanted_ports);
static void bridge_configure_flow_miss_model(const char *opt);
static void bridge_configure_datapath_id(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_ipfix(struct bridge *);
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 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 *);
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 *, const struct ovsrec_interface *,
const struct ovsrec_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 iface *iface_from_ofp_port(const struct bridge *,
ofp_port_t ofp_port);
static void iface_set_mac(struct iface *, const uint8_t *);
static void iface_set_ofport(const struct ovsrec_interface *, ofp_port_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 ofp_port_t iface_get_requested_ofp_port(
const struct ovsrec_interface *);
static ofp_port_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, 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_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_mac_in_use);
ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_ifindex);
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_flap_count);
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_bfd_status);
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_ipfix_col_external_ids);
ovsdb_idl_omit(idl, &ovsrec_flow_sample_collector_set_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_port(target,
OVSDB_OLD_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 sockaddr_in *managers;
struct bridge *br, *next;
int sflow_bridge_number;
size_t n_managers;
COVERAGE_INC(bridge_reconfigure);
ofproto_set_flow_limit(smap_get_int(&ovs_cfg->other_config, "flow-limit",
OFPROTO_FLOW_LIMIT_DEFAULT));
ofproto_set_threads(
smap_get_int(&ovs_cfg->other_config, "n-handler-threads", 0),
smap_get_int(&ovs_cfg->other_config, "n-revalidator-threads", 0));
bridge_configure_flow_miss_model(smap_get(&ovs_cfg->other_config,
"force-miss-model"));
/* Destroy "struct bridge"s, "struct port"s, and "struct iface"s according
* to 'ovs_cfg', 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_collect_wanted_ports(br, splinter_vlans, &br->wanted_ports);
bridge_del_ports(br, &br->wanted_ports);
}
free(splinter_vlans);
/* Start pushing configuration changes down to the ofproto layer:
*
* - Delete ofprotos that are no longer configured.
*
* - Delete ports that are no longer configured.
*
* - Reconfigure existing ports to their desired configurations, or
* delete them if not possible.
*
* We have to do all the deletions before we can do any additions, because
* the ports to be added might require resources that will be freed up by
* deletions (they might especially overlap in name). */
bridge_delete_ofprotos();
HMAP_FOR_EACH (br, node, &all_bridges) {
if (br->ofproto) {
bridge_delete_or_reconfigure_ports(br);
}
}
/* Finish pushing configuration changes to the ofproto layer:
*
* - Create ofprotos that are missing.
*
* - Add ports that are missing. */
HMAP_FOR_EACH_SAFE (br, next, node, &all_bridges) {
if (!br->ofproto) {
int error;
error = ofproto_create(br->name, br->type, &br->ofproto);
if (error) {
VLOG_ERR("failed to create bridge %s: %s", br->name,
ovs_strerror(error));
shash_destroy(&br->wanted_ports);
bridge_destroy(br);
}
}
}
HMAP_FOR_EACH (br, node, &all_bridges) {
bridge_add_ports(br, &br->wanted_ports);
shash_destroy(&br->wanted_ports);
}
reconfigure_system_stats(ovs_cfg);
/* 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_set_ofport(iface->cfg, iface->ofp_port);
iface_configure_cfm(iface);
iface_configure_qos(iface, port->cfg->qos);
iface_set_mac(iface, port->cfg->fake_bridge ? br->ea : NULL);
ofproto_port_set_bfd(br->ofproto, iface->ofp_port,
&iface->cfg->bfd);
}
}
bridge_configure_mirrors(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_ipfix(br);
bridge_configure_stp(br);
bridge_configure_tables(br);
bridge_configure_dp_desc(br);
if (smap_get(&br->cfg->other_config, "flow-eviction-threshold")) {
/* XXX: Remove this warning message eventually. */
VLOG_WARN_ONCE("As of June 2013, flow-eviction-threshold has been"
" moved to the Open_vSwitch table. Ignoring its"
" setting in the bridge table.");
}
}
free(managers);
/* The ofproto-dpif provider does some final reconfiguration in its
* ->type_run() function. We have to call it before notifying the database
* client that reconfiguration is complete, otherwise there is a very
* narrow race window in which e.g. ofproto/trace will not recognize the
* new configuration (sometimes this causes unit test failures). */
bridge_run__();
}
/* Delete ofprotos which aren't configured or have the wrong type. Create
* ofprotos which don't exist but need to. */
static void
bridge_delete_ofprotos(void)
{
struct bridge *br;
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);
}
static ofp_port_t *
add_ofp_port(ofp_port_t port, ofp_port_t *ports, size_t *n, size_t *allocated)
{
if (*n >= *allocated) {
ports = x2nrealloc(ports, allocated, sizeof *ports);
}
ports[(*n)++] = port;
return ports;
}
static void
bridge_delete_or_reconfigure_ports(struct bridge *br)
{
struct ofproto_port ofproto_port;
struct ofproto_port_dump dump;
/* List of "ofp_port"s to delete. We make a list instead of deleting them
* right away because ofproto implementations aren't necessarily able to
* iterate through a changing list of ports in an entirely robust way. */
ofp_port_t *del;
size_t n, allocated;
size_t i;
del = NULL;
n = allocated = 0;
OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, br->ofproto) {
ofp_port_t requested_ofp_port;
struct iface *iface;
iface = iface_lookup(br, ofproto_port.name);
if (!iface) {
/* No such iface is configured, so we should delete this
* ofproto_port.
*
* As a corner case exception, keep the port if it's a bond fake
* interface. */
if (bridge_has_bond_fake_iface(br, ofproto_port.name)
&& !strcmp(ofproto_port.type, "internal")) {
continue;
}
goto delete;
}
if (strcmp(ofproto_port.type, iface->type)
|| netdev_set_config(iface->netdev, &iface->cfg->options)) {
/* The interface is the wrong type or can't be configured.
* Delete it. */
goto delete;
}
/* If the requested OpenFlow port for 'iface' changed, and it's not
* already the correct port, then we might want to temporarily delete
* this interface, so we can add it back again with the new OpenFlow
* port number. */
requested_ofp_port = iface_get_requested_ofp_port(iface->cfg);
if (iface->ofp_port != OFPP_LOCAL &&
requested_ofp_port != OFPP_NONE &&
requested_ofp_port != iface->ofp_port) {
ofp_port_t victim_request;
struct iface *victim;
/* Check for an existing OpenFlow port currently occupying
* 'iface''s requested port number. If there isn't one, then
* delete this port. Otherwise we need to consider further. */
victim = iface_from_ofp_port(br, requested_ofp_port);
if (!victim) {
goto delete;
}
/* 'victim' is a port currently using 'iface''s requested port
* number. Unless 'victim' specifically requested that port
* number, too, then we can delete both 'iface' and 'victim'
* temporarily. (We'll add both of them back again later with new
* OpenFlow port numbers.)
*
* If 'victim' did request port number 'requested_ofp_port', just
* like 'iface', then that's a configuration inconsistency that we
* can't resolve. We might as well let it keep its current port
* number. */
victim_request = iface_get_requested_ofp_port(victim->cfg);
if (victim_request != requested_ofp_port) {
del = add_ofp_port(victim->ofp_port, del, &n, &allocated);
iface_destroy(victim);
goto delete;
}
}
/* Keep it. */
continue;
delete:
iface_destroy(iface);
del = add_ofp_port(ofproto_port.ofp_port, del, &n, &allocated);
}
for (i = 0; i < n; i++) {
ofproto_port_del(br->ofproto, del[i]);
}
free(del);
}
static void
bridge_add_ports__(struct bridge *br, const struct shash *wanted_ports,
bool with_requested_port)
{
struct shash_node *port_node;
SHASH_FOR_EACH (port_node, wanted_ports) {
const struct ovsrec_port *port_cfg = port_node->data;
size_t i;
for (i = 0; i < port_cfg->n_interfaces; i++) {
const struct ovsrec_interface *iface_cfg = port_cfg->interfaces[i];
ofp_port_t requested_ofp_port;
requested_ofp_port = iface_get_requested_ofp_port(iface_cfg);
if ((requested_ofp_port != OFPP_NONE) == with_requested_port) {
struct iface *iface = iface_lookup(br, iface_cfg->name);
if (!iface) {
iface_create(br, iface_cfg, port_cfg);
}
}
}
}
}
static void
bridge_add_ports(struct bridge *br, const struct shash *wanted_ports)
{
/* First add interfaces that request a particular port number. */
bridge_add_ports__(br, wanted_ports, true);
/* Then add interfaces that want automatic port number assignment.
* We add these afterward to avoid accidentally taking a specifically
* requested port number. */
bridge_add_ports__(br, wanted_ports, false);
}
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;
port_configure_bond(port, &bond_settings);
} else {
s.bond = 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);
}
static void
bridge_configure_flow_miss_model(const char *opt)
{
enum ofproto_flow_miss_model model = OFPROTO_HANDLE_MISS_AUTO;
if (opt) {
if (!strcmp(opt, "with-facets")) {
model = OFPROTO_HANDLE_MISS_WITH_FACETS;
} else if (!strcmp(opt, "without-facets")) {
model = OFPROTO_HANDLE_MISS_WITHOUT_FACETS;
}
}
ofproto_set_flow_miss_model(model);
}
/* 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, ovs_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);
}