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"
54 #include "socket-util.h"
61 #include "vconn-ssl.h"
62 #include "xenserver.h"
64 #include "sflow_api.h"
66 #define THIS_MODULE VLM_bridge
74 extern uint64_t mgmt_id;
77 /* These members are always valid. */
78 struct port *port; /* Containing port. */
79 size_t port_ifidx; /* Index within containing port. */
80 char *name; /* Host network device name. */
81 tag_type tag; /* Tag associated with this interface. */
82 long long delay_expires; /* Time after which 'enabled' may change. */
84 /* These members are valid only after bridge_reconfigure() causes them to
86 int dp_ifidx; /* Index within kernel datapath. */
87 struct netdev *netdev; /* Network device. */
88 bool enabled; /* May be chosen for flows? */
91 #define BOND_MASK 0xff
93 int iface_idx; /* Index of assigned iface, or -1 if none. */
94 uint64_t tx_bytes; /* Count of bytes recently transmitted. */
95 tag_type iface_tag; /* Tag associated with iface_idx. */
98 #define MAX_MIRRORS 32
99 typedef uint32_t mirror_mask_t;
100 #define MIRROR_MASK_C(X) UINT32_C(X)
101 BUILD_ASSERT_DECL(sizeof(mirror_mask_t) * CHAR_BIT >= MAX_MIRRORS);
103 struct bridge *bridge;
107 /* Selection criteria. */
108 struct svec src_ports;
109 struct svec dst_ports;
114 struct port *out_port;
118 #define FLOOD_PORT ((struct port *) 1) /* The 'flood' output port. */
120 struct bridge *bridge;
122 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
123 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1. */
126 /* An ordinary bridge port has 1 interface.
127 * A bridge port for bonding has at least 2 interfaces. */
128 struct iface **ifaces;
129 size_t n_ifaces, allocated_ifaces;
132 struct bond_entry *bond_hash; /* An array of (BOND_MASK + 1) elements. */
133 int active_iface; /* Ifidx on which bcasts accepted, or -1. */
134 tag_type active_iface_tag; /* Tag for bcast flows. */
135 tag_type no_ifaces_tag; /* Tag for flows when all ifaces disabled. */
136 int updelay, downdelay; /* Delay before iface goes up/down, in ms. */
137 bool bond_compat_is_stale; /* Need to call port_update_bond_compat()? */
139 /* Port mirroring info. */
140 mirror_mask_t src_mirrors; /* Mirrors triggered when packet received. */
141 mirror_mask_t dst_mirrors; /* Mirrors triggered when packet sent. */
142 bool is_mirror_output_port; /* Does port mirroring send frames here? */
144 /* Spanning tree info. */
145 enum stp_state stp_state; /* Always STP_FORWARDING if STP not in use. */
146 tag_type stp_state_tag; /* Tag for STP state change. */
149 #define DP_MAX_PORTS 255
151 struct list node; /* Node in global list of bridges. */
152 char *name; /* User-specified arbitrary name. */
153 struct mac_learning *ml; /* MAC learning table. */
154 bool sent_config_request; /* Successfully sent config request? */
155 uint8_t default_ea[ETH_ADDR_LEN]; /* Default MAC. */
157 /* Support for remote controllers. */
158 char *controller; /* NULL if there is no remote controller;
159 * "discover" to do controller discovery;
160 * otherwise a vconn name. */
162 /* OpenFlow switch processing. */
163 struct ofproto *ofproto; /* OpenFlow switch. */
165 /* Kernel datapath information. */
166 struct dpif *dpif; /* Datapath. */
167 struct port_array ifaces; /* Indexed by kernel datapath port number. */
171 size_t n_ports, allocated_ports;
174 bool has_bonded_ports;
175 long long int bond_next_rebalance;
180 /* Flow statistics gathering. */
181 time_t next_stats_request;
183 /* Port mirroring. */
184 struct mirror *mirrors[MAX_MIRRORS];
188 long long int stp_last_tick;
191 /* List of all bridges. */
192 static struct list all_bridges = LIST_INITIALIZER(&all_bridges);
194 /* Maximum number of datapaths. */
195 enum { DP_MAX = 256 };
197 static struct bridge *bridge_create(const char *name);
198 static void bridge_destroy(struct bridge *);
199 static struct bridge *bridge_lookup(const char *name);
200 static void bridge_unixctl_dump_flows(struct unixctl_conn *, const char *);
201 static int bridge_run_one(struct bridge *);
202 static void bridge_reconfigure_one(struct bridge *);
203 static void bridge_reconfigure_controller(struct bridge *);
204 static void bridge_get_all_ifaces(const struct bridge *, struct svec *ifaces);
205 static void bridge_fetch_dp_ifaces(struct bridge *);
206 static void bridge_flush(struct bridge *);
207 static void bridge_pick_local_hw_addr(struct bridge *,
208 uint8_t ea[ETH_ADDR_LEN],
209 struct iface **hw_addr_iface);
210 static uint64_t bridge_pick_datapath_id(struct bridge *,
211 const uint8_t bridge_ea[ETH_ADDR_LEN],
212 struct iface *hw_addr_iface);
213 static struct iface *bridge_get_local_iface(struct bridge *);
214 static const char *bridge_get_controller(const struct bridge *br);
215 static uint64_t dpid_from_hash(const void *, size_t nbytes);
217 static void bridge_unixctl_fdb_show(struct unixctl_conn *, const char *args);
219 static void bond_init(void);
220 static void bond_run(struct bridge *);
221 static void bond_wait(struct bridge *);
222 static void bond_rebalance_port(struct port *);
223 static void bond_send_learning_packets(struct port *);
224 static void bond_enable_slave(struct iface *iface, bool enable);
226 static void port_create(struct bridge *, const char *name);
227 static void port_reconfigure(struct port *);
228 static void port_destroy(struct port *);
229 static struct port *port_lookup(const struct bridge *, const char *name);
230 static struct iface *port_lookup_iface(const struct port *, const char *name);
231 static struct port *port_from_dp_ifidx(const struct bridge *,
233 static void port_update_bond_compat(struct port *);
234 static void port_update_vlan_compat(struct port *);
235 static void port_update_bonding(struct port *);
237 static void mirror_create(struct bridge *, const char *name);
238 static void mirror_destroy(struct mirror *);
239 static void mirror_reconfigure(struct bridge *);
240 static void mirror_reconfigure_one(struct mirror *);
241 static bool vlan_is_mirrored(const struct mirror *, int vlan);
243 static void brstp_reconfigure(struct bridge *);
244 static void brstp_adjust_timers(struct bridge *);
245 static void brstp_run(struct bridge *);
246 static void brstp_wait(struct bridge *);
248 static void iface_create(struct port *, const char *name);
249 static void iface_destroy(struct iface *);
250 static struct iface *iface_lookup(const struct bridge *, const char *name);
251 static struct iface *iface_from_dp_ifidx(const struct bridge *,
253 static bool iface_is_internal(const struct bridge *, const char *name);
254 static void iface_set_mac(struct iface *);
256 /* Hooks into ofproto processing. */
257 static struct ofhooks bridge_ofhooks;
259 /* Public functions. */
261 /* Adds the name of each interface used by a bridge, including local and
262 * internal ports, to 'svec'. */
264 bridge_get_ifaces(struct svec *svec)
266 struct bridge *br, *next;
269 LIST_FOR_EACH_SAFE (br, next, struct bridge, node, &all_bridges) {
270 for (i = 0; i < br->n_ports; i++) {
271 struct port *port = br->ports[i];
273 for (j = 0; j < port->n_ifaces; j++) {
274 struct iface *iface = port->ifaces[j];
275 if (iface->dp_ifidx < 0) {
276 VLOG_ERR("%s interface not in datapath %s, ignoring",
277 iface->name, dpif_name(br->dpif));
279 if (iface->dp_ifidx != ODPP_LOCAL) {
280 svec_add(svec, iface->name);
288 /* The caller must already have called cfg_read(). */
292 struct svec dpif_names;
295 unixctl_command_register("fdb/show", bridge_unixctl_fdb_show);
297 svec_init(&dpif_names);
298 dp_enumerate(&dpif_names);
299 for (i = 0; i < dpif_names.n; i++) {
300 const char *dpif_name = dpif_names.names[i];
304 retval = dpif_open(dpif_name, &dpif);
306 struct svec all_names;
309 svec_init(&all_names);
310 dpif_get_all_names(dpif, &all_names);
311 for (j = 0; j < all_names.n; j++) {
312 if (cfg_has("bridge.%s.port", all_names.names[j])) {
318 svec_destroy(&all_names);
322 svec_destroy(&dpif_names);
324 unixctl_command_register("bridge/dump-flows", bridge_unixctl_dump_flows);
327 bridge_reconfigure();
332 config_string_change(const char *key, char **valuep)
334 const char *value = cfg_get_string(0, "%s", key);
335 if (value && (!*valuep || strcmp(value, *valuep))) {
337 *valuep = xstrdup(value);
345 bridge_configure_ssl(void)
347 /* XXX SSL should be configurable on a per-bridge basis.
348 * XXX should be possible to de-configure SSL. */
349 static char *private_key_file;
350 static char *certificate_file;
351 static char *cacert_file;
354 if (config_string_change("ssl.private-key", &private_key_file)) {
355 vconn_ssl_set_private_key_file(private_key_file);
358 if (config_string_change("ssl.certificate", &certificate_file)) {
359 vconn_ssl_set_certificate_file(certificate_file);
362 /* We assume that even if the filename hasn't changed, if the CA cert
363 * file has been removed, that we want to move back into
364 * boot-strapping mode. This opens a small security hole, because
365 * the old certificate will still be trusted until vSwitch is
366 * restarted. We may want to address this in vconn's SSL library. */
367 if (config_string_change("ssl.ca-cert", &cacert_file)
368 || (cacert_file && stat(cacert_file, &s) && errno == ENOENT)) {
369 vconn_ssl_set_ca_cert_file(cacert_file,
370 cfg_get_bool(0, "ssl.bootstrap-ca-cert"));
375 /* Attempt to create the network device 'iface_name' through the netdev
378 set_up_iface(const char *iface_name, bool create)
382 struct svec arg_svec;
387 /* If a type is not explicitly declared, then assume it's an existing
388 * "system" device. */
389 type = cfg_get_string(0, "iface.%s.type", iface_name);
390 if (!type || !strcmp(type, "system")) {
394 svec_init(&arg_svec);
395 cfg_get_subsections(&arg_svec, "iface.%s.args", iface_name);
398 SVEC_FOR_EACH (i, arg, &arg_svec) {
401 value = cfg_get_string(0, "iface.%s.args.%s", iface_name, arg);
403 shash_add(&args, arg, xstrdup(value));
408 error = netdev_create(iface_name, type, &args);
410 /* xxx Check to make sure that the type hasn't changed. */
411 error = netdev_reconfigure(iface_name, &args);
414 svec_destroy(&arg_svec);
415 shash_destroy(&args);
421 create_iface(const char *iface_name)
423 return set_up_iface(iface_name, true);
427 reconfigure_iface(const char *iface_name)
429 return set_up_iface(iface_name, false);
433 destroy_iface(const char *iface_name)
435 netdev_destroy(iface_name);
439 /* iterate_and_prune_ifaces() callback function that opens the network device
440 * for 'iface', if it is not already open, and retrieves the interface's MAC
441 * address and carrier status. */
443 init_iface_netdev(struct bridge *br UNUSED, struct iface *iface,
448 } else if (!netdev_open(iface->name, NETDEV_ETH_TYPE_NONE,
450 netdev_get_carrier(iface->netdev, &iface->enabled);
453 /* If the network device can't be opened, then we're not going to try
454 * to do anything with this interface. */
460 check_iface_dp_ifidx(struct bridge *br, struct iface *iface, void *aux UNUSED)
462 if (iface->dp_ifidx >= 0) {
463 VLOG_DBG("%s has interface %s on port %d",
465 iface->name, iface->dp_ifidx);
468 VLOG_ERR("%s interface not in %s, dropping",
469 iface->name, dpif_name(br->dpif));
475 set_iface_properties(struct bridge *br UNUSED, struct iface *iface,
480 /* Set policing attributes. */
481 rate = cfg_get_int(0, "port.%s.ingress.policing-rate", iface->name);
482 burst = cfg_get_int(0, "port.%s.ingress.policing-burst", iface->name);
483 netdev_set_policing(iface->netdev, rate, burst);
485 /* Set MAC address of internal interfaces other than the local
487 if (iface->dp_ifidx != ODPP_LOCAL
488 && iface_is_internal(br, iface->name)) {
489 iface_set_mac(iface);
495 /* Calls 'cb' for each interfaces in 'br', passing along the 'aux' argument.
496 * Deletes from 'br' all the interfaces for which 'cb' returns false, and then
497 * deletes from 'br' any ports that no longer have any interfaces. */
499 iterate_and_prune_ifaces(struct bridge *br,
500 bool (*cb)(struct bridge *, struct iface *,
506 for (i = 0; i < br->n_ports; ) {
507 struct port *port = br->ports[i];
508 for (j = 0; j < port->n_ifaces; ) {
509 struct iface *iface = port->ifaces[j];
510 if (cb(br, iface, aux)) {
513 iface_destroy(iface);
517 if (port->n_ifaces) {
520 VLOG_ERR("%s port has no interfaces, dropping", port->name);
527 bridge_reconfigure(void)
529 struct svec old_br, new_br;
530 struct bridge *br, *next;
532 int sflow_bridge_number;
534 COVERAGE_INC(bridge_reconfigure);
536 /* Collect old and new bridges. */
539 LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
540 svec_add(&old_br, br->name);
542 cfg_get_subsections(&new_br, "bridge");
544 /* Get rid of deleted bridges and add new bridges. */
547 assert(svec_is_unique(&old_br));
548 assert(svec_is_unique(&new_br));
549 LIST_FOR_EACH_SAFE (br, next, struct bridge, node, &all_bridges) {
550 if (!svec_contains(&new_br, br->name)) {
554 for (i = 0; i < new_br.n; i++) {
555 const char *name = new_br.names[i];
556 if (!svec_contains(&old_br, name)) {
560 svec_destroy(&old_br);
561 svec_destroy(&new_br);
565 bridge_configure_ssl();
568 /* Reconfigure all bridges. */
569 LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
570 bridge_reconfigure_one(br);
573 /* Add and delete ports on all datapaths.
575 * The kernel will reject any attempt to add a given port to a datapath if
576 * that port already belongs to a different datapath, so we must do all
577 * port deletions before any port additions. */
578 LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
579 struct odp_port *dpif_ports;
581 struct svec want_ifaces;
583 dpif_port_list(br->dpif, &dpif_ports, &n_dpif_ports);
584 bridge_get_all_ifaces(br, &want_ifaces);
585 for (i = 0; i < n_dpif_ports; i++) {
586 const struct odp_port *p = &dpif_ports[i];
587 if (!svec_contains(&want_ifaces, p->devname)
588 && strcmp(p->devname, br->name)) {
589 int retval = dpif_port_del(br->dpif, p->port);
591 VLOG_ERR("failed to remove %s interface from %s: %s",
592 p->devname, dpif_name(br->dpif),
595 destroy_iface(p->devname);
598 svec_destroy(&want_ifaces);
601 LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
602 struct odp_port *dpif_ports;
604 struct svec cur_ifaces, want_ifaces, add_ifaces;
606 dpif_port_list(br->dpif, &dpif_ports, &n_dpif_ports);
607 svec_init(&cur_ifaces);
608 for (i = 0; i < n_dpif_ports; i++) {
609 svec_add(&cur_ifaces, dpif_ports[i].devname);
612 svec_sort_unique(&cur_ifaces);
613 bridge_get_all_ifaces(br, &want_ifaces);
614 svec_diff(&want_ifaces, &cur_ifaces, &add_ifaces, NULL, NULL);
616 for (i = 0; i < cur_ifaces.n; i++) {
617 const char *if_name = cur_ifaces.names[i];
618 reconfigure_iface(if_name);
621 for (i = 0; i < add_ifaces.n; i++) {
622 const char *if_name = add_ifaces.names[i];
626 /* Attempt to create the network interface in case it
627 * doesn't exist yet. */
628 error = create_iface(if_name);
630 VLOG_WARN("could not create iface %s: %s\n", if_name,
635 /* Add to datapath. */
636 internal = iface_is_internal(br, if_name);
637 error = dpif_port_add(br->dpif, if_name,
638 internal ? ODP_PORT_INTERNAL : 0, NULL);
639 if (error == EFBIG) {
640 VLOG_ERR("ran out of valid port numbers on %s",
641 dpif_name(br->dpif));
644 VLOG_ERR("failed to add %s interface to %s: %s",
645 if_name, dpif_name(br->dpif), strerror(error));
648 svec_destroy(&cur_ifaces);
649 svec_destroy(&want_ifaces);
650 svec_destroy(&add_ifaces);
652 sflow_bridge_number = 0;
653 LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
656 struct iface *local_iface;
657 struct iface *hw_addr_iface;
658 struct netflow_options nf_options;
660 bridge_fetch_dp_ifaces(br);
661 iterate_and_prune_ifaces(br, init_iface_netdev, NULL);
663 iterate_and_prune_ifaces(br, check_iface_dp_ifidx, NULL);
665 /* Pick local port hardware address, datapath ID. */
666 bridge_pick_local_hw_addr(br, ea, &hw_addr_iface);
667 local_iface = bridge_get_local_iface(br);
669 int error = netdev_set_etheraddr(local_iface->netdev, ea);
671 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
672 VLOG_ERR_RL(&rl, "bridge %s: failed to set bridge "
673 "Ethernet address: %s",
674 br->name, strerror(error));
678 dpid = bridge_pick_datapath_id(br, ea, hw_addr_iface);
679 ofproto_set_datapath_id(br->ofproto, dpid);
681 /* Set NetFlow configuration on this bridge. */
682 memset(&nf_options, 0, sizeof nf_options);
683 dpif_get_netflow_ids(br->dpif, &nf_options.engine_type,
684 &nf_options.engine_id);
685 nf_options.active_timeout = -1;
687 if (cfg_has("netflow.%s.engine-type", br->name)) {
688 nf_options.engine_type = cfg_get_int(0, "netflow.%s.engine-type",
691 if (cfg_has("netflow.%s.engine-id", br->name)) {
692 nf_options.engine_id = cfg_get_int(0, "netflow.%s.engine-id",
695 if (cfg_has("netflow.%s.active-timeout", br->name)) {
696 nf_options.active_timeout = cfg_get_int(0,
697 "netflow.%s.active-timeout",
700 if (cfg_has("netflow.%s.add-id-to-iface", br->name)) {
701 nf_options.add_id_to_iface = cfg_get_bool(0,
702 "netflow.%s.add-id-to-iface",
705 if (nf_options.add_id_to_iface && nf_options.engine_id > 0x7f) {
706 VLOG_WARN("bridge %s: netflow port mangling may conflict with "
707 "another vswitch, choose an engine id less than 128",
710 if (nf_options.add_id_to_iface && br->n_ports > 508) {
711 VLOG_WARN("bridge %s: netflow port mangling will conflict with "
712 "another port when more than 508 ports are used",
715 svec_init(&nf_options.collectors);
716 cfg_get_all_keys(&nf_options.collectors, "netflow.%s.host", br->name);
717 if (ofproto_set_netflow(br->ofproto, &nf_options)) {
718 VLOG_ERR("bridge %s: problem setting netflow collectors",
721 svec_destroy(&nf_options.collectors);
723 if (cfg_has("sflow.%s.host", br->name)) {
724 struct ofproto_sflow_options oso;
726 svec_init(&oso.targets);
727 cfg_get_all_keys(&oso.targets, "sflow.%s.host", br->name);
729 oso.sampling_rate = SFL_DEFAULT_SAMPLING_RATE;
730 if (cfg_has("sflow.%s.sampling", br->name)) {
731 oso.sampling_rate = cfg_get_int(0, "sflow.%s.sampling",
735 oso.polling_interval = SFL_DEFAULT_POLLING_INTERVAL;
736 if (cfg_has("sflow.%s.polling", br->name)) {
737 oso.polling_interval = cfg_get_int(0, "sflow.%s.polling",
741 oso.header_len = SFL_DEFAULT_HEADER_SIZE;
742 if (cfg_has("sflow.%s.header", br->name)) {
743 oso.header_len = cfg_get_int(0, "sflow.%s.header", br->name);
746 oso.sub_id = sflow_bridge_number++;
747 oso.agent_device = (char *) cfg_get_string(0, "sflow.%s.agent",
749 oso.control_ip = (char *) cfg_get_string(0,
750 "bridge.%s.controller.ip",
752 ofproto_set_sflow(br->ofproto, &oso);
754 svec_destroy(&oso.targets);
756 ofproto_set_sflow(br->ofproto, NULL);
759 /* Update the controller and related settings. It would be more
760 * straightforward to call this from bridge_reconfigure_one(), but we
761 * can't do it there for two reasons. First, and most importantly, at
762 * that point we don't know the dp_ifidx of any interfaces that have
763 * been added to the bridge (because we haven't actually added them to
764 * the datapath). Second, at that point we haven't set the datapath ID
765 * yet; when a controller is configured, resetting the datapath ID will
766 * immediately disconnect from the controller, so it's better to set
767 * the datapath ID before the controller. */
768 bridge_reconfigure_controller(br);
770 LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
771 for (i = 0; i < br->n_ports; i++) {
772 struct port *port = br->ports[i];
774 port_update_vlan_compat(port);
775 port_update_bonding(port);
778 LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
779 brstp_reconfigure(br);
780 iterate_and_prune_ifaces(br, set_iface_properties, NULL);
785 bridge_pick_local_hw_addr(struct bridge *br, uint8_t ea[ETH_ADDR_LEN],
786 struct iface **hw_addr_iface)
788 uint64_t requested_ea;
792 *hw_addr_iface = NULL;
794 /* Did the user request a particular MAC? */
795 requested_ea = cfg_get_mac(0, "bridge.%s.mac", br->name);
797 eth_addr_from_uint64(requested_ea, ea);
798 if (eth_addr_is_multicast(ea)) {
799 VLOG_ERR("bridge %s: cannot set MAC address to multicast "
800 "address "ETH_ADDR_FMT, br->name, ETH_ADDR_ARGS(ea));
801 } else if (eth_addr_is_zero(ea)) {
802 VLOG_ERR("bridge %s: cannot set MAC address to zero", br->name);
808 /* Otherwise choose the minimum MAC address among all of the interfaces.
809 * (Xen uses FE:FF:FF:FF:FF:FF for virtual interfaces so this will get the
810 * MAC of the physical interface in such an environment.) */
811 memset(ea, 0xff, sizeof ea);
812 for (i = 0; i < br->n_ports; i++) {
813 struct port *port = br->ports[i];
814 uint8_t iface_ea[ETH_ADDR_LEN];
815 uint64_t iface_ea_u64;
818 /* Mirror output ports don't participate. */
819 if (port->is_mirror_output_port) {
823 /* Choose the MAC address to represent the port. */
824 iface_ea_u64 = cfg_get_mac(0, "port.%s.mac", port->name);
826 /* User specified explicitly. */
827 eth_addr_from_uint64(iface_ea_u64, iface_ea);
829 /* Find the interface with this Ethernet address (if any) so that
830 * we can provide the correct devname to the caller. */
832 for (j = 0; j < port->n_ifaces; j++) {
833 struct iface *candidate = port->ifaces[j];
834 uint8_t candidate_ea[ETH_ADDR_LEN];
835 if (!netdev_get_etheraddr(candidate->netdev, candidate_ea)
836 && eth_addr_equals(iface_ea, candidate_ea)) {
841 /* Choose the interface whose MAC address will represent the port.
842 * The Linux kernel bonding code always chooses the MAC address of
843 * the first slave added to a bond, and the Fedora networking
844 * scripts always add slaves to a bond in alphabetical order, so
845 * for compatibility we choose the interface with the name that is
846 * first in alphabetical order. */
847 iface = port->ifaces[0];
848 for (j = 1; j < port->n_ifaces; j++) {
849 struct iface *candidate = port->ifaces[j];
850 if (strcmp(candidate->name, iface->name) < 0) {
855 /* The local port doesn't count (since we're trying to choose its
856 * MAC address anyway). Other internal ports don't count because
857 * we really want a physical MAC if we can get it, and internal
858 * ports typically have randomly generated MACs. */
859 if (iface->dp_ifidx == ODPP_LOCAL
860 || cfg_get_bool(0, "iface.%s.internal", iface->name)) {
865 error = netdev_get_etheraddr(iface->netdev, iface_ea);
867 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
868 VLOG_ERR_RL(&rl, "failed to obtain Ethernet address of %s: %s",
869 iface->name, strerror(error));
874 /* Compare against our current choice. */
875 if (!eth_addr_is_multicast(iface_ea) &&
876 !eth_addr_is_reserved(iface_ea) &&
877 !eth_addr_is_zero(iface_ea) &&
878 memcmp(iface_ea, ea, ETH_ADDR_LEN) < 0)
880 memcpy(ea, iface_ea, ETH_ADDR_LEN);
881 *hw_addr_iface = iface;
884 if (eth_addr_is_multicast(ea) || eth_addr_is_vif(ea)) {
885 memcpy(ea, br->default_ea, ETH_ADDR_LEN);
886 *hw_addr_iface = NULL;
887 VLOG_WARN("bridge %s: using default bridge Ethernet "
888 "address "ETH_ADDR_FMT, br->name, ETH_ADDR_ARGS(ea));
890 VLOG_DBG("bridge %s: using bridge Ethernet address "ETH_ADDR_FMT,
891 br->name, ETH_ADDR_ARGS(ea));
895 /* Choose and returns the datapath ID for bridge 'br' given that the bridge
896 * Ethernet address is 'bridge_ea'. If 'bridge_ea' is the Ethernet address of
897 * an interface on 'br', then that interface must be passed in as
898 * 'hw_addr_iface'; if 'bridge_ea' was derived some other way, then
899 * 'hw_addr_iface' must be passed in as a null pointer. */
901 bridge_pick_datapath_id(struct bridge *br,
902 const uint8_t bridge_ea[ETH_ADDR_LEN],
903 struct iface *hw_addr_iface)
906 * The procedure for choosing a bridge MAC address will, in the most
907 * ordinary case, also choose a unique MAC that we can use as a datapath
908 * ID. In some special cases, though, multiple bridges will end up with
909 * the same MAC address. This is OK for the bridges, but it will confuse
910 * the OpenFlow controller, because each datapath needs a unique datapath
913 * Datapath IDs must be unique. It is also very desirable that they be
914 * stable from one run to the next, so that policy set on a datapath
919 dpid = cfg_get_dpid(0, "bridge.%s.datapath-id", br->name);
926 if (!netdev_get_vlan_vid(hw_addr_iface->netdev, &vlan)) {
928 * A bridge whose MAC address is taken from a VLAN network device
929 * (that is, a network device created with vconfig(8) or similar
930 * tool) will have the same MAC address as a bridge on the VLAN
931 * device's physical network device.
933 * Handle this case by hashing the physical network device MAC
934 * along with the VLAN identifier.
936 uint8_t buf[ETH_ADDR_LEN + 2];
937 memcpy(buf, bridge_ea, ETH_ADDR_LEN);
938 buf[ETH_ADDR_LEN] = vlan >> 8;
939 buf[ETH_ADDR_LEN + 1] = vlan;
940 return dpid_from_hash(buf, sizeof buf);
943 * Assume that this bridge's MAC address is unique, since it
944 * doesn't fit any of the cases we handle specially.
949 * A purely internal bridge, that is, one that has no non-virtual
950 * network devices on it at all, is more difficult because it has no
951 * natural unique identifier at all.
953 * When the host is a XenServer, we handle this case by hashing the
954 * host's UUID with the name of the bridge. Names of bridges are
955 * persistent across XenServer reboots, although they can be reused if
956 * an internal network is destroyed and then a new one is later
957 * created, so this is fairly effective.
959 * When the host is not a XenServer, we punt by using a random MAC
960 * address on each run.
962 const char *host_uuid = xenserver_get_host_uuid();
964 char *combined = xasprintf("%s,%s", host_uuid, br->name);
965 dpid = dpid_from_hash(combined, strlen(combined));
971 return eth_addr_to_uint64(bridge_ea);
975 dpid_from_hash(const void *data, size_t n)
977 uint8_t hash[SHA1_DIGEST_SIZE];
979 BUILD_ASSERT_DECL(sizeof hash >= ETH_ADDR_LEN);
980 sha1_bytes(data, n, hash);
981 eth_addr_mark_random(hash);
982 return eth_addr_to_uint64(hash);
988 struct bridge *br, *next;
992 LIST_FOR_EACH_SAFE (br, next, struct bridge, node, &all_bridges) {
993 int error = bridge_run_one(br);
995 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
996 VLOG_ERR_RL(&rl, "bridge %s: datapath was destroyed externally, "
997 "forcing reconfiguration", br->name);
1011 LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
1012 ofproto_wait(br->ofproto);
1013 if (br->controller) {
1017 mac_learning_wait(br->ml);
1023 /* Forces 'br' to revalidate all of its flows. This is appropriate when 'br''s
1024 * configuration changes. */
1026 bridge_flush(struct bridge *br)
1028 COVERAGE_INC(bridge_flush);
1030 mac_learning_flush(br->ml);
1033 /* Returns the 'br' interface for the ODPP_LOCAL port, or null if 'br' has no
1034 * such interface. */
1035 static struct iface *
1036 bridge_get_local_iface(struct bridge *br)
1040 for (i = 0; i < br->n_ports; i++) {
1041 struct port *port = br->ports[i];
1042 for (j = 0; j < port->n_ifaces; j++) {
1043 struct iface *iface = port->ifaces[j];
1044 if (iface->dp_ifidx == ODPP_LOCAL) {
1053 /* Bridge unixctl user interface functions. */
1055 bridge_unixctl_fdb_show(struct unixctl_conn *conn, const char *args)
1057 struct ds ds = DS_EMPTY_INITIALIZER;
1058 const struct bridge *br;
1059 const struct mac_entry *e;
1061 br = bridge_lookup(args);
1063 unixctl_command_reply(conn, 501, "no such bridge");
1067 ds_put_cstr(&ds, " port VLAN MAC Age\n");
1068 LIST_FOR_EACH (e, struct mac_entry, lru_node, &br->ml->lrus) {
1069 if (e->port < 0 || e->port >= br->n_ports) {
1072 ds_put_format(&ds, "%5d %4d "ETH_ADDR_FMT" %3d\n",
1073 br->ports[e->port]->ifaces[0]->dp_ifidx,
1074 e->vlan, ETH_ADDR_ARGS(e->mac), mac_entry_age(e));
1076 unixctl_command_reply(conn, 200, ds_cstr(&ds));
1080 /* Bridge reconfiguration functions. */
1082 static struct bridge *
1083 bridge_create(const char *name)
1088 assert(!bridge_lookup(name));
1089 br = xcalloc(1, sizeof *br);
1091 error = dpif_create_and_open(name, &br->dpif);
1096 dpif_flow_flush(br->dpif);
1098 error = ofproto_create(name, &bridge_ofhooks, br, &br->ofproto);
1100 VLOG_ERR("failed to create switch %s: %s", name, strerror(error));
1101 dpif_delete(br->dpif);
1102 dpif_close(br->dpif);
1107 br->name = xstrdup(name);
1108 br->ml = mac_learning_create();
1109 br->sent_config_request = false;
1110 eth_addr_random(br->default_ea);
1112 port_array_init(&br->ifaces);
1115 br->bond_next_rebalance = time_msec() + 10000;
1117 list_push_back(&all_bridges, &br->node);
1119 VLOG_INFO("created bridge %s on %s", br->name, dpif_name(br->dpif));
1125 bridge_destroy(struct bridge *br)
1130 while (br->n_ports > 0) {
1131 port_destroy(br->ports[br->n_ports - 1]);
1133 list_remove(&br->node);
1134 error = dpif_delete(br->dpif);
1135 if (error && error != ENOENT) {
1136 VLOG_ERR("failed to delete %s: %s",
1137 dpif_name(br->dpif), strerror(error));
1139 dpif_close(br->dpif);
1140 ofproto_destroy(br->ofproto);
1141 free(br->controller);
1142 mac_learning_destroy(br->ml);
1143 port_array_destroy(&br->ifaces);
1150 static struct bridge *
1151 bridge_lookup(const char *name)
1155 LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
1156 if (!strcmp(br->name, name)) {
1164 bridge_exists(const char *name)
1166 return bridge_lookup(name) ? true : false;
1170 bridge_get_datapathid(const char *name)
1172 struct bridge *br = bridge_lookup(name);
1173 return br ? ofproto_get_datapath_id(br->ofproto) : 0;
1176 /* Handle requests for a listing of all flows known by the OpenFlow
1177 * stack, including those normally hidden. */
1179 bridge_unixctl_dump_flows(struct unixctl_conn *conn, const char *args)
1184 br = bridge_lookup(args);
1186 unixctl_command_reply(conn, 501, "Unknown bridge");
1191 ofproto_get_all_flows(br->ofproto, &results);
1193 unixctl_command_reply(conn, 200, ds_cstr(&results));
1194 ds_destroy(&results);
1198 bridge_run_one(struct bridge *br)
1202 error = ofproto_run1(br->ofproto);
1207 mac_learning_run(br->ml, ofproto_get_revalidate_set(br->ofproto));
1211 error = ofproto_run2(br->ofproto, br->flush);
1218 bridge_get_controller(const struct bridge *br)
1220 const char *controller;
1222 controller = cfg_get_string(0, "bridge.%s.controller", br->name);
1224 controller = cfg_get_string(0, "mgmt.controller");
1226 return controller && controller[0] ? controller : NULL;
1230 check_duplicate_ifaces(struct bridge *br, struct iface *iface, void *ifaces_)
1232 struct svec *ifaces = ifaces_;
1233 if (!svec_contains(ifaces, iface->name)) {
1234 svec_add(ifaces, iface->name);
1238 VLOG_ERR("bridge %s: %s interface is on multiple ports, "
1240 br->name, iface->name, iface->port->name);
1246 bridge_reconfigure_one(struct bridge *br)
1248 struct svec old_ports, new_ports, ifaces;
1249 struct svec listeners, old_listeners;
1250 struct svec snoops, old_snoops;
1253 /* Collect old ports. */
1254 svec_init(&old_ports);
1255 for (i = 0; i < br->n_ports; i++) {
1256 svec_add(&old_ports, br->ports[i]->name);
1258 svec_sort(&old_ports);
1259 assert(svec_is_unique(&old_ports));
1261 /* Collect new ports. */
1262 svec_init(&new_ports);
1263 cfg_get_all_keys(&new_ports, "bridge.%s.port", br->name);
1264 svec_sort(&new_ports);
1265 if (bridge_get_controller(br)) {
1266 char local_name[IF_NAMESIZE];
1269 error = dpif_port_get_name(br->dpif, ODPP_LOCAL,
1270 local_name, sizeof local_name);
1271 if (!error && !svec_contains(&new_ports, local_name)) {
1272 svec_add(&new_ports, local_name);
1273 svec_sort(&new_ports);
1276 if (!svec_is_unique(&new_ports)) {
1277 VLOG_WARN("bridge %s: %s specified twice as bridge port",
1278 br->name, svec_get_duplicate(&new_ports));
1279 svec_unique(&new_ports);
1282 ofproto_set_mgmt_id(br->ofproto, mgmt_id);
1284 /* Get rid of deleted ports and add new ports. */
1285 for (i = 0; i < br->n_ports; ) {
1286 struct port *port = br->ports[i];
1287 if (!svec_contains(&new_ports, port->name)) {
1293 for (i = 0; i < new_ports.n; i++) {
1294 const char *name = new_ports.names[i];
1295 if (!svec_contains(&old_ports, name)) {
1296 port_create(br, name);
1299 svec_destroy(&old_ports);
1300 svec_destroy(&new_ports);
1302 /* Reconfigure all ports. */
1303 for (i = 0; i < br->n_ports; i++) {
1304 port_reconfigure(br->ports[i]);
1307 /* Check and delete duplicate interfaces. */
1309 iterate_and_prune_ifaces(br, check_duplicate_ifaces, &ifaces);
1310 svec_destroy(&ifaces);
1312 /* Delete all flows if we're switching from connected to standalone or vice
1313 * versa. (XXX Should we delete all flows if we are switching from one
1314 * controller to another?) */
1316 /* Configure OpenFlow management listeners. */
1317 svec_init(&listeners);
1318 cfg_get_all_strings(&listeners, "bridge.%s.openflow.listeners", br->name);
1320 svec_add_nocopy(&listeners, xasprintf("punix:%s/%s.mgmt",
1321 ovs_rundir, br->name));
1322 } else if (listeners.n == 1 && !strcmp(listeners.names[0], "none")) {
1323 svec_clear(&listeners);
1325 svec_sort_unique(&listeners);
1327 svec_init(&old_listeners);
1328 ofproto_get_listeners(br->ofproto, &old_listeners);
1329 svec_sort_unique(&old_listeners);
1331 if (!svec_equal(&listeners, &old_listeners)) {
1332 ofproto_set_listeners(br->ofproto, &listeners);
1334 svec_destroy(&listeners);
1335 svec_destroy(&old_listeners);
1337 /* Configure OpenFlow controller connection snooping. */
1339 cfg_get_all_strings(&snoops, "bridge.%s.openflow.snoops", br->name);
1341 svec_add_nocopy(&snoops, xasprintf("punix:%s/%s.snoop",
1342 ovs_rundir, br->name));
1343 } else if (snoops.n == 1 && !strcmp(snoops.names[0], "none")) {
1344 svec_clear(&snoops);
1346 svec_sort_unique(&snoops);
1348 svec_init(&old_snoops);
1349 ofproto_get_snoops(br->ofproto, &old_snoops);
1350 svec_sort_unique(&old_snoops);
1352 if (!svec_equal(&snoops, &old_snoops)) {
1353 ofproto_set_snoops(br->ofproto, &snoops);
1355 svec_destroy(&snoops);
1356 svec_destroy(&old_snoops);
1358 mirror_reconfigure(br);
1362 bridge_reconfigure_controller(struct bridge *br)
1364 char *pfx = xasprintf("bridge.%s.controller", br->name);
1365 const char *controller;
1367 controller = bridge_get_controller(br);
1368 if ((br->controller != NULL) != (controller != NULL)) {
1369 ofproto_flush_flows(br->ofproto);
1371 free(br->controller);
1372 br->controller = controller ? xstrdup(controller) : NULL;
1375 const char *fail_mode;
1376 int max_backoff, probe;
1377 int rate_limit, burst_limit;
1379 if (!strcmp(controller, "discover")) {
1380 bool update_resolv_conf = true;
1382 if (cfg_has("%s.update-resolv.conf", pfx)) {
1383 update_resolv_conf = cfg_get_bool(0, "%s.update-resolv.conf",
1386 ofproto_set_discovery(br->ofproto, true,
1387 cfg_get_string(0, "%s.accept-regex", pfx),
1388 update_resolv_conf);
1390 struct iface *local_iface;
1393 in_band = (!cfg_is_valid(CFG_BOOL | CFG_REQUIRED,
1395 || cfg_get_bool(0, "%s.in-band", pfx));
1396 ofproto_set_discovery(br->ofproto, false, NULL, NULL);
1397 ofproto_set_in_band(br->ofproto, in_band);
1399 local_iface = bridge_get_local_iface(br);
1401 && cfg_is_valid(CFG_IP | CFG_REQUIRED, "%s.ip", pfx)) {
1402 struct netdev *netdev = local_iface->netdev;
1403 struct in_addr ip, mask, gateway;
1404 ip.s_addr = cfg_get_ip(0, "%s.ip", pfx);
1405 mask.s_addr = cfg_get_ip(0, "%s.netmask", pfx);
1406 gateway.s_addr = cfg_get_ip(0, "%s.gateway", pfx);
1408 netdev_turn_flags_on(netdev, NETDEV_UP, true);
1410 mask.s_addr = guess_netmask(ip.s_addr);
1412 if (!netdev_set_in4(netdev, ip, mask)) {
1413 VLOG_INFO("bridge %s: configured IP address "IP_FMT", "
1415 br->name, IP_ARGS(&ip.s_addr),
1416 IP_ARGS(&mask.s_addr));
1419 if (gateway.s_addr) {
1420 if (!netdev_add_router(netdev, gateway)) {
1421 VLOG_INFO("bridge %s: configured gateway "IP_FMT,
1422 br->name, IP_ARGS(&gateway.s_addr));
1428 fail_mode = cfg_get_string(0, "%s.fail-mode", pfx);
1430 fail_mode = cfg_get_string(0, "mgmt.fail-mode");
1432 ofproto_set_failure(br->ofproto,
1434 || !strcmp(fail_mode, "standalone")
1435 || !strcmp(fail_mode, "open")));
1437 probe = cfg_get_int(0, "%s.inactivity-probe", pfx);
1439 probe = cfg_get_int(0, "mgmt.inactivity-probe");
1444 ofproto_set_probe_interval(br->ofproto, probe);
1446 max_backoff = cfg_get_int(0, "%s.max-backoff", pfx);
1448 max_backoff = cfg_get_int(0, "mgmt.max-backoff");
1453 ofproto_set_max_backoff(br->ofproto, max_backoff);
1455 rate_limit = cfg_get_int(0, "%s.rate-limit", pfx);
1457 rate_limit = cfg_get_int(0, "mgmt.rate-limit");
1459 burst_limit = cfg_get_int(0, "%s.burst-limit", pfx);
1461 burst_limit = cfg_get_int(0, "mgmt.burst-limit");
1463 ofproto_set_rate_limit(br->ofproto, rate_limit, burst_limit);
1465 ofproto_set_stp(br->ofproto, cfg_get_bool(0, "%s.stp", pfx));
1467 if (cfg_has("%s.commands.acl", pfx)) {
1468 struct svec command_acls;
1471 svec_init(&command_acls);
1472 cfg_get_all_strings(&command_acls, "%s.commands.acl", pfx);
1473 command_acl = svec_join(&command_acls, ",", "");
1475 ofproto_set_remote_execution(br->ofproto, command_acl,
1476 cfg_get_string(0, "%s.commands.dir",
1479 svec_destroy(&command_acls);
1482 ofproto_set_remote_execution(br->ofproto, NULL, NULL);
1485 union ofp_action action;
1488 /* Set up a flow that matches every packet and directs them to
1489 * OFPP_NORMAL (which goes to us). */
1490 memset(&action, 0, sizeof action);
1491 action.type = htons(OFPAT_OUTPUT);
1492 action.output.len = htons(sizeof action);
1493 action.output.port = htons(OFPP_NORMAL);
1494 memset(&flow, 0, sizeof flow);
1495 ofproto_add_flow(br->ofproto, &flow, OFPFW_ALL, 0,
1498 ofproto_set_in_band(br->ofproto, false);
1499 ofproto_set_max_backoff(br->ofproto, 1);
1500 ofproto_set_probe_interval(br->ofproto, 5);
1501 ofproto_set_failure(br->ofproto, false);
1502 ofproto_set_stp(br->ofproto, false);
1506 ofproto_set_controller(br->ofproto, br->controller);
1510 bridge_get_all_ifaces(const struct bridge *br, struct svec *ifaces)
1515 for (i = 0; i < br->n_ports; i++) {
1516 struct port *port = br->ports[i];
1517 for (j = 0; j < port->n_ifaces; j++) {
1518 struct iface *iface = port->ifaces[j];
1519 svec_add(ifaces, iface->name);
1521 if (port->n_ifaces > 1
1522 && cfg_get_bool(0, "bonding.%s.fake-iface", port->name)) {
1523 svec_add(ifaces, port->name);
1526 svec_sort_unique(ifaces);
1529 /* For robustness, in case the administrator moves around datapath ports behind
1530 * our back, we re-check all the datapath port numbers here.
1532 * This function will set the 'dp_ifidx' members of interfaces that have
1533 * disappeared to -1, so only call this function from a context where those
1534 * 'struct iface's will be removed from the bridge. Otherwise, the -1
1535 * 'dp_ifidx'es will cause trouble later when we try to send them to the
1536 * datapath, which doesn't support UINT16_MAX+1 ports. */
1538 bridge_fetch_dp_ifaces(struct bridge *br)
1540 struct odp_port *dpif_ports;
1541 size_t n_dpif_ports;
1544 /* Reset all interface numbers. */
1545 for (i = 0; i < br->n_ports; i++) {
1546 struct port *port = br->ports[i];
1547 for (j = 0; j < port->n_ifaces; j++) {
1548 struct iface *iface = port->ifaces[j];
1549 iface->dp_ifidx = -1;
1552 port_array_clear(&br->ifaces);
1554 dpif_port_list(br->dpif, &dpif_ports, &n_dpif_ports);
1555 for (i = 0; i < n_dpif_ports; i++) {
1556 struct odp_port *p = &dpif_ports[i];
1557 struct iface *iface = iface_lookup(br, p->devname);
1559 if (iface->dp_ifidx >= 0) {
1560 VLOG_WARN("%s reported interface %s twice",
1561 dpif_name(br->dpif), p->devname);
1562 } else if (iface_from_dp_ifidx(br, p->port)) {
1563 VLOG_WARN("%s reported interface %"PRIu16" twice",
1564 dpif_name(br->dpif), p->port);
1566 port_array_set(&br->ifaces, p->port, iface);
1567 iface->dp_ifidx = p->port;
1574 /* Bridge packet processing functions. */
1577 bond_hash(const uint8_t mac[ETH_ADDR_LEN])
1579 return hash_bytes(mac, ETH_ADDR_LEN, 0) & BOND_MASK;
1582 static struct bond_entry *
1583 lookup_bond_entry(const struct port *port, const uint8_t mac[ETH_ADDR_LEN])
1585 return &port->bond_hash[bond_hash(mac)];
1589 bond_choose_iface(const struct port *port)
1591 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
1592 size_t i, best_down_slave = -1;
1593 long long next_delay_expiration = LLONG_MAX;
1595 for (i = 0; i < port->n_ifaces; i++) {
1596 struct iface *iface = port->ifaces[i];
1598 if (iface->enabled) {
1600 } else if (iface->delay_expires < next_delay_expiration) {
1601 best_down_slave = i;
1602 next_delay_expiration = iface->delay_expires;
1606 if (best_down_slave != -1) {
1607 struct iface *iface = port->ifaces[best_down_slave];
1609 VLOG_INFO_RL(&rl, "interface %s: skipping remaining %lli ms updelay "
1610 "since no other interface is up", iface->name,
1611 iface->delay_expires - time_msec());
1612 bond_enable_slave(iface, true);
1615 return best_down_slave;
1619 choose_output_iface(const struct port *port, const uint8_t *dl_src,
1620 uint16_t *dp_ifidx, tag_type *tags)
1622 struct iface *iface;
1624 assert(port->n_ifaces);
1625 if (port->n_ifaces == 1) {
1626 iface = port->ifaces[0];
1628 struct bond_entry *e = lookup_bond_entry(port, dl_src);
1629 if (e->iface_idx < 0 || e->iface_idx >= port->n_ifaces
1630 || !port->ifaces[e->iface_idx]->enabled) {
1631 /* XXX select interface properly. The current interface selection
1632 * is only good for testing the rebalancing code. */
1633 e->iface_idx = bond_choose_iface(port);
1634 if (e->iface_idx < 0) {
1635 *tags |= port->no_ifaces_tag;
1638 e->iface_tag = tag_create_random();
1639 ((struct port *) port)->bond_compat_is_stale = true;
1641 *tags |= e->iface_tag;
1642 iface = port->ifaces[e->iface_idx];
1644 *dp_ifidx = iface->dp_ifidx;
1645 *tags |= iface->tag; /* Currently only used for bonding. */
1650 bond_link_status_update(struct iface *iface, bool carrier)
1652 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
1653 struct port *port = iface->port;
1655 if ((carrier == iface->enabled) == (iface->delay_expires == LLONG_MAX)) {
1656 /* Nothing to do. */
1659 VLOG_INFO_RL(&rl, "interface %s: carrier %s",
1660 iface->name, carrier ? "detected" : "dropped");
1661 if (carrier == iface->enabled) {
1662 iface->delay_expires = LLONG_MAX;
1663 VLOG_INFO_RL(&rl, "interface %s: will not be %s",
1664 iface->name, carrier ? "disabled" : "enabled");
1665 } else if (carrier && port->active_iface < 0) {
1666 bond_enable_slave(iface, true);
1667 if (port->updelay) {
1668 VLOG_INFO_RL(&rl, "interface %s: skipping %d ms updelay since no "
1669 "other interface is up", iface->name, port->updelay);
1672 int delay = carrier ? port->updelay : port->downdelay;
1673 iface->delay_expires = time_msec() + delay;
1676 "interface %s: will be %s if it stays %s for %d ms",
1678 carrier ? "enabled" : "disabled",
1679 carrier ? "up" : "down",
1686 bond_choose_active_iface(struct port *port)
1688 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
1690 port->active_iface = bond_choose_iface(port);
1691 port->active_iface_tag = tag_create_random();
1692 if (port->active_iface >= 0) {
1693 VLOG_INFO_RL(&rl, "port %s: active interface is now %s",
1694 port->name, port->ifaces[port->active_iface]->name);
1696 VLOG_WARN_RL(&rl, "port %s: all ports disabled, no active interface",
1702 bond_enable_slave(struct iface *iface, bool enable)
1704 struct port *port = iface->port;
1705 struct bridge *br = port->bridge;
1707 /* This acts as a recursion check. If the act of disabling a slave
1708 * causes a different slave to be enabled, the flag will allow us to
1709 * skip redundant work when we reenter this function. It must be
1710 * cleared on exit to keep things safe with multiple bonds. */
1711 static bool moving_active_iface = false;
1713 iface->delay_expires = LLONG_MAX;
1714 if (enable == iface->enabled) {
1718 iface->enabled = enable;
1719 if (!iface->enabled) {
1720 VLOG_WARN("interface %s: disabled", iface->name);
1721 ofproto_revalidate(br->ofproto, iface->tag);
1722 if (iface->port_ifidx == port->active_iface) {
1723 ofproto_revalidate(br->ofproto,
1724 port->active_iface_tag);
1726 /* Disabling a slave can lead to another slave being immediately
1727 * enabled if there will be no active slaves but one is waiting
1728 * on an updelay. In this case we do not need to run most of the
1729 * code for the newly enabled slave since there was no period
1730 * without an active slave and it is redundant with the disabling
1732 moving_active_iface = true;
1733 bond_choose_active_iface(port);
1735 bond_send_learning_packets(port);
1737 VLOG_WARN("interface %s: enabled", iface->name);
1738 if (port->active_iface < 0 && !moving_active_iface) {
1739 ofproto_revalidate(br->ofproto, port->no_ifaces_tag);
1740 bond_choose_active_iface(port);
1741 bond_send_learning_packets(port);
1743 iface->tag = tag_create_random();
1746 moving_active_iface = false;
1747 port->bond_compat_is_stale = true;
1751 bond_run(struct bridge *br)
1755 for (i = 0; i < br->n_ports; i++) {
1756 struct port *port = br->ports[i];
1758 if (port->n_ifaces >= 2) {
1759 for (j = 0; j < port->n_ifaces; j++) {
1760 struct iface *iface = port->ifaces[j];
1761 if (time_msec() >= iface->delay_expires) {
1762 bond_enable_slave(iface, !iface->enabled);
1767 if (port->bond_compat_is_stale) {
1768 port->bond_compat_is_stale = false;
1769 port_update_bond_compat(port);
1775 bond_wait(struct bridge *br)
1779 for (i = 0; i < br->n_ports; i++) {
1780 struct port *port = br->ports[i];
1781 if (port->n_ifaces < 2) {
1784 for (j = 0; j < port->n_ifaces; j++) {
1785 struct iface *iface = port->ifaces[j];
1786 if (iface->delay_expires != LLONG_MAX) {
1787 poll_timer_wait(iface->delay_expires - time_msec());
1794 set_dst(struct dst *p, const flow_t *flow,
1795 const struct port *in_port, const struct port *out_port,
1800 * XXX This uses too many tags: any broadcast flow will get one tag per
1801 * destination port, and thus a broadcast on a switch of any size is likely
1802 * to have all tag bits set. We should figure out a way to be smarter.
1804 * This is OK when STP is disabled, because stp_state_tag is 0 then. */
1805 *tags |= out_port->stp_state_tag;
1806 if (!(out_port->stp_state & (STP_DISABLED | STP_FORWARDING))) {
1810 p->vlan = (out_port->vlan >= 0 ? OFP_VLAN_NONE
1811 : in_port->vlan >= 0 ? in_port->vlan
1812 : ntohs(flow->dl_vlan));
1813 return choose_output_iface(out_port, flow->dl_src, &p->dp_ifidx, tags);
1817 swap_dst(struct dst *p, struct dst *q)
1819 struct dst tmp = *p;
1824 /* Moves all the dsts with vlan == 'vlan' to the front of the 'n_dsts' in
1825 * 'dsts'. (This may help performance by reducing the number of VLAN changes
1826 * that we push to the datapath. We could in fact fully sort the array by
1827 * vlan, but in most cases there are at most two different vlan tags so that's
1828 * possibly overkill.) */
1830 partition_dsts(struct dst *dsts, size_t n_dsts, int vlan)
1832 struct dst *first = dsts;
1833 struct dst *last = dsts + n_dsts;
1835 while (first != last) {
1837 * - All dsts < first have vlan == 'vlan'.
1838 * - All dsts >= last have vlan != 'vlan'.
1839 * - first < last. */
1840 while (first->vlan == vlan) {
1841 if (++first == last) {
1846 /* Same invariants, plus one additional:
1847 * - first->vlan != vlan.
1849 while (last[-1].vlan != vlan) {
1850 if (--last == first) {
1855 /* Same invariants, plus one additional:
1856 * - last[-1].vlan == vlan.*/
1857 swap_dst(first++, --last);
1862 mirror_mask_ffs(mirror_mask_t mask)
1864 BUILD_ASSERT_DECL(sizeof(unsigned int) >= sizeof(mask));
1869 dst_is_duplicate(const struct dst *dsts, size_t n_dsts,
1870 const struct dst *test)
1873 for (i = 0; i < n_dsts; i++) {
1874 if (dsts[i].vlan == test->vlan && dsts[i].dp_ifidx == test->dp_ifidx) {
1882 port_trunks_vlan(const struct port *port, uint16_t vlan)
1884 return port->vlan < 0 && bitmap_is_set(port->trunks, vlan);
1888 port_includes_vlan(const struct port *port, uint16_t vlan)
1890 return vlan == port->vlan || port_trunks_vlan(port, vlan);
1894 compose_dsts(const struct bridge *br, const flow_t *flow, uint16_t vlan,
1895 const struct port *in_port, const struct port *out_port,
1896 struct dst dsts[], tag_type *tags, uint16_t *nf_output_iface)
1898 mirror_mask_t mirrors = in_port->src_mirrors;
1899 struct dst *dst = dsts;
1902 *tags |= in_port->stp_state_tag;
1903 if (out_port == FLOOD_PORT) {
1904 /* XXX use ODP_FLOOD if no vlans or bonding. */
1905 /* XXX even better, define each VLAN as a datapath port group */
1906 for (i = 0; i < br->n_ports; i++) {
1907 struct port *port = br->ports[i];
1908 if (port != in_port && port_includes_vlan(port, vlan)
1909 && !port->is_mirror_output_port
1910 && set_dst(dst, flow, in_port, port, tags)) {
1911 mirrors |= port->dst_mirrors;
1915 *nf_output_iface = NF_OUT_FLOOD;
1916 } else if (out_port && set_dst(dst, flow, in_port, out_port, tags)) {
1917 *nf_output_iface = dst->dp_ifidx;
1918 mirrors |= out_port->dst_mirrors;
1923 struct mirror *m = br->mirrors[mirror_mask_ffs(mirrors) - 1];
1924 if (!m->n_vlans || vlan_is_mirrored(m, vlan)) {
1926 if (set_dst(dst, flow, in_port, m->out_port, tags)
1927 && !dst_is_duplicate(dsts, dst - dsts, dst)) {
1931 for (i = 0; i < br->n_ports; i++) {
1932 struct port *port = br->ports[i];
1933 if (port_includes_vlan(port, m->out_vlan)
1934 && set_dst(dst, flow, in_port, port, tags))
1938 if (port->vlan < 0) {
1939 dst->vlan = m->out_vlan;
1941 if (dst_is_duplicate(dsts, dst - dsts, dst)) {
1945 /* Use the vlan tag on the original flow instead of
1946 * the one passed in the vlan parameter. This ensures
1947 * that we compare the vlan from before any implicit
1948 * tagging tags place. This is necessary because
1949 * dst->vlan is the final vlan, after removing implicit
1951 flow_vlan = ntohs(flow->dl_vlan);
1952 if (flow_vlan == 0) {
1953 flow_vlan = OFP_VLAN_NONE;
1955 if (port == in_port && dst->vlan == flow_vlan) {
1956 /* Don't send out input port on same VLAN. */
1964 mirrors &= mirrors - 1;
1967 partition_dsts(dsts, dst - dsts, ntohs(flow->dl_vlan));
1972 print_dsts(const struct dst *dsts, size_t n)
1974 for (; n--; dsts++) {
1975 printf(">p%"PRIu16, dsts->dp_ifidx);
1976 if (dsts->vlan != OFP_VLAN_NONE) {
1977 printf("v%"PRIu16, dsts->vlan);
1983 compose_actions(struct bridge *br, const flow_t *flow, uint16_t vlan,
1984 const struct port *in_port, const struct port *out_port,
1985 tag_type *tags, struct odp_actions *actions,
1986 uint16_t *nf_output_iface)
1988 struct dst dsts[DP_MAX_PORTS * (MAX_MIRRORS + 1)];
1990 const struct dst *p;
1993 n_dsts = compose_dsts(br, flow, vlan, in_port, out_port, dsts, tags,
1996 cur_vlan = ntohs(flow->dl_vlan);
1997 for (p = dsts; p < &dsts[n_dsts]; p++) {
1998 union odp_action *a;
1999 if (p->vlan != cur_vlan) {
2000 if (p->vlan == OFP_VLAN_NONE) {
2001 odp_actions_add(actions, ODPAT_STRIP_VLAN);
2003 a = odp_actions_add(actions, ODPAT_SET_VLAN_VID);
2004 a->vlan_vid.vlan_vid = htons(p->vlan);
2008 a = odp_actions_add(actions, ODPAT_OUTPUT);
2009 a->output.port = p->dp_ifidx;
2013 /* Returns the effective vlan of a packet, taking into account both the
2014 * 802.1Q header and implicitly tagged ports. A value of 0 indicates that
2015 * the packet is untagged and -1 indicates it has an invalid header and
2016 * should be dropped. */
2017 static int flow_get_vlan(struct bridge *br, const flow_t *flow,
2018 struct port *in_port, bool have_packet)
2020 /* Note that dl_vlan of 0 and of OFP_VLAN_NONE both mean that the packet
2021 * belongs to VLAN 0, so we should treat both cases identically. (In the
2022 * former case, the packet has an 802.1Q header that specifies VLAN 0,
2023 * presumably to allow a priority to be specified. In the latter case, the
2024 * packet does not have any 802.1Q header.) */
2025 int vlan = ntohs(flow->dl_vlan);
2026 if (vlan == OFP_VLAN_NONE) {
2029 if (in_port->vlan >= 0) {
2031 /* XXX support double tagging? */
2033 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2034 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %"PRIu16" tagged "
2035 "packet received on port %s configured with "
2036 "implicit VLAN %"PRIu16,
2037 br->name, ntohs(flow->dl_vlan),
2038 in_port->name, in_port->vlan);
2042 vlan = in_port->vlan;
2044 if (!port_includes_vlan(in_port, vlan)) {
2046 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2047 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
2048 "packet received on port %s not configured for "
2050 br->name, vlan, in_port->name, vlan);
2060 update_learning_table(struct bridge *br, const flow_t *flow, int vlan,
2061 struct port *in_port)
2063 tag_type rev_tag = mac_learning_learn(br->ml, flow->dl_src,
2064 vlan, in_port->port_idx);
2066 /* The log messages here could actually be useful in debugging,
2067 * so keep the rate limit relatively high. */
2068 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30,
2070 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
2071 "on port %s in VLAN %d",
2072 br->name, ETH_ADDR_ARGS(flow->dl_src),
2073 in_port->name, vlan);
2074 ofproto_revalidate(br->ofproto, rev_tag);
2079 is_bcast_arp_reply(const flow_t *flow)
2081 return (flow->dl_type == htons(ETH_TYPE_ARP)
2082 && flow->nw_proto == ARP_OP_REPLY
2083 && eth_addr_is_broadcast(flow->dl_dst));
2086 /* If the composed actions may be applied to any packet in the given 'flow',
2087 * returns true. Otherwise, the actions should only be applied to 'packet', or
2088 * not at all, if 'packet' was NULL. */
2090 process_flow(struct bridge *br, const flow_t *flow,
2091 const struct ofpbuf *packet, struct odp_actions *actions,
2092 tag_type *tags, uint16_t *nf_output_iface)
2094 struct iface *in_iface;
2095 struct port *in_port;
2096 struct port *out_port = NULL; /* By default, drop the packet/flow. */
2100 /* Find the interface and port structure for the received packet. */
2101 in_iface = iface_from_dp_ifidx(br, flow->in_port);
2103 /* No interface? Something fishy... */
2104 if (packet != NULL) {
2105 /* Odd. A few possible reasons here:
2107 * - We deleted an interface but there are still a few packets
2108 * queued up from it.
2110 * - Someone externally added an interface (e.g. with "ovs-dpctl
2111 * add-if") that we don't know about.
2113 * - Packet arrived on the local port but the local port is not
2114 * one of our bridge ports.
2116 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2118 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
2119 "interface %"PRIu16, br->name, flow->in_port);
2122 /* Return without adding any actions, to drop packets on this flow. */
2125 in_port = in_iface->port;
2126 vlan = flow_get_vlan(br, flow, in_port, !!packet);
2131 /* Drop frames for ports that STP wants entirely killed (both for
2132 * forwarding and for learning). Later, after we do learning, we'll drop
2133 * the frames that STP wants to do learning but not forwarding on. */
2134 if (in_port->stp_state & (STP_LISTENING | STP_BLOCKING)) {
2138 /* Drop frames for reserved multicast addresses. */
2139 if (eth_addr_is_reserved(flow->dl_dst)) {
2143 /* Drop frames on ports reserved for mirroring. */
2144 if (in_port->is_mirror_output_port) {
2145 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2146 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port %s, "
2147 "which is reserved exclusively for mirroring",
2148 br->name, in_port->name);
2152 /* Packets received on bonds need special attention to avoid duplicates. */
2153 if (in_port->n_ifaces > 1) {
2156 if (eth_addr_is_multicast(flow->dl_dst)) {
2157 *tags |= in_port->active_iface_tag;
2158 if (in_port->active_iface != in_iface->port_ifidx) {
2159 /* Drop all multicast packets on inactive slaves. */
2164 /* Drop all packets for which we have learned a different input
2165 * port, because we probably sent the packet on one slave and got
2166 * it back on the other. Broadcast ARP replies are an exception
2167 * to this rule: the host has moved to another switch. */
2168 src_idx = mac_learning_lookup(br->ml, flow->dl_src, vlan);
2169 if (src_idx != -1 && src_idx != in_port->port_idx &&
2170 !is_bcast_arp_reply(flow)) {
2176 out_port = FLOOD_PORT;
2177 /* Learn source MAC (but don't try to learn from revalidation). */
2179 update_learning_table(br, flow, vlan, in_port);
2182 /* Determine output port. */
2183 out_port_idx = mac_learning_lookup_tag(br->ml, flow->dl_dst, vlan,
2185 if (out_port_idx >= 0 && out_port_idx < br->n_ports) {
2186 out_port = br->ports[out_port_idx];
2187 } else if (!packet && !eth_addr_is_multicast(flow->dl_dst)) {
2188 /* If we are revalidating but don't have a learning entry then
2189 * eject the flow. Installing a flow that floods packets opens
2190 * up a window of time where we could learn from a packet reflected
2191 * on a bond and blackhole packets before the learning table is
2192 * updated to reflect the correct port. */
2196 /* Don't send packets out their input ports. Don't forward frames that STP
2197 * wants us to discard. */
2198 if (in_port == out_port || in_port->stp_state == STP_LEARNING) {
2203 compose_actions(br, flow, vlan, in_port, out_port, tags, actions,
2209 /* Careful: 'opp' is in host byte order and opp->port_no is an OFP port
2212 bridge_port_changed_ofhook_cb(enum ofp_port_reason reason,
2213 const struct ofp_phy_port *opp,
2216 struct bridge *br = br_;
2217 struct iface *iface;
2220 iface = iface_from_dp_ifidx(br, ofp_port_to_odp_port(opp->port_no));
2226 if (reason == OFPPR_DELETE) {
2227 VLOG_WARN("bridge %s: interface %s deleted unexpectedly",
2228 br->name, iface->name);
2229 iface_destroy(iface);
2230 if (!port->n_ifaces) {
2231 VLOG_WARN("bridge %s: port %s has no interfaces, dropping",
2232 br->name, port->name);
2238 if (port->n_ifaces > 1) {
2239 bool up = !(opp->state & OFPPS_LINK_DOWN);
2240 bond_link_status_update(iface, up);
2241 port_update_bond_compat(port);
2247 bridge_normal_ofhook_cb(const flow_t *flow, const struct ofpbuf *packet,
2248 struct odp_actions *actions, tag_type *tags,
2249 uint16_t *nf_output_iface, void *br_)
2251 struct bridge *br = br_;
2254 if (flow->dl_type == htons(OFP_DL_TYPE_NOT_ETH_TYPE)
2255 && eth_addr_equals(flow->dl_dst, stp_eth_addr)) {
2256 brstp_receive(br, flow, payload);
2261 COVERAGE_INC(bridge_process_flow);
2262 return process_flow(br, flow, packet, actions, tags, nf_output_iface);
2266 bridge_account_flow_ofhook_cb(const flow_t *flow,
2267 const union odp_action *actions,
2268 size_t n_actions, unsigned long long int n_bytes,
2271 struct bridge *br = br_;
2272 struct port *in_port;
2273 const union odp_action *a;
2275 /* Feed information from the active flows back into the learning table
2276 * to ensure that table is always in sync with what is actually flowing
2277 * through the datapath. */
2278 in_port = port_from_dp_ifidx(br, flow->in_port);
2280 int vlan = flow_get_vlan(br, flow, in_port, false);
2282 update_learning_table(br, flow, vlan, in_port);
2286 if (!br->has_bonded_ports) {
2290 for (a = actions; a < &actions[n_actions]; a++) {
2291 if (a->type == ODPAT_OUTPUT) {
2292 struct port *out_port = port_from_dp_ifidx(br, a->output.port);
2293 if (out_port && out_port->n_ifaces >= 2) {
2294 struct bond_entry *e = lookup_bond_entry(out_port,
2296 e->tx_bytes += n_bytes;
2303 bridge_account_checkpoint_ofhook_cb(void *br_)
2305 struct bridge *br = br_;
2308 if (!br->has_bonded_ports) {
2312 /* The current ofproto implementation calls this callback at least once a
2313 * second, so this timer implementation is sufficient. */
2314 if (time_msec() < br->bond_next_rebalance) {
2317 br->bond_next_rebalance = time_msec() + 10000;
2319 for (i = 0; i < br->n_ports; i++) {
2320 struct port *port = br->ports[i];
2321 if (port->n_ifaces > 1) {
2322 bond_rebalance_port(port);
2327 static struct ofhooks bridge_ofhooks = {
2328 bridge_port_changed_ofhook_cb,
2329 bridge_normal_ofhook_cb,
2330 bridge_account_flow_ofhook_cb,
2331 bridge_account_checkpoint_ofhook_cb,
2334 /* Bonding functions. */
2336 /* Statistics for a single interface on a bonded port, used for load-based
2337 * bond rebalancing. */
2338 struct slave_balance {
2339 struct iface *iface; /* The interface. */
2340 uint64_t tx_bytes; /* Sum of hashes[*]->tx_bytes. */
2342 /* All the "bond_entry"s that are assigned to this interface, in order of
2343 * increasing tx_bytes. */
2344 struct bond_entry **hashes;
2348 /* Sorts pointers to pointers to bond_entries in ascending order by the
2349 * interface to which they are assigned, and within a single interface in
2350 * ascending order of bytes transmitted. */
2352 compare_bond_entries(const void *a_, const void *b_)
2354 const struct bond_entry *const *ap = a_;
2355 const struct bond_entry *const *bp = b_;
2356 const struct bond_entry *a = *ap;
2357 const struct bond_entry *b = *bp;
2358 if (a->iface_idx != b->iface_idx) {
2359 return a->iface_idx > b->iface_idx ? 1 : -1;
2360 } else if (a->tx_bytes != b->tx_bytes) {
2361 return a->tx_bytes > b->tx_bytes ? 1 : -1;
2367 /* Sorts slave_balances so that enabled ports come first, and otherwise in
2368 * *descending* order by number of bytes transmitted. */
2370 compare_slave_balance(const void *a_, const void *b_)
2372 const struct slave_balance *a = a_;
2373 const struct slave_balance *b = b_;
2374 if (a->iface->enabled != b->iface->enabled) {
2375 return a->iface->enabled ? -1 : 1;
2376 } else if (a->tx_bytes != b->tx_bytes) {
2377 return a->tx_bytes > b->tx_bytes ? -1 : 1;
2384 swap_bals(struct slave_balance *a, struct slave_balance *b)
2386 struct slave_balance tmp = *a;
2391 /* Restores the 'n_bals' slave_balance structures in 'bals' to sorted order
2392 * given that 'p' (and only 'p') might be in the wrong location.
2394 * This function invalidates 'p', since it might now be in a different memory
2397 resort_bals(struct slave_balance *p,
2398 struct slave_balance bals[], size_t n_bals)
2401 for (; p > bals && p->tx_bytes > p[-1].tx_bytes; p--) {
2402 swap_bals(p, p - 1);
2404 for (; p < &bals[n_bals - 1] && p->tx_bytes < p[1].tx_bytes; p++) {
2405 swap_bals(p, p + 1);
2411 log_bals(const struct slave_balance *bals, size_t n_bals, struct port *port)
2413 if (VLOG_IS_DBG_ENABLED()) {
2414 struct ds ds = DS_EMPTY_INITIALIZER;
2415 const struct slave_balance *b;
2417 for (b = bals; b < bals + n_bals; b++) {
2421 ds_put_char(&ds, ',');
2423 ds_put_format(&ds, " %s %"PRIu64"kB",
2424 b->iface->name, b->tx_bytes / 1024);
2426 if (!b->iface->enabled) {
2427 ds_put_cstr(&ds, " (disabled)");
2429 if (b->n_hashes > 0) {
2430 ds_put_cstr(&ds, " (");
2431 for (i = 0; i < b->n_hashes; i++) {
2432 const struct bond_entry *e = b->hashes[i];
2434 ds_put_cstr(&ds, " + ");
2436 ds_put_format(&ds, "h%td: %"PRIu64"kB",
2437 e - port->bond_hash, e->tx_bytes / 1024);
2439 ds_put_cstr(&ds, ")");
2442 VLOG_DBG("bond %s:%s", port->name, ds_cstr(&ds));
2447 /* Shifts 'hash' from 'from' to 'to' within 'port'. */
2449 bond_shift_load(struct slave_balance *from, struct slave_balance *to,
2452 struct bond_entry *hash = from->hashes[hash_idx];
2453 struct port *port = from->iface->port;
2454 uint64_t delta = hash->tx_bytes;
2456 VLOG_INFO("bond %s: shift %"PRIu64"kB of load (with hash %td) "
2457 "from %s to %s (now carrying %"PRIu64"kB and "
2458 "%"PRIu64"kB load, respectively)",
2459 port->name, delta / 1024, hash - port->bond_hash,
2460 from->iface->name, to->iface->name,
2461 (from->tx_bytes - delta) / 1024,
2462 (to->tx_bytes + delta) / 1024);
2464 /* Delete element from from->hashes.
2466 * We don't bother to add the element to to->hashes because not only would
2467 * it require more work, the only purpose it would be to allow that hash to
2468 * be migrated to another slave in this rebalancing run, and there is no
2469 * point in doing that. */
2470 if (hash_idx == 0) {
2473 memmove(from->hashes + hash_idx, from->hashes + hash_idx + 1,
2474 (from->n_hashes - (hash_idx + 1)) * sizeof *from->hashes);
2478 /* Shift load away from 'from' to 'to'. */
2479 from->tx_bytes -= delta;
2480 to->tx_bytes += delta;
2482 /* Arrange for flows to be revalidated. */
2483 ofproto_revalidate(port->bridge->ofproto, hash->iface_tag);
2484 hash->iface_idx = to->iface->port_ifidx;
2485 hash->iface_tag = tag_create_random();
2489 bond_rebalance_port(struct port *port)
2491 struct slave_balance bals[DP_MAX_PORTS];
2493 struct bond_entry *hashes[BOND_MASK + 1];
2494 struct slave_balance *b, *from, *to;
2495 struct bond_entry *e;
2498 /* Sets up 'bals' to describe each of the port's interfaces, sorted in
2499 * descending order of tx_bytes, so that bals[0] represents the most
2500 * heavily loaded slave and bals[n_bals - 1] represents the least heavily
2503 * The code is a bit tricky: to avoid dynamically allocating a 'hashes'
2504 * array for each slave_balance structure, we sort our local array of
2505 * hashes in order by slave, so that all of the hashes for a given slave
2506 * become contiguous in memory, and then we point each 'hashes' members of
2507 * a slave_balance structure to the start of a contiguous group. */
2508 n_bals = port->n_ifaces;
2509 for (b = bals; b < &bals[n_bals]; b++) {
2510 b->iface = port->ifaces[b - bals];
2515 for (i = 0; i <= BOND_MASK; i++) {
2516 hashes[i] = &port->bond_hash[i];
2518 qsort(hashes, BOND_MASK + 1, sizeof *hashes, compare_bond_entries);
2519 for (i = 0; i <= BOND_MASK; i++) {
2521 if (e->iface_idx >= 0 && e->iface_idx < port->n_ifaces) {
2522 b = &bals[e->iface_idx];
2523 b->tx_bytes += e->tx_bytes;
2525 b->hashes = &hashes[i];
2530 qsort(bals, n_bals, sizeof *bals, compare_slave_balance);
2531 log_bals(bals, n_bals, port);
2533 /* Discard slaves that aren't enabled (which were sorted to the back of the
2534 * array earlier). */
2535 while (!bals[n_bals - 1].iface->enabled) {
2542 /* Shift load from the most-loaded slaves to the least-loaded slaves. */
2543 to = &bals[n_bals - 1];
2544 for (from = bals; from < to; ) {
2545 uint64_t overload = from->tx_bytes - to->tx_bytes;
2546 if (overload < to->tx_bytes >> 5 || overload < 100000) {
2547 /* The extra load on 'from' (and all less-loaded slaves), compared
2548 * to that of 'to' (the least-loaded slave), is less than ~3%, or
2549 * it is less than ~1Mbps. No point in rebalancing. */
2551 } else if (from->n_hashes == 1) {
2552 /* 'from' only carries a single MAC hash, so we can't shift any
2553 * load away from it, even though we want to. */
2556 /* 'from' is carrying significantly more load than 'to', and that
2557 * load is split across at least two different hashes. Pick a hash
2558 * to migrate to 'to' (the least-loaded slave), given that doing so
2559 * must decrease the ratio of the load on the two slaves by at
2562 * The sort order we use means that we prefer to shift away the
2563 * smallest hashes instead of the biggest ones. There is little
2564 * reason behind this decision; we could use the opposite sort
2565 * order to shift away big hashes ahead of small ones. */
2569 for (i = 0; i < from->n_hashes; i++) {
2570 double old_ratio, new_ratio;
2571 uint64_t delta = from->hashes[i]->tx_bytes;
2573 if (delta == 0 || from->tx_bytes - delta == 0) {
2574 /* Pointless move. */
2578 order_swapped = from->tx_bytes - delta < to->tx_bytes + delta;
2580 if (to->tx_bytes == 0) {
2581 /* Nothing on the new slave, move it. */
2585 old_ratio = (double)from->tx_bytes / to->tx_bytes;
2586 new_ratio = (double)(from->tx_bytes - delta) /
2587 (to->tx_bytes + delta);
2589 if (new_ratio == 0) {
2590 /* Should already be covered but check to prevent division
2595 if (new_ratio < 1) {
2596 new_ratio = 1 / new_ratio;
2599 if (old_ratio - new_ratio > 0.1) {
2600 /* Would decrease the ratio, move it. */
2604 if (i < from->n_hashes) {
2605 bond_shift_load(from, to, i);
2606 port->bond_compat_is_stale = true;
2608 /* If the result of the migration changed the relative order of
2609 * 'from' and 'to' swap them back to maintain invariants. */
2610 if (order_swapped) {
2611 swap_bals(from, to);
2614 /* Re-sort 'bals'. Note that this may make 'from' and 'to'
2615 * point to different slave_balance structures. It is only
2616 * valid to do these two operations in a row at all because we
2617 * know that 'from' will not move past 'to' and vice versa. */
2618 resort_bals(from, bals, n_bals);
2619 resort_bals(to, bals, n_bals);
2626 /* Implement exponentially weighted moving average. A weight of 1/2 causes
2627 * historical data to decay to <1% in 7 rebalancing runs. */
2628 for (e = &port->bond_hash[0]; e <= &port->bond_hash[BOND_MASK]; e++) {
2634 bond_send_learning_packets(struct port *port)
2636 struct bridge *br = port->bridge;
2637 struct mac_entry *e;
2638 struct ofpbuf packet;
2639 int error, n_packets, n_errors;
2641 if (!port->n_ifaces || port->active_iface < 0) {
2645 ofpbuf_init(&packet, 128);
2646 error = n_packets = n_errors = 0;
2647 LIST_FOR_EACH (e, struct mac_entry, lru_node, &br->ml->lrus) {
2648 union ofp_action actions[2], *a;
2654 if (e->port == port->port_idx
2655 || !choose_output_iface(port, e->mac, &dp_ifidx, &tags)) {
2659 /* Compose actions. */
2660 memset(actions, 0, sizeof actions);
2663 a->vlan_vid.type = htons(OFPAT_SET_VLAN_VID);
2664 a->vlan_vid.len = htons(sizeof *a);
2665 a->vlan_vid.vlan_vid = htons(e->vlan);
2668 a->output.type = htons(OFPAT_OUTPUT);
2669 a->output.len = htons(sizeof *a);
2670 a->output.port = htons(odp_port_to_ofp_port(dp_ifidx));
2675 compose_benign_packet(&packet, "Open vSwitch Bond Failover", 0xf177,
2677 flow_extract(&packet, ODPP_NONE, &flow);
2678 retval = ofproto_send_packet(br->ofproto, &flow, actions, a - actions,
2685 ofpbuf_uninit(&packet);
2688 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2689 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
2690 "packets, last error was: %s",
2691 port->name, n_errors, n_packets, strerror(error));
2693 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
2694 port->name, n_packets);
2698 /* Bonding unixctl user interface functions. */
2701 bond_unixctl_list(struct unixctl_conn *conn, const char *args UNUSED)
2703 struct ds ds = DS_EMPTY_INITIALIZER;
2704 const struct bridge *br;
2706 ds_put_cstr(&ds, "bridge\tbond\tslaves\n");
2708 LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
2711 for (i = 0; i < br->n_ports; i++) {
2712 const struct port *port = br->ports[i];
2713 if (port->n_ifaces > 1) {
2716 ds_put_format(&ds, "%s\t%s\t", br->name, port->name);
2717 for (j = 0; j < port->n_ifaces; j++) {
2718 const struct iface *iface = port->ifaces[j];
2720 ds_put_cstr(&ds, ", ");
2722 ds_put_cstr(&ds, iface->name);
2724 ds_put_char(&ds, '\n');
2728 unixctl_command_reply(conn, 200, ds_cstr(&ds));
2732 static struct port *
2733 bond_find(const char *name)
2735 const struct bridge *br;
2737 LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
2740 for (i = 0; i < br->n_ports; i++) {
2741 struct port *port = br->ports[i];
2742 if (!strcmp(port->name, name) && port->n_ifaces > 1) {
2751 bond_unixctl_show(struct unixctl_conn *conn, const char *args)
2753 struct ds ds = DS_EMPTY_INITIALIZER;
2754 const struct port *port;
2757 port = bond_find(args);
2759 unixctl_command_reply(conn, 501, "no such bond");
2763 ds_put_format(&ds, "updelay: %d ms\n", port->updelay);
2764 ds_put_format(&ds, "downdelay: %d ms\n", port->downdelay);
2765 ds_put_format(&ds, "next rebalance: %lld ms\n",
2766 port->bridge->bond_next_rebalance - time_msec());
2767 for (j = 0; j < port->n_ifaces; j++) {
2768 const struct iface *iface = port->ifaces[j];
2769 struct bond_entry *be;
2772 ds_put_format(&ds, "slave %s: %s\n",
2773 iface->name, iface->enabled ? "enabled" : "disabled");
2774 if (j == port->active_iface) {
2775 ds_put_cstr(&ds, "\tactive slave\n");
2777 if (iface->delay_expires != LLONG_MAX) {
2778 ds_put_format(&ds, "\t%s expires in %lld ms\n",
2779 iface->enabled ? "downdelay" : "updelay",
2780 iface->delay_expires - time_msec());
2784 for (be = port->bond_hash; be <= &port->bond_hash[BOND_MASK]; be++) {
2785 int hash = be - port->bond_hash;
2786 struct mac_entry *me;
2788 if (be->iface_idx != j) {
2792 ds_put_format(&ds, "\thash %d: %"PRIu64" kB load\n",
2793 hash, be->tx_bytes / 1024);
2796 LIST_FOR_EACH (me, struct mac_entry, lru_node,
2797 &port->bridge->ml->lrus) {
2800 if (bond_hash(me->mac) == hash
2801 && me->port != port->port_idx
2802 && choose_output_iface(port, me->mac, &dp_ifidx, &tags)
2803 && dp_ifidx == iface->dp_ifidx)
2805 ds_put_format(&ds, "\t\t"ETH_ADDR_FMT"\n",
2806 ETH_ADDR_ARGS(me->mac));
2811 unixctl_command_reply(conn, 200, ds_cstr(&ds));
2816 bond_unixctl_migrate(struct unixctl_conn *conn, const char *args_)
2818 char *args = (char *) args_;
2819 char *save_ptr = NULL;
2820 char *bond_s, *hash_s, *slave_s;
2821 uint8_t mac[ETH_ADDR_LEN];
2823 struct iface *iface;
2824 struct bond_entry *entry;
2827 bond_s = strtok_r(args, " ", &save_ptr);
2828 hash_s = strtok_r(NULL, " ", &save_ptr);
2829 slave_s = strtok_r(NULL, " ", &save_ptr);
2831 unixctl_command_reply(conn, 501,
2832 "usage: bond/migrate BOND HASH SLAVE");
2836 port = bond_find(bond_s);
2838 unixctl_command_reply(conn, 501, "no such bond");
2842 if (sscanf(hash_s, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))
2843 == ETH_ADDR_SCAN_COUNT) {
2844 hash = bond_hash(mac);
2845 } else if (strspn(hash_s, "0123456789") == strlen(hash_s)) {
2846 hash = atoi(hash_s) & BOND_MASK;
2848 unixctl_command_reply(conn, 501, "bad hash");
2852 iface = port_lookup_iface(port, slave_s);
2854 unixctl_command_reply(conn, 501, "no such slave");
2858 if (!iface->enabled) {
2859 unixctl_command_reply(conn, 501, "cannot migrate to disabled slave");
2863 entry = &port->bond_hash[hash];
2864 ofproto_revalidate(port->bridge->ofproto, entry->iface_tag);
2865 entry->iface_idx = iface->port_ifidx;
2866 entry->iface_tag = tag_create_random();
2867 port->bond_compat_is_stale = true;
2868 unixctl_command_reply(conn, 200, "migrated");
2872 bond_unixctl_set_active_slave(struct unixctl_conn *conn, const char *args_)
2874 char *args = (char *) args_;
2875 char *save_ptr = NULL;
2876 char *bond_s, *slave_s;
2878 struct iface *iface;
2880 bond_s = strtok_r(args, " ", &save_ptr);
2881 slave_s = strtok_r(NULL, " ", &save_ptr);
2883 unixctl_command_reply(conn, 501,
2884 "usage: bond/set-active-slave BOND SLAVE");
2888 port = bond_find(bond_s);
2890 unixctl_command_reply(conn, 501, "no such bond");
2894 iface = port_lookup_iface(port, slave_s);
2896 unixctl_command_reply(conn, 501, "no such slave");
2900 if (!iface->enabled) {
2901 unixctl_command_reply(conn, 501, "cannot make disabled slave active");
2905 if (port->active_iface != iface->port_ifidx) {
2906 ofproto_revalidate(port->bridge->ofproto, port->active_iface_tag);
2907 port->active_iface = iface->port_ifidx;
2908 port->active_iface_tag = tag_create_random();
2909 VLOG_INFO("port %s: active interface is now %s",
2910 port->name, iface->name);
2911 bond_send_learning_packets(port);
2912 unixctl_command_reply(conn, 200, "done");
2914 unixctl_command_reply(conn, 200, "no change");
2919 enable_slave(struct unixctl_conn *conn, const char *args_, bool enable)
2921 char *args = (char *) args_;
2922 char *save_ptr = NULL;
2923 char *bond_s, *slave_s;
2925 struct iface *iface;
2927 bond_s = strtok_r(args, " ", &save_ptr);
2928 slave_s = strtok_r(NULL, " ", &save_ptr);
2930 unixctl_command_reply(conn, 501,
2931 "usage: bond/enable/disable-slave BOND SLAVE");
2935 port = bond_find(bond_s);
2937 unixctl_command_reply(conn, 501, "no such bond");
2941 iface = port_lookup_iface(port, slave_s);
2943 unixctl_command_reply(conn, 501, "no such slave");
2947 bond_enable_slave(iface, enable);
2948 unixctl_command_reply(conn, 501, enable ? "enabled" : "disabled");
2952 bond_unixctl_enable_slave(struct unixctl_conn *conn, const char *args)
2954 enable_slave(conn, args, true);
2958 bond_unixctl_disable_slave(struct unixctl_conn *conn, const char *args)
2960 enable_slave(conn, args, false);
2964 bond_unixctl_hash(struct unixctl_conn *conn, const char *args)
2966 uint8_t mac[ETH_ADDR_LEN];
2970 if (sscanf(args, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))
2971 == ETH_ADDR_SCAN_COUNT) {
2972 hash = bond_hash(mac);
2974 hash_cstr = xasprintf("%u", hash);
2975 unixctl_command_reply(conn, 200, hash_cstr);
2978 unixctl_command_reply(conn, 501, "invalid mac");
2985 unixctl_command_register("bond/list", bond_unixctl_list);
2986 unixctl_command_register("bond/show", bond_unixctl_show);
2987 unixctl_command_register("bond/migrate", bond_unixctl_migrate);
2988 unixctl_command_register("bond/set-active-slave",
2989 bond_unixctl_set_active_slave);
2990 unixctl_command_register("bond/enable-slave", bond_unixctl_enable_slave);
2991 unixctl_command_register("bond/disable-slave", bond_unixctl_disable_slave);
2992 unixctl_command_register("bond/hash", bond_unixctl_hash);
2995 /* Port functions. */
2998 port_create(struct bridge *br, const char *name)
3002 port = xcalloc(1, sizeof *port);
3004 port->port_idx = br->n_ports;
3006 port->trunks = NULL;
3007 port->name = xstrdup(name);
3008 port->active_iface = -1;
3009 port->stp_state = STP_DISABLED;
3010 port->stp_state_tag = 0;
3012 if (br->n_ports >= br->allocated_ports) {
3013 br->ports = x2nrealloc(br->ports, &br->allocated_ports,
3016 br->ports[br->n_ports++] = port;
3018 VLOG_INFO("created port %s on bridge %s", port->name, br->name);
3023 port_reconfigure(struct port *port)
3025 bool bonded = cfg_has_section("bonding.%s", port->name);
3026 struct svec old_ifaces, new_ifaces;
3027 unsigned long *trunks;
3031 /* Collect old and new interfaces. */
3032 svec_init(&old_ifaces);
3033 svec_init(&new_ifaces);
3034 for (i = 0; i < port->n_ifaces; i++) {
3035 svec_add(&old_ifaces, port->ifaces[i]->name);
3037 svec_sort(&old_ifaces);
3039 cfg_get_all_keys(&new_ifaces, "bonding.%s.slave", port->name);
3040 if (!new_ifaces.n) {
3041 VLOG_ERR("port %s: no interfaces specified for bonded port",
3043 } else if (new_ifaces.n == 1) {
3044 VLOG_WARN("port %s: only 1 interface specified for bonded port",
3048 port->updelay = cfg_get_int(0, "bonding.%s.updelay", port->name);
3049 if (port->updelay < 0) {
3052 port->downdelay = cfg_get_int(0, "bonding.%s.downdelay", port->name);
3053 if (port->downdelay < 0) {
3054 port->downdelay = 0;
3057 svec_init(&new_ifaces);
3058 svec_add(&new_ifaces, port->name);
3061 /* Get rid of deleted interfaces and add new interfaces. */
3062 for (i = 0; i < port->n_ifaces; i++) {
3063 struct iface *iface = port->ifaces[i];
3064 if (!svec_contains(&new_ifaces, iface->name)) {
3065 iface_destroy(iface);
3070 for (i = 0; i < new_ifaces.n; i++) {
3071 const char *name = new_ifaces.names[i];
3072 if (!svec_contains(&old_ifaces, name)) {
3073 iface_create(port, name);
3079 if (cfg_has("vlan.%s.tag", port->name)) {
3081 vlan = cfg_get_vlan(0, "vlan.%s.tag", port->name);
3082 if (vlan >= 0 && vlan <= 4095) {
3083 VLOG_DBG("port %s: assigning VLAN tag %d", port->name, vlan);
3086 /* It's possible that bonded, VLAN-tagged ports make sense. Maybe
3087 * they even work as-is. But they have not been tested. */
3088 VLOG_WARN("port %s: VLAN tags not supported on bonded ports",
3092 if (port->vlan != vlan) {
3094 bridge_flush(port->bridge);
3097 /* Get trunked VLANs. */
3100 size_t n_trunks, n_errors;
3103 trunks = bitmap_allocate(4096);
3104 n_trunks = cfg_count("vlan.%s.trunks", port->name);
3106 for (i = 0; i < n_trunks; i++) {
3107 int trunk = cfg_get_vlan(i, "vlan.%s.trunks", port->name);
3109 bitmap_set1(trunks, trunk);
3115 VLOG_ERR("port %s: invalid values for %zu trunk VLANs",
3116 port->name, n_trunks);
3118 if (n_errors == n_trunks) {
3120 VLOG_ERR("port %s: no valid trunks, trunking all VLANs",
3123 bitmap_set_multiple(trunks, 0, 4096, 1);
3126 if (cfg_has("vlan.%s.trunks", port->name)) {
3127 VLOG_ERR("ignoring vlan.%s.trunks in favor of vlan.%s.vlan",
3128 port->name, port->name);
3132 ? port->trunks != NULL
3133 : port->trunks == NULL || !bitmap_equal(trunks, port->trunks, 4096)) {
3134 bridge_flush(port->bridge);
3136 bitmap_free(port->trunks);
3137 port->trunks = trunks;
3139 svec_destroy(&old_ifaces);
3140 svec_destroy(&new_ifaces);
3144 port_destroy(struct port *port)
3147 struct bridge *br = port->bridge;
3151 proc_net_compat_update_vlan(port->name, NULL, 0);
3152 proc_net_compat_update_bond(port->name, NULL);
3154 for (i = 0; i < MAX_MIRRORS; i++) {
3155 struct mirror *m = br->mirrors[i];
3156 if (m && m->out_port == port) {
3161 while (port->n_ifaces > 0) {
3162 iface_destroy(port->ifaces[port->n_ifaces - 1]);
3165 del = br->ports[port->port_idx] = br->ports[--br->n_ports];
3166 del->port_idx = port->port_idx;
3169 bitmap_free(port->trunks);
3176 static struct port *
3177 port_from_dp_ifidx(const struct bridge *br, uint16_t dp_ifidx)
3179 struct iface *iface = iface_from_dp_ifidx(br, dp_ifidx);
3180 return iface ? iface->port : NULL;
3183 static struct port *
3184 port_lookup(const struct bridge *br, const char *name)
3188 for (i = 0; i < br->n_ports; i++) {
3189 struct port *port = br->ports[i];
3190 if (!strcmp(port->name, name)) {
3197 static struct iface *
3198 port_lookup_iface(const struct port *port, const char *name)
3202 for (j = 0; j < port->n_ifaces; j++) {
3203 struct iface *iface = port->ifaces[j];
3204 if (!strcmp(iface->name, name)) {
3212 port_update_bonding(struct port *port)
3214 if (port->n_ifaces < 2) {
3215 /* Not a bonded port. */
3216 if (port->bond_hash) {
3217 free(port->bond_hash);
3218 port->bond_hash = NULL;
3219 port->bond_compat_is_stale = true;
3222 if (!port->bond_hash) {
3225 port->bond_hash = xcalloc(BOND_MASK + 1, sizeof *port->bond_hash);
3226 for (i = 0; i <= BOND_MASK; i++) {
3227 struct bond_entry *e = &port->bond_hash[i];
3231 port->no_ifaces_tag = tag_create_random();
3232 bond_choose_active_iface(port);
3234 port->bond_compat_is_stale = true;
3239 port_update_bond_compat(struct port *port)
3241 struct compat_bond_hash compat_hashes[BOND_MASK + 1];
3242 struct compat_bond bond;
3245 if (port->n_ifaces < 2) {
3246 proc_net_compat_update_bond(port->name, NULL);
3251 bond.updelay = port->updelay;
3252 bond.downdelay = port->downdelay;
3255 bond.hashes = compat_hashes;
3256 if (port->bond_hash) {
3257 const struct bond_entry *e;
3258 for (e = port->bond_hash; e <= &port->bond_hash[BOND_MASK]; e++) {
3259 if (e->iface_idx >= 0 && e->iface_idx < port->n_ifaces) {
3260 struct compat_bond_hash *cbh = &bond.hashes[bond.n_hashes++];
3261 cbh->hash = e - port->bond_hash;
3262 cbh->netdev_name = port->ifaces[e->iface_idx]->name;
3267 bond.n_slaves = port->n_ifaces;
3268 bond.slaves = xmalloc(port->n_ifaces * sizeof *bond.slaves);
3269 for (i = 0; i < port->n_ifaces; i++) {
3270 struct iface *iface = port->ifaces[i];
3271 struct compat_bond_slave *slave = &bond.slaves[i];
3272 slave->name = iface->name;
3274 /* We need to make the same determination as the Linux bonding
3275 * code to determine whether a slave should be consider "up".
3276 * The Linux function bond_miimon_inspect() supports four
3277 * BOND_LINK_* states:
3279 * - BOND_LINK_UP: carrier detected, updelay has passed.
3280 * - BOND_LINK_FAIL: carrier lost, downdelay in progress.
3281 * - BOND_LINK_DOWN: carrier lost, downdelay has passed.
3282 * - BOND_LINK_BACK: carrier detected, updelay in progress.
3284 * The function bond_info_show_slave() only considers BOND_LINK_UP
3285 * to be "up" and anything else to be "down".
3287 slave->up = iface->enabled && iface->delay_expires == LLONG_MAX;
3291 netdev_get_etheraddr(iface->netdev, slave->mac);
3294 if (cfg_get_bool(0, "bonding.%s.fake-iface", port->name)) {
3295 struct netdev *bond_netdev;
3297 if (!netdev_open(port->name, NETDEV_ETH_TYPE_NONE, &bond_netdev)) {
3299 netdev_turn_flags_on(bond_netdev, NETDEV_UP, true);
3301 netdev_turn_flags_off(bond_netdev, NETDEV_UP, true);
3303 netdev_close(bond_netdev);
3307 proc_net_compat_update_bond(port->name, &bond);
3312 port_update_vlan_compat(struct port *port)
3314 struct bridge *br = port->bridge;
3315 char *vlandev_name = NULL;
3317 if (port->vlan > 0) {
3318 /* Figure out the name that the VLAN device should actually have, if it
3319 * existed. This takes some work because the VLAN device would not
3320 * have port->name in its name; rather, it would have the trunk port's
3321 * name, and 'port' would be attached to a bridge that also had the
3322 * VLAN device one of its ports. So we need to find a trunk port that
3323 * includes port->vlan.
3325 * There might be more than one candidate. This doesn't happen on
3326 * XenServer, so if it happens we just pick the first choice in
3327 * alphabetical order instead of creating multiple VLAN devices. */
3329 for (i = 0; i < br->n_ports; i++) {
3330 struct port *p = br->ports[i];
3331 if (port_trunks_vlan(p, port->vlan)
3333 && (!vlandev_name || strcmp(p->name, vlandev_name) <= 0))
3335 uint8_t ea[ETH_ADDR_LEN];
3336 netdev_get_etheraddr(p->ifaces[0]->netdev, ea);
3337 if (!eth_addr_is_multicast(ea) &&
3338 !eth_addr_is_reserved(ea) &&
3339 !eth_addr_is_zero(ea)) {
3340 vlandev_name = p->name;
3345 proc_net_compat_update_vlan(port->name, vlandev_name, port->vlan);
3348 /* Interface functions. */
3351 iface_create(struct port *port, const char *name)
3353 struct iface *iface;
3355 iface = xcalloc(1, sizeof *iface);
3357 iface->port_ifidx = port->n_ifaces;
3358 iface->name = xstrdup(name);
3359 iface->dp_ifidx = -1;
3360 iface->tag = tag_create_random();
3361 iface->delay_expires = LLONG_MAX;
3362 iface->netdev = NULL;
3364 if (port->n_ifaces >= port->allocated_ifaces) {
3365 port->ifaces = x2nrealloc(port->ifaces, &port->allocated_ifaces,
3366 sizeof *port->ifaces);
3368 port->ifaces[port->n_ifaces++] = iface;
3369 if (port->n_ifaces > 1) {
3370 port->bridge->has_bonded_ports = true;
3373 VLOG_DBG("attached network device %s to port %s", iface->name, port->name);
3375 bridge_flush(port->bridge);
3379 iface_destroy(struct iface *iface)
3382 struct port *port = iface->port;
3383 struct bridge *br = port->bridge;
3384 bool del_active = port->active_iface == iface->port_ifidx;
3387 if (iface->dp_ifidx >= 0) {
3388 port_array_set(&br->ifaces, iface->dp_ifidx, NULL);
3391 del = port->ifaces[iface->port_ifidx] = port->ifaces[--port->n_ifaces];
3392 del->port_ifidx = iface->port_ifidx;
3394 netdev_close(iface->netdev);
3399 ofproto_revalidate(port->bridge->ofproto, port->active_iface_tag);
3400 bond_choose_active_iface(port);
3401 bond_send_learning_packets(port);
3404 bridge_flush(port->bridge);
3408 static struct iface *
3409 iface_lookup(const struct bridge *br, const char *name)
3413 for (i = 0; i < br->n_ports; i++) {
3414 struct port *port = br->ports[i];
3415 for (j = 0; j < port->n_ifaces; j++) {
3416 struct iface *iface = port->ifaces[j];
3417 if (!strcmp(iface->name, name)) {
3425 static struct iface *
3426 iface_from_dp_ifidx(const struct bridge *br, uint16_t dp_ifidx)
3428 return port_array_get(&br->ifaces, dp_ifidx);
3431 /* Returns true if 'iface' is the name of an "internal" interface on bridge
3432 * 'br', that is, an interface that is entirely simulated within the datapath.
3433 * The local port (ODPP_LOCAL) is always an internal interface. Other local
3434 * interfaces are created by setting "iface.<iface>.internal = true".
3436 * In addition, we have a kluge-y feature that creates an internal port with
3437 * the name of a bonded port if "bonding.<bondname>.fake-iface = true" is set.
3438 * This feature needs to go away in the long term. Until then, this is one
3439 * reason why this function takes a name instead of a struct iface: the fake
3440 * interfaces created this way do not have a struct iface. */
3442 iface_is_internal(const struct bridge *br, const char *iface)
3444 if (!strcmp(iface, br->name)
3445 || cfg_get_bool(0, "iface.%s.internal", iface)) {
3449 if (cfg_get_bool(0, "bonding.%s.fake-iface", iface)) {
3450 struct port *port = port_lookup(br, iface);
3451 if (port && port->n_ifaces > 1) {
3459 /* Set Ethernet address of 'iface', if one is specified in the configuration
3462 iface_set_mac(struct iface *iface)
3464 uint64_t mac = cfg_get_mac(0, "iface.%s.mac", iface->name);
3466 static uint8_t ea[ETH_ADDR_LEN];
3468 eth_addr_from_uint64(mac, ea);
3469 if (eth_addr_is_multicast(ea)) {
3470 VLOG_ERR("interface %s: cannot set MAC to multicast address",
3472 } else if (iface->dp_ifidx == ODPP_LOCAL) {
3473 VLOG_ERR("ignoring iface.%s.mac; use bridge.%s.mac instead",
3474 iface->name, iface->name);
3476 int error = netdev_set_etheraddr(iface->netdev, ea);
3478 VLOG_ERR("interface %s: setting MAC failed (%s)",
3479 iface->name, strerror(error));
3485 /* Port mirroring. */
3488 mirror_reconfigure(struct bridge *br)
3490 struct svec old_mirrors, new_mirrors;
3491 size_t i, n_rspan_vlans;
3492 unsigned long *rspan_vlans;
3494 /* Collect old and new mirrors. */
3495 svec_init(&old_mirrors);
3496 svec_init(&new_mirrors);
3497 cfg_get_subsections(&new_mirrors, "mirror.%s", br->name);
3498 for (i = 0; i < MAX_MIRRORS; i++) {
3499 if (br->mirrors[i]) {
3500 svec_add(&old_mirrors, br->mirrors[i]->name);
3504 /* Get rid of deleted mirrors and add new mirrors. */
3505 svec_sort(&old_mirrors);
3506 assert(svec_is_unique(&old_mirrors));
3507 svec_sort(&new_mirrors);
3508 assert(svec_is_unique(&new_mirrors));
3509 for (i = 0; i < MAX_MIRRORS; i++) {
3510 struct mirror *m = br->mirrors[i];
3511 if (m && !svec_contains(&new_mirrors, m->name)) {
3515 for (i = 0; i < new_mirrors.n; i++) {
3516 const char *name = new_mirrors.names[i];
3517 if (!svec_contains(&old_mirrors, name)) {
3518 mirror_create(br, name);
3521 svec_destroy(&old_mirrors);
3522 svec_destroy(&new_mirrors);
3524 /* Reconfigure all mirrors. */
3525 for (i = 0; i < MAX_MIRRORS; i++) {
3526 if (br->mirrors[i]) {
3527 mirror_reconfigure_one(br->mirrors[i]);
3531 /* Update port reserved status. */
3532 for (i = 0; i < br->n_ports; i++) {
3533 br->ports[i]->is_mirror_output_port = false;
3535 for (i = 0; i < MAX_MIRRORS; i++) {
3536 struct mirror *m = br->mirrors[i];
3537 if (m && m->out_port) {
3538 m->out_port->is_mirror_output_port = true;
3542 /* Update learning disabled vlans (for RSPAN). */
3544 n_rspan_vlans = cfg_count("vlan.%s.disable-learning", br->name);
3545 if (n_rspan_vlans) {
3546 rspan_vlans = bitmap_allocate(4096);
3548 for (i = 0; i < n_rspan_vlans; i++) {
3549 int vlan = cfg_get_vlan(i, "vlan.%s.disable-learning", br->name);
3551 bitmap_set1(rspan_vlans, vlan);
3552 VLOG_INFO("bridge %s: disabling learning on vlan %d\n",
3555 VLOG_ERR("bridge %s: invalid value '%s' for learning disabled "
3557 cfg_get_string(i, "vlan.%s.disable-learning", br->name));
3561 if (mac_learning_set_disabled_vlans(br->ml, rspan_vlans)) {
3567 mirror_create(struct bridge *br, const char *name)
3572 for (i = 0; ; i++) {
3573 if (i >= MAX_MIRRORS) {
3574 VLOG_WARN("bridge %s: maximum of %d port mirrors reached, "
3575 "cannot create %s", br->name, MAX_MIRRORS, name);
3578 if (!br->mirrors[i]) {
3583 VLOG_INFO("created port mirror %s on bridge %s", name, br->name);
3586 br->mirrors[i] = m = xcalloc(1, sizeof *m);
3589 m->name = xstrdup(name);
3590 svec_init(&m->src_ports);
3591 svec_init(&m->dst_ports);
3599 mirror_destroy(struct mirror *m)
3602 struct bridge *br = m->bridge;
3605 for (i = 0; i < br->n_ports; i++) {
3606 br->ports[i]->src_mirrors &= ~(MIRROR_MASK_C(1) << m->idx);
3607 br->ports[i]->dst_mirrors &= ~(MIRROR_MASK_C(1) << m->idx);
3610 svec_destroy(&m->src_ports);
3611 svec_destroy(&m->dst_ports);
3614 m->bridge->mirrors[m->idx] = NULL;
3622 prune_ports(struct mirror *m, struct svec *ports)
3627 svec_sort_unique(ports);
3630 for (i = 0; i < ports->n; i++) {
3631 const char *name = ports->names[i];
3632 if (port_lookup(m->bridge, name)) {
3633 svec_add(&tmp, name);
3635 VLOG_WARN("mirror.%s.%s: cannot match on nonexistent port %s",
3636 m->bridge->name, m->name, name);
3639 svec_swap(ports, &tmp);
3644 prune_vlans(struct mirror *m, struct svec *vlan_strings, int **vlans)
3648 /* This isn't perfect: it won't combine "0" and "00", and the textual sort
3649 * order won't give us numeric sort order. But that's good enough for what
3650 * we need right now. */
3651 svec_sort_unique(vlan_strings);
3653 *vlans = xmalloc(sizeof *vlans * vlan_strings->n);
3655 for (i = 0; i < vlan_strings->n; i++) {
3656 const char *name = vlan_strings->names[i];
3658 if (!str_to_int(name, 10, &vlan) || vlan < 0 || vlan > 4095) {
3659 VLOG_WARN("mirror.%s.%s.select.vlan: ignoring invalid VLAN %s",
3660 m->bridge->name, m->name, name);
3662 (*vlans)[n_vlans++] = vlan;
3669 vlan_is_mirrored(const struct mirror *m, int vlan)
3673 for (i = 0; i < m->n_vlans; i++) {
3674 if (m->vlans[i] == vlan) {
3682 port_trunks_any_mirrored_vlan(const struct mirror *m, const struct port *p)
3686 for (i = 0; i < m->n_vlans; i++) {
3687 if (port_trunks_vlan(p, m->vlans[i])) {
3695 mirror_reconfigure_one(struct mirror *m)
3697 char *pfx = xasprintf("mirror.%s.%s", m->bridge->name, m->name);
3698 struct svec src_ports, dst_ports, ports;
3699 struct svec vlan_strings;
3700 mirror_mask_t mirror_bit;
3701 const char *out_port_name;
3702 struct port *out_port;
3707 bool mirror_all_ports;
3708 bool any_ports_specified;
3710 /* Get output port. */
3711 out_port_name = cfg_get_key(0, "mirror.%s.%s.output.port",
3712 m->bridge->name, m->name);
3713 if (out_port_name) {
3714 out_port = port_lookup(m->bridge, out_port_name);
3716 VLOG_ERR("%s.output.port: bridge %s does not have a port "
3717 "named %s", pfx, m->bridge->name, out_port_name);
3724 if (cfg_has("%s.output.vlan", pfx)) {
3725 VLOG_ERR("%s.output.port and %s.output.vlan both specified; "
3726 "ignoring %s.output.vlan", pfx, pfx, pfx);
3728 } else if (cfg_has("%s.output.vlan", pfx)) {
3730 out_vlan = cfg_get_vlan(0, "%s.output.vlan", pfx);
3732 VLOG_ERR("%s: neither %s.output.port nor %s.output.vlan specified, "
3733 "but exactly one is required; disabling port mirror %s",
3734 pfx, pfx, pfx, pfx);
3740 /* Get all the ports, and drop duplicates and ports that don't exist. */
3741 svec_init(&src_ports);
3742 svec_init(&dst_ports);
3744 cfg_get_all_keys(&src_ports, "%s.select.src-port", pfx);
3745 cfg_get_all_keys(&dst_ports, "%s.select.dst-port", pfx);
3746 cfg_get_all_keys(&ports, "%s.select.port", pfx);
3747 any_ports_specified = src_ports.n || dst_ports.n || ports.n;
3748 svec_append(&src_ports, &ports);
3749 svec_append(&dst_ports, &ports);
3750 svec_destroy(&ports);
3751 prune_ports(m, &src_ports);
3752 prune_ports(m, &dst_ports);
3753 if (any_ports_specified && !src_ports.n && !dst_ports.n) {
3754 VLOG_ERR("%s: none of the specified ports exist; "
3755 "disabling port mirror %s", pfx, pfx);
3760 /* Get all the vlans, and drop duplicate and invalid vlans. */
3761 svec_init(&vlan_strings);
3762 cfg_get_all_keys(&vlan_strings, "%s.select.vlan", pfx);
3763 n_vlans = prune_vlans(m, &vlan_strings, &vlans);
3764 svec_destroy(&vlan_strings);
3766 /* Update mirror data. */
3767 if (!svec_equal(&m->src_ports, &src_ports)
3768 || !svec_equal(&m->dst_ports, &dst_ports)
3769 || m->n_vlans != n_vlans
3770 || memcmp(m->vlans, vlans, sizeof *vlans * n_vlans)
3771 || m->out_port != out_port
3772 || m->out_vlan != out_vlan) {
3773 bridge_flush(m->bridge);
3775 svec_swap(&m->src_ports, &src_ports);
3776 svec_swap(&m->dst_ports, &dst_ports);
3779 m->n_vlans = n_vlans;
3780 m->out_port = out_port;
3781 m->out_vlan = out_vlan;
3783 /* If no selection criteria have been given, mirror for all ports. */
3784 mirror_all_ports = (!m->src_ports.n) && (!m->dst_ports.n) && (!m->n_vlans);
3787 mirror_bit = MIRROR_MASK_C(1) << m->idx;
3788 for (i = 0; i < m->bridge->n_ports; i++) {
3789 struct port *port = m->bridge->ports[i];
3791 if (mirror_all_ports
3792 || svec_contains(&m->src_ports, port->name)
3795 ? port_trunks_any_mirrored_vlan(m, port)
3796 : vlan_is_mirrored(m, port->vlan)))) {
3797 port->src_mirrors |= mirror_bit;
3799 port->src_mirrors &= ~mirror_bit;
3802 if (mirror_all_ports || svec_contains(&m->dst_ports, port->name)) {
3803 port->dst_mirrors |= mirror_bit;
3805 port->dst_mirrors &= ~mirror_bit;
3811 svec_destroy(&src_ports);
3812 svec_destroy(&dst_ports);
3816 /* Spanning tree protocol. */
3818 static void brstp_update_port_state(struct port *);
3821 brstp_send_bpdu(struct ofpbuf *pkt, int port_no, void *br_)
3823 struct bridge *br = br_;
3824 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3825 struct iface *iface = iface_from_dp_ifidx(br, port_no);
3827 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d",
3830 struct eth_header *eth = pkt->l2;
3832 netdev_get_etheraddr(iface->netdev, eth->eth_src);
3833 if (eth_addr_is_zero(eth->eth_src)) {
3834 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d "
3835 "with unknown MAC", br->name, port_no);
3837 union ofp_action action;
3840 memset(&action, 0, sizeof action);
3841 action.type = htons(OFPAT_OUTPUT);
3842 action.output.len = htons(sizeof action);
3843 action.output.port = htons(port_no);
3845 flow_extract(pkt, ODPP_NONE, &flow);
3846 ofproto_send_packet(br->ofproto, &flow, &action, 1, pkt);
3853 brstp_reconfigure(struct bridge *br)
3857 if (!cfg_get_bool(0, "stp.%s.enabled", br->name)) {
3859 stp_destroy(br->stp);
3865 uint64_t bridge_address, bridge_id;
3866 int bridge_priority;
3868 bridge_address = cfg_get_mac(0, "stp.%s.address", br->name);
3869 if (!bridge_address) {
3871 bridge_address = (stp_get_bridge_id(br->stp)
3872 & ((UINT64_C(1) << 48) - 1));
3874 uint8_t mac[ETH_ADDR_LEN];
3875 eth_addr_random(mac);
3876 bridge_address = eth_addr_to_uint64(mac);
3880 if (cfg_is_valid(CFG_INT | CFG_REQUIRED, "stp.%s.priority",
3882 bridge_priority = cfg_get_int(0, "stp.%s.priority", br->name);
3884 bridge_priority = STP_DEFAULT_BRIDGE_PRIORITY;
3887 bridge_id = bridge_address | ((uint64_t) bridge_priority << 48);
3889 br->stp = stp_create(br->name, bridge_id, brstp_send_bpdu, br);
3890 br->stp_last_tick = time_msec();
3893 if (bridge_id != stp_get_bridge_id(br->stp)) {
3894 stp_set_bridge_id(br->stp, bridge_id);
3899 for (i = 0; i < br->n_ports; i++) {
3900 struct port *p = br->ports[i];
3902 struct stp_port *sp;
3903 int path_cost, priority;
3909 dp_ifidx = p->ifaces[0]->dp_ifidx;
3910 if (dp_ifidx < 0 || dp_ifidx >= STP_MAX_PORTS) {
3914 sp = stp_get_port(br->stp, dp_ifidx);
3915 enable = (!cfg_is_valid(CFG_BOOL | CFG_REQUIRED,
3916 "stp.%s.port.%s.enabled",
3918 || cfg_get_bool(0, "stp.%s.port.%s.enabled",
3919 br->name, p->name));
3920 if (p->is_mirror_output_port) {
3923 if (enable != (stp_port_get_state(sp) != STP_DISABLED)) {
3924 bridge_flush(br); /* Might not be necessary. */
3926 stp_port_enable(sp);
3928 stp_port_disable(sp);
3932 path_cost = cfg_get_int(0, "stp.%s.port.%s.path-cost",
3934 stp_port_set_path_cost(sp, path_cost ? path_cost : 19 /* XXX */);
3936 priority = (cfg_is_valid(CFG_INT | CFG_REQUIRED,
3937 "stp.%s.port.%s.priority",
3939 ? cfg_get_int(0, "stp.%s.port.%s.priority",
3941 : STP_DEFAULT_PORT_PRIORITY);
3942 stp_port_set_priority(sp, priority);
3945 brstp_adjust_timers(br);
3947 for (i = 0; i < br->n_ports; i++) {
3948 brstp_update_port_state(br->ports[i]);
3953 brstp_update_port_state(struct port *p)
3955 struct bridge *br = p->bridge;
3956 enum stp_state state;
3958 /* Figure out new state. */
3959 state = STP_DISABLED;
3960 if (br->stp && p->n_ifaces > 0) {
3961 int dp_ifidx = p->ifaces[0]->dp_ifidx;
3962 if (dp_ifidx >= 0 && dp_ifidx < STP_MAX_PORTS) {
3963 state = stp_port_get_state(stp_get_port(br->stp, dp_ifidx));
3968 if (p->stp_state != state) {
3969 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3970 VLOG_INFO_RL(&rl, "port %s: STP state changed from %s to %s",
3971 p->name, stp_state_name(p->stp_state),
3972 stp_state_name(state));
3973 if (p->stp_state == STP_DISABLED) {
3976 ofproto_revalidate(p->bridge->ofproto, p->stp_state_tag);
3978 p->stp_state = state;
3979 p->stp_state_tag = (p->stp_state == STP_DISABLED ? 0
3980 : tag_create_random());
3985 brstp_adjust_timers(struct bridge *br)
3987 int hello_time = cfg_get_int(0, "stp.%s.hello-time", br->name);
3988 int max_age = cfg_get_int(0, "stp.%s.max-age", br->name);
3989 int forward_delay = cfg_get_int(0, "stp.%s.forward-delay", br->name);
3991 stp_set_hello_time(br->stp, hello_time ? hello_time : 2000);
3992 stp_set_max_age(br->stp, max_age ? max_age : 20000);
3993 stp_set_forward_delay(br->stp, forward_delay ? forward_delay : 15000);
3997 brstp_run(struct bridge *br)
4000 long long int now = time_msec();
4001 long long int elapsed = now - br->stp_last_tick;
4002 struct stp_port *sp;
4005 stp_tick(br->stp, MIN(INT_MAX, elapsed));
4006 br->stp_last_tick = now;
4008 while (stp_get_changed_port(br->stp, &sp)) {
4009 struct port *p = port_from_dp_ifidx(br, stp_port_no(sp));
4011 brstp_update_port_state(p);
4018 brstp_wait(struct bridge *br)
4021 poll_timer_wait(1000);