1 /* Copyright (c) 2008, 2009 Nicira Networks
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
20 #include <arpa/inet.h>
24 #include <openflow/openflow.h>
29 #include <sys/socket.h>
30 #include <sys/types.h>
37 #include "dynamic-string.h"
41 #include "mac-learning.h"
44 #include "ofp-print.h"
46 #include "ofproto/netflow.h"
47 #include "ofproto/ofproto.h"
49 #include "poll-loop.h"
50 #include "port-array.h"
51 #include "proc-net-compat.h"
53 #include "socket-util.h"
60 #include "vconn-ssl.h"
61 #include "xenserver.h"
64 #define THIS_MODULE VLM_bridge
72 extern uint64_t mgmt_id;
75 /* These members are always valid. */
76 struct port *port; /* Containing port. */
77 size_t port_ifidx; /* Index within containing port. */
78 char *name; /* Host network device name. */
79 tag_type tag; /* Tag associated with this interface. */
80 long long delay_expires; /* Time after which 'enabled' may change. */
82 /* These members are valid only after bridge_reconfigure() causes them to
84 int dp_ifidx; /* Index within kernel datapath. */
85 struct netdev *netdev; /* Network device. */
86 bool enabled; /* May be chosen for flows? */
89 #define BOND_MASK 0xff
91 int iface_idx; /* Index of assigned iface, or -1 if none. */
92 uint64_t tx_bytes; /* Count of bytes recently transmitted. */
93 tag_type iface_tag; /* Tag associated with iface_idx. */
96 #define MAX_MIRRORS 32
97 typedef uint32_t mirror_mask_t;
98 #define MIRROR_MASK_C(X) UINT32_C(X)
99 BUILD_ASSERT_DECL(sizeof(mirror_mask_t) * CHAR_BIT >= MAX_MIRRORS);
101 struct bridge *bridge;
105 /* Selection criteria. */
106 struct svec src_ports;
107 struct svec dst_ports;
112 struct port *out_port;
116 #define FLOOD_PORT ((struct port *) 1) /* The 'flood' output port. */
118 struct bridge *bridge;
120 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
121 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1. */
124 /* An ordinary bridge port has 1 interface.
125 * A bridge port for bonding has at least 2 interfaces. */
126 struct iface **ifaces;
127 size_t n_ifaces, allocated_ifaces;
130 struct bond_entry *bond_hash; /* An array of (BOND_MASK + 1) elements. */
131 int active_iface; /* Ifidx on which bcasts accepted, or -1. */
132 tag_type active_iface_tag; /* Tag for bcast flows. */
133 tag_type no_ifaces_tag; /* Tag for flows when all ifaces disabled. */
134 int updelay, downdelay; /* Delay before iface goes up/down, in ms. */
135 bool bond_compat_is_stale; /* Need to call port_update_bond_compat()? */
137 /* Port mirroring info. */
138 mirror_mask_t src_mirrors; /* Mirrors triggered when packet received. */
139 mirror_mask_t dst_mirrors; /* Mirrors triggered when packet sent. */
140 bool is_mirror_output_port; /* Does port mirroring send frames here? */
142 /* Spanning tree info. */
143 enum stp_state stp_state; /* Always STP_FORWARDING if STP not in use. */
144 tag_type stp_state_tag; /* Tag for STP state change. */
147 #define DP_MAX_PORTS 255
149 struct list node; /* Node in global list of bridges. */
150 char *name; /* User-specified arbitrary name. */
151 struct mac_learning *ml; /* MAC learning table. */
152 bool sent_config_request; /* Successfully sent config request? */
153 uint8_t default_ea[ETH_ADDR_LEN]; /* Default MAC. */
155 /* Support for remote controllers. */
156 char *controller; /* NULL if there is no remote controller;
157 * "discover" to do controller discovery;
158 * otherwise a vconn name. */
160 /* OpenFlow switch processing. */
161 struct ofproto *ofproto; /* OpenFlow switch. */
163 /* Kernel datapath information. */
164 struct dpif *dpif; /* Datapath. */
165 struct port_array ifaces; /* Indexed by kernel datapath port number. */
169 size_t n_ports, allocated_ports;
172 bool has_bonded_ports;
173 long long int bond_next_rebalance;
178 /* Flow statistics gathering. */
179 time_t next_stats_request;
181 /* Port mirroring. */
182 struct mirror *mirrors[MAX_MIRRORS];
186 long long int stp_last_tick;
189 /* List of all bridges. */
190 static struct list all_bridges = LIST_INITIALIZER(&all_bridges);
192 /* Maximum number of datapaths. */
193 enum { DP_MAX = 256 };
195 static struct bridge *bridge_create(const char *name);
196 static void bridge_destroy(struct bridge *);
197 static struct bridge *bridge_lookup(const char *name);
198 static void bridge_unixctl_dump_flows(struct unixctl_conn *, const char *);
199 static int bridge_run_one(struct bridge *);
200 static void bridge_reconfigure_one(struct bridge *);
201 static void bridge_reconfigure_controller(struct bridge *);
202 static void bridge_get_all_ifaces(const struct bridge *, struct svec *ifaces);
203 static void bridge_fetch_dp_ifaces(struct bridge *);
204 static void bridge_flush(struct bridge *);
205 static void bridge_pick_local_hw_addr(struct bridge *,
206 uint8_t ea[ETH_ADDR_LEN],
207 struct iface **hw_addr_iface);
208 static uint64_t bridge_pick_datapath_id(struct bridge *,
209 const uint8_t bridge_ea[ETH_ADDR_LEN],
210 struct iface *hw_addr_iface);
211 static struct iface *bridge_get_local_iface(struct bridge *);
212 static uint64_t dpid_from_hash(const void *, size_t nbytes);
214 static void bridge_unixctl_fdb_show(struct unixctl_conn *, const char *args);
216 static void bond_init(void);
217 static void bond_run(struct bridge *);
218 static void bond_wait(struct bridge *);
219 static void bond_rebalance_port(struct port *);
220 static void bond_send_learning_packets(struct port *);
221 static void bond_enable_slave(struct iface *iface, bool enable);
223 static void port_create(struct bridge *, const char *name);
224 static void port_reconfigure(struct port *);
225 static void port_destroy(struct port *);
226 static struct port *port_lookup(const struct bridge *, const char *name);
227 static struct iface *port_lookup_iface(const struct port *, const char *name);
228 static struct port *port_from_dp_ifidx(const struct bridge *,
230 static void port_update_bond_compat(struct port *);
231 static void port_update_vlan_compat(struct port *);
232 static void port_update_bonding(struct port *);
234 static void mirror_create(struct bridge *, const char *name);
235 static void mirror_destroy(struct mirror *);
236 static void mirror_reconfigure(struct bridge *);
237 static void mirror_reconfigure_one(struct mirror *);
238 static bool vlan_is_mirrored(const struct mirror *, int vlan);
240 static void brstp_reconfigure(struct bridge *);
241 static void brstp_adjust_timers(struct bridge *);
242 static void brstp_run(struct bridge *);
243 static void brstp_wait(struct bridge *);
245 static void iface_create(struct port *, const char *name);
246 static void iface_destroy(struct iface *);
247 static struct iface *iface_lookup(const struct bridge *, const char *name);
248 static struct iface *iface_from_dp_ifidx(const struct bridge *,
250 static bool iface_is_internal(const struct bridge *, const char *name);
251 static void iface_set_mac(struct iface *);
253 /* Hooks into ofproto processing. */
254 static struct ofhooks bridge_ofhooks;
256 /* Public functions. */
258 /* Adds the name of each interface used by a bridge, including local and
259 * internal ports, to 'svec'. */
261 bridge_get_ifaces(struct svec *svec)
263 struct bridge *br, *next;
266 LIST_FOR_EACH_SAFE (br, next, struct bridge, node, &all_bridges) {
267 for (i = 0; i < br->n_ports; i++) {
268 struct port *port = br->ports[i];
270 for (j = 0; j < port->n_ifaces; j++) {
271 struct iface *iface = port->ifaces[j];
272 if (iface->dp_ifidx < 0) {
273 VLOG_ERR("%s interface not in datapath %s, ignoring",
274 iface->name, dpif_name(br->dpif));
276 if (iface->dp_ifidx != ODPP_LOCAL) {
277 svec_add(svec, iface->name);
285 /* The caller must already have called cfg_read(). */
289 struct svec dpif_names;
292 unixctl_command_register("fdb/show", bridge_unixctl_fdb_show);
294 svec_init(&dpif_names);
295 dp_enumerate(&dpif_names);
296 for (i = 0; i < dpif_names.n; i++) {
297 const char *dpif_name = dpif_names.names[i];
301 retval = dpif_open(dpif_name, &dpif);
303 struct svec all_names;
306 svec_init(&all_names);
307 dpif_get_all_names(dpif, &all_names);
308 for (j = 0; j < all_names.n; j++) {
309 if (cfg_has("bridge.%s.port", all_names.names[j])) {
315 svec_destroy(&all_names);
319 svec_destroy(&dpif_names);
321 unixctl_command_register("bridge/dump-flows", bridge_unixctl_dump_flows);
324 bridge_reconfigure();
329 config_string_change(const char *key, char **valuep)
331 const char *value = cfg_get_string(0, "%s", key);
332 if (value && (!*valuep || strcmp(value, *valuep))) {
334 *valuep = xstrdup(value);
342 bridge_configure_ssl(void)
344 /* XXX SSL should be configurable on a per-bridge basis.
345 * XXX should be possible to de-configure SSL. */
346 static char *private_key_file;
347 static char *certificate_file;
348 static char *cacert_file;
351 if (config_string_change("ssl.private-key", &private_key_file)) {
352 vconn_ssl_set_private_key_file(private_key_file);
355 if (config_string_change("ssl.certificate", &certificate_file)) {
356 vconn_ssl_set_certificate_file(certificate_file);
359 /* We assume that even if the filename hasn't changed, if the CA cert
360 * file has been removed, that we want to move back into
361 * boot-strapping mode. This opens a small security hole, because
362 * the old certificate will still be trusted until vSwitch is
363 * restarted. We may want to address this in vconn's SSL library. */
364 if (config_string_change("ssl.ca-cert", &cacert_file)
365 || (cacert_file && stat(cacert_file, &s) && errno == ENOENT)) {
366 vconn_ssl_set_ca_cert_file(cacert_file,
367 cfg_get_bool(0, "ssl.bootstrap-ca-cert"));
372 /* iterate_and_prune_ifaces() callback function that opens the network device
373 * for 'iface', if it is not already open, and retrieves the interface's MAC
374 * address and carrier status. */
376 init_iface_netdev(struct bridge *br UNUSED, struct iface *iface,
381 } else if (!netdev_open(iface->name, NETDEV_ETH_TYPE_NONE,
383 netdev_get_carrier(iface->netdev, &iface->enabled);
386 /* If the network device can't be opened, then we're not going to try
387 * to do anything with this interface. */
393 check_iface_dp_ifidx(struct bridge *br, struct iface *iface, void *aux UNUSED)
395 if (iface->dp_ifidx >= 0) {
396 VLOG_DBG("%s has interface %s on port %d",
398 iface->name, iface->dp_ifidx);
401 VLOG_ERR("%s interface not in %s, dropping",
402 iface->name, dpif_name(br->dpif));
408 set_iface_properties(struct bridge *br UNUSED, struct iface *iface,
413 /* Set policing attributes. */
414 rate = cfg_get_int(0, "port.%s.ingress.policing-rate", iface->name);
415 burst = cfg_get_int(0, "port.%s.ingress.policing-burst", iface->name);
416 netdev_set_policing(iface->netdev, rate, burst);
418 /* Set MAC address of internal interfaces other than the local
420 if (iface->dp_ifidx != ODPP_LOCAL
421 && iface_is_internal(br, iface->name)) {
422 iface_set_mac(iface);
428 /* Calls 'cb' for each interfaces in 'br', passing along the 'aux' argument.
429 * Deletes from 'br' all the interfaces for which 'cb' returns false, and then
430 * deletes from 'br' any ports that no longer have any interfaces. */
432 iterate_and_prune_ifaces(struct bridge *br,
433 bool (*cb)(struct bridge *, struct iface *,
439 for (i = 0; i < br->n_ports; ) {
440 struct port *port = br->ports[i];
441 for (j = 0; j < port->n_ifaces; ) {
442 struct iface *iface = port->ifaces[j];
443 if (cb(br, iface, aux)) {
446 iface_destroy(iface);
450 if (port->n_ifaces) {
453 VLOG_ERR("%s port has no interfaces, dropping", port->name);
460 bridge_reconfigure(void)
462 struct svec old_br, new_br;
463 struct bridge *br, *next;
466 COVERAGE_INC(bridge_reconfigure);
468 /* Collect old and new bridges. */
471 LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
472 svec_add(&old_br, br->name);
474 cfg_get_subsections(&new_br, "bridge");
476 /* Get rid of deleted bridges and add new bridges. */
479 assert(svec_is_unique(&old_br));
480 assert(svec_is_unique(&new_br));
481 LIST_FOR_EACH_SAFE (br, next, struct bridge, node, &all_bridges) {
482 if (!svec_contains(&new_br, br->name)) {
486 for (i = 0; i < new_br.n; i++) {
487 const char *name = new_br.names[i];
488 if (!svec_contains(&old_br, name)) {
492 svec_destroy(&old_br);
493 svec_destroy(&new_br);
497 bridge_configure_ssl();
500 /* Reconfigure all bridges. */
501 LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
502 bridge_reconfigure_one(br);
505 /* Add and delete ports on all datapaths.
507 * The kernel will reject any attempt to add a given port to a datapath if
508 * that port already belongs to a different datapath, so we must do all
509 * port deletions before any port additions. */
510 LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
511 struct odp_port *dpif_ports;
513 struct svec want_ifaces;
515 dpif_port_list(br->dpif, &dpif_ports, &n_dpif_ports);
516 bridge_get_all_ifaces(br, &want_ifaces);
517 for (i = 0; i < n_dpif_ports; i++) {
518 const struct odp_port *p = &dpif_ports[i];
519 if (!svec_contains(&want_ifaces, p->devname)
520 && strcmp(p->devname, br->name)) {
521 int retval = dpif_port_del(br->dpif, p->port);
523 VLOG_ERR("failed to remove %s interface from %s: %s",
524 p->devname, dpif_name(br->dpif),
529 svec_destroy(&want_ifaces);
532 LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
533 struct odp_port *dpif_ports;
535 struct svec cur_ifaces, want_ifaces, add_ifaces;
537 dpif_port_list(br->dpif, &dpif_ports, &n_dpif_ports);
538 svec_init(&cur_ifaces);
539 for (i = 0; i < n_dpif_ports; i++) {
540 svec_add(&cur_ifaces, dpif_ports[i].devname);
543 svec_sort_unique(&cur_ifaces);
544 bridge_get_all_ifaces(br, &want_ifaces);
545 svec_diff(&want_ifaces, &cur_ifaces, &add_ifaces, NULL, NULL);
547 for (i = 0; i < add_ifaces.n; i++) {
548 const char *if_name = add_ifaces.names[i];
552 /* Add to datapath. */
553 internal = iface_is_internal(br, if_name);
554 error = dpif_port_add(br->dpif, if_name,
555 internal ? ODP_PORT_INTERNAL : 0, NULL);
556 if (error == EFBIG) {
557 VLOG_ERR("ran out of valid port numbers on %s",
558 dpif_name(br->dpif));
561 VLOG_ERR("failed to add %s interface to %s: %s",
562 if_name, dpif_name(br->dpif), strerror(error));
565 svec_destroy(&cur_ifaces);
566 svec_destroy(&want_ifaces);
567 svec_destroy(&add_ifaces);
569 LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
572 struct iface *local_iface;
573 struct iface *hw_addr_iface;
574 struct netflow_options nf_options;
576 bridge_fetch_dp_ifaces(br);
577 iterate_and_prune_ifaces(br, init_iface_netdev, NULL);
579 iterate_and_prune_ifaces(br, check_iface_dp_ifidx, NULL);
581 /* Pick local port hardware address, datapath ID. */
582 bridge_pick_local_hw_addr(br, ea, &hw_addr_iface);
583 local_iface = bridge_get_local_iface(br);
585 int error = netdev_set_etheraddr(local_iface->netdev, ea);
587 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
588 VLOG_ERR_RL(&rl, "bridge %s: failed to set bridge "
589 "Ethernet address: %s",
590 br->name, strerror(error));
594 dpid = bridge_pick_datapath_id(br, ea, hw_addr_iface);
595 ofproto_set_datapath_id(br->ofproto, dpid);
597 /* Set NetFlow configuration on this bridge. */
598 memset(&nf_options, 0, sizeof nf_options);
599 dpif_get_netflow_ids(br->dpif, &nf_options.engine_type,
600 &nf_options.engine_id);
601 nf_options.active_timeout = -1;
603 if (cfg_has("netflow.%s.engine-type", br->name)) {
604 nf_options.engine_type = cfg_get_int(0, "netflow.%s.engine-type",
607 if (cfg_has("netflow.%s.engine-id", br->name)) {
608 nf_options.engine_id = cfg_get_int(0, "netflow.%s.engine-id",
611 if (cfg_has("netflow.%s.active-timeout", br->name)) {
612 nf_options.active_timeout = cfg_get_int(0,
613 "netflow.%s.active-timeout",
616 if (cfg_has("netflow.%s.add-id-to-iface", br->name)) {
617 nf_options.add_id_to_iface = cfg_get_bool(0,
618 "netflow.%s.add-id-to-iface",
621 if (nf_options.add_id_to_iface && nf_options.engine_id > 0x7f) {
622 VLOG_WARN("bridge %s: netflow port mangling may conflict with "
623 "another vswitch, choose an engine id less than 128",
626 if (nf_options.add_id_to_iface && br->n_ports > 508) {
627 VLOG_WARN("bridge %s: netflow port mangling will conflict with "
628 "another port when more than 508 ports are used",
631 svec_init(&nf_options.collectors);
632 cfg_get_all_keys(&nf_options.collectors, "netflow.%s.host", br->name);
633 if (ofproto_set_netflow(br->ofproto, &nf_options)) {
634 VLOG_ERR("bridge %s: problem setting netflow collectors",
637 svec_destroy(&nf_options.collectors);
639 /* Update the controller and related settings. It would be more
640 * straightforward to call this from bridge_reconfigure_one(), but we
641 * can't do it there for two reasons. First, and most importantly, at
642 * that point we don't know the dp_ifidx of any interfaces that have
643 * been added to the bridge (because we haven't actually added them to
644 * the datapath). Second, at that point we haven't set the datapath ID
645 * yet; when a controller is configured, resetting the datapath ID will
646 * immediately disconnect from the controller, so it's better to set
647 * the datapath ID before the controller. */
648 bridge_reconfigure_controller(br);
650 LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
651 for (i = 0; i < br->n_ports; i++) {
652 struct port *port = br->ports[i];
654 port_update_vlan_compat(port);
655 port_update_bonding(port);
658 LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
659 brstp_reconfigure(br);
660 iterate_and_prune_ifaces(br, set_iface_properties, NULL);
665 bridge_pick_local_hw_addr(struct bridge *br, uint8_t ea[ETH_ADDR_LEN],
666 struct iface **hw_addr_iface)
668 uint64_t requested_ea;
672 *hw_addr_iface = NULL;
674 /* Did the user request a particular MAC? */
675 requested_ea = cfg_get_mac(0, "bridge.%s.mac", br->name);
677 eth_addr_from_uint64(requested_ea, ea);
678 if (eth_addr_is_multicast(ea)) {
679 VLOG_ERR("bridge %s: cannot set MAC address to multicast "
680 "address "ETH_ADDR_FMT, br->name, ETH_ADDR_ARGS(ea));
681 } else if (eth_addr_is_zero(ea)) {
682 VLOG_ERR("bridge %s: cannot set MAC address to zero", br->name);
688 /* Otherwise choose the minimum MAC address among all of the interfaces.
689 * (Xen uses FE:FF:FF:FF:FF:FF for virtual interfaces so this will get the
690 * MAC of the physical interface in such an environment.) */
691 memset(ea, 0xff, sizeof ea);
692 for (i = 0; i < br->n_ports; i++) {
693 struct port *port = br->ports[i];
694 uint8_t iface_ea[ETH_ADDR_LEN];
695 uint64_t iface_ea_u64;
698 /* Mirror output ports don't participate. */
699 if (port->is_mirror_output_port) {
703 /* Choose the MAC address to represent the port. */
704 iface_ea_u64 = cfg_get_mac(0, "port.%s.mac", port->name);
706 /* User specified explicitly. */
707 eth_addr_from_uint64(iface_ea_u64, iface_ea);
709 /* Find the interface with this Ethernet address (if any) so that
710 * we can provide the correct devname to the caller. */
712 for (j = 0; j < port->n_ifaces; j++) {
713 struct iface *candidate = port->ifaces[j];
714 uint8_t candidate_ea[ETH_ADDR_LEN];
715 if (!netdev_get_etheraddr(candidate->netdev, candidate_ea)
716 && eth_addr_equals(iface_ea, candidate_ea)) {
721 /* Choose the interface whose MAC address will represent the port.
722 * The Linux kernel bonding code always chooses the MAC address of
723 * the first slave added to a bond, and the Fedora networking
724 * scripts always add slaves to a bond in alphabetical order, so
725 * for compatibility we choose the interface with the name that is
726 * first in alphabetical order. */
727 iface = port->ifaces[0];
728 for (j = 1; j < port->n_ifaces; j++) {
729 struct iface *candidate = port->ifaces[j];
730 if (strcmp(candidate->name, iface->name) < 0) {
735 /* The local port doesn't count (since we're trying to choose its
736 * MAC address anyway). Other internal ports don't count because
737 * we really want a physical MAC if we can get it, and internal
738 * ports typically have randomly generated MACs. */
739 if (iface->dp_ifidx == ODPP_LOCAL
740 || cfg_get_bool(0, "iface.%s.internal", iface->name)) {
745 error = netdev_get_etheraddr(iface->netdev, iface_ea);
747 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
748 VLOG_ERR_RL(&rl, "failed to obtain Ethernet address of %s: %s",
749 iface->name, strerror(error));
754 /* Compare against our current choice. */
755 if (!eth_addr_is_multicast(iface_ea) &&
756 !eth_addr_is_reserved(iface_ea) &&
757 !eth_addr_is_zero(iface_ea) &&
758 memcmp(iface_ea, ea, ETH_ADDR_LEN) < 0)
760 memcpy(ea, iface_ea, ETH_ADDR_LEN);
761 *hw_addr_iface = iface;
764 if (eth_addr_is_multicast(ea) || eth_addr_is_vif(ea)) {
765 memcpy(ea, br->default_ea, ETH_ADDR_LEN);
766 *hw_addr_iface = NULL;
767 VLOG_WARN("bridge %s: using default bridge Ethernet "
768 "address "ETH_ADDR_FMT, br->name, ETH_ADDR_ARGS(ea));
770 VLOG_DBG("bridge %s: using bridge Ethernet address "ETH_ADDR_FMT,
771 br->name, ETH_ADDR_ARGS(ea));
775 /* Choose and returns the datapath ID for bridge 'br' given that the bridge
776 * Ethernet address is 'bridge_ea'. If 'bridge_ea' is the Ethernet address of
777 * an interface on 'br', then that interface must be passed in as
778 * 'hw_addr_iface'; if 'bridge_ea' was derived some other way, then
779 * 'hw_addr_iface' must be passed in as a null pointer. */
781 bridge_pick_datapath_id(struct bridge *br,
782 const uint8_t bridge_ea[ETH_ADDR_LEN],
783 struct iface *hw_addr_iface)
786 * The procedure for choosing a bridge MAC address will, in the most
787 * ordinary case, also choose a unique MAC that we can use as a datapath
788 * ID. In some special cases, though, multiple bridges will end up with
789 * the same MAC address. This is OK for the bridges, but it will confuse
790 * the OpenFlow controller, because each datapath needs a unique datapath
793 * Datapath IDs must be unique. It is also very desirable that they be
794 * stable from one run to the next, so that policy set on a datapath
799 dpid = cfg_get_dpid(0, "bridge.%s.datapath-id", br->name);
806 if (!netdev_get_vlan_vid(hw_addr_iface->netdev, &vlan)) {
808 * A bridge whose MAC address is taken from a VLAN network device
809 * (that is, a network device created with vconfig(8) or similar
810 * tool) will have the same MAC address as a bridge on the VLAN
811 * device's physical network device.
813 * Handle this case by hashing the physical network device MAC
814 * along with the VLAN identifier.
816 uint8_t buf[ETH_ADDR_LEN + 2];
817 memcpy(buf, bridge_ea, ETH_ADDR_LEN);
818 buf[ETH_ADDR_LEN] = vlan >> 8;
819 buf[ETH_ADDR_LEN + 1] = vlan;
820 return dpid_from_hash(buf, sizeof buf);
823 * Assume that this bridge's MAC address is unique, since it
824 * doesn't fit any of the cases we handle specially.
829 * A purely internal bridge, that is, one that has no non-virtual
830 * network devices on it at all, is more difficult because it has no
831 * natural unique identifier at all.
833 * When the host is a XenServer, we handle this case by hashing the
834 * host's UUID with the name of the bridge. Names of bridges are
835 * persistent across XenServer reboots, although they can be reused if
836 * an internal network is destroyed and then a new one is later
837 * created, so this is fairly effective.
839 * When the host is not a XenServer, we punt by using a random MAC
840 * address on each run.
842 const char *host_uuid = xenserver_get_host_uuid();
844 char *combined = xasprintf("%s,%s", host_uuid, br->name);
845 dpid = dpid_from_hash(combined, strlen(combined));
851 return eth_addr_to_uint64(bridge_ea);
855 dpid_from_hash(const void *data, size_t n)
857 uint8_t hash[SHA1_DIGEST_SIZE];
859 BUILD_ASSERT_DECL(sizeof hash >= ETH_ADDR_LEN);
860 sha1_bytes(data, n, hash);
861 eth_addr_mark_random(hash);
862 return eth_addr_to_uint64(hash);
868 struct bridge *br, *next;
872 LIST_FOR_EACH_SAFE (br, next, struct bridge, node, &all_bridges) {
873 int error = bridge_run_one(br);
875 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
876 VLOG_ERR_RL(&rl, "bridge %s: datapath was destroyed externally, "
877 "forcing reconfiguration", br->name);
891 LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
892 ofproto_wait(br->ofproto);
893 if (br->controller) {
897 mac_learning_wait(br->ml);
903 /* Forces 'br' to revalidate all of its flows. This is appropriate when 'br''s
904 * configuration changes. */
906 bridge_flush(struct bridge *br)
908 COVERAGE_INC(bridge_flush);
910 mac_learning_flush(br->ml);
913 /* Returns the 'br' interface for the ODPP_LOCAL port, or null if 'br' has no
915 static struct iface *
916 bridge_get_local_iface(struct bridge *br)
920 for (i = 0; i < br->n_ports; i++) {
921 struct port *port = br->ports[i];
922 for (j = 0; j < port->n_ifaces; j++) {
923 struct iface *iface = port->ifaces[j];
924 if (iface->dp_ifidx == ODPP_LOCAL) {
933 /* Bridge unixctl user interface functions. */
935 bridge_unixctl_fdb_show(struct unixctl_conn *conn, const char *args)
937 struct ds ds = DS_EMPTY_INITIALIZER;
938 const struct bridge *br;
939 const struct mac_entry *e;
941 br = bridge_lookup(args);
943 unixctl_command_reply(conn, 501, "no such bridge");
947 ds_put_cstr(&ds, " port VLAN MAC Age\n");
948 LIST_FOR_EACH (e, struct mac_entry, lru_node, &br->ml->lrus) {
949 if (e->port < 0 || e->port >= br->n_ports) {
952 ds_put_format(&ds, "%5d %4d "ETH_ADDR_FMT" %3d\n",
953 br->ports[e->port]->ifaces[0]->dp_ifidx,
954 e->vlan, ETH_ADDR_ARGS(e->mac), mac_entry_age(e));
956 unixctl_command_reply(conn, 200, ds_cstr(&ds));
960 /* Bridge reconfiguration functions. */
962 static struct bridge *
963 bridge_create(const char *name)
968 assert(!bridge_lookup(name));
969 br = xcalloc(1, sizeof *br);
971 error = dpif_create_and_open(name, &br->dpif);
976 dpif_flow_flush(br->dpif);
978 error = ofproto_create(name, &bridge_ofhooks, br, &br->ofproto);
980 VLOG_ERR("failed to create switch %s: %s", name, strerror(error));
981 dpif_delete(br->dpif);
982 dpif_close(br->dpif);
987 br->name = xstrdup(name);
988 br->ml = mac_learning_create();
989 br->sent_config_request = false;
990 eth_addr_random(br->default_ea);
992 port_array_init(&br->ifaces);
995 br->bond_next_rebalance = time_msec() + 10000;
997 list_push_back(&all_bridges, &br->node);
999 VLOG_INFO("created bridge %s on %s", br->name, dpif_name(br->dpif));
1005 bridge_destroy(struct bridge *br)
1010 while (br->n_ports > 0) {
1011 port_destroy(br->ports[br->n_ports - 1]);
1013 list_remove(&br->node);
1014 error = dpif_delete(br->dpif);
1015 if (error && error != ENOENT) {
1016 VLOG_ERR("failed to delete %s: %s",
1017 dpif_name(br->dpif), strerror(error));
1019 dpif_close(br->dpif);
1020 ofproto_destroy(br->ofproto);
1021 free(br->controller);
1022 mac_learning_destroy(br->ml);
1023 port_array_destroy(&br->ifaces);
1030 static struct bridge *
1031 bridge_lookup(const char *name)
1035 LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
1036 if (!strcmp(br->name, name)) {
1044 bridge_exists(const char *name)
1046 return bridge_lookup(name) ? true : false;
1050 bridge_get_datapathid(const char *name)
1052 struct bridge *br = bridge_lookup(name);
1053 return br ? ofproto_get_datapath_id(br->ofproto) : 0;
1056 /* Handle requests for a listing of all flows known by the OpenFlow
1057 * stack, including those normally hidden. */
1059 bridge_unixctl_dump_flows(struct unixctl_conn *conn, const char *args)
1064 br = bridge_lookup(args);
1066 unixctl_command_reply(conn, 501, "Unknown bridge");
1071 ofproto_get_all_flows(br->ofproto, &results);
1073 unixctl_command_reply(conn, 200, ds_cstr(&results));
1074 ds_destroy(&results);
1078 bridge_run_one(struct bridge *br)
1082 error = ofproto_run1(br->ofproto);
1087 mac_learning_run(br->ml, ofproto_get_revalidate_set(br->ofproto));
1091 error = ofproto_run2(br->ofproto, br->flush);
1098 bridge_get_controller(const struct bridge *br)
1100 const char *controller;
1102 controller = cfg_get_string(0, "bridge.%s.controller", br->name);
1104 controller = cfg_get_string(0, "mgmt.controller");
1106 return controller && controller[0] ? controller : NULL;
1110 check_duplicate_ifaces(struct bridge *br, struct iface *iface, void *ifaces_)
1112 struct svec *ifaces = ifaces_;
1113 if (!svec_contains(ifaces, iface->name)) {
1114 svec_add(ifaces, iface->name);
1118 VLOG_ERR("bridge %s: %s interface is on multiple ports, "
1120 br->name, iface->name, iface->port->name);
1126 bridge_reconfigure_one(struct bridge *br)
1128 struct svec old_ports, new_ports, ifaces;
1129 struct svec listeners, old_listeners;
1130 struct svec snoops, old_snoops;
1133 /* Collect old ports. */
1134 svec_init(&old_ports);
1135 for (i = 0; i < br->n_ports; i++) {
1136 svec_add(&old_ports, br->ports[i]->name);
1138 svec_sort(&old_ports);
1139 assert(svec_is_unique(&old_ports));
1141 /* Collect new ports. */
1142 svec_init(&new_ports);
1143 cfg_get_all_keys(&new_ports, "bridge.%s.port", br->name);
1144 svec_sort(&new_ports);
1145 if (bridge_get_controller(br)) {
1146 char local_name[IF_NAMESIZE];
1149 error = dpif_port_get_name(br->dpif, ODPP_LOCAL,
1150 local_name, sizeof local_name);
1151 if (!error && !svec_contains(&new_ports, local_name)) {
1152 svec_add(&new_ports, local_name);
1153 svec_sort(&new_ports);
1156 if (!svec_is_unique(&new_ports)) {
1157 VLOG_WARN("bridge %s: %s specified twice as bridge port",
1158 br->name, svec_get_duplicate(&new_ports));
1159 svec_unique(&new_ports);
1162 ofproto_set_mgmt_id(br->ofproto, mgmt_id);
1164 /* Get rid of deleted ports and add new ports. */
1165 for (i = 0; i < br->n_ports; ) {
1166 struct port *port = br->ports[i];
1167 if (!svec_contains(&new_ports, port->name)) {
1173 for (i = 0; i < new_ports.n; i++) {
1174 const char *name = new_ports.names[i];
1175 if (!svec_contains(&old_ports, name)) {
1176 port_create(br, name);
1179 svec_destroy(&old_ports);
1180 svec_destroy(&new_ports);
1182 /* Reconfigure all ports. */
1183 for (i = 0; i < br->n_ports; i++) {
1184 port_reconfigure(br->ports[i]);
1187 /* Check and delete duplicate interfaces. */
1189 iterate_and_prune_ifaces(br, check_duplicate_ifaces, &ifaces);
1190 svec_destroy(&ifaces);
1192 /* Delete all flows if we're switching from connected to standalone or vice
1193 * versa. (XXX Should we delete all flows if we are switching from one
1194 * controller to another?) */
1196 /* Configure OpenFlow management listeners. */
1197 svec_init(&listeners);
1198 cfg_get_all_strings(&listeners, "bridge.%s.openflow.listeners", br->name);
1200 svec_add_nocopy(&listeners, xasprintf("punix:%s/%s.mgmt",
1201 ovs_rundir, br->name));
1202 } else if (listeners.n == 1 && !strcmp(listeners.names[0], "none")) {
1203 svec_clear(&listeners);
1205 svec_sort_unique(&listeners);
1207 svec_init(&old_listeners);
1208 ofproto_get_listeners(br->ofproto, &old_listeners);
1209 svec_sort_unique(&old_listeners);
1211 if (!svec_equal(&listeners, &old_listeners)) {
1212 ofproto_set_listeners(br->ofproto, &listeners);
1214 svec_destroy(&listeners);
1215 svec_destroy(&old_listeners);
1217 /* Configure OpenFlow controller connection snooping. */
1219 cfg_get_all_strings(&snoops, "bridge.%s.openflow.snoops", br->name);
1221 svec_add_nocopy(&snoops, xasprintf("punix:%s/%s.snoop",
1222 ovs_rundir, br->name));
1223 } else if (snoops.n == 1 && !strcmp(snoops.names[0], "none")) {
1224 svec_clear(&snoops);
1226 svec_sort_unique(&snoops);
1228 svec_init(&old_snoops);
1229 ofproto_get_snoops(br->ofproto, &old_snoops);
1230 svec_sort_unique(&old_snoops);
1232 if (!svec_equal(&snoops, &old_snoops)) {
1233 ofproto_set_snoops(br->ofproto, &snoops);
1235 svec_destroy(&snoops);
1236 svec_destroy(&old_snoops);
1238 mirror_reconfigure(br);
1242 bridge_reconfigure_controller(struct bridge *br)
1244 char *pfx = xasprintf("bridge.%s.controller", br->name);
1245 const char *controller;
1247 controller = bridge_get_controller(br);
1248 if ((br->controller != NULL) != (controller != NULL)) {
1249 ofproto_flush_flows(br->ofproto);
1251 free(br->controller);
1252 br->controller = controller ? xstrdup(controller) : NULL;
1255 const char *fail_mode;
1256 int max_backoff, probe;
1257 int rate_limit, burst_limit;
1259 if (!strcmp(controller, "discover")) {
1260 bool update_resolv_conf = true;
1262 if (cfg_has("%s.update-resolv.conf", pfx)) {
1263 update_resolv_conf = cfg_get_bool(0, "%s.update-resolv.conf",
1266 ofproto_set_discovery(br->ofproto, true,
1267 cfg_get_string(0, "%s.accept-regex", pfx),
1268 update_resolv_conf);
1270 struct iface *local_iface;
1273 in_band = (!cfg_is_valid(CFG_BOOL | CFG_REQUIRED,
1275 || cfg_get_bool(0, "%s.in-band", pfx));
1276 ofproto_set_discovery(br->ofproto, false, NULL, NULL);
1277 ofproto_set_in_band(br->ofproto, in_band);
1279 local_iface = bridge_get_local_iface(br);
1281 && cfg_is_valid(CFG_IP | CFG_REQUIRED, "%s.ip", pfx)) {
1282 struct netdev *netdev = local_iface->netdev;
1283 struct in_addr ip, mask, gateway;
1284 ip.s_addr = cfg_get_ip(0, "%s.ip", pfx);
1285 mask.s_addr = cfg_get_ip(0, "%s.netmask", pfx);
1286 gateway.s_addr = cfg_get_ip(0, "%s.gateway", pfx);
1288 netdev_turn_flags_on(netdev, NETDEV_UP, true);
1290 mask.s_addr = guess_netmask(ip.s_addr);
1292 if (!netdev_set_in4(netdev, ip, mask)) {
1293 VLOG_INFO("bridge %s: configured IP address "IP_FMT", "
1295 br->name, IP_ARGS(&ip.s_addr),
1296 IP_ARGS(&mask.s_addr));
1299 if (gateway.s_addr) {
1300 if (!netdev_add_router(netdev, gateway)) {
1301 VLOG_INFO("bridge %s: configured gateway "IP_FMT,
1302 br->name, IP_ARGS(&gateway.s_addr));
1308 fail_mode = cfg_get_string(0, "%s.fail-mode", pfx);
1310 fail_mode = cfg_get_string(0, "mgmt.fail-mode");
1312 ofproto_set_failure(br->ofproto,
1314 || !strcmp(fail_mode, "standalone")
1315 || !strcmp(fail_mode, "open")));
1317 probe = cfg_get_int(0, "%s.inactivity-probe", pfx);
1319 probe = cfg_get_int(0, "mgmt.inactivity-probe");
1324 ofproto_set_probe_interval(br->ofproto, probe);
1326 max_backoff = cfg_get_int(0, "%s.max-backoff", pfx);
1328 max_backoff = cfg_get_int(0, "mgmt.max-backoff");
1333 ofproto_set_max_backoff(br->ofproto, max_backoff);
1335 rate_limit = cfg_get_int(0, "%s.rate-limit", pfx);
1337 rate_limit = cfg_get_int(0, "mgmt.rate-limit");
1339 burst_limit = cfg_get_int(0, "%s.burst-limit", pfx);
1341 burst_limit = cfg_get_int(0, "mgmt.burst-limit");
1343 ofproto_set_rate_limit(br->ofproto, rate_limit, burst_limit);
1345 ofproto_set_stp(br->ofproto, cfg_get_bool(0, "%s.stp", pfx));
1347 if (cfg_has("%s.commands.acl", pfx)) {
1348 struct svec command_acls;
1351 svec_init(&command_acls);
1352 cfg_get_all_strings(&command_acls, "%s.commands.acl", pfx);
1353 command_acl = svec_join(&command_acls, ",", "");
1355 ofproto_set_remote_execution(br->ofproto, command_acl,
1356 cfg_get_string(0, "%s.commands.dir",
1359 svec_destroy(&command_acls);
1362 ofproto_set_remote_execution(br->ofproto, NULL, NULL);
1365 union ofp_action action;
1368 /* Set up a flow that matches every packet and directs them to
1369 * OFPP_NORMAL (which goes to us). */
1370 memset(&action, 0, sizeof action);
1371 action.type = htons(OFPAT_OUTPUT);
1372 action.output.len = htons(sizeof action);
1373 action.output.port = htons(OFPP_NORMAL);
1374 memset(&flow, 0, sizeof flow);
1375 ofproto_add_flow(br->ofproto, &flow, OFPFW_ALL, 0,
1378 ofproto_set_in_band(br->ofproto, false);
1379 ofproto_set_max_backoff(br->ofproto, 1);
1380 ofproto_set_probe_interval(br->ofproto, 5);
1381 ofproto_set_failure(br->ofproto, false);
1382 ofproto_set_stp(br->ofproto, false);
1386 ofproto_set_controller(br->ofproto, br->controller);
1390 bridge_get_all_ifaces(const struct bridge *br, struct svec *ifaces)
1395 for (i = 0; i < br->n_ports; i++) {
1396 struct port *port = br->ports[i];
1397 for (j = 0; j < port->n_ifaces; j++) {
1398 struct iface *iface = port->ifaces[j];
1399 svec_add(ifaces, iface->name);
1401 if (port->n_ifaces > 1
1402 && cfg_get_bool(0, "bonding.%s.fake-iface", port->name)) {
1403 svec_add(ifaces, port->name);
1406 svec_sort_unique(ifaces);
1409 /* For robustness, in case the administrator moves around datapath ports behind
1410 * our back, we re-check all the datapath port numbers here.
1412 * This function will set the 'dp_ifidx' members of interfaces that have
1413 * disappeared to -1, so only call this function from a context where those
1414 * 'struct iface's will be removed from the bridge. Otherwise, the -1
1415 * 'dp_ifidx'es will cause trouble later when we try to send them to the
1416 * datapath, which doesn't support UINT16_MAX+1 ports. */
1418 bridge_fetch_dp_ifaces(struct bridge *br)
1420 struct odp_port *dpif_ports;
1421 size_t n_dpif_ports;
1424 /* Reset all interface numbers. */
1425 for (i = 0; i < br->n_ports; i++) {
1426 struct port *port = br->ports[i];
1427 for (j = 0; j < port->n_ifaces; j++) {
1428 struct iface *iface = port->ifaces[j];
1429 iface->dp_ifidx = -1;
1432 port_array_clear(&br->ifaces);
1434 dpif_port_list(br->dpif, &dpif_ports, &n_dpif_ports);
1435 for (i = 0; i < n_dpif_ports; i++) {
1436 struct odp_port *p = &dpif_ports[i];
1437 struct iface *iface = iface_lookup(br, p->devname);
1439 if (iface->dp_ifidx >= 0) {
1440 VLOG_WARN("%s reported interface %s twice",
1441 dpif_name(br->dpif), p->devname);
1442 } else if (iface_from_dp_ifidx(br, p->port)) {
1443 VLOG_WARN("%s reported interface %"PRIu16" twice",
1444 dpif_name(br->dpif), p->port);
1446 port_array_set(&br->ifaces, p->port, iface);
1447 iface->dp_ifidx = p->port;
1454 /* Bridge packet processing functions. */
1457 bond_hash(const uint8_t mac[ETH_ADDR_LEN])
1459 return hash_bytes(mac, ETH_ADDR_LEN, 0) & BOND_MASK;
1462 static struct bond_entry *
1463 lookup_bond_entry(const struct port *port, const uint8_t mac[ETH_ADDR_LEN])
1465 return &port->bond_hash[bond_hash(mac)];
1469 bond_choose_iface(const struct port *port)
1471 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
1472 size_t i, best_down_slave = -1;
1473 long long next_delay_expiration = LLONG_MAX;
1475 for (i = 0; i < port->n_ifaces; i++) {
1476 struct iface *iface = port->ifaces[i];
1478 if (iface->enabled) {
1480 } else if (iface->delay_expires < next_delay_expiration) {
1481 best_down_slave = i;
1482 next_delay_expiration = iface->delay_expires;
1486 if (best_down_slave != -1) {
1487 struct iface *iface = port->ifaces[best_down_slave];
1489 VLOG_INFO_RL(&rl, "interface %s: skipping remaining %lli ms updelay "
1490 "since no other interface is up", iface->name,
1491 iface->delay_expires - time_msec());
1492 bond_enable_slave(iface, true);
1495 return best_down_slave;
1499 choose_output_iface(const struct port *port, const uint8_t *dl_src,
1500 uint16_t *dp_ifidx, tag_type *tags)
1502 struct iface *iface;
1504 assert(port->n_ifaces);
1505 if (port->n_ifaces == 1) {
1506 iface = port->ifaces[0];
1508 struct bond_entry *e = lookup_bond_entry(port, dl_src);
1509 if (e->iface_idx < 0 || e->iface_idx >= port->n_ifaces
1510 || !port->ifaces[e->iface_idx]->enabled) {
1511 /* XXX select interface properly. The current interface selection
1512 * is only good for testing the rebalancing code. */
1513 e->iface_idx = bond_choose_iface(port);
1514 if (e->iface_idx < 0) {
1515 *tags |= port->no_ifaces_tag;
1518 e->iface_tag = tag_create_random();
1519 ((struct port *) port)->bond_compat_is_stale = true;
1521 *tags |= e->iface_tag;
1522 iface = port->ifaces[e->iface_idx];
1524 *dp_ifidx = iface->dp_ifidx;
1525 *tags |= iface->tag; /* Currently only used for bonding. */
1530 bond_link_status_update(struct iface *iface, bool carrier)
1532 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
1533 struct port *port = iface->port;
1535 if ((carrier == iface->enabled) == (iface->delay_expires == LLONG_MAX)) {
1536 /* Nothing to do. */
1539 VLOG_INFO_RL(&rl, "interface %s: carrier %s",
1540 iface->name, carrier ? "detected" : "dropped");
1541 if (carrier == iface->enabled) {
1542 iface->delay_expires = LLONG_MAX;
1543 VLOG_INFO_RL(&rl, "interface %s: will not be %s",
1544 iface->name, carrier ? "disabled" : "enabled");
1545 } else if (carrier && port->active_iface < 0) {
1546 bond_enable_slave(iface, true);
1547 if (port->updelay) {
1548 VLOG_INFO_RL(&rl, "interface %s: skipping %d ms updelay since no "
1549 "other interface is up", iface->name, port->updelay);
1552 int delay = carrier ? port->updelay : port->downdelay;
1553 iface->delay_expires = time_msec() + delay;
1556 "interface %s: will be %s if it stays %s for %d ms",
1558 carrier ? "enabled" : "disabled",
1559 carrier ? "up" : "down",
1566 bond_choose_active_iface(struct port *port)
1568 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
1570 port->active_iface = bond_choose_iface(port);
1571 port->active_iface_tag = tag_create_random();
1572 if (port->active_iface >= 0) {
1573 VLOG_INFO_RL(&rl, "port %s: active interface is now %s",
1574 port->name, port->ifaces[port->active_iface]->name);
1576 VLOG_WARN_RL(&rl, "port %s: all ports disabled, no active interface",
1582 bond_enable_slave(struct iface *iface, bool enable)
1584 struct port *port = iface->port;
1585 struct bridge *br = port->bridge;
1587 /* This acts as a recursion check. If the act of disabling a slave
1588 * causes a different slave to be enabled, the flag will allow us to
1589 * skip redundant work when we reenter this function. It must be
1590 * cleared on exit to keep things safe with multiple bonds. */
1591 static bool moving_active_iface = false;
1593 iface->delay_expires = LLONG_MAX;
1594 if (enable == iface->enabled) {
1598 iface->enabled = enable;
1599 if (!iface->enabled) {
1600 VLOG_WARN("interface %s: disabled", iface->name);
1601 ofproto_revalidate(br->ofproto, iface->tag);
1602 if (iface->port_ifidx == port->active_iface) {
1603 ofproto_revalidate(br->ofproto,
1604 port->active_iface_tag);
1606 /* Disabling a slave can lead to another slave being immediately
1607 * enabled if there will be no active slaves but one is waiting
1608 * on an updelay. In this case we do not need to run most of the
1609 * code for the newly enabled slave since there was no period
1610 * without an active slave and it is redundant with the disabling
1612 moving_active_iface = true;
1613 bond_choose_active_iface(port);
1615 bond_send_learning_packets(port);
1617 VLOG_WARN("interface %s: enabled", iface->name);
1618 if (port->active_iface < 0 && !moving_active_iface) {
1619 ofproto_revalidate(br->ofproto, port->no_ifaces_tag);
1620 bond_choose_active_iface(port);
1621 bond_send_learning_packets(port);
1623 iface->tag = tag_create_random();
1626 moving_active_iface = false;
1627 port->bond_compat_is_stale = true;
1631 bond_run(struct bridge *br)
1635 for (i = 0; i < br->n_ports; i++) {
1636 struct port *port = br->ports[i];
1638 if (port->n_ifaces >= 2) {
1639 for (j = 0; j < port->n_ifaces; j++) {
1640 struct iface *iface = port->ifaces[j];
1641 if (time_msec() >= iface->delay_expires) {
1642 bond_enable_slave(iface, !iface->enabled);
1647 if (port->bond_compat_is_stale) {
1648 port->bond_compat_is_stale = false;
1649 port_update_bond_compat(port);
1655 bond_wait(struct bridge *br)
1659 for (i = 0; i < br->n_ports; i++) {
1660 struct port *port = br->ports[i];
1661 if (port->n_ifaces < 2) {
1664 for (j = 0; j < port->n_ifaces; j++) {
1665 struct iface *iface = port->ifaces[j];
1666 if (iface->delay_expires != LLONG_MAX) {
1667 poll_timer_wait(iface->delay_expires - time_msec());
1674 set_dst(struct dst *p, const flow_t *flow,
1675 const struct port *in_port, const struct port *out_port,
1680 * XXX This uses too many tags: any broadcast flow will get one tag per
1681 * destination port, and thus a broadcast on a switch of any size is likely
1682 * to have all tag bits set. We should figure out a way to be smarter.
1684 * This is OK when STP is disabled, because stp_state_tag is 0 then. */
1685 *tags |= out_port->stp_state_tag;
1686 if (!(out_port->stp_state & (STP_DISABLED | STP_FORWARDING))) {
1690 p->vlan = (out_port->vlan >= 0 ? OFP_VLAN_NONE
1691 : in_port->vlan >= 0 ? in_port->vlan
1692 : ntohs(flow->dl_vlan));
1693 return choose_output_iface(out_port, flow->dl_src, &p->dp_ifidx, tags);
1697 swap_dst(struct dst *p, struct dst *q)
1699 struct dst tmp = *p;
1704 /* Moves all the dsts with vlan == 'vlan' to the front of the 'n_dsts' in
1705 * 'dsts'. (This may help performance by reducing the number of VLAN changes
1706 * that we push to the datapath. We could in fact fully sort the array by
1707 * vlan, but in most cases there are at most two different vlan tags so that's
1708 * possibly overkill.) */
1710 partition_dsts(struct dst *dsts, size_t n_dsts, int vlan)
1712 struct dst *first = dsts;
1713 struct dst *last = dsts + n_dsts;
1715 while (first != last) {
1717 * - All dsts < first have vlan == 'vlan'.
1718 * - All dsts >= last have vlan != 'vlan'.
1719 * - first < last. */
1720 while (first->vlan == vlan) {
1721 if (++first == last) {
1726 /* Same invariants, plus one additional:
1727 * - first->vlan != vlan.
1729 while (last[-1].vlan != vlan) {
1730 if (--last == first) {
1735 /* Same invariants, plus one additional:
1736 * - last[-1].vlan == vlan.*/
1737 swap_dst(first++, --last);
1742 mirror_mask_ffs(mirror_mask_t mask)
1744 BUILD_ASSERT_DECL(sizeof(unsigned int) >= sizeof(mask));
1749 dst_is_duplicate(const struct dst *dsts, size_t n_dsts,
1750 const struct dst *test)
1753 for (i = 0; i < n_dsts; i++) {
1754 if (dsts[i].vlan == test->vlan && dsts[i].dp_ifidx == test->dp_ifidx) {
1762 port_trunks_vlan(const struct port *port, uint16_t vlan)
1764 return port->vlan < 0 && bitmap_is_set(port->trunks, vlan);
1768 port_includes_vlan(const struct port *port, uint16_t vlan)
1770 return vlan == port->vlan || port_trunks_vlan(port, vlan);
1774 compose_dsts(const struct bridge *br, const flow_t *flow, uint16_t vlan,
1775 const struct port *in_port, const struct port *out_port,
1776 struct dst dsts[], tag_type *tags, uint16_t *nf_output_iface)
1778 mirror_mask_t mirrors = in_port->src_mirrors;
1779 struct dst *dst = dsts;
1782 *tags |= in_port->stp_state_tag;
1783 if (out_port == FLOOD_PORT) {
1784 /* XXX use ODP_FLOOD if no vlans or bonding. */
1785 /* XXX even better, define each VLAN as a datapath port group */
1786 for (i = 0; i < br->n_ports; i++) {
1787 struct port *port = br->ports[i];
1788 if (port != in_port && port_includes_vlan(port, vlan)
1789 && !port->is_mirror_output_port
1790 && set_dst(dst, flow, in_port, port, tags)) {
1791 mirrors |= port->dst_mirrors;
1795 *nf_output_iface = NF_OUT_FLOOD;
1796 } else if (out_port && set_dst(dst, flow, in_port, out_port, tags)) {
1797 *nf_output_iface = dst->dp_ifidx;
1798 mirrors |= out_port->dst_mirrors;
1803 struct mirror *m = br->mirrors[mirror_mask_ffs(mirrors) - 1];
1804 if (!m->n_vlans || vlan_is_mirrored(m, vlan)) {
1806 if (set_dst(dst, flow, in_port, m->out_port, tags)
1807 && !dst_is_duplicate(dsts, dst - dsts, dst)) {
1811 for (i = 0; i < br->n_ports; i++) {
1812 struct port *port = br->ports[i];
1813 if (port_includes_vlan(port, m->out_vlan)
1814 && set_dst(dst, flow, in_port, port, tags))
1818 if (port->vlan < 0) {
1819 dst->vlan = m->out_vlan;
1821 if (dst_is_duplicate(dsts, dst - dsts, dst)) {
1825 /* Use the vlan tag on the original flow instead of
1826 * the one passed in the vlan parameter. This ensures
1827 * that we compare the vlan from before any implicit
1828 * tagging tags place. This is necessary because
1829 * dst->vlan is the final vlan, after removing implicit
1831 flow_vlan = ntohs(flow->dl_vlan);
1832 if (flow_vlan == 0) {
1833 flow_vlan = OFP_VLAN_NONE;
1835 if (port == in_port && dst->vlan == flow_vlan) {
1836 /* Don't send out input port on same VLAN. */
1844 mirrors &= mirrors - 1;
1847 partition_dsts(dsts, dst - dsts, ntohs(flow->dl_vlan));
1852 print_dsts(const struct dst *dsts, size_t n)
1854 for (; n--; dsts++) {
1855 printf(">p%"PRIu16, dsts->dp_ifidx);
1856 if (dsts->vlan != OFP_VLAN_NONE) {
1857 printf("v%"PRIu16, dsts->vlan);
1863 compose_actions(struct bridge *br, const flow_t *flow, uint16_t vlan,
1864 const struct port *in_port, const struct port *out_port,
1865 tag_type *tags, struct odp_actions *actions,
1866 uint16_t *nf_output_iface)
1868 struct dst dsts[DP_MAX_PORTS * (MAX_MIRRORS + 1)];
1870 const struct dst *p;
1873 n_dsts = compose_dsts(br, flow, vlan, in_port, out_port, dsts, tags,
1876 cur_vlan = ntohs(flow->dl_vlan);
1877 for (p = dsts; p < &dsts[n_dsts]; p++) {
1878 union odp_action *a;
1879 if (p->vlan != cur_vlan) {
1880 if (p->vlan == OFP_VLAN_NONE) {
1881 odp_actions_add(actions, ODPAT_STRIP_VLAN);
1883 a = odp_actions_add(actions, ODPAT_SET_VLAN_VID);
1884 a->vlan_vid.vlan_vid = htons(p->vlan);
1888 a = odp_actions_add(actions, ODPAT_OUTPUT);
1889 a->output.port = p->dp_ifidx;
1893 /* Returns the effective vlan of a packet, taking into account both the
1894 * 802.1Q header and implicitly tagged ports. A value of 0 indicates that
1895 * the packet is untagged and -1 indicates it has an invalid header and
1896 * should be dropped. */
1897 static int flow_get_vlan(struct bridge *br, const flow_t *flow,
1898 struct port *in_port, bool have_packet)
1900 /* Note that dl_vlan of 0 and of OFP_VLAN_NONE both mean that the packet
1901 * belongs to VLAN 0, so we should treat both cases identically. (In the
1902 * former case, the packet has an 802.1Q header that specifies VLAN 0,
1903 * presumably to allow a priority to be specified. In the latter case, the
1904 * packet does not have any 802.1Q header.) */
1905 int vlan = ntohs(flow->dl_vlan);
1906 if (vlan == OFP_VLAN_NONE) {
1909 if (in_port->vlan >= 0) {
1911 /* XXX support double tagging? */
1913 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1914 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %"PRIu16" tagged "
1915 "packet received on port %s configured with "
1916 "implicit VLAN %"PRIu16,
1917 br->name, ntohs(flow->dl_vlan),
1918 in_port->name, in_port->vlan);
1922 vlan = in_port->vlan;
1924 if (!port_includes_vlan(in_port, vlan)) {
1926 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1927 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
1928 "packet received on port %s not configured for "
1930 br->name, vlan, in_port->name, vlan);
1940 update_learning_table(struct bridge *br, const flow_t *flow, int vlan,
1941 struct port *in_port)
1943 tag_type rev_tag = mac_learning_learn(br->ml, flow->dl_src,
1944 vlan, in_port->port_idx);
1946 /* The log messages here could actually be useful in debugging,
1947 * so keep the rate limit relatively high. */
1948 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30,
1950 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
1951 "on port %s in VLAN %d",
1952 br->name, ETH_ADDR_ARGS(flow->dl_src),
1953 in_port->name, vlan);
1954 ofproto_revalidate(br->ofproto, rev_tag);
1959 is_bcast_arp_reply(const flow_t *flow)
1961 return (flow->dl_type == htons(ETH_TYPE_ARP)
1962 && flow->nw_proto == ARP_OP_REPLY
1963 && eth_addr_is_broadcast(flow->dl_dst));
1966 /* If the composed actions may be applied to any packet in the given 'flow',
1967 * returns true. Otherwise, the actions should only be applied to 'packet', or
1968 * not at all, if 'packet' was NULL. */
1970 process_flow(struct bridge *br, const flow_t *flow,
1971 const struct ofpbuf *packet, struct odp_actions *actions,
1972 tag_type *tags, uint16_t *nf_output_iface)
1974 struct iface *in_iface;
1975 struct port *in_port;
1976 struct port *out_port = NULL; /* By default, drop the packet/flow. */
1980 /* Find the interface and port structure for the received packet. */
1981 in_iface = iface_from_dp_ifidx(br, flow->in_port);
1983 /* No interface? Something fishy... */
1984 if (packet != NULL) {
1985 /* Odd. A few possible reasons here:
1987 * - We deleted an interface but there are still a few packets
1988 * queued up from it.
1990 * - Someone externally added an interface (e.g. with "ovs-dpctl
1991 * add-if") that we don't know about.
1993 * - Packet arrived on the local port but the local port is not
1994 * one of our bridge ports.
1996 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1998 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
1999 "interface %"PRIu16, br->name, flow->in_port);
2002 /* Return without adding any actions, to drop packets on this flow. */
2005 in_port = in_iface->port;
2006 vlan = flow_get_vlan(br, flow, in_port, !!packet);
2011 /* Drop frames for ports that STP wants entirely killed (both for
2012 * forwarding and for learning). Later, after we do learning, we'll drop
2013 * the frames that STP wants to do learning but not forwarding on. */
2014 if (in_port->stp_state & (STP_LISTENING | STP_BLOCKING)) {
2018 /* Drop frames for reserved multicast addresses. */
2019 if (eth_addr_is_reserved(flow->dl_dst)) {
2023 /* Drop frames on ports reserved for mirroring. */
2024 if (in_port->is_mirror_output_port) {
2025 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2026 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port %s, "
2027 "which is reserved exclusively for mirroring",
2028 br->name, in_port->name);
2032 /* Packets received on bonds need special attention to avoid duplicates. */
2033 if (in_port->n_ifaces > 1) {
2036 if (eth_addr_is_multicast(flow->dl_dst)) {
2037 *tags |= in_port->active_iface_tag;
2038 if (in_port->active_iface != in_iface->port_ifidx) {
2039 /* Drop all multicast packets on inactive slaves. */
2044 /* Drop all packets for which we have learned a different input
2045 * port, because we probably sent the packet on one slave and got
2046 * it back on the other. Broadcast ARP replies are an exception
2047 * to this rule: the host has moved to another switch. */
2048 src_idx = mac_learning_lookup(br->ml, flow->dl_src, vlan);
2049 if (src_idx != -1 && src_idx != in_port->port_idx &&
2050 !is_bcast_arp_reply(flow)) {
2056 out_port = FLOOD_PORT;
2057 /* Learn source MAC (but don't try to learn from revalidation). */
2059 update_learning_table(br, flow, vlan, in_port);
2062 /* Determine output port. */
2063 out_port_idx = mac_learning_lookup_tag(br->ml, flow->dl_dst, vlan,
2065 if (out_port_idx >= 0 && out_port_idx < br->n_ports) {
2066 out_port = br->ports[out_port_idx];
2067 } else if (!packet && !eth_addr_is_multicast(flow->dl_dst)) {
2068 /* If we are revalidating but don't have a learning entry then
2069 * eject the flow. Installing a flow that floods packets opens
2070 * up a window of time where we could learn from a packet reflected
2071 * on a bond and blackhole packets before the learning table is
2072 * updated to reflect the correct port. */
2076 /* Don't send packets out their input ports. Don't forward frames that STP
2077 * wants us to discard. */
2078 if (in_port == out_port || in_port->stp_state == STP_LEARNING) {
2083 compose_actions(br, flow, vlan, in_port, out_port, tags, actions,
2089 /* Careful: 'opp' is in host byte order and opp->port_no is an OFP port
2092 bridge_port_changed_ofhook_cb(enum ofp_port_reason reason,
2093 const struct ofp_phy_port *opp,
2096 struct bridge *br = br_;
2097 struct iface *iface;
2100 iface = iface_from_dp_ifidx(br, ofp_port_to_odp_port(opp->port_no));
2106 if (reason == OFPPR_DELETE) {
2107 VLOG_WARN("bridge %s: interface %s deleted unexpectedly",
2108 br->name, iface->name);
2109 iface_destroy(iface);
2110 if (!port->n_ifaces) {
2111 VLOG_WARN("bridge %s: port %s has no interfaces, dropping",
2112 br->name, port->name);
2118 if (port->n_ifaces > 1) {
2119 bool up = !(opp->state & OFPPS_LINK_DOWN);
2120 bond_link_status_update(iface, up);
2121 port_update_bond_compat(port);
2127 bridge_normal_ofhook_cb(const flow_t *flow, const struct ofpbuf *packet,
2128 struct odp_actions *actions, tag_type *tags,
2129 uint16_t *nf_output_iface, void *br_)
2131 struct bridge *br = br_;
2134 if (flow->dl_type == htons(OFP_DL_TYPE_NOT_ETH_TYPE)
2135 && eth_addr_equals(flow->dl_dst, stp_eth_addr)) {
2136 brstp_receive(br, flow, payload);
2141 COVERAGE_INC(bridge_process_flow);
2142 return process_flow(br, flow, packet, actions, tags, nf_output_iface);
2146 bridge_account_flow_ofhook_cb(const flow_t *flow,
2147 const union odp_action *actions,
2148 size_t n_actions, unsigned long long int n_bytes,
2151 struct bridge *br = br_;
2152 struct port *in_port;
2153 const union odp_action *a;
2155 /* Feed information from the active flows back into the learning table
2156 * to ensure that table is always in sync with what is actually flowing
2157 * through the datapath. */
2158 in_port = port_from_dp_ifidx(br, flow->in_port);
2160 int vlan = flow_get_vlan(br, flow, in_port, false);
2162 update_learning_table(br, flow, vlan, in_port);
2166 if (!br->has_bonded_ports) {
2170 for (a = actions; a < &actions[n_actions]; a++) {
2171 if (a->type == ODPAT_OUTPUT) {
2172 struct port *out_port = port_from_dp_ifidx(br, a->output.port);
2173 if (out_port && out_port->n_ifaces >= 2) {
2174 struct bond_entry *e = lookup_bond_entry(out_port,
2176 e->tx_bytes += n_bytes;
2183 bridge_account_checkpoint_ofhook_cb(void *br_)
2185 struct bridge *br = br_;
2188 if (!br->has_bonded_ports) {
2192 /* The current ofproto implementation calls this callback at least once a
2193 * second, so this timer implementation is sufficient. */
2194 if (time_msec() < br->bond_next_rebalance) {
2197 br->bond_next_rebalance = time_msec() + 10000;
2199 for (i = 0; i < br->n_ports; i++) {
2200 struct port *port = br->ports[i];
2201 if (port->n_ifaces > 1) {
2202 bond_rebalance_port(port);
2207 static struct ofhooks bridge_ofhooks = {
2208 bridge_port_changed_ofhook_cb,
2209 bridge_normal_ofhook_cb,
2210 bridge_account_flow_ofhook_cb,
2211 bridge_account_checkpoint_ofhook_cb,
2214 /* Bonding functions. */
2216 /* Statistics for a single interface on a bonded port, used for load-based
2217 * bond rebalancing. */
2218 struct slave_balance {
2219 struct iface *iface; /* The interface. */
2220 uint64_t tx_bytes; /* Sum of hashes[*]->tx_bytes. */
2222 /* All the "bond_entry"s that are assigned to this interface, in order of
2223 * increasing tx_bytes. */
2224 struct bond_entry **hashes;
2228 /* Sorts pointers to pointers to bond_entries in ascending order by the
2229 * interface to which they are assigned, and within a single interface in
2230 * ascending order of bytes transmitted. */
2232 compare_bond_entries(const void *a_, const void *b_)
2234 const struct bond_entry *const *ap = a_;
2235 const struct bond_entry *const *bp = b_;
2236 const struct bond_entry *a = *ap;
2237 const struct bond_entry *b = *bp;
2238 if (a->iface_idx != b->iface_idx) {
2239 return a->iface_idx > b->iface_idx ? 1 : -1;
2240 } else if (a->tx_bytes != b->tx_bytes) {
2241 return a->tx_bytes > b->tx_bytes ? 1 : -1;
2247 /* Sorts slave_balances so that enabled ports come first, and otherwise in
2248 * *descending* order by number of bytes transmitted. */
2250 compare_slave_balance(const void *a_, const void *b_)
2252 const struct slave_balance *a = a_;
2253 const struct slave_balance *b = b_;
2254 if (a->iface->enabled != b->iface->enabled) {
2255 return a->iface->enabled ? -1 : 1;
2256 } else if (a->tx_bytes != b->tx_bytes) {
2257 return a->tx_bytes > b->tx_bytes ? -1 : 1;
2264 swap_bals(struct slave_balance *a, struct slave_balance *b)
2266 struct slave_balance tmp = *a;
2271 /* Restores the 'n_bals' slave_balance structures in 'bals' to sorted order
2272 * given that 'p' (and only 'p') might be in the wrong location.
2274 * This function invalidates 'p', since it might now be in a different memory
2277 resort_bals(struct slave_balance *p,
2278 struct slave_balance bals[], size_t n_bals)
2281 for (; p > bals && p->tx_bytes > p[-1].tx_bytes; p--) {
2282 swap_bals(p, p - 1);
2284 for (; p < &bals[n_bals - 1] && p->tx_bytes < p[1].tx_bytes; p++) {
2285 swap_bals(p, p + 1);
2291 log_bals(const struct slave_balance *bals, size_t n_bals, struct port *port)
2293 if (VLOG_IS_DBG_ENABLED()) {
2294 struct ds ds = DS_EMPTY_INITIALIZER;
2295 const struct slave_balance *b;
2297 for (b = bals; b < bals + n_bals; b++) {
2301 ds_put_char(&ds, ',');
2303 ds_put_format(&ds, " %s %"PRIu64"kB",
2304 b->iface->name, b->tx_bytes / 1024);
2306 if (!b->iface->enabled) {
2307 ds_put_cstr(&ds, " (disabled)");
2309 if (b->n_hashes > 0) {
2310 ds_put_cstr(&ds, " (");
2311 for (i = 0; i < b->n_hashes; i++) {
2312 const struct bond_entry *e = b->hashes[i];
2314 ds_put_cstr(&ds, " + ");
2316 ds_put_format(&ds, "h%td: %"PRIu64"kB",
2317 e - port->bond_hash, e->tx_bytes / 1024);
2319 ds_put_cstr(&ds, ")");
2322 VLOG_DBG("bond %s:%s", port->name, ds_cstr(&ds));
2327 /* Shifts 'hash' from 'from' to 'to' within 'port'. */
2329 bond_shift_load(struct slave_balance *from, struct slave_balance *to,
2332 struct bond_entry *hash = from->hashes[hash_idx];
2333 struct port *port = from->iface->port;
2334 uint64_t delta = hash->tx_bytes;
2336 VLOG_INFO("bond %s: shift %"PRIu64"kB of load (with hash %td) "
2337 "from %s to %s (now carrying %"PRIu64"kB and "
2338 "%"PRIu64"kB load, respectively)",
2339 port->name, delta / 1024, hash - port->bond_hash,
2340 from->iface->name, to->iface->name,
2341 (from->tx_bytes - delta) / 1024,
2342 (to->tx_bytes + delta) / 1024);
2344 /* Delete element from from->hashes.
2346 * We don't bother to add the element to to->hashes because not only would
2347 * it require more work, the only purpose it would be to allow that hash to
2348 * be migrated to another slave in this rebalancing run, and there is no
2349 * point in doing that. */
2350 if (hash_idx == 0) {
2353 memmove(from->hashes + hash_idx, from->hashes + hash_idx + 1,
2354 (from->n_hashes - (hash_idx + 1)) * sizeof *from->hashes);
2358 /* Shift load away from 'from' to 'to'. */
2359 from->tx_bytes -= delta;
2360 to->tx_bytes += delta;
2362 /* Arrange for flows to be revalidated. */
2363 ofproto_revalidate(port->bridge->ofproto, hash->iface_tag);
2364 hash->iface_idx = to->iface->port_ifidx;
2365 hash->iface_tag = tag_create_random();
2369 bond_rebalance_port(struct port *port)
2371 struct slave_balance bals[DP_MAX_PORTS];
2373 struct bond_entry *hashes[BOND_MASK + 1];
2374 struct slave_balance *b, *from, *to;
2375 struct bond_entry *e;
2378 /* Sets up 'bals' to describe each of the port's interfaces, sorted in
2379 * descending order of tx_bytes, so that bals[0] represents the most
2380 * heavily loaded slave and bals[n_bals - 1] represents the least heavily
2383 * The code is a bit tricky: to avoid dynamically allocating a 'hashes'
2384 * array for each slave_balance structure, we sort our local array of
2385 * hashes in order by slave, so that all of the hashes for a given slave
2386 * become contiguous in memory, and then we point each 'hashes' members of
2387 * a slave_balance structure to the start of a contiguous group. */
2388 n_bals = port->n_ifaces;
2389 for (b = bals; b < &bals[n_bals]; b++) {
2390 b->iface = port->ifaces[b - bals];
2395 for (i = 0; i <= BOND_MASK; i++) {
2396 hashes[i] = &port->bond_hash[i];
2398 qsort(hashes, BOND_MASK + 1, sizeof *hashes, compare_bond_entries);
2399 for (i = 0; i <= BOND_MASK; i++) {
2401 if (e->iface_idx >= 0 && e->iface_idx < port->n_ifaces) {
2402 b = &bals[e->iface_idx];
2403 b->tx_bytes += e->tx_bytes;
2405 b->hashes = &hashes[i];
2410 qsort(bals, n_bals, sizeof *bals, compare_slave_balance);
2411 log_bals(bals, n_bals, port);
2413 /* Discard slaves that aren't enabled (which were sorted to the back of the
2414 * array earlier). */
2415 while (!bals[n_bals - 1].iface->enabled) {
2422 /* Shift load from the most-loaded slaves to the least-loaded slaves. */
2423 to = &bals[n_bals - 1];
2424 for (from = bals; from < to; ) {
2425 uint64_t overload = from->tx_bytes - to->tx_bytes;
2426 if (overload < to->tx_bytes >> 5 || overload < 100000) {
2427 /* The extra load on 'from' (and all less-loaded slaves), compared
2428 * to that of 'to' (the least-loaded slave), is less than ~3%, or
2429 * it is less than ~1Mbps. No point in rebalancing. */
2431 } else if (from->n_hashes == 1) {
2432 /* 'from' only carries a single MAC hash, so we can't shift any
2433 * load away from it, even though we want to. */
2436 /* 'from' is carrying significantly more load than 'to', and that
2437 * load is split across at least two different hashes. Pick a hash
2438 * to migrate to 'to' (the least-loaded slave), given that doing so
2439 * must decrease the ratio of the load on the two slaves by at
2442 * The sort order we use means that we prefer to shift away the
2443 * smallest hashes instead of the biggest ones. There is little
2444 * reason behind this decision; we could use the opposite sort
2445 * order to shift away big hashes ahead of small ones. */
2449 for (i = 0; i < from->n_hashes; i++) {
2450 double old_ratio, new_ratio;
2451 uint64_t delta = from->hashes[i]->tx_bytes;
2453 if (delta == 0 || from->tx_bytes - delta == 0) {
2454 /* Pointless move. */
2458 order_swapped = from->tx_bytes - delta < to->tx_bytes + delta;
2460 if (to->tx_bytes == 0) {
2461 /* Nothing on the new slave, move it. */
2465 old_ratio = (double)from->tx_bytes / to->tx_bytes;
2466 new_ratio = (double)(from->tx_bytes - delta) /
2467 (to->tx_bytes + delta);
2469 if (new_ratio == 0) {
2470 /* Should already be covered but check to prevent division
2475 if (new_ratio < 1) {
2476 new_ratio = 1 / new_ratio;
2479 if (old_ratio - new_ratio > 0.1) {
2480 /* Would decrease the ratio, move it. */
2484 if (i < from->n_hashes) {
2485 bond_shift_load(from, to, i);
2486 port->bond_compat_is_stale = true;
2488 /* If the result of the migration changed the relative order of
2489 * 'from' and 'to' swap them back to maintain invariants. */
2490 if (order_swapped) {
2491 swap_bals(from, to);
2494 /* Re-sort 'bals'. Note that this may make 'from' and 'to'
2495 * point to different slave_balance structures. It is only
2496 * valid to do these two operations in a row at all because we
2497 * know that 'from' will not move past 'to' and vice versa. */
2498 resort_bals(from, bals, n_bals);
2499 resort_bals(to, bals, n_bals);
2506 /* Implement exponentially weighted moving average. A weight of 1/2 causes
2507 * historical data to decay to <1% in 7 rebalancing runs. */
2508 for (e = &port->bond_hash[0]; e <= &port->bond_hash[BOND_MASK]; e++) {
2514 bond_send_learning_packets(struct port *port)
2516 struct bridge *br = port->bridge;
2517 struct mac_entry *e;
2518 struct ofpbuf packet;
2519 int error, n_packets, n_errors;
2521 if (!port->n_ifaces || port->active_iface < 0) {
2525 ofpbuf_init(&packet, 128);
2526 error = n_packets = n_errors = 0;
2527 LIST_FOR_EACH (e, struct mac_entry, lru_node, &br->ml->lrus) {
2528 union ofp_action actions[2], *a;
2534 if (e->port == port->port_idx
2535 || !choose_output_iface(port, e->mac, &dp_ifidx, &tags)) {
2539 /* Compose actions. */
2540 memset(actions, 0, sizeof actions);
2543 a->vlan_vid.type = htons(OFPAT_SET_VLAN_VID);
2544 a->vlan_vid.len = htons(sizeof *a);
2545 a->vlan_vid.vlan_vid = htons(e->vlan);
2548 a->output.type = htons(OFPAT_OUTPUT);
2549 a->output.len = htons(sizeof *a);
2550 a->output.port = htons(odp_port_to_ofp_port(dp_ifidx));
2555 compose_benign_packet(&packet, "Open vSwitch Bond Failover", 0xf177,
2557 flow_extract(&packet, ODPP_NONE, &flow);
2558 retval = ofproto_send_packet(br->ofproto, &flow, actions, a - actions,
2565 ofpbuf_uninit(&packet);
2568 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2569 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
2570 "packets, last error was: %s",
2571 port->name, n_errors, n_packets, strerror(error));
2573 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
2574 port->name, n_packets);
2578 /* Bonding unixctl user interface functions. */
2581 bond_unixctl_list(struct unixctl_conn *conn, const char *args UNUSED)
2583 struct ds ds = DS_EMPTY_INITIALIZER;
2584 const struct bridge *br;
2586 ds_put_cstr(&ds, "bridge\tbond\tslaves\n");
2588 LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
2591 for (i = 0; i < br->n_ports; i++) {
2592 const struct port *port = br->ports[i];
2593 if (port->n_ifaces > 1) {
2596 ds_put_format(&ds, "%s\t%s\t", br->name, port->name);
2597 for (j = 0; j < port->n_ifaces; j++) {
2598 const struct iface *iface = port->ifaces[j];
2600 ds_put_cstr(&ds, ", ");
2602 ds_put_cstr(&ds, iface->name);
2604 ds_put_char(&ds, '\n');
2608 unixctl_command_reply(conn, 200, ds_cstr(&ds));
2612 static struct port *
2613 bond_find(const char *name)
2615 const struct bridge *br;
2617 LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
2620 for (i = 0; i < br->n_ports; i++) {
2621 struct port *port = br->ports[i];
2622 if (!strcmp(port->name, name) && port->n_ifaces > 1) {
2631 bond_unixctl_show(struct unixctl_conn *conn, const char *args)
2633 struct ds ds = DS_EMPTY_INITIALIZER;
2634 const struct port *port;
2637 port = bond_find(args);
2639 unixctl_command_reply(conn, 501, "no such bond");
2643 ds_put_format(&ds, "updelay: %d ms\n", port->updelay);
2644 ds_put_format(&ds, "downdelay: %d ms\n", port->downdelay);
2645 ds_put_format(&ds, "next rebalance: %lld ms\n",
2646 port->bridge->bond_next_rebalance - time_msec());
2647 for (j = 0; j < port->n_ifaces; j++) {
2648 const struct iface *iface = port->ifaces[j];
2649 struct bond_entry *be;
2652 ds_put_format(&ds, "slave %s: %s\n",
2653 iface->name, iface->enabled ? "enabled" : "disabled");
2654 if (j == port->active_iface) {
2655 ds_put_cstr(&ds, "\tactive slave\n");
2657 if (iface->delay_expires != LLONG_MAX) {
2658 ds_put_format(&ds, "\t%s expires in %lld ms\n",
2659 iface->enabled ? "downdelay" : "updelay",
2660 iface->delay_expires - time_msec());
2664 for (be = port->bond_hash; be <= &port->bond_hash[BOND_MASK]; be++) {
2665 int hash = be - port->bond_hash;
2666 struct mac_entry *me;
2668 if (be->iface_idx != j) {
2672 ds_put_format(&ds, "\thash %d: %"PRIu64" kB load\n",
2673 hash, be->tx_bytes / 1024);
2676 LIST_FOR_EACH (me, struct mac_entry, lru_node,
2677 &port->bridge->ml->lrus) {
2680 if (bond_hash(me->mac) == hash
2681 && me->port != port->port_idx
2682 && choose_output_iface(port, me->mac, &dp_ifidx, &tags)
2683 && dp_ifidx == iface->dp_ifidx)
2685 ds_put_format(&ds, "\t\t"ETH_ADDR_FMT"\n",
2686 ETH_ADDR_ARGS(me->mac));
2691 unixctl_command_reply(conn, 200, ds_cstr(&ds));
2696 bond_unixctl_migrate(struct unixctl_conn *conn, const char *args_)
2698 char *args = (char *) args_;
2699 char *save_ptr = NULL;
2700 char *bond_s, *hash_s, *slave_s;
2701 uint8_t mac[ETH_ADDR_LEN];
2703 struct iface *iface;
2704 struct bond_entry *entry;
2707 bond_s = strtok_r(args, " ", &save_ptr);
2708 hash_s = strtok_r(NULL, " ", &save_ptr);
2709 slave_s = strtok_r(NULL, " ", &save_ptr);
2711 unixctl_command_reply(conn, 501,
2712 "usage: bond/migrate BOND HASH SLAVE");
2716 port = bond_find(bond_s);
2718 unixctl_command_reply(conn, 501, "no such bond");
2722 if (sscanf(hash_s, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))
2723 == ETH_ADDR_SCAN_COUNT) {
2724 hash = bond_hash(mac);
2725 } else if (strspn(hash_s, "0123456789") == strlen(hash_s)) {
2726 hash = atoi(hash_s) & BOND_MASK;
2728 unixctl_command_reply(conn, 501, "bad hash");
2732 iface = port_lookup_iface(port, slave_s);
2734 unixctl_command_reply(conn, 501, "no such slave");
2738 if (!iface->enabled) {
2739 unixctl_command_reply(conn, 501, "cannot migrate to disabled slave");
2743 entry = &port->bond_hash[hash];
2744 ofproto_revalidate(port->bridge->ofproto, entry->iface_tag);
2745 entry->iface_idx = iface->port_ifidx;
2746 entry->iface_tag = tag_create_random();
2747 port->bond_compat_is_stale = true;
2748 unixctl_command_reply(conn, 200, "migrated");
2752 bond_unixctl_set_active_slave(struct unixctl_conn *conn, const char *args_)
2754 char *args = (char *) args_;
2755 char *save_ptr = NULL;
2756 char *bond_s, *slave_s;
2758 struct iface *iface;
2760 bond_s = strtok_r(args, " ", &save_ptr);
2761 slave_s = strtok_r(NULL, " ", &save_ptr);
2763 unixctl_command_reply(conn, 501,
2764 "usage: bond/set-active-slave BOND SLAVE");
2768 port = bond_find(bond_s);
2770 unixctl_command_reply(conn, 501, "no such bond");
2774 iface = port_lookup_iface(port, slave_s);
2776 unixctl_command_reply(conn, 501, "no such slave");
2780 if (!iface->enabled) {
2781 unixctl_command_reply(conn, 501, "cannot make disabled slave active");
2785 if (port->active_iface != iface->port_ifidx) {
2786 ofproto_revalidate(port->bridge->ofproto, port->active_iface_tag);
2787 port->active_iface = iface->port_ifidx;
2788 port->active_iface_tag = tag_create_random();
2789 VLOG_INFO("port %s: active interface is now %s",
2790 port->name, iface->name);
2791 bond_send_learning_packets(port);
2792 unixctl_command_reply(conn, 200, "done");
2794 unixctl_command_reply(conn, 200, "no change");
2799 enable_slave(struct unixctl_conn *conn, const char *args_, bool enable)
2801 char *args = (char *) args_;
2802 char *save_ptr = NULL;
2803 char *bond_s, *slave_s;
2805 struct iface *iface;
2807 bond_s = strtok_r(args, " ", &save_ptr);
2808 slave_s = strtok_r(NULL, " ", &save_ptr);
2810 unixctl_command_reply(conn, 501,
2811 "usage: bond/enable/disable-slave BOND SLAVE");
2815 port = bond_find(bond_s);
2817 unixctl_command_reply(conn, 501, "no such bond");
2821 iface = port_lookup_iface(port, slave_s);
2823 unixctl_command_reply(conn, 501, "no such slave");
2827 bond_enable_slave(iface, enable);
2828 unixctl_command_reply(conn, 501, enable ? "enabled" : "disabled");
2832 bond_unixctl_enable_slave(struct unixctl_conn *conn, const char *args)
2834 enable_slave(conn, args, true);
2838 bond_unixctl_disable_slave(struct unixctl_conn *conn, const char *args)
2840 enable_slave(conn, args, false);
2844 bond_unixctl_hash(struct unixctl_conn *conn, const char *args)
2846 uint8_t mac[ETH_ADDR_LEN];
2850 if (sscanf(args, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))
2851 == ETH_ADDR_SCAN_COUNT) {
2852 hash = bond_hash(mac);
2854 hash_cstr = xasprintf("%u", hash);
2855 unixctl_command_reply(conn, 200, hash_cstr);
2858 unixctl_command_reply(conn, 501, "invalid mac");
2865 unixctl_command_register("bond/list", bond_unixctl_list);
2866 unixctl_command_register("bond/show", bond_unixctl_show);
2867 unixctl_command_register("bond/migrate", bond_unixctl_migrate);
2868 unixctl_command_register("bond/set-active-slave",
2869 bond_unixctl_set_active_slave);
2870 unixctl_command_register("bond/enable-slave", bond_unixctl_enable_slave);
2871 unixctl_command_register("bond/disable-slave", bond_unixctl_disable_slave);
2872 unixctl_command_register("bond/hash", bond_unixctl_hash);
2875 /* Port functions. */
2878 port_create(struct bridge *br, const char *name)
2882 port = xcalloc(1, sizeof *port);
2884 port->port_idx = br->n_ports;
2886 port->trunks = NULL;
2887 port->name = xstrdup(name);
2888 port->active_iface = -1;
2889 port->stp_state = STP_DISABLED;
2890 port->stp_state_tag = 0;
2892 if (br->n_ports >= br->allocated_ports) {
2893 br->ports = x2nrealloc(br->ports, &br->allocated_ports,
2896 br->ports[br->n_ports++] = port;
2898 VLOG_INFO("created port %s on bridge %s", port->name, br->name);
2903 port_reconfigure(struct port *port)
2905 bool bonded = cfg_has_section("bonding.%s", port->name);
2906 struct svec old_ifaces, new_ifaces;
2907 unsigned long *trunks;
2911 /* Collect old and new interfaces. */
2912 svec_init(&old_ifaces);
2913 svec_init(&new_ifaces);
2914 for (i = 0; i < port->n_ifaces; i++) {
2915 svec_add(&old_ifaces, port->ifaces[i]->name);
2917 svec_sort(&old_ifaces);
2919 cfg_get_all_keys(&new_ifaces, "bonding.%s.slave", port->name);
2920 if (!new_ifaces.n) {
2921 VLOG_ERR("port %s: no interfaces specified for bonded port",
2923 } else if (new_ifaces.n == 1) {
2924 VLOG_WARN("port %s: only 1 interface specified for bonded port",
2928 port->updelay = cfg_get_int(0, "bonding.%s.updelay", port->name);
2929 if (port->updelay < 0) {
2932 port->downdelay = cfg_get_int(0, "bonding.%s.downdelay", port->name);
2933 if (port->downdelay < 0) {
2934 port->downdelay = 0;
2937 svec_init(&new_ifaces);
2938 svec_add(&new_ifaces, port->name);
2941 /* Get rid of deleted interfaces and add new interfaces. */
2942 for (i = 0; i < port->n_ifaces; i++) {
2943 struct iface *iface = port->ifaces[i];
2944 if (!svec_contains(&new_ifaces, iface->name)) {
2945 iface_destroy(iface);
2950 for (i = 0; i < new_ifaces.n; i++) {
2951 const char *name = new_ifaces.names[i];
2952 if (!svec_contains(&old_ifaces, name)) {
2953 iface_create(port, name);
2959 if (cfg_has("vlan.%s.tag", port->name)) {
2961 vlan = cfg_get_vlan(0, "vlan.%s.tag", port->name);
2962 if (vlan >= 0 && vlan <= 4095) {
2963 VLOG_DBG("port %s: assigning VLAN tag %d", port->name, vlan);
2966 /* It's possible that bonded, VLAN-tagged ports make sense. Maybe
2967 * they even work as-is. But they have not been tested. */
2968 VLOG_WARN("port %s: VLAN tags not supported on bonded ports",
2972 if (port->vlan != vlan) {
2974 bridge_flush(port->bridge);
2977 /* Get trunked VLANs. */
2980 size_t n_trunks, n_errors;
2983 trunks = bitmap_allocate(4096);
2984 n_trunks = cfg_count("vlan.%s.trunks", port->name);
2986 for (i = 0; i < n_trunks; i++) {
2987 int trunk = cfg_get_vlan(i, "vlan.%s.trunks", port->name);
2989 bitmap_set1(trunks, trunk);
2995 VLOG_ERR("port %s: invalid values for %zu trunk VLANs",
2996 port->name, n_trunks);
2998 if (n_errors == n_trunks) {
3000 VLOG_ERR("port %s: no valid trunks, trunking all VLANs",
3003 bitmap_set_multiple(trunks, 0, 4096, 1);
3006 if (cfg_has("vlan.%s.trunks", port->name)) {
3007 VLOG_ERR("ignoring vlan.%s.trunks in favor of vlan.%s.vlan",
3008 port->name, port->name);
3012 ? port->trunks != NULL
3013 : port->trunks == NULL || !bitmap_equal(trunks, port->trunks, 4096)) {
3014 bridge_flush(port->bridge);
3016 bitmap_free(port->trunks);
3017 port->trunks = trunks;
3019 svec_destroy(&old_ifaces);
3020 svec_destroy(&new_ifaces);
3024 port_destroy(struct port *port)
3027 struct bridge *br = port->bridge;
3031 proc_net_compat_update_vlan(port->name, NULL, 0);
3032 proc_net_compat_update_bond(port->name, NULL);
3034 for (i = 0; i < MAX_MIRRORS; i++) {
3035 struct mirror *m = br->mirrors[i];
3036 if (m && m->out_port == port) {
3041 while (port->n_ifaces > 0) {
3042 iface_destroy(port->ifaces[port->n_ifaces - 1]);
3045 del = br->ports[port->port_idx] = br->ports[--br->n_ports];
3046 del->port_idx = port->port_idx;
3049 bitmap_free(port->trunks);
3056 static struct port *
3057 port_from_dp_ifidx(const struct bridge *br, uint16_t dp_ifidx)
3059 struct iface *iface = iface_from_dp_ifidx(br, dp_ifidx);
3060 return iface ? iface->port : NULL;
3063 static struct port *
3064 port_lookup(const struct bridge *br, const char *name)
3068 for (i = 0; i < br->n_ports; i++) {
3069 struct port *port = br->ports[i];
3070 if (!strcmp(port->name, name)) {
3077 static struct iface *
3078 port_lookup_iface(const struct port *port, const char *name)
3082 for (j = 0; j < port->n_ifaces; j++) {
3083 struct iface *iface = port->ifaces[j];
3084 if (!strcmp(iface->name, name)) {
3092 port_update_bonding(struct port *port)
3094 if (port->n_ifaces < 2) {
3095 /* Not a bonded port. */
3096 if (port->bond_hash) {
3097 free(port->bond_hash);
3098 port->bond_hash = NULL;
3099 port->bond_compat_is_stale = true;
3102 if (!port->bond_hash) {
3105 port->bond_hash = xcalloc(BOND_MASK + 1, sizeof *port->bond_hash);
3106 for (i = 0; i <= BOND_MASK; i++) {
3107 struct bond_entry *e = &port->bond_hash[i];
3111 port->no_ifaces_tag = tag_create_random();
3112 bond_choose_active_iface(port);
3114 port->bond_compat_is_stale = true;
3119 port_update_bond_compat(struct port *port)
3121 struct compat_bond_hash compat_hashes[BOND_MASK + 1];
3122 struct compat_bond bond;
3125 if (port->n_ifaces < 2) {
3126 proc_net_compat_update_bond(port->name, NULL);
3131 bond.updelay = port->updelay;
3132 bond.downdelay = port->downdelay;
3135 bond.hashes = compat_hashes;
3136 if (port->bond_hash) {
3137 const struct bond_entry *e;
3138 for (e = port->bond_hash; e <= &port->bond_hash[BOND_MASK]; e++) {
3139 if (e->iface_idx >= 0 && e->iface_idx < port->n_ifaces) {
3140 struct compat_bond_hash *cbh = &bond.hashes[bond.n_hashes++];
3141 cbh->hash = e - port->bond_hash;
3142 cbh->netdev_name = port->ifaces[e->iface_idx]->name;
3147 bond.n_slaves = port->n_ifaces;
3148 bond.slaves = xmalloc(port->n_ifaces * sizeof *bond.slaves);
3149 for (i = 0; i < port->n_ifaces; i++) {
3150 struct iface *iface = port->ifaces[i];
3151 struct compat_bond_slave *slave = &bond.slaves[i];
3152 slave->name = iface->name;
3154 /* We need to make the same determination as the Linux bonding
3155 * code to determine whether a slave should be consider "up".
3156 * The Linux function bond_miimon_inspect() supports four
3157 * BOND_LINK_* states:
3159 * - BOND_LINK_UP: carrier detected, updelay has passed.
3160 * - BOND_LINK_FAIL: carrier lost, downdelay in progress.
3161 * - BOND_LINK_DOWN: carrier lost, downdelay has passed.
3162 * - BOND_LINK_BACK: carrier detected, updelay in progress.
3164 * The function bond_info_show_slave() only considers BOND_LINK_UP
3165 * to be "up" and anything else to be "down".
3167 slave->up = iface->enabled && iface->delay_expires == LLONG_MAX;
3171 netdev_get_etheraddr(iface->netdev, slave->mac);
3174 if (cfg_get_bool(0, "bonding.%s.fake-iface", port->name)) {
3175 struct netdev *bond_netdev;
3177 if (!netdev_open(port->name, NETDEV_ETH_TYPE_NONE, &bond_netdev)) {
3179 netdev_turn_flags_on(bond_netdev, NETDEV_UP, true);
3181 netdev_turn_flags_off(bond_netdev, NETDEV_UP, true);
3183 netdev_close(bond_netdev);
3187 proc_net_compat_update_bond(port->name, &bond);
3192 port_update_vlan_compat(struct port *port)
3194 struct bridge *br = port->bridge;
3195 char *vlandev_name = NULL;
3197 if (port->vlan > 0) {
3198 /* Figure out the name that the VLAN device should actually have, if it
3199 * existed. This takes some work because the VLAN device would not
3200 * have port->name in its name; rather, it would have the trunk port's
3201 * name, and 'port' would be attached to a bridge that also had the
3202 * VLAN device one of its ports. So we need to find a trunk port that
3203 * includes port->vlan.
3205 * There might be more than one candidate. This doesn't happen on
3206 * XenServer, so if it happens we just pick the first choice in
3207 * alphabetical order instead of creating multiple VLAN devices. */
3209 for (i = 0; i < br->n_ports; i++) {
3210 struct port *p = br->ports[i];
3211 if (port_trunks_vlan(p, port->vlan)
3213 && (!vlandev_name || strcmp(p->name, vlandev_name) <= 0))
3215 uint8_t ea[ETH_ADDR_LEN];
3216 netdev_get_etheraddr(p->ifaces[0]->netdev, ea);
3217 if (!eth_addr_is_multicast(ea) &&
3218 !eth_addr_is_reserved(ea) &&
3219 !eth_addr_is_zero(ea)) {
3220 vlandev_name = p->name;
3225 proc_net_compat_update_vlan(port->name, vlandev_name, port->vlan);
3228 /* Interface functions. */
3231 iface_create(struct port *port, const char *name)
3233 struct iface *iface;
3235 iface = xcalloc(1, sizeof *iface);
3237 iface->port_ifidx = port->n_ifaces;
3238 iface->name = xstrdup(name);
3239 iface->dp_ifidx = -1;
3240 iface->tag = tag_create_random();
3241 iface->delay_expires = LLONG_MAX;
3242 iface->netdev = NULL;
3244 if (port->n_ifaces >= port->allocated_ifaces) {
3245 port->ifaces = x2nrealloc(port->ifaces, &port->allocated_ifaces,
3246 sizeof *port->ifaces);
3248 port->ifaces[port->n_ifaces++] = iface;
3249 if (port->n_ifaces > 1) {
3250 port->bridge->has_bonded_ports = true;
3253 VLOG_DBG("attached network device %s to port %s", iface->name, port->name);
3255 bridge_flush(port->bridge);
3259 iface_destroy(struct iface *iface)
3262 struct port *port = iface->port;
3263 struct bridge *br = port->bridge;
3264 bool del_active = port->active_iface == iface->port_ifidx;
3267 if (iface->dp_ifidx >= 0) {
3268 port_array_set(&br->ifaces, iface->dp_ifidx, NULL);
3271 del = port->ifaces[iface->port_ifidx] = port->ifaces[--port->n_ifaces];
3272 del->port_ifidx = iface->port_ifidx;
3274 netdev_close(iface->netdev);
3279 ofproto_revalidate(port->bridge->ofproto, port->active_iface_tag);
3280 bond_choose_active_iface(port);
3281 bond_send_learning_packets(port);
3284 bridge_flush(port->bridge);
3288 static struct iface *
3289 iface_lookup(const struct bridge *br, const char *name)
3293 for (i = 0; i < br->n_ports; i++) {
3294 struct port *port = br->ports[i];
3295 for (j = 0; j < port->n_ifaces; j++) {
3296 struct iface *iface = port->ifaces[j];
3297 if (!strcmp(iface->name, name)) {
3305 static struct iface *
3306 iface_from_dp_ifidx(const struct bridge *br, uint16_t dp_ifidx)
3308 return port_array_get(&br->ifaces, dp_ifidx);
3311 /* Returns true if 'iface' is the name of an "internal" interface on bridge
3312 * 'br', that is, an interface that is entirely simulated within the datapath.
3313 * The local port (ODPP_LOCAL) is always an internal interface. Other local
3314 * interfaces are created by setting "iface.<iface>.internal = true".
3316 * In addition, we have a kluge-y feature that creates an internal port with
3317 * the name of a bonded port if "bonding.<bondname>.fake-iface = true" is set.
3318 * This feature needs to go away in the long term. Until then, this is one
3319 * reason why this function takes a name instead of a struct iface: the fake
3320 * interfaces created this way do not have a struct iface. */
3322 iface_is_internal(const struct bridge *br, const char *iface)
3324 if (!strcmp(iface, br->name)
3325 || cfg_get_bool(0, "iface.%s.internal", iface)) {
3329 if (cfg_get_bool(0, "bonding.%s.fake-iface", iface)) {
3330 struct port *port = port_lookup(br, iface);
3331 if (port && port->n_ifaces > 1) {
3339 /* Set Ethernet address of 'iface', if one is specified in the configuration
3342 iface_set_mac(struct iface *iface)
3344 uint64_t mac = cfg_get_mac(0, "iface.%s.mac", iface->name);
3346 static uint8_t ea[ETH_ADDR_LEN];
3348 eth_addr_from_uint64(mac, ea);
3349 if (eth_addr_is_multicast(ea)) {
3350 VLOG_ERR("interface %s: cannot set MAC to multicast address",
3352 } else if (iface->dp_ifidx == ODPP_LOCAL) {
3353 VLOG_ERR("ignoring iface.%s.mac; use bridge.%s.mac instead",
3354 iface->name, iface->name);
3356 int error = netdev_set_etheraddr(iface->netdev, ea);
3358 VLOG_ERR("interface %s: setting MAC failed (%s)",
3359 iface->name, strerror(error));
3365 /* Port mirroring. */
3368 mirror_reconfigure(struct bridge *br)
3370 struct svec old_mirrors, new_mirrors;
3371 size_t i, n_rspan_vlans;
3372 unsigned long *rspan_vlans;
3374 /* Collect old and new mirrors. */
3375 svec_init(&old_mirrors);
3376 svec_init(&new_mirrors);
3377 cfg_get_subsections(&new_mirrors, "mirror.%s", br->name);
3378 for (i = 0; i < MAX_MIRRORS; i++) {
3379 if (br->mirrors[i]) {
3380 svec_add(&old_mirrors, br->mirrors[i]->name);
3384 /* Get rid of deleted mirrors and add new mirrors. */
3385 svec_sort(&old_mirrors);
3386 assert(svec_is_unique(&old_mirrors));
3387 svec_sort(&new_mirrors);
3388 assert(svec_is_unique(&new_mirrors));
3389 for (i = 0; i < MAX_MIRRORS; i++) {
3390 struct mirror *m = br->mirrors[i];
3391 if (m && !svec_contains(&new_mirrors, m->name)) {
3395 for (i = 0; i < new_mirrors.n; i++) {
3396 const char *name = new_mirrors.names[i];
3397 if (!svec_contains(&old_mirrors, name)) {
3398 mirror_create(br, name);
3401 svec_destroy(&old_mirrors);
3402 svec_destroy(&new_mirrors);
3404 /* Reconfigure all mirrors. */
3405 for (i = 0; i < MAX_MIRRORS; i++) {
3406 if (br->mirrors[i]) {
3407 mirror_reconfigure_one(br->mirrors[i]);
3411 /* Update port reserved status. */
3412 for (i = 0; i < br->n_ports; i++) {
3413 br->ports[i]->is_mirror_output_port = false;
3415 for (i = 0; i < MAX_MIRRORS; i++) {
3416 struct mirror *m = br->mirrors[i];
3417 if (m && m->out_port) {
3418 m->out_port->is_mirror_output_port = true;
3422 /* Update learning disabled vlans (for RSPAN). */
3424 n_rspan_vlans = cfg_count("vlan.%s.disable-learning", br->name);
3425 if (n_rspan_vlans) {
3426 rspan_vlans = bitmap_allocate(4096);
3428 for (i = 0; i < n_rspan_vlans; i++) {
3429 int vlan = cfg_get_vlan(i, "vlan.%s.disable-learning", br->name);
3431 bitmap_set1(rspan_vlans, vlan);
3432 VLOG_INFO("bridge %s: disabling learning on vlan %d\n",
3435 VLOG_ERR("bridge %s: invalid value '%s' for learning disabled "
3437 cfg_get_string(i, "vlan.%s.disable-learning", br->name));
3441 if (mac_learning_set_disabled_vlans(br->ml, rspan_vlans)) {
3447 mirror_create(struct bridge *br, const char *name)
3452 for (i = 0; ; i++) {
3453 if (i >= MAX_MIRRORS) {
3454 VLOG_WARN("bridge %s: maximum of %d port mirrors reached, "
3455 "cannot create %s", br->name, MAX_MIRRORS, name);
3458 if (!br->mirrors[i]) {
3463 VLOG_INFO("created port mirror %s on bridge %s", name, br->name);
3466 br->mirrors[i] = m = xcalloc(1, sizeof *m);
3469 m->name = xstrdup(name);
3470 svec_init(&m->src_ports);
3471 svec_init(&m->dst_ports);
3479 mirror_destroy(struct mirror *m)
3482 struct bridge *br = m->bridge;
3485 for (i = 0; i < br->n_ports; i++) {
3486 br->ports[i]->src_mirrors &= ~(MIRROR_MASK_C(1) << m->idx);
3487 br->ports[i]->dst_mirrors &= ~(MIRROR_MASK_C(1) << m->idx);
3490 svec_destroy(&m->src_ports);
3491 svec_destroy(&m->dst_ports);
3494 m->bridge->mirrors[m->idx] = NULL;
3502 prune_ports(struct mirror *m, struct svec *ports)
3507 svec_sort_unique(ports);
3510 for (i = 0; i < ports->n; i++) {
3511 const char *name = ports->names[i];
3512 if (port_lookup(m->bridge, name)) {
3513 svec_add(&tmp, name);
3515 VLOG_WARN("mirror.%s.%s: cannot match on nonexistent port %s",
3516 m->bridge->name, m->name, name);
3519 svec_swap(ports, &tmp);
3524 prune_vlans(struct mirror *m, struct svec *vlan_strings, int **vlans)
3528 /* This isn't perfect: it won't combine "0" and "00", and the textual sort
3529 * order won't give us numeric sort order. But that's good enough for what
3530 * we need right now. */
3531 svec_sort_unique(vlan_strings);
3533 *vlans = xmalloc(sizeof *vlans * vlan_strings->n);
3535 for (i = 0; i < vlan_strings->n; i++) {
3536 const char *name = vlan_strings->names[i];
3538 if (!str_to_int(name, 10, &vlan) || vlan < 0 || vlan > 4095) {
3539 VLOG_WARN("mirror.%s.%s.select.vlan: ignoring invalid VLAN %s",
3540 m->bridge->name, m->name, name);
3542 (*vlans)[n_vlans++] = vlan;
3549 vlan_is_mirrored(const struct mirror *m, int vlan)
3553 for (i = 0; i < m->n_vlans; i++) {
3554 if (m->vlans[i] == vlan) {
3562 port_trunks_any_mirrored_vlan(const struct mirror *m, const struct port *p)
3566 for (i = 0; i < m->n_vlans; i++) {
3567 if (port_trunks_vlan(p, m->vlans[i])) {
3575 mirror_reconfigure_one(struct mirror *m)
3577 char *pfx = xasprintf("mirror.%s.%s", m->bridge->name, m->name);
3578 struct svec src_ports, dst_ports, ports;
3579 struct svec vlan_strings;
3580 mirror_mask_t mirror_bit;
3581 const char *out_port_name;
3582 struct port *out_port;
3587 bool mirror_all_ports;
3588 bool any_ports_specified;
3590 /* Get output port. */
3591 out_port_name = cfg_get_key(0, "mirror.%s.%s.output.port",
3592 m->bridge->name, m->name);
3593 if (out_port_name) {
3594 out_port = port_lookup(m->bridge, out_port_name);
3596 VLOG_ERR("%s.output.port: bridge %s does not have a port "
3597 "named %s", pfx, m->bridge->name, out_port_name);
3604 if (cfg_has("%s.output.vlan", pfx)) {
3605 VLOG_ERR("%s.output.port and %s.output.vlan both specified; "
3606 "ignoring %s.output.vlan", pfx, pfx, pfx);
3608 } else if (cfg_has("%s.output.vlan", pfx)) {
3610 out_vlan = cfg_get_vlan(0, "%s.output.vlan", pfx);
3612 VLOG_ERR("%s: neither %s.output.port nor %s.output.vlan specified, "
3613 "but exactly one is required; disabling port mirror %s",
3614 pfx, pfx, pfx, pfx);
3620 /* Get all the ports, and drop duplicates and ports that don't exist. */
3621 svec_init(&src_ports);
3622 svec_init(&dst_ports);
3624 cfg_get_all_keys(&src_ports, "%s.select.src-port", pfx);
3625 cfg_get_all_keys(&dst_ports, "%s.select.dst-port", pfx);
3626 cfg_get_all_keys(&ports, "%s.select.port", pfx);
3627 any_ports_specified = src_ports.n || dst_ports.n || ports.n;
3628 svec_append(&src_ports, &ports);
3629 svec_append(&dst_ports, &ports);
3630 svec_destroy(&ports);
3631 prune_ports(m, &src_ports);
3632 prune_ports(m, &dst_ports);
3633 if (any_ports_specified && !src_ports.n && !dst_ports.n) {
3634 VLOG_ERR("%s: none of the specified ports exist; "
3635 "disabling port mirror %s", pfx, pfx);
3640 /* Get all the vlans, and drop duplicate and invalid vlans. */
3641 svec_init(&vlan_strings);
3642 cfg_get_all_keys(&vlan_strings, "%s.select.vlan", pfx);
3643 n_vlans = prune_vlans(m, &vlan_strings, &vlans);
3644 svec_destroy(&vlan_strings);
3646 /* Update mirror data. */
3647 if (!svec_equal(&m->src_ports, &src_ports)
3648 || !svec_equal(&m->dst_ports, &dst_ports)
3649 || m->n_vlans != n_vlans
3650 || memcmp(m->vlans, vlans, sizeof *vlans * n_vlans)
3651 || m->out_port != out_port
3652 || m->out_vlan != out_vlan) {
3653 bridge_flush(m->bridge);
3655 svec_swap(&m->src_ports, &src_ports);
3656 svec_swap(&m->dst_ports, &dst_ports);
3659 m->n_vlans = n_vlans;
3660 m->out_port = out_port;
3661 m->out_vlan = out_vlan;
3663 /* If no selection criteria have been given, mirror for all ports. */
3664 mirror_all_ports = (!m->src_ports.n) && (!m->dst_ports.n) && (!m->n_vlans);
3667 mirror_bit = MIRROR_MASK_C(1) << m->idx;
3668 for (i = 0; i < m->bridge->n_ports; i++) {
3669 struct port *port = m->bridge->ports[i];
3671 if (mirror_all_ports
3672 || svec_contains(&m->src_ports, port->name)
3675 ? port_trunks_any_mirrored_vlan(m, port)
3676 : vlan_is_mirrored(m, port->vlan)))) {
3677 port->src_mirrors |= mirror_bit;
3679 port->src_mirrors &= ~mirror_bit;
3682 if (mirror_all_ports || svec_contains(&m->dst_ports, port->name)) {
3683 port->dst_mirrors |= mirror_bit;
3685 port->dst_mirrors &= ~mirror_bit;
3691 svec_destroy(&src_ports);
3692 svec_destroy(&dst_ports);
3696 /* Spanning tree protocol. */
3698 static void brstp_update_port_state(struct port *);
3701 brstp_send_bpdu(struct ofpbuf *pkt, int port_no, void *br_)
3703 struct bridge *br = br_;
3704 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3705 struct iface *iface = iface_from_dp_ifidx(br, port_no);
3707 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d",
3710 struct eth_header *eth = pkt->l2;
3712 netdev_get_etheraddr(iface->netdev, eth->eth_src);
3713 if (eth_addr_is_zero(eth->eth_src)) {
3714 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d "
3715 "with unknown MAC", br->name, port_no);
3717 union ofp_action action;
3720 memset(&action, 0, sizeof action);
3721 action.type = htons(OFPAT_OUTPUT);
3722 action.output.len = htons(sizeof action);
3723 action.output.port = htons(port_no);
3725 flow_extract(pkt, ODPP_NONE, &flow);
3726 ofproto_send_packet(br->ofproto, &flow, &action, 1, pkt);
3733 brstp_reconfigure(struct bridge *br)
3737 if (!cfg_get_bool(0, "stp.%s.enabled", br->name)) {
3739 stp_destroy(br->stp);
3745 uint64_t bridge_address, bridge_id;
3746 int bridge_priority;
3748 bridge_address = cfg_get_mac(0, "stp.%s.address", br->name);
3749 if (!bridge_address) {
3751 bridge_address = (stp_get_bridge_id(br->stp)
3752 & ((UINT64_C(1) << 48) - 1));
3754 uint8_t mac[ETH_ADDR_LEN];
3755 eth_addr_random(mac);
3756 bridge_address = eth_addr_to_uint64(mac);
3760 if (cfg_is_valid(CFG_INT | CFG_REQUIRED, "stp.%s.priority",
3762 bridge_priority = cfg_get_int(0, "stp.%s.priority", br->name);
3764 bridge_priority = STP_DEFAULT_BRIDGE_PRIORITY;
3767 bridge_id = bridge_address | ((uint64_t) bridge_priority << 48);
3769 br->stp = stp_create(br->name, bridge_id, brstp_send_bpdu, br);
3770 br->stp_last_tick = time_msec();
3773 if (bridge_id != stp_get_bridge_id(br->stp)) {
3774 stp_set_bridge_id(br->stp, bridge_id);
3779 for (i = 0; i < br->n_ports; i++) {
3780 struct port *p = br->ports[i];
3782 struct stp_port *sp;
3783 int path_cost, priority;
3789 dp_ifidx = p->ifaces[0]->dp_ifidx;
3790 if (dp_ifidx < 0 || dp_ifidx >= STP_MAX_PORTS) {
3794 sp = stp_get_port(br->stp, dp_ifidx);
3795 enable = (!cfg_is_valid(CFG_BOOL | CFG_REQUIRED,
3796 "stp.%s.port.%s.enabled",
3798 || cfg_get_bool(0, "stp.%s.port.%s.enabled",
3799 br->name, p->name));
3800 if (p->is_mirror_output_port) {
3803 if (enable != (stp_port_get_state(sp) != STP_DISABLED)) {
3804 bridge_flush(br); /* Might not be necessary. */
3806 stp_port_enable(sp);
3808 stp_port_disable(sp);
3812 path_cost = cfg_get_int(0, "stp.%s.port.%s.path-cost",
3814 stp_port_set_path_cost(sp, path_cost ? path_cost : 19 /* XXX */);
3816 priority = (cfg_is_valid(CFG_INT | CFG_REQUIRED,
3817 "stp.%s.port.%s.priority",
3819 ? cfg_get_int(0, "stp.%s.port.%s.priority",
3821 : STP_DEFAULT_PORT_PRIORITY);
3822 stp_port_set_priority(sp, priority);
3825 brstp_adjust_timers(br);
3827 for (i = 0; i < br->n_ports; i++) {
3828 brstp_update_port_state(br->ports[i]);
3833 brstp_update_port_state(struct port *p)
3835 struct bridge *br = p->bridge;
3836 enum stp_state state;
3838 /* Figure out new state. */
3839 state = STP_DISABLED;
3840 if (br->stp && p->n_ifaces > 0) {
3841 int dp_ifidx = p->ifaces[0]->dp_ifidx;
3842 if (dp_ifidx >= 0 && dp_ifidx < STP_MAX_PORTS) {
3843 state = stp_port_get_state(stp_get_port(br->stp, dp_ifidx));
3848 if (p->stp_state != state) {
3849 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3850 VLOG_INFO_RL(&rl, "port %s: STP state changed from %s to %s",
3851 p->name, stp_state_name(p->stp_state),
3852 stp_state_name(state));
3853 if (p->stp_state == STP_DISABLED) {
3856 ofproto_revalidate(p->bridge->ofproto, p->stp_state_tag);
3858 p->stp_state = state;
3859 p->stp_state_tag = (p->stp_state == STP_DISABLED ? 0
3860 : tag_create_random());
3865 brstp_adjust_timers(struct bridge *br)
3867 int hello_time = cfg_get_int(0, "stp.%s.hello-time", br->name);
3868 int max_age = cfg_get_int(0, "stp.%s.max-age", br->name);
3869 int forward_delay = cfg_get_int(0, "stp.%s.forward-delay", br->name);
3871 stp_set_hello_time(br->stp, hello_time ? hello_time : 2000);
3872 stp_set_max_age(br->stp, max_age ? max_age : 20000);
3873 stp_set_forward_delay(br->stp, forward_delay ? forward_delay : 15000);
3877 brstp_run(struct bridge *br)
3880 long long int now = time_msec();
3881 long long int elapsed = now - br->stp_last_tick;
3882 struct stp_port *sp;
3885 stp_tick(br->stp, MIN(INT_MAX, elapsed));
3886 br->stp_last_tick = now;
3888 while (stp_get_changed_port(br->stp, &sp)) {
3889 struct port *p = port_from_dp_ifidx(br, stp_port_no(sp));
3891 brstp_update_port_state(p);
3898 brstp_wait(struct bridge *br)
3901 poll_timer_wait(1000);