forked from openvswitch/ovs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bridge.c
3534 lines (3128 loc) · 112 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 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 <assert.h>
#include <errno.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <inttypes.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 "cfg.h"
#include "coverage.h"
#include "dirs.h"
#include "dpif.h"
#include "dynamic-string.h"
#include "flow.h"
#include "hash.h"
#include "list.h"
#include "mac-learning.h"
#include "netdev.h"
#include "odp-util.h"
#include "ofp-print.h"
#include "ofpbuf.h"
#include "packets.h"
#include "poll-loop.h"
#include "port-array.h"
#include "proc-net-compat.h"
#include "process.h"
#include "secchan/ofproto.h"
#include "socket-util.h"
#include "stp.h"
#include "svec.h"
#include "timeval.h"
#include "util.h"
#include "unixctl.h"
#include "vconn.h"
#include "vconn-ssl.h"
#include "xenserver.h"
#include "xtoxll.h"
#define THIS_MODULE VLM_bridge
#include "vlog.h"
struct dst {
uint16_t vlan;
uint16_t dp_ifidx;
};
extern uint64_t mgmt_id;
struct iface {
struct port *port; /* Containing port. */
size_t port_ifidx; /* Index within containing port. */
char *name; /* Host network device name. */
int dp_ifidx; /* Index within kernel datapath. */
uint8_t mac[ETH_ADDR_LEN]; /* Ethernet address (all zeros if unknowns). */
tag_type tag; /* Tag associated with this interface. */
bool enabled; /* May be chosen for flows? */
long long delay_expires; /* Time after which 'enabled' may change. */
};
#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;
/* Selection criteria. */
struct svec src_ports;
struct svec dst_ports;
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. */
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. */
/* 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? */
/* Spanning tree info. */
enum stp_state stp_state; /* Always STP_FORWARDING if STP not in use. */
tag_type stp_state_tag; /* Tag for STP state change. */
};
#define DP_MAX_PORTS 255
struct bridge {
struct list node; /* Node in global list of bridges. */
char *name; /* User-specified arbitrary name. */
struct mac_learning *ml; /* MAC learning table, or null not to learn. */
bool sent_config_request; /* Successfully sent config request? */
uint8_t default_ea[ETH_ADDR_LEN]; /* Default MAC. */
/* Support for remote controllers. */
char *controller; /* NULL if there is no remote controller;
* "discover" to do controller discovery;
* otherwise a vconn name. */
/* OpenFlow switch processing. */
struct ofproto *ofproto; /* OpenFlow switch. */
/* Kernel datapath information. */
struct dpif dpif; /* Kernel datapath. */
struct port_array ifaces; /* Indexed by kernel datapath port number. */
/* Bridge ports. */
struct port **ports;
size_t n_ports, allocated_ports;
/* Bonding. */
bool has_bonded_ports;
long long int bond_next_rebalance;
/* Flow tracking. */
bool flush;
/* Flow statistics gathering. */
time_t next_stats_request;
/* Port mirroring. */
struct mirror *mirrors[MAX_MIRRORS];
/* Spanning tree. */
struct stp *stp;
long long int stp_last_tick;
};
/* List of all bridges. */
static struct list all_bridges = LIST_INITIALIZER(&all_bridges);
/* Maximum number of datapaths. */
enum { DP_MAX = 256 };
static struct bridge *bridge_create(const char *name);
static void bridge_destroy(struct bridge *);
static struct bridge *bridge_lookup(const char *name);
static int bridge_run_one(struct bridge *);
static void bridge_reconfigure_one(struct bridge *);
static void bridge_reconfigure_controller(struct bridge *);
static void bridge_get_all_ifaces(const struct bridge *, struct svec *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],
const char **devname);
static uint64_t bridge_pick_datapath_id(struct bridge *,
const uint8_t bridge_ea[ETH_ADDR_LEN],
const char *devname);
static uint64_t dpid_from_hash(const void *, size_t nbytes);
static void bridge_unixctl_fdb_show(struct unixctl_conn *, const char *args);
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 port_create(struct bridge *, const char *name);
static void port_reconfigure(struct 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 mirror_create(struct bridge *, const char *name);
static void mirror_destroy(struct mirror *);
static void mirror_reconfigure(struct bridge *);
static void mirror_reconfigure_one(struct mirror *);
static bool vlan_is_mirrored(const struct mirror *, int vlan);
static void brstp_reconfigure(struct bridge *);
static void brstp_adjust_timers(struct bridge *);
static void brstp_run(struct bridge *);
static void brstp_wait(struct bridge *);
static void iface_create(struct port *, const char *name);
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);
/* Hooks into ofproto processing. */
static struct ofhooks bridge_ofhooks;
/* Public functions. */
/* Adds the name of each interface used by a bridge, including local and
* internal ports, to 'svec'. */
void
bridge_get_ifaces(struct svec *svec)
{
struct bridge *br, *next;
size_t i, j;
LIST_FOR_EACH_SAFE (br, next, struct bridge, node, &all_bridges) {
for (i = 0; i < br->n_ports; i++) {
struct port *port = br->ports[i];
for (j = 0; j < port->n_ifaces; j++) {
struct iface *iface = port->ifaces[j];
if (iface->dp_ifidx < 0) {
VLOG_ERR("%s interface not in dp%u, ignoring",
iface->name, dpif_id(&br->dpif));
} else {
if (iface->dp_ifidx != ODPP_LOCAL) {
svec_add(svec, iface->name);
}
}
}
}
}
}
/* The caller must already have called cfg_read(). */
void
bridge_init(void)
{
int retval;
int i;
bond_init();
unixctl_command_register("fdb/show", bridge_unixctl_fdb_show);
for (i = 0; i < DP_MAX; i++) {
struct dpif dpif;
char devname[16];
sprintf(devname, "dp%d", i);
retval = dpif_open(devname, &dpif);
if (!retval) {
char dpif_name[IF_NAMESIZE];
if (dpif_get_name(&dpif, dpif_name, sizeof dpif_name)
|| !cfg_has("bridge.%s.port", dpif_name)) {
dpif_delete(&dpif);
}
dpif_close(&dpif);
} else if (retval != ENODEV) {
VLOG_ERR("failed to delete datapath dp%d: %s",
i, strerror(retval));
}
}
bridge_reconfigure();
}
#ifdef HAVE_OPENSSL
static bool
config_string_change(const char *key, char **valuep)
{
const char *value = cfg_get_string(0, "%s", key);
if (value && (!*valuep || strcmp(value, *valuep))) {
free(*valuep);
*valuep = xstrdup(value);
return true;
} else {
return false;
}
}
static void
bridge_configure_ssl(void)
{
/* XXX SSL should be configurable on a per-bridge basis.
* XXX should be possible to de-configure SSL. */
static char *private_key_file;
static char *certificate_file;
static char *cacert_file;
struct stat s;
if (config_string_change("ssl.private-key", &private_key_file)) {
vconn_ssl_set_private_key_file(private_key_file);
}
if (config_string_change("ssl.certificate", &certificate_file)) {
vconn_ssl_set_certificate_file(certificate_file);
}
/* We assume that even if the filename hasn't changed, if the CA cert
* file has been removed, that we want to move back into
* boot-strapping mode. This opens a small security hole, because
* the old certificate will still be trusted until vSwitch is
* restarted. We may want to address this in vconn's SSL library. */
if (config_string_change("ssl.ca-cert", &cacert_file)
|| (stat(cacert_file, &s) && errno == ENOENT)) {
vconn_ssl_set_ca_cert_file(cacert_file,
cfg_get_bool(0, "ssl.bootstrap-ca-cert"));
}
}
#endif
void
bridge_reconfigure(void)
{
struct svec old_br, new_br, raw_new_br;
struct bridge *br, *next;
size_t i, j;
COVERAGE_INC(bridge_reconfigure);
/* Collect old bridges. */
svec_init(&old_br);
LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
svec_add(&old_br, br->name);
}
/* Collect new bridges. */
svec_init(&raw_new_br);
cfg_get_subsections(&raw_new_br, "bridge");
svec_init(&new_br);
for (i = 0; i < raw_new_br.n; i++) {
const char *name = raw_new_br.names[i];
if ((!strncmp(name, "dp", 2) && isdigit(name[2])) ||
(!strncmp(name, "nl:", 3) && isdigit(name[3]))) {
VLOG_ERR("%s is not a valid bridge name (bridges may not be "
"named \"dp\" or \"nl:\" followed by a digit)", name);
} else {
svec_add(&new_br, name);
}
}
svec_destroy(&raw_new_br);
/* Get rid of deleted bridges and add new bridges. */
svec_sort(&old_br);
svec_sort(&new_br);
assert(svec_is_unique(&old_br));
assert(svec_is_unique(&new_br));
LIST_FOR_EACH_SAFE (br, next, struct bridge, node, &all_bridges) {
if (!svec_contains(&new_br, br->name)) {
bridge_destroy(br);
}
}
for (i = 0; i < new_br.n; i++) {
const char *name = new_br.names[i];
if (!svec_contains(&old_br, name)) {
bridge_create(name);
}
}
svec_destroy(&old_br);
svec_destroy(&new_br);
#ifdef HAVE_OPENSSL
/* Configure SSL. */
bridge_configure_ssl();
#endif
/* Reconfigure all bridges. */
LIST_FOR_EACH (br, struct bridge, 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, struct bridge, node, &all_bridges) {
struct odp_port *dpif_ports;
size_t n_dpif_ports;
struct svec 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 (!svec_contains(&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 dp%u: %s",
p->devname, dpif_id(&br->dpif), strerror(retval));
}
}
}
svec_destroy(&want_ifaces);
free(dpif_ports);
}
LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
struct odp_port *dpif_ports;
size_t n_dpif_ports;
struct svec cur_ifaces, want_ifaces, add_ifaces;
int next_port_no;
dpif_port_list(&br->dpif, &dpif_ports, &n_dpif_ports);
svec_init(&cur_ifaces);
for (i = 0; i < n_dpif_ports; i++) {
svec_add(&cur_ifaces, dpif_ports[i].devname);
}
free(dpif_ports);
svec_sort_unique(&cur_ifaces);
bridge_get_all_ifaces(br, &want_ifaces);
svec_diff(&want_ifaces, &cur_ifaces, &add_ifaces, NULL, NULL);
next_port_no = 1;
for (i = 0; i < add_ifaces.n; i++) {
const char *if_name = add_ifaces.names[i];
for (;;) {
int internal = cfg_get_bool(0, "iface.%s.internal", if_name);
int error = dpif_port_add(&br->dpif, if_name, next_port_no++,
internal ? ODP_PORT_INTERNAL : 0);
if (error != EEXIST) {
if (next_port_no >= 256) {
VLOG_ERR("ran out of valid port numbers on dp%u",
dpif_id(&br->dpif));
goto out;
}
if (error) {
VLOG_ERR("failed to add %s interface to dp%u: %s",
if_name, dpif_id(&br->dpif), strerror(error));
}
break;
}
}
}
out:
svec_destroy(&cur_ifaces);
svec_destroy(&want_ifaces);
svec_destroy(&add_ifaces);
}
LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
uint8_t ea[8];
uint64_t dpid;
struct iface *local_iface = NULL;
const char *devname;
uint8_t engine_type = br->dpif.minor;
uint8_t engine_id = br->dpif.minor;
bool add_id_to_iface = false;
struct svec nf_hosts;
bridge_fetch_dp_ifaces(br);
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 (iface->dp_ifidx < 0) {
VLOG_ERR("%s interface not in dp%u, dropping",
iface->name, dpif_id(&br->dpif));
iface_destroy(iface);
} else {
if (iface->dp_ifidx == ODPP_LOCAL) {
local_iface = iface;
}
VLOG_DBG("dp%u has interface %s on port %d",
dpif_id(&br->dpif), iface->name, iface->dp_ifidx);
j++;
}
}
if (!port->n_ifaces) {
VLOG_ERR("%s port has no interfaces, dropping", port->name);
port_destroy(port);
continue;
}
i++;
}
/* Pick local port hardware address, datapath ID. */
bridge_pick_local_hw_addr(br, ea, &devname);
if (local_iface) {
int error = netdev_nodev_set_etheraddr(local_iface->name, 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, devname);
ofproto_set_datapath_id(br->ofproto, dpid);
/* Set NetFlow configuration on this bridge. */
if (cfg_has("netflow.%s.engine-type", br->name)) {
engine_type = cfg_get_int(0, "netflow.%s.engine-type",
br->name);
}
if (cfg_has("netflow.%s.engine-id", br->name)) {
engine_id = cfg_get_int(0, "netflow.%s.engine-id", br->name);
}
if (cfg_has("netflow.%s.add-id-to-iface", br->name)) {
add_id_to_iface = cfg_get_bool(0, "netflow.%s.add-id-to-iface",
br->name);
}
if (add_id_to_iface && 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 (add_id_to_iface && br->n_ports > 0x1ff) {
VLOG_WARN("bridge %s: netflow port mangling will conflict with "
"another port when 512 or more ports are used",
br->name);
}
svec_init(&nf_hosts);
cfg_get_all_keys(&nf_hosts, "netflow.%s.host", br->name);
if (ofproto_set_netflow(br->ofproto, &nf_hosts, engine_type,
engine_id, add_id_to_iface)) {
VLOG_ERR("bridge %s: problem setting netflow collectors",
br->name);
}
/* 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_controller(br);
}
LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
for (i = 0; i < br->n_ports; i++) {
struct port *port = br->ports[i];
port_update_vlan_compat(port);
}
}
LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
brstp_reconfigure(br);
}
}
static void
bridge_pick_local_hw_addr(struct bridge *br, uint8_t ea[ETH_ADDR_LEN],
const char **devname)
{
uint64_t requested_ea;
size_t i, j;
int error;
*devname = NULL;
/* Did the user request a particular MAC? */
requested_ea = cfg_get_mac(0, "bridge.%s.mac", br->name);
if (requested_ea) {
eth_addr_from_uint64(requested_ea, 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 MAC address among all of the interfaces.
* (Xen uses FE:FF:FF:FF:FF:FF for virtual interfaces so this will get the
* MAC of the physical interface in such an environment.) */
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];
uint64_t iface_ea_u64;
struct iface *iface;
/* Mirror output ports don't participate. */
if (port->is_mirror_output_port) {
continue;
}
/* Choose the MAC address to represent the port. */
iface_ea_u64 = cfg_get_mac(0, "port.%s.mac", port->name);
if (iface_ea_u64) {
/* User specified explicitly. */
eth_addr_from_uint64(iface_ea_u64, iface_ea);
} 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). Other internal ports don't count because
* we really want a physical MAC if we can get it, and internal
* ports typically have randomly generated MACs. */
if (iface->dp_ifidx == ODPP_LOCAL
|| cfg_get_bool(0, "iface.%s.internal", iface->name)) {
continue;
}
/* Grab MAC. */
error = netdev_nodev_get_etheraddr(iface->name, 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_reserved(iface_ea) &&
!eth_addr_is_zero(iface_ea) &&
memcmp(iface_ea, ea, ETH_ADDR_LEN) < 0)
{
memcpy(ea, iface_ea, ETH_ADDR_LEN);
*devname = iface->name;
}
}
if (eth_addr_is_multicast(ea) || eth_addr_is_vif(ea)) {
memcpy(ea, br->default_ea, ETH_ADDR_LEN);
*devname = NULL;
VLOG_WARN("bridge %s: using default bridge Ethernet "
"address "ETH_ADDR_FMT, br->name, ETH_ADDR_ARGS(ea));
} else {
VLOG_DBG("bridge %s: using bridge Ethernet address "ETH_ADDR_FMT,
br->name, ETH_ADDR_ARGS(ea));
}
}
/* Choose and returns the datapath ID for bridge 'br' given that the bridge
* Ethernet address is 'bridge_ea'. If 'bridge_ea' is the Ethernet address of
* a network device, then that network device's name must be passed in as
* 'devname'; if 'bridge_ea' was derived some other way, then 'devname' must be
* passed in as a null pointer. */
static uint64_t
bridge_pick_datapath_id(struct bridge *br,
const uint8_t bridge_ea[ETH_ADDR_LEN],
const char *devname)
{
/*
* The procedure for choosing a bridge MAC address will, in the most
* ordinary case, also choose a unique MAC that we can use as a datapath
* ID. In some special cases, though, multiple bridges will end up with
* the same MAC address. This is OK for the bridges, but it will confuse
* the OpenFlow controller, because each datapath needs a unique datapath
* ID.
*
* Datapath IDs must be unique. It is also very desirable that they be
* stable from one run to the next, so that policy set on a datapath
* "sticks".
*/
uint64_t dpid;
dpid = cfg_get_dpid(0, "bridge.%s.datapath-id", br->name);
if (dpid) {
return dpid;
}
if (devname) {
int vlan;
if (!netdev_get_vlan_vid(devname, &vlan)) {
/*
* A bridge whose MAC address is taken from a VLAN network device
* (that is, a network device created with vconfig(8) or similar
* tool) will have the same MAC address as a bridge on the VLAN
* device's physical network device.
*
* Handle this case by hashing the physical network device MAC
* along with the VLAN identifier.
*/
uint8_t buf[ETH_ADDR_LEN + 2];
memcpy(buf, bridge_ea, ETH_ADDR_LEN);
buf[ETH_ADDR_LEN] = vlan >> 8;
buf[ETH_ADDR_LEN + 1] = vlan;
return dpid_from_hash(buf, sizeof buf);
} else {
/*
* Assume that this bridge's MAC address is unique, since it
* doesn't fit any of the cases we handle specially.
*/
}
} else {
/*
* A purely internal bridge, that is, one that has no non-virtual
* network devices on it at all, is more difficult because it has no
* natural unique identifier at all.
*
* When the host is a XenServer, we handle this case by hashing the
* host's UUID with the name of the bridge. Names of bridges are
* persistent across XenServer reboots, although they can be reused if
* an internal network is destroyed and then a new one is later
* created, so this is fairly effective.
*
* When the host is not a XenServer, we punt by using a random MAC
* address on each run.
*/
const char *host_uuid = xenserver_get_host_uuid();
if (host_uuid) {
char *combined = xasprintf("%s,%s", host_uuid, br->name);
dpid = dpid_from_hash(combined, strlen(combined));
free(combined);
return dpid;
}
}
return eth_addr_to_uint64(bridge_ea);
}
static uint64_t
dpid_from_hash(const void *data, size_t n)
{
uint8_t hash[SHA1_DIGEST_SIZE];
BUILD_ASSERT_DECL(sizeof hash >= ETH_ADDR_LEN);
sha1_bytes(data, n, hash);
eth_addr_mark_random(hash);
return eth_addr_to_uint64(hash);
}
int
bridge_run(void)
{
struct bridge *br, *next;
int retval;
retval = 0;
LIST_FOR_EACH_SAFE (br, next, struct bridge, node, &all_bridges) {
int error = bridge_run_one(br);
if (error) {
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
VLOG_ERR_RL(&rl, "bridge %s: datapath was destroyed externally, "
"forcing reconfiguration", br->name);
if (!retval) {
retval = error;
}
}
}
return retval;
}
void
bridge_wait(void)
{
struct bridge *br;
LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
ofproto_wait(br->ofproto);
if (br->controller) {
continue;
}
if (br->ml) {
mac_learning_wait(br->ml);
}
bond_wait(br);
brstp_wait(br);
}
}
/* Forces 'br' to revalidate all of its flows. This is appropriate when 'br''s
* configuration changes. */
static void
bridge_flush(struct bridge *br)
{
COVERAGE_INC(bridge_flush);
br->flush = true;
if (br->ml) {
mac_learning_flush(br->ml);
}
}
/* Bridge unixctl user interface functions. */
static void
bridge_unixctl_fdb_show(struct unixctl_conn *conn, const char *args)
{
struct ds ds = DS_EMPTY_INITIALIZER;
const struct bridge *br;
br = bridge_lookup(args);
if (!br) {
unixctl_command_reply(conn, 501, "no such bridge");
return;
}
ds_put_cstr(&ds, " port VLAN MAC Age\n");
if (br->ml) {
const struct mac_entry *e;
LIST_FOR_EACH (e, struct mac_entry, lru_node, &br->ml->lrus) {
if (e->port < 0 || e->port >= br->n_ports) {
continue;
}
ds_put_format(&ds, "%5d %4d "ETH_ADDR_FMT" %3d\n",
br->ports[e->port]->ifaces[0]->dp_ifidx,
e->vlan, ETH_ADDR_ARGS(e->mac), mac_entry_age(e));
}
}
unixctl_command_reply(conn, 200, ds_cstr(&ds));
ds_destroy(&ds);
}
/* Bridge reconfiguration functions. */
static struct bridge *
bridge_create(const char *name)
{
struct bridge *br;
int error;
assert(!bridge_lookup(name));
br = xcalloc(1, sizeof *br);
error = dpif_create(name, &br->dpif);
if (error == EEXIST) {
error = dpif_open(name, &br->dpif);
if (error) {
VLOG_ERR("datapath %s already exists but cannot be opened: %s",
name, strerror(error));
free(br);
return NULL;
}
dpif_flow_flush(&br->dpif);
} else if (error) {
VLOG_ERR("failed to create datapath %s: %s", name, strerror(error));
free(br);
return NULL;
}
error = ofproto_create(name, &bridge_ofhooks, br, &br->ofproto);
if (error) {
VLOG_ERR("failed to create switch %s: %s", name, strerror(error));
dpif_delete(&br->dpif);
dpif_close(&br->dpif);
free(br);
return NULL;
}
br->name = xstrdup(name);
br->ml = mac_learning_create();
br->sent_config_request = false;
eth_addr_random(br->default_ea);
port_array_init(&br->ifaces);
br->flush = false;
br->bond_next_rebalance = time_msec() + 10000;
list_push_back(&all_bridges, &br->node);
VLOG_INFO("created bridge %s on dp%u", br->name, dpif_id(&br->dpif));
return br;
}
static void
bridge_destroy(struct bridge *br)
{
if (br) {
int error;
while (br->n_ports > 0) {
port_destroy(br->ports[br->n_ports - 1]);
}
list_remove(&br->node);
error = dpif_delete(&br->dpif);
if (error && error != ENOENT) {
VLOG_ERR("failed to delete dp%u: %s",
dpif_id(&br->dpif), strerror(error));
}
dpif_close(&br->dpif);
ofproto_destroy(br->ofproto);
free(br->controller);
mac_learning_destroy(br->ml);
port_array_destroy(&br->ifaces);
free(br->ports);
free(br->name);
free(br);
}
}
static struct bridge *
bridge_lookup(const char *name)
{
struct bridge *br;
LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
if (!strcmp(br->name, name)) {
return br;
}
}
return NULL;
}
bool
bridge_exists(const char *name)
{
return bridge_lookup(name) ? true : false;
}
uint64_t
bridge_get_datapathid(const char *name)
{
struct bridge *br = bridge_lookup(name);
return br ? ofproto_get_datapath_id(br->ofproto) : 0;
}
static int
bridge_run_one(struct bridge *br)
{
int error;
error = ofproto_run1(br->ofproto);
if (error) {
return error;
}
if (br->ml) {
mac_learning_run(br->ml, ofproto_get_revalidate_set(br->ofproto));
}
bond_run(br);
brstp_run(br);
error = ofproto_run2(br->ofproto, br->flush);
br->flush = false;
return error;
}
static const char *
bridge_get_controller(const struct bridge *br)
{
const char *controller;
controller = cfg_get_string(0, "bridge.%s.controller", br->name);
if (!controller) {
controller = cfg_get_string(0, "mgmt.controller");
}
return controller && controller[0] ? controller : NULL;
}
static void
bridge_reconfigure_one(struct bridge *br)
{
struct svec old_ports, new_ports, ifaces;
struct svec listeners, old_listeners;
struct svec snoops, old_snoops;