forked from openvswitch/ovs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbridge.c
More file actions
4502 lines (3922 loc) · 144 KB
/
Copy pathbridge.c
File metadata and controls
4502 lines (3922 loc) · 144 KB
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 Nicira Networks
*
* 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 "byte-order.h"
#include <assert.h>
#include <errno.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <inttypes.h>
#include <sys/socket.h>
#include <net/if.h>
#include <openflow/openflow.h>
#include <signal.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include "bitmap.h"
#include "cfm.h"
#include "classifier.h"
#include "coverage.h"
#include "dirs.h"
#include "dpif.h"
#include "dynamic-string.h"
#include "flow.h"
#include "hash.h"
#include "hmap.h"
#include "jsonrpc.h"
#include "list.h"
#include "mac-learning.h"
#include "netdev.h"
#include "netlink.h"
#include "odp-util.h"
#include "ofp-print.h"
#include "ofpbuf.h"
#include "ofproto/netflow.h"
#include "ofproto/ofproto.h"
#include "ovsdb-data.h"
#include "packets.h"
#include "poll-loop.h"
#include "proc-net-compat.h"
#include "process.h"
#include "sha1.h"
#include "shash.h"
#include "socket-util.h"
#include "stream-ssl.h"
#include "svec.h"
#include "system-stats.h"
#include "timeval.h"
#include "util.h"
#include "unixctl.h"
#include "vconn.h"
#include "vswitchd/vswitch-idl.h"
#include "xenserver.h"
#include "vlog.h"
#include "sflow_api.h"
VLOG_DEFINE_THIS_MODULE(bridge);
COVERAGE_DEFINE(bridge_flush);
COVERAGE_DEFINE(bridge_process_flow);
COVERAGE_DEFINE(bridge_reconfigure);
struct dst {
uint16_t vlan;
uint16_t dp_ifidx;
};
struct dst_set {
struct dst builtin[32];
struct dst *dsts;
size_t n, allocated;
};
static void dst_set_init(struct dst_set *);
static void dst_set_add(struct dst_set *, const struct dst *);
static void dst_set_free(struct dst_set *);
struct iface {
/* These members are always valid. */
struct port *port; /* Containing port. */
size_t port_ifidx; /* Index within containing port. */
char *name; /* Host network device name. */
tag_type tag; /* Tag associated with this interface. */
long long delay_expires; /* Time after which 'enabled' may change. */
/* These members are valid only after bridge_reconfigure() causes them to
* be initialized. */
struct hmap_node dp_ifidx_node; /* In struct bridge's "ifaces" hmap. */
int dp_ifidx; /* Index within kernel datapath. */
struct netdev *netdev; /* Network device. */
bool enabled; /* May be chosen for flows? */
const char *type; /* Usually same as cfg->type. */
struct cfm *cfm; /* Connectivity Fault Management */
const struct ovsrec_interface *cfg;
};
#define BOND_MASK 0xff
struct bond_entry {
int iface_idx; /* Index of assigned iface, or -1 if none. */
uint64_t tx_bytes; /* Count of bytes recently transmitted. */
tag_type iface_tag; /* Tag associated with iface_idx. */
};
#define MAX_MIRRORS 32
typedef uint32_t mirror_mask_t;
#define MIRROR_MASK_C(X) UINT32_C(X)
BUILD_ASSERT_DECL(sizeof(mirror_mask_t) * CHAR_BIT >= MAX_MIRRORS);
struct mirror {
struct bridge *bridge;
size_t idx;
char *name;
struct uuid uuid; /* UUID of this "mirror" record in database. */
/* Selection criteria. */
struct shash src_ports; /* Name is port name; data is always NULL. */
struct shash dst_ports; /* Name is port name; data is always NULL. */
int *vlans;
size_t n_vlans;
/* Output. */
struct port *out_port;
int out_vlan;
};
#define FLOOD_PORT ((struct port *) 1) /* The 'flood' output port. */
struct port {
struct bridge *bridge;
size_t port_idx;
int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
* NULL if all VLANs are trunked. */
const struct ovsrec_port *cfg;
char *name;
/* An ordinary bridge port has 1 interface.
* A bridge port for bonding has at least 2 interfaces. */
struct iface **ifaces;
size_t n_ifaces, allocated_ifaces;
/* Bonding info. */
struct bond_entry *bond_hash; /* An array of (BOND_MASK + 1) elements. */
int active_iface; /* Ifidx on which bcasts accepted, or -1. */
tag_type active_iface_tag; /* Tag for bcast flows. */
tag_type no_ifaces_tag; /* Tag for flows when all ifaces disabled. */
int updelay, downdelay; /* Delay before iface goes up/down, in ms. */
bool bond_compat_is_stale; /* Need to call port_update_bond_compat()? */
bool bond_fake_iface; /* Fake a bond interface for legacy compat? */
long long int bond_next_fake_iface_update; /* Time of next update. */
int bond_rebalance_interval; /* Interval between rebalances, in ms. */
long long int bond_next_rebalance; /* Next rebalancing time. */
struct netdev_monitor *monitor; /* Tracks carrier up/down status. */
/* Port mirroring info. */
mirror_mask_t src_mirrors; /* Mirrors triggered when packet received. */
mirror_mask_t dst_mirrors; /* Mirrors triggered when packet sent. */
bool is_mirror_output_port; /* Does port mirroring send frames here? */
};
struct bridge {
struct list node; /* Node in global list of bridges. */
char *name; /* User-specified arbitrary name. */
struct mac_learning *ml; /* MAC learning table. */
uint8_t default_ea[ETH_ADDR_LEN]; /* Default MAC. */
const struct ovsrec_bridge *cfg;
/* OpenFlow switch processing. */
struct ofproto *ofproto; /* OpenFlow switch. */
/* Kernel datapath information. */
struct dpif *dpif; /* Datapath. */
struct hmap ifaces; /* Contains "struct iface"s. */
/* Bridge ports. */
struct port **ports;
size_t n_ports, allocated_ports;
struct shash iface_by_name; /* "struct iface"s indexed by name. */
struct shash port_by_name; /* "struct port"s indexed by name. */
/* Bonding. */
bool has_bonded_ports;
/* Flow tracking. */
bool flush;
/* Port mirroring. */
struct mirror *mirrors[MAX_MIRRORS];
};
/* List of all bridges. */
static struct list all_bridges = LIST_INITIALIZER(&all_bridges);
/* OVSDB IDL used to obtain configuration. */
static struct ovsdb_idl *idl;
/* Each time this timer expires, the bridge fetches systems and interface
* statistics and pushes them into the database. */
#define STATS_INTERVAL (5 * 1000) /* In milliseconds. */
static long long int stats_timer = LLONG_MIN;
static struct bridge *bridge_create(const struct ovsrec_bridge *br_cfg);
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 int bridge_run_one(struct bridge *);
static size_t bridge_get_controllers(const struct bridge *br,
struct ovsrec_controller ***controllersp);
static void bridge_reconfigure_one(struct bridge *);
static void bridge_reconfigure_remotes(struct bridge *,
const struct sockaddr_in *managers,
size_t n_managers);
static void bridge_get_all_ifaces(const struct bridge *, struct shash *ifaces);
static void bridge_fetch_dp_ifaces(struct bridge *);
static void bridge_flush(struct bridge *);
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 struct iface *bridge_get_local_iface(struct bridge *);
static uint64_t dpid_from_hash(const void *, size_t nbytes);
static unixctl_cb_func bridge_unixctl_fdb_show;
static void bond_init(void);
static void bond_run(struct bridge *);
static void bond_wait(struct bridge *);
static void bond_rebalance_port(struct port *);
static void bond_send_learning_packets(struct port *);
static void bond_enable_slave(struct iface *iface, bool enable);
static struct port *port_create(struct bridge *, const char *name);
static void port_reconfigure(struct port *, const struct ovsrec_port *);
static void port_del_ifaces(struct port *, const struct ovsrec_port *);
static void port_destroy(struct port *);
static struct port *port_lookup(const struct bridge *, const char *name);
static struct iface *port_lookup_iface(const struct port *, const char *name);
static struct port *port_from_dp_ifidx(const struct bridge *,
uint16_t dp_ifidx);
static void port_update_bond_compat(struct port *);
static void port_update_vlan_compat(struct port *);
static void port_update_bonding(struct port *);
static void mirror_create(struct bridge *, struct ovsrec_mirror *);
static void mirror_destroy(struct mirror *);
static void mirror_reconfigure(struct bridge *);
static void mirror_reconfigure_one(struct mirror *, struct ovsrec_mirror *);
static bool vlan_is_mirrored(const struct mirror *, int vlan);
static struct iface *iface_create(struct port *port,
const struct ovsrec_interface *if_cfg);
static void iface_destroy(struct iface *);
static struct iface *iface_lookup(const struct bridge *, const char *name);
static struct iface *iface_from_dp_ifidx(const struct bridge *,
uint16_t dp_ifidx);
static void iface_set_mac(struct iface *);
static void iface_set_ofport(const struct ovsrec_interface *, int64_t ofport);
static void iface_update_qos(struct iface *, const struct ovsrec_qos *);
static void iface_update_cfm(struct iface *);
static void iface_refresh_cfm_stats(struct iface *iface);
static void iface_send_packet(struct iface *, struct ofpbuf *packet);
static void shash_from_ovs_idl_map(char **keys, char **values, size_t n,
struct shash *);
/* Hooks into ofproto processing. */
static struct ofhooks bridge_ofhooks;
/* 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);
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_bridge_col_external_ids);
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_ofport);
ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_statistics);
ovsdb_idl_omit(idl, &ovsrec_interface_col_external_ids);
/* Register unixctl commands. */
unixctl_command_register("fdb/show", bridge_unixctl_fdb_show, NULL);
unixctl_command_register("bridge/dump-flows", bridge_unixctl_dump_flows,
NULL);
unixctl_command_register("bridge/reconnect", bridge_unixctl_reconnect,
NULL);
bond_init();
}
void
bridge_exit(void)
{
struct bridge *br, *next_br;
LIST_FOR_EACH_SAFE (br, next_br, node, &all_bridges) {
bridge_destroy(br);
}
ovsdb_idl_destroy(idl);
}
/* Performs configuration that is only necessary once at ovs-vswitchd startup,
* but for which the ovs-vswitchd configuration 'cfg' is required. */
static void
bridge_configure_once(const struct ovsrec_open_vswitch *cfg)
{
static bool already_configured_once;
struct svec bridge_names;
struct svec dpif_names, dpif_types;
size_t i;
/* Only do this once per ovs-vswitchd run. */
if (already_configured_once) {
return;
}
already_configured_once = true;
stats_timer = time_msec() + STATS_INTERVAL;
/* Get all the configured bridges' names from 'cfg' into 'bridge_names'. */
svec_init(&bridge_names);
for (i = 0; i < cfg->n_bridges; i++) {
svec_add(&bridge_names, cfg->bridges[i]->name);
}
svec_sort(&bridge_names);
/* Iterate over all system dpifs and delete any of them that do not appear
* in 'cfg'. */
svec_init(&dpif_names);
svec_init(&dpif_types);
dp_enumerate_types(&dpif_types);
for (i = 0; i < dpif_types.n; i++) {
struct dpif *dpif;
int retval;
size_t j;
dp_enumerate_names(dpif_types.names[i], &dpif_names);
/* For each dpif... */
for (j = 0; j < dpif_names.n; j++) {
retval = dpif_open(dpif_names.names[j], dpif_types.names[i], &dpif);
if (!retval) {
struct svec all_names;
size_t k;
/* ...check whether any of its names is in 'bridge_names'. */
svec_init(&all_names);
dpif_get_all_names(dpif, &all_names);
for (k = 0; k < all_names.n; k++) {
if (svec_contains(&bridge_names, all_names.names[k])) {
goto found;
}
}
/* No. Delete the dpif. */
dpif_delete(dpif);
found:
svec_destroy(&all_names);
dpif_close(dpif);
}
}
}
svec_destroy(&bridge_names);
svec_destroy(&dpif_names);
svec_destroy(&dpif_types);
}
/* Callback for iterate_and_prune_ifaces(). */
static bool
check_iface(struct bridge *br, struct iface *iface, void *aux OVS_UNUSED)
{
if (!iface->netdev) {
/* We already reported a related error, don't bother duplicating it. */
return false;
}
if (iface->dp_ifidx < 0) {
VLOG_ERR("%s interface not in %s, dropping",
iface->name, dpif_name(br->dpif));
return false;
}
VLOG_DBG("%s has interface %s on port %d", dpif_name(br->dpif),
iface->name, iface->dp_ifidx);
return true;
}
/* Callback for iterate_and_prune_ifaces(). */
static bool
set_iface_properties(struct bridge *br OVS_UNUSED, struct iface *iface,
void *aux OVS_UNUSED)
{
/* Set policing attributes. */
netdev_set_policing(iface->netdev,
iface->cfg->ingress_policing_rate,
iface->cfg->ingress_policing_burst);
/* Set MAC address of internal interfaces other than the local
* interface. */
if (iface->dp_ifidx != ODPP_LOCAL && !strcmp(iface->type, "internal")) {
iface_set_mac(iface);
}
return true;
}
/* Calls 'cb' for each interfaces in 'br', passing along the 'aux' argument.
* Deletes from 'br' all the interfaces for which 'cb' returns false, and then
* deletes from 'br' any ports that no longer have any interfaces. */
static void
iterate_and_prune_ifaces(struct bridge *br,
bool (*cb)(struct bridge *, struct iface *,
void *aux),
void *aux)
{
size_t i, j;
for (i = 0; i < br->n_ports; ) {
struct port *port = br->ports[i];
for (j = 0; j < port->n_ifaces; ) {
struct iface *iface = port->ifaces[j];
if (cb(br, iface, aux)) {
j++;
} else {
iface_set_ofport(iface->cfg, -1);
iface_destroy(iface);
}
}
if (port->n_ifaces) {
i++;
} else {
VLOG_ERR("%s port has no interfaces, dropping", port->name);
port_destroy(port);
}
}
}
/* 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 shash targets;
size_t i;
/* Collect all of the potential targets, as the union of the "managers"
* column and the "targets" columns of the rows pointed to by
* "manager_options", excluding any that are out-of-band. */
shash_init(&targets);
for (i = 0; i < ovs_cfg->n_managers; i++) {
shash_add_once(&targets, ovs_cfg->managers[i], NULL);
}
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")) {
shash_find_and_delete(&targets, m->target);
} else {
shash_add_once(&targets, m->target, NULL);
}
}
/* Now extract the targets' IP addresses. */
if (!shash_is_empty(&targets)) {
struct shash_node *node;
managers = xmalloc(shash_count(&targets) * sizeof *managers);
SHASH_FOR_EACH (node, &targets) {
const char *target = node->name;
struct sockaddr_in *sin = &managers[n_managers];
if ((!strncmp(target, "tcp:", 4)
&& inet_parse_active(target + 4, JSONRPC_TCP_PORT, sin)) ||
(!strncmp(target, "ssl:", 4)
&& inet_parse_active(target + 4, JSONRPC_SSL_PORT, sin))) {
n_managers++;
}
}
}
shash_destroy(&targets);
*managersp = managers;
*n_managersp = n_managers;
}
static void
bridge_reconfigure(const struct ovsrec_open_vswitch *ovs_cfg)
{
struct shash old_br, new_br;
struct shash_node *node;
struct bridge *br, *next;
struct sockaddr_in *managers;
size_t n_managers;
size_t i;
int sflow_bridge_number;
COVERAGE_INC(bridge_reconfigure);
collect_in_band_managers(ovs_cfg, &managers, &n_managers);
/* Collect old and new bridges. */
shash_init(&old_br);
shash_init(&new_br);
LIST_FOR_EACH (br, node, &all_bridges) {
shash_add(&old_br, br->name, br);
}
for (i = 0; i < ovs_cfg->n_bridges; i++) {
const struct ovsrec_bridge *br_cfg = ovs_cfg->bridges[i];
if (!shash_add_once(&new_br, br_cfg->name, br_cfg)) {
VLOG_WARN("more than one bridge named %s", br_cfg->name);
}
}
/* Get rid of deleted bridges and add new bridges. */
LIST_FOR_EACH_SAFE (br, next, node, &all_bridges) {
struct ovsrec_bridge *br_cfg = shash_find_data(&new_br, br->name);
if (br_cfg) {
br->cfg = br_cfg;
} else {
bridge_destroy(br);
}
}
SHASH_FOR_EACH (node, &new_br) {
const char *br_name = node->name;
const struct ovsrec_bridge *br_cfg = node->data;
br = shash_find_data(&old_br, br_name);
if (br) {
/* If the bridge datapath type has changed, we need to tear it
* down and recreate. */
if (strcmp(br->cfg->datapath_type, br_cfg->datapath_type)) {
bridge_destroy(br);
bridge_create(br_cfg);
}
} else {
bridge_create(br_cfg);
}
}
shash_destroy(&old_br);
shash_destroy(&new_br);
/* Reconfigure all bridges. */
LIST_FOR_EACH (br, node, &all_bridges) {
bridge_reconfigure_one(br);
}
/* Add and delete ports on all datapaths.
*
* 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. */
LIST_FOR_EACH (br, node, &all_bridges) {
struct odp_port *dpif_ports;
size_t n_dpif_ports;
struct shash want_ifaces;
dpif_port_list(br->dpif, &dpif_ports, &n_dpif_ports);
bridge_get_all_ifaces(br, &want_ifaces);
for (i = 0; i < n_dpif_ports; i++) {
const struct odp_port *p = &dpif_ports[i];
if (!shash_find(&want_ifaces, p->devname)
&& strcmp(p->devname, br->name)) {
int retval = dpif_port_del(br->dpif, p->port);
if (retval) {
VLOG_ERR("failed to remove %s interface from %s: %s",
p->devname, dpif_name(br->dpif),
strerror(retval));
}
}
}
shash_destroy(&want_ifaces);
free(dpif_ports);
}
LIST_FOR_EACH (br, node, &all_bridges) {
struct odp_port *dpif_ports;
size_t n_dpif_ports;
struct shash cur_ifaces, want_ifaces;
/* Get the set of interfaces currently in this datapath. */
dpif_port_list(br->dpif, &dpif_ports, &n_dpif_ports);
shash_init(&cur_ifaces);
for (i = 0; i < n_dpif_ports; i++) {
const char *name = dpif_ports[i].devname;
shash_add_once(&cur_ifaces, name, &dpif_ports[i]);
}
/* Get the set of interfaces we want on this datapath. */
bridge_get_all_ifaces(br, &want_ifaces);
hmap_clear(&br->ifaces);
SHASH_FOR_EACH (node, &want_ifaces) {
const char *if_name = node->name;
struct iface *iface = node->data;
struct odp_port *dpif_port = shash_find_data(&cur_ifaces, if_name);
const char *type = iface ? iface->type : "internal";
int error;
/* If we have a port or a netdev already, and it's not the type we
* want, then delete the port (if any) and close the netdev (if
* any). */
if ((dpif_port && strcmp(dpif_port->type, type))
|| (iface && iface->netdev
&& strcmp(type, netdev_get_type(iface->netdev)))) {
if (dpif_port) {
error = ofproto_port_del(br->ofproto, dpif_port->port);
if (error) {
continue;
}
dpif_port = NULL;
}
if (iface) {
netdev_close(iface->netdev);
iface->netdev = NULL;
}
}
/* If the port doesn't exist or we don't have the netdev open,
* we need to do more work. */
if (!dpif_port || (iface && !iface->netdev)) {
struct netdev_options options;
struct netdev *netdev;
struct shash args;
/* First open the network device. */
options.name = if_name;
options.type = type;
options.args = &args;
options.ethertype = NETDEV_ETH_TYPE_NONE;
shash_init(&args);
if (iface) {
shash_from_ovs_idl_map(iface->cfg->key_options,
iface->cfg->value_options,
iface->cfg->n_options, &args);
}
error = netdev_open(&options, &netdev);
shash_destroy(&args);
if (error) {
VLOG_WARN("could not open network device %s (%s)",
if_name, strerror(error));
continue;
}
/* Then add the port if we haven't already. */
if (!dpif_port) {
error = dpif_port_add(br->dpif, netdev, NULL);
if (error) {
netdev_close(netdev);
if (error == EFBIG) {
VLOG_ERR("ran out of valid port numbers on %s",
dpif_name(br->dpif));
break;
} else {
VLOG_ERR("failed to add %s interface to %s: %s",
if_name, dpif_name(br->dpif),
strerror(error));
continue;
}
}
}
/* Update 'iface'. */
if (iface) {
iface->netdev = netdev;
iface->enabled = netdev_get_carrier(iface->netdev);
}
} else if (iface && iface->netdev) {
struct shash args;
shash_init(&args);
shash_from_ovs_idl_map(iface->cfg->key_options,
iface->cfg->value_options,
iface->cfg->n_options, &args);
netdev_reconfigure(iface->netdev, &args);
shash_destroy(&args);
}
}
free(dpif_ports);
shash_destroy(&cur_ifaces);
shash_destroy(&want_ifaces);
}
sflow_bridge_number = 0;
LIST_FOR_EACH (br, node, &all_bridges) {
uint8_t ea[8];
uint64_t dpid;
struct iface *local_iface;
struct iface *hw_addr_iface;
char *dpid_string;
bridge_fetch_dp_ifaces(br);
iterate_and_prune_ifaces(br, check_iface, NULL);
/* Pick local port hardware address, datapath ID. */
bridge_pick_local_hw_addr(br, ea, &hw_addr_iface);
local_iface = bridge_get_local_iface(br);
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));
}
}
dpid = bridge_pick_datapath_id(br, ea, hw_addr_iface);
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);
/* Set NetFlow configuration on this bridge. */
if (br->cfg->netflow) {
struct ovsrec_netflow *nf_cfg = br->cfg->netflow;
struct netflow_options opts;
memset(&opts, 0, sizeof opts);
dpif_get_netflow_ids(br->dpif, &opts.engine_type, &opts.engine_id);
if (nf_cfg->engine_type) {
opts.engine_type = *nf_cfg->engine_type;
}
if (nf_cfg->engine_id) {
opts.engine_id = *nf_cfg->engine_id;
}
opts.active_timeout = nf_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;
}
opts.add_id_to_iface = nf_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 (br->n_ports > 508) {
VLOG_WARN("bridge %s: netflow port mangling will conflict "
"with another port when more than 508 ports are "
"used", br->name);
}
}
opts.collectors.n = nf_cfg->n_targets;
opts.collectors.names = nf_cfg->targets;
if (ofproto_set_netflow(br->ofproto, &opts)) {
VLOG_ERR("bridge %s: problem setting netflow collectors",
br->name);
}
} else {
ofproto_set_netflow(br->ofproto, NULL);
}
/* Set sFlow configuration on this bridge. */
if (br->cfg->sflow) {
const struct ovsrec_sflow *sflow_cfg = br->cfg->sflow;
struct ovsrec_controller **controllers;
struct ofproto_sflow_options oso;
size_t n_controllers;
memset(&oso, 0, sizeof oso);
oso.targets.n = sflow_cfg->n_targets;
oso.targets.names = sflow_cfg->targets;
oso.sampling_rate = SFL_DEFAULT_SAMPLING_RATE;
if (sflow_cfg->sampling) {
oso.sampling_rate = *sflow_cfg->sampling;
}
oso.polling_interval = SFL_DEFAULT_POLLING_INTERVAL;
if (sflow_cfg->polling) {
oso.polling_interval = *sflow_cfg->polling;
}
oso.header_len = SFL_DEFAULT_HEADER_SIZE;
if (sflow_cfg->header) {
oso.header_len = *sflow_cfg->header;
}
oso.sub_id = sflow_bridge_number++;
oso.agent_device = sflow_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);
/* Do not destroy oso.targets because it is owned by sflow_cfg. */
} else {
ofproto_set_sflow(br->ofproto, NULL);
}
/* Update the controller and related settings. It would be more
* straightforward to call this from bridge_reconfigure_one(), but we
* can't do it there for two reasons. First, and most importantly, at
* that point we don't know the dp_ifidx of any interfaces that have
* been added to the bridge (because we haven't actually added them to
* the datapath). Second, at that point we haven't set the datapath ID
* yet; when a controller is configured, resetting the datapath ID will
* immediately disconnect from the controller, so it's better to set
* the datapath ID before the controller. */
bridge_reconfigure_remotes(br, managers, n_managers);
}
LIST_FOR_EACH (br, node, &all_bridges) {
for (i = 0; i < br->n_ports; i++) {
struct port *port = br->ports[i];
int j;
port_update_vlan_compat(port);
port_update_bonding(port);
for (j = 0; j < port->n_ifaces; j++) {
iface_update_qos(port->ifaces[j], port->cfg->qos);
}
}
}
LIST_FOR_EACH (br, node, &all_bridges) {
iterate_and_prune_ifaces(br, set_iface_properties, NULL);
}
LIST_FOR_EACH (br, node, &all_bridges) {
struct iface *iface;
HMAP_FOR_EACH (iface, dp_ifidx_node, &br->ifaces) {
iface_update_cfm(iface);
}
}
free(managers);
}
static const char *
get_ovsrec_key_value(const struct ovsdb_idl_row *row,
const struct ovsdb_idl_column *column,
const char *key)
{
const struct ovsdb_datum *datum;
union ovsdb_atom atom;
unsigned int idx;
datum = ovsdb_idl_get(row, column, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING);
atom.string = (char *) key;
idx = ovsdb_datum_find_key(datum, &atom, OVSDB_TYPE_STRING);
return idx == UINT_MAX ? NULL : datum->values[idx].string;
}
static const char *
bridge_get_other_config(const struct ovsrec_bridge *br_cfg, const char *key)
{
return get_ovsrec_key_value(&br_cfg->header_,
&ovsrec_bridge_col_other_config, key);
}
static void
bridge_pick_local_hw_addr(struct bridge *br, uint8_t ea[ETH_ADDR_LEN],
struct iface **hw_addr_iface)
{
const char *hwaddr;
size_t i, j;
int error;
*hw_addr_iface = NULL;
/* Did the user request a particular MAC? */
hwaddr = bridge_get_other_config(br->cfg, "hwaddr");
if (hwaddr && eth_addr_from_string(hwaddr, ea)) {
if (eth_addr_is_multicast(ea)) {
VLOG_ERR("bridge %s: cannot set MAC address to multicast "
"address "ETH_ADDR_FMT, br->name, ETH_ADDR_ARGS(ea));
} else if (eth_addr_is_zero(ea)) {
VLOG_ERR("bridge %s: cannot set MAC address to zero", br->name);
} else {
return;
}
}
/* Otherwise choose the minimum non-local MAC address among all of the
* interfaces. */
memset(ea, 0xff, sizeof ea);
for (i = 0; i < br->n_ports; i++) {
struct port *port = br->ports[i];
uint8_t iface_ea[ETH_ADDR_LEN];
struct iface *iface;
/* Mirror output ports don't participate. */
if (port->is_mirror_output_port) {
continue;
}
/* Choose the MAC address to represent the port. */
if (port->cfg->mac && eth_addr_from_string(port->cfg->mac, iface_ea)) {
/* Find the interface with this Ethernet address (if any) so that
* we can provide the correct devname to the caller. */
iface = NULL;
for (j = 0; j < port->n_ifaces; j++) {
struct iface *candidate = port->ifaces[j];
uint8_t candidate_ea[ETH_ADDR_LEN];
if (!netdev_get_etheraddr(candidate->netdev, candidate_ea)
&& eth_addr_equals(iface_ea, candidate_ea)) {
iface = candidate;
}
}
} else {
/* Choose the interface whose MAC address will represent the port.
* The Linux kernel bonding code always chooses the MAC address of
* the first slave added to a bond, and the Fedora networking
* scripts always add slaves to a bond in alphabetical order, so
* for compatibility we choose the interface with the name that is
* first in alphabetical order. */
iface = port->ifaces[0];
for (j = 1; j < port->n_ifaces; j++) {
struct iface *candidate = port->ifaces[j];
if (strcmp(candidate->name, iface->name) < 0) {
iface = candidate;
}
}
/* The local port doesn't count (since we're trying to choose its
* MAC address anyway). */
if (iface->dp_ifidx == ODPP_LOCAL) {
continue;
}
/* Grab MAC. */
error = netdev_get_etheraddr(iface->netdev, iface_ea);
if (error) {
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
VLOG_ERR_RL(&rl, "failed to obtain Ethernet address of %s: %s",
iface->name, strerror(error));
continue;
}
}
/* Compare against our current choice. */
if (!eth_addr_is_multicast(iface_ea) &&
!eth_addr_is_local(iface_ea) &&
!eth_addr_is_reserved(iface_ea) &&
!eth_addr_is_zero(iface_ea) &&
memcmp(iface_ea, ea, ETH_ADDR_LEN) < 0)
{
memcpy(ea, iface_ea, ETH_ADDR_LEN);
*hw_addr_iface = iface;
}